asm.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.asm
  4. ~~~~~~~~~~~~~~~~~~~
  5. Lexers for assembly languages.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import RegexLexer, include, bygroups, using, words, \
  11. DelegatingLexer
  12. from pygments.lexers.c_cpp import CppLexer, CLexer
  13. from pygments.lexers.d import DLexer
  14. from pygments.token import Text, Name, Number, String, Comment, Punctuation, \
  15. Other, Keyword, Operator
  16. __all__ = ['GasLexer', 'ObjdumpLexer', 'DObjdumpLexer', 'CppObjdumpLexer',
  17. 'CObjdumpLexer', 'HsailLexer', 'LlvmLexer', 'NasmLexer',
  18. 'NasmObjdumpLexer', 'TasmLexer', 'Ca65Lexer', 'Dasm16Lexer']
  19. class GasLexer(RegexLexer):
  20. """
  21. For Gas (AT&T) assembly code.
  22. """
  23. name = 'GAS'
  24. aliases = ['gas', 'asm']
  25. filenames = ['*.s', '*.S']
  26. mimetypes = ['text/x-gas']
  27. #: optional Comment or Whitespace
  28. string = r'"(\\"|[^"])*"'
  29. char = r'[\w$.@-]'
  30. identifier = r'(?:[a-zA-Z$_]' + char + r'*|\.' + char + '+)'
  31. number = r'(?:0[xX][a-zA-Z0-9]+|\d+)'
  32. register = '%' + identifier
  33. tokens = {
  34. 'root': [
  35. include('whitespace'),
  36. (identifier + ':', Name.Label),
  37. (r'\.' + identifier, Name.Attribute, 'directive-args'),
  38. (r'lock|rep(n?z)?|data\d+', Name.Attribute),
  39. (identifier, Name.Function, 'instruction-args'),
  40. (r'[\r\n]+', Text)
  41. ],
  42. 'directive-args': [
  43. (identifier, Name.Constant),
  44. (string, String),
  45. ('@' + identifier, Name.Attribute),
  46. (number, Number.Integer),
  47. (register, Name.Variable),
  48. (r'[\r\n]+', Text, '#pop'),
  49. (r'[;#].*?\n', Comment, '#pop'),
  50. include('punctuation'),
  51. include('whitespace')
  52. ],
  53. 'instruction-args': [
  54. # For objdump-disassembled code, shouldn't occur in
  55. # actual assembler input
  56. ('([a-z0-9]+)( )(<)('+identifier+')(>)',
  57. bygroups(Number.Hex, Text, Punctuation, Name.Constant,
  58. Punctuation)),
  59. ('([a-z0-9]+)( )(<)('+identifier+')([-+])('+number+')(>)',
  60. bygroups(Number.Hex, Text, Punctuation, Name.Constant,
  61. Punctuation, Number.Integer, Punctuation)),
  62. # Address constants
  63. (identifier, Name.Constant),
  64. (number, Number.Integer),
  65. # Registers
  66. (register, Name.Variable),
  67. # Numeric constants
  68. ('$'+number, Number.Integer),
  69. (r"$'(.|\\')'", String.Char),
  70. (r'[\r\n]+', Text, '#pop'),
  71. (r'[;#].*?\n', Comment, '#pop'),
  72. include('punctuation'),
  73. include('whitespace')
  74. ],
  75. 'whitespace': [
  76. (r'\n', Text),
  77. (r'\s+', Text),
  78. (r'[;#].*?\n', Comment)
  79. ],
  80. 'punctuation': [
  81. (r'[-*,.()\[\]!:]+', Punctuation)
  82. ]
  83. }
  84. def analyse_text(text):
  85. if re.match(r'^\.(text|data|section)', text, re.M):
  86. return True
  87. elif re.match(r'^\.\w+', text, re.M):
  88. return 0.1
  89. def _objdump_lexer_tokens(asm_lexer):
  90. """
  91. Common objdump lexer tokens to wrap an ASM lexer.
  92. """
  93. hex_re = r'[0-9A-Za-z]'
  94. return {
  95. 'root': [
  96. # File name & format:
  97. ('(.*?)(:)( +file format )(.*?)$',
  98. bygroups(Name.Label, Punctuation, Text, String)),
  99. # Section header
  100. ('(Disassembly of section )(.*?)(:)$',
  101. bygroups(Text, Name.Label, Punctuation)),
  102. # Function labels
  103. # (With offset)
  104. ('('+hex_re+'+)( )(<)(.*?)([-+])(0[xX][A-Za-z0-9]+)(>:)$',
  105. bygroups(Number.Hex, Text, Punctuation, Name.Function,
  106. Punctuation, Number.Hex, Punctuation)),
  107. # (Without offset)
  108. ('('+hex_re+'+)( )(<)(.*?)(>:)$',
  109. bygroups(Number.Hex, Text, Punctuation, Name.Function,
  110. Punctuation)),
  111. # Code line with disassembled instructions
  112. ('( *)('+hex_re+r'+:)(\t)((?:'+hex_re+hex_re+' )+)( *\t)([a-zA-Z].*?)$',
  113. bygroups(Text, Name.Label, Text, Number.Hex, Text,
  114. using(asm_lexer))),
  115. # Code line with ascii
  116. ('( *)('+hex_re+r'+:)(\t)((?:'+hex_re+hex_re+' )+)( *)(.*?)$',
  117. bygroups(Text, Name.Label, Text, Number.Hex, Text, String)),
  118. # Continued code line, only raw opcodes without disassembled
  119. # instruction
  120. ('( *)('+hex_re+r'+:)(\t)((?:'+hex_re+hex_re+' )+)$',
  121. bygroups(Text, Name.Label, Text, Number.Hex)),
  122. # Skipped a few bytes
  123. (r'\t\.\.\.$', Text),
  124. # Relocation line
  125. # (With offset)
  126. (r'(\t\t\t)('+hex_re+r'+:)( )([^\t]+)(\t)(.*?)([-+])(0x'+hex_re+'+)$',
  127. bygroups(Text, Name.Label, Text, Name.Property, Text,
  128. Name.Constant, Punctuation, Number.Hex)),
  129. # (Without offset)
  130. (r'(\t\t\t)('+hex_re+r'+:)( )([^\t]+)(\t)(.*?)$',
  131. bygroups(Text, Name.Label, Text, Name.Property, Text,
  132. Name.Constant)),
  133. (r'[^\n]+\n', Other)
  134. ]
  135. }
  136. class ObjdumpLexer(RegexLexer):
  137. """
  138. For the output of 'objdump -dr'
  139. """
  140. name = 'objdump'
  141. aliases = ['objdump']
  142. filenames = ['*.objdump']
  143. mimetypes = ['text/x-objdump']
  144. tokens = _objdump_lexer_tokens(GasLexer)
  145. class DObjdumpLexer(DelegatingLexer):
  146. """
  147. For the output of 'objdump -Sr on compiled D files'
  148. """
  149. name = 'd-objdump'
  150. aliases = ['d-objdump']
  151. filenames = ['*.d-objdump']
  152. mimetypes = ['text/x-d-objdump']
  153. def __init__(self, **options):
  154. super(DObjdumpLexer, self).__init__(DLexer, ObjdumpLexer, **options)
  155. class CppObjdumpLexer(DelegatingLexer):
  156. """
  157. For the output of 'objdump -Sr on compiled C++ files'
  158. """
  159. name = 'cpp-objdump'
  160. aliases = ['cpp-objdump', 'c++-objdumb', 'cxx-objdump']
  161. filenames = ['*.cpp-objdump', '*.c++-objdump', '*.cxx-objdump']
  162. mimetypes = ['text/x-cpp-objdump']
  163. def __init__(self, **options):
  164. super(CppObjdumpLexer, self).__init__(CppLexer, ObjdumpLexer, **options)
  165. class CObjdumpLexer(DelegatingLexer):
  166. """
  167. For the output of 'objdump -Sr on compiled C files'
  168. """
  169. name = 'c-objdump'
  170. aliases = ['c-objdump']
  171. filenames = ['*.c-objdump']
  172. mimetypes = ['text/x-c-objdump']
  173. def __init__(self, **options):
  174. super(CObjdumpLexer, self).__init__(CLexer, ObjdumpLexer, **options)
  175. class HsailLexer(RegexLexer):
  176. """
  177. For HSAIL assembly code.
  178. .. versionadded:: 2.2
  179. """
  180. name = 'HSAIL'
  181. aliases = ['hsail', 'hsa']
  182. filenames = ['*.hsail']
  183. mimetypes = ['text/x-hsail']
  184. string = r'"[^"]*?"'
  185. identifier = r'[a-zA-Z_][\w.]*'
  186. # Registers
  187. register_number = r'[0-9]+'
  188. register = r'(\$(c|s|d|q)' + register_number + ')'
  189. # Qualifiers
  190. alignQual = r'(align\(\d+\))'
  191. widthQual = r'(width\((\d+|all)\))'
  192. allocQual = r'(alloc\(agent\))'
  193. # Instruction Modifiers
  194. roundingMod = (r'((_ftz)?(_up|_down|_zero|_near))')
  195. datatypeMod = (r'_('
  196. # packedTypes
  197. r'u8x4|s8x4|u16x2|s16x2|u8x8|s8x8|u16x4|s16x4|u32x2|s32x2|'
  198. r'u8x16|s8x16|u16x8|s16x8|u32x4|s32x4|u64x2|s64x2|'
  199. r'f16x2|f16x4|f16x8|f32x2|f32x4|f64x2|'
  200. # baseTypes
  201. r'u8|s8|u16|s16|u32|s32|u64|s64|'
  202. r'b128|b8|b16|b32|b64|b1|'
  203. r'f16|f32|f64|'
  204. # opaqueType
  205. r'roimg|woimg|rwimg|samp|sig32|sig64)')
  206. # Numeric Constant
  207. float = r'((\d+\.)|(\d*\.\d+))[eE][+-]?\d+'
  208. hexfloat = r'0[xX](([0-9a-fA-F]+\.[0-9a-fA-F]*)|([0-9a-fA-F]*\.[0-9a-fA-F]+))[pP][+-]?\d+'
  209. ieeefloat = r'0((h|H)[0-9a-fA-F]{4}|(f|F)[0-9a-fA-F]{8}|(d|D)[0-9a-fA-F]{16})'
  210. tokens = {
  211. 'root': [
  212. include('whitespace'),
  213. include('comments'),
  214. (string, String),
  215. (r'@' + identifier + ':?', Name.Label),
  216. (register, Name.Variable.Anonymous),
  217. include('keyword'),
  218. (r'&' + identifier, Name.Variable.Global),
  219. (r'%' + identifier, Name.Variable),
  220. (hexfloat, Number.Hex),
  221. (r'0[xX][a-fA-F0-9]+', Number.Hex),
  222. (ieeefloat, Number.Float),
  223. (float, Number.Float),
  224. (r'\d+', Number.Integer),
  225. (r'[=<>{}\[\]()*.,:;!]|x\b', Punctuation)
  226. ],
  227. 'whitespace': [
  228. (r'(\n|\s)+', Text),
  229. ],
  230. 'comments': [
  231. (r'/\*.*?\*/', Comment.Multiline),
  232. (r'//.*?\n', Comment.Single),
  233. ],
  234. 'keyword': [
  235. # Types
  236. (r'kernarg' + datatypeMod, Keyword.Type),
  237. # Regular keywords
  238. (r'\$(full|base|small|large|default|zero|near)', Keyword),
  239. (words((
  240. 'module', 'extension', 'pragma', 'prog', 'indirect', 'signature',
  241. 'decl', 'kernel', 'function', 'enablebreakexceptions',
  242. 'enabledetectexceptions', 'maxdynamicgroupsize', 'maxflatgridsize',
  243. 'maxflatworkgroupsize', 'requireddim', 'requiredgridsize',
  244. 'requiredworkgroupsize', 'requirenopartialworkgroups'),
  245. suffix=r'\b'), Keyword),
  246. # instructions
  247. (roundingMod, Keyword),
  248. (datatypeMod, Keyword),
  249. (r'_(' + alignQual + '|' + widthQual + ')', Keyword),
  250. (r'_kernarg', Keyword),
  251. (r'(nop|imagefence)\b', Keyword),
  252. (words((
  253. 'cleardetectexcept', 'clock', 'cuid', 'debugtrap', 'dim',
  254. 'getdetectexcept', 'groupbaseptr', 'kernargbaseptr', 'laneid',
  255. 'maxcuid', 'maxwaveid', 'packetid', 'setdetectexcept', 'waveid',
  256. 'workitemflatabsid', 'workitemflatid', 'nullptr', 'abs', 'bitrev',
  257. 'currentworkgroupsize', 'currentworkitemflatid', 'fract', 'ncos',
  258. 'neg', 'nexp2', 'nlog2', 'nrcp', 'nrsqrt', 'nsin', 'nsqrt',
  259. 'gridgroups', 'gridsize', 'not', 'sqrt', 'workgroupid',
  260. 'workgroupsize', 'workitemabsid', 'workitemid', 'ceil', 'floor',
  261. 'rint', 'trunc', 'add', 'bitmask', 'borrow', 'carry', 'copysign',
  262. 'div', 'rem', 'sub', 'shl', 'shr', 'and', 'or', 'xor', 'unpackhi',
  263. 'unpacklo', 'max', 'min', 'fma', 'mad', 'bitextract', 'bitselect',
  264. 'shuffle', 'cmov', 'bitalign', 'bytealign', 'lerp', 'nfma', 'mul',
  265. 'mulhi', 'mul24hi', 'mul24', 'mad24', 'mad24hi', 'bitinsert',
  266. 'combine', 'expand', 'lda', 'mov', 'pack', 'unpack', 'packcvt',
  267. 'unpackcvt', 'sad', 'sementp', 'ftos', 'stof', 'cmp', 'ld', 'st',
  268. '_eq', '_ne', '_lt', '_le', '_gt', '_ge', '_equ', '_neu', '_ltu',
  269. '_leu', '_gtu', '_geu', '_num', '_nan', '_seq', '_sne', '_slt',
  270. '_sle', '_sgt', '_sge', '_snum', '_snan', '_sequ', '_sneu', '_sltu',
  271. '_sleu', '_sgtu', '_sgeu', 'atomic', '_ld', '_st', '_cas', '_add',
  272. '_and', '_exch', '_max', '_min', '_or', '_sub', '_wrapdec',
  273. '_wrapinc', '_xor', 'ret', 'cvt', '_readonly', '_kernarg', '_global',
  274. 'br', 'cbr', 'sbr', '_scacq', '_screl', '_scar', '_rlx', '_wave',
  275. '_wg', '_agent', '_system', 'ldimage', 'stimage', '_v2', '_v3', '_v4',
  276. '_1d', '_2d', '_3d', '_1da', '_2da', '_1db', '_2ddepth', '_2dadepth',
  277. '_width', '_height', '_depth', '_array', '_channelorder',
  278. '_channeltype', 'querysampler', '_coord', '_filter', '_addressing',
  279. 'barrier', 'wavebarrier', 'initfbar', 'joinfbar', 'waitfbar',
  280. 'arrivefbar', 'leavefbar', 'releasefbar', 'ldf', 'activelaneid',
  281. 'activelanecount', 'activelanemask', 'activelanepermute', 'call',
  282. 'scall', 'icall', 'alloca', 'packetcompletionsig',
  283. 'addqueuewriteindex', 'casqueuewriteindex', 'ldqueuereadindex',
  284. 'stqueuereadindex', 'readonly', 'global', 'private', 'group',
  285. 'spill', 'arg', '_upi', '_downi', '_zeroi', '_neari', '_upi_sat',
  286. '_downi_sat', '_zeroi_sat', '_neari_sat', '_supi', '_sdowni',
  287. '_szeroi', '_sneari', '_supi_sat', '_sdowni_sat', '_szeroi_sat',
  288. '_sneari_sat', '_pp', '_ps', '_sp', '_ss', '_s', '_p', '_pp_sat',
  289. '_ps_sat', '_sp_sat', '_ss_sat', '_s_sat', '_p_sat')), Keyword),
  290. # Integer types
  291. (r'i[1-9]\d*', Keyword)
  292. ]
  293. }
  294. class LlvmLexer(RegexLexer):
  295. """
  296. For LLVM assembly code.
  297. """
  298. name = 'LLVM'
  299. aliases = ['llvm']
  300. filenames = ['*.ll']
  301. mimetypes = ['text/x-llvm']
  302. #: optional Comment or Whitespace
  303. string = r'"[^"]*?"'
  304. identifier = r'([-a-zA-Z$._][\w\-$.]*|' + string + ')'
  305. tokens = {
  306. 'root': [
  307. include('whitespace'),
  308. # Before keywords, because keywords are valid label names :(...
  309. (identifier + r'\s*:', Name.Label),
  310. include('keyword'),
  311. (r'%' + identifier, Name.Variable),
  312. (r'@' + identifier, Name.Variable.Global),
  313. (r'%\d+', Name.Variable.Anonymous),
  314. (r'@\d+', Name.Variable.Global),
  315. (r'#\d+', Name.Variable.Global),
  316. (r'!' + identifier, Name.Variable),
  317. (r'!\d+', Name.Variable.Anonymous),
  318. (r'c?' + string, String),
  319. (r'0[xX][a-fA-F0-9]+', Number),
  320. (r'-?\d+(?:[.]\d+)?(?:[eE][-+]?\d+(?:[.]\d+)?)?', Number),
  321. (r'[=<>{}\[\]()*.,!]|x\b', Punctuation)
  322. ],
  323. 'whitespace': [
  324. (r'(\n|\s)+', Text),
  325. (r';.*?\n', Comment)
  326. ],
  327. 'keyword': [
  328. # Regular keywords
  329. (words((
  330. 'acq_rel', 'acquire', 'add', 'addrspace', 'addrspacecast', 'afn', 'alias',
  331. 'aliasee', 'align', 'alignLog2', 'alignstack', 'alloca', 'allocsize', 'allOnes',
  332. 'alwaysinline', 'amdgpu_cs', 'amdgpu_es', 'amdgpu_gs', 'amdgpu_hs',
  333. 'amdgpu_kernel', 'amdgpu_ls', 'amdgpu_ps', 'amdgpu_vs', 'and', 'any',
  334. 'anyregcc', 'appending', 'arcp', 'argmemonly', 'args', 'arm_aapcs_vfpcc',
  335. 'arm_aapcscc', 'arm_apcscc', 'ashr', 'asm', 'atomic', 'atomicrmw', 'attributes',
  336. 'available_externally', 'avr_intrcc', 'avr_signalcc', 'bit', 'bitcast',
  337. 'bitMask', 'blockaddress', 'br', 'branchFunnel', 'builtin', 'byArg', 'byte',
  338. 'byteArray', 'byval', 'c', 'call', 'callee', 'caller', 'calls', 'catch',
  339. 'catchpad', 'catchret', 'catchswitch', 'cc', 'ccc', 'cleanup', 'cleanuppad',
  340. 'cleanupret', 'cmpxchg', 'cold', 'coldcc', 'comdat', 'common', 'constant',
  341. 'contract', 'convergent', 'critical', 'cxx_fast_tlscc', 'datalayout', 'declare',
  342. 'default', 'define', 'deplibs', 'dereferenceable', 'dereferenceable_or_null',
  343. 'distinct', 'dllexport', 'dllimport', 'double', 'dso_local', 'dso_preemptable',
  344. 'dsoLocal', 'eq', 'exact', 'exactmatch', 'extern_weak', 'external',
  345. 'externally_initialized', 'extractelement', 'extractvalue', 'fadd', 'false',
  346. 'fast', 'fastcc', 'fcmp', 'fdiv', 'fence', 'filter', 'flags', 'float', 'fmul',
  347. 'fp128', 'fpext', 'fptosi', 'fptoui', 'fptrunc', 'frem', 'from', 'fsub',
  348. 'funcFlags', 'function', 'gc', 'getelementptr', 'ghccc', 'global', 'guid', 'gv',
  349. 'half', 'hash', 'hhvm_ccc', 'hhvmcc', 'hidden', 'hot', 'hotness', 'icmp',
  350. 'ifunc', 'inaccessiblemem_or_argmemonly', 'inaccessiblememonly', 'inalloca',
  351. 'inbounds', 'indir', 'indirectbr', 'info', 'initialexec', 'inline',
  352. 'inlineBits', 'inlinehint', 'inrange', 'inreg', 'insertelement', 'insertvalue',
  353. 'insts', 'intel_ocl_bicc', 'inteldialect', 'internal', 'inttoptr', 'invoke',
  354. 'jumptable', 'kind', 'label', 'landingpad', 'largest', 'linkage', 'linkonce',
  355. 'linkonce_odr', 'live', 'load', 'local_unnamed_addr', 'localdynamic',
  356. 'localexec', 'lshr', 'max', 'metadata', 'min', 'minsize', 'module', 'monotonic',
  357. 'msp430_intrcc', 'mul', 'musttail', 'naked', 'name', 'nand', 'ne', 'nest',
  358. 'ninf', 'nnan', 'noalias', 'nobuiltin', 'nocapture', 'nocf_check',
  359. 'noduplicate', 'noduplicates', 'noimplicitfloat', 'noinline', 'none',
  360. 'nonlazybind', 'nonnull', 'norecurse', 'noRecurse', 'noredzone', 'noreturn',
  361. 'notail', 'notEligibleToImport', 'nounwind', 'nsw', 'nsz', 'null', 'nuw', 'oeq',
  362. 'offset', 'oge', 'ogt', 'ole', 'olt', 'one', 'opaque', 'optforfuzzing',
  363. 'optnone', 'optsize', 'or', 'ord', 'path', 'personality', 'phi', 'ppc_fp128',
  364. 'prefix', 'preserve_allcc', 'preserve_mostcc', 'private', 'prologue',
  365. 'protected', 'ptrtoint', 'ptx_device', 'ptx_kernel', 'readnone', 'readNone',
  366. 'readonly', 'readOnly', 'reassoc', 'refs', 'relbf', 'release', 'resByArg',
  367. 'resume', 'ret', 'returnDoesNotAlias', 'returned', 'returns_twice', 'safestack',
  368. 'samesize', 'sanitize_address', 'sanitize_hwaddress', 'sanitize_memory',
  369. 'sanitize_thread', 'sdiv', 'section', 'select', 'seq_cst', 'sext', 'sge', 'sgt',
  370. 'shadowcallstack', 'shl', 'shufflevector', 'sideeffect', 'signext', 'single',
  371. 'singleImpl', 'singleImplName', 'sitofp', 'sizeM1', 'sizeM1BitWidth', 'sle',
  372. 'slt', 'source_filename', 'speculatable', 'spir_func', 'spir_kernel', 'srem',
  373. 'sret', 'ssp', 'sspreq', 'sspstrong', 'store', 'strictfp', 'sub', 'summaries',
  374. 'summary', 'swiftcc', 'swifterror', 'swiftself', 'switch', 'syncscope', 'tail',
  375. 'target', 'thread_local', 'to', 'token', 'triple', 'true', 'trunc', 'type',
  376. 'typeCheckedLoadConstVCalls', 'typeCheckedLoadVCalls', 'typeid', 'typeIdInfo',
  377. 'typeTestAssumeConstVCalls', 'typeTestAssumeVCalls', 'typeTestRes', 'typeTests',
  378. 'udiv', 'ueq', 'uge', 'ugt', 'uitofp', 'ule', 'ult', 'umax', 'umin', 'undef',
  379. 'une', 'uniformRetVal', 'uniqueRetVal', 'unknown', 'unnamed_addr', 'uno',
  380. 'unordered', 'unreachable', 'unsat', 'unwind', 'urem', 'uselistorder',
  381. 'uselistorder_bb', 'uwtable', 'va_arg', 'variable', 'vFuncId',
  382. 'virtualConstProp', 'void', 'volatile', 'weak', 'weak_odr', 'webkit_jscc',
  383. 'win64cc', 'within', 'wpdRes', 'wpdResolutions', 'writeonly', 'x',
  384. 'x86_64_sysvcc', 'x86_fastcallcc', 'x86_fp80', 'x86_intrcc', 'x86_mmx',
  385. 'x86_regcallcc', 'x86_stdcallcc', 'x86_thiscallcc', 'x86_vectorcallcc', 'xchg',
  386. 'xor', 'zeroext', 'zeroinitializer', 'zext'),
  387. suffix=r'\b'), Keyword),
  388. # Types
  389. (words(('void', 'half', 'float', 'double', 'x86_fp80', 'fp128',
  390. 'ppc_fp128', 'label', 'metadata', 'token')), Keyword.Type),
  391. # Integer types
  392. (r'i[1-9]\d*', Keyword)
  393. ]
  394. }
  395. class NasmLexer(RegexLexer):
  396. """
  397. For Nasm (Intel) assembly code.
  398. """
  399. name = 'NASM'
  400. aliases = ['nasm']
  401. filenames = ['*.asm', '*.ASM']
  402. mimetypes = ['text/x-nasm']
  403. # Tasm uses the same file endings, but TASM is not as common as NASM, so
  404. # we prioritize NASM higher by default
  405. priority = 1.0
  406. identifier = r'[a-z$._?][\w$.?#@~]*'
  407. hexn = r'(?:0x[0-9a-f]+|$0[0-9a-f]*|[0-9]+[0-9a-f]*h)'
  408. octn = r'[0-7]+q'
  409. binn = r'[01]+b'
  410. decn = r'[0-9]+'
  411. floatn = decn + r'\.e?' + decn
  412. string = r'"(\\"|[^"\n])*"|' + r"'(\\'|[^'\n])*'|" + r"`(\\`|[^`\n])*`"
  413. declkw = r'(?:res|d)[bwdqt]|times'
  414. register = (r'r[0-9][0-5]?[bwd]|'
  415. r'[a-d][lh]|[er]?[a-d]x|[er]?[sb]p|[er]?[sd]i|[c-gs]s|st[0-7]|'
  416. r'mm[0-7]|cr[0-4]|dr[0-367]|tr[3-7]')
  417. wordop = r'seg|wrt|strict'
  418. type = r'byte|[dq]?word'
  419. # Directives must be followed by whitespace, otherwise CPU will match
  420. # cpuid for instance.
  421. directives = (r'(?:BITS|USE16|USE32|SECTION|SEGMENT|ABSOLUTE|EXTERN|GLOBAL|'
  422. r'ORG|ALIGN|STRUC|ENDSTRUC|COMMON|CPU|GROUP|UPPERCASE|IMPORT|'
  423. r'EXPORT|LIBRARY|MODULE)\s+')
  424. flags = re.IGNORECASE | re.MULTILINE
  425. tokens = {
  426. 'root': [
  427. (r'^\s*%', Comment.Preproc, 'preproc'),
  428. include('whitespace'),
  429. (identifier + ':', Name.Label),
  430. (r'(%s)(\s+)(equ)' % identifier,
  431. bygroups(Name.Constant, Keyword.Declaration, Keyword.Declaration),
  432. 'instruction-args'),
  433. (directives, Keyword, 'instruction-args'),
  434. (declkw, Keyword.Declaration, 'instruction-args'),
  435. (identifier, Name.Function, 'instruction-args'),
  436. (r'[\r\n]+', Text)
  437. ],
  438. 'instruction-args': [
  439. (string, String),
  440. (hexn, Number.Hex),
  441. (octn, Number.Oct),
  442. (binn, Number.Bin),
  443. (floatn, Number.Float),
  444. (decn, Number.Integer),
  445. include('punctuation'),
  446. (register, Name.Builtin),
  447. (identifier, Name.Variable),
  448. (r'[\r\n]+', Text, '#pop'),
  449. include('whitespace')
  450. ],
  451. 'preproc': [
  452. (r'[^;\n]+', Comment.Preproc),
  453. (r';.*?\n', Comment.Single, '#pop'),
  454. (r'\n', Comment.Preproc, '#pop'),
  455. ],
  456. 'whitespace': [
  457. (r'\n', Text),
  458. (r'[ \t]+', Text),
  459. (r';.*', Comment.Single)
  460. ],
  461. 'punctuation': [
  462. (r'[,():\[\]]+', Punctuation),
  463. (r'[&|^<>+*/%~-]+', Operator),
  464. (r'[$]+', Keyword.Constant),
  465. (wordop, Operator.Word),
  466. (type, Keyword.Type)
  467. ],
  468. }
  469. def analyse_text(text):
  470. # Probably TASM
  471. if re.match(r'PROC', text, re.IGNORECASE):
  472. return False
  473. class NasmObjdumpLexer(ObjdumpLexer):
  474. """
  475. For the output of 'objdump -d -M intel'.
  476. .. versionadded:: 2.0
  477. """
  478. name = 'objdump-nasm'
  479. aliases = ['objdump-nasm']
  480. filenames = ['*.objdump-intel']
  481. mimetypes = ['text/x-nasm-objdump']
  482. tokens = _objdump_lexer_tokens(NasmLexer)
  483. class TasmLexer(RegexLexer):
  484. """
  485. For Tasm (Turbo Assembler) assembly code.
  486. """
  487. name = 'TASM'
  488. aliases = ['tasm']
  489. filenames = ['*.asm', '*.ASM', '*.tasm']
  490. mimetypes = ['text/x-tasm']
  491. identifier = r'[@a-z$._?][\w$.?#@~]*'
  492. hexn = r'(?:0x[0-9a-f]+|$0[0-9a-f]*|[0-9]+[0-9a-f]*h)'
  493. octn = r'[0-7]+q'
  494. binn = r'[01]+b'
  495. decn = r'[0-9]+'
  496. floatn = decn + r'\.e?' + decn
  497. string = r'"(\\"|[^"\n])*"|' + r"'(\\'|[^'\n])*'|" + r"`(\\`|[^`\n])*`"
  498. declkw = r'(?:res|d)[bwdqt]|times'
  499. register = (r'r[0-9][0-5]?[bwd]|'
  500. r'[a-d][lh]|[er]?[a-d]x|[er]?[sb]p|[er]?[sd]i|[c-gs]s|st[0-7]|'
  501. r'mm[0-7]|cr[0-4]|dr[0-367]|tr[3-7]')
  502. wordop = r'seg|wrt|strict'
  503. type = r'byte|[dq]?word'
  504. directives = (r'BITS|USE16|USE32|SECTION|SEGMENT|ABSOLUTE|EXTERN|GLOBAL|'
  505. r'ORG|ALIGN|STRUC|ENDSTRUC|ENDS|COMMON|CPU|GROUP|UPPERCASE|INCLUDE|'
  506. r'EXPORT|LIBRARY|MODULE|PROC|ENDP|USES|ARG|DATASEG|UDATASEG|END|IDEAL|'
  507. r'P386|MODEL|ASSUME|CODESEG|SIZE')
  508. # T[A-Z][a-z] is more of a convention. Lexer should filter out STRUC definitions
  509. # and then 'add' them to datatype somehow.
  510. datatype = (r'db|dd|dw|T[A-Z][a-z]+')
  511. flags = re.IGNORECASE | re.MULTILINE
  512. tokens = {
  513. 'root': [
  514. (r'^\s*%', Comment.Preproc, 'preproc'),
  515. include('whitespace'),
  516. (identifier + ':', Name.Label),
  517. (directives, Keyword, 'instruction-args'),
  518. (r'(%s)(\s+)(%s)' % (identifier, datatype),
  519. bygroups(Name.Constant, Keyword.Declaration, Keyword.Declaration),
  520. 'instruction-args'),
  521. (declkw, Keyword.Declaration, 'instruction-args'),
  522. (identifier, Name.Function, 'instruction-args'),
  523. (r'[\r\n]+', Text)
  524. ],
  525. 'instruction-args': [
  526. (string, String),
  527. (hexn, Number.Hex),
  528. (octn, Number.Oct),
  529. (binn, Number.Bin),
  530. (floatn, Number.Float),
  531. (decn, Number.Integer),
  532. include('punctuation'),
  533. (register, Name.Builtin),
  534. (identifier, Name.Variable),
  535. # Do not match newline when it's preceeded by a backslash
  536. (r'(\\\s*)(;.*)([\r\n])', bygroups(Text, Comment.Single, Text)),
  537. (r'[\r\n]+', Text, '#pop'),
  538. include('whitespace')
  539. ],
  540. 'preproc': [
  541. (r'[^;\n]+', Comment.Preproc),
  542. (r';.*?\n', Comment.Single, '#pop'),
  543. (r'\n', Comment.Preproc, '#pop'),
  544. ],
  545. 'whitespace': [
  546. (r'[\n\r]', Text),
  547. (r'\\[\n\r]', Text),
  548. (r'[ \t]+', Text),
  549. (r';.*', Comment.Single)
  550. ],
  551. 'punctuation': [
  552. (r'[,():\[\]]+', Punctuation),
  553. (r'[&|^<>+*=/%~-]+', Operator),
  554. (r'[$]+', Keyword.Constant),
  555. (wordop, Operator.Word),
  556. (type, Keyword.Type)
  557. ],
  558. }
  559. def analyse_text(text):
  560. # See above
  561. if re.match(r'PROC', text, re.I):
  562. return True
  563. class Ca65Lexer(RegexLexer):
  564. """
  565. For ca65 assembler sources.
  566. .. versionadded:: 1.6
  567. """
  568. name = 'ca65 assembler'
  569. aliases = ['ca65']
  570. filenames = ['*.s']
  571. flags = re.IGNORECASE
  572. tokens = {
  573. 'root': [
  574. (r';.*', Comment.Single),
  575. (r'\s+', Text),
  576. (r'[a-z_.@$][\w.@$]*:', Name.Label),
  577. (r'((ld|st)[axy]|(in|de)[cxy]|asl|lsr|ro[lr]|adc|sbc|cmp|cp[xy]'
  578. r'|cl[cvdi]|se[cdi]|jmp|jsr|bne|beq|bpl|bmi|bvc|bvs|bcc|bcs'
  579. r'|p[lh][ap]|rt[is]|brk|nop|ta[xy]|t[xy]a|txs|tsx|and|ora|eor'
  580. r'|bit)\b', Keyword),
  581. (r'\.\w+', Keyword.Pseudo),
  582. (r'[-+~*/^&|!<>=]', Operator),
  583. (r'"[^"\n]*.', String),
  584. (r"'[^'\n]*.", String.Char),
  585. (r'\$[0-9a-f]+|[0-9a-f]+h\b', Number.Hex),
  586. (r'\d+', Number.Integer),
  587. (r'%[01]+', Number.Bin),
  588. (r'[#,.:()=\[\]]', Punctuation),
  589. (r'[a-z_.@$][\w.@$]*', Name),
  590. ]
  591. }
  592. def analyse_text(self, text):
  593. # comments in GAS start with "#"
  594. if re.match(r'^\s*;', text, re.MULTILINE):
  595. return 0.9
  596. class Dasm16Lexer(RegexLexer):
  597. """
  598. Simple lexer for DCPU-16 Assembly
  599. Check http://0x10c.com/doc/dcpu-16.txt
  600. .. versionadded:: 2.4
  601. """
  602. name = 'DASM16'
  603. aliases = ['dasm16']
  604. filenames = ['*.dasm16', '*.dasm']
  605. mimetypes = ['text/x-dasm16']
  606. INSTRUCTIONS = [
  607. 'SET',
  608. 'ADD', 'SUB',
  609. 'MUL', 'MLI',
  610. 'DIV', 'DVI',
  611. 'MOD', 'MDI',
  612. 'AND', 'BOR', 'XOR',
  613. 'SHR', 'ASR', 'SHL',
  614. 'IFB', 'IFC', 'IFE', 'IFN', 'IFG', 'IFA', 'IFL', 'IFU',
  615. 'ADX', 'SBX',
  616. 'STI', 'STD',
  617. 'JSR',
  618. 'INT', 'IAG', 'IAS', 'RFI', 'IAQ', 'HWN', 'HWQ', 'HWI',
  619. ]
  620. REGISTERS = [
  621. 'A', 'B', 'C',
  622. 'X', 'Y', 'Z',
  623. 'I', 'J',
  624. 'SP', 'PC', 'EX',
  625. 'POP', 'PEEK', 'PUSH'
  626. ]
  627. # Regexes yo
  628. char = r'[a-zA-Z$._0-9@]'
  629. identifier = r'(?:[a-zA-Z$_]' + char + r'*|\.' + char + '+)'
  630. number = r'[+-]?(?:0[xX][a-zA-Z0-9]+|\d+)'
  631. binary_number = r'0b[01_]+'
  632. instruction = r'(?i)(' + '|'.join(INSTRUCTIONS) + ')'
  633. single_char = r"'\\?" + char + "'"
  634. string = r'"(\\"|[^"])*"'
  635. def guess_identifier(lexer, match):
  636. ident = match.group(0)
  637. klass = Name.Variable if ident.upper() in lexer.REGISTERS else Name.Label
  638. yield match.start(), klass, ident
  639. tokens = {
  640. 'root': [
  641. include('whitespace'),
  642. (':' + identifier, Name.Label),
  643. (identifier + ':', Name.Label),
  644. (instruction, Name.Function, 'instruction-args'),
  645. (r'\.' + identifier, Name.Function, 'data-args'),
  646. (r'[\r\n]+', Text)
  647. ],
  648. 'numeric' : [
  649. (binary_number, Number.Integer),
  650. (number, Number.Integer),
  651. (single_char, String),
  652. ],
  653. 'arg' : [
  654. (identifier, guess_identifier),
  655. include('numeric')
  656. ],
  657. 'deref' : [
  658. (r'\+', Punctuation),
  659. (r'\]', Punctuation, '#pop'),
  660. include('arg'),
  661. include('whitespace')
  662. ],
  663. 'instruction-line' : [
  664. (r'[\r\n]+', Text, '#pop'),
  665. (r';.*?$', Comment, '#pop'),
  666. include('whitespace')
  667. ],
  668. 'instruction-args': [
  669. (r',', Punctuation),
  670. (r'\[', Punctuation, 'deref'),
  671. include('arg'),
  672. include('instruction-line')
  673. ],
  674. 'data-args' : [
  675. (r',', Punctuation),
  676. include('numeric'),
  677. (string, String),
  678. include('instruction-line')
  679. ],
  680. 'whitespace': [
  681. (r'\n', Text),
  682. (r'\s+', Text),
  683. (r';.*?\n', Comment)
  684. ],
  685. }