blob: cbb72e92831a5aeabe5faefe5c37692112c26281 [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
Dan Gohman571238a2009-01-05 03:21:23 +0000164 | noinline
165 | alwaysinline
166 | optsize
167 | ssp
168 | sspreq
Dan Gohman890c31c2008-05-22 22:45:03 +0000169 ;
170
171OptFuncAttrs ::= + _ | OptFuncAttrs FuncAttr ;
172
173OptGC ::= + _ | gc STRINGCONSTANT ;
174
175OptAlign ::= + _ | align EUINT64VAL ;
176OptCAlign ::= + _ | ^ "," align EUINT64VAL ;
177
178SectionString ::= section STRINGCONSTANT ;
179
180OptSection ::= + _ | SectionString ;
181
182GlobalVarAttributes ::= + _ | ^ "," GlobalVarAttribute GlobalVarAttributes ;
183GlobalVarAttribute ::= SectionString | align EUINT64VAL ;
184
185PrimType ::= INTTYPE | float | double | "ppc_fp128" | fp128 | "x86_fp80"
186 | - label ;
187
188Types
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 "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000199 | "{" ^ "}"
Dan Gohman890c31c2008-05-22 22:45:03 +0000200 | "<" ^ "{" TypeListI "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000201 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000202 ;
203
204ArgType ::= Types OptParamAttrs ;
205
206ResultTypes ::= Types | void ;
207
208ArgTypeList ::= ArgType | ArgTypeList ^ "," ArgType ;
209
210ArgTypeListI ::= ArgTypeList | ArgTypeList ^ "," "..." | "..." | _ ;
211
212TypeListI ::= Types | TypeListI ^ "," Types ;
213
214ConstVal::= Types "[" ^ ConstVector ^ "]"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000215 | Types "[" ^ "]"
Dan Gohman890c31c2008-05-22 22:45:03 +0000216 | Types "c" ^ STRINGCONSTANT
217 | Types "<" ^ ConstVector ^ ">"
218 | Types "{" ConstVector "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000219 | Types "{" ^ "}"
Dan Gohman890c31c2008-05-22 22:45:03 +0000220 | Types "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000221 | Types "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000222 | Types null
223 | Types undef
224 | Types SymbolicValueRef
225 | Types ConstExpr
226 | Types zeroinitializer
Dan Gohman571238a2009-01-05 03:21:23 +0000227 | Types ESINT64VAL
228 | Types ESAPINTVAL
229 | Types EUINT64VAL
230 | Types EUAPINTVAL
231 | Types true
232 | Types false
233 | Types FPVAL ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000234
235ConstExpr::= 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 ^ ")"
Dan Gohman890c31c2008-05-22 22:45:03 +0000242 | extractelement "(" ^ ConstVal ^ "," ConstVal ^ ")"
243 | insertelement "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohmane4977cf2008-05-23 01:55:30 +0000244 | shufflevector "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohman34e71fe2008-06-02 19:47:09 +0000245 | extractvalue "(" ^ ConstVal ^ ConstantIndexList ^ ")"
246 | insertvalue "(" ^ ConstVal ^ "," ConstVal ^ ConstantIndexList ^ ")" ;
Dan Gohman890c31c2008-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 Gohmande0e7f22009-01-05 23:03:03 +0000266 | OptGlobalAssign GVVisibilityStyle ThreadLocal OptAddrSpace GlobalType
Dan Gohman8f56eba2009-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 Gohman890c31c2008-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 Gohman8f56eba2009-01-05 17:29:42 +0000282LibrariesDefinition ::= "[" ( LibList | _ ) "]";
Dan Gohman890c31c2008-05-22 22:45:03 +0000283
Dan Gohman8f56eba2009-01-05 17:29:42 +0000284LibList ::= LibList ^ "," STRINGCONSTANT | STRINGCONSTANT ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000285
286ArgListH ::= ArgListH ^ "," Types OptParamAttrs OptLocalName
287 | Types OptParamAttrs OptLocalName ;
288
289ArgList ::= ArgListH | ArgListH ^ "," "..." | "..." | _ ;
290
Dan Gohman571238a2009-01-05 03:21:23 +0000291FunctionHeaderH ::= OptCallingConv OptRetAttrs ResultTypes
Dan Gohman8f56eba2009-01-05 17:29:42 +0000292 GlobalName ^ "(" ^ ArgList ^ ")"
Dan Gohman571238a2009-01-05 03:21:23 +0000293 OptFuncAttrs OptSection OptAlign OptGC ;
Dan Gohman890c31c2008-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 Gohmanf910eaa2008-06-09 14:45:02 +0000317 | "[" ConstVector "]"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000318 | "[" ^ "]"
Dan Gohmanf910eaa2008-06-09 14:45:02 +0000319 | "c" ^ STRINGCONSTANT
320 | "{" ConstVector "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000321 | "{" ^ "}"
Dan Gohmanf910eaa2008-06-09 14:45:02 +0000322 | "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000323 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-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 Gohman0d58eb62008-09-15 16:10:51 +0000352 | switch IntType ValueRef ^ "," label ValueRef "[" ^ "]"
353 | invoke OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
354 OptFuncAttrs
Dan Gohman890c31c2008-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 Gohman34e71fe2008-06-02 19:47:09 +0000375ConstantIndexList ::= "," EUINT64VAL | ConstantIndexList ^ "," EUINT64VAL ;
376
Dan Gohman890c31c2008-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
Dan Gohman890c31c2008-05-22 22:45:03 +0000384 | CastOps ResolvedVal to Types
385 | select ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
Dan Gohmanb737a642008-05-23 17:11:55 +0000386 | "va_arg" ResolvedVal ^ "," Types
Dan Gohman890c31c2008-05-22 22:45:03 +0000387 | extractelement ResolvedVal ^ "," ResolvedVal
388 | insertelement ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
389 | shufflevector ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
390 | phi PHIList
Dan Gohman0d58eb62008-09-15 16:10:51 +0000391 | OptTailCall OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
Dan Gohman890c31c2008-05-22 22:45:03 +0000392 OptFuncAttrs
393 | MemoryInst ;
394
395OptVolatile ::= - volatile | _ ;
Dan Gohmanbfacb7e2009-07-22 22:45:30 +0000396OptExact ::= - exact | _ ;
397OptNSW ::= - nsw | _ ;
398OptNUW ::= - nuw | _ ;
399OptNW ::= OptNUW OptNSW ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000400
401MemoryInst ::= 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
Dan Gohmane4977cf2008-05-23 01:55:30 +0000409 | getelementptr Types ValueRef IndexList
Dan Gohman34e71fe2008-06-02 19:47:09 +0000410 | extractvalue Types ValueRef ^ ConstantIndexList
411 | insertvalue Types ValueRef ^ "," Types ValueRef ^ ConstantIndexList ;