Deleted Added
full compact
LLParser.cpp (200581) LLParser.cpp (201360)
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"
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/Metadata.h"
23#include "llvm/Module.h"
24#include "llvm/Operator.h"
25#include "llvm/ValueSymbolTable.h"
26#include "llvm/ADT/SmallPtrSet.h"
27#include "llvm/ADT/StringExtras.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30using namespace llvm;

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

165 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
166 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
167 case lltok::kw_type: if (ParseUnnamedType()) return true; break;
168 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
169 case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0
170 case lltok::LocalVar: if (ParseNamedType()) return true; break;
171 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
172 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
21#include "llvm/Module.h"
22#include "llvm/Operator.h"
23#include "llvm/ValueSymbolTable.h"
24#include "llvm/ADT/SmallPtrSet.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
28using namespace llvm;

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

163 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
164 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
165 case lltok::kw_type: if (ParseUnnamedType()) return true; break;
166 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
167 case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0
168 case lltok::LocalVar: if (ParseNamedType()) return true; break;
169 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
170 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
173 case lltok::Metadata: if (ParseStandaloneMetadata()) return true; break;
174 case lltok::NamedOrCustomMD: if (ParseNamedMetadata()) return true; break;
171 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
172 case lltok::MetadataVar: if (ParseNamedMetadata()) return true; break;
175
176 // The Global variable production with no name can have many different
177 // optional leading prefixes, the production is:
178 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
179 // OptionalAddrSpace ('constant'|'global') ...
180 case lltok::kw_private : // OptionalLinkage
181 case lltok::kw_linker_private: // OptionalLinkage
182 case lltok::kw_internal: // OptionalLinkage

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

460
461 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
462 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
463 return ParseAlias(Name, NameLoc, Visibility);
464}
465
466// MDString:
467// ::= '!' STRINGCONSTANT
173
174 // The Global variable production with no name can have many different
175 // optional leading prefixes, the production is:
176 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
177 // OptionalAddrSpace ('constant'|'global') ...
178 case lltok::kw_private : // OptionalLinkage
179 case lltok::kw_linker_private: // OptionalLinkage
180 case lltok::kw_internal: // OptionalLinkage

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

458
459 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
460 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
461 return ParseAlias(Name, NameLoc, Visibility);
462}
463
464// MDString:
465// ::= '!' STRINGCONSTANT
468bool LLParser::ParseMDString(MetadataBase *&MDS) {
466bool LLParser::ParseMDString(MDString *&Result) {
469 std::string Str;
470 if (ParseStringConstant(Str)) return true;
467 std::string Str;
468 if (ParseStringConstant(Str)) return true;
471 MDS = MDString::get(Context, Str);
469 Result = MDString::get(Context, Str);
472 return false;
473}
474
475// MDNode:
476// ::= '!' MDNodeNumber
470 return false;
471}
472
473// MDNode:
474// ::= '!' MDNodeNumber
477bool LLParser::ParseMDNode(MetadataBase *&Node) {
475bool LLParser::ParseMDNodeID(MDNode *&Result) {
478 // !{ ..., !42, ... }
479 unsigned MID = 0;
476 // !{ ..., !42, ... }
477 unsigned MID = 0;
480 if (ParseUInt32(MID)) return true;
478 if (ParseUInt32(MID)) return true;
481
482 // Check existing MDNode.
479
480 // Check existing MDNode.
483 std::map<unsigned, WeakVH>::iterator I = MetadataCache.find(MID);
484 if (I != MetadataCache.end()) {
485 Node = cast<MetadataBase>(I->second);
481 if (MID < NumberedMetadata.size() && NumberedMetadata[MID] != 0) {
482 Result = NumberedMetadata[MID];
486 return false;
487 }
488
483 return false;
484 }
485
489 // Check known forward references.
490 std::map<unsigned, std::pair<WeakVH, LocTy> >::iterator
491 FI = ForwardRefMDNodes.find(MID);
492 if (FI != ForwardRefMDNodes.end()) {
493 Node = cast<MetadataBase>(FI->second.first);
494 return false;
495 }
486 // Create MDNode forward reference.
496
487
497 // Create MDNode forward reference
498 SmallVector<Value *, 1> Elts;
488 // FIXME: This is not unique enough!
499 std::string FwdRefName = "llvm.mdnode.fwdref." + utostr(MID);
489 std::string FwdRefName = "llvm.mdnode.fwdref." + utostr(MID);
500 Elts.push_back(MDString::get(Context, FwdRefName));
501 MDNode *FwdNode = MDNode::get(Context, Elts.data(), Elts.size());
490 Value *V = MDString::get(Context, FwdRefName);
491 MDNode *FwdNode = MDNode::get(Context, &V, 1);
502 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
492 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
503 Node = FwdNode;
493
494 if (NumberedMetadata.size() <= MID)
495 NumberedMetadata.resize(MID+1);
496 NumberedMetadata[MID] = FwdNode;
497 Result = FwdNode;
504 return false;
505}
506
498 return false;
499}
500
507///ParseNamedMetadata:
501/// ParseNamedMetadata:
508/// !foo = !{ !1, !2 }
509bool LLParser::ParseNamedMetadata() {
502/// !foo = !{ !1, !2 }
503bool LLParser::ParseNamedMetadata() {
510 assert(Lex.getKind() == lltok::NamedOrCustomMD);
511 Lex.Lex();
504 assert(Lex.getKind() == lltok::MetadataVar);
512 std::string Name = Lex.getStrVal();
505 std::string Name = Lex.getStrVal();
506 Lex.Lex();
513
507
514 if (ParseToken(lltok::equal, "expected '=' here"))
508 if (ParseToken(lltok::equal, "expected '=' here") ||
509 ParseToken(lltok::exclaim, "Expected '!' here") ||
510 ParseToken(lltok::lbrace, "Expected '{' here"))
515 return true;
516
511 return true;
512
517 if (Lex.getKind() != lltok::Metadata)
518 return TokError("Expected '!' here");
519 Lex.Lex();
520
521 if (Lex.getKind() != lltok::lbrace)
522 return TokError("Expected '{' here");
523 Lex.Lex();
524 SmallVector<MetadataBase *, 8> Elts;
525 do {
513 SmallVector<MetadataBase *, 8> Elts;
514 do {
526 if (Lex.getKind() != lltok::Metadata)
527 return TokError("Expected '!' here");
528 Lex.Lex();
529 MetadataBase *N = 0;
530 if (ParseMDNode(N)) return true;
515 if (ParseToken(lltok::exclaim, "Expected '!' here"))
516 return true;
517
518 // FIXME: This rejects MDStrings. Are they legal in an named MDNode or not?
519 MDNode *N = 0;
520 if (ParseMDNodeID(N)) return true;
531 Elts.push_back(N);
532 } while (EatIfPresent(lltok::comma));
533
534 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
535 return true;
536
537 NamedMDNode::Create(Context, Name, Elts.data(), Elts.size(), M);
538 return false;
539}
540
541/// ParseStandaloneMetadata:
542/// !42 = !{...}
543bool LLParser::ParseStandaloneMetadata() {
521 Elts.push_back(N);
522 } while (EatIfPresent(lltok::comma));
523
524 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
525 return true;
526
527 NamedMDNode::Create(Context, Name, Elts.data(), Elts.size(), M);
528 return false;
529}
530
531/// ParseStandaloneMetadata:
532/// !42 = !{...}
533bool LLParser::ParseStandaloneMetadata() {
544 assert(Lex.getKind() == lltok::Metadata);
534 assert(Lex.getKind() == lltok::exclaim);
545 Lex.Lex();
546 unsigned MetadataID = 0;
535 Lex.Lex();
536 unsigned MetadataID = 0;
547 if (ParseUInt32(MetadataID))
548 return true;
549 if (MetadataCache.find(MetadataID) != MetadataCache.end())
550 return TokError("Metadata id is already used");
551 if (ParseToken(lltok::equal, "expected '=' here"))
552 return true;
553
554 LocTy TyLoc;
555 PATypeHolder Ty(Type::getVoidTy(Context));
537
538 LocTy TyLoc;
539 PATypeHolder Ty(Type::getVoidTy(Context));
556 if (ParseType(Ty, TyLoc))
557 return true;
558
559 if (Lex.getKind() != lltok::Metadata)
560 return TokError("Expected metadata here");
561
562 Lex.Lex();
563 if (Lex.getKind() != lltok::lbrace)
564 return TokError("Expected '{' here");
565
566 SmallVector<Value *, 16> Elts;
540 SmallVector<Value *, 16> Elts;
567 if (ParseMDNodeVector(Elts)
568 || ParseToken(lltok::rbrace, "expected end of metadata node"))
541 if (ParseUInt32(MetadataID) ||
542 ParseToken(lltok::equal, "expected '=' here") ||
543 ParseType(Ty, TyLoc) ||
544 ParseToken(lltok::exclaim, "Expected '!' here") ||
545 ParseToken(lltok::lbrace, "Expected '{' here") ||
546 ParseMDNodeVector(Elts) ||
547 ParseToken(lltok::rbrace, "expected end of metadata node"))
569 return true;
570
571 MDNode *Init = MDNode::get(Context, Elts.data(), Elts.size());
548 return true;
549
550 MDNode *Init = MDNode::get(Context, Elts.data(), Elts.size());
572 MetadataCache[MetadataID] = Init;
573 std::map<unsigned, std::pair<WeakVH, LocTy> >::iterator
551
552 // See if this was forward referenced, if so, handle it.
553 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
574 FI = ForwardRefMDNodes.find(MetadataID);
575 if (FI != ForwardRefMDNodes.end()) {
554 FI = ForwardRefMDNodes.find(MetadataID);
555 if (FI != ForwardRefMDNodes.end()) {
576 MDNode *FwdNode = cast<MDNode>(FI->second.first);
577 FwdNode->replaceAllUsesWith(Init);
556 FI->second.first->replaceAllUsesWith(Init);
578 ForwardRefMDNodes.erase(FI);
557 ForwardRefMDNodes.erase(FI);
579 }
558
559 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
560 } else {
561 if (MetadataID >= NumberedMetadata.size())
562 NumberedMetadata.resize(MetadataID+1);
580
563
581 return false;
582}
583
584/// ParseInlineMetadata:
585/// !{type %instr}
586/// !{...} MDNode
587/// !"foo" MDString
588bool LLParser::ParseInlineMetadata(Value *&V, PerFunctionState &PFS) {
589 assert(Lex.getKind() == lltok::Metadata && "Only for Metadata");
590 V = 0;
591
592 Lex.Lex();
593 if (Lex.getKind() == lltok::lbrace) {
594 Lex.Lex();
595 if (ParseTypeAndValue(V, PFS) ||
596 ParseToken(lltok::rbrace, "expected end of metadata node"))
597 return true;
598
599 Value *Vals[] = { V };
600 V = MDNode::get(Context, Vals, 1);
601 return false;
564 if (NumberedMetadata[MetadataID] != 0)
565 return TokError("Metadata id is already used");
566 NumberedMetadata[MetadataID] = Init;
602 }
603
567 }
568
604 // Standalone metadata reference
605 // !{ ..., !42, ... }
606 if (!ParseMDNode((MetadataBase *&)V))
607 return false;
608
609 // MDString:
610 // '!' STRINGCONSTANT
611 if (ParseMDString((MetadataBase *&)V)) return true;
612 return false;
613}
614
615/// ParseAlias:
616/// ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee
617/// Aliasee
618/// ::= TypeAndValue
619/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')'

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

1100 }
1101 break;
1102 }
1103
1104 Lex.Lex();
1105 return false;
1106}
1107
569 return false;
570}
571
572/// ParseAlias:
573/// ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee
574/// Aliasee
575/// ::= TypeAndValue
576/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')'

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

