mips.py 4.5 KB

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