To protect your data, the CISO officer has suggested users to enable GitLab 2FA as soon as possible.

UIR.g4 9.42 KB
Newer Older
Kunshan Wang's avatar
Kunshan Wang committed
1
grammar UIR;
Kunshan Wang's avatar
Kunshan Wang committed
2
3

ir
Kunshan Wang's avatar
Kunshan Wang committed
4
    :   topLevelDef*
Kunshan Wang's avatar
Kunshan Wang committed
5
6
    ;

Kunshan Wang's avatar
Kunshan Wang committed
7
topLevelDef
Kunshan Wang's avatar
Kunshan Wang committed
8
9
10
11
12
13
14
15
16
    :   typeDef
    |   funcSigDef
    |   constDef
    |   globalDef
    |   funcDecl
    |   funcDef
    ;

typeDef
Kunshan Wang's avatar
Kunshan Wang committed
17
    :   '.typedef' nam=GLOBAL_NAME '=' ctor=typeConstructor
Kunshan Wang's avatar
Kunshan Wang committed
18
19
20
    ;

funcSigDef
Kunshan Wang's avatar
Kunshan Wang committed
21
    :   '.funcsig' nam=GLOBAL_NAME '=' ctor=funcSigConstructor
Kunshan Wang's avatar
Kunshan Wang committed
22
23
24
    ;

constDef
Kunshan Wang's avatar
Kunshan Wang committed
25
    :   '.const' nam=GLOBAL_NAME '<' ty=type '>' '=' ctor=constConstructor
Kunshan Wang's avatar
Kunshan Wang committed
26
27
28
    ;
    
globalDef
Kunshan Wang's avatar
Kunshan Wang committed
29
    :   '.global' nam=GLOBAL_NAME '<' ty=type '>'
Kunshan Wang's avatar
Kunshan Wang committed
30
31
32
    ;

funcDecl
Kunshan Wang's avatar
Kunshan Wang committed
33
    :   '.funcdecl' nam=GLOBAL_NAME '<' sig=funcSig '>'
Kunshan Wang's avatar
Kunshan Wang committed
34
35
36
    ;
    
funcDef
Kunshan Wang's avatar
Kunshan Wang committed
37
    :   '.funcdef' nam=GLOBAL_NAME 'VERSION' ver=GLOBAL_NAME '<' sig=funcSig '>' params=paramList body=funcBody
Kunshan Wang's avatar
Kunshan Wang committed
38
39
40
    ;

typeConstructor
Kunshan Wang's avatar
Kunshan Wang committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    :   'int' '<' length=intLiteral '>'                         # TypeInt
    |   'float'                                                 # TypeFloat
    |   'double'                                                # TypeDouble
    |   'ref' '<' type '>'                                      # TypeRef
    |   'iref' '<' type '>'                                     # TypeIRef
    |   'weakref' '<' type '>'                                  # TypeWeakRef
    |   'struct' '<' type+ '>'                                  # TypeStruct
    |   'array' '<' type length=intLiteral '>'                  # TypeArray
    |   'hybrid' '<' fixedTy=type varTy=type '>'                # TypeHybrid
    |   'void'                                                  # TypeVoid
    |   'func' '<' funcSig '>'                                  # TypeFunc
    |   'thread'                                                # TypeThread
    |   'stack'                                                 # TypeStack
    |   'tagref64'                                              # TypeTagRef64
    |   'vector' '<' type length=intLiteral '>'                 # TypeVector
Kunshan Wang's avatar
Kunshan Wang committed
56
57
58
    ;

funcSigConstructor
Kunshan Wang's avatar
Kunshan Wang committed
59
    :   retTy=type '(' (paramTy+=type*) ')'
Kunshan Wang's avatar
Kunshan Wang committed
60
61
    ;

Kunshan Wang's avatar
Kunshan Wang committed
62
63
64
65
constConstructor
    :   intLiteral                  # ConstInt
    |   floatLiteral                # ConstFloat
    |   doubleLiteral               # ConstDouble
Kunshan Wang's avatar
Kunshan Wang committed
66
    |   '{' GLOBAL_NAME* '}'        # ConstStruct
Kunshan Wang's avatar
Kunshan Wang committed
67
68
    |   'NULL'                      # ConstNull
    |   'VEC' '{' constant* '}'     # ConstVector
