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