Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store bool as i8 #386

Merged
merged 6 commits into from
Jun 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dmd2/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct elem;

#if IN_LLVM
struct IRState;
struct DValue;
class DValue;
namespace llvm {
class Constant;
class ConstantInt;
Expand Down
2 changes: 1 addition & 1 deletion dmd2/irstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct Symbol;
struct FuncDeclaration;
struct Blockx;
#if IN_LLVM
struct DValue;
class DValue;
typedef DValue elem;
#else
struct elem;
Expand Down
2 changes: 1 addition & 1 deletion dmd2/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Library;
// Back end
#if IN_LLVM
class Ir;
struct DValue;
class DValue;
typedef DValue elem;
namespace llvm {
class LLVMContext;
Expand Down
2 changes: 1 addition & 1 deletion dmd2/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace llvm
struct IRState;
struct Blockx;
#if IN_LLVM
struct DValue;
class DValue;
typedef DValue elem;
#endif

Expand Down
2 changes: 1 addition & 1 deletion gen/aa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ DValue* DtoAAIndex(Loc& loc, Type* type, DValue* aa, DValue* key, bool lvalue)
}

// cast return value
LLType* targettype = getPtrToType(DtoType(type));
LLType* targettype = getPtrToType(i1ToI8(DtoType(type)));
if (ret->getType() != targettype)
ret = DtoBitCast(ret, targettype);

Expand Down
2 changes: 1 addition & 1 deletion gen/aa.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "lexer.h"

enum TOK;
struct DValue;
class DValue;
struct Loc;
struct Type;
namespace llvm { class Value; }
Expand Down
2 changes: 1 addition & 1 deletion gen/abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
struct Type;
struct TypeFunction;
struct IrFuncTyArg;
struct DValue;
class DValue;

namespace llvm
{
Expand Down
15 changes: 5 additions & 10 deletions gen/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ static LLValue *DtoSlicePtr(DValue *dval)
LLStructType* DtoArrayType(Type* arrayTy)
{
assert(arrayTy->nextOf());
LLType* elemty = DtoType(arrayTy->nextOf());
if (elemty == LLType::getVoidTy(gIR->context()))
elemty = LLType::getInt8Ty(gIR->context());
LLType* elemty = i1ToI8(voidToI8(DtoType(arrayTy->nextOf())));

llvm::Type *elems[] = { DtoSize_t(), getPtrToType(elemty) };
return llvm::StructType::get(gIR->context(), elems, false);
Expand All @@ -87,10 +85,7 @@ LLArrayType* DtoStaticArrayType(Type* t)
TypeSArray* tsa = static_cast<TypeSArray*>(t);
Type* tnext = tsa->nextOf();

LLType* elemty = DtoType(tnext);
if (elemty == LLType::getVoidTy(gIR->context()))
elemty = LLType::getInt8Ty(gIR->context());

LLType* elemty = i1ToI8(voidToI8(DtoType(tnext)));
return LLArrayType::get(elemty, tsa->dim->toUInteger());
}

Expand Down Expand Up @@ -291,7 +286,7 @@ LLConstant* DtoConstArrayInitializer(ArrayInitializer* arrinit)

// get elem type
Type* elemty = arrty->nextOf();
LLType* llelemty = DtoTypeNotVoid(elemty);
LLType* llelemty = i1ToI8(voidToI8(DtoType(elemty)));

// true if array elements differ in type, can happen with array of unions
bool mismatch = false;
Expand Down Expand Up @@ -424,7 +419,7 @@ void DtoArrayCopyToSlice(DSliceValue* dst, DValue* src)
LLValue* dstarr = get_slice_ptr(dst,sz1);

LLValue* srcarr = DtoBitCast(DtoArrayPtr(src), getVoidPtrType());
LLType* arrayelemty = DtoTypeNotVoid(src->getType()->nextOf()->toBasetype());
LLType* arrayelemty = voidToI8(DtoType(src->getType()->nextOf()->toBasetype()));
LLValue* sz2 = gIR->ir->CreateMul(DtoConstSize_t(getTypePaddedSize(arrayelemty)), DtoArrayLen(src), "tmp");

copySlice(dstarr, sz1, srcarr, sz2);
Expand Down Expand Up @@ -939,7 +934,7 @@ DValue* DtoCastArray(Loc& loc, DValue* u, Type* to)
Logger::cout() << "to array" << '\n';

LLType* ptrty = DtoArrayType(totype)->getContainedType(1);
LLType* ety = DtoTypeNotVoid(fromtype->nextOf());
LLType* ety = voidToI8(DtoType(fromtype->nextOf()));

if (fromtype->ty == Tsarray) {
LLValue* uval = u->getRVal();
Expand Down
4 changes: 2 additions & 2 deletions gen/arrays.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include "gen/llvm.h"

struct ArrayInitializer;
struct DSliceValue;
struct DValue;
class DSliceValue;
class DValue;
struct Expression;
struct Loc;
struct Type;
Expand Down
2 changes: 1 addition & 1 deletion gen/classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ LLValue* DtoIndexClass(LLValue* src, ClassDeclaration* cd, VarDeclaration* vd)
}

// cast it to the right type
val = DtoBitCast(val, getPtrToType(DtoType(vd->type)));
val = DtoBitCast(val, getPtrToType(i1ToI8(DtoType(vd->type))));

if (Logger::enabled())
Logger::cout() << "value: " << *val << '\n';
Expand Down
2 changes: 1 addition & 1 deletion gen/complex.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "lexer.h"
#include "longdouble.h"

struct DValue;
class DValue;
struct Loc;
struct Type;
namespace llvm
Expand Down
4 changes: 2 additions & 2 deletions gen/declarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void VarDeclaration::codegen(Ir* p)
// this->ir.irGlobal->value!), and in case we also do an initializer
// with a different type later, swap it out and replace any existing
// uses with bitcasts to the previous type.
llvm::GlobalVariable* gvar = createGlobal(DtoType(type), isLLConst,
llvm::GlobalVariable* gvar = createGlobal(i1ToI8(DtoType(type)), isLLConst,
llLinkage, llName, isThreadlocal());
this->ir.irGlobal->value = gvar;

Expand Down Expand Up @@ -206,7 +206,7 @@ void VarDeclaration::codegen(Ir* p)
ir.irGlobal->constInit = initVal;
gvar->setInitializer(initVal);

// Also set up the debug info.
// Also set up the edbug info.
DtoDwarfGlobalVariable(gvar, this);
}

Expand Down
47 changes: 37 additions & 10 deletions gen/dvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,36 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

static bool checkVarValueType(LLType* t, bool extraDeref)
{
if (extraDeref)
{
llvm::PointerType* pt = llvm::dyn_cast<llvm::PointerType>(t);
if (!pt) return false;

t = pt->getElementType();
}

llvm::PointerType* pt = llvm::dyn_cast<llvm::PointerType>(t);
if (!pt) return false;

// bools should not be stored as i1 any longer.
if (pt->getElementType() == llvm::Type::getInt1Ty(gIR->context()))
return false;

return true;
}

DVarValue::DVarValue(Type* t, VarDeclaration* vd, LLValue* llvmValue)
: DValue(t), var(vd), val(llvmValue)
{
assert(isaPointer(llvmValue));
assert(!isSpecialRefVar(vd) ||
isaPointer(isaPointer(llvmValue)->getElementType()));
assert(checkVarValueType(llvmValue->getType(), isSpecialRefVar(vd)));
}

DVarValue::DVarValue(Type* t, LLValue* llvmValue)
: DValue(t), var(0), val(llvmValue)
{
assert(isaPointer(llvmValue));
assert(checkVarValueType(llvmValue->getType(), false));
}

LLValue* DVarValue::getLVal()
Expand All @@ -43,15 +61,24 @@ LLValue* DVarValue::getLVal()
LLValue* DVarValue::getRVal()
{
assert(val);
Type* bt = type->toBasetype();

LLValue* tmp = val;
llvm::Value* storage = val;
if (var && isSpecialRefVar(var))
tmp = DtoLoad(tmp);
storage = DtoLoad(storage);

if (DtoIsPassedByRef(type->toBasetype()))
return storage;

llvm::Value* rawValue = DtoLoad(storage);

if (type->toBasetype()->ty == Tbool)
{
assert(rawValue->getType() == llvm::Type::getInt8Ty(gIR->context()));
return gIR->ir->CreateTrunc(rawValue,
llvm::Type::getInt1Ty(gIR->context()));
}

if (DtoIsPassedByRef(bt))
return tmp;
return DtoLoad(tmp);
return rawValue;
}

LLValue* DVarValue::getRefStorage()
Expand Down
70 changes: 41 additions & 29 deletions gen/dvalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ namespace llvm
class Constant;
}

struct DImValue;
struct DConstValue;
struct DNullValue;
struct DVarValue;
struct DFieldValue;
struct DFuncValue;
struct DSliceValue;
class DImValue;
class DConstValue;
class DNullValue;
class DVarValue;
class DFieldValue;
class DFuncValue;
class DSliceValue;

// base class for d-values
struct DValue : Object
class DValue
{
public:
Type* type;
DValue(Type* ty) : type(ty) {}
virtual ~DValue() {}

Type*& getType() { assert(type); return type; }

Expand All @@ -60,49 +62,52 @@ struct DValue : Object
virtual DFieldValue* isField() { return NULL; }
virtual DSliceValue* isSlice() { return NULL; }
virtual DFuncValue* isFunc() { return NULL; }

protected:
DValue() {}
DValue(const DValue&) { }
DValue& operator=(const DValue& other) { type = other.type; return *this; }
};

// immediate d-value
struct DImValue : DValue
class DImValue : public DValue
{
llvm::Value* val;

public:
DImValue(Type* t, llvm::Value* v) : DValue(t), val(v) { }

virtual llvm::Value* getRVal() { assert(val); return val; }

virtual DImValue* isIm() { return this; }

protected:
llvm::Value* val;
};

// constant d-value
struct DConstValue : DValue
class DConstValue : public DValue
{
llvm::Constant* c;

public:
DConstValue(Type* t, llvm::Constant* con) : DValue(t), c(con) {}

virtual llvm::Value* getRVal();

virtual DConstValue* isConst() { return this; }

llvm::Constant* c;
};

// null d-value
struct DNullValue : DConstValue
class DNullValue : public DConstValue
{
public:
DNullValue(Type* t, llvm::Constant* con) : DConstValue(t,con) {}
virtual DNullValue* isNull() { return this; }
};

// variable d-value
struct DVarValue : DValue
class DVarValue : public DValue
{
VarDeclaration* var;
llvm::Value* val;

public:
DVarValue(Type* t, VarDeclaration* vd, llvm::Value* llvmValue);
DVarValue(Type* t, llvm::Value* llvmValue);

Expand All @@ -115,40 +120,47 @@ struct DVarValue : DValue
virtual llvm::Value* getRefStorage();

virtual DVarValue* isVar() { return this; }

VarDeclaration* var;
protected:
llvm::Value* val;
};

// field d-value
struct DFieldValue : DVarValue
class DFieldValue : public DVarValue
{
public:
DFieldValue(Type* t, llvm::Value* llvmValue) : DVarValue(t, llvmValue) {}
virtual DFieldValue* isField() { return this; }
};

// slice d-value
struct DSliceValue : DValue
class DSliceValue : public DValue
{
llvm::Value* len;
llvm::Value* ptr;

public:
DSliceValue(Type* t, llvm::Value* l, llvm::Value* p) : DValue(t), len(l), ptr(p) {}

virtual llvm::Value* getRVal();

virtual DSliceValue* isSlice() { return this; }

llvm::Value* len;
llvm::Value* ptr;
};

// function d-value
struct DFuncValue : DValue
class DFuncValue : public DValue
{
FuncDeclaration* func;
llvm::Value* val;
llvm::Value* vthis;

public:
DFuncValue(FuncDeclaration* fd, llvm::Value* v, llvm::Value* vt = 0);

virtual llvm::Value* getRVal();

virtual DFuncValue* isFunc() { return this; }

FuncDeclaration* func;
llvm::Value* val;
llvm::Value* vthis;
};

#endif // LDC_GEN_DVALUE_H
2 changes: 1 addition & 1 deletion gen/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ void DtoDefineFunction(FuncDeclaration* fd)
if (lazy)
argt = irparam->value->getType();
else
argt = DtoType(vd->type);
argt = i1ToI8(DtoType(vd->type));
LLValue* mem = DtoRawAlloca(argt, 0, vd->ident->toChars());

// let the abi transform the argument back first
Expand Down
Loading