ptx_parser/type/common.rs
1/* --------------------------------------------------- */
2/* -------------------- Basic Directives ------------- */
3/* --------------------------------------------------- */
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum CodeLinkage {
7 /// `.visible`
8 Visible,
9 /// `.extern`
10 Extern,
11 /// `.weak`
12 Weak,
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum DataLinkage {
17 /// `.visible`
18 Visible,
19 /// `.extern`
20 Extern,
21 /// `.weak`
22 Weak,
23 /// `.common`
24 Common,
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum CodeOrDataLinkage {
29 /// `.visible`
30 Visible,
31 /// `.extern`
32 Extern,
33 /// `.weak`
34 Weak,
35 /// `.common`
36 Common,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40pub enum TexType {
41 /// `.texref`
42 TexRef,
43 /// `.samplerref`
44 SamplerRef,
45 /// `.surfref`
46 SurfRef,
47}
48
49/// Memory spaces
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub enum AddressSpace {
52 Global,
53 Const,
54 Shared,
55 Local,
56 Param,
57 Reg,
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub enum AttributeDirective {
62 /// `.unified(uuid1, uuid2)`
63 Unified(u64, u64),
64 /// `.managed`
65 Managed,
66}
67
68#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69pub enum DataType {
70 /// `.u8`
71 U8,
72 /// `.u16`
73 U16,
74 /// `.u32`
75 U32,
76 /// `.u64`
77 U64,
78 /// `.s8`
79 S8,
80 /// `.s16`
81 S16,
82 /// `.s32`
83 S32,
84 /// `.s64`
85 S64,
86 /// `.f16`
87 F16,
88 /// `.f16x2`
89 F16x2,
90 /// `.f32`
91 F32,
92 /// `.f64`
93 F64,
94 /// `.b8`
95 B8,
96 /// `.b16`
97 B16,
98 /// `.b32`
99 B32,
100 /// `.b64`
101 B64,
102 /// `.b128`
103 B128,
104 /// `.pred`
105 Pred,
106}
107
108/* -------------------------------------------------- */
109/* -------------------- Math Basics ----------------- */
110/* -------------------------------------------------- */
111
112#[derive(Debug, Clone, Copy, PartialEq, Eq)]
113pub enum Sign {
114 Negative,
115 Positive,
116}
117
118/// Axis component for 3-component special registers (x/y/z)
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub enum Axis {
121 /// No axis component present
122 None,
123 X,
124 Y,
125 Z,
126}
127
128/* --------------------------------------------------- */
129/* -------------------- Label ------------------------ */
130/* --------------------------------------------------- */
131
132/// Label name (e.g., `L__label_1`).
133#[derive(Debug, Clone, PartialEq, Eq)]
134pub struct Label(pub String);
135
136/* --------------------------------------------------- */
137/* -------------------- Special Registers ------------ */
138/* --------------------------------------------------- */
139
140#[derive(Debug, Clone, Copy, PartialEq, Eq)]
141pub enum SpecialRegister {
142 /// `%aggr_smem_size`
143 AggrSmemSize,
144 /// `%dynamic_smem_size`
145 DynamicSmemSize,
146 /// `%lanemask_gt`
147 LanemaskGt,
148 /// `%reserved_smem_offset_begin`
149 ReservedSmemOffsetBegin,
150 /// `%clock`
151 Clock,
152 /// `%envreg0`, ..., `%envreg31`
153 Envreg(u8),
154 /// `%lanemask_le`
155 LanemaskLe,
156 /// `%reserved_smem_offset_cap`
157 ReservedSmemOffsetCap,
158 /// `%clock64`
159 Clock64,
160 /// `%globaltimer`
161 Globaltimer,
162 /// `%lanemask_lt`
163 LanemaskLt,
164 /// `%reserved_smem_offset_end`
165 ReservedSmemOffsetEnd,
166 /// `%cluster_ctaid` (optionally `.x/.y/.z`)
167 ClusterCtaid(Axis),
168 /// `%globaltimer_hi`
169 GlobaltimerHi,
170 /// `%nclusterid`
171 Nclusterid,
172 /// `%smid`
173 Smid,
174 /// `%cluster_ctarank` (optionally `.x/.y/.z`)
175 ClusterCtarank(Axis),
176 /// `%globaltimer_lo`
177 GlobaltimerLo,
178 /// `%nctaid` (optionally `.x/.y/.z`)
179 Nctaid(Axis),
180 /// `%tid` (optionally `.x/.y/.z`)
181 Tid(Axis),
182 /// `%cluster_nctaid` (optionally `.x/.y/.z`)
183 ClusterNctaid(Axis),
184 /// `%gridid`
185 Gridid,
186 /// `%nsmid`
187 Nsmid,
188 /// `%total_smem_size`
189 TotalSmemSize,
190 /// `%cluster_nctarank` (optionally `.x/.y/.z`)
191 ClusterNctarank(Axis),
192 /// `%is_explicit_cluster`
193 IsExplicitCluster,
194 /// `%ntid` (optionally `.x/.y/.z`)
195 Ntid(Axis),
196 /// `%warpid`
197 Warpid,
198 /// `%clusterid`
199 Clusterid,
200 /// `%laneid`
201 Laneid,
202 /// `%nwarpid`
203 Nwarpid,
204 /// `%WARPSZ`
205 WARPSZ,
206 /// `%ctaid` (optionally `.x/.y/.z`)
207 Ctaid(Axis),
208 /// `%lanemask_eq`
209 LanemaskEq,
210 /// `%pm0`, ..., `%pm7`
211 Pm(u8),
212 /// `%pm0_64`, ..., `%pm7_64`
213 Pm64(u8),
214 /// `%current_graph_exec`
215 CurrentGraphExec,
216 /// `%lanemask_ge`
217 LanemaskGe,
218 /// `%reserved_smem_offset_0`, `%reserved_smem_offset_1`
219 ReservedSmemOffset(u8),
220}
221
222/* --------------------------------------------------- */
223/* ------------------- Operands ---------------------- */
224/* --------------------------------------------------- */
225
226/// Texture handler with 2 operands, e.g. [%r1, %r2]
227#[derive(Debug, Clone, PartialEq, Eq)]
228pub struct TexHandler2(pub [GeneralOperand; 2]);
229
230/// Texture handler with optional sampler operand, e.g. `[tex, coords]` or `[tex, sampler, coords]`
231#[derive(Debug, Clone, PartialEq, Eq)]
232pub struct TexHandler3Optional {
233 pub handle: GeneralOperand,
234 pub sampler: Option<GeneralOperand>,
235 pub coords: GeneralOperand,
236}
237
238/// Texture handler with optional sampler operand, e.g. `[tex, coords]` or `[tex, sampler, coords]`
239#[derive(Debug, Clone, PartialEq, Eq)]
240pub struct TexHandler3 {
241 pub handle: GeneralOperand,
242 pub sampler: GeneralOperand,
243 pub coords: GeneralOperand,
244}
245
246#[derive(Debug, Clone, PartialEq, Eq)]
247pub enum GeneralOperand {
248 Vec(VectorOperand),
249 Single(Operand),
250}
251
252#[derive(Debug, Clone, PartialEq, Eq)]
253pub enum VectorOperand {
254 /// {%r1}
255 Vector1(Operand),
256 /// {%r1, %r2}
257 Vector2([Operand; 2]),
258 /// {%r1, %r2, %r3}
259 Vector3([Operand; 3]),
260 /// {%r1, %r2, %r3, %r4}
261 Vector4([Operand; 4]),
262 /// {%r1, %r2, %r3, %r4, %r5, %r6, %r7, %r8}
263 Vector8([Operand; 8]),
264}
265
266#[derive(Debug, Clone, PartialEq, Eq)]
267pub enum Operand {
268 /// %r1
269 Register(RegisterOperand),
270 /// 0xffff
271 Immediate(Immediate),
272 /// foo
273 Symbol(String),
274 /// foo + 4 (symbol + immediate offset)
275 SymbolOffset(String, Immediate),
276}
277
278/// Register operand starting with % (e.g., `%r1`).
279#[derive(Debug, Clone, PartialEq, Eq)]
280pub struct RegisterOperand(pub String);
281/// Predicate register names (e.g., `%p0`).
282#[derive(Debug, Clone, PartialEq, Eq)]
283pub struct PredicateRegister(pub String);
284
285/// Representation of an address operand.
286#[derive(Debug, Clone, PartialEq, Eq)]
287pub enum AddressOperand {
288 /// base[immIndex]
289 Array(VariableSymbol, Immediate),
290 /// Immediate address value, e.g., [0xffff], unsigned, 32-bit
291 ImmediateAddress(Immediate),
292 /// Offset address with optional displacement, e.g., [base + offset] and [base]
293 Offset(AddressBase, Option<AddressOffset>),
294}
295
296/// Base location referenced by an address expression.
297#[derive(Debug, Clone, PartialEq, Eq)]
298pub enum AddressBase {
299 Register(RegisterOperand),
300 Variable(VariableSymbol),
301}
302
303/// Specific adjustment applied within a displacement term.
304#[derive(Debug, Clone, PartialEq, Eq)]
305pub enum AddressOffset {
306 Register(RegisterOperand),
307 Immediate(Sign, Immediate),
308}
309
310/* --------------------------------------------------- */
311/* -------------------- Immediate -------------------- */
312/* --------------------------------------------------- */
313
314/// Immediate value represented as a string.
315/// TODO: Replace with appropriate numeric type later.
316#[derive(Debug, Clone, PartialEq, Eq)]
317pub struct Immediate(pub String);
318
319/* --------------------------------------------------- */
320/* -------------------- Symbols ---------------------- */
321/* --------------------------------------------------- */
322
323/// Function symbol
324#[derive(Debug, Clone, PartialEq, Eq)]
325pub struct FunctionSymbol(pub String);
326
327/// Variable symbol
328#[derive(Debug, Clone, PartialEq, Eq)]
329pub struct VariableSymbol(pub String);
330
331/* --------------------------------------------------- */
332/* -------------------- Predicate -------------------- */
333/* --------------------------------------------------- */
334
335/// Predicate guard for conditional instruction execution
336#[derive(Debug, Clone, PartialEq)]
337pub struct Predicate {
338 pub negated: bool,
339 pub operand: Operand,
340}
341
342/* --------------------------------------------------- */
343/* -------------------- Instruction ------------------ */
344/* --------------------------------------------------- */
345
346/// Represents a complete instruction with optional label and predicate guard
347/// Format: [label:] [@{!}pred] instruction
348#[derive(Debug, Clone, PartialEq)]
349pub struct Instruction {
350 pub label: Option<String>,
351 pub predicate: Option<Predicate>,
352 pub inst: crate::r#type::instruction::Inst,
353}