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

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

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

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

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

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

funcDecl
Kunshan Wang's avatar
Kunshan Wang committed
34
    :   '.funcdecl' nam=globalName '<' sig=funcSig '>'
35 36 37
    ;
    
funcDef
Kunshan Wang's avatar
Kunshan Wang committed
38
    :   '.funcdef' nam=globalName 'VERSION' ver=name '<' sig=funcSig '>' body=funcBody
39
    ;
40
    
Kunshan Wang's avatar
Kunshan Wang committed
41
funcExpDef
Kunshan Wang's avatar
Kunshan Wang committed
42
    :   '.expose' nam=globalName '=' func callConv=flag cookie=constant
43
    ;
44 45

typeConstructor
Kunshan Wang's avatar
Kunshan Wang committed
46
    :   'int'    	'<' length=intLiteral '>'                	# TypeInt
Kunshan Wang's avatar
Kunshan Wang committed
47 48
    |   'float'                                                 # TypeFloat
    |   'double'                                                # TypeDouble
Kunshan Wang's avatar
Kunshan Wang committed
49 50 51 52 53 54 55
    |   'uptr'      '<' ty=type '>'                             # TypeUPtr
    |   'ufuncptr'  '<' funcSig '>'                             # TypeUFuncPtr
    |   'struct'    '<' fieldTys+=type+ '>'                     # TypeStruct
    |   'hybrid'    '<' fieldTys+=type* varTy=type '>'          # TypeHybrid
    |   'array'     '<' ty=type length=intLiteral '>'           # TypeArray
    |   'vector'    '<' ty=type length=intLiteral '>'           # TypeVector
    |   'void'                                                  # TypeVoid
Kunshan Wang's avatar
Kunshan Wang committed
56 57 58
    |   'ref'	 	'<' ty=type '>'                             # TypeRef
    |   'iref' 		'<' ty=type '>'                             # TypeIRef
    |   'weakref' 	'<' ty=type '>'                             # TypeWeakRef
Kunshan Wang's avatar
Kunshan Wang committed
59
    |   'tagref64'                                              # TypeTagRef64
Kunshan Wang's avatar
Kunshan Wang committed
60 61 62
    |   'funcref' 	'<' funcSig '>'                             # TypeFuncRef
    |   'threadref'                                             # TypeThreadRef
    |   'stackref'                                              # TypeStackRef
Kunshan Wang's avatar
Kunshan Wang committed
63
    |   'framecursorref'                                        # TypeFrameCursorRef
64
    |   'irbuilderref'                                          # TypeIRBuilderRef
65 66 67
    ;

funcSigConstructor
68
    :   '(' paramTys+=type* ')' '->' '(' retTys+=type* ')'
69 70
    ;

Kunshan Wang's avatar
Kunshan Wang committed
71
constConstructor
Kunshan Wang's avatar
Kunshan Wang committed
72 73 74 75
    :   intLiteral                  # CtorInt
    |   floatLiteral                # CtorFloat
    |   doubleLiteral               # CtorDouble
    |   'NULL'                      # CtorNull
Kunshan Wang's avatar
Kunshan Wang committed
76 77
    |   '{' globalVar* '}'          # CtorList
    |   'EXTERN' symbol=stringLiteral     # CtorExtern
78 79
    ;

Kunshan Wang's avatar
Kunshan Wang committed
80
type
Kunshan Wang's avatar
Kunshan Wang committed
81
    :   globalName
82 83
    ;

Kunshan Wang's avatar
Kunshan Wang committed
84
funcSig
Kunshan Wang's avatar
Kunshan Wang committed
85
    :   globalName
86 87
    ;

Kunshan Wang's avatar
Kunshan Wang committed
88
constant
Kunshan Wang's avatar
Kunshan Wang committed
89
    :   globalName
90 91
    ;

Kunshan Wang's avatar
Kunshan Wang committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
globalCell
    :   globalName
    ;

func
    :   globalName
    ;

funcVer
    :   globalName
    ;

expFunc
    :   globalName
    ;
    
globalVar
    :   globalName
110 111
    ;

Kunshan Wang's avatar
Kunshan Wang committed
112
funcBody
113
    :   '{' basicBlock+ '}'
114 115
    ;

Kunshan Wang's avatar
Kunshan Wang committed
116
basicBlock
117 118 119 120
    :   label inst+
    ;

label
Kunshan Wang's avatar
Kunshan Wang committed
121
    :   name '(' bbParam* ')' excParam? ':'
122
    ;
Kunshan Wang's avatar
Kunshan Wang committed
123 124
	
bbParam
125
	: 	'<' ty=type '>' name
