asm.py 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. """
  2. pygments.lexers.asm
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for assembly languages.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, include, bygroups, using, words, \
  10. DelegatingLexer, default
  11. from pygments.lexers.c_cpp import CppLexer, CLexer
  12. from pygments.lexers.d import DLexer
  13. from pygments.token import Text, Name, Number, String, Comment, Punctuation, \
  14. Other, Keyword, Operator, Literal
  15. __all__ = ['GasLexer', 'ObjdumpLexer', 'DObjdumpLexer', 'CppObjdumpLexer',
  16. 'CObjdumpLexer', 'HsailLexer', 'LlvmLexer', 'LlvmMirBodyLexer',
  17. 'LlvmMirLexer', 'NasmLexer', 'NasmObjdumpLexer', 'TasmLexer',
  18. '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-fA-F0-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.Single, '#pop'),
  50. (r'/[*].*?[*]/', Comment.Multiline),
  51. (r'/[*].*?\n[\w\W]*?[*]/', Comment.Multiline, '#pop'),
  52. include('punctuation'),
  53. include('whitespace')
  54. ],
  55. 'instruction-args': [
  56. # For objdump-disassembled code, shouldn't occur in
  57. # actual assembler input
  58. ('([a-z0-9]+)( )(<)('+identifier+')(>)',
  59. bygroups(Number.Hex, Text, Punctuation, Name.Constant,
  60. Punctuation)),
  61. ('([a-z0-9]+)( )(<)('+identifier+')([-+])('+number+')(>)',
  62. bygroups(Number.Hex, Text, Punctuation, Name.Constant,
  63. Punctuation, Number.Integer, Punctuation)),
  64. # Address constants
  65. (identifier, Name.Constant),
  66. (number, Number.Integer),
  67. # Registers
  68. (register, Name.Variable),
  69. # Numeric constants
  70. ('$'+number, Number.Integer),
  71. (r"$'(.|\\')'", String.Char),
  72. (r'[\r\n]+', Text, '#pop'),
  73. (r'([;#]|//).*?\n', Comment.Single, '#pop'),
  74. (r'/[*].*?[*]/', Comment.Multiline),
  75. (r'/[*].*?\n[\w\W]*?[*]/', Comment.Multiline, '#pop'),
  76. include('punctuation'),
  77. include('whitespace')
  78. ],
  79. 'whitespace': [
  80. (r'\n', Text),
  81. (r'\s+', Text),
  82. (r'([;#]|//).*?\n', Comment.Single),
  83. (r'/[*][\w\W]*?[*]/', Comment.Multiline)
  84. ],
  85. 'punctuation': [
  86. (r'[-*,.()\[\]!:]+', Punctuation)
  87. ]
  88. }
  89. def analyse_text(text):
  90. if re.search(r'^\.(text|data|section)', text, re.M):
  91. return True
  92. elif re.search(r'^\.\w+', text, re.M):
  93. return 0.1
  94. def _objdump_lexer_tokens(asm_lexer):
  95. """
  96. Common objdump lexer tokens to wrap an ASM lexer.
  97. """
  98. hex_re = r'[0-9A-Za-z]'
  99. return {
  100. 'root': [
  101. # File name & format:
  102. ('(.*?)(:)( +file format )(.*?)$',
  103. bygroups(Name.Label, Punctuation, Text, String)),
  104. # Section header
  105. ('(Disassembly of section )(.*?)(:)$',
  106. bygroups(Text, Name.Label, Punctuation)),
  107. # Function labels
  108. # (With offset)
  109. ('('+hex_re+'+)( )(<)(.*?)([-+])(0[xX][A-Za-z0-9]+)(>:)$',
  110. bygroups(Number.Hex, Text, Punctuation, Name.Function,
  111. Punctuation, Number.Hex, Punctuation)),
  112. # (Without offset)
  113. ('('+hex_re+'+)( )(<)(.*?)(>:)$',
  114. bygroups(Number.Hex, Text, Punctuation, Name.Function,
  115. Punctuation)),
  116. # Code line with disassembled instructions
  117. ('( *)('+hex_re+r'+:)(\t)((?:'+hex_re+hex_re+' )+)( *\t)([a-zA-Z].*?)$',
  118. bygroups(Text, Name.Label, Text, Number.Hex, Text,
  119. using(asm_lexer))),
  120. # Code line with ascii
  121. ('( *)('+hex_re+r'+:)(\t)((?:'+hex_re+hex_re+' )+)( *)(.*?)$',
  122. bygroups(Text, Name.Label, Text, Number.Hex, Text, String)),
  123. # Continued code line, only raw opcodes without disassembled
  124. # instruction
  125. ('( *)('+hex_re+r'+:)(\t)((?:'+hex_re+hex_re+' )+)$',
  126. bygroups(Text, Name.Label, Text, Number.Hex)),
  127. # Skipped a few bytes
  128. (r'\t\.\.\.$', Text),
  129. # Relocation line
  130. # (With offset)
  131. (r'(\t\t\t)('+hex_re+r'+:)( )([^\t]+)(\t)(.*?)([-+])(0x'+hex_re+'+)$',
  132. bygroups(Text, Name.Label, Text, Name.Property, Text,
  133. Name.Constant, Punctuation, Number.Hex)),
  134. # (Without offset)
  135. (r'(\t\t\t)('+hex_re+r'+:)( )([^\t]+)(\t)(.*?)$',
  136. bygroups(Text, Name.Label, Text, Name.Property, Text,
  137. Name.Constant)),
  138. (r'[^\n]+\n', Other)
  139. ]
  140. }
  141. class ObjdumpLexer(RegexLexer):
  142. """
  143. For the output of ``objdump -dr``.
  144. """
  145. name = 'objdump'
  146. aliases = ['objdump']
  147. filenames = ['*.objdump']
  148. mimetypes = ['text/x-objdump']
  149. tokens = _objdump_lexer_tokens(GasLexer)
  150. class DObjdumpLexer(DelegatingLexer):
  151. """
  152. For the output of ``objdump -Sr`` on compiled D files.
  153. """
  154. name = 'd-objdump'
  155. aliases = ['d-objdump']
  156. filenames = ['*.d-objdump']
  157. mimetypes = ['text/x-d-objdump']
  158. def __init__(self, **options):
  159. super().__init__(DLexer, ObjdumpLexer, **options)
  160. class CppObjdumpLexer(DelegatingLexer):
  161. """
  162. For the output of ``objdump -Sr`` on compiled C++ files.
  163. """
  164. name = 'cpp-objdump'
  165. aliases = ['cpp-objdump', 'c++-objdumb', 'cxx-objdump']
  166. filenames = ['*.cpp-objdump', '*.c++-objdump', '*.cxx-objdump']
  167. mimetypes = ['text/x-cpp-objdump']
  168. def __init__(self, **options):
  169. super().__init__(CppLexer, ObjdumpLexer, **options)
  170. class CObjdumpLexer(DelegatingLexer):
  171. """
  172. For the output of ``objdump -Sr`` on compiled C files.
  173. """
  174. name = 'c-objdump'
  175. aliases = ['c-objdump']
  176. filenames = ['*.c-objdump']
  177. mimetypes = ['text/x-c-objdump']
  178. def __init__(self, **options):
  179. super().__init__(CLexer, ObjdumpLexer, **options)
  180. class HsailLexer(RegexLexer):
  181. """
  182. For HSAIL assembly code.
  183. .. versionadded:: 2.2
  184. """
  185. name = 'HSAIL'
  186. aliases = ['hsail', 'hsa']
  187. filenames = ['*.hsail']
  188. mimetypes = ['text/x-hsail']
  189. string = r'"[^"]*?"'
  190. identifier = r'[a-zA-Z_][\w.]*'
  191. # Registers
  192. register_number = r'[0-9]+'
  193. register = r'(\$(c|s|d|q)' + register_number + ')'
  194. # Qualifiers
  195. alignQual = r'(align\(\d+\))'
  196. widthQual = r'(width\((\d+|all)\))'
  197. allocQual = r'(alloc\(agent\))'
  198. # Instruction Modifiers
  199. roundingMod = (r'((_ftz)?(_up|_down|_zero|_near))')
  200. datatypeMod = (r'_('
  201. # packedTypes
  202. r'u8x4|s8x4|u16x2|s16x2|u8x8|s8x8|u16x4|s16x4|u32x2|s32x2|'
  203. r'u8x16|s8x16|u16x8|s16x8|u32x4|s32x4|u64x2|s64x2|'
  204. r'f16x2|f16x4|f16x8|f32x2|f32x4|f64x2|'
  205. # baseTypes
  206. r'u8|s8|u16|s16|u32|s32|u64|s64|'
  207. r'b128|b8|b16|b32|b64|b1|'
  208. r'f16|f32|f64|'
  209. # opaqueType
  210. r'roimg|woimg|rwimg|samp|sig32|sig64)')
  211. # Numeric Constant
  212. float = r'((\d+\.)|(\d*\.\d+))[eE][+-]?\d+'
  213. hexfloat = r'0[xX](([0-9a-fA-F]+\.[0-9a-fA-F]*)|([0-9a-fA-F]*\.[0-9a-fA-F]+))[pP][+-]?\d+'
  214. ieeefloat = r'0((h|H)[0-9a-fA-F]{4}|(f|F)[0-9a-fA-F]{8}|(d|D)[0-9a-fA-F]{16})'
  215. tokens = {
  216. 'root': [
  217. include('whitespace'),
  218. include('comments'),
  219. (string, String),
  220. (r'@' + identifier + ':?', Name.Label),
  221. (register, Name.Variable.Anonymous),
  222. include('keyword'),
  223. (r'&' + identifier, Name.Variable.Global),
  224. (r'%' + identifier, Name.Variable),
  225. (hexfloat, Number.Hex),
  226. (r'0[xX][a-fA-F0-9]+', Number.Hex),
  227. (ieeefloat, Number.Float),
  228. (float, Number.Float),
  229. (r'\d+', Number.Integer),
  230. (r'[=<>{}\[\]()*.,:;!]|x\b', Punctuation)
  231. ],
  232. 'whitespace': [
  233. (r'(\n|\s)+', Text),
  234. ],
  235. 'comments': [
  236. (r'/\*.*?\*/', Comment.Multiline),
  237. (r'//.*?\n', Comment.Single),
  238. ],
  239. 'keyword': [
  240. # Types
  241. (r'kernarg' + datatypeMod, Keyword.Type),
  242. # Regular keywords
  243. (r'\$(full|base|small|large|default|zero|near)', Keyword),
  244. (words((
  245. 'module', 'extension', 'pragma', 'prog', 'indirect', 'signature',
  246. 'decl', 'kernel', 'function', 'enablebreakexceptions',
  247. 'enabledetectexceptions', 'maxdynamicgroupsize', 'maxflatgridsize',
  248. 'maxflatworkgroupsize', 'requireddim', 'requiredgridsize',
  249. 'requiredworkgroupsize', 'requirenopartialworkgroups'),
  250. suffix=r'\b'), Keyword),
  251. # instructions
  252. (roundingMod, Keyword),
  253. (datatypeMod, Keyword),
  254. (r'_(' + alignQual + '|' + widthQual + ')', Keyword),
  255. (r'_kernarg', Keyword),
  256. (r'(nop|imagefence)\b', Keyword),
  257. (words((
  258. 'cleardetectexcept', 'clock', 'cuid', 'debugtrap', 'dim',
  259. 'getdetectexcept', 'groupbaseptr', 'kernargbaseptr', 'laneid',
  260. 'maxcuid', 'maxwaveid', 'packetid', 'setdetectexcept', 'waveid',
  261. 'workitemflatabsid', 'workitemflatid', 'nullptr', 'abs', 'bitrev',
  262. 'currentworkgroupsize', 'currentworkitemflatid', 'fract', 'ncos',
  263. 'neg', 'nexp2', 'nlog2', 'nrcp', 'nrsqrt', 'nsin', 'nsqrt',
  264. 'gridgroups', 'gridsize', 'not', 'sqrt', 'workgroupid',
  265. 'workgroupsize', 'workitemabsid', 'workitemid', 'ceil', 'floor',
  266. 'rint', 'trunc', 'add', 'bitmask', 'borrow', 'carry', 'copysign',
  267. 'div', 'rem', 'sub', 'shl', 'shr', 'and', 'or', 'xor', 'unpackhi',
  268. 'unpacklo', 'max', 'min', 'fma', 'mad', 'bitextract', 'bitselect',
  269. 'shuffle', 'cmov', 'bitalign', 'bytealign', 'lerp', 'nfma', 'mul',
  270. 'mulhi', 'mul24hi', 'mul24', 'mad24', 'mad24hi', 'bitinsert',
  271. 'combine', 'expand', 'lda', 'mov', 'pack', 'unpack', 'packcvt',
  272. 'unpackcvt', 'sad', 'sementp', 'ftos', 'stof', 'cmp', 'ld', 'st',
  273. '_eq', '_ne', '_lt', '_le', '_gt', '_ge', '_equ', '_neu', '_ltu',
  274. '_leu', '_gtu', '_geu', '_num', '_nan', '_seq', '_sne', '_slt',
  275. '_sle', '_sgt', '_sge', '_snum', '_snan', '_sequ', '_sneu', '_sltu',
  276. '_sleu', '_sgtu', '_sgeu', 'atomic', '_ld', '_st', '_cas', '_add',
  277. '_and', '_exch', '_max', '_min', '_or', '_sub', '_wrapdec',
  278. '_wrapinc', '_xor', 'ret', 'cvt', '_readonly', '_kernarg', '_global',
  279. 'br', 'cbr', 'sbr', '_scacq', '_screl', '_scar', '_rlx', '_wave',
  280. '_wg', '_agent', '_system', 'ldimage', 'stimage', '_v2', '_v3', '_v4',
  281. '_1d', '_2d', '_3d', '_1da', '_2da', '_1db', '_2ddepth', '_2dadepth',
  282. '_width', '_height', '_depth', '_array', '_channelorder',
  283. '_channeltype', 'querysampler', '_coord', '_filter', '_addressing',
  284. 'barrier', 'wavebarrier', 'initfbar', 'joinfbar', 'waitfbar',
  285. 'arrivefbar', 'leavefbar', 'releasefbar', 'ldf', 'activelaneid',
  286. 'activelanecount', 'activelanemask', 'activelanepermute', 'call',
  287. 'scall', 'icall', 'alloca', 'packetcompletionsig',
  288. 'addqueuewriteindex', 'casqueuewriteindex', 'ldqueuereadindex',
  289. 'stqueuereadindex', 'readonly', 'global', 'private', 'group',
  290. 'spill', 'arg', '_upi', '_downi', '_zeroi', '_neari', '_upi_sat',
  291. '_downi_sat', '_zeroi_sat', '_neari_sat', '_supi', '_sdowni',
  292. '_szeroi', '_sneari', '_supi_sat', '_sdowni_sat', '_szeroi_sat',
  293. '_sneari_sat', '_pp', '_ps', '_sp', '_ss', '_s', '_p', '_pp_sat',
  294. '_ps_sat', '_sp_sat', '_ss_sat', '_s_sat', '_p_sat')), Keyword),
  295. # Integer types
  296. (r'i[1-9]\d*', Keyword)
  297. ]
  298. }
  299. class LlvmLexer(RegexLexer):
  300. """
  301. For LLVM assembly code.
  302. """
  303. name = 'LLVM'
  304. aliases = ['llvm']
  305. filenames = ['*.ll']
  306. mimetypes = ['text/x-llvm']
  307. #: optional Comment or Whitespace
  308. string = r'"[^"]*?"'
  309. identifier = r'([-a-zA-Z$._][\w\-$.]*|' + string + ')'
  310. tokens = {
  311. 'root': [
  312. include('whitespace'),
  313. # Before keywords, because keywords are valid label names :(...
  314. (identifier + r'\s*:', Name.Label),
  315. include('keyword'),
  316. (r'%' + identifier, Name.Variable),
  317. (r'@' + identifier, Name.Variable.Global),
  318. (r'%\d+', Name.Variable.Anonymous),
  319. (r'@\d+', Name.Variable.Global),
  320. (r'#\d+', Name.Variable.Global),
  321. (r'!' + identifier, Name.Variable),
  322. (r'!\d+', Name.Variable.Anonymous),
  323. (r'c?' + string, String),
  324. (r'0[xX][a-fA-F0-9]+', Number),
  325. (r'-?\d+(?:[.]\d+)?(?:[eE][-+]?\d+(?:[.]\d+)?)?', Number),
  326. (r'[=<>{}\[\]()*.,!]|x\b', Punctuation)
  327. ],
  328. 'whitespace': [
  329. (r'(\n|\s)+', Text),
  330. (r';.*?\n', Comment)
  331. ],
  332. 'keyword': [
  333. # Regular keywords
  334. (words((
  335. 'acq_rel', 'acquire', 'add', 'addrspace', 'addrspacecast', 'afn', 'alias',
  336. 'aliasee', 'align', 'alignLog2', 'alignstack', 'alloca', 'allocsize', 'allOnes',
  337. 'alwaysinline', 'amdgpu_cs', 'amdgpu_es', 'amdgpu_gs', 'amdgpu_hs',
  338. 'amdgpu_kernel', 'amdgpu_ls', 'amdgpu_ps', 'amdgpu_vs', 'and', 'any',
  339. 'anyregcc', 'appending', 'arcp', 'argmemonly', 'args', 'arm_aapcs_vfpcc',
  340. 'arm_aapcscc', 'arm_apcscc', 'ashr', 'asm', 'atomic', 'atomicrmw', 'attributes',
  341. 'available_externally', 'avr_intrcc', 'avr_signalcc', 'bit', 'bitcast',
  342. 'bitMask', 'blockaddress', 'br', 'branchFunnel', 'builtin', 'byArg', 'byte',
  343. 'byteArray', 'byval', 'c', 'call', 'callee', 'caller', 'calls', 'catch',
  344. 'catchpad', 'catchret', 'catchswitch', 'cc', 'ccc', 'cleanup', 'cleanuppad',
  345. 'cleanupret', 'cmpxchg', 'cold', 'coldcc', 'comdat', 'common', 'constant',
  346. 'contract', 'convergent', 'critical', 'cxx_fast_tlscc', 'datalayout', 'declare',
  347. 'default', 'define', 'deplibs', 'dereferenceable', 'dereferenceable_or_null',
  348. 'distinct', 'dllexport', 'dllimport', 'dso_local', 'dso_preemptable',
  349. 'dsoLocal', 'eq', 'exact', 'exactmatch', 'extern_weak', 'external',
  350. 'externally_initialized', 'extractelement', 'extractvalue', 'fadd', 'false',
  351. 'fast', 'fastcc', 'fcmp', 'fdiv', 'fence', 'filter', 'flags', 'fmul',
  352. 'fpext', 'fptosi', 'fptoui', 'fptrunc', 'freeze', 'frem', 'from', 'fsub',
  353. 'funcFlags', 'function', 'gc', 'getelementptr', 'ghccc', 'global', 'guid', 'gv',
  354. 'hash', 'hhvm_ccc', 'hhvmcc', 'hidden', 'hot', 'hotness', 'icmp',
  355. 'ifunc', 'inaccessiblemem_or_argmemonly', 'inaccessiblememonly', 'inalloca',
  356. 'inbounds', 'indir', 'indirectbr', 'info', 'initialexec', 'inline',
  357. 'inlineBits', 'inlinehint', 'inrange', 'inreg', 'insertelement', 'insertvalue',
  358. 'insts', 'intel_ocl_bicc', 'inteldialect', 'internal', 'inttoptr', 'invoke',
  359. 'jumptable', 'kind', 'landingpad', 'largest', 'linkage', 'linkonce',
  360. 'linkonce_odr', 'live', 'load', 'local_unnamed_addr', 'localdynamic',
  361. 'localexec', 'lshr', 'max', 'metadata', 'min', 'minsize', 'module', 'monotonic',
  362. 'msp430_intrcc', 'mul', 'musttail', 'naked', 'name', 'nand', 'ne', 'nest',
  363. 'ninf', 'nnan', 'noalias', 'nobuiltin', 'nocapture', 'nocf_check',
  364. 'noduplicate', 'noduplicates', 'noimplicitfloat', 'noinline', 'none',
  365. 'nonlazybind', 'nonnull', 'norecurse', 'noRecurse', 'noredzone', 'noreturn',
  366. 'notail', 'notEligibleToImport', 'nounwind', 'nsw', 'nsz', 'null', 'nuw', 'oeq',
  367. 'offset', 'oge', 'ogt', 'ole', 'olt', 'one', 'opaque', 'optforfuzzing',
  368. 'optnone', 'optsize', 'or', 'ord', 'path', 'personality', 'phi', 'poison',
  369. 'prefix', 'preserve_allcc', 'preserve_mostcc', 'private', 'prologue',
  370. 'protected', 'ptrtoint', 'ptx_device', 'ptx_kernel', 'readnone', 'readNone',
  371. 'readonly', 'readOnly', 'reassoc', 'refs', 'relbf', 'release', 'resByArg',
  372. 'resume', 'ret', 'returnDoesNotAlias', 'returned', 'returns_twice', 'safestack',
  373. 'samesize', 'sanitize_address', 'sanitize_hwaddress', 'sanitize_memory',
  374. 'sanitize_thread', 'sdiv', 'section', 'select', 'seq_cst', 'sext', 'sge', 'sgt',
  375. 'shadowcallstack', 'shl', 'shufflevector', 'sideeffect', 'signext', 'single',
  376. 'singleImpl', 'singleImplName', 'sitofp', 'sizeM1', 'sizeM1BitWidth', 'sle',
  377. 'slt', 'source_filename', 'speculatable', 'spir_func', 'spir_kernel', 'srem',
  378. 'sret', 'ssp', 'sspreq', 'sspstrong', 'store', 'strictfp', 'sub', 'summaries',
  379. 'summary', 'swiftcc', 'swifterror', 'swiftself', 'switch', 'syncscope', 'tail',
  380. 'target', 'thread_local', 'to', 'token', 'triple', 'true', 'trunc', 'type',
  381. 'typeCheckedLoadConstVCalls', 'typeCheckedLoadVCalls', 'typeid', 'typeIdInfo',
  382. 'typeTestAssumeConstVCalls', 'typeTestAssumeVCalls', 'typeTestRes', 'typeTests',
  383. 'udiv', 'ueq', 'uge', 'ugt', 'uitofp', 'ule', 'ult', 'umax', 'umin', 'undef',
  384. 'une', 'uniformRetVal', 'uniqueRetVal', 'unknown', 'unnamed_addr', 'uno',
  385. 'unordered', 'unreachable', 'unsat', 'unwind', 'urem', 'uselistorder',
  386. 'uselistorder_bb', 'uwtable', 'va_arg', 'variable', 'vFuncId',
  387. 'virtualConstProp', 'void', 'volatile', 'weak', 'weak_odr', 'webkit_jscc',
  388. 'win64cc', 'within', 'wpdRes', 'wpdResolutions', 'writeonly',
  389. 'x86_64_sysvcc', 'x86_fastcallcc', 'x86_intrcc', 'x86_mmx',
  390. 'x86_regcallcc', 'x86_stdcallcc', 'x86_thiscallcc', 'x86_vectorcallcc', 'xchg',
  391. 'xor', 'zeroext', 'zeroinitializer', 'zext', 'immarg', 'willreturn'),
  392. suffix=r'\b'), Keyword),
  393. # Types
  394. (words(('void', 'half', 'bfloat', 'float', 'double', 'fp128',
  395. 'x86_fp80', 'ppc_fp128', 'label', 'metadata', 'token')),
  396. Keyword.Type),
  397. # Integer types
  398. (r'i[1-9]\d*', Keyword.Type)
  399. ]
  400. }
  401. class LlvmMirBodyLexer(RegexLexer):
  402. """
  403. For LLVM MIR examples without the YAML wrapper.
  404. For more information on LLVM MIR see https://llvm.org/docs/MIRLangRef.html.
  405. .. versionadded:: 2.6
  406. """
  407. name = 'LLVM-MIR Body'
  408. aliases = ['llvm-mir-body']
  409. filenames = []
  410. mimetypes = []
  411. tokens = {
  412. 'root': [
  413. # Attributes on basic blocks
  414. (words(('liveins', 'successors'), suffix=':'), Keyword),
  415. # Basic Block Labels
  416. (r'bb\.[0-9]+(\.[a-zA-Z0-9_.-]+)?( \(address-taken\))?:', Name.Label),
  417. (r'bb\.[0-9]+ \(%[a-zA-Z0-9_.-]+\)( \(address-taken\))?:', Name.Label),
  418. (r'%bb\.[0-9]+(\.\w+)?', Name.Label),
  419. # Stack references
  420. (r'%stack\.[0-9]+(\.\w+\.addr)?', Name),
  421. # Subreg indices
  422. (r'%subreg\.\w+', Name),
  423. # Virtual registers
  424. (r'%[a-zA-Z0-9_]+ *', Name.Variable, 'vreg'),
  425. # Reference to LLVM-IR global
  426. include('global'),
  427. # Reference to Intrinsic
  428. (r'intrinsic\(\@[a-zA-Z0-9_.]+\)', Name.Variable.Global),
  429. # Comparison predicates
  430. (words(('eq', 'ne', 'sgt', 'sge', 'slt', 'sle', 'ugt', 'uge', 'ult',
  431. 'ule'), prefix=r'intpred\(', suffix=r'\)'), Name.Builtin),
  432. (words(('oeq', 'one', 'ogt', 'oge', 'olt', 'ole', 'ugt', 'uge',
  433. 'ult', 'ule'), prefix=r'floatpred\(', suffix=r'\)'),
  434. Name.Builtin),
  435. # Physical registers
  436. (r'\$\w+', String.Single),
  437. # Assignment operator
  438. (r'=', Operator),
  439. # gMIR Opcodes
  440. (r'(G_ANYEXT|G_[SZ]EXT|G_SEXT_INREG|G_TRUNC|G_IMPLICIT_DEF|G_PHI|'
  441. r'G_FRAME_INDEX|G_GLOBAL_VALUE|G_INTTOPTR|G_PTRTOINT|G_BITCAST|'
  442. r'G_CONSTANT|G_FCONSTANT|G_VASTART|G_VAARG|G_CTLZ|G_CTLZ_ZERO_UNDEF|'
  443. r'G_CTTZ|G_CTTZ_ZERO_UNDEF|G_CTPOP|G_BSWAP|G_BITREVERSE|'
  444. r'G_ADDRSPACE_CAST|G_BLOCK_ADDR|G_JUMP_TABLE|G_DYN_STACKALLOC|'
  445. r'G_ADD|G_SUB|G_MUL|G_[SU]DIV|G_[SU]REM|G_AND|G_OR|G_XOR|G_SHL|'
  446. r'G_[LA]SHR|G_[IF]CMP|G_SELECT|G_GEP|G_PTR_MASK|G_SMIN|G_SMAX|'
  447. r'G_UMIN|G_UMAX|G_[US]ADDO|G_[US]ADDE|G_[US]SUBO|G_[US]SUBE|'
  448. r'G_[US]MULO|G_[US]MULH|G_FNEG|G_FPEXT|G_FPTRUNC|G_FPTO[US]I|'
  449. r'G_[US]ITOFP|G_FABS|G_FCOPYSIGN|G_FCANONICALIZE|G_FMINNUM|'
  450. r'G_FMAXNUM|G_FMINNUM_IEEE|G_FMAXNUM_IEEE|G_FMINIMUM|G_FMAXIMUM|'
  451. r'G_FADD|G_FSUB|G_FMUL|G_FMA|G_FMAD|G_FDIV|G_FREM|G_FPOW|G_FEXP|'
  452. r'G_FEXP2|G_FLOG|G_FLOG2|G_FLOG10|G_FCEIL|G_FCOS|G_FSIN|G_FSQRT|'
  453. r'G_FFLOOR|G_FRINT|G_FNEARBYINT|G_INTRINSIC_TRUNC|'
  454. r'G_INTRINSIC_ROUND|G_LOAD|G_[ZS]EXTLOAD|G_INDEXED_LOAD|'
  455. r'G_INDEXED_[ZS]EXTLOAD|G_STORE|G_INDEXED_STORE|'
  456. r'G_ATOMIC_CMPXCHG_WITH_SUCCESS|G_ATOMIC_CMPXCHG|'
  457. r'G_ATOMICRMW_(XCHG|ADD|SUB|AND|NAND|OR|XOR|MAX|MIN|UMAX|UMIN|FADD|'
  458. r'FSUB)'
  459. r'|G_FENCE|G_EXTRACT|G_UNMERGE_VALUES|G_INSERT|G_MERGE_VALUES|'
  460. r'G_BUILD_VECTOR|G_BUILD_VECTOR_TRUNC|G_CONCAT_VECTORS|'
  461. r'G_INTRINSIC|G_INTRINSIC_W_SIDE_EFFECTS|G_BR|G_BRCOND|'
  462. r'G_BRINDIRECT|G_BRJT|G_INSERT_VECTOR_ELT|G_EXTRACT_VECTOR_ELT|'
  463. r'G_SHUFFLE_VECTOR)\b',
  464. Name.Builtin),
  465. # Target independent opcodes
  466. (r'(COPY|PHI|INSERT_SUBREG|EXTRACT_SUBREG|REG_SEQUENCE)\b',
  467. Name.Builtin),
  468. # Flags
  469. (words(('killed', 'implicit')), Keyword),
  470. # ConstantInt values
  471. (r'i[0-9]+ +', Keyword.Type, 'constantint'),
  472. # ConstantFloat values
  473. (r'(half|float|double) +', Keyword.Type, 'constantfloat'),
  474. # Bare immediates
  475. include('integer'),
  476. # MMO's
  477. (r':: *', Operator, 'mmo'),
  478. # MIR Comments
  479. (r';.*', Comment),
  480. # If we get here, assume it's a target instruction
  481. (r'[a-zA-Z0-9_]+', Name),
  482. # Everything else that isn't highlighted
  483. (r'[(), \n]+', Text),
  484. ],
  485. # The integer constant from a ConstantInt value
  486. 'constantint': [
  487. include('integer'),
  488. (r'(?=.)', Text, '#pop'),
  489. ],
  490. # The floating point constant from a ConstantFloat value
  491. 'constantfloat': [
  492. include('float'),
  493. (r'(?=.)', Text, '#pop'),
  494. ],
  495. 'vreg': [
  496. # The bank or class if there is one
  497. (r' *:(?!:)', Keyword, ('#pop', 'vreg_bank_or_class')),
  498. # The LLT if there is one
  499. (r' *\(', Text, 'vreg_type'),
  500. (r'(?=.)', Text, '#pop'),
  501. ],
  502. 'vreg_bank_or_class': [
  503. # The unassigned bank/class
  504. (r' *_', Name.Variable.Magic),
  505. (r' *[a-zA-Z0-9_]+', Name.Variable),
  506. # The LLT if there is one
  507. (r' *\(', Text, 'vreg_type'),
  508. (r'(?=.)', Text, '#pop'),
  509. ],
  510. 'vreg_type': [
  511. # Scalar and pointer types
  512. (r' *[sp][0-9]+', Keyword.Type),
  513. (r' *<[0-9]+ *x *[sp][0-9]+>', Keyword.Type),
  514. (r'\)', Text, '#pop'),
  515. (r'(?=.)', Text, '#pop'),
  516. ],
  517. 'mmo': [
  518. (r'\(', Text),
  519. (r' +', Text),
  520. (words(('load', 'store', 'on', 'into', 'from', 'align', 'monotonic',
  521. 'acquire', 'release', 'acq_rel', 'seq_cst')),
  522. Keyword),
  523. # IR references
  524. (r'%ir\.[a-zA-Z0-9_.-]+', Name),
  525. (r'%ir-block\.[a-zA-Z0-9_.-]+', Name),
  526. (r'[-+]', Operator),
  527. include('integer'),
  528. include('global'),
  529. (r',', Punctuation),
  530. (r'\), \(', Text),
  531. (r'\)', Text, '#pop'),
  532. ],
  533. 'integer': [(r'-?[0-9]+', Number.Integer),],
  534. 'float': [(r'-?[0-9]+\.[0-9]+(e[+-][0-9]+)?', Number.Float)],
  535. 'global': [(r'\@[a-zA-Z0-9_.]+', Name.Variable.Global)],
  536. }
  537. class LlvmMirLexer(RegexLexer):
  538. """
  539. Lexer for the overall LLVM MIR document format.
  540. MIR is a human readable serialization format that's used to represent LLVM's
  541. machine specific intermediate representation. It allows LLVM's developers to
  542. see the state of the compilation process at various points, as well as test
  543. individual pieces of the compiler.
  544. For more information on LLVM MIR see https://llvm.org/docs/MIRLangRef.html.
  545. .. versionadded:: 2.6
  546. """
  547. name = 'LLVM-MIR'
  548. aliases = ['llvm-mir']
  549. filenames = ['*.mir']
  550. tokens = {
  551. 'root': [
  552. # Comments are hashes at the YAML level
  553. (r'#.*', Comment),
  554. # Documents starting with | are LLVM-IR
  555. (r'--- \|$', Keyword, 'llvm_ir'),
  556. # Other documents are MIR
  557. (r'---', Keyword, 'llvm_mir'),
  558. # Consume everything else in one token for efficiency
  559. (r'[^-#]+|.', Text),
  560. ],
  561. 'llvm_ir': [
  562. # Documents end with '...' or '---'
  563. (r'(\.\.\.|(?=---))', Keyword, '#pop'),
  564. # Delegate to the LlvmLexer
  565. (r'((?:.|\n)+?)(?=(\.\.\.|---))', bygroups(using(LlvmLexer))),
  566. ],
  567. 'llvm_mir': [
  568. # Comments are hashes at the YAML level
  569. (r'#.*', Comment),
  570. # Documents end with '...' or '---'
  571. (r'(\.\.\.|(?=---))', Keyword, '#pop'),
  572. # Handle the simple attributes
  573. (r'name:', Keyword, 'name'),
  574. (words(('alignment', ),
  575. suffix=':'), Keyword, 'number'),
  576. (words(('legalized', 'regBankSelected', 'tracksRegLiveness',
  577. 'selected', 'exposesReturnsTwice'),
  578. suffix=':'), Keyword, 'boolean'),
  579. # Handle the attributes don't highlight inside
  580. (words(('registers', 'stack', 'fixedStack', 'liveins', 'frameInfo',
  581. 'machineFunctionInfo'),
  582. suffix=':'), Keyword),
  583. # Delegate the body block to the LlvmMirBodyLexer
  584. (r'body: *\|', Keyword, 'llvm_mir_body'),
  585. # Consume everything else
  586. (r'.+', Text),
  587. (r'\n', Text),
  588. ],
  589. 'name': [
  590. (r'[^\n]+', Name),
  591. default('#pop'),
  592. ],
  593. 'boolean': [
  594. (r' *(true|false)', Name.Builtin),
  595. default('#pop'),
  596. ],
  597. 'number': [
  598. (r' *[0-9]+', Number),
  599. default('#pop'),
  600. ],
  601. 'llvm_mir_body': [
  602. # Documents end with '...' or '---'.
  603. # We have to pop llvm_mir_body and llvm_mir
  604. (r'(\.\.\.|(?=---))', Keyword, '#pop:2'),
  605. # Delegate the body block to the LlvmMirBodyLexer
  606. (r'((?:.|\n)+?)(?=\.\.\.|---)', bygroups(using(LlvmMirBodyLexer))),
  607. # The '...' is optional. If we didn't already find it then it isn't
  608. # there. There might be a '---' instead though.
  609. (r'(?!\.\.\.|---)((?:.|\n)+)', bygroups(using(LlvmMirBodyLexer))),
  610. ],
  611. }
  612. class NasmLexer(RegexLexer):
  613. """
  614. For Nasm (Intel) assembly code.
  615. """
  616. name = 'NASM'
  617. aliases = ['nasm']
  618. filenames = ['*.asm', '*.ASM']
  619. mimetypes = ['text/x-nasm']
  620. # Tasm uses the same file endings, but TASM is not as common as NASM, so
  621. # we prioritize NASM higher by default
  622. priority = 1.0
  623. identifier = r'[a-z$._?][\w$.?#@~]*'
  624. hexn = r'(?:0x[0-9a-f]+|$0[0-9a-f]*|[0-9]+[0-9a-f]*h)'
  625. octn = r'[0-7]+q'
  626. binn = r'[01]+b'
  627. decn = r'[0-9]+'
  628. floatn = decn + r'\.e?' + decn
  629. string = r'"(\\"|[^"\n])*"|' + r"'(\\'|[^'\n])*'|" + r"`(\\`|[^`\n])*`"
  630. declkw = r'(?:res|d)[bwdqt]|times'
  631. register = (r'r[0-9][0-5]?[bwd]?|'
  632. r'[a-d][lh]|[er]?[a-d]x|[er]?[sb]p|[er]?[sd]i|[c-gs]s|st[0-7]|'
  633. r'mm[0-7]|cr[0-4]|dr[0-367]|tr[3-7]')
  634. wordop = r'seg|wrt|strict'
  635. type = r'byte|[dq]?word'
  636. # Directives must be followed by whitespace, otherwise CPU will match
  637. # cpuid for instance.
  638. directives = (r'(?:BITS|USE16|USE32|SECTION|SEGMENT|ABSOLUTE|EXTERN|GLOBAL|'
  639. r'ORG|ALIGN|STRUC|ENDSTRUC|COMMON|CPU|GROUP|UPPERCASE|IMPORT|'
  640. r'EXPORT|LIBRARY|MODULE)\s+')
  641. flags = re.IGNORECASE | re.MULTILINE
  642. tokens = {
  643. 'root': [
  644. (r'^\s*%', Comment.Preproc, 'preproc'),
  645. include('whitespace'),
  646. (identifier + ':', Name.Label),
  647. (r'(%s)(\s+)(equ)' % identifier,
  648. bygroups(Name.Constant, Keyword.Declaration, Keyword.Declaration),
  649. 'instruction-args'),
  650. (directives, Keyword, 'instruction-args'),
  651. (declkw, Keyword.Declaration, 'instruction-args'),
  652. (identifier, Name.Function, 'instruction-args'),
  653. (r'[\r\n]+', Text)
  654. ],
  655. 'instruction-args': [
  656. (string, String),
  657. (hexn, Number.Hex),
  658. (octn, Number.Oct),
  659. (binn, Number.Bin),
  660. (floatn, Number.Float),
  661. (decn, Number.Integer),
  662. include('punctuation'),
  663. (register, Name.Builtin),
  664. (identifier, Name.Variable),
  665. (r'[\r\n]+', Text, '#pop'),
  666. include('whitespace')
  667. ],
  668. 'preproc': [
  669. (r'[^;\n]+', Comment.Preproc),
  670. (r';.*?\n', Comment.Single, '#pop'),
  671. (r'\n', Comment.Preproc, '#pop'),
  672. ],
  673. 'whitespace': [
  674. (r'\n', Text),
  675. (r'[ \t]+', Text),
  676. (r';.*', Comment.Single)
  677. ],
  678. 'punctuation': [
  679. (r'[,():\[\]]+', Punctuation),
  680. (r'[&|^<>+*/%~-]+', Operator),
  681. (r'[$]+', Keyword.Constant),
  682. (wordop, Operator.Word),
  683. (type, Keyword.Type)
  684. ],
  685. }
  686. def analyse_text(text):
  687. # Probably TASM
  688. if re.match(r'PROC', text, re.IGNORECASE):
  689. return False
  690. class NasmObjdumpLexer(ObjdumpLexer):
  691. """
  692. For the output of ``objdump -d -M intel``.
  693. .. versionadded:: 2.0
  694. """
  695. name = 'objdump-nasm'
  696. aliases = ['objdump-nasm']
  697. filenames = ['*.objdump-intel']
  698. mimetypes = ['text/x-nasm-objdump']
  699. tokens = _objdump_lexer_tokens(NasmLexer)
  700. class TasmLexer(RegexLexer):
  701. """
  702. For Tasm (Turbo Assembler) assembly code.
  703. """
  704. name = 'TASM'
  705. aliases = ['tasm']
  706. filenames = ['*.asm', '*.ASM', '*.tasm']
  707. mimetypes = ['text/x-tasm']
  708. identifier = r'[@a-z$._?][\w$.?#@~]*'
  709. hexn = r'(?:0x[0-9a-f]+|$0[0-9a-f]*|[0-9]+[0-9a-f]*h)'
  710. octn = r'[0-7]+q'
  711. binn = r'[01]+b'
  712. decn = r'[0-9]+'
  713. floatn = decn + r'\.e?' + decn
  714. string = r'"(\\"|[^"\n])*"|' + r"'(\\'|[^'\n])*'|" + r"`(\\`|[^`\n])*`"
  715. declkw = r'(?:res|d)[bwdqt]|times'
  716. register = (r'r[0-9][0-5]?[bwd]|'
  717. r'[a-d][lh]|[er]?[a-d]x|[er]?[sb]p|[er]?[sd]i|[c-gs]s|st[0-7]|'
  718. r'mm[0-7]|cr[0-4]|dr[0-367]|tr[3-7]')
  719. wordop = r'seg|wrt|strict'
  720. type = r'byte|[dq]?word'
  721. directives = (r'BITS|USE16|USE32|SECTION|SEGMENT|ABSOLUTE|EXTERN|GLOBAL|'
  722. r'ORG|ALIGN|STRUC|ENDSTRUC|ENDS|COMMON|CPU|GROUP|UPPERCASE|INCLUDE|'
  723. r'EXPORT|LIBRARY|MODULE|PROC|ENDP|USES|ARG|DATASEG|UDATASEG|END|IDEAL|'
  724. r'P386|MODEL|ASSUME|CODESEG|SIZE')
  725. # T[A-Z][a-z] is more of a convention. Lexer should filter out STRUC definitions
  726. # and then 'add' them to datatype somehow.
  727. datatype = (r'db|dd|dw|T[A-Z][a-z]+')
  728. flags = re.IGNORECASE | re.MULTILINE
  729. tokens = {
  730. 'root': [
  731. (r'^\s*%', Comment.Preproc, 'preproc'),
  732. include('whitespace'),
  733. (identifier + ':', Name.Label),
  734. (directives, Keyword, 'instruction-args'),
  735. (r'(%s)(\s+)(%s)' % (identifier, datatype),
  736. bygroups(Name.Constant, Keyword.Declaration, Keyword.Declaration),
  737. 'instruction-args'),
  738. (declkw, Keyword.Declaration, 'instruction-args'),
  739. (identifier, Name.Function, 'instruction-args'),
  740. (r'[\r\n]+', Text)
  741. ],
  742. 'instruction-args': [
  743. (string, String),
  744. (hexn, Number.Hex),
  745. (octn, Number.Oct),
  746. (binn, Number.Bin),
  747. (floatn, Number.Float),
  748. (decn, Number.Integer),
  749. include('punctuation'),
  750. (register, Name.Builtin),
  751. (identifier, Name.Variable),
  752. # Do not match newline when it's preceeded by a backslash
  753. (r'(\\\s*)(;.*)([\r\n])', bygroups(Text, Comment.Single, Text)),
  754. (r'[\r\n]+', Text, '#pop'),
  755. include('whitespace')
  756. ],
  757. 'preproc': [
  758. (r'[^;\n]+', Comment.Preproc),
  759. (r';.*?\n', Comment.Single, '#pop'),
  760. (r'\n', Comment.Preproc, '#pop'),
  761. ],
  762. 'whitespace': [
  763. (r'[\n\r]', Text),
  764. (r'\\[\n\r]', Text),
  765. (r'[ \t]+', Text),
  766. (r';.*', Comment.Single)
  767. ],
  768. 'punctuation': [
  769. (r'[,():\[\]]+', Punctuation),
  770. (r'[&|^<>+*=/%~-]+', Operator),
  771. (r'[$]+', Keyword.Constant),
  772. (wordop, Operator.Word),
  773. (type, Keyword.Type)
  774. ],
  775. }
  776. def analyse_text(text):
  777. # See above
  778. if re.match(r'PROC', text, re.I):
  779. return True
  780. class Ca65Lexer(RegexLexer):
  781. """
  782. For ca65 assembler sources.
  783. .. versionadded:: 1.6
  784. """
  785. name = 'ca65 assembler'
  786. aliases = ['ca65']
  787. filenames = ['*.s']
  788. flags = re.IGNORECASE
  789. tokens = {
  790. 'root': [
  791. (r';.*', Comment.Single),
  792. (r'\s+', Text),
  793. (r'[a-z_.@$][\w.@$]*:', Name.Label),
  794. (r'((ld|st)[axy]|(in|de)[cxy]|asl|lsr|ro[lr]|adc|sbc|cmp|cp[xy]'
  795. r'|cl[cvdi]|se[cdi]|jmp|jsr|bne|beq|bpl|bmi|bvc|bvs|bcc|bcs'
  796. r'|p[lh][ap]|rt[is]|brk|nop|ta[xy]|t[xy]a|txs|tsx|and|ora|eor'
  797. r'|bit)\b', Keyword),
  798. (r'\.\w+', Keyword.Pseudo),
  799. (r'[-+~*/^&|!<>=]', Operator),
  800. (r'"[^"\n]*.', String),
  801. (r"'[^'\n]*.", String.Char),
  802. (r'\$[0-9a-f]+|[0-9a-f]+h\b', Number.Hex),
  803. (r'\d+', Number.Integer),
  804. (r'%[01]+', Number.Bin),
  805. (r'[#,.:()=\[\]]', Punctuation),
  806. (r'[a-z_.@$][\w.@$]*', Name),
  807. ]
  808. }
  809. def analyse_text(self, text):
  810. # comments in GAS start with "#"
  811. if re.search(r'^\s*;', text, re.MULTILINE):
  812. return 0.9
  813. class Dasm16Lexer(RegexLexer):
  814. """
  815. For DCPU-16 Assembly.
  816. Check http://0x10c.com/doc/dcpu-16.txt
  817. .. versionadded:: 2.4
  818. """
  819. name = 'DASM16'
  820. aliases = ['dasm16']
  821. filenames = ['*.dasm16', '*.dasm']
  822. mimetypes = ['text/x-dasm16']
  823. INSTRUCTIONS = [
  824. 'SET',
  825. 'ADD', 'SUB',
  826. 'MUL', 'MLI',
  827. 'DIV', 'DVI',
  828. 'MOD', 'MDI',
  829. 'AND', 'BOR', 'XOR',
  830. 'SHR', 'ASR', 'SHL',
  831. 'IFB', 'IFC', 'IFE', 'IFN', 'IFG', 'IFA', 'IFL', 'IFU',
  832. 'ADX', 'SBX',
  833. 'STI', 'STD',
  834. 'JSR',
  835. 'INT', 'IAG', 'IAS', 'RFI', 'IAQ', 'HWN', 'HWQ', 'HWI',
  836. ]
  837. REGISTERS = [
  838. 'A', 'B', 'C',
  839. 'X', 'Y', 'Z',
  840. 'I', 'J',
  841. 'SP', 'PC', 'EX',
  842. 'POP', 'PEEK', 'PUSH'
  843. ]
  844. # Regexes yo
  845. char = r'[a-zA-Z0-9_$@.]'
  846. identifier = r'(?:[a-zA-Z$_]' + char + r'*|\.' + char + '+)'
  847. number = r'[+-]?(?:0[xX][a-zA-Z0-9]+|\d+)'
  848. binary_number = r'0b[01_]+'
  849. instruction = r'(?i)(' + '|'.join(INSTRUCTIONS) + ')'
  850. single_char = r"'\\?" + char + "'"
  851. string = r'"(\\"|[^"])*"'
  852. def guess_identifier(lexer, match):
  853. ident = match.group(0)
  854. klass = Name.Variable if ident.upper() in lexer.REGISTERS else Name.Label
  855. yield match.start(), klass, ident
  856. tokens = {
  857. 'root': [
  858. include('whitespace'),
  859. (':' + identifier, Name.Label),
  860. (identifier + ':', Name.Label),
  861. (instruction, Name.Function, 'instruction-args'),
  862. (r'\.' + identifier, Name.Function, 'data-args'),
  863. (r'[\r\n]+', Text)
  864. ],
  865. 'numeric' : [
  866. (binary_number, Number.Integer),
  867. (number, Number.Integer),
  868. (single_char, String),
  869. ],
  870. 'arg' : [
  871. (identifier, guess_identifier),
  872. include('numeric')
  873. ],
  874. 'deref' : [
  875. (r'\+', Punctuation),
  876. (r'\]', Punctuation, '#pop'),
  877. include('arg'),
  878. include('whitespace')
  879. ],
  880. 'instruction-line' : [
  881. (r'[\r\n]+', Text, '#pop'),
  882. (r';.*?$', Comment, '#pop'),
  883. include('whitespace')
  884. ],
  885. 'instruction-args': [
  886. (r',', Punctuation),
  887. (r'\[', Punctuation, 'deref'),
  888. include('arg'),
  889. include('instruction-line')
  890. ],
  891. 'data-args' : [
  892. (r',', Punctuation),
  893. include('numeric'),
  894. (string, String),
  895. include('instruction-line')
  896. ],
  897. 'whitespace': [
  898. (r'\n', Text),
  899. (r'\s+', Text),
  900. (r';.*?\n', Comment)
  901. ],
  902. }