Update to work with LLVM 2.7.

Removed use of dyn_cast, llvm no compiles
without exceptions and rtti by
default. We do need exceptions for the libconfig stuff, but rtti isn't
necessary (anymore).

Debug info needs to be rewritten, as in LLVM 2.7 the format has
completely changed. To have something to look at while rewriting, the
old code has been wrapped inside #ifndef DISABLE_DEBUG_INFO , this means
that you have to define this to compile at the moment.

Updated tango 0.99.9 patch to include updated EH runtime code, which is
needed for LLVM 2.7 as well.
This commit is contained in:
Tomas Lindquist Olsen
2010-05-19 12:42:32 +02:00
parent 5707961243
commit 1fac40d2bd
27 changed files with 992 additions and 242 deletions

View File

@@ -41,6 +41,7 @@ struct TemplateDeclaration;
struct ClassDeclaration; struct ClassDeclaration;
struct HdrGenState; struct HdrGenState;
struct BinExp; struct BinExp;
struct AssignExp;
struct InterState; struct InterState;
struct OverloadSet; struct OverloadSet;
@@ -177,6 +178,8 @@ struct Expression : Object
virtual void cacheLvalue(IRState* irs); virtual void cacheLvalue(IRState* irs);
llvm::Value* cachedLvalue; llvm::Value* cachedLvalue;
virtual AssignExp* isAssignExp() { return NULL; }
#endif #endif
}; };
@@ -1378,6 +1381,8 @@ struct AssignExp : BinExp
#if IN_LLVM #if IN_LLVM
DValue* toElem(IRState* irs); DValue* toElem(IRState* irs);
#endif #endif
AssignExp* isAssignExp() { return this; }
}; };
#if IN_DMD #if IN_DMD

View File

@@ -141,6 +141,7 @@ struct Statement : Object
virtual ReturnStatement *isReturnStatement() { return NULL; } virtual ReturnStatement *isReturnStatement() { return NULL; }
virtual IfStatement *isIfStatement() { return NULL; } virtual IfStatement *isIfStatement() { return NULL; }
virtual CaseStatement* isCaseStatement() { return NULL; } virtual CaseStatement* isCaseStatement() { return NULL; }
virtual LabelStatement* isLabelStatement() { return NULL; }
#if IN_LLVM #if IN_LLVM
virtual void toNakedIR(IRState *irs); virtual void toNakedIR(IRState *irs);
@@ -863,6 +864,8 @@ struct LabelStatement : Statement
// LDC // LDC
bool asmLabel; // for labels inside inline assembler bool asmLabel; // for labels inside inline assembler
void toNakedIR(IRState *irs); void toNakedIR(IRState *irs);
LabelStatement* isLabelStatement() { return this; }
}; };
struct LabelDsymbol : Dsymbol struct LabelDsymbol : Dsymbol

View File