Kunshan Wang's avatar
Kunshan Wang committed
126 127 128 129 130
	;
	
excParam
	: 	'[' name ']'
	;
131 132 133 134 135
	
instResults
    :   results+=name
    |   '(' results+=name* ')'
    ;
136 137

inst
138
    :   (instResults '=')? ('[' name ']')? instBody
139 140 141 142
    ;

instBody
    // Integer/FP Arithmetic
143
    :   binop (flagList)? '<' ty=type '>' op1=value op2=value excClause                # InstBinOp
144 145

    // Integer/FP Comparison
146
    |   cmpop '<' ty=type '>' op1=value op2=value                          # InstCmp
147 148

    // Conversions
Kunshan Wang's avatar
Kunshan Wang committed
149
    |   convop  '<' fromTy=type toTy=type '>' opnd=value                # InstConversion
150 151
    
    // Select
Kunshan Wang's avatar
Kunshan Wang committed
152
    |   'SELECT' '<' condTy=type resTy=type '>' cond=value ifTrue=value ifFalse=value       # InstSelect
153 154

    // Intra-function Control Flow
Kunshan Wang's avatar
Kunshan Wang committed
155 156
    |   'BRANCH' dest=destClause                                        # InstBranch
    |   'BRANCH2' cond=value ifTrue=destClause ifFalse=destClause       # InstBranch2
157
    |   'SWITCH' '<' ty=type '>' opnd=value defDest=destClause '{'
158
            (caseVal+=constant caseDest+=destClause )* '}'              # InstSwitch
159 160

    // Inter-function Control Flow
Kunshan Wang's avatar
Kunshan Wang committed
161
    |   'CALL' funcCallBody excClause keepaliveClause                   # InstCall
162 163
    |   'TAILCALL' funcCallBody                     # InstTailCall

164
    |   'RET' retVals                               # InstRet
Kunshan Wang's avatar
Kunshan Wang committed
165
    |   'THROW' exc=value                           # InstThrow
166 167

    // Aggregate Operations
Kunshan Wang's avatar
Kunshan Wang committed
168 169 170 171 172
    |   'EXTRACTVALUE' 		'<' ty=type intLiteral '>' 		opnd=value               				# InstExtractValue
    |   'INSERTVALUE' 		'<' ty=type intLiteral '>' 		opnd=value newVal=value   				# InstInsertValue
    |   'EXTRACTELEMENT' 	'<' seqTy=type indTy=type '>' 	opnd=value index=value                  # InstExtractElement
    |   'INSERTELEMENT' 	'<' seqTy=type indTy=type '>' 	opnd=value index=value newVal=value     # InstInsertElement
    |   'SHUFFLEVECTOR' 	'<' vecTy=type maskTy=type '>' 	vec1=value vec2=value  mask=value       # InstShuffleVector
173 174

    // Memory Operations
Kunshan Wang's avatar
Kunshan Wang committed
175 176 177 178
    |   '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
179
    
Kunshan Wang's avatar
Kunshan Wang committed
180
    |   'GETIREF'       '<' refTy=type '>' opnd=value                               # InstGetIRef
181

182 183 184 185
    |   'GETFIELDIREF'      (ptr='PTR'?) '<' refTy=type index=intLiteral '>' opnd=value          # InstGetFieldIRef
    |   'GETELEMIREF'       (ptr='PTR'?) '<' refTy=type indTy=type '>' opnd=value index=value    # InstGetElemIRef
    |   'SHIFTIREF'         (ptr='PTR'?) '<' refTy=type offTy=type '>' opnd=value offset=value   # InstShiftIRef
    |   'GETVARPARTIREF'    (ptr='PTR'?) '<' refTy=type '>' opnd=value                           # InstGetVarPartIRef
186
    
187 188 189 190 191
    |   'LOAD'      (ptr='PTR'?) memord? '<' type '>' loc=value excClause                        # InstLoad
    |   'STORE'     (ptr='PTR'?) memord? '<' type '>' loc=value newVal=value excClause           # InstStore
    |   'CMPXCHG'   (ptr='PTR'?) (isWeak='WEAK'?) ordSucc=memord ordFail=memord
                    '<' type '>' loc=value expected=value desired=value excClause                # InstCmpXchg
    |   'ATOMICRMW' (ptr='PTR'?) memord atomicrmwop '<' type '>' loc=value opnd=value excClause  # InstAtomicRMW
192

Kunshan Wang's avatar
Kunshan Wang committed
193
    |   'FENCE' memord                                                              # InstFence
194 195

    // Trap
