/// 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 '" +
/// ::= /* 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;
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;
}
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);
if (ParseType(Ty, true /*void allowed*/)) return true;
if (Ty->isVoidTy()) {
- if (EatIfPresent(lltok::comma))
- if (ParseOptionalCustomMetadata()) return true;
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;
}