John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame^] | 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 | TODO: Metadata, in all its forms |
| 12 | TODO: Union types |
| 13 | |
| 14 | *) |
| 15 | |
| 16 | I ::= "title: LLVM assembly language\n" |
| 17 | ^ "status: experimental\n" |
| 18 | ^ "audience: LLVM developers\n" |
| 19 | ; |
| 20 | |
| 21 | S ::= Module ; |
| 22 | |
| 23 | (* |
| 24 | Define rules for non-keyword tokens. This is currently just a bunch |
| 25 | of hacks. They don't cover many valid forms of tokens, and they also |
| 26 | generate some invalid forms of tokens. The LLVM parser has custom |
| 27 | C++ code to lex these; custom C++ code for emitting them would be |
| 28 | convenient, but polygen doesn't support that. |
| 29 | *) |
| 30 | NonZeroDecimalDigit ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ; |
| 31 | DecimalDigit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ; |
| 32 | DecimalDigitSeq ::= DecimalDigit [^ DecimalDigitSeq ]; |
| 33 | HexDigit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 34 | | a | b | c | d | e | f ; |
| 35 | HexDigitSeq ::= HexDigit [^ HexDigitSeq ]; |
| 36 | StringChar ::= a | b | c | d | e | f | g | h | i | j | k | l | m |
| 37 | | n | o | p | q | r | s | t | u | v | w | x | y | z ; |
| 38 | StringConstantSeq ::= StringChar [^ StringConstantSeq ]; |
| 39 | StringConstant ::= StringChar [^ StringConstantSeq ]; |
| 40 | EUINT64VAL ::= NonZeroDecimalDigit [^ DecimalDigitSeq ]; |
| 41 | ESINT64VAL ::= [ "-" ] ^ EUINT64VAL ; |
| 42 | EUAPINTVAL ::= EUINT64VAL ; |
| 43 | ESAPINTVAL ::= ESINT64VAL ; |
| 44 | LOCALVALID ::= "%" ^ DecimalDigitSeq ; |
| 45 | GLOBALVALID ::= "@" ^ DecimalDigitSeq ; |
| 46 | INTTYPE ::= "i" ^ EUINT64VAL ; |
| 47 | GLOBALVAR ::= "@" ^ StringConstant ; |
| 48 | LOCALVAR ::= "%" ^ StringConstant ; |
| 49 | STRINGCONSTANT ::= "\"" ^ StringConstant ^ "\"" ; |
| 50 | ATSTRINGCONSTANT ::= "@" ^ STRINGCONSTANT ; |
| 51 | PCTSTRINGCONSTANT ::= "%" ^ STRINGCONSTANT ; |
| 52 | LABELSTR ::= StringConstant ; |
| 53 | FPVAL ::= ESAPINTVAL ^ "." ^ EUAPINTVAL | "0x" ^ HexDigitSeq ; |
| 54 | |
| 55 | (* |
| 56 | The rest of this file is derived directly from llvmAsmParser.y. |
| 57 | *) |
| 58 | |
| 59 | ArithmeticOps ::= + OptNW add | fadd | OptNW sub | fsub | OptNW mul | fmul | |
| 60 | udiv | OptExact sdiv | fdiv | urem | srem | frem ; |
| 61 | LogicalOps ::= shl | lshr | ashr | and | or | xor; |
| 62 | CastOps ::= trunc | zext | sext | fptrunc | fpext | bitcast | |
| 63 | uitofp | sitofp | fptoui | fptosi | inttoptr | ptrtoint ; |
| 64 | |
| 65 | IPredicates ::= eq | ne | slt | sgt | sle | sge | ult | ugt | ule | uge ; |
| 66 | |
| 67 | FPredicates ::= oeq | one | olt | ogt | ole | oge | ord | uno | ueq | une |
| 68 | | ult | ugt | ule | uge | true | false ; |
| 69 | |
| 70 | IntType ::= INTTYPE; |
| 71 | FPType ::= float | double | "ppc_fp128" | fp128 | "x86_fp80"; |
| 72 | |
| 73 | LocalName ::= LOCALVAR | STRINGCONSTANT | PCTSTRINGCONSTANT ; |
| 74 | OptLocalName ::= LocalName | _ ; |
| 75 | |
| 76 | OptAddrSpace ::= - addrspace ^ "(" ^ EUINT64VAL ^ ")" | _ ; |
| 77 | |
| 78 | OptLocalAssign ::= LocalName "=" | _ ; |
| 79 | |
| 80 | GlobalName ::= GLOBALVAR | ATSTRINGCONSTANT ; |
| 81 | |
| 82 | OptGlobalAssign ::= GlobalAssign | _ ; |
| 83 | |
| 84 | GlobalAssign ::= GlobalName "=" ; |
| 85 | |
| 86 | GVInternalLinkage |
| 87 | ::= + internal |
| 88 | | weak |
| 89 | | "weak_odr" |
| 90 | | linkonce |
| 91 | | "linkonce_odr" |
| 92 | | appending |
| 93 | | dllexport |
| 94 | | common |
| 95 | | private |
| 96 | | "linker_private" |
| 97 | | "linker_private_weak" |
| 98 | ; |
| 99 | |
| 100 | GVExternalLinkage |
| 101 | ::= dllimport |
| 102 | | "extern_weak" |
| 103 | | + external |
| 104 | ; |
| 105 | |
| 106 | GVVisibilityStyle |
| 107 | ::= + _ |
| 108 | | default |
| 109 | | hidden |
| 110 | | protected |
| 111 | ; |
| 112 | |
| 113 | FunctionDeclareLinkage |
| 114 | ::= + _ |
| 115 | | dllimport |
| 116 | | "extern_weak" |
| 117 | ; |
| 118 | |
| 119 | FunctionDefineLinkage |
| 120 | ::= + _ |
| 121 | | internal |
| 122 | | linkonce |
| 123 | | "linkonce_odr" |
| 124 | | weak |
| 125 | | "weak_odr" |
| 126 | | dllexport |
| 127 | ; |
| 128 | |
| 129 | AliasLinkage ::= + _ | weak | "weak_odr" | internal ; |
| 130 | |
| 131 | OptCallingConv ::= + _ | |
| 132 | ccc | |
| 133 | fastcc | |
| 134 | coldcc | |
| 135 | "x86_stdcallcc" | |
| 136 | "x86_fastcallcc" | |
| 137 | cc EUINT64VAL ; |
| 138 | |
| 139 | ParamAttr ::= zeroext |
| 140 | | signext |
| 141 | | inreg |
| 142 | | sret |
| 143 | | noalias |
| 144 | | nocapture |
| 145 | | byval |
| 146 | | nest |
| 147 | | align EUINT64VAL |
| 148 | ; |
| 149 | |
| 150 | OptParamAttrs ::= + _ | OptParamAttrs ParamAttr ; |
| 151 | |
| 152 | RetAttr ::= inreg |
| 153 | | zeroext |
| 154 | | signext |
| 155 | | noalias |
| 156 | ; |
| 157 | |
| 158 | OptRetAttrs ::= _ |
| 159 | | OptRetAttrs RetAttr |
| 160 | ; |
| 161 | |
| 162 | FuncAttr ::= noreturn |
| 163 | | nounwind |
| 164 | | inreg |
| 165 | | zeroext |
| 166 | | signext |
| 167 | | readnone |
| 168 | | readonly |
| 169 | | inlinehint |
| 170 | | alignstack |
| 171 | | noinline |
| 172 | | alwaysinline |
| 173 | | optsize |
| 174 | | ssp |
| 175 | | sspreq |
| 176 | ; |
| 177 | |
| 178 | OptFuncAttrs ::= + _ | OptFuncAttrs FuncAttr ; |
| 179 | |
| 180 | OptGC ::= + _ | gc STRINGCONSTANT ; |
| 181 | |
| 182 | OptAlign ::= + _ | align EUINT64VAL ; |
| 183 | OptCAlign ::= + _ | ^ "," align EUINT64VAL ; |
| 184 | |
| 185 | SectionString ::= section STRINGCONSTANT ; |
| 186 | |
| 187 | OptSection ::= + _ | SectionString ; |
| 188 | |
| 189 | GlobalVarAttributes ::= + _ | ^ "," GlobalVarAttribute GlobalVarAttributes ; |
| 190 | GlobalVarAttribute ::= SectionString | align EUINT64VAL ; |
| 191 | |
| 192 | PrimType ::= INTTYPE | float | double | "ppc_fp128" | fp128 | "x86_fp80" |
| 193 | | - label ; |
| 194 | |
| 195 | Types |
| 196 | ::= opaque |
| 197 | | PrimType |
| 198 | | Types OptAddrSpace ^ "*" |
| 199 | | SymbolicValueRef |
| 200 | | "\\" ^ EUINT64VAL |
| 201 | | Types "(" ^ ArgTypeListI ^ ")" OptFuncAttrs |
| 202 | | void "(" ^ ArgTypeListI ^ ")" OptFuncAttrs |
| 203 | | "[" ^ EUINT64VAL "x" Types ^ "]" |
| 204 | | "<" ^ EUINT64VAL "x" Types ^ ">" |
| 205 | | "{" TypeListI "}" |
| 206 | | "{" ^ "}" |
| 207 | | "<" ^ "{" TypeListI "}" ^ ">" |
| 208 | | "<" ^ "{" ^ "}" ^ ">" |
| 209 | ; |
| 210 | |
| 211 | ArgType ::= Types OptParamAttrs ; |
| 212 | |
| 213 | ResultTypes ::= Types | void ; |
| 214 | |
| 215 | ArgTypeList ::= ArgType | ArgTypeList ^ "," ArgType ; |
| 216 | |
| 217 | ArgTypeListI ::= ArgTypeList | ArgTypeList ^ "," "..." | "..." | _ ; |
| 218 | |
| 219 | TypeListI ::= Types | TypeListI ^ "," Types ; |
| 220 | |
| 221 | ConstVal::= Types "[" ^ ConstVector ^ "]" |
| 222 | | Types "[" ^ "]" |
| 223 | | Types "c" ^ STRINGCONSTANT |
| 224 | | Types "<" ^ ConstVector ^ ">" |
| 225 | | Types "{" ConstVector "}" |
| 226 | | Types "{" ^ "}" |
| 227 | | Types "<" ^ "{" ConstVector "}" ^ ">" |
| 228 | | Types "<" ^ "{" ^ "}" ^ ">" |
| 229 | | Types null |
| 230 | | Types undef |
| 231 | | Types SymbolicValueRef |
| 232 | | Types ConstExpr |
| 233 | | Types zeroinitializer |
| 234 | | Types ESINT64VAL |
| 235 | | Types ESAPINTVAL |
| 236 | | Types EUINT64VAL |
| 237 | | Types EUAPINTVAL |
| 238 | | Types true |
| 239 | | Types false |
| 240 | | Types FPVAL ; |
| 241 | |
| 242 | ConstExpr::= CastOps "(" ^ ConstVal to Types ^ ")" |
| 243 | | getelementptr OptInBounds "(" ^ ConstVal IndexList ^ ")" |
| 244 | | select "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")" |
| 245 | | ArithmeticOps "(" ^ ConstVal ^ "," ConstVal ^ ")" |
| 246 | | LogicalOps "(" ^ ConstVal ^ "," ConstVal ^ ")" |
| 247 | | icmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")" |
| 248 | | fcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")" |
| 249 | | extractelement "(" ^ ConstVal ^ "," ConstVal ^ ")" |
| 250 | | insertelement "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")" |
| 251 | | shufflevector "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")" |
| 252 | | extractvalue "(" ^ ConstVal ^ ConstantIndexList ^ ")" |
| 253 | | insertvalue "(" ^ ConstVal ^ "," ConstVal ^ ConstantIndexList ^ ")" ; |
| 254 | |
| 255 | ConstVector ::= ConstVector ^ "," ConstVal | ConstVal ; |
| 256 | |
| 257 | GlobalType ::= global | constant ; |
| 258 | |
| 259 | ThreadLocal ::= - "thread_local" | _ ; |
| 260 | |
| 261 | AliaseeRef ::= ResultTypes SymbolicValueRef |
| 262 | | bitcast "(" ^ AliaseeRef to Types ^ ")" ; |
| 263 | |
| 264 | Module ::= +++ DefinitionList | --- _ ; |
| 265 | |
| 266 | DefinitionList ::= - Definition | + DefinitionList Definition ; |
| 267 | |
| 268 | Definition |
| 269 | ::= ^ ( +++++ define Function |
| 270 | | declare FunctionProto |
| 271 | | - module asm AsmBlock |
| 272 | | OptLocalAssign type Types |
| 273 | | OptGlobalAssign GVVisibilityStyle ThreadLocal OptAddrSpace GlobalType |
| 274 | ConstVal GlobalVarAttributes |
| 275 | | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace |
| 276 | GlobalType ConstVal GlobalVarAttributes |
| 277 | | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace |
| 278 | GlobalType Types GlobalVarAttributes |
| 279 | | OptGlobalAssign GVVisibilityStyle alias AliasLinkage AliaseeRef |
| 280 | | target TargetDefinition |
| 281 | | deplibs "=" LibrariesDefinition |
| 282 | ) ^ "\n"; |
| 283 | |
| 284 | AsmBlock ::= STRINGCONSTANT ; |
| 285 | |
| 286 | TargetDefinition ::= triple "=" STRINGCONSTANT |
| 287 | | datalayout "=" STRINGCONSTANT ; |
| 288 | |
| 289 | LibrariesDefinition ::= "[" ( LibList | _ ) "]"; |
| 290 | |
| 291 | LibList ::= LibList ^ "," STRINGCONSTANT | STRINGCONSTANT ; |
| 292 | |
| 293 | ArgListH ::= ArgListH ^ "," Types OptParamAttrs OptLocalName |
| 294 | | Types OptParamAttrs OptLocalName ; |
| 295 | |
| 296 | ArgList ::= ArgListH | ArgListH ^ "," "..." | "..." | _ ; |
| 297 | |
| 298 | FunctionHeaderH ::= OptCallingConv OptRetAttrs ResultTypes |
| 299 | GlobalName ^ "(" ^ ArgList ^ ")" |
| 300 | OptFuncAttrs OptSection OptAlign OptGC ; |
| 301 | |
| 302 | BEGIN ::= ( begin | "{" ) ^ "\n"; |
| 303 | |
| 304 | FunctionHeader ::= |
| 305 | FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN ; |
| 306 | |
| 307 | END ::= ^ ( end | "}" ) ^ "\n"; |
| 308 | |
| 309 | Function ::= BasicBlockList END ; |
| 310 | |
| 311 | FunctionProto ::= FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH ; |
| 312 | |
| 313 | OptSideEffect ::= _ | sideeffect ; |
| 314 | |
| 315 | ConstValueRef ::= ESINT64VAL |
| 316 | | EUINT64VAL |
| 317 | | FPVAL |
| 318 | | true |
| 319 | | false |
| 320 | | null |
| 321 | | undef |
| 322 | | zeroinitializer |
| 323 | | "<" ConstVector ">" |
| 324 | | "[" ConstVector "]" |
| 325 | | "[" ^ "]" |
| 326 | | "c" ^ STRINGCONSTANT |
| 327 | | "{" ConstVector "}" |
| 328 | | "{" ^ "}" |
| 329 | | "<" ^ "{" ConstVector "}" ^ ">" |
| 330 | | "<" ^ "{" ^ "}" ^ ">" |
| 331 | | ConstExpr |
| 332 | | asm OptSideEffect STRINGCONSTANT ^ "," STRINGCONSTANT ; |
| 333 | |
| 334 | SymbolicValueRef ::= LOCALVALID |
| 335 | | GLOBALVALID |
| 336 | | LocalName |
| 337 | | GlobalName ; |
| 338 | |
| 339 | ValueRef ::= SymbolicValueRef | ConstValueRef; |
| 340 | |
| 341 | ResolvedVal ::= Types ValueRef ; |
| 342 | |
| 343 | ReturnedVal ::= ResolvedVal | ReturnedVal ^ "," ResolvedVal ; |
| 344 | |
| 345 | BasicBlockList ::= BasicBlockList BasicBlock | FunctionHeader BasicBlock ; |
| 346 | |
| 347 | BasicBlock ::= InstructionList OptLocalAssign BBTerminatorInst ; |
| 348 | |
| 349 | InstructionList ::= +++ InstructionList Inst |
| 350 | | - _ |
| 351 | | ^ LABELSTR ^ ":\n" ; |
| 352 | |
| 353 | BBTerminatorInst ::= ^ " " ^ |
| 354 | ( ret ReturnedVal |
| 355 | | ret void |
| 356 | | br label ValueRef |
| 357 | | br INTTYPE ValueRef ^ "," label ValueRef ^ "," label ValueRef |
| 358 | | switch IntType ValueRef ^ "," label ValueRef "[" JumpTable "]" |
| 359 | | switch IntType ValueRef ^ "," label ValueRef "[" ^ "]" |
| 360 | | invoke OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")" |
| 361 | OptFuncAttrs |
| 362 | to label ValueRef unwind label ValueRef |
| 363 | | unwind |
| 364 | | unreachable ) ^ "\n"; |
| 365 | |
| 366 | JumpTable ::= JumpTable IntType ConstValueRef ^ "," label ValueRef |
| 367 | | IntType ConstValueRef ^ "," label ValueRef ; |
| 368 | |
| 369 | Inst ::= ^ " " ^ OptLocalAssign InstVal ^ "\n"; |
| 370 | |
| 371 | PHIList ::= Types "[" ValueRef ^ "," ValueRef "]" |
| 372 | | PHIList ^ "," "[" ValueRef ^ "," ValueRef "]" ; |
| 373 | |
| 374 | ParamList ::= Types OptParamAttrs ValueRef OptParamAttrs |
| 375 | | label OptParamAttrs ValueRef OptParamAttrs |
| 376 | | ParamList ^ "," Types OptParamAttrs ValueRef OptParamAttrs |
| 377 | | ParamList ^ "," label OptParamAttrs ValueRef OptParamAttrs |
| 378 | | - _ ; |
| 379 | |
| 380 | IndexList ::= _ | IndexList ^ "," ResolvedVal ; |
| 381 | |
| 382 | ConstantIndexList ::= "," EUINT64VAL | ConstantIndexList ^ "," EUINT64VAL ; |
| 383 | |
| 384 | OptTailCall ::= tail call | call ; |
| 385 | |
| 386 | InstVal ::= |
| 387 | ArithmeticOps Types ValueRef ^ "," ValueRef |
| 388 | | LogicalOps Types ValueRef ^ "," ValueRef |
| 389 | | icmp IPredicates Types ValueRef ^ "," ValueRef |
| 390 | | fcmp FPredicates Types ValueRef ^ "," ValueRef |
| 391 | | CastOps ResolvedVal to Types |
| 392 | | select ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal |
| 393 | | "va_arg" ResolvedVal ^ "," Types |
| 394 | | extractelement ResolvedVal ^ "," ResolvedVal |
| 395 | | insertelement ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal |
| 396 | | shufflevector ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal |
| 397 | | phi PHIList |
| 398 | | OptTailCall OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")" |
| 399 | OptFuncAttrs |
| 400 | | MemoryInst ; |
| 401 | |
| 402 | OptVolatile ::= - volatile | _ ; |
| 403 | OptExact ::= - exact | _ ; |
| 404 | OptNSW ::= - nsw | _ ; |
| 405 | OptNUW ::= - nuw | _ ; |
| 406 | OptNW ::= OptNUW OptNSW | OptNSW OptNUW ; |
| 407 | OptInBounds ::= - inbounds | _ ; |
| 408 | |
| 409 | MemoryInst ::= malloc Types OptCAlign |
| 410 | | malloc Types ^ "," INTTYPE ValueRef OptCAlign |
| 411 | | alloca Types OptCAlign |
| 412 | | alloca Types ^ "," INTTYPE ValueRef OptCAlign |
| 413 | | free ResolvedVal |
| 414 | | OptVolatile load Types ValueRef OptCAlign |
| 415 | | OptVolatile store ResolvedVal ^ "," Types ValueRef OptCAlign |
| 416 | | getresult Types ValueRef ^ "," EUINT64VAL |
| 417 | | getelementptr OptInBounds Types ValueRef IndexList |
| 418 | | extractvalue Types ValueRef ^ ConstantIndexList |
| 419 | | insertvalue Types ValueRef ^ "," Types ValueRef ^ ConstantIndexList ; |