1057 }
1058 break;
1059 }
1060
1061 Lex.Lex();
1062 return false;
1063}
1064
1108/// ParseOptionalCustomMetadata
1109/// ::= /* empty */
1110/// ::= !dbg !42
1111bool LLParser::ParseOptionalCustomMetadata() {
1112 if (Lex.getKind() != lltok::NamedOrCustomMD)
1113 return false;
1065/// ParseInstructionMetadata
1066/// ::= !dbg !42 (',' !dbg !57)*
1067bool LLParser::
1068ParseInstructionMetadata(SmallVectorImpl<std::pair<unsigned,
1069 MDNode *> > &Result){
1070 do {
1071 if (Lex.getKind() != lltok::MetadataVar)
1072 return TokError("expected metadata after comma");
1114
1073
1115 std::string Name = Lex.getStrVal();
1116 Lex.Lex();
1074 std::string Name = Lex.getStrVal();
1075 Lex.Lex();
1117
1076
1118 if (Lex.getKind() != lltok::Metadata)
1119 return TokError("Expected '!' here");
1120 Lex.Lex();
1077 MDNode *Node;
1078 if (ParseToken(lltok::exclaim, "expected '!' here") ||
1079 ParseMDNodeID(Node))
1080 return true;
1121
1081
1122 MetadataBase *Node;
1123 if (ParseMDNode(Node)) return true;
1082 unsigned MDK = M->getMDKindID(Name.c_str());
1083 Result.push_back(std::make_pair(MDK, Node));
1124
1084
1125 MetadataContext &TheMetadata = M->getContext().getMetadata();
1126 unsigned MDK = TheMetadata.getMDKind(Name.c_str());
1127 if (!MDK)
1128 MDK = TheMetadata.registerMDKind(Name.c_str());
1129 MDsOnInst.push_back(std::make_pair(MDK, cast<MDNode>(Node)));
1130
1085 // If this is the end of the list, we're done.
1086 } while (EatIfPresent(lltok::comma));
1131 return false;
1132}
1133
1134/// ParseOptionalAlignment
1135/// ::= /* empty */
1136/// ::= 'align' 4
1137bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1138 Alignment = 0;
1139 if (!EatIfPresent(lltok::kw_align))
1140 return false;
1141 LocTy AlignLoc = Lex.getLoc();
1142 if (ParseUInt32(Alignment)) return true;
1143 if (!isPowerOf2_32(Alignment))
1144 return Error(AlignLoc, "alignment is not a power of two");
1145 return false;
1146}
1147
1087 return false;
1088}
1089
1090/// ParseOptionalAlignment
1091/// ::= /* empty */
1092/// ::= 'align' 4
1093bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1094 Alignment = 0;
1095 if (!EatIfPresent(lltok::kw_align))
1096 return false;
1097 LocTy AlignLoc = Lex.getLoc();
1098 if (ParseUInt32(Alignment)) return true;
1099 if (!isPowerOf2_32(Alignment))
1100 return Error(AlignLoc, "alignment is not a power of two");
1101 return false;
1102}
1103
1148/// ParseOptionalInfo
1149/// ::= OptionalInfo (',' OptionalInfo)+
1150bool LLParser::ParseOptionalInfo(unsigned &Alignment) {
1151
1152 // FIXME: Handle customized metadata info attached with an instruction.
1153 do {
1154 if (Lex.getKind() == lltok::NamedOrCustomMD) {
1155 if (ParseOptionalCustomMetadata()) return true;
1156 } else if (Lex.getKind() == lltok::kw_align) {
1104/// ParseOptionalCommaAlign
1105/// ::=
1106/// ::= ',' align 4
1107///
1108/// This returns with AteExtraComma set to true if it ate an excess comma at the
1109/// end.
1110bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1111 bool &AteExtraComma) {
1112 AteExtraComma = false;
1113 while (EatIfPresent(lltok::comma)) {
1114 // Metadata at the end is an early exit.
1115 if (Lex.getKind() == lltok::MetadataVar) {
1116 AteExtraComma = true;
1117 return false;
1118 }
1119
1120 if (Lex.getKind() == lltok::kw_align) {
1157 if (ParseOptionalAlignment(Alignment)) return true;
1158 } else
1159 return true;
1121 if (ParseOptionalAlignment(Alignment)) return true;
1122 } else
1123 return true;
1160 } while (EatIfPresent(lltok::comma));
1124 }
1161
1162 return false;
1163}
1164
1165
1125
1126 return false;
1127}
1128
1129
1130/// ParseIndexList - This parses the index list for an insert/extractvalue
1131/// instruction. This sets AteExtraComma in the case where we eat an extra
1132/// comma at the end of the line and find that it is followed by metadata.
1133/// Clients that don't allow metadata can call the version of this function that
1134/// only takes one argument.
1135///
1166/// ParseIndexList
1167/// ::= (',' uint32)+
1136/// ParseIndexList
1137/// ::= (',' uint32)+
1168bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
1138///
1139bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1140 bool &AteExtraComma) {
1141 AteExtraComma = false;
1142
1169 if (Lex.getKind() != lltok::comma)
1170 return TokError("expected ',' as start of index list");
1171
1172 while (EatIfPresent(lltok::comma)) {
1143 if (Lex.getKind() != lltok::comma)
1144 return TokError("expected ',' as start of index list");
1145
1146 while (EatIfPresent(lltok::comma)) {
1173 if (Lex.getKind() == lltok::NamedOrCustomMD)
1174 break;
1147 if (Lex.getKind() == lltok::MetadataVar) {
1148 AteExtraComma = true;
1149 return false;
1150 }
1175 unsigned Idx;
1176 if (ParseUInt32(Idx)) return true;
1177 Indices.push_back(Idx);
1178 }
1179
1180 return false;
1181}
1182

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

1208///
1209PATypeHolder LLParser::HandleUpRefs(const Type *ty) {
1210 // If Ty isn't abstract, or if there are no up-references in it, then there is
1211 // nothing to resolve here.
1212 if (!ty->isAbstract() || UpRefs.empty()) return ty;
1213
1214 PATypeHolder Ty(ty);
1215#if 0
1151 unsigned Idx;
1152 if (ParseUInt32(Idx)) return true;
1153 Indices.push_back(Idx);
1154 }
1155
1156 return false;
1157}
1158

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

