Deleted Added
full compact
1//===-- LLParser.cpp - Parser Class ---------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//

--- 4 unchanged lines hidden (view full) ---

13
14#include "LLParser.h"
15#include "llvm/AutoUpgrade.h"
16#include "llvm/CallingConv.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/InlineAsm.h"
20#include "llvm/Instructions.h"
21#include "llvm/LLVMContext.h"
22#include "llvm/MDNode.h"
23#include "llvm/Module.h"
24#include "llvm/ValueSymbolTable.h"
25#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/StringExtras.h"
27#include "llvm/Support/raw_ostream.h"
28using namespace llvm;
29

--- 75 unchanged lines hidden (view full) ---

105 case lltok::kw_define: if (ParseDefine()) return true; break;
106 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
107 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
108 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
109 case lltok::kw_type: if (ParseUnnamedType()) return true; break;
110 case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0
111 case lltok::LocalVar: if (ParseNamedType()) return true; break;
112 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
113 case lltok::Metadata: if (ParseStandaloneMetadata()) return true; break;
114
115 // The Global variable production with no name can have many different
116 // optional leading prefixes, the production is:
117 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
118 // OptionalAddrSpace ('constant'|'global') ...
119 case lltok::kw_private: // OptionalLinkage
120 case lltok::kw_internal: // OptionalLinkage
121 case lltok::kw_weak: // OptionalLinkage

--- 4 unchanged lines hidden (view full) ---

126 case lltok::kw_dllexport: // OptionalLinkage
127 case lltok::kw_common: // OptionalLinkage
128 case lltok::kw_dllimport: // OptionalLinkage
129 case lltok::kw_extern_weak: // OptionalLinkage
130 case lltok::kw_external: { // OptionalLinkage
131 unsigned Linkage, Visibility;
132 if (ParseOptionalLinkage(Linkage) ||
133 ParseOptionalVisibility(Visibility) ||
132 ParseGlobal("", 0, Linkage, true, Visibility))
134 ParseGlobal("", SMLoc(), Linkage, true, Visibility))
135 return true;
136 break;
137 }
138 case lltok::kw_default: // OptionalVisibility
139 case lltok::kw_hidden: // OptionalVisibility
140 case lltok::kw_protected: { // OptionalVisibility
141 unsigned Visibility;
142 if (ParseOptionalVisibility(Visibility) ||
141 ParseGlobal("", 0, 0, false, Visibility))
143 ParseGlobal("", SMLoc(), 0, false, Visibility))
144 return true;
145 break;
146 }
147
148 case lltok::kw_thread_local: // OptionalThreadLocal
149 case lltok::kw_addrspace: // OptionalAddrSpace
150 case lltok::kw_constant: // GlobalType
151 case lltok::kw_global: // GlobalType
150 if (ParseGlobal("", 0, 0, false, 0)) return true;
152 if (ParseGlobal("", SMLoc(), 0, false, 0)) return true;
153 break;
154 }
155 }
156}
157
158
159/// toplevelentity
160/// ::= 'module' 'asm' STRINGCONSTANT

--- 191 unchanged lines hidden (view full) ---

352 ParseOptionalVisibility(Visibility))
353 return true;
354
355 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
356 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
357 return ParseAlias(Name, NameLoc, Visibility);
358}
359
360/// ParseStandaloneMetadata:
361/// !42 = !{...}
362bool LLParser::ParseStandaloneMetadata() {
363 assert(Lex.getKind() == lltok::Metadata);
364 Lex.Lex();
365 unsigned MetadataID = 0;
366 if (ParseUInt32(MetadataID))
367 return true;
368 if (MetadataCache.find(MetadataID) != MetadataCache.end())
369 return TokError("Metadata id is already used");
370 if (ParseToken(lltok::equal, "expected '=' here"))
371 return true;
372
373 LocTy TyLoc;
374 bool IsConstant;
375 PATypeHolder Ty(Type::VoidTy);
376 if (ParseGlobalType(IsConstant) ||
377 ParseType(Ty, TyLoc))
378 return true;
379
380 Constant *Init = 0;
381 if (ParseGlobalValue(Ty, Init))
382 return true;
383
384 MetadataCache[MetadataID] = Init;
385 return false;
386}
387
388/// ParseAlias:
389/// ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee
390/// Aliasee
391/// ::= TypeAndValue
392/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')'
393/// ::= 'getelementptr' '(' ... ')'
394///
395/// Everything through visibility has already been parsed.

--- 597 unchanged lines hidden (view full) ---

