LLVM  7.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups
llvm::LLParser::PerFunctionState Class Reference
Collaboration diagram for llvm::LLParser::PerFunctionState:

Public Member Functions

 PerFunctionState (LLParser &p, Function &f, int FunctionNumber)
 
 ~PerFunctionState ()
 
FunctiongetFunction () const
 
bool FinishFunction ()
 
ValueGetVal (const std::string &Name, Type *Ty, LocTy Loc)
 
ValueGetVal (unsigned ID, Type *Ty, LocTy Loc)
 
bool SetInstName (int NameID, const std::string &NameStr, LocTy NameLoc, Instruction *Inst)
 
BasicBlockGetBB (const std::string &Name, LocTy Loc)
 
BasicBlockGetBB (unsigned ID, LocTy Loc)
 
BasicBlockDefineBB (const std::string &Name, LocTy Loc)
 

Private Attributes

LLParserP
 
FunctionF
 
std::map< std::string,
std::pair< Value *, LocTy > > 
ForwardRefVals
 
std::map< unsigned, std::pair
< Value *, LocTy > > 
ForwardRefValIDs
 
std::vector< Value * > NumberedVals
 
int FunctionNumber
 

Detailed Description

Definition at line 265 of file LLParser.h.

Constructor & Destructor Documentation

llvm::LLParser::PerFunctionState::PerFunctionState ( LLParser p,
Function f,
int  FunctionNumber 
)
LLParser::PerFunctionState::~PerFunctionState ( )

Definition at line 1975 of file LLParser.cpp.

1975  {
1976  // If there were any forward referenced non-basicblock values, delete them.
1977  for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
1978  I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
1979  if (!isa<BasicBlock>(I->second.first)) {
1980  I->second.first->replaceAllUsesWith(
1981  UndefValue::get(I->second.first->getType()));
1982  delete I->second.first;
1983  I->second.first = 0;
1984  }
1985 
1986  for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1987  I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
1988  if (!isa<BasicBlock>(I->second.first)) {
1989  I->second.first->replaceAllUsesWith(
1990  UndefValue::get(I->second.first->getType()));
1991  delete I->second.first;
1992  I->second.first = 0;
1993  }
1994 }
::std::string string
Definition: gtest-port.h:761
static UndefValue * get(Type *T)
Definition: Constants.cpp:1334
#define I(x, y, z)
Definition: MD5.cpp:54
std::map< std::string, std::pair< Value *, LocTy > > ForwardRefVals
Definition: LLParser.h:268
std::map< unsigned, std::pair< Value *, LocTy > > ForwardRefValIDs
Definition: LLParser.h:269

Here is the call graph for this function:

Member Function Documentation

BasicBlock * LLParser::PerFunctionState::DefineBB ( const std::string &  Name,
LocTy  Loc 
)

DefineBB - Define the specified basic block, which is either named or unnamed. If there is an error, this returns null otherwise it returns the block being defined.

Definition at line 2192 of file LLParser.cpp.

2193  {
2194  BasicBlock *BB;
2195  if (Name.empty())
2196  BB = GetBB(NumberedVals.size(), Loc);
2197  else
2198  BB = GetBB(Name, Loc);
2199  if (BB == 0) return 0; // Already diagnosed error.
2200 
2201  // Move the block to the end of the function. Forward ref'd blocks are
2202  // inserted wherever they happen to be referenced.
2204 
2205  // Remove the block from forward ref sets.
2206  if (Name.empty()) {
2207  ForwardRefValIDs.erase(NumberedVals.size());
2208  NumberedVals.push_back(BB);
2209  } else {
2210  // BB forward references are already in the function symbol table.
2211  ForwardRefVals.erase(Name);
2212  }
2213 
2214  return BB;
2215 }
iterator end()
Definition: Function.h:397
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
const BasicBlockListType & getBasicBlockList() const
Definition: Function.h:374
std::vector< Value * > NumberedVals
Definition: LLParser.h:270
BasicBlock * GetBB(const std::string &Name, LocTy Loc)
Definition: LLParser.cpp:2178
void splice(iterator where, iplist &L2)
Definition: ilist.h:570
std::map< std::string, std::pair< Value *, LocTy > > ForwardRefVals
Definition: LLParser.h:268
std::map< unsigned, std::pair< Value *, LocTy > > ForwardRefValIDs
Definition: LLParser.h:269

