From b3b40bc8b99ba429df9f675c6d51856fa2ea3f1a Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 9 Jul 2001 19:38:36 +0000 Subject: [PATCH] Make sure that types go in the constant pool if they are used. llvm-svn: 171 --- lib/AsmParser/llvmAsmParser.cpp | 271 ++++++++++++++++++-------------- lib/AsmParser/llvmAsmParser.y | 69 +++++--- 2 files changed, 199 insertions(+), 141 deletions(-) diff --git a/lib/AsmParser/llvmAsmParser.cpp b/lib/AsmParser/llvmAsmParser.cpp index 95b97219959..a2ffb8fece8 100644 --- a/lib/AsmParser/llvmAsmParser.cpp +++ b/lib/AsmParser/llvmAsmParser.cpp @@ -82,6 +82,7 @@ #include "llvm/iMemory.h" #include #include // Get definition of pair class +#include // Get definition of find_if #include // This embarasment is due to our flex lexer... int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit @@ -371,6 +372,41 @@ static ConstPoolVal *addConstValToConstantPool(ConstPoolVal *C) { } } + +struct EqualsType { + const Type *T; + inline EqualsType(const Type *t) { T = t; } + inline bool operator()(const ConstPoolVal *CPV) const { + return static_cast(CPV)->getValue() == T; + } +}; + + +// checkNewType - We have to be careful to add all types referenced by the +// program to the constant pool of the method or module. Because of this, we +// often want to check to make sure that types used are in the constant pool, +// and add them if they aren't. That's what this function does. +// +static const Type *checkNewType(const Type *Ty) { + ConstantPool &CP = CurMeth.CurrentMethod ? + CurMeth.CurrentMethod->getConstantPool() : + CurModule.CurrentModule->getConstantPool(); + + // Get the type type plane... + ConstantPool::PlaneType &P = CP.getPlane(Type::TypeTy); + ConstantPool::PlaneType::const_iterator PI = find_if(P.begin(), P.end(), + EqualsType(Ty)); + if (PI == P.end()) { + vector &ValTab = CurMeth.CurrentMethod ? + CurMeth.Values : CurModule.Values; + ConstPoolVal *CPT = new ConstPoolType(Ty); + CP.insert(CPT); + InsertValue(CPT, ValTab); + } + return Ty; +} + + //===----------------------------------------------------------------------===// // RunVMAsmParser - Define an interface to this parser //===----------------------------------------------------------------------===// @@ -391,7 +427,7 @@ Module *RunVMAsmParser(const ToolCommandLine &Opts, FILE *F) { } -#line 337 "llvmAsmParser.y" +#line 373 "llvmAsmParser.y" typedef union { Module *ModuleVal; Method *MethodVal; @@ -540,19 +576,19 @@ static const short yyrhs[] = { 5, #if YYDEBUG != 0 static const short yyrline[] = { 0, - 434, 435, 442, 443, 454, 454, 454, 454, 454, 454, - 454, 455, 455, 455, 455, 455, 455, 455, 458, 458, - 463, 464, 464, 464, 464, 464, 465, 465, 465, 465, - 465, 465, 466, 466, 470, 470, 470, 470, 471, 471, - 471, 471, 472, 472, 474, 477, 481, 486, 491, 494, - 497, 503, 506, 519, 523, 541, 548, 556, 570, 573, - 579, 587, 598, 603, 608, 617, 617, 619, 627, 631, - 636, 639, 643, 670, 674, 683, 686, 689, 692, 695, - 700, 703, 706, 713, 721, 726, 730, 733, 736, 741, - 744, 749, 753, 758, 762, 771, 776, 785, 789, 793, - 796, 799, 802, 807, 818, 826, 836, 844, 849, 856, - 860, 866, 866, 868, 873, 878, 882, 885, 896, 933, - 938, 940, 944, 949, 958, 963, 972, 978, 987, 999 + 470, 471, 478, 479, 490, 490, 490, 490, 490, 490, + 490, 491, 491, 491, 491, 491, 491, 491, 494, 494, + 499, 500, 500, 500, 500, 500, 501, 501, 501, 501, + 501, 501, 502, 502, 506, 506, 506, 506, 507, 507, + 507, 507, 508, 508, 510, 513, 517, 522, 527, 530, + 533, 539, 542, 555, 559, 577, 584, 592, 606, 609, + 615, 623, 634, 639, 644, 653, 653, 655, 663, 667, + 672, 675, 679, 706, 710, 719, 722, 725, 728, 731, + 736, 739, 742, 749, 757, 762, 766, 769, 772, 777, + 780, 785, 789, 794, 798, 807, 812, 821, 825, 829, + 832, 835, 838, 843, 854, 862, 872, 880, 885, 892, + 896, 902, 902, 904, 909, 914, 918, 921, 932, 969, + 974, 976, 980, 983, 990, 993, 1001, 1007, 1016, 1028 }; #endif @@ -1339,7 +1375,7 @@ yyreduce: switch (yyn) { case 2: -#line 435 "llvmAsmParser.y" +#line 471 "llvmAsmParser.y" { if (yyvsp[0].UIntVal > (uint32_t)INT32_MAX) // Outside of my range! ThrowException("Value too large for type!"); @@ -1347,7 +1383,7 @@ case 2: ; break;} case 4: -#line 443 "llvmAsmParser.y" +#line 479 "llvmAsmParser.y" { if (yyvsp[0].UInt64Val > (uint64_t)INT64_MAX) // Outside of my range! ThrowException("Value too large for type!"); @@ -1355,19 +1391,19 @@ case 4: ; break;} case 45: -#line 474 "llvmAsmParser.y" +#line 510 "llvmAsmParser.y" { yyval.StrVal = yyvsp[-1].StrVal; ; break;} case 46: -#line 477 "llvmAsmParser.y" +#line 513 "llvmAsmParser.y" { yyval.StrVal = 0; ; break;} case 47: -#line 481 "llvmAsmParser.y" +#line 517 "llvmAsmParser.y" { // integral constants if (!ConstPoolSInt::isValueValidForType(yyvsp[-1].TypeVal, yyvsp[0].SInt64Val)) ThrowException("Constant value doesn't fit in type!"); @@ -1375,7 +1411,7 @@ case 47: ; break;} case 48: -#line 486 "llvmAsmParser.y" +#line 522 "llvmAsmParser.y" { // integral constants if (!ConstPoolUInt::isValueValidForType(yyvsp[-1].TypeVal, yyvsp[0].UInt64Val)) ThrowException("Constant value doesn't fit in type!"); @@ -1383,19 +1419,19 @@ case 48: ; break;} case 49: -#line 491 "llvmAsmParser.y" +#line 527 "llvmAsmParser.y" { // Boolean constants yyval.ConstVal = new ConstPoolBool(true); ; break;} case 50: -#line 494 "llvmAsmParser.y" +#line 530 "llvmAsmParser.y" { // Boolean constants yyval.ConstVal = new ConstPoolBool(false); ; break;} case 51: -#line 497 "llvmAsmParser.y" +#line 533 "llvmAsmParser.y" { // String constants cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n"; abort(); @@ -1404,13 +1440,13 @@ case 51: ; break;} case 52: -#line 503 "llvmAsmParser.y" +#line 539 "llvmAsmParser.y" { // Type constants yyval.ConstVal = new ConstPoolType(yyvsp[0].TypeVal); ; break;} case 53: -#line 506 "llvmAsmParser.y" +#line 542 "llvmAsmParser.y" { // Nonempty array constant // Verify all elements are correct type! const ArrayType *AT = ArrayType::getArrayType(yyvsp[-4].TypeVal); @@ -1426,14 +1462,14 @@ case 53: ; break;} case 54: -#line 519 "llvmAsmParser.y" +#line 555 "llvmAsmParser.y" { // Empty array constant vector Empty; yyval.ConstVal = new ConstPoolArray(ArrayType::getArrayType(yyvsp[-3].TypeVal), Empty); ; break;} case 55: -#line 523 "llvmAsmParser.y" +#line 559 "llvmAsmParser.y" { // Verify all elements are correct type! const ArrayType *AT = ArrayType::getArrayType(yyvsp[-4].TypeVal, (int)yyvsp[-6].UInt64Val); @@ -1454,7 +1490,7 @@ case 55: ; break;} case 56: -#line 541 "llvmAsmParser.y" +#line 577 "llvmAsmParser.y" { if (yyvsp[-5].UInt64Val != 0) ThrowException("Type mismatch: constant sized array initialized with 0" @@ -1464,7 +1500,7 @@ case 56: ; break;} case 57: -#line 548 "llvmAsmParser.y" +#line 584 "llvmAsmParser.y" { StructType::ElementTypes Types(yyvsp[-4].TypeList->begin(), yyvsp[-4].TypeList->end()); delete yyvsp[-4].TypeList; @@ -1475,7 +1511,7 @@ case 57: ; break;} case 58: -#line 556 "llvmAsmParser.y" +#line 592 "llvmAsmParser.y" { const StructType *St = StructType::getStructType(StructType::ElementTypes()); @@ -1484,20 +1520,20 @@ case 58: ; break;} case 59: -#line 570 "llvmAsmParser.y" +#line 606 "llvmAsmParser.y" { (yyval.ConstVector = yyvsp[-2].ConstVector)->push_back(addConstValToConstantPool(yyvsp[0].ConstVal)); ; break;} case 60: -#line 573 "llvmAsmParser.y" +#line 609 "llvmAsmParser.y" { yyval.ConstVector = new vector(); yyval.ConstVector->push_back(addConstValToConstantPool(yyvsp[0].ConstVal)); ; break;} case 61: -#line 579 "llvmAsmParser.y" +#line 615 "llvmAsmParser.y" { if (yyvsp[-1].StrVal) { yyvsp[0].ConstVal->setName(yyvsp[-1].StrVal); @@ -1508,19 +1544,19 @@ case 61: ; break;} case 62: -#line 587 "llvmAsmParser.y" +#line 623 "llvmAsmParser.y" { ; break;} case 63: -#line 598 "llvmAsmParser.y" +#line 634 "llvmAsmParser.y" { yyval.ModuleVal = ParserResult = yyvsp[0].ModuleVal; CurModule.ModuleDone(); ; break;} case 64: -#line 603 "llvmAsmParser.y" +#line 639 "llvmAsmParser.y" { yyvsp[-1].ModuleVal->getMethodList().push_back(yyvsp[0].MethodVal); CurMeth.MethodDone(); @@ -1528,17 +1564,17 @@ case 64: ; break;} case 65: -#line 608 "llvmAsmParser.y" +#line 644 "llvmAsmParser.y" { yyval.ModuleVal = CurModule.CurrentModule; ; break;} case 67: -#line 617 "llvmAsmParser.y" +#line 653 "llvmAsmParser.y" { yyval.StrVal = 0; ; break;} case 68: -#line 619 "llvmAsmParser.y" +#line 655 "llvmAsmParser.y" { yyval.MethArgVal = new MethodArgument(yyvsp[-1].TypeVal); if (yyvsp[0].StrVal) { // Was the argument named? @@ -1548,33 +1584,33 @@ case 68: ; break;} case 69: -#line 627 "llvmAsmParser.y" +#line 663 "llvmAsmParser.y" { yyval.MethodArgList = yyvsp[0].MethodArgList; yyvsp[0].MethodArgList->push_front(yyvsp[-2].MethArgVal); ; break;} case 70: -#line 631 "llvmAsmParser.y" +#line 667 "llvmAsmParser.y" { yyval.MethodArgList = new list(); yyval.MethodArgList->push_front(yyvsp[0].MethArgVal); ; break;} case 71: -#line 636 "llvmAsmParser.y" +#line 672 "llvmAsmParser.y" { yyval.MethodArgList = yyvsp[0].MethodArgList; ; break;} case 72: -#line 639 "llvmAsmParser.y" +#line 675 "llvmAsmParser.y" { yyval.MethodArgList = 0; ; break;} case 73: -#line 643 "llvmAsmParser.y" +#line 679 "llvmAsmParser.y" { MethodType::ParamTypes ParamTypeList; if (yyvsp[-1].MethodArgList) @@ -1603,67 +1639,67 @@ case 73: ; break;} case 74: -#line 670 "llvmAsmParser.y" +#line 706 "llvmAsmParser.y" { yyval.MethodVal = CurMeth.CurrentMethod; ; break;} case 75: -#line 674 "llvmAsmParser.y" +#line 710 "llvmAsmParser.y" { yyval.MethodVal = yyvsp[-1].MethodVal; ; break;} case 76: -#line 683 "llvmAsmParser.y" +#line 719 "llvmAsmParser.y" { // A reference to a direct constant yyval.ValIDVal = ValID::create(yyvsp[0].SInt64Val); ; break;} case 77: -#line 686 "llvmAsmParser.y" +#line 722 "llvmAsmParser.y" { yyval.ValIDVal = ValID::create(yyvsp[0].UInt64Val); ; break;} case 78: -#line 689 "llvmAsmParser.y" +#line 725 "llvmAsmParser.y" { yyval.ValIDVal = ValID::create((int64_t)1); ; break;} case 79: -#line 692 "llvmAsmParser.y" +#line 728 "llvmAsmParser.y" { yyval.ValIDVal = ValID::create((int64_t)0); ; break;} case 80: -#line 695 "llvmAsmParser.y" +#line 731 "llvmAsmParser.y" { // Quoted strings work too... especially for methods yyval.ValIDVal = ValID::create_conststr(yyvsp[0].StrVal); ; break;} case 81: -#line 700 "llvmAsmParser.y" +#line 736 "llvmAsmParser.y" { // Is it an integer reference...? yyval.ValIDVal = ValID::create(yyvsp[0].SIntVal); ; break;} case 82: -#line 703 "llvmAsmParser.y" +#line 739 "llvmAsmParser.y" { // It must be a named reference then... yyval.ValIDVal = ValID::create(yyvsp[0].StrVal); ; break;} case 83: -#line 706 "llvmAsmParser.y" +#line 742 "llvmAsmParser.y" { yyval.ValIDVal = yyvsp[0].ValIDVal; ; break;} case 84: -#line 713 "llvmAsmParser.y" +#line 749 "llvmAsmParser.y" { Value *D = getVal(Type::TypeTy, yyvsp[0].ValIDVal, true); if (D == 0) ThrowException("Invalid user defined type: " + yyvsp[0].ValIDVal.getName()); @@ -1674,81 +1710,81 @@ case 84: ; break;} case 85: -#line 721 "llvmAsmParser.y" +#line 757 "llvmAsmParser.y" { // Method derived type? MethodType::ParamTypes Params(yyvsp[-1].TypeList->begin(), yyvsp[-1].TypeList->end()); delete yyvsp[-1].TypeList; - yyval.TypeVal = MethodType::getMethodType(yyvsp[-3].TypeVal, Params); + yyval.TypeVal = checkNewType(MethodType::getMethodType(yyvsp[-3].TypeVal, Params)); ; break;} case 86: -#line 726 "llvmAsmParser.y" +#line 762 "llvmAsmParser.y" { // Method derived type? MethodType::ParamTypes Params; // Empty list - yyval.TypeVal = MethodType::getMethodType(yyvsp[-2].TypeVal, Params); + yyval.TypeVal = checkNewType(MethodType::getMethodType(yyvsp[-2].TypeVal, Params)); ; break;} case 87: -#line 730 "llvmAsmParser.y" +#line 766 "llvmAsmParser.y" { - yyval.TypeVal = ArrayType::getArrayType(yyvsp[-1].TypeVal); + yyval.TypeVal = checkNewType(ArrayType::getArrayType(yyvsp[-1].TypeVal)); ; break;} case 88: -#line 733 "llvmAsmParser.y" +#line 769 "llvmAsmParser.y" { - yyval.TypeVal = ArrayType::getArrayType(yyvsp[-1].TypeVal, (int)yyvsp[-3].UInt64Val); + yyval.TypeVal = checkNewType(ArrayType::getArrayType(yyvsp[-1].TypeVal, (int)yyvsp[-3].UInt64Val)); ; break;} case 89: -#line 736 "llvmAsmParser.y" +#line 772 "llvmAsmParser.y" { StructType::ElementTypes Elements(yyvsp[-1].TypeList->begin(), yyvsp[-1].TypeList->end()); delete yyvsp[-1].TypeList; - yyval.TypeVal = StructType::getStructType(Elements); + yyval.TypeVal = checkNewType(StructType::getStructType(Elements)); ; break;} case 90: -#line 741 "llvmAsmParser.y" +#line 777 "llvmAsmParser.y" { - yyval.TypeVal = StructType::getStructType(StructType::ElementTypes()); + yyval.TypeVal = checkNewType(StructType::getStructType(StructType::ElementTypes())); ; break;} case 91: -#line 744 "llvmAsmParser.y" +#line 780 "llvmAsmParser.y" { - yyval.TypeVal = PointerType::getPointerType(yyvsp[-1].TypeVal); + yyval.TypeVal = checkNewType(PointerType::getPointerType(yyvsp[-1].TypeVal)); ; break;} case 92: -#line 749 "llvmAsmParser.y" +#line 785 "llvmAsmParser.y" { yyval.TypeList = new list(); yyval.TypeList->push_back(yyvsp[0].TypeVal); ; break;} case 93: -#line 753 "llvmAsmParser.y" +#line 789 "llvmAsmParser.y" { (yyval.TypeList=yyvsp[-2].TypeList)->push_back(yyvsp[0].TypeVal); ; break;} case 94: -#line 758 "llvmAsmParser.y" +#line 794 "llvmAsmParser.y" { yyvsp[-1].MethodVal->getBasicBlocks().push_back(yyvsp[0].BasicBlockVal); yyval.MethodVal = yyvsp[-1].MethodVal; ; break;} case 95: -#line 762 "llvmAsmParser.y" +#line 798 "llvmAsmParser.y" { // Do not allow methods with 0 basic blocks yyval.MethodVal = yyvsp[-1].MethodVal; // in them... yyvsp[-1].MethodVal->getBasicBlocks().push_back(yyvsp[0].BasicBlockVal); ; break;} case 96: -#line 771 "llvmAsmParser.y" +#line 807 "llvmAsmParser.y" { yyvsp[-1].BasicBlockVal->getInstList().push_back(yyvsp[0].TermInstVal); InsertValue(yyvsp[-1].BasicBlockVal); @@ -1756,7 +1792,7 @@ case 96: ; break;} case 97: -#line 776 "llvmAsmParser.y" +#line 812 "llvmAsmParser.y" { yyvsp[-1].BasicBlockVal->getInstList().push_back(yyvsp[0].TermInstVal); yyvsp[-1].BasicBlockVal->setName(yyvsp[-2].StrVal); @@ -1767,38 +1803,38 @@ case 97: ; break;} case 98: -#line 785 "llvmAsmParser.y" +#line 821 "llvmAsmParser.y" { yyvsp[-1].BasicBlockVal->getInstList().push_back(yyvsp[0].InstVal); yyval.BasicBlockVal = yyvsp[-1].BasicBlockVal; ; break;} case 99: -#line 789 "llvmAsmParser.y" +#line 825 "llvmAsmParser.y" { yyval.BasicBlockVal = new BasicBlock(); ; break;} case 100: -#line 793 "llvmAsmParser.y" +#line 829 "llvmAsmParser.y" { // Return with a result... yyval.TermInstVal = new ReturnInst(getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal)); ; break;} case 101: -#line 796 "llvmAsmParser.y" +#line 832 "llvmAsmParser.y" { // Return with no result... yyval.TermInstVal = new ReturnInst(); ; break;} case 102: -#line 799 "llvmAsmParser.y" +#line 835 "llvmAsmParser.y" { // Unconditional Branch... yyval.TermInstVal = new BranchInst((BasicBlock*)getVal(Type::LabelTy, yyvsp[0].ValIDVal)); ; break;} case 103: -#line 802 "llvmAsmParser.y" +#line 838 "llvmAsmParser.y" { yyval.TermInstVal = new BranchInst((BasicBlock*)getVal(Type::LabelTy, yyvsp[-3].ValIDVal), (BasicBlock*)getVal(Type::LabelTy, yyvsp[0].ValIDVal), @@ -1806,7 +1842,7 @@ case 103: ; break;} case 104: -#line 807 "llvmAsmParser.y" +#line 843 "llvmAsmParser.y" { SwitchInst *S = new SwitchInst(getVal(yyvsp[-7].TypeVal, yyvsp[-6].ValIDVal), (BasicBlock*)getVal(Type::LabelTy, yyvsp[-3].ValIDVal)); @@ -1819,7 +1855,7 @@ case 104: ; break;} case 105: -#line 818 "llvmAsmParser.y" +#line 854 "llvmAsmParser.y" { yyval.JumpTable = yyvsp[-5].JumpTable; ConstPoolVal *V = (ConstPoolVal*)getVal(yyvsp[-4].TypeVal, yyvsp[-3].ValIDVal, true); @@ -1830,7 +1866,7 @@ case 105: ; break;} case 106: -#line 826 "llvmAsmParser.y" +#line 862 "llvmAsmParser.y" { yyval.JumpTable = new list >(); ConstPoolVal *V = (ConstPoolVal*)getVal(yyvsp[-4].TypeVal, yyvsp[-3].ValIDVal, true); @@ -1842,7 +1878,7 @@ case 106: ; break;} case 107: -#line 836 "llvmAsmParser.y" +#line 872 "llvmAsmParser.y" { if (yyvsp[-1].StrVal) // Is this definition named?? yyvsp[0].InstVal->setName(yyvsp[-1].StrVal); // if so, assign the name... @@ -1852,7 +1888,7 @@ case 107: ; break;} case 108: -#line 844 "llvmAsmParser.y" +#line 880 "llvmAsmParser.y" { // Used for PHI nodes yyval.PHIList = new list >(); yyval.PHIList->push_back(make_pair(getVal(yyvsp[-5].TypeVal, yyvsp[-3].ValIDVal), @@ -1860,7 +1896,7 @@ case 108: ; break;} case 109: -#line 849 "llvmAsmParser.y" +#line 885 "llvmAsmParser.y" { yyval.PHIList = yyvsp[-6].PHIList; yyvsp[-6].PHIList->push_back(make_pair(getVal(yyvsp[-6].PHIList->front().first->getType(), yyvsp[-3].ValIDVal), @@ -1868,25 +1904,25 @@ case 109: ; break;} case 110: -#line 856 "llvmAsmParser.y" +#line 892 "llvmAsmParser.y" { // Used for call statements... yyval.ValueList = new list(); yyval.ValueList->push_back(getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal)); ; break;} case 111: -#line 860 "llvmAsmParser.y" +#line 896 "llvmAsmParser.y" { yyval.ValueList = yyvsp[-3].ValueList; yyvsp[-3].ValueList->push_back(getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal)); ; break;} case 113: -#line 866 "llvmAsmParser.y" +#line 902 "llvmAsmParser.y" { yyval.ValueList = 0; ; break;} case 114: -#line 868 "llvmAsmParser.y" +#line 904 "llvmAsmParser.y" { yyval.InstVal = BinaryOperator::create(yyvsp[-4].BinaryOpVal, getVal(yyvsp[-3].TypeVal, yyvsp[-2].ValIDVal), getVal(yyvsp[-3].TypeVal, yyvsp[0].ValIDVal)); if (yyval.InstVal == 0) @@ -1894,7 +1930,7 @@ case 114: ; break;} case 115: -#line 873 "llvmAsmParser.y" +#line 909 "llvmAsmParser.y" { yyval.InstVal = UnaryOperator::create(yyvsp[-2].UnaryOpVal, getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal)); if (yyval.InstVal == 0) @@ -1902,20 +1938,20 @@ case 115: ; break;} case 116: -#line 878 "llvmAsmParser.y" +#line 914 "llvmAsmParser.y" { if (yyvsp[-1].TypeVal != Type::UByteTy) ThrowException("Shift amount must be ubyte!"); yyval.InstVal = new ShiftInst(yyvsp[-5].OtherOpVal, getVal(yyvsp[-4].TypeVal, yyvsp[-3].ValIDVal), getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal)); ; break;} case 117: -#line 882 "llvmAsmParser.y" +#line 918 "llvmAsmParser.y" { yyval.InstVal = new CastInst(getVal(yyvsp[-3].TypeVal, yyvsp[-2].ValIDVal), yyvsp[0].TypeVal); ; break;} case 118: -#line 885 "llvmAsmParser.y" +#line 921 "llvmAsmParser.y" { const Type *Ty = yyvsp[0].PHIList->front().first->getType(); yyval.InstVal = new PHINode(Ty); @@ -1929,7 +1965,7 @@ case 118: ; break;} case 119: -#line 896 "llvmAsmParser.y" +#line 932 "llvmAsmParser.y" { if (!yyvsp[-4].TypeVal->isMethodType()) ThrowException("Can only call methods: invalid type '" + @@ -1969,65 +2005,58 @@ case 119: ; break;} case 120: -#line 933 "llvmAsmParser.y" +#line 969 "llvmAsmParser.y" { yyval.InstVal = yyvsp[0].InstVal; ; break;} case 121: -#line 938 "llvmAsmParser.y" +#line 974 "llvmAsmParser.y" { yyval.ConstVector = yyvsp[0].ConstVector; ; break;} case 122: -#line 940 "llvmAsmParser.y" +#line 976 "llvmAsmParser.y" { yyval.ConstVector = new vector(); ; break;} case 123: -#line 944 "llvmAsmParser.y" +#line 980 "llvmAsmParser.y" { - const Type *Ty = PointerType::getPointerType(yyvsp[0].TypeVal); - addConstValToConstantPool(new ConstPoolType(Ty)); - yyval.InstVal = new MallocInst(Ty); + yyval.InstVal = new MallocInst(checkNewType(PointerType::getPointerType(yyvsp[0].TypeVal))); ; break;} case 124: -#line 949 "llvmAsmParser.y" +#line 983 "llvmAsmParser.y" { if (!yyvsp[-3].TypeVal->isArrayType() || ((const ArrayType*)yyvsp[-3].TypeVal)->isSized()) ThrowException("Trying to allocate " + yyvsp[-3].TypeVal->getName() + " as unsized array!"); - const Type *Ty = PointerType::getPointerType(yyvsp[-3].TypeVal); - addConstValToConstantPool(new ConstPoolType(Ty)); - Value *ArrSize = getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal); - yyval.InstVal = new MallocInst(Ty, ArrSize); + const Type *Ty = checkNewType(PointerType::getPointerType(yyvsp[-3].TypeVal)); + yyval.InstVal = new MallocInst(Ty, getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal)); ; break;} case 125: -#line 958 "llvmAsmParser.y" +#line 990 "llvmAsmParser.y" { - const Type *Ty = PointerType::getPointerType(yyvsp[0].TypeVal); - addConstValToConstantPool(new ConstPoolType(Ty)); - yyval.InstVal = new AllocaInst(Ty); + yyval.InstVal = new AllocaInst(checkNewType(PointerType::getPointerType(yyvsp[0].TypeVal))); ; break;} case 126: -#line 963 "llvmAsmParser.y" +#line 993 "llvmAsmParser.y" { if (!yyvsp[-3].TypeVal->isArrayType() || ((const ArrayType*)yyvsp[-3].TypeVal)->isSized()) ThrowException("Trying to allocate " + yyvsp[-3].TypeVal->getName() + " as unsized array!"); - const Type *Ty = PointerType::getPointerType(yyvsp[-3].TypeVal); - addConstValToConstantPool(new ConstPoolType(Ty)); + const Type *Ty = checkNewType(PointerType::getPointerType(yyvsp[-3].TypeVal)); Value *ArrSize = getVal(yyvsp[-1].TypeVal, yyvsp[0].ValIDVal); yyval.InstVal = new AllocaInst(Ty, ArrSize); ; break;} case 127: -#line 972 "llvmAsmParser.y" +#line 1001 "llvmAsmParser.y" { if (!yyvsp[-1].TypeVal->isPointerType()) ThrowException("Trying to free nonpointer type " + yyvsp[-1].TypeVal->getName() + "!"); @@ -2035,7 +2064,7 @@ case 127: ; break;} case 128: -#line 978 "llvmAsmParser.y" +#line 1007 "llvmAsmParser.y" { if (!yyvsp[-2].TypeVal->isPointerType()) ThrowException("Can't load from nonpointer type: " + yyvsp[-2].TypeVal->getName()); @@ -2047,7 +2076,7 @@ case 128: ; break;} case 129: -#line 987 "llvmAsmParser.y" +#line 1016 "llvmAsmParser.y" { if (!yyvsp[-2].TypeVal->isPointerType()) ThrowException("Can't store to a nonpointer type: " + yyvsp[-2].TypeVal->getName()); @@ -2062,7 +2091,7 @@ case 129: ; break;} case 130: -#line 999 "llvmAsmParser.y" +#line 1028 "llvmAsmParser.y" { if (!yyvsp[-2].TypeVal->isPointerType()) ThrowException("getelementptr insn requires pointer operand!"); @@ -2070,7 +2099,7 @@ case 130: ThrowException("Can't get element ptr '" + yyvsp[-2].TypeVal->getName() + "'!"); yyval.InstVal = new GetElementPtrInst(getVal(yyvsp[-2].TypeVal, yyvsp[-1].ValIDVal), *yyvsp[0].ConstVector); delete yyvsp[0].ConstVector; - addConstValToConstantPool(new ConstPoolType(yyval.InstVal->getType())); + checkNewType(yyval.InstVal->getType()); ; break;} } @@ -2295,7 +2324,7 @@ yyerrhandle: } return 1; } -#line 1009 "llvmAsmParser.y" +#line 1038 "llvmAsmParser.y" int yyerror(const char *ErrorMsg) { ThrowException(string("Parse error: ") + ErrorMsg); diff --git a/lib/AsmParser/llvmAsmParser.y b/lib/AsmParser/llvmAsmParser.y index 1b10042e037..a70e873cf46 100644 --- a/lib/AsmParser/llvmAsmParser.y +++ b/lib/AsmParser/llvmAsmParser.y @@ -24,6 +24,7 @@ #include "llvm/iMemory.h" #include #include // Get definition of pair class +#include // Get definition of find_if #include // This embarasment is due to our flex lexer... int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit @@ -313,6 +314,41 @@ static ConstPoolVal *addConstValToConstantPool(ConstPoolVal *C) { } } + +struct EqualsType { + const Type *T; + inline EqualsType(const Type *t) { T = t; } + inline bool operator()(const ConstPoolVal *CPV) const { + return static_cast(CPV)->getValue() == T; + } +}; + + +// checkNewType - We have to be careful to add all types referenced by the +// program to the constant pool of the method or module. Because of this, we +// often want to check to make sure that types used are in the constant pool, +// and add them if they aren't. That's what this function does. +// +static const Type *checkNewType(const Type *Ty) { + ConstantPool &CP = CurMeth.CurrentMethod ? + CurMeth.CurrentMethod->getConstantPool() : + CurModule.CurrentModule->getConstantPool(); + + // Get the type type plane... + ConstantPool::PlaneType &P = CP.getPlane(Type::TypeTy); + ConstantPool::PlaneType::const_iterator PI = find_if(P.begin(), P.end(), + EqualsType(Ty)); + if (PI == P.end()) { + vector &ValTab = CurMeth.CurrentMethod ? + CurMeth.Values : CurModule.Values; + ConstPoolVal *CPT = new ConstPoolType(Ty); + CP.insert(CPT); + InsertValue(CPT, ValTab); + } + return Ty; +} + + //===----------------------------------------------------------------------===// // RunVMAsmParser - Define an interface to this parser //===----------------------------------------------------------------------===// @@ -721,28 +757,28 @@ Types : ValueRef { | TypesV '(' TypeList ')' { // Method derived type? MethodType::ParamTypes Params($3->begin(), $3->end()); delete $3; - $$ = MethodType::getMethodType($1, Params); + $$ = checkNewType(MethodType::getMethodType($1, Params)); } | TypesV '(' ')' { // Method derived type? MethodType::ParamTypes Params; // Empty list - $$ = MethodType::getMethodType($1, Params); + $$ = checkNewType(MethodType::getMethodType($1, Params)); } | '[' Types ']' { - $$ = ArrayType::getArrayType($2); + $$ = checkNewType(ArrayType::getArrayType($2)); } | '[' EUINT64VAL 'x' Types ']' { - $$ = ArrayType::getArrayType($4, (int)$2); + $$ = checkNewType(ArrayType::getArrayType($4, (int)$2)); } | '{' TypeList '}' { StructType::ElementTypes Elements($2->begin(), $2->end()); delete $2; - $$ = StructType::getStructType(Elements); + $$ = checkNewType(StructType::getStructType(Elements)); } | '{' '}' { - $$ = StructType::getStructType(StructType::ElementTypes()); + $$ = checkNewType(StructType::getStructType(StructType::ElementTypes())); } | Types '*' { - $$ = PointerType::getPointerType($1); + $$ = checkNewType(PointerType::getPointerType($1)); } @@ -942,30 +978,23 @@ UByteList : ',' ConstVector { } MemoryInst : MALLOC Types { - const Type *Ty = PointerType::getPointerType($2); - addConstValToConstantPool(new ConstPoolType(Ty)); - $$ = new MallocInst(Ty); + $$ = new MallocInst(checkNewType(PointerType::getPointerType($2))); } | MALLOC Types ',' UINT ValueRef { if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized()) ThrowException("Trying to allocate " + $2->getName() + " as unsized array!"); - const Type *Ty = PointerType::getPointerType($2); - addConstValToConstantPool(new ConstPoolType(Ty)); - Value *ArrSize = getVal($4, $5); - $$ = new MallocInst(Ty, ArrSize); + const Type *Ty = checkNewType(PointerType::getPointerType($2)); + $$ = new MallocInst(Ty, getVal($4, $5)); } | ALLOCA Types { - const Type *Ty = PointerType::getPointerType($2); - addConstValToConstantPool(new ConstPoolType(Ty)); - $$ = new AllocaInst(Ty); + $$ = new AllocaInst(checkNewType(PointerType::getPointerType($2))); } | ALLOCA Types ',' UINT ValueRef { if (!$2->isArrayType() || ((const ArrayType*)$2)->isSized()) ThrowException("Trying to allocate " + $2->getName() + " as unsized array!"); - const Type *Ty = PointerType::getPointerType($2); - addConstValToConstantPool(new ConstPoolType(Ty)); + const Type *Ty = checkNewType(PointerType::getPointerType($2)); Value *ArrSize = getVal($4, $5); $$ = new AllocaInst(Ty, ArrSize); } @@ -1003,7 +1032,7 @@ MemoryInst : MALLOC Types { ThrowException("Can't get element ptr '" + $2->getName() + "'!"); $$ = new GetElementPtrInst(getVal($2, $3), *$4); delete $4; - addConstValToConstantPool(new ConstPoolType($$->getType())); + checkNewType($$->getType()); } %%