Kunshan Wang's avatar
Kunshan Wang committed
69
70
    ;

Kunshan Wang's avatar
Kunshan Wang committed
71
72
type
    :   GLOBAL_NAME
Kunshan Wang's avatar
Kunshan Wang committed
73
74
    ;

Kunshan Wang's avatar
Kunshan Wang committed
75
76
funcSig
    :   GLOBAL_NAME
Kunshan Wang's avatar
Kunshan Wang committed
77
78
    ;

Kunshan Wang's avatar
Kunshan Wang committed
79
80
constant
    :   GLOBAL_NAME
Kunshan Wang's avatar
Kunshan Wang committed
81
82
    ;

Kunshan Wang's avatar
Kunshan Wang committed
83
84
paramList
    :   '(' name* ')'
Kunshan Wang's avatar
Kunshan Wang committed
85
86
    ;

Kunshan Wang's avatar
Kunshan Wang committed
87
88
funcBody
    :   '{' basicBlock* '}'
Kunshan Wang's avatar
Kunshan Wang committed
89
90
    ;

Kunshan Wang's avatar
Kunshan Wang committed
91
basicBlock
Kunshan Wang's avatar
Kunshan Wang committed
92
93
94
95
    :   label inst+
    ;

label
Kunshan Wang's avatar
Kunshan Wang committed
96
    :   name ':'
Kunshan Wang's avatar
Kunshan Wang committed
97
98
99
    ;

inst
Kunshan Wang's avatar
Kunshan Wang committed
100
    :   (name '=')? instBody
Kunshan Wang's avatar
Kunshan Wang committed
101
102
103
104
    ;

instBody
    // Integer/FP Arithmetic
Kunshan Wang's avatar
Kunshan Wang committed
105
    :   binop '<' type '>' op1=value op2=value excClause                # InstBinOp
Kunshan Wang's avatar
Kunshan Wang committed
106
107

    // Integer/FP Comparison
Kunshan Wang's avatar
Kunshan Wang committed
108
    |   cmpop '<' type '>' op1=value op2=value                          # InstCmp
Kunshan Wang's avatar
Kunshan Wang committed
109
110

    // Conversions
Kunshan Wang's avatar
Kunshan Wang committed
111
    |   convop  '<' fromTy=type toTy=type '>' opnd=value                # InstConversion
Kunshan Wang's avatar
Kunshan Wang committed
112
113
    
    // Select
Kunshan Wang's avatar
Kunshan Wang committed
114
    |   'SELECT' '<' condTy=type resTy=type '>' cond=value ifTrue=value ifFalse=value       # InstSelect
Kunshan Wang's avatar
Kunshan Wang committed
115
116

    // Intra-function Control Flow
Kunshan Wang's avatar
Kunshan Wang committed
117
118
119
120
    |   'BRANCH' bbName                                                 # InstBranch
    |   'BRANCH2' cond=value ifTrue=bbName ifFalse=bbName               # InstBranch2
    |   'SWITCH' '<' type '>' opnd=value defDest=bbName '{'
            (caseVal=value ':' caseDest=bbName ';')* '}'                # InstSwitch
Kunshan Wang's avatar
Kunshan Wang committed
121
    |   'PHI' '<' type '>' '{'
Kunshan Wang's avatar
Kunshan Wang committed
122
            (caseSrc=bbName ':' caseVal=value ';')* '}'                 # InstPhi
Kunshan Wang's avatar
Kunshan Wang committed
123
124

    // Inter-function Control Flow
Kunshan Wang's avatar
Kunshan Wang committed
125
    |   'CALL' funcCallBody excClause keepAliveClause                   # InstCall
Kunshan Wang's avatar
Kunshan Wang committed
126
127
    |   'TAILCALL' funcCallBody                     # InstTailCall

Kunshan Wang's avatar
Kunshan Wang committed
128
    |   'RET' '<' type '>' retVal=value             # InstRet
Kunshan Wang's avatar
Kunshan Wang committed
129
    |   'RETVOID'                                   # InstRetVoid
Kunshan Wang's avatar
Kunshan Wang committed
130
    |   'THROW' exc=value                           # InstThrow
Kunshan Wang's avatar
Kunshan Wang committed
131
132
133
    |   'LANDINGPAD'                                # InstLandingPad

    // Aggregate Operations