Kunshan Wang's avatar
Kunshan Wang committed
196
    |   'TRAP' typeList excClause keepaliveClause                               # InstTrap
197
    |   'WATCHPOINT' wpid=intLiteral typeList
Kunshan Wang's avatar
Kunshan Wang committed
198
            dis=destClause ena=destClause ('WPEXC' '(' wpExc=destClause ')')? keepaliveClause    # InstWatchPoint
Kunshan Wang's avatar
Kunshan Wang committed
199
    |   'WPBRANCH' wpid=intLiteral dis=destClause ena=destClause                # InstWPBranch
200 201

    // Foreign Function Interface
Kunshan Wang's avatar
Kunshan Wang committed
202
    |   'CCALL' callConv=flag '<' funcTy=type funcSig '>' callee=value argList excClause keepaliveClause   # InstCCall
203 204

    // Thread and Stack Operations
Kunshan Wang's avatar
Kunshan Wang committed
205
    |   'NEWTHREAD' stack=value threadLocalClause? newStackClause excClause                 # InstNewThread
Kunshan Wang's avatar
Kunshan Wang committed
206
    |   'SWAPSTACK' swappee=value curStackClause newStackClause excClause keepaliveClause   # InstSwapStack
207

Kunshan Wang's avatar
Kunshan Wang committed
208
    // Common Instructions
Kunshan Wang's avatar
Kunshan Wang committed
209
    |   'COMMINST' nam=globalName flagList? typeList? funcSigList? argList? excClause keepaliveClause     # InstCommInst
210
    ;
211 212 213 214 215
    
retVals
    :   '(' vals+=value* ')'
    |   vals+=value
    ;
216

Kunshan Wang's avatar
Kunshan Wang committed
217 218
destClause
    :   bb argList
219
    ;
Kunshan Wang's avatar
Kunshan Wang committed
220 221 222 223
    
bb
	:	name
	;
224

Kunshan Wang's avatar
Kunshan Wang committed
225 226
value
    :   name
227 228
    ;

Kunshan Wang's avatar
Kunshan Wang committed
229 230
funcCallBody
    :   '<' funcSig '>' callee=value argList
231 232
    ;

Kunshan Wang's avatar
Kunshan Wang committed
233
excClause
Kunshan Wang's avatar
Kunshan Wang committed
234
    :   ('EXC' '(' nor=destClause exc=destClause ')')?
Kunshan Wang's avatar
Kunshan Wang committed
235
    ;
236

Kunshan Wang's avatar
Kunshan Wang committed
237
keepaliveClause
Kunshan Wang's avatar
Kunshan Wang committed
238 239
    :   ('KEEPALIVE' '(' value* ')')?
    ;
Kunshan Wang's avatar
Kunshan Wang committed
240 241 242 243
    
threadLocalClause
    :   ('THREADLOCAL' '(' value ')')
    ;
244

245
flagList
246
    :   '[' flag* ']'
247 248
    ;

Kunshan Wang's avatar
Kunshan Wang committed
249 250
typeList
    :   '<' type* '>'
251
    ;
Kunshan Wang's avatar
Kunshan Wang committed
252

253 254 255 256
funcSigList
    :   '<[' funcSig* ']>'
    ;

Kunshan Wang's avatar
Kunshan Wang committed
257 258 259 260 261
argList
    :   '(' value* ')'
    ;

curStackClause
262 263
    :   'RET_WITH' typeList             # CurStackRetWith
    |   'KILL_OLD'                      # CurStackKillOld
264 265
    ;

Kunshan Wang's avatar
Kunshan Wang committed
266
newStackClause
267
    :   'PASS_VALUES' typeList argList      # NewStackPassValue
Kunshan Wang's avatar
Kunshan Wang committed
268 269
    |   'THROW_EXC' exc=value               # NewStackThrowExc
    ;
270

Kunshan Wang's avatar
Kunshan Wang committed
271 272 273
binop
    : 'ADD' | 'SUB' | 'MUL' | 'UDIV' | 'SDIV' | 'UREM' | 'SREM' | 'SHL' | 'LSHR' | 'ASHR' | 'AND' | 'OR' | 'XOR'
    | 'FADD'| 'FSUB' | 'FMUL' | 'FDIV' | 'FREM'
274 275
    ;

Kunshan Wang's avatar
Kunshan Wang committed
276 277 278
cmpop
    : 'EQ' | 'NE' | 'SGT' | 'SLT' | 'SGE' | 'SLE' | 'UGT' | 'ULT' | 'UGE' | 'ULE'
    | 'FTRUE' | 'FFALSE'
