/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
/// module.
bool LLParser::ValidateEndOfModule() {
+ // Update auto-upgraded malloc calls to "malloc".
+ // FIXME: Remove in LLVM 3.0.
+ if (MallocF) {
+ MallocF->setName("malloc");
+ // If setName() does not set the name to "malloc", then there is already a
+ // declaration of "malloc". In that case, iterate over all calls to MallocF
+ // and get them to call the declared "malloc" instead.
+ if (MallocF->getName() != "malloc") {
+ Constant* RealMallocF = M->getFunction("malloc");
+ if (RealMallocF->getType() != MallocF->getType())
+ RealMallocF = ConstantExpr::getBitCast(RealMallocF, MallocF->getType());
+ MallocF->replaceAllUsesWith(RealMallocF);
+ MallocF->eraseFromParent();
+ MallocF = NULL;
+ }
+ }
+
if (!ForwardRefTypes.empty())
return Error(ForwardRefTypes.begin()->second.second,
"use of undefined type named '" +
return true;
}
- if (isa<FunctionType>(Ty) || Ty == Type::getLabelTy(Context))
+ if (isa<FunctionType>(Ty) || Ty->isLabelTy())
return Error(TyLoc, "invalid type for global variable");
GlobalVariable *GV = 0;
/// ::= /* empty */
/// ::= !dbg !42
bool LLParser::ParseOptionalCustomMetadata() {
-
- std::string Name;
- if (Lex.getKind() == lltok::NamedOrCustomMD) {
- Name = Lex.getStrVal();
- Lex.Lex();
- } else
+ if (Lex.getKind() != lltok::NamedOrCustomMD)
return false;
+ std::string Name = Lex.getStrVal();
+ Lex.Lex();
+
if (Lex.getKind() != lltok::Metadata)
return TokError("Expected '!' here");
Lex.Lex();
MetadataContext &TheMetadata = M->getContext().getMetadata();
unsigned MDK = TheMetadata.getMDKind(Name.c_str());
if (!MDK)
- MDK = TheMetadata.RegisterMDKind(Name.c_str());
+ MDK = TheMetadata.registerMDKind(Name.c_str());
MDsOnInst.push_back(std::make_pair(MDK, cast<MDNode>(Node)));
return false;
if (!UpRefs.empty())
return Error(UpRefs.back().Loc, "invalid unresolved type up reference");
- if (!AllowVoid && Result.get() == Type::getVoidTy(Context))
+ if (!AllowVoid && Result.get()->isVoidTy())
return Error(TypeLoc, "void type only allowed for function results");
return false;
// TypeRec ::= TypeRec '*'
case lltok::star:
- if (Result.get() == Type::getLabelTy(Context))
+ if (Result.get()->isLabelTy())
return TokError("basic block pointers are invalid");
- if (Result.get() == Type::getVoidTy(Context))
+ if (Result.get()->isVoidTy())
return TokError("pointers to void are invalid; use i8* instead");
if (!PointerType::isValidElementType(Result.get()))
return TokError("pointer to this type is invalid");
// TypeRec ::= TypeRec 'addrspace' '(' uint32 ')' '*'
case lltok::kw_addrspace: {
- if (Result.get() == Type::getLabelTy(Context))
+ if (Result.get()->isLabelTy())
return TokError("basic block pointers are invalid");
- if (Result.get() == Type::getVoidTy(Context))
+ if (Result.get()->isVoidTy())
return TokError("pointers to void are invalid; use i8* instead");
if (!PointerType::isValidElementType(Result.get()))
return TokError("pointer to this type is invalid");
if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) ||
ParseOptionalAttrs(Attrs, 0)) return true;
- if (ArgTy == Type::getVoidTy(Context))
+ if (ArgTy->isVoidTy())
return Error(TypeLoc, "argument can not have void type");
if (Lex.getKind() == lltok::LocalVar ||
if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) ||
ParseOptionalAttrs(Attrs, 0)) return true;
- if (ArgTy == Type::getVoidTy(Context))
+ if (ArgTy->isVoidTy())
return Error(TypeLoc, "argument can not have void type");
if (Lex.getKind() == lltok::LocalVar ||
if (ParseTypeRec(Result)) return true;
ParamsList.push_back(Result);
- if (Result == Type::getVoidTy(Context))
+ if (Result->isVoidTy())
return Error(EltTyLoc, "struct element can not have void type");
if (!StructType::isValidElementType(Result))
return Error(EltTyLoc, "invalid element type for struct");
EltTyLoc = Lex.getLoc();
if (ParseTypeRec(Result)) return true;
- if (Result == Type::getVoidTy(Context))
+ if (Result->isVoidTy())
return Error(EltTyLoc, "struct element can not have void type");
if (!StructType::isValidElementType(Result))
return Error(EltTyLoc, "invalid element type for struct");
PATypeHolder EltTy(Type::getVoidTy(Context));
if (ParseTypeRec(EltTy)) return true;
- if (EltTy == Type::getVoidTy(Context))
+ if (EltTy->isVoidTy())
return Error(TypeLoc, "array and vector element type cannot be void");
if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
// If we have the value in the symbol table or fwd-ref table, return it.
if (Val) {
if (Val->getType() == Ty) return Val;
- if (Ty == Type::getLabelTy(F.getContext()))
+ if (Ty->isLabelTy())
P.Error(Loc, "'%" + Name + "' is not a basic block");
else
P.Error(Loc, "'%" + Name + "' defined with type '" +
// Otherwise, create a new forward reference for this value and remember it.
Value *FwdVal;
- if (Ty == Type::getLabelTy(F.getContext()))
+ if (Ty->isLabelTy())
FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
else
FwdVal = new Argument(Ty, Name);
// If we have the value in the symbol table or fwd-ref table, return it.
if (Val) {
if (Val->getType() == Ty) return Val;
- if (Ty == Type::getLabelTy(F.getContext()))
+ if (Ty->isLabelTy())
P.Error(Loc, "'%" + utostr(ID) + "' is not a basic block");
else
P.Error(Loc, "'%" + utostr(ID) + "' defined with type '" +
// Otherwise, create a new forward reference for this value and remember it.
Value *FwdVal;
- if (Ty == Type::getLabelTy(F.getContext()))
+ if (Ty->isLabelTy())
FwdVal = BasicBlock::Create(F.getContext(), "", &F);
else
FwdVal = new Argument(Ty);
const std::string &NameStr,
LocTy NameLoc, Instruction *Inst) {
// If this instruction has void type, it cannot have a name or ID specified.
- if (Inst->getType() == Type::getVoidTy(F.getContext())) {
+ if (Inst->getType()->isVoidTy()) {
if (NameID != -1 || !NameStr.empty())
return P.Error(NameLoc, "instructions returning void cannot have a name");
return false;
return false;
case lltok::kw_asm: {
- // ValID ::= 'asm' SideEffect? STRINGCONSTANT ',' STRINGCONSTANT
- bool HasSideEffect;
+ // ValID ::= 'asm' SideEffect? MsAsm? STRINGCONSTANT ',' STRINGCONSTANT
+ bool HasSideEffect, MsAsm;
Lex.Lex();
if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
+ ParseOptionalToken(lltok::kw_msasm, MsAsm) ||
ParseStringConstant(ID.StrVal) ||
ParseToken(lltok::comma, "expected comma in inline asm expression") ||
ParseToken(lltok::StringConstant, "expected constraint string"))
return true;
ID.StrVal2 = Lex.getStrVal();
- ID.UIntVal = HasSideEffect;
+ ID.UIntVal = HasSideEffect | ((unsigned)MsAsm<<1);
ID.Kind = ValID::t_InlineAsm;
return false;
}
// The lexer has no type info, so builds all float and double FP constants
// as double. Fix this here. Long double does not need this.
if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble &&
- Ty == Type::getFloatTy(Context)) {
+ Ty->isFloatTy()) {
bool Ignored;
ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
&Ignored);
return false;
case ValID::t_Undef:
// FIXME: LabelTy should not be a first-class type.
- if ((!Ty->isFirstClassType() || Ty == Type::getLabelTy(Context)) &&
+ if ((!Ty->isFirstClassType() || Ty->isLabelTy()) &&
!isa<OpaqueType>(Ty))
return Error(ID.Loc, "invalid type for undef constant");
V = UndefValue::get(Ty);
return false;
case ValID::t_Zero:
// FIXME: LabelTy should not be a first-class type.
- if (!Ty->isFirstClassType() || Ty == Type::getLabelTy(Context))
+ if (!Ty->isFirstClassType() || Ty->isLabelTy())
return Error(ID.Loc, "invalid type for null constant");
V = Constant::getNullValue(Ty);
return false;
PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
return Error(ID.Loc, "invalid type for inline asm constraint string");
- V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal);
+ V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1, ID.UIntVal>>1);
return false;
} else if (ID.Kind == ValID::t_Metadata) {
V = ID.MetadataVal;
case lltok::kw_call: return ParseCall(Inst, PFS, false);
case lltok::kw_tail: return ParseCall(Inst, PFS, true);
// Memory.
- case lltok::kw_alloca:
- case lltok::kw_malloc: return ParseAlloc(Inst, PFS, KeywordVal);
+ case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
+ case lltok::kw_malloc: return ParseAlloc(Inst, PFS, BB, false);
case lltok::kw_free: return ParseFree(Inst, PFS);
case lltok::kw_load: return ParseLoad(Inst, PFS, false);
case lltok::kw_store: return ParseStore(Inst, PFS, false);
PATypeHolder Ty(Type::getVoidTy(Context));
if (ParseType(Ty, true /*void allowed*/)) return true;
- if (Ty == Type::getVoidTy(Context)) {
- if (EatIfPresent(lltok::comma))
- if (ParseOptionalCustomMetadata()) return true;
+ if (Ty->isVoidTy()) {
Inst = ReturnInst::Create(Context);
return false;
}
}
}
}
- if (EatIfPresent(lltok::comma))
- if (ParseOptionalCustomMetadata()) return true;
Inst = ReturnInst::Create(Context, RV);
return false;
}
/// ParsePHI
-/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Valueß ']')*
+/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
bool LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
PATypeHolder Ty(Type::getVoidTy(Context));
Value *Op0, *Op1;
if (!EatIfPresent(lltok::comma))
break;
+ if (Lex.getKind() == lltok::NamedOrCustomMD)
+ break;
+
if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
ParseValue(Ty, Op0, PFS) ||
ParseToken(lltok::comma, "expected ',' after insertelement value") ||
return true;
}
+ if (Lex.getKind() == lltok::NamedOrCustomMD)
+ if (ParseOptionalCustomMetadata()) return true;
+
if (!Ty->isFirstClassType())
return Error(TypeLoc, "phi node must have first class type");
/// ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalInfo)?
/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)?
bool LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS,
- unsigned Opc) {
+ BasicBlock* BB, bool isAlloca) {
PATypeHolder Ty(Type::getVoidTy(Context));
Value *Size = 0;
LocTy SizeLoc;
if (Size && Size->getType() != Type::getInt32Ty(Context))
return Error(SizeLoc, "element count must be i32");
- if (Opc == Instruction::Malloc)
- Inst = new MallocInst(Ty, Size, Alignment);
- else
+ if (isAlloca) {
Inst = new AllocaInst(Ty, Size, Alignment);
+ return false;
+ }
+
+ // Autoupgrade old malloc instruction to malloc call.
+ // FIXME: Remove in LLVM 3.0.
+ const Type *IntPtrTy = Type::getInt32Ty(Context);
+ const Type *Int8PtrTy = Type::getInt8PtrTy(Context);
+ if (!MallocF)
+ // Prototype malloc as "void *(int32)".
+ // This function is renamed as "malloc" in ValidateEndOfModule().
+ MallocF = cast<Function>(M->getOrInsertFunction(NULL, Int8PtrTy,
+ IntPtrTy, NULL));
+ Inst = CallInst::CreateMalloc(BB, IntPtrTy, Ty, Size, MallocF);
return false;
}
SmallVector<Value*, 16> Indices;
while (EatIfPresent(lltok::comma)) {
+ if (Lex.getKind() == lltok::NamedOrCustomMD)
+ break;
if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
if (!isa<IntegerType>(Val->getType()))
return Error(EltLoc, "getelementptr index must be an integer");
Indices.push_back(Val);
}
+ if (Lex.getKind() == lltok::NamedOrCustomMD)
+ if (ParseOptionalCustomMetadata()) return true;
if (!GetElementPtrInst::getIndexedType(Ptr->getType(),
Indices.begin(), Indices.end()))