|
""" |
|
pygments.lexers.mips |
|
~~~~~~~~~~~~~~~~~~~~ |
|
|
|
Lexers for MIPS assembly. |
|
|
|
:copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS. |
|
:license: BSD, see LICENSE for details. |
|
""" |
|
|
|
from pygments.lexer import RegexLexer, words |
|
from pygments.token import Whitespace, Comment, String, Keyword, Name, Text |
|
|
|
__all__ = ["MIPSLexer"] |
|
|
|
|
|
class MIPSLexer(RegexLexer): |
|
""" |
|
A MIPS Assembly Lexer. |
|
|
|
Based on the Emacs major mode by hlissner: |
|
https://github.com/hlissner/emacs-mips-mode |
|
""" |
|
|
|
name = 'MIPS' |
|
aliases = ['mips'] |
|
version_added = '' |
|
|
|
|
|
|
|
filenames = ['*.mips', '*.MIPS'] |
|
url = 'https://mips.com' |
|
|
|
keywords = [ |
|
|
|
"add", "sub", "subu", "addi", "subi", "addu", "addiu", |
|
|
|
"mul", "mult", "multu", "mulu", "madd", "maddu", "msub", "msubu", "div", "divu", |
|
|
|
"and", "or", "nor", "xor", "andi", "ori", "xori", "clo", "clz", |
|
|
|
"sll", "srl", "sllv", "srlv", "sra", "srav", |
|
|
|
"slt", "sltu", "slti", "sltiu", |
|
|
|
"mfhi", "mthi", "mflo", "mtlo", "movn", "movz", "movf", "movt", |
|
|
|
"j", "jal", "jalr", "jr", |
|
|
|
"bc1f", "bc1t", "beq", "bgez", "bgezal", "bgtz", "blez", "bltzal", "bltz", "bne", |
|
|
|
"lui", "lb", "lbu", "lh", "lhu", "lw", "lwcl", "lwl", "lwr", |
|
|
|
"sb", "sh", "sw", "swl", "swr", |
|
|
|
"ll", "sc", |
|
|
|
"teq", "teqi", "tne", "tneqi", "tge", "tgeu", "tgei", "tgeiu", "tlt", "tltu", "tlti", |
|
"tltiu", |
|
|
|
"eret", "break", "bop", "syscall", |
|
|
|
|
|
"add.s", "add.d", "sub.s", "sub.d", "mul.s", "mul.d", "div.s", "div.d", "neg.d", |
|
"neg.s", |
|
|
|
"c.e.d", "c.e.s", "c.le.d", "c.le.s", "c.lt.s", "c.lt.d", |
|
"madd.s", "madd.d", "msub.s", "msub.d", |
|
|
|
"mov.d", "move.s", "movf.d", "movf.s", "movt.d", "movt.s", "movn.d", "movn.s", |
|
"movnzd", "movz.s", "movz.d", |
|
|
|
"cvt.d.s", "cvt.d.w", "cvt.s.d", "cvt.s.w", "cvt.w.d", "cvt.w.s", "trunc.w.d", |
|
"trunc.w.s", |
|
|
|
"abs.s", "abs.d", "sqrt.s", "sqrt.d", "ceil.w.d", "ceil.w.s", "floor.w.d", |
|
"floor.w.s", "round.w.d", "round.w.s", |
|
] |
|
|
|
pseudoinstructions = [ |
|
|
|
"rem", "remu", "mulo", "mulou", "abs", "neg", "negu", "not", "rol", "ror", |
|
|
|
"b", "beqz", "bge", "bgeu", "bgt", "bgtu", "ble", "bleu", "blt", "bltu", "bnez", |
|
|
|
"la", "li", "ld", "ulh", "ulhu", "ulw", |
|
|
|
"sd", "ush", "usw", |
|
|
|
"move", |
|
|
|
"sgt", "sgtu", "sge", "sgeu", "sle", "sleu", "sne", "seq", |
|
|
|
|
|
"l.d", "l.s", "s.d", "s.s", |
|
] |
|
|
|
directives = [ |
|
".align", ".ascii", ".asciiz", ".byte", ".data", ".double", ".extern", ".float", |
|
".globl", ".half", ".kdata", ".ktext", ".space", ".text", ".word", |
|
] |
|
|
|
deprecated = [ |
|
"beql", "bnel", "bgtzl", "bgezl", "bltzl", "blezl", "bltzall", "bgezall", |
|
] |
|
|
|
tokens = { |
|
'root': [ |
|
(r'\s+', Whitespace), |
|
(r'#.*', Comment), |
|
(r'"', String, 'string'), |
|
(r'-?[0-9]+?', Keyword.Constant), |
|
(r'\w*:', Name.Function), |
|
(words(deprecated, suffix=r'\b'), Keyword.Pseudo), |
|
(words(pseudoinstructions, suffix=r'\b'), Name.Variable), |
|
(words(keywords, suffix=r'\b'), Keyword), |
|
(r'[slm][ftwd]c[0-9]([.]d)?', Keyword), |
|
(r'\$(f?[0-2][0-9]|f?3[01]|[ft]?[0-9]|[vk][01]|a[0-3]|s[0-7]|[gsf]p|ra|at|zero)', |
|
Keyword.Type), |
|
(words(directives, suffix=r'\b'), Name.Entity), |
|
(r':|,|;|\{|\}|=>|@|\$|=', Name.Builtin), |
|
(r'\w+', Text), |
|
(r'.', Text), |
|
], |
|
'string': [ |
|
(r'\\.', String.Escape), |
|
(r'"', String, '#pop'), |
|
(r'[^\\"]+', String), |
|
], |
|
} |
|
|