1184///
1185PATypeHolder LLParser::HandleUpRefs(const Type *ty) {
1186 // If Ty isn't abstract, or if there are no up-references in it, then there is
1187 // nothing to resolve here.
1188 if (!ty->isAbstract() || UpRefs.empty()) return ty;
1189
1190 PATypeHolder Ty(ty);
1191#if 0
1216 errs() << "Type '" << Ty->getDescription()
1192 dbgs() << "Type '" << Ty->getDescription()
1217 << "' newly formed. Resolving upreferences.\n"
1218 << UpRefs.size() << " upreferences active!\n";
1219#endif
1220
1221 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
1222 // to zero), we resolve them all together before we resolve them to Ty. At
1223 // the end of the loop, if there is anything to resolve to Ty, it will be in
1224 // this variable.
1225 OpaqueType *TypeToResolve = 0;
1226
1227 for (unsigned i = 0; i != UpRefs.size(); ++i) {
1228 // Determine if 'Ty' directly contains this up-references 'LastContainedTy'.
1229 bool ContainsType =
1230 std::find(Ty->subtype_begin(), Ty->subtype_end(),
1231 UpRefs[i].LastContainedTy) != Ty->subtype_end();
1232
1233#if 0
1193 << "' newly formed. Resolving upreferences.\n"
1194 << UpRefs.size() << " upreferences active!\n";
1195#endif
1196
1197 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
1198 // to zero), we resolve them all together before we resolve them to Ty. At
1199 // the end of the loop, if there is anything to resolve to Ty, it will be in
1200 // this variable.
1201 OpaqueType *TypeToResolve = 0;
1202
1203 for (unsigned i = 0; i != UpRefs.size(); ++i) {
1204 // Determine if 'Ty' directly contains this up-references 'LastContainedTy'.
1205 bool ContainsType =
1206 std::find(Ty->subtype_begin(), Ty->subtype_end(),
1207 UpRefs[i].LastContainedTy) != Ty->subtype_end();
1208
1209#if 0
1234 errs() << " UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
1210 dbgs() << " UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
1235 << UpRefs[i].LastContainedTy->getDescription() << ") = "
1236 << (ContainsType ? "true" : "false")
1237 << " level=" << UpRefs[i].NestingLevel << "\n";
1238#endif
1239 if (!ContainsType)
1240 continue;
1241
1242 // Decrement level of upreference
1243 unsigned Level = --UpRefs[i].NestingLevel;
1244 UpRefs[i].LastContainedTy = Ty;
1245
1246 // If the Up-reference has a non-zero level, it shouldn't be resolved yet.
1247 if (Level != 0)
1248 continue;
1249
1250#if 0
1211 << UpRefs[i].LastContainedTy->getDescription() << ") = "
1212 << (ContainsType ? "true" : "false")
1213 << " level=" << UpRefs[i].NestingLevel << "\n";
1214#endif
1215 if (!ContainsType)
1216 continue;
1217
1218 // Decrement level of upreference
1219 unsigned Level = --UpRefs[i].NestingLevel;
1220 UpRefs[i].LastContainedTy = Ty;
1221
1222 // If the Up-reference has a non-zero level, it shouldn't be resolved yet.
1223 if (Level != 0)
1224 continue;
1225
1226#if 0
1251 errs() << " * Resolving upreference for " << UpRefs[i].UpRefTy << "\n";
1227 dbgs() << " * Resolving upreference for " << UpRefs[i].UpRefTy << "\n";
1252#endif
1253 if (!TypeToResolve)
1254 TypeToResolve = UpRefs[i].UpRefTy;
1255 else
1256 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
1257 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list.
1258 --i; // Do not skip the next element.
1259 }

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

1411 LocTy ArgLoc;
1412 PATypeHolder ArgTy(Type::getVoidTy(Context));
1413 unsigned ArgAttrs1 = Attribute::None;
1414 unsigned ArgAttrs2 = Attribute::None;
1415 Value *V;
1416 if (ParseType(ArgTy, ArgLoc))
1417 return true;
1418
1228#endif
1229 if (!TypeToResolve)
1230 TypeToResolve = UpRefs[i].UpRefTy;
1231 else
1232 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
1233 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list.
1234 --i; // Do not skip the next element.
1235 }

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

1387 LocTy ArgLoc;
1388 PATypeHolder ArgTy(Type::getVoidTy(Context));
1389 unsigned ArgAttrs1 = Attribute::None;
1390 unsigned ArgAttrs2 = Attribute::None;
1391 Value *V;
1392 if (ParseType(ArgTy, ArgLoc))
1393 return true;
1394
1419 if (Lex.getKind() == lltok::Metadata) {
1420 if (ParseInlineMetadata(V, PFS))
1421 return true;
1422 } else {
1423 if (ParseOptionalAttrs(ArgAttrs1, 0) ||
1424 ParseValue(ArgTy, V, PFS) ||
1425 // FIXME: Should not allow attributes after the argument, remove this
1426 // in LLVM 3.0.
1427 ParseOptionalAttrs(ArgAttrs2, 3))
1428 return true;
1429 }
1395 // Otherwise, handle normal operands.
1396 if (ParseOptionalAttrs(ArgAttrs1, 0) ||
1397 ParseValue(ArgTy, V, PFS) ||
1398 // FIXME: Should not allow attributes after the argument, remove this
1399 // in LLVM 3.0.
1400 ParseOptionalAttrs(ArgAttrs2, 3))
1401 return true;
1430 ArgList.push_back(ParamInfo(ArgLoc, V, ArgAttrs1|ArgAttrs2));
1431 }
1432
1433 Lex.Lex(); // Lex the ')'.
1434 return false;
1435}
1436
1437

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

1926 ID.UIntVal = Lex.getUIntVal();
1927 ID.Kind = ValID::t_LocalID;
1928 break;
1929 case lltok::LocalVar: // %foo
1930 case lltok::StringConstant: // "foo" - FIXME: REMOVE IN LLVM 3.0
1931 ID.StrVal = Lex.getStrVal();
1932 ID.Kind = ValID::t_LocalName;
1933 break;
1402 ArgList.push_back(ParamInfo(ArgLoc, V, ArgAttrs1|ArgAttrs2));
1403 }
1404
1405 Lex.Lex(); // Lex the ')'.
1406 return false;
1407}
1408
1409

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

