Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=// |
| 2 | // |
| 3 | // This file implements the bison parser for LLVM assembly languages files. |
| 4 | // |
| 5 | //===------------------------------------------------------------------------=// |
| 6 | |
| 7 | // |
| 8 | // TODO: Parse comments and add them to an internal node... so that they may |
| 9 | // be saved in the bytecode format as well as everything else. Very important |
| 10 | // for a general IR format. |
| 11 | // |
| 12 | |
| 13 | %{ |
| 14 | #include "ParserInternals.h" |
| 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/Method.h" |
| 17 | #include "llvm/SymbolTable.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Type.h" |
| 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Assembly/Parser.h" |
| 22 | #include "llvm/ConstantPool.h" |
| 23 | #include "llvm/iTerminators.h" |
| 24 | #include "llvm/iMemory.h" |
| 25 | #include <list> |
| 26 | #include <utility> // Get definition of pair class |
| 27 | #include <stdio.h> // This embarasment is due to our flex lexer... |
| 28 | |
| 29 | int yyerror(char *ErrorMsg); // Forward declarations to prevent "implicit |
| 30 | int yylex(); // declaration" of xxx warnings. |
| 31 | int yyparse(); |
| 32 | |
| 33 | static Module *ParserResult; |
| 34 | const ToolCommandLine *CurOptions = 0; |
| 35 | |
| 36 | // This contains info used when building the body of a method. It is destroyed |
| 37 | // when the method is completed. |
| 38 | // |
| 39 | typedef vector<Value *> ValueList; // Numbered defs |
| 40 | static void ResolveDefinitions(vector<ValueList> &LateResolvers); |
| 41 | |
| 42 | static struct PerModuleInfo { |
| 43 | Module *CurrentModule; |
| 44 | vector<ValueList> Values; // Module level numbered definitions |
| 45 | vector<ValueList> LateResolveValues; |
| 46 | |
| 47 | void ModuleDone() { |
| 48 | // If we could not resolve some blocks at parsing time (forward branches) |
| 49 | // resolve the branches now... |
| 50 | ResolveDefinitions(LateResolveValues); |
| 51 | |
| 52 | Values.clear(); // Clear out method local definitions |
| 53 | CurrentModule = 0; |
| 54 | } |
| 55 | } CurModule; |
| 56 | |
| 57 | static struct PerMethodInfo { |
| 58 | Method *CurrentMethod; // Pointer to current method being created |
| 59 | |
| 60 | vector<ValueList> Values; // Keep track of numbered definitions |
| 61 | vector<ValueList> LateResolveValues; |
| 62 | |
| 63 | inline PerMethodInfo() { |
| 64 | CurrentMethod = 0; |
| 65 | } |
| 66 | |
| 67 | inline ~PerMethodInfo() {} |
| 68 | |
| 69 | inline void MethodStart(Method *M) { |
| 70 | CurrentMethod = M; |
| 71 | } |
| 72 | |
| 73 | void MethodDone() { |
| 74 | // If we could not resolve some blocks at parsing time (forward branches) |
| 75 | // resolve the branches now... |
| 76 | ResolveDefinitions(LateResolveValues); |
| 77 | |
| 78 | Values.clear(); // Clear out method local definitions |
| 79 | CurrentMethod = 0; |
| 80 | } |
| 81 | } CurMeth; // Info for the current method... |
| 82 | |
| 83 | |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | // Code to handle definitions of all the types |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | |
| 88 | static void InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) { |
| 89 | if (!D->hasName()) { // Is this a numbered definition? |
| 90 | unsigned type = D->getType()->getUniqueID(); |
| 91 | if (ValueTab.size() <= type) |
| 92 | ValueTab.resize(type+1, ValueList()); |
| 93 | //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D); |
| 94 | ValueTab[type].push_back(D); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static Value *getVal(const Type *Type, ValID &D, |
| 99 | bool DoNotImprovise = false) { |
| 100 | switch (D.Type) { |
| 101 | case 0: { // Is it a numbered definition? |
| 102 | unsigned type = Type->getUniqueID(); |
| 103 | unsigned Num = (unsigned)D.Num; |
| 104 | |
| 105 | // Module constants occupy the lowest numbered slots... |
| 106 | if (type < CurModule.Values.size()) { |
| 107 | if (Num < CurModule.Values[type].size()) |
| 108 | return CurModule.Values[type][Num]; |
| 109 | |
| 110 | Num -= CurModule.Values[type].size(); |
| 111 | } |
| 112 | |
| 113 | // Make sure that our type is within bounds |
| 114 | if (CurMeth.Values.size() <= type) |
| 115 | break; |
| 116 | |
| 117 | // Check that the number is within bounds... |
| 118 | if (CurMeth.Values[type].size() <= Num) |
| 119 | break; |
| 120 | |
| 121 | return CurMeth.Values[type][Num]; |
| 122 | } |
| 123 | case 1: { // Is it a named definition? |
| 124 | string Name(D.Name); |
| 125 | SymbolTable *SymTab = 0; |
| 126 | if (CurMeth.CurrentMethod) |
| 127 | SymTab = CurMeth.CurrentMethod->getSymbolTable(); |
| 128 | Value *N = SymTab ? SymTab->lookup(Type, Name) : 0; |
| 129 | |
| 130 | if (N == 0) { |
| 131 | SymTab = CurModule.CurrentModule->getSymbolTable(); |
| 132 | if (SymTab) |
| 133 | N = SymTab->lookup(Type, Name); |
| 134 | if (N == 0) break; |
| 135 | } |
| 136 | |
| 137 | D.destroy(); // Free old strdup'd memory... |
| 138 | return N; |
| 139 | } |
| 140 | |
| 141 | case 2: // Is it a constant pool reference?? |
| 142 | case 3: // Is it an unsigned const pool reference? |
| 143 | case 4:{ // Is it a string const pool reference? |
| 144 | ConstPoolVal *CPV = 0; |
| 145 | |
| 146 | // Check to make sure that "Type" is an integral type, and that our |
| 147 | // value will fit into the specified type... |
| 148 | switch (D.Type) { |
| 149 | case 2: |
| 150 | if (Type == Type::BoolTy) { // Special handling for boolean data |
| 151 | CPV = new ConstPoolBool(D.ConstPool64 != 0); |
| 152 | } else { |
| 153 | if (!ConstPoolSInt::isValueValidForType(Type, D.ConstPool64)) |
| 154 | ThrowException("Symbolic constant pool reference is invalid!"); |
| 155 | CPV = new ConstPoolSInt(Type, D.ConstPool64); |
| 156 | } |
| 157 | break; |
| 158 | case 3: |
| 159 | if (!ConstPoolUInt::isValueValidForType(Type, D.UConstPool64)) { |
| 160 | if (!ConstPoolSInt::isValueValidForType(Type, D.ConstPool64)) { |
| 161 | ThrowException("Symbolic constant pool reference is invalid!"); |
| 162 | } else { // This is really a signed reference. Transmogrify. |
| 163 | CPV = new ConstPoolSInt(Type, D.ConstPool64); |
| 164 | } |
| 165 | } else { |
| 166 | CPV = new ConstPoolUInt(Type, D.UConstPool64); |
| 167 | } |
| 168 | break; |
| 169 | case 4: |
| 170 | cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n"; |
| 171 | abort(); |
| 172 | //CPV = new ConstPoolString(D.Name); |
| 173 | D.destroy(); // Free the string memory |
| 174 | break; |
| 175 | } |
| 176 | assert(CPV && "How did we escape creating a constant??"); |
| 177 | |
| 178 | // Scan through the constant table and see if we already have loaded this |
| 179 | // constant. |
| 180 | // |
| 181 | ConstantPool &CP = CurMeth.CurrentMethod ? |
| 182 | CurMeth.CurrentMethod->getConstantPool() : |
| 183 | CurModule.CurrentModule->getConstantPool(); |
| 184 | ConstPoolVal *C = CP.find(CPV); // Already have this constant? |
| 185 | if (C) { |
| 186 | delete CPV; // Didn't need this after all, oh well. |
| 187 | return C; // Yup, we already have one, recycle it! |
| 188 | } |
| 189 | CP.insert(CPV); |
| 190 | |
| 191 | // Success, everything is kosher. Lets go! |
| 192 | return CPV; |
| 193 | } // End of case 2,3,4 |
| 194 | } // End of switch |
| 195 | |
| 196 | |
| 197 | // If we reached here, we referenced either a symbol that we don't know about |
| 198 | // or an id number that hasn't been read yet. We may be referencing something |
| 199 | // forward, so just create an entry to be resolved later and get to it... |
| 200 | // |
| 201 | if (DoNotImprovise) return 0; // Do we just want a null to be returned? |
| 202 | |
| 203 | // TODO: Attempt to coallecse nodes that are the same with previous ones. |
| 204 | Value *d = 0; |
| 205 | switch (Type->getPrimitiveID()) { |
| 206 | case Type::LabelTyID: d = new BBPlaceHolder(Type, D); break; |
| 207 | case Type::MethodTyID: |
| 208 | d = new MethPlaceHolder(Type, D); |
| 209 | InsertValue(d, CurModule.LateResolveValues); |
| 210 | return d; |
| 211 | //case Type::ClassTyID: d = new ClassPlaceHolder(Type, D); break; |
| 212 | default: d = new DefPlaceHolder(Type, D); break; |
| 213 | } |
| 214 | |
| 215 | assert(d != 0 && "How did we not make something?"); |
| 216 | InsertValue(d, CurMeth.LateResolveValues); |
| 217 | return d; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | //===----------------------------------------------------------------------===// |
| 222 | // Code to handle forward references in instructions |
| 223 | //===----------------------------------------------------------------------===// |
| 224 | // |
| 225 | // This code handles the late binding needed with statements that reference |
| 226 | // values not defined yet... for example, a forward branch, or the PHI node for |
| 227 | // a loop body. |
| 228 | // |
| 229 | // This keeps a table (CurMeth.LateResolveValues) of all such forward references |
| 230 | // and back patchs after we are done. |
| 231 | // |
| 232 | |
| 233 | // ResolveDefinitions - If we could not resolve some defs at parsing |
| 234 | // time (forward branches, phi functions for loops, etc...) resolve the |
| 235 | // defs now... |
| 236 | // |
| 237 | static void ResolveDefinitions(vector<ValueList> &LateResolvers) { |
| 238 | // Loop over LateResolveDefs fixing up stuff that couldn't be resolved |
| 239 | for (unsigned ty = 0; ty < LateResolvers.size(); ty++) { |
| 240 | while (!LateResolvers[ty].empty()) { |
| 241 | Value *V = LateResolvers[ty].back(); |
| 242 | LateResolvers[ty].pop_back(); |
| 243 | ValID &DID = getValIDFromPlaceHolder(V); |
| 244 | |
| 245 | Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true); |
| 246 | |
| 247 | if (TheRealValue == 0 && DID.Type == 1) |
| 248 | ThrowException("Reference to an invalid definition: '" +DID.getName() + |
| 249 | "' of type '" + V->getType()->getName() + "'"); |
| 250 | else if (TheRealValue == 0) |
| 251 | ThrowException("Reference to an invalid definition: #" +itostr(DID.Num)+ |
| 252 | " of type '" + V->getType()->getName() + "'"); |
| 253 | |
| 254 | V->replaceAllUsesWith(TheRealValue); |
| 255 | assert(V->use_empty()); |
| 256 | delete V; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | LateResolvers.clear(); |
| 261 | } |
| 262 | |
| 263 | // addConstValToConstantPool - This code is used to insert a constant into the |
| 264 | // current constant pool. This is designed to make maximal (but not more than |
| 265 | // possible) reuse (merging) of constants in the constant pool. This means that |
| 266 | // multiple references to %4, for example will all get merged. |
| 267 | // |
| 268 | static ConstPoolVal *addConstValToConstantPool(ConstPoolVal *C) { |
| 269 | vector<ValueList> &ValTab = CurMeth.CurrentMethod ? |
| 270 | CurMeth.Values : CurModule.Values; |
| 271 | ConstantPool &CP = CurMeth.CurrentMethod ? |
| 272 | CurMeth.CurrentMethod->getConstantPool() : |
| 273 | CurModule.CurrentModule->getConstantPool(); |
| 274 | |
| 275 | if (ConstPoolVal *CPV = CP.find(C)) { |
| 276 | // Constant already in constant pool. Try to merge the two constants |
| 277 | if (CPV->hasName() && !C->hasName()) { |
| 278 | // Merge the two values, we inherit the existing CPV's name. |
| 279 | // InsertValue requires that the value have no name to insert correctly |
| 280 | // (because we want to fill the slot this constant would have filled) |
| 281 | // |
| 282 | string Name = CPV->getName(); |
| 283 | CPV->setName(""); |
| 284 | InsertValue(CPV, ValTab); |
| 285 | CPV->setName(Name); |
| 286 | delete C; |
| 287 | return CPV; |
| 288 | } else if (!CPV->hasName() && C->hasName()) { |
| 289 | // If we have a name on this value and there isn't one in the const |
| 290 | // pool val already, propogate it. |
| 291 | // |
| 292 | CPV->setName(C->getName()); |
| 293 | delete C; // Sorry, you're toast |
| 294 | return CPV; |
| 295 | } else if (CPV->hasName() && C->hasName()) { |
| 296 | // Both values have distinct names. We cannot merge them. |
| 297 | CP.insert(C); |
| 298 | InsertValue(C, ValTab); |
| 299 | return C; |
| 300 | } else if (!CPV->hasName() && !C->hasName()) { |
| 301 | // Neither value has a name, trivially merge them. |
| 302 | InsertValue(CPV, ValTab); |
| 303 | delete C; |
| 304 | return CPV; |
| 305 | } |
| 306 | |
| 307 | assert(0 && "Not reached!"); |
| 308 | return 0; |
| 309 | } else { // No duplication of value. |
| 310 | CP.insert(C); |
| 311 | InsertValue(C, ValTab); |
| 312 | return C; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | //===----------------------------------------------------------------------===// |
| 317 | // RunVMAsmParser - Define an interface to this parser |
| 318 | //===----------------------------------------------------------------------===// |
| 319 | // |
| 320 | Module *RunVMAsmParser(const ToolCommandLine &Opts, FILE *F) { |
| 321 | llvmAsmin = F; |
| 322 | CurOptions = &Opts; |
| 323 | llvmAsmlineno = 1; // Reset the current line number... |
| 324 | |
| 325 | CurModule.CurrentModule = new Module(); // Allocate a new module to read |
| 326 | yyparse(); // Parse the file. |
| 327 | Module *Result = ParserResult; |
| 328 | CurOptions = 0; |
| 329 | llvmAsmin = stdin; // F is about to go away, don't use it anymore... |
| 330 | ParserResult = 0; |
| 331 | |
| 332 | return Result; |
| 333 | } |
| 334 | |
| 335 | %} |
| 336 | |
| 337 | %union { |
| 338 | Module *ModuleVal; |
| 339 | Method *MethodVal; |
| 340 | MethodArgument *MethArgVal; |
| 341 | BasicBlock *BasicBlockVal; |
| 342 | TerminatorInst *TermInstVal; |
| 343 | Instruction *InstVal; |
| 344 | ConstPoolVal *ConstVal; |
| 345 | const Type *TypeVal; |
| 346 | |
| 347 | list<MethodArgument*> *MethodArgList; |
| 348 | list<Value*> *ValueList; |
| 349 | list<const Type*> *TypeList; |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 350 | list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 351 | list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable; |
| 352 | vector<ConstPoolVal*> *ConstVector; |
| 353 | |
| 354 | int64_t SInt64Val; |
| 355 | uint64_t UInt64Val; |
| 356 | int SIntVal; |
| 357 | unsigned UIntVal; |
| 358 | |
| 359 | char *StrVal; // This memory is allocated by strdup! |
| 360 | ValID ValIDVal; // May contain memory allocated by strdup |
| 361 | |
| 362 | Instruction::UnaryOps UnaryOpVal; |
| 363 | Instruction::BinaryOps BinaryOpVal; |
| 364 | Instruction::TermOps TermOpVal; |
| 365 | Instruction::MemoryOps MemOpVal; |
| 366 | } |
| 367 | |
| 368 | %type <ModuleVal> Module MethodList |
| 369 | %type <MethodVal> Method MethodHeader BasicBlockList |
| 370 | %type <BasicBlockVal> BasicBlock InstructionList |
| 371 | %type <TermInstVal> BBTerminatorInst |
| 372 | %type <InstVal> Inst InstVal MemoryInst |
| 373 | %type <ConstVal> ConstVal |
| 374 | %type <ConstVector> ConstVector |
| 375 | %type <MethodArgList> ArgList ArgListH |
| 376 | %type <MethArgVal> ArgVal |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 377 | %type <PHIList> PHIList |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 378 | %type <ValueList> ValueRefList ValueRefListE |
| 379 | %type <TypeList> TypeList |
| 380 | %type <JumpTable> JumpTable |
| 381 | |
| 382 | %type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB |
| 383 | |
| 384 | // Tokens and types for handling constant integer values |
| 385 | // |
| 386 | // ESINT64VAL - A negative number within long long range |
| 387 | %token <SInt64Val> ESINT64VAL |
| 388 | |
| 389 | // EUINT64VAL - A positive number within uns. long long range |
| 390 | %token <UInt64Val> EUINT64VAL |
| 391 | %type <SInt64Val> EINT64VAL |
| 392 | |
| 393 | %token <SIntVal> SINTVAL // Signed 32 bit ints... |
| 394 | %token <UIntVal> UINTVAL // Unsigned 32 bit ints... |
| 395 | %type <SIntVal> INTVAL |
| 396 | |
| 397 | // Built in types... |
| 398 | %type <TypeVal> Types TypesV SIntType UIntType IntType |
| 399 | %token <TypeVal> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG |
| 400 | %token <TypeVal> FLOAT DOUBLE STRING TYPE LABEL |
| 401 | |
| 402 | %token <StrVal> VAR_ID LABELSTR STRINGCONSTANT |
| 403 | %type <StrVal> OptVAR_ID OptAssign |
| 404 | |
| 405 | |
| 406 | %token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE |
| 407 | %token PHI CALL |
| 408 | |
| 409 | // Basic Block Terminating Operators |
| 410 | %token <TermOpVal> RET BR SWITCH |
| 411 | |
| 412 | // Unary Operators |
| 413 | %type <UnaryOpVal> UnaryOps // all the unary operators |
| 414 | %token <UnaryOpVal> NEG NOT |
| 415 | |
| 416 | // Unary Conversion Operators |
| 417 | %token <UnaryOpVal> TOINT TOUINT |
| 418 | |
| 419 | // Binary Operators |
| 420 | %type <BinaryOpVal> BinaryOps // all the binary operators |
| 421 | %token <BinaryOpVal> ADD SUB MUL DIV REM |
| 422 | |
| 423 | // Binary Comarators |
| 424 | %token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE |
| 425 | |
| 426 | // Memory Instructions |
| 427 | %token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETFIELD PUTFIELD |
| 428 | |
| 429 | %start Module |
| 430 | %% |
| 431 | |
| 432 | // Handle constant integer size restriction and conversion... |
| 433 | // |
| 434 | |
| 435 | INTVAL : SINTVAL |
| 436 | INTVAL : UINTVAL { |
| 437 | if ($1 > (uint32_t)INT32_MAX) // Outside of my range! |
| 438 | ThrowException("Value too large for type!"); |
| 439 | $$ = (int32_t)$1; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | EINT64VAL : ESINT64VAL // These have same type and can't cause problems... |
| 444 | EINT64VAL : EUINT64VAL { |
| 445 | if ($1 > (uint64_t)INT64_MAX) // Outside of my range! |
| 446 | ThrowException("Value too large for type!"); |
| 447 | $$ = (int64_t)$1; |
| 448 | } |
| 449 | |
| 450 | // Types includes all predefined types... except void, because you can't do |
| 451 | // anything with it except for certain specific things... |
| 452 | // |
| 453 | // User defined types are added latter... |
| 454 | // |
| 455 | Types : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT |
| 456 | Types : LONG | ULONG | FLOAT | DOUBLE | STRING | TYPE | LABEL |
| 457 | |
| 458 | // TypesV includes all of 'Types', but it also includes the void type. |
| 459 | TypesV : Types | VOID |
| 460 | |
| 461 | // Operations that are notably excluded from this list include: |
| 462 | // RET, BR, & SWITCH because they end basic blocks and are treated specially. |
| 463 | // |
| 464 | UnaryOps : NEG | NOT | TOINT | TOUINT |
| 465 | BinaryOps : ADD | SUB | MUL | DIV | REM |
| 466 | BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE |
| 467 | |
| 468 | // Valueine some types that allow classification if we only want a particular |
| 469 | // thing... |
| 470 | SIntType : LONG | INT | SHORT | SBYTE |
| 471 | UIntType : ULONG | UINT | USHORT | UBYTE |
| 472 | IntType : SIntType | UIntType |
| 473 | |
| 474 | OptAssign : VAR_ID '=' { |
| 475 | $$ = $1; |
| 476 | } |
| 477 | | /*empty*/ { |
| 478 | $$ = 0; |
| 479 | } |
| 480 | |
| 481 | ConstVal : SIntType EINT64VAL { // integral constants |
| 482 | if (!ConstPoolSInt::isValueValidForType($1, $2)) |
| 483 | ThrowException("Constant value doesn't fit in type!"); |
| 484 | $$ = new ConstPoolSInt($1, $2); |
| 485 | } |
| 486 | | UIntType EUINT64VAL { // integral constants |
| 487 | if (!ConstPoolUInt::isValueValidForType($1, $2)) |
| 488 | ThrowException("Constant value doesn't fit in type!"); |
| 489 | $$ = new ConstPoolUInt($1, $2); |
| 490 | } |
| 491 | | BOOL TRUE { // Boolean constants |
| 492 | $$ = new ConstPoolBool(true); |
| 493 | } |
| 494 | | BOOL FALSE { // Boolean constants |
| 495 | $$ = new ConstPoolBool(false); |
| 496 | } |
| 497 | | STRING STRINGCONSTANT { // String constants |
| 498 | cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n"; |
| 499 | abort(); |
| 500 | //$$ = new ConstPoolString($2); |
| 501 | free($2); |
| 502 | } |
| 503 | | TYPE Types { // Type constants |
| 504 | $$ = new ConstPoolType($2); |
| 505 | } |
| 506 | | '[' Types ']' '[' ConstVector ']' { // Nonempty array constant |
| 507 | // Verify all elements are correct type! |
| 508 | const ArrayType *AT = ArrayType::getArrayType($2); |
| 509 | for (unsigned i = 0; i < $5->size(); i++) { |
| 510 | if ($2 != (*$5)[i]->getType()) |
| 511 | ThrowException("Element #" + utostr(i) + " is not of type '" + |
| 512 | $2->getName() + "' as required!\nIt is of type '" + |
| 513 | (*$5)[i]->getType()->getName() + "'."); |
| 514 | } |
| 515 | |
| 516 | $$ = new ConstPoolArray(AT, *$5); |
| 517 | delete $5; |
| 518 | } |
| 519 | | '[' Types ']' '[' ']' { // Empty array constant |
| 520 | vector<ConstPoolVal*> Empty; |
| 521 | $$ = new ConstPoolArray(ArrayType::getArrayType($2), Empty); |
| 522 | } |
| 523 | | '[' EUINT64VAL 'x' Types ']' '[' ConstVector ']' { |
| 524 | // Verify all elements are correct type! |
| 525 | const ArrayType *AT = ArrayType::getArrayType($4, (int)$2); |
| 526 | if ($2 != $7->size()) |
| 527 | ThrowException("Type mismatch: constant sized array initialized with " + |
| 528 | utostr($7->size()) + " arguments, but has size of " + |
| 529 | itostr((int)$2) + "!"); |
| 530 | |
| 531 | for (unsigned i = 0; i < $7->size(); i++) { |
| 532 | if ($4 != (*$7)[i]->getType()) |
| 533 | ThrowException("Element #" + utostr(i) + " is not of type '" + |
| 534 | $4->getName() + "' as required!\nIt is of type '" + |
| 535 | (*$7)[i]->getType()->getName() + "'."); |
| 536 | } |
| 537 | |
| 538 | $$ = new ConstPoolArray(AT, *$7); |
| 539 | delete $7; |
| 540 | } |
| 541 | | '[' EUINT64VAL 'x' Types ']' '[' ']' { |
| 542 | if ($2 != 0) |
| 543 | ThrowException("Type mismatch: constant sized array initialized with 0" |
| 544 | " arguments, but has size of " + itostr((int)$2) + "!"); |
| 545 | vector<ConstPoolVal*> Empty; |
| 546 | $$ = new ConstPoolArray(ArrayType::getArrayType($4, 0), Empty); |
| 547 | } |
| 548 | | '{' TypeList '}' '{' ConstVector '}' { |
| 549 | StructType::ElementTypes Types($2->begin(), $2->end()); |
| 550 | delete $2; |
| 551 | |
| 552 | const StructType *St = StructType::getStructType(Types); |
| 553 | $$ = new ConstPoolStruct(St, *$5); |
| 554 | delete $5; |
| 555 | } |
| 556 | | '{' '}' '{' '}' { |
| 557 | const StructType *St = |
| 558 | StructType::getStructType(StructType::ElementTypes()); |
| 559 | vector<ConstPoolVal*> Empty; |
| 560 | $$ = new ConstPoolStruct(St, Empty); |
| 561 | } |
| 562 | /* |
| 563 | | Types '*' ConstVal { |
| 564 | assert(0); |
| 565 | $$ = 0; |
| 566 | } |
| 567 | */ |
| 568 | |
| 569 | |
| 570 | ConstVector : ConstVector ',' ConstVal { |
| 571 | ($$ = $1)->push_back(addConstValToConstantPool($3)); |
| 572 | } |
| 573 | | ConstVal { |
| 574 | $$ = new vector<ConstPoolVal*>(); |
| 575 | $$->push_back(addConstValToConstantPool($1)); |
| 576 | } |
| 577 | |
| 578 | |
| 579 | ConstPool : ConstPool OptAssign ConstVal { |
| 580 | if ($2) { |
| 581 | $3->setName($2); |
| 582 | free($2); |
| 583 | } |
| 584 | |
| 585 | addConstValToConstantPool($3); |
| 586 | } |
| 587 | | /* empty: end of list */ { |
| 588 | } |
| 589 | |
| 590 | |
| 591 | //===----------------------------------------------------------------------===// |
| 592 | // Rules to match Modules |
| 593 | //===----------------------------------------------------------------------===// |
| 594 | |
| 595 | // Module rule: Capture the result of parsing the whole file into a result |
| 596 | // variable... |
| 597 | // |
| 598 | Module : MethodList { |
| 599 | $$ = ParserResult = $1; |
| 600 | CurModule.ModuleDone(); |
| 601 | } |
| 602 | |
| 603 | MethodList : MethodList Method { |
| 604 | $1->getMethodList().push_back($2); |
| 605 | CurMeth.MethodDone(); |
| 606 | $$ = $1; |
| 607 | } |
| 608 | | ConstPool IMPLEMENTATION { |
| 609 | $$ = CurModule.CurrentModule; |
| 610 | } |
| 611 | |
| 612 | |
| 613 | //===----------------------------------------------------------------------===// |
| 614 | // Rules to match Method Headers |
| 615 | //===----------------------------------------------------------------------===// |
| 616 | |
| 617 | OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; } |
| 618 | |
| 619 | ArgVal : Types OptVAR_ID { |
| 620 | $$ = new MethodArgument($1); |
| 621 | if ($2) { // Was the argument named? |
| 622 | $$->setName($2); |
| 623 | free($2); // The string was strdup'd, so free it now. |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | ArgListH : ArgVal ',' ArgListH { |
| 628 | $$ = $3; |
| 629 | $3->push_front($1); |
| 630 | } |
| 631 | | ArgVal { |
| 632 | $$ = new list<MethodArgument*>(); |
| 633 | $$->push_front($1); |
| 634 | } |
| 635 | |
| 636 | ArgList : ArgListH { |
| 637 | $$ = $1; |
| 638 | } |
| 639 | | /* empty */ { |
| 640 | $$ = 0; |
| 641 | } |
| 642 | |
| 643 | MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' { |
| 644 | MethodType::ParamTypes ParamTypeList; |
| 645 | if ($4) |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 646 | for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 647 | ParamTypeList.push_back((*I)->getType()); |
| 648 | |
| 649 | const MethodType *MT = MethodType::getMethodType($1, ParamTypeList); |
| 650 | |
| 651 | Method *M = new Method(MT, $2); |
| 652 | free($2); // Free strdup'd memory! |
| 653 | |
| 654 | InsertValue(M, CurModule.Values); |
| 655 | |
| 656 | CurMeth.MethodStart(M); |
| 657 | |
| 658 | // Add all of the arguments we parsed to the method... |
| 659 | if ($4) { // Is null if empty... |
| 660 | Method::ArgumentListType &ArgList = M->getArgumentList(); |
| 661 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 662 | for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 663 | InsertValue(*I); |
| 664 | ArgList.push_back(*I); |
| 665 | } |
| 666 | delete $4; // We're now done with the argument list |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | MethodHeader : MethodHeaderH ConstPool BEGINTOK { |
| 671 | $$ = CurMeth.CurrentMethod; |
| 672 | } |
| 673 | |
| 674 | Method : BasicBlockList END { |
| 675 | $$ = $1; |
| 676 | } |
| 677 | |
| 678 | |
| 679 | //===----------------------------------------------------------------------===// |
| 680 | // Rules to match Basic Blocks |
| 681 | //===----------------------------------------------------------------------===// |
| 682 | |
| 683 | ConstValueRef : ESINT64VAL { // A reference to a direct constant |
| 684 | $$ = ValID::create($1); |
| 685 | } |
| 686 | | EUINT64VAL { |
| 687 | $$ = ValID::create($1); |
| 688 | } |
| 689 | | TRUE { |
| 690 | $$ = ValID::create((int64_t)1); |
| 691 | } |
| 692 | | FALSE { |
| 693 | $$ = ValID::create((int64_t)0); |
| 694 | } |
| 695 | | STRINGCONSTANT { // Quoted strings work too... especially for methods |
| 696 | $$ = ValID::create_conststr($1); |
| 697 | } |
| 698 | |
| 699 | // ValueRef - A reference to a definition... |
| 700 | ValueRef : INTVAL { // Is it an integer reference...? |
| 701 | $$ = ValID::create($1); |
| 702 | } |
| 703 | | VAR_ID { // It must be a named reference then... |
| 704 | $$ = ValID::create($1); |
| 705 | } |
| 706 | | ConstValueRef { |
| 707 | $$ = $1; |
| 708 | } |
| 709 | |
| 710 | // The user may refer to a user defined type by its typeplane... check for this |
| 711 | // now... |
| 712 | // |
| 713 | Types : ValueRef { |
| 714 | Value *D = getVal(Type::TypeTy, $1, true); |
| 715 | if (D == 0) ThrowException("Invalid user defined type: " + $1.getName()); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 716 | |
| 717 | // User defined type not in const pool! |
| 718 | ConstPoolType *CPT = (ConstPoolType*)D->castConstantAsserting(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 719 | $$ = CPT->getValue(); |
| 720 | } |
| 721 | | TypesV '(' TypeList ')' { // Method derived type? |
| 722 | MethodType::ParamTypes Params($3->begin(), $3->end()); |
| 723 | delete $3; |
| 724 | $$ = MethodType::getMethodType($1, Params); |
| 725 | } |
| 726 | | TypesV '(' ')' { // Method derived type? |
| 727 | MethodType::ParamTypes Params; // Empty list |
| 728 | $$ = MethodType::getMethodType($1, Params); |
| 729 | } |
| 730 | | '[' Types ']' { |
| 731 | $$ = ArrayType::getArrayType($2); |
| 732 | } |
| 733 | | '[' EUINT64VAL 'x' Types ']' { |
| 734 | $$ = ArrayType::getArrayType($4, (int)$2); |
| 735 | } |
| 736 | | '{' TypeList '}' { |
| 737 | StructType::ElementTypes Elements($2->begin(), $2->end()); |
| 738 | delete $2; |
| 739 | $$ = StructType::getStructType(Elements); |
| 740 | } |
| 741 | | '{' '}' { |
| 742 | $$ = StructType::getStructType(StructType::ElementTypes()); |
| 743 | } |
| 744 | | Types '*' { |
| 745 | $$ = PointerType::getPointerType($1); |
| 746 | } |
| 747 | |
| 748 | |
| 749 | TypeList : Types { |
| 750 | $$ = new list<const Type*>(); |
| 751 | $$->push_back($1); |
| 752 | } |
| 753 | | TypeList ',' Types { |
| 754 | ($$=$1)->push_back($3); |
| 755 | } |
| 756 | |
| 757 | |
| 758 | BasicBlockList : BasicBlockList BasicBlock { |
| 759 | $1->getBasicBlocks().push_back($2); |
| 760 | $$ = $1; |
| 761 | } |
| 762 | | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks |
| 763 | $$ = $1; // in them... |
| 764 | $1->getBasicBlocks().push_back($2); |
| 765 | } |
| 766 | |
| 767 | |
| 768 | // Basic blocks are terminated by branching instructions: |
| 769 | // br, br/cc, switch, ret |
| 770 | // |
| 771 | BasicBlock : InstructionList BBTerminatorInst { |
| 772 | $1->getInstList().push_back($2); |
| 773 | InsertValue($1); |
| 774 | $$ = $1; |
| 775 | } |
| 776 | | LABELSTR InstructionList BBTerminatorInst { |
| 777 | $2->getInstList().push_back($3); |
| 778 | $2->setName($1); |
| 779 | free($1); // Free the strdup'd memory... |
| 780 | |
| 781 | InsertValue($2); |
| 782 | $$ = $2; |
| 783 | } |
| 784 | |
| 785 | InstructionList : InstructionList Inst { |
| 786 | $1->getInstList().push_back($2); |
| 787 | $$ = $1; |
| 788 | } |
| 789 | | /* empty */ { |
| 790 | $$ = new BasicBlock(); |
| 791 | } |
| 792 | |
| 793 | BBTerminatorInst : RET Types ValueRef { // Return with a result... |
| 794 | $$ = new ReturnInst(getVal($2, $3)); |
| 795 | } |
| 796 | | RET VOID { // Return with no result... |
| 797 | $$ = new ReturnInst(); |
| 798 | } |
| 799 | | BR LABEL ValueRef { // Unconditional Branch... |
| 800 | $$ = new BranchInst((BasicBlock*)getVal(Type::LabelTy, $3)); |
| 801 | } // Conditional Branch... |
| 802 | | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef { |
| 803 | $$ = new BranchInst((BasicBlock*)getVal(Type::LabelTy, $6), |
| 804 | (BasicBlock*)getVal(Type::LabelTy, $9), |
| 805 | getVal(Type::BoolTy, $3)); |
| 806 | } |
| 807 | | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' { |
| 808 | SwitchInst *S = new SwitchInst(getVal($2, $3), |
| 809 | (BasicBlock*)getVal(Type::LabelTy, $6)); |
| 810 | $$ = S; |
| 811 | |
| 812 | list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(), |
| 813 | end = $8->end(); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 814 | for (; I != end; ++I) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 815 | S->dest_push_back(I->first, I->second); |
| 816 | } |
| 817 | |
| 818 | JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef { |
| 819 | $$ = $1; |
| 820 | ConstPoolVal *V = (ConstPoolVal*)getVal($2, $3, true); |
| 821 | if (V == 0) |
| 822 | ThrowException("May only switch on a constant pool value!"); |
| 823 | |
| 824 | $$->push_back(make_pair(V, (BasicBlock*)getVal($5, $6))); |
| 825 | } |
| 826 | | IntType ConstValueRef ',' LABEL ValueRef { |
| 827 | $$ = new list<pair<ConstPoolVal*, BasicBlock*> >(); |
| 828 | ConstPoolVal *V = (ConstPoolVal*)getVal($1, $2, true); |
| 829 | |
| 830 | if (V == 0) |
| 831 | ThrowException("May only switch on a constant pool value!"); |
| 832 | |
| 833 | $$->push_back(make_pair(V, (BasicBlock*)getVal($4, $5))); |
| 834 | } |
| 835 | |
| 836 | Inst : OptAssign InstVal { |
| 837 | if ($1) // Is this definition named?? |
| 838 | $2->setName($1); // if so, assign the name... |
| 839 | |
| 840 | InsertValue($2); |
| 841 | $$ = $2; |
| 842 | } |
| 843 | |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 844 | PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes |
| 845 | $$ = new list<pair<Value*, BasicBlock*> >(); |
| 846 | $$->push_back(make_pair(getVal($1, $3), |
| 847 | (BasicBlock*)getVal(Type::LabelTy, $5))); |
| 848 | } |
| 849 | | PHIList ',' '[' ValueRef ',' ValueRef ']' { |
| 850 | $$ = $1; |
| 851 | $1->push_back(make_pair(getVal($1->front().first->getType(), $4), |
| 852 | (BasicBlock*)getVal(Type::LabelTy, $6))); |
| 853 | } |
| 854 | |
| 855 | |
| 856 | ValueRefList : Types ValueRef { // Used for call statements... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 857 | $$ = new list<Value*>(); |
| 858 | $$->push_back(getVal($1, $2)); |
| 859 | } |
| 860 | | ValueRefList ',' ValueRef { |
| 861 | $$ = $1; |
| 862 | $1->push_back(getVal($1->front()->getType(), $3)); |
| 863 | } |
| 864 | |
| 865 | // ValueRefListE - Just like ValueRefList, except that it may also be empty! |
| 866 | ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; } |
| 867 | |
| 868 | InstVal : BinaryOps Types ValueRef ',' ValueRef { |
Chris Lattner | bebd60d | 2001-06-25 07:31:31 +0000 | [diff] [blame] | 869 | $$ = BinaryOperator::create($1, getVal($2, $3), getVal($2, $5)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 870 | if ($$ == 0) |
| 871 | ThrowException("binary operator returned null!"); |
| 872 | } |
| 873 | | UnaryOps Types ValueRef { |
Chris Lattner | bebd60d | 2001-06-25 07:31:31 +0000 | [diff] [blame] | 874 | $$ = UnaryOperator::create($1, getVal($2, $3)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 875 | if ($$ == 0) |
| 876 | ThrowException("unary operator returned null!"); |
| 877 | } |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 878 | | PHI PHIList { |
| 879 | const Type *Ty = $2->front().first->getType(); |
| 880 | $$ = new PHINode(Ty); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 881 | while ($2->begin() != $2->end()) { |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 882 | if ($2->front().first->getType() != Ty) |
| 883 | ThrowException("All elements of a PHI node must be of the same type!"); |
| 884 | ((PHINode*)$$)->addIncoming($2->front().first, $2->front().second); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 885 | $2->pop_front(); |
| 886 | } |
| 887 | delete $2; // Free the list... |
| 888 | } |
| 889 | | CALL Types ValueRef '(' ValueRefListE ')' { |
| 890 | if (!$2->isMethodType()) |
| 891 | ThrowException("Can only call methods: invalid type '" + |
| 892 | $2->getName() + "'!"); |
| 893 | |
| 894 | const MethodType *Ty = (const MethodType*)$2; |
| 895 | |
| 896 | Value *V = getVal(Ty, $3); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 897 | if (!V->isMethod() || V->getType() != Ty) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 898 | ThrowException("Cannot call: " + $3.getName() + "!"); |
| 899 | |
| 900 | // Create or access a new type that corresponds to the function call... |
| 901 | vector<Value *> Params; |
| 902 | |
| 903 | if ($5) { |
| 904 | // Pull out just the arguments... |
| 905 | Params.insert(Params.begin(), $5->begin(), $5->end()); |
| 906 | delete $5; |
| 907 | |
| 908 | // Loop through MethodType's arguments and ensure they are specified |
| 909 | // correctly! |
| 910 | // |
| 911 | MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin(); |
| 912 | unsigned i; |
| 913 | for (i = 0; i < Params.size() && I != Ty->getParamTypes().end(); ++i,++I){ |
| 914 | if (Params[i]->getType() != *I) |
| 915 | ThrowException("Parameter " + utostr(i) + " is not of type '" + |
| 916 | (*I)->getName() + "'!"); |
| 917 | } |
| 918 | |
| 919 | if (i != Params.size() || I != Ty->getParamTypes().end()) |
| 920 | ThrowException("Invalid number of parameters detected!"); |
| 921 | } |
| 922 | |
| 923 | // Create the call node... |
| 924 | $$ = new CallInst((Method*)V, Params); |
| 925 | } |
| 926 | | MemoryInst { |
| 927 | $$ = $1; |
| 928 | } |
| 929 | |
| 930 | MemoryInst : MALLOC Types { |
Chris Lattner | f0d0e9c | 2001-07-07 08:36:30 +0000 | [diff] [blame^] | 931 | const Type *Ty = PointerType::getPointerType($2); |
| 932 | addConstValToConstantPool(new ConstPoolType(Ty)); |
| 933 | $$ = new MallocInst(Ty); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 934 | } |
| 935 | | MALLOC Types ',' UINT ValueRef { |
| 936 | if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized()) |
| 937 | ThrowException("Trying to allocate " + $2->getName() + |
| 938 | " as unsized array!"); |
Chris Lattner | f0d0e9c | 2001-07-07 08:36:30 +0000 | [diff] [blame^] | 939 | const Type *Ty = PointerType::getPointerType($2); |
| 940 | addConstValToConstantPool(new ConstPoolType(Ty)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 941 | Value *ArrSize = getVal($4, $5); |
Chris Lattner | f0d0e9c | 2001-07-07 08:36:30 +0000 | [diff] [blame^] | 942 | $$ = new MallocInst(Ty, ArrSize); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 943 | } |
| 944 | | ALLOCA Types { |
Chris Lattner | f0d0e9c | 2001-07-07 08:36:30 +0000 | [diff] [blame^] | 945 | const Type *Ty = PointerType::getPointerType($2); |
| 946 | addConstValToConstantPool(new ConstPoolType(Ty)); |
| 947 | $$ = new AllocaInst(Ty); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 948 | } |
| 949 | | ALLOCA Types ',' UINT ValueRef { |
| 950 | if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized()) |
| 951 | ThrowException("Trying to allocate " + $2->getName() + |
| 952 | " as unsized array!"); |
Chris Lattner | f0d0e9c | 2001-07-07 08:36:30 +0000 | [diff] [blame^] | 953 | const Type *Ty = PointerType::getPointerType($2); |
| 954 | addConstValToConstantPool(new ConstPoolType(Ty)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 955 | Value *ArrSize = getVal($4, $5); |
Chris Lattner | f0d0e9c | 2001-07-07 08:36:30 +0000 | [diff] [blame^] | 956 | $$ = new AllocaInst(Ty, ArrSize); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 957 | } |
| 958 | | FREE Types ValueRef { |
| 959 | if (!$2->isPointerType()) |
| 960 | ThrowException("Trying to free nonpointer type " + $2->getName() + "!"); |
| 961 | $$ = new FreeInst(getVal($2, $3)); |
| 962 | } |
| 963 | |
| 964 | %% |
| 965 | int yyerror(char *ErrorMsg) { |
| 966 | ThrowException(string("Parse error: ") + ErrorMsg); |
| 967 | return 0; |
| 968 | } |