Here is the call graph for this function:

bool LLParser::PerFunctionState::FinishFunction ( )

Definition at line 1996 of file LLParser.cpp.

1996  {
1997  // Check to see if someone took the address of labels in this block.
1998  if (!P.ForwardRefBlockAddresses.empty()) {
1999  ValID FunctionID;
2000  if (!F.getName().empty()) {
2001  FunctionID.Kind = ValID::t_GlobalName;
2002  FunctionID.StrVal = F.getName();
2003  } else {
2004  FunctionID.Kind = ValID::t_GlobalID;
2005  FunctionID.UIntVal = FunctionNumber;
2006  }
2007 
2008  std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
2009  FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
2010  if (FRBAI != P.ForwardRefBlockAddresses.end()) {
2011  // Resolve all these references.
2012  if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
2013  return true;
2014 
2015  P.ForwardRefBlockAddresses.erase(FRBAI);
2016  }
2017  }
2018 
2019  if (!ForwardRefVals.empty())
2020  return P.Error(ForwardRefVals.begin()->second.second,
2021  "use of undefined value '%" + ForwardRefVals.begin()->first +
2022  "'");
2023  if (!ForwardRefValIDs.empty())
2024  return P.Error(ForwardRefValIDs.begin()->second.second,
2025  "use of undefined value '%" +
2026  Twine(ForwardRefValIDs.begin()->first) + "'");
2027  return false;
2028 }
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110
enum llvm::ValID::@149 Kind
std::map< ValID, std::vector< std::pair< ValID, GlobalValue * > > > ForwardRefBlockAddresses
Definition: LLParser.h:128
StringRef getName() const
Definition: Value.cpp:167
bool Error(LocTy L, const Twine &Msg) const
Definition: LLParser.h:144
std::string StrVal
Definition: LLParser.h:62
std::map< std::string, std::pair< Value *, LocTy > > ForwardRefVals
Definition: LLParser.h:268
unsigned UIntVal
Definition: LLParser.h:61
std::map< unsigned, std::pair< Value *, LocTy > > ForwardRefValIDs
Definition: LLParser.h:269
bool ResolveForwardRefBlockAddresses(Function *TheFn, std::vector< std::pair< ValID, GlobalValue * > > &Refs, PerFunctionState *PFS)
Definition: LLParser.cpp:190

Here is the call graph for this function:

BasicBlock * LLParser::PerFunctionState::GetBB ( const std::string &  Name,
LocTy  Loc 
)

GetBB - Get a basic block with the specified name or ID, creating a forward reference record if needed. This can return null if the value is not a BasicBlock.

GetBB - Get a basic block with the specified name or ID, creating a forward reference record if needed.

Definition at line 2178 of file LLParser.cpp.

2179  {
2180  return cast_or_null<BasicBlock>(GetVal(Name,
2181  Type::getLabelTy(F.getContext()), Loc));
2182 }
LLVMContext & getContext() const
Definition: Function.cpp:167
static Type * getLabelTy(LLVMContext &C)
Definition: Type.cpp:228
Value * GetVal(const std::string &Name, Type *Ty, LocTy Loc)
Definition: LLParser.cpp:2034

Here is the call graph for this function:

Here is the caller graph for this function:

BasicBlock * LLParser::PerFunctionState::GetBB ( unsigned  ID,
LocTy  Loc 
)

Definition at line 2184 of file LLParser.cpp.

2184  {
2185  return cast_or_null<BasicBlock>(GetVal(ID,
2186  Type::getLabelTy(F.getContext()), Loc));
2187 }
LLVMContext & getContext() const
Definition: Function.cpp:167
static Type * getLabelTy(LLVMContext &C)
Definition: Type.cpp:228
Value * GetVal(const std::string &Name, Type *Ty, LocTy Loc)
Definition: LLParser.cpp:2034

Here is the call graph for this function:

Function& llvm::LLParser::PerFunctionState::getFunction ( ) const
inline

Definition at line 279 of file LLParser.h.

279 { return F; }
Value * LLParser::PerFunctionState::GetVal ( const std::string &  Name,
Type Ty,
LocTy  Loc 
)

GetVal - Get a value with the specified name or ID, creating a forward reference record if needed. This can return null if the value exists but does not have the right type.

Definition at line 2034 of file LLParser.cpp.

