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 ; 96 97GVExternalLinkage 98 ::= dllimport 99 | "extern_weak" 100 | + external 101 ; 102 103GVVisibilityStyle 104 ::= + _ 105 | default 106 | hidden 107 | protected 108 ; 109 110FunctionDeclareLinkage 111 ::= + _ 112 | dllimport 113 | "extern_weak" 114 ; 115 116FunctionDefineLinkage 117 ::= + _ 118 | internal 119 | linkonce 120 | "linkonce_odr" 121 | weak 122 | "weak_odr" 123 | dllexport 124 ; 125 126AliasLinkage ::= + _ | weak | "weak_odr" | internal ; 127 128OptCallingConv ::= + _ | 129 ccc | 130 fastcc | 131 coldcc | 132 "x86_stdcallcc" | 133 "x86_fastcallcc" | 134 cc EUINT64VAL ; 135 136ParamAttr ::= zeroext 137 | signext 138 | inreg 139 | sret 140 | noalias 141 | nocapture 142 | byval 143 | nest 144 | align EUINT64VAL 145 ; 146 147OptParamAttrs ::= + _ | OptParamAttrs ParamAttr ; 148 149RetAttr ::= inreg 150 | zeroext 151 | signext 152 | noalias 153 ; 154 155OptRetAttrs ::= _ 156 | OptRetAttrs RetAttr 157 ; 158 159FuncAttr ::= noreturn 160 | nounwind 161 | inreg 162 | zeroext 163 | signext 164 | readnone 165 | readonly 166 | inlinehint 167 | alignstack 168 | noinline 169 | alwaysinline 170 | optsize 171 | ssp 172 | sspreq 173 | returns_twice 174 | nonlazybind 175 | sanitize_address 176 | sanitize_thread 177 | sanitize_memory 178 ; 179 180OptFuncAttrs ::= + _ | OptFuncAttrs FuncAttr ; 181 182OptGC ::= + _ | gc STRINGCONSTANT ; 183 184OptAlign ::= + _ | align EUINT64VAL ; 185OptCAlign ::= + _ | ^ "," align EUINT64VAL ; 186 187SectionString ::= section STRINGCONSTANT ; 188 189OptSection ::= + _ | SectionString ; 190 191GlobalVarAttributes ::= + _ | ^ "," GlobalVarAttribute GlobalVarAttributes ; 192GlobalVarAttribute ::= SectionString | align EUINT64VAL ; 193 194PrimType ::= INTTYPE | float | double | "ppc_fp128" | fp128 | "x86_fp80" 195 | - label ; 196 197Types 198 ::= opaque 199 | PrimType 200 | Types OptAddrSpace ^ "*" 201 | SymbolicValueRef 202 | "\\" ^ EUINT64VAL 203 | Types "(" ^ ArgTypeListI ^ ")" OptFuncAttrs 204 | void "(" ^ ArgTypeListI ^ ")" OptFuncAttrs 205 | "[" ^ EUINT64VAL "x" Types ^ "]" 206 | "<" ^ EUINT64VAL "x" Types ^ ">" 207 | "{" TypeListI "}" 208 | "{" ^ "}" 209 | "<" ^ "{" TypeListI "}" ^ ">" 210 | "<" ^ "{" ^ "}" ^ ">" 211 ; 212 213ArgType ::= Types OptParamAttrs ; 214 215ResultTypes ::= Types | void ; 216 217ArgTypeList ::= ArgType | ArgTypeList ^ "," ArgType ; 218 219ArgTypeListI ::= ArgTypeList | ArgTypeList ^ "," "..." | "..." | _ ; 220 221TypeListI ::= Types | TypeListI ^ "," Types ; 222 223ConstVal::= Types "[" ^ ConstVector ^ "]" 224 | Types "[" ^ "]" 225 | Types "c" ^ STRINGCONSTANT 226 | Types "<" ^ ConstVector ^ ">" 227 | Types "{" ConstVector "}" 228 | Types "{" ^ "}" 229 | Types "<" ^ "{" ConstVector "}" ^ ">" 230 | Types "<" ^ "{" ^ "}" ^ ">" 231 | Types null 232 | Types undef 233 | Types SymbolicValueRef 234 | Types ConstExpr 235 | Types zeroinitializer 236 | Types ESINT64VAL 237 | Types ESAPINTVAL 238 | Types EUINT64VAL 239 | Types EUAPINTVAL 240 | Types true 241 | Types false 242 | Types FPVAL ; 243 244ConstExpr::= CastOps "(" ^ ConstVal to Types ^ ")" 245 | getelementptr OptInBounds "(" ^ ConstVal IndexList ^ ")" 246 | select "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")" 247 | ArithmeticOps "(" ^ ConstVal ^ "," ConstVal ^ ")" 248 | LogicalOps "(" ^ ConstVal ^ "," ConstVal ^ ")" 249 | icmp IPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")" 250 | fcmp FPredicates "(" ^ ConstVal ^ "," ConstVal ^ ")" 251 | extractelement "(" ^ ConstVal ^ "," ConstVal ^ ")" 252 | insertelement "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")" 253 | shufflevector "(" ^ ConstVal ^ "," ConstVal ^ "," ConstVal ^ ")" 254 | extractvalue "(" ^ ConstVal ^ ConstantIndexList ^ ")" 255 | insertvalue "(" ^ ConstVal ^ "," ConstVal ^ ConstantIndexList ^ ")" ; 256 257ConstVector ::= ConstVector ^ "," ConstVal | ConstVal ; 258 259GlobalType ::= global | constant ; 260 261ThreadLocal ::= - "thread_local" | _ ; 262 263AliaseeRef ::= ResultTypes SymbolicValueRef 264 | bitcast "(" ^ AliaseeRef to Types ^ ")" ; 265 266Module ::= +++ DefinitionList | --- _ ; 267 268DefinitionList ::= - Definition | + DefinitionList Definition ; 269 270Definition 271 ::= ^ ( +++++ define Function 272 | declare FunctionProto 273 | - module asm AsmBlock 274 | OptLocalAssign type Types 275 | OptGlobalAssign GVVisibilityStyle ThreadLocal OptAddrSpace GlobalType 276 ConstVal GlobalVarAttributes 277 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace 278 GlobalType ConstVal GlobalVarAttributes 279 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle ThreadLocal OptAddrSpace 280 GlobalType Types GlobalVarAttributes 281 | OptGlobalAssign GVVisibilityStyle alias AliasLinkage AliaseeRef 282 | target TargetDefinition 283 | deplibs "=" LibrariesDefinition 284 ) ^ "\n"; 285 286AsmBlock ::= STRINGCONSTANT ; 287 288TargetDefinition ::= triple "=" STRINGCONSTANT 289 | datalayout "=" STRINGCONSTANT ; 290 291LibrariesDefinition ::= "[" ( LibList | _ ) "]"; 292 293LibList ::= LibList ^ "," STRINGCONSTANT | STRINGCONSTANT ; 294 295ArgListH ::= ArgListH ^ "," Types OptParamAttrs OptLocalName 296 | Types OptParamAttrs OptLocalName ; 297 298ArgList ::= ArgListH | ArgListH ^ "," "..." | "..." | _ ; 299 300FunctionHeaderH ::= OptCallingConv OptRetAttrs ResultTypes 301 GlobalName ^ "(" ^ ArgList ^ ")" 302 OptFuncAttrs OptSection OptAlign OptGC ; 303 304BEGIN ::= ( begin | "{" ) ^ "\n"; 305 306FunctionHeader ::= 307 FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN ; 308 309END ::= ^ ( end | "}" ) ^ "\n"; 310 311Function ::= BasicBlockList END ; 312 313FunctionProto ::= FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH ; 314 315OptSideEffect ::= _ | sideeffect ; 316 317ConstValueRef ::= ESINT64VAL 318 | EUINT64VAL 319 | FPVAL 320 | true 321 | false 322 | null 323 | undef 324 | zeroinitializer 325 | "<" ConstVector ">" 326 | "[" ConstVector "]" 327 | "[" ^ "]" 328 | "c" ^ STRINGCONSTANT 329 | "{" ConstVector "}" 330 | "{" ^ "}" 331 | "<" ^ "{" ConstVector "}" ^ ">" 332 | "<" ^ "{" ^ "}" ^ ">" 333 | ConstExpr 334 | asm OptSideEffect STRINGCONSTANT ^ "," STRINGCONSTANT ; 335 336SymbolicValueRef ::= LOCALVALID 337 | GLOBALVALID 338 | LocalName 339 | GlobalName ; 340 341ValueRef ::= SymbolicValueRef | ConstValueRef; 342 343ResolvedVal ::= Types ValueRef ; 344 345ReturnedVal ::= ResolvedVal | ReturnedVal ^ "," ResolvedVal ; 346 347BasicBlockList ::= BasicBlockList BasicBlock | FunctionHeader BasicBlock ; 348 349BasicBlock ::= InstructionList OptLocalAssign BBTerminatorInst ; 350 351InstructionList ::= +++ InstructionList Inst 352 | - _ 353 | ^ LABELSTR ^ ":\n" ; 354 355BBTerminatorInst ::= ^ " " ^ 356 ( ret ReturnedVal 357 | ret void 358 | br label ValueRef 359 | br INTTYPE ValueRef ^ "," label ValueRef ^ "," label ValueRef 360 | switch IntType ValueRef ^ "," label ValueRef "[" JumpTable "]" 361 | switch IntType ValueRef ^ "," label ValueRef "[" ^ "]" 362 | invoke OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")" 363 OptFuncAttrs 364 to label ValueRef unwind label ValueRef 365 | unwind 366 | unreachable ) ^ "\n"; 367 368JumpTable ::= JumpTable IntType ConstValueRef ^ "," label ValueRef 369 | IntType ConstValueRef ^ "," label ValueRef ; 370 371Inst ::= ^ " " ^ OptLocalAssign InstVal ^ "\n"; 372 373PHIList ::= Types "[" ValueRef ^ "," ValueRef "]" 374 | PHIList ^ "," "[" ValueRef ^ "," ValueRef "]" ; 375 376ParamList ::= Types OptParamAttrs ValueRef OptParamAttrs 377 | label OptParamAttrs ValueRef OptParamAttrs 378 | ParamList ^ "," Types OptParamAttrs ValueRef OptParamAttrs 379 | ParamList ^ "," label OptParamAttrs ValueRef OptParamAttrs 380 | - _ ; 381 382IndexList ::= _ | IndexList ^ "," ResolvedVal ; 383 384ConstantIndexList ::= "," EUINT64VAL | ConstantIndexList ^ "," EUINT64VAL ; 385 386OptTailCall ::= tail call | call ; 387 388InstVal ::= 389 ArithmeticOps Types ValueRef ^ "," ValueRef 390 | LogicalOps Types ValueRef ^ "," ValueRef 391 | icmp IPredicates Types ValueRef ^ "," ValueRef 392 | fcmp FPredicates Types ValueRef ^ "," ValueRef 393 | CastOps ResolvedVal to Types 394 | select ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal 395 | "va_arg" ResolvedVal ^ "," Types 396 | extractelement ResolvedVal ^ "," ResolvedVal 397 | insertelement ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal 398 | shufflevector ResolvedVal ^ "," ResolvedVal ^ "," ResolvedVal 399 | phi PHIList 400 | OptTailCall OptCallingConv ResultTypes ValueRef ^ "(" ^ ParamList ^ ")" 401 OptFuncAttrs 402 | MemoryInst ; 403 404OptVolatile ::= - volatile | _ ; 405OptExact ::= - exact | _ ; 406OptNSW ::= - nsw | _ ; 407OptNUW ::= - nuw | _ ; 408OptNW ::= OptNUW OptNSW | OptNSW OptNUW ; 409OptInBounds ::= - inbounds | _ ; 410 411MemoryInst ::= malloc Types OptCAlign 412 | malloc Types ^ "," INTTYPE ValueRef OptCAlign 413 | alloca Types OptCAlign 414 | alloca Types ^ "," INTTYPE ValueRef OptCAlign 415 | free ResolvedVal 416 | OptVolatile load Types ValueRef OptCAlign 417 | OptVolatile store ResolvedVal ^ "," Types ValueRef OptCAlign 418 | getresult Types ValueRef ^ "," EUINT64VAL 419 | getelementptr OptInBounds Types ValueRef IndexList 420 | extractvalue Types ValueRef ^ ConstantIndexList 421 | insertvalue Types ValueRef ^ "," Types ValueRef ^ ConstantIndexList ; 422