mips.py 4.5 KB

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