1
|
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 <sstream>
|
|
12 #include <fstream>
|
|
13 #include <iostream>
|
|
14
|
|
15 #include "llvm/Type.h"
|
|
16 #include "llvm/DerivedTypes.h"
|
|
17 #include "llvm/Constants.h"
|
|
18 #include "llvm/Instructions.h"
|
|
19 #include "llvm/IntrinsicInst.h"
|
|
20 #include "llvm/CallingConv.h"
|
|
21
|
|
22 #include "total.h"
|
|
23 #include "init.h"
|
|
24 #include "symbol.h"
|
|
25 #include "mtype.h"
|
|
26 #include "hdrgen.h"
|
|
27 #include "irstate.h"
|
|
28 #include "elem.h"
|
|
29 #include "port.h"
|
|
30 #include "logger.h"
|
|
31
|
|
32 #include "tollvm.h"
|
|
33 #include "runtime.h"
|
|
34
|
|
35 //////////////////////////////////////////////////////////////////////////////////////////
|
|
36
|
|
37 elem* DeclarationExp::toElem(IRState* p)
|
|
38 {
|
|
39 Logger::print("DeclarationExp::toElem: %s | T=%s\n", toChars(), type->toChars());
|
|
40 LOG_SCOPE;
|
|
41 elem* e = new elem;
|
|
42
|
|
43 // variable declaration
|
|
44 if (VarDeclaration* vd = declaration->isVarDeclaration())
|
|
45 {
|
|
46 Logger::println("VarDeclaration");
|
|
47
|
|
48 // handle const
|
|
49 // TODO probably not correct
|
|
50 bool isconst = (vd->storage_class & STCconst) != 0;
|
|
51
|
|
52 // allocate storage on the stack
|
|
53 Logger::println("vdtype = %s", vd->type->toChars());
|
|
54 const llvm::Type* lltype = LLVM_DtoType(vd->type);
|
|
55 llvm::AllocaInst* allocainst = new llvm::AllocaInst(lltype, vd->toChars(), p->topallocapoint());
|
|
56 //allocainst->setAlignment(vd->type->alignsize()); // TODO
|
|
57 vd->llvmValue = allocainst;
|
|
58 // e->val = really needed??
|
|
59
|
|
60 LLVM_DtoInitializer(vd->type, vd->init);
|
|
61 }
|
|
62 // struct declaration
|
|
63 else if (StructDeclaration* s = declaration->isStructDeclaration())
|
|
64 {
|
|
65 Logger::println("StructDeclaration");
|
|
66 s->toObjFile();
|
|
67 }
|
|
68 // unsupported declaration
|
|
69 else
|
|
70 {
|
|
71 error("Only Var/Struct-Declaration is supported for DeclarationExp");
|
|
72 fatal();
|
|
73 }
|
|
74 return e;
|
|
75 }
|
|
76
|
|
77 //////////////////////////////////////////////////////////////////////////////////////////
|
|
78
|
|
79 elem* VarExp::toElem(IRState* p)
|
|
80 {
|
|
81 Logger::print("VarExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
82 LOG_SCOPE;
|
|
83
|
|
84 elem* e = new elem;
|
|
85
|
|
86 assert(var);
|
|
87 if (VarDeclaration* vd = var->isVarDeclaration())
|
|
88 {
|
|
89 Logger::println("VarDeclaration");
|
|
90
|
|
91 if (TypeInfoDeclaration* tid = vd->isTypeInfoDeclaration())
|
|
92 {
|
|
93 Logger::println("TypeInfoDeclaration");
|
|
94 }
|
|
95
|
|
96 // this must be a dollar expression or some other magic value
|
|
97 if (!vd->llvmValue)
|
|
98 {
|
|
99 // dollar
|
|
100 if (!p->arrays.empty())
|
|
101 {
|
|
102 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
103 llvm::Value* tmp = new llvm::GetElementPtrInst(p->arrays.back(),zero,zero,"tmp",p->scopebb());
|
|
104 e->val = new llvm::LoadInst(tmp,"tmp",p->scopebb());
|
|
105 e->type = elem::VAL;
|
|
106 }
|
|
107 // magic
|
|
108 else
|
|
109 {
|
|
110 if (TypeInfoDeclaration* tid = vd->isTypeInfoDeclaration())
|
|
111 {
|
|
112 tid->toObjFile();
|
|
113 e->mem = tid->llvmValue;
|
|
114 e->type = elem::VAR;
|
|
115 }
|
|
116 else
|
|
117 assert(0 && "only magic supported is typeinfo");
|
|
118 }
|
|
119 return e;
|
|
120 }
|
|
121
|
|
122 // function parameter
|
|
123 if (vd->storage_class & STCparameter) {
|
|
124 Logger::println("function param");
|
|
125 if (vd->storage_class & (STCref | STCout)) {
|
|
126 e->mem = vd->llvmValue;
|
|
127 e->type = elem::VAR;
|
|
128 }
|
|
129 else {
|
|
130 if (vd->type->ty == Tstruct || vd->type->ty == Tdelegate || vd->type->ty == Tarray) {
|
|
131 e->mem = vd->llvmValue;
|
|
132 e->type = elem::VAR;
|
|
133 }
|
|
134 else {
|
|
135 e->val = vd->llvmValue;
|
|
136 e->type = elem::VAL;
|
|
137 }
|
|
138 }
|
|
139 }
|
|
140 else {
|
|
141 e->mem = vd->llvmValue;
|
|
142 //e->mem->setName(toChars());
|
|
143 e->vardecl = vd;
|
|
144 e->type = elem::VAR;
|
|
145 }
|
|
146 }
|
|
147 else if (FuncDeclaration* fdecl = var->isFuncDeclaration())
|
|
148 {
|
|
149 Logger::println("FuncDeclaration");
|
|
150 if (fdecl->llvmValue == 0) {
|
|
151 fdecl->toObjFile();
|
|
152 }
|
|
153 e->val = fdecl->llvmValue;
|
|
154 e->type = elem::FUNC;
|
|
155 e->funcdecl = fdecl;
|
|
156 }
|
|
157 else if (SymbolDeclaration* sdecl = var->isSymbolDeclaration())
|
|
158 {
|
|
159 // this seems to be the static initialiser for structs
|
|
160 Logger::print("Sym: type=%s\n", sdecl->type->toChars());
|
|
161 assert(sdecl->type->ty == Tstruct);
|
|
162 //assert(sdecl->llvmInitZ);
|
|
163 //e->val = sdecl->llvmInitZ;
|
|
164 TypeStruct* ts = (TypeStruct*)sdecl->type;
|
|
165 e->mem = ts->llvmInit;
|
|
166 assert(e->mem);
|
|
167 e->type = elem::VAR;
|
|
168 }
|
|
169 else
|
|
170 {
|
|
171 assert(0 && "Unimplemented VarExp type");
|
|
172 }
|
|
173
|
|
174 assert(e->mem || e->val);
|
|
175 return e;
|
|
176 }
|
|
177
|
|
178 //////////////////////////////////////////////////////////////////////////////////////////
|
|
179
|
|
180 elem* IntegerExp::toElem(IRState* p)
|
|
181 {
|
|
182 Logger::print("IntegerExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
183 LOG_SCOPE;
|
|
184 elem* e = new elem;
|
|
185 const llvm::Type* t = LLVM_DtoType(type);
|
|
186 if (llvm::isa<llvm::PointerType>(t)) {
|
|
187 llvm::Constant* i = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)value,false);
|
|
188 e->val = llvm::ConstantExpr::getIntToPtr(i, t);
|
|
189 }
|
|
190 else if (llvm::isa<llvm::IntegerType>(t)) {
|
|
191 e->val = llvm::ConstantInt::get(t,(uint64_t)value,!type->isunsigned());
|
|
192 }
|
|
193 else {
|
|
194 assert(0);
|
|
195 }
|
|
196 e->type = elem::CONST;
|
|
197 return e;
|
|
198 }
|
|
199
|
|
200 //////////////////////////////////////////////////////////////////////////////////////////
|
|
201
|
|
202 elem* RealExp::toElem(IRState* p)
|
|
203 {
|
|
204 Logger::print("RealExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
205 LOG_SCOPE;
|
|
206 elem* e = new elem;
|
|
207 e->val = llvm::ConstantFP::get(LLVM_DtoType(type),value);
|
|
208 e->type = elem::CONST;
|
|
209 return e;
|
|
210 }
|
|
211
|
|
212 //////////////////////////////////////////////////////////////////////////////////////////
|
|
213
|
|
214 elem* NullExp::toElem(IRState* p)
|
|
215 {
|
|
216 Logger::print("NullExp::toElem(type=%s): %s\n", type->toChars(),toChars());
|
|
217 LOG_SCOPE;
|
|
218 elem* e = new elem;
|
|
219 const llvm::Type* t = LLVM_DtoType(type);
|
|
220 if (llvm::isa<llvm::StructType>(t))
|
|
221 t = llvm::PointerType::get(t);
|
|
222 Logger::cout() << *t << '\n';
|
|
223 e->val = llvm::Constant::getNullValue(t);
|
|
224 assert(e->val);
|
|
225 Logger::cout() << *e->val << '\n';
|
|
226 e->type = elem::NUL;
|
|
227 return e;
|
|
228 }
|
|
229
|
|
230 //////////////////////////////////////////////////////////////////////////////////////////
|
|
231
|
|
232 elem* StringExp::toElem(IRState* p)
|
|
233 {
|
|
234 Logger::print("StringExp::toElem: %s\n", toChars());
|
|
235 LOG_SCOPE;
|
|
236
|
|
237 assert(type->next->ty == Tchar && "Only char is supported");
|
|
238 assert(sz == 1);
|
|
239
|
|
240 const llvm::Type* ct = LLVM_DtoType(type->next);
|
|
241 //printf("ct = %s\n", type->next->toChars());
|
|
242 const llvm::ArrayType* at = llvm::ArrayType::get(ct,len+1);
|
|
243
|
|
244 uint8_t* str = (uint8_t*)string;
|
|
245 std::string cont((char*)str, len);
|
|
246
|
|
247 llvm::Constant* _init = llvm::ConstantArray::get(cont,true);
|
|
248
|
|
249 llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::InternalLinkage;//WeakLinkage;
|
|
250 llvm::GlobalVariable* gvar = new llvm::GlobalVariable(at,true,_linkage,_init,"stringliteral",gIR->module);
|
|
251
|
|
252 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
253 llvm::Value* arrptr = new llvm::GetElementPtrInst(gvar,zero,zero,"tmp",p->scopebb());
|
|
254
|
|
255 elem* e = new elem;
|
|
256
|
|
257 if (type->ty == Tarray) {
|
|
258 llvm::Value* arr = p->toplval();
|
|
259 LLVM_DtoSetArray(arr, llvm::ConstantInt::get(LLVM_DtoSize_t(),len,false), arrptr);
|
|
260 }
|
|
261 else if (type->ty == Tpointer) {
|
|
262 e->mem = arrptr;
|
|
263 }
|
|
264 else {
|
|
265 assert(0);
|
|
266 }
|
|
267
|
|
268 e->inplace = true;
|
|
269 e->type = elem::VAL;
|
|
270
|
|
271 return e;
|
|
272 }
|
|
273
|
|
274 //////////////////////////////////////////////////////////////////////////////////////////
|
|
275
|
|
276 elem* AssignExp::toElem(IRState* p)
|
|
277 {
|
|
278 Logger::print("AssignExp::toElem: %s | %s = %s\n", toChars(), e1->type->toChars(), e2->type->toChars());
|
|
279 LOG_SCOPE;
|
|
280
|
|
281 assert(e1 && e2);
|
|
282 p->inLvalue = true;
|
|
283 elem* l = e1->toElem(p);
|
|
284 p->inLvalue = false;
|
|
285
|
|
286 p->lvals.push_back(l->mem);
|
|
287 elem* r = e2->toElem(p);
|
|
288 p->lvals.pop_back();
|
|
289
|
|
290 assert(l->mem);
|
|
291 //e->val = l->store(r->getValue());
|
|
292
|
|
293 TY e1ty = e1->type->ty;
|
|
294 TY e2ty = e2->type->ty;
|
|
295
|
|
296 elem* e = new elem;
|
|
297
|
|
298 // struct
|
|
299 if (e1ty == Tstruct) {
|
|
300 // struct + struct
|
|
301 if (e2ty == Tstruct) {
|
|
302 // struct literals do the assignment themselvs (in place)
|
|
303 if (!r->inplace) {
|
|
304 TypeStruct* ts = (TypeStruct*)e2->type;
|
|
305 assert(r->mem);
|
|
306 LLVM_DtoStructCopy(ts,l->mem,r->mem);
|
|
307 }
|
|
308 else {
|
|
309 e->inplace = true;
|
|
310 }
|
|
311 }
|
|
312 // struct + const int
|
|
313 else if (e2->type->isintegral()){
|
|
314 IntegerExp* iexp = (IntegerExp*)e2;
|
|
315 assert(iexp->value == 0 && "Only integral struct initializer allowed is zero");
|
|
316 TypeStruct* st = (TypeStruct*)e1->type;
|
|
317 LLVM_DtoStructZeroInit(st, l->mem);
|
|
318 }
|
|
319 // :x
|
|
320 else
|
|
321 assert(0 && "struct = unknown");
|
|
322 }
|
|
323 else if (e1ty == Tsarray) {
|
|
324 assert(0 && "static array = not supported");
|
|
325 }
|
|
326 else if (e1ty == Tarray) {
|
|
327 if (e2->type->isscalar() || e2->type->ty == Tclass){
|
|
328 LLVM_DtoArrayInit(l->mem, r->getValue());
|
|
329 }
|
|
330 else if (e2ty == Tarray) {
|
|
331 //new llvm::StoreInst(r->val,l->val,p->scopebb());
|
|
332 if (r->type == elem::NUL) {
|
|
333 llvm::Constant* c = llvm::cast<llvm::Constant>(r->val);
|
|
334 assert(c->isNullValue());
|
|
335 LLVM_DtoNullArray(l->mem);
|
|
336 }
|
|
337 else if (r->type == elem::SLICE) {
|
|
338 if (l->type == elem::SLICE)
|
|
339 LLVM_DtoArrayCopy(l,r);
|
|
340 else
|
|
341 LLVM_DtoSetArray(l->mem,r->arg,r->mem);
|
|
342 }
|
|
343 else {
|
|
344 // new expressions write directly to the array reference
|
|
345 // so do string literals
|
|
346 if (!r->inplace) {
|
|
347 assert(r->mem);
|
|
348 LLVM_DtoArrayAssign(l->mem, r->mem);
|
|
349 }
|
|
350 else {
|
|
351 e->inplace = true;
|
|
352 }
|
|
353 }
|
|
354 }
|
|
355 else
|
|
356 assert(0);
|
|
357 }
|
|
358 else if (e1ty == Tpointer) {
|
|
359 if (e2ty == Tpointer) {
|
|
360 llvm::Value* v = r->field ? r->mem : r->getValue();
|
|
361 Logger::cout() << "*=*: " << *v << ", " << *l->mem << '\n';
|
|
362 new llvm::StoreInst(v, l->mem, p->scopebb());
|
|
363 }
|
|
364 else
|
|
365 assert(0);
|
|
366 }
|
|
367 else if (e1ty == Tclass) {
|
|
368 if (e2ty == Tclass) {
|
|
369 llvm::Value* tmp = r->getValue();
|
|
370 Logger::cout() << "tmp: " << *tmp << ", " << *l->mem << '\n';
|
|
371 new llvm::StoreInst(tmp, l->mem, p->scopebb());
|
|
372 }
|
|
373 else
|
|
374 assert(0);
|
|
375 }
|
|
376 else if (e1ty == Tdelegate) {
|
|
377 Logger::println("Assigning to delegate");
|
|
378 if (e2ty == Tdelegate) {
|
|
379 if (r->type == elem::NUL) {
|
|
380 llvm::Constant* c = llvm::cast<llvm::Constant>(r->val);
|
|
381 if (c->isNullValue()) {
|
|
382 LLVM_DtoNullDelegate(l->mem);
|
|
383 }
|
|
384 else
|
|
385 assert(0);
|
|
386 }
|
|
387 else if (r->inplace) {
|
|
388 // do nothing
|
|
389 e->inplace = true;
|
|
390 }
|
|
391 else
|
|
392 assert(0);
|
|
393 }
|
|
394 else
|
|
395 assert(0);
|
|
396 }
|
|
397 // !struct && !array && !pointer && !class
|
|
398 else {
|
|
399 Logger::cout() << *l->mem << '\n';
|
|
400 new llvm::StoreInst(r->getValue(),l->mem,p->scopebb());
|
|
401 }
|
|
402
|
|
403 delete r;
|
|
404 delete l;
|
|
405 return e;
|
|
406 }
|
|
407
|
|
408 //////////////////////////////////////////////////////////////////////////////////////////
|
|
409
|
|
410 elem* AddExp::toElem(IRState* p)
|
|
411 {
|
|
412 Logger::print("AddExp::toElem: %s\n", toChars());
|
|
413 LOG_SCOPE;
|
|
414 elem* e = new elem;
|
|
415 elem* l = e1->toElem(p);
|
|
416 elem* r = e2->toElem(p);
|
|
417
|
|
418 if (e1->type != e2->type) {
|
|
419 if (e1->type->ty == Tpointer && e1->type->next->ty == Tstruct) {
|
|
420 assert(l->field);
|
|
421 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
422 assert(r->type == elem::CONST);
|
|
423 llvm::ConstantInt* cofs = llvm::cast<llvm::ConstantInt>(r->val);
|
|
424
|
|
425 TypeStruct* ts = (TypeStruct*)e1->type->next;
|
|
426 llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, ts->sym->offsetToIndex(cofs->getZExtValue()), false);
|
|
427
|
|
428 e->mem = new llvm::GetElementPtrInst(l->getValue(), zero, offset, "tmp", p->scopebb());
|
|
429 e->type = elem::VAR;
|
|
430 e->field = true;
|
|
431 }
|
|
432 else if (e1->type->ty == Tpointer) {
|
|
433 e->val = new llvm::GetElementPtrInst(l->getValue(), r->getValue(), "tmp", p->scopebb());
|
|
434 e->type = elem::VAR;
|
|
435 }
|
|
436 else {
|
|
437 assert(0);
|
|
438 }
|
|
439 }
|
|
440 else {
|
|
441 e->val = llvm::BinaryOperator::createAdd(l->getValue(), r->getValue(), "tmp", p->scopebb());
|
|
442 e->type = elem::VAL;
|
|
443 }
|
|
444 delete l;
|
|
445 delete r;
|
|
446 return e;
|
|
447 }
|
|
448
|
|
449 //////////////////////////////////////////////////////////////////////////////////////////
|
|
450
|
|
451 elem* AddAssignExp::toElem(IRState* p)
|
|
452 {
|
|
453 Logger::print("AddAssignExp::toElem: %s\n", toChars());
|
|
454 LOG_SCOPE;
|
|
455
|
|
456 elem* l = e1->toElem(p);
|
|
457 elem* r = e2->toElem(p);
|
|
458
|
|
459 elem* e = new elem;
|
|
460 llvm::Value* val = 0;
|
|
461 if (e1->type->ty == Tpointer) {
|
|
462 val = e->mem = new llvm::GetElementPtrInst(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
463 }
|
|
464 else {
|
|
465 val = e->val = llvm::BinaryOperator::createAdd(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
466 }
|
|
467
|
|
468 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val;
|
|
469 if (llvm::isa<llvm::PointerType>(storeVal->getType()) && storeVal->getType()->getContainedType(0) != tmp->getType())
|
|
470 {
|
|
471 tmp = LLVM_DtoPointedType(storeVal, tmp);
|
|
472 }*/
|
|
473
|
|
474 new llvm::StoreInst(val,l->mem,p->scopebb());
|
|
475 e->type = elem::VAR;
|
|
476
|
|
477 delete l;
|
|
478 delete r;
|
|
479 return e;
|
|
480 }
|
|
481
|
|
482 //////////////////////////////////////////////////////////////////////////////////////////
|
|
483
|
|
484 elem* MinExp::toElem(IRState* p)
|
|
485 {
|
|
486 Logger::print("MinExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
487 LOG_SCOPE;
|
|
488 elem* e = new elem;
|
|
489 elem* l = e1->toElem(p);
|
|
490 elem* r = e2->toElem(p);
|
|
491
|
|
492 llvm::Value* left = l->getValue();
|
|
493 if (llvm::isa<llvm::PointerType>(left->getType()))
|
|
494 left = new llvm::PtrToIntInst(left,LLVM_DtoSize_t(),"tmp",p->scopebb());
|
|
495
|
|
496 llvm::Value* right = r->getValue();
|
|
497 if (llvm::isa<llvm::PointerType>(right->getType()))
|
|
498 right = new llvm::PtrToIntInst(right,LLVM_DtoSize_t(),"tmp",p->scopebb());
|
|
499
|
|
500 e->val = llvm::BinaryOperator::createSub(left,right,"tmp",p->scopebb());
|
|
501 e->type = elem::VAL;
|
|
502
|
|
503 const llvm::Type* totype = LLVM_DtoType(type);
|
|
504 if (e->val->getType() != totype) {
|
|
505 assert(0);
|
|
506 assert(llvm::isa<llvm::PointerType>(e->val->getType()));
|
|
507 assert(llvm::isa<llvm::IntegerType>(totype));
|
|
508 e->val = new llvm::IntToPtrInst(e->val,totype,"tmp",p->scopebb());
|
|
509 }
|
|
510
|
|
511 delete l;
|
|
512 delete r;
|
|
513 return e;
|
|
514 }
|
|
515
|
|
516 //////////////////////////////////////////////////////////////////////////////////////////
|
|
517
|
|
518 elem* MinAssignExp::toElem(IRState* p)
|
|
519 {
|
|
520 Logger::print("MinAssignExp::toElem: %s\n", toChars());
|
|
521 LOG_SCOPE;
|
|
522
|
|
523 elem* l = e1->toElem(p);
|
|
524 elem* r = e2->toElem(p);
|
|
525
|
|
526 llvm::Value* tmp = 0;
|
|
527 if (e1->type->ty == Tpointer) {
|
|
528 tmp = r->getValue();
|
|
529 llvm::Value* zero = llvm::ConstantInt::get(tmp->getType(),0,false);
|
|
530 tmp = llvm::BinaryOperator::createSub(zero,tmp,"tmp",p->scopebb());
|
|
531 tmp = new llvm::GetElementPtrInst(l->getValue(),tmp,"tmp",p->scopebb());
|
|
532 }
|
|
533 else {
|
|
534 tmp = llvm::BinaryOperator::createSub(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
535 }
|
|
536
|
|
537 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val;
|
|
538 if (storeVal->getType()->getContainedType(0) != tmp->getType())
|
|
539 {
|
|
540 tmp = LLVM_DtoPointedType(storeVal, tmp);
|
|
541 }*/
|
|
542
|
|
543 new llvm::StoreInst(tmp, l->mem, p->scopebb());
|
|
544
|
|
545 delete l;
|
|
546 delete r;
|
|
547
|
|
548 elem* e = new elem;
|
|
549 e->val = tmp;
|
|
550 e->type = elem::VAR;
|
|
551 return e;
|
|
552 }
|
|
553
|
|
554 //////////////////////////////////////////////////////////////////////////////////////////
|
|
555
|
|
556 elem* MulExp::toElem(IRState* p)
|
|
557 {
|
|
558 Logger::print("MulExp::toElem: %s\n", toChars());
|
|
559 LOG_SCOPE;
|
|
560 elem* e = new elem;
|
|
561 elem* l = e1->toElem(p);
|
|
562 elem* r = e2->toElem(p);
|
|
563 llvm::Value* vl = l->getValue();
|
|
564 llvm::Value* vr = r->getValue();
|
|
565 Logger::cout() << "mul: " << *vl << ", " << *vr << '\n';
|
|
566 e->val = llvm::BinaryOperator::createMul(vl,vr,"tmp",p->scopebb());
|
|
567 e->type = elem::VAL;
|
|
568 delete l;
|
|
569 delete r;
|
|
570 return e;
|
|
571 }
|
|
572
|
|
573 //////////////////////////////////////////////////////////////////////////////////////////
|
|
574
|
|
575 elem* MulAssignExp::toElem(IRState* p)
|
|
576 {
|
|
577 Logger::print("MulAssignExp::toElem: %s\n", toChars());
|
|
578 LOG_SCOPE;
|
|
579
|
|
580 elem* l = e1->toElem(p);
|
|
581 elem* r = e2->toElem(p);
|
|
582 llvm::Value* vl = l->getValue();
|
|
583 llvm::Value* vr = r->getValue();
|
|
584 Logger::cout() << "mulassign: " << *vl << ", " << *vr << '\n';
|
|
585 llvm::Value* tmp = llvm::BinaryOperator::createMul(vl,vr,"tmp",p->scopebb());
|
|
586
|
|
587 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val;
|
|
588 if (storeVal->getType()->getContainedType(0) != tmp->getType())
|
|
589 {
|
|
590 tmp = LLVM_DtoPointedType(storeVal, tmp);
|
|
591 }*/
|
|
592
|
|
593 new llvm::StoreInst(tmp,l->mem,p->scopebb());
|
|
594
|
|
595 delete l;
|
|
596 delete r;
|
|
597
|
|
598 elem* e = new elem;
|
|
599 e->val = tmp;
|
|
600 e->type = elem::VAR;
|
|
601 return e;
|
|
602 }
|
|
603
|
|
604 //////////////////////////////////////////////////////////////////////////////////////////
|
|
605
|
|
606 elem* DivExp::toElem(IRState* p)
|
|
607 {
|
|
608 Logger::print("DivExp::toElem: %s\n", toChars());
|
|
609 LOG_SCOPE;
|
|
610 elem* e = new elem;
|
|
611 elem* l = e1->toElem(p);
|
|
612 elem* r = e2->toElem(p);
|
|
613
|
|
614 if (type->isunsigned())
|
|
615 e->val = llvm::BinaryOperator::createUDiv(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
616 else if (type->isintegral())
|
|
617 e->val = llvm::BinaryOperator::createSDiv(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
618 else if (type->isfloating())
|
|
619 e->val = llvm::BinaryOperator::createFDiv(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
620 else
|
|
621 assert(0);
|
|
622 e->type = elem::VAL;
|
|
623 delete l;
|
|
624 delete r;
|
|
625 return e;
|
|
626 }
|
|
627
|
|
628 //////////////////////////////////////////////////////////////////////////////////////////
|
|
629
|
|
630 elem* DivAssignExp::toElem(IRState* p)
|
|
631 {
|
|
632 Logger::print("DivAssignExp::toElem: %s\n", toChars());
|
|
633 LOG_SCOPE;
|
|
634
|
|
635 elem* l = e1->toElem(p);
|
|
636 elem* r = e2->toElem(p);
|
|
637
|
|
638 llvm::Value* tmp;
|
|
639 if (type->isunsigned())
|
|
640 tmp = llvm::BinaryOperator::createUDiv(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
641 else if (type->isintegral())
|
|
642 tmp = llvm::BinaryOperator::createSDiv(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
643 else if (type->isfloating())
|
|
644 tmp = llvm::BinaryOperator::createFDiv(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
645 else
|
|
646 assert(0);
|
|
647
|
|
648 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val;
|
|
649 if (storeVal->getType()->getContainedType(0) != tmp->getType())
|
|
650 {
|
|
651 tmp = LLVM_DtoPointedType(storeVal, tmp);
|
|
652 }*/
|
|
653
|
|
654 new llvm::StoreInst(tmp,l->mem,p->scopebb());
|
|
655
|
|
656 delete l;
|
|
657 delete r;
|
|
658
|
|
659 elem* e = new elem;
|
|
660 e->val = tmp;
|
|
661 e->type = elem::VAR;
|
|
662 return e;
|
|
663 }
|
|
664
|
|
665 //////////////////////////////////////////////////////////////////////////////////////////
|
|
666
|
|
667 elem* ModExp::toElem(IRState* p)
|
|
668 {
|
|
669 Logger::print("ModExp::toElem: %s\n", toChars());
|
|
670 LOG_SCOPE;
|
|
671 elem* e = new elem;
|
|
672 elem* l = e1->toElem(p);
|
|
673 elem* r = e2->toElem(p);
|
|
674
|
|
675 if (type->isunsigned())
|
|
676 e->val = llvm::BinaryOperator::createURem(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
677 else if (type->isintegral())
|
|
678 e->val = llvm::BinaryOperator::createSRem(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
679 else if (type->isfloating())
|
|
680 e->val = llvm::BinaryOperator::createFRem(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
681 else
|
|
682 assert(0);
|
|
683 e->type = elem::VAL;
|
|
684 delete l;
|
|
685 delete r;
|
|
686 return e;
|
|
687 }
|
|
688
|
|
689 //////////////////////////////////////////////////////////////////////////////////////////
|
|
690
|
|
691 elem* ModAssignExp::toElem(IRState* p)
|
|
692 {
|
|
693 Logger::print("ModAssignExp::toElem: %s\n", toChars());
|
|
694 LOG_SCOPE;
|
|
695
|
|
696 elem* l = e1->toElem(p);
|
|
697 elem* r = e2->toElem(p);
|
|
698
|
|
699 llvm::Value* tmp;
|
|
700 if (type->isunsigned())
|
|
701 tmp = llvm::BinaryOperator::createURem(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
702 else if (type->isintegral())
|
|
703 tmp = llvm::BinaryOperator::createSRem(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
704 else if (type->isfloating())
|
|
705 tmp = llvm::BinaryOperator::createFRem(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
706 else
|
|
707 assert(0);
|
|
708
|
|
709 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val;
|
|
710 if (storeVal->getType()->getContainedType(0) != tmp->getType())
|
|
711 {
|
|
712 tmp = LLVM_DtoPointedType(storeVal, tmp);
|
|
713 }*/
|
|
714
|
|
715 new llvm::StoreInst(tmp,l->mem,p->scopebb());
|
|
716
|
|
717 delete l;
|
|
718 delete r;
|
|
719
|
|
720 elem* e = new elem;
|
|
721 e->val = tmp;
|
|
722 e->type = elem::VAR;
|
|
723 return e;
|
|
724 }
|
|
725
|
|
726 //////////////////////////////////////////////////////////////////////////////////////////
|
|
727
|
|
728 elem* CallExp::toElem(IRState* p)
|
|
729 {
|
|
730 Logger::print("CallExp::toElem: %s\n", toChars());
|
|
731 LOG_SCOPE;
|
|
732 elem* e = new elem;
|
|
733 elem* fn = e1->toElem(p);
|
|
734 LINK dlink = LINKdefault;
|
|
735
|
|
736 bool delegateCall = false;
|
|
737 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false);
|
|
738 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty,1,false);
|
|
739
|
|
740 // hidden struct return parameter handling
|
|
741 bool retinptr = false;
|
|
742
|
|
743 TypeFunction* tf = 0;
|
|
744
|
|
745 // regular functions
|
|
746 if (e1->type->ty == Tfunction) {
|
|
747 tf = (TypeFunction*)e1->type;
|
|
748 if (tf->llvmRetInPtr) {
|
|
749 retinptr = true;
|
|
750 }
|
|
751 dlink = tf->linkage;
|
|
752 }
|
|
753
|
|
754 // delegates
|
|
755 else if (e1->type->ty == Tdelegate) {
|
|
756 Logger::println("delegateTy = %s\n", e1->type->toChars());
|
|
757 assert(e1->type->next->ty == Tfunction);
|
|
758 tf = (TypeFunction*)e1->type->next;
|
|
759 if (tf->llvmRetInPtr) {
|
|
760 retinptr = true;
|
|
761 }
|
|
762 dlink = tf->linkage;
|
|
763 delegateCall = true;
|
|
764 }
|
|
765
|
|
766 // invalid
|
|
767 else {
|
|
768 assert(tf);
|
|
769 }
|
|
770
|
|
771 size_t n = arguments->dim;
|
|
772 if (fn->arg || delegateCall) n++;
|
|
773 if (retinptr) n++;
|
|
774
|
|
775 llvm::Value* funcval = fn->getValue();
|
|
776 std::vector<llvm::Value*> llargs(n, 0);
|
|
777
|
|
778 const llvm::FunctionType* llfnty = 0;
|
|
779
|
|
780 // normal function call
|
|
781 if (llvm::isa<llvm::FunctionType>(funcval->getType())) {
|
|
782 llfnty = llvm::cast<llvm::FunctionType>(funcval->getType());
|
|
783 }
|
|
784 // pointer to something
|
|
785 else if (llvm::isa<llvm::PointerType>(funcval->getType())) {
|
|
786 // pointer to function pointer - I think this not really supposed to happen, but does :/
|
|
787 // seems like sometimes we get a func* other times a func**
|
|
788 if (llvm::isa<llvm::PointerType>(funcval->getType()->getContainedType(0))) {
|
|
789 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb());
|
|
790 }
|
|
791
|
|
792 // function pointer
|
|
793 if (llvm::isa<llvm::FunctionType>(funcval->getType()->getContainedType(0))) {
|
|
794 //Logger::cout() << "function pointer type:\n" << *funcval << '\n';
|
|
795 llfnty = llvm::cast<llvm::FunctionType>(funcval->getType()->getContainedType(0));
|
|
796 }
|
|
797 // struct pointer - delegate
|
|
798 else if (llvm::isa<llvm::StructType>(funcval->getType()->getContainedType(0))) {
|
|
799 funcval = new llvm::GetElementPtrInst(funcval,zero,one,"tmp",p->scopebb());
|
|
800 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb());
|
|
801 const llvm::Type* ty = funcval->getType()->getContainedType(0);
|
|
802 llfnty = llvm::cast<llvm::FunctionType>(ty);
|
|
803 }
|
|
804 // unknown
|
|
805 else {
|
|
806 Logger::cout() << "what kind of pointer are we calling? : " << *funcval->getType() << '\n';
|
|
807 }
|
|
808 }
|
|
809 else {
|
|
810 Logger::cout() << "what are we calling? : " << *funcval << '\n';
|
|
811 }
|
|
812 assert(llfnty);
|
|
813 Logger::cout() << "Function LLVM type: " << *llfnty << '\n';
|
|
814
|
|
815 // argument handling
|
|
816 llvm::FunctionType::param_iterator argiter = llfnty->param_begin();
|
|
817 int j = 0;
|
|
818
|
|
819 // hidden struct return parameter
|
|
820 if (retinptr) {
|
|
821 if (!p->lvals.empty()) {
|
|
822 assert(llvm::isa<llvm::StructType>(p->toplval()->getType()->getContainedType(0)));
|
|
823 llargs[j] = p->toplval();
|
|
824 TY Dty = tf->next->ty;
|
|
825 if (Dty == Tstruct || Dty == Tdelegate || Dty == Tarray) {
|
|
826 e->inplace = true;
|
|
827 }
|
|
828 else
|
|
829 assert(0);
|
|
830 }
|
|
831 else {
|
|
832 llargs[j] = new llvm::AllocaInst(argiter->get()->getContainedType(0),"rettmp",p->topallocapoint());
|
|
833 }
|
|
834 ++j;
|
|
835 ++argiter;
|
|
836 e->type = elem::VAR;
|
|
837 }
|
|
838 else {
|
|
839 e->type = elem::VAL;
|
|
840 }
|
|
841
|
|
842 // this parameter
|
|
843 if (fn->arg) {
|
|
844 Logger::println("This Call");
|
|
845 if (fn->arg->getType() != argiter->get()) {
|
|
846 //Logger::cout() << *fn->thisparam << '|' << *argiter->get() << '\n';
|
|
847 llargs[j] = new llvm::BitCastInst(fn->arg, argiter->get(), "tmp", p->scopebb());
|
|
848 }
|
|
849 else {
|
|
850 llargs[j] = fn->arg;
|
|
851 }
|
|
852 ++j;
|
|
853 ++argiter;
|
|
854 }
|
|
855 // delegate context parameter
|
|
856 else if (delegateCall) {
|
|
857 Logger::println("Delegate Call");
|
|
858 llvm::Value* contextptr = new llvm::GetElementPtrInst(fn->mem,zero,zero,"tmp",p->scopebb());
|
|
859 llargs[j] = new llvm::LoadInst(contextptr,"tmp",p->scopebb());
|
|
860 ++j;
|
|
861 ++argiter;
|
|
862 }
|
|
863
|
|
864 // regular parameters
|
|
865 for (int i=0; i<arguments->dim; i++,j++)
|
|
866 {
|
|
867 Expression* argexp = (Expression*)arguments->data[i];
|
|
868 elem* arg = argexp->toElem(p);
|
|
869
|
|
870 Argument* fnarg = Argument::getNth(tf->parameters, i);
|
|
871
|
|
872 TY argty = argexp->type->ty;
|
|
873 if (argty == Tstruct || argty == Tdelegate || argty == Tarray) {
|
|
874 if (!fnarg || !fnarg->llvmCopy) {
|
|
875 llargs[j] = arg->getValue();
|
|
876 assert(llargs[j] != 0);
|
|
877 }
|
|
878 else {
|
|
879 llvm::Value* allocaInst = 0;
|
|
880 llvm::BasicBlock* entryblock = &p->topfunc()->front();
|
|
881 const llvm::PointerType* pty = llvm::cast<llvm::PointerType>(arg->mem->getType());
|
|
882 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", p->topallocapoint());
|
|
883 if (argty == Tstruct) {
|
|
884 TypeStruct* ts = (TypeStruct*)argexp->type;
|
|
885 LLVM_DtoStructCopy(ts,allocaInst,arg->mem);
|
|
886 }
|
|
887 else if (argty == Tdelegate) {
|
|
888 LLVM_DtoDelegateCopy(allocaInst,arg->mem);
|
|
889 }
|
|
890 else if (argty == Tarray) {
|
|
891 LLVM_DtoArrayAssign(allocaInst,arg->mem);
|
|
892 }
|
|
893 else
|
|
894 assert(0);
|
|
895
|
|
896 llargs[j] = allocaInst;
|
|
897 assert(llargs[j] != 0);
|
|
898 }
|
|
899 }
|
|
900 else if (!fnarg || fnarg->llvmCopy) {
|
|
901 llargs[j] = arg->getValue();
|
|
902 assert(llargs[j] != 0);
|
|
903 }
|
|
904 else {
|
|
905 llargs[j] = arg->mem;
|
|
906 assert(llargs[j] != 0);
|
|
907 }
|
|
908
|
|
909 delete arg;
|
|
910 }
|
|
911
|
|
912 // void returns cannot not be named
|
|
913 const char* varname = "";
|
|
914 if (llfnty->getReturnType() != llvm::Type::VoidTy)
|
|
915 varname = "tmp";
|
|
916
|
|
917 Logger::println("%d params passed", n);
|
|
918 for (int i=0; i<n; ++i)
|
|
919 {
|
|
920 Logger::cout() << *llargs[i] << '\n';
|
|
921 }
|
|
922
|
|
923 Logger::cout() << "Calling: " << *funcval->getType() << '\n';
|
|
924
|
|
925 // call the function
|
|
926 llvm::CallInst* call = new llvm::CallInst(funcval, llargs.begin(), llargs.end(), varname, p->scopebb());
|
|
927 e->val = call;
|
|
928
|
|
929 // set calling convention
|
|
930 if ((fn->funcdecl && (fn->funcdecl->llvmInternal != LLVMintrinsic)) || delegateCall)
|
|
931 call->setCallingConv(LLVM_DtoCallingConv(dlink));
|
|
932
|
|
933 delete fn;
|
|
934 return e;
|
|
935 }
|
|
936
|
|
937 //////////////////////////////////////////////////////////////////////////////////////////
|
|
938
|
|
939 elem* CastExp::toElem(IRState* p)
|
|
940 {
|
|
941 Logger::print("CastExp::toElem: %s\n", toChars());
|
|
942 LOG_SCOPE;
|
|
943 elem* e = new elem;
|
|
944 elem* u = e1->toElem(p);
|
|
945 const llvm::Type* totype = LLVM_DtoType(to);
|
|
946 Type* from = e1->type;
|
|
947 int lsz = from->size();
|
|
948 int rsz = to->size();
|
|
949
|
|
950 // this makes sure the strange lvalue casts don't screw things up
|
|
951 e->mem = u->mem;
|
|
952
|
|
953 if (from->isintegral()) {
|
|
954 if (to->isintegral()) {
|
|
955 if (lsz < rsz) {
|
|
956 Logger::cout() << *totype << '\n';
|
|
957 if (from->isunsigned() || from->ty == Tbool) {
|
|
958 e->val = new llvm::ZExtInst(u->getValue(), totype, "tmp", p->scopebb());
|
|
959 } else {
|
|
960 e->val = new llvm::SExtInst(u->getValue(), totype, "tmp", p->scopebb());
|
|
961 }
|
|
962 }
|
|
963 else if (lsz > rsz) {
|
|
964 e->val = new llvm::TruncInst(u->getValue(), totype, "tmp", p->scopebb());
|
|
965 }
|
|
966 else {
|
|
967 e->val = new llvm::BitCastInst(u->getValue(), totype, "tmp", p->scopebb());
|
|
968 }
|
|
969 }
|
|
970 else if (to->isfloating()) {
|
|
971 if (from->isunsigned()) {
|
|
972 e->val = new llvm::UIToFPInst(u->getValue(), totype, "tmp", p->scopebb());
|
|
973 }
|
|
974 else {
|
|
975 e->val = new llvm::SIToFPInst(u->getValue(), totype, "tmp", p->scopebb());
|
|
976 }
|
|
977 }
|
|
978 else {
|
|
979 assert(0);
|
|
980 }
|
|
981 //e->storeVal = u->storeVal ? u->storeVal : u->val;
|
|
982 e->type = elem::VAL;
|
|
983 }
|
|
984 else if (from->isfloating()) {
|
|
985 if (to->isfloating()) {
|
|
986 if ((from->ty == Tfloat80 || from->ty == Tfloat64) && (to->ty == Tfloat80 || to->ty == Tfloat64)) {
|
|
987 e->val = u->getValue();
|
|
988 }
|
|
989 else if (lsz < rsz) {
|
|
990 e->val = new llvm::FPExtInst(u->getValue(), totype, "tmp", p->scopebb());
|
|
991 }
|
|
992 else if (lsz > rsz) {
|
|
993 e->val = new llvm::FPTruncInst(u->getValue(), totype, "tmp", p->scopebb());
|
|
994 }
|
|
995 else {
|
|
996 assert(0);
|
|
997 }
|
|
998 }
|
|
999 else if (to->isintegral()) {
|
|
1000 if (to->isunsigned()) {
|
|
1001 e->val = new llvm::FPToUIInst(u->getValue(), totype, "tmp", p->scopebb());
|
|
1002 }
|
|
1003 else {
|
|
1004 e->val = new llvm::FPToSIInst(u->getValue(), totype, "tmp", p->scopebb());
|
|
1005 }
|
|
1006 }
|
|
1007 else {
|
|
1008 assert(0);
|
|
1009 }
|
|
1010 e->type = elem::VAL;
|
|
1011 }
|
|
1012 else if (from->ty == Tclass) {
|
|
1013 //assert(to->ty == Tclass);
|
|
1014 e->val = new llvm::BitCastInst(u->getValue(), totype, "tmp", p->scopebb());
|
|
1015 e->type = elem::VAL;
|
|
1016 }
|
|
1017 else if (from->ty == Tarray || from->ty == Tsarray) {
|
|
1018 Logger::cout() << "from array or sarray" << '\n';
|
|
1019 if (to->ty == Tpointer) {
|
|
1020 Logger::cout() << "to pointer" << '\n';
|
|
1021 assert(from->next == to->next);
|
|
1022 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
1023 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false);
|
|
1024 llvm::Value* ptr = new llvm::GetElementPtrInst(u->getValue(),zero,one,"tmp",p->scopebb());
|
|
1025 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb());
|
|
1026 e->type = elem::VAL;
|
|
1027 }
|
|
1028 else if (to->ty == Tarray) {
|
|
1029 Logger::cout() << "to array" << '\n';
|
|
1030 assert(from->next->size() == to->next->size());
|
|
1031 const llvm::Type* ptrty = LLVM_DtoType(to->next);
|
|
1032 if (ptrty == llvm::Type::VoidTy)
|
|
1033 ptrty = llvm::Type::Int8Ty;
|
|
1034 ptrty = llvm::PointerType::get(ptrty);
|
|
1035
|
|
1036 if (u->type == elem::SLICE) {
|
|
1037 e->mem = new llvm::BitCastInst(u->mem, ptrty, "tmp", p->scopebb());
|
|
1038 e->arg = u->arg;
|
|
1039 }
|
|
1040 else {
|
|
1041 llvm::Value* uval = u->getValue();
|
|
1042 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
1043 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false);
|
|
1044 e->arg = new llvm::GetElementPtrInst(uval,zero,zero,"tmp",p->scopebb());
|
|
1045 e->arg = new llvm::LoadInst(e->arg, "tmp", p->scopebb());
|
|
1046
|
|
1047 e->mem = new llvm::GetElementPtrInst(uval,zero,one,"tmp",p->scopebb());
|
|
1048 e->mem = new llvm::LoadInst(e->mem, "tmp", p->scopebb());
|
|
1049 e->mem = new llvm::BitCastInst(e->mem, ptrty, "tmp", p->scopebb());
|
|
1050 }
|
|
1051 e->type = elem::SLICE;
|
|
1052 }
|
|
1053 else if (to->ty == Tsarray) {
|
|
1054 Logger::cout() << "to sarray" << '\n';
|
|
1055 assert(0);
|
|
1056 }
|
|
1057 else {
|
|
1058 assert(0);
|
|
1059 }
|
|
1060 }
|
|
1061 else if (from->ty == Tpointer) {
|
|
1062 if (to->ty == Tpointer || to->ty == Tclass) {
|
|
1063 llvm::Value* src = u->getValue();
|
|
1064 //Logger::cout() << *src << '|' << *totype << '\n';
|
|
1065 e->val = new llvm::BitCastInst(src, totype, "tmp", p->scopebb());
|
|
1066 }
|
|
1067 else if (to->isintegral()) {
|
|
1068 e->val = new llvm::PtrToIntInst(u->getValue(), totype, "tmp", p->scopebb());
|
|
1069 }
|
|
1070 else
|
|
1071 assert(0);
|
|
1072 e->type = elem::VAL;
|
|
1073 }
|
|
1074 else {
|
|
1075 assert(0);
|
|
1076 }
|
|
1077 delete u;
|
|
1078 return e;
|
|
1079 }
|
|
1080
|
|
1081 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1082
|
|
1083 elem* SymOffExp::toElem(IRState* p)
|
|
1084 {
|
|
1085 Logger::print("SymOffExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1086 LOG_SCOPE;
|
|
1087 elem* e = 0;
|
|
1088 if (VarDeclaration* vd = var->isVarDeclaration())
|
|
1089 {
|
|
1090 Logger::println("VarDeclaration");
|
|
1091 if (vd->type->ty == Tstruct && !(type->ty == Tpointer && type->next == vd->type)) {
|
|
1092 TypeStruct* vdt = (TypeStruct*)vd->type;
|
|
1093 e = new elem;
|
|
1094 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
1095 llvm::Value* idx1 = llvm::ConstantInt::get(llvm::Type::Int32Ty, (uint64_t)vdt->sym->offsetToIndex(offset), false);
|
|
1096 //const llvm::Type* _typ = llvm::GetElementPtrInst::getIndexedType(LLVM_DtoType(type), idx1);
|
|
1097 llvm::Value* ptr = vd->llvmValue;
|
|
1098 assert(ptr);
|
|
1099 e->mem = new llvm::GetElementPtrInst(ptr,idx0,idx1,"tmp",p->scopebb());
|
|
1100 e->type = elem::VAL;
|
|
1101 e->field = true;
|
|
1102 }
|
|
1103 else if (vd->type->ty == Tsarray) {
|
|
1104 /*e = new elem;
|
|
1105 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
1106 e->val = new llvm::GetElementPtrInst(vd->llvmValue,idx0,idx0,"tmp",p->scopebb());*/
|
|
1107 e = new elem;
|
|
1108 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
1109 //llvm::Value* idx1 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false);
|
|
1110 e->mem = new llvm::GetElementPtrInst(vd->llvmValue,idx0,idx0,"tmp",p->scopebb());
|
|
1111 e->type = elem::VAL;
|
|
1112 }
|
|
1113 else if (offset == 0) {
|
|
1114 vd->toObjFile();
|
|
1115 e = new elem;
|
|
1116 e->mem = vd->llvmValue;
|
|
1117 //e->vardecl = vd;
|
|
1118 e->type = elem::VAL;
|
|
1119 }
|
|
1120 else {
|
|
1121 assert(0);
|
|
1122 }
|
|
1123 }
|
|
1124 else if (FuncDeclaration* fd = var->isFuncDeclaration())
|
|
1125 {
|
|
1126 Logger::println("FuncDeclaration");
|
|
1127 e = new elem;
|
|
1128 if (fd->llvmValue == 0)
|
|
1129 fd->toObjFile();
|
|
1130 e->val = fd->llvmValue;
|
|
1131 //e->aspointer = true;
|
|
1132 e->type = elem::FUNC;
|
|
1133 }
|
|
1134 assert(e != 0);
|
|
1135 assert(e->type != elem::NONE);
|
|
1136 return e;
|
|
1137 }
|
|
1138
|
|
1139 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1140
|
|
1141 elem* PtrExp::toElem(IRState* p)
|
|
1142 {
|
|
1143 Logger::print("PtrExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1144 LOG_SCOPE;
|
|
1145 elem* e = new elem;
|
|
1146 elem* a = e1->toElem(p);
|
|
1147
|
|
1148 if (a->mem)
|
|
1149 Logger::cout() << "mem: " << *a->mem << '\n';
|
|
1150 if (a->val)
|
|
1151 Logger::cout() << "val: " << *a->val << '\n';
|
|
1152
|
|
1153 if (a->field)
|
|
1154 e->mem = a->mem;
|
|
1155 else
|
|
1156 e->mem = a->getValue();
|
|
1157 e->type = elem::VAR;
|
|
1158
|
|
1159 delete a;
|
|
1160 return e;
|
|
1161 }
|
|
1162
|
|
1163 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1164
|
|
1165 elem* DotVarExp::toElem(IRState* p)
|
|
1166 {
|
|
1167 Logger::print("DotVarExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1168 LOG_SCOPE;
|
|
1169 elem* e = new elem;
|
|
1170
|
|
1171 elem* l = e1->toElem(p);
|
|
1172
|
|
1173 Logger::print("e1->type=%s\n", e1->type->toChars());
|
|
1174
|
|
1175 if (VarDeclaration* vd = var->isVarDeclaration()) {
|
|
1176 size_t vdoffset = (size_t)-1;
|
|
1177 llvm::Value* src = 0;
|
|
1178 if (e1->type->ty == Tpointer) {
|
|
1179 assert(e1->type->next->ty == Tstruct);
|
|
1180 TypeStruct* ts = (TypeStruct*)e1->type->next;
|
|
1181 vdoffset = ts->sym->offsetToIndex(vd->offset);
|
|
1182 Logger::println("Struct member offset:%d index:%d", vd->offset, vdoffset);
|
|
1183 src = l->val;
|
|
1184 }
|
|
1185 else if (e1->type->ty == Tclass) {
|
|
1186 TypeClass* tc = (TypeClass*)e1->type;
|
|
1187 Logger::println("Class member offset: %d", vd->offset);
|
|
1188 vdoffset = tc->sym->offsetToIndex(vd->offset);
|
|
1189 src = l->getValue();
|
|
1190 }
|
|
1191 assert(vdoffset != (size_t)-1);
|
|
1192 assert(src != 0);
|
|
1193 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
1194 llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, vdoffset, false);
|
|
1195 llvm::Value* arrptr = new llvm::GetElementPtrInst(src,zero,offset,"tmp",p->scopebb());
|
|
1196 e->mem = arrptr;
|
|
1197 Logger::cout() << "mem: " << *e->mem << '\n';
|
|
1198 e->type = elem::VAR;
|
|
1199 }
|
|
1200 else if (FuncDeclaration* fdecl = var->isFuncDeclaration())
|
|
1201 {
|
|
1202 if (fdecl->llvmValue == 0)
|
|
1203 {
|
|
1204 fdecl->toObjFile();
|
|
1205 }
|
|
1206
|
|
1207 llvm::Value* funcval = fdecl->llvmValue;
|
|
1208 e->arg = l->getValue();
|
|
1209
|
|
1210 // virtual call
|
|
1211 if (fdecl->isVirtual()) {
|
|
1212 assert(fdecl->vtblIndex > 0);
|
|
1213 assert(e1->type->ty == Tclass);
|
|
1214
|
|
1215 const llvm::Type* vtbltype = llvm::PointerType::get(llvm::ArrayType::get(llvm::PointerType::get(llvm::Type::Int8Ty),0));
|
|
1216
|
|
1217 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
1218 llvm::Value* vtblidx = llvm::ConstantInt::get(llvm::Type::Int32Ty, (size_t)fdecl->vtblIndex, false);
|
|
1219 funcval = new llvm::GetElementPtrInst(e->arg, zero, zero, "tmp", p->scopebb());
|
|
1220 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb());
|
|
1221 funcval = new llvm::BitCastInst(funcval, vtbltype, "tmp", p->scopebb());
|
|
1222 funcval = new llvm::GetElementPtrInst(funcval, zero, vtblidx, "tmp", p->scopebb());
|
|
1223 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb());
|
|
1224 funcval = new llvm::BitCastInst(funcval, fdecl->llvmValue->getType(), "tmp", p->scopebb());
|
|
1225 }
|
|
1226 e->val = funcval;
|
|
1227 e->type = elem::VAL;
|
|
1228 }
|
|
1229 else {
|
|
1230 printf("unknown: %s\n", var->toChars());
|
|
1231 assert(0);
|
|
1232 }
|
|
1233
|
|
1234 delete l;
|
|
1235
|
|
1236 return e;
|
|
1237 }
|
|
1238
|
|
1239 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1240
|
|
1241 elem* ThisExp::toElem(IRState* p)
|
|
1242 {
|
|
1243 Logger::print("ThisExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1244 LOG_SCOPE;
|
|
1245 elem* e = new elem;
|
|
1246
|
|
1247 if (VarDeclaration* vd = var->isVarDeclaration()) {
|
|
1248 assert(vd->llvmValue == 0);
|
|
1249
|
|
1250 llvm::Function* fn = p->topfunc();
|
|
1251 assert(fn);
|
|
1252
|
|
1253 TypeFunction* tf = p->topfunctype();
|
|
1254 assert(tf);
|
|
1255
|
|
1256 llvm::Value* v = 0;
|
|
1257 if (tf->llvmRetInPtr)
|
|
1258 v = ++fn->arg_begin();
|
|
1259 else
|
|
1260 v = fn->arg_begin();
|
|
1261 assert(v);
|
|
1262
|
|
1263 e->val = v;
|
|
1264 e->type = elem::VAL;
|
|
1265 }
|
|
1266 else {
|
|
1267 assert(0);
|
|
1268 }
|
|
1269
|
|
1270 return e;
|
|
1271 }
|
|
1272
|
|
1273 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1274
|
|
1275 elem* AddrExp::toElem(IRState* p)
|
|
1276 {
|
|
1277 Logger::print("AddrExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1278 LOG_SCOPE;
|
|
1279 elem* e = e1->toElem(p);
|
|
1280 e->field = true;
|
|
1281 return e;
|
|
1282 }
|
|
1283
|
|
1284 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1285
|
|
1286 elem* StructLiteralExp::toElem(IRState* p)
|
|
1287 {
|
|
1288 Logger::print("StructLiteralExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1289 LOG_SCOPE;
|
|
1290 elem* e = new elem;
|
|
1291
|
|
1292 // if there is no lval, this must be a static initializer for a global. correct?
|
|
1293 if (p->lvals.empty())
|
|
1294 {
|
|
1295 // TODO
|
|
1296 assert(0);
|
|
1297 }
|
|
1298 // otherwise write directly in the lvalue
|
|
1299 else
|
|
1300 {
|
|
1301 llvm::Value* sptr = p->toplval();
|
|
1302 assert(sptr);
|
|
1303
|
|
1304 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
1305
|
|
1306 unsigned n = elements->dim;
|
|
1307 for (unsigned i=0; i<n; ++i)
|
|
1308 {
|
|
1309 llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false);
|
|
1310 llvm::Value* arrptr = new llvm::GetElementPtrInst(sptr,zero,offset,"tmp",p->scopebb());
|
|
1311
|
|
1312 Expression* vx = (Expression*)elements->data[i];
|
|
1313 if (vx != 0) {
|
|
1314 elem* ve = vx->toElem(p);
|
|
1315 //Logger::cout() << *ve->val << " | " << *arrptr << '\n';
|
|
1316 new llvm::StoreInst(ve->getValue(), arrptr, p->scopebb());
|
|
1317 delete ve;
|
|
1318 }
|
|
1319 else {
|
|
1320 assert(0);
|
|
1321 }
|
|
1322 }
|
|
1323 }
|
|
1324
|
|
1325 e->inplace = true;
|
|
1326
|
|
1327 return e;
|
|
1328 }
|
|
1329
|
|
1330 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1331
|
|
1332 elem* IndexExp::toElem(IRState* p)
|
|
1333 {
|
|
1334 Logger::print("IndexExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1335 LOG_SCOPE;
|
|
1336
|
|
1337 elem* e = new elem;
|
|
1338
|
|
1339 elem* l = e1->toElem(p);
|
|
1340
|
|
1341 p->arrays.push_back(l->mem); // if $ is used it must be an array so this is fine.
|
|
1342 elem* r = e2->toElem(p);
|
|
1343 p->arrays.pop_back();
|
|
1344
|
|
1345 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
1346 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false);
|
|
1347
|
|
1348 llvm::Value* arrptr = 0;
|
|
1349 if (e1->type->ty == Tpointer) {
|
|
1350 arrptr = new llvm::GetElementPtrInst(l->getValue(),r->getValue(),"tmp",p->scopebb());
|
|
1351 }
|
|
1352 else if (e1->type->ty == Tsarray) {
|
|
1353 arrptr = new llvm::GetElementPtrInst(l->mem, zero, r->getValue(),"tmp",p->scopebb());
|
|
1354 }
|
|
1355 else if (e1->type->ty == Tarray) {
|
|
1356 arrptr = new llvm::GetElementPtrInst(l->mem,zero,one,"tmp",p->scopebb());
|
|
1357 arrptr = new llvm::LoadInst(arrptr,"tmp",p->scopebb());
|
|
1358 arrptr = new llvm::GetElementPtrInst(arrptr,r->getValue(),"tmp",p->scopebb());
|
|
1359 }
|
|
1360 assert(arrptr);
|
|
1361
|
|
1362 e->mem = arrptr;
|
|
1363 e->type = elem::VAR;
|
|
1364
|
|
1365 delete l;
|
|
1366 delete r;
|
|
1367
|
|
1368 return e;
|
|
1369 }
|
|
1370
|
|
1371 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1372
|
|
1373 elem* SliceExp::toElem(IRState* p)
|
|
1374 {
|
|
1375 Logger::print("SliceExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1376 LOG_SCOPE;
|
|
1377
|
|
1378 elem* v = e1->toElem(p);
|
|
1379
|
|
1380 elem* e = new elem;
|
|
1381 assert(v->mem);
|
|
1382 e->type = elem::SLICE;
|
|
1383
|
|
1384 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
1385 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false);
|
|
1386
|
|
1387 // partial slice
|
|
1388 if (lwr)
|
|
1389 {
|
|
1390 assert(upr);
|
|
1391 p->arrays.push_back(v->mem);
|
|
1392 elem* lo = lwr->toElem(p);
|
|
1393
|
|
1394 bool lwr_is_zero = false;
|
|
1395 if (lo->type == elem::CONST)
|
|
1396 {
|
|
1397 assert(lo->val);
|
|
1398 assert(llvm::isa<llvm::ConstantInt>(lo->val));
|
|
1399
|
|
1400 if (e1->type->ty == Tpointer) {
|
|
1401 e->mem = v->getValue();
|
|
1402 }
|
|
1403 else if (e1->type->ty == Tarray) {
|
|
1404 llvm::Value* tmp = new llvm::GetElementPtrInst(v->mem,zero,one,"tmp",p->scopebb());
|
|
1405 e->mem = new llvm::LoadInst(tmp,"tmp",p->scopebb());
|
|
1406 }
|
|
1407 else
|
|
1408 assert(e->mem);
|
|
1409
|
|
1410 llvm::ConstantInt* c = llvm::cast<llvm::ConstantInt>(lo->val);
|
|
1411 if (!(lwr_is_zero = c->isZero())) {
|
|
1412 e->mem = new llvm::GetElementPtrInst(e->mem,lo->val,"tmp",p->scopebb());
|
|
1413 }
|
|
1414 }
|
|
1415 else
|
|
1416 {
|
|
1417 llvm::Value* tmp = new llvm::GetElementPtrInst(v->mem,zero,one,"tmp",p->scopebb());
|
|
1418 tmp = new llvm::LoadInst(tmp,"tmp",p->scopebb());
|
|
1419 e->mem = new llvm::GetElementPtrInst(tmp,lo->getValue(),"tmp",p->scopebb());
|
|
1420 }
|
|
1421
|
|
1422 elem* up = upr->toElem(p);
|
|
1423 p->arrays.pop_back();
|
|
1424
|
|
1425 if (up->type == elem::CONST)
|
|
1426 {
|
|
1427 assert(up->val);
|
|
1428 assert(llvm::isa<llvm::ConstantInt>(up->val));
|
|
1429 if (lwr_is_zero) {
|
|
1430 e->arg = up->val;
|
|
1431 }
|
|
1432 else {
|
|
1433 if (lo->type == elem::CONST) {
|
|
1434 llvm::Constant* clo = llvm::cast<llvm::Constant>(lo->val);
|
|
1435 llvm::Constant* cup = llvm::cast<llvm::Constant>(up->val);
|
|
1436 e->arg = llvm::ConstantExpr::getSub(cup, clo);
|
|
1437 }
|
|
1438 else {
|
|
1439 e->arg = llvm::BinaryOperator::createSub(up->val, lo->getValue(), "tmp", p->scopebb());
|
|
1440 }
|
|
1441 }
|
|
1442 }
|
|
1443 else
|
|
1444 {
|
|
1445 if (lwr_is_zero) {
|
|
1446 e->arg = up->getValue();
|
|
1447 }
|
|
1448 else {
|
|
1449 e->arg = llvm::BinaryOperator::createSub(up->getValue(), lo->getValue(), "tmp", p->scopebb());
|
|
1450 }
|
|
1451 }
|
|
1452
|
|
1453 delete lo;
|
|
1454 delete up;
|
|
1455 }
|
|
1456 // full slice
|
|
1457 else
|
|
1458 {
|
|
1459 e->mem = v->mem;
|
|
1460 }
|
|
1461
|
|
1462 delete v;
|
|
1463
|
|
1464 return e;
|
|
1465 }
|
|
1466
|
|
1467 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1468
|
|
1469 elem* CmpExp::toElem(IRState* p)
|
|
1470 {
|
|
1471 Logger::print("CmpExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1472 LOG_SCOPE;
|
|
1473
|
|
1474 elem* e = new elem;
|
|
1475
|
|
1476 elem* l = e1->toElem(p);
|
|
1477 elem* r = e2->toElem(p);
|
|
1478
|
|
1479 assert(e1->type == e2->type);
|
|
1480
|
|
1481 Type* t = e1->type;
|
|
1482
|
|
1483 if (t->isintegral())
|
|
1484 {
|
|
1485 llvm::ICmpInst::Predicate cmpop;
|
|
1486 switch(op)
|
|
1487 {
|
|
1488 case TOKlt:
|
|
1489 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_ULT : llvm::ICmpInst::ICMP_SLT;
|
|
1490 break;
|
|
1491 case TOKle:
|
|
1492 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_ULE : llvm::ICmpInst::ICMP_SLE;
|
|
1493 break;
|
|
1494 case TOKgt:
|
|
1495 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_UGT : llvm::ICmpInst::ICMP_SGT;
|
|
1496 break;
|
|
1497 case TOKge:
|
|
1498 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_UGE : llvm::ICmpInst::ICMP_SGE;
|
|
1499 break;
|
|
1500 default:
|
|
1501 assert(0);
|
|
1502 }
|
|
1503 e->val = new llvm::ICmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb());
|
|
1504 }
|
|
1505 else if (t->isfloating())
|
|
1506 {
|
|
1507 llvm::FCmpInst::Predicate cmpop;
|
|
1508 switch(op)
|
|
1509 {
|
|
1510 case TOKlt:
|
|
1511 cmpop = llvm::FCmpInst::FCMP_OLT;break;
|
|
1512 case TOKle:
|
|
1513 cmpop = llvm::FCmpInst::FCMP_OLE;break;
|
|
1514 case TOKgt:
|
|
1515 cmpop = llvm::FCmpInst::FCMP_OGT;break;
|
|
1516 case TOKge:
|
|
1517 cmpop = llvm::FCmpInst::FCMP_OGE;break;
|
|
1518 case TOKunord:
|
|
1519 cmpop = llvm::FCmpInst::FCMP_UNO;break;
|
|
1520 case TOKule:
|
|
1521 cmpop = llvm::FCmpInst::FCMP_ULE;break;
|
|
1522 case TOKul:
|
|
1523 cmpop = llvm::FCmpInst::FCMP_ULT;break;
|
|
1524 case TOKuge:
|
|
1525 cmpop = llvm::FCmpInst::FCMP_UGE;break;
|
|
1526 case TOKug:
|
|
1527 cmpop = llvm::FCmpInst::FCMP_UGT;break;
|
|
1528 case TOKue:
|
|
1529 cmpop = llvm::FCmpInst::FCMP_UEQ;break;
|
|
1530 case TOKlg:
|
|
1531 cmpop = llvm::FCmpInst::FCMP_ONE;break;
|
|
1532 case TOKleg:
|
|
1533 cmpop = llvm::FCmpInst::FCMP_ORD;break;
|
|
1534
|
|
1535 default:
|
|
1536 assert(0);
|
|
1537 }
|
|
1538 e->val = new llvm::FCmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb());
|
|
1539 }
|
|
1540 else
|
|
1541 {
|
|
1542 assert(0 && "Unsupported CmpExp type");
|
|
1543 }
|
|
1544
|
|
1545 delete l;
|
|
1546 delete r;
|
|
1547
|
|
1548 e->type = elem::VAL;
|
|
1549
|
|
1550 return e;
|
|
1551 }
|
|
1552
|
|
1553 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1554
|
|
1555 elem* EqualExp::toElem(IRState* p)
|
|
1556 {
|
|
1557 Logger::print("EqualExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1558 LOG_SCOPE;
|
|
1559
|
|
1560 elem* e = new elem;
|
|
1561
|
|
1562 elem* l = e1->toElem(p);
|
|
1563 elem* r = e2->toElem(p);
|
|
1564
|
|
1565 assert(e1->type == e2->type);
|
|
1566
|
|
1567 Type* t = e1->type;
|
|
1568
|
|
1569 if (t->isintegral() || t->ty == Tpointer)
|
|
1570 {
|
|
1571 llvm::ICmpInst::Predicate cmpop;
|
|
1572 switch(op)
|
|
1573 {
|
|
1574 case TOKequal:
|
|
1575 cmpop = llvm::ICmpInst::ICMP_EQ;
|
|
1576 break;
|
|
1577 case TOKnotequal:
|
|
1578 cmpop = llvm::ICmpInst::ICMP_NE;
|
|
1579 break;
|
|
1580 default:
|
|
1581 assert(0);
|
|
1582 }
|
|
1583 e->val = new llvm::ICmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb());
|
|
1584 }
|
|
1585 else if (t->isfloating())
|
|
1586 {
|
|
1587 llvm::FCmpInst::Predicate cmpop;
|
|
1588 switch(op)
|
|
1589 {
|
|
1590 case TOKequal:
|
|
1591 cmpop = llvm::FCmpInst::FCMP_OEQ;
|
|
1592 break;
|
|
1593 case TOKnotequal:
|
|
1594 cmpop = llvm::FCmpInst::FCMP_UNE;
|
|
1595 break;
|
|
1596 default:
|
|
1597 assert(0);
|
|
1598 }
|
|
1599 e->val = new llvm::FCmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb());
|
|
1600 }
|
|
1601 else if (t->ty == Tarray)
|
|
1602 {
|
|
1603 // array comparison invokes the typeinfo runtime
|
|
1604 assert(0);
|
|
1605 }
|
|
1606 else
|
|
1607 {
|
|
1608 assert(0 && "Unsupported EqualExp type");
|
|
1609 }
|
|
1610
|
|
1611 delete l;
|
|
1612 delete r;
|
|
1613
|
|
1614 e->type = elem::VAL;
|
|
1615
|
|
1616 return e;
|
|
1617 }
|
|
1618
|
|
1619 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1620
|
|
1621 elem* PostExp::toElem(IRState* p)
|
|
1622 {
|
|
1623 Logger::print("PostExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1624 LOG_SCOPE;
|
|
1625
|
|
1626 elem* l = e1->toElem(p);
|
|
1627 elem* r = e2->toElem(p);
|
|
1628
|
|
1629 elem* e = new elem;
|
|
1630 e->mem = l->mem;
|
|
1631 e->val = l->getValue();
|
|
1632 e->type = elem::VAL;
|
|
1633
|
|
1634 llvm::Value* val = e->val;
|
|
1635 llvm::Value* post = 0;
|
|
1636
|
|
1637 if (e1->type->isintegral())
|
|
1638 {
|
|
1639 assert(e2->type->isintegral());
|
|
1640 llvm::Value* one = llvm::ConstantInt::get(val->getType(), 1, !e2->type->isunsigned());
|
|
1641 if (op == TOKplusplus) {
|
|
1642 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb());
|
|
1643 }
|
|
1644 else if (op == TOKminusminus) {
|
|
1645 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb());
|
|
1646 }
|
|
1647 }
|
|
1648 else if (e1->type->ty == Tpointer)
|
|
1649 {
|
|
1650 assert(e2->type->isintegral());
|
|
1651 llvm::Constant* minusone = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)-1,true);
|
|
1652 llvm::Constant* plusone = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)1,false);
|
|
1653 llvm::Constant* whichone = (op == TOKplusplus) ? plusone : minusone;
|
|
1654 post = new llvm::GetElementPtrInst(val, whichone, "tmp", p->scopebb());
|
|
1655 }
|
|
1656 else if (e1->type->isfloating())
|
|
1657 {
|
|
1658 assert(e2->type->isfloating());
|
|
1659 llvm::Value* one = llvm::ConstantFP::get(val->getType(), 1.0f);
|
|
1660 if (op == TOKplusplus) {
|
|
1661 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb());
|
|
1662 }
|
|
1663 else if (op == TOKminusminus) {
|
|
1664 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb());
|
|
1665 }
|
|
1666 }
|
|
1667 else
|
|
1668 assert(post);
|
|
1669
|
|
1670 //llvm::Value* tostore = l->storeVal ? l->storeVal : l->val;
|
|
1671 new llvm::StoreInst(post,l->mem,p->scopebb());
|
|
1672
|
|
1673 delete l;
|
|
1674 delete r;
|
|
1675
|
|
1676 return e;
|
|
1677 }
|
|
1678
|
|
1679 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1680
|
|
1681 elem* NewExp::toElem(IRState* p)
|
|
1682 {
|
|
1683 Logger::print("NewExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1684 LOG_SCOPE;
|
|
1685
|
|
1686 assert(!thisexp);
|
|
1687 assert(!newargs);
|
|
1688 assert(newtype);
|
|
1689 //assert(!arguments);
|
|
1690 //assert(!member);
|
|
1691 assert(!allocator);
|
|
1692
|
|
1693 elem* e = new elem;
|
|
1694
|
|
1695 const llvm::Type* t = LLVM_DtoType(newtype);
|
|
1696
|
|
1697 if (onstack) {
|
|
1698 assert(newtype->ty == Tclass);
|
|
1699 e->mem = new llvm::AllocaInst(t->getContainedType(0),"tmp",p->topallocapoint());
|
|
1700 }
|
|
1701 else {
|
|
1702 if (newtype->ty == Tclass) {
|
|
1703 e->mem = new llvm::MallocInst(t->getContainedType(0),"tmp",p->scopebb());
|
|
1704 }
|
|
1705 else if (newtype->ty == Tarray) {
|
|
1706 t = LLVM_DtoType(newtype->next);
|
|
1707 assert(arguments);
|
|
1708 if (arguments->dim == 1) {
|
|
1709 elem* sz = ((Expression*)arguments->data[0])->toElem(p);
|
|
1710 llvm::Value* dimval = sz->getValue();
|
|
1711 llvm::Value* usedimval = dimval;
|
|
1712 if (dimval->getType() != llvm::Type::Int32Ty)
|
|
1713 usedimval = new llvm::TruncInst(dimval, llvm::Type::Int32Ty,"tmp",p->scopebb());
|
|
1714 e->mem = new llvm::MallocInst(t,usedimval,"tmp",p->scopebb());
|
|
1715
|
|
1716 LLVM_DtoSetArray(p->toplval(), dimval, e->mem);
|
|
1717 delete sz;
|
|
1718 }
|
|
1719 else {
|
|
1720 assert(0);
|
|
1721 }
|
|
1722 }
|
|
1723 else {
|
|
1724 e->mem = new llvm::MallocInst(t,"tmp",p->scopebb());
|
|
1725 }
|
|
1726 }
|
|
1727
|
|
1728 if (newtype->ty == Tclass) {
|
|
1729 // first apply the static initializer
|
|
1730 assert(e->mem);
|
|
1731 LLVM_DtoInitClass((TypeClass*)newtype, e->mem);
|
|
1732
|
|
1733 // then call constructor
|
|
1734 if (arguments) {
|
|
1735 std::vector<llvm::Value*> ctorargs;
|
|
1736 ctorargs.push_back(e->mem);
|
|
1737 for (size_t i=0; i<arguments->dim; ++i)
|
|
1738 {
|
|
1739 Expression* ex = (Expression*)arguments->data[i];
|
|
1740 Logger::println("arg=%s", ex->toChars());
|
|
1741 elem* exe = ex->toElem(p);
|
|
1742 assert(exe->getValue());
|
|
1743 ctorargs.push_back(exe->getValue());
|
|
1744 delete exe;
|
|
1745 }
|
|
1746 assert(member);
|
|
1747 assert(member->llvmValue);
|
|
1748 new llvm::CallInst(member->llvmValue, ctorargs.begin(), ctorargs.end(), "", p->scopebb());
|
|
1749 }
|
|
1750 }
|
|
1751 else if (newtype->ty == Tstruct) {
|
|
1752 TypeStruct* ts = (TypeStruct*)newtype;
|
|
1753 if (ts->isZeroInit()) {
|
|
1754 LLVM_DtoStructZeroInit(ts,e->mem);
|
|
1755 }
|
|
1756 else {
|
|
1757 LLVM_DtoStructCopy(ts,e->mem,ts->llvmInit);
|
|
1758 }
|
|
1759 }
|
|
1760
|
|
1761 e->inplace = true;
|
|
1762 e->type = elem::VAR;
|
|
1763
|
|
1764 return e;
|
|
1765 }
|
|
1766
|
|
1767 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1768
|
|
1769 elem* DeleteExp::toElem(IRState* p)
|
|
1770 {
|
|
1771 Logger::print("DeleteExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1772 LOG_SCOPE;
|
|
1773
|
|
1774 //assert(e1->type->ty != Tclass);
|
|
1775
|
|
1776 elem* v = e1->toElem(p);
|
|
1777 llvm::Value* val = v->getValue();
|
|
1778 llvm::Value* ldval = 0;
|
|
1779
|
|
1780 const llvm::Type* t = val->getType();
|
|
1781 llvm::Constant* z = llvm::Constant::getNullValue(t);
|
|
1782
|
|
1783 if (e1->type->ty == Tpointer) {
|
|
1784 ldval = v->getValue();
|
|
1785 new llvm::FreeInst(ldval, p->scopebb());
|
|
1786
|
|
1787 Logger::cout() << *z << '\n';
|
|
1788 Logger::cout() << *val << '\n';
|
|
1789 new llvm::StoreInst(z, v->mem, p->scopebb());
|
|
1790 }
|
|
1791 else if (e1->type->ty == Tclass) {
|
|
1792 TypeClass* tc = (TypeClass*)e1->type;
|
|
1793 LLVM_DtoCallClassDtors(tc, val);
|
|
1794
|
|
1795 if (v->vardecl && !v->vardecl->onstack) {
|
|
1796 new llvm::FreeInst(val, p->scopebb());
|
|
1797 }
|
|
1798 new llvm::StoreInst(z, v->mem, p->scopebb());
|
|
1799 }
|
|
1800 else if (e1->type->ty == Tarray) {
|
|
1801 // must be on the heap (correct?)
|
|
1802 ldval = v->getValue();
|
|
1803
|
|
1804 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
1805 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false);
|
|
1806
|
|
1807 llvm::Value* ptr = new llvm::GetElementPtrInst(ldval,zero,one,"tmp",p->scopebb());
|
|
1808 ptr = new llvm::LoadInst(ptr,"tmp",p->scopebb());
|
|
1809 new llvm::FreeInst(ptr, p->scopebb());
|
|
1810 LLVM_DtoNullArray(val);
|
|
1811 }
|
|
1812 else {
|
|
1813 assert(0);
|
|
1814 }
|
|
1815
|
|
1816 delete v;
|
|
1817
|
|
1818 // this expression produces no useful data
|
|
1819 return 0;
|
|
1820 }
|
|
1821
|
|
1822 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1823
|
|
1824 elem* ArrayLengthExp::toElem(IRState* p)
|
|
1825 {
|
|
1826 Logger::print("ArrayLengthExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1827 LOG_SCOPE;
|
|
1828
|
|
1829 elem* e = new elem;
|
|
1830 elem* u = e1->toElem(p);
|
|
1831
|
|
1832 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
1833 llvm::Value* ptr = new llvm::GetElementPtrInst(u->mem,zero,zero,"tmp",p->scopebb());
|
|
1834 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb());
|
|
1835 e->type = elem::VAL;
|
|
1836
|
|
1837 delete u;
|
|
1838
|
|
1839 return e;
|
|
1840 }
|
|
1841
|
|
1842 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1843
|
|
1844 elem* AssertExp::toElem(IRState* p)
|
|
1845 {
|
|
1846 Logger::print("AssertExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1847 LOG_SCOPE;
|
|
1848
|
|
1849 elem* e = new elem;
|
|
1850 elem* u = e1->toElem(p);
|
|
1851 elem* m = msg ? msg->toElem(p) : 0;
|
|
1852
|
|
1853 std::vector<llvm::Value*> llargs;
|
|
1854 llargs.resize(3);
|
|
1855 llargs[0] = LLVM_DtoBoolean(u->getValue());
|
|
1856 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false);
|
|
1857 llargs[2] = m ? m->val : llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty));
|
|
1858
|
|
1859 delete m;
|
|
1860 delete u;
|
|
1861
|
|
1862 //Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n';
|
|
1863
|
|
1864 llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert");
|
|
1865 assert(fn);
|
|
1866 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb());
|
|
1867 call->setCallingConv(llvm::CallingConv::C);
|
|
1868
|
|
1869 return e;
|
|
1870 }
|
|
1871
|
|
1872 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1873
|
|
1874 elem* NotExp::toElem(IRState* p)
|
|
1875 {
|
|
1876 Logger::print("NotExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1877 LOG_SCOPE;
|
|
1878
|
|
1879 elem* e = new elem;
|
|
1880 elem* u = e1->toElem(p);
|
|
1881
|
|
1882 llvm::Value* b = LLVM_DtoBoolean(u->getValue());
|
|
1883
|
|
1884 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, true);
|
|
1885 e->val = new llvm::ICmpInst(llvm::ICmpInst::ICMP_EQ,b,zero,"tmp",p->scopebb());
|
|
1886 e->type = elem::VAL;
|
|
1887
|
|
1888 delete u;
|
|
1889
|
|
1890 return e;
|
|
1891 }
|
|
1892
|
|
1893 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1894
|
|
1895 elem* AndAndExp::toElem(IRState* p)
|
|
1896 {
|
|
1897 Logger::print("AndAndExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1898 LOG_SCOPE;
|
|
1899
|
|
1900 // allocate a temporary for the final result. failed to come up with a better way :/
|
|
1901 llvm::Value* resval = 0;
|
|
1902 llvm::BasicBlock* entryblock = &p->topfunc()->front();
|
|
1903 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"andandtmp",p->topallocapoint());
|
|
1904
|
|
1905 elem* e = new elem;
|
|
1906 elem* u = e1->toElem(p);
|
|
1907
|
|
1908 llvm::BasicBlock* oldend = p->scopeend();
|
|
1909 llvm::BasicBlock* andand = new llvm::BasicBlock("andand", gIR->topfunc(), oldend);
|
|
1910 llvm::BasicBlock* andandend = new llvm::BasicBlock("andandend", gIR->topfunc(), oldend);
|
|
1911
|
|
1912 llvm::Value* ubool = LLVM_DtoBoolean(u->getValue());
|
|
1913 new llvm::StoreInst(ubool,resval,p->scopebb());
|
|
1914 new llvm::BranchInst(andand,andandend,ubool,p->scopebb());
|
|
1915
|
|
1916 p->scope() = IRScope(andand, andandend);
|
|
1917 elem* v = e2->toElem(p);
|
|
1918
|
|
1919 llvm::Value* vbool = LLVM_DtoBoolean(v->getValue());
|
|
1920 llvm::Value* uandvbool = llvm::BinaryOperator::create(llvm::BinaryOperator::And, ubool, vbool,"tmp",p->scopebb());
|
|
1921 new llvm::StoreInst(uandvbool,resval,p->scopebb());
|
|
1922 new llvm::BranchInst(andandend,p->scopebb());
|
|
1923
|
|
1924 delete u;
|
|
1925 delete v;
|
|
1926
|
|
1927 p->scope() = IRScope(andandend, oldend);
|
|
1928
|
|
1929 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb());
|
|
1930 e->type = elem::VAL;
|
|
1931
|
|
1932 return e;
|
|
1933 }
|
|
1934
|
|
1935 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1936
|
|
1937 elem* OrOrExp::toElem(IRState* p)
|
|
1938 {
|
|
1939 Logger::print("OrOrExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
1940 LOG_SCOPE;
|
|
1941
|
|
1942 // allocate a temporary for the final result. failed to come up with a better way :/
|
|
1943 llvm::Value* resval = 0;
|
|
1944 llvm::BasicBlock* entryblock = &p->topfunc()->front();
|
|
1945 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"orortmp",p->topallocapoint());
|
|
1946
|
|
1947 elem* e = new elem;
|
|
1948 elem* u = e1->toElem(p);
|
|
1949
|
|
1950 llvm::BasicBlock* oldend = p->scopeend();
|
|
1951 llvm::BasicBlock* oror = new llvm::BasicBlock("oror", gIR->topfunc(), oldend);
|
|
1952 llvm::BasicBlock* ororend = new llvm::BasicBlock("ororend", gIR->topfunc(), oldend);
|
|
1953
|
|
1954 llvm::Value* ubool = LLVM_DtoBoolean(u->getValue());
|
|
1955 new llvm::StoreInst(ubool,resval,p->scopebb());
|
|
1956 new llvm::BranchInst(ororend,oror,ubool,p->scopebb());
|
|
1957
|
|
1958 p->scope() = IRScope(oror, ororend);
|
|
1959 elem* v = e2->toElem(p);
|
|
1960
|
|
1961 llvm::Value* vbool = LLVM_DtoBoolean(v->getValue());
|
|
1962 new llvm::StoreInst(vbool,resval,p->scopebb());
|
|
1963 new llvm::BranchInst(ororend,p->scopebb());
|
|
1964
|
|
1965 delete u;
|
|
1966 delete v;
|
|
1967
|
|
1968 p->scope() = IRScope(ororend, oldend);
|
|
1969
|
|
1970 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb());
|
|
1971 e->type = elem::VAL;
|
|
1972
|
|
1973 return e;
|
|
1974 }
|
|
1975
|
|
1976 //////////////////////////////////////////////////////////////////////////////////////////
|
|
1977
|
|
1978 #define BinBitExp(X,Y) \
|
|
1979 elem* X##Exp::toElem(IRState* p) \
|
|
1980 { \
|
|
1981 Logger::print("%sExp::toElem: %s | %s\n", #X, toChars(), type->toChars()); \
|
|
1982 LOG_SCOPE; \
|
|
1983 elem* e = new elem; \
|
|
1984 elem* u = e1->toElem(p); \
|
|
1985 elem* v = e2->toElem(p); \
|
|
1986 e->val = llvm::BinaryOperator::create(llvm::Instruction::Y, u->getValue(), v->getValue(), "tmp", p->scopebb()); \
|
|
1987 e->type = elem::VAL; \
|
|
1988 delete u; \
|
|
1989 delete v; \
|
|
1990 return e; \
|
|
1991 } \
|
|
1992 \
|
|
1993 elem* X##AssignExp::toElem(IRState* p) \
|
|
1994 { \
|
|
1995 Logger::print("%sAssignExp::toElem: %s | %s\n", #X, toChars(), type->toChars()); \
|
|
1996 LOG_SCOPE; \
|
|
1997 elem* u = e1->toElem(p); \
|
|
1998 elem* v = e2->toElem(p); \
|
|
1999 llvm::Value* tmp = llvm::BinaryOperator::create(llvm::Instruction::Y, u->getValue(), v->getValue(), "tmp", p->scopebb()); \
|
|
2000 Logger::cout() << *tmp << '|' << *u->mem << '\n'; \
|
|
2001 new llvm::StoreInst(LLVM_DtoPointedType(u->mem, tmp), u->mem, p->scopebb()); \
|
|
2002 delete u; \
|
|
2003 delete v; \
|
|
2004 elem* e = new elem; \
|
|
2005 e->mem = u->mem; \
|
|
2006 e->type = elem::VAR; \
|
|
2007 return e; \
|
|
2008 }
|
|
2009
|
|
2010 BinBitExp(And,And);
|
|
2011 BinBitExp(Or,Or);
|
|
2012 BinBitExp(Xor,Xor);
|
|
2013 BinBitExp(Shl,Shl);
|
|
2014 BinBitExp(Shr,AShr);
|
|
2015 BinBitExp(Ushr,LShr);
|
|
2016
|
|
2017 //////////////////////////////////////////////////////////////////////////////////////////
|
|
2018
|
|
2019 elem* HaltExp::toElem(IRState* p)
|
|
2020 {
|
|
2021 Logger::print("HaltExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
2022 LOG_SCOPE;
|
|
2023
|
|
2024 std::vector<llvm::Value*> llargs;
|
|
2025 llargs.resize(3);
|
|
2026 llargs[0] = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, false);
|
|
2027 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false);
|
|
2028 llargs[2] = llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty));
|
|
2029
|
|
2030 //Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n';
|
|
2031
|
|
2032 llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert");
|
|
2033 assert(fn);
|
|
2034 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb());
|
|
2035 call->setCallingConv(llvm::CallingConv::C);
|
|
2036
|
|
2037 //new llvm::UnreachableInst(p->scopebb());
|
|
2038
|
|
2039 return 0;
|
|
2040 }
|
|
2041
|
|
2042 //////////////////////////////////////////////////////////////////////////////////////////
|
|
2043
|
|
2044 elem* DelegateExp::toElem(IRState* p)
|
|
2045 {
|
|
2046 Logger::print("DelegateExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
2047 LOG_SCOPE;
|
|
2048
|
|
2049 elem* e = new elem;
|
|
2050 elem* u = e1->toElem(p);
|
|
2051
|
|
2052 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
|
|
2053 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false);
|
|
2054
|
|
2055 const llvm::Type* int8ptrty = llvm::PointerType::get(llvm::Type::Int8Ty);
|
|
2056
|
|
2057 llvm::Value* lval = p->toplval();
|
|
2058
|
|
2059 llvm::Value* context = new llvm::GetElementPtrInst(lval,zero,zero,"tmp",p->scopebb());
|
|
2060 llvm::Value* castcontext = new llvm::BitCastInst(u->getValue(),int8ptrty,"tmp",p->scopebb());
|
|
2061 new llvm::StoreInst(castcontext, context, p->scopebb());
|
|
2062
|
|
2063 llvm::Value* fptr = new llvm::GetElementPtrInst(lval,zero,one,"tmp",p->scopebb());
|
|
2064
|
|
2065 assert(func->llvmValue);
|
|
2066 llvm::Value* castfptr = new llvm::BitCastInst(func->llvmValue,fptr->getType()->getContainedType(0),"tmp",p->scopebb());
|
|
2067 new llvm::StoreInst(castfptr, fptr, p->scopebb());
|
|
2068
|
|
2069 e->inplace = true;
|
|
2070
|
|
2071 delete u;
|
|
2072 return e;
|
|
2073 }
|
|
2074
|
|
2075 //////////////////////////////////////////////////////////////////////////////////////////
|
|
2076
|
|
2077 elem* IdentityExp::toElem(IRState* p)
|
|
2078 {
|
|
2079 Logger::print("IdentityExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
2080 LOG_SCOPE;
|
|
2081
|
|
2082 elem* u = e1->toElem(p);
|
|
2083 elem* v = e2->toElem(p);
|
|
2084
|
|
2085 elem* e = new elem;
|
|
2086
|
|
2087 llvm::Value* l = u->getValue();
|
|
2088 llvm::Value* r = 0;
|
|
2089 if (v->type == elem::NUL)
|
|
2090 r = llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(l->getType()));
|
|
2091 else
|
|
2092 r = v->getValue();
|
|
2093
|
|
2094 llvm::ICmpInst::Predicate pred = (op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE;
|
|
2095 e->val = new llvm::ICmpInst(pred, l, r, "tmp", p->scopebb());
|
|
2096 e->type = elem::VAL;
|
|
2097
|
|
2098 delete u;
|
|
2099 delete v;
|
|
2100
|
|
2101 return e;
|
|
2102 }
|
|
2103
|
|
2104 //////////////////////////////////////////////////////////////////////////////////////////
|
|
2105
|
|
2106 elem* CommaExp::toElem(IRState* p)
|
|
2107 {
|
|
2108 Logger::print("CommaExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
2109 LOG_SCOPE;
|
|
2110
|
|
2111 elem* u = e1->toElem(p);
|
|
2112 elem* v = e2->toElem(p);
|
|
2113 delete u;
|
|
2114 return v;
|
|
2115 }
|
|
2116
|
|
2117 //////////////////////////////////////////////////////////////////////////////////////////
|
|
2118
|
|
2119 elem* CondExp::toElem(IRState* p)
|
|
2120 {
|
|
2121 Logger::print("CondExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
2122 LOG_SCOPE;
|
|
2123
|
|
2124 const llvm::Type* resty = LLVM_DtoType(type);
|
|
2125
|
|
2126 // allocate a temporary for the final result. failed to come up with a better way :/
|
|
2127 llvm::BasicBlock* entryblock = &p->topfunc()->front();
|
|
2128 llvm::Value* resval = new llvm::AllocaInst(resty,"condtmp",p->topallocapoint());
|
|
2129
|
|
2130 llvm::BasicBlock* oldend = p->scopeend();
|
|
2131 llvm::BasicBlock* condtrue = new llvm::BasicBlock("condtrue", gIR->topfunc(), oldend);
|
|
2132 llvm::BasicBlock* condfalse = new llvm::BasicBlock("condfalse", gIR->topfunc(), oldend);
|
|
2133 llvm::BasicBlock* condend = new llvm::BasicBlock("condend", gIR->topfunc(), oldend);
|
|
2134
|
|
2135 elem* c = econd->toElem(p);
|
|
2136 llvm::Value* cond_val = LLVM_DtoBoolean(c->getValue());
|
|
2137 delete c;
|
|
2138 new llvm::BranchInst(condtrue,condfalse,cond_val,p->scopebb());
|
|
2139
|
|
2140 p->scope() = IRScope(condtrue, condfalse);
|
|
2141 elem* u = e1->toElem(p);
|
|
2142 Logger::cout() << *u->val << '|' << *resval << '\n'; \
|
|
2143 new llvm::StoreInst(u->getValue(),resval,p->scopebb());
|
|
2144 new llvm::BranchInst(condend,p->scopebb());
|
|
2145 delete u;
|
|
2146
|
|
2147 p->scope() = IRScope(condfalse, condend);
|
|
2148 elem* v = e2->toElem(p);
|
|
2149 new llvm::StoreInst(v->getValue(),resval,p->scopebb());
|
|
2150 new llvm::BranchInst(condend,p->scopebb());
|
|
2151 delete v;
|
|
2152
|
|
2153 p->scope() = IRScope(condend, oldend);
|
|
2154
|
|
2155 elem* e = new elem;
|
|
2156 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb());
|
|
2157 e->type = elem::VAL;
|
|
2158 return e;
|
|
2159 }
|
|
2160
|
|
2161 //////////////////////////////////////////////////////////////////////////////////////////
|
|
2162
|
|
2163 elem* ComExp::toElem(IRState* p)
|
|
2164 {
|
|
2165 Logger::print("ComExp::toElem: %s | %s\n", toChars(), type->toChars());
|
|
2166 LOG_SCOPE;
|
|
2167
|
|
2168 elem* e = new elem;
|
|
2169 elem* u = e1->toElem(p);
|
|
2170
|
|
2171 llvm::Value* value = u->getValue();
|
|
2172 llvm::Value* minusone = llvm::ConstantInt::get(value->getType(), -1, true);
|
|
2173 e->val = llvm::BinaryOperator::create(llvm::Instruction::Xor, value, minusone, "tmp", p->scopebb());
|
|
2174
|
|
2175 delete u;
|
|
2176
|
|
2177 e->type = elem::VAL;
|
|
2178
|
|
2179 return e;
|
|
2180 }
|
|
2181
|
|
2182 //////////////////////////////////////////////////////////////////////////////////////////
|
|
2183
|
|
2184 #define STUB(x) elem *x::toElem(IRState * p) {error("Exp type "#x" not implemented: %s", toChars()); fatal(); return 0; }
|
|
2185 //STUB(IdentityExp);
|
|
2186 //STUB(CondExp);
|
|
2187 //STUB(EqualExp);
|
|
2188 STUB(InExp);
|
|
2189 //STUB(CmpExp);
|
|
2190 //STUB(AndAndExp);
|
|
2191 //STUB(OrOrExp);
|
|
2192 //STUB(AndExp);
|
|
2193 //STUB(AndAssignExp);
|
|
2194 //STUB(OrExp);
|
|
2195 //STUB(OrAssignExp);
|
|
2196 //STUB(XorExp);
|
|
2197 //STUB(XorAssignExp);
|
|
2198 //STUB(ShrExp);
|
|
2199 //STUB(ShrAssignExp);
|
|
2200 //STUB(ShlExp);
|
|
2201 //STUB(ShlAssignExp);
|
|
2202 //STUB(UshrExp);
|
|
2203 //STUB(UshrAssignExp);
|
|
2204 //STUB(DivExp);
|
|
2205 //STUB(DivAssignExp);
|
|
2206 //STUB(MulExp);
|
|
2207 //STUB(MulAssignExp);
|
|
2208 //STUB(ModExp);
|
|
2209 //STUB(ModAssignExp);
|
|
2210 STUB(CatExp);
|
|
2211 STUB(CatAssignExp);
|
|
2212 //STUB(AddExp);
|
|
2213 //STUB(AddAssignExp);
|
|
2214 STUB(Expression);
|
|
2215 //STUB(MinExp);
|
|
2216 //STUB(MinAssignExp);
|
|
2217 //STUB(PostExp);
|
|
2218 //STUB(NullExp);
|
|
2219 //STUB(ThisExp);
|
|
2220 //STUB(CallExp);
|
|
2221 STUB(DotTypeExp);
|
|
2222 STUB(TypeDotIdExp);
|
|
2223 //STUB(DotVarExp);
|
|
2224 //STUB(AssertExp);
|
|
2225 STUB(FuncExp);
|
|
2226 //STUB(DelegateExp);
|
|
2227 //STUB(VarExp);
|
|
2228 //STUB(DeclarationExp);
|
|
2229 //STUB(NewExp);
|
|
2230 //STUB(SymOffExp);
|
|
2231 STUB(ScopeExp);
|
|
2232 //STUB(AssignExp);
|
|
2233
|
|
2234 STUB(TypeExp);
|
|
2235 //STUB(RealExp);
|
|
2236 STUB(ComplexExp);
|
|
2237 //STUB(StringExp);
|
|
2238 //STUB(IntegerExp);
|
|
2239 STUB(BoolExp);
|
|
2240
|
|
2241 //STUB(NotExp);
|
|
2242 //STUB(ComExp);
|
|
2243 STUB(NegExp);
|
|
2244 //STUB(PtrExp);
|
|
2245 //STUB(AddrExp);
|
|
2246 //STUB(SliceExp);
|
|
2247 //STUB(CastExp);
|
|
2248 //STUB(DeleteExp);
|
|
2249 //STUB(IndexExp);
|
|
2250 //STUB(CommaExp);
|
|
2251 //STUB(ArrayLengthExp);
|
|
2252 //STUB(HaltExp);
|
|
2253 STUB(RemoveExp);
|
|
2254 STUB(ArrayLiteralExp);
|
|
2255 STUB(AssocArrayLiteralExp);
|
|
2256 //STUB(StructLiteralExp);
|
|
2257
|
|
2258 unsigned Type::totym() { return 0; }
|
|
2259
|
|
2260 type *
|
|
2261 Type::toCtype() {
|
|
2262 return 0;
|
|
2263 }
|
|
2264
|
|
2265 type * Type::toCParamtype()
|
|
2266 {
|
|
2267 return 0;
|
|
2268 }
|
|
2269 Symbol * Type::toSymbol()
|
|
2270 {
|
|
2271 return 0;
|
|
2272 }
|
|
2273
|
|
2274 type *
|
|
2275 TypeTypedef::toCtype()
|
|
2276 {
|
|
2277 return 0;
|
|
2278 }
|
|
2279
|
|
2280 type *
|
|
2281 TypeTypedef::toCParamtype()
|
|
2282 {
|
|
2283 return 0;
|
|
2284 }
|
|
2285
|
|
2286 void
|
|
2287 TypedefDeclaration::toDebug()
|
|
2288 {
|
|
2289 }
|
|
2290
|
|
2291
|
|
2292 type *
|
|
2293 TypeEnum::toCtype()
|
|
2294 {
|
|
2295 return 0;
|
|
2296 }
|
|
2297
|
|
2298 type *
|
|
2299 TypeStruct::toCtype()
|
|
2300 {
|
|
2301 return 0;
|
|
2302 }
|
|
2303
|
|
2304 void
|
|
2305 StructDeclaration::toDebug()
|
|
2306 {
|
|
2307 }
|
|
2308
|
|
2309 Symbol * TypeClass::toSymbol()
|
|
2310 {
|
|
2311 return 0;
|
|
2312 }
|
|
2313
|
|
2314 unsigned TypeFunction::totym()
|
|
2315 {
|
|
2316 return 0;
|
|
2317 }
|
|
2318
|
|
2319 type *
|
|
2320 TypeFunction::toCtype()
|
|
2321 {
|
|
2322 return 0;
|
|
2323 }
|
|
2324
|
|
2325 type *
|
|
2326 TypeSArray::toCtype()
|
|
2327 {
|
|
2328 return 0;
|
|
2329 }
|
|
2330
|
|
2331 type *TypeSArray::toCParamtype() { return 0; }
|
|
2332
|
|
2333 type *
|
|
2334 TypeDArray::toCtype()
|
|
2335 {
|
|
2336 return 0;
|
|
2337 }
|
|
2338
|
|
2339 type *
|
|
2340 TypeAArray::toCtype()
|
|
2341 {
|
|
2342 return 0;
|
|
2343 }
|
|
2344
|
|
2345 type *
|
|
2346 TypePointer::toCtype()
|
|
2347 {
|
|
2348 return 0;
|
|
2349 }
|
|
2350
|
|
2351 type *
|
|
2352 TypeDelegate::toCtype()
|
|
2353 {
|
|
2354 return 0;
|
|
2355 }
|
|
2356
|
|
2357 type *
|
|
2358 TypeClass::toCtype()
|
|
2359 {
|
|
2360 return 0;
|
|
2361 }
|
|
2362
|
|
2363 void
|
|
2364 ClassDeclaration::toDebug()
|
|
2365 {
|
|
2366 }
|
|
2367
|
|
2368 /* --------------------------------------------------------------------------------------- */
|
|
2369
|
|
2370 void CompoundStatement::toIR(IRState* p)
|
|
2371 {
|
|
2372 static int csi = 0;
|
|
2373 Logger::println("CompoundStatement::toIR(%d):\n<<<\n%s>>>", csi++, toChars());
|
|
2374 LOG_SCOPE;
|
|
2375
|
|
2376 /*
|
|
2377 const char* labelname;
|
|
2378 bool insterm = false;
|
|
2379
|
|
2380 if (!p->scopes()) {
|
|
2381 labelname = "bb";
|
|
2382 insterm = true;
|
|
2383 }
|
|
2384 else
|
|
2385 labelname = "entry";
|
|
2386
|
|
2387 //if (!llvm::isa<llvm::TerminatorInst>(p->topfunc()->back().back()))
|
|
2388 // insterm = true;
|
|
2389
|
|
2390 llvm::BasicBlock* bb = new llvm::BasicBlock(labelname, p->topfunc());
|
|
2391
|
|
2392 if (insterm) {
|
|
2393 new llvm::BranchInst(bb,p->topbb());
|
|
2394 }
|
|
2395
|
|
2396 p->bbs.push(bb);
|
|
2397 */
|
|
2398
|
|
2399 size_t n = statements->dim;
|
|
2400 for (size_t i=0; i<n; i++)
|
|
2401 {
|
|
2402 Statement* s = (Statement*)statements->data[i];
|
|
2403 if (s)
|
|
2404 s->toIR(p);
|
|
2405 else
|
|
2406 Logger::println("NULL statement found in CompoundStatement !! :S");
|
|
2407 }
|
|
2408
|
|
2409 //p->bbs.pop();
|
|
2410 }
|
|
2411
|
|
2412 void ReturnStatement::toIR(IRState* p)
|
|
2413 {
|
|
2414 static int rsi = 0;
|
|
2415 Logger::println("ReturnStatement::toIR(%d): %s", rsi++, toChars());
|
|
2416 LOG_SCOPE;
|
|
2417
|
|
2418 if (exp)
|
|
2419 {
|
|
2420 TY expty = exp->type->ty;
|
|
2421 if (p->topfunc()->getReturnType() == llvm::Type::VoidTy) {
|
|
2422 assert(expty == Tstruct || expty == Tdelegate || expty == Tarray);
|
|
2423
|
|
2424 TypeFunction* f = p->topfunctype();
|
|
2425 assert(f->llvmRetInPtr && f->llvmRetArg);
|
|
2426
|
|
2427 p->lvals.push_back(f->llvmRetArg);
|
|
2428 elem* e = exp->toElem(p);
|
|
2429 p->lvals.pop_back();
|
|
2430
|
|
2431 // structliterals do this themselves
|
|
2432 // also they dont produce any value
|
|
2433 if (expty == Tstruct) {
|
|
2434 if (!e->inplace) {
|
|
2435 TypeStruct* ts = (TypeStruct*)exp->type;
|
|
2436 assert(e->mem);
|
|
2437 LLVM_DtoStructCopy(ts,f->llvmRetArg,e->mem);
|
|
2438 }
|
|
2439 }
|
|
2440 else if (expty == Tdelegate) {
|
|
2441 // do nothing, handled by the DelegateExp
|
|
2442 LLVM_DtoDelegateCopy(f->llvmRetArg,e->mem);
|
|
2443 }
|
|
2444 else if (expty == Tarray) {
|
|
2445 if (e->type == elem::SLICE) {
|
|
2446 LLVM_DtoSetArray(f->llvmRetArg,e->arg,e->mem);
|
|
2447 }
|
|
2448 // else the return value is a variable and should already have been assigned by now
|
|
2449 }
|
|
2450 else
|
|
2451 assert(0);
|
|
2452
|
|
2453 new llvm::ReturnInst(p->scopebb());
|
|
2454 delete e;
|
|
2455 }
|
|
2456 else {
|
|
2457 elem* e = exp->toElem(p);
|
|
2458 llvm::Value* v = e->getValue();
|
|
2459 Logger::cout() << *v << '\n';
|
|
2460 new llvm::ReturnInst(v, p->scopebb());
|
|
2461 delete e;
|
|
2462 }
|
|
2463 }
|
|
2464 else
|
|
2465 {
|
|
2466 if (p->topfunc()->getReturnType() == llvm::Type::VoidTy)
|
|
2467 new llvm::ReturnInst(p->scopebb());
|
|
2468 else
|
|
2469 new llvm::UnreachableInst(p->scopebb());
|
|
2470 }
|
|
2471
|
|
2472 p->scope().returned = true;
|
|
2473 }
|
|
2474
|
|
2475 void ExpStatement::toIR(IRState* p)
|
|
2476 {
|
|
2477 static int esi = 0;
|
|
2478 Logger::println("ExpStatement::toIR(%d): %s", esi++, toChars());
|
|
2479 LOG_SCOPE;
|
|
2480
|
|
2481 if (exp != 0) {
|
|
2482 elem* e = exp->toElem(p);
|
|
2483 delete e;
|
|
2484 }
|
|
2485 /*elem* e = exp->toElem(p);
|
|
2486 p->buf.printf("%s", e->toChars());
|
|
2487 delete e;
|
|
2488 p->buf.writenl();*/
|
|
2489 }
|
|
2490
|
|
2491 void IfStatement::toIR(IRState* p)
|
|
2492 {
|
|
2493 static int wsi = 0;
|
|
2494 Logger::println("IfStatement::toIR(%d): %s", wsi++, toChars());
|
|
2495 LOG_SCOPE;
|
|
2496
|
|
2497 elem* cond_e = condition->toElem(p);
|
|
2498 llvm::Value* cond_val = cond_e->getValue();
|
|
2499 delete cond_e;
|
|
2500
|
|
2501 llvm::BasicBlock* oldend = gIR->scopeend();
|
|
2502
|
|
2503 llvm::BasicBlock* ifbb = new llvm::BasicBlock("if", gIR->topfunc(), oldend);
|
|
2504 llvm::BasicBlock* endbb = new llvm::BasicBlock("endif", gIR->topfunc(), oldend);
|
|
2505 llvm::BasicBlock* elsebb = 0;
|
|
2506 if (elsebody) {
|
|
2507 elsebb = new llvm::BasicBlock("else", gIR->topfunc(), endbb);
|
|
2508 }
|
|
2509 else {
|
|
2510 elsebb = endbb;
|
|
2511 }
|
|
2512
|
|
2513 if (cond_val->getType() != llvm::Type::Int1Ty) {
|
|
2514 Logger::cout() << "if conditional: " << *cond_val << '\n';
|
|
2515 cond_val = LLVM_DtoBoolean(cond_val);
|
|
2516 }
|
|
2517 llvm::Value* ifgoback = new llvm::BranchInst(ifbb, elsebb, cond_val, gIR->scopebegin());
|
|
2518
|
|
2519 // replace current scope
|
|
2520 gIR->scope() = IRScope(ifbb,elsebb);
|
|
2521
|
|
2522 bool endifUsed = false;
|
|
2523
|
|
2524 // do scoped statements
|
|
2525 ifbody->toIR(p);
|
|
2526 if (!gIR->scopereturned()) {
|
|
2527 new llvm::BranchInst(endbb,gIR->scopebegin());
|
|
2528 endifUsed = true;
|
|
2529 }
|
|
2530
|
|
2531 if (elsebody) {
|
|
2532 //assert(0);
|
|
2533 gIR->scope() = IRScope(elsebb,endbb);
|
|
2534 elsebody->toIR(p);
|
|
2535 if (!gIR->scopereturned()) {
|
|
2536 new llvm::BranchInst(endbb,gIR->scopebegin());
|
|
2537 endifUsed = true;
|
|
2538 }
|
|
2539 }
|
|
2540
|
|
2541 // rewrite the scope
|
|
2542 gIR->scope() = IRScope(endbb,oldend);
|
|
2543 }
|
|
2544
|
|
2545 void ScopeStatement::toIR(IRState* p)
|
|
2546 {
|
|
2547 static int wsi = 0;
|
|
2548 Logger::println("ScopeStatement::toIR(%d): %s", wsi++, toChars());
|
|
2549 LOG_SCOPE;
|
|
2550
|
|
2551 llvm::BasicBlock* oldend = gIR->scopeend();
|
|
2552
|
|
2553 IRScope irs;
|
|
2554 irs.begin = new llvm::BasicBlock("scope", gIR->topfunc(), oldend);
|
|
2555 irs.end = new llvm::BasicBlock("endscope", gIR->topfunc(), oldend);
|
|
2556
|
|
2557 // pass the previous BB into this
|
|
2558 new llvm::BranchInst(irs.begin, gIR->scopebegin());
|
|
2559
|
|
2560 gIR->scope() = irs;
|
|
2561
|
|
2562 statement->toIR(p);
|
|
2563 if (!gIR->scopereturned()) {
|
|
2564 new llvm::BranchInst(irs.end, gIR->scopebegin());
|
|
2565 }
|
|
2566
|
|
2567 // rewrite the scope
|
|
2568 gIR->scope() = IRScope(irs.end,oldend);
|
|
2569 }
|
|
2570
|
|
2571 void WhileStatement::toIR(IRState* p)
|
|
2572 {
|
|
2573 static int wsi = 0;
|
|
2574 Logger::println("WhileStatement::toIR(%d): %s", wsi++, toChars());
|
|
2575 LOG_SCOPE;
|
|
2576
|
|
2577 // create while blocks
|
|
2578 llvm::BasicBlock* oldend = gIR->scopeend();
|
|
2579 llvm::BasicBlock* whilebb = new llvm::BasicBlock("whilecond", gIR->topfunc(), oldend);
|
|
2580 llvm::BasicBlock* endbb = new llvm::BasicBlock("endwhile", gIR->topfunc(), oldend);
|
|
2581
|
|
2582 // move into the while block
|
|
2583 new llvm::BranchInst(whilebb, gIR->scopebegin());
|
|
2584
|
|
2585 // replace current scope
|
|
2586 gIR->scope() = IRScope(whilebb,endbb);
|
|
2587
|
|
2588 // create the condition
|
|
2589 elem* cond_e = condition->toElem(p);
|
|
2590 llvm::Value* cond_val = LLVM_DtoBoolean(cond_e->getValue());
|
|
2591 delete cond_e;
|
|
2592
|
|
2593 // while body block
|
|
2594 llvm::BasicBlock* whilebodybb = new llvm::BasicBlock("whilebody", gIR->topfunc(), endbb);
|
|
2595
|
|
2596 // conditional branch
|
|
2597 llvm::Value* ifbreak = new llvm::BranchInst(whilebodybb, endbb, cond_val, whilebb);
|
|
2598
|
|
2599 // rewrite scope
|
|
2600 gIR->scope() = IRScope(whilebodybb,endbb);
|
|
2601
|
|
2602 // do while body code
|
|
2603 body->toIR(p);
|
|
2604
|
|
2605 // loop
|
|
2606 new llvm::BranchInst(whilebb, gIR->scopebegin());
|
|
2607
|
|
2608 // rewrite the scope
|
|
2609 gIR->scope() = IRScope(endbb,oldend);
|
|
2610 }
|
|
2611
|
|
2612 void DoStatement::toIR(IRState* p)
|
|
2613 {
|
|
2614 static int wsi = 0;
|
|
2615 Logger::println("DoStatement::toIR(%d): %s", wsi++, toChars());
|
|
2616 LOG_SCOPE;
|
|
2617
|
|
2618 // create while blocks
|
|
2619 llvm::BasicBlock* oldend = gIR->scopeend();
|
|
2620 llvm::BasicBlock* dowhilebb = new llvm::BasicBlock("dowhile", gIR->topfunc(), oldend);
|
|
2621 llvm::BasicBlock* endbb = new llvm::BasicBlock("enddowhile", gIR->topfunc(), oldend);
|
|
2622
|
|
2623 // move into the while block
|
|
2624 new llvm::BranchInst(dowhilebb, gIR->scopebegin());
|
|
2625
|
|
2626 // replace current scope
|
|
2627 gIR->scope() = IRScope(dowhilebb,endbb);
|
|
2628
|
|
2629 // do do-while body code
|
|
2630 body->toIR(p);
|
|
2631
|
|
2632 // create the condition
|
|
2633 elem* cond_e = condition->toElem(p);
|
|
2634 llvm::Value* cond_val = LLVM_DtoBoolean(cond_e->getValue());
|
|
2635 delete cond_e;
|
|
2636
|
|
2637 // conditional branch
|
|
2638 llvm::Value* ifbreak = new llvm::BranchInst(dowhilebb, endbb, cond_val, gIR->scopebegin());
|
|
2639
|
|
2640 // rewrite the scope
|
|
2641 gIR->scope() = IRScope(endbb,oldend);
|
|
2642 }
|
|
2643
|
|
2644 void ForStatement::toIR(IRState* p)
|
|
2645 {
|
|
2646 static int wsi = 0;
|
|
2647 Logger::println("ForStatement::toIR(%d): %s", wsi++, toChars());
|
|
2648 LOG_SCOPE;
|
|
2649
|
|
2650 // create for blocks
|
|
2651 llvm::BasicBlock* oldend = gIR->scopeend();
|
|
2652 llvm::BasicBlock* forbb = new llvm::BasicBlock("forcond", gIR->topfunc(), oldend);
|
|
2653 llvm::BasicBlock* forbodybb = new llvm::BasicBlock("forbody", gIR->topfunc(), oldend);
|
|
2654 llvm::BasicBlock* forincbb = new llvm::BasicBlock("forinc", gIR->topfunc(), oldend);
|
|
2655 llvm::BasicBlock* endbb = new llvm::BasicBlock("endfor", gIR->topfunc(), oldend);
|
|
2656
|
|
2657 // init
|
|
2658 if (init != 0)
|
|
2659 init->toIR(p);
|
|
2660
|
|
2661 // move into the for condition block, ie. start the loop
|
|
2662 new llvm::BranchInst(forbb, gIR->scopebegin());
|
|
2663
|
|
2664 IRScope loop;
|
|
2665 loop.begin = forincbb;
|
|
2666 loop.end = endbb;
|
|
2667 p->loopbbs.push_back(loop);
|
|
2668
|
|
2669 // replace current scope
|
|
2670 gIR->scope() = IRScope(forbb,forbodybb);
|
|
2671
|
|
2672 // create the condition
|
|
2673 elem* cond_e = condition->toElem(p);
|
|
2674 llvm::Value* cond_val = LLVM_DtoBoolean(cond_e->getValue());
|
|
2675 delete cond_e;
|
|
2676
|
|
2677 // conditional branch
|
|
2678 llvm::Value* ifbreak = new llvm::BranchInst(forbodybb, endbb, cond_val, forbb);
|
|
2679
|
|
2680 // rewrite scope
|
|
2681 gIR->scope() = IRScope(forbodybb,forincbb);
|
|
2682
|
|
2683 // do for body code
|
|
2684 body->toIR(p);
|
|
2685
|
|
2686 // move into the for increment block
|
|
2687 new llvm::BranchInst(forincbb, gIR->scopebegin());
|
|
2688 gIR->scope() = IRScope(forincbb, endbb);
|
|
2689
|
|
2690 // increment
|
|
2691 if (increment) {
|
|
2692 elem* inc = increment->toElem(p);
|
|
2693 delete inc;
|
|
2694 }
|
|
2695
|
|
2696 // loop
|
|
2697 new llvm::BranchInst(forbb, gIR->scopebegin());
|
|
2698
|
|
2699 p->loopbbs.pop_back();
|
|
2700
|
|
2701 // rewrite the scope
|
|
2702 gIR->scope() = IRScope(endbb,oldend);
|
|
2703 }
|
|
2704
|
|
2705 void BreakStatement::toIR(IRState* p)
|
|
2706 {
|
|
2707 static int wsi = 0;
|
|
2708 Logger::println("BreakStatement::toIR(%d): %s", wsi++, toChars());
|
|
2709 LOG_SCOPE;
|
|
2710
|
|
2711 if (ident != 0) {
|
|
2712 Logger::println("ident = %s", ident->toChars());
|
|
2713 assert(0);
|
|
2714 }
|
|
2715 else {
|
|
2716 new llvm::BranchInst(gIR->loopbbs.back().end, gIR->scopebegin());
|
|
2717 }
|
|
2718 }
|
|
2719
|
|
2720 void ContinueStatement::toIR(IRState* p)
|
|
2721 {
|
|
2722 static int wsi = 0;
|
|
2723 Logger::println("ContinueStatement::toIR(%d): %s", wsi++, toChars());
|
|
2724 LOG_SCOPE;
|
|
2725
|
|
2726 if (ident != 0) {
|
|
2727 Logger::println("ident = %s", ident->toChars());
|
|
2728 assert(0);
|
|
2729 }
|
|
2730 else {
|
|
2731 new llvm::BranchInst(gIR->loopbbs.back().begin, gIR->scopebegin());
|
|
2732 }
|
|
2733 }
|
|
2734
|
|
2735 void OnScopeStatement::toIR(IRState* p)
|
|
2736 {
|
|
2737 static int wsi = 0;
|
|
2738 Logger::println("OnScopeStatement::toIR(%d): %s", wsi++, toChars());
|
|
2739 LOG_SCOPE;
|
|
2740
|
|
2741 assert(statement);
|
|
2742 //statement->toIR(p); // this seems to be redundant
|
|
2743 }
|
|
2744
|
|
2745 void TryFinallyStatement::toIR(IRState* p)
|
|
2746 {
|
|
2747 static int wsi = 0;
|
|
2748 Logger::println("TryFinallyStatement::toIR(%d): %s", wsi++, toChars());
|
|
2749 LOG_SCOPE;
|
|
2750
|
|
2751 llvm::BasicBlock* oldend = gIR->scopeend();
|
|
2752
|
|
2753 llvm::BasicBlock* trybb = new llvm::BasicBlock("try", gIR->topfunc(), oldend);
|
|
2754 llvm::BasicBlock* finallybb = new llvm::BasicBlock("finally", gIR->topfunc(), oldend);
|
|
2755 llvm::BasicBlock* endbb = new llvm::BasicBlock("endtryfinally", gIR->topfunc(), oldend);
|
|
2756
|
|
2757 // pass the previous BB into this
|
|
2758 new llvm::BranchInst(trybb, gIR->scopebegin());
|
|
2759
|
|
2760 gIR->scope() = IRScope(trybb,finallybb);
|
|
2761
|
|
2762 assert(body);
|
|
2763 body->toIR(p);
|
|
2764 new llvm::BranchInst(finallybb, gIR->scopebegin());
|
|
2765
|
|
2766 // rewrite the scope
|
|
2767 gIR->scope() = IRScope(finallybb,endbb);
|
|
2768
|
|
2769 assert(finalbody);
|
|
2770 finalbody->toIR(p);
|
|
2771 new llvm::BranchInst(endbb, gIR->scopebegin());
|
|
2772
|
|
2773 // rewrite the scope
|
|
2774 gIR->scope() = IRScope(endbb,oldend);
|
|
2775 }
|
|
2776
|
|
2777 void TryCatchStatement::toIR(IRState* p)
|
|
2778 {
|
|
2779 static int wsi = 0;
|
|
2780 Logger::println("TryCatchStatement::toIR(%d): %s", wsi++, toChars());
|
|
2781 LOG_SCOPE;
|
|
2782
|
|
2783 assert(0 && "try-catch is not properly");
|
|
2784
|
|
2785 assert(body);
|
|
2786 body->toIR(p);
|
|
2787
|
|
2788 assert(catches);
|
|
2789 for(size_t i=0; i<catches->dim; ++i)
|
|
2790 {
|
|
2791 Catch* c = (Catch*)catches->data[i];
|
|
2792 c->handler->toIR(p);
|
|
2793 }
|
|
2794 }
|
|
2795
|
|
2796 void ThrowStatement::toIR(IRState* p)
|
|
2797 {
|
|
2798 static int wsi = 0;
|
|
2799 Logger::println("ThrowStatement::toIR(%d): %s", wsi++, toChars());
|
|
2800 LOG_SCOPE;
|
|
2801
|
|
2802 assert(0 && "throw is not implemented");
|
|
2803
|
|
2804 assert(exp);
|
|
2805 elem* e = exp->toElem(p);
|
|
2806 delete e;
|
|
2807 }
|
|
2808
|
|
2809 #define STUBST(x) void x::toIR(IRState * p) {error("Statement type "#x" not implemented: %s", toChars());fatal();}
|
|
2810 //STUBST(BreakStatement);
|
|
2811 //STUBST(ForStatement);
|
|
2812 STUBST(WithStatement);
|
|
2813 STUBST(SynchronizedStatement);
|
|
2814 //STUBST(ReturnStatement);
|
|
2815 //STUBST(ContinueStatement);
|
|
2816 STUBST(DefaultStatement);
|
|
2817 STUBST(CaseStatement);
|
|
2818 STUBST(SwitchStatement);
|
|
2819 STUBST(SwitchErrorStatement);
|
|
2820 STUBST(Statement);
|
|
2821 //STUBST(IfStatement);
|
|
2822 STUBST(ForeachStatement);
|
|
2823 //STUBST(DoStatement);
|
|
2824 //STUBST(WhileStatement);
|
|
2825 //STUBST(ExpStatement);
|
|
2826 //STUBST(CompoundStatement);
|
|
2827 //STUBST(ScopeStatement);
|
|
2828 STUBST(AsmStatement);
|
|
2829 //STUBST(TryCatchStatement);
|
|
2830 //STUBST(TryFinallyStatement);
|
|
2831 STUBST(VolatileStatement);
|
|
2832 STUBST(LabelStatement);
|
|
2833 //STUBST(ThrowStatement);
|
|
2834 STUBST(GotoCaseStatement);
|
|
2835 STUBST(GotoDefaultStatement);
|
|
2836 STUBST(GotoStatement);
|
|
2837 STUBST(UnrolledLoopStatement);
|
|
2838 //STUBST(OnScopeStatement);
|
|
2839
|
|
2840
|
|
2841 void
|
|
2842 EnumDeclaration::toDebug()
|
|
2843 {
|
|
2844
|
|
2845 }
|
|
2846
|
|
2847 int
|
|
2848 Dsymbol::cvMember(unsigned char*)
|
|
2849 {
|
|
2850 return 0;
|
|
2851 }
|
|
2852 int
|
|
2853 EnumDeclaration::cvMember(unsigned char*)
|
|
2854 {
|
|
2855 return 0;
|
|
2856 }
|
|
2857 int
|
|
2858 FuncDeclaration::cvMember(unsigned char*)
|
|
2859 {
|
|
2860 return 0;
|
|
2861 }
|
|
2862 int
|
|
2863 VarDeclaration::cvMember(unsigned char*)
|
|
2864 {
|
|
2865 return 0;
|
|
2866 }
|
|
2867 int
|
|
2868 TypedefDeclaration::cvMember(unsigned char*)
|
|
2869 {
|
|
2870 return 0;
|
|
2871 }
|
|
2872
|
|
2873 void obj_includelib(char*){}
|
|
2874
|
|
2875 AsmStatement::AsmStatement(Loc loc, Token *tokens) :
|
|
2876 Statement(loc)
|
|
2877 {
|
|
2878 }
|
|
2879 Statement *AsmStatement::syntaxCopy() {
|
|
2880 return 0;
|
|
2881 }
|
|
2882
|
|
2883 Statement *AsmStatement::semantic(Scope *sc)
|
|
2884 {
|
|
2885 return Statement::semantic(sc);
|
|
2886 }
|
|
2887
|
|
2888
|
|
2889 void AsmStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
2890 {
|
|
2891 Statement::toCBuffer(buf, hgs);
|
|
2892 }
|
|
2893
|
|
2894 int AsmStatement::comeFrom()
|
|
2895 {
|
|
2896 return FALSE;
|
|
2897 }
|
|
2898
|
|
2899 void
|
|
2900 backend_init()
|
|
2901 {
|
|
2902 //LLVM_D_InitRuntime();
|
|
2903 // lazily loaded
|
|
2904 }
|
|
2905
|
|
2906 void
|
|
2907 backend_term()
|
|
2908 {
|
|
2909 LLVM_D_FreeRuntime();
|
|
2910 }
|