1"""
2 pygments.lexers.mips
3 ~~~~~~~~~~~~~~~~~~~~
4
5 Lexers for MIPS assembly.
6
7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11from pygments.lexer import RegexLexer, words
12from pygments.token import Whitespace, Comment, String, Keyword, Name, Text
13
14__all__ = ["MIPSLexer"]
15
16
17class MIPSLexer(RegexLexer):
18 """
19 A MIPS Assembly Lexer.
20
21 Based on the Emacs major mode by hlissner:
22 https://github.com/hlissner/emacs-mips-mode
23 """
24
25 name = 'MIPS'
26 aliases = ['mips']
27 version_added = ''
28 # TODO: add '*.s' and '*.asm', which will require designing an analyse_text
29 # method for this lexer and refactoring those from Gas and Nasm in order to
30 # have relatively reliable detection
31 filenames = ['*.mips', '*.MIPS']
32 url = 'https://mips.com'
33
34 keywords = [
35 # Arithmetic insturctions
36 "add", "sub", "subu", "addi", "subi", "addu", "addiu",
37 # Multiplication/division
38 "mul", "mult", "multu", "mulu", "madd", "maddu", "msub", "msubu", "div", "divu",
39 # Bitwise operations
40 "and", "or", "nor", "xor", "andi", "ori", "xori", "clo", "clz",
41 # Shifts
42 "sll", "srl", "sllv", "srlv", "sra", "srav",
43 # Comparisons
44 "slt", "sltu", "slti", "sltiu",
45 # Move data
46 "mfhi", "mthi", "mflo", "mtlo", "movn", "movz", "movf", "movt",
47 # Jump
48 "j", "jal", "jalr", "jr",
49 # branch
50 "bc1f", "bc1t", "beq", "bgez", "bgezal", "bgtz", "blez", "bltzal", "bltz", "bne",
51 # Load
52 "lui", "lb", "lbu", "lh", "lhu", "lw", "lwcl", "lwl", "lwr",
53 # Store
54 "sb", "sh", "sw", "swl", "swr", # coproc: swc1 sdc1
55 # Concurrent load/store
56 "ll", "sc",
57 # Trap handling
58 "teq", "teqi", "tne", "tneqi", "tge", "tgeu", "tgei", "tgeiu", "tlt", "tltu", "tlti",
59 "tltiu",
60 # Exception / Interrupt
61 "eret", "break", "bop", "syscall",
62 # --- Floats -----------------------------------------------------
63 # Arithmetic
64 "add.s", "add.d", "sub.s", "sub.d", "mul.s", "mul.d", "div.s", "div.d", "neg.d",
65 "neg.s",
66 # Comparison
67 "c.e.d", "c.e.s", "c.le.d", "c.le.s", "c.lt.s", "c.lt.d", # "c.gt.s", "c.gt.d",
68 "madd.s", "madd.d", "msub.s", "msub.d",
69 # Move Floats
70 "mov.d", "move.s", "movf.d", "movf.s", "movt.d", "movt.s", "movn.d", "movn.s",
71 "movnzd", "movz.s", "movz.d",
72 # Conversion
73 "cvt.d.s", "cvt.d.w", "cvt.s.d", "cvt.s.w", "cvt.w.d", "cvt.w.s", "trunc.w.d",
74 "trunc.w.s",
75 # Math
76 "abs.s", "abs.d", "sqrt.s", "sqrt.d", "ceil.w.d", "ceil.w.s", "floor.w.d",
77 "floor.w.s", "round.w.d", "round.w.s",
78 ]
79
80 pseudoinstructions = [
81 # Arithmetic & logical
82 "rem", "remu", "mulo", "mulou", "abs", "neg", "negu", "not", "rol", "ror",
83 # branches
84 "b", "beqz", "bge", "bgeu", "bgt", "bgtu", "ble", "bleu", "blt", "bltu", "bnez",
85 # loads
86 "la", "li", "ld", "ulh", "ulhu", "ulw",
87 # Store
88 "sd", "ush", "usw",
89 # move
90 "move", # coproc: "mfc1.d",
91 # comparisons
92 "sgt", "sgtu", "sge", "sgeu", "sle", "sleu", "sne", "seq",
93 # --- Floats -----------------------------------------------------
94 # load-store
95 "l.d", "l.s", "s.d", "s.s",
96 ]
97
98 directives = [
99 ".align", ".ascii", ".asciiz", ".byte", ".data", ".double", ".extern", ".float",
100 ".globl", ".half", ".kdata", ".ktext", ".space", ".text", ".word",
101 ]
102
103 deprecated = [
104 "beql", "bnel", "bgtzl", "bgezl", "bltzl", "blezl", "bltzall", "bgezall",
105 ]
106
107 tokens = {
108 'root': [
109 (r'\s+', Whitespace),
110 (r'#.*', Comment),
111 (r'"', String, 'string'),
112 (r'-?[0-9]+?', Keyword.Constant),
113 (r'\w*:', Name.Function),
114 (words(deprecated, suffix=r'\b'), Keyword.Pseudo), # need warning face
115 (words(pseudoinstructions, suffix=r'\b'), Name.Variable),
116 (words(keywords, suffix=r'\b'), Keyword),
117 (r'[slm][ftwd]c[0-9]([.]d)?', Keyword),
118 (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)',
119 Keyword.Type),
120 (words(directives, suffix=r'\b'), Name.Entity), # Preprocessor?
121 (r':|,|;|\{|\}|=>|@|\$|=', Name.Builtin),
122 (r'\w+', Text),
123 (r'.', Text),
124 ],
125 'string': [
126 (r'\\.', String.Escape),
127 (r'"', String, '#pop'),
128 (r'[^\\"]+', String),
129 ],
130 }