2035  {
2036  // Look this name up in the normal function symbol table.
2038 
2039  // If this is a forward reference for the value, see if we already created a
2040  // forward ref record.
2041  if (Val == 0) {
2042  std::map<std::string, std::pair<Value*, LocTy> >::iterator
2043  I = ForwardRefVals.find(Name);
2044  if (I != ForwardRefVals.end())
2045  Val = I->second.first;
2046  }
2047 
2048  // If we have the value in the symbol table or fwd-ref table, return it.
2049  if (Val) {
2050  if (Val->getType() == Ty) return Val;
2051  if (Ty->isLabelTy())
2052  P.Error(Loc, "'%" + Name + "' is not a basic block");
2053  else
2054  P.Error(Loc, "'%" + Name + "' defined with type '" +
2055  getTypeString(Val->getType()) + "'");
2056  return 0;
2057  }
2058 
2059  // Don't make placeholders with invalid type.
2060  if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
2061  P.Error(Loc, "invalid use of a non-first-class type");
2062  return 0;
2063  }
2064 
2065  // Otherwise, create a new forward reference for this value and remember it.
2066  Value *FwdVal;
2067  if (Ty->isLabelTy())
2068  FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
2069  else
2070  FwdVal = new Argument(Ty, Name);
2071 
2072  ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2073  return FwdVal;
2074 }
LLVM Value Representation.
Definition: Value.h:66
Type * getType() const
Definition: Value.h:111
LLVMContext & getContext() const
Definition: Function.cpp:167
bool isFirstClassType() const
Definition: Type.h:251
bool Error(LocTy L, const Twine &Msg) const
Definition: LLParser.h:144
Value * lookup(StringRef Name) const
Lookup a named Value.
static std::string getTypeString(Type *T)
Definition: LLParser.cpp:30
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=0, BasicBlock *InsertBefore=0)
Creates a new BasicBlock.
Definition: BasicBlock.h:109
#define I(x, y, z)
Definition: MD5.cpp:54
bool isLabelTy() const
isLabelTy - Return true if this is 'label'.
Definition: Type.h:189
std::map< std::string, std::pair< Value *, LocTy > > ForwardRefVals
Definition: LLParser.h:268
ValueSymbolTable & getValueSymbolTable()
Definition: Function.h:388

Here is the call graph for this function:

Value * LLParser::PerFunctionState::GetVal ( unsigned  ID,
Type Ty,
LocTy  Loc 
)

Definition at line 2076 of file LLParser.cpp.

2077  {
2078  // Look this name up in the normal function symbol table.
2079  Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
2080 
2081  // If this is a forward reference for the value, see if we already created a
2082  // forward ref record.
2083  if (Val == 0) {
2084  std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2085  I = ForwardRefValIDs.find(ID);
2086  if (I != ForwardRefValIDs.end())
2087  Val = I->second.first;
2088  }
2089 
2090  // If we have the value in the symbol table or fwd-ref table, return it.
2091  if (Val) {
2092  if (Val->getType() == Ty) return Val;
2093  if (Ty->isLabelTy())
2094  P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
2095  else
2096  P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
2097  getTypeString(Val->getType()) + "'");
2098  return 0;
2099  }
2100 
2101  if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
2102  P.Error(Loc, "invalid use of a non-first-class type");
2103  return 0;
2104  }
2105 
2106  // Otherwise, create a new forward reference for this value and remember it.
2107  Value *FwdVal;
2108  if (Ty->isLabelTy())
2109  FwdVal = BasicBlock::Create(F.getContext(), "", &F);
2110  else
2111  FwdVal = new Argument(Ty);
2112 
2113  ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2114  return FwdVal;
2115 }
LLVM Value Representation.
Definition: Value.h:66
Type * getType() const
Definition: Value.h:111
LLVMContext & getContext() const
Definition: Function.cpp:167
bool isFirstClassType() const
Definition: Type.h:251
bool Error(LocTy L, const Twine &Msg) const
Definition: LLParser.h:144
std::vector< Value * > NumberedVals
Definition: LLParser.h:270
static std::string getTypeString(Type *T)
Definition: LLParser.cpp:30
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=0, BasicBlock *InsertBefore=0)
Creates a new BasicBlock.
Definition: BasicBlock.h:109
#define I(x, y, z)
Definition: MD5.cpp:54
bool isLabelTy() const
isLabelTy - Return true if this is 'label'.
Definition: Type.h:189
std::map< unsigned, std::pair< Value *, LocTy > > ForwardRefValIDs
Definition: LLParser.h:269