Kunshan Wang's avatar
Kunshan Wang committed
134
135
136
137
138
    |   'EXTRACTVALUE' '<' type intLiteral '>' opnd=value               # InstExtractValue
    |   'INSERTVALUE' '<' type intLiteral '>' opnd=value newVal=value   # InstInsertValue
    |   'EXTRACTELEMENT' '<' vecTy=type indTy=type '>' opnd=value index=value                           # InstExtractElement
    |   'INSERTELEMENT' '<' vecTy=type indTy=type '>' opnd=value index=value opnd=value newVal=value    # InstInsertElement
    |   'SHUFFLEVECTOR' '<' vecTy=type maskTy=type '>' vec1=value vec2=value mask=value                 # InstShuffleVector
Kunshan Wang's avatar
Kunshan Wang committed
139
140

    // Memory Operations
Kunshan Wang's avatar
Kunshan Wang committed
141
142
143
144
    |   'NEW'           '<' allocTy=type '>' excClause                              # InstNew
    |   'NEWHYBRID'     '<' allocTy=type lenTy=type '>' length=value excClause      # InstNewHybrid
    |   'ALLOCA'        '<' allocTy=type '>' excClause                              # InstAlloca
    |   'ALLOCAHYBRID'  '<' allocTy=type lenTy=type '>' length=value excClause      # InstAllocaHybrid
Kunshan Wang's avatar
Kunshan Wang committed
145
    
Kunshan Wang's avatar
Kunshan Wang committed
146
    |   'GETIREF'       '<' refTy=type '>' opnd=value                               # InstGetIRef
Kunshan Wang's avatar
Kunshan Wang committed
147

Kunshan Wang's avatar
Kunshan Wang committed
148
149
150
151
152
    |   'GETFIELDIREF'  '<' refTy=type index=intLiteral '>' opnd=value              # InstGetFieldIRef
    |   'GETELEMIREF'   '<' refTy=type indTy=type '>' opnd=value index=value        # InstGetElemIRef
    |   'SHIFTIREF'     '<' refTy=type offTy=type '>' opnd=value offset=value       # InstShiftIRef
    |   'GETFIXEDPARTIREF'  '<' refTy=type '>' opnd=value                           # InstGetFixedPartIRef
    |   'GETVARPARTIREF'    '<' refTy=type '>' opnd=value                           # InstGetVarPartIRef
Kunshan Wang's avatar
Kunshan Wang committed
153
    
Kunshan Wang's avatar
Kunshan Wang committed
154
155
156
157
158
    |   'LOAD' memord? '<' type '>' loc=value excClause                             # InstLoad
    |   'STORE' memord? '<' type '>' loc=value newVal=value excClause               # InstStore
    |   'CMPXCHG' (isWeak='WEAK'?) ordSucc=memord ordFail=memord
                    '<' type '>' loc=value expected=value desired=value excClause   # InstCmpXchg
    |   'ATOMICRMW' memord atomicrmwop '<' type '>' loc=value opnd=value excClause  # InstAtomicRMW
Kunshan Wang's avatar
Kunshan Wang committed
159

Kunshan Wang's avatar
Kunshan Wang committed
160
    |   'FENCE' memord                                                              # InstFence
Kunshan Wang's avatar
Kunshan Wang committed
161
162

    // Trap
Kunshan Wang's avatar
Kunshan Wang committed
163
164
165
    |   'TRAP' '<' type '>' excClause keepAliveClause                               # InstTrap
    |   'WATCHPOINT' wpid=intLiteral '<' type '>'
            dis=bbName ena=bbName ('WPEXC' '(' wpExc=bbName ')')? keepAliveClause   # InstWatchPoint
Kunshan Wang's avatar
Kunshan Wang committed
166
167

    // Foreign Function Interface
Kunshan Wang's avatar
Kunshan Wang committed
168
    |   'CCALL' '<' funcTy=type funcSig '>' callconv funcCallBody      # InstCCall
Kunshan Wang's avatar
Kunshan Wang committed
169
170

    // Thread and Stack Operations
Kunshan Wang's avatar
Kunshan Wang committed
171
172
    |   'NEWSTACK' funcCallBody excClause                                                   # InstNewStack
    |   'SWAPSTACK' swappee=value curStackClause newStackClause excClause keepAliveClause   # InstSwapStack
