It makes no sense to have a ODR version of common
[oota-llvm.git] / utils / llvm.grm
1 (*
2
3 polygen grammar for LLVM assembly language.
4
5 This file defines an LLVM assembly language grammar for polygen,
6 which is a tool for generating random text based on a grammar.
7 It is strictly syntax-based, and makes no attempt to generate
8 IR that is semantically valid. Most of the IR produced doesn't
9 pass the Verifier.
10
11 *)
12
13 I ::=   "title:    LLVM assembly language\n"
14       ^ "status:   experimental\n"
15       ^ "audience: LLVM developers\n"
16 ;
17
18 S ::= Module ;
19
20 (*
21 Define rules for non-keyword tokens. This is currently just a bunch
22 of hacks. They don't cover many valid forms of tokens, and they also
23 generate some invalid forms of tokens. The LLVM parser has custom
24 C++ code to lex these; custom C++ code for emitting them would be
25 convenient, but polygen doesn't support that.
26 *)
27 NonZeroDecimalDigit ::=     1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ;
28 DecimalDigit        ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ;
29 DecimalDigitSeq     ::= DecimalDigit [^ DecimalDigitSeq ];
30 HexDigit            ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
31                       | a | b | c | d | e | f ;
32 HexDigitSeq         ::= HexDigit [^ HexDigitSeq ];
33 StringChar          ::= 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 ;
35 StringConstantSeq   ::= StringChar [^ StringConstantSeq ];
36 StringConstant      ::= StringChar [^ StringConstantSeq ];
37 EUINT64VAL          ::= NonZeroDecimalDigit [^ DecimalDigitSeq ];
38 ESINT64VAL          ::= [ "-" ] ^ EUINT64VAL ;
39 EUAPINTVAL          ::= EUINT64VAL ;
40 ESAPINTVAL          ::= ESINT64VAL ;
41 LOCALVALID          ::= "%" ^ DecimalDigitSeq ;
42 GLOBALVALID         ::= "@" ^ DecimalDigitSeq ;
43 INTTYPE             ::= "i" ^ EUINT64VAL ;
44 GLOBALVAR           ::= "@" ^ StringConstant ;
45 LOCALVAR            ::= "%" ^ StringConstant ;
46 STRINGCONSTANT      ::= "\"" ^ StringConstant ^ "\"" ;
47 ATSTRINGCONSTANT    ::= "@" ^ STRINGCONSTANT ;
48 PCTSTRINGCONSTANT   ::= "%" ^ STRINGCONSTANT ;
49 LABELSTR            ::= StringConstant ;
50 FPVAL               ::= ESAPINTVAL ^ "." ^ EUAPINTVAL | "0x" ^ HexDigitSeq ;
51
52 (*
53 The rest of this file is derived directly from llvmAsmParser.y.
54 *)
55
56 ArithmeticOps ::= add | sub | mul | udiv | sdiv | fdiv | urem | srem | frem ;
57 LogicalOps    ::= shl | lshr | ashr | and | or | xor;
58 CastOps       ::= trunc | zext | sext | fptrunc | fpext | bitcast |
59                   uitofp | sitofp | fptoui | fptosi | inttoptr | ptrtoint ;
60
61 IPredicates ::= eq | ne | slt | sgt | sle | sge | ult | ugt | ule | uge ;
62
63 FPredicates ::= oeq | one | olt | ogt | ole | oge | ord | uno | ueq | une
64               | ult | ugt | ule | uge | true | false ;
65
66 IntType ::= INTTYPE;
67 FPType  ::= float | double | "ppc_fp128" | fp128 | "x86_fp80";
68
69 LocalName ::= LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
70 OptLocalName ::= LocalName | _ ;
71
72 OptAddrSpace ::= - addrspace ^ "(" ^ EUINT64VAL ^ ")" | _ ;
73
74 OptLocalAssign ::= LocalName "=" | _ ;
75
76 GlobalName ::= GLOBALVAR | ATSTRINGCONSTANT ;
77
78 OptGlobalAssign ::= GlobalAssign | _ ;
79
80 GlobalAssign ::= GlobalName "=" ;
81
82 GVInternalLinkage
83   ::= + internal
84  | weak
85  | "weak_odr"
86  | linkonce
87  | "linkonce_odr"
88  | appending
89  | dllexport
90  | common
91  ;
92
93 GVExternalLinkage
94   ::= dllimport
95  | "extern_weak"
96  | + external
97  ;
98
99 GVVisibilityStyle
100   ::= + _
101  | default
102  | hidden
103  | protected
104  ;
105
106 FunctionDeclareLinkage
107   ::= + _
108  | dllimport
109  | "extern_weak"
110  ;
111
112 FunctionDefineLinkage
113   ::= + _
114  | internal
115  | linkonce
116  | "linkonce_odr"
117  | weak
118  | "weak_odr"
119  | dllexport
120  ;
121
122 AliasLinkage ::= + _ | weak | "weak_odr" | internal ;
123
124 OptCallingConv ::= + _ |
125                  ccc |
126                  fastcc |
127                  coldcc |
128                  "x86_stdcallcc" |
129                  "x86_fastcallcc" |
130                  cc EUINT64VAL ;
131
132 ParamAttr ::= zeroext
133  | zext
134  | signext
135  | sext
136  | inreg
137  | sret
138  | noalias
139  | nocapture
140  | byval
141  | nest
142  | align EUINT64VAL
143  ;
144
145 OptParamAttrs ::= + _ | OptParamAttrs ParamAttr ;
146
147 RetAttr       ::= inreg
148               | zeroext
149               | signext
150               | noalias
151               ;
152
153 OptRetAttrs  ::= _
154              | OptRetAttrs RetAttr
155              ;
156
157 FuncAttr      ::= noreturn
158  | nounwind
159  | inreg
160  | zeroext
161  | signext
162  | readnone
163  | readonly
164  | noinline
165  | alwaysinline
166  | optsize
167  | ssp
168  | sspreq
169  ;
170
171 OptFuncAttrs  ::= + _ | OptFuncAttrs FuncAttr ;
172
173 OptGC         ::= + _ | gc STRINGCONSTANT ;
174
175 OptAlign      ::= + _ | align EUINT64VAL ;
176 OptCAlign     ::= + _ | ^ "," align EUINT64VAL ;
177
178 SectionString ::= section STRINGCONSTANT ;
179
180 OptSection    ::= + _ | SectionString ;
181
182 GlobalVarAttributes ::= + _ | ^ "," GlobalVarAttribute GlobalVarAttributes ;
183 GlobalVarAttribute  ::= SectionString | align EUINT64VAL ;
184
185 PrimType ::= INTTYPE | float | double | "ppc_fp128" | fp128 | "x86_fp80"
186           | - label ;
187
188 Types
189   ::= opaque
190  | PrimType
191  | Types OptAddrSpace ^ "*"
192  | SymbolicValueRef
193  | "\\" ^ EUINT64VAL
194  | Types "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
195  | void "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
196  | "[" ^ EUINT64VAL "x" Types ^ "]"
197  | "<" ^ EUINT64VAL "x" Types ^ ">"
198  | "{" TypeListI "}"
199  | "{" ^ "}"
200  | "<" ^ "{" TypeListI "}" ^ ">"
201  | "<" ^ "{" ^ "}" ^ ">"
202  ;
203
204 ArgType ::= Types OptParamAttrs ;
205
206 ResultTypes ::= Types | void ;
207
208 ArgTypeList ::= ArgType | ArgTypeList ^ "," ArgType ;
209
210 ArgTypeListI ::= ArgTypeList | ArgTypeList ^ "," "..." | "..." | _ ;
211
212 TypeListI ::= Types | TypeListI ^ "," Types ;
213
214 ConstVal::= Types "[" ^ ConstVector ^ "]"
215  | Types "[" ^ "]"
216  | Types "c" ^ STRINGCONSTANT
217  | Types "<" ^ ConstVector ^ ">"
218  | Types "{" ConstVector "}"
219  | Types "{" ^ "}"
220  | Types "<" ^ "{" ConstVector "}" ^ ">"
221  | Types "<" ^ "{" ^ "}" ^ ">"
222  | Types null
223  | Types undef
224  | Types SymbolicValueRef
225  | Types ConstExpr
226  | Types zeroinitializer
227  | Types ESINT64VAL
228  | Types ESAPINTVAL
229  | Types EUINT64VAL
230  | Types EUAPINTVAL
231  | Types true
232  | Types false
233  | Types FPVAL ;
234
235 ConstExpr::= CastOps "(" ^ ConstVal to Types ^ ")"
236  | getelementptr "(" ^ ConstVal IndexList ^ ")"
237  | select "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
238  | ArithmeticOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
239  | LogicalOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
240  | icmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
241  | fcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
242  | vicmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
243  | vfcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
244  | extractelement "(" ^ ConstVal ^ "," ConstVal ^ ")"
245  | insertelement "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
246  | shufflevector "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
247  | extractvalue "(" ^ ConstVal ^ ConstantIndexList ^ ")"
248  | insertvalue "(" ^ ConstVal ^ "," ConstVal ^ ConstantIndexList ^ ")" ;
249
250 ConstVector ::= ConstVector ^ "," ConstVal | ConstVal ;
251
252 GlobalType ::= global | constant ;
253
254 ThreadLocal ::= - "thread_local" | _ ;
255
256 AliaseeRef ::= ResultTypes SymbolicValueRef
257  | bitcast "(" ^ AliaseeRef to Types ^ ")" ;
258
259 Module ::= +++ DefinitionList | --- _ ;
260
261 DefinitionList ::= - Definition | + DefinitionList Definition ;
262
263 Definition
264   ::= ^ ( +++++ define Function
265  | declare FunctionProto
266  | - module asm AsmBlock
267  | OptLocalAssign type Types
268  | OptGlobalAssign GVVisibilityStyle ThreadLocal OptAddrSpace GlobalType
269    ConstVal GlobalVarAttributes
270  | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
271    GlobalType ConstVal GlobalVarAttributes
272  | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
273    GlobalType Types GlobalVarAttributes
274  | OptGlobalAssign GVVisibilityStyle alias AliasLinkage AliaseeRef
275  | target TargetDefinition
276  | deplibs "=" LibrariesDefinition
277  ) ^ "\n";
278
279 AsmBlock ::= STRINGCONSTANT ;
280
281 TargetDefinition ::= triple "=" STRINGCONSTANT
282  | datalayout "=" STRINGCONSTANT ;
283
284 LibrariesDefinition ::= "[" ( LibList | _ ) "]";
285
286 LibList ::= LibList ^ "," STRINGCONSTANT | STRINGCONSTANT ;
287
288 ArgListH ::= ArgListH ^ "," Types OptParamAttrs OptLocalName
289  | Types OptParamAttrs OptLocalName ;
290
291 ArgList ::= ArgListH | ArgListH ^ "," "..." | "..." | _ ;
292
293 FunctionHeaderH ::= OptCallingConv OptRetAttrs ResultTypes
294                   GlobalName ^ "(" ^ ArgList ^ ")"
295                   OptFuncAttrs OptSection OptAlign OptGC ;
296
297 BEGIN ::= ( begin | "{" ) ^ "\n";
298
299 FunctionHeader ::=
300   FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN ;
301
302 END ::= ^ ( end | "}" ) ^ "\n";
303
304 Function ::= BasicBlockList END ;
305
306 FunctionProto ::= FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH ;
307
308 OptSideEffect ::= _ | sideeffect ;
309
310 ConstValueRef ::= ESINT64VAL
311  | EUINT64VAL
312  | FPVAL
313  | true
314  | false
315  | null
316  | undef
317  | zeroinitializer
318  | "<" ConstVector ">"
319  | "[" ConstVector "]"
320  | "[" ^ "]"
321  | "c" ^ STRINGCONSTANT
322  | "{" ConstVector "}"
323  | "{" ^ "}"
324  | "<" ^ "{" ConstVector "}" ^ ">"
325  | "<" ^ "{" ^ "}" ^ ">"
326  | ConstExpr
327  | asm OptSideEffect STRINGCONSTANT ^ "," STRINGCONSTANT ;
328
329 SymbolicValueRef ::= LOCALVALID
330  | GLOBALVALID
331  | LocalName
332  | GlobalName ;
333
334 ValueRef ::= SymbolicValueRef | ConstValueRef;
335
336 ResolvedVal ::= Types ValueRef ;
337
338 ReturnedVal ::= ResolvedVal | ReturnedVal ^ "," ResolvedVal ;
339
340 BasicBlockList ::= BasicBlockList BasicBlock | FunctionHeader BasicBlock ;
341
342 BasicBlock ::= InstructionList OptLocalAssign BBTerminatorInst ;
343
344 InstructionList ::= +++ InstructionList Inst
345  | - _
346  | ^ LABELSTR ^ ":\n" ;
347
348 BBTerminatorInst ::= ^ "  " ^
349  ( ret ReturnedVal
350  | ret void
351  | br label ValueRef
352  | br INTTYPE ValueRef ^ "," label ValueRef ^ "," label ValueRef
353  | switch IntType ValueRef ^ "," label ValueRef "[" JumpTable "]"
354  | switch IntType ValueRef ^ "," label ValueRef "[" ^ "]"
355  | invoke OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
356    OptFuncAttrs
357    to label ValueRef unwind label ValueRef
358  | unwind
359  | unreachable ) ^ "\n";
360
361 JumpTable ::= JumpTable IntType ConstValueRef ^ "," label ValueRef
362  | IntType ConstValueRef ^ "," label ValueRef ;
363
364 Inst ::= ^ "  " ^ OptLocalAssign InstVal ^ "\n";
365
366 PHIList ::= Types "[" ValueRef ^ "," ValueRef "]"
367  | PHIList ^ "," "[" ValueRef ^ "," ValueRef "]" ;
368
369 ParamList ::= Types OptParamAttrs ValueRef OptParamAttrs
370  | label OptParamAttrs ValueRef OptParamAttrs
371  | ParamList ^ "," Types OptParamAttrs ValueRef OptParamAttrs
372  | ParamList ^ "," label OptParamAttrs ValueRef OptParamAttrs
373  | - _ ;
374
375 IndexList ::= _ | IndexList ^ "," ResolvedVal ;
376
377 ConstantIndexList ::= "," EUINT64VAL | ConstantIndexList ^ "," EUINT64VAL ;
378
379 OptTailCall ::= tail call | call ;
380
381 InstVal ::=
382    ArithmeticOps Types ValueRef ^ "," ValueRef
383  | LogicalOps Types ValueRef ^ "," ValueRef
384  | icmp IPredicates Types ValueRef ^ "," ValueRef
385  | fcmp FPredicates Types ValueRef ^ "," ValueRef
386  | vicmp IPredicates Types ValueRef ^ "," ValueRef
387  | vfcmp FPredicates Types ValueRef ^ "," ValueRef
388  | CastOps ResolvedVal to Types
389  | select ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
390  | "va_arg" ResolvedVal ^ "," Types
391  | extractelement ResolvedVal ^ "," ResolvedVal
392  | insertelement ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
393  | shufflevector ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
394  | phi PHIList
395  | OptTailCall OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
396    OptFuncAttrs
397  | MemoryInst ;
398
399 OptVolatile ::= - volatile | _ ;
400
401 MemoryInst ::= malloc Types OptCAlign
402  | malloc Types ^ "," INTTYPE ValueRef OptCAlign
403  | alloca Types OptCAlign
404  | alloca Types ^ "," INTTYPE ValueRef OptCAlign
405  | free ResolvedVal
406  | OptVolatile load Types ValueRef OptCAlign
407  | OptVolatile store ResolvedVal ^ "," Types ValueRef OptCAlign
408  | getresult Types ValueRef ^ "," EUINT64VAL
409  | getelementptr Types ValueRef IndexList
410  | extractvalue Types ValueRef ^ ConstantIndexList 
411  | insertvalue Types ValueRef ^ "," Types ValueRef ^ ConstantIndexList ;