993 return TokError("expected type");
994 case lltok::Type:
995 // TypeRec ::= 'float' | 'void' (etc)
996 Result = Lex.getTyVal();
997 Lex.Lex();
998 break;
999 case lltok::kw_opaque:
1000 // TypeRec ::= 'opaque'
971 Result = OpaqueType::get();
1001 Result = Context.getOpaqueType();
1002 Lex.Lex();
1003 break;
1004 case lltok::lbrace:
1005 // TypeRec ::= '{' ... '}'
1006 if (ParseStructType(Result, false))
1007 return true;
1008 break;
1009 case lltok::lsquare:

--- 13 unchanged lines hidden (view full) ---

1023 return true;
1024 break;
1025 case lltok::LocalVar:
1026 case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0
1027 // TypeRec ::= %foo
1028 if (const Type *T = M->getTypeByName(Lex.getStrVal())) {
1029 Result = T;
1030 } else {
1001 Result = OpaqueType::get();
1031 Result = Context.getOpaqueType();
1032 ForwardRefTypes.insert(std::make_pair(Lex.getStrVal(),
1033 std::make_pair(Result,
1034 Lex.getLoc())));
1035 M->addTypeName(Lex.getStrVal(), Result.get());
1036 }
1037 Lex.Lex();
1038 break;
1039
1040 case lltok::LocalVarID:
1041 // TypeRec ::= %4
1042 if (Lex.getUIntVal() < NumberedTypes.size())
1043 Result = NumberedTypes[Lex.getUIntVal()];
1044 else {
1045 std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator
1046 I = ForwardRefTypeIDs.find(Lex.getUIntVal());
1047 if (I != ForwardRefTypeIDs.end())
1048 Result = I->second.first;
1049 else {
1020 Result = OpaqueType::get();
1050 Result = Context.getOpaqueType();
1051 ForwardRefTypeIDs.insert(std::make_pair(Lex.getUIntVal(),
1052 std::make_pair(Result,
1053 Lex.getLoc())));
1054 }
1055 }
1056 Lex.Lex();
1057 break;
1058 case lltok::backslash: {
1059 // TypeRec ::= '\' 4
1060 Lex.Lex();
1061 unsigned Val;
1062 if (ParseUInt32(Val)) return true;
1033 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder.
1063 OpaqueType *OT = Context.getOpaqueType(); //Use temporary placeholder.
1064 UpRefs.push_back(UpRefRecord(Lex.getLoc(), Val, OT));
1065 Result = OT;
1066 break;
1067 }
1068 }
1069
1070 // Parse the type suffixes.
1071 while (1) {

--- 4 unchanged lines hidden (view full) ---

1076 // TypeRec ::= TypeRec '*'
1077 case lltok::star:
1078 if (Result.get() == Type::LabelTy)
1079 return TokError("basic block pointers are invalid");
1080 if (Result.get() == Type::VoidTy)
1081 return TokError("pointers to void are invalid; use i8* instead");
1082 if (!PointerType::isValidElementType(Result.get()))
1083 return TokError("pointer to this type is invalid");
1054 Result = HandleUpRefs(PointerType::getUnqual(Result.get()));
1084 Result = HandleUpRefs(Context.getPointerTypeUnqual(Result.get()));
1085 Lex.Lex();
1086 break;
1087
1088 // TypeRec ::= TypeRec 'addrspace' '(' uint32 ')' '*'
1089 case lltok::kw_addrspace: {
1090 if (Result.get() == Type::LabelTy)
1091 return TokError("basic block pointers are invalid");
1092 if (Result.get() == Type::VoidTy)
1093 return TokError("pointers to void are invalid; use i8* instead");
1094 if (!PointerType::isValidElementType(Result.get()))
1095 return TokError("pointer to this type is invalid");
1096 unsigned AddrSpace;
1097 if (ParseOptionalAddrSpace(AddrSpace) ||
1098 ParseToken(lltok::star, "expected '*' in address space"))
1099 return true;
1100
1071 Result = HandleUpRefs(PointerType::get(Result.get(), AddrSpace));
1101 Result = HandleUpRefs(Context.getPointerType(Result.get(), AddrSpace));
1102 break;
1103 }
1104
1105 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1106 case lltok::lparen:
1107 if (ParseFunctionType(Result))
1108 return true;
1109 break;

--- 144 unchanged lines hidden (view full) ---

1254 // FIXME: REJECT ATTRIBUTES ON FUNCTION TYPES in LLVM 3.0
1255 }
1256 }
1257
1258 std::vector<const Type*> ArgListTy;
1259 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
1260 ArgListTy.push_back(ArgList[i].Type);
1261
1232 Result = HandleUpRefs(FunctionType::get(Result.get(), ArgListTy, isVarArg));
1262 Result = HandleUpRefs(Context.getFunctionType(Result.get(),
1263 ArgListTy, isVarArg));
1264 return false;
1265}
1266
1267/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
1268/// TypeRec
1269/// ::= '{' '}'
1270/// ::= '{' TypeRec (',' TypeRec)* '}'
1271/// ::= '<' '{' '}' '>'
1272/// ::= '<' '{' TypeRec (',' TypeRec)* '}' '>'
1273bool LLParser::ParseStructType(PATypeHolder &Result, bool Packed) {
1274 assert(Lex.getKind() == lltok::lbrace);
1275 Lex.Lex(); // Consume the '{'
1276
1277 if (EatIfPresent(lltok::rbrace)) {
1247 Result = StructType::get(std::vector<const Type*>(), Packed);
1278 Result = Context.getStructType(Packed);
1279 return false;
1280 }
1281
1282 std::vector<PATypeHolder> ParamsList;
1283 LocTy EltTyLoc = Lex.getLoc();
1284 if (ParseTypeRec(Result)) return true;
1285 ParamsList.push_back(Result);
1286

--- 15 unchanged lines hidden (view full) ---

1302 }
1303
1304 if (ParseToken(lltok::rbrace, "expected '}' at end of struct"))
1305 return true;
1306
1307 std::vector<const Type*> ParamsListTy;
1308 for (unsigned i = 0, e = ParamsList.size(); i != e; ++i)
1309 ParamsListTy.push_back(ParamsList[i].get());
1279 Result = HandleUpRefs(StructType::get(ParamsListTy, Packed));
1310 Result = HandleUpRefs(Context.getStructType(ParamsListTy, Packed));
1311 return false;
1312}
1313
1314/// ParseArrayVectorType - Parse an array or vector type, assuming the first
1315/// token has already been consumed.
1316/// TypeRec
1317/// ::= '[' APSINTVAL 'x' Types ']'
1318/// ::= '<' APSINTVAL 'x' Types '>'

