blob: 641ba01f20d83fe379c5b7f010d53b1e36b54f5a [file] [log] [blame]
Dan Gohman29b46c92008-05-22 22:45:03 +00001(*
2
3polygen grammar for LLVM assembly language.
4
5This file defines an LLVM assembly language grammar for polygen,
6which is a tool for generating random text based on a grammar.
7It is strictly syntax-based, and makes no attempt to generate
8IR that is semantically valid. Most of the IR produced doesn't
9pass the Verifier.
10
11*)
12
13I ::= "title: LLVM assembly language\n"
14 ^ "status: experimental\n"
15 ^ "audience: LLVM developers\n"
16;
17
18S ::= Module ;
19
20(*
21Define rules for non-keyword tokens. This is currently just a bunch
22of hacks. They don't cover many valid forms of tokens, and they also
23generate some invalid forms of tokens. The LLVM parser has custom
24C++ code to lex these; custom C++ code for emitting them would be
25convenient, but polygen doesn't support that.
26*)
27NonZeroDecimalDigit ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ;
28DecimalDigit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ;
29DecimalDigitSeq ::= DecimalDigit [^ DecimalDigitSeq ];
30HexDigit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
31 | a | b | c | d | e | f ;
32HexDigitSeq ::= HexDigit [^ HexDigitSeq ];
33StringChar ::= a | b | c | d | e | f | g | h | i | j | k | l | m
34 | n | o | p | q | r | s | t | u | v | w | x | y | z ;
35StringConstantSeq ::= StringChar [^ StringConstantSeq ];
36StringConstant ::= StringChar [^ StringConstantSeq ];
37EUINT64VAL ::= NonZeroDecimalDigit [^ DecimalDigitSeq ];
38ESINT64VAL ::= [ "-" ] ^ EUINT64VAL ;
39EUAPINTVAL ::= EUINT64VAL ;
40ESAPINTVAL ::= ESINT64VAL ;
41LOCALVALID ::= "%" ^ DecimalDigitSeq ;
42GLOBALVALID ::= "@" ^ DecimalDigitSeq ;
43INTTYPE ::= "i" ^ EUINT64VAL ;
44GLOBALVAR ::= "@" ^ StringConstant ;
45LOCALVAR ::= "%" ^ StringConstant ;
46STRINGCONSTANT ::= "\"" ^ StringConstant ^ "\"" ;
47ATSTRINGCONSTANT ::= "@" ^ STRINGCONSTANT ;
48PCTSTRINGCONSTANT ::= "%" ^ STRINGCONSTANT ;
49LABELSTR ::= StringConstant ;
50FPVAL ::= ESAPINTVAL ^ "." ^ EUAPINTVAL | "0x" ^ HexDigitSeq ;
51
52(*
53The rest of this file is derived directly from llvmAsmParser.y.
54*)
55
56ArithmeticOps ::= add | sub | mul | udiv | sdiv | fdiv | urem | srem | frem ;
57LogicalOps ::= shl | lshr | ashr | and | or | xor;
58CastOps ::= trunc | zext | sext | fptrunc | fpext | bitcast |
59 uitofp | sitofp | fptoui | fptosi | inttoptr | ptrtoint ;
60
61IPredicates ::= eq | ne | slt | sgt | sle | sge | ult | ugt | ule | uge ;
62
63FPredicates ::= oeq | one | olt | ogt | ole | oge | ord | uno | ueq | une
64 | ult | ugt | ule | uge | true | false ;
65
66IntType ::= INTTYPE;
67FPType ::= float | double | "ppc_fp128" | fp128 | "x86_fp80";
68
69LocalName ::= LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
70OptLocalName ::= LocalName | _ ;
71
Dan Gohman844695c2009-01-05 17:29:42 +000072OptAddrSpace ::= - addrspace ^ "(" ^ EUINT64VAL ^ ")" | _ ;
Dan Gohman29b46c92008-05-22 22:45:03 +000073
74OptLocalAssign ::= LocalName "=" | _ ;
75
76GlobalName ::= GLOBALVAR | ATSTRINGCONSTANT ;
77
78OptGlobalAssign ::= GlobalAssign | _ ;
79
80GlobalAssign ::= GlobalName "=" ;
81
82GVInternalLinkage
83 ::= + internal
84 | weak
Duncan Sands19d161f2009-03-07 15:45:40 +000085 | "weak_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +000086 | linkonce
Duncan Sands19d161f2009-03-07 15:45:40 +000087 | "linkonce_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +000088 | appending
89 | dllexport
90 | common
Duncan Sands19d161f2009-03-07 15:45:40 +000091 | "common_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +000092 ;
93
94GVExternalLinkage
95 ::= dllimport
96 | "extern_weak"
Duncan Sands19d161f2009-03-07 15:45:40 +000097 | "extern_weak_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +000098 | + external
99 ;
100
101GVVisibilityStyle
102 ::= + _
103 | default
104 | hidden
105 | protected
106 ;
107
108FunctionDeclareLinkage
109 ::= + _
110 | dllimport
111 | "extern_weak"
Duncan Sands19d161f2009-03-07 15:45:40 +0000112 | "extern_weak_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +0000113 ;
114
115FunctionDefineLinkage
116 ::= + _
117 | internal
118 | linkonce
Duncan Sands19d161f2009-03-07 15:45:40 +0000119 | "linkonce_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +0000120 | weak
Duncan Sands19d161f2009-03-07 15:45:40 +0000121 | "weak_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +0000122 | dllexport
123 ;
124
Duncan Sands19d161f2009-03-07 15:45:40 +0000125AliasLinkage ::= + _ | weak | "weak_odr" | internal ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000126
127OptCallingConv ::= + _ |
128 ccc |
129 fastcc |
130 coldcc |
131 "x86_stdcallcc" |
132 "x86_fastcallcc" |
133 cc EUINT64VAL ;
134
135ParamAttr ::= zeroext
136 | zext
137 | signext
138 | sext
139 | inreg
140 | sret
141 | noalias
Dan Gohman713c0732009-01-05 03:21:23 +0000142 | nocapture
Dan Gohman29b46c92008-05-22 22:45:03 +0000143 | byval
144 | nest
145 | align EUINT64VAL
146 ;
147
148OptParamAttrs ::= + _ | OptParamAttrs ParamAttr ;
149
Dan Gohman713c0732009-01-05 03:21:23 +0000150RetAttr ::= inreg
151 | zeroext
152 | signext
153 | noalias
154 ;
155
156OptRetAttrs ::= _
157 | OptRetAttrs RetAttr
158 ;
159
Dan Gohman29b46c92008-05-22 22:45:03 +0000160FuncAttr ::= noreturn
161 | nounwind
Dan Gohman713c0732009-01-05 03:21:23 +0000162 | inreg
Dan Gohman29b46c92008-05-22 22:45:03 +0000163 | zeroext
164 | signext
165 | readnone
166 | readonly
Dan Gohman713c0732009-01-05 03:21:23 +0000167 | noinline
168 | alwaysinline
169 | optsize
170 | ssp
171 | sspreq
Dan Gohman29b46c92008-05-22 22:45:03 +0000172 ;
173
174OptFuncAttrs ::= + _ | OptFuncAttrs FuncAttr ;
175
176OptGC ::= + _ | gc STRINGCONSTANT ;
177
178OptAlign ::= + _ | align EUINT64VAL ;
179OptCAlign ::= + _ | ^ "," align EUINT64VAL ;
180
181SectionString ::= section STRINGCONSTANT ;
182
183OptSection ::= + _ | SectionString ;
184
185GlobalVarAttributes ::= + _ | ^ "," GlobalVarAttribute GlobalVarAttributes ;
186GlobalVarAttribute ::= SectionString | align EUINT64VAL ;
187
188PrimType ::= INTTYPE | float | double | "ppc_fp128" | fp128 | "x86_fp80"
189 | - label ;
190
191Types
192 ::= opaque
193 | PrimType
194 | Types OptAddrSpace ^ "*"
195 | SymbolicValueRef
196 | "\\" ^ EUINT64VAL
197 | Types "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
198 | void "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
199 | "[" ^ EUINT64VAL "x" Types ^ "]"
200 | "<" ^ EUINT64VAL "x" Types ^ ">"
201 | "{" TypeListI "}"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000202 | "{" ^ "}"
Dan Gohman29b46c92008-05-22 22:45:03 +0000203 | "<" ^ "{" TypeListI "}" ^ ">"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000204 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman29b46c92008-05-22 22:45:03 +0000205 ;
206
207ArgType ::= Types OptParamAttrs ;
208
209ResultTypes ::= Types | void ;
210
211ArgTypeList ::= ArgType | ArgTypeList ^ "," ArgType ;
212
213ArgTypeListI ::= ArgTypeList | ArgTypeList ^ "," "..." | "..." | _ ;
214
215TypeListI ::= Types | TypeListI ^ "," Types ;
216
217ConstVal::= Types "[" ^ ConstVector ^ "]"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000218 | Types "[" ^ "]"
Dan Gohman29b46c92008-05-22 22:45:03 +0000219 | Types "c" ^ STRINGCONSTANT
220 | Types "<" ^ ConstVector ^ ">"
221 | Types "{" ConstVector "}"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000222 | Types "{" ^ "}"
Dan Gohman29b46c92008-05-22 22:45:03 +0000223 | Types "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000224 | Types "<" ^ "{" ^ "}" ^ ">"
Dan Gohman29b46c92008-05-22 22:45:03 +0000225 | Types null
226 | Types undef
227 | Types SymbolicValueRef
228 | Types ConstExpr
229 | Types zeroinitializer
Dan Gohman713c0732009-01-05 03:21:23 +0000230 | Types ESINT64VAL
231 | Types ESAPINTVAL
232 | Types EUINT64VAL
233 | Types EUAPINTVAL
234 | Types true
235 | Types false
236 | Types FPVAL ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000237
238ConstExpr::= CastOps "(" ^ ConstVal to Types ^ ")"
239 | getelementptr "(" ^ ConstVal IndexList ^ ")"
240 | select "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
241 | ArithmeticOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
242 | LogicalOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
243 | icmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
244 | fcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
245 | vicmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
246 | vfcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
247 | extractelement "(" ^ ConstVal ^ "," ConstVal ^ ")"
248 | insertelement "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000249 | shufflevector "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohmanb48ded92008-06-02 19:47:09 +0000250 | extractvalue "(" ^ ConstVal ^ ConstantIndexList ^ ")"
251 | insertvalue "(" ^ ConstVal ^ "," ConstVal ^ ConstantIndexList ^ ")" ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000252
253ConstVector ::= ConstVector ^ "," ConstVal | ConstVal ;
254
255GlobalType ::= global | constant ;
256
257ThreadLocal ::= - "thread_local" | _ ;
258
259AliaseeRef ::= ResultTypes SymbolicValueRef
260 | bitcast "(" ^ AliaseeRef to Types ^ ")" ;
261
262Module ::= +++ DefinitionList | --- _ ;
263
264DefinitionList ::= - Definition | + DefinitionList Definition ;
265
266Definition
267 ::= ^ ( +++++ define Function
268 | declare FunctionProto
269 | - module asm AsmBlock
270 | OptLocalAssign type Types
Dan Gohman4b0d9d82009-01-05 23:03:03 +0000271 | OptGlobalAssign GVVisibilityStyle ThreadLocal OptAddrSpace GlobalType
Dan Gohman844695c2009-01-05 17:29:42 +0000272 ConstVal GlobalVarAttributes
273 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
274 GlobalType ConstVal GlobalVarAttributes
275 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
276 GlobalType Types GlobalVarAttributes
Dan Gohman29b46c92008-05-22 22:45:03 +0000277 | OptGlobalAssign GVVisibilityStyle alias AliasLinkage AliaseeRef
278 | target TargetDefinition
279 | deplibs "=" LibrariesDefinition
280 ) ^ "\n";
281
282AsmBlock ::= STRINGCONSTANT ;
283
284TargetDefinition ::= triple "=" STRINGCONSTANT
285 | datalayout "=" STRINGCONSTANT ;
286
Dan Gohman844695c2009-01-05 17:29:42 +0000287LibrariesDefinition ::= "[" ( LibList | _ ) "]";
Dan Gohman29b46c92008-05-22 22:45:03 +0000288
Dan Gohman844695c2009-01-05 17:29:42 +0000289LibList ::= LibList ^ "," STRINGCONSTANT | STRINGCONSTANT ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000290
291ArgListH ::= ArgListH ^ "," Types OptParamAttrs OptLocalName
292 | Types OptParamAttrs OptLocalName ;
293
294ArgList ::= ArgListH | ArgListH ^ "," "..." | "..." | _ ;
295
Dan Gohman713c0732009-01-05 03:21:23 +0000296FunctionHeaderH ::= OptCallingConv OptRetAttrs ResultTypes
Dan Gohman844695c2009-01-05 17:29:42 +0000297 GlobalName ^ "(" ^ ArgList ^ ")"
Dan Gohman713c0732009-01-05 03:21:23 +0000298 OptFuncAttrs OptSection OptAlign OptGC ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000299
300BEGIN ::= ( begin | "{" ) ^ "\n";
301
302FunctionHeader ::=
303 FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN ;
304
305END ::= ^ ( end | "}" ) ^ "\n";
306
307Function ::= BasicBlockList END ;
308
309FunctionProto ::= FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH ;
310
311OptSideEffect ::= _ | sideeffect ;
312
313ConstValueRef ::= ESINT64VAL
314 | EUINT64VAL
315 | FPVAL
316 | true
317 | false
318 | null
319 | undef
320 | zeroinitializer
321 | "<" ConstVector ">"
Dan Gohman9fc6cb02008-06-09 14:45:02 +0000322 | "[" ConstVector "]"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000323 | "[" ^ "]"
Dan Gohman9fc6cb02008-06-09 14:45:02 +0000324 | "c" ^ STRINGCONSTANT
325 | "{" ConstVector "}"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000326 | "{" ^ "}"
Dan Gohman9fc6cb02008-06-09 14:45:02 +0000327 | "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000328 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman29b46c92008-05-22 22:45:03 +0000329 | ConstExpr
330 | asm OptSideEffect STRINGCONSTANT ^ "," STRINGCONSTANT ;
331
332SymbolicValueRef ::= LOCALVALID
333 | GLOBALVALID
334 | LocalName
335 | GlobalName ;
336
337ValueRef ::= SymbolicValueRef | ConstValueRef;
338
339ResolvedVal ::= Types ValueRef ;
340
341ReturnedVal ::= ResolvedVal | ReturnedVal ^ "," ResolvedVal ;
342
343BasicBlockList ::= BasicBlockList BasicBlock | FunctionHeader BasicBlock ;
344
345BasicBlock ::= InstructionList OptLocalAssign BBTerminatorInst ;
346
347InstructionList ::= +++ InstructionList Inst
348 | - _
349 | ^ LABELSTR ^ ":\n" ;
350
351BBTerminatorInst ::= ^ " " ^
352 ( ret ReturnedVal
353 | ret void
354 | br label ValueRef
355 | br INTTYPE ValueRef ^ "," label ValueRef ^ "," label ValueRef
356 | switch IntType ValueRef ^ "," label ValueRef "[" JumpTable "]"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000357 | switch IntType ValueRef ^ "," label ValueRef "[" ^ "]"
358 | invoke OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
359 OptFuncAttrs
Dan Gohman29b46c92008-05-22 22:45:03 +0000360 to label ValueRef unwind label ValueRef
361 | unwind
362 | unreachable ) ^ "\n";
363
364JumpTable ::= JumpTable IntType ConstValueRef ^ "," label ValueRef
365 | IntType ConstValueRef ^ "," label ValueRef ;
366
367Inst ::= ^ " " ^ OptLocalAssign InstVal ^ "\n";
368
369PHIList ::= Types "[" ValueRef ^ "," ValueRef "]"
370 | PHIList ^ "," "[" ValueRef ^ "," ValueRef "]" ;
371
372ParamList ::= Types OptParamAttrs ValueRef OptParamAttrs
373 | label OptParamAttrs ValueRef OptParamAttrs
374 | ParamList ^ "," Types OptParamAttrs ValueRef OptParamAttrs
375 | ParamList ^ "," label OptParamAttrs ValueRef OptParamAttrs
376 | - _ ;
377
378IndexList ::= _ | IndexList ^ "," ResolvedVal ;
379
Dan Gohmanb48ded92008-06-02 19:47:09 +0000380ConstantIndexList ::= "," EUINT64VAL | ConstantIndexList ^ "," EUINT64VAL ;
381
Dan Gohman29b46c92008-05-22 22:45:03 +0000382OptTailCall ::= tail call | call ;
383
384InstVal ::=
385 ArithmeticOps Types ValueRef ^ "," ValueRef
386 | LogicalOps Types ValueRef ^ "," ValueRef
387 | icmp IPredicates Types ValueRef ^ "," ValueRef
388 | fcmp FPredicates Types ValueRef ^ "," ValueRef
389 | vicmp IPredicates Types ValueRef ^ "," ValueRef
390 | vfcmp FPredicates Types ValueRef ^ "," ValueRef
391 | CastOps ResolvedVal to Types
392 | select ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
Dan Gohman0f0195b2008-05-23 17:11:55 +0000393 | "va_arg" ResolvedVal ^ "," Types
Dan Gohman29b46c92008-05-22 22:45:03 +0000394 | extractelement ResolvedVal ^ "," ResolvedVal
395 | insertelement ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
396 | shufflevector ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
397 | phi PHIList
Dan Gohmaneecdb832008-09-15 16:10:51 +0000398 | OptTailCall OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
Dan Gohman29b46c92008-05-22 22:45:03 +0000399 OptFuncAttrs
400 | MemoryInst ;
401
402OptVolatile ::= - volatile | _ ;
403
404MemoryInst ::= malloc Types OptCAlign
405 | malloc Types ^ "," INTTYPE ValueRef OptCAlign
406 | alloca Types OptCAlign
407 | alloca Types ^ "," INTTYPE ValueRef OptCAlign
408 | free ResolvedVal
409 | OptVolatile load Types ValueRef OptCAlign
410 | OptVolatile store ResolvedVal ^ "," Types ValueRef OptCAlign
411 | getresult Types ValueRef ^ "," EUINT64VAL
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000412 | getelementptr Types ValueRef IndexList
Dan Gohmanb48ded92008-06-02 19:47:09 +0000413 | extractvalue Types ValueRef ^ ConstantIndexList
414 | insertvalue Types ValueRef ^ "," Types ValueRef ^ ConstantIndexList ;