blob: d65f075076cf8ef8eb4c064a8cece328c5d40acf [file] [log] [blame]
Dan Gohman890c31c2008-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
Dan Gohmana2858522010-08-04 17:01:59 +000011TODO: Metadata, in all its forms
Dan Gohmana2858522010-08-04 17:01:59 +000012
Dan Gohman890c31c2008-05-22 22:45:03 +000013*)
14
15I ::= "title: LLVM assembly language\n"
16 ^ "status: experimental\n"
17 ^ "audience: LLVM developers\n"
18;
19
20S ::= Module ;
21
22(*
23Define rules for non-keyword tokens. This is currently just a bunch
24of hacks. They don't cover many valid forms of tokens, and they also
25generate some invalid forms of tokens. The LLVM parser has custom
26C++ code to lex these; custom C++ code for emitting them would be
27convenient, but polygen doesn't support that.
28*)
29NonZeroDecimalDigit ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ;
30DecimalDigit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ;
31DecimalDigitSeq ::= DecimalDigit [^ DecimalDigitSeq ];
32HexDigit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
33 | a | b | c | d | e | f ;
34HexDigitSeq ::= HexDigit [^ HexDigitSeq ];
35StringChar ::= a | b | c | d | e | f | g | h | i | j | k | l | m
36 | n | o | p | q | r | s | t | u | v | w | x | y | z ;
37StringConstantSeq ::= StringChar [^ StringConstantSeq ];
38StringConstant ::= StringChar [^ StringConstantSeq ];
39EUINT64VAL ::= NonZeroDecimalDigit [^ DecimalDigitSeq ];
40ESINT64VAL ::= [ "-" ] ^ EUINT64VAL ;
41EUAPINTVAL ::= EUINT64VAL ;
42ESAPINTVAL ::= ESINT64VAL ;
43LOCALVALID ::= "%" ^ DecimalDigitSeq ;
44GLOBALVALID ::= "@" ^ DecimalDigitSeq ;
45INTTYPE ::= "i" ^ EUINT64VAL ;
46GLOBALVAR ::= "@" ^ StringConstant ;
47LOCALVAR ::= "%" ^ StringConstant ;
48STRINGCONSTANT ::= "\"" ^ StringConstant ^ "\"" ;
49ATSTRINGCONSTANT ::= "@" ^ STRINGCONSTANT ;
50PCTSTRINGCONSTANT ::= "%" ^ STRINGCONSTANT ;
51LABELSTR ::= StringConstant ;
52FPVAL ::= ESAPINTVAL ^ "." ^ EUAPINTVAL | "0x" ^ HexDigitSeq ;
53
54(*
55The rest of this file is derived directly from llvmAsmParser.y.
56*)
57
Dan Gohmanbfacb7e2009-07-22 22:45:30 +000058ArithmeticOps ::= + OptNW add | fadd | OptNW sub | fsub | OptNW mul | fmul |
59 udiv | OptExact sdiv | fdiv | urem | srem | frem ;
Dan Gohman890c31c2008-05-22 22:45:03 +000060LogicalOps ::= shl | lshr | ashr | and | or | xor;
61CastOps ::= trunc | zext | sext | fptrunc | fpext | bitcast |
62 uitofp | sitofp | fptoui | fptosi | inttoptr | ptrtoint ;
63
64IPredicates ::= eq | ne | slt | sgt | sle | sge | ult | ugt | ule | uge ;
65
66FPredicates ::= oeq | one | olt | ogt | ole | oge | ord | uno | ueq | une
67 | ult | ugt | ule | uge | true | false ;
68
69IntType ::= INTTYPE;
70FPType ::= float | double | "ppc_fp128" | fp128 | "x86_fp80";
71
72LocalName ::= LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
73OptLocalName ::= LocalName | _ ;
74
Dan Gohman8f56eba2009-01-05 17:29:42 +000075OptAddrSpace ::= - addrspace ^ "(" ^ EUINT64VAL ^ ")" | _ ;
Dan Gohman890c31c2008-05-22 22:45:03 +000076
77OptLocalAssign ::= LocalName "=" | _ ;
78
79GlobalName ::= GLOBALVAR | ATSTRINGCONSTANT ;
80
81OptGlobalAssign ::= GlobalAssign | _ ;
82
83GlobalAssign ::= GlobalName "=" ;
84
85GVInternalLinkage
86 ::= + internal
87 | weak
Duncan Sands667d4b82009-03-07 15:45:40 +000088 | "weak_odr"
Dan Gohman890c31c2008-05-22 22:45:03 +000089 | linkonce
Duncan Sands667d4b82009-03-07 15:45:40 +000090 | "linkonce_odr"
Dan Gohman890c31c2008-05-22 22:45:03 +000091 | appending
92 | dllexport
93 | common
Dan Gohman923a5792009-07-17 01:07:45 +000094 | private
Dan Gohmana2858522010-08-04 17:01:59 +000095 | "linker_private"
96 | "linker_private_weak"
Dan Gohman890c31c2008-05-22 22:45:03 +000097 ;
98
99GVExternalLinkage
100 ::= dllimport
101 | "extern_weak"
102 | + external
103 ;
104
105GVVisibilityStyle
106 ::= + _
107 | default
108 | hidden
109 | protected
110 ;
111
112FunctionDeclareLinkage
113 ::= + _
114 | dllimport
115 | "extern_weak"
116 ;
117
118FunctionDefineLinkage
119 ::= + _
120 | internal
121 | linkonce
Duncan Sands667d4b82009-03-07 15:45:40 +0000122 | "linkonce_odr"
Dan Gohman890c31c2008-05-22 22:45:03 +0000123 | weak
Duncan Sands667d4b82009-03-07 15:45:40 +0000124 | "weak_odr"
Dan Gohman890c31c2008-05-22 22:45:03 +0000125 | dllexport
126 ;
127
Duncan Sands667d4b82009-03-07 15:45:40 +0000128AliasLinkage ::= + _ | weak | "weak_odr" | internal ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000129
130OptCallingConv ::= + _ |
131 ccc |
132 fastcc |
133 coldcc |
134 "x86_stdcallcc" |
135 "x86_fastcallcc" |
136 cc EUINT64VAL ;
137
138ParamAttr ::= zeroext
Dan Gohman890c31c2008-05-22 22:45:03 +0000139 | signext
Dan Gohman890c31c2008-05-22 22:45:03 +0000140 | inreg
141 | sret
142 | noalias
Dan Gohman571238a2009-01-05 03:21:23 +0000143 | nocapture
Dan Gohman890c31c2008-05-22 22:45:03 +0000144 | byval
145 | nest
146 | align EUINT64VAL
147 ;
148
149OptParamAttrs ::= + _ | OptParamAttrs ParamAttr ;
150
Dan Gohman571238a2009-01-05 03:21:23 +0000151RetAttr ::= inreg
152 | zeroext
153 | signext
154 | noalias
155 ;
156
157OptRetAttrs ::= _
158 | OptRetAttrs RetAttr
159 ;
160
Dan Gohman890c31c2008-05-22 22:45:03 +0000161FuncAttr ::= noreturn
162 | nounwind
Dan Gohman571238a2009-01-05 03:21:23 +0000163 | inreg
Dan Gohman890c31c2008-05-22 22:45:03 +0000164 | zeroext
165 | signext
166 | readnone
167 | readonly
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000168 | inlinehint
Dan Gohmanc235a0c2010-03-01 17:53:39 +0000169 | alignstack
Dan Gohman571238a2009-01-05 03:21:23 +0000170 | noinline
171 | alwaysinline
172 | optsize
173 | ssp
174 | sspreq
Rafael Espindolab3c4e262011-10-04 03:08:43 +0000175 | returns_twice
John McCall3a3465b2011-06-15 20:36:13 +0000176 | nonlazybind
Kostya Serebryany8eec41f2013-02-26 06:58:09 +0000177 | sanitize_address
178 | sanitize_thread
179 | sanitize_memory
Dan Gohman890c31c2008-05-22 22:45:03 +0000180 ;
181
182OptFuncAttrs ::= + _ | OptFuncAttrs FuncAttr ;
183
184OptGC ::= + _ | gc STRINGCONSTANT ;
185
186OptAlign ::= + _ | align EUINT64VAL ;
187OptCAlign ::= + _ | ^ "," align EUINT64VAL ;
188
189SectionString ::= section STRINGCONSTANT ;
190
191OptSection ::= + _ | SectionString ;
192
193GlobalVarAttributes ::= + _ | ^ "," GlobalVarAttribute GlobalVarAttributes ;
194GlobalVarAttribute ::= SectionString | align EUINT64VAL ;
195
196PrimType ::= INTTYPE | float | double | "ppc_fp128" | fp128 | "x86_fp80"
197 | - label ;
198
199Types
200 ::= opaque
201 | PrimType
202 | Types OptAddrSpace ^ "*"
203 | SymbolicValueRef
204 | "\\" ^ EUINT64VAL
205 | Types "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
206 | void "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
207 | "[" ^ EUINT64VAL "x" Types ^ "]"
208 | "<" ^ EUINT64VAL "x" Types ^ ">"
209 | "{" TypeListI "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000210 | "{" ^ "}"
Dan Gohman890c31c2008-05-22 22:45:03 +0000211 | "<" ^ "{" TypeListI "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000212 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000213 ;
214
215ArgType ::= Types OptParamAttrs ;
216
217ResultTypes ::= Types | void ;
218
219ArgTypeList ::= ArgType | ArgTypeList ^ "," ArgType ;
220
221ArgTypeListI ::= ArgTypeList | ArgTypeList ^ "," "..." | "..." | _ ;
222
223TypeListI ::= Types | TypeListI ^ "," Types ;
224
225ConstVal::= Types "[" ^ ConstVector ^ "]"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000226 | Types "[" ^ "]"
Dan Gohman890c31c2008-05-22 22:45:03 +0000227 | Types "c" ^ STRINGCONSTANT
228 | Types "<" ^ ConstVector ^ ">"
229 | Types "{" ConstVector "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000230 | Types "{" ^ "}"
Dan Gohman890c31c2008-05-22 22:45:03 +0000231 | Types "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000232 | Types "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000233 | Types null
234 | Types undef
235 | Types SymbolicValueRef
236 | Types ConstExpr
237 | Types zeroinitializer
Dan Gohman571238a2009-01-05 03:21:23 +0000238 | Types ESINT64VAL
239 | Types ESAPINTVAL
240 | Types EUINT64VAL
241 | Types EUAPINTVAL
242 | Types true
243 | Types false
244 | Types FPVAL ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000245
246ConstExpr::= CastOps "(" ^ ConstVal to Types ^ ")"
Dan Gohman224251d2009-07-27 21:55:32 +0000247 | getelementptr OptInBounds "(" ^ ConstVal IndexList ^ ")"
Dan Gohman890c31c2008-05-22 22:45:03 +0000248 | select "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
249 | ArithmeticOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
250 | LogicalOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
251 | icmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
252 | fcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
Dan Gohman890c31c2008-05-22 22:45:03 +0000253 | extractelement "(" ^ ConstVal ^ "," ConstVal ^ ")"
254 | insertelement "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohmane4977cf2008-05-23 01:55:30 +0000255 | shufflevector "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohman34e71fe2008-06-02 19:47:09 +0000256 | extractvalue "(" ^ ConstVal ^ ConstantIndexList ^ ")"
257 | insertvalue "(" ^ ConstVal ^ "," ConstVal ^ ConstantIndexList ^ ")" ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000258
259ConstVector ::= ConstVector ^ "," ConstVal | ConstVal ;
260
261GlobalType ::= global | constant ;
262
263ThreadLocal ::= - "thread_local" | _ ;
264
265AliaseeRef ::= ResultTypes SymbolicValueRef
266 | bitcast "(" ^ AliaseeRef to Types ^ ")" ;
267
268Module ::= +++ DefinitionList | --- _ ;
269
270DefinitionList ::= - Definition | + DefinitionList Definition ;
271
272Definition
273 ::= ^ ( +++++ define Function
274 | declare FunctionProto
275 | - module asm AsmBlock
276 | OptLocalAssign type Types
Dan Gohmande0e7f22009-01-05 23:03:03 +0000277 | OptGlobalAssign GVVisibilityStyle ThreadLocal OptAddrSpace GlobalType
Dan Gohman8f56eba2009-01-05 17:29:42 +0000278 ConstVal GlobalVarAttributes
279 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
280 GlobalType ConstVal GlobalVarAttributes
281 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
282 GlobalType Types GlobalVarAttributes
Dan Gohman890c31c2008-05-22 22:45:03 +0000283 | OptGlobalAssign GVVisibilityStyle alias AliasLinkage AliaseeRef
284 | target TargetDefinition
285 | deplibs "=" LibrariesDefinition
286 ) ^ "\n";
287
288AsmBlock ::= STRINGCONSTANT ;
289
290TargetDefinition ::= triple "=" STRINGCONSTANT
291 | datalayout "=" STRINGCONSTANT ;
292
Dan Gohman8f56eba2009-01-05 17:29:42 +0000293LibrariesDefinition ::= "[" ( LibList | _ ) "]";
Dan Gohman890c31c2008-05-22 22:45:03 +0000294
Dan Gohman8f56eba2009-01-05 17:29:42 +0000295LibList ::= LibList ^ "," STRINGCONSTANT | STRINGCONSTANT ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000296
297ArgListH ::= ArgListH ^ "," Types OptParamAttrs OptLocalName
298 | Types OptParamAttrs OptLocalName ;
299
300ArgList ::= ArgListH | ArgListH ^ "," "..." | "..." | _ ;
301
Dan Gohman571238a2009-01-05 03:21:23 +0000302FunctionHeaderH ::= OptCallingConv OptRetAttrs ResultTypes
Dan Gohman8f56eba2009-01-05 17:29:42 +0000303 GlobalName ^ "(" ^ ArgList ^ ")"
Dan Gohman571238a2009-01-05 03:21:23 +0000304 OptFuncAttrs OptSection OptAlign OptGC ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000305
306BEGIN ::= ( begin | "{" ) ^ "\n";
307
308FunctionHeader ::=
309 FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN ;
310
311END ::= ^ ( end | "}" ) ^ "\n";
312
313Function ::= BasicBlockList END ;
314
315FunctionProto ::= FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH ;
316
317OptSideEffect ::= _ | sideeffect ;
318
319ConstValueRef ::= ESINT64VAL
320 | EUINT64VAL
321 | FPVAL
322 | true
323 | false
324 | null
325 | undef
326 | zeroinitializer
327 | "<" ConstVector ">"
Dan Gohmanf910eaa2008-06-09 14:45:02 +0000328 | "[" ConstVector "]"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000329 | "[" ^ "]"
Dan Gohmanf910eaa2008-06-09 14:45:02 +0000330 | "c" ^ STRINGCONSTANT
331 | "{" ConstVector "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000332 | "{" ^ "}"
Dan Gohmanf910eaa2008-06-09 14:45:02 +0000333 | "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000334 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000335 | ConstExpr
336 | asm OptSideEffect STRINGCONSTANT ^ "," STRINGCONSTANT ;
337
338SymbolicValueRef ::= LOCALVALID
339 | GLOBALVALID
340 | LocalName
341 | GlobalName ;
342
343ValueRef ::= SymbolicValueRef | ConstValueRef;
344
345ResolvedVal ::= Types ValueRef ;
346
347ReturnedVal ::= ResolvedVal | ReturnedVal ^ "," ResolvedVal ;
348
349BasicBlockList ::= BasicBlockList BasicBlock | FunctionHeader BasicBlock ;
350
351BasicBlock ::= InstructionList OptLocalAssign BBTerminatorInst ;
352
353InstructionList ::= +++ InstructionList Inst
354 | - _
355 | ^ LABELSTR ^ ":\n" ;
356
357BBTerminatorInst ::= ^ " " ^
358 ( ret ReturnedVal
359 | ret void
360 | br label ValueRef
361 | br INTTYPE ValueRef ^ "," label ValueRef ^ "," label ValueRef
362 | switch IntType ValueRef ^ "," label ValueRef "[" JumpTable "]"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000363 | switch IntType ValueRef ^ "," label ValueRef "[" ^ "]"
364 | invoke OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
365 OptFuncAttrs
Dan Gohman890c31c2008-05-22 22:45:03 +0000366 to label ValueRef unwind label ValueRef
367 | unwind
368 | unreachable ) ^ "\n";
369
370JumpTable ::= JumpTable IntType ConstValueRef ^ "," label ValueRef
371 | IntType ConstValueRef ^ "," label ValueRef ;
372
373Inst ::= ^ " " ^ OptLocalAssign InstVal ^ "\n";
374
375PHIList ::= Types "[" ValueRef ^ "," ValueRef "]"
376 | PHIList ^ "," "[" ValueRef ^ "," ValueRef "]" ;
377
378ParamList ::= Types OptParamAttrs ValueRef OptParamAttrs
379 | label OptParamAttrs ValueRef OptParamAttrs
380 | ParamList ^ "," Types OptParamAttrs ValueRef OptParamAttrs
381 | ParamList ^ "," label OptParamAttrs ValueRef OptParamAttrs
382 | - _ ;
383
384IndexList ::= _ | IndexList ^ "," ResolvedVal ;
385
Dan Gohman34e71fe2008-06-02 19:47:09 +0000386ConstantIndexList ::= "," EUINT64VAL | ConstantIndexList ^ "," EUINT64VAL ;
387
Dan Gohman890c31c2008-05-22 22:45:03 +0000388OptTailCall ::= tail call | call ;
389
390InstVal ::=
391 ArithmeticOps Types ValueRef ^ "," ValueRef
392 | LogicalOps Types ValueRef ^ "," ValueRef
393 | icmp IPredicates Types ValueRef ^ "," ValueRef
394 | fcmp FPredicates Types ValueRef ^ "," ValueRef
Dan Gohman890c31c2008-05-22 22:45:03 +0000395 | CastOps ResolvedVal to Types
396 | select ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
Dan Gohmanb737a642008-05-23 17:11:55 +0000397 | "va_arg" ResolvedVal ^ "," Types
Dan Gohman890c31c2008-05-22 22:45:03 +0000398 | extractelement ResolvedVal ^ "," ResolvedVal
399 | insertelement ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
400 | shufflevector ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
401 | phi PHIList
Dan Gohman0d58eb62008-09-15 16:10:51 +0000402 | OptTailCall OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
Dan Gohman890c31c2008-05-22 22:45:03 +0000403 OptFuncAttrs
404 | MemoryInst ;
405
406OptVolatile ::= - volatile | _ ;
Dan Gohmanbfacb7e2009-07-22 22:45:30 +0000407OptExact ::= - exact | _ ;
408OptNSW ::= - nsw | _ ;
409OptNUW ::= - nuw | _ ;
Dan Gohmand622b0b2010-05-04 00:13:24 +0000410OptNW ::= OptNUW OptNSW | OptNSW OptNUW ;
Dan Gohman224251d2009-07-27 21:55:32 +0000411OptInBounds ::= - inbounds | _ ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000412
413MemoryInst ::= malloc Types OptCAlign
414 | malloc Types ^ "," INTTYPE ValueRef OptCAlign
415 | alloca Types OptCAlign
416 | alloca Types ^ "," INTTYPE ValueRef OptCAlign
417 | free ResolvedVal
418 | OptVolatile load Types ValueRef OptCAlign
419 | OptVolatile store ResolvedVal ^ "," Types ValueRef OptCAlign
420 | getresult Types ValueRef ^ "," EUINT64VAL
Dan Gohman224251d2009-07-27 21:55:32 +0000421 | getelementptr OptInBounds Types ValueRef IndexList
Dan Gohman34e71fe2008-06-02 19:47:09 +0000422 | extractvalue Types ValueRef ^ ConstantIndexList
423 | insertvalue Types ValueRef ^ "," Types ValueRef ^ ConstantIndexList ;