blob: 86a707a925d90a4bb12c23c995a114dcf0f62390 [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
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
Dan Gohmanbfacb7e2009-07-22 22:45:30 +000056ArithmeticOps ::= + OptNW add | fadd | OptNW sub | fsub | OptNW mul | fmul |
57 udiv | OptExact sdiv | fdiv | urem | srem | frem ;
Dan Gohman890c31c2008-05-22 22:45:03 +000058LogicalOps ::= shl | lshr | ashr | and | or | xor;
59CastOps ::= trunc | zext | sext | fptrunc | fpext | bitcast |
60 uitofp | sitofp | fptoui | fptosi | inttoptr | ptrtoint ;
61
62IPredicates ::= eq | ne | slt | sgt | sle | sge | ult | ugt | ule | uge ;
63
64FPredicates ::= oeq | one | olt | ogt | ole | oge | ord | uno | ueq | une
65 | ult | ugt | ule | uge | true | false ;
66
67IntType ::= INTTYPE;
68FPType ::= float | double | "ppc_fp128" | fp128 | "x86_fp80";
69
70LocalName ::= LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
71OptLocalName ::= LocalName | _ ;
72
Dan Gohman8f56eba2009-01-05 17:29:42 +000073OptAddrSpace ::= - addrspace ^ "(" ^ EUINT64VAL ^ ")" | _ ;
Dan Gohman890c31c2008-05-22 22:45:03 +000074
75OptLocalAssign ::= LocalName "=" | _ ;
76
77GlobalName ::= GLOBALVAR | ATSTRINGCONSTANT ;
78
79OptGlobalAssign ::= GlobalAssign | _ ;
80
81GlobalAssign ::= GlobalName "=" ;
82
83GVInternalLinkage
84 ::= + internal
85 | weak
Duncan Sands667d4b82009-03-07 15:45:40 +000086 | "weak_odr"
Dan Gohman890c31c2008-05-22 22:45:03 +000087 | linkonce
Duncan Sands667d4b82009-03-07 15:45:40 +000088 | "linkonce_odr"
Dan Gohman890c31c2008-05-22 22:45:03 +000089 | appending
90 | dllexport
91 | common
Dan Gohman923a5792009-07-17 01:07:45 +000092 | private
Dan Gohman890c31c2008-05-22 22:45:03 +000093 ;
94
95GVExternalLinkage
96 ::= dllimport
97 | "extern_weak"
98 | + external
99 ;
100
101GVVisibilityStyle
102 ::= + _
103 | default
104 | hidden
105 | protected
106 ;
107
108FunctionDeclareLinkage
109 ::= + _
110 | dllimport
111 | "extern_weak"
112 ;
113
114FunctionDefineLinkage
115 ::= + _
116 | internal
117 | linkonce
Duncan Sands667d4b82009-03-07 15:45:40 +0000118 | "linkonce_odr"
Dan Gohman890c31c2008-05-22 22:45:03 +0000119 | weak
Duncan Sands667d4b82009-03-07 15:45:40 +0000120 | "weak_odr"
Dan Gohman890c31c2008-05-22 22:45:03 +0000121 | dllexport
122 ;
123
Duncan Sands667d4b82009-03-07 15:45:40 +0000124AliasLinkage ::= + _ | weak | "weak_odr" | internal ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000125
126OptCallingConv ::= + _ |
127 ccc |
128 fastcc |
129 coldcc |
130 "x86_stdcallcc" |
131 "x86_fastcallcc" |
132 cc EUINT64VAL ;
133
134ParamAttr ::= zeroext
Dan Gohman890c31c2008-05-22 22:45:03 +0000135 | signext
Dan Gohman890c31c2008-05-22 22:45:03 +0000136 | inreg
137 | sret
138 | noalias
Dan Gohman571238a2009-01-05 03:21:23 +0000139 | nocapture
Dan Gohman890c31c2008-05-22 22:45:03 +0000140 | byval
141 | nest
142 | align EUINT64VAL
143 ;
144
145OptParamAttrs ::= + _ | OptParamAttrs ParamAttr ;
146
Dan Gohman571238a2009-01-05 03:21:23 +0000147RetAttr ::= inreg
148 | zeroext
149 | signext
150 | noalias
151 ;
152
153OptRetAttrs ::= _
154 | OptRetAttrs RetAttr
155 ;
156
Dan Gohman890c31c2008-05-22 22:45:03 +0000157FuncAttr ::= noreturn
158 | nounwind
Dan Gohman571238a2009-01-05 03:21:23 +0000159 | inreg
Dan Gohman890c31c2008-05-22 22:45:03 +0000160 | zeroext
161 | signext
162 | readnone
163 | readonly
Dale Johannesende86d472009-08-26 01:08:21 +0000164 | inlinehint
Dan Gohman571238a2009-01-05 03:21:23 +0000165 | noinline
166 | alwaysinline
167 | optsize
168 | ssp
169 | sspreq
Dan Gohman890c31c2008-05-22 22:45:03 +0000170 ;
171
172OptFuncAttrs ::= + _ | OptFuncAttrs FuncAttr ;
173
174OptGC ::= + _ | gc STRINGCONSTANT ;
175
176OptAlign ::= + _ | align EUINT64VAL ;
177OptCAlign ::= + _ | ^ "," align EUINT64VAL ;
178
179SectionString ::= section STRINGCONSTANT ;
180
181OptSection ::= + _ | SectionString ;
182
183GlobalVarAttributes ::= + _ | ^ "," GlobalVarAttribute GlobalVarAttributes ;
184GlobalVarAttribute ::= SectionString | align EUINT64VAL ;
185
186PrimType ::= INTTYPE | float | double | "ppc_fp128" | fp128 | "x86_fp80"
187 | - label ;
188
189Types
190 ::= opaque
191 | PrimType
192 | Types OptAddrSpace ^ "*"
193 | SymbolicValueRef
194 | "\\" ^ EUINT64VAL
195 | Types "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
196 | void "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
197 | "[" ^ EUINT64VAL "x" Types ^ "]"
198 | "<" ^ EUINT64VAL "x" Types ^ ">"
199 | "{" TypeListI "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000200 | "{" ^ "}"
Dan Gohman890c31c2008-05-22 22:45:03 +0000201 | "<" ^ "{" TypeListI "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000202 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000203 ;
204
205ArgType ::= Types OptParamAttrs ;
206
207ResultTypes ::= Types | void ;
208
209ArgTypeList ::= ArgType | ArgTypeList ^ "," ArgType ;
210
211ArgTypeListI ::= ArgTypeList | ArgTypeList ^ "," "..." | "..." | _ ;
212
213TypeListI ::= Types | TypeListI ^ "," Types ;
214
215ConstVal::= Types "[" ^ ConstVector ^ "]"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000216 | Types "[" ^ "]"
Dan Gohman890c31c2008-05-22 22:45:03 +0000217 | Types "c" ^ STRINGCONSTANT
218 | Types "<" ^ ConstVector ^ ">"
219 | Types "{" ConstVector "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000220 | Types "{" ^ "}"
Dan Gohman890c31c2008-05-22 22:45:03 +0000221 | Types "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000222 | Types "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000223 | Types null
224 | Types undef
225 | Types SymbolicValueRef
226 | Types ConstExpr
227 | Types zeroinitializer
Dan Gohman571238a2009-01-05 03:21:23 +0000228 | Types ESINT64VAL
229 | Types ESAPINTVAL
230 | Types EUINT64VAL
231 | Types EUAPINTVAL
232 | Types true
233 | Types false
234 | Types FPVAL ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000235
236ConstExpr::= CastOps "(" ^ ConstVal to Types ^ ")"
Dan Gohman224251d2009-07-27 21:55:32 +0000237 | getelementptr OptInBounds "(" ^ ConstVal IndexList ^ ")"
Dan Gohman890c31c2008-05-22 22:45:03 +0000238 | select "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
239 | ArithmeticOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
240 | LogicalOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
241 | icmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
242 | fcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
Dan Gohman890c31c2008-05-22 22:45:03 +0000243 | extractelement "(" ^ ConstVal ^ "," ConstVal ^ ")"
244 | insertelement "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohmane4977cf2008-05-23 01:55:30 +0000245 | shufflevector "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohman34e71fe2008-06-02 19:47:09 +0000246 | extractvalue "(" ^ ConstVal ^ ConstantIndexList ^ ")"
247 | insertvalue "(" ^ ConstVal ^ "," ConstVal ^ ConstantIndexList ^ ")" ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000248
249ConstVector ::= ConstVector ^ "," ConstVal | ConstVal ;
250
251GlobalType ::= global | constant ;
252
253ThreadLocal ::= - "thread_local" | _ ;
254
255AliaseeRef ::= ResultTypes SymbolicValueRef
256 | bitcast "(" ^ AliaseeRef to Types ^ ")" ;
257
258Module ::= +++ DefinitionList | --- _ ;
259
260DefinitionList ::= - Definition | + DefinitionList Definition ;
261
262Definition
263 ::= ^ ( +++++ define Function
264 | declare FunctionProto
265 | - module asm AsmBlock
266 | OptLocalAssign type Types
Dan Gohmande0e7f22009-01-05 23:03:03 +0000267 | OptGlobalAssign GVVisibilityStyle ThreadLocal OptAddrSpace GlobalType
Dan Gohman8f56eba2009-01-05 17:29:42 +0000268 ConstVal GlobalVarAttributes
269 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
270 GlobalType ConstVal GlobalVarAttributes
271 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
272 GlobalType Types GlobalVarAttributes
Dan Gohman890c31c2008-05-22 22:45:03 +0000273 | OptGlobalAssign GVVisibilityStyle alias AliasLinkage AliaseeRef
274 | target TargetDefinition
275 | deplibs "=" LibrariesDefinition
276 ) ^ "\n";
277
278AsmBlock ::= STRINGCONSTANT ;
279
280TargetDefinition ::= triple "=" STRINGCONSTANT
281 | datalayout "=" STRINGCONSTANT ;
282
Dan Gohman8f56eba2009-01-05 17:29:42 +0000283LibrariesDefinition ::= "[" ( LibList | _ ) "]";
Dan Gohman890c31c2008-05-22 22:45:03 +0000284
Dan Gohman8f56eba2009-01-05 17:29:42 +0000285LibList ::= LibList ^ "," STRINGCONSTANT | STRINGCONSTANT ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000286
287ArgListH ::= ArgListH ^ "," Types OptParamAttrs OptLocalName
288 | Types OptParamAttrs OptLocalName ;
289
290ArgList ::= ArgListH | ArgListH ^ "," "..." | "..." | _ ;
291
Dan Gohman571238a2009-01-05 03:21:23 +0000292FunctionHeaderH ::= OptCallingConv OptRetAttrs ResultTypes
Dan Gohman8f56eba2009-01-05 17:29:42 +0000293 GlobalName ^ "(" ^ ArgList ^ ")"
Dan Gohman571238a2009-01-05 03:21:23 +0000294 OptFuncAttrs OptSection OptAlign OptGC ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000295
296BEGIN ::= ( begin | "{" ) ^ "\n";
297
298FunctionHeader ::=
299 FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN ;
300
301END ::= ^ ( end | "}" ) ^ "\n";
302
303Function ::= BasicBlockList END ;
304
305FunctionProto ::= FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH ;
306
307OptSideEffect ::= _ | sideeffect ;
308
309ConstValueRef ::= ESINT64VAL
310 | EUINT64VAL
311 | FPVAL
312 | true
313 | false
314 | null
315 | undef
316 | zeroinitializer
317 | "<" ConstVector ">"
Dan Gohmanf910eaa2008-06-09 14:45:02 +0000318 | "[" ConstVector "]"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000319 | "[" ^ "]"
Dan Gohmanf910eaa2008-06-09 14:45:02 +0000320 | "c" ^ STRINGCONSTANT
321 | "{" ConstVector "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000322 | "{" ^ "}"
Dan Gohmanf910eaa2008-06-09 14:45:02 +0000323 | "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000324 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000325 | ConstExpr
326 | asm OptSideEffect STRINGCONSTANT ^ "," STRINGCONSTANT ;
327
328SymbolicValueRef ::= LOCALVALID
329 | GLOBALVALID
330 | LocalName
331 | GlobalName ;
332
333ValueRef ::= SymbolicValueRef | ConstValueRef;
334
335ResolvedVal ::= Types ValueRef ;
336
337ReturnedVal ::= ResolvedVal | ReturnedVal ^ "," ResolvedVal ;
338
339BasicBlockList ::= BasicBlockList BasicBlock | FunctionHeader BasicBlock ;
340
341BasicBlock ::= InstructionList OptLocalAssign BBTerminatorInst ;
342
343InstructionList ::= +++ InstructionList Inst
344 | - _
345 | ^ LABELSTR ^ ":\n" ;
346
347BBTerminatorInst ::= ^ " " ^
348 ( ret ReturnedVal
349 | ret void
350 | br label ValueRef
351 | br INTTYPE ValueRef ^ "," label ValueRef ^ "," label ValueRef
352 | switch IntType ValueRef ^ "," label ValueRef "[" JumpTable "]"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000353 | switch IntType ValueRef ^ "," label ValueRef "[" ^ "]"
354 | invoke OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
355 OptFuncAttrs
Dan Gohman890c31c2008-05-22 22:45:03 +0000356 to label ValueRef unwind label ValueRef
357 | unwind
358 | unreachable ) ^ "\n";
359
360JumpTable ::= JumpTable IntType ConstValueRef ^ "," label ValueRef
361 | IntType ConstValueRef ^ "," label ValueRef ;
362
363Inst ::= ^ " " ^ OptLocalAssign InstVal ^ "\n";
364
365PHIList ::= Types "[" ValueRef ^ "," ValueRef "]"
366 | PHIList ^ "," "[" ValueRef ^ "," ValueRef "]" ;
367
368ParamList ::= Types OptParamAttrs ValueRef OptParamAttrs
369 | label OptParamAttrs ValueRef OptParamAttrs
370 | ParamList ^ "," Types OptParamAttrs ValueRef OptParamAttrs
371 | ParamList ^ "," label OptParamAttrs ValueRef OptParamAttrs
372 | - _ ;
373
374IndexList ::= _ | IndexList ^ "," ResolvedVal ;
375
Dan Gohman34e71fe2008-06-02 19:47:09 +0000376ConstantIndexList ::= "," EUINT64VAL | ConstantIndexList ^ "," EUINT64VAL ;
377
Dan Gohman890c31c2008-05-22 22:45:03 +0000378OptTailCall ::= tail call | call ;
379
380InstVal ::=
381 ArithmeticOps Types ValueRef ^ "," ValueRef
382 | LogicalOps Types ValueRef ^ "," ValueRef
383 | icmp IPredicates Types ValueRef ^ "," ValueRef
384 | fcmp FPredicates Types ValueRef ^ "," ValueRef
Dan Gohman890c31c2008-05-22 22:45:03 +0000385 | CastOps ResolvedVal to Types
386 | select ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
Dan Gohmanb737a642008-05-23 17:11:55 +0000387 | "va_arg" ResolvedVal ^ "," Types
Dan Gohman890c31c2008-05-22 22:45:03 +0000388 | extractelement ResolvedVal ^ "," ResolvedVal
389 | insertelement ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
390 | shufflevector ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
391 | phi PHIList
Dan Gohman0d58eb62008-09-15 16:10:51 +0000392 | OptTailCall OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
Dan Gohman890c31c2008-05-22 22:45:03 +0000393 OptFuncAttrs
394 | MemoryInst ;
395
396OptVolatile ::= - volatile | _ ;
Dan Gohmanbfacb7e2009-07-22 22:45:30 +0000397OptExact ::= - exact | _ ;
398OptNSW ::= - nsw | _ ;
399OptNUW ::= - nuw | _ ;
400OptNW ::= OptNUW OptNSW ;
Dan Gohman224251d2009-07-27 21:55:32 +0000401OptInBounds ::= - inbounds | _ ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000402
403MemoryInst ::= malloc Types OptCAlign
404 | malloc Types ^ "," INTTYPE ValueRef OptCAlign
405 | alloca Types OptCAlign
406 | alloca Types ^ "," INTTYPE ValueRef OptCAlign
407 | free ResolvedVal
408 | OptVolatile load Types ValueRef OptCAlign
409 | OptVolatile store ResolvedVal ^ "," Types ValueRef OptCAlign
410 | getresult Types ValueRef ^ "," EUINT64VAL
Dan Gohman224251d2009-07-27 21:55:32 +0000411 | getelementptr OptInBounds Types ValueRef IndexList
Dan Gohman34e71fe2008-06-02 19:47:09 +0000412 | extractvalue Types ValueRef ^ ConstantIndexList
413 | insertvalue Types ValueRef ^ "," Types ValueRef ^ ConstantIndexList ;