1898 ID.UIntVal = Lex.getUIntVal();
1899 ID.Kind = ValID::t_LocalID;
1900 break;
1901 case lltok::LocalVar: // %foo
1902 case lltok::StringConstant: // "foo" - FIXME: REMOVE IN LLVM 3.0
1903 ID.StrVal = Lex.getStrVal();
1904 ID.Kind = ValID::t_LocalName;
1905 break;
1934 case lltok::Metadata: { // !{...} MDNode, !"foo" MDString
1935 ID.Kind = ValID::t_Metadata;
1906 case lltok::exclaim: // !{...} MDNode, !"foo" MDString
1936 Lex.Lex();
1907 Lex.Lex();
1937 if (Lex.getKind() == lltok::lbrace) {
1908
1909 if (EatIfPresent(lltok::lbrace)) {
1938 SmallVector<Value*, 16> Elts;
1939 if (ParseMDNodeVector(Elts) ||
1940 ParseToken(lltok::rbrace, "expected end of metadata node"))
1941 return true;
1942
1910 SmallVector<Value*, 16> Elts;
1911 if (ParseMDNodeVector(Elts) ||
1912 ParseToken(lltok::rbrace, "expected end of metadata node"))
1913 return true;
1914
1943 ID.MetadataVal = MDNode::get(Context, Elts.data(), Elts.size());
1915 ID.MDNodeVal = MDNode::get(Context, Elts.data(), Elts.size());
1916 ID.Kind = ValID::t_MDNode;
1944 return false;
1945 }
1946
1947 // Standalone metadata reference
1948 // !{ ..., !42, ... }
1917 return false;
1918 }
1919
1920 // Standalone metadata reference
1921 // !{ ..., !42, ... }
1949 if (!ParseMDNode(ID.MetadataVal))
1922 if (Lex.getKind() == lltok::APSInt) {
1923 if (ParseMDNodeID(ID.MDNodeVal)) return true;
1924 ID.Kind = ValID::t_MDNode;
1950 return false;
1925 return false;
1951
1926 }
1927
1952 // MDString:
1953 // ::= '!' STRINGCONSTANT
1928 // MDString:
1929 // ::= '!' STRINGCONSTANT
1954 if (ParseMDString(ID.MetadataVal)) return true;
1955 ID.Kind = ValID::t_Metadata;
1930 if (ParseMDString(ID.MDStringVal)) return true;
1931 ID.Kind = ValID::t_MDString;
1956 return false;
1932 return false;
1957 }
1958 case lltok::APSInt:
1959 ID.APSIntVal = Lex.getAPSIntVal();
1960 ID.Kind = ValID::t_APSInt;
1961 break;
1962 case lltok::APFloat:
1963 ID.APFloatVal = Lex.getAPFloatVal();
1964 ID.Kind = ValID::t_APFloat;
1965 break;

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

2149 Lex.Lex();
2150 Constant *Val;
2151 SmallVector<unsigned, 4> Indices;
2152 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2153 ParseGlobalTypeAndValue(Val) ||
2154 ParseIndexList(Indices) ||
2155 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2156 return true;
1933 case lltok::APSInt:
1934 ID.APSIntVal = Lex.getAPSIntVal();
1935 ID.Kind = ValID::t_APSInt;
1936 break;
1937 case lltok::APFloat:
1938 ID.APFloatVal = Lex.getAPFloatVal();
1939 ID.Kind = ValID::t_APFloat;
1940 break;

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

2124 Lex.Lex();
2125 Constant *Val;
2126 SmallVector<unsigned, 4> Indices;
2127 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2128 ParseGlobalTypeAndValue(Val) ||
2129 ParseIndexList(Indices) ||
2130 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2131 return true;
2157 if (Lex.getKind() == lltok::NamedOrCustomMD)
2158 if (ParseOptionalCustomMetadata()) return true;
2159
2160 if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType()))
2161 return Error(ID.Loc, "extractvalue operand must be array or struct");
2162 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
2163 Indices.end()))
2164 return Error(ID.Loc, "invalid indices for extractvalue");
2165 ID.ConstantVal =
2166 ConstantExpr::getExtractValue(Val, Indices.data(), Indices.size());

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

2173 SmallVector<unsigned, 4> Indices;
2174 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2175 ParseGlobalTypeAndValue(Val0) ||
2176 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2177 ParseGlobalTypeAndValue(Val1) ||
2178 ParseIndexList(Indices) ||
2179 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2180 return true;
2132
2133 if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType()))
2134 return Error(ID.Loc, "extractvalue operand must be array or struct");
2135 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
2136 Indices.end()))
2137 return Error(ID.Loc, "invalid indices for extractvalue");
2138 ID.ConstantVal =
2139 ConstantExpr::getExtractValue(Val, Indices.data(), Indices.size());

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

2146 SmallVector<unsigned, 4> Indices;
2147 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2148 ParseGlobalTypeAndValue(Val0) ||
2149 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2150 ParseGlobalTypeAndValue(Val1) ||
2151 ParseIndexList(Indices) ||
2152 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2153 return true;
2181 if (Lex.getKind() == lltok::NamedOrCustomMD)
2182 if (ParseOptionalCustomMetadata()) return true;
2183 if (!isa<StructType>(Val0->getType()) && !isa<ArrayType>(Val0->getType()))
2184 return Error(ID.Loc, "extractvalue operand must be array or struct");
2185 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
2186 Indices.end()))
2187 return Error(ID.Loc, "invalid indices for insertvalue");
2188 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1,
2189 Indices.data(), Indices.size());
2190 ID.Kind = ValID::t_Constant;

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

2393/// constant.
2394bool LLParser::ConvertGlobalValIDToValue(const Type *Ty, ValID &ID,
2395 Constant *&V) {
2396 if (isa<FunctionType>(Ty))
2397 return Error(ID.Loc, "functions are not values, refer to them as pointers");
2398
2399 switch (ID.Kind) {
2400 default: llvm_unreachable("Unknown ValID!");
2154 if (!isa<StructType>(Val0->getType()) && !isa<ArrayType>(Val0->getType()))
2155 return Error(ID.Loc, "extractvalue operand must be array or struct");
2156 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
2157 Indices.end()))
2158 return Error(ID.Loc, "invalid indices for insertvalue");
2159 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1,
2160 Indices.data(), Indices.size());
2161 ID.Kind = ValID::t_Constant;

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

2364/// constant.
2365bool LLParser::ConvertGlobalValIDToValue(const Type *Ty, ValID &ID,
2366 Constant *&V) {
2367 if (isa<FunctionType>(Ty))
2368 return Error(ID.Loc, "functions are not values, refer to them as pointers");
2369
2370 switch (ID.Kind) {
2371 default: llvm_unreachable("Unknown ValID!");
2401 case ValID::t_Metadata:
2372 case ValID::t_MDNode:
2373 case ValID::t_MDString:
2402 return Error(ID.Loc, "invalid use of metadata");
2403 case ValID::t_LocalID:
2404 case ValID::t_LocalName:
2405 return Error(ID.Loc, "invalid use of function-local name");
2406 case ValID::t_InlineAsm:
2407 return Error(ID.Loc, "inline asm can only be an operand of call/invoke");
2408 case ValID::t_GlobalName:
2409 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);

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

2463 case ValID::t_Constant:
2464 if (ID.ConstantVal->getType() != Ty)
2465 return Error(ID.Loc, "constant expression type mismatch");
2466 V = ID.ConstantVal;
2467 return false;
2468 }
2469}
2470
2374 return Error(ID.Loc, "invalid use of metadata");
2375 case ValID::t_LocalID:
2376 case ValID::t_LocalName:
2377 return Error(ID.Loc, "invalid use of function-local name");
2378 case ValID::t_InlineAsm:
2379 return Error(ID.Loc, "inline asm can only be an operand of call/invoke");
2380 case ValID::t_GlobalName:
2381 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);

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

2435 case ValID::t_Constant:
2436 if (ID.ConstantVal->getType() != Ty)
2437 return Error(ID.Loc, "constant expression type mismatch");
2438 V = ID.ConstantVal;
2439 return false;
2440 }
2441}
2442
2443/// ConvertGlobalOrMetadataValIDToValue - Apply a type to a ValID to get a fully
2444/// resolved constant or metadata value.
2445bool LLParser::ConvertGlobalOrMetadataValIDToValue(const Type *Ty, ValID &ID,
2446 Value *&V) {
2447 switch (ID.Kind) {
2448 case ValID::t_MDNode:
2449 if (!Ty->isMetadataTy())
2450 return Error(ID.Loc, "metadata value must have metadata type");
2451 V = ID.MDNodeVal;
2452 return false;
2453 case ValID::t_MDString:
2454 if (!Ty->isMetadataTy())
2455 return Error(ID.Loc, "metadata value must have metadata type");
2456 V = ID.MDStringVal;
2457 return false;
2458 default:
2459 Constant *C;
2460 if (ConvertGlobalValIDToValue(Ty, ID, C)) return true;
2461 V = C;
2462 return false;
2463 }
2464}
2465
2466
2471bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
2472 PATypeHolder Type(Type::getVoidTy(Context));
2473 return ParseType(Type) ||
2474 ParseGlobalValue(Type, V);
2475}
2476
2477/// ParseGlobalValueVector
2478/// ::= /*empty*/

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