@@ -309,7 +309,7 @@ LLConstant* DtoConstArrayInitializer(ArrayInitializer* arrinit)
LLConstant* constarr; LLConstant* constarr;
if (mismatch) if (mismatch)
constarr = LLConstantStruct::get(gIR->context(), initvals); constarr = LLConstantStruct::get(gIR->context(), initvals, false); // FIXME should this pack?
else else
constarr = LLConstantArray::get(LLArrayType::get(llelemty, arrlen), initvals); constarr = LLConstantArray::get(LLArrayType::get(llelemty, arrlen), initvals);
@@ -394,7 +394,7 @@ void DtoStaticArrayCopy(LLValue* dst, LLValue* src)
LLConstant* DtoConstSlice(LLConstant* dim, LLConstant* ptr) LLConstant* DtoConstSlice(LLConstant* dim, LLConstant* ptr)
{ {
LLConstant* values[2] = { dim, ptr }; LLConstant* values[2] = { dim, ptr };
return LLConstantStruct::get(gIR->context(), values, 2); return LLConstantStruct::get(gIR->context(), values, 2, false);
} }
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -196,9 +196,11 @@ AsmStatement::toIR(IRState * irs)
IRAsmBlock* asmblock = irs->asmBlock; IRAsmBlock* asmblock = irs->asmBlock;
assert(asmblock); assert(asmblock);
#ifndef DISABLE_DEBUG_INFO
// debug info // debug info
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
if (! asmcode) if (! asmcode)
return; return;
@@ -257,8 +259,7 @@ AsmStatement::toIR(IRState * irs)
break; break;
case Arg_FrameRelative: case Arg_FrameRelative:
// FIXME // FIXME
llvm::cout << "asm fixme Arg_FrameRelative" << std::endl; assert(0 && "asm fixme Arg_FrameRelative");
assert(0);
/* if (arg->expr->op == TOKvar) /* if (arg->expr->op == TOKvar)
arg_val = ((VarExp *) arg->expr)->var->toSymbol()->Stree; arg_val = ((VarExp *) arg->expr)->var->toSymbol()->Stree;
else else
@@ -275,8 +276,7 @@ assert(0);
break;*/ break;*/
case Arg_LocalSize: case Arg_LocalSize:
// FIXME // FIXME
llvm::cout << "asm fixme Arg_LocalSize" << std::endl; assert(0 && "asm fixme Arg_LocalSize");
assert(0);
/* var_frame_offset = cfun->x_frame_offset; /* var_frame_offset = cfun->x_frame_offset;
if (var_frame_offset < 0) if (var_frame_offset < 0)
var_frame_offset = - var_frame_offset; var_frame_offset = - var_frame_offset;

View File

@@ -17,18 +17,18 @@ static char toLower(char c) {
return c; return c;
} }
bool FlagParser::parse(cl::Option &O, const char *ArgName, const std::string &Arg, bool &Val) { bool FlagParser::parse(cl::Option &O, llvm::StringRef ArgName, llvm::StringRef Arg, bool &Val) {
// Make a std::string out of it to make comparisons easier // Make a std::string out of it to make comparisons easier
// (and avoid repeated conversion) // (and avoid repeated conversion)
std::string argname = ArgName; llvm::StringRef argname = ArgName;
typedef std::vector<std::pair<std::string, bool> >::iterator It; typedef std::vector<std::pair<std::string, bool> >::iterator It;
for (It I = switches.begin(), E = switches.end(); I != E; ++I) { for (It I = switches.begin(), E = switches.end(); I != E; ++I) {
std::string name = I->first; llvm::StringRef name = I->first;
if (name == argname if (name == argname
|| (name.length() < argname.length() || (name.size() < argname.size()
&& argname.substr(0, name.length()) == name && argname.substr(0, name.size()) == name
&& argname[name.length()] == '=')) { && argname[name.size()] == '=')) {
if (!cl::parser<bool>::parse(O, ArgName, Arg, Val)) { if (!cl::parser<bool>::parse(O, ArgName, Arg, Val)) {
Val = (Val == I->second); Val = (Val == I->second);
@@ -41,10 +41,10 @@ bool FlagParser::parse(cl::Option &O, const char *ArgName, const std::string &Ar
return true; return true;
} }
void FlagParser::getExtraOptionNames(std::vector<const char*> &Names) { void FlagParser::getExtraOptionNames(llvm::SmallVectorImpl<const char*> &Names) {
typedef std::vector<std::pair<std::string, bool> >::iterator It; typedef std::vector<std::pair<std::string, bool> >::iterator It;
for (It I = switches.begin() + 1, E = switches.end(); I != E; ++I) { for (It I = switches.begin() + 1, E = switches.end(); I != E; ++I) {
Names.push_back(I->first.c_str()); Names.push_back(I->first.data());
} }
} }

View File

@@ -21,12 +21,12 @@ namespace opts {
switches.push_back(make_pair("enable-" + Name, true)); switches.push_back(make_pair("enable-" + Name, true));
switches.push_back(make_pair("disable-" + Name, false)); switches.push_back(make_pair("disable-" + Name, false));
// Replace <foo> with -enable-<foo> // Replace <foo> with -enable-<foo>
O.ArgStr = switches[0].first.c_str(); O.ArgStr = switches[0].first.data();
} }
bool parse(cl::Option &O, const char *ArgName, const std::string &ArgValue, bool &Val); bool parse(cl::Option &O, llvm::StringRef ArgName, llvm::StringRef ArgValue, bool &Val);
void getExtraOptionNames(std::vector<const char*> &Names); void getExtraOptionNames(llvm::SmallVectorImpl<const char*> &Names);
}; };
/// Helper class for options that set multiple flags /// Helper class for options that set multiple flags

View File

@@ -109,7 +109,7 @@ bool ConfigFile::read(const char* argv0, void* mainAddr, const char* filename)
} }
// save config file path for -v output // save config file path for -v output
pathstr = p.toString(); pathstr = p.str();
try try
{ {

View File

@@ -599,10 +599,11 @@ void DtoDefineFunction(FuncDeclaration* fd)
return; return;
} }
#ifndef DISABLE_DEBUG_INFO
// debug info // debug info
if (global.params.symdebug) { if (global.params.symdebug)
fd->ir.irFunc->diSubprogram = DtoDwarfSubProgram(fd); fd->ir.irFunc->diSubprogram = DtoDwarfSubProgram(fd);
} #endif
Type* t = fd->type->toBasetype(); Type* t = fd->type->toBasetype();
TypeFunction* f = (TypeFunction*)t; TypeFunction* f = (TypeFunction*)t;
@@ -642,8 +643,10 @@ void DtoDefineFunction(FuncDeclaration* fd)
llvm::Instruction* allocaPoint = new llvm::AllocaInst(LLType::getInt32Ty(gIR->context()), "alloca point", beginbb); llvm::Instruction* allocaPoint = new llvm::AllocaInst(LLType::getInt32Ty(gIR->context()), "alloca point", beginbb);
irfunction->allocapoint = allocaPoint; irfunction->allocapoint = allocaPoint;
#ifndef DISABLE_DEBUG_INFO
// debug info - after all allocas, but before any llvm.dbg.declare etc // debug info - after all allocas, but before any llvm.dbg.declare etc
if (global.params.symdebug) DtoDwarfFuncStart(fd); if (global.params.symdebug) DtoDwarfFuncStart(fd);
#endif
// this hack makes sure the frame pointer elimination optimization is disabled. // this hack makes sure the frame pointer elimination optimization is disabled.
// this this eliminates a bunch of inline asm related issues. // this this eliminates a bunch of inline asm related issues.
@@ -668,8 +671,10 @@ void DtoDefineFunction(FuncDeclaration* fd)
fd->vthis->ir.irLocal = new IrLocal(fd->vthis); fd->vthis->ir.irLocal = new IrLocal(fd->vthis);
fd->vthis->ir.irLocal->value = thismem; fd->vthis->ir.irLocal->value = thismem;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfLocalVariable(thismem, fd->vthis); DtoDwarfLocalVariable(thismem, fd->vthis);
#endif
#if DMDV1 #if DMDV1
if (fd->vthis->nestedref) if (fd->vthis->nestedref)
@@ -722,8 +727,10 @@ void DtoDefineFunction(FuncDeclaration* fd)
irloc->value = mem; irloc->value = mem;
} }
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug && !(isaArgument(irloc->value) && !isaArgument(irloc->value)->hasByValAttr()) && !refout) if (global.params.symdebug && !(isaArgument(irloc->value) && !isaArgument(irloc->value)->hasByValAttr()) && !refout)
DtoDwarfLocalVariable(irloc->value, vd); DtoDwarfLocalVariable(irloc->value, vd);
#endif
} }
} }
@@ -796,7 +803,9 @@ void DtoDefineFunction(FuncDeclaration* fd)
// in automatically, so we do it here. // in automatically, so we do it here.
// pass the previous block into this block // pass the previous block into this block
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) DtoDwarfFuncEnd(fd); if (global.params.symdebug) DtoDwarfFuncEnd(fd);
#endif
if (func->getReturnType() == LLType::getVoidTy(gIR->context())) { if (func->getReturnType() == LLType::getVoidTy(gIR->context())) {
llvm::ReturnInst::Create(gIR->context(), gIR->scopebb()); llvm::ReturnInst::Create(gIR->context(), gIR->scopebb());
} }

View File

@@ -222,13 +222,13 @@ int linkObjToExecutable(const char* argv0)
// find gcc for linking // find gcc for linking
llvm::sys::Path gcc = getGcc(); llvm::sys::Path gcc = getGcc();
// get a string version for argv[0] // get a string version for argv[0]
std::string gccStr = gcc.toString(); const char* gccStr = gcc.c_str();
// build arguments // build arguments
std::vector<const char*> args; std::vector<const char*> args;
// first the program name ?? // first the program name ??
args.push_back(gccStr.c_str()); args.push_back(gccStr);
// object files // object files
for (int i = 0; i < global.params.objfiles->dim; i++) for (int i = 0; i < global.params.objfiles->dim; i++)
@@ -335,7 +335,7 @@ int linkObjToExecutable(const char* argv0)
for (; I != E; ++I) for (; I != E; ++I)
if (*I) if (*I)
logstr << "'" << *I << "'" << " "; logstr << "'" << *I << "'" << " ";
logstr << "\n" << std::flush; logstr << "\n"; // FIXME where's flush ?
// terminate args list // terminate args list
@@ -375,7 +375,7 @@ int runExecutable()
// build arguments // build arguments
std::vector<const char*> args; std::vector<const char*> args;
// args[0] should be the name of the executable // args[0] should be the name of the executable
args.push_back(gExePath.toString().c_str()); args.push_back(gExePath.c_str());
// Skip first argument to -run; it's a D source file. // Skip first argument to -run; it's a D source file.
for (size_t i = 1, length = opts::runargs.size(); i < length; i++) for (size_t i = 1, length = opts::runargs.size(); i < length; i++)
{ {

View File

@@ -160,9 +160,11 @@ void DtoAssert(Module* M, Loc loc, DValue* msg)
// call // call
gIR->CreateCallOrInvoke(fn, args.begin(), args.end()); gIR->CreateCallOrInvoke(fn, args.begin(), args.end());
#ifndef DISABLE_DEBUG_INFO
// end debug info // end debug info
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfFuncEnd(gIR->func()->decl); DtoDwarfFuncEnd(gIR->func()->decl);
#endif
// after assert is always unreachable // after assert is always unreachable
gIR->ir->CreateUnreachable(); gIR->ir->CreateUnreachable();
@@ -268,7 +270,7 @@ void DtoEnclosingHandlers(Loc loc, Statement* target)
{ {
// labels are a special case: they are not required to enclose the current scope // labels are a special case: they are not required to enclose the current scope
// for them we use the enclosing scope handler as a reference point // for them we use the enclosing scope handler as a reference point
LabelStatement* lblstmt = dynamic_cast<LabelStatement*>(target); LabelStatement* lblstmt = target ? target->isLabelStatement() : 0;
if (lblstmt) if (lblstmt)
target = lblstmt->enclosingScopeExit; target = lblstmt->enclosingScopeExit;
@@ -485,7 +487,7 @@ DValue* DtoNullValue(Type* type)
} }
// unknown // unknown
llvm::cout << "unsupported: null value for " << type->toChars() << '\n'; error("unsupported: null value for %s", type->toChars());
assert(0); assert(0);
return 0; return 0;
@@ -854,6 +856,7 @@ void DtoConstInitGlobal(VarDeclaration* vd)
gvar->setInitializer(initVal); gvar->setInitializer(initVal);
#ifndef DISABLE_DEBUG_INFO
// do debug info // do debug info
if (global.params.symdebug) if (global.params.symdebug)
{ {
@@ -861,6 +864,7 @@ void DtoConstInitGlobal(VarDeclaration* vd)
// keep a reference so GDCE doesn't delete it ! // keep a reference so GDCE doesn't delete it !
gIR->usedArray.push_back(llvm::ConstantExpr::getBitCast(gv, getVoidPtrType())); gIR->usedArray.push_back(llvm::ConstantExpr::getBitCast(gv, getVoidPtrType()));
} }
#endif
} }
} }
@@ -906,7 +910,7 @@ DValue* DtoDeclarationExp(Dsymbol* declaration)
ExpInitializer* ex = vd->init->isExpInitializer(); ExpInitializer* ex = vd->init->isExpInitializer();
assert(ex && "ref vars must have expression initializer"); assert(ex && "ref vars must have expression initializer");
assert(ex->exp); assert(ex->exp);
AssignExp* as = dynamic_cast<AssignExp*>(ex->exp); AssignExp* as = ex->exp->isAssignExp();
assert(as && "ref vars must be initialized by an assign exp"); assert(as && "ref vars must be initialized by an assign exp");
vd->ir.irLocal->value = as->e2->toElem(gIR)->getLVal(); vd->ir.irLocal->value = as->e2->toElem(gIR)->getLVal();
} }
@@ -937,10 +941,10 @@ DValue* DtoDeclarationExp(Dsymbol* declaration)
//allocainst->setAlignment(vd->type->alignsize()); // TODO //allocainst->setAlignment(vd->type->alignsize()); // TODO
vd->ir.irLocal->value = allocainst; vd->ir.irLocal->value = allocainst;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
{
DtoDwarfLocalVariable(allocainst, vd); DtoDwarfLocalVariable(allocainst, vd);
} #endif
} }
else else
{ {
@@ -1053,9 +1057,11 @@ LLValue* DtoRawVarDeclaration(VarDeclaration* var, LLValue* addr)
{ {
addr = DtoAlloca(var->type, var->toChars()); addr = DtoAlloca(var->type, var->toChars());
#ifndef DISABLE_DEBUG_INFO
// add debug info // add debug info
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfLocalVariable(addr, var); DtoDwarfLocalVariable(addr, var);
#endif
} }
// referenced by nested function? // referenced by nested function?

View File

@@ -156,7 +156,7 @@ LLValue* makeLValue(Loc& loc, DValue* value);
//////////////////////////////////////////// ////////////////////////////////////////////
/// convert DMD calling conv to LLVM /// convert DMD calling conv to LLVM
unsigned DtoCallingConv(Loc loc, LINK l); llvm::CallingConv::ID DtoCallingConv(Loc loc, LINK l);
/// ///
TypeFunction* DtoTypeFunction(DValue* fnval); TypeFunction* DtoTypeFunction(DValue* fnval);

View File

@@ -12,7 +12,7 @@
#include "llvm/GlobalValue.h" #include "llvm/GlobalValue.h"
#include "llvm/Support/Casting.h" #include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_os_ostream.h"
#include "llvm/Assembly/Writer.h" #include "llvm/Assembly/Writer.h"
#include "gen/logger.h" #include "gen/logger.h"
@@ -27,10 +27,11 @@ void Stream::writeValue(std::ostream& OS, const llvm::Value& V) {
// Constants don't always get their types pretty-printed. // Constants don't always get their types pretty-printed.
// (Only treat non-global constants like this, so that e.g. global variables // (Only treat non-global constants like this, so that e.g. global variables
// still get their initializers printed) // still get their initializers printed)
llvm::raw_os_ostream raw(OS);
if (llvm::isa<llvm::Constant>(V) && !llvm::isa<llvm::GlobalValue>(V)) if (llvm::isa<llvm::Constant>(V) && !llvm::isa<llvm::GlobalValue>(V))
llvm::WriteAsOperand(OS, &V, true, gIR->module); llvm::WriteAsOperand(raw, &V, true, gIR->module);
else else
OS << V; V.print(raw);
} }
namespace Logger namespace Logger

View File

@@ -26,13 +26,15 @@ public:
Stream(std::ostream* S) : OS(S) {} Stream(std::ostream* S) : OS(S) {}
Stream(std::ostream& S) : OS(&S) {} Stream(std::ostream& S) : OS(&S) {}
/*
Stream operator << (std::ios_base &(*Func)(std::ios_base&)) { Stream operator << (std::ios_base &(*Func)(std::ios_base&)) {
if (OS) *OS << Func; if (OS) *OS << Func;
return *this; return *this;
} }
*/
Stream operator << (std::ostream &(*Func)(std::ostream&)) { Stream operator << (std::ostream &(*Func)(std::ostream&)) {
if (OS) *OS << Func; if (OS) Func(*OS);
return *this; return *this;
} }

View File

@@ -484,10 +484,15 @@ LDC_TARGETS
FeaturesStr = Features.getString(); FeaturesStr = Features.getString();
} }
std::auto_ptr<llvm::TargetMachine> target(theTarget->createTargetMachine(triple, FeaturesStr)); // FIXME
assert(target.get() && "Could not allocate target machine!"); //std::auto_ptr<llvm::TargetMachine> target(theTarget->createTargetMachine(triple, FeaturesStr));
gTargetMachine = target.get(); //assert(target.get() && "Could not allocate target machine!");
gTargetData = gTargetMachine->getTargetData(); //gTargetMachine = target.get();
llvm::TargetMachine* target = theTarget->createTargetMachine(triple, FeaturesStr);
gTargetMachine = target;
gTargetData = target->getTargetData();
// get final data layout // get final data layout
std::string datalayout = gTargetData->getStringRepresentation(); std::string datalayout = gTargetData->getStringRepresentation();

View File

