blob: 1426e3b98e24884aa9ad46bace19804330ac3b84 [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
Dan Gohman7ce405e2009-06-04 22:49:04 +000056ArithmeticOps ::= add | fadd | sub | fsub | mul | fmul |
57 udiv | sdiv | fdiv | urem | srem | frem ;
Dan Gohman29b46c92008-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 Gohman844695c2009-01-05 17:29:42 +000073OptAddrSpace ::= - addrspace ^ "(" ^ EUINT64VAL ^ ")" | _ ;
Dan Gohman29b46c92008-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 Sands19d161f2009-03-07 15:45:40 +000086 | "weak_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +000087 | linkonce
Duncan Sands19d161f2009-03-07 15:45:40 +000088 | "linkonce_odr"
Dan Gohman29b46c92008-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 Sands19d161f2009-03-07 15:45:40 +0000117 | "linkonce_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +0000118 | weak
Duncan Sands19d161f2009-03-07 15:45:40 +0000119 | "weak_odr"
Dan Gohman29b46c92008-05-22 22:45:03 +0000120 | dllexport
121 ;
122
Duncan Sands19d161f2009-03-07 15:45:40 +0000123AliasLinkage ::= + _ | weak | "weak_odr" | internal ;
Dan Gohman29b46c92008-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 Gohman29b46c92008-05-22 22:45:03 +0000134 | signext
Dan Gohman29b46c92008-05-22 22:45:03 +0000135 | inreg
136 | sret
137 | noalias
Dan Gohman713c0732009-01-05 03:21:23 +0000138 | nocapture
Dan Gohman29b46c92008-05-22 22:45:03 +0000139 | byval
140 | nest
141 | align EUINT64VAL
142 ;
143
144OptParamAttrs ::= + _ | OptParamAttrs ParamAttr ;
145
Dan Gohman713c0732009-01-05 03:21:23 +0000146RetAttr ::= inreg
147 | zeroext
148 | signext
149 | noalias
150 ;
151
152OptRetAttrs ::= _
153 | OptRetAttrs RetAttr
154 ;
155
Dan Gohman29b46c92008-05-22 22:45:03 +0000156FuncAttr ::= noreturn
157 | nounwind
Dan Gohman713c0732009-01-05 03:21:23 +0000158 | inreg
Dan Gohman29b46c92008-05-22 22:45:03 +0000159 | zeroext
160 | signext
161 | readnone
162 | readonly
Dan Gohman713c0732009-01-05 03:21:23 +0000163 | noinline
164 | alwaysinline
165 | optsize
166 | ssp
167 | sspreq
Dan Gohman29b46c92008-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 Gohmaneecdb832008-09-15 16:10:51 +0000198 | "{" ^ "}"
Dan Gohman29b46c92008-05-22 22:45:03 +0000199 | "<" ^ "{" TypeListI "}" ^ ">"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000200 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman29b46c92008-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 Gohmaneecdb832008-09-15 16:10:51 +0000214 | Types "[" ^ "]"
Dan Gohman29b46c92008-05-22 22:45:03 +0000215 | Types "c" ^ STRINGCONSTANT
216 | Types "<" ^ ConstVector ^ ">"
217 | Types "{" ConstVector "}"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000218 | Types "{" ^ "}"
Dan Gohman29b46c92008-05-22 22:45:03 +0000219 | Types "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000220 | Types "<" ^ "{" ^ "}" ^ ">"
Dan Gohman29b46c92008-05-22 22:45:03 +0000221 | Types null
222 | Types undef
223 | Types SymbolicValueRef
224 | Types ConstExpr
225 | Types zeroinitializer
Dan Gohman713c0732009-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 Gohman29b46c92008-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 ^ ")"
Dan Gohman29b46c92008-05-22 22:45:03 +0000241 | extractelement "(" ^ ConstVal ^ "," ConstVal ^ ")"
242 | insertelement "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000243 | shufflevector "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")"
Dan Gohmanb48ded92008-06-02 19:47:09 +0000244 | extractvalue "(" ^ ConstVal ^ ConstantIndexList ^ ")"
245 | insertvalue "(" ^ ConstVal ^ "," ConstVal ^ ConstantIndexList ^ ")" ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000246
247ConstVector ::= ConstVector ^ "," ConstVal | ConstVal ;
248
249GlobalType ::= global | constant ;
250
251ThreadLocal ::= - "thread_local" | _ ;
252
253AliaseeRef ::= ResultTypes SymbolicValueRef
254 | bitcast "(" ^ AliaseeRef to Types ^ ")" ;
255
256Module ::= +++ DefinitionList | --- _ ;
257
258DefinitionList ::= - Definition | + DefinitionList Definition ;
259
260Definition
261 ::= ^ ( +++++ define Function
262 | declare FunctionProto
263 | - module asm AsmBlock
264 | OptLocalAssign type Types
Dan Gohman4b0d9d82009-01-05 23:03:03 +0000265 | OptGlobalAssign GVVisibilityStyle ThreadLocal OptAddrSpace GlobalType
Dan Gohman844695c2009-01-05 17:29:42 +0000266 ConstVal GlobalVarAttributes
267 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
268 GlobalType ConstVal GlobalVarAttributes
269 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace
270 GlobalType Types GlobalVarAttributes
Dan Gohman29b46c92008-05-22 22:45:03 +0000271 | OptGlobalAssign GVVisibilityStyle alias AliasLinkage AliaseeRef
272 | target TargetDefinition
273 | deplibs "=" LibrariesDefinition
274 ) ^ "\n";
275
276AsmBlock ::= STRINGCONSTANT ;
277
278TargetDefinition ::= triple "=" STRINGCONSTANT
279 | datalayout "=" STRINGCONSTANT ;
280
Dan Gohman844695c2009-01-05 17:29:42 +0000281LibrariesDefinition ::= "[" ( LibList | _ ) "]";
Dan Gohman29b46c92008-05-22 22:45:03 +0000282
Dan Gohman844695c2009-01-05 17:29:42 +0000283LibList ::= LibList ^ "," STRINGCONSTANT | STRINGCONSTANT ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000284
285ArgListH ::= ArgListH ^ "," Types OptParamAttrs OptLocalName
286 | Types OptParamAttrs OptLocalName ;
287
288ArgList ::= ArgListH | ArgListH ^ "," "..." | "..." | _ ;
289
Dan Gohman713c0732009-01-05 03:21:23 +0000290FunctionHeaderH ::= OptCallingConv OptRetAttrs ResultTypes
Dan Gohman844695c2009-01-05 17:29:42 +0000291 GlobalName ^ "(" ^ ArgList ^ ")"
Dan Gohman713c0732009-01-05 03:21:23 +0000292 OptFuncAttrs OptSection OptAlign OptGC ;
Dan Gohman29b46c92008-05-22 22:45:03 +0000293
294BEGIN ::= ( begin | "{" ) ^ "\n";
295
296FunctionHeader ::=
297 FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN ;
298
299END ::= ^ ( end | "}" ) ^ "\n";
300
301Function ::= BasicBlockList END ;
302
303FunctionProto ::= FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH ;
304
305OptSideEffect ::= _ | sideeffect ;
306
307ConstValueRef ::= ESINT64VAL
308 | EUINT64VAL
309 | FPVAL
310 | true
311 | false
312 | null
313 | undef
314 | zeroinitializer
315 | "<" ConstVector ">"
Dan Gohman9fc6cb02008-06-09 14:45:02 +0000316 | "[" ConstVector "]"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000317 | "[" ^ "]"
Dan Gohman9fc6cb02008-06-09 14:45:02 +0000318 | "c" ^ STRINGCONSTANT
319 | "{" ConstVector "}"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000320 | "{" ^ "}"
Dan Gohman9fc6cb02008-06-09 14:45:02 +0000321 | "<" ^ "{" ConstVector "}" ^ ">"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000322 | "<" ^ "{" ^ "}" ^ ">"
Dan Gohman29b46c92008-05-22 22:45:03 +0000323 | ConstExpr
324 | asm OptSideEffect STRINGCONSTANT ^ "," STRINGCONSTANT ;
325
326SymbolicValueRef ::= LOCALVALID
327 | GLOBALVALID
328 | LocalName
329 | GlobalName ;
330
331ValueRef ::= SymbolicValueRef | ConstValueRef;
332
333ResolvedVal ::= Types ValueRef ;
334
335ReturnedVal ::= ResolvedVal | ReturnedVal ^ "," ResolvedVal ;
336
337BasicBlockList ::= BasicBlockList BasicBlock | FunctionHeader BasicBlock ;
338
339BasicBlock ::= InstructionList OptLocalAssign BBTerminatorInst ;
340
341InstructionList ::= +++ InstructionList Inst
342 | - _
343 | ^ LABELSTR ^ ":\n" ;
344
345BBTerminatorInst ::= ^ " " ^
346 ( ret ReturnedVal
347 | ret void
348 | br label ValueRef
349 | br INTTYPE ValueRef ^ "," label ValueRef ^ "," label ValueRef
350 | switch IntType ValueRef ^ "," label ValueRef "[" JumpTable "]"
Dan Gohmaneecdb832008-09-15 16:10:51 +0000351 | switch IntType ValueRef ^ "," label ValueRef "[" ^ "]"
352 | invoke OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
353 OptFuncAttrs
Dan Gohman29b46c92008-05-22 22:45:03 +0000354 to label ValueRef unwind label ValueRef
355 | unwind
356 | unreachable ) ^ "\n";
357
358JumpTable ::= JumpTable IntType ConstValueRef ^ "," label ValueRef
359 | IntType ConstValueRef ^ "," label ValueRef ;
360
361Inst ::= ^ " " ^ OptLocalAssign InstVal ^ "\n";
362
363PHIList ::= Types "[" ValueRef ^ "," ValueRef "]"
364 | PHIList ^ "," "[" ValueRef ^ "," ValueRef "]" ;
365
366ParamList ::= Types OptParamAttrs ValueRef OptParamAttrs
367 | label OptParamAttrs ValueRef OptParamAttrs
368 | ParamList ^ "," Types OptParamAttrs ValueRef OptParamAttrs
369 | ParamList ^ "," label OptParamAttrs ValueRef OptParamAttrs
370 | - _ ;
371
372IndexList ::= _ | IndexList ^ "," ResolvedVal ;
373
Dan Gohmanb48ded92008-06-02 19:47:09 +0000374ConstantIndexList ::= "," EUINT64VAL | ConstantIndexList ^ "," EUINT64VAL ;
375
Dan Gohman29b46c92008-05-22 22:45:03 +0000376OptTailCall ::= tail call | call ;
377
378InstVal ::=
379 ArithmeticOps Types ValueRef ^ "," ValueRef
380 | LogicalOps Types ValueRef ^ "," ValueRef
381 | icmp IPredicates Types ValueRef ^ "," ValueRef
382 | fcmp FPredicates Types ValueRef ^ "," ValueRef
Dan Gohman29b46c92008-05-22 22:45:03 +0000383 | CastOps ResolvedVal to Types
384 | select ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
Dan Gohman0f0195b2008-05-23 17:11:55 +0000385 | "va_arg" ResolvedVal ^ "," Types
Dan Gohman29b46c92008-05-22 22:45:03 +0000386 | extractelement ResolvedVal ^ "," ResolvedVal
387 | insertelement ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
388 | shufflevector ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal
389 | phi PHIList
Dan Gohmaneecdb832008-09-15 16:10:51 +0000390 | OptTailCall OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")"
Dan Gohman29b46c92008-05-22 22:45:03 +0000391 OptFuncAttrs
392 | MemoryInst ;
393
394OptVolatile ::= - volatile | _ ;
395
396MemoryInst ::= malloc Types OptCAlign
397 | malloc Types ^ "," INTTYPE ValueRef OptCAlign
398 | alloca Types OptCAlign
399 | alloca Types ^ "," INTTYPE ValueRef OptCAlign
400 | free ResolvedVal
401 | OptVolatile load Types ValueRef OptCAlign
402 | OptVolatile store ResolvedVal ^ "," Types ValueRef OptCAlign
403 | getresult Types ValueRef ^ "," EUINT64VAL
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000404 | getelementptr Types ValueRef IndexList
Dan Gohmanb48ded92008-06-02 19:47:09 +0000405 | extractvalue Types ValueRef ^ ConstantIndexList
406 | insertvalue Types ValueRef ^ "," Types ValueRef ^ ConstantIndexList ;