LLParser.cpp revision 309b3af547a60bedd74daa2a94ebd3d3ed5f06e9
1df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===-- LLParser.cpp - Parser Class ---------------------------------------===//
2df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//
3df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//                     The LLVM Compiler Infrastructure
4df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//
5df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// This file is distributed under the University of Illinois Open Source
6df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// License. See LICENSE.TXT for details.
7df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//
8df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
9df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//
10df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//  This file defines the parser class for .ll files.
11df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//
12df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
13df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
14df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "LLParser.h"
15df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/AutoUpgrade.h"
16df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/CallingConv.h"
17df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/Constants.h"
18df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/DerivedTypes.h"
19df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/InlineAsm.h"
20df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/Instructions.h"
21df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/Module.h"
221224c386981f7948f298ed9ad444c40609570f2eDan Gohman#include "llvm/Operator.h"
23df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/ValueSymbolTable.h"
24df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/ADT/SmallPtrSet.h"
25df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/ADT/StringExtras.h"
26c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin#include "llvm/Support/ErrorHandling.h"
27df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#include "llvm/Support/raw_ostream.h"
28df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerusing namespace llvm;
29df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
303ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// Run: module ::= toplevelentity*
31ad7d1e2085a489dbedc65b99bab811771ead1aabChris Lattnerbool LLParser::Run() {
323ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  // Prime the lexer.
333ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  Lex.Lex();
343ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner
35ad7d1e2085a489dbedc65b99bab811771ead1aabChris Lattner  return ParseTopLevelEntities() ||
36ad7d1e2085a489dbedc65b99bab811771ead1aabChris Lattner         ValidateEndOfModule();
37df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
38df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
39df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
40df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// module.
41df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ValidateEndOfModule() {
42449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  // Handle any instruction metadata forward references.
43449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  if (!ForwardRefInstMetadata.empty()) {
44449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner    for (DenseMap<Instruction*, std::vector<MDRef> >::iterator
45449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner         I = ForwardRefInstMetadata.begin(), E = ForwardRefInstMetadata.end();
46449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner         I != E; ++I) {
47449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner      Instruction *Inst = I->first;
48449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner      const std::vector<MDRef> &MDList = I->second;
49449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner
50449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner      for (unsigned i = 0, e = MDList.size(); i != e; ++i) {
51449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner        unsigned SlotNo = MDList[i].MDSlot;
52449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner
53449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner        if (SlotNo >= NumberedMetadata.size() || NumberedMetadata[SlotNo] == 0)
54449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner          return Error(MDList[i].Loc, "use of undefined metadata '!" +
55449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner                       utostr(SlotNo) + "'");
56449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner        Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]);
57449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner      }
58449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner    }
59449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner    ForwardRefInstMetadata.clear();
60449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  }
61449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner
62449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner
6368afa54033eccda0f55e920cb548588e71a45418Victor Hernandez  // Update auto-upgraded malloc calls to "malloc".
64cf4d2f11a1acc12a0f6b50991907e4a998ee9cddChris Lattner  // FIXME: Remove in LLVM 3.0.
6513ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez  if (MallocF) {
6613ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez    MallocF->setName("malloc");
6713ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez    // If setName() does not set the name to "malloc", then there is already a
6813ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez    // declaration of "malloc".  In that case, iterate over all calls to MallocF
6913ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez    // and get them to call the declared "malloc" instead.
7013ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez    if (MallocF->getName() != "malloc") {
7109d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      Constant *RealMallocF = M->getFunction("malloc");
7268afa54033eccda0f55e920cb548588e71a45418Victor Hernandez      if (RealMallocF->getType() != MallocF->getType())
7368afa54033eccda0f55e920cb548588e71a45418Victor Hernandez        RealMallocF = ConstantExpr::getBitCast(RealMallocF, MallocF->getType());
7468afa54033eccda0f55e920cb548588e71a45418Victor Hernandez      MallocF->replaceAllUsesWith(RealMallocF);
7513ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez      MallocF->eraseFromParent();
7613ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez      MallocF = NULL;
7713ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez    }
7813ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez  }
7909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
8009d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
8109d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  // If there are entries in ForwardRefBlockAddresses at this point, they are
8209d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  // references after the function was defined.  Resolve those now.
8309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  while (!ForwardRefBlockAddresses.empty()) {
8409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    // Okay, we are referencing an already-parsed function, resolve them now.
8509d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    Function *TheFn = 0;
8609d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    const ValID &Fn = ForwardRefBlockAddresses.begin()->first;
8709d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    if (Fn.Kind == ValID::t_GlobalName)
8809d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      TheFn = M->getFunction(Fn.StrVal);
8909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    else if (Fn.UIntVal < NumberedVals.size())
9009d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      TheFn = dyn_cast<Function>(NumberedVals[Fn.UIntVal]);
9109d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
9209d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    if (TheFn == 0)
9309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      return Error(Fn.Loc, "unknown function referenced by blockaddress");
9409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
9509d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    // Resolve all these references.
9609d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    if (ResolveForwardRefBlockAddresses(TheFn,
9709d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner                                      ForwardRefBlockAddresses.begin()->second,
9809d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner                                        0))
9909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      return true;
10009d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
10109d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    ForwardRefBlockAddresses.erase(ForwardRefBlockAddresses.begin());
10209d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  }
10309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
10409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
105df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!ForwardRefTypes.empty())
106df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(ForwardRefTypes.begin()->second.second,
107df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                 "use of undefined type named '" +
108df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                 ForwardRefTypes.begin()->first + "'");
109df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!ForwardRefTypeIDs.empty())
110df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(ForwardRefTypeIDs.begin()->second.second,
111df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                 "use of undefined type '%" +
112df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                 utostr(ForwardRefTypeIDs.begin()->first) + "'");
113a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
114df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!ForwardRefVals.empty())
115df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(ForwardRefVals.begin()->second.second,
116df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                 "use of undefined value '@" + ForwardRefVals.begin()->first +
117df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                 "'");
118a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
119df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!ForwardRefValIDs.empty())
120df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(ForwardRefValIDs.begin()->second.second,
121df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                 "use of undefined value '@" +
122df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                 utostr(ForwardRefValIDs.begin()->first) + "'");
123a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1241c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel  if (!ForwardRefMDNodes.empty())
1251c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel    return Error(ForwardRefMDNodes.begin()->second.second,
1261c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel                 "use of undefined metadata '!" +
1271c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel                 utostr(ForwardRefMDNodes.begin()->first) + "'");
128a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1291c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel
130df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Look for intrinsic functions and CallInst that need to be upgraded
131df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
132df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
133a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
134e4b275610a7a05b7ee4c0378a906a6330e4c4ab0Devang Patel  // Check debug info intrinsics.
135e4b275610a7a05b7ee4c0378a906a6330e4c4ab0Devang Patel  CheckDebugInfoIntrinsics(M);
136df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
137df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
138df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
13909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattnerbool LLParser::ResolveForwardRefBlockAddresses(Function *TheFn,
14009d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner                             std::vector<std::pair<ValID, GlobalValue*> > &Refs,
14109d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner                                               PerFunctionState *PFS) {
14209d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  // Loop over all the references, resolving them.
14309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
14409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    BasicBlock *Res;
145cdfc940912d56a63b6f12eaa7f3faf79cf74c693Chris Lattner    if (PFS) {
14609d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      if (Refs[i].first.Kind == ValID::t_LocalName)
14709d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner        Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc);
148cdfc940912d56a63b6f12eaa7f3faf79cf74c693Chris Lattner      else
14909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner        Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc);
15009d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    } else if (Refs[i].first.Kind == ValID::t_LocalID) {
15109d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      return Error(Refs[i].first.Loc,
152ee7644da4cbc18f95495a5217c6a0c61eb13c767Chris Lattner       "cannot take address of numeric label after the function is defined");
15309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    } else {
15409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      Res = dyn_cast_or_null<BasicBlock>(
15509d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner                     TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal));
15609d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    }
15709d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
158cdfc940912d56a63b6f12eaa7f3faf79cf74c693Chris Lattner    if (Res == 0)
15909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      return Error(Refs[i].first.Loc,
16009d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner                   "referenced value is not a basic block");
16109d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
16209d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    // Get the BlockAddress for this and update references to use it.
16309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    BlockAddress *BA = BlockAddress::get(TheFn, Res);
16409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    Refs[i].second->replaceAllUsesWith(BA);
16509d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    Refs[i].second->eraseFromParent();
16609d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  }
16709d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  return false;
16809d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner}
16909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
17009d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
171df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
172df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Top-Level Entities
173df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
174df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
175df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseTopLevelEntities() {
176df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  while (1) {
177df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    switch (Lex.getKind()) {
178df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    default:         return TokError("expected top-level entity");
179df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::Eof: return false;
180df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    //case lltok::kw_define:
181df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_declare: if (ParseDeclare()) return true; break;
182df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_define:  if (ParseDefine()) return true; break;
183df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_module:  if (ParseModuleAsm()) return true; break;
184df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_target:  if (ParseTargetDefinition()) return true; break;
185df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
186df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_type:    if (ParseUnnamedType()) return true; break;
1873845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman    case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
188df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0
189df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::LocalVar:   if (ParseNamedType()) return true; break;
1903845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman    case lltok::GlobalID:   if (ParseUnnamedGlobal()) return true; break;
191df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::GlobalVar:  if (ParseNamedGlobal()) return true; break;
192e434d277ca5183eeb6f881000732dcf4c8edd52eChris Lattner    case lltok::exclaim:    if (ParseStandaloneMetadata()) return true; break;
1931d92831759620a2e5ce4f5a3088c0a1a77a48c8fChris Lattner    case lltok::MetadataVar: if (ParseNamedMetadata()) return true; break;
194df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
195df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // The Global variable production with no name can have many different
196df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // optional leading prefixes, the production is:
197df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
198df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    //               OptionalAddrSpace ('constant'|'global') ...
1995e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_private:             // OptionalLinkage
2005e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_linker_private:      // OptionalLinkage
2015e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_linker_private_weak: // OptionalLinkage
20255ae515f9db484125a23429d4906c5edaf9f10d2Bill Wendling    case lltok::kw_linker_private_weak_def_auto: // OptionalLinkage
2035e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_internal:            // OptionalLinkage
2045e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_weak:                // OptionalLinkage
2055e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_weak_odr:            // OptionalLinkage
2065e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_linkonce:            // OptionalLinkage
2075e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_linkonce_odr:        // OptionalLinkage
2085e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_appending:           // OptionalLinkage
2095e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_dllexport:           // OptionalLinkage
2105e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_common:              // OptionalLinkage
2115e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_dllimport:           // OptionalLinkage
2125e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_extern_weak:         // OptionalLinkage
2135e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    case lltok::kw_external: {          // OptionalLinkage
214df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      unsigned Linkage, Visibility;
215df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (ParseOptionalLinkage(Linkage) ||
216df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner          ParseOptionalVisibility(Visibility) ||
217eeb4a84ac8d91fb1d5a7c484a1c7047409faee30Chris Lattner          ParseGlobal("", SMLoc(), Linkage, true, Visibility))
218df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return true;
219df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      break;
220df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
221df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_default:       // OptionalVisibility
222df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_hidden:        // OptionalVisibility
223df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_protected: {   // OptionalVisibility
224df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      unsigned Visibility;
225df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (ParseOptionalVisibility(Visibility) ||
226eeb4a84ac8d91fb1d5a7c484a1c7047409faee30Chris Lattner          ParseGlobal("", SMLoc(), 0, false, Visibility))
227df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return true;
228df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      break;
229df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
230a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
231df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_thread_local:  // OptionalThreadLocal
232df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_addrspace:     // OptionalAddrSpace
233df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_constant:      // GlobalType
234df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_global:        // GlobalType
235eeb4a84ac8d91fb1d5a7c484a1c7047409faee30Chris Lattner      if (ParseGlobal("", SMLoc(), 0, false, 0)) return true;
236df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      break;
237df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
238df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
239df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
240df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
241df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
242df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity
243df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'module' 'asm' STRINGCONSTANT
244df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseModuleAsm() {
245df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  assert(Lex.getKind() == lltok::kw_module);
246df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
247a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
248a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar  std::string AsmStr;
2493ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
2503ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      ParseStringConstant(AsmStr)) return true;
251a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
252df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  const std::string &AsmSoFar = M->getModuleInlineAsm();
253df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (AsmSoFar.empty())
2543ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    M->setModuleInlineAsm(AsmStr);
255df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  else
2563ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    M->setModuleInlineAsm(AsmSoFar+"\n"+AsmStr);
257df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
258df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
259df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
260df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity
261df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'target' 'triple' '=' STRINGCONSTANT
262df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'target' 'datalayout' '=' STRINGCONSTANT
263df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseTargetDefinition() {
264df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  assert(Lex.getKind() == lltok::kw_target);
2653ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  std::string Str;
266df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  switch (Lex.Lex()) {
267df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  default: return TokError("unknown target property");
268df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_triple:
269df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
2703ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    if (ParseToken(lltok::equal, "expected '=' after target triple") ||
2713ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner        ParseStringConstant(Str))
272df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2733ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    M->setTargetTriple(Str);
274df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
275df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_datalayout:
276df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
2773ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
2783ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner        ParseStringConstant(Str))
279df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2803ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    M->setDataLayout(Str);
281df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
282df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
283df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
284df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
285df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity
286df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'deplibs' '=' '[' ']'
287df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
288df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseDepLibs() {
289df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  assert(Lex.getKind() == lltok::kw_deplibs);
2903ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  Lex.Lex();
2913ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
2923ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      ParseToken(lltok::lsquare, "expected '=' after deplibs"))
2933ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    return true;
294df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2953ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (EatIfPresent(lltok::rsquare))
296df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
297a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2983ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  std::string Str;
2993ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (ParseStringConstant(Str)) return true;
3003ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  M->addLibrary(Str);
301df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3023ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  while (EatIfPresent(lltok::comma)) {
3033ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    if (ParseStringConstant(Str)) return true;
3043ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    M->addLibrary(Str);
305df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
306df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3073ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  return ParseToken(lltok::rsquare, "expected ']' at end of list");
308df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
309df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3103845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman/// ParseUnnamedType:
311df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'type' type
3123845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman///   ::= LocalVarID '=' 'type' type
313df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseUnnamedType() {
3143845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  unsigned TypeID = NumberedTypes.size();
3153845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman
3163845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  // Handle the LocalVarID form.
3173845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  if (Lex.getKind() == lltok::LocalVarID) {
3183845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman    if (Lex.getUIntVal() != TypeID)
3193845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman      return Error(Lex.getLoc(), "type expected to be numbered '%" +
3203845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman                   utostr(TypeID) + "'");
3213845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman    Lex.Lex(); // eat LocalVarID;
3223845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman
3233845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman    if (ParseToken(lltok::equal, "expected '=' after name"))
3243845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman      return true;
3253845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  }
3263845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman
327df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy TypeLoc = Lex.getLoc();
328f7240de877e790d9e377c1fe49b6d7a14c1a9674Chris Lattner  if (ParseToken(lltok::kw_type, "expected 'type' after '='")) return true;
329df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3301d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder Ty(Type::getVoidTy(Context));
331df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseType(Ty)) return true;
332a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
333df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // See if this type was previously referenced.
334df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator
335df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    FI = ForwardRefTypeIDs.find(TypeID);
336df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (FI != ForwardRefTypeIDs.end()) {
337c38daba25b66f9a0386fc28861db66e62663310dChris Lattner    if (FI->second.first.get() == Ty)
338c38daba25b66f9a0386fc28861db66e62663310dChris Lattner      return Error(TypeLoc, "self referential type is invalid");
339a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
340df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    cast<DerivedType>(FI->second.first.get())->refineAbstractTypeTo(Ty);
341df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Ty = FI->second.first.get();
342df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ForwardRefTypeIDs.erase(FI);
343df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
344a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
345df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  NumberedTypes.push_back(Ty);
346a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
347df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
348df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
349df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
350df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity
351df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= LocalVar '=' 'type' type
352df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseNamedType() {
353df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::string Name = Lex.getStrVal();
354df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy NameLoc = Lex.getLoc();
3553ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  Lex.Lex();  // eat LocalVar.
356a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3571d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder Ty(Type::getVoidTy(Context));
358a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3593ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (ParseToken(lltok::equal, "expected '=' after name") ||
3603ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      ParseToken(lltok::kw_type, "expected 'type' after name") ||
3613ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      ParseType(Ty))
3623ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    return true;
363a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
364df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Set the type name, checking for conflicts as we do so.
365df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  bool AlreadyExists = M->addTypeName(Name, Ty);
366df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!AlreadyExists) return false;
367df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
368df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // See if this type is a forward reference.  We need to eagerly resolve
369df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // types to allow recursive type redefinitions below.
370df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::map<std::string, std::pair<PATypeHolder, LocTy> >::iterator
371df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  FI = ForwardRefTypes.find(Name);
372df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (FI != ForwardRefTypes.end()) {
373c38daba25b66f9a0386fc28861db66e62663310dChris Lattner    if (FI->second.first.get() == Ty)
374c38daba25b66f9a0386fc28861db66e62663310dChris Lattner      return Error(NameLoc, "self referential type is invalid");
375c38daba25b66f9a0386fc28861db66e62663310dChris Lattner
376df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    cast<DerivedType>(FI->second.first.get())->refineAbstractTypeTo(Ty);
377df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Ty = FI->second.first.get();
378df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ForwardRefTypes.erase(FI);
379df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
380a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
381df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Inserting a name that is already defined, get the existing name.
382df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  const Type *Existing = M->getTypeByName(Name);
383df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  assert(Existing && "Conflict but no matching type?!");
384a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
385df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Otherwise, this is an attempt to redefine a type. That's okay if
386df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // the redefinition is identical to the original.
387df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // FIXME: REMOVE REDEFINITIONS IN LLVM 3.0
388df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Existing == Ty) return false;
389a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
390df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Any other kind of (non-equivalent) redefinition is an error.
391df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return Error(NameLoc, "redefinition of type named '" + Name + "' of type '" +
392df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner               Ty->getDescription() + "'");
393df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
394df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
395df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
396df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity
397df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'declare' FunctionHeader
398df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseDeclare() {
399df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  assert(Lex.getKind() == lltok::kw_declare);
400df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
401a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
402df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Function *F;
403df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return ParseFunctionHeader(F, false);
404df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
405df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
406df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// toplevelentity
407df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'define' FunctionHeader '{' ...
408df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseDefine() {
409df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  assert(Lex.getKind() == lltok::kw_define);
410df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
411a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
412df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Function *F;
4133ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  return ParseFunctionHeader(F, true) ||
4143ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner         ParseFunctionBody(*F);
415df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
416df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
4173ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ParseGlobalType
4183ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner///   ::= 'constant'
4193ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner///   ::= 'global'
420df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGlobalType(bool &IsConstant) {
421df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Lex.getKind() == lltok::kw_constant)
422df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    IsConstant = true;
423df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  else if (Lex.getKind() == lltok::kw_global)
424df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    IsConstant = false;
42535b5107951582f58bf9d6f786fdddbf7811420fdDuncan Sands  else {
42635b5107951582f58bf9d6f786fdddbf7811420fdDuncan Sands    IsConstant = false;
427df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return TokError("expected 'global' or 'constant'");
42835b5107951582f58bf9d6f786fdddbf7811420fdDuncan Sands  }
429df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
430df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
431df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
432df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
4333845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman/// ParseUnnamedGlobal:
4343845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman///   OptionalVisibility ALIAS ...
4353845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman///   OptionalLinkage OptionalVisibility ...   -> global variable
4363845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman///   GlobalID '=' OptionalVisibility ALIAS ...
4373845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman///   GlobalID '=' OptionalLinkage OptionalVisibility ...   -> global variable
4383845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohmanbool LLParser::ParseUnnamedGlobal() {
4393845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  unsigned VarID = NumberedVals.size();
4403845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  std::string Name;
4413845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  LocTy NameLoc = Lex.getLoc();
4423845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman
4433845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  // Handle the GlobalID form.
4443845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  if (Lex.getKind() == lltok::GlobalID) {
4453845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman    if (Lex.getUIntVal() != VarID)
4463845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman      return Error(Lex.getLoc(), "variable expected to be numbered '%" +
4473845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman                   utostr(VarID) + "'");
4483845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman    Lex.Lex(); // eat GlobalID;
4493845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman
4503845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman    if (ParseToken(lltok::equal, "expected '=' after name"))
4513845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman      return true;
4523845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  }
4533845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman
4543845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  bool HasLinkage;
4553845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  unsigned Linkage, Visibility;
4563845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  if (ParseOptionalLinkage(Linkage, HasLinkage) ||
4573845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman      ParseOptionalVisibility(Visibility))
4583845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman    return true;
459a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
4603845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  if (HasLinkage || Lex.getKind() != lltok::kw_alias)
4613845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman    return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
4623845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman  return ParseAlias(Name, NameLoc, Visibility);
4633845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman}
4643845e50edea58edd0f5e0f2b732e0106b53ba279Dan Gohman
465df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseNamedGlobal:
466df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   GlobalVar '=' OptionalVisibility ALIAS ...
467df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   GlobalVar '=' OptionalLinkage OptionalVisibility ...   -> global variable
468df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseNamedGlobal() {
469df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  assert(Lex.getKind() == lltok::GlobalVar);
470df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy NameLoc = Lex.getLoc();
471df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::string Name = Lex.getStrVal();
472df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
473a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
474df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  bool HasLinkage;
475df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned Linkage, Visibility;
476df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseToken(lltok::equal, "expected '=' in global variable") ||
477df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseOptionalLinkage(Linkage, HasLinkage) ||
478df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseOptionalVisibility(Visibility))
479df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
480a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
481df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (HasLinkage || Lex.getKind() != lltok::kw_alias)
482df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
483df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return ParseAlias(Name, NameLoc, Visibility);
484df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
485df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
486256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel// MDString:
487256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel//   ::= '!' STRINGCONSTANT
488442ffa1c5688f32b275f7657b5ee3646bfe8ffa3Chris Lattnerbool LLParser::ParseMDString(MDString *&Result) {
489256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel  std::string Str;
490256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel  if (ParseStringConstant(Str)) return true;
491442ffa1c5688f32b275f7657b5ee3646bfe8ffa3Chris Lattner  Result = MDString::get(Context, Str);
492256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel  return false;
493256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel}
494256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel
495256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel// MDNode:
496256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel//   ::= '!' MDNodeNumber
497449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner//
498449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner/// This version of ParseMDNodeID returns the slot number and null in the case
499449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner/// of a forward reference.
500449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattnerbool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
501449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  // !{ ..., !42, ... }
502449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  if (ParseUInt32(SlotNo)) return true;
503449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner
504449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  // Check existing MDNode.
505449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != 0)
506449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner    Result = NumberedMetadata[SlotNo];
507449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  else
508449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner    Result = 0;
509449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  return false;
510449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner}
511449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner
5124a72efc0ad18c1b9c98bb2f0cb881a39a7010b53Chris Lattnerbool LLParser::ParseMDNodeID(MDNode *&Result) {
513256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel  // !{ ..., !42, ... }
514256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel  unsigned MID = 0;
515449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  if (ParseMDNodeID(Result, MID)) return true;
516a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
517449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  // If not a forward reference, just return it now.
518449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  if (Result) return false;
519256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel
520449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner  // Otherwise, create MDNode forward reference.
521489b29b0a4ee4526e3d50ad88d3d48745baf5042Dan Gohman  MDNode *FwdNode = MDNode::getTemporary(Context, 0, 0);
522256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel  ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
5230834e6a07b1965712781804b0113a8b282f40a5fChris Lattner
5240834e6a07b1965712781804b0113a8b282f40a5fChris Lattner  if (NumberedMetadata.size() <= MID)
5250834e6a07b1965712781804b0113a8b282f40a5fChris Lattner    NumberedMetadata.resize(MID+1);
5260834e6a07b1965712781804b0113a8b282f40a5fChris Lattner  NumberedMetadata[MID] = FwdNode;
527442ffa1c5688f32b275f7657b5ee3646bfe8ffa3Chris Lattner  Result = FwdNode;
528256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel  return false;
529a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar}
530256be96457faf173de2ac3f8145077b7e6fb41baDevang Patel
53184d03b11d016821e0a2f31fc14a3cfd24b0df862Chris Lattner/// ParseNamedMetadata:
532eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel///   !foo = !{ !1, !2 }
533eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patelbool LLParser::ParseNamedMetadata() {
5341d92831759620a2e5ce4f5a3088c0a1a77a48c8fChris Lattner  assert(Lex.getKind() == lltok::MetadataVar);
535eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel  std::string Name = Lex.getStrVal();
5361d92831759620a2e5ce4f5a3088c0a1a77a48c8fChris Lattner  Lex.Lex();
537eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel
53884d03b11d016821e0a2f31fc14a3cfd24b0df862Chris Lattner  if (ParseToken(lltok::equal, "expected '=' here") ||
539e434d277ca5183eeb6f881000732dcf4c8edd52eChris Lattner      ParseToken(lltok::exclaim, "Expected '!' here") ||
54084d03b11d016821e0a2f31fc14a3cfd24b0df862Chris Lattner      ParseToken(lltok::lbrace, "Expected '{' here"))
541eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel    return true;
542eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel
54317aa92c92a925b4a674440c7ef088c223990e854Dan Gohman  NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
5449dc8ae110f4b23df8f42fe6a09087061938a06a7Dan Gohman  if (Lex.getKind() != lltok::rbrace)
5459dc8ae110f4b23df8f42fe6a09087061938a06a7Dan Gohman    do {
5469dc8ae110f4b23df8f42fe6a09087061938a06a7Dan Gohman      if (ParseToken(lltok::exclaim, "Expected '!' here"))
5479dc8ae110f4b23df8f42fe6a09087061938a06a7Dan Gohman        return true;
548442ffa1c5688f32b275f7657b5ee3646bfe8ffa3Chris Lattner
5499dc8ae110f4b23df8f42fe6a09087061938a06a7Dan Gohman      MDNode *N = 0;
5509dc8ae110f4b23df8f42fe6a09087061938a06a7Dan Gohman      if (ParseMDNodeID(N)) return true;
55117aa92c92a925b4a674440c7ef088c223990e854Dan Gohman      NMD->addOperand(N);
5529dc8ae110f4b23df8f42fe6a09087061938a06a7Dan Gohman    } while (EatIfPresent(lltok::comma));
553eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel
554eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel  if (ParseToken(lltok::rbrace, "expected end of metadata node"))
555eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel    return true;
556eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel
557eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel  return false;
558eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel}
559eff2ab61b5d411fe64ba601d402b7c549644b590Devang Patel
560923078c65d5a37a4f135705300c9feea49487de5Devang Patel/// ParseStandaloneMetadata:
561a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar///   !42 = !{...}
562923078c65d5a37a4f135705300c9feea49487de5Devang Patelbool LLParser::ParseStandaloneMetadata() {
563e434d277ca5183eeb6f881000732dcf4c8edd52eChris Lattner  assert(Lex.getKind() == lltok::exclaim);
564923078c65d5a37a4f135705300c9feea49487de5Devang Patel  Lex.Lex();
565923078c65d5a37a4f135705300c9feea49487de5Devang Patel  unsigned MetadataID = 0;
566923078c65d5a37a4f135705300c9feea49487de5Devang Patel
567923078c65d5a37a4f135705300c9feea49487de5Devang Patel  LocTy TyLoc;
5681d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder Ty(Type::getVoidTy(Context));
569104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel  SmallVector<Value *, 16> Elts;
5703f5132a024960b7d2e226a210c5923c094f036efChris Lattner  if (ParseUInt32(MetadataID) ||
5713f5132a024960b7d2e226a210c5923c094f036efChris Lattner      ParseToken(lltok::equal, "expected '=' here") ||
5723f5132a024960b7d2e226a210c5923c094f036efChris Lattner      ParseType(Ty, TyLoc) ||
573e434d277ca5183eeb6f881000732dcf4c8edd52eChris Lattner      ParseToken(lltok::exclaim, "Expected '!' here") ||
5743f5132a024960b7d2e226a210c5923c094f036efChris Lattner      ParseToken(lltok::lbrace, "Expected '{' here") ||
57524e64df7ec25b55aa872c2ef33728dfbb8c353c4Victor Hernandez      ParseMDNodeVector(Elts, NULL) ||
5763f5132a024960b7d2e226a210c5923c094f036efChris Lattner      ParseToken(lltok::rbrace, "expected end of metadata node"))
577104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel    return true;
578104cf9e02b0ed94d4173869a598af6c6972a8660Devang Patel
579647e3016de18d2fc8b0f233a0b356809e3fdcc54Owen Anderson  MDNode *Init = MDNode::get(Context, Elts.data(), Elts.size());
5800834e6a07b1965712781804b0113a8b282f40a5fChris Lattner
5810834e6a07b1965712781804b0113a8b282f40a5fChris Lattner  // See if this was forward referenced, if so, handle it.
582e80250ec84e6e3aa916a66acc507241e7bde89c9Chris Lattner  std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
5831c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel    FI = ForwardRefMDNodes.find(MetadataID);
5841c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel  if (FI != ForwardRefMDNodes.end()) {
585489b29b0a4ee4526e3d50ad88d3d48745baf5042Dan Gohman    MDNode *Temp = FI->second.first;
586489b29b0a4ee4526e3d50ad88d3d48745baf5042Dan Gohman    Temp->replaceAllUsesWith(Init);
587489b29b0a4ee4526e3d50ad88d3d48745baf5042Dan Gohman    MDNode::deleteTemporary(Temp);
5881c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel    ForwardRefMDNodes.erase(FI);
5890834e6a07b1965712781804b0113a8b282f40a5fChris Lattner
5900834e6a07b1965712781804b0113a8b282f40a5fChris Lattner    assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
5910834e6a07b1965712781804b0113a8b282f40a5fChris Lattner  } else {
5920834e6a07b1965712781804b0113a8b282f40a5fChris Lattner    if (MetadataID >= NumberedMetadata.size())
5930834e6a07b1965712781804b0113a8b282f40a5fChris Lattner      NumberedMetadata.resize(MetadataID+1);
5940834e6a07b1965712781804b0113a8b282f40a5fChris Lattner
5950834e6a07b1965712781804b0113a8b282f40a5fChris Lattner    if (NumberedMetadata[MetadataID] != 0)
5960834e6a07b1965712781804b0113a8b282f40a5fChris Lattner      return TokError("Metadata id is already used");
5970834e6a07b1965712781804b0113a8b282f40a5fChris Lattner    NumberedMetadata[MetadataID] = Init;
5981c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel  }
5991c7eea60d3cbada2bae54c42fa178078a3ace9b3Devang Patel
600923078c65d5a37a4f135705300c9feea49487de5Devang Patel  return false;
601923078c65d5a37a4f135705300c9feea49487de5Devang Patel}
602923078c65d5a37a4f135705300c9feea49487de5Devang Patel
603df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseAlias:
604df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee
605df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Aliasee
606040f758e3abc75bdf6ddb1ce7bea1e1ed1293ccfChris Lattner///   ::= TypeAndValue
607040f758e3abc75bdf6ddb1ce7bea1e1ed1293ccfChris Lattner///   ::= 'bitcast' '(' TypeAndValue 'to' Type ')'
608dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman///   ::= 'getelementptr' 'inbounds'? '(' ... ')'
609df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///
610df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Everything through visibility has already been parsed.
611df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///
612df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
613df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                          unsigned Visibility) {
614df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  assert(Lex.getKind() == lltok::kw_alias);
615df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
616df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned Linkage;
617df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy LinkageLoc = Lex.getLoc();
618df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseOptionalLinkage(Linkage))
619df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
620df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
621df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Linkage != GlobalValue::ExternalLinkage &&
622667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands      Linkage != GlobalValue::WeakAnyLinkage &&
623667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands      Linkage != GlobalValue::WeakODRLinkage &&
624bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola      Linkage != GlobalValue::InternalLinkage &&
6253d10a5a75794356a0a568ce283713adc3a963200Bill Wendling      Linkage != GlobalValue::PrivateLinkage &&
6265e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling      Linkage != GlobalValue::LinkerPrivateLinkage &&
62755ae515f9db484125a23429d4906c5edaf9f10d2Bill Wendling      Linkage != GlobalValue::LinkerPrivateWeakLinkage &&
62855ae515f9db484125a23429d4906c5edaf9f10d2Bill Wendling      Linkage != GlobalValue::LinkerPrivateWeakDefAutoLinkage)
629df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(LinkageLoc, "invalid linkage type for alias");
630a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
631df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Constant *Aliasee;
632df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy AliaseeLoc = Lex.getLoc();
633040f758e3abc75bdf6ddb1ce7bea1e1ed1293ccfChris Lattner  if (Lex.getKind() != lltok::kw_bitcast &&
634040f758e3abc75bdf6ddb1ce7bea1e1ed1293ccfChris Lattner      Lex.getKind() != lltok::kw_getelementptr) {
635df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseGlobalTypeAndValue(Aliasee)) return true;
636df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  } else {
637df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // The bitcast dest type is not present, it is implied by the dest type.
638df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ValID ID;
639df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseValID(ID)) return true;
640df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ID.Kind != ValID::t_Constant)
641df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(AliaseeLoc, "invalid aliasee");
642df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Aliasee = ID.ConstantVal;
643df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
644a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
6451df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!Aliasee->getType()->isPointerTy())
646df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(AliaseeLoc, "alias must have pointer type");
647df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
648df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Okay, create the alias but do not insert it into the module yet.
649df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  GlobalAlias* GA = new GlobalAlias(Aliasee->getType(),
650df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                    (GlobalValue::LinkageTypes)Linkage, Name,
651df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                    Aliasee);
652df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
653a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
654df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // See if this value already exists in the symbol table.  If so, it is either
655df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // a redefinition or a definition of a forward reference.
6561d871c5e547996a8dd5192e48d22aadf11cd2a9dChris Lattner  if (GlobalValue *Val = M->getNamedValue(Name)) {
657df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // See if this was a redefinition.  If so, there is no entry in
658df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // ForwardRefVals.
659df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
660df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      I = ForwardRefVals.find(Name);
661df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (I == ForwardRefVals.end())
662df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(NameLoc, "redefinition of global named '@" + Name + "'");
663df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
664df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // Otherwise, this was a definition of forward ref.  Verify that types
665df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // agree.
666df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Val->getType() != GA->getType())
667df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(NameLoc,
668df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner              "forward reference and definition of alias have different types");
669a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
670df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // If they agree, just RAUW the old value with the alias and remove the
671df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // forward ref info.
672df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Val->replaceAllUsesWith(GA);
673df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Val->eraseFromParent();
674df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ForwardRefVals.erase(I);
675df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
676a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
677df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Insert into the module, we know its name won't collide now.
678df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  M->getAliasList().push_back(GA);
679df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  assert(GA->getNameStr() == Name && "Should not be a name conflict!");
680a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
681df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
682df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
683df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
684df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseGlobal
685df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalThreadLocal
686df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///       OptionalAddrSpace GlobalType Type Const
687df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
688df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///       OptionalAddrSpace GlobalType Type Const
689df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///
690df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// Everything through visibility has been parsed already.
691df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///
692df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
693df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                           unsigned Linkage, bool HasLinkage,
694df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                           unsigned Visibility) {
695df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned AddrSpace;
696df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  bool ThreadLocal, IsConstant;
697df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy TyLoc;
698a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
6991d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder Ty(Type::getVoidTy(Context));
700df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) ||
701df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseOptionalAddrSpace(AddrSpace) ||
702df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseGlobalType(IsConstant) ||
703df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseType(Ty, TyLoc))
704df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
705a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
706df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If the linkage is specified and is external, then no initializer is
707df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // present.
708df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Constant *Init = 0;
709df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!HasLinkage || (Linkage != GlobalValue::DLLImportLinkage &&
7105f4ee1fc5d00ae55c30fa2ce450c69be4c6d6e63Duncan Sands                      Linkage != GlobalValue::ExternalWeakLinkage &&
711df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                      Linkage != GlobalValue::ExternalLinkage)) {
712df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseGlobalValue(Ty, Init))
713df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
714df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
715df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
7161df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (Ty->isFunctionTy() || Ty->isLabelTy())
7174a2f112a5c774cb1ab3fd890a88ba1db9709f561Chris Lattner    return Error(TyLoc, "invalid type for global variable");
718a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
719df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  GlobalVariable *GV = 0;
720df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
721df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // See if the global was forward referenced, if so, use the global.
72291dad87ddc1e69ac5fa8953a583de4185e1dd0d7Chris Lattner  if (!Name.empty()) {
7231d871c5e547996a8dd5192e48d22aadf11cd2a9dChris Lattner    if (GlobalValue *GVal = M->getNamedValue(Name)) {
7241d871c5e547996a8dd5192e48d22aadf11cd2a9dChris Lattner      if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
7251d871c5e547996a8dd5192e48d22aadf11cd2a9dChris Lattner        return Error(NameLoc, "redefinition of global '@" + Name + "'");
7261d871c5e547996a8dd5192e48d22aadf11cd2a9dChris Lattner      GV = cast<GlobalVariable>(GVal);
7271d871c5e547996a8dd5192e48d22aadf11cd2a9dChris Lattner    }
728df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  } else {
729df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
730df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      I = ForwardRefValIDs.find(NumberedVals.size());
731df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (I != ForwardRefValIDs.end()) {
732df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      GV = cast<GlobalVariable>(I->second.first);
733df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ForwardRefValIDs.erase(I);
734df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
735df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
736df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
737df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (GV == 0) {
738a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar    GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, 0,
739e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson                            Name, 0, false, AddrSpace);
740df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  } else {
741df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (GV->getType()->getElementType() != Ty)
742df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(TyLoc,
743df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner            "forward reference and definition of global have different types");
744a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
745df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // Move the forward-reference to the correct spot in the module.
746df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
747df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
748df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
749df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Name.empty())
750df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    NumberedVals.push_back(GV);
751a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
752df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Set the parsed properties on the global.
753df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Init)
754df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    GV->setInitializer(Init);
755df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  GV->setConstant(IsConstant);
756df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
757df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
758df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  GV->setThreadLocal(ThreadLocal);
759a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
760df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Parse attributes on the global.
761df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  while (Lex.getKind() == lltok::comma) {
762df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
763a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
764df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Lex.getKind() == lltok::kw_section) {
765df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Lex.Lex();
766df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      GV->setSection(Lex.getStrVal());
767df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (ParseToken(lltok::StringConstant, "expected global section string"))
768df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return true;
769df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    } else if (Lex.getKind() == lltok::kw_align) {
770df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      unsigned Alignment;
771df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (ParseOptionalAlignment(Alignment)) return true;
772df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      GV->setAlignment(Alignment);
773df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    } else {
774df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      TokError("unknown global variable property!");
775df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
776df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
777a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
778df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
779df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
780df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
781df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
782df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
783df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// GlobalValue Reference/Resolution Routines.
784df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
785df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
786df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GetGlobalVal - Get a value with the specified name or ID, creating a
787df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// forward reference record if needed.  This can return null if the value
788df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// exists but does not have the right type.
789df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerGlobalValue *LLParser::GetGlobalVal(const std::string &Name, const Type *Ty,
790df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                    LocTy Loc) {
791df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  const PointerType *PTy = dyn_cast<PointerType>(Ty);
792df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (PTy == 0) {
793df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Error(Loc, "global variable reference must have pointer type");
794df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return 0;
795df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
796a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
797df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Look this name up in the normal function symbol table.
798df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  GlobalValue *Val =
799df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
800a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
801df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If this is a forward reference for the value, see if we already created a
802df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // forward ref record.
803df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Val == 0) {
804df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
805df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      I = ForwardRefVals.find(Name);
806df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (I != ForwardRefVals.end())
807df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Val = I->second.first;
808df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
809a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
810df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If we have the value in the symbol table or fwd-ref table, return it.
811df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Val) {
812df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Val->getType() == Ty) return Val;
813df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Error(Loc, "'@" + Name + "' defined with type '" +
814df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner          Val->getType()->getDescription() + "'");
815df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return 0;
816df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
817a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
818df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Otherwise, create a new forward reference for this value and remember it.
819df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  GlobalValue *FwdVal;
8201e407c338f6944242facb1e0c32903162cc366f0Chris Lattner  if (const FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) {
8211e407c338f6944242facb1e0c32903162cc366f0Chris Lattner    // Function types can return opaque but functions can't.
82247c5188789bc40671504ed1fa3a44765cefba44fDuncan Sands    if (FT->getReturnType()->isOpaqueTy()) {
8231e407c338f6944242facb1e0c32903162cc366f0Chris Lattner      Error(Loc, "function may not return opaque type");
8241e407c338f6944242facb1e0c32903162cc366f0Chris Lattner      return 0;
8251e407c338f6944242facb1e0c32903162cc366f0Chris Lattner    }
826a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
8275f4ee1fc5d00ae55c30fa2ce450c69be4c6d6e63Duncan Sands    FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
8281e407c338f6944242facb1e0c32903162cc366f0Chris Lattner  } else {
829e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson    FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
830e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson                                GlobalValue::ExternalWeakLinkage, 0, Name);
8311e407c338f6944242facb1e0c32903162cc366f0Chris Lattner  }
832a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
833df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
834df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return FwdVal;
835df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
836df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
837df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerGlobalValue *LLParser::GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc) {
838df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  const PointerType *PTy = dyn_cast<PointerType>(Ty);
839df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (PTy == 0) {
840df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Error(Loc, "global variable reference must have pointer type");
841df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return 0;
842df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
843a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
844df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
845a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
846df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If this is a forward reference for the value, see if we already created a
847df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // forward ref record.
848df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Val == 0) {
849df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
850df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      I = ForwardRefValIDs.find(ID);
851df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (I != ForwardRefValIDs.end())
852df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Val = I->second.first;
853df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
854a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
855df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If we have the value in the symbol table or fwd-ref table, return it.
856df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Val) {
857df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Val->getType() == Ty) return Val;
858df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Error(Loc, "'@" + utostr(ID) + "' defined with type '" +
859df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner          Val->getType()->getDescription() + "'");
860df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return 0;
861df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
862a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
863df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Otherwise, create a new forward reference for this value and remember it.
864df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  GlobalValue *FwdVal;
865830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner  if (const FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) {
866830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner    // Function types can return opaque but functions can't.
86747c5188789bc40671504ed1fa3a44765cefba44fDuncan Sands    if (FT->getReturnType()->isOpaqueTy()) {
8680d8484f2edb1069522f5c3de6467433e0fbe9323Chris Lattner      Error(Loc, "function may not return opaque type");
869830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner      return 0;
870830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner    }
8715f4ee1fc5d00ae55c30fa2ce450c69be4c6d6e63Duncan Sands    FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
872830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner  } else {
873e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson    FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
874e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson                                GlobalValue::ExternalWeakLinkage, 0, "");
875830703b1bd88511e29eec76b1fd146524bc9c5a1Chris Lattner  }
876a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
877df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
878df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return FwdVal;
879df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
880df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
881df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
882df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
883df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Helper Routines.
884df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
885df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
886df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseToken - If the current token has the specified kind, eat it and return
887df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// success.  Otherwise, emit the specified error and return failure.
888df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
889df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Lex.getKind() != T)
890df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return TokError(ErrMsg);
891df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
892df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
893df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
894df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
8953ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ParseStringConstant
8963ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner///   ::= StringConstant
8973ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattnerbool LLParser::ParseStringConstant(std::string &Result) {
8983ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (Lex.getKind() != lltok::StringConstant)
8993ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    return TokError("expected string constant");
9003ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  Result = Lex.getStrVal();
9013ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  Lex.Lex();
9023ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  return false;
9033ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner}
9043ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner
9053ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner/// ParseUInt32
9063ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner///   ::= uint32
9073ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattnerbool LLParser::ParseUInt32(unsigned &Val) {
908df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
909df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return TokError("expected integer");
910df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
911df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Val64 != unsigned(Val64))
912df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return TokError("expected 32-bit integer (too large)");
913df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Val = Val64;
914df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
915df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
916df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
917df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
918df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
919df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalAddrSpace
920df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   := /*empty*/
921df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   := 'addrspace' '(' uint32 ')'
922df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
923df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  AddrSpace = 0;
9243ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (!EatIfPresent(lltok::kw_addrspace))
925df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
926df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return ParseToken(lltok::lparen, "expected '(' in address space") ||
9273ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner         ParseUInt32(AddrSpace) ||
928df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner         ParseToken(lltok::rparen, "expected ')' in address space");
929a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar}
930df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
931df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalAttrs - Parse a potentially empty attribute list.  AttrKind
932df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// indicates what kind of attribute list this is: 0: function arg, 1: result,
933df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// 2: function attr.
934ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner/// 3: function arg after value: FIXME: REMOVE IN LLVM 3.0
935df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind) {
936df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Attrs = Attribute::None;
937df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy AttrLoc = Lex.getLoc();
938a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
939df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  while (1) {
940df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    switch (Lex.getKind()) {
941df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_sext:
942df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_zext:
943ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner      // Treat these as signext/zeroext if they occur in the argument list after
944ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner      // the value, as in "call i8 @foo(i8 10 sext)".  If they occur before the
945ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner      // value, as in "call i8 @foo(i8 sext (" then it is part of a constant
946ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner      // expr.
947df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // FIXME: REMOVE THIS IN LLVM 3.0
948ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner      if (AttrKind == 3) {
949df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        if (Lex.getKind() == lltok::kw_sext)
950df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner          Attrs |= Attribute::SExt;
951df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        else
952df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner          Attrs |= Attribute::ZExt;
953df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        break;
954df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      }
955df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // FALL THROUGH.
956df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    default:  // End of attributes.
957df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (AttrKind != 2 && (Attrs & Attribute::FunctionOnly))
958df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(AttrLoc, "invalid use of function-only attribute");
959a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
960ad9ad7c827b585c018288c89d611d8c97d8d2e7dChris Lattner      if (AttrKind != 0 && AttrKind != 3 && (Attrs & Attribute::ParameterOnly))
961df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(AttrLoc, "invalid use of parameter-only attribute");
962a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
963df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return false;
964578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_zeroext:         Attrs |= Attribute::ZExt; break;
965578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_signext:         Attrs |= Attribute::SExt; break;
966578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_inreg:           Attrs |= Attribute::InReg; break;
967578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_sret:            Attrs |= Attribute::StructRet; break;
968578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_noalias:         Attrs |= Attribute::NoAlias; break;
969578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_nocapture:       Attrs |= Attribute::NoCapture; break;
970578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_byval:           Attrs |= Attribute::ByVal; break;
971578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_nest:            Attrs |= Attribute::Nest; break;
972578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel
973578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_noreturn:        Attrs |= Attribute::NoReturn; break;
974578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_nounwind:        Attrs |= Attribute::NoUnwind; break;
975578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_noinline:        Attrs |= Attribute::NoInline; break;
976578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_readnone:        Attrs |= Attribute::ReadNone; break;
977578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_readonly:        Attrs |= Attribute::ReadOnly; break;
978570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen    case lltok::kw_inlinehint:      Attrs |= Attribute::InlineHint; break;
979578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_alwaysinline:    Attrs |= Attribute::AlwaysInline; break;
980578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_optsize:         Attrs |= Attribute::OptimizeForSize; break;
981578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_ssp:             Attrs |= Attribute::StackProtect; break;
982578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_sspreq:          Attrs |= Attribute::StackProtectReq; break;
983578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_noredzone:       Attrs |= Attribute::NoRedZone; break;
984578efa920abd218ba75a0fb3c9b8398f4c0a774bDevang Patel    case lltok::kw_noimplicitfloat: Attrs |= Attribute::NoImplicitFloat; break;
985c5ec8a78ea898087ad361e5b755f74a76150e5fdAnton Korobeynikov    case lltok::kw_naked:           Attrs |= Attribute::Naked; break;
986a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
9871e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis    case lltok::kw_alignstack: {
9881e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis      unsigned Alignment;
9891e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis      if (ParseOptionalStackAlignment(Alignment))
9901e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis        return true;
9911e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis      Attrs |= Attribute::constructStackAlignmentFromInt(Alignment);
9921e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis      continue;
9931e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis    }
9941e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis
995df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_align: {
996df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      unsigned Alignment;
997df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (ParseOptionalAlignment(Alignment))
998df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return true;
999df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Attrs |= Attribute::constructAlignmentFromInt(Alignment);
1000df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      continue;
1001df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
10021e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis
1003df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
1004df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
1005df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1006df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1007df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1008df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalLinkage
1009df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= /*empty*/
1010bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola///   ::= 'private'
10113d10a5a75794356a0a568ce283713adc3a963200Bill Wendling///   ::= 'linker_private'
10125e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling///   ::= 'linker_private_weak'
101355ae515f9db484125a23429d4906c5edaf9f10d2Bill Wendling///   ::= 'linker_private_weak_def_auto'
1014df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'internal'
1015df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'weak'
1016667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands///   ::= 'weak_odr'
1017df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'linkonce'
1018667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands///   ::= 'linkonce_odr'
10195e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling///   ::= 'available_externally'
1020df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'appending'
1021df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'dllexport'
1022df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'common'
1023df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'dllimport'
1024df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'extern_weak'
1025df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'external'
1026df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1027df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  HasLinkage = false;
1028df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  switch (Lex.getKind()) {
10293d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  default:                       Res=GlobalValue::ExternalLinkage; return false;
10303d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_private:        Res = GlobalValue::PrivateLinkage;       break;
10313d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_linker_private: Res = GlobalValue::LinkerPrivateLinkage; break;
10325e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling  case lltok::kw_linker_private_weak:
10335e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    Res = GlobalValue::LinkerPrivateWeakLinkage;
10345e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling    break;
103555ae515f9db484125a23429d4906c5edaf9f10d2Bill Wendling  case lltok::kw_linker_private_weak_def_auto:
103655ae515f9db484125a23429d4906c5edaf9f10d2Bill Wendling    Res = GlobalValue::LinkerPrivateWeakDefAutoLinkage;
103755ae515f9db484125a23429d4906c5edaf9f10d2Bill Wendling    break;
10383d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_internal:       Res = GlobalValue::InternalLinkage;      break;
10393d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_weak:           Res = GlobalValue::WeakAnyLinkage;       break;
10403d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_weak_odr:       Res = GlobalValue::WeakODRLinkage;       break;
10413d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_linkonce:       Res = GlobalValue::LinkOnceAnyLinkage;   break;
10423d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_linkonce_odr:   Res = GlobalValue::LinkOnceODRLinkage;   break;
1043266c7bbbbcc4b326dea82e577de1a415d6acc23eChris Lattner  case lltok::kw_available_externally:
1044266c7bbbbcc4b326dea82e577de1a415d6acc23eChris Lattner    Res = GlobalValue::AvailableExternallyLinkage;
1045266c7bbbbcc4b326dea82e577de1a415d6acc23eChris Lattner    break;
10463d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_appending:      Res = GlobalValue::AppendingLinkage;     break;
10473d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_dllexport:      Res = GlobalValue::DLLExportLinkage;     break;
10483d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_common:         Res = GlobalValue::CommonLinkage;        break;
10493d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_dllimport:      Res = GlobalValue::DLLImportLinkage;     break;
10503d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_extern_weak:    Res = GlobalValue::ExternalWeakLinkage;  break;
10513d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case lltok::kw_external:       Res = GlobalValue::ExternalLinkage;      break;
1052df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1053df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
1054df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  HasLinkage = true;
1055df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
1056df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1057df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1058df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalVisibility
1059df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= /*empty*/
1060df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'default'
1061df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'hidden'
1062df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'protected'
1063a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar///
1064df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalVisibility(unsigned &Res) {
1065df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  switch (Lex.getKind()) {
1066df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  default:                  Res = GlobalValue::DefaultVisibility; return false;
1067df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_default:   Res = GlobalValue::DefaultVisibility; break;
1068df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_hidden:    Res = GlobalValue::HiddenVisibility; break;
1069df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1070df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1071df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
1072df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
1073df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1074df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1075df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalCallingConv
1076df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= /*empty*/
1077df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'ccc'
1078df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'fastcc'
1079df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'coldcc'
1080df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'x86_stdcallcc'
1081df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'x86_fastcallcc'
1082ded05e34b65dc42998e9db6ca1abd513e7a9d120Anton Korobeynikov///   ::= 'x86_thiscallcc'
1083385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov///   ::= 'arm_apcscc'
1084385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov///   ::= 'arm_aapcscc'
1085385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov///   ::= 'arm_aapcs_vfpcc'
1086211a14e476abc9b864ab6a5d5e0bbb86d288b650Anton Korobeynikov///   ::= 'msp430_intrcc'
1087df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'cc' UINT
1088385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov///
108965c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patelbool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
1090df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  switch (Lex.getKind()) {
1091df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  default:                       CC = CallingConv::C; return false;
1092df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_ccc:            CC = CallingConv::C; break;
1093df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_fastcc:         CC = CallingConv::Fast; break;
1094df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_coldcc:         CC = CallingConv::Cold; break;
1095df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_x86_stdcallcc:  CC = CallingConv::X86_StdCall; break;
1096df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
1097ded05e34b65dc42998e9db6ca1abd513e7a9d120Anton Korobeynikov  case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
1098385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov  case lltok::kw_arm_apcscc:     CC = CallingConv::ARM_APCS; break;
1099385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov  case lltok::kw_arm_aapcscc:    CC = CallingConv::ARM_AAPCS; break;
1100385f5a99ecc7fee48a7539bc63d3e1d3b5089c0dAnton Korobeynikov  case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
1101211a14e476abc9b864ab6a5d5e0bbb86d288b650Anton Korobeynikov  case lltok::kw_msp430_intrcc:  CC = CallingConv::MSP430_INTR; break;
110265c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel  case lltok::kw_cc: {
110365c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel      unsigned ArbitraryCC;
110465c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel      Lex.Lex();
110565c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel      if (ParseUInt32(ArbitraryCC)) {
110665c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel        return true;
110765c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel      } else
110865c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel        CC = static_cast<CallingConv::ID>(ArbitraryCC);
110965c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel        return false;
111065c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel    }
111165c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel    break;
1112df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1113a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1114df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
1115df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
1116df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1117df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1118b8c4686a46da2832ff4502136f16f71f789be078Chris Lattner/// ParseInstructionMetadata
11193f3a0f6b498e80ffaeb83f03c8697e5d54fd9337Chris Lattner///   ::= !dbg !42 (',' !dbg !57)*
11209d072f51ebf61edda9c31f831c2642d110170d13Dan Gohmanbool LLParser::ParseInstructionMetadata(Instruction *Inst,
11219d072f51ebf61edda9c31f831c2642d110170d13Dan Gohman                                        PerFunctionState *PFS) {
1122b8c4686a46da2832ff4502136f16f71f789be078Chris Lattner  do {
1123b8c4686a46da2832ff4502136f16f71f789be078Chris Lattner    if (Lex.getKind() != lltok::MetadataVar)
1124b8c4686a46da2832ff4502136f16f71f789be078Chris Lattner      return TokError("expected metadata after comma");
11250475c9170e9621292a48c688c3976031be5650bbDevang Patel
11263f3a0f6b498e80ffaeb83f03c8697e5d54fd9337Chris Lattner    std::string Name = Lex.getStrVal();
1127309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman    unsigned MDK = M->getMDKindID(Name.c_str());
11283f3a0f6b498e80ffaeb83f03c8697e5d54fd9337Chris Lattner    Lex.Lex();
112952e2031d4a5d76ff37458015a086285dfaceb6d2Chris Lattner
1130442ffa1c5688f32b275f7657b5ee3646bfe8ffa3Chris Lattner    MDNode *Node;
1131449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner    unsigned NodeID;
1132449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner    SMLoc Loc = Lex.getLoc();
1133309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman
1134309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman    if (ParseToken(lltok::exclaim, "expected '!' here"))
1135e434d277ca5183eeb6f881000732dcf4c8edd52eChris Lattner      return true;
1136a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1137309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman    if (Lex.getKind() == lltok::lbrace) {
1138309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman      ValID ID;
1139309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman      if (ParseMetadataListValue(ID, PFS))
1140309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman        return true;
1141309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman      assert(ID.Kind == ValID::t_MDNode);
1142309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman      Inst->setMetadata(MDK, ID.MDNodeVal);
1143449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner    } else {
1144309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman      if (ParseMDNodeID(Node, NodeID))
1145309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman        return true;
1146309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman      if (Node) {
1147309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman        // If we got the node, add it to the instruction.
1148309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman        Inst->setMetadata(MDK, Node);
1149309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman      } else {
1150309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman        MDRef R = { Loc, MDK, NodeID };
1151309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman        // Otherwise, remember that this should be resolved later.
1152309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman        ForwardRefInstMetadata[Inst].push_back(R);
1153309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman      }
1154449c3103c5d8a3105ac1a46187ac1c7e1bdc0ba2Chris Lattner    }
11553f3a0f6b498e80ffaeb83f03c8697e5d54fd9337Chris Lattner
11563f3a0f6b498e80ffaeb83f03c8697e5d54fd9337Chris Lattner    // If this is the end of the list, we're done.
1157b8c4686a46da2832ff4502136f16f71f789be078Chris Lattner  } while (EatIfPresent(lltok::comma));
1158b8c4686a46da2832ff4502136f16f71f789be078Chris Lattner  return false;
1159f633a065de959b32892c51d741d4db010c5d25b1Devang Patel}
1160f633a065de959b32892c51d741d4db010c5d25b1Devang Patel
1161df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseOptionalAlignment
1162df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= /* empty */
1163df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'align' 4
1164df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1165df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Alignment = 0;
11663ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (!EatIfPresent(lltok::kw_align))
11673ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    return false;
11683fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner  LocTy AlignLoc = Lex.getLoc();
11693fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner  if (ParseUInt32(Alignment)) return true;
11703fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner  if (!isPowerOf2_32(Alignment))
11713fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner    return Error(AlignLoc, "alignment is not a power of two");
1172e16829b401409b398c9de9847c1d12eb931f7d63Dan Gohman  if (Alignment > Value::MaximumAlignment)
1173138aa2a82bc6de611f28e51332fb0a30262a58e3Dan Gohman    return Error(AlignLoc, "huge alignments are not supported yet");
11743fbb3ab14d86f752a933b5af4e6728c5e57e1155Chris Lattner  return false;
1175df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1176df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1177c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner/// ParseOptionalCommaAlign
1178c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner///   ::=
1179c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner///   ::= ',' align 4
1180c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner///
1181c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner/// This returns with AteExtraComma set to true if it ate an excess comma at the
1182c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner/// end.
1183c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattnerbool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1184c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner                                       bool &AteExtraComma) {
1185c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner  AteExtraComma = false;
1186c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner  while (EatIfPresent(lltok::comma)) {
1187c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner    // Metadata at the end is an early exit.
11881d92831759620a2e5ce4f5a3088c0a1a77a48c8fChris Lattner    if (Lex.getKind() == lltok::MetadataVar) {
1189c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner      AteExtraComma = true;
1190c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner      return false;
1191c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner    }
1192c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner
1193093eed15ae690f26f3f23731eed9d34da978d5feChris Lattner    if (Lex.getKind() != lltok::kw_align)
1194093eed15ae690f26f3f23731eed9d34da978d5feChris Lattner      return Error(Lex.getLoc(), "expected metadata or 'align'");
1195093eed15ae690f26f3f23731eed9d34da978d5feChris Lattner
1196138aa2a82bc6de611f28e51332fb0a30262a58e3Dan Gohman    LocTy AlignLoc = Lex.getLoc();
1197093eed15ae690f26f3f23731eed9d34da978d5feChris Lattner    if (ParseOptionalAlignment(Alignment)) return true;
1198c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner  }
1199a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1200f633a065de959b32892c51d741d4db010c5d25b1Devang Patel  return false;
1201df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1202df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
12031e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis/// ParseOptionalStackAlignment
12041e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis///   ::= /* empty */
12051e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis///   ::= 'alignstack' '(' 4 ')'
12061e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davisbool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
12071e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis  Alignment = 0;
12081e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis  if (!EatIfPresent(lltok::kw_alignstack))
12091e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis    return false;
12101e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis  LocTy ParenLoc = Lex.getLoc();
12111e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis  if (!EatIfPresent(lltok::lparen))
12121e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis    return Error(ParenLoc, "expected '('");
12131e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis  LocTy AlignLoc = Lex.getLoc();
12141e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis  if (ParseUInt32(Alignment)) return true;
12151e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis  ParenLoc = Lex.getLoc();
12161e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis  if (!EatIfPresent(lltok::rparen))
12171e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis    return Error(ParenLoc, "expected ')'");
12181e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis  if (!isPowerOf2_32(Alignment))
12191e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis    return Error(AlignLoc, "stack alignment is not a power of two");
12201e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis  return false;
12211e063d14df0f182626ebdd7ac7f32405aa754e03Charles Davis}
1222f633a065de959b32892c51d741d4db010c5d25b1Devang Patel
1223628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner/// ParseIndexList - This parses the index list for an insert/extractvalue
1224628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner/// instruction.  This sets AteExtraComma in the case where we eat an extra
1225628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner/// comma at the end of the line and find that it is followed by metadata.
1226628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner/// Clients that don't allow metadata can call the version of this function that
1227628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner/// only takes one argument.
1228628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner///
1229df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseIndexList
1230df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///    ::=  (',' uint32)+
1231628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner///
1232628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattnerbool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1233628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner                              bool &AteExtraComma) {
1234628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner  AteExtraComma = false;
1235628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner
1236df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Lex.getKind() != lltok::comma)
1237df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return TokError("expected ',' as start of index list");
1238a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
12393ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  while (EatIfPresent(lltok::comma)) {
1240628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner    if (Lex.getKind() == lltok::MetadataVar) {
1241628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner      AteExtraComma = true;
1242628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner      return false;
1243628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner    }
1244df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    unsigned Idx;
12453ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    if (ParseUInt32(Idx)) return true;
1246df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Indices.push_back(Idx);
1247df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1248a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1249df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
1250df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1251df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1252df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
1253df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Type Parsing.
1254df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
1255df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1256df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseType - Parse and resolve a full type.
1257a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattnerbool LLParser::ParseType(PATypeHolder &Result, bool AllowVoid) {
1258a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner  LocTy TypeLoc = Lex.getLoc();
1259df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeRec(Result)) return true;
1260a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1261df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Verify no unresolved uprefs.
1262df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!UpRefs.empty())
1263df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(UpRefs.back().Loc, "invalid unresolved type up reference");
1264a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1265cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner  if (!AllowVoid && Result.get()->isVoidTy())
1266a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner    return Error(TypeLoc, "void type only allowed for function results");
1267a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1268df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
1269df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1270df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1271df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// HandleUpRefs - Every time we finish a new layer of types, this function is
1272df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// called.  It loops through the UpRefs vector, which is a list of the
1273df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// currently active types.  For each type, if the up-reference is contained in
1274df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// the newly completed type, we decrement the level count.  When the level
1275df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// count reaches zero, the up-referenced type is the type that is passed in:
1276df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// thus we can complete the cycle.
1277df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///
1278df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerPATypeHolder LLParser::HandleUpRefs(const Type *ty) {
1279df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If Ty isn't abstract, or if there are no up-references in it, then there is
1280df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // nothing to resolve here.
1281df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!ty->isAbstract() || UpRefs.empty()) return ty;
1282a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1283df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  PATypeHolder Ty(ty);
1284df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#if 0
12850e28d76696dce146266cab547033dd895ffd0323David Greene  dbgs() << "Type '" << Ty->getDescription()
1286df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner         << "' newly formed.  Resolving upreferences.\n"
1287df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner         << UpRefs.size() << " upreferences active!\n";
1288df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#endif
1289a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1290df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
1291df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // to zero), we resolve them all together before we resolve them to Ty.  At
1292df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // the end of the loop, if there is anything to resolve to Ty, it will be in
1293df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // this variable.
1294df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  OpaqueType *TypeToResolve = 0;
1295a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1296df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (unsigned i = 0; i != UpRefs.size(); ++i) {
1297df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // Determine if 'Ty' directly contains this up-references 'LastContainedTy'.
1298df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    bool ContainsType =
1299df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      std::find(Ty->subtype_begin(), Ty->subtype_end(),
1300df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                UpRefs[i].LastContainedTy) != Ty->subtype_end();
1301a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1302df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#if 0
13030e28d76696dce146266cab547033dd895ffd0323David Greene    dbgs() << "  UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
1304df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner           << UpRefs[i].LastContainedTy->getDescription() << ") = "
1305df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner           << (ContainsType ? "true" : "false")
1306df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner           << " level=" << UpRefs[i].NestingLevel << "\n";
1307df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#endif
1308df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!ContainsType)
1309df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      continue;
1310a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1311df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // Decrement level of upreference
1312df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    unsigned Level = --UpRefs[i].NestingLevel;
1313df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    UpRefs[i].LastContainedTy = Ty;
1314a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1315df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // If the Up-reference has a non-zero level, it shouldn't be resolved yet.
1316df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Level != 0)
1317df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      continue;
1318a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1319df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#if 0
13200e28d76696dce146266cab547033dd895ffd0323David Greene    dbgs() << "  * Resolving upreference for " << UpRefs[i].UpRefTy << "\n";
1321df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner#endif
1322df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!TypeToResolve)
1323df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      TypeToResolve = UpRefs[i].UpRefTy;
1324df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    else
1325df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
1326df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    UpRefs.erase(UpRefs.begin()+i);     // Remove from upreference list.
1327df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    --i;                                // Do not skip the next element.
1328df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1329a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1330df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (TypeToResolve)
1331df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    TypeToResolve->refineAbstractTypeTo(Ty);
1332a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1333df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return Ty;
1334df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1335df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1336df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1337df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseTypeRec - The recursive function used to process the internal
1338df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// implementation details of types.
1339df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseTypeRec(PATypeHolder &Result) {
1340df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  switch (Lex.getKind()) {
1341df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  default:
1342df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return TokError("expected type");
1343df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::Type:
1344df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // TypeRec ::= 'float' | 'void' (etc)
1345df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Result = Lex.getTyVal();
1346a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar    Lex.Lex();
1347df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
1348df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_opaque:
1349df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // TypeRec ::= 'opaque'
13500e275dc53880a7f14f8b8c83cc6e0290a215492dOwen Anderson    Result = OpaqueType::get(Context);
1351df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
1352df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
1353df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::lbrace:
1354df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // TypeRec ::= '{' ... '}'
1355df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseStructType(Result, false))
1356df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
1357df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
1358fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  case lltok::kw_union:
1359fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    // TypeRec ::= 'union' '{' ... '}'
1360fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    if (ParseUnionType(Result))
1361fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner      return true;
1362fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    break;
1363df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::lsquare:
1364df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // TypeRec ::= '[' ... ']'
1365df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex(); // eat the lsquare.
1366df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseArrayVectorType(Result, false))
1367df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
1368df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
1369df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::less: // Either vector or packed struct.
1370df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // TypeRec ::= '<' ... '>'
13713ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    Lex.Lex();
13723ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    if (Lex.getKind() == lltok::lbrace) {
13733ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      if (ParseStructType(Result, true) ||
13743ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner          ParseToken(lltok::greater, "expected '>' at end of packed struct"))
1375df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return true;
1376df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    } else if (ParseArrayVectorType(Result, true))
1377df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
1378df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
1379df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::LocalVar:
1380df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::StringConstant:  // FIXME: REMOVE IN LLVM 3.0
1381df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // TypeRec ::= %foo
1382df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (const Type *T = M->getTypeByName(Lex.getStrVal())) {
1383df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Result = T;
1384df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    } else {
13850e275dc53880a7f14f8b8c83cc6e0290a215492dOwen Anderson      Result = OpaqueType::get(Context);
1386df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ForwardRefTypes.insert(std::make_pair(Lex.getStrVal(),
1387df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                            std::make_pair(Result,
1388df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                                           Lex.getLoc())));
1389df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      M->addTypeName(Lex.getStrVal(), Result.get());
1390df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
1391df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
1392df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
1393a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1394df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::LocalVarID:
1395df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // TypeRec ::= %4
1396df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Lex.getUIntVal() < NumberedTypes.size())
1397df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Result = NumberedTypes[Lex.getUIntVal()];
1398df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    else {
1399df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator
1400df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        I = ForwardRefTypeIDs.find(Lex.getUIntVal());
1401df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (I != ForwardRefTypeIDs.end())
1402df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        Result = I->second.first;
1403df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      else {
14040e275dc53880a7f14f8b8c83cc6e0290a215492dOwen Anderson        Result = OpaqueType::get(Context);
1405df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ForwardRefTypeIDs.insert(std::make_pair(Lex.getUIntVal(),
1406df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                                std::make_pair(Result,
1407df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                                               Lex.getLoc())));
1408df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      }
1409df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
1410df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
1411df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
1412df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::backslash: {
1413df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // TypeRec ::= '\' 4
1414df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
14153ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    unsigned Val;
14163ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    if (ParseUInt32(Val)) return true;
14170e275dc53880a7f14f8b8c83cc6e0290a215492dOwen Anderson    OpaqueType *OT = OpaqueType::get(Context); //Use temporary placeholder.
1418df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    UpRefs.push_back(UpRefRecord(Lex.getLoc(), Val, OT));
1419df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Result = OT;
1420df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
1421df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1422df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1423a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1424a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar  // Parse the type suffixes.
1425df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  while (1) {
1426df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    switch (Lex.getKind()) {
1427df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // End of type.
1428a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar    default: return false;
1429df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1430df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // TypeRec ::= TypeRec '*'
1431df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::star:
1432cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner      if (Result.get()->isLabelTy())
1433df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return TokError("basic block pointers are invalid");
1434cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner      if (Result.get()->isVoidTy())
1435b9070d3f6309929e47503e2790a368592b4bd201Dan Gohman        return TokError("pointers to void are invalid; use i8* instead");
1436a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky      if (!PointerType::isValidElementType(Result.get()))
1437a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky        return TokError("pointer to this type is invalid");
1438debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson      Result = HandleUpRefs(PointerType::getUnqual(Result.get()));
1439df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Lex.Lex();
1440df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      break;
1441df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1442df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // TypeRec ::= TypeRec 'addrspace' '(' uint32 ')' '*'
1443df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_addrspace: {
1444cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner      if (Result.get()->isLabelTy())
1445df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return TokError("basic block pointers are invalid");
1446cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner      if (Result.get()->isVoidTy())
1447b9070d3f6309929e47503e2790a368592b4bd201Dan Gohman        return TokError("pointers to void are invalid; use i8* instead");
1448a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky      if (!PointerType::isValidElementType(Result.get()))
1449a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky        return TokError("pointer to this type is invalid");
1450df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      unsigned AddrSpace;
1451df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (ParseOptionalAddrSpace(AddrSpace) ||
1452df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner          ParseToken(lltok::star, "expected '*' in address space"))
1453df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return true;
1454df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1455debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson      Result = HandleUpRefs(PointerType::get(Result.get(), AddrSpace));
1456df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      break;
1457df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
1458a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1459df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    /// Types '(' ArgTypeListI ')' OptFuncAttrs
1460df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::lparen:
1461df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (ParseFunctionType(Result))
1462df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return true;
1463df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      break;
1464df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
1465df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1466df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1467df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1468df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseParameterList
1469df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///    ::= '(' ')'
1470df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///    ::= '(' Arg (',' Arg)* ')'
1471df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///  Arg
1472df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///    ::= Type OptionalAttributes Value OptionalAttributes
1473df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1474df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                  PerFunctionState &PFS) {
1475df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseToken(lltok::lparen, "expected '(' in call"))
1476df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
1477a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1478df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  while (Lex.getKind() != lltok::rparen) {
1479df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // If this isn't the first argument, we need a comma.
1480df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!ArgList.empty() &&
1481df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::comma, "expected ',' in argument list"))
1482df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
1483a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1484df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // Parse the argument.
1485df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    LocTy ArgLoc;
14861d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson    PATypeHolder ArgTy(Type::getVoidTy(Context));
14871971556cc271710b683bc3ca8327c903791c910fVictor Hernandez    unsigned ArgAttrs1 = Attribute::None;
14881971556cc271710b683bc3ca8327c903791c910fVictor Hernandez    unsigned ArgAttrs2 = Attribute::None;
1489df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Value *V;
14901971556cc271710b683bc3ca8327c903791c910fVictor Hernandez    if (ParseType(ArgTy, ArgLoc))
1491df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
14921971556cc271710b683bc3ca8327c903791c910fVictor Hernandez
1493287881d51086c4c5b38d109142f97442c9283374Chris Lattner    // Otherwise, handle normal operands.
1494287881d51086c4c5b38d109142f97442c9283374Chris Lattner    if (ParseOptionalAttrs(ArgAttrs1, 0) ||
1495287881d51086c4c5b38d109142f97442c9283374Chris Lattner        ParseValue(ArgTy, V, PFS) ||
1496287881d51086c4c5b38d109142f97442c9283374Chris Lattner        // FIXME: Should not allow attributes after the argument, remove this
1497287881d51086c4c5b38d109142f97442c9283374Chris Lattner        // in LLVM 3.0.
1498287881d51086c4c5b38d109142f97442c9283374Chris Lattner        ParseOptionalAttrs(ArgAttrs2, 3))
1499287881d51086c4c5b38d109142f97442c9283374Chris Lattner      return true;
1500df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ArgList.push_back(ParamInfo(ArgLoc, V, ArgAttrs1|ArgAttrs2));
1501df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1502df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1503df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();  // Lex the ')'.
1504df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
1505df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1506df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1507df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1508df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1509dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner/// ParseArgumentList - Parse the argument list for a function type or function
1510dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner/// prototype.  If 'inType' is true then we are parsing a FunctionType.
1511df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= '(' ArgTypeListI ')'
1512df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ArgTypeListI
1513df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= /*empty*/
1514df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= '...'
1515df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= ArgTypeList ',' '...'
1516df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= ArgType (',' ArgType)*
1517dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner///
1518df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseArgumentList(std::vector<ArgInfo> &ArgList,
1519dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner                                 bool &isVarArg, bool inType) {
1520df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  isVarArg = false;
1521df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  assert(Lex.getKind() == lltok::lparen);
1522df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex(); // eat the (.
1523a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1524df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Lex.getKind() == lltok::rparen) {
1525df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // empty
1526df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  } else if (Lex.getKind() == lltok::dotdotdot) {
1527df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    isVarArg = true;
1528df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
1529df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  } else {
1530df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    LocTy TypeLoc = Lex.getLoc();
15311d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson    PATypeHolder ArgTy(Type::getVoidTy(Context));
1532df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    unsigned Attrs;
1533df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    std::string Name;
1534a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1535dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner    // If we're parsing a type, use ParseTypeRec, because we allow recursive
1536dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner    // types (such as a function returning a pointer to itself).  If parsing a
1537dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner    // function prototype, we require fully resolved types.
1538dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner    if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) ||
15393ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner        ParseOptionalAttrs(Attrs, 0)) return true;
1540a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1541cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner    if (ArgTy->isVoidTy())
1542a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner      return Error(TypeLoc, "argument can not have void type");
1543a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1544df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Lex.getKind() == lltok::LocalVar ||
1545df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0
1546df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Name = Lex.getStrVal();
1547df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Lex.Lex();
1548df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
15493ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner
1550a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky    if (!FunctionType::isValidArgumentType(ArgTy))
15513ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      return Error(TypeLoc, "invalid type for function argument");
1552a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1553df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name));
1554a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
15553ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    while (EatIfPresent(lltok::comma)) {
1556df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // Handle ... at end of arg list.
15573ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      if (EatIfPresent(lltok::dotdotdot)) {
1558df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        isVarArg = true;
1559df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        break;
1560df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      }
1561a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1562df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // Otherwise must be an argument type.
1563df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      TypeLoc = Lex.getLoc();
1564a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner      if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) ||
15653ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner          ParseOptionalAttrs(Attrs, 0)) return true;
15663ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner
1567cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner      if (ArgTy->isVoidTy())
1568a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner        return Error(TypeLoc, "argument can not have void type");
1569a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner
1570df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (Lex.getKind() == lltok::LocalVar ||
1571df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner          Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0
1572df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        Name = Lex.getStrVal();
1573df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        Lex.Lex();
1574df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      } else {
1575df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        Name = "";
1576df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      }
15773ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner
157847c5188789bc40671504ed1fa3a44765cefba44fDuncan Sands      if (!ArgTy->isFirstClassType() && !ArgTy->isOpaqueTy())
15793ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner        return Error(TypeLoc, "invalid type for function argument");
1580a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1581df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name));
1582df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
1583df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1584a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
15853ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  return ParseToken(lltok::rparen, "expected ')' at end of argument list");
1586df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1587a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1588df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseFunctionType
1589df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///  ::= Type ArgumentList OptionalAttrs
1590df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseFunctionType(PATypeHolder &Result) {
1591df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  assert(Lex.getKind() == lltok::lparen);
1592df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1593d77d04c323f856b07c6e46450e7578dd09c0165fChris Lattner  if (!FunctionType::isValidReturnType(Result))
1594d77d04c323f856b07c6e46450e7578dd09c0165fChris Lattner    return TokError("invalid function return type");
1595a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1596df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::vector<ArgInfo> ArgList;
1597df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  bool isVarArg;
1598df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned Attrs;
1599dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner  if (ParseArgumentList(ArgList, isVarArg, true) ||
1600df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // FIXME: Allow, but ignore attributes on function types!
1601df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // FIXME: Remove in LLVM 3.0
1602df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseOptionalAttrs(Attrs, 2))
1603df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
1604a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1605df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Reject names on the arguments lists.
1606df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1607df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!ArgList[i].Name.empty())
1608df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ArgList[i].Loc, "argument name invalid in function type");
1609df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!ArgList[i].Attrs != 0) {
1610df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // Allow but ignore attributes on function types; this permits
1611df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // auto-upgrade.
1612df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // FIXME: REJECT ATTRIBUTES ON FUNCTION TYPES in LLVM 3.0
1613df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
1614df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1615a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1616df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::vector<const Type*> ArgListTy;
1617df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
1618df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ArgListTy.push_back(ArgList[i].Type);
1619a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1620debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson  Result = HandleUpRefs(FunctionType::get(Result.get(),
1621fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson                                                ArgListTy, isVarArg));
1622df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
1623df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1624df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1625df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseStructType: Handles packed and unpacked types.  </> parsed elsewhere.
1626df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   TypeRec
1627df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///     ::= '{' '}'
1628df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///     ::= '{' TypeRec (',' TypeRec)* '}'
1629df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///     ::= '<' '{' '}' '>'
1630df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///     ::= '<' '{' TypeRec (',' TypeRec)* '}' '>'
1631df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseStructType(PATypeHolder &Result, bool Packed) {
1632df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  assert(Lex.getKind() == lltok::lbrace);
1633df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex(); // Consume the '{'
1634a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
16353ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (EatIfPresent(lltok::rbrace)) {
1636d7f2a6cb3fbc012763adb42fd967f6fefbb22a37Owen Anderson    Result = StructType::get(Context, Packed);
1637df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
1638df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1639df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1640df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::vector<PATypeHolder> ParamsList;
1641a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner  LocTy EltTyLoc = Lex.getLoc();
1642df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeRec(Result)) return true;
1643df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  ParamsList.push_back(Result);
1644a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1645cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner  if (Result->isVoidTy())
1646a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner    return Error(EltTyLoc, "struct element can not have void type");
1647a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky  if (!StructType::isValidElementType(Result))
1648a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky    return Error(EltTyLoc, "invalid element type for struct");
1649a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
16503ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  while (EatIfPresent(lltok::comma)) {
1651a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner    EltTyLoc = Lex.getLoc();
1652df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseTypeRec(Result)) return true;
1653a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1654cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner    if (Result->isVoidTy())
1655a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner      return Error(EltTyLoc, "struct element can not have void type");
1656a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky    if (!StructType::isValidElementType(Result))
1657a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky      return Error(EltTyLoc, "invalid element type for struct");
1658a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1659df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ParamsList.push_back(Result);
1660df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1661a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
16623ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (ParseToken(lltok::rbrace, "expected '}' at end of struct"))
16633ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    return true;
1664a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1665df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::vector<const Type*> ParamsListTy;
1666df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (unsigned i = 0, e = ParamsList.size(); i != e; ++i)
1667df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ParamsListTy.push_back(ParamsList[i].get());
1668d7f2a6cb3fbc012763adb42fd967f6fefbb22a37Owen Anderson  Result = HandleUpRefs(StructType::get(Context, ParamsListTy, Packed));
1669df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
1670df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1671df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1672fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner/// ParseUnionType
1673fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner///   TypeRec
1674fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner///     ::= 'union' '{' TypeRec (',' TypeRec)* '}'
1675fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattnerbool LLParser::ParseUnionType(PATypeHolder &Result) {
1676fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  assert(Lex.getKind() == lltok::kw_union);
1677fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  Lex.Lex(); // Consume the 'union'
1678fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
1679fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  if (ParseToken(lltok::lbrace, "'{' expected after 'union'")) return true;
1680fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
1681fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  SmallVector<PATypeHolder, 8> ParamsList;
1682fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  do {
1683fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    LocTy EltTyLoc = Lex.getLoc();
1684fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    if (ParseTypeRec(Result)) return true;
1685fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    ParamsList.push_back(Result);
1686fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
1687fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    if (Result->isVoidTy())
1688fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner      return Error(EltTyLoc, "union element can not have void type");
1689fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    if (!UnionType::isValidElementType(Result))
1690fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner      return Error(EltTyLoc, "invalid element type for union");
1691fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
1692fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  } while (EatIfPresent(lltok::comma)) ;
1693fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
1694fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  if (ParseToken(lltok::rbrace, "expected '}' at end of union"))
1695fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    return true;
1696fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
1697fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  SmallVector<const Type*, 8> ParamsListTy;
1698fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  for (unsigned i = 0, e = ParamsList.size(); i != e; ++i)
1699fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    ParamsListTy.push_back(ParamsList[i].get());
1700fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  Result = HandleUpRefs(UnionType::get(&ParamsListTy[0], ParamsListTy.size()));
1701fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  return false;
1702fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner}
1703fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
1704df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseArrayVectorType - Parse an array or vector type, assuming the first
1705df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// token has already been consumed.
1706a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar///   TypeRec
1707df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///     ::= '[' APSINTVAL 'x' Types ']'
1708df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///     ::= '<' APSINTVAL 'x' Types '>'
1709df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseArrayVectorType(PATypeHolder &Result, bool isVector) {
1710df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
1711df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Lex.getAPSIntVal().getBitWidth() > 64)
1712df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return TokError("expected number in address space");
1713a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1714df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy SizeLoc = Lex.getLoc();
1715df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  uint64_t Size = Lex.getAPSIntVal().getZExtValue();
17163ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  Lex.Lex();
1717a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
17183ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
17193ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      return true;
1720df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1721df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy TypeLoc = Lex.getLoc();
17221d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder EltTy(Type::getVoidTy(Context));
1723df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeRec(EltTy)) return true;
1724a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1725cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner  if (EltTy->isVoidTy())
1726a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner    return Error(TypeLoc, "array and vector element type cannot be void");
1727a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner
17283ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
17293ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner                 "expected end of sequential type"))
17303ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    return true;
1731a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1732df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (isVector) {
1733452e2627dc1de4fb5a2b1c9ac999f1b3396f82c7Chris Lattner    if (Size == 0)
1734452e2627dc1de4fb5a2b1c9ac999f1b3396f82c7Chris Lattner      return Error(SizeLoc, "zero element vector is illegal");
1735df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if ((unsigned)Size != Size)
1736df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(SizeLoc, "size too large for vector");
1737a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky    if (!VectorType::isValidElementType(EltTy))
1738df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(TypeLoc, "vector element type must be fp or integer");
1739debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson    Result = VectorType::get(EltTy, unsigned(Size));
1740df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  } else {
1741a5f54a06b09acf5c4ed3571039f262128226145bNick Lewycky    if (!ArrayType::isValidElementType(EltTy))
1742df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(TypeLoc, "invalid array element type");
1743debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson    Result = HandleUpRefs(ArrayType::get(EltTy, Size));
1744df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1745df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
1746df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1747df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1748df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
1749df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Function Semantic Analysis.
1750df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
1751df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
175209d9ef4122414a1a2ec95f52d660d6500f2819d0Chris LattnerLLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
175309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner                                             int functionNumber)
175409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  : P(p), F(f), FunctionNumber(functionNumber) {
1755df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1756df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Insert unnamed arguments into the NumberedVals list.
1757df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
1758df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner       AI != E; ++AI)
1759df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!AI->hasName())
1760df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      NumberedVals.push_back(AI);
1761df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1762df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1763df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerLLParser::PerFunctionState::~PerFunctionState() {
1764df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If there were any forward referenced non-basicblock values, delete them.
1765df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
1766df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner       I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
1767df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!isa<BasicBlock>(I->second.first)) {
1768b43eae745eb9a7c46fe76b0ed8053b782ca67536Owen Anderson      I->second.first->replaceAllUsesWith(
17699e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson                           UndefValue::get(I->second.first->getType()));
1770df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      delete I->second.first;
1771df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      I->second.first = 0;
1772df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
1773a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1774df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1775df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner       I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
1776df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!isa<BasicBlock>(I->second.first)) {
1777b43eae745eb9a7c46fe76b0ed8053b782ca67536Owen Anderson      I->second.first->replaceAllUsesWith(
17789e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson                           UndefValue::get(I->second.first->getType()));
1779df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      delete I->second.first;
1780df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      I->second.first = 0;
1781df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
1782df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1783df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
178409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattnerbool LLParser::PerFunctionState::FinishFunction() {
178509d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  // Check to see if someone took the address of labels in this block.
178609d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  if (!P.ForwardRefBlockAddresses.empty()) {
178709d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    ValID FunctionID;
178809d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    if (!F.getName().empty()) {
178909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      FunctionID.Kind = ValID::t_GlobalName;
179009d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      FunctionID.StrVal = F.getName();
179109d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    } else {
179209d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      FunctionID.Kind = ValID::t_GlobalID;
179309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      FunctionID.UIntVal = FunctionNumber;
179409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    }
179509d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
179609d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
179709d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
179809d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    if (FRBAI != P.ForwardRefBlockAddresses.end()) {
179909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      // Resolve all these references.
180009d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
180109d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner        return true;
180209d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
180309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      P.ForwardRefBlockAddresses.erase(FRBAI);
180409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    }
180509d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  }
180609d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
1807df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!ForwardRefVals.empty())
1808df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return P.Error(ForwardRefVals.begin()->second.second,
1809df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   "use of undefined value '%" + ForwardRefVals.begin()->first +
1810df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   "'");
1811df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!ForwardRefValIDs.empty())
1812df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return P.Error(ForwardRefValIDs.begin()->second.second,
1813df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   "use of undefined value '%" +
1814df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   utostr(ForwardRefValIDs.begin()->first) + "'");
1815df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
1816df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1817df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1818df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1819df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GetVal - Get a value with the specified name or ID, creating a
1820df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// forward reference record if needed.  This can return null if the value
1821df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// exists but does not have the right type.
1822df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerValue *LLParser::PerFunctionState::GetVal(const std::string &Name,
1823df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                          const Type *Ty, LocTy Loc) {
1824df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Look this name up in the normal function symbol table.
1825df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Val = F.getValueSymbolTable().lookup(Name);
1826a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1827df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If this is a forward reference for the value, see if we already created a
1828df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // forward ref record.
1829df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Val == 0) {
1830df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    std::map<std::string, std::pair<Value*, LocTy> >::iterator
1831df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      I = ForwardRefVals.find(Name);
1832df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (I != ForwardRefVals.end())
1833df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Val = I->second.first;
1834df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1835a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1836df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If we have the value in the symbol table or fwd-ref table, return it.
1837df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Val) {
1838df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Val->getType() == Ty) return Val;
1839cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner    if (Ty->isLabelTy())
1840df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      P.Error(Loc, "'%" + Name + "' is not a basic block");
1841df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    else
1842df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      P.Error(Loc, "'%" + Name + "' defined with type '" +
1843df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner              Val->getType()->getDescription() + "'");
1844df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return 0;
1845df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1846a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1847df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Don't make placeholders with invalid type.
184847c5188789bc40671504ed1fa3a44765cefba44fDuncan Sands  if (!Ty->isFirstClassType() && !Ty->isOpaqueTy() && !Ty->isLabelTy()) {
1849df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    P.Error(Loc, "invalid use of a non-first-class type");
1850df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return 0;
1851df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1852a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1853df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Otherwise, create a new forward reference for this value and remember it.
1854df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *FwdVal;
1855cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner  if (Ty->isLabelTy())
18561d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson    FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
1857df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  else
1858df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    FwdVal = new Argument(Ty, Name);
1859a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1860df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1861df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return FwdVal;
1862df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1863df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1864df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerValue *LLParser::PerFunctionState::GetVal(unsigned ID, const Type *Ty,
1865df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                          LocTy Loc) {
1866df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Look this name up in the normal function symbol table.
1867df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
1868a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1869df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If this is a forward reference for the value, see if we already created a
1870df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // forward ref record.
1871df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Val == 0) {
1872df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1873df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      I = ForwardRefValIDs.find(ID);
1874df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (I != ForwardRefValIDs.end())
1875df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Val = I->second.first;
1876df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1877a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1878df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If we have the value in the symbol table or fwd-ref table, return it.
1879df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Val) {
1880df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Val->getType() == Ty) return Val;
1881cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner    if (Ty->isLabelTy())
1882df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      P.Error(Loc, "'%" + utostr(ID) + "' is not a basic block");
1883df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    else
1884df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      P.Error(Loc, "'%" + utostr(ID) + "' defined with type '" +
1885df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner              Val->getType()->getDescription() + "'");
1886df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return 0;
1887df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1888a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
188947c5188789bc40671504ed1fa3a44765cefba44fDuncan Sands  if (!Ty->isFirstClassType() && !Ty->isOpaqueTy() && !Ty->isLabelTy()) {
1890df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    P.Error(Loc, "invalid use of a non-first-class type");
1891df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return 0;
1892df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1893a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1894df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Otherwise, create a new forward reference for this value and remember it.
1895df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *FwdVal;
1896cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner  if (Ty->isLabelTy())
18971d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson    FwdVal = BasicBlock::Create(F.getContext(), "", &F);
1898df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  else
1899df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    FwdVal = new Argument(Ty);
1900a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1901df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1902df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return FwdVal;
1903df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1904df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1905df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// SetInstName - After an instruction is parsed and inserted into its
1906df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// basic block, this installs its name.
1907df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::PerFunctionState::SetInstName(int NameID,
1908df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                             const std::string &NameStr,
1909df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                             LocTy NameLoc, Instruction *Inst) {
1910df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If this instruction has void type, it cannot have a name or ID specified.
1911cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner  if (Inst->getType()->isVoidTy()) {
1912df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (NameID != -1 || !NameStr.empty())
1913df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return P.Error(NameLoc, "instructions returning void cannot have a name");
1914df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
1915df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1916a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1917df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If this was a numbered instruction, verify that the instruction is the
1918df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // expected value and resolve any forward references.
1919df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (NameStr.empty()) {
1920df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // If neither a name nor an ID was specified, just use the next ID.
1921df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (NameID == -1)
1922df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      NameID = NumberedVals.size();
1923a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1924df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (unsigned(NameID) != NumberedVals.size())
1925df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return P.Error(NameLoc, "instruction expected to be numbered '%" +
1926df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                     utostr(NumberedVals.size()) + "'");
1927a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1928df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
1929df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ForwardRefValIDs.find(NameID);
1930df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (FI != ForwardRefValIDs.end()) {
1931df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (FI->second.first->getType() != Inst->getType())
1932a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar        return P.Error(NameLoc, "instruction forward referenced with type '" +
1933df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                       FI->second.first->getType()->getDescription() + "'");
1934df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      FI->second.first->replaceAllUsesWith(Inst);
19352b4f28e270c5091a7e70991adc3e36aff0c85b0eNuno Lopes      delete FI->second.first;
1936df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ForwardRefValIDs.erase(FI);
1937df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
1938df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1939df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    NumberedVals.push_back(Inst);
1940df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
1941df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1942df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1943df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Otherwise, the instruction had a name.  Resolve forward refs and set it.
1944df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::map<std::string, std::pair<Value*, LocTy> >::iterator
1945df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    FI = ForwardRefVals.find(NameStr);
1946df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (FI != ForwardRefVals.end()) {
1947df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (FI->second.first->getType() != Inst->getType())
1948a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar      return P.Error(NameLoc, "instruction forward referenced with type '" +
1949df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                     FI->second.first->getType()->getDescription() + "'");
1950df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    FI->second.first->replaceAllUsesWith(Inst);
1951531552aaa24218c1b2043cf56d719f3d940aeed4Nuno Lopes    delete FI->second.first;
1952df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ForwardRefVals.erase(FI);
1953df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
1954a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1955df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Set the name on the instruction.
1956df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst->setName(NameStr);
1957a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1958df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Inst->getNameStr() != NameStr)
1959a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar    return P.Error(NameLoc, "multiple definition of local value named '" +
1960df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   NameStr + "'");
1961df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
1962df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1963df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1964df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// GetBB - Get a basic block with the specified name or ID, creating a
1965df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// forward reference record if needed.
1966df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerBasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
1967df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                              LocTy Loc) {
19681d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  return cast_or_null<BasicBlock>(GetVal(Name,
19691d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson                                        Type::getLabelTy(F.getContext()), Loc));
1970df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1971df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1972df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerBasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
19731d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  return cast_or_null<BasicBlock>(GetVal(ID,
19741d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson                                        Type::getLabelTy(F.getContext()), Loc));
1975df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
1976df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
1977df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// DefineBB - Define the specified basic block, which is either named or
1978df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// unnamed.  If there is an error, this returns null otherwise it returns
1979df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// the block being defined.
1980df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris LattnerBasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
1981df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                                 LocTy Loc) {
1982df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  BasicBlock *BB;
1983df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Name.empty())
1984df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    BB = GetBB(NumberedVals.size(), Loc);
1985df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  else
1986df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    BB = GetBB(Name, Loc);
1987df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (BB == 0) return 0; // Already diagnosed error.
1988a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1989df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Move the block to the end of the function.  Forward ref'd blocks are
1990df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // inserted wherever they happen to be referenced.
1991df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
1992a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
1993df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Remove the block from forward ref sets.
1994df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Name.empty()) {
1995df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ForwardRefValIDs.erase(NumberedVals.size());
1996df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    NumberedVals.push_back(BB);
1997df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  } else {
1998df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // BB forward references are already in the function symbol table.
1999df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ForwardRefVals.erase(Name);
2000df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2001a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2002df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return BB;
2003df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
2004df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2005df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
2006df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Constants.
2007df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
2008df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2009df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseValID - Parse an abstract value that doesn't necessarily have a
2010df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// type implied.  For example, if we parse "4" we don't know what integer type
2011df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// it has.  The value will later be combined with its type and checked for
201224e64df7ec25b55aa872c2ef33728dfbb8c353c4Victor Hernandez/// sanity.  PFS is used to convert function-local operands of metadata (since
201324e64df7ec25b55aa872c2ef33728dfbb8c353c4Victor Hernandez/// metadata operands are not just parsed here but also converted to values).
201424e64df7ec25b55aa872c2ef33728dfbb8c353c4Victor Hernandez/// PFS can be null when we are not parsing metadata values inside a function.
2015bf170d4c2ffd9dc99fad1fabfbf1f90f980a9782Victor Hernandezbool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
2016df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  ID.Loc = Lex.getLoc();
2017df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  switch (Lex.getKind()) {
2018df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  default: return TokError("expected value token");
2019df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::GlobalID:  // @42
2020df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.UIntVal = Lex.getUIntVal();
2021df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_GlobalID;
2022df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
2023df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::GlobalVar:  // @foo
2024df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.StrVal = Lex.getStrVal();
2025df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_GlobalName;
2026df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
2027df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::LocalVarID:  // %42
2028df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.UIntVal = Lex.getUIntVal();
2029df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_LocalID;
2030df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
2031df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::LocalVar:  // %foo
2032df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::StringConstant:  // "foo" - FIXME: REMOVE IN LLVM 3.0
2033df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.StrVal = Lex.getStrVal();
2034df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_LocalName;
2035df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
2036834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  case lltok::exclaim:   // !42, !{...}, or !"foo"
2037834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman    return ParseMetadataValue(ID, PFS);
2038df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::APSInt:
2039a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar    ID.APSIntVal = Lex.getAPSIntVal();
2040df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_APSInt;
2041df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
2042df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::APFloat:
2043df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.APFloatVal = Lex.getAPFloatVal();
2044df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_APFloat;
2045df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
2046df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_true:
20475defacc6e605f4651c6300237cef8e9bb2eb6d0eOwen Anderson    ID.ConstantVal = ConstantInt::getTrue(Context);
2048df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2049df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
2050df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_false:
20515defacc6e605f4651c6300237cef8e9bb2eb6d0eOwen Anderson    ID.ConstantVal = ConstantInt::getFalse(Context);
2052df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2053df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
2054df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2055df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2056df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
2057a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2058df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::lbrace: {
2059df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // ValID ::= '{' ConstVector '}'
2060df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
2061df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    SmallVector<Constant*, 16> Elts;
2062df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseGlobalValueVector(Elts) ||
2063df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::rbrace, "expected end of struct constant"))
2064df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2065a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2066d7f2a6cb3fbc012763adb42fd967f6fefbb22a37Owen Anderson    ID.ConstantVal = ConstantStruct::get(Context, Elts.data(),
2067d7f2a6cb3fbc012763adb42fd967f6fefbb22a37Owen Anderson                                         Elts.size(), false);
2068df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2069df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2070df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2071df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::less: {
2072df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // ValID ::= '<' ConstVector '>'         --> Vector.
2073df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2074df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
20753ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    bool isPackedStruct = EatIfPresent(lltok::lbrace);
2076a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2077df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    SmallVector<Constant*, 16> Elts;
2078df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    LocTy FirstEltLoc = Lex.getLoc();
2079df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseGlobalValueVector(Elts) ||
2080df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        (isPackedStruct &&
2081df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner         ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2082df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::greater, "expected end of constant"))
2083df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2084a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2085df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (isPackedStruct) {
2086fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson      ID.ConstantVal =
2087d7f2a6cb3fbc012763adb42fd967f6fefbb22a37Owen Anderson        ConstantStruct::get(Context, Elts.data(), Elts.size(), true);
2088df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ID.Kind = ValID::t_Constant;
2089df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return false;
2090df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
2091a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2092df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Elts.empty())
2093df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "constant vector must not be empty");
2094df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2095b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands    if (!Elts[0]->getType()->isIntegerTy() &&
2096b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands        !Elts[0]->getType()->isFloatingPointTy())
2097df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(FirstEltLoc,
2098df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   "vector elements must have integer or floating point type");
2099a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2100df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // Verify that all the vector elements have the same type.
2101df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2102df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (Elts[i]->getType() != Elts[0]->getType())
2103df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(FirstEltLoc,
2104df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                     "vector element #" + utostr(i) +
2105df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                    " is not of type '" + Elts[0]->getType()->getDescription());
2106a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2107af7ec975870f92245f1f1484ac80a1e2db6a0afaOwen Anderson    ID.ConstantVal = ConstantVector::get(Elts.data(), Elts.size());
2108df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2109df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2110df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2111df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::lsquare: {   // Array Constant
2112df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
2113df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    SmallVector<Constant*, 16> Elts;
2114df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    LocTy FirstEltLoc = Lex.getLoc();
2115df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseGlobalValueVector(Elts) ||
2116df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::rsquare, "expected end of array constant"))
2117df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2118df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2119df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // Handle empty element.
2120df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Elts.empty()) {
2121df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // Use undef instead of an array because it's inconvenient to determine
2122df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // the element type at this point, there being no elements to examine.
2123081b505f77b183b7b029818218245cf4986e8948Chris Lattner      ID.Kind = ValID::t_EmptyArray;
2124df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return false;
2125df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
2126a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2127df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!Elts[0]->getType()->isFirstClassType())
2128a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar      return Error(FirstEltLoc, "invalid array element type: " +
2129df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   Elts[0]->getType()->getDescription());
2130a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2131debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson    ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
2132a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2133df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // Verify all elements are correct type!
21346d6b3cc89b9d1911cff4931dadfe670f94fcbb3bChris Lattner    for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
2135df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (Elts[i]->getType() != Elts[0]->getType())
2136df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(FirstEltLoc,
2137df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                     "array element #" + utostr(i) +
2138df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                     " is not of type '" +Elts[0]->getType()->getDescription());
2139df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
2140a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
21411fd7096407d5e598ed3366a1141548e71273f1c5Owen Anderson    ID.ConstantVal = ConstantArray::get(ATy, Elts.data(), Elts.size());
2142df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2143df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2144df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2145df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_c:  // c "foo"
2146df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
21471d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson    ID.ConstantVal = ConstantArray::get(Context, Lex.getStrVal(), false);
2148df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseToken(lltok::StringConstant, "expected string")) return true;
2149df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2150df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2151df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2152df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_asm: {
21538ba2d5befc05ca73d3bac8708819bbbe759e2cf9Dale Johannesen    // ValID ::= 'asm' SideEffect? AlignStack? STRINGCONSTANT ',' STRINGCONSTANT
21548ba2d5befc05ca73d3bac8708819bbbe759e2cf9Dale Johannesen    bool HasSideEffect, AlignStack;
2155df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
2156df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
21578ba2d5befc05ca73d3bac8708819bbbe759e2cf9Dale Johannesen        ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
21583ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner        ParseStringConstant(ID.StrVal) ||
21593ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner        ParseToken(lltok::comma, "expected comma in inline asm expression") ||
2160df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::StringConstant, "expected constraint string"))
2161df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2162df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.StrVal2 = Lex.getStrVal();
2163f0bb41c078d657c70ce6b95d5b56645c72ca7db4Daniel Dunbar    ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1);
2164df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_InlineAsm;
2165df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2166df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2167a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
216809d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  case lltok::kw_blockaddress: {
216909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
217009d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    Lex.Lex();
217109d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
217209d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    ValID Fn, Label;
217309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    LocTy FnLoc, LabelLoc;
217409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
217509d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
217609d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner        ParseValID(Fn) ||
217709d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner        ParseToken(lltok::comma, "expected comma in block address expression")||
217809d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner        ParseValID(Label) ||
217909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner        ParseToken(lltok::rparen, "expected ')' in block address expression"))
218009d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      return true;
2181cdfc940912d56a63b6f12eaa7f3faf79cf74c693Chris Lattner
218209d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
218309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      return Error(Fn.Loc, "expected function name in blockaddress");
2184cdfc940912d56a63b6f12eaa7f3faf79cf74c693Chris Lattner    if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
218509d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner      return Error(Label.Loc, "expected basic block name in blockaddress");
218609d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
218709d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    // Make a global variable as a placeholder for this reference.
218809d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
218909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner                                           false, GlobalValue::InternalLinkage,
219009d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner                                                0, "");
219109d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
219209d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    ID.ConstantVal = FwdRef;
219309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    ID.Kind = ValID::t_Constant;
219409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner    return false;
219509d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  }
219609d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
2197df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_trunc:
2198df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_zext:
2199df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_sext:
2200df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_fptrunc:
2201df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_fpext:
2202df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_bitcast:
2203df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_uitofp:
2204df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_sitofp:
2205df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_fptoui:
2206a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar  case lltok::kw_fptosi:
2207df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_inttoptr:
2208a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar  case lltok::kw_ptrtoint: {
2209df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    unsigned Opc = Lex.getUIntVal();
22101d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson    PATypeHolder DestTy(Type::getVoidTy(Context));
2211df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Constant *SrcVal;
2212df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
2213df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2214df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseGlobalTypeAndValue(SrcVal) ||
221524b108bc8ca75922d4cb8e7f197934b8269d4fc3Dan Gohman        ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
2216df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseType(DestTy) ||
2217df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2218df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2219df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2220df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "invalid cast opcode for cast from '" +
2221df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   SrcVal->getType()->getDescription() + "' to '" +
2222df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   DestTy->getDescription() + "'");
2223a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar    ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
2224fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson                                                 SrcVal, DestTy);
2225df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2226df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2227df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2228df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_extractvalue: {
2229df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
2230df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Constant *Val;
2231df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    SmallVector<unsigned, 4> Indices;
2232df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2233df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseGlobalTypeAndValue(Val) ||
2234df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseIndexList(Indices) ||
2235df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2236df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2237e8bc45a645f1df1c9f8b23c6f29247a61af14f19Devang Patel
2238fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    if (!Val->getType()->isAggregateType())
2239fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner      return Error(ID.Loc, "extractvalue operand must be aggregate type");
2240df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
2241df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                          Indices.end()))
2242df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "invalid indices for extractvalue");
2243e3e51c0038bd6ba2add82e2246e97edec0ab2204Jay Foad    ID.ConstantVal =
2244baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson      ConstantExpr::getExtractValue(Val, Indices.data(), Indices.size());
2245df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2246df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2247df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2248df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_insertvalue: {
2249df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
2250df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Constant *Val0, *Val1;
2251df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    SmallVector<unsigned, 4> Indices;
2252df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2253df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseGlobalTypeAndValue(Val0) ||
2254df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2255df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseGlobalTypeAndValue(Val1) ||
2256df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseIndexList(Indices) ||
2257df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2258df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2259fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    if (!Val0->getType()->isAggregateType())
2260fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner      return Error(ID.Loc, "insertvalue operand must be aggregate type");
2261df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
2262df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                          Indices.end()))
2263df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "invalid indices for insertvalue");
2264baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson    ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1,
2265fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson                       Indices.data(), Indices.size());
2266df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2267df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2268df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2269df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_icmp:
22707f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky  case lltok::kw_fcmp: {
2271df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    unsigned PredVal, Opc = Lex.getUIntVal();
2272df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Constant *Val0, *Val1;
2273df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
2274df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseCmpPredicate(PredVal, Opc) ||
2275df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2276df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseGlobalTypeAndValue(Val0) ||
2277df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2278df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseGlobalTypeAndValue(Val1) ||
2279df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2280df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2281a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2282df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Val0->getType() != Val1->getType())
2283df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "compare operands must have the same type");
2284a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2285df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
2286a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2287df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Opc == Instruction::FCmp) {
2288b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands      if (!Val0->getType()->isFPOrFPVectorTy())
2289df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(ID.Loc, "fcmp requires floating point operands");
2290baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson      ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
22917f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky    } else {
22927f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky      assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
2293b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands      if (!Val0->getType()->isIntOrIntVectorTy() &&
22941df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands          !Val0->getType()->isPointerTy())
2295df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(ID.Loc, "icmp requires pointer or integer operands");
2296baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson      ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
2297df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
2298df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2299df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2300df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2301a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2302df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Binary Operators.
2303df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_add:
2304ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  case lltok::kw_fadd:
2305df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_sub:
2306ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  case lltok::kw_fsub:
2307df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_mul:
2308ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  case lltok::kw_fmul:
2309df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_udiv:
2310df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_sdiv:
2311df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_fdiv:
2312df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_urem:
2313df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_srem:
2314df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_frem: {
231559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    bool NUW = false;
231659858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    bool NSW = false;
231759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    bool Exact = false;
2318df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    unsigned Opc = Lex.getUIntVal();
2319df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Constant *Val0, *Val1;
2320df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
232159858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    LocTy ModifierLoc = Lex.getLoc();
232259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    if (Opc == Instruction::Add ||
232359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman        Opc == Instruction::Sub ||
232459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman        Opc == Instruction::Mul) {
232559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      if (EatIfPresent(lltok::kw_nuw))
232659858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman        NUW = true;
232759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      if (EatIfPresent(lltok::kw_nsw)) {
232859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman        NSW = true;
232959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman        if (EatIfPresent(lltok::kw_nuw))
233059858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman          NUW = true;
233159858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      }
233259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    } else if (Opc == Instruction::SDiv) {
233359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      if (EatIfPresent(lltok::kw_exact))
233459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman        Exact = true;
233559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    }
2336df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2337df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseGlobalTypeAndValue(Val0) ||
2338df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2339df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseGlobalTypeAndValue(Val1) ||
2340df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2341df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2342df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Val0->getType() != Val1->getType())
2343df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "operands of constexpr must have same type");
2344b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands    if (!Val0->getType()->isIntOrIntVectorTy()) {
234559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      if (NUW)
234659858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman        return Error(ModifierLoc, "nuw only applies to integer operations");
234759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      if (NSW)
234859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman        return Error(ModifierLoc, "nsw only applies to integer operations");
234959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    }
23501eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    // Check that the type is valid for the operator.
23511eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    switch (Opc) {
23521eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    case Instruction::Add:
23531eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    case Instruction::Sub:
23541eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    case Instruction::Mul:
23551eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    case Instruction::UDiv:
23561eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    case Instruction::SDiv:
23571eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    case Instruction::URem:
23581eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    case Instruction::SRem:
23591eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman      if (!Val0->getType()->isIntOrIntVectorTy())
23601eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman        return Error(ID.Loc, "constexpr requires integer operands");
23611eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman      break;
23621eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    case Instruction::FAdd:
23631eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    case Instruction::FSub:
23641eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    case Instruction::FMul:
23651eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    case Instruction::FDiv:
23661eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    case Instruction::FRem:
23671eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman      if (!Val0->getType()->isFPOrFPVectorTy())
23681eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman        return Error(ID.Loc, "constexpr requires fp operands");
23691eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman      break;
23701eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    default: llvm_unreachable("Unknown binary operator!");
23711eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    }
2372f8dbee7cea072eb63ae343759975109553697bcbDan Gohman    unsigned Flags = 0;
2373f8dbee7cea072eb63ae343759975109553697bcbDan Gohman    if (NUW)   Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2374f8dbee7cea072eb63ae343759975109553697bcbDan Gohman    if (NSW)   Flags |= OverflowingBinaryOperator::NoSignedWrap;
2375f8dbee7cea072eb63ae343759975109553697bcbDan Gohman    if (Exact) Flags |= SDivOperator::IsExact;
2376f8dbee7cea072eb63ae343759975109553697bcbDan Gohman    Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
237759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    ID.ConstantVal = C;
2378df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2379df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2380df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2381a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2382df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Logical Operations
2383df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_shl:
2384df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_lshr:
2385df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_ashr:
2386df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_and:
2387df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_or:
2388df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_xor: {
2389df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    unsigned Opc = Lex.getUIntVal();
2390df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Constant *Val0, *Val1;
2391df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
2392df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2393df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseGlobalTypeAndValue(Val0) ||
2394df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2395df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseGlobalTypeAndValue(Val1) ||
2396df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2397df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2398df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Val0->getType() != Val1->getType())
2399df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "operands of constexpr must have same type");
2400b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands    if (!Val0->getType()->isIntOrIntVectorTy())
2401df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc,
2402df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   "constexpr requires integer or integer vector operands");
2403baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson    ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
2404df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2405df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2406a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar  }
2407a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2408df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_getelementptr:
2409df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_shufflevector:
2410df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_insertelement:
2411df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_extractelement:
2412df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_select: {
2413df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    unsigned Opc = Lex.getUIntVal();
2414df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    SmallVector<Constant*, 16> Elts;
2415dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman    bool InBounds = false;
2416df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
2417dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman    if (Opc == Instruction::GetElementPtr)
2418dcb40a3c1c747c88c4e634e34d1b4d7533a86adaDan Gohman      InBounds = EatIfPresent(lltok::kw_inbounds);
2419df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2420df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseGlobalValueVector(Elts) ||
2421df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2422df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
2423a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2424df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Opc == Instruction::GetElementPtr) {
24251df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands      if (Elts.size() == 0 || !Elts[0]->getType()->isPointerTy())
2426df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(ID.Loc, "getelementptr requires pointer operand");
2427a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2428df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(),
24294e9bac3769cb668aad20f1ac2d6a35d9ba452f3aEli Friedman                                             (Value**)(Elts.data() + 1),
24304e9bac3769cb668aad20f1ac2d6a35d9ba452f3aEli Friedman                                             Elts.size() - 1))
2431df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(ID.Loc, "invalid indices for getelementptr");
2432f8dbee7cea072eb63ae343759975109553697bcbDan Gohman      ID.ConstantVal = InBounds ?
2433f8dbee7cea072eb63ae343759975109553697bcbDan Gohman        ConstantExpr::getInBoundsGetElementPtr(Elts[0],
2434f8dbee7cea072eb63ae343759975109553697bcbDan Gohman                                               Elts.data() + 1,
2435f8dbee7cea072eb63ae343759975109553697bcbDan Gohman                                               Elts.size() - 1) :
2436f8dbee7cea072eb63ae343759975109553697bcbDan Gohman        ConstantExpr::getGetElementPtr(Elts[0],
2437f8dbee7cea072eb63ae343759975109553697bcbDan Gohman                                       Elts.data() + 1, Elts.size() - 1);
2438df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    } else if (Opc == Instruction::Select) {
2439df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (Elts.size() != 3)
2440df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(ID.Loc, "expected three operands to select");
2441df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2442df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                                              Elts[2]))
2443df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(ID.Loc, Reason);
2444baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson      ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
2445df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    } else if (Opc == Instruction::ShuffleVector) {
2446df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (Elts.size() != 3)
2447df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(ID.Loc, "expected three operands to shufflevector");
2448df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2449df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(ID.Loc, "invalid operands to shufflevector");
2450fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson      ID.ConstantVal =
2451baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson                 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
2452df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    } else if (Opc == Instruction::ExtractElement) {
2453df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (Elts.size() != 2)
2454df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(ID.Loc, "expected two operands to extractelement");
2455df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2456df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(ID.Loc, "invalid extractelement operands");
2457baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson      ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
2458df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    } else {
2459df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      assert(Opc == Instruction::InsertElement && "Unknown opcode");
2460df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (Elts.size() != 3)
2461df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "expected three operands to insertelement");
2462df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2463df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(ID.Loc, "invalid insertelement operands");
2464fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson      ID.ConstantVal =
2465baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson                 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
2466df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
2467a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2468df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.Kind = ValID::t_Constant;
2469df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2470df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2471df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2472a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2473df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
2474df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
2475df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
2476df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2477df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseGlobalValue - Parse a global value with the specified type.
247892f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandezbool LLParser::ParseGlobalValue(const Type *Ty, Constant *&C) {
247992f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  C = 0;
2480df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  ValID ID;
248192f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  Value *V = NULL;
248292f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  bool Parsed = ParseValID(ID) ||
248392f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez                ConvertValIDToValue(Ty, ID, V, NULL);
248492f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  if (V && !(C = dyn_cast<Constant>(V)))
248592f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    return Error(ID.Loc, "global values must be constants");
248692f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  return Parsed;
248792f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez}
248892f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez
248992f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandezbool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
249092f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  PATypeHolder Type(Type::getVoidTy(Context));
249192f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  return ParseType(Type) ||
249292f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez         ParseGlobalValue(Type, V);
249392f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez}
249492f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez
249592f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez/// ParseGlobalValueVector
249692f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez///   ::= /*empty*/
249792f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez///   ::= TypeAndValue (',' TypeAndValue)*
249892f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandezbool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
249992f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  // Empty list.
250092f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  if (Lex.getKind() == lltok::rbrace ||
250192f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez      Lex.getKind() == lltok::rsquare ||
250292f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez      Lex.getKind() == lltok::greater ||
250392f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez      Lex.getKind() == lltok::rparen)
250492f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    return false;
250592f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez
250692f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  Constant *C;
250792f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  if (ParseGlobalTypeAndValue(C)) return true;
250892f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  Elts.push_back(C);
250992f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez
251092f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  while (EatIfPresent(lltok::comma)) {
251192f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    if (ParseGlobalTypeAndValue(C)) return true;
251292f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    Elts.push_back(C);
251392f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  }
251492f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez
251592f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  return false;
2516df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
2517df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2518309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohmanbool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2519309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman  assert(Lex.getKind() == lltok::lbrace);
2520309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman  Lex.Lex();
2521309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman
2522309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman  SmallVector<Value*, 16> Elts;
2523309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman  if (ParseMDNodeVector(Elts, PFS) ||
2524309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman      ParseToken(lltok::rbrace, "expected end of metadata node"))
2525309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman    return true;
2526309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman
2527309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman  ID.MDNodeVal = MDNode::get(Context, Elts.data(), Elts.size());
2528309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman  ID.Kind = ValID::t_MDNode;
2529309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman  return false;
2530309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman}
2531309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman
2532834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman/// ParseMetadataValue
2533834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman///  ::= !42
2534834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman///  ::= !{...}
2535834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman///  ::= !"string"
2536834480374ba38b2a31d6b63b492e442d75a462cfDan Gohmanbool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2537834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  assert(Lex.getKind() == lltok::exclaim);
2538834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  Lex.Lex();
2539834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman
2540834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  // MDNode:
2541834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  // !{ ... }
2542309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman  if (Lex.getKind() == lltok::lbrace)
2543309b3af547a60bedd74daa2a94ebd3d3ed5f06e9Dan Gohman    return ParseMetadataListValue(ID, PFS);
2544834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman
2545834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  // Standalone metadata reference
2546834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  // !42
2547834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  if (Lex.getKind() == lltok::APSInt) {
2548834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman    if (ParseMDNodeID(ID.MDNodeVal)) return true;
2549834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman    ID.Kind = ValID::t_MDNode;
2550834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman    return false;
2551834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  }
2552834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman
2553834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  // MDString:
2554834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  //   ::= '!' STRINGCONSTANT
2555834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  if (ParseMDString(ID.MDStringVal)) return true;
2556834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  ID.Kind = ValID::t_MDString;
2557834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman  return false;
2558834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman}
2559834480374ba38b2a31d6b63b492e442d75a462cfDan Gohman
256092f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez
256192f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez//===----------------------------------------------------------------------===//
256292f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez// Function Parsing.
256392f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez//===----------------------------------------------------------------------===//
256492f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez
256592f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandezbool LLParser::ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
256692f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez                                   PerFunctionState *PFS) {
25671df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (Ty->isFunctionTy())
2568df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(ID.Loc, "functions are not values, refer to them as pointers");
2569a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2570df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  switch (ID.Kind) {
2571a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar  default: llvm_unreachable("Unknown ValID!");
2572df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case ValID::t_LocalID:
257392f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
257492f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
257592f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    return (V == 0);
2576df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case ValID::t_LocalName:
257792f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
257892f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
257992f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    return (V == 0);
258092f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  case ValID::t_InlineAsm: {
258192f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    const PointerType *PTy = dyn_cast<PointerType>(Ty);
258292f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    const FunctionType *FTy =
258392f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez      PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
258492f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
258592f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez      return Error(ID.Loc, "invalid type for inline asm constraint string");
258692f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1, ID.UIntVal>>1);
258792f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    return false;
258892f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  }
258992f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  case ValID::t_MDNode:
259092f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    if (!Ty->isMetadataTy())
259192f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez      return Error(ID.Loc, "metadata value must have metadata type");
259292f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    V = ID.MDNodeVal;
259392f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    return false;
259492f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  case ValID::t_MDString:
259592f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    if (!Ty->isMetadataTy())
259692f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez      return Error(ID.Loc, "metadata value must have metadata type");
259792f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    V = ID.MDStringVal;
259892f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez    return false;
2599df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case ValID::t_GlobalName:
2600df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
2601df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return V == 0;
2602df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case ValID::t_GlobalID:
2603df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
2604df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return V == 0;
2605df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case ValID::t_APSInt:
26061df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands    if (!Ty->isIntegerTy())
2607df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "integer constant must have integer type");
2608df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
2609eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson    V = ConstantInt::get(Context, ID.APSIntVal);
2610df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2611df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case ValID::t_APFloat:
2612b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands    if (!Ty->isFloatingPointTy() ||
2613df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2614df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "floating point constant invalid for type");
2615a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2616df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // The lexer has no type info, so builds all float and double FP constants
2617df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // as double.  Fix this here.  Long double does not need this.
2618df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble &&
2619cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner        Ty->isFloatTy()) {
2620df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      bool Ignored;
2621df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2622df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                            &Ignored);
2623df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
26246f83c9c6ef0e7f79825a0a8f22941815e4b684c7Owen Anderson    V = ConstantFP::get(Context, ID.APFloatVal);
2625a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2626959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner    if (V->getType() != Ty)
2627959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner      return Error(ID.Loc, "floating point constant does not have type '" +
2628959873d2da763c0e3b3c67c39aab179fc43c5a52Chris Lattner                   Ty->getDescription() + "'");
2629a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2630df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2631df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case ValID::t_Null:
26321df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands    if (!Ty->isPointerTy())
2633df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "null must be a pointer type");
26349e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson    V = ConstantPointerNull::get(cast<PointerType>(Ty));
2635df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2636df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case ValID::t_Undef:
2637e67c1aa3986cf98f2021a31a6ad502857c757b7bChris Lattner    // FIXME: LabelTy should not be a first-class type.
2638cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner    if ((!Ty->isFirstClassType() || Ty->isLabelTy()) &&
263947c5188789bc40671504ed1fa3a44765cefba44fDuncan Sands        !Ty->isOpaqueTy())
2640e67c1aa3986cf98f2021a31a6ad502857c757b7bChris Lattner      return Error(ID.Loc, "invalid type for undef constant");
26419e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson    V = UndefValue::get(Ty);
2642df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2643081b505f77b183b7b029818218245cf4986e8948Chris Lattner  case ValID::t_EmptyArray:
26441df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands    if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
2645081b505f77b183b7b029818218245cf4986e8948Chris Lattner      return Error(ID.Loc, "invalid empty array initializer");
26469e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson    V = UndefValue::get(Ty);
2647081b505f77b183b7b029818218245cf4986e8948Chris Lattner    return false;
2648df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case ValID::t_Zero:
2649e67c1aa3986cf98f2021a31a6ad502857c757b7bChris Lattner    // FIXME: LabelTy should not be a first-class type.
2650cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner    if (!Ty->isFirstClassType() || Ty->isLabelTy())
2651df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "invalid type for null constant");
2652a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson    V = Constant::getNullValue(Ty);
2653df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2654df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case ValID::t_Constant:
2655fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    if (ID.ConstantVal->getType() != Ty) {
2656fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner      // Allow a constant struct with a single member to be converted
2657fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner      // to a union, if the union has a member which is the same type
2658fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner      // as the struct member.
2659fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner      if (const UnionType* utype = dyn_cast<UnionType>(Ty)) {
2660fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner        return ParseUnionValue(utype, ID, V);
2661fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner      }
2662fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
2663df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ID.Loc, "constant expression type mismatch");
2664fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    }
2665fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
2666df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    V = ID.ConstantVal;
2667df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
2668df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2669df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
2670a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2671df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) {
2672df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  V = 0;
2673df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  ValID ID;
2674bf170d4c2ffd9dc99fad1fabfbf1f90f980a9782Victor Hernandez  return ParseValID(ID, &PFS) ||
267592f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez         ConvertValIDToValue(Ty, ID, V, &PFS);
2676df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
2677df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2678df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
26791d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder T(Type::getVoidTy(Context));
26803ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  return ParseType(T) ||
26813ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner         ParseValue(T, V, PFS);
2682df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
2683df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2684f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattnerbool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2685f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner                                      PerFunctionState &PFS) {
2686f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  Value *V;
2687f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  Loc = Lex.getLoc();
2688f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  if (ParseTypeAndValue(V, PFS)) return true;
2689f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  if (!isa<BasicBlock>(V))
2690f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner    return Error(Loc, "expected a basic block");
2691f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  BB = cast<BasicBlock>(V);
2692f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  return false;
2693f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner}
2694f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner
2695fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattnerbool LLParser::ParseUnionValue(const UnionType* utype, ValID &ID, Value *&V) {
2696fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  if (const StructType* stype = dyn_cast<StructType>(ID.ConstantVal->getType())) {
2697fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    if (stype->getNumContainedTypes() != 1)
2698fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner      return Error(ID.Loc, "constant expression type mismatch");
2699fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    int index = utype->getElementTypeIndex(stype->getContainedType(0));
2700fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    if (index < 0)
2701fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner      return Error(ID.Loc, "initializer type is not a member of the union");
2702fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
2703fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    V = ConstantUnion::get(
2704fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner        utype, cast<Constant>(ID.ConstantVal->getOperand(0)));
2705fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    return false;
2706fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  }
2707fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
2708fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  return Error(ID.Loc, "constant expression type mismatch");
2709fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner}
2710fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner
2711f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner
2712df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// FunctionHeader
2713df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
2714df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///       Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
2715df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///       OptionalAlign OptGC
2716df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
2717df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Parse the linkage.
2718df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy LinkageLoc = Lex.getLoc();
2719df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned Linkage;
2720a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
272165c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel  unsigned Visibility, RetAttrs;
272265c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel  CallingConv::ID CC;
27231d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder RetType(Type::getVoidTy(Context));
2724df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy RetTypeLoc = Lex.getLoc();
2725df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseOptionalLinkage(Linkage) ||
2726df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseOptionalVisibility(Visibility) ||
2727df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseOptionalCallingConv(CC) ||
2728df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseOptionalAttrs(RetAttrs, 1) ||
2729a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner      ParseType(RetType, RetTypeLoc, true /*void allowed*/))
2730df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
2731df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2732df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Verify that the linkage is ok.
2733df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  switch ((GlobalValue::LinkageTypes)Linkage) {
2734df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case GlobalValue::ExternalLinkage:
2735df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break; // always ok.
2736df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case GlobalValue::DLLImportLinkage:
27375f4ee1fc5d00ae55c30fa2ce450c69be4c6d6e63Duncan Sands  case GlobalValue::ExternalWeakLinkage:
2738df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (isDefine)
2739df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(LinkageLoc, "invalid linkage for function definition");
2740df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
2741bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola  case GlobalValue::PrivateLinkage:
27423d10a5a75794356a0a568ce283713adc3a963200Bill Wendling  case GlobalValue::LinkerPrivateLinkage:
27435e721d768254a920b78b9129d79a84c0163cb3f4Bill Wendling  case GlobalValue::LinkerPrivateWeakLinkage:
274455ae515f9db484125a23429d4906c5edaf9f10d2Bill Wendling  case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
2745df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case GlobalValue::InternalLinkage:
274655f64dbb398cc8d967d1e32a8bb2190a60f5b61eNick Lewycky  case GlobalValue::AvailableExternallyLinkage:
2747667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands  case GlobalValue::LinkOnceAnyLinkage:
2748667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands  case GlobalValue::LinkOnceODRLinkage:
2749667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands  case GlobalValue::WeakAnyLinkage:
2750667d4b8de6dea70195ff12ef39a4deebffa2f5c7Duncan Sands  case GlobalValue::WeakODRLinkage:
2751df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case GlobalValue::DLLExportLinkage:
2752df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!isDefine)
2753df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(LinkageLoc, "invalid linkage for function declaration");
2754df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    break;
2755df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case GlobalValue::AppendingLinkage:
27564dc2b39bf89d7c87868008ef8a0f807e0419aca6Duncan Sands  case GlobalValue::CommonLinkage:
2757df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(LinkageLoc, "invalid function linkage type");
2758df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2759a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
276099bb315f32a5f82f9e4978345ee39177db973242Chris Lattner  if (!FunctionType::isValidReturnType(RetType) ||
276147c5188789bc40671504ed1fa3a44765cefba44fDuncan Sands      RetType->isOpaqueTy())
2762df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(RetTypeLoc, "invalid function return type");
2763a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2764f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner  LocTy NameLoc = Lex.getLoc();
2765f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner
2766f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner  std::string FunctionName;
2767f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner  if (Lex.getKind() == lltok::GlobalVar) {
2768f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner    FunctionName = Lex.getStrVal();
2769f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner  } else if (Lex.getKind() == lltok::GlobalID) {     // @42 is ok.
2770f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner    unsigned NameID = Lex.getUIntVal();
2771f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner
2772f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner    if (NameID != NumberedVals.size())
2773f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner      return TokError("function expected to be numbered '%" +
2774f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner                      utostr(NumberedVals.size()) + "'");
2775f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner  } else {
2776df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return TokError("expected function name");
2777f570e621ab6eec6c7656cc4c511e19aba12b42cdChris Lattner  }
2778a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
27793ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  Lex.Lex();
2780a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
27813ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (Lex.getKind() != lltok::lparen)
2782df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return TokError("expected '(' in function argument list");
2783a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2784df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::vector<ArgInfo> ArgList;
2785df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  bool isVarArg;
2786df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned FuncAttrs;
2787df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::string Section;
2788df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned Alignment;
27893ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  std::string GC;
27903ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner
2791dfd19ddcfa7568d2118fbc3c6da612295200c31cChris Lattner  if (ParseArgumentList(ArgList, isVarArg, false) ||
27923ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      ParseOptionalAttrs(FuncAttrs, 2) ||
27933ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      (EatIfPresent(lltok::kw_section) &&
27943ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner       ParseStringConstant(Section)) ||
27953ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      ParseOptionalAlignment(Alignment) ||
27963ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      (EatIfPresent(lltok::kw_gc) &&
27973ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner       ParseStringConstant(GC)))
27983ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    return true;
2799df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2800df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If the alignment was parsed as an attribute, move to the alignment field.
2801df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (FuncAttrs & Attribute::Alignment) {
2802df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Alignment = Attribute::getAlignmentFromAttrs(FuncAttrs);
2803df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    FuncAttrs &= ~Attribute::Alignment;
2804df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2805a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2806df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Okay, if we got here, the function is syntactically valid.  Convert types
2807df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // and do semantic checks.
2808df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::vector<const Type*> ParamTypeList;
2809df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallVector<AttributeWithIndex, 8> Attrs;
2810a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar  // FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
2811df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // attributes.
2812df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
2813df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (FuncAttrs & ObsoleteFuncAttrs) {
2814df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    RetAttrs |= FuncAttrs & ObsoleteFuncAttrs;
2815df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    FuncAttrs &= ~ObsoleteFuncAttrs;
2816df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2817a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2818df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (RetAttrs != Attribute::None)
2819df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
2820a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2821df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
2822df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ParamTypeList.push_back(ArgList[i].Type);
2823df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ArgList[i].Attrs != Attribute::None)
2824df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
2825df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2826df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2827df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (FuncAttrs != Attribute::None)
2828df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Attrs.push_back(AttributeWithIndex::get(~0, FuncAttrs));
2829df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2830df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
2831a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2832f012705c7e4ca8cf90b6b734ce1d5355daca5ba5Benjamin Kramer  if (PAL.paramHasAttr(1, Attribute::StructRet) && !RetType->isVoidTy())
2833a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar    return Error(RetTypeLoc, "functions with 'sret' argument must return void");
2834a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2835fba933c823a2862067e0696bd5032ab18185bd77Owen Anderson  const FunctionType *FT =
2836debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson    FunctionType::get(RetType, ParamTypeList, isVarArg);
2837debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson  const PointerType *PFT = PointerType::getUnqual(FT);
2838df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2839df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Fn = 0;
2840df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!FunctionName.empty()) {
2841df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // If this was a definition of a forward reference, remove the definition
2842df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // from the forward reference table and fill in the forward ref.
2843df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
2844df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ForwardRefVals.find(FunctionName);
2845df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (FRVI != ForwardRefVals.end()) {
2846df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Fn = M->getFunction(FunctionName);
2847f1cfb953375b726f999976924f95fb80db3c1c97Chris Lattner      if (Fn->getType() != PFT)
2848f1cfb953375b726f999976924f95fb80db3c1c97Chris Lattner        return Error(FRVI->second.second, "invalid forward reference to "
2849f1cfb953375b726f999976924f95fb80db3c1c97Chris Lattner                     "function '" + FunctionName + "' with wrong type!");
2850f1cfb953375b726f999976924f95fb80db3c1c97Chris Lattner
2851df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ForwardRefVals.erase(FRVI);
2852df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    } else if ((Fn = M->getFunction(FunctionName))) {
2853df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // If this function already exists in the symbol table, then it is
2854df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // multiply defined.  We accept a few cases for old backwards compat.
2855df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      // FIXME: Remove this stuff for LLVM 3.0.
2856df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (Fn->getType() != PFT || Fn->getAttributes() != PAL ||
2857df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner          (!Fn->isDeclaration() && isDefine)) {
2858df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        // If the redefinition has different type or different attributes,
2859df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        // reject it.  If both have bodies, reject it.
2860df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(NameLoc, "invalid redefinition of function '" +
2861df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                     FunctionName + "'");
2862df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      } else if (Fn->isDeclaration()) {
2863df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        // Make sure to strip off any argument names so we can't get conflicts.
2864df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2865df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner             AI != AE; ++AI)
2866df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner          AI->setName("");
2867df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      }
28681d871c5e547996a8dd5192e48d22aadf11cd2a9dChris Lattner    } else if (M->getNamedValue(FunctionName)) {
28691d871c5e547996a8dd5192e48d22aadf11cd2a9dChris Lattner      return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
2870df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
2871a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2872419055453cdc4f9be4a3ad80f9b49e881d528fe9Dan Gohman  } else {
2873df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // If this is a definition of a forward referenced function, make sure the
2874df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // types agree.
2875df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
2876df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      = ForwardRefValIDs.find(NumberedVals.size());
2877df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (I != ForwardRefValIDs.end()) {
2878df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Fn = cast<Function>(I->second.first);
2879df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (Fn->getType() != PFT)
2880df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return Error(NameLoc, "type of definition and forward reference of '@" +
2881df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                     utostr(NumberedVals.size()) +"' disagree");
2882df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ForwardRefValIDs.erase(I);
2883df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
2884df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2885df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2886df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Fn == 0)
2887df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
2888df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  else // Move the forward-reference to the correct spot in the module.
2889df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
2890df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2891df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (FunctionName.empty())
2892df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    NumberedVals.push_back(Fn);
2893a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2894df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
2895df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
2896df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Fn->setCallingConv(CC);
2897df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Fn->setAttributes(PAL);
2898df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Fn->setAlignment(Alignment);
2899df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Fn->setSection(Section);
2900df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!GC.empty()) Fn->setGC(GC.c_str());
2901a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2902df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Add all of the arguments we parsed to the function.
2903df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Function::arg_iterator ArgIt = Fn->arg_begin();
2904df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
29055bda3792b44c8f5e1f9b974a18eb6061c3d27f89Chris Lattner    // If we run out of arguments in the Function prototype, exit early.
29065bda3792b44c8f5e1f9b974a18eb6061c3d27f89Chris Lattner    // FIXME: REMOVE THIS IN LLVM 3.0, this is just for the mismatch case above.
29075bda3792b44c8f5e1f9b974a18eb6061c3d27f89Chris Lattner    if (ArgIt == Fn->arg_end()) break;
29085bda3792b44c8f5e1f9b974a18eb6061c3d27f89Chris Lattner
2909df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // If the argument has a name, insert it into the argument symbol table.
2910df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ArgList[i].Name.empty()) continue;
2911a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2912df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // Set the name, if it conflicted, it will be auto-renamed.
2913df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    ArgIt->setName(ArgList[i].Name);
2914a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2915df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ArgIt->getNameStr() != ArgList[i].Name)
2916df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ArgList[i].Loc, "redefinition of argument '%" +
2917df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   ArgList[i].Name + "'");
2918df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2919a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2920df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
2921df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
2922df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2923df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2924df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseFunctionBody
2925df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= '{' BasicBlock+ '}'
2926df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'begin' BasicBlock+ 'end'  // FIXME: remove in LLVM 3.0
2927df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///
2928df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseFunctionBody(Function &Fn) {
2929df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Lex.getKind() != lltok::lbrace && Lex.getKind() != lltok::kw_begin)
2930df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return TokError("expected '{' in function body");
2931df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();  // eat the {.
2932a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
293309d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  int FunctionNumber = -1;
293409d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
293509d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner
293609d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  PerFunctionState PFS(*this, Fn, FunctionNumber);
2937a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
29382fdf8dbed5661336059f647700e66bca1f4cd51aChris Lattner  // We need at least one basic block.
29392fdf8dbed5661336059f647700e66bca1f4cd51aChris Lattner  if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_end)
29402fdf8dbed5661336059f647700e66bca1f4cd51aChris Lattner    return TokError("function body requires at least one basic block");
29412fdf8dbed5661336059f647700e66bca1f4cd51aChris Lattner
2942df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  while (Lex.getKind() != lltok::rbrace && Lex.getKind() != lltok::kw_end)
2943df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseBasicBlock(PFS)) return true;
2944a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2945df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Eat the }.
2946df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
2947a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2948df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Verify function is ok.
294909d9ef4122414a1a2ec95f52d660d6500f2819d0Chris Lattner  return PFS.FinishFunction();
2950df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
2951df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
2952df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseBasicBlock
2953df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= LabelStr? Instruction*
2954df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
2955df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If this basic block starts out with a name, remember it.
2956df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::string Name;
2957df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy NameLoc = Lex.getLoc();
2958df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Lex.getKind() == lltok::LabelStr) {
2959df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Name = Lex.getStrVal();
2960df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Lex.Lex();
2961df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
2962a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2963df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
2964df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (BB == 0) return true;
2965a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2966df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  std::string NameStr;
2967a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2968df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Parse the instructions in this block until we get a terminator.
2969df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Instruction *Inst;
29701340dd37087c1d47a85a4461a8d33cd6ccbcfaafChris Lattner  SmallVector<std::pair<unsigned, MDNode *>, 4> MetadataOnInst;
2971df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  do {
2972df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // This instruction may have three possibilities for a name: a) none
2973df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // specified, b) name specified "%foo =", c) number specified: "%4 =".
2974df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    LocTy NameLoc = Lex.getLoc();
2975df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    int NameID = -1;
2976df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    NameStr = "";
2977a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
2978df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (Lex.getKind() == lltok::LocalVarID) {
2979df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      NameID = Lex.getUIntVal();
2980df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Lex.Lex();
2981df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (ParseToken(lltok::equal, "expected '=' after instruction id"))
2982df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return true;
2983df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    } else if (Lex.getKind() == lltok::LocalVar ||
2984df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner               // FIXME: REMOVE IN LLVM 3.0
2985df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner               Lex.getKind() == lltok::StringConstant) {
2986df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      NameStr = Lex.getStrVal();
2987df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Lex.Lex();
2988df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      if (ParseToken(lltok::equal, "expected '=' after instruction name"))
2989df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        return true;
2990df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
2991f633a065de959b32892c51d741d4db010c5d25b1Devang Patel
2992f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner    switch (ParseInstruction(Inst, BB, PFS)) {
2993f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner    default: assert(0 && "Unknown ParseInstruction result!");
2994f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner    case InstError: return true;
2995f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner    case InstNormal:
29964ba9d9b67be87ce82f71023643b759ff12fdb411Chris Lattner      BB->getInstList().push_back(Inst);
29974ba9d9b67be87ce82f71023643b759ff12fdb411Chris Lattner
2998f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner      // With a normal result, we check to see if the instruction is followed by
2999f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner      // a comma and metadata.
3000f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner      if (EatIfPresent(lltok::comma))
30019d072f51ebf61edda9c31f831c2642d110170d13Dan Gohman        if (ParseInstructionMetadata(Inst, &PFS))
3002f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner          return true;
3003f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner      break;
3004f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner    case InstExtraComma:
30054ba9d9b67be87ce82f71023643b759ff12fdb411Chris Lattner      BB->getInstList().push_back(Inst);
30064ba9d9b67be87ce82f71023643b759ff12fdb411Chris Lattner
3007f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner      // If the instruction parser ate an extra comma at the end of it, it
3008f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner      // *must* be followed by metadata.
30099d072f51ebf61edda9c31f831c2642d110170d13Dan Gohman      if (ParseInstructionMetadata(Inst, &PFS))
3010f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner        return true;
3011f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner      break;
3012f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner    }
3013f633a065de959b32892c51d741d4db010c5d25b1Devang Patel
3014df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // Set the name on the instruction.
3015df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3016df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  } while (!isa<TerminatorInst>(Inst));
3017a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3018df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3019df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3020df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3021df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
3022df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Instruction Parsing.
3023df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
3024df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3025df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseInstruction - Parse one of the many different instructions.
3026df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///
3027f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattnerint LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3028f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner                               PerFunctionState &PFS) {
3029df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  lltok::Kind Token = Lex.getKind();
3030df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Token == lltok::Eof)
3031df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return TokError("found end of file when expecting more instructions");
3032df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy Loc = Lex.getLoc();
3033f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner  unsigned KeywordVal = Lex.getUIntVal();
3034df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();  // Eat the keyword.
3035a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3036df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  switch (Token) {
3037df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  default:                    return Error(Loc, "expected instruction opcode");
3038df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Terminator Instructions.
30391d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  case lltok::kw_unwind:      Inst = new UnwindInst(Context); return false;
30401d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
3041df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_ret:         return ParseRet(Inst, BB, PFS);
3042df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_br:          return ParseBr(Inst, PFS);
3043df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_switch:      return ParseSwitch(Inst, PFS);
3044ab21db79ef1d2530880ad11f21f0b87ffca02dd4Chris Lattner  case lltok::kw_indirectbr:  return ParseIndirectBr(Inst, PFS);
3045df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_invoke:      return ParseInvoke(Inst, PFS);
3046df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Binary Operators.
3047df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_add:
3048df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_sub:
304959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman  case lltok::kw_mul: {
305059858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    bool NUW = false;
305159858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    bool NSW = false;
305259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    LocTy ModifierLoc = Lex.getLoc();
305359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    if (EatIfPresent(lltok::kw_nuw))
305459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      NUW = true;
305559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    if (EatIfPresent(lltok::kw_nsw)) {
305659858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      NSW = true;
305759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      if (EatIfPresent(lltok::kw_nuw))
305859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman        NUW = true;
305959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    }
30601eaac532615a54a1133367dc2c4ceaa79a150a9dDan Gohman    bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 1);
306159858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    if (!Result) {
3062b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands      if (!Inst->getType()->isIntOrIntVectorTy()) {
306359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman        if (NUW)
306459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman          return Error(ModifierLoc, "nuw only applies to integer operations");
306559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman        if (NSW)
306659858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman          return Error(ModifierLoc, "nsw only applies to integer operations");
306759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      }
306859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      if (NUW)
3069f8dbee7cea072eb63ae343759975109553697bcbDan Gohman        cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
307059858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      if (NSW)
3071f8dbee7cea072eb63ae343759975109553697bcbDan Gohman        cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
307259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    }
307359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    return Result;
307459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman  }
3075ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  case lltok::kw_fadd:
3076ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  case lltok::kw_fsub:
3077ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  case lltok::kw_fmul:    return ParseArithmetic(Inst, PFS, KeywordVal, 2);
3078ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman
307959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman  case lltok::kw_sdiv: {
308059858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    bool Exact = false;
308159858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    if (EatIfPresent(lltok::kw_exact))
308259858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      Exact = true;
308359858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    bool Result = ParseArithmetic(Inst, PFS, KeywordVal, 1);
308459858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    if (!Result)
308559858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman      if (Exact)
3086f8dbee7cea072eb63ae343759975109553697bcbDan Gohman        cast<BinaryOperator>(Inst)->setIsExact(true);
308759858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman    return Result;
308859858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman  }
308959858cf7926702596cf708daf21aeaf5b6feedf8Dan Gohman
3090df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_udiv:
3091df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_urem:
3092f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner  case lltok::kw_srem:   return ParseArithmetic(Inst, PFS, KeywordVal, 1);
3093e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner  case lltok::kw_fdiv:
3094f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner  case lltok::kw_frem:   return ParseArithmetic(Inst, PFS, KeywordVal, 2);
3095df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_shl:
3096df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_lshr:
3097df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_ashr:
3098df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_and:
3099df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_or:
3100f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner  case lltok::kw_xor:    return ParseLogical(Inst, PFS, KeywordVal);
3101df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_icmp:
31027f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky  case lltok::kw_fcmp:   return ParseCompare(Inst, PFS, KeywordVal);
3103df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Casts.
3104df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_trunc:
3105df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_zext:
3106df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_sext:
3107df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_fptrunc:
3108df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_fpext:
3109df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_bitcast:
3110df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_uitofp:
3111df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_sitofp:
3112df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_fptoui:
3113a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar  case lltok::kw_fptosi:
3114df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_inttoptr:
3115f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner  case lltok::kw_ptrtoint:       return ParseCast(Inst, PFS, KeywordVal);
3116df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Other.
3117df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_select:         return ParseSelect(Inst, PFS);
31180088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner  case lltok::kw_va_arg:         return ParseVA_Arg(Inst, PFS);
3119df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3120df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_insertelement:  return ParseInsertElement(Inst, PFS);
3121df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_shufflevector:  return ParseShuffleVector(Inst, PFS);
3122df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_phi:            return ParsePHI(Inst, PFS);
3123df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_call:           return ParseCall(Inst, PFS, false);
3124df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_tail:           return ParseCall(Inst, PFS, true);
3125df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Memory.
312613ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez  case lltok::kw_alloca:         return ParseAlloc(Inst, PFS);
312713ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez  case lltok::kw_malloc:         return ParseAlloc(Inst, PFS, BB, false);
312866284e063a1e46500acae48bdc0e4a00652021d1Victor Hernandez  case lltok::kw_free:           return ParseFree(Inst, PFS, BB);
3129df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_load:           return ParseLoad(Inst, PFS, false);
3130df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_store:          return ParseStore(Inst, PFS, false);
3131df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_volatile:
31323ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    if (EatIfPresent(lltok::kw_load))
3133df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return ParseLoad(Inst, PFS, true);
31343ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    else if (EatIfPresent(lltok::kw_store))
3135df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return ParseStore(Inst, PFS, true);
31363ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    else
3137df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return TokError("expected 'load' or 'store'");
3138df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_getresult:     return ParseGetResult(Inst, PFS);
3139df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3140df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_extractvalue:  return ParseExtractValue(Inst, PFS);
3141df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  case lltok::kw_insertvalue:   return ParseInsertValue(Inst, PFS);
3142df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3143df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3144df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3145df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3146df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
31477f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky  if (Opc == Instruction::FCmp) {
3148df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    switch (Lex.getKind()) {
3149df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    default: TokError("expected fcmp predicate (e.g. 'oeq')");
3150df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3151df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3152df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3153df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3154df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3155df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3156df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3157df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3158df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3159df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3160df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3161df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3162df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3163df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3164df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3165df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3166df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
3167df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  } else {
3168df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    switch (Lex.getKind()) {
3169df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    default: TokError("expected icmp predicate (e.g. 'eq')");
3170df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_eq:  P = CmpInst::ICMP_EQ; break;
3171df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_ne:  P = CmpInst::ICMP_NE; break;
3172df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3173df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3174df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3175df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3176df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3177df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3178df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3179df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3180df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
3181df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3182df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();
3183df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3184df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3185df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3186df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
3187df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Terminator Instructions.
3188df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
3189df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3190df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseRet - Parse a return instruction.
31913f3a0f6b498e80ffaeb83f03c8697e5d54fd9337Chris Lattner///   ::= 'ret' void (',' !dbg, !1)*
31923f3a0f6b498e80ffaeb83f03c8697e5d54fd9337Chris Lattner///   ::= 'ret' TypeAndValue (',' !dbg, !1)*
31933f3a0f6b498e80ffaeb83f03c8697e5d54fd9337Chris Lattner///   ::= 'ret' TypeAndValue (',' TypeAndValue)+  (',' !dbg, !1)*
3194f633a065de959b32892c51d741d4db010c5d25b1Devang Patel///         [[obsolete: LLVM 3.0]]
3195f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattnerint LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
3196f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner                       PerFunctionState &PFS) {
31971d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder Ty(Type::getVoidTy(Context));
3198a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner  if (ParseType(Ty, true /*void allowed*/)) return true;
3199a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3200cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner  if (Ty->isVoidTy()) {
32011d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson    Inst = ReturnInst::Create(Context);
3202df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
3203df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3204a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3205df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *RV;
3206df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseValue(Ty, RV, PFS)) return true;
3207df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3208f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner  bool ExtraComma = false;
3209f633a065de959b32892c51d741d4db010c5d25b1Devang Patel  if (EatIfPresent(lltok::comma)) {
32100475c9170e9621292a48c688c3976031be5650bbDevang Patel    // Parse optional custom metadata, e.g. !dbg
32111d92831759620a2e5ce4f5a3088c0a1a77a48c8fChris Lattner    if (Lex.getKind() == lltok::MetadataVar) {
3212f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner      ExtraComma = true;
3213f633a065de959b32892c51d741d4db010c5d25b1Devang Patel    } else {
3214f633a065de959b32892c51d741d4db010c5d25b1Devang Patel      // The normal case is one return value.
32153f3a0f6b498e80ffaeb83f03c8697e5d54fd9337Chris Lattner      // FIXME: LLVM 3.0 remove MRV support for 'ret i32 1, i32 2', requiring
32163f3a0f6b498e80ffaeb83f03c8697e5d54fd9337Chris Lattner      // use of 'ret {i32,i32} {i32 1, i32 2}'
3217f633a065de959b32892c51d741d4db010c5d25b1Devang Patel      SmallVector<Value*, 8> RVs;
3218f633a065de959b32892c51d741d4db010c5d25b1Devang Patel      RVs.push_back(RV);
3219a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3220f633a065de959b32892c51d741d4db010c5d25b1Devang Patel      do {
32210475c9170e9621292a48c688c3976031be5650bbDevang Patel        // If optional custom metadata, e.g. !dbg is seen then this is the
32220475c9170e9621292a48c688c3976031be5650bbDevang Patel        // end of MRV.
32231d92831759620a2e5ce4f5a3088c0a1a77a48c8fChris Lattner        if (Lex.getKind() == lltok::MetadataVar)
3224a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar          break;
3225a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar        if (ParseTypeAndValue(RV, PFS)) return true;
3226a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar        RVs.push_back(RV);
3227f633a065de959b32892c51d741d4db010c5d25b1Devang Patel      } while (EatIfPresent(lltok::comma));
3228f633a065de959b32892c51d741d4db010c5d25b1Devang Patel
3229f633a065de959b32892c51d741d4db010c5d25b1Devang Patel      RV = UndefValue::get(PFS.getFunction().getReturnType());
3230f633a065de959b32892c51d741d4db010c5d25b1Devang Patel      for (unsigned i = 0, e = RVs.size(); i != e; ++i) {
3231a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar        Instruction *I = InsertValueInst::Create(RV, RVs[i], i, "mrv");
3232a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar        BB->getInstList().push_back(I);
3233a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar        RV = I;
3234f633a065de959b32892c51d741d4db010c5d25b1Devang Patel      }
3235df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
3236df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3237f633a065de959b32892c51d741d4db010c5d25b1Devang Patel
32381d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  Inst = ReturnInst::Create(Context, RV);
3239f1bc7ce7b362d2859349c7a1e2bc6df493d0f809Chris Lattner  return ExtraComma ? InstExtraComma : InstNormal;
3240df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3241df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3242df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3243df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseBr
3244df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'br' TypeAndValue
3245df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3246df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3247df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy Loc, Loc2;
3248f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  Value *Op0;
3249f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  BasicBlock *Op1, *Op2;
3250df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
3251a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3252df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3253df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Inst = BranchInst::Create(BB);
3254df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return false;
3255df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3256a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
32571d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  if (Op0->getType() != Type::getInt1Ty(Context))
3258df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc, "branch condition must have 'i1' type");
3259a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3260df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
3261f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner      ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
3262df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after true destination") ||
3263f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner      ParseTypeAndBasicBlock(Op2, Loc2, PFS))
3264df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3265a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3266f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  Inst = BranchInst::Create(Op1, Op2, Op0);
3267df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3268df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3269df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3270df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseSwitch
3271df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///  Instruction
3272df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///    ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3273df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///  JumpTable
3274df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///    ::= (TypeAndValue ',' TypeAndValue)*
3275df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3276df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy CondLoc, BBLoc;
3277f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  Value *Cond;
3278f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  BasicBlock *DefaultBB;
3279df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3280df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after switch condition") ||
3281f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner      ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
3282df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::lsquare, "expected '[' with switch table"))
3283df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3284df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
32851df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!Cond->getType()->isIntegerTy())
3286df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(CondLoc, "switch condition must have integer type");
3287a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3288df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Parse the jump table pairs.
3289df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallPtrSet<Value*, 32> SeenCases;
3290df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3291df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  while (Lex.getKind() != lltok::rsquare) {
3292f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner    Value *Constant;
3293f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner    BasicBlock *DestBB;
3294a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3295df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3296df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::comma, "expected ',' after case value") ||
3297f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner        ParseTypeAndBasicBlock(DestBB, PFS))
3298df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
3299f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner
3300df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!SeenCases.insert(Constant))
3301df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(CondLoc, "duplicate case value in switch");
3302df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!isa<ConstantInt>(Constant))
3303df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(CondLoc, "case value is not a constant integer");
3304a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3305f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner    Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
3306df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3307a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3308df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Lex.Lex();  // Eat the ']'.
3309a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3310f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
3311df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (unsigned i = 0, e = Table.size(); i != e; ++i)
3312df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    SI->addCase(Table[i].first, Table[i].second);
3313df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = SI;
3314df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3315df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3316df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3317ab21db79ef1d2530880ad11f21f0b87ffca02dd4Chris Lattner/// ParseIndirectBr
3318f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner///  Instruction
3319ab21db79ef1d2530880ad11f21f0b87ffca02dd4Chris Lattner///    ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3320ab21db79ef1d2530880ad11f21f0b87ffca02dd4Chris Lattnerbool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
3321f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  LocTy AddrLoc;
3322f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  Value *Address;
3323f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
3324ab21db79ef1d2530880ad11f21f0b87ffca02dd4Chris Lattner      ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3325ab21db79ef1d2530880ad11f21f0b87ffca02dd4Chris Lattner      ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
3326f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner    return true;
3327f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner
33281df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!Address->getType()->isPointerTy())
3329ab21db79ef1d2530880ad11f21f0b87ffca02dd4Chris Lattner    return Error(AddrLoc, "indirectbr address must have pointer type");
3330f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner
3331f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  // Parse the destination list.
3332f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  SmallVector<BasicBlock*, 16> DestList;
3333f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner
3334f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  if (Lex.getKind() != lltok::rsquare) {
3335f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner    BasicBlock *DestBB;
3336f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner    if (ParseTypeAndBasicBlock(DestBB, PFS))
3337f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner      return true;
3338f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner    DestList.push_back(DestBB);
3339f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner
3340f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner    while (EatIfPresent(lltok::comma)) {
3341f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner      if (ParseTypeAndBasicBlock(DestBB, PFS))
3342f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner        return true;
3343f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner      DestList.push_back(DestBB);
3344f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner    }
3345f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  }
3346f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner
3347f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3348f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner    return true;
3349f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner
3350ab21db79ef1d2530880ad11f21f0b87ffca02dd4Chris Lattner  IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
3351f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3352f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner    IBI->addDestination(DestList[i]);
3353f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  Inst = IBI;
3354f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  return false;
3355f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner}
3356f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner
3357f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner
3358df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseInvoke
3359df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3360df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///       OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3361df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3362df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy CallLoc = Lex.getLoc();
336365c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel  unsigned RetAttrs, FnAttrs;
336465c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel  CallingConv::ID CC;
33651d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder RetType(Type::getVoidTy(Context));
3366df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy RetTypeLoc;
3367df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  ValID CalleeID;
3368df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallVector<ParamInfo, 16> ArgList;
3369df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3370f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  BasicBlock *NormalBB, *UnwindBB;
3371df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseOptionalCallingConv(CC) ||
3372df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseOptionalAttrs(RetAttrs, 1) ||
3373a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner      ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
3374df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseValID(CalleeID) ||
3375df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseParameterList(ArgList, PFS) ||
3376df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseOptionalAttrs(FnAttrs, 2) ||
3377df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
3378f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner      ParseTypeAndBasicBlock(NormalBB, PFS) ||
3379df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
3380f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner      ParseTypeAndBasicBlock(UnwindBB, PFS))
3381df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3382a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3383df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If RetType is a non-function pointer type, then this is the short syntax
3384df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // for the call, which means that RetType is just the return type.  Infer the
3385df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // rest of the function argument types from the arguments that are present.
3386df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  const PointerType *PFTy = 0;
3387df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  const FunctionType *Ty = 0;
3388df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3389df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3390df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // Pull out the types of all of the arguments...
3391df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    std::vector<const Type*> ParamTypes;
3392df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3393df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParamTypes.push_back(ArgList[i].V->getType());
3394a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3395df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!FunctionType::isValidReturnType(RetType))
3396df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(RetTypeLoc, "Invalid result type for LLVM function");
3397a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3398debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson    Ty = FunctionType::get(RetType, ParamTypes, false);
3399debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson    PFTy = PointerType::getUnqual(Ty);
3400df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3401a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3402df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Look up the callee.
3403df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Callee;
340492f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
3405a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3406df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional
3407df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // function attributes.
3408df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
3409df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (FnAttrs & ObsoleteFuncAttrs) {
3410df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    RetAttrs |= FnAttrs & ObsoleteFuncAttrs;
3411df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    FnAttrs &= ~ObsoleteFuncAttrs;
3412df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3413a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3414df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Set up the Attributes for the function.
3415df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallVector<AttributeWithIndex, 8> Attrs;
3416df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (RetAttrs != Attribute::None)
3417df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
3418a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3419df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallVector<Value*, 8> Args;
3420a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3421df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Loop through FunctionType's arguments and ensure they are specified
3422df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // correctly.  Also, gather any parameter attributes.
3423df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  FunctionType::param_iterator I = Ty->param_begin();
3424df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  FunctionType::param_iterator E = Ty->param_end();
3425df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
3426df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    const Type *ExpectedTy = 0;
3427df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (I != E) {
3428df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ExpectedTy = *I++;
3429df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    } else if (!Ty->isVarArg()) {
3430df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ArgList[i].Loc, "too many arguments specified");
3431df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
3432a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3433df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3434df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ArgList[i].Loc, "argument is not of expected type '" +
3435df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   ExpectedTy->getDescription() + "'");
3436df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Args.push_back(ArgList[i].V);
3437df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ArgList[i].Attrs != Attribute::None)
3438df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
3439df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3440a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3441df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (I != E)
3442df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(CallLoc, "not enough parameters specified for call");
3443a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3444df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (FnAttrs != Attribute::None)
3445df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs));
3446a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3447df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Finish off the Attributes and check them
3448df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
3449a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3450f9be95f867745b6754b2402b9b72f9eaeabd637fChris Lattner  InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB,
3451df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                      Args.begin(), Args.end());
3452df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  II->setCallingConv(CC);
3453df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  II->setAttributes(PAL);
3454df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = II;
3455df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3456df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3457df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3458df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3459df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3460df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
3461df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Binary Operators.
3462df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
3463df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3464df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseArithmetic
3465e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner///  ::= ArithmeticOps TypeAndValue ',' Value
3466e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner///
3467e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner/// If OperandType is 0, then any FP or integer operand is allowed.  If it is 1,
3468e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
3469df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
3470e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner                               unsigned Opc, unsigned OperandType) {
3471df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy Loc; Value *LHS, *RHS;
3472df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(LHS, Loc, PFS) ||
3473df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3474df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseValue(LHS->getType(), RHS, PFS))
3475df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3476df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3477e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner  bool Valid;
3478e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner  switch (OperandType) {
3479c23197a26f34f559ea9797de51e187087c039c42Torok Edwin  default: llvm_unreachable("Unknown operand type!");
3480e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner  case 0: // int or FP.
3481b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands    Valid = LHS->getType()->isIntOrIntVectorTy() ||
3482b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands            LHS->getType()->isFPOrFPVectorTy();
3483e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner    break;
3484b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands  case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3485b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands  case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
3486e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner  }
3487a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3488e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner  if (!Valid)
3489e914b59c008745204c49fcf0751f9cc4d91f44bbChris Lattner    return Error(Loc, "invalid operand type for instruction");
3490a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3491df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3492df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3493df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3494df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3495df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseLogical
3496df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///  ::= ArithmeticOps TypeAndValue ',' Value {
3497df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3498df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                            unsigned Opc) {
3499df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy Loc; Value *LHS, *RHS;
3500df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(LHS, Loc, PFS) ||
3501df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' in logical operation") ||
3502df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseValue(LHS->getType(), RHS, PFS))
3503df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3504df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3505b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands  if (!LHS->getType()->isIntOrIntVectorTy())
3506df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc,"instruction requires integer or integer vector operands");
3507df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3508df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3509df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3510df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3511df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3512df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3513df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseCompare
3514df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///  ::= 'icmp' IPredicates TypeAndValue ',' Value
3515df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///  ::= 'fcmp' FPredicates TypeAndValue ',' Value
3516df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3517df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                            unsigned Opc) {
3518df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Parse the integer/fp comparison predicate.
3519df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy Loc;
3520df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned Pred;
3521df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *LHS, *RHS;
3522df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseCmpPredicate(Pred, Opc) ||
3523df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseTypeAndValue(LHS, Loc, PFS) ||
3524df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after compare value") ||
3525df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseValue(LHS->getType(), RHS, PFS))
3526df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3527a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3528df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (Opc == Instruction::FCmp) {
3529b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands    if (!LHS->getType()->isFPOrFPVectorTy())
3530df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(Loc, "fcmp requires floating point operands");
35311c8a23c440b1665ba422778cdc74a0c59ecaf39eDan Gohman    Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
35327f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky  } else {
35337f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky    assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
3534b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands    if (!LHS->getType()->isIntOrIntVectorTy() &&
35351df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands        !LHS->getType()->isPointerTy())
3536df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(Loc, "icmp requires integer operands");
35371c8a23c440b1665ba422778cdc74a0c59ecaf39eDan Gohman    Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
3538df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3539df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3540df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3541df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3542df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
3543df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Other Instructions.
3544df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
3545df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3546df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3547df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseCast
3548df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= CastOpc TypeAndValue 'to' Type
3549df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3550df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                         unsigned Opc) {
3551df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy Loc;  Value *Op;
35521d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder DestTy(Type::getVoidTy(Context));
3553df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Op, Loc, PFS) ||
3554df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3555df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseType(DestTy))
3556df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3557a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3558f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner  if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3559f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner    CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
3560df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc, "invalid cast opcode for cast from '" +
3561df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                 Op->getType()->getDescription() + "' to '" +
3562df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                 DestTy->getDescription() + "'");
3563f6f0bdfec336b596b095b4db8126149bbb33fec1Chris Lattner  }
3564df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3565df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3566df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3567df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3568df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseSelect
3569df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3570df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3571df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy Loc;
3572df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Op0, *Op1, *Op2;
3573df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Op0, Loc, PFS) ||
3574df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after select condition") ||
3575df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseTypeAndValue(Op1, PFS) ||
3576df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after select value") ||
3577df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseTypeAndValue(Op2, PFS))
3578df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3579a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3580df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3581df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc, Reason);
3582a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3583df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = SelectInst::Create(Op0, Op1, Op2);
3584df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3585df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3586df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
35870088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner/// ParseVA_Arg
35880088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner///   ::= 'va_arg' TypeAndValue ',' Type
35890088a5cc722b9e32820d31c70175a7650ad4772dChris Lattnerbool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
3590df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Op;
35911d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder EltTy(Type::getVoidTy(Context));
35920088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner  LocTy TypeLoc;
3593df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Op, PFS) ||
3594df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
35950088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner      ParseType(EltTy, TypeLoc))
3596df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3597a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
35980088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner  if (!EltTy->isFirstClassType())
35990088a5cc722b9e32820d31c70175a7650ad4772dChris Lattner    return Error(TypeLoc, "va_arg requires operand with first class type");
3600df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3601df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = new VAArgInst(Op, EltTy);
3602df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3603df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3604df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3605df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseExtractElement
3606df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'extractelement' TypeAndValue ',' TypeAndValue
3607df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3608df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy Loc;
3609df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Op0, *Op1;
3610df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Op0, Loc, PFS) ||
3611df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after extract value") ||
3612df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseTypeAndValue(Op1, PFS))
3613df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3614a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3615df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!ExtractElementInst::isValidOperands(Op0, Op1))
3616df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc, "invalid extractelement operands");
3617a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3618a3500da5592aee83675d6714d4f1e9d5ad96d1f2Eric Christopher  Inst = ExtractElementInst::Create(Op0, Op1);
3619df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3620df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3621df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3622df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseInsertElement
3623df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3624df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3625df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy Loc;
3626df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Op0, *Op1, *Op2;
3627df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Op0, Loc, PFS) ||
3628df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3629df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseTypeAndValue(Op1, PFS) ||
3630df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3631df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseTypeAndValue(Op2, PFS))
3632df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3633a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3634df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
36350aaf4e91c2961585c160af0cc87f040301aafbd2Eric Christopher    return Error(Loc, "invalid insertelement operands");
3636a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3637df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = InsertElementInst::Create(Op0, Op1, Op2);
3638df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3639df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3640df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3641df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseShuffleVector
3642df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3643df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3644df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy Loc;
3645df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Op0, *Op1, *Op2;
3646df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Op0, Loc, PFS) ||
3647df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3648df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseTypeAndValue(Op1, PFS) ||
3649df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3650df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseTypeAndValue(Op2, PFS))
3651df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3652a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3653df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
3654df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc, "invalid extractelement operands");
3655a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3656df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3657df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3658df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3659df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3660df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParsePHI
3661c6e200960812992f1124c92f801166bd1b387971Chris Lattner///   ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
3662a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattnerint LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
36631d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder Ty(Type::getVoidTy(Context));
3664df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Op0, *Op1;
3665df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy TypeLoc = Lex.getLoc();
3666a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3667df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseType(Ty) ||
3668df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3669df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseValue(Ty, Op0, PFS) ||
3670df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after insertelement value") ||
36711d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson      ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
3672df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3673df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3674a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3675a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner  bool AteExtraComma = false;
3676df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3677df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  while (1) {
3678df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
3679a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
36803ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    if (!EatIfPresent(lltok::comma))
3681df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      break;
3682df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3683a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner    if (Lex.getKind() == lltok::MetadataVar) {
3684a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner      AteExtraComma = true;
3685a43d46f523dff4b784ee28a3b196a065a15d90f2Devang Patel      break;
3686a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner    }
3687a43d46f523dff4b784ee28a3b196a065a15d90f2Devang Patel
36883ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3689df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseValue(Ty, Op0, PFS) ||
3690df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::comma, "expected ',' after insertelement value") ||
36911d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson        ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
3692df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner        ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3693df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return true;
3694df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3695a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3696df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!Ty->isFirstClassType())
3697df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(TypeLoc, "phi node must have first class type");
3698df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3699df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  PHINode *PN = PHINode::Create(Ty);
3700df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  PN->reserveOperandSpace(PHIVals.size());
3701df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3702df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3703df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = PN;
3704a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner  return AteExtraComma ? InstExtraComma : InstNormal;
3705df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3706df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3707df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseCall
3708df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value
3709df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///       ParameterList OptionalAttrs
3710df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
3711df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                         bool isTail) {
371265c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel  unsigned RetAttrs, FnAttrs;
371365c3c8f323198b99b88b109654194540cf9b3fa5Sandeep Patel  CallingConv::ID CC;
37141d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder RetType(Type::getVoidTy(Context));
3715df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy RetTypeLoc;
3716df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  ValID CalleeID;
3717df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallVector<ParamInfo, 16> ArgList;
3718df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  LocTy CallLoc = Lex.getLoc();
3719a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3720df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) ||
3721df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseOptionalCallingConv(CC) ||
3722df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseOptionalAttrs(RetAttrs, 1) ||
3723a9a9e07d1c5d3c73835e716d81c2ec94ad0b865fChris Lattner      ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
3724df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseValID(CalleeID) ||
3725df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseParameterList(ArgList, PFS) ||
3726df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseOptionalAttrs(FnAttrs, 2))
3727df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3728a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3729df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // If RetType is a non-function pointer type, then this is the short syntax
3730df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // for the call, which means that RetType is just the return type.  Infer the
3731df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // rest of the function argument types from the arguments that are present.
3732df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  const PointerType *PFTy = 0;
3733df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  const FunctionType *Ty = 0;
3734df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3735df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3736df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    // Pull out the types of all of the arguments...
3737df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    std::vector<const Type*> ParamTypes;
373883b4a97060c2c0a2ab08b8755c8f0915c49fd0cbEli Friedman    for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
373983b4a97060c2c0a2ab08b8755c8f0915c49fd0cbEli Friedman      ParamTypes.push_back(ArgList[i].V->getType());
3740a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3741df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (!FunctionType::isValidReturnType(RetType))
3742df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(RetTypeLoc, "Invalid result type for LLVM function");
3743a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3744debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson    Ty = FunctionType::get(RetType, ParamTypes, false);
3745debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson    PFTy = PointerType::getUnqual(Ty);
3746df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3747a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3748df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Look up the callee.
3749df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Callee;
375092f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez  if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
3751a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3752df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional
3753df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // function attributes.
3754df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
3755df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (FnAttrs & ObsoleteFuncAttrs) {
3756df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    RetAttrs |= FnAttrs & ObsoleteFuncAttrs;
3757df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    FnAttrs &= ~ObsoleteFuncAttrs;
3758df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3759df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3760df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Set up the Attributes for the function.
3761df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallVector<AttributeWithIndex, 8> Attrs;
3762df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (RetAttrs != Attribute::None)
3763df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
3764a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3765df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallVector<Value*, 8> Args;
3766a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3767df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Loop through FunctionType's arguments and ensure they are specified
3768df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // correctly.  Also, gather any parameter attributes.
3769df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  FunctionType::param_iterator I = Ty->param_begin();
3770df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  FunctionType::param_iterator E = Ty->param_end();
3771df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
3772df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    const Type *ExpectedTy = 0;
3773df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (I != E) {
3774df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ExpectedTy = *I++;
3775df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    } else if (!Ty->isVarArg()) {
3776df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ArgList[i].Loc, "too many arguments specified");
3777df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
3778a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3779df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3780df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(ArgList[i].Loc, "argument is not of expected type '" +
3781df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                   ExpectedTy->getDescription() + "'");
3782df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Args.push_back(ArgList[i].V);
3783df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    if (ArgList[i].Attrs != Attribute::None)
3784df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
3785df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3786a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3787df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (I != E)
3788df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(CallLoc, "not enough parameters specified for call");
3789df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3790df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (FnAttrs != Attribute::None)
3791df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs));
3792df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3793df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  // Finish off the Attributes and check them
3794df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
3795a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3796df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  CallInst *CI = CallInst::Create(Callee, Args.begin(), Args.end());
3797df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  CI->setTailCall(isTail);
3798df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  CI->setCallingConv(CC);
3799df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  CI->setAttributes(PAL);
3800df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = CI;
3801df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3802df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3803df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3804df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
3805df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner// Memory Instructions.
3806df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner//===----------------------------------------------------------------------===//
3807df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3808df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseAlloc
3809f633a065de959b32892c51d741d4db010c5d25b1Devang Patel///   ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalInfo)?
3810f633a065de959b32892c51d741d4db010c5d25b1Devang Patel///   ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)?
3811c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattnerint LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS,
3812c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner                         BasicBlock* BB, bool isAlloca) {
38131d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PATypeHolder Ty(Type::getVoidTy(Context));
3814df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Size = 0;
3815eeb4a84ac8d91fb1d5a7c484a1c7047409faee30Chris Lattner  LocTy SizeLoc;
3816df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned Alignment = 0;
38173ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (ParseType(Ty)) return true;
3818df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3819c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner  bool AteExtraComma = false;
38203ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (EatIfPresent(lltok::comma)) {
3821c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner    if (Lex.getKind() == lltok::kw_align) {
3822c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner      if (ParseOptionalAlignment(Alignment)) return true;
3823c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner    } else if (Lex.getKind() == lltok::MetadataVar) {
3824c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner      AteExtraComma = true;
3825f633a065de959b32892c51d741d4db010c5d25b1Devang Patel    } else {
3826c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner      if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
3827c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner          ParseOptionalCommaAlign(Alignment, AteExtraComma))
3828c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner        return true;
3829df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    }
3830df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3831df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3832f75a7d3fbf7fbb3071f45e248d3fb93312ec4fbdDan Gohman  if (Size && !Size->getType()->isIntegerTy())
3833f75a7d3fbf7fbb3071f45e248d3fb93312ec4fbdDan Gohman    return Error(SizeLoc, "element count must have integer type");
3834df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
383568afa54033eccda0f55e920cb548588e71a45418Victor Hernandez  if (isAlloca) {
383650dead06ffc107edb7e84857baaeeb09039c631cOwen Anderson    Inst = new AllocaInst(Ty, Size, Alignment);
3837c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner    return AteExtraComma ? InstExtraComma : InstNormal;
383813ad5aaaff8a446758b402fd5e9aea22f5bc5682Victor Hernandez  }
383968afa54033eccda0f55e920cb548588e71a45418Victor Hernandez
384068afa54033eccda0f55e920cb548588e71a45418Victor Hernandez  // Autoupgrade old malloc instruction to malloc call.
384168afa54033eccda0f55e920cb548588e71a45418Victor Hernandez  // FIXME: Remove in LLVM 3.0.
3842f75a7d3fbf7fbb3071f45e248d3fb93312ec4fbdDan Gohman  if (Size && !Size->getType()->isIntegerTy(32))
3843f75a7d3fbf7fbb3071f45e248d3fb93312ec4fbdDan Gohman    return Error(SizeLoc, "element count must be i32");
384468afa54033eccda0f55e920cb548588e71a45418Victor Hernandez  const Type *IntPtrTy = Type::getInt32Ty(Context);
38459d0b704e3ea418441001dac4d1a56c2c224cdbf5Victor Hernandez  Constant *AllocSize = ConstantExpr::getSizeOf(Ty);
38469d0b704e3ea418441001dac4d1a56c2c224cdbf5Victor Hernandez  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, IntPtrTy);
384768afa54033eccda0f55e920cb548588e71a45418Victor Hernandez  if (!MallocF)
384868afa54033eccda0f55e920cb548588e71a45418Victor Hernandez    // Prototype malloc as "void *(int32)".
384968afa54033eccda0f55e920cb548588e71a45418Victor Hernandez    // This function is renamed as "malloc" in ValidateEndOfModule().
3850336ea06493dfa03352d04d94cc8bc2c3c29f8165Victor Hernandez    MallocF = cast<Function>(
3851336ea06493dfa03352d04d94cc8bc2c3c29f8165Victor Hernandez       M->getOrInsertFunction("", Type::getInt8PtrTy(Context), IntPtrTy, NULL));
38529d0b704e3ea418441001dac4d1a56c2c224cdbf5Victor Hernandez  Inst = CallInst::CreateMalloc(BB, IntPtrTy, Ty, AllocSize, Size, MallocF);
3853c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattnerreturn AteExtraComma ? InstExtraComma : InstNormal;
3854df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3855df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3856df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseFree
3857df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'free' TypeAndValue
385866284e063a1e46500acae48bdc0e4a00652021d1Victor Hernandezbool LLParser::ParseFree(Instruction *&Inst, PerFunctionState &PFS,
385966284e063a1e46500acae48bdc0e4a00652021d1Victor Hernandez                         BasicBlock* BB) {
3860df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Val; LocTy Loc;
3861df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Val, Loc, PFS)) return true;
38621df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!Val->getType()->isPointerTy())
3863df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc, "operand to free must be a pointer");
386466284e063a1e46500acae48bdc0e4a00652021d1Victor Hernandez  Inst = CallInst::CreateFree(Val, BB);
3865df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3866df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3867df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3868df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseLoad
3869f633a065de959b32892c51d741d4db010c5d25b1Devang Patel///   ::= 'volatile'? 'load' TypeAndValue (',' OptionalInfo)?
3870c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattnerint LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS,
3871c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner                        bool isVolatile) {
3872df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Val; LocTy Loc;
3873f633a065de959b32892c51d741d4db010c5d25b1Devang Patel  unsigned Alignment = 0;
3874c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner  bool AteExtraComma = false;
3875c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner  if (ParseTypeAndValue(Val, Loc, PFS) ||
3876c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner      ParseOptionalCommaAlign(Alignment, AteExtraComma))
3877c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner    return true;
3878df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
38791df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!Val->getType()->isPointerTy() ||
3880df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
3881df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc, "load operand must be a pointer to a first class type");
3882a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3883df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = new LoadInst(Val, "", isVolatile, Alignment);
3884c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner  return AteExtraComma ? InstExtraComma : InstNormal;
3885df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3886df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3887df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseStore
3888a119de86a064414622562cfe32953de7f9b0ee40Dan Gohman///   ::= 'volatile'? 'store' TypeAndValue ',' TypeAndValue (',' 'align' i32)?
3889c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattnerint LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS,
3890c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner                         bool isVolatile) {
3891df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Val, *Ptr; LocTy Loc, PtrLoc;
3892f633a065de959b32892c51d741d4db010c5d25b1Devang Patel  unsigned Alignment = 0;
3893c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner  bool AteExtraComma = false;
3894df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Val, Loc, PFS) ||
3895df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after store operand") ||
3896c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner      ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
3897c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner      ParseOptionalCommaAlign(Alignment, AteExtraComma))
3898df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3899f633a065de959b32892c51d741d4db010c5d25b1Devang Patel
39001df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!Ptr->getType()->isPointerTy())
3901df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(PtrLoc, "store operand must be a pointer");
3902df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!Val->getType()->isFirstClassType())
3903df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc, "store operand must be a first class value");
3904df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
3905df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc, "stored value and pointer type do not match");
3906a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3907df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = new StoreInst(Val, Ptr, isVolatile, Alignment);
3908c3a6c5c83b5fa7b813de1741ac9e9be702b03846Chris Lattner  return AteExtraComma ? InstExtraComma : InstNormal;
3909df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3910df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3911df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseGetResult
3912a119de86a064414622562cfe32953de7f9b0ee40Dan Gohman///   ::= 'getresult' TypeAndValue ',' i32
3913df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// FIXME: Remove support for getresult in LLVM 3.0
3914df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattnerbool LLParser::ParseGetResult(Instruction *&Inst, PerFunctionState &PFS) {
3915df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Val; LocTy ValLoc, EltLoc;
3916df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  unsigned Element;
3917df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Val, ValLoc, PFS) ||
3918df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected ',' after getresult operand") ||
39193ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner      ParseUInt32(Element, EltLoc))
3920df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3921a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
39221df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!Val->getType()->isStructTy() && !Val->getType()->isArrayTy())
3923df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(ValLoc, "getresult inst requires an aggregate operand");
3924df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!ExtractValueInst::getIndexedType(Val->getType(), Element))
3925df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(EltLoc, "invalid getresult index for value");
3926df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = ExtractValueInst::Create(Val, Element);
3927df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  return false;
3928df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3929df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3930df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseGetElementPtr
3931dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman///   ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
3932a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattnerint LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
3933df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Ptr, *Val; LocTy Loc, EltLoc;
3934dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman
3935dcb40a3c1c747c88c4e634e34d1b4d7533a86adaDan Gohman  bool InBounds = EatIfPresent(lltok::kw_inbounds);
3936dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman
39373ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
3938a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
39391df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands  if (!Ptr->getType()->isPointerTy())
3940df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc, "base of getelementptr must be a pointer");
3941a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3942df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallVector<Value*, 16> Indices;
3943a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner  bool AteExtraComma = false;
39443ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner  while (EatIfPresent(lltok::comma)) {
3945a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner    if (Lex.getKind() == lltok::MetadataVar) {
3946a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner      AteExtraComma = true;
39476225d64ba244cdf4af3c44e113fc8c589fa39508Devang Patel      break;
3948a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner    }
39493ed88efb03c63aae5542efcdfc5cedec9bc4c18aChris Lattner    if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
39501df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands    if (!Val->getType()->isIntegerTy())
3951df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      return Error(EltLoc, "getelementptr index must be an integer");
3952df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    Indices.push_back(Val);
3953df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  }
3954a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3955df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!GetElementPtrInst::getIndexedType(Ptr->getType(),
3956df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                         Indices.begin(), Indices.end()))
3957df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc, "invalid getelementptr indices");
3958df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = GetElementPtrInst::Create(Ptr, Indices.begin(), Indices.end());
3959dd8004dc73d091ccb3927dbbc3b41639a3738ae3Dan Gohman  if (InBounds)
3960f8dbee7cea072eb63ae343759975109553697bcbDan Gohman    cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
3961a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner  return AteExtraComma ? InstExtraComma : InstNormal;
3962df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3963df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3964df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseExtractValue
3965df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'extractvalue' TypeAndValue (',' uint32)+
3966a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattnerint LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
3967df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Val; LocTy Loc;
3968df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallVector<unsigned, 4> Indices;
3969a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner  bool AteExtraComma;
3970df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Val, Loc, PFS) ||
3971a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner      ParseIndexList(Indices, AteExtraComma))
3972df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3973df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3974fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  if (!Val->getType()->isAggregateType())
3975fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    return Error(Loc, "extractvalue operand must be aggregate type");
3976df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3977df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
3978df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                        Indices.end()))
3979df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc, "invalid indices for extractvalue");
3980df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = ExtractValueInst::Create(Val, Indices.begin(), Indices.end());
3981a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner  return AteExtraComma ? InstExtraComma : InstNormal;
3982df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
3983df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner
3984df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner/// ParseInsertValue
3985df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner///   ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
3986a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattnerint LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
3987df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Value *Val0, *Val1; LocTy Loc0, Loc1;
3988df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  SmallVector<unsigned, 4> Indices;
3989a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner  bool AteExtraComma;
3990df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (ParseTypeAndValue(Val0, Loc0, PFS) ||
3991df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
3992df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner      ParseTypeAndValue(Val1, Loc1, PFS) ||
3993a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner      ParseIndexList(Indices, AteExtraComma))
3994df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return true;
3995628c13ad76fb4b7ce2f105c0e92644d1c39ee2f8Chris Lattner
3996fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner  if (!Val0->getType()->isAggregateType())
3997fdfeb6976f07ad10d809b922ed7376ba2a3539beChris Lattner    return Error(Loc0, "insertvalue operand must be aggregate type");
3998a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar
3999df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
4000df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner                                        Indices.end()))
4001df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner    return Error(Loc0, "invalid indices for insertvalue");
4002df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner  Inst = InsertValueInst::Create(Val0, Val1, Indices.begin(), Indices.end());
4003a7d7f2c0239ecc8513461ec69c8b922698ce5fe0Chris Lattner  return AteExtraComma ? InstExtraComma : InstNormal;
4004df98617b23315e427cc4fad8ccfdd50d68bec2f9Chris Lattner}
400521cc4460efa104e8591b05a90f20130291614344Nick Lewycky
400621cc4460efa104e8591b05a90f20130291614344Nick Lewycky//===----------------------------------------------------------------------===//
400721cc4460efa104e8591b05a90f20130291614344Nick Lewycky// Embedded metadata.
400821cc4460efa104e8591b05a90f20130291614344Nick Lewycky//===----------------------------------------------------------------------===//
400921cc4460efa104e8591b05a90f20130291614344Nick Lewycky
401021cc4460efa104e8591b05a90f20130291614344Nick Lewycky/// ParseMDNodeVector
4011cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky///   ::= Element (',' Element)*
4012cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky/// Element
4013cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky///   ::= 'null' | TypeAndValue
4014bf170d4c2ffd9dc99fad1fabfbf1f90f980a9782Victor Hernandezbool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
401524e64df7ec25b55aa872c2ef33728dfbb8c353c4Victor Hernandez                                 PerFunctionState *PFS) {
4016ac80975ea4103fcceab38cde69d98d3fb3b01db4Dan Gohman  // Check for an empty list.
4017ac80975ea4103fcceab38cde69d98d3fb3b01db4Dan Gohman  if (Lex.getKind() == lltok::rbrace)
4018ac80975ea4103fcceab38cde69d98d3fb3b01db4Dan Gohman    return false;
4019ac80975ea4103fcceab38cde69d98d3fb3b01db4Dan Gohman
402021cc4460efa104e8591b05a90f20130291614344Nick Lewycky  do {
4021a73523958d39ca10a322fd8b177104ebc2045130Chris Lattner    // Null is a special case since it is typeless.
4022a73523958d39ca10a322fd8b177104ebc2045130Chris Lattner    if (EatIfPresent(lltok::kw_null)) {
4023a73523958d39ca10a322fd8b177104ebc2045130Chris Lattner      Elts.push_back(0);
4024a73523958d39ca10a322fd8b177104ebc2045130Chris Lattner      continue;
4025cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky    }
4026a73523958d39ca10a322fd8b177104ebc2045130Chris Lattner
4027a73523958d39ca10a322fd8b177104ebc2045130Chris Lattner    Value *V = 0;
4028a73523958d39ca10a322fd8b177104ebc2045130Chris Lattner    PATypeHolder Ty(Type::getVoidTy(Context));
4029a73523958d39ca10a322fd8b177104ebc2045130Chris Lattner    ValID ID;
4030bf170d4c2ffd9dc99fad1fabfbf1f90f980a9782Victor Hernandez    if (ParseType(Ty) || ParseValID(ID, PFS) ||
403192f238dd8fe19b26103bd451c571281d9b7ad324Victor Hernandez        ConvertValIDToValue(Ty, ID, V, PFS))
4032a73523958d39ca10a322fd8b177104ebc2045130Chris Lattner      return true;
4033a73523958d39ca10a322fd8b177104ebc2045130Chris Lattner
4034cb33799b9f4e152e3460faa83e59b53ff604c87dNick Lewycky    Elts.push_back(V);
403521cc4460efa104e8591b05a90f20130291614344Nick Lewycky  } while (EatIfPresent(lltok::comma));
403621cc4460efa104e8591b05a90f20130291614344Nick Lewycky
403721cc4460efa104e8591b05a90f20130291614344Nick Lewycky  return false;
403821cc4460efa104e8591b05a90f20130291614344Nick Lewycky}
4039