Kunshan Wang's avatar
Kunshan Wang committed
173

Kunshan Wang's avatar
Kunshan Wang committed
174
175
    // Common Instructions
    |   'COMMINST' typeList? argList? excClause keepAliveClause     # InstCommInst
Kunshan Wang's avatar
Kunshan Wang committed
176
177
    ;

Kunshan Wang's avatar
Kunshan Wang committed
178
179
bbName
    :   name
Kunshan Wang's avatar
Kunshan Wang committed
180
181
    ;

Kunshan Wang's avatar
Kunshan Wang committed
182
183
value
    :   name
Kunshan Wang's avatar
Kunshan Wang committed
184
185
    ;

Kunshan Wang's avatar
Kunshan Wang committed
186
187
funcCallBody
    :   '<' funcSig '>' callee=value argList
Kunshan Wang's avatar
Kunshan Wang committed
188
189
    ;

Kunshan Wang's avatar
Kunshan Wang committed
190
191
192
excClause
    :   ('EXC' '(' nor=bbName exc=bbName ')')?
    ;
Kunshan Wang's avatar
Kunshan Wang committed
193

Kunshan Wang's avatar
Kunshan Wang committed
194
195
196
keepAliveClause
    :   ('KEEPALIVE' '(' value* ')')?
    ;
Kunshan Wang's avatar
Kunshan Wang committed
197

Kunshan Wang's avatar
Kunshan Wang committed
198
199
typeList
    :   '<' type* '>'
Kunshan Wang's avatar
Kunshan Wang committed
200
    ;
Kunshan Wang's avatar
Kunshan Wang committed
201
202
203
204
205
206
207
208

argList
    :   '(' value* ')'
    ;

curStackClause
    :   'RET_WITH' '<' type '>'     # CurStackRetWith
    |   'KILL_OLD'                  # CurStackKillOld
Kunshan Wang's avatar
Kunshan Wang committed
209
210
    ;

Kunshan Wang's avatar
Kunshan Wang committed
211
212
213
214
215
newStackClause
    :   'PASS_PARAM' '<' type '>' value     # NewStackPassParam
    |   'NO_PARAM'                          # NewStackNoParam
    |   'THROW_EXC' exc=value               # NewStackThrowExc
    ;
Kunshan Wang's avatar
Kunshan Wang committed
216

Kunshan Wang's avatar
Kunshan Wang committed
217
218
219
binop
    : 'ADD' | 'SUB' | 'MUL' | 'UDIV' | 'SDIV' | 'UREM' | 'SREM' | 'SHL' | 'LSHR' | 'ASHR' | 'AND' | 'OR' | 'XOR'
    | 'FADD'| 'FSUB' | 'FMUL' | 'FDIV' | 'FREM'
Kunshan Wang's avatar
Kunshan Wang committed
220
221
    ;

Kunshan Wang's avatar
Kunshan Wang committed
222
223
224
cmpop
    : 'EQ' | 'NE' | 'SGT' | 'SLT' | 'SGE' | 'SLE' | 'UGT' | 'ULT' | 'UGE' | 'ULE'
    | 'FTRUE' | 'FFALSE'
Kunshan Wang's avatar
Kunshan Wang committed
225
226
227
228
    | 'FUNO' | 'FUEQ' | 'FUNE' | 'FUGT' | 'FULT' | 'FUGE' | 'FULE'
    | 'FORD' | 'FOEQ' | 'FONE' | 'FOGT' | 'FOLT' | 'FOGE' | 'FOLE'
    ;
    
Kunshan Wang's avatar
Kunshan Wang committed
229
convop
Kunshan Wang's avatar
Kunshan Wang committed
230
    : 'TRUNC' | 'ZEXT' | 'SEXT' | 'FPTRUNC' | 'FPEXT'
Kunshan Wang's avatar
Kunshan Wang committed
231
232
    | 'FPTOUI' | 'FPTOSI' | 'UITOFP' | 'SITOFP'
    | 'BITCAST' | 'REFCAST'
Kunshan Wang's avatar
Kunshan Wang committed
233
234
    ;

Kunshan Wang's avatar
Kunshan Wang committed
235
236
memord
    : 'NOT_ATOMIC' | 'RELAXED' | 'CONSUME' | 'ACQUIRE' | 'RELEASE' | 'ACQ_REL' | 'SEQ_CST'
