Make structs and arrays first-class types, and add assembly
[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  | linkonce
86  | appending
87  | dllexport
88  | common
89  ;
90
91 GVExternalLinkage
92   ::= dllimport
93  | "extern_weak"
94  | + external
95  ;
96
97 GVVisibilityStyle
98   ::= + _
99  | default
100  | hidden
101  | protected
102  ;
103
104 FunctionDeclareLinkage
105   ::= + _
106  | dllimport
107  | "extern_weak"
108  ;
109
110 FunctionDefineLinkage
111   ::= + _
112  | internal
113  | linkonce
114  | weak
115  | dllexport
116  ;
117
118 AliasLinkage ::= + _ | weak | internal ;
119
120 OptCallingConv ::= + _ |
121                  ccc |
122                  fastcc |
123                  coldcc |
124                  "x86_stdcallcc" |
125                  "x86_fastcallcc" |
126                  cc EUINT64VAL ;
127
128 ParamAttr ::= zeroext
129  | zext
130  | signext
131  | sext
132  | inreg
133  | sret
134  | noalias
135  | byval
136  | nest
137  | align EUINT64VAL
138  ;
139
140 OptParamAttrs ::= + _ | OptParamAttrs ParamAttr ;
141
142 FuncAttr      ::= noreturn
143  | nounwind
144  | zeroext
145  | signext
146  | readnone
147  | readonly
148  ;
149
150 OptFuncAttrs  ::= + _ | OptFuncAttrs FuncAttr ;
151
152 OptGC         ::= + _ | gc STRINGCONSTANT ;
153
154 OptAlign      ::= + _ | align EUINT64VAL ;
155 OptCAlign     ::= + _ | ^ "," align EUINT64VAL ;
156
157 SectionString ::= section STRINGCONSTANT ;
158
159 OptSection    ::= + _ | SectionString ;
160
161 GlobalVarAttributes ::= + _ | ^ "," GlobalVarAttribute GlobalVarAttributes ;
162 GlobalVarAttribute  ::= SectionString | align EUINT64VAL ;
163
164 PrimType ::= INTTYPE | float | double | "ppc_fp128" | fp128 | "x86_fp80"
165           | - label ;
166
167 Types
168   ::= opaque
169  | PrimType
170  | Types OptAddrSpace ^ "*"
171  | SymbolicValueRef
172  | "\\" ^ EUINT64VAL
173  | Types "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
174  | void "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
175  | "[" ^ EUINT64VAL "x" Types ^ "]"
176  | "<" ^ EUINT64VAL "x" Types ^ ">"
177  | "{" TypeListI "}"
178  | "{" "}"
179  | "<" ^ "{" TypeListI "}" ^ ">"
180  | "<" ^ "{" "}" ^ ">"
181  ;
182
183 ArgType ::= Types OptParamAttrs ;
184
185 ResultTypes ::= Types | void ;
186
187 ArgTypeList ::= ArgType | ArgTypeList ^ "," ArgType ;
188
189 ArgTypeListI ::= ArgTypeList | ArgTypeList ^ "," "..." | "..." | _ ;
190
191 TypeListI ::= Types | TypeListI ^ "," Types ;
192
193 ConstVal::= Types "[" ^ ConstVector ^ "]"
194  | Types "[" "]"
195  | Types "c" ^ STRINGCONSTANT
196  | Types "<" ^ ConstVector ^ ">"
197  | Types "{" ConstVector "}"
198  | Types "{" "}"
199  | Types "<" ^ "{" ConstVector "}" ^ ">"
200  | Types "<" ^ "{" "}" ^ ">"
201  | Types null
202  | Types undef
203  | Types SymbolicValueRef
204  | Types ConstExpr
205  | Types zeroinitializer
206  | IntType ESINT64VAL
207  | IntType ESAPINTVAL
208  | IntType EUINT64VAL
209  | IntType EUAPINTVAL
210  | INTTYPE true
211  | INTTYPE false
212  | FPType FPVAL ;
213
214 ConstExpr::= CastOps "(" ^ ConstVal to Types ^ ")"
215  | getelementptr "(" ^ ConstVal IndexList ^ ")"
216  | select "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
217  | ArithmeticOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
218  | LogicalOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
219  | icmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
220  | fcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
221  | vicmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
222  | vfcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
223  | extractelement "(" ^ ConstVal ^ "," ConstVal ^ ")"
224  | insertelement "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
225  | shufflevector "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
226  | extractvalue "(" ^ ConstVal IndexList ^ ")"
227  | insertvalue "(" ^ ConstVal ^ "," ConstVal IndexList ^ ")" ;
228
229 ConstVector ::= ConstVector ^ "," ConstVal | ConstVal ;
230
231 GlobalType ::= global | constant ;
232
233 ThreadLocal ::= - "thread_local" | _ ;
234
235 AliaseeRef ::= ResultTypes SymbolicValueRef
236  | bitcast "(" ^ AliaseeRef to Types ^ ")" ;
237
238 Module ::= +++ DefinitionList | --- _ ;
239
240 DefinitionList ::= - Definition | + DefinitionList Definition ;
241
242 Definition
243   ::= ^ ( +++++ define Function
244  | declare FunctionProto
245  | - module asm AsmBlock
246  | OptLocalAssign type Types
247  | OptLocalAssign type void
248  | OptGlobalAssign GVVisibilityStyle ThreadLocal GlobalType ConstVal
249    OptAddrSpace GlobalVarAttributes
250  | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal GlobalType
251    ConstVal OptAddrSpace GlobalVarAttributes
252  | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal GlobalType
253    Types OptAddrSpace GlobalVarAttributes
254  | OptGlobalAssign GVVisibilityStyle alias AliasLinkage AliaseeRef
255  | target TargetDefinition
256  | deplibs "=" LibrariesDefinition
257  ) ^ "\n";
258
259 AsmBlock ::= STRINGCONSTANT ;
260
261 TargetDefinition ::= triple "=" STRINGCONSTANT
262  | datalayout "=" STRINGCONSTANT ;
263
264 LibrariesDefinition ::= "[" LibList "]";
265
266 LibList ::= LibList ^ "," STRINGCONSTANT | STRINGCONSTANT | _ ;
267
268 ArgListH ::= ArgListH ^ "," Types OptParamAttrs OptLocalName
269  | Types OptParamAttrs OptLocalName ;
270
271 ArgList ::= ArgListH | ArgListH ^ "," "..." | "..." | _ ;
272
273 FunctionHeaderH ::= OptCallingConv ResultTypes GlobalName "(" ^ ArgList ^ ")"
274                   OptFuncAttrs OptSection OptAlign OptGC ;
275
276 BEGIN ::= ( begin | "{" ) ^ "\n";
277
278 FunctionHeader ::=
279   FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN ;
280
281 END ::= ^ ( end | "}" ) ^ "\n";
282
283 Function ::= BasicBlockList END ;
284
285 FunctionProto ::= FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH ;
286
287 OptSideEffect ::= _ | sideeffect ;
288
289 ConstValueRef ::= ESINT64VAL
290  | EUINT64VAL
291  | FPVAL
292  | true
293  | false
294  | null
295  | undef
296  | zeroinitializer
297  | "<" ConstVector ">"
298  | ConstExpr
299  | asm OptSideEffect STRINGCONSTANT ^ "," STRINGCONSTANT ;
300
301 SymbolicValueRef ::= LOCALVALID
302  | GLOBALVALID
303  | LocalName
304  | GlobalName ;
305
306 ValueRef ::= SymbolicValueRef | ConstValueRef;
307
308 ResolvedVal ::= Types ValueRef ;
309
310 ReturnedVal ::= ResolvedVal | ReturnedVal ^ "," ResolvedVal ;
311
312 BasicBlockList ::= BasicBlockList BasicBlock | FunctionHeader BasicBlock ;
313
314 BasicBlock ::= InstructionList OptLocalAssign BBTerminatorInst ;
315
316 InstructionList ::= +++ InstructionList Inst
317  | - _
318  | ^ LABELSTR ^ ":\n" ;
319
320 BBTerminatorInst ::= ^ "  " ^
321  ( ret ReturnedVal
322  | ret void
323  | br label ValueRef
324  | br INTTYPE ValueRef ^ "," label ValueRef ^ "," label ValueRef
325  | switch IntType ValueRef ^ "," label ValueRef "[" JumpTable "]"
326  | switch IntType ValueRef ^ "," label ValueRef "[" "]"
327  | invoke OptCallingConv ResultTypes ValueRef "(" ^ ParamList ^ ")" OptFuncAttrs
328    to label ValueRef unwind label ValueRef
329  | unwind
330  | unreachable ) ^ "\n";
331
332 JumpTable ::= JumpTable IntType ConstValueRef ^ "," label ValueRef
333  | IntType ConstValueRef ^ "," label ValueRef ;
334
335 Inst ::= ^ "  " ^ OptLocalAssign InstVal ^ "\n";
336
337 PHIList ::= Types "[" ValueRef ^ "," ValueRef "]"
338  | PHIList ^ "," "[" ValueRef ^ "," ValueRef "]" ;
339
340 ParamList ::= Types OptParamAttrs ValueRef OptParamAttrs
341  | label OptParamAttrs ValueRef OptParamAttrs
342  | ParamList ^ "," Types OptParamAttrs ValueRef OptParamAttrs
343  | ParamList ^ "," label OptParamAttrs ValueRef OptParamAttrs
344  | - _ ;
345
346 IndexList ::= _ | IndexList ^ "," ResolvedVal ;
347
348 OptTailCall ::= tail call | call ;
349
350 InstVal ::=
351    ArithmeticOps Types ValueRef ^ "," ValueRef
352  | LogicalOps Types ValueRef ^ "," ValueRef
353  | icmp IPredicates Types ValueRef ^ "," ValueRef
354  | fcmp FPredicates Types ValueRef ^ "," ValueRef
355  | vicmp IPredicates Types ValueRef ^ "," ValueRef
356  | vfcmp FPredicates Types ValueRef ^ "," ValueRef
357  | CastOps ResolvedVal to Types
358  | select ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
359  | vaarg ResolvedVal ^ "," Types
360  | extractelement ResolvedVal ^ "," ResolvedVal
361  | insertelement ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
362  | shufflevector ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
363  | phi PHIList
364  | OptTailCall OptCallingConv ResultTypes ValueRef "(" ^ ParamList ^ ")"
365    OptFuncAttrs
366  | MemoryInst ;
367
368 OptVolatile ::= - volatile | _ ;
369
370 MemoryInst ::= malloc Types OptCAlign
371  | malloc Types ^ "," INTTYPE ValueRef OptCAlign
372  | alloca Types OptCAlign
373  | alloca Types ^ "," INTTYPE ValueRef OptCAlign
374  | free ResolvedVal
375  | OptVolatile load Types ValueRef OptCAlign
376  | OptVolatile store ResolvedVal ^ "," Types ValueRef OptCAlign
377  | getresult Types ValueRef ^ "," EUINT64VAL
378  | getelementptr Types ValueRef IndexList
379  | extractvalue Types ValueRef IndexList 
380  | insertvalue Types ValueRef ^ "," Types ValueRef IndexList ;