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