Here is the call graph for this function:

bool LLParser::PerFunctionState::SetInstName ( int  NameID,
const std::string &  NameStr,
LocTy  NameLoc,
Instruction Inst 
)

SetInstName - After an instruction is parsed and inserted into its basic block, this installs its name.

Definition at line 2119 of file LLParser.cpp.

2121  {
2122  // If this instruction has void type, it cannot have a name or ID specified.
2123  if (Inst->getType()->isVoidTy()) {
2124  if (NameID != -1 || !NameStr.empty())
2125  return P.Error(NameLoc, "instructions returning void cannot have a name");
2126  return false;
2127  }
2128 
2129  // If this was a numbered instruction, verify that the instruction is the
2130  // expected value and resolve any forward references.
2131  if (NameStr.empty()) {
2132  // If neither a name nor an ID was specified, just use the next ID.
2133  if (NameID == -1)
2134  NameID = NumberedVals.size();
2135 
2136  if (unsigned(NameID) != NumberedVals.size())
2137  return P.Error(NameLoc, "instruction expected to be numbered '%" +
2138  Twine(NumberedVals.size()) + "'");
2139 
2140  std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2141  ForwardRefValIDs.find(NameID);
2142  if (FI != ForwardRefValIDs.end()) {
2143  if (FI->second.first->getType() != Inst->getType())
2144  return P.Error(NameLoc, "instruction forward referenced with type '" +
2145  getTypeString(FI->second.first->getType()) + "'");
2146  FI->second.first->replaceAllUsesWith(Inst);
2147  delete FI->second.first;
2148  ForwardRefValIDs.erase(FI);
2149  }
2150 
2151  NumberedVals.push_back(Inst);
2152  return false;
2153  }
2154 
2155  // Otherwise, the instruction had a name. Resolve forward refs and set it.
2156  std::map<std::string, std::pair<Value*, LocTy> >::iterator
2157  FI = ForwardRefVals.find(NameStr);
2158  if (FI != ForwardRefVals.end()) {
2159  if (FI->second.first->getType() != Inst->getType())
2160  return P.Error(NameLoc, "instruction forward referenced with type '" +
2161  getTypeString(FI->second.first->getType()) + "'");
2162  FI->second.first->replaceAllUsesWith(Inst);
2163  delete FI->second.first;
2164  ForwardRefVals.erase(FI);
2165  }
2166 
2167  // Set the name on the instruction.
2168  Inst->setName(NameStr);
2169 
2170  if (Inst->getName() != NameStr)
2171  return P.Error(NameLoc, "multiple definition of local value named '" +
2172  NameStr + "'");
2173  return false;
2174 }
Type * getType() const
Definition: Value.h:111
void setName(const Twine &Name)
Definition: Value.cpp:175
StringRef getName() const
Definition: Value.cpp:167
bool Error(LocTy L, const Twine &Msg) const
Definition: LLParser.h:144
std::vector< Value * > NumberedVals
Definition: LLParser.h:270
static std::string getTypeString(Type *T)
Definition: LLParser.cpp:30
std::map< std::string, std::pair< Value *, LocTy > > ForwardRefVals
Definition: LLParser.h:268
std::map< unsigned, std::pair< Value *, LocTy > > ForwardRefValIDs
Definition: LLParser.h:269
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
Definition: Type.h:140

Here is the call graph for this function:

Member Data Documentation

Function& llvm::LLParser::PerFunctionState::F
private

Definition at line 267 of file LLParser.h.

std::map<unsigned, std::pair<Value*, LocTy> > llvm::LLParser::PerFunctionState::ForwardRefValIDs
private

Definition at line 269 of file LLParser.h.

std::map<std::string, std::pair<Value*, LocTy> > llvm::LLParser::PerFunctionState::ForwardRefVals
private

Definition at line 268 of file LLParser.h.

int llvm::LLParser::PerFunctionState::FunctionNumber
private

FunctionNumber - If this is an unnamed function, this is the slot number of it, otherwise it is -1.

Definition at line 274 of file LLParser.h.

std::vector<Value*> llvm::LLParser::PerFunctionState::NumberedVals
private

Definition at line 270 of file LLParser.h.

LLParser& llvm::LLParser::PerFunctionState::P
private

Definition at line 266 of file LLParser.h.


The documentation for this class was generated from the following files: