1"""
2 pygments.lexers.ptx
3 ~~~~~~~~~~~~~~~~~~~
4
5 Lexer for other PTX language.
6
7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11from pygments.lexer import RegexLexer, include, words
12from pygments.token import Comment, Keyword, Name, String, Number, \
13 Punctuation, Whitespace, Operator
14
15__all__ = ["PtxLexer"]
16
17
18class PtxLexer(RegexLexer):
19 """
20 For NVIDIA `PTX <https://docs.nvidia.com/cuda/parallel-thread-execution/>`_
21 source.
22 """
23 name = 'PTX'
24 url = "https://docs.nvidia.com/cuda/parallel-thread-execution/"
25 filenames = ['*.ptx']
26 aliases = ['ptx']
27 mimetypes = ['text/x-ptx']
28 version_added = '2.16'
29
30 #: optional Comment or Whitespace
31 string = r'"[^"]*?"'
32 followsym = r'[a-zA-Z0-9_$]'
33 identifier = r'([-a-zA-Z$._][\w\-$.]*|' + string + ')'
34 block_label = r'(' + identifier + r'|(\d+))'
35
36 tokens = {
37 'root': [
38 include('whitespace'),
39
40 (block_label + r'\s*:', Name.Label),
41
42 include('keyword'),
43
44 (r'%' + identifier, Name.Variable),
45 (r'%\d+', Name.Variable.Anonymous),
46 (r'c?' + string, String),
47 (identifier, Name.Variable),
48 (r';', Punctuation),
49 (r'[*+-/]', Operator),
50
51 (r'0[xX][a-fA-F0-9]+', Number),
52 (r'-?\d+(?:[.]\d+)?(?:[eE][-+]?\d+(?:[.]\d+)?)?', Number),
53
54 (r'[=<>{}\[\]()*.,!]|x\b', Punctuation)
55
56 ],
57 'whitespace': [
58 (r'(\n|\s+)+', Whitespace),
59 (r'//.*?\n', Comment)
60 ],
61
62 'keyword': [
63 # Instruction keywords
64 (words((
65 'abs', 'discard', 'min', 'shf', 'vadd',
66 'activemask', 'div', 'mma', 'shfl', 'vadd2',
67 'add', 'dp2a', 'mov', 'shl', 'vadd4',
68 'addc', 'dp4a', 'movmatrix', 'shr', 'vavrg2',
69 'alloca', 'elect', 'mul', 'sin', 'vavrg4',
70 'and', 'ex2', 'mul24', 'slct', 'vmad',
71 'applypriority', 'exit', 'multimem', 'sqrt', 'vmax',
72 'atom', 'fence', 'nanosleep', 'st', 'vmax2',
73 'bar', 'fma', 'neg', 'stackrestore', 'vmax4',
74 'barrier', 'fns', 'not', 'stacksave', 'vmin',
75 'bfe', 'getctarank', 'or', 'stmatrix', 'vmin2',
76 'bfi', 'griddepcontrol', 'pmevent', 'sub', 'vmin4',
77 'bfind', 'isspacep', 'popc', 'subc', 'vote',
78 'bmsk', 'istypep', 'prefetch', 'suld', 'vset',
79 'bra', 'ld', 'prefetchu', 'suq', 'vset2',
80 'brev', 'ldmatrix', 'prmt', 'sured', 'vset4',
81 'brkpt', 'ldu', 'rcp', 'sust', 'vshl',
82 'brx', 'lg2', 'red', 'szext', 'vshr',
83 'call', 'lop3', 'redux', 'tanh', 'vsub',
84 'clz', 'mad', 'rem', 'testp', 'vsub2',
85 'cnot', 'mad24', 'ret', 'tex', 'vsub4',
86 'copysign', 'madc', 'rsqrt', 'tld4', 'wgmma',
87 'cos', 'mapa', 'sad', 'trap', 'wmma',
88 'cp', 'match', 'selp', 'txq', 'xor',
89 'createpolicy', 'max', 'set', 'vabsdiff', 'cvt',
90 'mbarrier', 'setmaxnreg', 'vabsdiff2', 'cvta',
91 'membar', 'setp', 'vabsdiff4')), Keyword),
92 # State Spaces and Suffixes
93 (words((
94 'reg', '.sreg', '.const', '.global',
95 '.local', '.param', '.shared', '.tex',
96 '.wide', '.loc'
97 )), Keyword.Pseudo),
98 # PTX Directives
99 (words((
100 '.address_size', '.explicitcluster', '.maxnreg', '.section',
101 '.alias', '.extern', '.maxntid', '.shared',
102 '.align', '.file', '.minnctapersm', '.sreg',
103 '.branchtargets', '.func', '.noreturn', '.target',
104 '.callprototype', '.global', '.param', '.tex',
105 '.calltargets', '.loc', '.pragma', '.version',
106 '.common', '.local', '.reg', '.visible',
107 '.const', '.maxclusterrank', '.reqnctapercluster', '.weak',
108 '.entry', '.maxnctapersm', '.reqntid')), Keyword.Reserved),
109 # Fundamental Types
110 (words((
111 '.s8', '.s16', '.s32', '.s64',
112 '.u8', '.u16', '.u32', '.u64',
113 '.f16', '.f16x2', '.f32', '.f64',
114 '.b8', '.b16', '.b32', '.b64',
115 '.pred'
116 )), Keyword.Type)
117 ],
118
119 }