| 1 |
#include "gen/llvm.h" |
|---|
| 2 |
|
|---|
| 3 |
#include "aggregate.h" |
|---|
| 4 |
#include "declaration.h" |
|---|
| 5 |
#include "enum.h" |
|---|
| 6 |
#include "id.h" |
|---|
| 7 |
#include "mem.h" |
|---|
| 8 |
#include "template.h" |
|---|
| 9 |
|
|---|
| 10 |
#include "gen/irstate.h" |
|---|
| 11 |
#include "gen/tollvm.h" |
|---|
| 12 |
#include "gen/llvmhelpers.h" |
|---|
| 13 |
#include "gen/logger.h" |
|---|
| 14 |
|
|---|
| 15 |
#include "ir/ir.h" |
|---|
| 16 |
#include "ir/irvar.h" |
|---|
| 17 |
#include "ir/irtype.h" |
|---|
| 18 |
#include "ir/irtypestruct.h" |
|---|
| 19 |
|
|---|
| 20 |
/* ================================================================== */ |
|---|
| 21 |
|
|---|
| 22 |
void Dsymbol::codegen(Ir*) |
|---|
| 23 |
{ |
|---|
| 24 |
Logger::println("Ignoring Dsymbol::toObjFile for %s", toChars()); |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
/* ================================================================== */ |
|---|
| 28 |
|
|---|
| 29 |
void Declaration::codegen(Ir*) |
|---|
| 30 |
{ |
|---|
| 31 |
Logger::println("Ignoring Declaration::toObjFile for %s", toChars()); |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
/* ================================================================== */ |
|---|
| 35 |
|
|---|
| 36 |
void InterfaceDeclaration::codegen(Ir*) |
|---|
| 37 |
{ |
|---|
| 38 |
//Logger::println("Ignoring InterfaceDeclaration::toObjFile for %s", toChars()); |
|---|
| 39 |
DtoResolveDsymbol(this); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
/* ================================================================== */ |
|---|
| 43 |
|
|---|
| 44 |
void StructDeclaration::codegen(Ir*) |
|---|
| 45 |
{ |
|---|
| 46 |
DtoResolveDsymbol(this); |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
/* ================================================================== */ |
|---|
| 50 |
|
|---|
| 51 |
void ClassDeclaration::codegen(Ir*) |
|---|
| 52 |
{ |
|---|
| 53 |
DtoResolveDsymbol(this); |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
/* ================================================================== */ |
|---|
| 57 |
|
|---|
| 58 |
void TupleDeclaration::codegen(Ir* p) |
|---|
| 59 |
{ |
|---|
| 60 |
Logger::println("TupleDeclaration::toObjFile(): %s", toChars()); |
|---|
| 61 |
|
|---|
| 62 |
assert(isexp); |
|---|
| 63 |
assert(objects); |
|---|
| 64 |
|
|---|
| 65 |
int n = objects->dim; |
|---|
| 66 |
|
|---|
| 67 |
for (int i=0; i < n; ++i) |
|---|
| 68 |
{ |
|---|
| 69 |
DsymbolExp* exp = (DsymbolExp*)objects->data[i]; |
|---|
| 70 |
assert(exp->op == TOKdsymbol); |
|---|
| 71 |
exp->s->codegen(p); |
|---|
| 72 |
} |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
/* ================================================================== */ |
|---|
| 76 |
|
|---|
| 77 |
// FIXME: this is horrible!!! |
|---|
| 78 |
|
|---|
| 79 |
void VarDeclaration::codegen(Ir* p) |
|---|
| 80 |
{ |
|---|
| 81 |
Logger::print("VarDeclaration::toObjFile(): %s | %s\n", toChars(), type->toChars()); |
|---|
| 82 |
LOG_SCOPE; |
|---|
| 83 |
|
|---|
| 84 |
// just forward aliases |
|---|
| 85 |
if (aliassym) |
|---|
| 86 |
{ |
|---|
| 87 |
Logger::println("alias sym"); |
|---|
| 88 |
toAlias()->codegen(p); |
|---|
| 89 |
return; |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
// output the parent aggregate first |
|---|
| 93 |
if (AggregateDeclaration* ad = isMember()) |
|---|
| 94 |
ad->codegen(p); |
|---|
| 95 |
|
|---|
| 96 |
// global variable |
|---|
| 97 |
#if DMDV2 |
|---|
| 98 |
// taken from dmd2/structs |
|---|
| 99 |
if (isDataseg() || (storage_class & (STCconst | STCimmutable) && init)) |
|---|
| 100 |
#else |
|---|
| 101 |
if (isDataseg()) |
|---|
| 102 |
#endif |
|---|
| 103 |
{ |
|---|
| 104 |
Logger::println("data segment"); |
|---|
| 105 |
|
|---|
| 106 |
#if DMDV2 |
|---|
| 107 |
if (storage_class & STCmanifest) |
|---|
| 108 |
{ |
|---|
| 109 |
assert(0 && "manifest constant being codegened!!!"); |
|---|
| 110 |
} |
|---|
| 111 |
#endif |
|---|
| 112 |
|
|---|
| 113 |
// don't duplicate work |
|---|
| 114 |
if (this->ir.resolved) return; |
|---|
| 115 |
this->ir.resolved = true; |
|---|
| 116 |
this->ir.declared = true; |
|---|
| 117 |
|
|---|
| 118 |
this->ir.irGlobal = new IrGlobal(this); |
|---|
| 119 |
|
|---|
| 120 |
Logger::println("parent: %s (%s)", parent->toChars(), parent->kind()); |
|---|
| 121 |
|
|---|
| 122 |
#if DMDV2 |
|---|
| 123 |
// not sure why this is only needed for d2 |
|---|
| 124 |
bool _isconst = isConst() && init; |
|---|
| 125 |
#else |
|---|
| 126 |
bool _isconst = isConst(); |
|---|
| 127 |
#endif |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
Logger::println("Creating global variable"); |
|---|
| 131 |
|
|---|
| 132 |
const LLType* _type = this->ir.irGlobal->type.get(); |
|---|
| 133 |
llvm::GlobalValue::LinkageTypes _linkage = DtoLinkage(this); |
|---|
| 134 |
std::string _name(mangle()); |
|---|
| 135 |
|
|---|
| 136 |
llvm::GlobalVariable* gvar = new llvm::GlobalVariable(*gIR->module,_type,_isconst,_linkage,NULL,_name); |
|---|
| 137 |
this->ir.irGlobal->value = gvar; |
|---|
| 138 |
|
|---|
| 139 |
// set the alignment |
|---|
| 140 |
gvar->setAlignment(this->type->alignsize()); |
|---|
| 141 |
|
|---|
| 142 |
if (Logger::enabled()) |
|---|
| 143 |
Logger::cout() << *gvar << '\n'; |
|---|
| 144 |
|
|---|
| 145 |
// if this global is used from a nested function, this is necessary or |
|---|
| 146 |
// optimization could potentially remove the global (if it's the only use) |
|---|
| 147 |
if (nakedUse) |
|---|
| 148 |
gIR->usedArray.push_back(DtoBitCast(gvar, getVoidPtrType())); |
|---|
| 149 |
|
|---|
| 150 |
// initialize |
|---|
| 151 |
DtoConstInitGlobal(this); |
|---|
| 152 |
} |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
/* ================================================================== */ |
|---|
| 156 |
|
|---|
| 157 |
void TypedefDeclaration::codegen(Ir*) |
|---|
| 158 |
{ |
|---|
| 159 |
Logger::print("TypedefDeclaration::toObjFile: %s\n", toChars()); |
|---|
| 160 |
LOG_SCOPE; |
|---|
| 161 |
|
|---|
| 162 |
// generate typeinfo |
|---|
| 163 |
DtoTypeInfoOf(type, false); |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
/* ================================================================== */ |
|---|
| 167 |
|
|---|
| 168 |
void EnumDeclaration::codegen(Ir*) |
|---|
| 169 |
{ |
|---|
| 170 |
Logger::println("Ignoring EnumDeclaration::toObjFile for %s", toChars()); |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
/* ================================================================== */ |
|---|
| 174 |
|
|---|
| 175 |
void FuncDeclaration::codegen(Ir* p) |
|---|
| 176 |
{ |
|---|
| 177 |
// don't touch function aliases, they don't contribute any new symbols |
|---|
| 178 |
if (!isFuncAliasDeclaration()) |
|---|
| 179 |
{ |
|---|
| 180 |
DtoResolveDsymbol(this); |
|---|
| 181 |
} |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
/* ================================================================== */ |
|---|
| 185 |
|
|---|
| 186 |
void TemplateInstance::codegen(Ir* p) |
|---|
| 187 |
{ |
|---|
| 188 |
#if LOG |
|---|
| 189 |
printf("TemplateInstance::toObjFile('%s', this = %p)\n", toChars(), this); |
|---|
| 190 |
#endif |
|---|
| 191 |
if (!errors && members) |
|---|
| 192 |
{ |
|---|
| 193 |
for (int i = 0; i < members->dim; i++) |
|---|
| 194 |
{ |
|---|
| 195 |
Dsymbol *s = (Dsymbol *)members->data[i]; |
|---|
| 196 |
s->codegen(p); |
|---|
| 197 |
} |
|---|
| 198 |
} |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
/* ================================================================== */ |
|---|
| 202 |
|
|---|
| 203 |
void TemplateMixin::codegen(Ir* p) |
|---|
| 204 |
{ |
|---|
| 205 |
TemplateInstance::codegen(p); |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
/* ================================================================== */ |
|---|
| 209 |
|
|---|
| 210 |
void AttribDeclaration::codegen(Ir* p) |
|---|
| 211 |
{ |
|---|
| 212 |
Array *d = include(NULL, NULL); |
|---|
| 213 |
|
|---|
| 214 |
if (d) |
|---|
| 215 |
{ |
|---|
| 216 |
for (unsigned i = 0; i < d->dim; i++) |
|---|
| 217 |
{ Dsymbol *s = (Dsymbol *)d->data[i]; |
|---|
| 218 |
s->codegen(p); |
|---|
| 219 |
} |
|---|
| 220 |
} |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
/* ================================================================== */ |
|---|
| 224 |
|
|---|
| 225 |
void obj_includelib(const char* lib); |
|---|
| 226 |
|
|---|
| 227 |
void PragmaDeclaration::codegen(Ir* p) |
|---|
| 228 |
{ |
|---|
| 229 |
if (ident == Id::lib) |
|---|
| 230 |
{ |
|---|
| 231 |
assert(args && args->dim == 1); |
|---|
| 232 |
|
|---|
| 233 |
Expression *e = (Expression *)args->data[0]; |
|---|
| 234 |
|
|---|
| 235 |
assert(e->op == TOKstring); |
|---|
| 236 |
|
|---|
| 237 |
StringExp *se = (StringExp *)e; |
|---|
| 238 |
char *name = (char *)mem.malloc(se->len + 1); |
|---|
| 239 |
memcpy(name, se->string, se->len); |
|---|
| 240 |
name[se->len] = 0; |
|---|
| 241 |
obj_includelib(name); |
|---|
| 242 |
} |
|---|
| 243 |
AttribDeclaration::codegen(p); |
|---|
| 244 |
} |
|---|
| 245 |
|
|---|
| 246 |
/* ================================================================== */ |
|---|