--- 22 unchanged lines hidden (view full) ---

1341
1342 if (isVector) {
1343 if (Size == 0)
1344 return Error(SizeLoc, "zero element vector is illegal");
1345 if ((unsigned)Size != Size)
1346 return Error(SizeLoc, "size too large for vector");
1347 if (!VectorType::isValidElementType(EltTy))
1348 return Error(TypeLoc, "vector element type must be fp or integer");
1318 Result = VectorType::get(EltTy, unsigned(Size));
1349 Result = Context.getVectorType(EltTy, unsigned(Size));
1350 } else {
1351 if (!ArrayType::isValidElementType(EltTy))
1352 return Error(TypeLoc, "invalid array element type");
1322 Result = HandleUpRefs(ArrayType::get(EltTy, Size));
1353 Result = HandleUpRefs(Context.getArrayType(EltTy, Size));
1354 }
1355 return false;
1356}
1357
1358//===----------------------------------------------------------------------===//
1359// Function Semantic Analysis.
1360//===----------------------------------------------------------------------===//
1361

--- 7 unchanged lines hidden (view full) ---

1369 NumberedVals.push_back(AI);
1370}
1371
1372LLParser::PerFunctionState::~PerFunctionState() {
1373 // If there were any forward referenced non-basicblock values, delete them.
1374 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
1375 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
1376 if (!isa<BasicBlock>(I->second.first)) {
1346 I->second.first->replaceAllUsesWith(UndefValue::get(I->second.first
1347 ->getType()));
1377 I->second.first->replaceAllUsesWith(
1378 P.getContext().getUndef(I->second.first->getType()));
1379 delete I->second.first;
1380 I->second.first = 0;
1381 }
1382
1383 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1384 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
1385 if (!isa<BasicBlock>(I->second.first)) {
1355 I->second.first->replaceAllUsesWith(UndefValue::get(I->second.first
1356 ->getType()));
1386 I->second.first->replaceAllUsesWith(
1387 P.getContext().getUndef(I->second.first->getType()));
1388 delete I->second.first;
1389 I->second.first = 0;
1390 }
1391}
1392
1393bool LLParser::PerFunctionState::VerifyFunctionComplete() {
1394 if (!ForwardRefVals.empty())
1395 return P.Error(ForwardRefVals.begin()->second.second,

--- 222 unchanged lines hidden (view full) ---

1618 ID.Kind = ValID::t_Constant;
1619 Lex.Lex();
1620 if (Lex.getKind() == lltok::lbrace) {
1621 SmallVector<Value*, 16> Elts;
1622 if (ParseMDNodeVector(Elts) ||
1623 ParseToken(lltok::rbrace, "expected end of metadata node"))
1624 return true;
1625
1595 ID.ConstantVal = MDNode::get(Elts.data(), Elts.size());
1626 ID.ConstantVal = Context.getMDNode(Elts.data(), Elts.size());
1627 return false;
1628 }
1629
1630 // Standalone metadata reference
1631 // !{ ..., !42, ... }
1632 unsigned MID = 0;
1633 if (!ParseUInt32(MID)) {
1634 std::map<unsigned, Constant *>::iterator I = MetadataCache.find(MID);
1635 if (I == MetadataCache.end())
1636 return TokError("Unknown metadata reference");
1637 ID.ConstantVal = I->second;
1638 return false;
1639 }
1640
1641 // MDString:
1642 // ::= '!' STRINGCONSTANT
1643 std::string Str;
1644 if (ParseStringConstant(Str)) return true;
1645
1604 ID.ConstantVal = MDString::get(Str.data(), Str.data() + Str.size());
1646 ID.ConstantVal = Context.getMDString(Str.data(), Str.data() + Str.size());
1647 return false;
1648 }
1649 case lltok::APSInt:
1650 ID.APSIntVal = Lex.getAPSIntVal();
1651 ID.Kind = ValID::t_APSInt;
1652 break;
1653 case lltok::APFloat:
1654 ID.APFloatVal = Lex.getAPFloatVal();
1655 ID.Kind = ValID::t_APFloat;
1656 break;
1657 case lltok::kw_true:
1616 ID.ConstantVal = ConstantInt::getTrue();
1658 ID.ConstantVal = Context.getConstantIntTrue();
1659 ID.Kind = ValID::t_Constant;
1660 break;
1661 case lltok::kw_false:
1620 ID.ConstantVal = ConstantInt::getFalse();
1662 ID.ConstantVal = Context.getConstantIntFalse();
1663 ID.Kind = ValID::t_Constant;
1664 break;
1665 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
1666 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
1667 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
1668
1669 case lltok::lbrace: {
1670 // ValID ::= '{' ConstVector '}'
1671 Lex.Lex();
1672 SmallVector<Constant*, 16> Elts;
1673 if (ParseGlobalValueVector(Elts) ||
1674 ParseToken(lltok::rbrace, "expected end of struct constant"))
1675 return true;
1676
1635 ID.ConstantVal = ConstantStruct::get(Elts.data(), Elts.size(), false);
1677 ID.ConstantVal = Context.getConstantStruct(Elts.data(), Elts.size(), false);
1678 ID.Kind = ValID::t_Constant;
1679 return false;
1680 }
1681 case lltok::less: {
1682 // ValID ::= '<' ConstVector '>' --> Vector.
1683 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
1684 Lex.Lex();
1685 bool isPackedStruct = EatIfPresent(lltok::lbrace);
1686
1687 SmallVector<Constant*, 16> Elts;
1688 LocTy FirstEltLoc = Lex.getLoc();
1689 if (ParseGlobalValueVector(Elts) ||
1690 (isPackedStruct &&
1691 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
1692 ParseToken(lltok::greater, "expected end of constant"))
1693 return true;
1694
1695 if (isPackedStruct) {
1654 ID.ConstantVal = ConstantStruct::get(Elts.data(), Elts.size(), true);
1696 ID.ConstantVal =
1697 Context.getConstantStruct(Elts.data(), Elts.size(), true);
1698 ID.Kind = ValID::t_Constant;
1699 return false;
1700 }
1701
1702 if (Elts.empty())
1703 return Error(ID.Loc, "constant vector must not be empty");
1704
1705 if (!Elts[0]->getType()->isInteger() &&
1706 !Elts[0]->getType()->isFloatingPoint())
1707 return Error(FirstEltLoc,
1708 "vector elements must have integer or floating point type");
1709
1710 // Verify that all the vector elements have the same type.
1711 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
1712 if (Elts[i]->getType() != Elts[0]->getType())
1713 return Error(FirstEltLoc,
1714 "vector element #" + utostr(i) +
1715 " is not of type '" + Elts[0]->getType()->getDescription());
1716
1674 ID.ConstantVal = ConstantVector::get(Elts.data(), Elts.size());
1717 ID.ConstantVal = Context.getConstantVector(Elts.data(), Elts.size());
1718 ID.Kind = ValID::t_Constant;
1719 return false;
1720 }
1721 case lltok::lsquare: { // Array Constant
1722 Lex.Lex();
1723 SmallVector<Constant*, 16> Elts;
1724 LocTy FirstEltLoc = Lex.getLoc();
1725 if (ParseGlobalValueVector(Elts) ||

--- 7 unchanged lines hidden (view full) ---

1733 ID.Kind = ValID::t_EmptyArray;
1734 return false;
1735 }
1736
1737 if (!Elts[0]->getType()->isFirstClassType())
1738 return Error(FirstEltLoc, "invalid array element type: " +
1739 Elts[0]->getType()->getDescription());
1740
1698 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
1741 ArrayType *ATy = Context.getArrayType(Elts[0]->getType(), Elts.size());
1742
1743 // Verify all elements are correct type!
1744 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
1745 if (Elts[i]->getType() != Elts[0]->getType())
1746 return Error(FirstEltLoc,
1747 "array element #" + utostr(i) +
1748 " is not of type '" +Elts[0]->getType()->getDescription());
1749 }
1750
1708 ID.ConstantVal = ConstantArray::get(ATy, Elts.data(), Elts.size());
1751 ID.ConstantVal = Context.getConstantArray(ATy, Elts.data(), Elts.size());
1752 ID.Kind = ValID::t_Constant;
1753 return false;
1754 }
1755 case lltok::kw_c: // c "foo"
1756 Lex.Lex();
1714 ID.ConstantVal = ConstantArray::get(Lex.getStrVal(), false);
1757 ID.ConstantVal = Context.getConstantArray(Lex.getStrVal(), false);
1758 if (ParseToken(lltok::StringConstant, "expected string")) return true;
1759 ID.Kind = ValID::t_Constant;
1760 return false;
1761
1762 case lltok::kw_asm: {
1763 // ValID ::= 'asm' SideEffect? STRINGCONSTANT ',' STRINGCONSTANT
1764 bool HasSideEffect;
1765 Lex.Lex();

--- 29 unchanged lines hidden (view full) ---

1795 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
1796 ParseType(DestTy) ||
1797 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
1798 return true;
1799 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
1800 return Error(ID.Loc, "invalid cast opcode for cast from '" +
1801 SrcVal->getType()->getDescription() + "' to '" +
1802 DestTy->getDescription() + "'");
1760 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc, SrcVal,
1761 DestTy);
1803 ID.ConstantVal = Context.getConstantExprCast((Instruction::CastOps)Opc,
1804 SrcVal, DestTy);
1805 ID.Kind = ValID::t_Constant;
1806 return false;
1807 }
1808 case lltok::kw_extractvalue: {
1809 Lex.Lex();
1810 Constant *Val;
1811 SmallVector<unsigned, 4> Indices;
1812 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
1813 ParseGlobalTypeAndValue(Val) ||
1814 ParseIndexList(Indices) ||
1815 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
1816 return true;
1817 if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType()))
1818 return Error(ID.Loc, "extractvalue operand must be array or struct");
1819 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
1820 Indices.end()))
1821 return Error(ID.Loc, "invalid indices for extractvalue");
1822 ID.ConstantVal =
1780 ConstantExpr::getExtractValue(Val, Indices.data(), Indices.size());
1823 Context.getConstantExprExtractValue(Val, Indices.data(), Indices.size());
1824 ID.Kind = ValID::t_Constant;
1825 return false;
1826 }
1827 case lltok::kw_insertvalue: {
1828 Lex.Lex();
1829 Constant *Val0, *Val1;
1830 SmallVector<unsigned, 4> Indices;
1831 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
1832 ParseGlobalTypeAndValue(Val0) ||
1833 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
1834 ParseGlobalTypeAndValue(Val1) ||
1835 ParseIndexList(Indices) ||
1836 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
1837 return true;
1838 if (!isa<StructType>(Val0->getType()) && !isa<ArrayType>(Val0->getType()))
1839 return Error(ID.Loc, "extractvalue operand must be array or struct");
1840 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
1841 Indices.end()))
1842 return Error(ID.Loc, "invalid indices for insertvalue");
1800 ID.ConstantVal =
1801 ConstantExpr::getInsertValue(Val0, Val1, Indices.data(), Indices.size());
1843 ID.ConstantVal = Context.getConstantExprInsertValue(Val0, Val1,
1844 Indices.data(), Indices.size());
1845 ID.Kind = ValID::t_Constant;
1846 return false;
1847 }
1848 case lltok::kw_icmp:
1849 case lltok::kw_fcmp:
1850 case lltok::kw_vicmp:
1851 case lltok::kw_vfcmp: {
1852 unsigned PredVal, Opc = Lex.getUIntVal();

--- 10 unchanged lines hidden (view full) ---

1863 if (Val0->getType() != Val1->getType())
1864 return Error(ID.Loc, "compare operands must have the same type");
1865
1866 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
1867
1868 if (Opc == Instruction::FCmp) {
1869 if (!Val0->getType()->isFPOrFPVector())
1870 return Error(ID.Loc, "fcmp requires floating point operands");
1828 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
1871 ID.ConstantVal = Context.getConstantExprFCmp(Pred, Val0, Val1);
1872 } else if (Opc == Instruction::ICmp) {
1873 if (!Val0->getType()->isIntOrIntVector() &&
1874 !isa<PointerType>(Val0->getType()))
1875 return Error(ID.Loc, "icmp requires pointer or integer operands");
1833 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
1876 ID.ConstantVal = Context.getConstantExprICmp(Pred, Val0, Val1);
1877 } else if (Opc == Instruction::VFCmp) {
1878 // FIXME: REMOVE VFCMP Support
1879 if (!Val0->getType()->isFPOrFPVector() ||
1880 !isa<VectorType>(Val0->getType()))
1881 return Error(ID.Loc, "vfcmp requires vector floating point operands");
1839 ID.ConstantVal = ConstantExpr::getVFCmp(Pred, Val0, Val1);
1882 ID.ConstantVal = Context.getConstantExprVFCmp(Pred, Val0, Val1);
1883 } else if (Opc == Instruction::VICmp) {
1884 // FIXME: REMOVE VICMP Support
1885 if (!Val0->getType()->isIntOrIntVector() ||
1886 !isa<VectorType>(Val0->getType()))
1887 return Error(ID.Loc, "vicmp requires vector floating point operands");
1845 ID.ConstantVal = ConstantExpr::getVICmp(Pred, Val0, Val1);
1888 ID.ConstantVal = Context.getConstantExprVICmp(Pred, Val0, Val1);
1889 }
1890 ID.Kind = ValID::t_Constant;
1891 return false;
1892 }
1893
1894 // Binary Operators.
1895 case lltok::kw_add:
1896 case lltok::kw_fadd:

