asm.py 25 KB

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