2499
2500
2501//===----------------------------------------------------------------------===//
2502// Function Parsing.
2503//===----------------------------------------------------------------------===//
2504
2505bool LLParser::ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
2506 PerFunctionState &PFS) {
2467bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
2468 PATypeHolder Type(Type::getVoidTy(Context));
2469 return ParseType(Type) ||
2470 ParseGlobalValue(Type, V);
2471}
2472
2473/// ParseGlobalValueVector
2474/// ::= /*empty*/

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

2495
2496
2497//===----------------------------------------------------------------------===//
2498// Function Parsing.
2499//===----------------------------------------------------------------------===//
2500
2501bool LLParser::ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
2502 PerFunctionState &PFS) {
2507 if (ID.Kind == ValID::t_LocalID)
2508 V = PFS.GetVal(ID.UIntVal, Ty, ID.Loc);
2509 else if (ID.Kind == ValID::t_LocalName)
2510 V = PFS.GetVal(ID.StrVal, Ty, ID.Loc);
2511 else if (ID.Kind == ValID::t_InlineAsm) {
2503 switch (ID.Kind) {
2504 case ValID::t_LocalID: V = PFS.GetVal(ID.UIntVal, Ty, ID.Loc); break;
2505 case ValID::t_LocalName: V = PFS.GetVal(ID.StrVal, Ty, ID.Loc); break;
2506 case ValID::t_InlineAsm: {
2512 const PointerType *PTy = dyn_cast<PointerType>(Ty);
2507 const PointerType *PTy = dyn_cast<PointerType>(Ty);
2513 const FunctionType *FTy =
2508 const FunctionType *FTy =
2514 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
2515 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2516 return Error(ID.Loc, "invalid type for inline asm constraint string");
2517 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1, ID.UIntVal>>1);
2518 return false;
2509 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
2510 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2511 return Error(ID.Loc, "invalid type for inline asm constraint string");
2512 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1, ID.UIntVal>>1);
2513 return false;
2519 } else if (ID.Kind == ValID::t_Metadata) {
2520 V = ID.MetadataVal;
2521 } else {
2522 Constant *C;
2523 if (ConvertGlobalValIDToValue(Ty, ID, C)) return true;
2524 V = C;
2525 return false;
2526 }
2514 }
2515 default:
2516 return ConvertGlobalOrMetadataValIDToValue(Ty, ID, V);
2517 }
2527
2528 return V == 0;
2529}
2530
2531bool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) {
2532 V = 0;
2533 ValID ID;
2534 return ParseValID(ID) ||

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

2798
2799 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
2800 if (BB == 0) return true;
2801
2802 std::string NameStr;
2803
2804 // Parse the instructions in this block until we get a terminator.
2805 Instruction *Inst;
2518
2519 return V == 0;
2520}
2521
2522bool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) {
2523 V = 0;
2524 ValID ID;
2525 return ParseValID(ID) ||

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

2789
2790 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
2791 if (BB == 0) return true;
2792
2793 std::string NameStr;
2794
2795 // Parse the instructions in this block until we get a terminator.
2796 Instruction *Inst;
2797 SmallVector<std::pair<unsigned, MDNode *>, 4> MetadataOnInst;
2806 do {
2807 // This instruction may have three possibilities for a name: a) none
2808 // specified, b) name specified "%foo =", c) number specified: "%4 =".
2809 LocTy NameLoc = Lex.getLoc();
2810 int NameID = -1;
2811 NameStr = "";
2812
2813 if (Lex.getKind() == lltok::LocalVarID) {

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

2819 // FIXME: REMOVE IN LLVM 3.0
2820 Lex.getKind() == lltok::StringConstant) {
2821 NameStr = Lex.getStrVal();
2822 Lex.Lex();
2823 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
2824 return true;
2825 }
2826
2798 do {
2799 // This instruction may have three possibilities for a name: a) none
2800 // specified, b) name specified "%foo =", c) number specified: "%4 =".
2801 LocTy NameLoc = Lex.getLoc();
2802 int NameID = -1;
2803 NameStr = "";
2804
2805 if (Lex.getKind() == lltok::LocalVarID) {

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

2811 // FIXME: REMOVE IN LLVM 3.0
2812 Lex.getKind() == lltok::StringConstant) {
2813 NameStr = Lex.getStrVal();
2814 Lex.Lex();
2815 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
2816 return true;
2817 }
2818
2827 if (ParseInstruction(Inst, BB, PFS)) return true;
2828 if (EatIfPresent(lltok::comma))
2829 ParseOptionalCustomMetadata();
2819 switch (ParseInstruction(Inst, BB, PFS)) {
2820 default: assert(0 && "Unknown ParseInstruction result!");
2821 case InstError: return true;
2822 case InstNormal:
2823 // With a normal result, we check to see if the instruction is followed by
2824 // a comma and metadata.
2825 if (EatIfPresent(lltok::comma))
2826 if (ParseInstructionMetadata(MetadataOnInst))
2827 return true;
2828 break;
2829 case InstExtraComma:
2830 // If the instruction parser ate an extra comma at the end of it, it
2831 // *must* be followed by metadata.
2832 if (ParseInstructionMetadata(MetadataOnInst))
2833 return true;
2834 break;
2835 }
2830
2831 // Set metadata attached with this instruction.
2836
2837 // Set metadata attached with this instruction.
2832 MetadataContext &TheMetadata = M->getContext().getMetadata();
2833 for (SmallVector<std::pair<unsigned, MDNode *>, 2>::iterator
2834 MDI = MDsOnInst.begin(), MDE = MDsOnInst.end(); MDI != MDE; ++MDI)
2835 TheMetadata.addMD(MDI->first, MDI->second, Inst);
2836 MDsOnInst.clear();
2838 for (unsigned i = 0, e = MetadataOnInst.size(); i != e; ++i)
2839 Inst->setMetadata(MetadataOnInst[i].first, MetadataOnInst[i].second);
2840 MetadataOnInst.clear();
2837
2838 BB->getInstList().push_back(Inst);
2839
2840 // Set the name on the instruction.
2841 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
2842 } while (!isa<TerminatorInst>(Inst));
2843
2844 return false;
2845}
2846
2847//===----------------------------------------------------------------------===//
2848// Instruction Parsing.
2849//===----------------------------------------------------------------------===//
2850
2851/// ParseInstruction - Parse one of the many different instructions.
2852///
2841
2842 BB->getInstList().push_back(Inst);
2843
2844 // Set the name on the instruction.
2845 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
2846 } while (!isa<TerminatorInst>(Inst));
2847
2848 return false;
2849}
2850
2851//===----------------------------------------------------------------------===//
2852// Instruction Parsing.
2853//===----------------------------------------------------------------------===//
2854
2855/// ParseInstruction - Parse one of the many different instructions.
2856///
2853bool LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
2854 PerFunctionState &PFS) {
2857int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
2858 PerFunctionState &PFS) {
2855 lltok::Kind Token = Lex.getKind();
2856 if (Token == lltok::Eof)
2857 return TokError("found end of file when expecting more instructions");
2858 LocTy Loc = Lex.getLoc();
2859 unsigned KeywordVal = Lex.getUIntVal();
2860 Lex.Lex(); // Eat the keyword.
2861
2862 switch (Token) {

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

3010 return false;
3011}
3012
3013//===----------------------------------------------------------------------===//
3014// Terminator Instructions.
3015//===----------------------------------------------------------------------===//
3016
3017/// ParseRet - Parse a return instruction.
2859 lltok::Kind Token = Lex.getKind();
2860 if (Token == lltok::Eof)
2861 return TokError("found end of file when expecting more instructions");
2862 LocTy Loc = Lex.getLoc();
2863 unsigned KeywordVal = Lex.getUIntVal();
2864 Lex.Lex(); // Eat the keyword.
2865
2866 switch (Token) {

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

3014 return false;
3015}
3016
3017//===----------------------------------------------------------------------===//
3018// Terminator Instructions.
3019//===----------------------------------------------------------------------===//
3020
3021/// ParseRet - Parse a return instruction.
3018/// ::= 'ret' void (',' !dbg, !1)
3019/// ::= 'ret' TypeAndValue (',' !dbg, !1)
3020/// ::= 'ret' TypeAndValue (',' TypeAndValue)+ (',' !dbg, !1)
3022/// ::= 'ret' void (',' !dbg, !1)*
3023/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
3024/// ::= 'ret' TypeAndValue (',' TypeAndValue)+ (',' !dbg, !1)*
3021/// [[obsolete: LLVM 3.0]]
3025/// [[obsolete: LLVM 3.0]]
3022bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
3023 PerFunctionState &PFS) {
3026int LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
3027 PerFunctionState &PFS) {
3024 PATypeHolder Ty(Type::getVoidTy(Context));
3025 if (ParseType(Ty, true /*void allowed*/)) return true;
3026
3027 if (Ty->isVoidTy()) {
3028 Inst = ReturnInst::Create(Context);
3029 return false;
3030 }
3031
3032 Value *RV;
3033 if (ParseValue(Ty, RV, PFS)) return true;
3034
3028 PATypeHolder Ty(Type::getVoidTy(Context));
3029 if (ParseType(Ty, true /*void allowed*/)) return true;
3030
3031 if (Ty->isVoidTy()) {
3032 Inst = ReturnInst::Create(Context);
3033 return false;
3034 }
3035
3036 Value *RV;
3037 if (ParseValue(Ty, RV, PFS)) return true;
3038
3039 bool ExtraComma = false;
3035 if (EatIfPresent(lltok::comma)) {
3036 // Parse optional custom metadata, e.g. !dbg
3040 if (EatIfPresent(lltok::comma)) {
3041 // Parse optional custom metadata, e.g. !dbg
3037 if (Lex.getKind() == lltok::NamedOrCustomMD) {
3038 if (ParseOptionalCustomMetadata()) return true;
3042 if (Lex.getKind() == lltok::MetadataVar) {
3043 ExtraComma = true;
3039 } else {
3040 // The normal case is one return value.
3044 } else {
3045 // The normal case is one return value.
3041 // FIXME: LLVM 3.0 remove MRV support for 'ret i32 1, i32 2', requiring use
3042 // of 'ret {i32,i32} {i32 1, i32 2}'
3046 // FIXME: LLVM 3.0 remove MRV support for 'ret i32 1, i32 2', requiring
3047 // use of 'ret {i32,i32} {i32 1, i32 2}'
3043 SmallVector<Value*, 8> RVs;
3044 RVs.push_back(RV);
3045
3046 do {
3047 // If optional custom metadata, e.g. !dbg is seen then this is the
3048 // end of MRV.
3048 SmallVector<Value*, 8> RVs;
3049 RVs.push_back(RV);
3050
3051 do {
3052 // If optional custom metadata, e.g. !dbg is seen then this is the
3053 // end of MRV.
3049 if (Lex.getKind() == lltok::NamedOrCustomMD)
3054 if (Lex.getKind() == lltok::MetadataVar)
3050 break;
3051 if (ParseTypeAndValue(RV, PFS)) return true;
3052 RVs.push_back(RV);
3053 } while (EatIfPresent(lltok::comma));
3054
3055 RV = UndefValue::get(PFS.getFunction().getReturnType());
3056 for (unsigned i = 0, e = RVs.size(); i != e; ++i) {
3057 Instruction *I = InsertValueInst::Create(RV, RVs[i], i, "mrv");
3058 BB->getInstList().push_back(I);
3059 RV = I;
3060 }
3061 }
3062 }
3063
3064 Inst = ReturnInst::Create(Context, RV);
3055 break;
3056 if (ParseTypeAndValue(RV, PFS)) return true;
3057 RVs.push_back(RV);
3058 } while (EatIfPresent(lltok::comma));
3059
3060 RV = UndefValue::get(PFS.getFunction().getReturnType());
3061 for (unsigned i = 0, e = RVs.size(); i != e; ++i) {
3062 Instruction *I = InsertValueInst::Create(RV, RVs[i], i, "mrv");
3063 BB->getInstList().push_back(I);
3064 RV = I;
3065 }
3066 }
3067 }
3068
3069 Inst = ReturnInst::Create(Context, RV);
3065 return false;
3070 return ExtraComma ? InstExtraComma : InstNormal;
3066}
3067
3068
3069/// ParseBr
3070/// ::= 'br' TypeAndValue
3071/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3072bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3073 LocTy Loc, Loc2;

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