--- 16 unchanged lines hidden (view full) ---

1913 ParseGlobalTypeAndValue(Val1) ||
1914 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
1915 return true;
1916 if (Val0->getType() != Val1->getType())
1917 return Error(ID.Loc, "operands of constexpr must have same type");
1918 if (!Val0->getType()->isIntOrIntVector() &&
1919 !Val0->getType()->isFPOrFPVector())
1920 return Error(ID.Loc,"constexpr requires integer, fp, or vector operands");
1878 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
1921 ID.ConstantVal = Context.getConstantExpr(Opc, Val0, Val1);
1922 ID.Kind = ValID::t_Constant;
1923 return false;
1924 }
1925
1926 // Logical Operations
1927 case lltok::kw_shl:
1928 case lltok::kw_lshr:
1929 case lltok::kw_ashr:

--- 9 unchanged lines hidden (view full) ---

1939 ParseGlobalTypeAndValue(Val1) ||
1940 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
1941 return true;
1942 if (Val0->getType() != Val1->getType())
1943 return Error(ID.Loc, "operands of constexpr must have same type");
1944 if (!Val0->getType()->isIntOrIntVector())
1945 return Error(ID.Loc,
1946 "constexpr requires integer or integer vector operands");
1904 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
1947 ID.ConstantVal = Context.getConstantExpr(Opc, Val0, Val1);
1948 ID.Kind = ValID::t_Constant;
1949 return false;
1950 }
1951
1952 case lltok::kw_getelementptr:
1953 case lltok::kw_shufflevector:
1954 case lltok::kw_insertelement:
1955 case lltok::kw_extractelement:

--- 8 unchanged lines hidden (view full) ---

1964
1965 if (Opc == Instruction::GetElementPtr) {
1966 if (Elts.size() == 0 || !isa<PointerType>(Elts[0]->getType()))
1967 return Error(ID.Loc, "getelementptr requires pointer operand");
1968
1969 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(),
1970 (Value**)&Elts[1], Elts.size()-1))
1971 return Error(ID.Loc, "invalid indices for getelementptr");
1929 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0],
1972 ID.ConstantVal = Context.getConstantExprGetElementPtr(Elts[0],
1973 &Elts[1], Elts.size()-1);
1974 } else if (Opc == Instruction::Select) {
1975 if (Elts.size() != 3)
1976 return Error(ID.Loc, "expected three operands to select");
1977 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
1978 Elts[2]))
1979 return Error(ID.Loc, Reason);
1937 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
1980 ID.ConstantVal = Context.getConstantExprSelect(Elts[0], Elts[1], Elts[2]);
1981 } else if (Opc == Instruction::ShuffleVector) {
1982 if (Elts.size() != 3)
1983 return Error(ID.Loc, "expected three operands to shufflevector");
1984 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
1985 return Error(ID.Loc, "invalid operands to shufflevector");
1943 ID.ConstantVal = ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
1986 ID.ConstantVal =
1987 Context.getConstantExprShuffleVector(Elts[0], Elts[1],Elts[2]);
1988 } else if (Opc == Instruction::ExtractElement) {
1989 if (Elts.size() != 2)
1990 return Error(ID.Loc, "expected two operands to extractelement");
1991 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
1992 return Error(ID.Loc, "invalid extractelement operands");
1949 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
1993 ID.ConstantVal = Context.getConstantExprExtractElement(Elts[0], Elts[1]);
1994 } else {
1995 assert(Opc == Instruction::InsertElement && "Unknown opcode");
1996 if (Elts.size() != 3)
1997 return Error(ID.Loc, "expected three operands to insertelement");
1998 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
1999 return Error(ID.Loc, "invalid insertelement operands");
1956 ID.ConstantVal = ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
2000 ID.ConstantVal =
2001 Context.getConstantExprInsertElement(Elts[0], Elts[1],Elts[2]);
2002 }
2003
2004 ID.Kind = ValID::t_Constant;
2005 return false;
2006 }
2007 }
2008
2009 Lex.Lex();