279 280 281 282
    | 'FUNO' | 'FUEQ' | 'FUNE' | 'FUGT' | 'FULT' | 'FUGE' | 'FULE'
    | 'FORD' | 'FOEQ' | 'FONE' | 'FOGT' | 'FOLT' | 'FOGE' | 'FOLE'
    ;
    
Kunshan Wang's avatar
Kunshan Wang committed
283
convop
284
    : 'TRUNC' | 'ZEXT' | 'SEXT' | 'FPTRUNC' | 'FPEXT'
Kunshan Wang's avatar
Kunshan Wang committed
285
    | 'FPTOUI' | 'FPTOSI' | 'UITOFP' | 'SITOFP'
286
    | 'BITCAST' | 'REFCAST' | 'PTRCAST'
287 288
    ;

Kunshan Wang's avatar
Kunshan Wang committed
289 290
memord
    : 'NOT_ATOMIC' | 'RELAXED' | 'CONSUME' | 'ACQUIRE' | 'RELEASE' | 'ACQ_REL' | 'SEQ_CST'
291 292 293
    ;

atomicrmwop
Kunshan Wang's avatar
Kunshan Wang committed
294
    : 'XCHG' | 'ADD' | 'SUB' | 'AND' | 'NAND' | 'OR' | 'XOR' | 'MAX' | 'MIN' | 'UMAX' | 'UMIN'
295
    ;
Kunshan Wang's avatar
Kunshan Wang committed
296

297
flag
298
    :   FLAG
299 300
    ;

301
intLiteral
Kunshan Wang's avatar
Kunshan Wang committed
302 303 304
    :   INT_DEC
    |   INT_OCT
    |   INT_HEX
305 306 307 308 309 310 311 312
    ;

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

314 315 316 317 318 319
doubleLiteral
    :   FP_NUM 'd'  # DoubleNumber
    |   INF 'd'     # DoubleInf
    |   NAN 'd'     # DoubleNan
    |   'bitsd' '(' intLiteral ')'   # DoubleBits
    ;
Kunshan Wang's avatar
Kunshan Wang committed
320 321 322 323
    
stringLiteral
    :   STRING_LITERAL
    ;
324

Kunshan Wang's avatar
Kunshan Wang committed
325
name
Kunshan Wang's avatar
Kunshan Wang committed
326 327
    :   globalName
    |   localName
328 329
    ;

Kunshan Wang's avatar
Kunshan Wang committed
330 331 332
globalName : GLOBAL_NAME;
localName : LOCAL_NAME;

333 334 335 336 337 338 339
// LEXER

INT_DEC
    :   ('+'|'-')? DIGIT_NON_ZERO DIGIT*
    ;
    
INT_OCT
Kunshan Wang's avatar
Kunshan Wang committed
340
    :   ('+'|'-')? '0' OCT_DIGIT*
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    ;

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

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

Kunshan Wang's avatar
Kunshan Wang committed
359 360
GLOBAL_NAME
    :   GLOBAL_NAME_PREFIX IDCHAR+
361 362
    ;

Kunshan Wang's avatar
Kunshan Wang committed
363 364
LOCAL_NAME
    :   LOCAL_NAME_PREFIX IDCHAR+
365
    ;
366 367 368 369
    
FLAG
    :   FLAG_PREFIX [A-Z_]+
    ;
Kunshan Wang's avatar
Kunshan Wang committed
370 371 372 373
    
STRING_LITERAL
    :   '"' NON_QUOTE* '"'
    ;
374 375 376 377 378 379 380 381 382 383 384

fragment
DIGIT
    :   [0-9]
    ;

fragment
DIGIT_NON_ZERO
    :   [1-9]
    ;

Kunshan Wang's avatar
Kunshan Wang committed
385 386 387 388 389
fragment
OCT_DIGIT
    :   [0-7]
    ;

390 391 392 393 394 395
fragment
HEX_DIGIT
    :   [0-9a-fA-F]
    ;

fragment
Kunshan Wang's avatar
Kunshan Wang committed
396
GLOBAL_NAME_PREFIX: '@';
397 398

fragment
Kunshan Wang's avatar
Kunshan Wang committed
399
LOCAL_NAME_PREFIX: '%';
400

401 402 403
fragment
FLAG_PREFIX: '#';

404 405 406 407 408 409 410 411 412 413
fragment
IDCHAR
    :   [a-z]
    |   [A-Z]
    |   [0-9]
    |   '-'
    |   '_'
    |   '.'
    ;

Kunshan Wang's avatar
Kunshan Wang committed
414 415 416 417 418
fragment
NON_QUOTE
    :   [!#-~]
    ;

419 420 421 422 423
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

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