blob: 9d6bdf79f539a23b1250af13a79d778bff1cba0e [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
Dan Gohman890c31c2008-05-22 22:45:03 +0000175 ;
176
177OptFuncAttrs ::= + _ | OptFuncAttrs FuncAttr ;
178
179OptGC ::= + _ | gc STRINGCONSTANT ;
180
181OptAlign ::= + _ | align EUINT64VAL ;
182OptCAlign ::= + _ | ^ "," align EUINT64VAL ;
183
184SectionString ::= section STRINGCONSTANT ;
185
186OptSection ::= + _ | SectionString ;
187
188GlobalVarAttributes ::= + _ | ^ "," GlobalVarAttribute GlobalVarAttributes ;
189GlobalVarAttribute ::= SectionString | align EUINT64VAL ;
190
191PrimType ::= INTTYPE | float | double | "ppc_fp128" | fp128 | "x86_fp80"
192 | - label ;
193
194Types
195 ::= opaque
196 | PrimType
197 | Types OptAddrSpace ^ "*"
198 | SymbolicValueRef
199 | "\\" ^ EUINT64VAL
200 | Types "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
201 | void "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
202 | "[" ^ EUINT64VAL "x" Types ^ "]"
203 | "<" ^ EUINT64VAL "x" Types ^ ">"
204 | "{" TypeListI "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000205 | "{" ^ "}"
Dan Gohman890c31c2008-05-22 22:45:03 +0000206 | "<" ^ "{" TypeListI "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000207 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000208 ;
209
210ArgType ::= Types OptParamAttrs ;
211
212ResultTypes ::= Types | void ;
213
214ArgTypeList ::= ArgType | ArgTypeList ^ "," ArgType ;
215
216ArgTypeListI ::= ArgTypeList | ArgTypeList ^ "," "..." | "..." | _ ;
217
218TypeListI ::= Types | TypeListI ^ "," Types ;
219
220ConstVal::= Types "[" ^ ConstVector ^ "]"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000221 | Types "[" ^ "]"
Dan Gohman890c31c2008-05-22 22:45:03 +0000222 | Types "c" ^ STRINGCONSTANT
223 | Types "<" ^ ConstVector ^ ">"
224 | Types "{" ConstVector "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000225 | Types "{" ^ "}"
Dan Gohman890c31c2008-05-22 22:45:03 +0000226 | Types "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000227 | Types "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000228 | Types null
229 | Types undef
230 | Types SymbolicValueRef
231 | Types ConstExpr
232 | Types zeroinitializer
Dan Gohman571238a2009-01-05 03:21:23 +0000233 | Types ESINT64VAL
234 | Types ESAPINTVAL
235 | Types EUINT64VAL
236 | Types EUAPINTVAL
237 | Types true
238 | Types false
239 | Types FPVAL ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000240
241ConstExpr::= CastOps "(" ^ ConstVal to Types ^ ")"
Dan Gohman224251d2009-07-27 21:55:32 +0000242 | getelementptr OptInBounds "(" ^ ConstVal IndexList ^ ")"
Dan Gohman890c31c2008-05-22 22:45:03 +0000243 | select "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
244 | ArithmeticOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
245 | LogicalOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
246 | icmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
247 | fcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
Dan Gohman890c31c2008-05-22 22:45:03 +0000248 | extractelement "(" ^ ConstVal ^ "," ConstVal ^ ")"
249 | insertelement "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohmane4977cf2008-05-23 01:55:30 +0000250 | shufflevector "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohman34e71fe2008-06-02 19:47:09 +0000251 | extractvalue "(" ^ ConstVal ^ ConstantIndexList ^ ")"
252 | insertvalue "(" ^ ConstVal ^ "," ConstVal ^ ConstantIndexList ^ ")" ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000253
254ConstVector ::= ConstVector ^ "," ConstVal | ConstVal ;
255
256GlobalType ::= global | constant ;
257
258ThreadLocal ::= - "thread_local" | _ ;
259
260AliaseeRef ::= ResultTypes SymbolicValueRef
261 | bitcast "(" ^ AliaseeRef to Types ^ ")" ;
262
263Module ::= +++ DefinitionList | --- _ ;
264
265DefinitionList ::= - Definition | + DefinitionList Definition ;
266
267Definition
268 ::= ^ ( +++++ define Function
269 | declare FunctionProto
270 | - module asm AsmBlock
271 | OptLocalAssign type Types
Dan Gohmande0e7f22009-01-05 23:03:03 +0000272 | OptGlobalAssign GVVisibilityStyle ThreadLocal OptAddrSpace GlobalType
Dan Gohman8f56eba2009-01-05 17:29:42 +0000273 ConstVal GlobalVarAttributes
274 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
275 GlobalType ConstVal GlobalVarAttributes
276 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
277 GlobalType Types GlobalVarAttributes
Dan Gohman890c31c2008-05-22 22:45:03 +0000278 | OptGlobalAssign GVVisibilityStyle alias AliasLinkage AliaseeRef
279 | target TargetDefinition
280 | deplibs "=" LibrariesDefinition
281 ) ^ "\n";
282
283AsmBlock ::= STRINGCONSTANT ;
284
285TargetDefinition ::= triple "=" STRINGCONSTANT
286 | datalayout "=" STRINGCONSTANT ;
287
Dan Gohman8f56eba2009-01-05 17:29:42 +0000288LibrariesDefinition ::= "[" ( LibList | _ ) "]";
Dan Gohman890c31c2008-05-22 22:45:03 +0000289
Dan Gohman8f56eba2009-01-05 17:29:42 +0000290LibList ::= LibList ^ "," STRINGCONSTANT | STRINGCONSTANT ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000291
292ArgListH ::= ArgListH ^ "," Types OptParamAttrs OptLocalName
293 | Types OptParamAttrs OptLocalName ;
294
295ArgList ::= ArgListH | ArgListH ^ "," "..." | "..." | _ ;
296
Dan Gohman571238a2009-01-05 03:21:23 +0000297FunctionHeaderH ::= OptCallingConv OptRetAttrs ResultTypes
Dan Gohman8f56eba2009-01-05 17:29:42 +0000298 GlobalName ^ "(" ^ ArgList ^ ")"
Dan Gohman571238a2009-01-05 03:21:23 +0000299 OptFuncAttrs OptSection OptAlign OptGC ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000300
301BEGIN ::= ( begin | "{" ) ^ "\n";
302
303FunctionHeader ::=
304 FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN ;
305
306END ::= ^ ( end | "}" ) ^ "\n";
307
308Function ::= BasicBlockList END ;
309
310FunctionProto ::= FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH ;
311
312OptSideEffect ::= _ | sideeffect ;
313
314ConstValueRef ::= ESINT64VAL
315 | EUINT64VAL
316 | FPVAL
317 | true
318 | false
319 | null
320 | undef
321 | zeroinitializer
322 | "<" ConstVector ">"
Dan Gohmanf910eaa2008-06-09 14:45:02 +0000323 | "[" ConstVector "]"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000324 | "[" ^ "]"
Dan Gohmanf910eaa2008-06-09 14:45:02 +0000325 | "c" ^ STRINGCONSTANT
326 | "{" ConstVector "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000327 | "{" ^ "}"
Dan Gohmanf910eaa2008-06-09 14:45:02 +0000328 | "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000329 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000330 | ConstExpr
331 | asm OptSideEffect STRINGCONSTANT ^ "," STRINGCONSTANT ;
332
333SymbolicValueRef ::= LOCALVALID
334 | GLOBALVALID
335 | LocalName
336 | GlobalName ;
337
338ValueRef ::= SymbolicValueRef | ConstValueRef;
339
340ResolvedVal ::= Types ValueRef ;
341
342ReturnedVal ::= ResolvedVal | ReturnedVal ^ "," ResolvedVal ;
343
344BasicBlockList ::= BasicBlockList BasicBlock | FunctionHeader BasicBlock ;
345
346BasicBlock ::= InstructionList OptLocalAssign BBTerminatorInst ;
347
348InstructionList ::= +++ InstructionList Inst
349 | - _
350 | ^ LABELSTR ^ ":\n" ;
351
352BBTerminatorInst ::= ^ " " ^
353 ( ret ReturnedVal
354 | ret void
355 | br label ValueRef
356 | br INTTYPE ValueRef ^ "," label ValueRef ^ "," label ValueRef
357 | switch IntType ValueRef ^ "," label ValueRef "[" JumpTable "]"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000358 | switch IntType ValueRef ^ "," label ValueRef "[" ^ "]"
359 | invoke OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
360 OptFuncAttrs
Dan Gohman890c31c2008-05-22 22:45:03 +0000361 to label ValueRef unwind label ValueRef
362 | unwind
363 | unreachable ) ^ "\n";
364
365JumpTable ::= JumpTable IntType ConstValueRef ^ "," label ValueRef
366 | IntType ConstValueRef ^ "," label ValueRef ;
367
368Inst ::= ^ " " ^ OptLocalAssign InstVal ^ "\n";
369
370PHIList ::= Types "[" ValueRef ^ "," ValueRef "]"
371 | PHIList ^ "," "[" ValueRef ^ "," ValueRef "]" ;
372
373ParamList ::= Types OptParamAttrs ValueRef OptParamAttrs
374 | label OptParamAttrs ValueRef OptParamAttrs
375 | ParamList ^ "," Types OptParamAttrs ValueRef OptParamAttrs
376 | ParamList ^ "," label OptParamAttrs ValueRef OptParamAttrs
377 | - _ ;
378
379IndexList ::= _ | IndexList ^ "," ResolvedVal ;
380
Dan Gohman34e71fe2008-06-02 19:47:09 +0000381ConstantIndexList ::= "," EUINT64VAL | ConstantIndexList ^ "," EUINT64VAL ;
382
Dan Gohman890c31c2008-05-22 22:45:03 +0000383OptTailCall ::= tail call | call ;
384
385InstVal ::=
386 ArithmeticOps Types ValueRef ^ "," ValueRef
387 | LogicalOps Types ValueRef ^ "," ValueRef
388 | icmp IPredicates Types ValueRef ^ "," ValueRef
389 | fcmp FPredicates Types ValueRef ^ "," ValueRef
Dan Gohman890c31c2008-05-22 22:45:03 +0000390 | CastOps ResolvedVal to Types
391 | select ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
Dan Gohmanb737a642008-05-23 17:11:55 +0000392 | "va_arg" ResolvedVal ^ "," Types
Dan Gohman890c31c2008-05-22 22:45:03 +0000393 | extractelement ResolvedVal ^ "," ResolvedVal
394 | insertelement ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
395 | shufflevector ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
396 | phi PHIList
Dan Gohman0d58eb62008-09-15 16:10:51 +0000397 | OptTailCall OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
Dan Gohman890c31c2008-05-22 22:45:03 +0000398 OptFuncAttrs
399 | MemoryInst ;
400
401OptVolatile ::= - volatile | _ ;
Dan Gohmanbfacb7e2009-07-22 22:45:30 +0000402OptExact ::= - exact | _ ;
403OptNSW ::= - nsw | _ ;
404OptNUW ::= - nuw | _ ;
Dan Gohmand622b0b2010-05-04 00:13:24 +0000405OptNW ::= OptNUW OptNSW | OptNSW OptNUW ;
Dan Gohman224251d2009-07-27 21:55:32 +0000406OptInBounds ::= - inbounds | _ ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000407
408MemoryInst ::= malloc Types OptCAlign
409 | malloc Types ^ "," INTTYPE ValueRef OptCAlign
410 | alloca Types OptCAlign
411 | alloca Types ^ "," INTTYPE ValueRef OptCAlign
412 | free ResolvedVal
413 | OptVolatile load Types ValueRef OptCAlign
414 | OptVolatile store ResolvedVal ^ "," Types ValueRef OptCAlign
415 | getresult Types ValueRef ^ "," EUINT64VAL
Dan Gohman224251d2009-07-27 21:55:32 +0000416 | getelementptr OptInBounds Types ValueRef IndexList
Dan Gohman34e71fe2008-06-02 19:47:09 +0000417 | extractvalue Types ValueRef ^ ConstantIndexList
418 | insertvalue Types ValueRef ^ "," Types ValueRef ^ ConstantIndexList ;