--- 27 unchanged lines hidden (view full) ---

2037 return V == 0;
2038 case ValID::t_GlobalID:
2039 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
2040 return V == 0;
2041 case ValID::t_APSInt:
2042 if (!isa<IntegerType>(Ty))
2043 return Error(ID.Loc, "integer constant must have integer type");
2044 ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
2000 V = ConstantInt::get(ID.APSIntVal);
2045 V = Context.getConstantInt(ID.APSIntVal);
2046 return false;
2047 case ValID::t_APFloat:
2048 if (!Ty->isFloatingPoint() ||
2049 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2050 return Error(ID.Loc, "floating point constant invalid for type");
2051
2052 // The lexer has no type info, so builds all float and double FP constants
2053 // as double. Fix this here. Long double does not need this.
2054 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble &&
2055 Ty == Type::FloatTy) {
2056 bool Ignored;
2057 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2058 &Ignored);
2059 }
2015 V = ConstantFP::get(ID.APFloatVal);
2060 V = Context.getConstantFP(ID.APFloatVal);
2061
2062 if (V->getType() != Ty)
2063 return Error(ID.Loc, "floating point constant does not have type '" +
2064 Ty->getDescription() + "'");
2065
2066 return false;
2067 case ValID::t_Null:
2068 if (!isa<PointerType>(Ty))
2069 return Error(ID.Loc, "null must be a pointer type");
2025 V = ConstantPointerNull::get(cast<PointerType>(Ty));
2070 V = Context.getConstantPointerNull(cast<PointerType>(Ty));
2071 return false;
2072 case ValID::t_Undef:
2073 // FIXME: LabelTy should not be a first-class type.
2074 if ((!Ty->isFirstClassType() || Ty == Type::LabelTy) &&
2075 !isa<OpaqueType>(Ty))
2076 return Error(ID.Loc, "invalid type for undef constant");
2032 V = UndefValue::get(Ty);
2077 V = Context.getUndef(Ty);
2078 return false;
2079 case ValID::t_EmptyArray:
2080 if (!isa<ArrayType>(Ty) || cast<ArrayType>(Ty)->getNumElements() != 0)
2081 return Error(ID.Loc, "invalid empty array initializer");
2037 V = UndefValue::get(Ty);
2082 V = Context.getUndef(Ty);
2083 return false;
2084 case ValID::t_Zero:
2085 // FIXME: LabelTy should not be a first-class type.
2086 if (!Ty->isFirstClassType() || Ty == Type::LabelTy)
2087 return Error(ID.Loc, "invalid type for null constant");
2043 V = Constant::getNullValue(Ty);
2088 V = Context.getNullValue(Ty);
2089 return false;
2090 case ValID::t_Constant:
2091 if (ID.ConstantVal->getType() != Ty)
2092 return Error(ID.Loc, "constant expression type mismatch");
2093 V = ID.ConstantVal;
2094 return false;
2095 }
2096}

