#include "llvm/Instructions.h"
#include "llvm/Module.h"
#include "llvm/SymbolTable.h"
-#include "llvm/Assembly/AutoUpgrade.h"
#include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/MathExtras.h"
#define YYERROR_VERBOSE 1
-static bool ObsoleteVarArgs;
static bool NewVarArgs;
-static BasicBlock *CurBB;
static GlobalVariable *CurGV;
static Module* RunParser(Module * M) {
llvmAsmlineno = 1; // Reset the current line number...
- ObsoleteVarArgs = false;
NewVarArgs = false;
CurModule.CurrentModule = M;
Module *Result = ParserResult;
ParserResult = 0;
- //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
- {
- Function* F;
- if ((F = Result->getNamedFunction("llvm.va_start"))
- && F->getFunctionType()->getNumParams() == 0)
- ObsoleteVarArgs = true;
- if((F = Result->getNamedFunction("llvm.va_copy"))
- && F->getFunctionType()->getNumParams() == 1)
- ObsoleteVarArgs = true;
- }
-
- if (ObsoleteVarArgs && NewVarArgs) {
- GenerateError(
- "This file is corrupt: it uses both new and old style varargs");
- return 0;
- }
-
- if(ObsoleteVarArgs) {
- if(Function* F = Result->getNamedFunction("llvm.va_start")) {
- if (F->arg_size() != 0) {
- GenerateError("Obsolete va_start takes 0 argument!");
- return 0;
- }
-
- //foo = va_start()
- // ->
- //bar = alloca typeof(foo)
- //va_start(bar)
- //foo = load bar
-
- const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
- const Type* ArgTy = F->getFunctionType()->getReturnType();
- const Type* ArgTyPtr = PointerType::get(ArgTy);
- Function* NF = Result->getOrInsertFunction("llvm.va_start",
- RetTy, ArgTyPtr, (Type *)0);
-
- while (!F->use_empty()) {
- CallInst* CI = cast<CallInst>(F->use_back());
- AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
- new CallInst(NF, bar, "", CI);
- Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
- CI->replaceAllUsesWith(foo);
- CI->getParent()->getInstList().erase(CI);
- }
- Result->getFunctionList().erase(F);
- }
-
- if(Function* F = Result->getNamedFunction("llvm.va_end")) {
- if(F->arg_size() != 1) {
- GenerateError("Obsolete va_end takes 1 argument!");
- return 0;
- }
-
- //vaend foo
- // ->
- //bar = alloca 1 of typeof(foo)
- //vaend bar
- const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
- const Type* ArgTy = F->getFunctionType()->getParamType(0);
- const Type* ArgTyPtr = PointerType::get(ArgTy);
- Function* NF = Result->getOrInsertFunction("llvm.va_end",
- RetTy, ArgTyPtr, (Type *)0);
-
- while (!F->use_empty()) {
- CallInst* CI = cast<CallInst>(F->use_back());
- AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
- new StoreInst(CI->getOperand(1), bar, CI);
- new CallInst(NF, bar, "", CI);
- CI->getParent()->getInstList().erase(CI);
- }
- Result->getFunctionList().erase(F);
- }
-
- if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
- if(F->arg_size() != 1) {
- GenerateError("Obsolete va_copy takes 1 argument!");
- return 0;
- }
- //foo = vacopy(bar)
- // ->
- //a = alloca 1 of typeof(foo)
- //b = alloca 1 of typeof(foo)
- //store bar -> b
- //vacopy(a, b)
- //foo = load a
-
- const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
- const Type* ArgTy = F->getFunctionType()->getReturnType();
- const Type* ArgTyPtr = PointerType::get(ArgTy);
- Function* NF = Result->getOrInsertFunction("llvm.va_copy",
- RetTy, ArgTyPtr, ArgTyPtr,
- (Type *)0);
-
- while (!F->use_empty()) {
- CallInst* CI = cast<CallInst>(F->use_back());
- AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
- AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
- new StoreInst(CI->getOperand(1), b, CI);
- new CallInst(NF, a, b, "", CI);
- Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
- CI->replaceAllUsesWith(foo);
- CI->getParent()->getInstList().erase(CI);
- }
- Result->getFunctionList().erase(F);
- }
- }
-
return Result;
}
%type <OtherOpVal> ShiftOps
%token <OtherOpVal> PHI_TOK SELECT SHL LSHR ASHR VAARG
%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
-%token VAARG_old VANEXT_old //OBSOLETE
%start Module
UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
ShiftOps : SHL | LSHR | ASHR;
IPredicates
- : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
+ : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
| SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
| SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
| ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
if (!isa<PointerType>($3->getType()))
GEN_ERROR("GetElementPtr requires a pointer operand!");
- // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
- // indices to uint struct indices for compatibility.
- generic_gep_type_iterator<std::vector<Value*>::iterator>
- GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
- GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
- for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
- if (isa<StructType>(*GTI)) // Only change struct indices
- if (ConstantInt *CUI = dyn_cast<ConstantInt>((*$4)[i]))
- if (CUI->getType() == Type::UByteTy)
- (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
-
const Type *IdxTy =
GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
if (!IdxTy)
if ($3->getType() != $5->getType())
GEN_ERROR("Binary operator types must match!");
CHECK_FOR_ERROR;
-
- // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
- // To retain backward compatibility with these early compilers, we emit a
- // cast to the appropriate integer type automatically if we are in the
- // broken case. See PR424 for more information.
- if (!isa<PointerType>($3->getType())) {
- $$ = ConstantExpr::get($1, $3, $5);
- } else {
- const Type *IntPtrTy = 0;
- switch (CurModule.CurrentModule->getPointerSize()) {
- case Module::Pointer32: IntPtrTy = Type::IntTy; break;
- case Module::Pointer64: IntPtrTy = Type::LongTy; break;
- default: GEN_ERROR("invalid pointer binary constant expr!");
- }
- $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
- ConstantExpr::getCast($5, IntPtrTy));
- $$ = ConstantExpr::getCast($$, $3->getType());
- }
- CHECK_FOR_ERROR
+ $$ = ConstantExpr::get($1, $3, $5);
}
| LogicalOps '(' ConstVal ',' ConstVal ')' {
if ($3->getType() != $5->getType())
$$ = ConstantExpr::get($1, $3, $5);
CHECK_FOR_ERROR
}
- | ICMP '(' IPredicates ',' ConstVal ',' ConstVal ')' {
- if ($5->getType() != $7->getType())
+ | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
+ if ($4->getType() != $6->getType())
GEN_ERROR("icmp operand types must match!");
- $$ = ConstantExpr::getICmp($3, $5, $7);
+ $$ = ConstantExpr::getICmp($2, $4, $6);
}
- | FCMP '(' FPredicates ',' ConstVal ',' ConstVal ')' {
- if ($5->getType() != $7->getType())
+ | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
+ if ($4->getType() != $6->getType())
GEN_ERROR("fcmp operand types must match!");
- $$ = ConstantExpr::getFCmp($3, $5, $7);
+ $$ = ConstantExpr::getFCmp($2, $4, $6);
}
| ShiftOps '(' ConstVal ',' ConstVal ')' {
if ($5->getType() != Type::UByteTy)
CHECK_FOR_ERROR
}
| /* empty */ {
- $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
+ $$ = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
CHECK_FOR_ERROR
// Make sure to move the basic block to the correct location in the
CHECK_FOR_ERROR
}
| LABELSTR {
- $$ = CurBB = getBBVal(ValID::create($1), true);
+ $$ = getBBVal(ValID::create($1), true);
CHECK_FOR_ERROR
// Make sure to move the basic block to the correct location in the
delete $4;
CHECK_FOR_ERROR
}
- | VAARG_old ResolvedVal ',' Types {
- ObsoleteVarArgs = true;
- const Type* ArgTy = $2->getType();
- Function* NF = CurModule.CurrentModule->
- getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
-
- //b = vaarg a, t ->
- //foo = alloca 1 of t
- //bar = vacopy a
- //store bar -> foo
- //b = vaarg foo, t
- AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
- CurBB->getInstList().push_back(foo);
- CallInst* bar = new CallInst(NF, $2);
- CurBB->getInstList().push_back(bar);
- CurBB->getInstList().push_back(new StoreInst(bar, foo));
- $$ = new VAArgInst(foo, *$4);
- delete $4;
- CHECK_FOR_ERROR
- }
- | VANEXT_old ResolvedVal ',' Types {
- ObsoleteVarArgs = true;
- const Type* ArgTy = $2->getType();
- Function* NF = CurModule.CurrentModule->
- getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
-
- //b = vanext a, t ->
- //foo = alloca 1 of t
- //bar = vacopy a
- //store bar -> foo
- //tmp = vaarg foo, t
- //b = load foo
- AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
- CurBB->getInstList().push_back(foo);
- CallInst* bar = new CallInst(NF, $2);
- CurBB->getInstList().push_back(bar);
- CurBB->getInstList().push_back(new StoreInst(bar, foo));
- Instruction* tmp = new VAArgInst(foo, *$4);
- CurBB->getInstList().push_back(tmp);
- $$ = new LoadInst(foo);
- delete $4;
- CHECK_FOR_ERROR
- }
| EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
if (!ExtractElementInst::isValidOperands($2, $4))
GEN_ERROR("Invalid extractelement operands!");
if (!isa<PointerType>($2->get()))
GEN_ERROR("getelementptr insn requires pointer operand!");
- // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
- // indices to uint struct indices for compatibility.
- generic_gep_type_iterator<std::vector<Value*>::iterator>
- GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
- GTE = gep_type_end($2->get(), $4->begin(), $4->end());
- for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
- if (isa<StructType>(*GTI)) // Only change struct indices
- if (ConstantInt *CUI = dyn_cast<ConstantInt>((*$4)[i]))
- if (CUI->getType() == Type::UByteTy)
- (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
-
if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
GEN_ERROR("Invalid getelementptr indices for type '" +
(*$2)->getDescription()+ "'!");