Wiki Roadmap Timeline Tickets New Ticket Source Search Help / Guide About Trac Login

root/gen/toir.cpp

Revision 1643:8f121883bce8, 80.0 kB (checked in by Kelly Wilson <wilsonk cpsc.ucalgary.ca>, 2 years ago)

Apply patch from klickverbot. This is his 'proper fix' patch for bug #395.

Line 
1 // Backend stubs
2
3 /* DMDFE backend stubs
4  * This file contains the implementations of the backend routines.
5  * For dmdfe these do nothing but print a message saying the module
6  * has been parsed. Substitute your own behaviors for these routimes.
7  */
8
9 #include <stdio.h>
10 #include <math.h>
11 #include <fstream>
12
13 #include "gen/llvm.h"
14 #include "llvm/Support/CommandLine.h"
15
16 #include "attrib.h"
17 #include "init.h"
18 #include "mtype.h"
19 #include "template.h"
20 #include "hdrgen.h"
21 #include "port.h"
22 #include "rmem.h"
23 #include "id.h"
24 #include "enum.h"
25
26 #include "gen/irstate.h"
27 #include "gen/logger.h"
28 #include "gen/tollvm.h"
29 #include "gen/llvmhelpers.h"
30 #include "gen/runtime.h"
31 #include "gen/arrays.h"
32 #include "gen/structs.h"
33 #include "gen/classes.h"
34 #include "gen/typeinf.h"
35 #include "gen/complex.h"
36 #include "gen/dvalue.h"
37 #include "gen/aa.h"
38 #include "gen/functions.h"
39 #include "gen/todebug.h"
40 #include "gen/nested.h"
41 #include "gen/utils.h"
42 #include "gen/warnings.h"
43
44 #include "llvm/Support/ManagedStatic.h"
45
46 llvm::cl::opt<bool> checkPrintf("check-printf-calls",
47     llvm::cl::desc("Validate printf call format strings against arguments"),
48     llvm::cl::ZeroOrMore);
49
50 //////////////////////////////////////////////////////////////////////////////////////////
51
52 void Expression::cacheLvalue(IRState* irs)
53 {
54     error("expression %s does not mask any l-value", toChars());
55     fatal();
56 }
57
58 //////////////////////////////////////////////////////////////////////////////////////////
59
60 DValue* DeclarationExp::toElem(IRState* p)
61 {
62     Logger::print("DeclarationExp::toElem: %s | T=%s\n", toChars(), type->toChars());
63     LOG_SCOPE;
64
65     return DtoDeclarationExp(declaration);
66 }
67
68 //////////////////////////////////////////////////////////////////////////////////////////
69
70 void VarExp::cacheLvalue(IRState* p)
71 {
72     Logger::println("Caching l-value of %s", toChars());
73     LOG_SCOPE;
74     cachedLvalue = toElem(p)->getLVal();
75 }
76
77 DValue* VarExp::toElem(IRState* p)
78 {
79     Logger::print("VarExp::toElem: %s @ %s\n", toChars(), type->toChars());
80     LOG_SCOPE;
81
82     assert(var);
83
84     if (cachedLvalue)
85     {
86         LLValue* V = cachedLvalue;
87         cachedLvalue = NULL;
88         return new DVarValue(type, V);
89     }
90
91     if (VarDeclaration* vd = var->isVarDeclaration())
92     {
93         Logger::println("VarDeclaration ' %s ' of type ' %s '", vd->toChars(), vd->type->toChars());
94
95         // this is an error! must be accessed with DotVarExp
96         if (var->needThis())
97         {
98             error("need 'this' to access member %s", toChars());
99             fatal();
100         }
101
102         // _arguments
103         if (vd->ident == Id::_arguments && p->func()->_arguments)
104         {
105             Logger::println("Id::_arguments");
106             LLValue* v = p->func()->_arguments;
107             return new DVarValue(type, vd, v);
108         }
109         // _argptr
110         else if (vd->ident == Id::_argptr && p->func()->_argptr)
111         {
112             Logger::println("Id::_argptr");
113             LLValue* v = p->func()->_argptr;
114             return new DVarValue(type, vd, v);
115         }
116         // _dollar
117         else if (vd->ident == Id::dollar)
118         {
119             Logger::println("Id::dollar");
120             assert(!p->arrays.empty());
121             LLValue* tmp = DtoArrayLen(p->arrays.back());
122             return new DImValue(type, tmp);
123         }
124         // typeinfo
125         else if (TypeInfoDeclaration* tid = vd->isTypeInfoDeclaration())
126         {
127             Logger::println("TypeInfoDeclaration");
128             tid->codegen(Type::sir);
129             assert(tid->ir.getIrValue());
130             const LLType* vartype = DtoType(type);
131             LLValue* m = tid->ir.getIrValue();
132             if (m->getType() != getPtrToType(vartype))
133                 m = p->ir->CreateBitCast(m, vartype, "tmp");
134             return new DImValue(type, m);
135         }
136         // classinfo
137         else if (ClassInfoDeclaration* cid = vd->isClassInfoDeclaration())
138         {
139             Logger::println("ClassInfoDeclaration: %s", cid->cd->toChars());
140             cid->cd->codegen(Type::sir);;
141             return new DVarValue(type, vd, cid->cd->ir.irStruct->getClassInfoSymbol());
142         }
143         // nested variable
144     #if DMDV2
145         else if (vd->nestedrefs.dim) {
146     #else
147         else if (vd->nestedref) {
148     #endif
149             Logger::println("nested variable");
150             return DtoNestedVariable(loc, type, vd);
151         }
152         // function parameter
153         else if (vd->isParameter()) {
154             Logger::println("function param");
155             Logger::println("type: %s", vd->type->toChars());
156             FuncDeclaration* fd = vd->toParent2()->isFuncDeclaration();
157             if (fd && fd != p->func()->decl) {
158                 Logger::println("nested parameter");
159                 return DtoNestedVariable(loc, type, vd);
160             }
161             else if (vd->storage_class & STClazy) {
162                 Logger::println("lazy parameter");
163                 assert(type->ty == Tdelegate);
164                 return new DVarValue(type, vd->ir.getIrValue());
165             }
166             else if (vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type) || llvm::isa<llvm::AllocaInst>(vd->ir.getIrValue())) {
167                 return new DVarValue(type, vd, vd->ir.getIrValue());
168             }
169             else if (llvm::isa<llvm::Argument>(vd->ir.getIrValue())) {
170                 return new DImValue(type, vd->ir.getIrValue());
171             }
172             else assert(0);
173         }
174         else {
175             Logger::println("a normal variable");
176
177             // take care of forward references of global variables
178             if (vd->isDataseg() || (vd->storage_class & STCextern)) {
179                 vd->codegen(Type::sir);
180             }
181
182             LLValue* val;
183
184             if (!vd->ir.isSet() || !(val = vd->ir.getIrValue())) {
185                 // FIXME: this error is bad!
186                 // We should be VERY careful about adding errors in general, as they have
187                 // a tendency to "mask" out the underlying problems ...
188                 error("variable %s not resolved", vd->toChars());
189                 if (Logger::enabled())
190                     Logger::cout() << "unresolved variable had type: " << *DtoType(vd->type) << '\n';
191                 fatal();
192             }
193
194             if (vd->isDataseg() || (vd->storage_class & STCextern)) {
195                 DtoConstInitGlobal(vd);
196                 val = DtoBitCast(val, DtoType(type->pointerTo()));
197             }
198
199             return new DVarValue(type, vd, val);
200         }
201     }
202     else if (FuncDeclaration* fdecl = var->isFuncDeclaration())
203     {
204         Logger::println("FuncDeclaration");
205         LLValue* func = 0;
206         if (fdecl->llvmInternal == LLVMinline_asm) {
207             error("special ldc inline asm is not a normal function");
208             fatal();
209         }
210         else if (fdecl->llvmInternal != LLVMva_arg) {
211             fdecl->codegen(Type::sir);
212             func = fdecl->ir.irFunc->func;
213         }
214         return new DFuncValue(fdecl, func);
215     }
216     else if (StaticStructInitDeclaration* sdecl = var->isStaticStructInitDeclaration())
217     {
218         // this seems to be the static initialiser for structs
219         Type* sdecltype = sdecl->type->toBasetype();
220         Logger::print("Sym: type=%s\n", sdecltype->toChars());
221         assert(sdecltype->ty == Tstruct);
222         TypeStruct* ts = (TypeStruct*)sdecltype;
223         assert(ts->sym);
224         ts->sym->codegen(Type::sir);
225
226         LLValue* initsym = ts->sym->ir.irStruct->getInitSymbol();
227         initsym = DtoBitCast(initsym, DtoType(ts->pointerTo()));
228         return new DVarValue(type, initsym);
229     }
230     else
231     {
232         assert(0 && "Unimplemented VarExp type");
233     }
234
235     return 0;
236 }
237
238 //////////////////////////////////////////////////////////////////////////////////////////
239
240 LLConstant* VarExp::toConstElem(IRState* p)
241 {
242     Logger::print("VarExp::toConstElem: %s @ %s\n", toChars(), type->toChars());
243     LOG_SCOPE;
244
245     if (StaticStructInitDeclaration* sdecl = var->isStaticStructInitDeclaration())
246     {
247         // this seems to be the static initialiser for structs
248         Type* sdecltype = sdecl->type->toBasetype();
249         Logger::print("Sym: type=%s\n", sdecltype->toChars());
250         assert(sdecltype->ty == Tstruct);
251         TypeStruct* ts = (TypeStruct*)sdecltype;
252         ts->sym->codegen(Type::sir);
253
254         return ts->sym->ir.irStruct->getDefaultInit();
255     }
256
257     if (TypeInfoDeclaration* ti = var->isTypeInfoDeclaration())
258     {
259         const LLType* vartype = DtoType(type);
260         LLConstant* m = DtoTypeInfoOf(ti->tinfo, false);
261         if (m->getType() != getPtrToType(vartype))
262             m = llvm::ConstantExpr::getBitCast(m, vartype);
263         return m;
264     }
265
266     VarDeclaration* vd = var->isVarDeclaration();
267     if (vd && vd->isConst() && vd->init)
268     {
269         if (vd->inuse)
270         {
271             error("recursive reference %s", toChars());
272             return llvm::UndefValue::get(DtoType(type));
273         }
274         vd->inuse++;
275         LLConstant* ret = DtoConstInitializer(loc, type, vd->init);
276         vd->inuse--;
277         // return the initializer
278         return ret;
279     }
280
281     // fail
282     error("non-constant expression %s", toChars());
283     return llvm::UndefValue::get(DtoType(type));
284 }
285
286 //////////////////////////////////////////////////////////////////////////////////////////
287
288 DValue* IntegerExp::toElem(IRState* p)
289 {
290     Logger::print("IntegerExp::toElem: %s @ %s\n", toChars(), type->toChars());
291     LOG_SCOPE;
292     LLConstant* c = toConstElem(p);
293     return new DConstValue(type, c);
294 }
295
296 //////////////////////////////////////////////////////////////////////////////////////////
297
298 LLConstant* IntegerExp::toConstElem(IRState* p)
299 {
300     Logger::print("IntegerExp::toConstElem: %s @ %s\n", toChars(), type->toChars());
301     LOG_SCOPE;
302     const LLType* t = DtoType(type);
303     if (isaPointer(t)) {
304         Logger::println("pointer");
305         LLConstant* i = LLConstantInt::get(DtoSize_t(),(uint64_t)value,false);
306         return llvm::ConstantExpr::getIntToPtr(i, t);
307     }
308     assert(llvm::isa<LLIntegerType>(t));
309     LLConstant* c = LLConstantInt::get(t,(uint64_t)value,!type->isunsigned());
310     assert(c);
311     if (Logger::enabled())
312         Logger::cout() << "value = " << *c << '\n';
313     return c;
314 }
315
316 //////////////////////////////////////////////////////////////////////////////////////////
317
318 DValue* RealExp::toElem(IRState* p)
319 {
320     Logger::print("RealExp::toElem: %s @ %s\n", toChars(), type->toChars());
321     LOG_SCOPE;
322     LLConstant* c = toConstElem(p);
323     return new DConstValue(type, c);
324 }
325
326 //////////////////////////////////////////////////////////////////////////////////////////
327
328 LLConstant* RealExp::toConstElem(IRState* p)
329 {
330     Logger::print("RealExp::toConstElem: %s @ %s | %La\n", toChars(), type->toChars(), value);
331     LOG_SCOPE;
332     Type* t = type->toBasetype();
333     return DtoConstFP(t, value);
334 }
335
336 //////////////////////////////////////////////////////////////////////////////////////////
337
338 DValue* NullExp::toElem(IRState* p)
339 {
340     Logger::print("NullExp::toElem(type=%s): %s\n", type->toChars(),toChars());
341     LOG_SCOPE;
342     LLConstant* c = toConstElem(p);
343     return new DNullValue(type, c);
344 }
345
346 //////////////////////////////////////////////////////////////////////////////////////////
347
348 LLConstant* NullExp::toConstElem(IRState* p)
349 {
350     Logger::print("NullExp::toConstElem(type=%s): %s\n", type->toChars(),toChars());
351     LOG_SCOPE;
352     const LLType* t = DtoType(type);
353     if (type->ty == Tarray) {
354         assert(isaStruct(t));
355         return llvm::ConstantAggregateZero::get(t);
356     }
357     else {
358         return LLConstant::getNullValue(t);
359     }
360     assert(0);
361     return NULL;
362 }
363
364 //////////////////////////////////////////////////////////////////////////////////////////
365
366 DValue* ComplexExp::toElem(IRState* p)
367 {
368     Logger::print("ComplexExp::toElem(): %s @ %s\n", toChars(), type->toChars());
369     LOG_SCOPE;
370     LLConstant* c = toConstElem(p);
371     LLValue* res;
372
373     if (c->isNullValue()) {
374         Type* t = type->toBasetype();
375         if (t->ty == Tcomplex32)
376             c = DtoConstFP(Type::tfloat32, 0);
377         else if (t->ty == Tcomplex64)
378             c = DtoConstFP(Type::tfloat64, 0);
379         else if (t->ty == Tcomplex80)
380             c = DtoConstFP(Type::tfloat80, 0);
381         else
382             assert(0);
383         res = DtoAggrPair(DtoType(type), c, c);
384     }
385     else {
386         res = DtoAggrPair(DtoType(type), c->getOperand(0), c->getOperand(1));
387     }
388
389     return new DImValue(type, res);
390 }
391
392 //////////////////////////////////////////////////////////////////////////////////////////
393
394 LLConstant* ComplexExp::toConstElem(IRState* p)
395 {
396     Logger::print("ComplexExp::toConstElem(): %s @ %s\n", toChars(), type->toChars());
397     LOG_SCOPE;
398     return DtoConstComplex(type, value.re, value.im);
399 }
400
401 //////////////////////////////////////////////////////////////////////////////////////////
402
403 DValue* StringExp::toElem(IRState* p)
404 {
405     Logger::print("StringExp::toElem: %s @ %s\n", toChars(), type->toChars());
406     LOG_SCOPE;
407
408     Type* dtype = type->toBasetype();
409     Type* cty = dtype->nextOf()->toBasetype();
410
411     const LLType* ct = DtoTypeNotVoid(cty);
412     //printf("ct = %s\n", type->nextOf()->toChars());
413     const LLArrayType* at = LLArrayType::get(ct,len+1);
414
415     LLConstant* _init;
416     if (cty->size() == 1) {
417         uint8_t* str = (uint8_t*)string;
418         llvm::StringRef cont((char*)str, len);
419         _init = LLConstantArray::get(p->context(), cont, true);
420     }
421     else if (cty->size() == 2) {
422         uint16_t* str = (uint16_t*)string;
423         std::vector<LLConstant*> vals;
424         vals.reserve(len+1);
425         for(size_t i=0; i<len; ++i) {
426             vals.push_back(LLConstantInt::get(ct, str[i], false));;
427         }
428         vals.push_back(LLConstantInt::get(ct, 0, false));
429         _init = LLConstantArray::get(at,vals);
430     }
431     else if (cty->size() == 4) {
432         uint32_t* str = (uint32_t*)string;
433         std::vector<LLConstant*> vals;
434         vals.reserve(len+1);
435         for(size_t i=0; i<len; ++i) {
436             vals.push_back(LLConstantInt::get(ct, str[i], false));;
437         }
438         vals.push_back(LLConstantInt::get(ct, 0, false));
439         _init = LLConstantArray::get(at,vals);
440     }
441     else
442     assert(0);
443
444     llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::InternalLinkage;
445     if (Logger::enabled())
446         Logger::cout() << "type: " << *at << "\ninit: " << *_init << '\n';
447     llvm::GlobalVariable* gvar = new llvm::GlobalVariable(*gIR->module,at,true,_linkage,_init,".str");
448
449     llvm::ConstantInt* zero = LLConstantInt::get(LLType::getInt32Ty(gIR->context()), 0, false);
450     LLConstant* idxs[2] = { zero, zero };
451     LLConstant* arrptr = llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2);
452
453     if (dtype->ty == Tarray) {
454         LLConstant* clen = LLConstantInt::get(DtoSize_t(),len,false);
455         return new DImValue(type, DtoConstSlice(clen, arrptr));
456     }
457     else if (dtype->ty == Tsarray) {
458         const LLType* dstType = getPtrToType(LLArrayType::get(ct, len));
459         LLValue* emem = (gvar->getType() == dstType) ? gvar : DtoBitCast(gvar, dstType);
460         return new DVarValue(type, emem);
461     }
462     else if (dtype->ty == Tpointer) {
463         return new DImValue(type, arrptr);
464     }
465
466     assert(0);
467     return 0;
468 }
469
470 //////////////////////////////////////////////////////////////////////////////////////////
471
472 LLConstant* StringExp::toConstElem(IRState* p)
473 {
474     Logger::print("StringExp::toConstElem: %s @ %s\n", toChars(), type->toChars());
475     LOG_SCOPE;
476
477     Type* t = type->toBasetype();
478     Type* cty = t->nextOf()->toBasetype();
479
480     bool nullterm = (t->ty != Tsarray);
481     size_t endlen = nullterm ? len+1 : len;
482
483     const LLType* ct = DtoTypeNotVoid(cty);
484     const LLArrayType* at = LLArrayType::get(ct,endlen);
485
486     LLConstant* _init;
487     if (cty->size() == 1) {
488         uint8_t* str = (uint8_t*)string;
489         llvm::StringRef cont((char*)str, len);
490         _init = LLConstantArray::get(p->context(), cont, nullterm);
491     }
492     else if (cty->size() == 2) {
493         uint16_t* str = (uint16_t*)string;
494         std::vector<LLConstant*> vals;
495         vals.reserve(len+1);
496         for(size_t i=0; i<len; ++i) {
497             vals.push_back(LLConstantInt::get(ct, str[i], false));;
498         }
499         if (nullterm)
500             vals.push_back(LLConstantInt::get(ct, 0, false));
501         _init = LLConstantArray::get(at,vals);
502     }
503     else if (cty->size() == 4) {
504         uint32_t* str = (uint32_t*)string;
505         std::vector<LLConstant*> vals;
506         vals.reserve(len+1);
507         for(size_t i=0; i<len; ++i) {
508             vals.push_back(LLConstantInt::get(ct, str[i], false));;
509         }
510         if (nullterm)
511             vals.push_back(LLConstantInt::get(ct, 0, false));
512         _init = LLConstantArray::get(at,vals);
513     }
514     else
515     assert(0);
516
517     if (t->ty == Tsarray)
518     {
519         return _init;
520     }
521
522     llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::InternalLinkage;
523     llvm::GlobalVariable* gvar = new llvm::GlobalVariable(*gIR->module,_init->getType(),true,_linkage,_init,".str");
524
525     llvm::ConstantInt* zero = LLConstantInt::get(LLType::getInt32Ty(gIR->context()), 0, false);
526     LLConstant* idxs[2] = { zero, zero };
527     LLConstant* arrptr = llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2);
528
529     if (t->ty == Tpointer) {
530         return arrptr;
531     }
532     else if (t->ty == Tarray) {
533         LLConstant* clen = LLConstantInt::get(DtoSize_t(),len,false);
534         return DtoConstSlice(clen, arrptr);
535     }
536
537     assert(0);
538     return NULL;
539 }
540
541 //////////////////////////////////////////////////////////////////////////////////////////
542
543 DValue* AssignExp::toElem(IRState* p)
544 {
545     Logger::print("AssignExp::toElem: %s | (%s)(%s = %s)\n", toChars(), type->toChars(), e1->type->toChars(), e2->type ? e2->type->toChars() : 0);
546     LOG_SCOPE;
547
548     if (e1->op == TOKarraylength)
549     {
550         Logger::println("performing array.length assignment");
551         ArrayLengthExp *ale = (ArrayLengthExp *)e1;
552         DValue* arr = ale->e1->toElem(p);
553         DVarValue arrval(ale->e1->type, arr->getLVal());
554         DValue* newlen = e2->toElem(p);
555         DSliceValue* slice = DtoResizeDynArray(arrval.getType(), &arrval, newlen);
556         DtoAssign(loc, &arrval, slice);
557         return newlen;
558     }
559
560     Logger::println("performing normal assignment");
561
562     DValue* l = e1->toElem(p);
563     DValue* r = e2->toElem(p);
564     DtoAssign(loc, l, r);
565
566     if (l->isSlice())
567         return l;
568
569     return r;
570 }
571
572 //////////////////////////////////////////////////////////////////////////////////////////
573
574 /// Finds the proper lvalue for a binassign expressions.
575 /// Makes sure the given LHS expression is only evaluated once.
576 static Expression* findLvalue(IRState* irs, Expression* exp)
577 {
578     Expression* e = exp;
579
580     // skip past any casts
581     while(e->op == TOKcast)
582         e = ((CastExp*)e)->e1;
583
584     // cache lvalue and return
585     e->cacheLvalue(irs);
586     return e;
587 }
588
589 #define BIN_ASSIGN(X) \
590 DValue* X##AssignExp::toElem(IRState* p) \
591 { \
592     Logger::print(#X"AssignExp::toElem: %s @ %s\n", toChars(), type->toChars()); \
593     LOG_SCOPE; \
594     X##Exp e3(loc, e1, e2); \
595     e3.type = e1->type; \
596     DValue* dst = findLvalue(p, e1)->toElem(p); \
597     DValue* res = e3.toElem(p); \
598     DValue* stval = DtoCast(loc, res, dst->getType()); \
599     DtoAssign(loc, dst, stval); \
600     return DtoCast(loc, res, type); \
601 }
602
603 BIN_ASSIGN(Add)
604 BIN_ASSIGN(Min)
605 BIN_ASSIGN(Mul)
606 BIN_ASSIGN(Div)
607 BIN_ASSIGN(Mod)
608 BIN_ASSIGN(And)
609 BIN_ASSIGN(Or)
610 BIN_ASSIGN(Xor)
611 BIN_ASSIGN(Shl)
612 BIN_ASSIGN(Shr)
613 BIN_ASSIGN(Ushr)
614
615 #undef BIN_ASSIGN
616
617 //////////////////////////////////////////////////////////////////////////////////////////
618
619 static void errorOnIllegalArrayOp(Expression* base, Expression* e1, Expression* e2)
620 {
621     Type* t1 = e1->type->toBasetype();
622     Type* t2 = e2->type->toBasetype();
623
624     // valid array ops would have been transformed by optimize
625     if ((t1->ty == Tarray || t1->ty == Tsarray) &&
626         (t2->ty == Tarray || t2->ty == Tsarray)
627        )
628     {
629         base->error("Array operation %s not recognized", base->toChars());
630         fatal();
631     }
632 }
633
634 //////////////////////////////////////////////////////////////////////////////////////////
635
636 DValue* AddExp::toElem(IRState* p)
637 {
638     Logger::print("AddExp::toElem: %s @ %s\n", toChars(), type->toChars());
639     LOG_SCOPE;
640
641     DValue* l = e1->toElem(p);
642     DValue* r = e2->toElem(p);
643
644     Type* t = type->toBasetype();
645     Type* e1type = e1->type->toBasetype();
646     Type* e1next = e1type->nextOf() ? e1type->nextOf()->toBasetype() : NULL;
647     Type* e2type = e2->type->toBasetype();
648
649     errorOnIllegalArrayOp(this, e1, e2);
650
651     if (e1type != e2type) {
652         if (e1type->ty == Tpointer) {
653             Logger::println("add to pointer");
654             if (DConstValue* cv = r->isConst()) {
655                 if (cv->c->isNullValue()) {
656                     Logger::println("is zero");
657                     return new DImValue(type, l->getRVal());
658                 }
659             }
660             LLValue* v = llvm::GetElementPtrInst::Create(l->getRVal(), r->getRVal(), "tmp", p->scopebb());
661             return new DImValue(type, v);
662         }
663         else if (t->iscomplex()) {
664             return DtoComplexAdd(loc, type, l, r);
665         }
666         assert(0);
667     }
668     else if (t->iscomplex()) {
669         return DtoComplexAdd(loc, type, l, r);
670     }
671     else {
672         return DtoBinAdd(l,r);
673     }
674 }
675
676 //////////////////////////////////////////////////////////////////////////////////////////
677
678 DValue* MinExp::toElem(IRState* p)
679 {
680     Logger::print("MinExp::toElem: %s @ %s\n", toChars(), type->toChars());
681     LOG_SCOPE;
682
683     DValue* l = e1->toElem(p);
684     DValue* r = e2->toElem(p);
685
686     Type* t = type->toBasetype();
687     Type* t1 = e1->type->toBasetype();
688     Type* t2 = e2->type->toBasetype();
689
690     errorOnIllegalArrayOp(this, e1, e2);
691
692     if (t1->ty == Tpointer && t2->ty == Tpointer) {
693         LLValue* lv = l->getRVal();
694         LLValue* rv = r->getRVal();
695         if (Logger::enabled())
696             Logger::cout() << "lv: " << *lv << " rv: " << *rv << '\n';
697         lv = p->ir->CreatePtrToInt(lv, DtoSize_t(), "tmp");
698         rv = p->ir->CreatePtrToInt(rv, DtoSize_t(), "tmp");
699         LLValue* diff = p->ir->CreateSub(lv,rv,"tmp");
700         if (diff->getType() != DtoType(type))
701             diff = p->ir->CreateIntToPtr(diff, DtoType(type), "tmp");
702         return new DImValue(type, diff);
703     }
704     else if (t1->ty == Tpointer) {
705         LLValue* idx = p->ir->CreateNeg(r->getRVal(), "tmp");
706         LLValue* v = llvm::GetElementPtrInst::Create(l->getRVal(), idx, "tmp", p->scopebb());
707         return new DImValue(type, v);
708     }
709     else if (t->iscomplex()) {
710         return DtoComplexSub(loc, type, l, r);
711     }
712     else {
713         return DtoBinSub(l,r);
714     }
715 }
716
717 //////////////////////////////////////////////////////////////////////////////////////////
718
719 DValue* MulExp::toElem(IRState* p)
720 {
721     Logger::print("MulExp::toElem: %s @ %s\n", toChars(), type->toChars());
722     LOG_SCOPE;
723
724     DValue* l = e1->toElem(p);
725     DValue* r = e2->toElem(p);
726
727     errorOnIllegalArrayOp(this, e1, e2);
728
729     if (type->iscomplex()) {
730         return DtoComplexMul(loc, type, l, r);
731     }
732
733     return DtoBinMul(type, l, r);
734 }
735
736 //////////////////////////////////////////////////////////////////////////////////////////
737
738 DValue* DivExp::toElem(IRState* p)
739 {
740     Logger::print("DivExp::toElem: %s @ %s\n", toChars(), type->toChars());
741     LOG_SCOPE;
742
743     DValue* l = e1->toElem(p);
744     DValue* r = e2->toElem(p);
745
746     errorOnIllegalArrayOp(this, e1, e2);
747
748     if (type->iscomplex()) {
749         return DtoComplexDiv(loc, type, l, r);
750     }
751
752     return DtoBinDiv(type, l, r);
753 }
754
755 //////////////////////////////////////////////////////////////////////////////////////////
756
757 DValue* ModExp::toElem(IRState* p)
758 {
759     Logger::print("ModExp::toElem: %s @ %s\n", toChars(), type->toChars());
760     LOG_SCOPE;
761
762     DValue* l = e1->toElem(p);
763     DValue* r = e2->toElem(p);
764
765     errorOnIllegalArrayOp(this, e1, e2);
766
767     return DtoBinRem(type, l, r);
768 }
769
770 //////////////////////////////////////////////////////////////////////////////////////////
771
772 DValue* CallExp::toElem(IRState* p)
773 {
774     Logger::print("CallExp::toElem: %s @ %s\n", toChars(), type->toChars());
775     LOG_SCOPE;
776
777     // handle magic inline asm
778     if (e1->op == TOKvar)
779     {
780         VarExp* ve = (VarExp*)e1;
781         if (FuncDeclaration* fd = ve->var->isFuncDeclaration())
782         {
783             if (fd->llvmInternal == LLVMinline_asm)
784             {
785                 return DtoInlineAsmExpr(loc, fd, arguments);
786             }
787         }
788     }
789
790     // get the callee value
791     DValue* fnval = e1->toElem(p);
792
793     // get func value if any
794     DFuncValue* dfnval = fnval->isFunc();
795
796     // handle magic intrinsics (mapping to instructions)
797     bool va_intrinsic = false;
798     if (dfnval && dfnval->func)
799     {
800         FuncDeclaration* fndecl = dfnval->func;
801
802         // as requested by bearophile, see if it's a C printf call and that it's valid.
803         if (global.params.warnings && checkPrintf)
804         {
805             if (fndecl->linkage == LINKc && strcmp(fndecl->ident->string, "printf") == 0)
806             {
807                 warnInvalidPrintfCall(loc, (Expression*)arguments->data[0], arguments->dim);
808             }
809         }
810
811         // va_start instruction
812         if (fndecl->llvmInternal == LLVMva_start) {
813             // llvm doesn't need the second param hence the override
814             Expression* exp = (Expression*)arguments->data[0];
815             DValue* expv = exp->toElem(p);
816             LLValue* arg = DtoBitCast(expv->getLVal(), getVoidPtrType());
817             return new DImValue(type, gIR->ir->CreateCall(GET_INTRINSIC_DECL(vastart), arg, ""));
818         }
819         // va_arg instruction
820         else if (fndecl->llvmInternal == LLVMva_arg) {
821             return DtoVaArg(loc, type, (Expression*)arguments->data[0]);
822         }
823         // C alloca
824         else if (fndecl->llvmInternal == LLVMalloca) {
825             Expression* exp = (Expression*)arguments->data[0];
826             DValue* expv = exp->toElem(p);
827             if (expv->getType()->toBasetype()->ty != Tint32)
828                 expv = DtoCast(loc, expv, Type::tint32);
829             return new DImValue(type, p->ir->CreateAlloca(LLType::getInt8Ty(gIR->context()), expv->getRVal(), ".alloca"));
830         }
831     }
832
833     return DtoCallFunction(loc, type, fnval, arguments);
834 }
835
836 //////////////////////////////////////////////////////////////////////////////////////////
837
838 DValue* CastExp::toElem(IRState* p)
839 {
840     Logger::print("CastExp::toElem: %s @ %s\n", toChars(), type->toChars());
841     LOG_SCOPE;
842
843     // get the value to cast
844     DValue* u = e1->toElem(p);
845
846     // cast it to the 'to' type, if necessary
847     DValue* v = u;
848     if (!to->equals(e1->type))
849         v = DtoCast(loc, u, to);
850
851     // paint the type, if necessary
852     if (!type->equals(to))
853         v = DtoPaintType(loc, v, type);
854
855     // return the new rvalue
856     return v;
857 }
858
859 //////////////////////////////////////////////////////////////////////////////////////////
860
861 LLConstant* CastExp::toConstElem(IRState* p)
862 {
863     Logger::print("CastExp::toConstElem: %s @ %s\n", toChars(), type->toChars());
864     LOG_SCOPE;
865
866     LLConstant* res;
867     const LLType* lltype = DtoType(type);
868     Type* tb = to->toBasetype();
869
870     // string literal to dyn array:
871     // reinterpret the string data as an array, calculate the length
872     if (e1->op == TOKstring && tb->ty == Tarray) {
873 /*        StringExp *strexp = (StringExp*)e1;
874         size_t datalen = strexp->sz * strexp->len;
875         Type* eltype = tb->nextOf()->toBasetype();
876         if (datalen % eltype->size() != 0) {
877             error("the sizes don't line up");
878             return e1->toConstElem(p);
879         }
880         size_t arrlen = datalen / eltype->size();*/
881         error("ct cast of string to dynamic array not fully implemented");
882         return e1->toConstElem(p);
883     }
884     // pointer to pointer
885     else if (tb->ty == Tpointer && e1->type->toBasetype()->ty == Tpointer) {
886         res = llvm::ConstantExpr::getBitCast(e1->toConstElem(p), lltype);
887     }
888     else {
889         error("can not cast %s to %s at compile time", e1->type->toChars(), type->toChars());
890         return e1->toConstElem(p);
891     }
892
893     return res;
894 }
895
896 //////////////////////////////////////////////////////////////////////////////////////////
897
898 DValue* SymOffExp::toElem(IRState* p)
899 {
900     Logger::print("SymOffExp::toElem: %s @ %s\n", toChars(), type->toChars());
901     LOG_SCOPE;
902
903     assert(0 && "SymOffExp::toElem should no longer be called :/");
904     return 0;
905 }
906
907 //////////////////////////////////////////////////////////////////////////////////////////
908
909 DValue* AddrExp::toElem(IRState* p)
910 {
911     Logger::println("AddrExp::toElem: %s @ %s", toChars(), type->toChars());
912     LOG_SCOPE;
913     DValue* v = e1->toElem(p);
914     if (v->isField()) {
915         Logger::println("is field");
916         return v;
917     }
918     else if (DFuncValue* fv = v->isFunc()) {
919         Logger::println("is func");
920         //Logger::println("FuncDeclaration");
921         FuncDeclaration* fd = fv->func;
922         assert(fd);
923         fd->codegen(Type::sir);
924         return new DFuncValue(fd, fd->ir.irFunc->func);
925     }
926     else if (DImValue* im = v->isIm()) {
927         Logger::println("is immediate");
928         return v;
929     }
930     Logger::println("is nothing special");
931
932     // we special case here, since apparently taking the address of a slice is ok
933     LLValue* lval;
934     if (v->isLVal())
935         lval = v->getLVal();
936     else
937     {
938         assert(v->isSlice());
939         LLValue* rval = v->getRVal();
940         lval = DtoRawAlloca(rval->getType(), 0, ".tmp_slice_storage");
941         DtoStore(rval, lval);
942     }
943
944     if (Logger::enabled())
945         Logger::cout() << "lval: " << *lval << '\n';
946
947     return new DImValue(type, DtoBitCast(lval, DtoType(type)));
948 }
949
950 LLConstant* AddrExp::toConstElem(IRState* p)
951 {
952     // FIXME: this should probably be generalized more so we don't
953     // need to have a case for each thing we can take the address of
954
955     // address of global variable
956     if (e1->op == TOKvar)
957     {
958         VarExp* vexp = (VarExp*)e1;
959
960         // make sure 'this' isn't needed
961         if (vexp->var->needThis())
962         {
963             error("need 'this' to access %s", vexp->var->toChars());
964             fatal();
965         }
966
967         // global variable
968         if (VarDeclaration* vd = vexp->var->isVarDeclaration())
969         {
970             vd->codegen(Type::sir);
971             LLConstant* llc = llvm::dyn_cast<LLConstant>(vd->ir.getIrValue());
972             assert(llc);
973             return llc;
974         }
975         // static function
976         else if (FuncDeclaration* fd = vexp->var->isFuncDeclaration())
977         {
978             fd->codegen(Type::sir);
979             IrFunction* irfunc = fd->ir.irFunc;
980             return irfunc->func;
981         }
982         // something else
983         else
984         {
985             // fail
986             goto Lerr;
987         }
988     }
989     // address of indexExp
990     else if (e1->op == TOKindex)
991     {
992         IndexExp* iexp = (IndexExp*)e1;
993
994         // indexee must be global static array var
995         assert(iexp->e1->op == TOKvar);
996         VarExp* vexp = (VarExp*)iexp->e1;
997         VarDeclaration* vd = vexp->var->isVarDeclaration();
998         assert(vd);
999         assert(vd->type->toBasetype()->ty == Tsarray);
1000         vd->codegen(Type::sir);
1001         assert(vd->ir.irGlobal);
1002
1003         // get index
1004         LLConstant* index = iexp->e2->toConstElem(p);
1005         assert(index->getType() == DtoSize_t());
1006
1007         // gep
1008         LLConstant* idxs[2] = { DtoConstSize_t(0), index };
1009         LLConstant* gep = llvm::ConstantExpr::getGetElementPtr(isaConstant(vd->ir.irGlobal->value), idxs, 2);
1010
1011         // bitcast to requested type
1012         assert(type->toBasetype()->ty == Tpointer);
1013         return DtoBitCast(gep, DtoType(type));
1014     }
1015     else if (
1016         e1->op == TOKstructliteral ||
1017         e1->op == TOKslice)
1018     {
1019         error("non-constant expression '%s'", toChars());
1020         fatal();
1021     }
1022     // not yet supported
1023     else
1024     {
1025     Lerr:
1026         error("constant expression '%s' not yet implemented", toChars());
1027         fatal();
1028     }
1029 }
1030
1031 //////////////////////////////////////////////////////////////////////////////////////////
1032
1033 void PtrExp::cacheLvalue(IRState* p)
1034 {
1035     Logger::println("Caching l-value of %s", toChars());
1036     LOG_SCOPE;
1037     cachedLvalue = e1->toElem(p)->getRVal();
1038 }
1039
1040 DValue* PtrExp::toElem(IRState* p)
1041 {
1042     Logger::println("PtrExp::toElem: %s @ %s", toChars(), type->toChars());
1043     LOG_SCOPE;
1044
1045     // function pointers are special
1046     if (type->toBasetype()->ty == Tfunction)
1047     {
1048         assert(!cachedLvalue);
1049         return new DImValue(type, e1->toElem(p)->getRVal());
1050     }
1051
1052     // get the rvalue and return it as an lvalue
1053     LLValue* V;
1054     if (cachedLvalue)
1055     {
1056         V = cachedLvalue;
1057         cachedLvalue = NULL;
1058     }
1059     else
1060     {
1061         V = e1->toElem(p)->getRVal();
1062     }
1063     return new DVarValue(type, V);
1064 }
1065
1066 //////////////////////////////////////////////////////////////////////////////////////////
1067
1068 void DotVarExp::cacheLvalue(IRState* p)
1069 {
1070     Logger::println("Caching l-value of %s", toChars());
1071     LOG_SCOPE;
1072     cachedLvalue = toElem(p)->getLVal();
1073 }
1074
1075 DValue* DotVarExp::toElem(IRState* p)
1076 {
1077     Logger::print("DotVarExp::toElem: %s @ %s\n", toChars(), type->toChars());
1078     LOG_SCOPE;
1079
1080     if (cachedLvalue)
1081     {
1082         Logger::println("using cached lvalue");
1083         LLValue *V = cachedLvalue;
1084         cachedLvalue = NULL;
1085         VarDeclaration* vd = var->isVarDeclaration();
1086         assert(vd);
1087         return new DVarValue(type, vd, V);
1088     }
1089
1090     DValue* l = e1->toElem(p);
1091
1092     Type* t = type->toBasetype();
1093     Type* e1type = e1->type->toBasetype();
1094
1095     //Logger::println("e1type=%s", e1type->toChars());
1096     //Logger::cout() << *DtoType(e1type) << '\n';
1097
1098     if (VarDeclaration* vd = var->isVarDeclaration()) {
1099         LLValue* arrptr;
1100         // indexing struct pointer
1101         if (e1type->ty == Tpointer) {
1102             assert(e1type->nextOf()->ty == Tstruct);
1103             TypeStruct* ts = (TypeStruct*)e1type->nextOf();
1104             arrptr = DtoIndexStruct(l->getRVal(), ts->sym, vd);
1105         }
1106         // indexing normal struct
1107         else if (e1type->ty == Tstruct) {
1108             TypeStruct* ts = (TypeStruct*)e1type;
1109             arrptr = DtoIndexStruct(l->getRVal(), ts->sym, vd);
1110         }
1111         // indexing class
1112         else if (e1type->ty == Tclass) {
1113             TypeClass* tc = (TypeClass*)e1type;
1114             arrptr = DtoIndexClass(l->getRVal(), tc->sym, vd);
1115         }
1116         else
1117             assert(0);
1118
1119         //Logger::cout() << "mem: " << *arrptr << '\n';
1120         return new DVarValue(type, vd, arrptr);
1121     }
1122     else if (FuncDeclaration* fdecl = var->isFuncDeclaration())
1123     {
1124         DtoResolveDsymbol(fdecl);
1125
1126         LLValue* funcval;
1127         LLValue* vthis2 = 0;
1128         if (e1type->ty == Tclass) {
1129             TypeClass* tc = (TypeClass*)e1type;
1130             if (tc->sym->isInterfaceDeclaration()) {
1131                 vthis2 = DtoCastInterfaceToObject(l, NULL)->getRVal();
1132             }
1133         }
1134         LLValue* vthis = l->getRVal();
1135         if (!vthis2) vthis2 = vthis;
1136
1137         //
1138         // decide whether this function needs to be looked up in the vtable
1139         //
1140         bool vtbllookup = fdecl->isAbstract() || (!fdecl->isFinal() && fdecl->isVirtual());
1141
1142         // even virtual functions are looked up directly if super or DotTypeExp
1143         // are used, thus we need to walk through the this expression and check
1144         Expression* e = e1;
1145         while (e && vtbllookup) {
1146             if (e->op == TOKsuper || e->op == TOKdottype)
1147                 vtbllookup = false;
1148             else if (e->op == TOKcast)
1149                 e = ((CastExp*)e)->e1;
1150             else
1151                 break;
1152         }
1153
1154         //
1155         // look up function
1156         //
1157         if (!vtbllookup) {
1158             fdecl->codegen(Type::sir);
1159             funcval = fdecl->ir.irFunc->func;
1160             assert(funcval);
1161         }
1162         else {
1163             DImValue vthis3(e1type, vthis);
1164             funcval = DtoVirtualFunctionPointer(&vthis3, fdecl, toChars());
1165         }
1166
1167         return new DFuncValue(fdecl, funcval, vthis2);
1168     }
1169     else {
1170         printf("unsupported dotvarexp: %s\n", var->toChars());
1171     }
1172
1173     assert(0);
1174     return 0;
1175 }
1176
1177 //////////////////////////////////////////////////////////////////////////////////////////
1178
1179 DValue* ThisExp::toElem(IRState* p)
1180 {
1181     Logger::print("ThisExp::toElem: %s @ %s\n", toChars(), type->toChars());
1182     LOG_SCOPE;
1183
1184     // regular this expr
1185     if (VarDeclaration* vd = var->isVarDeclaration()) {
1186         LLValue* v;
1187         if (vd->toParent2() != p->func()->decl) {
1188             Logger::println("nested this exp");
1189             return DtoNestedVariable(loc, type, vd);
1190         }
1191         else {
1192             Logger::println("normal this exp");
1193             v = p->func()->thisArg;
1194         }
1195         return new DVarValue(type, vd, v);
1196     }
1197
1198     // anything we're not yet handling ?
1199     assert(0 && "no var in ThisExp");
1200     return 0;
1201 }
1202
1203 //////////////////////////////////////////////////////////////////////////////////////////
1204
1205 void IndexExp::cacheLvalue(IRState* p)
1206 {
1207     Logger::println("Caching l-value of %s", toChars());
1208     LOG_SCOPE;
1209     cachedLvalue = toElem(p)->getLVal();
1210 }
1211
1212 DValue* IndexExp::toElem(IRState* p)
1213 {
1214     Logger::print("IndexExp::toElem: %s @ %s\n", toChars(), type->toChars());
1215     LOG_SCOPE;
1216
1217     if (cachedLvalue)
1218     {
1219         LLValue* V = cachedLvalue;
1220         cachedLvalue = NULL;
1221         return new DVarValue(type, V);
1222     }
1223
1224     DValue* l = e1->toElem(p);
1225
1226     Type* e1type = e1->type->toBasetype();
1227
1228     p->arrays.push_back(l); // if $ is used it must be an array so this is fine.
1229     DValue* r = e2->toElem(p);
1230     p->arrays.pop_back();
1231
1232     LLValue* zero = DtoConstUint(0);
1233     LLValue* one = DtoConstUint(1);
1234
1235     LLValue* arrptr = 0;
1236     if (e1type->ty == Tpointer) {
1237         arrptr = DtoGEP1(l->getRVal(),r->getRVal());
1238     }
1239     else if (e1type->ty == Tsarray) {
1240         if(global.params.useArrayBounds)
1241             DtoArrayBoundsCheck(loc, l, r, false);
1242         arrptr = DtoGEP(l->getRVal(), zero, r->getRVal());
1243     }
1244     else if (e1type->ty == Tarray) {
1245         if(global.params.useArrayBounds)
1246             DtoArrayBoundsCheck(loc, l, r, false);
1247         arrptr = DtoArrayPtr(l);
1248         arrptr = DtoGEP1(arrptr,r->getRVal());
1249     }
1250     else if (e1type->ty == Taarray) {
1251         return DtoAAIndex(loc, type, l, r, modifiable);
1252     }
1253     else {
1254         Logger::println("invalid index exp! e1type: %s", e1type->toChars());
1255         assert(0);
1256     }
1257     return new DVarValue(type, arrptr);
1258 }
1259
1260 //////////////////////////////////////////////////////////////////////////////////////////
1261
1262 DValue* SliceExp::toElem(IRState* p)
1263 {
1264     Logger::print("SliceExp::toElem: %s @ %s\n", toChars(), type->toChars());
1265     LOG_SCOPE;
1266
1267     // this is the new slicing code, it's different in that a full slice will no longer retain the original pointer.
1268     // but this was broken if there *was* no original pointer, ie. a slice of a slice...
1269     // now all slices have *both* the 'len' and 'ptr' fields set to != null.
1270
1271     // value being sliced
1272     LLValue* elen;
1273     LLValue* eptr;
1274     DValue* e = e1->toElem(p);
1275
1276     // handle pointer slicing
1277     Type* etype = e1->type->toBasetype();
1278     if (etype->ty == Tpointer)
1279     {
1280         assert(lwr);
1281         eptr = e->getRVal();
1282     }
1283     // array slice
1284     else
1285     {
1286         eptr = DtoArrayPtr(e);
1287     }
1288
1289     // has lower bound, pointer needs adjustment
1290     if (lwr)
1291     {
1292         // must have upper bound too then
1293         assert(upr);
1294
1295         // get bounds (make sure $ works)
1296         p->arrays.push_back(e);
1297         DValue* lo = lwr->toElem(p);
1298         DValue* up = upr->toElem(p);
1299         p->arrays.pop_back();
1300         LLValue* vlo = lo->getRVal();
1301         LLValue* vup = up->getRVal();
1302
1303         if(global.params.useArrayBounds && (etype->ty == Tsarray || etype->ty == Tarray))
1304             DtoArrayBoundsCheck(loc, e, up, true);
1305
1306         // offset by lower
1307         eptr = DtoGEP1(eptr, vlo);
1308
1309         // adjust length
1310         elen = p->ir->CreateSub(vup, vlo, "tmp");
1311     }
1312     // no bounds or full slice -> just convert to slice
1313     else
1314     {
1315         assert(e1->type->toBasetype()->ty != Tpointer);
1316         // if the sliceee is a static array, we use the length of that as DMD seems
1317         // to give contrary inconsistent sizesin some multidimensional static array cases.
1318         // (namely default initialization, int[16][16] arr; -> int[256] arr = 0;)
1319         if (etype->ty == Tsarray)
1320         {
1321             TypeSArray* tsa = (TypeSArray*)etype;
1322             elen = DtoConstSize_t(tsa->dim->toUInteger());
1323
1324             // in this case, we also need to make sure the pointer is cast to the innermost element type
1325             eptr = DtoBitCast(eptr, DtoType(tsa->nextOf()->pointerTo()));
1326         }
1327         // for normal code the actual array length is what we want!
1328         else
1329         {
1330             elen = DtoArrayLen(e);
1331         }
1332     }
1333
1334     return new DSliceValue(type, elen, eptr);
1335 }
1336
1337 //////////////////////////////////////////////////////////////////////////////////////////
1338
1339 DValue* CmpExp::toElem(IRState* p)
1340 {
1341     Logger::print("CmpExp::toElem: %s @ %s\n", toChars(), type->toChars());
1342     LOG_SCOPE;
1343
1344     DValue* l = e1->toElem(p);
1345     DValue* r = e2->toElem(p);
1346
1347     Type* t = e1->type->toBasetype();
1348     Type* e2t = e2->type->toBasetype();
1349
1350     LLValue* eval = 0;
1351
1352     if (t->isintegral() || t->ty == Tpointer)
1353     {
1354         llvm::ICmpInst::Predicate cmpop;
1355         bool skip = false;
1356         // pointers don't report as being unsigned
1357         bool uns = (t->isunsigned() || t->ty == Tpointer);
1358         switch(op)
1359         {
1360         case TOKlt:
1361         case TOKul:
1362             cmpop = uns ? llvm::ICmpInst::ICMP_ULT : llvm::ICmpInst::ICMP_SLT;
1363             break;
1364         case TOKle:
1365         case TOKule:
1366             cmpop = uns ? llvm::ICmpInst::ICMP_ULE : llvm::ICmpInst::ICMP_SLE;
1367             break;
1368         case TOKgt:
1369         case TOKug:
1370             cmpop = uns ? llvm::ICmpInst::ICMP_UGT : llvm::ICmpInst::ICMP_SGT;
1371             break;
1372         case TOKge:
1373         case TOKuge:
1374             cmpop = uns ? llvm::ICmpInst::ICMP_UGE : llvm::ICmpInst::ICMP_SGE;
1375             break;
1376         case TOKue:
1377             cmpop = llvm::ICmpInst::ICMP_EQ;
1378             break;
1379         case TOKlg:
1380             cmpop = llvm::ICmpInst::ICMP_NE;
1381             break;
1382         case TOKleg:
1383             skip = true;
1384             eval = LLConstantInt::getTrue(gIR->context());
1385             break;
1386         case TOKunord:
1387             skip = true;
1388             eval = LLConstantInt::getFalse(gIR->context());
1389             break;
1390
1391         default:
1392             assert(0);
1393         }
1394         if (!skip)
1395         {
1396             LLValue* a = l->getRVal();
1397             LLValue* b = r->getRVal();
1398             if (Logger::enabled())
1399             {
1400                 Logger::cout() << "type 1: " << *a << '\n';
1401                 Logger::cout() << "type 2: " << *b << '\n';
1402             }
1403             if (a->getType() != b->getType())
1404                 b = DtoBitCast(b, a->getType());
1405             eval = p->ir->CreateICmp(cmpop, a, b, "tmp");
1406         }
1407     }
1408     else if (t->isfloating())
1409     {
1410         llvm::FCmpInst::Predicate cmpop;
1411         switch(op)
1412         {
1413         case TOKlt:
1414             cmpop = llvm::FCmpInst::FCMP_OLT;break;
1415         case TOKle:
1416             cmpop = llvm::FCmpInst::FCMP_OLE;break;
1417         case TOKgt:
1418             cmpop = llvm::FCmpInst::FCMP_OGT;break;
1419         case TOKge:
1420             cmpop = llvm::FCmpInst::FCMP_OGE;break;
1421         case TOKunord:
1422             cmpop = llvm::FCmpInst::FCMP_UNO;break;
1423         case TOKule:
1424             cmpop = llvm::FCmpInst::FCMP_ULE;break;
1425         case TOKul:
1426             cmpop = llvm::FCmpInst::FCMP_ULT;break;
1427         case TOKuge:
1428             cmpop = llvm::FCmpInst::FCMP_UGE;break;
1429         case TOKug:
1430             cmpop = llvm::FCmpInst::FCMP_UGT;break;
1431         case TOKue:
1432             cmpop = llvm::FCmpInst::FCMP_UEQ;break;
1433         case TOKlg:
1434             cmpop = llvm::FCmpInst::FCMP_ONE;break;
1435         case TOKleg:
1436             cmpop = llvm::FCmpInst::FCMP_ORD;break;
1437
1438         default:
1439             assert(0);
1440         }
1441         eval = p->ir->CreateFCmp(cmpop, l->getRVal(), r->getRVal(), "tmp");
1442     }
1443     else if (t->ty == Tsarray || t->ty == Tarray)
1444     {
1445         Logger::println("static or dynamic array");
1446         eval = DtoArrayCompare(loc,op,l,r);
1447     }
1448     else
1449     {
1450         assert(0 && "Unsupported CmpExp type");
1451     }
1452
1453     return new DImValue(type, eval);
1454 }
1455
1456 //////////////////////////////////////////////////////////////////////////////////////////
1457
1458 DValue* EqualExp::toElem(IRState* p)
1459 {
1460     Logger::print("EqualExp::toElem: %s @ %s\n", toChars(), type->toChars());
1461     LOG_SCOPE;
1462
1463     DValue* l = e1->toElem(p);
1464     DValue* r = e2->toElem(p);
1465     LLValue* lv = l->getRVal();
1466     LLValue* rv = r->getRVal();
1467
1468     Type* t = e1->type->toBasetype();
1469     Type* e2t = e2->type->toBasetype();
1470     //assert(t == e2t);
1471
1472     LLValue* eval = 0;
1473
1474     // the Tclass catches interface comparisons, regular
1475     // class equality should be rewritten as a.opEquals(b) by this time
1476     if (t->isintegral() || t->ty == Tpointer || t->ty == Tclass)
1477     {
1478         Logger::println("integral or pointer or interface");
1479         llvm::ICmpInst::Predicate cmpop;
1480         switch(op)
1481         {
1482         case TOKequal:
1483             cmpop = llvm::ICmpInst::ICMP_EQ;
1484             break;
1485         case TOKnotequal:
1486             cmpop = llvm::ICmpInst::ICMP_NE;
1487             break;
1488         default:
1489             assert(0);
1490         }
1491         if (rv->getType() != lv->getType()) {
1492             rv = DtoBitCast(rv, lv->getType());
1493         }
1494         if (Logger::enabled())
1495         {
1496             Logger::cout() << "lv: " << *lv << '\n';
1497             Logger::cout() << "rv: " << *rv << '\n';
1498         }
1499         eval = p->ir->CreateICmp(cmpop, lv, rv, "tmp");
1500     }
1501     else if (t->isfloating()) // includes iscomplex
1502     {
1503         eval = DtoBinNumericEquals(loc, l, r, op);
1504     }
1505     else if (t->ty == Tsarray || t->ty == Tarray)
1506     {
1507         Logger::println("static or dynamic array");
1508         eval = DtoArrayEquals(loc,op,l,r);
1509     }
1510     else if (t->ty == Taarray)
1511     {
1512         Logger::println("associative array");
1513         eval = DtoAAEquals(loc,op,l,r);
1514     }
1515     else if (t->ty == Tdelegate)
1516     {
1517         Logger::println("delegate");
1518         eval = DtoDelegateEquals(op,l->getRVal(),r->getRVal());
1519     }
1520     else if (t->ty == Tstruct)
1521     {
1522         Logger::println("struct");
1523         // when this is reached it means there is no opEquals overload.
1524         eval = DtoStructEquals(op,l,r);
1525     }
1526     else
1527     {
1528         assert(0 && "Unsupported EqualExp type");
1529     }
1530
1531     return new DImValue(type, eval);
1532 }
1533
1534 //////////////////////////////////////////////////////////////////////////////////////////
1535
1536 DValue* PostExp::toElem(IRState* p)
1537 {
1538     Logger::print("PostExp::toElem: %s @ %s\n", toChars(), type->toChars());
1539     LOG_SCOPE;
1540
1541     DValue* l = e1->toElem(p);
1542     DValue* r = e2->toElem(p);
1543
1544     LLValue* val = l->getRVal();
1545     LLValue* post = 0;
1546
1547     Type* e1type = e1->type->toBasetype();
1548     Type* e2type = e2->type->toBasetype();
1549
1550     if (e1type->isintegral())
1551     {
1552         assert(e2type->isintegral());
1553         LLValue* one = LLConstantInt::get(val->getType(), 1, !e2type->isunsigned());
1554         if (op == TOKplusplus) {
1555             post = llvm::BinaryOperator::CreateAdd(val,one,"tmp",p->scopebb());
1556         }
1557         else if (op == TOKminusminus) {
1558             post = llvm::BinaryOperator::CreateSub(val,one,"tmp",p->scopebb());
1559         }
1560     }
1561     else if (e1type->ty == Tpointer)
1562     {
1563         assert(e2type->isintegral());
1564         LLConstant* minusone = LLConstantInt::get(DtoSize_t(),(uint64_t)-1,true);
1565         LLConstant* plusone = LLConstantInt::get(DtoSize_t(),(uint64_t)1,false);
1566         LLConstant* whichone = (op == TOKplusplus) ? plusone : minusone;
1567         post = llvm::GetElementPtrInst::Create(val, whichone, "tmp", p->scopebb());
1568     }
1569     else if (e1type->isfloating())
1570     {
1571         assert(e2type->isfloating());
1572         LLValue* one = DtoConstFP(e1type, 1.0);
1573         if (op == TOKplusplus) {
1574             post = llvm::BinaryOperator::CreateAdd(val,one,"tmp",p->scopebb());
1575         }
1576         else if (op == TOKminusminus) {
1577             post = llvm::BinaryOperator::CreateSub(val,one,"tmp",p->scopebb());
1578         }
1579     }
1580     else
1581     assert(post);
1582
1583     DtoStore(post,l->getLVal());
1584
1585     return new DImValue(type,val);
1586 }
1587
1588 //////////////////////////////////////////////////////////////////////////////////////////
1589
1590 DValue* NewExp::toElem(IRState* p)
1591 {
1592     Logger::print("NewExp::toElem: %s @ %s\n", toChars(), type->toChars());
1593     LOG_SCOPE;
1594
1595     assert(newtype);
1596     Type* ntype = newtype->toBasetype();
1597
1598     // new class
1599     if (ntype->ty == Tclass) {
1600         Logger::println("new class");
1601         return DtoNewClass(loc, (TypeClass*)ntype, this);
1602     }
1603     // new dynamic array
1604     else if (ntype->ty == Tarray)
1605     {
1606         Logger::println("new dynamic array: %s", newtype->toChars());
1607         // get dim
1608         assert(arguments);
1609         assert(arguments->dim >= 1);
1610         if (arguments->dim == 1)
1611         {
1612             DValue* sz = ((Expression*)arguments->data[0])->toElem(p);
1613             // allocate & init
1614             return DtoNewDynArray(loc, newtype, sz, true);
1615         }
1616         else
1617         {
1618             size_t ndims = arguments->dim;
1619             std::vector<DValue*> dims(ndims);
1620             for (size_t i=0; i<ndims; ++i)
1621                 dims[i] = ((Expression*)arguments->data[i])->toElem(p);
1622             return DtoNewMulDimDynArray(loc, newtype, &dims[0], ndims, true);
1623         }
1624     }
1625     // new static array
1626     else if (ntype->ty == Tsarray)
1627     {
1628         assert(0);
1629     }
1630     // new struct
1631     else if (ntype->ty == Tstruct)
1632     {
1633         Logger::println("new struct on heap: %s\n", newtype->toChars());
1634         // allocate
1635         LLValue* mem = DtoNew(newtype);
1636         // init
1637         TypeStruct* ts = (TypeStruct*)ntype;
1638         if (ts->isZeroInit(ts->sym->loc)) {
1639             DtoAggrZeroInit(mem);
1640         }
1641         else {
1642             assert(ts->sym);
1643             ts->sym->codegen(Type::sir);
1644             DtoAggrCopy(mem, ts->sym->ir.irStruct->getInitSymbol());
1645         }
1646         return new DImValue(type, mem);
1647     }
1648     // new basic type
1649     else
1650     {
1651         // allocate
1652         LLValue* mem = DtoNew(newtype);
1653         DVarValue tmpvar(newtype, mem);
1654
1655         // default initialize
1656         // static arrays never appear here, so using the defaultInit is ok!
1657         Expression* exp = newtype->defaultInit(loc);
1658         DValue* iv = exp->toElem(gIR);
1659         DtoAssign(loc, &tmpvar, iv);
1660
1661         // return as pointer-to
1662         return new DImValue(type, mem);
1663     }
1664
1665     assert(0);
1666 }
1667
1668 //////////////////////////////////////////////////////////////////////////////////////////
1669
1670 DValue* DeleteExp::toElem(IRState* p)
1671 {
1672     Logger::print("DeleteExp::toElem: %s @ %s\n", toChars(), type->toChars());
1673     LOG_SCOPE;
1674
1675     DValue* dval = e1->toElem(p);
1676     Type* et = e1->type->toBasetype();
1677
1678     // simple pointer
1679     if (et->ty == Tpointer)
1680     {
1681         LLValue* rval = dval->getRVal();
1682         DtoDeleteMemory(rval);
1683         if (dval->isVar())
1684             DtoStore(LLConstant::getNullValue(rval->getType()), dval->getLVal());
1685     }
1686     // class
1687     else if (et->ty == Tclass)
1688     {
1689         bool onstack = false;
1690         TypeClass* tc = (TypeClass*)et;
1691         if (tc->sym->isInterfaceDeclaration())
1692         {
1693             DtoDeleteInterface(dval->getRVal());
1694             onstack = true;
1695         }
1696         else if (DVarValue* vv = dval->isVar()) {
1697             if (vv->var && vv->var->onstack) {
1698                 DtoFinalizeClass(dval->getRVal());
1699                 onstack = true;
1700             }
1701         }
1702         if (!onstack) {
1703             LLValue* rval = dval->getRVal();
1704             DtoDeleteClass(rval);
1705         }
1706         if (dval->isVar()) {
1707             LLValue* lval = dval->getLVal();
1708             DtoStore(LLConstant::getNullValue(lval->getType()->getContainedType(0)), lval);
1709         }
1710     }
1711     // dyn array
1712     else if (et->ty == Tarray)
1713     {
1714         DtoDeleteArray(dval);
1715         if (dval->isLVal())
1716             DtoSetArrayToNull(dval->getLVal());
1717     }
1718     // unknown/invalid
1719     else
1720     {
1721         assert(0 && "invalid delete");
1722     }
1723
1724     // no value to return
1725     return NULL;
1726 }
1727
1728 //////////////////////////////////////////////////////////////////////////////////////////
1729
1730 DValue* ArrayLengthExp::toElem(IRState* p)
1731 {
1732     Logger::print("ArrayLengthExp::toElem: %s @ %s\n", toChars(), type->toChars());
1733     LOG_SCOPE;
1734
1735     DValue* u = e1->toElem(p);
1736     return new DImValue(type, DtoArrayLen(u));
1737 }
1738
1739 //////////////////////////////////////////////////////////////////////////////////////////
1740
1741 DValue* AssertExp::toElem(IRState* p)
1742 {
1743     Logger::print("AssertExp::toElem: %s\n", toChars());
1744     LOG_SCOPE;
1745
1746     if(!global.params.useAssert)
1747         return NULL;
1748
1749     // condition
1750     DValue* cond;
1751     Type* condty;
1752
1753     // special case for dmd generated assert(this); when not in -release mode
1754     if (e1->op == TOKthis && ((ThisExp*)e1)->var == NULL)
1755     {
1756         LLValue* thisarg = p->func()->thisArg;
1757         assert(thisarg && "null thisarg, but we're in assert(this) exp;");
1758         LLValue* thisptr = DtoLoad(p->func()->thisArg);
1759         condty = e1->type->toBasetype();
1760         cond = new DImValue(condty, thisptr);
1761     }
1762     else
1763     {
1764         cond = e1->toElem(p);
1765         condty = e1->type->toBasetype();
1766     }
1767
1768     InvariantDeclaration* invdecl;
1769
1770     // class invariants
1771     if(
1772         global.params.useInvariants &&
1773         condty->ty == Tclass &&
1774         !((TypeClass*)condty)->sym->isInterfaceDeclaration())
1775     {
1776         Logger::println("calling class invariant");
1777         llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_invariant");
1778         LLValue* arg = DtoBitCast(cond->getRVal(), fn->getFunctionType()->getParamType(0));
1779         gIR->CreateCallOrInvoke(fn, arg);
1780     }
1781     // struct invariants
1782     else if(
1783         global.params.useInvariants &&
1784         condty->ty == Tpointer && condty->nextOf()->ty == Tstruct &&
1785         (invdecl = ((TypeStruct*)condty->nextOf())->sym->inv) != NULL)
1786     {
1787         Logger::print("calling struct invariant");
1788         DFuncValue invfunc(invdecl, invdecl->ir.irFunc->func, cond->getRVal());
1789         DtoCallFunction(loc, NULL, &invfunc, NULL);
1790     }
1791     else
1792     {
1793         // create basic blocks
1794         llvm::BasicBlock* oldend = p->scopeend();
1795         llvm::BasicBlock* assertbb = llvm::BasicBlock::Create(gIR->context(), "assert", p->topfunc(), oldend);
1796         llvm::BasicBlock* endbb = llvm::BasicBlock::Create(gIR->context(), "noassert", p->topfunc(), oldend);
1797
1798         // test condition
1799         LLValue* condval = DtoCast(loc, cond, Type::tbool)->getRVal();
1800
1801         // branch
1802         llvm::BranchInst::Create(endbb, assertbb, condval, p->scopebb());
1803
1804         // call assert runtime functions
1805         p->scope() = IRScope(assertbb,endbb);
1806         DtoAssert(p->func()->decl->getModule(), loc, msg ? msg->toElem(p) : NULL);
1807
1808         // rewrite the scope
1809         p->scope() = IRScope(endbb,oldend);
1810     }
1811
1812     // no meaningful return value
1813     return NULL;
1814 }
1815
1816 //////////////////////////////////////////////////////////////////////////////////////////
1817
1818 DValue* NotExp::toElem(IRState* p)
1819 {
1820     Logger::print("NotExp::toElem: %s @ %s\n", toChars(), type->toChars());
1821     LOG_SCOPE;
1822
1823     DValue* u = e1->toElem(p);
1824
1825     LLValue* b = DtoCast(loc, u, Type::tbool)->getRVal();
1826
1827     LLConstant* zero = DtoConstBool(false);
1828     b = p->ir->CreateICmpEQ(b,zero);
1829
1830     return new DImValue(type, b);
1831 }
1832
1833 //////////////////////////////////////////////////////////////////////////////////////////
1834
1835 DValue* AndAndExp::toElem(IRState* p)
1836 {
1837     Logger::print("AndAndExp::toElem: %s @ %s\n", toChars(), type->toChars());
1838     LOG_SCOPE;
1839
1840     DValue* u = e1->toElem(p);
1841
1842     llvm::BasicBlock* oldend = p->scopeend();
1843     llvm::BasicBlock* andand = llvm::BasicBlock::Create(gIR->context(), "andand", gIR->topfunc(), oldend);
1844     llvm::BasicBlock* andandend = llvm::BasicBlock::Create(gIR->context(), "andandend", gIR->topfunc(), oldend);
1845
1846     LLValue* ubool = DtoCast(loc, u, Type::tbool)->getRVal();
1847
1848     llvm::BasicBlock* oldblock = p->scopebb();
1849     llvm::BranchInst::Create(andand,andandend,ubool,p->scopebb());
1850
1851     p->scope() = IRScope(andand, andandend);
1852     DValue* v = e2->toElem(p);
1853
1854     LLValue* vbool = 0;
1855     if (!v->isFunc() && v->getType() != Type::tvoid)
1856     {
1857         vbool = DtoCast(loc, v, Type::tbool)->getRVal();
1858     }
1859
1860     llvm::BasicBlock* newblock = p->scopebb();
1861     llvm::BranchInst::Create(andandend,p->scopebb());
1862     p->scope() = IRScope(andandend, oldend);
1863
1864     LLValue* resval = 0;
1865     if (ubool == vbool || !vbool) {
1866         // No need to create a PHI node.
1867         resval = ubool;
1868     } else {
1869         llvm::PHINode* phi = p->ir->CreatePHI(LLType::getInt1Ty(gIR->context()), "andandval");
1870         // If we jumped over evaluation of the right-hand side,
1871         // the result is false. Otherwise it's the value of the right-hand side.
1872         phi->addIncoming(LLConstantInt::getFalse(gIR->context()), oldblock);
1873         phi->addIncoming(vbool, newblock);
1874         resval = phi;
1875     }
1876
1877     return new DImValue(type, resval);
1878 }
1879
1880 //////////////////////////////////////////////////////////////////////////////////////////
1881
1882 DValue* OrOrExp::toElem(IRState* p)
1883 {
1884     Logger::print("OrOrExp::toElem: %s @ %s\n", toChars(), type->toChars());
1885     LOG_SCOPE;
1886
1887     DValue* u = e1->toElem(p);
1888
1889     llvm::BasicBlock* oldend = p->scopeend();
1890     llvm::BasicBlock* oror = llvm::BasicBlock::Create(gIR->context(), "oror", gIR->topfunc(), oldend);
1891     llvm::BasicBlock* ororend = llvm::BasicBlock::Create(gIR->context(), "ororend", gIR->topfunc(), oldend);
1892
1893     LLValue* ubool = DtoCast(loc, u, Type::tbool)->getRVal();
1894
1895     llvm::BasicBlock* oldblock = p->scopebb();
1896     llvm::BranchInst::Create(ororend,oror,ubool,p->scopebb());
1897
1898     p->scope() = IRScope(oror, ororend);
1899     DValue* v = e2->toElem(p);
1900
1901     LLValue* vbool = 0;
1902     if (!v->isFunc() && v->getType() != Type::tvoid)
1903     {
1904         vbool = DtoCast(loc, v, Type::tbool)->getRVal();
1905     }
1906
1907     llvm::BasicBlock* newblock = p->scopebb();
1908     llvm::BranchInst::Create(ororend,p->scopebb());
1909     p->scope() = IRScope(ororend, oldend);
1910
1911     LLValue* resval = 0;
1912     if (ubool == vbool || !vbool) {
1913         // No need to create a PHI node.
1914         resval = ubool;
1915     } else {
1916         llvm::PHINode* phi = p->ir->CreatePHI(LLType::getInt1Ty(gIR->context()), "ororval");
1917         // If we jumped over evaluation of the right-hand side,
1918         // the result is true. Otherwise, it's the value of the right-hand side.
1919         phi->addIncoming(LLConstantInt::getTrue(gIR->context()), oldblock);
1920         phi->addIncoming(vbool, newblock);
1921         resval = phi;
1922     }
1923
1924     return new DImValue(type, resval);
1925 }
1926
1927 //////////////////////////////////////////////////////////////////////////////////////////
1928
1929 #define BinBitExp(X,Y) \
1930 DValue* X##Exp::toElem(IRState* p) \
1931 { \
1932     Logger::print("%sExp::toElem: %s @ %s\n", #X, toChars(), type->toChars()); \
1933     LOG_SCOPE; \
1934     DValue* u = e1->toElem(p); \
1935     DValue* v = e2->toElem(p); \
1936     errorOnIllegalArrayOp(this, e1, e2); \
1937     LLValue* x = llvm::BinaryOperator::Create(llvm::Instruction::Y, u->getRVal(), v->getRVal(), "tmp", p->scopebb()); \
1938     return new DImValue(type, x); \
1939 }
1940
1941 BinBitExp(And,And);
1942 BinBitExp(Or,Or);
1943 BinBitExp(Xor,Xor);
1944 BinBitExp(Shl,Shl);
1945 BinBitExp(Ushr,LShr);
1946
1947 DValue* ShrExp::toElem(IRState* p)
1948 {
1949     Logger::print("ShrExp::toElem: %s @ %s\n", toChars(), type->toChars());
1950     LOG_SCOPE;
1951     DValue* u = e1->toElem(p);
1952     DValue* v = e2->toElem(p);
1953     LLValue* x;
1954     if (e1->type->isunsigned())
1955         x = p->ir->CreateLShr(u->getRVal(), v->getRVal(), "tmp");
1956     else
1957         x = p->ir->CreateAShr(u->getRVal(), v->getRVal(), "tmp");
1958     return new DImValue(type, x);
1959 }
1960
1961 //////////////////////////////////////////////////////////////////////////////////////////
1962
1963 DValue* HaltExp::toElem(IRState* p)
1964 {
1965     Logger::print("HaltExp::toElem: %s\n", toChars());
1966     LOG_SCOPE;
1967
1968     // FIXME: DMD inserts a trap here... we probably should as well !?!
1969
1970 #if 1
1971     DtoAssert(p->func()->decl->getModule(), loc, NULL);
1972 #else
1973     // call the new (?) trap intrinsic
1974     p->ir->CreateCall(GET_INTRINSIC_DECL(trap),"");
1975     new llvm::UnreachableInst(p->scopebb());
1976 #endif
1977
1978     // this terminated the basicblock, start a new one
1979     // this is sensible, since someone might goto behind the assert
1980     // and prevents compiler errors if a terminator follows the assert
1981     llvm::BasicBlock* oldend = gIR->scopeend();
1982     llvm::BasicBlock* bb = llvm::BasicBlock::Create(gIR->context(), "afterhalt", p->topfunc(), oldend);
1983     p->scope() = IRScope(bb,oldend);
1984
1985     return 0;
1986 }
1987
1988 //////////////////////////////////////////////////////////////////////////////////////////
1989
1990 DValue* DelegateExp::toElem(IRState* p)
1991 {
1992     Logger::print("DelegateExp::toElem: %s @ %s\n", toChars(), type->toChars());
1993     LOG_SCOPE;
1994
1995     if(func->isStatic())
1996         error("can't take delegate of static function %s, it does not require a context ptr", func->toChars());
1997
1998     const LLPointerType* int8ptrty = getPtrToType(LLType::getInt8Ty(gIR->context()));
1999
2000     assert(type->toBasetype()->ty == Tdelegate);
2001     const LLType* dgty = DtoType(type);
2002
2003     DValue* u = e1->toElem(p);
2004     LLValue* uval;
2005     if (DFuncValue* f = u->isFunc()) {
2006         assert(f->func);
2007         LLValue* contextptr = DtoNestedContext(loc, f->func);
2008         uval = DtoBitCast(contextptr, getVoidPtrType());
2009     }
2010     else {
2011         DValue* src = u;
2012         if (ClassDeclaration* cd = u->getType()->isClassHandle())
2013         {
2014             Logger::println("context type is class handle");
2015             if (cd->isInterfaceDeclaration())
2016             {
2017                 Logger::println("context type is interface");
2018                 src = DtoCastInterfaceToObject(u, ClassDeclaration::object->type);
2019             }
2020         }
2021         uval = src->getRVal();
2022     }
2023
2024     if (Logger::enabled())
2025         Logger::cout() << "context = " << *uval << '\n';
2026
2027     LLValue* castcontext = DtoBitCast(uval, int8ptrty);
2028
2029     Logger::println("func: '%s'", func->toPrettyChars());
2030
2031     LLValue* castfptr;
2032     if (func->isVirtual() && !func->isFinal())
2033         castfptr = DtoVirtualFunctionPointer(u, func, toChars());
2034     else if (func->isAbstract())
2035         assert(0 && "TODO delegate to abstract method");
2036     else if (func->toParent()->isInterfaceDeclaration())
2037         assert(0 && "TODO delegate to interface method");
2038     else
2039     {
2040         func->codegen(Type::sir);
2041         castfptr = func->ir.irFunc->func;
2042     }
2043
2044     castfptr = DtoBitCast(castfptr, dgty->getContainedType(1));
2045
2046     return new DImValue(type, DtoAggrPair(castcontext, castfptr, ".dg"));
2047 }
2048
2049 //////////////////////////////////////////////////////////////////////////////////////////
2050
2051 DValue* IdentityExp::toElem(IRState* p)
2052 {
2053     Logger::print("IdentityExp::toElem: %s @ %s\n", toChars(), type->toChars());
2054     LOG_SCOPE;
2055
2056     DValue* l = e1->toElem(p);
2057     DValue* r = e2->toElem(p);
2058     LLValue* lv = l->getRVal();
2059     LLValue* rv = r->getRVal();
2060
2061     Type* t1 = e1->type->toBasetype();
2062
2063     // handle dynarray specially
2064     if (t1->ty == Tarray)
2065         return new DImValue(type, DtoDynArrayIs(op,l,r));
2066     // also structs
2067     else if (t1->ty == Tstruct)
2068         return new DImValue(type, DtoStructEquals(op,l,r));
2069
2070     // FIXME this stuff isn't pretty
2071     LLValue* eval = 0;
2072
2073     if (t1->ty == Tdelegate) {
2074         if (r->isNull()) {
2075             rv = NULL;
2076         }
2077         else {
2078             assert(lv->getType() == rv->getType());
2079         }
2080         eval = DtoDelegateEquals(op,lv,rv);
2081     }
2082     else if (t1->isfloating()) // includes iscomplex
2083     {
2084        eval = DtoBinNumericEquals(loc, l, r, op);
2085     }
2086     else if (t1->ty == Tpointer || t1->ty == Tclass)
2087     {
2088         if (lv->getType() != rv->getType()) {
2089             if (r->isNull())
2090                 rv = llvm::ConstantPointerNull::get(isaPointer(lv->getType()));
2091             else
2092                 rv = DtoBitCast(rv, lv->getType());
2093         }
2094         eval = (op == TOKidentity)
2095         ?   p->ir->CreateICmpEQ(lv,rv,"tmp")
2096         :   p->ir->CreateICmpNE(lv,rv,"tmp");
2097     }
2098     else {
2099         assert(lv->getType() == rv->getType());
2100         eval = (op == TOKidentity)
2101         ?   p->ir->CreateICmpEQ(lv,rv,"tmp")
2102         :   p->ir->CreateICmpNE(lv,rv,"tmp");
2103     }
2104     return new DImValue(type, eval);
2105 }
2106
2107 //////////////////////////////////////////////////////////////////////////////////////////
2108
2109 DValue* CommaExp::toElem(IRState* p)
2110 {
2111     Logger::print("CommaExp::toElem: %s @ %s\n", toChars(), type->toChars());
2112     LOG_SCOPE;
2113
2114     DValue* u = e1->toElem(p);
2115     DValue* v = e2->toElem(p);
2116     assert(e2->type == type);
2117     return v;
2118 }
2119
2120 //////////////////////////////////////////////////////////////////////////////////////////
2121
2122 DValue* CondExp::toElem(IRState* p)
2123 {
2124     Logger::print("CondExp::toElem: %s @ %s\n", toChars(), type->toChars());
2125     LOG_SCOPE;
2126
2127     Type* dtype = type->toBasetype();
2128
2129     DValue* dvv;
2130     // voids returns will need no storage
2131     if (dtype->ty != Tvoid) {
2132         // allocate a temporary for the final result. failed to come up with a better way :/
2133         LLValue* resval = DtoAlloca(dtype,"condtmp");
2134         dvv = new DVarValue(type, resval);
2135     } else {
2136         dvv = new DConstValue(type, getNullValue(DtoTypeNotVoid(dtype)));
2137     }
2138
2139     llvm::BasicBlock* oldend = p->scopeend();
2140     llvm::BasicBlock* condtrue = llvm::BasicBlock::Create(gIR->context(), "condtrue", gIR->topfunc(), oldend);
2141     llvm::BasicBlock* condfalse = llvm::BasicBlock::Create(gIR->context(), "condfalse", gIR->topfunc(), oldend);
2142     llvm::BasicBlock* condend = llvm::BasicBlock::Create(gIR->context(), "condend", gIR->topfunc(), oldend);
2143
2144     DValue* c = econd->toElem(p);
2145     LLValue* cond_val = DtoCast(loc, c, Type::tbool)->getRVal();
2146     llvm::BranchInst::Create(condtrue,condfalse,cond_val,p->scopebb());
2147
2148     p->scope() = IRScope(condtrue, condfalse);
2149     DValue* u = e1->toElem(p);
2150     if (dtype->ty != Tvoid)
2151         DtoAssign(loc, dvv, u);
2152     llvm::BranchInst::Create(condend,p->scopebb());
2153
2154     p->scope() = IRScope(condfalse, condend);
2155     DValue* v = e2->toElem(p);
2156     if (dtype->ty != Tvoid)
2157         DtoAssign(loc, dvv, v);
2158     llvm::BranchInst::Create(condend,p->scopebb());
2159
2160     p->scope() = IRScope(condend, oldend);
2161     return dvv;
2162 }
2163
2164 //////////////////////////////////////////////////////////////////////////////////////////
2165
2166 DValue* ComExp::toElem(IRState* p)
2167 {
2168     Logger::print("ComExp::toElem: %s @ %s\n", toChars(), type->toChars());
2169     LOG_SCOPE;
2170
2171     DValue* u = e1->toElem(p);
2172
2173     LLValue* value = u->getRVal();
2174     LLValue* minusone = LLConstantInt::get(value->getType(), (uint64_t)-1, true);
2175     value = llvm::BinaryOperator::Create(llvm::Instruction::Xor, value, minusone, "tmp", p->scopebb());
2176
2177     return new DImValue(type, value);
2178 }
2179
2180 //////////////////////////////////////////////////////////////////////////////////////////
2181
2182 DValue* NegExp::toElem(IRState* p)
2183 {
2184     Logger::print("NegExp::toElem: %s @ %s\n", toChars(), type->toChars());
2185     LOG_SCOPE;
2186
2187     DValue* l = e1->toElem(p);
2188
2189     if (type->iscomplex()) {
2190         return DtoComplexNeg(loc, type, l);
2191     }
2192
2193     LLValue* val = l->getRVal();
2194
2195     val = gIR->ir->CreateNeg(val,"negval");
2196     return new DImValue(type, val);
2197 }
2198
2199 //////////////////////////////////////////////////////////////////////////////////////////
2200
2201 DValue* CatExp::toElem(IRState* p)
2202 {
2203     Logger::print("CatExp::toElem: %s @ %s\n", toChars(), type->toChars());
2204     LOG_SCOPE;
2205
2206     Type* t = type->toBasetype();
2207
2208     bool arrNarr = e1->type->toBasetype() == e2->type->toBasetype();
2209
2210     // array ~ array
2211     if (arrNarr)
2212     {
2213         return DtoCatArrays(type, e1, e2);
2214     }
2215     // array ~ element
2216     // element ~ array
2217     else
2218     {
2219         return DtoCatArrayElement(type, e1, e2);
2220     }
2221 }
2222
2223 //////////////////////////////////////////////////////////////////////////////////////////
2224
2225 DValue* CatAssignExp::toElem(IRState* p)
2226 {
2227     Logger::print("CatAssignExp::toElem: %s @ %s\n", toChars(), type->toChars());
2228     LOG_SCOPE;
2229
2230     DValue* l = e1->toElem(p);
2231
2232     Type* e1type = e1->type->toBasetype();
2233     Type* elemtype = e1type->nextOf()->toBasetype();
2234     Type* e2type = e2->type->toBasetype();
2235
2236     if (e2type == elemtype) {
2237         DtoCatAssignElement(loc, e1type, l, e2);
2238     }
2239     else if (e1type == e2type) {
2240         DSliceValue* slice = DtoCatAssignArray(l,e2);
2241         DtoAssign(loc, l, slice);
2242     }
2243     else
2244         assert(0 && "only one element at a time right now");
2245
2246     return l;
2247 }
2248
2249 //////////////////////////////////////////////////////////////////////////////////////////
2250
2251 DValue* FuncExp::toElem(IRState* p)
2252 {
2253     Logger::print("FuncExp::toElem: %s @ %s\n", toChars(), type->toChars());
2254     LOG_SCOPE;
2255
2256     assert(fd);
2257
2258     if (fd->isNested()) Logger::println("nested");
2259     Logger::println("kind = %s\n", fd->kind());
2260
2261     fd->codegen(Type::sir);
2262     assert(fd->ir.irFunc->func);
2263
2264     if(fd->tok == TOKdelegate) {
2265         const LLType* dgty = DtoType(type);
2266
2267         LLValue* cval;
2268         IrFunction* irfn = p->func();
2269         if (irfn->nestedVar)
2270             cval = irfn->nestedVar;
2271         else if (irfn->nestArg)
2272             cval = irfn->nestArg;
2273         else
2274             cval = getNullPtr(getVoidPtrType());
2275         cval = DtoBitCast(cval, dgty->getContainedType(0));
2276
2277         LLValue* castfptr = DtoBitCast(fd->ir.irFunc->func, dgty->getContainedType(1));
2278
2279         return new DImValue(type, DtoAggrPair(cval, castfptr, ".func"));
2280
2281     } else if(fd->tok == TOKfunction) {
2282         return new DImValue(type, fd->ir.irFunc->func);
2283     }
2284
2285     assert(0 && "fd->tok must be TOKfunction or TOKdelegate");
2286 }
2287
2288 //////////////////////////////////////////////////////////////////////////////////////////
2289
2290 LLConstant* FuncExp::toConstElem(IRState* p)
2291 {
2292     Logger::print("FuncExp::toConstElem: %s @ %s\n", toChars(), type->toChars());
2293     LOG_SCOPE;
2294
2295     assert(fd);
2296     if (fd->tok != TOKfunction)
2297     {
2298         assert(fd->tok == TOKdelegate);
2299         error("delegate literals as constant expressions are not yet allowed");
2300     }
2301
2302     fd->codegen(Type::sir);
2303     assert(fd->ir.irFunc->func);
2304
2305     return fd->ir.irFunc->func;
2306 }
2307
2308 //////////////////////////////////////////////////////////////////////////////////////////
2309
2310 DValue* ArrayLiteralExp::toElem(IRState* p)
2311 {
2312     Logger::print("ArrayLiteralExp::toElem: %s @ %s\n", toChars(), type->toChars());
2313     LOG_SCOPE;
2314
2315     // D types
2316     Type* arrayType = type->toBasetype();
2317     Type* elemType = arrayType->nextOf()->toBasetype();
2318
2319     // is dynamic ?
2320     bool dyn = (arrayType->ty == Tarray);
2321     // length
2322     size_t len = elements->dim;
2323
2324     // llvm target type
2325     const LLType* llType = DtoType(arrayType);
2326     if (Logger::enabled())
2327         Logger::cout() << (dyn?"dynamic":"static") << " array literal with length " << len << " of D type: '" << arrayType->toChars() << "' has llvm type: '" << *llType << "'\n";
2328
2329     // llvm storage type
2330     const LLType* llElemType = DtoTypeNotVoid(elemType);
2331     const LLType* llStoType = LLArrayType::get(llElemType, len);
2332     if (Logger::enabled())
2333         Logger::cout() << "llvm storage type: '" << *llStoType << "'\n";
2334
2335     // don't allocate storage for zero length dynamic array literals
2336     if (dyn && len == 0)
2337     {
2338         // dmd seems to just make them null...
2339         return new DSliceValue(type, DtoConstSize_t(0), getNullPtr(getPtrToType(llElemType)));
2340     }
2341
2342     // dst pointer
2343     LLValue* dstMem;
2344     DSliceValue* dynSlice = NULL;
2345     if(dyn)
2346     {
2347         dynSlice = DtoNewDynArray(loc, arrayType, new DConstValue(Type::tsize_t, DtoConstSize_t(len)), false);
2348         dstMem = dynSlice->ptr;
2349     }
2350     else
2351         dstMem = DtoRawAlloca(llStoType, 0, "arrayliteral");
2352
2353     // store elements
2354     for (size_t i=0; i<len; ++i)
2355     {
2356         Expression* expr = (Expression*)elements->data[i];
2357         LLValue* elemAddr;
2358         if(dyn)
2359             elemAddr = DtoGEPi1(dstMem, i, "tmp", p->scopebb());
2360         else
2361             elemAddr = DtoGEPi(dstMem,0,i,"tmp",p->scopebb());
2362
2363         // emulate assignment
2364         DVarValue* vv = new DVarValue(expr->type, elemAddr);
2365         DValue* e = expr->toElem(p);
2366         DtoAssign(loc, vv, e);
2367     }
2368
2369     // return storage directly ?
2370     if (!dyn)
2371         return new DImValue(type, dstMem);
2372
2373     // return slice
2374     return dynSlice;
2375 }
2376
2377 //////////////////////////////////////////////////////////////////////////////////////////
2378
2379 LLConstant* ArrayLiteralExp::toConstElem(IRState* p)
2380 {
2381     Logger::print("ArrayLiteralExp::toConstElem: %s @ %s\n", toChars(), type->toChars());
2382     LOG_SCOPE;
2383
2384     // extract D types
2385     Type* bt = type->toBasetype();
2386     Type* elemt = bt->nextOf();
2387
2388     // build llvm array type
2389     const LLArrayType* arrtype = LLArrayType::get(DtoTypeNotVoid(elemt), elements->dim);
2390
2391     // dynamic arrays can occur here as well ...
2392     bool dyn = (bt->ty == Tarray);
2393
2394     // build the initializer
2395     std::vector<LLConstant*> vals(elements->dim, NULL);
2396     for (unsigned i=0; i<elements->dim; ++i)
2397     {
2398         Expression* expr = (Expression*)elements->data[i];
2399         vals[i] = expr->toConstElem(p);
2400     }
2401
2402     // build the constant array initializer
2403     LLConstant* initval = LLConstantArray::get(arrtype, vals);
2404
2405     // if static array, we're done
2406     if (!dyn)
2407         return initval;
2408
2409     // for dynamic arrays we need to put the initializer in a global, and build a constant dynamic array reference with the .ptr field pointing into this global
2410     // Important: don't make the global constant, since this const initializer might
2411     // be used as an initializer for a static T[] - where modifying contents is allowed.
2412     LLConstant* globalstore = new LLGlobalVariable(*gIR->module, arrtype, false, LLGlobalValue::InternalLinkage, initval, ".dynarrayStorage");
2413     LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) };
2414     LLConstant* globalstorePtr = llvm::ConstantExpr::getGetElementPtr(globalstore, idxs, 2);
2415
2416     return DtoConstSlice(DtoConstSize_t(elements->dim), globalstorePtr);
2417 }
2418
2419 //////////////////////////////////////////////////////////////////////////////////////////
2420
2421 extern LLConstant* get_default_initializer(VarDeclaration* vd, Initializer* init);
2422
2423 static LLValue* write_zeroes(LLValue* mem, unsigned start, unsigned end) {
2424     mem = DtoBitCast(mem, getVoidPtrType());
2425     LLValue* gep = DtoGEPi1(mem, start, ".padding");
2426     DtoMemSetZero(gep, DtoConstSize_t(end - start));
2427     return mem;
2428 }
2429
2430 DValue* StructLiteralExp::toElem(IRState* p)
2431 {
2432     Logger::print("StructLiteralExp::toElem: %s @ %s\n", toChars(), type->toChars());
2433     LOG_SCOPE;
2434
2435     // make sure the struct is fully resolved
2436     sd->codegen(Type::sir);
2437
2438     // alloca a stack slot
2439     LLValue* mem = DtoRawAlloca(DtoType(type), 0, ".structliteral");
2440
2441     // ready elements data
2442     assert(elements && "struct literal has null elements");
2443     size_t nexprs = elements->dim;
2444     Expression** exprs = (Expression**)elements->data;
2445
2446     LLValue* voidptr = mem;
2447     unsigned offset = 0;
2448
2449     // go through fields
2450     ArrayIter<VarDeclaration> it(sd->fields);
2451     for (; !it.done(); it.next())
2452     {
2453         VarDeclaration* vd = it.get();
2454
2455         // don't re-initialize unions
2456         if (vd->offset < offset)
2457         {
2458             IF_LOG Logger::println("skipping field: %s %s (+%u)", vd->type->toChars(), vd->toChars(), vd->offset);
2459             continue;
2460         }
2461         // initialize any padding so struct comparisons work
2462         if (vd->offset != offset)
2463             voidptr = write_zeroes(voidptr, offset, vd->offset);
2464         offset = vd->offset + vd->type->size();
2465
2466         IF_LOG Logger::println("initializing field: %s %s (+%u)", vd->type->toChars(), vd->toChars(), vd->offset);
2467
2468         // get initializer
2469         Expression* expr = (it.index < nexprs) ? exprs[it.index] : NULL;
2470         IF_LOG Logger::println("expr: %p", expr);
2471         DValue* val;
2472         DConstValue cv(vd->type, NULL); // Only used in one branch; value is set beforehand
2473         if (expr)
2474         {
2475             IF_LOG Logger::println("expr %zu = %s", it.index, expr->toChars());
2476             val = expr->toElem(gIR);
2477         }
2478         else
2479         {
2480             IF_LOG Logger::println("using default initializer");
2481             cv.c = get_default_initializer(vd, NULL);
2482             val = &cv;
2483         }
2484
2485         // get a pointer to this field
2486         DVarValue field(vd->type, vd, DtoIndexStruct(mem, sd, vd));
2487
2488         // store the initializer there
2489         DtoAssign(loc, &field, val);
2490     }
2491     // initialize trailing padding
2492     if (sd->structsize != offset)
2493         write_zeroes(voidptr, offset, sd->structsize);
2494
2495     // return as a var
2496     return new DVarValue(type, mem);
2497 }
2498
2499 //////////////////////////////////////////////////////////////////////////////////////////
2500
2501 LLConstant* StructLiteralExp::toConstElem(IRState* p)
2502 {
2503     Logger::print("StructLiteralExp::toConstElem: %s @ %s\n", toChars(), type->toChars());
2504     LOG_SCOPE;
2505
2506     // make sure the struct is resolved
2507     sd->codegen(Type::sir);
2508
2509     // get inits
2510     std::vector<LLValue*> inits(sd->fields.dim, NULL);
2511
2512     size_t nexprs = elements->dim;;
2513     Expression** exprs = (Expression**)elements->data;
2514
2515     for (size_t i = 0; i < nexprs; i++)
2516         if (exprs[i])
2517             inits[i] = exprs[i]->toConstElem(p);
2518
2519     // vector of values to build aggregate from
2520     std::vector<LLValue*> values = DtoStructLiteralValues(sd, inits);
2521
2522     // we know those values are constants.. cast them
2523     std::vector<LLConstant*> constvals(values.size(), NULL);
2524     for (size_t i = 0; i < values.size(); ++i)
2525         constvals[i] = llvm::cast<LLConstant>(values[i]);
2526
2527     // return constant struct
2528     return LLConstantStruct::get(gIR->context(), constvals, sd->ir.irStruct->packed);
2529 }
2530
2531 //////////////////////////////////////////////////////////////////////////////////////////
2532
2533 DValue* InExp::toElem(IRState* p)
2534 {
2535     Logger::print("InExp::toElem: %s @ %s\n", toChars(), type->toChars());
2536     LOG_SCOPE;
2537
2538     DValue* key = e1->toElem(p);
2539     DValue* aa = e2->toElem(p);
2540
2541     return DtoAAIn(loc, type, aa, key);
2542 }
2543
2544 DValue* RemoveExp::toElem(IRState* p)
2545 {
2546     Logger::print("RemoveExp::toElem: %s\n", toChars());
2547     LOG_SCOPE;
2548
2549     DValue* aa = e1->toElem(p);
2550     DValue* key = e2->toElem(p);
2551
2552     DtoAARemove(loc, aa, key);
2553
2554     return NULL; // does not produce anything useful
2555 }
2556
2557 //////////////////////////////////////////////////////////////////////////////////////////
2558
2559 DValue* AssocArrayLiteralExp::toElem(IRState* p)
2560 {
2561     Logger::print("AssocArrayLiteralExp::toElem: %s @ %s\n", toChars(), type->toChars());
2562     LOG_SCOPE;
2563
2564     assert(keys);
2565     assert(values);
2566     assert(keys->dim == values->dim);
2567
2568     Type* aatype = type->toBasetype();
2569     Type* vtype = aatype->nextOf();
2570
2571     // it should be possible to avoid the temporary in some cases
2572     LLValue* tmp = DtoAlloca(type,"aaliteral");
2573     DValue* aa = new DVarValue(type, tmp);
2574     DtoStore(LLConstant::getNullValue(DtoType(type)), tmp);
2575
2576     const size_t n = keys->dim;
2577     for (size_t i=0; i<n; ++i)
2578     {
2579         Expression* ekey = (Expression*)keys->data[i];
2580         Expression* eval = (Expression*)values->data[i];
2581
2582         Logger::println("(%zu) aa[%s] = %s", i, ekey->toChars(), eval->toChars());
2583
2584         // index
2585         DValue* key = ekey->toElem(p);
2586         DValue* mem = DtoAAIndex(loc, vtype, aa, key, true);
2587
2588         // store
2589         DValue* val = eval->toElem(p);
2590         DtoAssign(loc, mem, val);
2591     }
2592
2593     return aa;
2594 }
2595
2596 //////////////////////////////////////////////////////////////////////////////////////////
2597
2598 DValue* GEPExp::toElem(IRState* p)
2599 {
2600     // this should be good enough for now!
2601     DValue* val = e1->toElem(p);
2602     assert(val->isLVal());
2603     LLValue* v = DtoGEPi(val->getLVal(), 0, index);
2604     return new DVarValue(type, DtoBitCast(v, getPtrToType(DtoType(type))));
2605 }
2606
2607 //////////////////////////////////////////////////////////////////////////////////////////
2608
2609 DValue* BoolExp::toElem(IRState* p)
2610 {
2611     return new DImValue(type, DtoCast(loc, e1->toElem(p), Type::tbool)->getRVal());
2612 }
2613
2614 //////////////////////////////////////////////////////////////////////////////////////////
2615
2616 DValue* DotTypeExp::toElem(IRState* p)
2617 {
2618     Type* t = sym->getType();
2619     assert(t);
2620     return e1->toElem(p);
2621 }
2622
2623 //////////////////////////////////////////////////////////////////////////////////////////
2624
2625 DValue* TypeExp::toElem(IRState *p)
2626 {
2627     error("type %s is not an expression", toChars());
2628     //TODO: Improve error handling. DMD just returns some value here and hopes
2629     // some more sensible error messages will be triggered.
2630     fatal();
2631     return NULL;
2632 }
2633
2634 //////////////////////////////////////////////////////////////////////////////////////////
2635
2636 #define STUB(x) DValue *x::toElem(IRState * p) {error("Exp type "#x" not implemented: %s", toChars()); fatal(); return 0; }
2637 STUB(Expression);
2638 STUB(ScopeExp);
2639 STUB(TupleExp);
2640
2641 #if DMDV2
2642 STUB(SymbolExp);
2643 #endif
2644
2645 #define CONSTSTUB(x) LLConstant* x::toConstElem(IRState * p) { \
2646     error("expression '%s' is not a constant", toChars()); \
2647     fatal(); \
2648     return NULL; \
2649 }
2650 CONSTSTUB(Expression);
2651 CONSTSTUB(GEPExp);
2652 CONSTSTUB(SliceExp);
2653 CONSTSTUB(IndexExp);
2654 CONSTSTUB(AssocArrayLiteralExp);
2655
2656 //////////////////////////////////////////////////////////////////////////////////////////
2657
2658 void obj_includelib(const char* lib)
2659 {
2660     size_t n = strlen(lib)+3;
2661     char *arg = (char *)mem.malloc(n);
2662     strcpy(arg, "-l");
2663     strncat(arg, lib, n);
2664     global.params.linkswitches->push(arg);
2665 }
2666
2667 void backend_init()
2668 {
2669     // LLVM_D_InitRuntime is done in Module::genLLVMModule
2670     // since it requires the semantic pass to be done
2671 }
2672
2673 void backend_term()
2674 {
2675     LLVM_D_FreeRuntime();
2676     llvm::llvm_shutdown();
2677 }
Note: See TracBrowser for help on using the browser.
Copyright © 2008, LDC Development Team.