--- 185 unchanged lines hidden (view full) ---

2282 Attrs.push_back(AttributeWithIndex::get(~0, FuncAttrs));
2283
2284 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
2285
2286 if (PAL.paramHasAttr(1, Attribute::StructRet) &&
2287 RetType != Type::VoidTy)
2288 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
2289
2245 const FunctionType *FT = FunctionType::get(RetType, ParamTypeList, isVarArg);
2246 const PointerType *PFT = PointerType::getUnqual(FT);
2290 const FunctionType *FT =
2291 Context.getFunctionType(RetType, ParamTypeList, isVarArg);
2292 const PointerType *PFT = Context.getPointerTypeUnqual(FT);
2293
2294 Fn = 0;
2295 if (!FunctionName.empty()) {
2296 // If this was a definition of a forward reference, remove the definition
2297 // from the forward reference table and fill in the forward ref.
2298 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
2299 ForwardRefVals.find(FunctionName);
2300 if (FRVI != ForwardRefVals.end()) {

--- 298 unchanged lines hidden (view full) ---

2599 SmallVector<Value*, 8> RVs;
2600 RVs.push_back(RV);
2601
2602 while (EatIfPresent(lltok::comma)) {
2603 if (ParseTypeAndValue(RV, PFS)) return true;
2604 RVs.push_back(RV);
2605 }
2606
2561 RV = UndefValue::get(PFS.getFunction().getReturnType());
2607 RV = Context.getUndef(PFS.getFunction().getReturnType());
2608 for (unsigned i = 0, e = RVs.size(); i != e; ++i) {
2609 Instruction *I = InsertValueInst::Create(RV, RVs[i], i, "mrv");
2610 BB->getInstList().push_back(I);
2611 RV = I;
2612 }
2613 }
2614 Inst = ReturnInst::Create(RV);
2615 return false;

--- 121 unchanged lines hidden (view full) ---

2737 // Pull out the types of all of the arguments...
2738 std::vector<const Type*> ParamTypes;
2739 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
2740 ParamTypes.push_back(ArgList[i].V->getType());
2741
2742 if (!FunctionType::isValidReturnType(RetType))
2743 return Error(RetTypeLoc, "Invalid result type for LLVM function");
2744
2699 Ty = FunctionType::get(RetType, ParamTypes, false);
2700 PFTy = PointerType::getUnqual(Ty);
2745 Ty = Context.getFunctionType(RetType, ParamTypes, false);
2746 PFTy = Context.getPointerTypeUnqual(Ty);
2747 }
2748
2749 // Look up the callee.
2750 Value *Callee;
2751 if (ConvertValIDToValue(PFTy, CalleeID, Callee, PFS)) return true;
2752
2753 // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional
2754 // function attributes.