@@ -107,7 +107,7 @@ static void addPassesForOptLevel(PassManager& pm) {
{ {
//addPass(pm, createStripDeadPrototypesPass()); //addPass(pm, createStripDeadPrototypesPass());
addPass(pm, createGlobalDCEPass()); addPass(pm, createGlobalDCEPass());
addPass(pm, createRaiseAllocationsPass()); addPass(pm, createPromoteMemoryToRegisterPass());
addPass(pm, createCFGSimplificationPass()); addPass(pm, createCFGSimplificationPass());
if (optimizeLevel == 1) if (optimizeLevel == 1)
addPass(pm, createPromoteMemoryToRegisterPass()); addPass(pm, createPromoteMemoryToRegisterPass());
@@ -177,7 +177,7 @@ static void addPassesForOptLevel(PassManager& pm) {
addPass(pm, createCFGSimplificationPass()); addPass(pm, createCFGSimplificationPass());
addPass(pm, createScalarReplAggregatesPass()); addPass(pm, createScalarReplAggregatesPass());
addPass(pm, createInstructionCombiningPass()); addPass(pm, createInstructionCombiningPass());
addPass(pm, createCondPropagationPass()); addPass(pm, createConstantPropagationPass());
addPass(pm, createReassociatePass()); addPass(pm, createReassociatePass());
addPass(pm, createLoopRotatePass()); addPass(pm, createLoopRotatePass());
@@ -194,7 +194,7 @@ static void addPassesForOptLevel(PassManager& pm) {
addPass(pm, createSCCPPass()); addPass(pm, createSCCPPass());
addPass(pm, createInstructionCombiningPass()); addPass(pm, createInstructionCombiningPass());
addPass(pm, createCondPropagationPass()); addPass(pm, createConstantPropagationPass());
addPass(pm, createDeadStoreEliminationPass()); addPass(pm, createDeadStoreEliminationPass());
addPass(pm, createAggressiveDCEPass()); addPass(pm, createAggressiveDCEPass());

View File

@@ -18,6 +18,7 @@
#include "Passes.h" #include "Passes.h"
#include "llvm/Function.h"
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include "llvm/Intrinsics.h" #include "llvm/Intrinsics.h"
#include "llvm/Support/IRBuilder.h" #include "llvm/Support/IRBuilder.h"

View File

@@ -53,8 +53,10 @@ void ReturnStatement::toIR(IRState* p)
Logger::println("ReturnStatement::toIR(): %s", loc.toChars()); Logger::println("ReturnStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
// is there a return value expression? // is there a return value expression?
if (exp || (!exp && (p->topfunc() == p->mainFunc)) ) if (exp || (!exp && (p->topfunc() == p->mainFunc)) )
@@ -78,8 +80,10 @@ void ReturnStatement::toIR(IRState* p)
// emit scopes // emit scopes
DtoEnclosingHandlers(loc, NULL); DtoEnclosingHandlers(loc, NULL);
#ifndef DISABLE_DEBUG_INFO
// emit dbg end function // emit dbg end function
if (global.params.symdebug) DtoDwarfFuncEnd(f->decl); if (global.params.symdebug) DtoDwarfFuncEnd(f->decl);
#endif
// emit ret // emit ret
llvm::ReturnInst::Create(gIR->context(), p->scopebb()); llvm::ReturnInst::Create(gIR->context(), p->scopebb());
@@ -126,7 +130,9 @@ void ReturnStatement::toIR(IRState* p)
DtoEnclosingHandlers(loc, NULL); DtoEnclosingHandlers(loc, NULL);
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) DtoDwarfFuncEnd(p->func()->decl); if (global.params.symdebug) DtoDwarfFuncEnd(p->func()->decl);
#endif
llvm::ReturnInst::Create(gIR->context(), v, p->scopebb()); llvm::ReturnInst::Create(gIR->context(), v, p->scopebb());
} }
} }
@@ -136,7 +142,9 @@ void ReturnStatement::toIR(IRState* p)
assert(p->topfunc()->getReturnType() == LLType::getVoidTy(gIR->context())); assert(p->topfunc()->getReturnType() == LLType::getVoidTy(gIR->context()));
DtoEnclosingHandlers(loc, NULL); DtoEnclosingHandlers(loc, NULL);
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) DtoDwarfFuncEnd(p->func()->decl); if (global.params.symdebug) DtoDwarfFuncEnd(p->func()->decl);
#endif
llvm::ReturnInst::Create(gIR->context(), p->scopebb()); llvm::ReturnInst::Create(gIR->context(), p->scopebb());
} }
@@ -153,8 +161,10 @@ void ExpStatement::toIR(IRState* p)
Logger::println("ExpStatement::toIR(): %s", loc.toChars()); Logger::println("ExpStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
if (exp) { if (exp) {
if (global.params.llvmAnnotate) if (global.params.llvmAnnotate)
@@ -182,8 +192,10 @@ void IfStatement::toIR(IRState* p)
Logger::println("IfStatement::toIR(): %s", loc.toChars()); Logger::println("IfStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
if (match) if (match)
DtoRawVarDeclaration(match); DtoRawVarDeclaration(match);
@@ -270,8 +282,10 @@ void WhileStatement::toIR(IRState* p)
Logger::println("WhileStatement::toIR(): %s", loc.toChars()); Logger::println("WhileStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
// create while blocks // create while blocks
llvm::BasicBlock* oldend = gIR->scopeend(); llvm::BasicBlock* oldend = gIR->scopeend();
@@ -318,8 +332,10 @@ void DoStatement::toIR(IRState* p)
Logger::println("DoStatement::toIR(): %s", loc.toChars()); Logger::println("DoStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
// create while blocks // create while blocks
llvm::BasicBlock* oldend = gIR->scopeend(); llvm::BasicBlock* oldend = gIR->scopeend();
@@ -363,8 +379,10 @@ void ForStatement::toIR(IRState* p)
Logger::println("ForStatement::toIR(): %s", loc.toChars()); Logger::println("ForStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
// create for blocks // create for blocks
llvm::BasicBlock* oldend = gIR->scopeend(); llvm::BasicBlock* oldend = gIR->scopeend();
@@ -443,8 +461,10 @@ void BreakStatement::toIR(IRState* p)
if (p->scopereturned()) if (p->scopereturned())
return; return;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
if (ident != 0) { if (ident != 0) {
Logger::println("ident = %s", ident->toChars()); Logger::println("ident = %s", ident->toChars());
@@ -498,8 +518,10 @@ void ContinueStatement::toIR(IRState* p)
Logger::println("ContinueStatement::toIR(): %s", loc.toChars()); Logger::println("ContinueStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
if (ident != 0) { if (ident != 0) {
Logger::println("ident = %s", ident->toChars()); Logger::println("ident = %s", ident->toChars());
@@ -564,8 +586,10 @@ void TryFinallyStatement::toIR(IRState* p)
Logger::println("TryFinallyStatement::toIR(): %s", loc.toChars()); Logger::println("TryFinallyStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
// if there's no finalbody or no body, things are simple // if there's no finalbody or no body, things are simple
if (!finalbody) { if (!finalbody) {
@@ -642,8 +666,10 @@ void TryCatchStatement::toIR(IRState* p)
Logger::println("TryCatchStatement::toIR(): %s", loc.toChars()); Logger::println("TryCatchStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
// create basic blocks // create basic blocks
llvm::BasicBlock* oldend = p->scopeend(); llvm::BasicBlock* oldend = p->scopeend();
@@ -698,13 +724,17 @@ void ThrowStatement::toIR(IRState* p)
Logger::println("ThrowStatement::toIR(): %s", loc.toChars()); Logger::println("ThrowStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
assert(exp); assert(exp);
DValue* e = exp->toElem(p); DValue* e = exp->toElem(p);
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) DtoDwarfFuncEnd(gIR->func()->decl); if (global.params.symdebug) DtoDwarfFuncEnd(gIR->func()->decl);
#endif
llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_throw_exception"); llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_throw_exception");
//Logger::cout() << "calling: " << *fn << '\n'; //Logger::cout() << "calling: " << *fn << '\n';
@@ -780,8 +810,10 @@ void SwitchStatement::toIR(IRState* p)
Logger::println("SwitchStatement::toIR(): %s", loc.toChars()); Logger::println("SwitchStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
llvm::BasicBlock* oldend = gIR->scopeend(); llvm::BasicBlock* oldend = gIR->scopeend();
@@ -951,8 +983,10 @@ void UnrolledLoopStatement::toIR(IRState* p)
if (!statements || !statements->dim) if (!statements || !statements->dim)
return; return;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
// DMD doesn't fold stuff like continue/break, and since this isn't really a loop // DMD doesn't fold stuff like continue/break, and since this isn't really a loop
// we have to keep track of each statement and jump to the next/end on continue/break // we have to keep track of each statement and jump to the next/end on continue/break
@@ -1017,8 +1051,10 @@ void ForeachStatement::toIR(IRState* p)
Logger::println("ForeachStatement::toIR(): %s", loc.toChars()); Logger::println("ForeachStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
//assert(arguments->dim == 1); //assert(arguments->dim == 1);
assert(value != 0); assert(value != 0);
@@ -1149,8 +1185,10 @@ void ForeachRangeStatement::toIR(IRState* p)
Logger::println("ForeachRangeStatement::toIR(): %s", loc.toChars()); Logger::println("ForeachRangeStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
// evaluate lwr/upr // evaluate lwr/upr
assert(lwr->type->isintegral()); assert(lwr->type->isintegral());
@@ -1298,8 +1336,10 @@ void GotoStatement::toIR(IRState* p)
Logger::println("GotoStatement::toIR(): %s", loc.toChars()); Logger::println("GotoStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
llvm::BasicBlock* oldend = gIR->scopeend(); llvm::BasicBlock* oldend = gIR->scopeend();
llvm::BasicBlock* bb = llvm::BasicBlock::Create(gIR->context(), "aftergoto", p->topfunc(), oldend); llvm::BasicBlock* bb = llvm::BasicBlock::Create(gIR->context(), "aftergoto", p->topfunc(), oldend);
@@ -1316,8 +1356,10 @@ void GotoDefaultStatement::toIR(IRState* p)
Logger::println("GotoDefaultStatement::toIR(): %s", loc.toChars()); Logger::println("GotoDefaultStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
llvm::BasicBlock* oldend = gIR->scopeend(); llvm::BasicBlock* oldend = gIR->scopeend();
llvm::BasicBlock* bb = llvm::BasicBlock::Create(gIR->context(), "aftergotodefault", p->topfunc(), oldend); llvm::BasicBlock* bb = llvm::BasicBlock::Create(gIR->context(), "aftergotodefault", p->topfunc(), oldend);
@@ -1338,8 +1380,10 @@ void GotoCaseStatement::toIR(IRState* p)
Logger::println("GotoCaseStatement::toIR(): %s", loc.toChars()); Logger::println("GotoCaseStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
llvm::BasicBlock* oldend = gIR->scopeend(); llvm::BasicBlock* oldend = gIR->scopeend();
llvm::BasicBlock* bb = llvm::BasicBlock::Create(gIR->context(), "aftergotocase", p->topfunc(), oldend); llvm::BasicBlock* bb = llvm::BasicBlock::Create(gIR->context(), "aftergotocase", p->topfunc(), oldend);
@@ -1363,8 +1407,10 @@ void WithStatement::toIR(IRState* p)
Logger::println("WithStatement::toIR(): %s", loc.toChars()); Logger::println("WithStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
assert(exp); assert(exp);
assert(body); assert(body);
@@ -1393,8 +1439,10 @@ void SynchronizedStatement::toIR(IRState* p)
Logger::println("SynchronizedStatement::toIR(): %s", loc.toChars()); Logger::println("SynchronizedStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
// enter lock // enter lock
if (exp) if (exp)
@@ -1430,8 +1478,10 @@ void VolatileStatement::toIR(IRState* p)
Logger::println("VolatileStatement::toIR(): %s", loc.toChars()); Logger::println("VolatileStatement::toIR(): %s", loc.toChars());
LOG_SCOPE; LOG_SCOPE;
#ifndef DISABLE_DEBUG_INFO
if (global.params.symdebug) if (global.params.symdebug)
DtoDwarfStopPoint(loc.linnum); DtoDwarfStopPoint(loc.linnum);
#endif
// mark in-volatile // mark in-volatile
// FIXME // FIXME

View File

@@ -35,7 +35,7 @@ TypeFunction* DtoTypeFunction(DValue* fnval)
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
unsigned DtoCallingConv(Loc loc, LINK l) llvm::CallingConv::ID DtoCallingConv(Loc loc, LINK l)
{ {
if (l == LINKc || l == LINKcpp || l == LINKintrinsic) if (l == LINKc || l == LINKcpp || l == LINKintrinsic)
return llvm::CallingConv::C; return llvm::CallingConv::C;
@@ -207,7 +207,7 @@ void DtoBuildDVarArgList(std::vector<LLValue*>& args, std::vector<llvm::Attribut
pinits.push_back(DtoConstSize_t(vtype->getNumElements())); pinits.push_back(DtoConstSize_t(vtype->getNumElements()));
pinits.push_back(llvm::ConstantExpr::getBitCast(typeinfomem, getPtrToType(typeinfotype))); pinits.push_back(llvm::ConstantExpr::getBitCast(typeinfomem, getPtrToType(typeinfotype)));
const LLType* tiarrty = DtoType(Type::typeinfo->type->arrayOf()); const LLType* tiarrty = DtoType(Type::typeinfo->type->arrayOf());
tiinits = LLConstantStruct::get(gIR->context(), pinits); tiinits = LLConstantStruct::get(gIR->context(), pinits, false);
LLValue* typeinfoarrayparam = new llvm::GlobalVariable(*gIR->module, tiarrty, LLValue* typeinfoarrayparam = new llvm::GlobalVariable(*gIR->module, tiarrty,
true, llvm::GlobalValue::InternalLinkage, tiinits, "._arguments.array"); true, llvm::GlobalValue::InternalLinkage, tiinits, "._arguments.array");
@@ -279,7 +279,7 @@ DValue* DtoCallFunction(Loc& loc, Type* resulttype, DValue* fnval, Expressions*
bool nestedcall = tf->fty.arg_nest; bool nestedcall = tf->fty.arg_nest;
bool dvarargs = (tf->linkage == LINKd && tf->varargs == 1); bool dvarargs = (tf->linkage == LINKd && tf->varargs == 1);
unsigned callconv = DtoCallingConv(loc, tf->linkage); llvm::CallingConv::ID callconv = DtoCallingConv(loc, tf->linkage);
// get callee llvm value // get callee llvm value
LLValue* callable = DtoCallableValue(fnval); LLValue* callable = DtoCallableValue(fnval);

View File

@@ -19,6 +19,8 @@
using namespace llvm::dwarf; using namespace llvm::dwarf;
#ifndef DISABLE_DEBUG_INFO
#define DBG_NULL ( LLConstant::getNullValue(DBG_TYPE) ) #define DBG_NULL ( LLConstant::getNullValue(DBG_TYPE) )
#define DBG_TYPE ( getPtrToType(llvm::StructType::get(gIR->context(),NULL,NULL)) ) #define DBG_TYPE ( getPtrToType(llvm::StructType::get(gIR->context(),NULL,NULL)) )
#define DBG_CAST(X) ( llvm::ConstantExpr::getBitCast(X, DBG_TYPE) ) #define DBG_CAST(X) ( llvm::ConstantExpr::getBitCast(X, DBG_TYPE) )
@@ -636,3 +638,5 @@ void DtoDwarfStopPoint(unsigned ln)
gIR->scopebb() gIR->scopebb()
); );
} }
#endif

View File

@@ -1,6 +1,8 @@
#ifndef LDC_GEN_TODEBUG_H #ifndef LDC_GEN_TODEBUG_H
#define LDC_GEN_TODEBUG_H #define LDC_GEN_TODEBUG_H
#ifndef DISABLE_DEBUG_INFO
void RegisterDwarfSymbols(llvm::Module* mod); void RegisterDwarfSymbols(llvm::Module* mod);
/** /**
@@ -45,6 +47,6 @@ void DtoDwarfLocalVariable(LLValue* ll, VarDeclaration* vd);
*/ */
llvm::DIGlobalVariable DtoDwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd); llvm::DIGlobalVariable DtoDwarfGlobalVariable(LLGlobalVariable* ll, VarDeclaration* vd);
#endif // DISABLE_DEBUG_INFO
#endif // LDC_GEN_TODEBUG_H #endif // LDC_GEN_TODEBUG_H

View File

@@ -362,7 +362,7 @@ LLValue* DtoPointedType(LLValue* ptr, LLValue* val)
return val; return val;
} }
// ptr is integer pointer // ptr is integer pointer
else if (ptrTy->isInteger()) else if (ptrTy->isIntegerTy())
{ {
// val is integer // val is integer
assert(valTy->isInteger()); assert(valTy->isInteger());

View File

@@ -14,7 +14,6 @@
#include "llvm/Analysis/Verifier.h" #include "llvm/Analysis/Verifier.h"
#include "llvm/Bitcode/ReaderWriter.h" #include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/ModuleProvider.h"
#include "llvm/PassManager.h" #include "llvm/PassManager.h"
#include "llvm/LinkAllPasses.h" #include "llvm/LinkAllPasses.h"
#include "llvm/System/Program.h" #include "llvm/System/Program.h"
@@ -22,6 +21,7 @@
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Support/FormattedStream.h" #include "llvm/Support/FormattedStream.h"
#include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetMachine.h"
#include "llvm/CodeGen/MachineCodeEmitter.h"
#include "mars.h" #include "mars.h"
#include "module.h" #include "module.h"
@@ -114,11 +114,13 @@ llvm::Module* Module::genLLVMModule(llvm::LLVMContext& context, Ir* sir)
// allocate the target abi // allocate the target abi
gABI = TargetABI::getTarget(); gABI = TargetABI::getTarget();
#ifndef DISABLE_DEBUG_INFO
// debug info // debug info
if (global.params.symdebug) { if (global.params.symdebug) {
RegisterDwarfSymbols(ir.module); RegisterDwarfSymbols(ir.module);
DtoDwarfCompileUnit(this); DtoDwarfCompileUnit(this);
} }
#endif
// handle invalid 'objectø module // handle invalid 'objectø module
if (!ClassDeclaration::object) { if (!ClassDeclaration::object) {
@@ -225,10 +227,11 @@ void writeModule(llvm::Module* m, std::string filename)
bcpath.eraseSuffix(); bcpath.eraseSuffix();
bcpath.appendSuffix(std::string(global.bc_ext)); bcpath.appendSuffix(std::string(global.bc_ext));
Logger::println("Writing LLVM bitcode to: %s\n", bcpath.c_str()); Logger::println("Writing LLVM bitcode to: %s\n", bcpath.c_str());
std::ofstream bos(bcpath.c_str(), std::ios::binary); std::string errinfo;
if (bos.fail()) llvm::raw_fd_ostream bos(bcpath.c_str(), errinfo, llvm::raw_fd_ostream::F_Binary);
if (bos.has_error())
{ {
error("cannot write LLVM bitcode, failed to open file '%s'", bcpath.c_str()); error("cannot write LLVM bitcode file '%s': %s", bcpath.c_str(), errinfo.c_str());
fatal(); fatal();
} }
llvm::WriteBitcodeToFile(m, bos); llvm::WriteBitcodeToFile(m, bos);
@@ -240,10 +243,11 @@ void writeModule(llvm::Module* m, std::string filename)
llpath.eraseSuffix(); llpath.eraseSuffix();
llpath.appendSuffix(std::string(global.ll_ext)); llpath.appendSuffix(std::string(global.ll_ext));
Logger::println("Writing LLVM asm to: %s\n", llpath.c_str()); Logger::println("Writing LLVM asm to: %s\n", llpath.c_str());
std::ofstream aos(llpath.c_str()); std::string errinfo;
if (aos.fail()) llvm::raw_fd_ostream aos(llpath.c_str(), errinfo);
if (aos.has_error())
{ {
error("cannot write LLVM asm, failed to open file '%s'", llpath.c_str()); error("cannot write LLVM asm file '%s': %s", llpath.c_str(), errinfo.c_str());
fatal(); fatal();
} }
m->print(aos, NULL); m->print(aos, NULL);
@@ -260,7 +264,7 @@ void writeModule(llvm::Module* m, std::string filename)
Logger::println("Writing native asm to: %s\n", spath.c_str()); Logger::println("Writing native asm to: %s\n", spath.c_str());
std::string err; std::string err;
{ {
llvm::raw_fd_ostream out(spath.c_str(), false, true, err); llvm::raw_fd_ostream out(spath.c_str(), err);
if (err.empty()) if (err.empty())
{ {
write_asm_to_file(*gTargetMachine, *m, out); write_asm_to_file(*gTargetMachine, *m, out);
@@ -292,17 +296,13 @@ void write_asm_to_file(llvm::TargetMachine &Target, llvm::Module& m, llvm::raw_f
using namespace llvm; using namespace llvm;
// Build up all of the passes that we want to do to the module. // Build up all of the passes that we want to do to the module.
ExistingModuleProvider Provider(&m); FunctionPassManager Passes(&m);
FunctionPassManager Passes(&Provider);
if (const TargetData *TD = Target.getTargetData()) if (const TargetData *TD = Target.getTargetData())
Passes.add(new TargetData(*TD)); Passes.add(new TargetData(*TD));
else else
Passes.add(new TargetData(&m)); Passes.add(new TargetData(&m));
// Ask the target to add backend passes as necessary.
MachineCodeEmitter *MCE = 0;
// Last argument is enum CodeGenOpt::Level OptLevel // Last argument is enum CodeGenOpt::Level OptLevel
// debug info doesn't work properly with OptLevel != None! // debug info doesn't work properly with OptLevel != None!
CodeGenOpt::Level LastArg = CodeGenOpt::Default; CodeGenOpt::Level LastArg = CodeGenOpt::Default;
@@ -312,11 +312,8 @@ void write_asm_to_file(llvm::TargetMachine &Target, llvm::Module& m, llvm::raw_f
LastArg = CodeGenOpt::Aggressive; LastArg = CodeGenOpt::Aggressive;
llvm::formatted_raw_ostream fout(out); llvm::formatted_raw_ostream fout(out);
FileModel::Model mod = Target.addPassesToEmitFile(Passes, fout, TargetMachine::AssemblyFile, LastArg); if (Target.addPassesToEmitFile(Passes, fout, TargetMachine::CGFT_AssemblyFile, LastArg))
assert(mod == FileModel::AsmFile); assert(0 && "no support for asm output");
bool err = Target.addPassesToEmitFileFinish(Passes, MCE, LastArg);
assert(!err);
Passes.doInitialization(); Passes.doInitialization();
@@ -328,9 +325,9 @@ void write_asm_to_file(llvm::TargetMachine &Target, llvm::Module& m, llvm::raw_f
Passes.doFinalization(); Passes.doFinalization();
// release module from module provider so we can delete it ourselves // release module from module provider so we can delete it ourselves
std::string Err; //std::string Err;
llvm::Module* rmod = Provider.releaseModule(&Err); //llvm::Module* rmod = Provider.releaseModule(&Err);
assert(rmod); //assert(rmod);
} }
/* ================================================================== */ /* ================================================================== */
@@ -350,14 +347,14 @@ void assemble(const llvm::sys::Path& asmpath, const llvm::sys::Path& objpath)
// and linker because we don't know where to put the _start symbol. // and linker because we don't know where to put the _start symbol.
// GCC mysteriously knows how to do it. // GCC mysteriously knows how to do it.
std::vector<std::string> args; std::vector<std::string> args;
args.push_back(gcc.toString()); args.push_back(gcc.str());
args.push_back("-fno-strict-aliasing"); args.push_back("-fno-strict-aliasing");
args.push_back("-O3"); args.push_back("-O3");
args.push_back("-c"); args.push_back("-c");
args.push_back("-xassembler"); args.push_back("-xassembler");
args.push_back(asmpath.toString()); args.push_back(asmpath.str());
args.push_back("-o"); args.push_back("-o");
args.push_back(objpath.toString()); args.push_back(objpath.str());
//FIXME: only use this if needed? //FIXME: only use this if needed?
args.push_back("-fpic"); args.push_back("-fpic");
@@ -431,11 +428,13 @@ llvm::Function* build_module_ctor()
IRBuilder<> builder(bb); IRBuilder<> builder(bb);
// debug info // debug info
#ifndef DISABLE_DEBUG_INFO
LLGlobalVariable* subprog; LLGlobalVariable* subprog;
if(global.params.symdebug) { if(global.params.symdebug) {
subprog = DtoDwarfSubProgramInternal(name.c_str(), name.c_str()).getGV(); subprog = DtoDwarfSubProgramInternal(name.c_str(), name.c_str()).getGV();
builder.CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(subprog)); builder.CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(subprog));
} }
#endif
for (size_t i=0; i<n; i++) { for (size_t i=0; i<n; i++) {
llvm::Function* f = gIR->ctors[i]->ir.irFunc->func; llvm::Function* f = gIR->ctors[i]->ir.irFunc->func;
@@ -444,8 +443,10 @@ llvm::Function* build_module_ctor()
} }
// debug info end // debug info end
#ifndef DISABLE_DEBUG_INFO
if(global.params.symdebug) if(global.params.symdebug)
builder.CreateCall(gIR->module->getFunction("llvm.dbg.region.end"), DBG_CAST(subprog)); builder.CreateCall(gIR->module->getFunction("llvm.dbg.region.end"), DBG_CAST(subprog));
#endif
builder.CreateRetVoid(); builder.CreateRetVoid();
return fn; return fn;
@@ -475,12 +476,14 @@ static llvm::Function* build_module_dtor()
llvm::BasicBlock* bb = llvm::BasicBlock::Create(gIR->context(), "entry", fn); llvm::BasicBlock* bb = llvm::BasicBlock::Create(gIR->context(), "entry", fn);
IRBuilder<> builder(bb); IRBuilder<> builder(bb);
#ifndef DISABLE_DEBUG_INFO
// debug info // debug info
LLGlobalVariable* subprog; LLGlobalVariable* subprog;
if(global.params.symdebug) { if(global.params.symdebug) {
subprog = DtoDwarfSubProgramInternal(name.c_str(), name.c_str()).getGV(); subprog = DtoDwarfSubProgramInternal(name.c_str(), name.c_str()).getGV();
builder.CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(subprog)); builder.CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(subprog));
} }
#endif
for (size_t i=0; i<n; i++) { for (size_t i=0; i<n; i++) {
llvm::Function* f = gIR->dtors[i]->ir.irFunc->func; llvm::Function* f = gIR->dtors[i]->ir.irFunc->func;
@@ -488,9 +491,11 @@ static llvm::Function* build_module_dtor()
call->setCallingConv(DtoCallingConv(0, LINKd)); call->setCallingConv(DtoCallingConv(0, LINKd));
} }
#ifndef DISABLE_DEBUG_INFO
// debug info end // debug info end
if(global.params.symdebug) if(global.params.symdebug)
builder.CreateCall(gIR->module->getFunction("llvm.dbg.region.end"), DBG_CAST(subprog)); builder.CreateCall(gIR->module->getFunction("llvm.dbg.region.end"), DBG_CAST(subprog));
#endif
builder.CreateRetVoid(); builder.CreateRetVoid();
return fn; return fn;
@@ -520,12 +525,14 @@ static llvm::Function* build_module_unittest()
llvm::BasicBlock* bb = llvm::BasicBlock::Create(gIR->context(), "entry", fn); llvm::BasicBlock* bb = llvm::BasicBlock::Create(gIR->context(), "entry", fn);
IRBuilder<> builder(bb); IRBuilder<> builder(bb);
#ifndef DISABLE_DEBUG_INFO
// debug info // debug info
LLGlobalVariable* subprog; LLGlobalVariable* subprog;
if(global.params.symdebug) { if(global.params.symdebug) {
subprog = DtoDwarfSubProgramInternal(name.c_str(), name.c_str()).getGV(); subprog = DtoDwarfSubProgramInternal(name.c_str(), name.c_str()).getGV();
builder.CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(subprog)); builder.CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(subprog));
} }
#endif
for (size_t i=0; i<n; i++) { for (size_t i=0; i<n; i++) {
llvm::Function* f = gIR->unitTests[i]->ir.irFunc->func; llvm::Function* f = gIR->unitTests[i]->ir.irFunc->func;
@@ -533,9 +540,11 @@ static llvm::Function* build_module_unittest()
call->setCallingConv(DtoCallingConv(0, LINKd)); call->setCallingConv(DtoCallingConv(0, LINKd));
} }
#ifndef DISABLE_DEBUG_INFO
// debug info end // debug info end
if(global.params.symdebug) if(global.params.symdebug)
builder.CreateCall(gIR->module->getFunction("llvm.dbg.region.end"), DBG_CAST(subprog)); builder.CreateCall(gIR->module->getFunction("llvm.dbg.region.end"), DBG_CAST(subprog));
#endif
builder.CreateRetVoid(); builder.CreateRetVoid();
return fn; return fn;
@@ -578,11 +587,13 @@ static LLFunction* build_module_reference_and_ctor(LLConstant* moduleinfo)
IRBuilder<> builder(bb); IRBuilder<> builder(bb);
// debug info // debug info
#ifndef DISABLE_DEBUG_INFO
LLGlobalVariable* subprog; LLGlobalVariable* subprog;
if(global.params.symdebug) { if(global.params.symdebug) {
subprog = DtoDwarfSubProgramInternal(fname.c_str(), fname.c_str()).getGV(); subprog = DtoDwarfSubProgramInternal(fname.c_str(), fname.c_str()).getGV();
builder.CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(subprog)); builder.CreateCall(gIR->module->getFunction("llvm.dbg.func.start"), DBG_CAST(subprog));
} }
#endif
// get current beginning // get current beginning
LLValue* curbeg = builder.CreateLoad(mref, "current"); LLValue* curbeg = builder.CreateLoad(mref, "current");
@@ -594,9 +605,11 @@ static LLFunction* build_module_reference_and_ctor(LLConstant* moduleinfo)
// replace beginning // replace beginning
builder.CreateStore(thismref, mref); builder.CreateStore(thismref, mref);
#ifndef DISABLE_DEBUG_INFO
// debug info end // debug info end
if(global.params.symdebug) if(global.params.symdebug)
builder.CreateCall(gIR->module->getFunction("llvm.dbg.region.end"), DBG_CAST(subprog)); builder.CreateCall(gIR->module->getFunction("llvm.dbg.region.end"), DBG_CAST(subprog));
#endif
// return // return
builder.CreateRetVoid(); builder.CreateRetVoid();

View File

@@ -483,7 +483,7 @@ LLConstant * IrStruct::getClassInfoInterfaces()
// create Interface struct // create Interface struct
LLConstant* inits[3] = { ci, vtb, off }; LLConstant* inits[3] = { ci, vtb, off };
LLConstant* entry = LLConstantStruct::get(gIR->context(), inits, 3); LLConstant* entry = LLConstantStruct::get(gIR->context(), inits, 3, false);
constants.push_back(entry); constants.push_back(entry);
} }

View File

@@ -133,7 +133,7 @@ void IRLandingPad::constructLandingPad(llvm::BasicBlock* inBB)
} }
// if there's a finally, the eh table has to have a 0 action // if there's a finally, the eh table has to have a 0 action
if(hasFinally) if(hasFinally)
selectorargs.push_back(DtoConstSize_t(0));//LLConstantInt::get(LLType::getInt32Ty(gIR->context()), 0)); selectorargs.push_back(DtoConstUint(0));
// personality fn // personality fn
llvm::Function* personality_fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_eh_personality"); llvm::Function* personality_fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_eh_personality");
@@ -151,11 +151,7 @@ void IRLandingPad::constructLandingPad(llvm::BasicBlock* inBB)
} }
// eh_sel = llvm.eh.selector(eh_ptr, cast(byte*)&_d_eh_personality, <selectorargs>); // eh_sel = llvm.eh.selector(eh_ptr, cast(byte*)&_d_eh_personality, <selectorargs>);
llvm::Function* eh_selector_fn; llvm::Function* eh_selector_fn = GET_INTRINSIC_DECL(eh_selector);
if (global.params.is64bit)
eh_selector_fn = GET_INTRINSIC_DECL(eh_selector_i64);
else
eh_selector_fn = GET_INTRINSIC_DECL(eh_selector_i32);
LLValue* eh_sel = gIR->ir->CreateCall(eh_selector_fn, selectorargs.begin(), selectorargs.end()); LLValue* eh_sel = gIR->ir->CreateCall(eh_selector_fn, selectorargs.begin(), selectorargs.end());
// emit finallys and switches that branch to catches until there are no more catches // emit finallys and switches that branch to catches until there are no more catches
@@ -186,7 +182,7 @@ void IRLandingPad::constructLandingPad(llvm::BasicBlock* inBB)
} }
// dubious comment // dubious comment
// catches matched first get the largest switchval, so do size - unique int // catches matched first get the largest switchval, so do size - unique int
llvm::ConstantInt* switchval = LLConstantInt::get(DtoSize_t(), catchToInt[rit->catchType]); llvm::ConstantInt* switchval = DtoConstUint(catchToInt[rit->catchType]);
// and make sure we don't add the same switchval twice, may happen with nested trys // and make sure we don't add the same switchval twice, may happen with nested trys
if(!switchinst->findCaseValue(switchval)) if(!switchinst->findCaseValue(switchval))
switchinst->addCase(switchval, rit->target); switchinst->addCase(switchval, rit->target);

View File

@@ -1,10 +1,14 @@
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/LLVMContext.h"
#include "mars.h" #include "mars.h"
#include "mtype.h" #include "mtype.h"
#include "gen/irstate.h" #include "gen/irstate.h"
#include "gen/logger.h" #include "gen/logger.h"
#include "ir/irtype.h" #include "ir/irtype.h"
// This code uses llvm::getGlobalContext() as these functions are invoked before gIR is set.
// ... thus it segfaults on gIR==NULL
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
extern const llvm::Type* DtoType(Type* dt); extern const llvm::Type* DtoType(Type* dt);
@@ -43,30 +47,31 @@ const llvm::Type * IrTypeBasic::basic2llvm(Type* t)
{ {
const llvm::Type* t2; const llvm::Type* t2;
// FIXME: don't use getGlobalContext llvm::LLVMContext& ctx = llvm::getGlobalContext();
switch(t->ty) switch(t->ty)
{ {
case Tvoid: case Tvoid:
return llvm::Type::getVoidTy(llvm::getGlobalContext()); return llvm::Type::getVoidTy(ctx);
case Tint8: case Tint8:
case Tuns8: case Tuns8:
case Tchar: case Tchar:
return llvm::Type::getInt8Ty(llvm::getGlobalContext()); return llvm::Type::getInt8Ty(ctx);
case Tint16: case Tint16:
case Tuns16: case Tuns16:
case Twchar: case Twchar:
return llvm::Type::getInt16Ty(llvm::getGlobalContext()); return llvm::Type::getInt16Ty(ctx);
case Tint32: case Tint32:
case Tuns32: case Tuns32:
case Tdchar: case Tdchar:
return llvm::Type::getInt32Ty(llvm::getGlobalContext()); return llvm::Type::getInt32Ty(ctx);
case Tint64: case Tint64:
case Tuns64: case Tuns64:
return llvm::Type::getInt64Ty(llvm::getGlobalContext()); return llvm::Type::getInt64Ty(ctx);
/* /*
case Tint128: case Tint128:
@@ -76,37 +81,37 @@ const llvm::Type * IrTypeBasic::basic2llvm(Type* t)
case Tfloat32: case Tfloat32:
case Timaginary32: case Timaginary32:
return llvm::Type::getFloatTy(llvm::getGlobalContext()); return llvm::Type::getFloatTy(ctx);
case Tfloat64: case Tfloat64:
case Timaginary64: case Timaginary64:
return llvm::Type::getDoubleTy(llvm::getGlobalContext()); return llvm::Type::getDoubleTy(ctx);
case Tfloat80: case Tfloat80:
case Timaginary80: case Timaginary80:
// only x86 has 80bit float // only x86 has 80bit float
if (global.params.cpu == ARCHx86 || global.params.cpu == ARCHx86_64) if (global.params.cpu == ARCHx86 || global.params.cpu == ARCHx86_64)
return llvm::Type::getX86_FP80Ty(llvm::getGlobalContext()); return llvm::Type::getX86_FP80Ty(ctx);
// other platforms use 64bit reals // other platforms use 64bit reals
else else
return llvm::Type::getDoubleTy(llvm::getGlobalContext()); return llvm::Type::getDoubleTy(ctx);
case Tcomplex32: case Tcomplex32:
t2 = llvm::Type::getFloatTy(llvm::getGlobalContext()); t2 = llvm::Type::getFloatTy(ctx);
return llvm::StructType::get(llvm::getGlobalContext(), t2, t2, NULL); return llvm::StructType::get(ctx, t2, t2, NULL);
case Tcomplex64: case Tcomplex64:
t2 = llvm::Type::getDoubleTy(llvm::getGlobalContext()); t2 = llvm::Type::getDoubleTy(ctx);
return llvm::StructType::get(llvm::getGlobalContext(), t2, t2, NULL); return llvm::StructType::get(ctx, t2, t2, NULL);
case Tcomplex80: case Tcomplex80:
t2 = (global.params.cpu == ARCHx86 || global.params.cpu == ARCHx86_64) t2 = (global.params.cpu == ARCHx86 || global.params.cpu == ARCHx86_64)
? llvm::Type::getX86_FP80Ty(llvm::getGlobalContext()) ? llvm::Type::getX86_FP80Ty(ctx)
: llvm::Type::getDoubleTy(llvm::getGlobalContext()); : llvm::Type::getDoubleTy(ctx);
return llvm::StructType::get(llvm::getGlobalContext(), t2, t2, NULL); return llvm::StructType::get(ctx, t2, t2, NULL);
case Tbool: case Tbool:
return llvm::Type::getInt1Ty(llvm::getGlobalContext()); return llvm::Type::getInt1Ty(ctx);
} }
assert(0 && "not basic type"); assert(0 && "not basic type");

View File

@@ -1,5 +1,6 @@
#include "gen/llvm.h" #include "gen/llvm.h"
#include "declaration.h" #include "declaration.h"
#include "gen/irstate.h"
#include "ir/irvar.h" #include "ir/irvar.h"
@@ -18,7 +19,7 @@ IrVar::IrVar(VarDeclaration* var)
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
IrGlobal::IrGlobal(VarDeclaration* v): IrVar(v), IrGlobal::IrGlobal(VarDeclaration* v): IrVar(v),
type(llvm::OpaqueType::get(llvm::getGlobalContext())) type(llvm::OpaqueType::get(gIR->context()))
{ {
constInit = NULL; constInit = NULL;
} }

View File

@@ -1,6 +1,6 @@
Index: tango/core/rt/compiler/ldc/rt/lifetime.d Index: tango/core/rt/compiler/ldc/rt/lifetime.d
=================================================================== ===================================================================
--- tango/core/rt/compiler/ldc/rt/lifetime.d (revision 5368) --- tango/core/rt/compiler/ldc/rt/lifetime.d (revision 5462)
+++ tango/core/rt/compiler/ldc/rt/lifetime.d (working copy) +++ tango/core/rt/compiler/ldc/rt/lifetime.d (working copy)
@@ -786,6 +786,7 @@ @@ -786,6 +786,7 @@
return *cast(long*)px; return *cast(long*)px;
@@ -53,3 +53,650 @@ Index: tango/core/rt/compiler/ldc/rt/lifetime.d
/** /**
* *
Index: tango/core/rt/compiler/ldc/rt/eh.d
===================================================================
--- tango/core/rt/compiler/ldc/rt/eh.d (revision 5462)
+++ tango/core/rt/compiler/ldc/rt/eh.d (working copy)
@@ -1,38 +1,34 @@
/**
* This module contains functions and structures required for
- * exception handling.
+ * dwarf exception handling with llvm
*/
module rt.eh;
-import ldc.cstdarg;
-import rt.compiler.util.console;
+//debug = EH_personality;
-// debug = EH_personality;
-
// current EH implementation works on x86
// if it has a working unwind runtime
version(X86) {
version(linux) version=X86_UNWIND;
version(darwin) version=X86_UNWIND;
version(solaris) version=X86_UNWIND;
- version(freebsd) version=X86_UNWIND;
}
version(X86_64) {
version(linux) version=X86_UNWIND;
version(darwin) version=X86_UNWIND;
version(solaris) version=X86_UNWIND;
- version(freebsd) version=X86_UNWIND;
}
//version = HP_LIBUNWIND;
private extern(C) void abort();
private extern(C) int printf(char*, ...);
-private extern(C) int vprintf(char*, va_list va);
+//private extern(C) int vprintf(char*, va_list va);
// D runtime functions
extern(C) {
- int _d_isbaseof(ClassInfo oc, ClassInfo c);
+// int _d_isbaseof(ClassInfo oc, ClassInfo c);
+ Object _d_dynamic_cast(Object o, ClassInfo c);
}
// libunwind headers
@@ -74,16 +70,19 @@
// interface to HP's libunwind from http://www.nongnu.org/libunwind/
version(HP_LIBUNWIND)
{
+ // Haven't checked whether and how it has _Unwind_Get{Text,Data}RelBase
+ pragma (msg, "HP_LIBUNWIND interface is out of date and untested");
+
void __libunwind_Unwind_Resume(_Unwind_Exception *);
_Unwind_Reason_Code __libunwind_Unwind_RaiseException(_Unwind_Exception *);
ptrdiff_t __libunwind_Unwind_GetLanguageSpecificData(_Unwind_Context_Ptr
context);
- ptrdiff_t __libunwind_Unwind_GetIP(_Unwind_Context_Ptr context);
+ size_t __libunwind_Unwind_GetIP(_Unwind_Context_Ptr context);
ptrdiff_t __libunwind_Unwind_SetIP(_Unwind_Context_Ptr context,
ptrdiff_t new_value);
ptrdiff_t __libunwind_Unwind_SetGR(_Unwind_Context_Ptr context, int index,
ptrdiff_t new_value);
- ptrdiff_t __libunwind_Unwind_GetRegionStart(_Unwind_Context_Ptr context);
+ size_t __libunwind_Unwind_GetRegionStart(_Unwind_Context_Ptr context);
alias __libunwind_Unwind_Resume _Unwind_Resume;
alias __libunwind_Unwind_RaiseException _Unwind_RaiseException;
@@ -94,27 +93,30 @@
alias __libunwind_Unwind_SetGR _Unwind_SetGR;
alias __libunwind_Unwind_GetRegionStart _Unwind_GetRegionStart;
}
-else version(X86_UNWIND)
+else version(X86_UNWIND)
{
void _Unwind_Resume(_Unwind_Exception*);
_Unwind_Reason_Code _Unwind_RaiseException(_Unwind_Exception*);
ptrdiff_t _Unwind_GetLanguageSpecificData(_Unwind_Context_Ptr context);
- ptrdiff_t _Unwind_GetIP(_Unwind_Context_Ptr context);
+ size_t _Unwind_GetIP(_Unwind_Context_Ptr context);
ptrdiff_t _Unwind_SetIP(_Unwind_Context_Ptr context, ptrdiff_t new_value);
ptrdiff_t _Unwind_SetGR(_Unwind_Context_Ptr context, int index,
ptrdiff_t new_value);
- ptrdiff_t _Unwind_GetRegionStart(_Unwind_Context_Ptr context);
+ size_t _Unwind_GetRegionStart(_Unwind_Context_Ptr context);
+
+ size_t _Unwind_GetTextRelBase(_Unwind_Context_Ptr);
+ size_t _Unwind_GetDataRelBase(_Unwind_Context_Ptr);
}
else
{
// runtime calls these directly
void _Unwind_Resume(_Unwind_Exception*)
{
- console("_Unwind_Resume is not implemented on this platform.\n");
+ printf("_Unwind_Resume is not implemented on this platform.\n");
}
_Unwind_Reason_Code _Unwind_RaiseException(_Unwind_Exception*)
{
- console("_Unwind_RaiseException is not implemented on this platform.\n");
+ printf("_Unwind_RaiseException is not implemented on this platform.\n");
return _Unwind_Reason_Code.FATAL_PHASE1_ERROR;
}
}
@@ -122,14 +124,161 @@
}
// error and exit
-extern(C) private void fatalerror(char[] format)
+extern(C) private void fatalerror(char* format, ...)
{
- printf("Fatal error in EH code: %.*s\n", format.length, format.ptr);
+// va_list args;
+// va_start(args, format);
+ printf("Fatal error in EH code: ");
+// vprintf(format, args);
+ printf("\n");
abort();
}
-// helpers for reading certain DWARF data
+// DWARF EH encoding enum
+// See e.g. http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/dwarfext.html
+private enum : ubyte {
+ DW_EH_PE_omit = 0xff, // value is not present
+
+ // value format
+ DW_EH_PE_absptr = 0x00, // literal pointer
+ DW_EH_PE_uleb128 = 0x01,
+ DW_EH_PE_udata2 = 0x02, // unsigned 2-byte
+ DW_EH_PE_udata4 = 0x03,
+ DW_EH_PE_udata8 = 0x04,
+ DW_EH_PE_sleb128 = 0x09,
+ DW_EH_PE_sdata2 = 0x0a,
+ DW_EH_PE_sdata4 = 0x0b,
+ DW_EH_PE_sdata8 = 0x0c,
+
+ // value meaning
+ DW_EH_PE_pcrel = 0x10, // relative to program counter
+ DW_EH_PE_textrel = 0x20, // relative to .text
+ DW_EH_PE_datarel = 0x30, // relative to .got or .eh_frame_hdr
+ DW_EH_PE_funcrel = 0x40, // relative to beginning of function
+ DW_EH_PE_aligned = 0x50, // is an aligned void*
+
+ // value is a pointer to the actual value
+ // this is a mask on top of one of the above
+ DW_EH_PE_indirect = 0x80
+}
+
+// Helpers for reading DWARF data
+
+// Given an encoding and a context, return the base to which the encoding is
+// relative
+private size_t base_of_encoded(_Unwind_Context_Ptr context, ubyte encoding)
+{
+ if (encoding == DW_EH_PE_omit)
+ return 0;
+
+ switch (encoding & 0x70) // ignore DW_EH_PE_indirect
+ {
+ case DW_EH_PE_absptr, DW_EH_PE_pcrel, DW_EH_PE_aligned:
+ return 0;
+
+ case DW_EH_PE_textrel: return _Unwind_GetTextRelBase(context);
+ case DW_EH_PE_datarel: return _Unwind_GetDataRelBase(context);
+ case DW_EH_PE_funcrel: return _Unwind_GetRegionStart(context);
+
+ default: fatalerror("Unrecognized base for DWARF value");
+ }
+}
+
+// Only defined for fixed-size encodings
+private size_t size_of_encoded(ubyte encoding)
+{
+ if (encoding == DW_EH_PE_omit)
+ return 0;
+
+ switch (encoding & 0x07) // ignore leb128
+ {
+ case DW_EH_PE_absptr: return (void*).sizeof;
+ case DW_EH_PE_udata2: return 2;
+ case DW_EH_PE_udata4: return 4;
+ case DW_EH_PE_udata8: return 8;
+
+ default: fatalerror("Unrecognized fixed-size DWARF value encoding");
+ }
+}
+
+// Actual value readers below: read a value from the given ubyte* into the
+// output parameter and return the pointer incremented past the value.
+
+// Like read_encoded_with_base but gets the base from the given context
+private ubyte* read_encoded(_Unwind_Context_Ptr context, ubyte encoding, ubyte* p, out size_t val)
+{
+ return read_encoded_with_base(encoding, base_of_encoded(context, encoding), p, val);
+}
+
+private ubyte* read_encoded_with_base(ubyte encoding, size_t base, ubyte* p, out size_t val)
+{
+ if (encoding == DW_EH_PE_aligned)
+ {
+ auto a = cast(size_t)p;
+ a = (a + (void*).sizeof - 1) & -(void*).sizeof;
+ val = *cast(size_t*)a;
+ return cast(ubyte*)(a + (void*).sizeof);
+ }
+
+ union U
+ {
+ size_t ptr;
+ ushort udata2;
+ uint udata4;
+ ulong udata8;
+ short sdata2;
+ int sdata4;
+ long sdata8;
+ }
+
+ auto u = cast(U*)p;
+
+ size_t result;
+
+ switch (encoding & 0x0f)
+ {
+ case DW_EH_PE_absptr:
+ result = u.ptr;
+ p += (void*).sizeof;
+ break;
+
+ case DW_EH_PE_uleb128:
+ {
+ p = get_uleb128(p, result);
+ break;
+ }
+ case DW_EH_PE_sleb128:
+ {
+ ptrdiff_t sleb128;
+ p = get_sleb128(p, sleb128);
+ result = cast(size_t)sleb128;
+ break;
+ }
+
+ case DW_EH_PE_udata2: result = cast(size_t)u.udata2; p += 2; break;
+ case DW_EH_PE_udata4: result = cast(size_t)u.udata4; p += 4; break;
+ case DW_EH_PE_udata8: result = cast(size_t)u.udata8; p += 8; break;
+ case DW_EH_PE_sdata2: result = cast(size_t)u.sdata2; p += 2; break;
+ case DW_EH_PE_sdata4: result = cast(size_t)u.sdata4; p += 4; break;
+ case DW_EH_PE_sdata8: result = cast(size_t)u.sdata8; p += 8; break;
+
+ default: fatalerror("Unrecognized DWARF value encoding format");
+ }
+ if (result)
+ {
+ if ((encoding & 0x70) == DW_EH_PE_pcrel)
+ result += cast(size_t)u;
+ else
+ result += base;
+
+ if (encoding & DW_EH_PE_indirect)
+ result = *cast(size_t*)result;
+ }
+ val = result;
+ return p;
+}
+
private ubyte* get_uleb128(ubyte* addr, ref size_t res)
{
res = 0;
@@ -137,7 +286,7 @@
// read as long as high bit is set
while(*addr & 0x80) {
- res |= (*addr & 0x7f) << bitsize;
+ res |= (*addr & 0x7fU) << bitsize;
bitsize += 7;
addr += 1;
if(bitsize >= size_t.sizeof*8)
@@ -153,12 +302,12 @@
private ubyte* get_sleb128(ubyte* addr, ref ptrdiff_t res)
{
- res = 0;
+ size_t tres = 0;
size_t bitsize = 0;
// read as long as high bit is set
while(*addr & 0x80) {
- res |= (*addr & 0x7f) << bitsize;
+ tres |= (*addr & 0x7fU) << bitsize;
bitsize += 7;
addr += 1;
if(bitsize >= size_t.sizeof*8)
@@ -167,12 +316,14 @@
// read last
if(bitsize != 0 && *addr >= 1 << size_t.sizeof*8 - bitsize)
fatalerror("tried to read sleb128 that exceeded size of size_t");
- res |= (*addr) << bitsize;
+ tres |= (*addr) << bitsize;
// take care of sign
- if(bitsize < size_t.sizeof*8 && ((*addr) & 0x40))
- res |= cast(ptrdiff_t)(-1) ^ ((1 << (bitsize+7)) - 1);
+ if(bitsize < size_t.sizeof*8 && (*addr & 0x40U) != 0)
+ tres |= cast(size_t)(-1) ^ ((1 << (bitsize+7)) - 1);
+ res = cast(ptrdiff_t)tres;
+
return addr + 1;
}
@@ -190,8 +341,7 @@
// the 8-byte string identifying the type of exception
// the first 4 are for vendor, the second 4 for language
-//TODO: This may be the wrong way around
-const char[8] _d_exception_class = "LLDCD1\0\0";
+const char[8] _d_exception_class = "LDC_D_10";
//
@@ -201,118 +351,174 @@
version(X86_UNWIND)
{
+// Various stuff we need
+struct Region
+{
+ ubyte* callsite_table;
+ ubyte* action_table;
+
+ // Note: classinfo_table points past the end of the table
+ ubyte* classinfo_table;
+
+ size_t start;
+ size_t lpStart_base; // landing pad base
+
+ ubyte ttypeEnc;
+ size_t ttype_base; // typeinfo base
+
+ ubyte callSiteEnc;
+}
+
// the personality routine gets called by the unwind handler and is responsible for
// reading the EH tables and deciding what to do
extern(C) _Unwind_Reason_Code _d_eh_personality(int ver, _Unwind_Action actions, ulong exception_class, _Unwind_Exception* exception_info, _Unwind_Context_Ptr context)
{
+ debug(EH_personality) printf("Entering personality routine, context=%p\n", context);
// check ver: the C++ Itanium ABI only allows ver == 1
if(ver != 1)
+ {
+ debug(EH_personality) printf("eh version mismatch\n");
return _Unwind_Reason_Code.FATAL_PHASE1_ERROR;
+ }
// check exceptionClass
//TODO: Treat foreign exceptions with more respect
- if((cast(char*)&exception_class)[0..8] != _d_exception_class)
+ auto wanted_ec = *cast(ulong*)_d_exception_class.ptr;
+ if(exception_class != wanted_ec)
+ {
+ debug(EH_personality) printf("exception class mismatch %p vs %p\n", exception_class, wanted_ec);
return _Unwind_Reason_Code.FATAL_PHASE1_ERROR;
+ }
// find call site table, action table and classinfo table
// Note: callsite and action tables do not contain static-length
// data and will be parsed as needed
- // Note: classinfo_table points past the end of the table
- ubyte* callsite_table;
- ubyte* action_table;
- ClassInfo* classinfo_table;
- _d_getLanguageSpecificTables(context, callsite_table, action_table, classinfo_table);
- if (!callsite_table)
+
+ Region region;
+
+ _d_getLanguageSpecificTables(context, region);
+
+ // workaround. this should not happen
+ if (!region.callsite_table)
+ {
+ debug(EH_personality) printf("callsite_table is null\n");
return _Unwind_Reason_Code.CONTINUE_UNWIND;
+ }
+ debug(EH_personality) printf("yay, checking\n");
+ debug(EH_personality) printf("region.start = %p\n", region.start);
+
/*
find landing pad and action table index belonging to ip by walking
the callsite_table
*/
- ubyte* callsite_walker = callsite_table;
+ ubyte* callsite_walker = region.callsite_table;
+ debug(EH_personality) printf("callsite table at: %p\n", region.callsite_table);
+ debug(EH_personality) printf("action table at: %p\n", region.action_table);
+ debug(EH_personality) printf("rtti table at %p\n", region.classinfo_table);
// get the instruction pointer
// will be used to find the right entry in the callsite_table
// -1 because it will point past the last instruction
- ptrdiff_t ip = _Unwind_GetIP(context) - 1;
+ debug(EH_personality) printf("check1\n");
+ size_t ip = _Unwind_GetIP(context) - 1;
+ debug(EH_personality) printf("check2\n");
- // address block_start is relative to
- ptrdiff_t region_start = _Unwind_GetRegionStart(context);
-
// table entries
- uint block_start_offset, block_size;
- ptrdiff_t landing_pad;
+ size_t landing_pad;
size_t action_offset;
while(true) {
// if we've gone through the list and found nothing...
- if(callsite_walker >= action_table)
+ if(callsite_walker >= region.action_table)
+ {
+ debug(EH_personality) printf("found nothing\n");
return _Unwind_Reason_Code.CONTINUE_UNWIND;
+ }
- block_start_offset = *cast(uint*)callsite_walker;
- block_size = *(cast(uint*)callsite_walker + 1);
- landing_pad = *(cast(uint*)callsite_walker + 2);
- if(landing_pad)
- landing_pad += region_start;
- callsite_walker = get_uleb128(callsite_walker + 3*uint.sizeof, action_offset);
+ size_t block_start, block_size;
- debug(EH_personality_verbose) printf("ip=%llx %d %d %llx\n", ip, block_start_offset, block_size, landing_pad);
+ callsite_walker = read_encoded(null, region.callSiteEnc, callsite_walker, block_start);
+ callsite_walker = read_encoded(null, region.callSiteEnc, callsite_walker, block_size);
+ callsite_walker = read_encoded(null, region.callSiteEnc, callsite_walker, landing_pad);
+ callsite_walker = get_uleb128(callsite_walker, action_offset);
+ debug(EH_personality) printf("*block start offset = %p\n", block_start);
+ debug(EH_personality) printf(" block size = %p\n", block_size);
+ debug(EH_personality) printf(" landing pad = %p\n", landing_pad);
+ debug(EH_personality) printf(" ip=%p %p %p %p\n", ip, block_start, block_size, landing_pad);
+
// since the list is sorted, as soon as we're past the ip
// there's no handler to be found
- if(ip < region_start + block_start_offset)
+ if(ip < region.start + block_start)
+ {
+ debug(EH_personality) printf("found nothing2\n");
return _Unwind_Reason_Code.CONTINUE_UNWIND;
+ }
+ if(landing_pad)
+ landing_pad += region.lpStart_base;
+
// if we've found our block, exit
- if(ip < region_start + block_start_offset + block_size)
+ if(ip < region.start + block_start + block_size)
break;
}
- debug(EH_personality) printf("Found correct landing pad and actionOffset %d\n", action_offset);
+ debug(EH_personality) printf("Found correct landing pad %p and actionOffset %p\n", landing_pad, action_offset);
// now we need the exception's classinfo to find a handler
// the exception_info is actually a member of a larger _d_exception struct
// the runtime allocated. get that now
- _d_exception* exception_struct = cast(_d_exception*)(cast(ubyte*)exception_info - _d_exception.unwind_info.offsetof);
+ _d_exception* exception_struct = cast(_d_exception*)(cast(ubyte*)exception_info - size_t.sizeof); //_d_exception.unwind_info.offsetof);
// if there's no action offset and no landing pad, continue unwinding
if(!action_offset && !landing_pad)
return _Unwind_Reason_Code.CONTINUE_UNWIND;
// if there's no action offset but a landing pad, this is a cleanup handler
- else if(!action_offset && landing_pad)
- return _d_eh_install_finally_context(actions, landing_pad, exception_struct, context);
+ else if(!action_offset && landing_pad != 0)
+ {
+ debug(EH_personality) printf("installing finally context\n");
+ return _d_eh_install_finally_context(actions, cast(ptrdiff_t)landing_pad, exception_struct, context);
+ }
/*
walk action table chain, comparing classinfos using _d_isbaseof
*/
- ubyte* action_walker = action_table + action_offset - 1;
+ ubyte* action_walker = region.action_table + action_offset - 1;
- ptrdiff_t ti_offset, next_action_offset;
while(true) {
+ ptrdiff_t ti_offset, next_action_offset;
+
action_walker = get_sleb128(action_walker, ti_offset);
// it is intentional that we not modify action_walker here
// next_action_offset is from current action_walker position
get_sleb128(action_walker, next_action_offset);
// negative are 'filters' which we don't use
- if(!(ti_offset >= 0))
+ if(ti_offset < 0)
fatalerror("Filter actions are unsupported");
// zero means cleanup, which we require to be the last action
if(ti_offset == 0) {
- if(!(next_action_offset == 0))
+ if(next_action_offset != 0)
fatalerror("Cleanup action must be last in chain");
- return _d_eh_install_finally_context(actions, landing_pad, exception_struct, context);
+ return _d_eh_install_finally_context(actions, cast(ptrdiff_t)landing_pad, exception_struct, context);
}
// get classinfo for action and check if the one in the
// exception structure is a base
- ClassInfo catch_ci = *(classinfo_table - ti_offset);
- debug(EH_personality) printf("Comparing catch %s to exception %s\n", catch_ci.name.ptr, exception_struct.exception_object.classinfo.name.ptr);
- if(_d_isbaseof(exception_struct.exception_object.classinfo, catch_ci))
- return _d_eh_install_catch_context(actions, ti_offset, landing_pad, exception_struct, context);
+ size_t typeinfo;
+ auto filter = cast(size_t)ti_offset * size_of_encoded(region.ttypeEnc);
+ read_encoded_with_base(region.ttypeEnc, region.ttype_base, region.classinfo_table - filter, typeinfo);
+ debug(EH_personality)
+ printf("classinfo at %zx (enc %zx (size %zx) base %zx ptr %zx)\n", typeinfo, region.ttypeEnc, size_of_encoded(region.ttypeEnc), region.ttype_base, region.classinfo_table - filter);
+
+ auto catch_ci = *cast(ClassInfo*)&typeinfo;
+ if(_d_dynamic_cast(exception_struct.exception_object, catch_ci) !is null)
+ return _d_eh_install_catch_context(actions, ti_offset, cast(ptrdiff_t)landing_pad, exception_struct, context);
+
// we've walked through all actions and found nothing...
if(next_action_offset == 0)
return _Unwind_Reason_Code.CONTINUE_UNWIND;
@@ -356,6 +562,7 @@
}
fatalerror("reached unreachable");
+
return _Unwind_Reason_Code.FATAL_PHASE2_ERROR;
}
@@ -370,56 +577,78 @@
_Unwind_SetGR(context, eh_exception_regno, cast(ptrdiff_t)exception_struct);
_Unwind_SetGR(context, eh_selector_regno, 0);
_Unwind_SetIP(context, landing_pad);
+
return _Unwind_Reason_Code.INSTALL_CONTEXT;
}
-private void _d_getLanguageSpecificTables(_Unwind_Context_Ptr context, ref ubyte* callsite, ref ubyte* action, ref ClassInfo* ci)
+private void _d_getLanguageSpecificTables(_Unwind_Context_Ptr context, out Region region)
{
- ubyte* data = cast(ubyte*)_Unwind_GetLanguageSpecificData(context);
+ auto data = cast(ubyte*)_Unwind_GetLanguageSpecificData(context);
+
+ // workaround. this should not be 0...
if (!data)
{
- callsite = null;
- action = null;
- ci = null;
- return;
+ //printf("language specific data is null\n");
+ return;
}
- //TODO: Do proper DWARF reading here
- if(*data++ != 0xff)
- fatalerror("DWARF header has unexpected format 1");
+ region.start = _Unwind_GetRegionStart(context);
- if(*data++ != 0x00)
- fatalerror("DWARF header has unexpected format 2");
- size_t cioffset;
- data = get_uleb128(data, cioffset);
- ci = cast(ClassInfo*)(data + cioffset);
+ // Read the C++-style LSDA: this is implementation-defined by GCC but LLVM
+ // outputs the same kind of table
- if(*data++ != 0x03)
- fatalerror("DWARF header has unexpected format 3");
- size_t callsitelength;
- data = get_uleb128(data, callsitelength);
- action = data + callsitelength;
+ // Get @LPStart: landing pad offsets are relative to it
+ auto lpStartEnc = *data++;
+ if (lpStartEnc == DW_EH_PE_omit)
+ region.lpStart_base = region.start;
+ else
+ data = read_encoded(context, lpStartEnc, data, region.lpStart_base);
- callsite = data;
+ // Get @TType: the offset to the handler and typeinfo
+ region.ttypeEnc = *data++;
+ if (region.ttypeEnc == DW_EH_PE_omit)
+ // Not sure about this one...
+ fatalerror("@TType must not be omitted from DWARF header");
+
+ size_t ciOffset;
+ data = get_uleb128(data, ciOffset);
+ region.classinfo_table = data + ciOffset;
+
+ region.ttype_base = base_of_encoded(context, region.ttypeEnc);
+
+ // Get encoding and length of the call site table, which precedes the action
+ // table.
+ region.callSiteEnc = *data++;
+ if (region.callSiteEnc == DW_EH_PE_omit)
+ fatalerror("Call site table encoding must not be omitted from DWARF header");
+
+ size_t callSiteLength;
+ region.callsite_table = get_uleb128(data, callSiteLength);
+ region.action_table = region.callsite_table + callSiteLength;
}
} // end of x86 Linux specific implementation
-
-extern(C) void _d_throw_exception(Object e)
+// called to throw object
+extern(C)
+void _d_throw_exception(Object e)
{
+ //printf("throwing %p, rtti = %p\n", e, **cast(ClassRTTI***)e);
if (e !is null)
{
_d_exception* exc_struct = new _d_exception;
exc_struct.unwind_info.exception_class = *cast(ulong*)_d_exception_class.ptr;
exc_struct.exception_object = e;
_Unwind_Reason_Code ret = _Unwind_RaiseException(&exc_struct.unwind_info);
- console("_Unwind_RaiseException failed with reason code: ")(ret)("\n");
+ printf("Error: returned %d from raise exception.\n", ret);
+ //console("_Unwind_RaiseException failed with reason code: ")(ret)("\n");
}
abort();
}
-extern(C) void _d_eh_resume_unwind(_d_exception* exception_struct)
+// called to resume unwinding
+extern(C)
+void _d_eh_resume_unwind(void* exception_struct)
{
- _Unwind_Resume(&exception_struct.unwind_info);
+ _Unwind_Resume(&(cast(_d_exception*)exception_struct).unwind_info);
}