3480 return Error(Loc, "invalid extractelement operands");
3481
3482 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3483 return false;
3484}
3485
3486/// ParsePHI
3487/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
3071}
3072
3073
3074/// ParseBr
3075/// ::= 'br' TypeAndValue
3076/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3077bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3078 LocTy Loc, Loc2;

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

3485 return Error(Loc, "invalid extractelement operands");
3486
3487 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3488 return false;
3489}
3490
3491/// ParsePHI
3492/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
3488bool LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
3493int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
3489 PATypeHolder Ty(Type::getVoidTy(Context));
3490 Value *Op0, *Op1;
3491 LocTy TypeLoc = Lex.getLoc();
3492
3493 if (ParseType(Ty) ||
3494 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3495 ParseValue(Ty, Op0, PFS) ||
3496 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3497 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
3498 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3499 return true;
3500
3494 PATypeHolder Ty(Type::getVoidTy(Context));
3495 Value *Op0, *Op1;
3496 LocTy TypeLoc = Lex.getLoc();
3497
3498 if (ParseType(Ty) ||
3499 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3500 ParseValue(Ty, Op0, PFS) ||
3501 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3502 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
3503 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3504 return true;
3505
3506 bool AteExtraComma = false;
3501 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3502 while (1) {
3503 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
3504
3505 if (!EatIfPresent(lltok::comma))
3506 break;
3507
3507 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3508 while (1) {
3509 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
3510
3511 if (!EatIfPresent(lltok::comma))
3512 break;
3513
3508 if (Lex.getKind() == lltok::NamedOrCustomMD)
3514 if (Lex.getKind() == lltok::MetadataVar) {
3515 AteExtraComma = true;
3509 break;
3516 break;
3517 }
3510
3511 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3512 ParseValue(Ty, Op0, PFS) ||
3513 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3514 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
3515 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3516 return true;
3517 }
3518
3518
3519 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3520 ParseValue(Ty, Op0, PFS) ||
3521 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3522 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
3523 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3524 return true;
3525 }
3526
3519 if (Lex.getKind() == lltok::NamedOrCustomMD)
3520 if (ParseOptionalCustomMetadata()) return true;
3521
3522 if (!Ty->isFirstClassType())
3523 return Error(TypeLoc, "phi node must have first class type");
3524
3525 PHINode *PN = PHINode::Create(Ty);
3526 PN->reserveOperandSpace(PHIVals.size());
3527 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3528 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3529 Inst = PN;
3527 if (!Ty->isFirstClassType())
3528 return Error(TypeLoc, "phi node must have first class type");
3529
3530 PHINode *PN = PHINode::Create(Ty);
3531 PN->reserveOperandSpace(PHIVals.size());
3532 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3533 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3534 Inst = PN;
3530 return false;
3535 return AteExtraComma ? InstExtraComma : InstNormal;
3531}
3532
3533/// ParseCall
3534/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value
3535/// ParameterList OptionalAttrs
3536bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
3537 bool isTail) {
3538 unsigned RetAttrs, FnAttrs;

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

3629
3630//===----------------------------------------------------------------------===//
3631// Memory Instructions.
3632//===----------------------------------------------------------------------===//
3633
3634/// ParseAlloc
3635/// ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalInfo)?
3636/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)?
3536}
3537
3538/// ParseCall
3539/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value
3540/// ParameterList OptionalAttrs
3541bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
3542 bool isTail) {
3543 unsigned RetAttrs, FnAttrs;

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

3634
3635//===----------------------------------------------------------------------===//
3636// Memory Instructions.
3637//===----------------------------------------------------------------------===//
3638
3639/// ParseAlloc
3640/// ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalInfo)?
3641/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)?
3637bool LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS,
3638 BasicBlock* BB, bool isAlloca) {
3642int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS,
3643 BasicBlock* BB, bool isAlloca) {
3639 PATypeHolder Ty(Type::getVoidTy(Context));
3640 Value *Size = 0;
3641 LocTy SizeLoc;
3642 unsigned Alignment = 0;
3643 if (ParseType(Ty)) return true;
3644
3644 PATypeHolder Ty(Type::getVoidTy(Context));
3645 Value *Size = 0;
3646 LocTy SizeLoc;
3647 unsigned Alignment = 0;
3648 if (ParseType(Ty)) return true;
3649
3650 bool AteExtraComma = false;
3645 if (EatIfPresent(lltok::comma)) {
3651 if (EatIfPresent(lltok::comma)) {
3646 if (Lex.getKind() == lltok::kw_align
3647 || Lex.getKind() == lltok::NamedOrCustomMD) {
3648 if (ParseOptionalInfo(Alignment)) return true;
3652 if (Lex.getKind() == lltok::kw_align) {
3653 if (ParseOptionalAlignment(Alignment)) return true;
3654 } else if (Lex.getKind() == lltok::MetadataVar) {
3655 AteExtraComma = true;
3649 } else {
3656 } else {
3650 if (ParseTypeAndValue(Size, SizeLoc, PFS)) return true;
3651 if (EatIfPresent(lltok::comma))
3652 if (ParseOptionalInfo(Alignment)) return true;
3657 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
3658 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3659 return true;
3653 }
3654 }
3655
3656 if (Size && Size->getType() != Type::getInt32Ty(Context))
3657 return Error(SizeLoc, "element count must be i32");
3658
3659 if (isAlloca) {
3660 Inst = new AllocaInst(Ty, Size, Alignment);
3660 }
3661 }
3662
3663 if (Size && Size->getType() != Type::getInt32Ty(Context))
3664 return Error(SizeLoc, "element count must be i32");
3665
3666 if (isAlloca) {
3667 Inst = new AllocaInst(Ty, Size, Alignment);
3661 return false;
3668 return AteExtraComma ? InstExtraComma : InstNormal;
3662 }
3663
3664 // Autoupgrade old malloc instruction to malloc call.
3665 // FIXME: Remove in LLVM 3.0.
3666 const Type *IntPtrTy = Type::getInt32Ty(Context);
3667 Constant *AllocSize = ConstantExpr::getSizeOf(Ty);
3668 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, IntPtrTy);
3669 if (!MallocF)
3670 // Prototype malloc as "void *(int32)".
3671 // This function is renamed as "malloc" in ValidateEndOfModule().
3672 MallocF = cast<Function>(
3673 M->getOrInsertFunction("", Type::getInt8PtrTy(Context), IntPtrTy, NULL));
3674 Inst = CallInst::CreateMalloc(BB, IntPtrTy, Ty, AllocSize, Size, MallocF);
3669 }
3670
3671 // Autoupgrade old malloc instruction to malloc call.
3672 // FIXME: Remove in LLVM 3.0.
3673 const Type *IntPtrTy = Type::getInt32Ty(Context);
3674 Constant *AllocSize = ConstantExpr::getSizeOf(Ty);
3675 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, IntPtrTy);
3676 if (!MallocF)
3677 // Prototype malloc as "void *(int32)".
3678 // This function is renamed as "malloc" in ValidateEndOfModule().
3679 MallocF = cast<Function>(
3680 M->getOrInsertFunction("", Type::getInt8PtrTy(Context), IntPtrTy, NULL));
3681 Inst = CallInst::CreateMalloc(BB, IntPtrTy, Ty, AllocSize, Size, MallocF);
3675 return false;
3682return AteExtraComma ? InstExtraComma : InstNormal;
3676}
3677
3678/// ParseFree
3679/// ::= 'free' TypeAndValue
3680bool LLParser::ParseFree(Instruction *&Inst, PerFunctionState &PFS,
3681 BasicBlock* BB) {
3682 Value *Val; LocTy Loc;
3683 if (ParseTypeAndValue(Val, Loc, PFS)) return true;
3684 if (!isa<PointerType>(Val->getType()))
3685 return Error(Loc, "operand to free must be a pointer");
3686 Inst = CallInst::CreateFree(Val, BB);
3687 return false;
3688}
3689
3690/// ParseLoad
3691/// ::= 'volatile'? 'load' TypeAndValue (',' OptionalInfo)?
3683}
3684
3685/// ParseFree
3686/// ::= 'free' TypeAndValue
3687bool LLParser::ParseFree(Instruction *&Inst, PerFunctionState &PFS,
3688 BasicBlock* BB) {
3689 Value *Val; LocTy Loc;
3690 if (ParseTypeAndValue(Val, Loc, PFS)) return true;
3691 if (!isa<PointerType>(Val->getType()))
3692 return Error(Loc, "operand to free must be a pointer");
3693 Inst = CallInst::CreateFree(Val, BB);
3694 return false;
3695}
3696
3697/// ParseLoad
3698/// ::= 'volatile'? 'load' TypeAndValue (',' OptionalInfo)?
3692bool LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS,
3693 bool isVolatile) {
3699int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS,
3700 bool isVolatile) {
3694 Value *Val; LocTy Loc;
3695 unsigned Alignment = 0;
3701 Value *Val; LocTy Loc;
3702 unsigned Alignment = 0;
3696 if (ParseTypeAndValue(Val, Loc, PFS)) return true;
3703 bool AteExtraComma = false;
3704 if (ParseTypeAndValue(Val, Loc, PFS) ||
3705 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3706 return true;
3697
3707
3698 if (EatIfPresent(lltok::comma))
3699 if (ParseOptionalInfo(Alignment)) return true;
3700
3701 if (!isa<PointerType>(Val->getType()) ||
3702 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
3703 return Error(Loc, "load operand must be a pointer to a first class type");
3704
3705 Inst = new LoadInst(Val, "", isVolatile, Alignment);
3708 if (!isa<PointerType>(Val->getType()) ||
3709 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
3710 return Error(Loc, "load operand must be a pointer to a first class type");
3711
3712 Inst = new LoadInst(Val, "", isVolatile, Alignment);
3706 return false;
3713 return AteExtraComma ? InstExtraComma : InstNormal;
3707}
3708
3709/// ParseStore
3710/// ::= 'volatile'? 'store' TypeAndValue ',' TypeAndValue (',' 'align' i32)?
3714}
3715
3716/// ParseStore
3717/// ::= 'volatile'? 'store' TypeAndValue ',' TypeAndValue (',' 'align' i32)?
3711bool LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS,
3712 bool isVolatile) {
3718int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS,
3719 bool isVolatile) {
3713 Value *Val, *Ptr; LocTy Loc, PtrLoc;
3714 unsigned Alignment = 0;
3720 Value *Val, *Ptr; LocTy Loc, PtrLoc;
3721 unsigned Alignment = 0;
3722 bool AteExtraComma = false;
3715 if (ParseTypeAndValue(Val, Loc, PFS) ||
3716 ParseToken(lltok::comma, "expected ',' after store operand") ||
3723 if (ParseTypeAndValue(Val, Loc, PFS) ||
3724 ParseToken(lltok::comma, "expected ',' after store operand") ||
3717 ParseTypeAndValue(Ptr, PtrLoc, PFS))
3725 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
3726 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3718 return true;
3719
3727 return true;
3728
3720 if (EatIfPresent(lltok::comma))
3721 if (ParseOptionalInfo(Alignment)) return true;
3722
3723 if (!isa<PointerType>(Ptr->getType()))
3724 return Error(PtrLoc, "store operand must be a pointer");
3725 if (!Val->getType()->isFirstClassType())
3726 return Error(Loc, "store operand must be a first class value");
3727 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
3728 return Error(Loc, "stored value and pointer type do not match");
3729
3730 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment);
3729 if (!isa<PointerType>(Ptr->getType()))
3730 return Error(PtrLoc, "store operand must be a pointer");
3731 if (!Val->getType()->isFirstClassType())
3732 return Error(Loc, "store operand must be a first class value");
3733 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
3734 return Error(Loc, "stored value and pointer type do not match");
3735
3736 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment);
3731 return false;
3737 return AteExtraComma ? InstExtraComma : InstNormal;
3732}
3733
3734/// ParseGetResult
3735/// ::= 'getresult' TypeAndValue ',' i32
3736/// FIXME: Remove support for getresult in LLVM 3.0
3737bool LLParser::ParseGetResult(Instruction *&Inst, PerFunctionState &PFS) {
3738 Value *Val; LocTy ValLoc, EltLoc;
3739 unsigned Element;

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

3747 if (!ExtractValueInst::getIndexedType(Val->getType(), Element))
3748 return Error(EltLoc, "invalid getresult index for value");
3749 Inst = ExtractValueInst::Create(Val, Element);
3750 return false;
3751}
3752
3753/// ParseGetElementPtr
3754/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
3738}
3739
3740/// ParseGetResult
3741/// ::= 'getresult' TypeAndValue ',' i32
3742/// FIXME: Remove support for getresult in LLVM 3.0
3743bool LLParser::ParseGetResult(Instruction *&Inst, PerFunctionState &PFS) {
3744 Value *Val; LocTy ValLoc, EltLoc;
3745 unsigned Element;

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

3753 if (!ExtractValueInst::getIndexedType(Val->getType(), Element))
3754 return Error(EltLoc, "invalid getresult index for value");
3755 Inst = ExtractValueInst::Create(Val, Element);
3756 return false;
3757}
3758
3759/// ParseGetElementPtr
3760/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
3755bool LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
3761int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
3756 Value *Ptr, *Val; LocTy Loc, EltLoc;
3757
3758 bool InBounds = EatIfPresent(lltok::kw_inbounds);
3759
3760 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
3761
3762 if (!isa<PointerType>(Ptr->getType()))
3763 return Error(Loc, "base of getelementptr must be a pointer");
3764
3765 SmallVector<Value*, 16> Indices;
3762 Value *Ptr, *Val; LocTy Loc, EltLoc;
3763
3764 bool InBounds = EatIfPresent(lltok::kw_inbounds);
3765
3766 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
3767
3768 if (!isa<PointerType>(Ptr->getType()))
3769 return Error(Loc, "base of getelementptr must be a pointer");
3770
3771 SmallVector<Value*, 16> Indices;
3772 bool AteExtraComma = false;
3766 while (EatIfPresent(lltok::comma)) {
3773 while (EatIfPresent(lltok::comma)) {
3767 if (Lex.getKind() == lltok::NamedOrCustomMD)
3774 if (Lex.getKind() == lltok::MetadataVar) {
3775 AteExtraComma = true;
3768 break;
3776 break;
3777 }
3769 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
3770 if (!isa<IntegerType>(Val->getType()))
3771 return Error(EltLoc, "getelementptr index must be an integer");
3772 Indices.push_back(Val);
3773 }
3778 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
3779 if (!isa<IntegerType>(Val->getType()))
3780 return Error(EltLoc, "getelementptr index must be an integer");
3781 Indices.push_back(Val);
3782 }
3774 if (Lex.getKind() == lltok::NamedOrCustomMD)
3775 if (ParseOptionalCustomMetadata()) return true;
3776
3777 if (!GetElementPtrInst::getIndexedType(Ptr->getType(),
3778 Indices.begin(), Indices.end()))
3779 return Error(Loc, "invalid getelementptr indices");
3780 Inst = GetElementPtrInst::Create(Ptr, Indices.begin(), Indices.end());
3781 if (InBounds)
3782 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
3783
3784 if (!GetElementPtrInst::getIndexedType(Ptr->getType(),
3785 Indices.begin(), Indices.end()))
3786 return Error(Loc, "invalid getelementptr indices");
3787 Inst = GetElementPtrInst::Create(Ptr, Indices.begin(), Indices.end());
3788 if (InBounds)
3789 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
3783 return false;
3790 return AteExtraComma ? InstExtraComma : InstNormal;
3784}
3785
3786/// ParseExtractValue
3787/// ::= 'extractvalue' TypeAndValue (',' uint32)+
3791}
3792
3793/// ParseExtractValue
3794/// ::= 'extractvalue' TypeAndValue (',' uint32)+
3788bool LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
3795int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
3789 Value *Val; LocTy Loc;
3790 SmallVector<unsigned, 4> Indices;
3796 Value *Val; LocTy Loc;
3797 SmallVector<unsigned, 4> Indices;
3798 bool AteExtraComma;
3791 if (ParseTypeAndValue(Val, Loc, PFS) ||
3799 if (ParseTypeAndValue(Val, Loc, PFS) ||
3792 ParseIndexList(Indices))
3800 ParseIndexList(Indices, AteExtraComma))
3793 return true;
3801 return true;
3794 if (Lex.getKind() == lltok::NamedOrCustomMD)
3795 if (ParseOptionalCustomMetadata()) return true;
3796
3797 if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType()))
3798 return Error(Loc, "extractvalue operand must be array or struct");
3799
3800 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
3801 Indices.end()))
3802 return Error(Loc, "invalid indices for extractvalue");
3803 Inst = ExtractValueInst::Create(Val, Indices.begin(), Indices.end());
3802
3803 if (!isa<StructType>(Val->getType()) && !isa<ArrayType>(Val->getType()))
3804 return Error(Loc, "extractvalue operand must be array or struct");
3805
3806 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
3807 Indices.end()))
3808 return Error(Loc, "invalid indices for extractvalue");
3809 Inst = ExtractValueInst::Create(Val, Indices.begin(), Indices.end());
3804 return false;
3810 return AteExtraComma ? InstExtraComma : InstNormal;
3805}
3806
3807/// ParseInsertValue
3808/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
3811}
3812
3813/// ParseInsertValue
3814/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
3809bool LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
3815int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
3810 Value *Val0, *Val1; LocTy Loc0, Loc1;
3811 SmallVector<unsigned, 4> Indices;
3816 Value *Val0, *Val1; LocTy Loc0, Loc1;
3817 SmallVector<unsigned, 4> Indices;
3818 bool AteExtraComma;
3812 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
3813 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
3814 ParseTypeAndValue(Val1, Loc1, PFS) ||
3819 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
3820 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
3821 ParseTypeAndValue(Val1, Loc1, PFS) ||
3815 ParseIndexList(Indices))
3822 ParseIndexList(Indices, AteExtraComma))
3816 return true;
3823 return true;
3817 if (Lex.getKind() == lltok::NamedOrCustomMD)
3818 if (ParseOptionalCustomMetadata()) return true;
3819
3824
3820 if (!isa<StructType>(Val0->getType()) && !isa<ArrayType>(Val0->getType()))
3821 return Error(Loc0, "extractvalue operand must be array or struct");
3822
3823 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
3824 Indices.end()))
3825 return Error(Loc0, "invalid indices for insertvalue");
3826 Inst = InsertValueInst::Create(Val0, Val1, Indices.begin(), Indices.end());
3825 if (!isa<StructType>(Val0->getType()) && !isa<ArrayType>(Val0->getType()))
3826 return Error(Loc0, "extractvalue operand must be array or struct");
3827
3828 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
3829 Indices.end()))
3830 return Error(Loc0, "invalid indices for insertvalue");
3831 Inst = InsertValueInst::Create(Val0, Val1, Indices.begin(), Indices.end());
3827 return false;
3832 return AteExtraComma ? InstExtraComma : InstNormal;
3828}
3829
3830//===----------------------------------------------------------------------===//
3831// Embedded metadata.
3832//===----------------------------------------------------------------------===//
3833
3834/// ParseMDNodeVector
3835/// ::= Element (',' Element)*
3836/// Element
3837/// ::= 'null' | TypeAndValue
3838bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts) {
3833}
3834
3835//===----------------------------------------------------------------------===//
3836// Embedded metadata.
3837//===----------------------------------------------------------------------===//
3838
3839/// ParseMDNodeVector
3840/// ::= Element (',' Element)*
3841/// Element
3842/// ::= 'null' | TypeAndValue
3843bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts) {
3839 assert(Lex.getKind() == lltok::lbrace);
3840 Lex.Lex();
3841 do {
3844 do {
3842 Value *V = 0;
3843 if (Lex.getKind() == lltok::kw_null) {
3844 Lex.Lex();
3845 V = 0;
3846 } else {
3847 PATypeHolder Ty(Type::getVoidTy(Context));
3848 if (ParseType(Ty)) return true;
3849 if (Lex.getKind() == lltok::Metadata) {
3850 Lex.Lex();
3851 MetadataBase *Node = 0;
3852 if (!ParseMDNode(Node))
3853 V = Node;
3854 else {
3855 MetadataBase *MDS = 0;
3856 if (ParseMDString(MDS)) return true;
3857 V = MDS;
3858 }
3859 } else {
3860 Constant *C;
3861 if (ParseGlobalValue(Ty, C)) return true;
3862 V = C;
3863 }
3845 // Null is a special case since it is typeless.
3846 if (EatIfPresent(lltok::kw_null)) {
3847 Elts.push_back(0);
3848 continue;
3864 }
3849 }
3850
3851 Value *V = 0;
3852 PATypeHolder Ty(Type::getVoidTy(Context));
3853 ValID ID;
3854 if (ParseType(Ty) || ParseValID(ID) ||
3855 ConvertGlobalOrMetadataValIDToValue(Ty, ID, V))
3856 return true;
3857
3865 Elts.push_back(V);
3866 } while (EatIfPresent(lltok::comma));
3867
3868 return false;
3869}
3858 Elts.push_back(V);
3859 } while (EatIfPresent(lltok::comma));
3860
3861 return false;
3862}