Skip to content

Commit dcfc61c

Browse files
committed
Merge pull request #386 from klickverbot/store-bool-as-i8
Store bool as i8.
2 parents 001a396 + 848dee3 commit dcfc61c

29 files changed

+168
-116
lines changed

dmd2/expression.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ struct elem;
7272

7373
#if IN_LLVM
7474
struct IRState;
75-
struct DValue;
75+
class DValue;
7676
namespace llvm {
7777
class Constant;
7878
class ConstantInt;

dmd2/irstate.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct Symbol;
2121
struct FuncDeclaration;
2222
struct Blockx;
2323
#if IN_LLVM
24-
struct DValue;
24+
class DValue;
2525
typedef DValue elem;
2626
#else
2727
struct elem;

dmd2/module.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Library;
2929
// Back end
3030
#if IN_LLVM
3131
class Ir;
32-
struct DValue;
32+
class DValue;
3333
typedef DValue elem;
3434
namespace llvm {
3535
class LLVMContext;

dmd2/statement.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace llvm
7575
struct IRState;
7676
struct Blockx;
7777
#if IN_LLVM
78-
struct DValue;
78+
class DValue;
7979
typedef DValue elem;
8080
#endif
8181

gen/aa.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ DValue* DtoAAIndex(Loc& loc, Type* type, DValue* aa, DValue* key, bool lvalue)
7474
}
7575

7676
// cast return value
77-
LLType* targettype = getPtrToType(DtoType(type));
77+
LLType* targettype = getPtrToType(i1ToI8(DtoType(type)));
7878
if (ret->getType() != targettype)
7979
ret = DtoBitCast(ret, targettype);
8080

gen/aa.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "lexer.h"
1818

1919
enum TOK;
20-
struct DValue;
20+
class DValue;
2121
struct Loc;
2222
struct Type;
2323
namespace llvm { class Value; }

gen/abi.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
struct Type;
2929
struct TypeFunction;
3030
struct IrFuncTyArg;
31-
struct DValue;
31+
class DValue;
3232

3333
namespace llvm
3434
{

gen/arrays.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ static LLValue *DtoSlicePtr(DValue *dval)
6464
LLStructType* DtoArrayType(Type* arrayTy)
6565
{
6666
assert(arrayTy->nextOf());
67-
LLType* elemty = DtoType(arrayTy->nextOf());
68-
if (elemty == LLType::getVoidTy(gIR->context()))
69-
elemty = LLType::getInt8Ty(gIR->context());
67+
LLType* elemty = i1ToI8(voidToI8(DtoType(arrayTy->nextOf())));
7068

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

90-
LLType* elemty = DtoType(tnext);
91-
if (elemty == LLType::getVoidTy(gIR->context()))
92-
elemty = LLType::getInt8Ty(gIR->context());
93-
88+
LLType* elemty = i1ToI8(voidToI8(DtoType(tnext)));
9489
return LLArrayType::get(elemty, tsa->dim->toUInteger());
9590
}
9691

@@ -291,7 +286,7 @@ LLConstant* DtoConstArrayInitializer(ArrayInitializer* arrinit)
291286

292287
// get elem type
293288
Type* elemty = arrty->nextOf();
294-
LLType* llelemty = DtoTypeNotVoid(elemty);
289+
LLType* llelemty = i1ToI8(voidToI8(DtoType(elemty)));
295290

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

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

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

941936
LLType* ptrty = DtoArrayType(totype)->getContainedType(1);
942-
LLType* ety = DtoTypeNotVoid(fromtype->nextOf());
937+
LLType* ety = voidToI8(DtoType(fromtype->nextOf()));
943938

944939
if (fromtype->ty == Tsarray) {
945940
LLValue* uval = u->getRVal();

gen/arrays.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include "gen/llvm.h"
1919

2020
struct ArrayInitializer;
21-
struct DSliceValue;
22-
struct DValue;
21+
class DSliceValue;
22+
class DValue;
2323
struct Expression;
2424
struct Loc;
2525
struct Type;

gen/classes.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ LLValue* DtoIndexClass(LLValue* src, ClassDeclaration* cd, VarDeclaration* vd)
497497
}
498498

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

502502
if (Logger::enabled())
503503
Logger::cout() << "value: " << *val << '\n';

gen/complex.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "lexer.h"
1818
#include "longdouble.h"
1919

20-
struct DValue;
20+
class DValue;
2121
struct Loc;
2222
struct Type;
2323
namespace llvm

gen/declarations.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void VarDeclaration::codegen(Ir* p)
172172
// this->ir.irGlobal->value!), and in case we also do an initializer
173173
// with a different type later, swap it out and replace any existing
174174
// uses with bitcasts to the previous type.
175-
llvm::GlobalVariable* gvar = createGlobal(DtoType(type), isLLConst,
175+
llvm::GlobalVariable* gvar = createGlobal(i1ToI8(DtoType(type)), isLLConst,
176176
llLinkage, llName, isThreadlocal());
177177
this->ir.irGlobal->value = gvar;
178178

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

209-
// Also set up the debug info.
209+
// Also set up the edbug info.
210210
DtoDwarfGlobalVariable(gvar, this);
211211
}
212212

gen/dvalue.cpp

+37-10
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,36 @@
1818
/////////////////////////////////////////////////////////////////////////////////////////////////
1919
/////////////////////////////////////////////////////////////////////////////////////////////////
2020

21+
static bool checkVarValueType(LLType* t, bool extraDeref)
22+
{
23+
if (extraDeref)
24+
{
25+
llvm::PointerType* pt = llvm::dyn_cast<llvm::PointerType>(t);
26+
if (!pt) return false;
27+
28+
t = pt->getElementType();
29+
}
30+
31+
llvm::PointerType* pt = llvm::dyn_cast<llvm::PointerType>(t);
32+
if (!pt) return false;
33+
34+
// bools should not be stored as i1 any longer.
35+
if (pt->getElementType() == llvm::Type::getInt1Ty(gIR->context()))
36+
return false;
37+
38+
return true;
39+
}
40+
2141
DVarValue::DVarValue(Type* t, VarDeclaration* vd, LLValue* llvmValue)
2242
: DValue(t), var(vd), val(llvmValue)
2343
{
24-
assert(isaPointer(llvmValue));
25-
assert(!isSpecialRefVar(vd) ||
26-
isaPointer(isaPointer(llvmValue)->getElementType()));
44+
assert(checkVarValueType(llvmValue->getType(), isSpecialRefVar(vd)));
2745
}
2846

2947
DVarValue::DVarValue(Type* t, LLValue* llvmValue)
3048
: DValue(t), var(0), val(llvmValue)
3149
{
32-
assert(isaPointer(llvmValue));
50+
assert(checkVarValueType(llvmValue->getType(), false));
3351
}
3452

3553
LLValue* DVarValue::getLVal()
@@ -43,15 +61,24 @@ LLValue* DVarValue::getLVal()
4361
LLValue* DVarValue::getRVal()
4462
{
4563
assert(val);
46-
Type* bt = type->toBasetype();
4764

48-
LLValue* tmp = val;
65+
llvm::Value* storage = val;
4966
if (var && isSpecialRefVar(var))
50-
tmp = DtoLoad(tmp);
67+
storage = DtoLoad(storage);
68+
69+
if (DtoIsPassedByRef(type->toBasetype()))
70+
return storage;
71+
72+
llvm::Value* rawValue = DtoLoad(storage);
73+
74+
if (type->toBasetype()->ty == Tbool)
75+
{
76+
assert(rawValue->getType() == llvm::Type::getInt8Ty(gIR->context()));
77+
return gIR->ir->CreateTrunc(rawValue,
78+
llvm::Type::getInt1Ty(gIR->context()));
79+
}
5180

52-
if (DtoIsPassedByRef(bt))
53-
return tmp;
54-
return DtoLoad(tmp);
81+
return rawValue;
5582
}
5683

5784
LLValue* DVarValue::getRefStorage()

gen/dvalue.h

+41-29
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,21 @@ namespace llvm
3232
class Constant;
3333
}
3434

35-
struct DImValue;
36-
struct DConstValue;
37-
struct DNullValue;
38-
struct DVarValue;
39-
struct DFieldValue;
40-
struct DFuncValue;
41-
struct DSliceValue;
35+
class DImValue;
36+
class DConstValue;
37+
class DNullValue;
38+
class DVarValue;
39+
class DFieldValue;
40+
class DFuncValue;
41+
class DSliceValue;
4242

4343
// base class for d-values
44-
struct DValue : Object
44+
class DValue
4545
{
46+
public:
4647
Type* type;
4748
DValue(Type* ty) : type(ty) {}
49+
virtual ~DValue() {}
4850

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

@@ -60,49 +62,52 @@ struct DValue : Object
6062
virtual DFieldValue* isField() { return NULL; }
6163
virtual DSliceValue* isSlice() { return NULL; }
6264
virtual DFuncValue* isFunc() { return NULL; }
65+
6366
protected:
6467
DValue() {}
6568
DValue(const DValue&) { }
6669
DValue& operator=(const DValue& other) { type = other.type; return *this; }
6770
};
6871

6972
// immediate d-value
70-
struct DImValue : DValue
73+
class DImValue : public DValue
7174
{
72-
llvm::Value* val;
73-
75+
public:
7476
DImValue(Type* t, llvm::Value* v) : DValue(t), val(v) { }
7577

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

7880
virtual DImValue* isIm() { return this; }
81+
82+
protected:
83+
llvm::Value* val;
7984
};
8085

8186
// constant d-value
82-
struct DConstValue : DValue
87+
class DConstValue : public DValue
8388
{
84-
llvm::Constant* c;
85-
89+
public:
8690
DConstValue(Type* t, llvm::Constant* con) : DValue(t), c(con) {}
8791

8892
virtual llvm::Value* getRVal();
8993

9094
virtual DConstValue* isConst() { return this; }
95+
96+
llvm::Constant* c;
9197
};
9298

9399
// null d-value
94-
struct DNullValue : DConstValue
100+
class DNullValue : public DConstValue
95101
{
102+
public:
96103
DNullValue(Type* t, llvm::Constant* con) : DConstValue(t,con) {}
97104
virtual DNullValue* isNull() { return this; }
98105
};
99106

100107
// variable d-value
101-
struct DVarValue : DValue
108+
class DVarValue : public DValue
102109
{
103-
VarDeclaration* var;
104-
llvm::Value* val;
105-
110+
public:
106111
DVarValue(Type* t, VarDeclaration* vd, llvm::Value* llvmValue);
107112
DVarValue(Type* t, llvm::Value* llvmValue);
108113

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

117122
virtual DVarValue* isVar() { return this; }
123+
124+
VarDeclaration* var;
125+
protected:
126+
llvm::Value* val;
118127
};
119128

120129
// field d-value
121-
struct DFieldValue : DVarValue
130+
class DFieldValue : public DVarValue
122131
{
132+
public:
123133
DFieldValue(Type* t, llvm::Value* llvmValue) : DVarValue(t, llvmValue) {}
124134
virtual DFieldValue* isField() { return this; }
125135
};
126136

127137
// slice d-value
128-
struct DSliceValue : DValue
138+
class DSliceValue : public DValue
129139
{
130-
llvm::Value* len;
131-
llvm::Value* ptr;
132-
140+
public:
133141
DSliceValue(Type* t, llvm::Value* l, llvm::Value* p) : DValue(t), len(l), ptr(p) {}
134142

135143
virtual llvm::Value* getRVal();
136144

137145
virtual DSliceValue* isSlice() { return this; }
146+
147+
llvm::Value* len;
148+
llvm::Value* ptr;
138149
};
139150

140151
// function d-value
141-
struct DFuncValue : DValue
152+
class DFuncValue : public DValue
142153
{
143-
FuncDeclaration* func;
144-
llvm::Value* val;
145-
llvm::Value* vthis;
146-
154+
public:
147155
DFuncValue(FuncDeclaration* fd, llvm::Value* v, llvm::Value* vt = 0);
148156

149157
virtual llvm::Value* getRVal();
150158

151159
virtual DFuncValue* isFunc() { return this; }
160+
161+
FuncDeclaration* func;
162+
llvm::Value* val;
163+
llvm::Value* vthis;
152164
};
153165

154166
#endif // LDC_GEN_DVALUE_H

gen/functions.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ void DtoDefineFunction(FuncDeclaration* fd)
10591059
if (lazy)
10601060
argt = irparam->value->getType();
10611061
else
1062-
argt = DtoType(vd->type);
1062+
argt = i1ToI8(DtoType(vd->type));
10631063
LLValue* mem = DtoRawAlloca(argt, 0, vd->ident->toChars());
10641064

10651065
// let the abi transform the argument back first

0 commit comments

Comments
 (0)