blob: 2ca849d679763adcd09251d2b260dd61772e074c [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 Gohmanae3a0be2009-06-04 22:49:04 +000056ArithmeticOps ::= add | fadd | sub | fsub | mul | fmul |
57 udiv | 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
92 ;
93
94GVExternalLinkage
95 ::= dllimport
96 | "extern_weak"
97 | + external
98 ;
99
100GVVisibilityStyle
101 ::= + _
102 | default
103 | hidden
104 | protected
105 ;
106
107FunctionDeclareLinkage
108 ::= + _
109 | dllimport
110 | "extern_weak"
111 ;
112
113FunctionDefineLinkage
114 ::= + _
115 | internal
116 | linkonce
Duncan Sands667d4b82009-03-07 15:45:40 +0000117 | "linkonce_odr"
Dan Gohman890c31c2008-05-22 22:45:03 +0000118 | weak
Duncan Sands667d4b82009-03-07 15:45:40 +0000119 | "weak_odr"
Dan Gohman890c31c2008-05-22 22:45:03 +0000120 | dllexport
121 ;
122
Duncan Sands667d4b82009-03-07 15:45:40 +0000123AliasLinkage ::= + _ | weak | "weak_odr" | internal ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000124
125OptCallingConv ::= + _ |
126 ccc |
127 fastcc |
128 coldcc |
129 "x86_stdcallcc" |
130 "x86_fastcallcc" |
131 cc EUINT64VAL ;
132
133ParamAttr ::= zeroext
Dan Gohman890c31c2008-05-22 22:45:03 +0000134 | signext
Dan Gohman890c31c2008-05-22 22:45:03 +0000135 | inreg
136 | sret
137 | noalias
Dan Gohman571238a2009-01-05 03:21:23 +0000138 | nocapture
Dan Gohman890c31c2008-05-22 22:45:03 +0000139 | byval
140 | nest
141 | align EUINT64VAL
142 ;
143
144OptParamAttrs ::= + _ | OptParamAttrs ParamAttr ;
145
Dan Gohman571238a2009-01-05 03:21:23 +0000146RetAttr ::= inreg
147 | zeroext
148 | signext
149 | noalias
150 ;
151
152OptRetAttrs ::= _
153 | OptRetAttrs RetAttr
154 ;
155
Dan Gohman890c31c2008-05-22 22:45:03 +0000156FuncAttr ::= noreturn
157 | nounwind
Dan Gohman571238a2009-01-05 03:21:23 +0000158 | inreg
Dan Gohman890c31c2008-05-22 22:45:03 +0000159 | zeroext
160 | signext
161 | readnone
162 | readonly
Dan Gohman571238a2009-01-05 03:21:23 +0000163 | noinline
164 | alwaysinline
165 | optsize
166 | ssp
167 | sspreq
Dan Gohman890c31c2008-05-22 22:45:03 +0000168 ;
169
170OptFuncAttrs ::= + _ | OptFuncAttrs FuncAttr ;
171
172OptGC ::= + _ | gc STRINGCONSTANT ;
173
174OptAlign ::= + _ | align EUINT64VAL ;
175OptCAlign ::= + _ | ^ "," align EUINT64VAL ;
176
177SectionString ::= section STRINGCONSTANT ;
178
179OptSection ::= + _ | SectionString ;
180
181GlobalVarAttributes ::= + _ | ^ "," GlobalVarAttribute GlobalVarAttributes ;
182GlobalVarAttribute ::= SectionString | align EUINT64VAL ;
183
184PrimType ::= INTTYPE | float | double | "ppc_fp128" | fp128 | "x86_fp80"
185 | - label ;
186
187Types
188 ::= opaque
189 | PrimType
190 | Types OptAddrSpace ^ "*"
191 | SymbolicValueRef
192 | "\\" ^ EUINT64VAL
193 | Types "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
194 | void "(" ^ ArgTypeListI ^ ")" OptFuncAttrs
195 | "[" ^ EUINT64VAL "x" Types ^ "]"
196 | "<" ^ EUINT64VAL "x" Types ^ ">"
197 | "{" TypeListI "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000198 | "{" ^ "}"
Dan Gohman890c31c2008-05-22 22:45:03 +0000199 | "<" ^ "{" TypeListI "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000200 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000201 ;
202
203ArgType ::= Types OptParamAttrs ;
204
205ResultTypes ::= Types | void ;
206
207ArgTypeList ::= ArgType | ArgTypeList ^ "," ArgType ;
208
209ArgTypeListI ::= ArgTypeList | ArgTypeList ^ "," "..." | "..." | _ ;
210
211TypeListI ::= Types | TypeListI ^ "," Types ;
212
213ConstVal::= Types "[" ^ ConstVector ^ "]"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000214 | Types "[" ^ "]"
Dan Gohman890c31c2008-05-22 22:45:03 +0000215 | Types "c" ^ STRINGCONSTANT
216 | Types "<" ^ ConstVector ^ ">"
217 | Types "{" ConstVector "}"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000218 | Types "{" ^ "}"
Dan Gohman890c31c2008-05-22 22:45:03 +0000219 | Types "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohman0d58eb62008-09-15 16:10:51 +0000220 | Types "<" ^ "{" ^ "}" ^ ">"
Dan Gohman890c31c2008-05-22 22:45:03 +0000221 | Types null
222 | Types undef
223 | Types SymbolicValueRef
224 | Types ConstExpr
225 | Types zeroinitializer
Dan Gohman571238a2009-01-05 03:21:23 +0000226 | Types ESINT64VAL
227 | Types ESAPINTVAL
228 | Types EUINT64VAL
229 | Types EUAPINTVAL
230 | Types true
231 | Types false
232 | Types FPVAL ;
Dan Gohman890c31c2008-05-22 22:45:03 +0000233
234ConstExpr::= CastOps "(" ^ ConstVal to Types ^ ")"
235 | getelementptr "(" ^ ConstVal IndexList ^ ")"
236 | select "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
237 | ArithmeticOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
238 | LogicalOps "(" ^ ConstVal ^ "," ConstVal ^ ")"
239 | icmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
240 | fcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
241 | vicmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
242 | vfcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")"
243 | 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
385 | vicmp IPredicates Types ValueRef ^ "," ValueRef
386 | vfcmp FPredicates Types ValueRef ^ "," ValueRef
387 | CastOps ResolvedVal to Types
388 | select ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
Dan Gohmanb737a642008-05-23 17:11:55 +0000389 | "va_arg" ResolvedVal ^ "," Types
Dan Gohman890c31c2008-05-22 22:45:03 +0000390 | extractelement ResolvedVal ^ "," ResolvedVal
391 | insertelement ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
392 | shufflevector ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
393 | phi PHIList
Dan Gohman0d58eb62008-09-15 16:10:51 +0000394 | OptTailCall OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
Dan Gohman890c31c2008-05-22 22:45:03 +0000395 OptFuncAttrs
396 | MemoryInst ;
397
398OptVolatile ::= - volatile | _ ;
399
400MemoryInst ::= malloc Types OptCAlign
401 | malloc Types ^ "," INTTYPE ValueRef OptCAlign
402 | alloca Types OptCAlign
403 | alloca Types ^ "," INTTYPE ValueRef OptCAlign
404 | free ResolvedVal
405 | OptVolatile load Types ValueRef OptCAlign
406 | OptVolatile store ResolvedVal ^ "," Types ValueRef OptCAlign
407 | getresult Types ValueRef ^ "," EUINT64VAL
Dan Gohmane4977cf2008-05-23 01:55:30 +0000408 | getelementptr Types ValueRef IndexList
Dan Gohman34e71fe2008-06-02 19:47:09 +0000409 | extractvalue Types ValueRef ^ ConstantIndexList
410 | insertvalue Types ValueRef ^ "," Types ValueRef ^ ConstantIndexList ;