blob: 05083bfede6a91d14d6d3bf632eafaf1ef7dc816 [file] [log] [blame]
Dan Gohman29b46c92008-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
56ArithmeticOps ::= add | sub | mul | udiv | sdiv | fdiv | urem | srem | frem ;
57LogicalOps ::= shl | lshr | ashr | and | or | xor;
58CastOps ::= trunc | zext | sext | fptrunc | fpext | bitcast |
59 uitofp | sitofp | fptoui | fptosi | inttoptr | ptrtoint ;
60
61IPredicates ::= eq | ne | slt | sgt | sle | sge | ult | ugt | ule | uge ;
62
63FPredicates ::= oeq | one | olt | ogt | ole | oge | ord | uno | ueq | une
64 | ult | ugt | ule | uge | true | false ;
65
66IntType ::= INTTYPE;
67FPType ::= float | double | "ppc_fp128" | fp128 | "x86_fp80";
68
69LocalName ::= LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ;
70OptLocalName ::= LocalName | _ ;
71
Dan Gohman844695c2009-01-05 17:29:42 +000072OptAddrSpace ::= - addrspace ^ "(" ^ EUINT64VAL ^ ")" | _ ;
Dan Gohman29b46c92008-05-22 22:45:03 +000073
74OptLocalAssign ::= LocalName "=" | _ ;
75
76GlobalName ::= GLOBALVAR | ATSTRINGCONSTANT ;
77
78OptGlobalAssign ::= GlobalAssign | _ ;
79
80GlobalAssign ::= GlobalName "=" ;
81
82GVInternalLinkage
83 ::= + internal
84 | weak
Duncan Sands19d161f2009-03-07 15:45:40 +000085 | "weak_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +000086 | linkonce
Duncan Sands19d161f2009-03-07 15:45:40 +000087 | "linkonce_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +000088 | appending
89 | dllexport
90 | common
91 ;
92
93GVExternalLinkage
94 ::= dllimport
95 | "extern_weak"
96 | + external
97 ;
98
99GVVisibilityStyle
100 ::= + _
101 | default
102 | hidden
103 | protected
104 ;
105
106FunctionDeclareLinkage
107 ::= + _
108 | dllimport
109 | "extern_weak"
110 ;
111
112FunctionDefineLinkage
113 ::= + _
114 | internal
115 | linkonce
Duncan Sands19d161f2009-03-07 15:45:40 +0000116 | "linkonce_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +0000117 | weak
Duncan Sands19d161f2009-03-07 15:45:40 +0000118 | "weak_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +0000119 | dllexport
120 ;
121
Duncan Sands19d161f2009-03-07 15:45:40 +0000122AliasLinkage ::= + _ | weak | "weak_odr" | internal ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000123
124OptCallingConv ::= + _ |
125 ccc |
126 fastcc |
127 coldcc |
128 "x86_stdcallcc" |
129 "x86_fastcallcc" |
130 cc EUINT64VAL ;
131
132ParamAttr ::= zeroext
Dan Gohman29b46c92008-05-22 22:45:03 +0000133 | signext
Dan Gohman29b46c92008-05-22 22:45:03 +0000134 | inreg
135 | sret
136 | noalias
Dan Gohman713c0732009-01-05 03:21:23 +0000137 | nocapture
Dan Gohman29b46c92008-05-22 22:45:03 +0000138 | byval
139 | nest
140 | align EUINT64VAL
141 ;
142
143OptParamAttrs ::= + _ | OptParamAttrs ParamAttr ;
144
Dan Gohman713c0732009-01-05 03:21:23 +0000145RetAttr ::= inreg
146 | zeroext
147 | signext
148 | noalias
149 ;
150
151OptRetAttrs ::= _
152 | OptRetAttrs RetAttr
153 ;
154
Dan Gohman29b46c92008-05-22 22:45:03 +0000155FuncAttr ::= noreturn
156 | nounwind
Dan Gohman713c0732009-01-05 03:21:23 +0000157 | inreg
Dan Gohman29b46c92008-05-22 22:45:03 +0000158 | zeroext
159 | signext
160 | readnone
161 | readonly
Dan Gohman713c0732009-01-05 03:21:23 +0000162 | noinline
163 | alwaysinline
164 | optsize
165 | ssp
166 | sspreq
Dan Gohman29b46c92008-05-22 22:45:03 +0000167 ;
168
169OptFuncAttrs ::= + _ | OptFuncAttrs FuncAttr ;
170
171OptGC ::= + _ | gc STRINGCONSTANT ;
172
173OptAlign ::= + _ | align EUINT64VAL ;
174OptCAlign ::= + _ | ^ "," align EUINT64VAL ;
175
176SectionString ::= section STRINGCONSTANT ;
177
178OptSection ::= + _ | SectionString ;
179
180GlobalVarAttributes ::= + _ | ^ "," GlobalVarAttribute GlobalVarAttributes ;
181GlobalVarAttribute ::= SectionString | align EUINT64VAL ;
182
183PrimType ::= INTTYPE | float | double | "ppc_fp128" | fp128 | "x86_fp80"
184 | - label ;
185
186Types
187 ::= opaque
188 | PrimType
189 | Types OptAddrSpace ^ "*"
190 | SymbolicValueRef
191 | "\\" ^ EUINT64VAL
192 | Types "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
193 | void "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
194 | "[" ^ EUINT64VAL "x" Types ^ "]"
195 | "<" ^ EUINT64VAL "x" Types ^ ">"
196 | "{" TypeListI "}"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000197 | "{" ^ "}"
Dan Gohman29b46c92008-05-22 22:45:03 +0000198 | "<" ^ "{" TypeListI "}" ^ ">"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000199 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman29b46c92008-05-22 22:45:03 +0000200 ;
201
202ArgType ::= Types OptParamAttrs ;
203
204ResultTypes ::= Types | void ;
205
206ArgTypeList ::= ArgType | ArgTypeList ^ "," ArgType ;
207
208ArgTypeListI ::= ArgTypeList | ArgTypeList ^ "," "..." | "..." | _ ;
209
210TypeListI ::= Types | TypeListI ^ "," Types ;
211
212ConstVal::= Types "[" ^ ConstVector ^ "]"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000213 | Types "[" ^ "]"
Dan Gohman29b46c92008-05-22 22:45:03 +0000214 | Types "c" ^ STRINGCONSTANT
215 | Types "<" ^ ConstVector ^ ">"
216 | Types "{" ConstVector "}"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000217 | Types "{" ^ "}"
Dan Gohman29b46c92008-05-22 22:45:03 +0000218 | Types "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000219 | Types "<" ^ "{" ^ "}" ^ ">"
Dan Gohman29b46c92008-05-22 22:45:03 +0000220 | Types null
221 | Types undef
222 | Types SymbolicValueRef
223 | Types ConstExpr
224 | Types zeroinitializer
Dan Gohman713c0732009-01-05 03:21:23 +0000225 | Types ESINT64VAL
226 | Types ESAPINTVAL
227 | Types EUINT64VAL
228 | Types EUAPINTVAL
229 | Types true
230 | Types false
231 | Types FPVAL ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000232
233ConstExpr::= CastOps "(" ^ ConstVal to Types ^ ")"
234 | getelementptr "(" ^ ConstVal IndexList ^ ")"
235 | select "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
236 | ArithmeticOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
237 | LogicalOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
238 | icmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
239 | fcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
240 | vicmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
241 | vfcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
242 | extractelement "(" ^ ConstVal ^ "," ConstVal ^ ")"
243 | insertelement "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000244 | shufflevector "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohmanb48ded92008-06-02 19:47:09 +0000245 | extractvalue "(" ^ ConstVal ^ ConstantIndexList ^ ")"
246 | insertvalue "(" ^ ConstVal ^ "," ConstVal ^ ConstantIndexList ^ ")" ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000247
248ConstVector ::= ConstVector ^ "," ConstVal | ConstVal ;
249
250GlobalType ::= global | constant ;
251
252ThreadLocal ::= - "thread_local" | _ ;
253
254AliaseeRef ::= ResultTypes SymbolicValueRef
255 | bitcast "(" ^ AliaseeRef to Types ^ ")" ;
256
257Module ::= +++ DefinitionList | --- _ ;
258
259DefinitionList ::= - Definition | + DefinitionList Definition ;
260
261Definition
262 ::= ^ ( +++++ define Function
263 | declare FunctionProto
264 | - module asm AsmBlock
265 | OptLocalAssign type Types
Dan Gohman4b0d9d82009-01-05 23:03:03 +0000266 | OptGlobalAssign GVVisibilityStyle ThreadLocal OptAddrSpace GlobalType
Dan Gohman844695c2009-01-05 17:29:42 +0000267 ConstVal GlobalVarAttributes
268 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
269 GlobalType ConstVal GlobalVarAttributes
270 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
271 GlobalType Types GlobalVarAttributes
Dan Gohman29b46c92008-05-22 22:45:03 +0000272 | OptGlobalAssign GVVisibilityStyle alias AliasLinkage AliaseeRef
273 | target TargetDefinition
274 | deplibs "=" LibrariesDefinition
275 ) ^ "\n";
276
277AsmBlock ::= STRINGCONSTANT ;
278
279TargetDefinition ::= triple "=" STRINGCONSTANT
280 | datalayout "=" STRINGCONSTANT ;
281
Dan Gohman844695c2009-01-05 17:29:42 +0000282LibrariesDefinition ::= "[" ( LibList | _ ) "]";
Dan Gohman29b46c92008-05-22 22:45:03 +0000283
Dan Gohman844695c2009-01-05 17:29:42 +0000284LibList ::= LibList ^ "," STRINGCONSTANT | STRINGCONSTANT ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000285
286ArgListH ::= ArgListH ^ "," Types OptParamAttrs OptLocalName
287 | Types OptParamAttrs OptLocalName ;
288
289ArgList ::= ArgListH | ArgListH ^ "," "..." | "..." | _ ;
290
Dan Gohman713c0732009-01-05 03:21:23 +0000291FunctionHeaderH ::= OptCallingConv OptRetAttrs ResultTypes
Dan Gohman844695c2009-01-05 17:29:42 +0000292 GlobalName ^ "(" ^ ArgList ^ ")"
Dan Gohman713c0732009-01-05 03:21:23 +0000293 OptFuncAttrs OptSection OptAlign OptGC ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000294
295BEGIN ::= ( begin | "{" ) ^ "\n";
296
297FunctionHeader ::=
298 FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN ;
299
300END ::= ^ ( end | "}" ) ^ "\n";
301
302Function ::= BasicBlockList END ;
303
304FunctionProto ::= FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH ;
305
306OptSideEffect ::= _ | sideeffect ;
307
308ConstValueRef ::= ESINT64VAL
309 | EUINT64VAL
310 | FPVAL
311 | true
312 | false
313 | null
314 | undef
315 | zeroinitializer
316 | "<" ConstVector ">"
Dan Gohman9fc6cb02008-06-09 14:45:02 +0000317 | "[" ConstVector "]"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000318 | "[" ^ "]"
Dan Gohman9fc6cb02008-06-09 14:45:02 +0000319 | "c" ^ STRINGCONSTANT
320 | "{" ConstVector "}"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000321 | "{" ^ "}"
Dan Gohman9fc6cb02008-06-09 14:45:02 +0000322 | "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000323 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman29b46c92008-05-22 22:45:03 +0000324 | ConstExpr
325 | asm OptSideEffect STRINGCONSTANT ^ "," STRINGCONSTANT ;
326
327SymbolicValueRef ::= LOCALVALID
328 | GLOBALVALID
329 | LocalName
330 | GlobalName ;
331
332ValueRef ::= SymbolicValueRef | ConstValueRef;
333
334ResolvedVal ::= Types ValueRef ;
335
336ReturnedVal ::= ResolvedVal | ReturnedVal ^ "," ResolvedVal ;
337
338BasicBlockList ::= BasicBlockList BasicBlock | FunctionHeader BasicBlock ;
339
340BasicBlock ::= InstructionList OptLocalAssign BBTerminatorInst ;
341
342InstructionList ::= +++ InstructionList Inst
343 | - _
344 | ^ LABELSTR ^ ":\n" ;
345
346BBTerminatorInst ::= ^ " " ^
347 ( ret ReturnedVal
348 | ret void
349 | br label ValueRef
350 | br INTTYPE ValueRef ^ "," label ValueRef ^ "," label ValueRef
351 | switch IntType ValueRef ^ "," label ValueRef "[" JumpTable "]"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000352 | switch IntType ValueRef ^ "," label ValueRef "[" ^ "]"
353 | invoke OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
354 OptFuncAttrs
Dan Gohman29b46c92008-05-22 22:45:03 +0000355 to label ValueRef unwind label ValueRef
356 | unwind
357 | unreachable ) ^ "\n";
358
359JumpTable ::= JumpTable IntType ConstValueRef ^ "," label ValueRef
360 | IntType ConstValueRef ^ "," label ValueRef ;
361
362Inst ::= ^ " " ^ OptLocalAssign InstVal ^ "\n";
363
364PHIList ::= Types "[" ValueRef ^ "," ValueRef "]"
365 | PHIList ^ "," "[" ValueRef ^ "," ValueRef "]" ;
366
367ParamList ::= Types OptParamAttrs ValueRef OptParamAttrs
368 | label OptParamAttrs ValueRef OptParamAttrs
369 | ParamList ^ "," Types OptParamAttrs ValueRef OptParamAttrs
370 | ParamList ^ "," label OptParamAttrs ValueRef OptParamAttrs
371 | - _ ;
372
373IndexList ::= _ | IndexList ^ "," ResolvedVal ;
374
Dan Gohmanb48ded92008-06-02 19:47:09 +0000375ConstantIndexList ::= "," EUINT64VAL | ConstantIndexList ^ "," EUINT64VAL ;
376
Dan Gohman29b46c92008-05-22 22:45:03 +0000377OptTailCall ::= tail call | call ;
378
379InstVal ::=
380 ArithmeticOps Types ValueRef ^ "," ValueRef
381 | LogicalOps Types ValueRef ^ "," ValueRef
382 | icmp IPredicates Types ValueRef ^ "," ValueRef
383 | fcmp FPredicates Types ValueRef ^ "," ValueRef
384 | vicmp IPredicates Types ValueRef ^ "," ValueRef
385 | vfcmp FPredicates Types ValueRef ^ "," ValueRef
386 | CastOps ResolvedVal to Types
387 | select ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
Dan Gohman0f0195b2008-05-23 17:11:55 +0000388 | "va_arg" ResolvedVal ^ "," Types
Dan Gohman29b46c92008-05-22 22:45:03 +0000389 | extractelement ResolvedVal ^ "," ResolvedVal
390 | insertelement ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
391 | shufflevector ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
392 | phi PHIList
Dan Gohmaneecdb832008-09-15 16:10:51 +0000393 | OptTailCall OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
Dan Gohman29b46c92008-05-22 22:45:03 +0000394 OptFuncAttrs
395 | MemoryInst ;
396
397OptVolatile ::= - volatile | _ ;
398
399MemoryInst ::= malloc Types OptCAlign
400 | malloc Types ^ "," INTTYPE ValueRef OptCAlign
401 | alloca Types OptCAlign
402 | alloca Types ^ "," INTTYPE ValueRef OptCAlign
403 | free ResolvedVal
404 | OptVolatile load Types ValueRef OptCAlign
405 | OptVolatile store ResolvedVal ^ "," Types ValueRef OptCAlign
406 | getresult Types ValueRef ^ "," EUINT64VAL
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000407 | getelementptr Types ValueRef IndexList
Dan Gohmanb48ded92008-06-02 19:47:09 +0000408 | extractvalue Types ValueRef ^ ConstantIndexList
409 | insertvalue Types ValueRef ^ "," Types ValueRef ^ ConstantIndexList ;