--- 331 unchanged lines hidden (view full) ---

3086 // Pull out the types of all of the arguments...
3087 std::vector<const Type*> ParamTypes;
3088 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3089 ParamTypes.push_back(ArgList[i].V->getType());
3090
3091 if (!FunctionType::isValidReturnType(RetType))
3092 return Error(RetTypeLoc, "Invalid result type for LLVM function");
3093
3048 Ty = FunctionType::get(RetType, ParamTypes, false);
3049 PFTy = PointerType::getUnqual(Ty);
3094 Ty = Context.getFunctionType(RetType, ParamTypes, false);
3095 PFTy = Context.getPointerTypeUnqual(Ty);
3096 }
3097
3098 // Look up the callee.
3099 Value *Callee;
3100 if (ConvertValIDToValue(PFTy, CalleeID, Callee, PFS)) return true;
3101
3102 // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional
3103 // function attributes.

--- 53 unchanged lines hidden (view full) ---

3157
3158/// ParseAlloc
3159/// ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalAlignment)?
3160/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalAlignment)?
3161bool LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS,
3162 unsigned Opc) {
3163 PATypeHolder Ty(Type::VoidTy);
3164 Value *Size = 0;
3119 LocTy SizeLoc = 0;
3165 LocTy SizeLoc;
3166 unsigned Alignment = 0;
3167 if (ParseType(Ty)) return true;
3168
3169 if (EatIfPresent(lltok::comma)) {
3170 if (Lex.getKind() == lltok::kw_align) {
3171 if (ParseOptionalAlignment(Alignment)) return true;
3172 } else if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
3173 ParseOptionalCommaAlignment(Alignment)) {

--- 175 unchanged lines hidden ---