Kunshan Wang's avatar
Kunshan Wang committed
237
238
239
    ;

atomicrmwop
Kunshan Wang's avatar
Kunshan Wang committed
240
    : 'XCHG' | 'ADD' | 'SUB' | 'AND' | 'NAND' | 'OR' | 'XOR' | 'MAX' | 'MIN' | 'UMAX' | 'UMIN'
Kunshan Wang's avatar
Kunshan Wang committed
241
    ;
Kunshan Wang's avatar
Kunshan Wang committed
242
243
244

callconv
    :   'DEFAULT'
Kunshan Wang's avatar
Kunshan Wang committed
245
246
247
    ;

intLiteral
Kunshan Wang's avatar
Kunshan Wang committed
248
249
250
    :   INT_DEC
    |   INT_OCT
    |   INT_HEX
Kunshan Wang's avatar
Kunshan Wang committed
251
252
253
254
255
256
257
258
    ;

floatLiteral
    :   FP_NUM 'f'  # FloatNumber
    |   INF 'f'     # FloatInf
    |   NAN 'f'     # FloatNan
    |   'bitsf' '(' intLiteral ')'   # FloatBits
    ;
Kunshan Wang's avatar
Kunshan Wang committed
259

Kunshan Wang's avatar
Kunshan Wang committed
260
261
262
263
264
265
266
doubleLiteral
    :   FP_NUM 'd'  # DoubleNumber
    |   INF 'd'     # DoubleInf
    |   NAN 'd'     # DoubleNan
    |   'bitsd' '(' intLiteral ')'   # DoubleBits
    ;

Kunshan Wang's avatar
Kunshan Wang committed
267
268
269
name
    :   GLOBAL_NAME
    |   LOCAL_NAME
Kunshan Wang's avatar
Kunshan Wang committed
270
271
272
273
274
275
276
277
278
    ;

// LEXER

INT_DEC
    :   ('+'|'-')? DIGIT_NON_ZERO DIGIT*
    ;
    
INT_OCT
Kunshan Wang's avatar
Kunshan Wang committed
279
    :   ('+'|'-')? '0' OCT_DIGIT*
Kunshan Wang's avatar
Kunshan Wang committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
    ;

INT_HEX
    :   ('+'|'-')? '0x' HEX_DIGIT+
    ;
    
FP_NUM
    :   ('+'|'-')? DIGIT+ '.' DIGIT+ ('e' ('+'|'-')? DIGIT+)?
    ;

INF
    :   ('+'|'-') 'inf'
    ;
    
NAN
    :   'nan'
    ;

Kunshan Wang's avatar
Kunshan Wang committed
298
299
GLOBAL_NAME
    :   GLOBAL_NAME_PREFIX IDCHAR+
Kunshan Wang's avatar
Kunshan Wang committed
300
301
    ;

Kunshan Wang's avatar
Kunshan Wang committed
302
303
LOCAL_NAME
    :   LOCAL_NAME_PREFIX IDCHAR+
Kunshan Wang's avatar
Kunshan Wang committed
304
305
306
307
308
309
310
311
312
313
314
315
    ;

fragment
DIGIT
    :   [0-9]
    ;

fragment
DIGIT_NON_ZERO
    :   [1-9]
    ;

Kunshan Wang's avatar
Kunshan Wang committed
316
317
318
319
320
fragment
OCT_DIGIT
    :   [0-7]
    ;

Kunshan Wang's avatar
Kunshan Wang committed
321
322
323
324
325
326
fragment
HEX_DIGIT
    :   [0-9a-fA-F]
    ;

fragment
Kunshan Wang's avatar
Kunshan Wang committed
327
GLOBAL_NAME_PREFIX: '@';
Kunshan Wang's avatar
Kunshan Wang committed
328
329

fragment
Kunshan Wang's avatar
Kunshan Wang committed
330
LOCAL_NAME_PREFIX: '%';
Kunshan Wang's avatar
Kunshan Wang committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

fragment
IDCHAR
    :   [a-z]
    |   [A-Z]
    |   [0-9]
    |   '-'
    |   '_'
    |   '.'
    ;

WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

LINE_COMMENT
    :   '//' ~[\r\n]* -> skip
    ;