Mercurial > projects > ldc
annotate gen/toir.c @ 8:5e69b77a5c51 trunk
[svn r12] fixed accessing aggregate fields of aggregates
removed some useless branches for successive scopes ala {}{}{}
author | lindquist |
---|---|
date | Thu, 27 Sep 2007 06:03:06 +0200 |
parents | 35d93ce68cf4 |
children | dafae18f9c08 |
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) { | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
437 //assert(l->field); |
1 | 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; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
443 std::vector<unsigned> offsets(1,0); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
444 ts->sym->offsetToIndex(cofs->getZExtValue(), offsets); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
445 e->mem = LLVM_DtoGEP(l->getValue(), offsets, "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; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1129 std::vector<unsigned> dst(1,0); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1130 vdt->sym->offsetToIndex(offset, dst); |
1 | 1131 llvm::Value* ptr = vd->llvmValue; |
1132 assert(ptr); | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1133 e->mem = LLVM_DtoGEP(ptr,dst,"tmp",p->scopebb()); |
1 | 1134 e->type = elem::VAL; |
1135 e->field = true; | |
1136 } | |
1137 else if (vd->type->ty == Tsarray) { | |
1138 /*e = new elem; | |
1139 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1140 e->val = new llvm::GetElementPtrInst(vd->llvmValue,idx0,idx0,"tmp",p->scopebb());*/ | |
1141 e = new elem; | |
1142 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1143 //llvm::Value* idx1 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
6 | 1144 e->mem = LLVM_DtoGEP(vd->llvmValue,idx0,idx0,"tmp",p->scopebb()); |
1 | 1145 e->type = elem::VAL; |
1146 } | |
1147 else if (offset == 0) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1148 /*if (!vd->llvmValue) |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1149 vd->toObjFile();*/ |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1150 assert(vd->llvmValue); |
1 | 1151 e = new elem; |
1152 e->mem = vd->llvmValue; | |
1153 //e->vardecl = vd; | |
1154 e->type = elem::VAL; | |
1155 } | |
1156 else { | |
1157 assert(0); | |
1158 } | |
1159 } | |
1160 else if (FuncDeclaration* fd = var->isFuncDeclaration()) | |
1161 { | |
1162 Logger::println("FuncDeclaration"); | |
1163 e = new elem; | |
1164 if (fd->llvmValue == 0) | |
1165 fd->toObjFile(); | |
1166 e->val = fd->llvmValue; | |
1167 //e->aspointer = true; | |
1168 e->type = elem::FUNC; | |
1169 } | |
1170 assert(e != 0); | |
1171 assert(e->type != elem::NONE); | |
1172 return e; | |
1173 } | |
1174 | |
1175 ////////////////////////////////////////////////////////////////////////////////////////// | |
1176 | |
1177 elem* PtrExp::toElem(IRState* p) | |
1178 { | |
1179 Logger::print("PtrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1180 LOG_SCOPE; | |
1181 elem* e = new elem; | |
1182 elem* a = e1->toElem(p); | |
1183 | |
1184 if (a->mem) | |
1185 Logger::cout() << "mem: " << *a->mem << '\n'; | |
1186 if (a->val) | |
1187 Logger::cout() << "val: " << *a->val << '\n'; | |
1188 | |
1189 if (a->field) | |
1190 e->mem = a->mem; | |
1191 else | |
1192 e->mem = a->getValue(); | |
1193 e->type = elem::VAR; | |
1194 | |
1195 delete a; | |
1196 return e; | |
1197 } | |
1198 | |
1199 ////////////////////////////////////////////////////////////////////////////////////////// | |
1200 | |
1201 elem* DotVarExp::toElem(IRState* p) | |
1202 { | |
1203 Logger::print("DotVarExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1204 LOG_SCOPE; | |
1205 elem* e = new elem; | |
1206 | |
1207 elem* l = e1->toElem(p); | |
1208 | |
1209 Logger::print("e1->type=%s\n", e1->type->toChars()); | |
1210 | |
1211 if (VarDeclaration* vd = var->isVarDeclaration()) { | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1212 std::vector<unsigned> vdoffsets(1,0); |
1 | 1213 llvm::Value* src = 0; |
1214 if (e1->type->ty == Tpointer) { | |
1215 assert(e1->type->next->ty == Tstruct); | |
1216 TypeStruct* ts = (TypeStruct*)e1->type->next; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1217 ts->sym->offsetToIndex(vd->offset, vdoffsets); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1218 Logger::println("Struct member offset:%d", vd->offset); |
1 | 1219 src = l->val; |
1220 } | |
1221 else if (e1->type->ty == Tclass) { | |
1222 TypeClass* tc = (TypeClass*)e1->type; | |
1223 Logger::println("Class member offset: %d", vd->offset); | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1224 tc->sym->offsetToIndex(vd->offset, vdoffsets); |
1 | 1225 src = l->getValue(); |
1226 } | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1227 assert(vdoffsets.size() != 1); |
1 | 1228 assert(src != 0); |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1229 llvm::Value* arrptr = LLVM_DtoGEP(src,vdoffsets,"tmp",p->scopebb()); |
1 | 1230 e->mem = arrptr; |
1231 Logger::cout() << "mem: " << *e->mem << '\n'; | |
1232 e->type = elem::VAR; | |
1233 } | |
1234 else if (FuncDeclaration* fdecl = var->isFuncDeclaration()) | |
1235 { | |
1236 if (fdecl->llvmValue == 0) | |
1237 { | |
1238 fdecl->toObjFile(); | |
1239 } | |
1240 | |
1241 llvm::Value* funcval = fdecl->llvmValue; | |
1242 e->arg = l->getValue(); | |
1243 | |
1244 // virtual call | |
1245 if (fdecl->isVirtual()) { | |
1246 assert(fdecl->vtblIndex > 0); | |
1247 assert(e1->type->ty == Tclass); | |
1248 | |
1249 const llvm::Type* vtbltype = llvm::PointerType::get(llvm::ArrayType::get(llvm::PointerType::get(llvm::Type::Int8Ty),0)); | |
1250 | |
1251 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1252 llvm::Value* vtblidx = llvm::ConstantInt::get(llvm::Type::Int32Ty, (size_t)fdecl->vtblIndex, false); | |
6 | 1253 funcval = LLVM_DtoGEP(e->arg, zero, zero, "tmp", p->scopebb()); |
1 | 1254 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); |
1255 funcval = new llvm::BitCastInst(funcval, vtbltype, "tmp", p->scopebb()); | |
6 | 1256 funcval = LLVM_DtoGEP(funcval, zero, vtblidx, "tmp", p->scopebb()); |
1 | 1257 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); |
1258 funcval = new llvm::BitCastInst(funcval, fdecl->llvmValue->getType(), "tmp", p->scopebb()); | |
1259 } | |
1260 e->val = funcval; | |
1261 e->type = elem::VAL; | |
1262 } | |
1263 else { | |
1264 printf("unknown: %s\n", var->toChars()); | |
1265 assert(0); | |
1266 } | |
1267 | |
1268 delete l; | |
1269 | |
1270 return e; | |
1271 } | |
1272 | |
1273 ////////////////////////////////////////////////////////////////////////////////////////// | |
1274 | |
1275 elem* ThisExp::toElem(IRState* p) | |
1276 { | |
1277 Logger::print("ThisExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1278 LOG_SCOPE; | |
1279 elem* e = new elem; | |
1280 | |
1281 if (VarDeclaration* vd = var->isVarDeclaration()) { | |
1282 assert(vd->llvmValue == 0); | |
1283 | |
1284 llvm::Function* fn = p->topfunc(); | |
1285 assert(fn); | |
1286 | |
1287 TypeFunction* tf = p->topfunctype(); | |
1288 assert(tf); | |
1289 | |
1290 llvm::Value* v = 0; | |
1291 if (tf->llvmRetInPtr) | |
1292 v = ++fn->arg_begin(); | |
1293 else | |
1294 v = fn->arg_begin(); | |
1295 assert(v); | |
1296 | |
1297 e->val = v; | |
1298 e->type = elem::VAL; | |
1299 } | |
1300 else { | |
1301 assert(0); | |
1302 } | |
1303 | |
1304 return e; | |
1305 } | |
1306 | |
1307 ////////////////////////////////////////////////////////////////////////////////////////// | |
1308 | |
1309 elem* AddrExp::toElem(IRState* p) | |
1310 { | |
1311 Logger::print("AddrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1312 LOG_SCOPE; | |
1313 elem* e = e1->toElem(p); | |
1314 e->field = true; | |
1315 return e; | |
1316 } | |
1317 | |
1318 ////////////////////////////////////////////////////////////////////////////////////////// | |
1319 | |
1320 elem* StructLiteralExp::toElem(IRState* p) | |
1321 { | |
1322 Logger::print("StructLiteralExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1323 LOG_SCOPE; | |
1324 elem* e = new elem; | |
1325 | |
1326 // if there is no lval, this must be a static initializer for a global. correct? | |
1327 if (p->lvals.empty()) | |
1328 { | |
1329 // TODO | |
1330 assert(0); | |
1331 } | |
1332 // otherwise write directly in the lvalue | |
1333 else | |
1334 { | |
1335 llvm::Value* sptr = p->toplval(); | |
1336 assert(sptr); | |
1337 | |
1338 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1339 | |
1340 unsigned n = elements->dim; | |
1341 for (unsigned i=0; i<n; ++i) | |
1342 { | |
1343 llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false); | |
6 | 1344 llvm::Value* arrptr = LLVM_DtoGEP(sptr,zero,offset,"tmp",p->scopebb()); |
1 | 1345 |
1346 Expression* vx = (Expression*)elements->data[i]; | |
1347 if (vx != 0) { | |
1348 elem* ve = vx->toElem(p); | |
1349 //Logger::cout() << *ve->val << " | " << *arrptr << '\n'; | |
1350 new llvm::StoreInst(ve->getValue(), arrptr, p->scopebb()); | |
1351 delete ve; | |
1352 } | |
1353 else { | |
1354 assert(0); | |
1355 } | |
1356 } | |
1357 } | |
1358 | |
1359 e->inplace = true; | |
1360 | |
1361 return e; | |
1362 } | |
1363 | |
1364 ////////////////////////////////////////////////////////////////////////////////////////// | |
1365 | |
1366 elem* IndexExp::toElem(IRState* p) | |
1367 { | |
1368 Logger::print("IndexExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1369 LOG_SCOPE; | |
1370 | |
1371 elem* e = new elem; | |
1372 | |
1373 elem* l = e1->toElem(p); | |
1374 | |
1375 p->arrays.push_back(l->mem); // if $ is used it must be an array so this is fine. | |
1376 elem* r = e2->toElem(p); | |
1377 p->arrays.pop_back(); | |
1378 | |
1379 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1380 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1381 | |
1382 llvm::Value* arrptr = 0; | |
1383 if (e1->type->ty == Tpointer) { | |
1384 arrptr = new llvm::GetElementPtrInst(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
1385 } | |
1386 else if (e1->type->ty == Tsarray) { | |
6 | 1387 arrptr = LLVM_DtoGEP(l->mem, zero, r->getValue(),"tmp",p->scopebb()); |
1 | 1388 } |
1389 else if (e1->type->ty == Tarray) { | |
6 | 1390 arrptr = LLVM_DtoGEP(l->mem,zero,one,"tmp",p->scopebb()); |
1 | 1391 arrptr = new llvm::LoadInst(arrptr,"tmp",p->scopebb()); |
1392 arrptr = new llvm::GetElementPtrInst(arrptr,r->getValue(),"tmp",p->scopebb()); | |
1393 } | |
1394 assert(arrptr); | |
1395 | |
1396 e->mem = arrptr; | |
1397 e->type = elem::VAR; | |
1398 | |
1399 delete l; | |
1400 delete r; | |
1401 | |
1402 return e; | |
1403 } | |
1404 | |
1405 ////////////////////////////////////////////////////////////////////////////////////////// | |
1406 | |
1407 elem* SliceExp::toElem(IRState* p) | |
1408 { | |
1409 Logger::print("SliceExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1410 LOG_SCOPE; | |
1411 | |
1412 elem* v = e1->toElem(p); | |
1413 | |
1414 elem* e = new elem; | |
1415 assert(v->mem); | |
1416 e->type = elem::SLICE; | |
1417 | |
1418 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1419 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1420 | |
1421 // partial slice | |
1422 if (lwr) | |
1423 { | |
1424 assert(upr); | |
1425 p->arrays.push_back(v->mem); | |
1426 elem* lo = lwr->toElem(p); | |
1427 | |
1428 bool lwr_is_zero = false; | |
1429 if (lo->type == elem::CONST) | |
1430 { | |
1431 assert(lo->val); | |
1432 assert(llvm::isa<llvm::ConstantInt>(lo->val)); | |
1433 | |
1434 if (e1->type->ty == Tpointer) { | |
1435 e->mem = v->getValue(); | |
1436 } | |
1437 else if (e1->type->ty == Tarray) { | |
6 | 1438 llvm::Value* tmp = LLVM_DtoGEP(v->mem,zero,one,"tmp",p->scopebb()); |
1 | 1439 e->mem = new llvm::LoadInst(tmp,"tmp",p->scopebb()); |
1440 } | |
1441 else | |
1442 assert(e->mem); | |
1443 | |
1444 llvm::ConstantInt* c = llvm::cast<llvm::ConstantInt>(lo->val); | |
1445 if (!(lwr_is_zero = c->isZero())) { | |
1446 e->mem = new llvm::GetElementPtrInst(e->mem,lo->val,"tmp",p->scopebb()); | |
1447 } | |
1448 } | |
1449 else | |
1450 { | |
6 | 1451 llvm::Value* tmp = LLVM_DtoGEP(v->mem,zero,one,"tmp",p->scopebb()); |
1 | 1452 tmp = new llvm::LoadInst(tmp,"tmp",p->scopebb()); |
1453 e->mem = new llvm::GetElementPtrInst(tmp,lo->getValue(),"tmp",p->scopebb()); | |
1454 } | |
1455 | |
1456 elem* up = upr->toElem(p); | |
1457 p->arrays.pop_back(); | |
1458 | |
1459 if (up->type == elem::CONST) | |
1460 { | |
1461 assert(up->val); | |
1462 assert(llvm::isa<llvm::ConstantInt>(up->val)); | |
1463 if (lwr_is_zero) { | |
1464 e->arg = up->val; | |
1465 } | |
1466 else { | |
1467 if (lo->type == elem::CONST) { | |
1468 llvm::Constant* clo = llvm::cast<llvm::Constant>(lo->val); | |
1469 llvm::Constant* cup = llvm::cast<llvm::Constant>(up->val); | |
1470 e->arg = llvm::ConstantExpr::getSub(cup, clo); | |
1471 } | |
1472 else { | |
1473 e->arg = llvm::BinaryOperator::createSub(up->val, lo->getValue(), "tmp", p->scopebb()); | |
1474 } | |
1475 } | |
1476 } | |
1477 else | |
1478 { | |
1479 if (lwr_is_zero) { | |
1480 e->arg = up->getValue(); | |
1481 } | |
1482 else { | |
1483 e->arg = llvm::BinaryOperator::createSub(up->getValue(), lo->getValue(), "tmp", p->scopebb()); | |
1484 } | |
1485 } | |
1486 | |
1487 delete lo; | |
1488 delete up; | |
1489 } | |
1490 // full slice | |
1491 else | |
1492 { | |
1493 e->mem = v->mem; | |
1494 } | |
1495 | |
1496 delete v; | |
1497 | |
1498 return e; | |
1499 } | |
1500 | |
1501 ////////////////////////////////////////////////////////////////////////////////////////// | |
1502 | |
1503 elem* CmpExp::toElem(IRState* p) | |
1504 { | |
1505 Logger::print("CmpExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1506 LOG_SCOPE; | |
1507 | |
1508 elem* e = new elem; | |
1509 | |
1510 elem* l = e1->toElem(p); | |
1511 elem* r = e2->toElem(p); | |
1512 | |
1513 assert(e1->type == e2->type); | |
1514 | |
1515 Type* t = e1->type; | |
1516 | |
1517 if (t->isintegral()) | |
1518 { | |
1519 llvm::ICmpInst::Predicate cmpop; | |
1520 switch(op) | |
1521 { | |
1522 case TOKlt: | |
1523 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_ULT : llvm::ICmpInst::ICMP_SLT; | |
1524 break; | |
1525 case TOKle: | |
1526 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_ULE : llvm::ICmpInst::ICMP_SLE; | |
1527 break; | |
1528 case TOKgt: | |
1529 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_UGT : llvm::ICmpInst::ICMP_SGT; | |
1530 break; | |
1531 case TOKge: | |
1532 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_UGE : llvm::ICmpInst::ICMP_SGE; | |
1533 break; | |
1534 default: | |
1535 assert(0); | |
1536 } | |
1537 e->val = new llvm::ICmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1538 } | |
1539 else if (t->isfloating()) | |
1540 { | |
1541 llvm::FCmpInst::Predicate cmpop; | |
1542 switch(op) | |
1543 { | |
1544 case TOKlt: | |
1545 cmpop = llvm::FCmpInst::FCMP_OLT;break; | |
1546 case TOKle: | |
1547 cmpop = llvm::FCmpInst::FCMP_OLE;break; | |
1548 case TOKgt: | |
1549 cmpop = llvm::FCmpInst::FCMP_OGT;break; | |
1550 case TOKge: | |
1551 cmpop = llvm::FCmpInst::FCMP_OGE;break; | |
1552 case TOKunord: | |
1553 cmpop = llvm::FCmpInst::FCMP_UNO;break; | |
1554 case TOKule: | |
1555 cmpop = llvm::FCmpInst::FCMP_ULE;break; | |
1556 case TOKul: | |
1557 cmpop = llvm::FCmpInst::FCMP_ULT;break; | |
1558 case TOKuge: | |
1559 cmpop = llvm::FCmpInst::FCMP_UGE;break; | |
1560 case TOKug: | |
1561 cmpop = llvm::FCmpInst::FCMP_UGT;break; | |
1562 case TOKue: | |
1563 cmpop = llvm::FCmpInst::FCMP_UEQ;break; | |
1564 case TOKlg: | |
1565 cmpop = llvm::FCmpInst::FCMP_ONE;break; | |
1566 case TOKleg: | |
1567 cmpop = llvm::FCmpInst::FCMP_ORD;break; | |
1568 | |
1569 default: | |
1570 assert(0); | |
1571 } | |
1572 e->val = new llvm::FCmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1573 } | |
1574 else | |
1575 { | |
1576 assert(0 && "Unsupported CmpExp type"); | |
1577 } | |
1578 | |
1579 delete l; | |
1580 delete r; | |
1581 | |
1582 e->type = elem::VAL; | |
1583 | |
1584 return e; | |
1585 } | |
1586 | |
1587 ////////////////////////////////////////////////////////////////////////////////////////// | |
1588 | |
1589 elem* EqualExp::toElem(IRState* p) | |
1590 { | |
1591 Logger::print("EqualExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1592 LOG_SCOPE; | |
1593 | |
1594 elem* e = new elem; | |
1595 | |
1596 elem* l = e1->toElem(p); | |
1597 elem* r = e2->toElem(p); | |
1598 | |
1599 assert(e1->type == e2->type); | |
1600 | |
1601 Type* t = e1->type; | |
1602 | |
1603 if (t->isintegral() || t->ty == Tpointer) | |
1604 { | |
1605 llvm::ICmpInst::Predicate cmpop; | |
1606 switch(op) | |
1607 { | |
1608 case TOKequal: | |
1609 cmpop = llvm::ICmpInst::ICMP_EQ; | |
1610 break; | |
1611 case TOKnotequal: | |
1612 cmpop = llvm::ICmpInst::ICMP_NE; | |
1613 break; | |
1614 default: | |
1615 assert(0); | |
1616 } | |
1617 e->val = new llvm::ICmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1618 } | |
1619 else if (t->isfloating()) | |
1620 { | |
1621 llvm::FCmpInst::Predicate cmpop; | |
1622 switch(op) | |
1623 { | |
1624 case TOKequal: | |
1625 cmpop = llvm::FCmpInst::FCMP_OEQ; | |
1626 break; | |
1627 case TOKnotequal: | |
1628 cmpop = llvm::FCmpInst::FCMP_UNE; | |
1629 break; | |
1630 default: | |
1631 assert(0); | |
1632 } | |
1633 e->val = new llvm::FCmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1634 } | |
1635 else if (t->ty == Tarray) | |
1636 { | |
1637 // array comparison invokes the typeinfo runtime | |
1638 assert(0); | |
1639 } | |
1640 else | |
1641 { | |
1642 assert(0 && "Unsupported EqualExp type"); | |
1643 } | |
1644 | |
1645 delete l; | |
1646 delete r; | |
1647 | |
1648 e->type = elem::VAL; | |
1649 | |
1650 return e; | |
1651 } | |
1652 | |
1653 ////////////////////////////////////////////////////////////////////////////////////////// | |
1654 | |
1655 elem* PostExp::toElem(IRState* p) | |
1656 { | |
1657 Logger::print("PostExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1658 LOG_SCOPE; | |
1659 | |
1660 elem* l = e1->toElem(p); | |
1661 elem* r = e2->toElem(p); | |
1662 | |
1663 elem* e = new elem; | |
1664 e->mem = l->mem; | |
1665 e->val = l->getValue(); | |
1666 e->type = elem::VAL; | |
1667 | |
1668 llvm::Value* val = e->val; | |
1669 llvm::Value* post = 0; | |
1670 | |
1671 if (e1->type->isintegral()) | |
1672 { | |
1673 assert(e2->type->isintegral()); | |
1674 llvm::Value* one = llvm::ConstantInt::get(val->getType(), 1, !e2->type->isunsigned()); | |
1675 if (op == TOKplusplus) { | |
1676 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb()); | |
1677 } | |
1678 else if (op == TOKminusminus) { | |
1679 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb()); | |
1680 } | |
1681 } | |
1682 else if (e1->type->ty == Tpointer) | |
1683 { | |
1684 assert(e2->type->isintegral()); | |
1685 llvm::Constant* minusone = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)-1,true); | |
1686 llvm::Constant* plusone = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)1,false); | |
1687 llvm::Constant* whichone = (op == TOKplusplus) ? plusone : minusone; | |
1688 post = new llvm::GetElementPtrInst(val, whichone, "tmp", p->scopebb()); | |
1689 } | |
1690 else if (e1->type->isfloating()) | |
1691 { | |
1692 assert(e2->type->isfloating()); | |
1693 llvm::Value* one = llvm::ConstantFP::get(val->getType(), 1.0f); | |
1694 if (op == TOKplusplus) { | |
1695 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb()); | |
1696 } | |
1697 else if (op == TOKminusminus) { | |
1698 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb()); | |
1699 } | |
1700 } | |
1701 else | |
1702 assert(post); | |
1703 | |
1704 //llvm::Value* tostore = l->storeVal ? l->storeVal : l->val; | |
1705 new llvm::StoreInst(post,l->mem,p->scopebb()); | |
1706 | |
1707 delete l; | |
1708 delete r; | |
1709 | |
1710 return e; | |
1711 } | |
1712 | |
1713 ////////////////////////////////////////////////////////////////////////////////////////// | |
1714 | |
1715 elem* NewExp::toElem(IRState* p) | |
1716 { | |
1717 Logger::print("NewExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1718 LOG_SCOPE; | |
1719 | |
1720 assert(!thisexp); | |
1721 assert(!newargs); | |
1722 assert(newtype); | |
1723 //assert(!arguments); | |
1724 //assert(!member); | |
1725 assert(!allocator); | |
1726 | |
1727 elem* e = new elem; | |
1728 | |
1729 const llvm::Type* t = LLVM_DtoType(newtype); | |
1730 | |
1731 if (onstack) { | |
1732 assert(newtype->ty == Tclass); | |
1733 e->mem = new llvm::AllocaInst(t->getContainedType(0),"tmp",p->topallocapoint()); | |
1734 } | |
1735 else { | |
1736 if (newtype->ty == Tclass) { | |
1737 e->mem = new llvm::MallocInst(t->getContainedType(0),"tmp",p->scopebb()); | |
1738 } | |
1739 else if (newtype->ty == Tarray) { | |
1740 t = LLVM_DtoType(newtype->next); | |
1741 assert(arguments); | |
1742 if (arguments->dim == 1) { | |
1743 elem* sz = ((Expression*)arguments->data[0])->toElem(p); | |
1744 llvm::Value* dimval = sz->getValue(); | |
1745 llvm::Value* usedimval = dimval; | |
1746 if (dimval->getType() != llvm::Type::Int32Ty) | |
1747 usedimval = new llvm::TruncInst(dimval, llvm::Type::Int32Ty,"tmp",p->scopebb()); | |
1748 e->mem = new llvm::MallocInst(t,usedimval,"tmp",p->scopebb()); | |
1749 | |
1750 LLVM_DtoSetArray(p->toplval(), dimval, e->mem); | |
1751 delete sz; | |
1752 } | |
1753 else { | |
1754 assert(0); | |
1755 } | |
1756 } | |
1757 else { | |
1758 e->mem = new llvm::MallocInst(t,"tmp",p->scopebb()); | |
1759 } | |
1760 } | |
1761 | |
1762 if (newtype->ty == Tclass) { | |
1763 // first apply the static initializer | |
1764 assert(e->mem); | |
1765 LLVM_DtoInitClass((TypeClass*)newtype, e->mem); | |
1766 | |
1767 // then call constructor | |
1768 if (arguments) { | |
1769 std::vector<llvm::Value*> ctorargs; | |
1770 ctorargs.push_back(e->mem); | |
1771 for (size_t i=0; i<arguments->dim; ++i) | |
1772 { | |
1773 Expression* ex = (Expression*)arguments->data[i]; | |
1774 Logger::println("arg=%s", ex->toChars()); | |
1775 elem* exe = ex->toElem(p); | |
1776 assert(exe->getValue()); | |
1777 ctorargs.push_back(exe->getValue()); | |
1778 delete exe; | |
1779 } | |
1780 assert(member); | |
1781 assert(member->llvmValue); | |
1782 new llvm::CallInst(member->llvmValue, ctorargs.begin(), ctorargs.end(), "", p->scopebb()); | |
1783 } | |
1784 } | |
1785 else if (newtype->ty == Tstruct) { | |
1786 TypeStruct* ts = (TypeStruct*)newtype; | |
1787 if (ts->isZeroInit()) { | |
1788 LLVM_DtoStructZeroInit(ts,e->mem); | |
1789 } | |
1790 else { | |
1791 LLVM_DtoStructCopy(ts,e->mem,ts->llvmInit); | |
1792 } | |
1793 } | |
1794 | |
1795 e->inplace = true; | |
1796 e->type = elem::VAR; | |
1797 | |
1798 return e; | |
1799 } | |
1800 | |
1801 ////////////////////////////////////////////////////////////////////////////////////////// | |
1802 | |
1803 elem* DeleteExp::toElem(IRState* p) | |
1804 { | |
1805 Logger::print("DeleteExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1806 LOG_SCOPE; | |
1807 | |
1808 //assert(e1->type->ty != Tclass); | |
1809 | |
1810 elem* v = e1->toElem(p); | |
1811 llvm::Value* val = v->getValue(); | |
1812 llvm::Value* ldval = 0; | |
1813 | |
1814 const llvm::Type* t = val->getType(); | |
1815 llvm::Constant* z = llvm::Constant::getNullValue(t); | |
1816 | |
1817 if (e1->type->ty == Tpointer) { | |
1818 ldval = v->getValue(); | |
1819 new llvm::FreeInst(ldval, p->scopebb()); | |
1820 | |
1821 Logger::cout() << *z << '\n'; | |
1822 Logger::cout() << *val << '\n'; | |
1823 new llvm::StoreInst(z, v->mem, p->scopebb()); | |
1824 } | |
1825 else if (e1->type->ty == Tclass) { | |
1826 TypeClass* tc = (TypeClass*)e1->type; | |
1827 LLVM_DtoCallClassDtors(tc, val); | |
1828 | |
1829 if (v->vardecl && !v->vardecl->onstack) { | |
1830 new llvm::FreeInst(val, p->scopebb()); | |
1831 } | |
1832 new llvm::StoreInst(z, v->mem, p->scopebb()); | |
1833 } | |
1834 else if (e1->type->ty == Tarray) { | |
1835 // must be on the heap (correct?) | |
1836 ldval = v->getValue(); | |
1837 | |
1838 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1839 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1840 | |
6 | 1841 llvm::Value* ptr = LLVM_DtoGEP(ldval,zero,one,"tmp",p->scopebb()); |
1 | 1842 ptr = new llvm::LoadInst(ptr,"tmp",p->scopebb()); |
1843 new llvm::FreeInst(ptr, p->scopebb()); | |
1844 LLVM_DtoNullArray(val); | |
1845 } | |
1846 else { | |
1847 assert(0); | |
1848 } | |
1849 | |
1850 delete v; | |
1851 | |
1852 // this expression produces no useful data | |
1853 return 0; | |
1854 } | |
1855 | |
1856 ////////////////////////////////////////////////////////////////////////////////////////// | |
1857 | |
1858 elem* ArrayLengthExp::toElem(IRState* p) | |
1859 { | |
1860 Logger::print("ArrayLengthExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1861 LOG_SCOPE; | |
1862 | |
1863 elem* e = new elem; | |
1864 elem* u = e1->toElem(p); | |
1865 | |
1866 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
6 | 1867 llvm::Value* ptr = LLVM_DtoGEP(u->mem,zero,zero,"tmp",p->scopebb()); |
1 | 1868 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb()); |
1869 e->type = elem::VAL; | |
1870 | |
1871 delete u; | |
1872 | |
1873 return e; | |
1874 } | |
1875 | |
1876 ////////////////////////////////////////////////////////////////////////////////////////// | |
1877 | |
1878 elem* AssertExp::toElem(IRState* p) | |
1879 { | |
1880 Logger::print("AssertExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1881 LOG_SCOPE; | |
1882 | |
1883 elem* e = new elem; | |
1884 elem* u = e1->toElem(p); | |
1885 elem* m = msg ? msg->toElem(p) : 0; | |
1886 | |
1887 std::vector<llvm::Value*> llargs; | |
1888 llargs.resize(3); | |
1889 llargs[0] = LLVM_DtoBoolean(u->getValue()); | |
1890 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false); | |
1891 llargs[2] = m ? m->val : llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
1892 | |
1893 delete m; | |
1894 delete u; | |
1895 | |
1896 //Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n'; | |
1897 | |
1898 llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert"); | |
1899 assert(fn); | |
1900 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb()); | |
1901 call->setCallingConv(llvm::CallingConv::C); | |
1902 | |
1903 return e; | |
1904 } | |
1905 | |
1906 ////////////////////////////////////////////////////////////////////////////////////////// | |
1907 | |
1908 elem* NotExp::toElem(IRState* p) | |
1909 { | |
1910 Logger::print("NotExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1911 LOG_SCOPE; | |
1912 | |
1913 elem* e = new elem; | |
1914 elem* u = e1->toElem(p); | |
1915 | |
1916 llvm::Value* b = LLVM_DtoBoolean(u->getValue()); | |
1917 | |
1918 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, true); | |
1919 e->val = new llvm::ICmpInst(llvm::ICmpInst::ICMP_EQ,b,zero,"tmp",p->scopebb()); | |
1920 e->type = elem::VAL; | |
1921 | |
1922 delete u; | |
1923 | |
1924 return e; | |
1925 } | |
1926 | |
1927 ////////////////////////////////////////////////////////////////////////////////////////// | |
1928 | |
1929 elem* AndAndExp::toElem(IRState* p) | |
1930 { | |
1931 Logger::print("AndAndExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1932 LOG_SCOPE; | |
1933 | |
1934 // allocate a temporary for the final result. failed to come up with a better way :/ | |
1935 llvm::Value* resval = 0; | |
1936 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
1937 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"andandtmp",p->topallocapoint()); | |
1938 | |
1939 elem* e = new elem; | |
1940 elem* u = e1->toElem(p); | |
1941 | |
1942 llvm::BasicBlock* oldend = p->scopeend(); | |
1943 llvm::BasicBlock* andand = new llvm::BasicBlock("andand", gIR->topfunc(), oldend); | |
1944 llvm::BasicBlock* andandend = new llvm::BasicBlock("andandend", gIR->topfunc(), oldend); | |
1945 | |
1946 llvm::Value* ubool = LLVM_DtoBoolean(u->getValue()); | |
1947 new llvm::StoreInst(ubool,resval,p->scopebb()); | |
1948 new llvm::BranchInst(andand,andandend,ubool,p->scopebb()); | |
1949 | |
1950 p->scope() = IRScope(andand, andandend); | |
1951 elem* v = e2->toElem(p); | |
1952 | |
1953 llvm::Value* vbool = LLVM_DtoBoolean(v->getValue()); | |
1954 llvm::Value* uandvbool = llvm::BinaryOperator::create(llvm::BinaryOperator::And, ubool, vbool,"tmp",p->scopebb()); | |
1955 new llvm::StoreInst(uandvbool,resval,p->scopebb()); | |
1956 new llvm::BranchInst(andandend,p->scopebb()); | |
1957 | |
1958 delete u; | |
1959 delete v; | |
1960 | |
1961 p->scope() = IRScope(andandend, oldend); | |
1962 | |
1963 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
1964 e->type = elem::VAL; | |
1965 | |
1966 return e; | |
1967 } | |
1968 | |
1969 ////////////////////////////////////////////////////////////////////////////////////////// | |
1970 | |
1971 elem* OrOrExp::toElem(IRState* p) | |
1972 { | |
1973 Logger::print("OrOrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1974 LOG_SCOPE; | |
1975 | |
1976 // allocate a temporary for the final result. failed to come up with a better way :/ | |
1977 llvm::Value* resval = 0; | |
1978 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
1979 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"orortmp",p->topallocapoint()); | |
1980 | |
1981 elem* e = new elem; | |
1982 elem* u = e1->toElem(p); | |
1983 | |
1984 llvm::BasicBlock* oldend = p->scopeend(); | |
1985 llvm::BasicBlock* oror = new llvm::BasicBlock("oror", gIR->topfunc(), oldend); | |
1986 llvm::BasicBlock* ororend = new llvm::BasicBlock("ororend", gIR->topfunc(), oldend); | |
1987 | |
1988 llvm::Value* ubool = LLVM_DtoBoolean(u->getValue()); | |
1989 new llvm::StoreInst(ubool,resval,p->scopebb()); | |
1990 new llvm::BranchInst(ororend,oror,ubool,p->scopebb()); | |
1991 | |
1992 p->scope() = IRScope(oror, ororend); | |
1993 elem* v = e2->toElem(p); | |
1994 | |
1995 llvm::Value* vbool = LLVM_DtoBoolean(v->getValue()); | |
1996 new llvm::StoreInst(vbool,resval,p->scopebb()); | |
1997 new llvm::BranchInst(ororend,p->scopebb()); | |
1998 | |
1999 delete u; | |
2000 delete v; | |
2001 | |
2002 p->scope() = IRScope(ororend, oldend); | |
2003 | |
2004 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
2005 e->type = elem::VAL; | |
2006 | |
2007 return e; | |
2008 } | |
2009 | |
2010 ////////////////////////////////////////////////////////////////////////////////////////// | |
2011 | |
2012 #define BinBitExp(X,Y) \ | |
2013 elem* X##Exp::toElem(IRState* p) \ | |
2014 { \ | |
2015 Logger::print("%sExp::toElem: %s | %s\n", #X, toChars(), type->toChars()); \ | |
2016 LOG_SCOPE; \ | |
2017 elem* e = new elem; \ | |
2018 elem* u = e1->toElem(p); \ | |
2019 elem* v = e2->toElem(p); \ | |
2020 e->val = llvm::BinaryOperator::create(llvm::Instruction::Y, u->getValue(), v->getValue(), "tmp", p->scopebb()); \ | |
2021 e->type = elem::VAL; \ | |
2022 delete u; \ | |
2023 delete v; \ | |
2024 return e; \ | |
2025 } \ | |
2026 \ | |
2027 elem* X##AssignExp::toElem(IRState* p) \ | |
2028 { \ | |
2029 Logger::print("%sAssignExp::toElem: %s | %s\n", #X, toChars(), type->toChars()); \ | |
2030 LOG_SCOPE; \ | |
2031 elem* u = e1->toElem(p); \ | |
2032 elem* v = e2->toElem(p); \ | |
2033 llvm::Value* tmp = llvm::BinaryOperator::create(llvm::Instruction::Y, u->getValue(), v->getValue(), "tmp", p->scopebb()); \ | |
2034 Logger::cout() << *tmp << '|' << *u->mem << '\n'; \ | |
2035 new llvm::StoreInst(LLVM_DtoPointedType(u->mem, tmp), u->mem, p->scopebb()); \ | |
2036 delete u; \ | |
2037 delete v; \ | |
2038 elem* e = new elem; \ | |
2039 e->mem = u->mem; \ | |
2040 e->type = elem::VAR; \ | |
2041 return e; \ | |
2042 } | |
2043 | |
2044 BinBitExp(And,And); | |
2045 BinBitExp(Or,Or); | |
2046 BinBitExp(Xor,Xor); | |
2047 BinBitExp(Shl,Shl); | |
2048 BinBitExp(Shr,AShr); | |
2049 BinBitExp(Ushr,LShr); | |
2050 | |
2051 ////////////////////////////////////////////////////////////////////////////////////////// | |
2052 | |
2053 elem* HaltExp::toElem(IRState* p) | |
2054 { | |
2055 Logger::print("HaltExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2056 LOG_SCOPE; | |
2057 | |
2058 std::vector<llvm::Value*> llargs; | |
2059 llargs.resize(3); | |
2060 llargs[0] = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, false); | |
2061 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false); | |
2062 llargs[2] = llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
2063 | |
2064 //Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n'; | |
2065 | |
2066 llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert"); | |
2067 assert(fn); | |
2068 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb()); | |
2069 call->setCallingConv(llvm::CallingConv::C); | |
2070 | |
2071 //new llvm::UnreachableInst(p->scopebb()); | |
2072 | |
2073 return 0; | |
2074 } | |
2075 | |
2076 ////////////////////////////////////////////////////////////////////////////////////////// | |
2077 | |
2078 elem* DelegateExp::toElem(IRState* p) | |
2079 { | |
2080 Logger::print("DelegateExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2081 LOG_SCOPE; | |
2082 | |
2083 elem* e = new elem; | |
2084 elem* u = e1->toElem(p); | |
2085 | |
2086 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
2087 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
2088 | |
2089 const llvm::Type* int8ptrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
2090 | |
2091 llvm::Value* lval = p->toplval(); | |
2092 | |
6 | 2093 llvm::Value* context = LLVM_DtoGEP(lval,zero,zero,"tmp",p->scopebb()); |
1 | 2094 llvm::Value* castcontext = new llvm::BitCastInst(u->getValue(),int8ptrty,"tmp",p->scopebb()); |
2095 new llvm::StoreInst(castcontext, context, p->scopebb()); | |
2096 | |
6 | 2097 llvm::Value* fptr = LLVM_DtoGEP(lval,zero,one,"tmp",p->scopebb()); |
1 | 2098 |
2099 assert(func->llvmValue); | |
2100 llvm::Value* castfptr = new llvm::BitCastInst(func->llvmValue,fptr->getType()->getContainedType(0),"tmp",p->scopebb()); | |
2101 new llvm::StoreInst(castfptr, fptr, p->scopebb()); | |
2102 | |
2103 e->inplace = true; | |
2104 | |
2105 delete u; | |
2106 return e; | |
2107 } | |
2108 | |
2109 ////////////////////////////////////////////////////////////////////////////////////////// | |
2110 | |
2111 elem* IdentityExp::toElem(IRState* p) | |
2112 { | |
2113 Logger::print("IdentityExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2114 LOG_SCOPE; | |
2115 | |
2116 elem* u = e1->toElem(p); | |
2117 elem* v = e2->toElem(p); | |
2118 | |
2119 elem* e = new elem; | |
2120 | |
2121 llvm::Value* l = u->getValue(); | |
2122 llvm::Value* r = 0; | |
2123 if (v->type == elem::NUL) | |
2124 r = llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(l->getType())); | |
2125 else | |
2126 r = v->getValue(); | |
2127 | |
2128 llvm::ICmpInst::Predicate pred = (op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE; | |
2129 e->val = new llvm::ICmpInst(pred, l, r, "tmp", p->scopebb()); | |
2130 e->type = elem::VAL; | |
2131 | |
2132 delete u; | |
2133 delete v; | |
2134 | |
2135 return e; | |
2136 } | |
2137 | |
2138 ////////////////////////////////////////////////////////////////////////////////////////// | |
2139 | |
2140 elem* CommaExp::toElem(IRState* p) | |
2141 { | |
2142 Logger::print("CommaExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2143 LOG_SCOPE; | |
2144 | |
2145 elem* u = e1->toElem(p); | |
2146 elem* v = e2->toElem(p); | |
2147 delete u; | |
2148 return v; | |
2149 } | |
2150 | |
2151 ////////////////////////////////////////////////////////////////////////////////////////// | |
2152 | |
2153 elem* CondExp::toElem(IRState* p) | |
2154 { | |
2155 Logger::print("CondExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2156 LOG_SCOPE; | |
2157 | |
2158 const llvm::Type* resty = LLVM_DtoType(type); | |
2159 | |
2160 // allocate a temporary for the final result. failed to come up with a better way :/ | |
2161 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
2162 llvm::Value* resval = new llvm::AllocaInst(resty,"condtmp",p->topallocapoint()); | |
2163 | |
2164 llvm::BasicBlock* oldend = p->scopeend(); | |
2165 llvm::BasicBlock* condtrue = new llvm::BasicBlock("condtrue", gIR->topfunc(), oldend); | |
2166 llvm::BasicBlock* condfalse = new llvm::BasicBlock("condfalse", gIR->topfunc(), oldend); | |
2167 llvm::BasicBlock* condend = new llvm::BasicBlock("condend", gIR->topfunc(), oldend); | |
2168 | |
2169 elem* c = econd->toElem(p); | |
2170 llvm::Value* cond_val = LLVM_DtoBoolean(c->getValue()); | |
2171 delete c; | |
2172 new llvm::BranchInst(condtrue,condfalse,cond_val,p->scopebb()); | |
2173 | |
2174 p->scope() = IRScope(condtrue, condfalse); | |
2175 elem* u = e1->toElem(p); | |
2176 Logger::cout() << *u->val << '|' << *resval << '\n'; \ | |
2177 new llvm::StoreInst(u->getValue(),resval,p->scopebb()); | |
2178 new llvm::BranchInst(condend,p->scopebb()); | |
2179 delete u; | |
2180 | |
2181 p->scope() = IRScope(condfalse, condend); | |
2182 elem* v = e2->toElem(p); | |
2183 new llvm::StoreInst(v->getValue(),resval,p->scopebb()); | |
2184 new llvm::BranchInst(condend,p->scopebb()); | |
2185 delete v; | |
2186 | |
2187 p->scope() = IRScope(condend, oldend); | |
2188 | |
2189 elem* e = new elem; | |
2190 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
2191 e->type = elem::VAL; | |
2192 return e; | |
2193 } | |
2194 | |
2195 ////////////////////////////////////////////////////////////////////////////////////////// | |
2196 | |
2197 elem* ComExp::toElem(IRState* p) | |
2198 { | |
2199 Logger::print("ComExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2200 LOG_SCOPE; | |
2201 | |
2202 elem* e = new elem; | |
2203 elem* u = e1->toElem(p); | |
2204 | |
2205 llvm::Value* value = u->getValue(); | |
2206 llvm::Value* minusone = llvm::ConstantInt::get(value->getType(), -1, true); | |
2207 e->val = llvm::BinaryOperator::create(llvm::Instruction::Xor, value, minusone, "tmp", p->scopebb()); | |
2208 | |
2209 delete u; | |
2210 | |
2211 e->type = elem::VAL; | |
2212 | |
2213 return e; | |
2214 } | |
2215 | |
2216 ////////////////////////////////////////////////////////////////////////////////////////// | |
2217 | |
2218 #define STUB(x) elem *x::toElem(IRState * p) {error("Exp type "#x" not implemented: %s", toChars()); fatal(); return 0; } | |
2219 //STUB(IdentityExp); | |
2220 //STUB(CondExp); | |
2221 //STUB(EqualExp); | |
2222 STUB(InExp); | |
2223 //STUB(CmpExp); | |
2224 //STUB(AndAndExp); | |
2225 //STUB(OrOrExp); | |
2226 //STUB(AndExp); | |
2227 //STUB(AndAssignExp); | |
2228 //STUB(OrExp); | |
2229 //STUB(OrAssignExp); | |
2230 //STUB(XorExp); | |
2231 //STUB(XorAssignExp); | |
2232 //STUB(ShrExp); | |
2233 //STUB(ShrAssignExp); | |
2234 //STUB(ShlExp); | |
2235 //STUB(ShlAssignExp); | |
2236 //STUB(UshrExp); | |
2237 //STUB(UshrAssignExp); | |
2238 //STUB(DivExp); | |
2239 //STUB(DivAssignExp); | |
2240 //STUB(MulExp); | |
2241 //STUB(MulAssignExp); | |
2242 //STUB(ModExp); | |
2243 //STUB(ModAssignExp); | |
2244 STUB(CatExp); | |
2245 STUB(CatAssignExp); | |
2246 //STUB(AddExp); | |
2247 //STUB(AddAssignExp); | |
2248 STUB(Expression); | |
2249 //STUB(MinExp); | |
2250 //STUB(MinAssignExp); | |
2251 //STUB(PostExp); | |
2252 //STUB(NullExp); | |
2253 //STUB(ThisExp); | |
2254 //STUB(CallExp); | |
2255 STUB(DotTypeExp); | |
2256 STUB(TypeDotIdExp); | |
2257 //STUB(DotVarExp); | |
2258 //STUB(AssertExp); | |
2259 STUB(FuncExp); | |
2260 //STUB(DelegateExp); | |
2261 //STUB(VarExp); | |
2262 //STUB(DeclarationExp); | |
2263 //STUB(NewExp); | |
2264 //STUB(SymOffExp); | |
2265 STUB(ScopeExp); | |
2266 //STUB(AssignExp); | |
2267 | |
2268 STUB(TypeExp); | |
2269 //STUB(RealExp); | |
2270 STUB(ComplexExp); | |
2271 //STUB(StringExp); | |
2272 //STUB(IntegerExp); | |
2273 STUB(BoolExp); | |
2274 | |
2275 //STUB(NotExp); | |
2276 //STUB(ComExp); | |
2277 STUB(NegExp); | |
2278 //STUB(PtrExp); | |
2279 //STUB(AddrExp); | |
2280 //STUB(SliceExp); | |
2281 //STUB(CastExp); | |
2282 //STUB(DeleteExp); | |
2283 //STUB(IndexExp); | |
2284 //STUB(CommaExp); | |
2285 //STUB(ArrayLengthExp); | |
2286 //STUB(HaltExp); | |
2287 STUB(RemoveExp); | |
2288 STUB(ArrayLiteralExp); | |
2289 STUB(AssocArrayLiteralExp); | |
2290 //STUB(StructLiteralExp); | |
2291 | |
2292 unsigned Type::totym() { return 0; } | |
2293 | |
2294 type * | |
2295 Type::toCtype() { | |
2296 return 0; | |
2297 } | |
2298 | |
2299 type * Type::toCParamtype() | |
2300 { | |
2301 return 0; | |
2302 } | |
2303 Symbol * Type::toSymbol() | |
2304 { | |
2305 return 0; | |
2306 } | |
2307 | |
2308 type * | |
2309 TypeTypedef::toCtype() | |
2310 { | |
2311 return 0; | |
2312 } | |
2313 | |
2314 type * | |
2315 TypeTypedef::toCParamtype() | |
2316 { | |
2317 return 0; | |
2318 } | |
2319 | |
2320 void | |
2321 TypedefDeclaration::toDebug() | |
2322 { | |
2323 } | |
2324 | |
2325 | |
2326 type * | |
2327 TypeEnum::toCtype() | |
2328 { | |
2329 return 0; | |
2330 } | |
2331 | |
2332 type * | |
2333 TypeStruct::toCtype() | |
2334 { | |
2335 return 0; | |
2336 } | |
2337 | |
2338 void | |
2339 StructDeclaration::toDebug() | |
2340 { | |
2341 } | |
2342 | |
2343 Symbol * TypeClass::toSymbol() | |
2344 { | |
2345 return 0; | |
2346 } | |
2347 | |
2348 unsigned TypeFunction::totym() | |
2349 { | |
2350 return 0; | |
2351 } | |
2352 | |
2353 type * | |
2354 TypeFunction::toCtype() | |
2355 { | |
2356 return 0; | |
2357 } | |
2358 | |
2359 type * | |
2360 TypeSArray::toCtype() | |
2361 { | |
2362 return 0; | |
2363 } | |
2364 | |
2365 type *TypeSArray::toCParamtype() { return 0; } | |
2366 | |
2367 type * | |
2368 TypeDArray::toCtype() | |
2369 { | |
2370 return 0; | |
2371 } | |
2372 | |
2373 type * | |
2374 TypeAArray::toCtype() | |
2375 { | |
2376 return 0; | |
2377 } | |
2378 | |
2379 type * | |
2380 TypePointer::toCtype() | |
2381 { | |
2382 return 0; | |
2383 } | |
2384 | |
2385 type * | |
2386 TypeDelegate::toCtype() | |
2387 { | |
2388 return 0; | |
2389 } | |
2390 | |
2391 type * | |
2392 TypeClass::toCtype() | |
2393 { | |
2394 return 0; | |
2395 } | |
2396 | |
2397 void | |
2398 ClassDeclaration::toDebug() | |
2399 { | |
2400 } | |
2401 | |
2402 /* --------------------------------------------------------------------------------------- */ | |
2403 | |
2404 void CompoundStatement::toIR(IRState* p) | |
2405 { | |
2406 static int csi = 0; | |
2407 Logger::println("CompoundStatement::toIR(%d):\n<<<\n%s>>>", csi++, toChars()); | |
2408 LOG_SCOPE; | |
2409 | |
2410 /* | |
2411 const char* labelname; | |
2412 bool insterm = false; | |
2413 | |
2414 if (!p->scopes()) { | |
2415 labelname = "bb"; | |
2416 insterm = true; | |
2417 } | |
2418 else | |
2419 labelname = "entry"; | |
2420 | |
2421 //if (!llvm::isa<llvm::TerminatorInst>(p->topfunc()->back().back())) | |
2422 // insterm = true; | |
2423 | |
2424 llvm::BasicBlock* bb = new llvm::BasicBlock(labelname, p->topfunc()); | |
2425 | |
2426 if (insterm) { | |
2427 new llvm::BranchInst(bb,p->topbb()); | |
2428 } | |
2429 | |
2430 p->bbs.push(bb); | |
2431 */ | |
2432 | |
2433 size_t n = statements->dim; | |
2434 for (size_t i=0; i<n; i++) | |
2435 { | |
2436 Statement* s = (Statement*)statements->data[i]; | |
2437 if (s) | |
2438 s->toIR(p); | |
2439 else | |
2440 Logger::println("NULL statement found in CompoundStatement !! :S"); | |
2441 } | |
2442 | |
2443 //p->bbs.pop(); | |
2444 } | |
2445 | |
2446 void ReturnStatement::toIR(IRState* p) | |
2447 { | |
2448 static int rsi = 0; | |
2449 Logger::println("ReturnStatement::toIR(%d): %s", rsi++, toChars()); | |
2450 LOG_SCOPE; | |
2451 | |
2452 if (exp) | |
2453 { | |
2454 TY expty = exp->type->ty; | |
2455 if (p->topfunc()->getReturnType() == llvm::Type::VoidTy) { | |
2456 assert(expty == Tstruct || expty == Tdelegate || expty == Tarray); | |
2457 | |
2458 TypeFunction* f = p->topfunctype(); | |
2459 assert(f->llvmRetInPtr && f->llvmRetArg); | |
2460 | |
2461 p->lvals.push_back(f->llvmRetArg); | |
2462 elem* e = exp->toElem(p); | |
2463 p->lvals.pop_back(); | |
2464 | |
2465 // structliterals do this themselves | |
2466 // also they dont produce any value | |
2467 if (expty == Tstruct) { | |
2468 if (!e->inplace) { | |
2469 TypeStruct* ts = (TypeStruct*)exp->type; | |
2470 assert(e->mem); | |
2471 LLVM_DtoStructCopy(ts,f->llvmRetArg,e->mem); | |
2472 } | |
2473 } | |
2474 else if (expty == Tdelegate) { | |
2475 // do nothing, handled by the DelegateExp | |
2476 LLVM_DtoDelegateCopy(f->llvmRetArg,e->mem); | |
2477 } | |
2478 else if (expty == Tarray) { | |
2479 if (e->type == elem::SLICE) { | |
2480 LLVM_DtoSetArray(f->llvmRetArg,e->arg,e->mem); | |
2481 } | |
2482 // else the return value is a variable and should already have been assigned by now | |
2483 } | |
2484 else | |
2485 assert(0); | |
2486 | |
2487 new llvm::ReturnInst(p->scopebb()); | |
2488 delete e; | |
2489 } | |
2490 else { | |
2491 elem* e = exp->toElem(p); | |
2492 llvm::Value* v = e->getValue(); | |
2493 Logger::cout() << *v << '\n'; | |
2494 new llvm::ReturnInst(v, p->scopebb()); | |
2495 delete e; | |
2496 } | |
2497 } | |
2498 else | |
2499 { | |
2500 if (p->topfunc()->getReturnType() == llvm::Type::VoidTy) | |
2501 new llvm::ReturnInst(p->scopebb()); | |
2502 else | |
2503 new llvm::UnreachableInst(p->scopebb()); | |
2504 } | |
2505 | |
2506 p->scope().returned = true; | |
2507 } | |
2508 | |
2509 void ExpStatement::toIR(IRState* p) | |
2510 { | |
2511 static int esi = 0; | |
2512 Logger::println("ExpStatement::toIR(%d): %s", esi++, toChars()); | |
2513 LOG_SCOPE; | |
2514 | |
2515 if (exp != 0) { | |
2516 elem* e = exp->toElem(p); | |
2517 delete e; | |
2518 } | |
2519 /*elem* e = exp->toElem(p); | |
2520 p->buf.printf("%s", e->toChars()); | |
2521 delete e; | |
2522 p->buf.writenl();*/ | |
2523 } | |
2524 | |
2525 void IfStatement::toIR(IRState* p) | |
2526 { | |
2527 static int wsi = 0; | |
2528 Logger::println("IfStatement::toIR(%d): %s", wsi++, toChars()); | |
2529 LOG_SCOPE; | |
2530 | |
2531 elem* cond_e = condition->toElem(p); | |
2532 llvm::Value* cond_val = cond_e->getValue(); | |
2533 delete cond_e; | |
2534 | |
2535 llvm::BasicBlock* oldend = gIR->scopeend(); | |
2536 | |
2537 llvm::BasicBlock* ifbb = new llvm::BasicBlock("if", gIR->topfunc(), oldend); | |
2538 llvm::BasicBlock* endbb = new llvm::BasicBlock("endif", gIR->topfunc(), oldend); | |
2539 llvm::BasicBlock* elsebb = 0; | |
2540 if (elsebody) { | |
2541 elsebb = new llvm::BasicBlock("else", gIR->topfunc(), endbb); | |
2542 } | |
2543 else { | |
2544 elsebb = endbb; | |
2545 } | |
2546 | |
2547 if (cond_val->getType() != llvm::Type::Int1Ty) { | |
2548 Logger::cout() << "if conditional: " << *cond_val << '\n'; | |
2549 cond_val = LLVM_DtoBoolean(cond_val); | |
2550 } | |
2551 llvm::Value* ifgoback = new llvm::BranchInst(ifbb, elsebb, cond_val, gIR->scopebegin()); | |
2552 | |
2553 // replace current scope | |
2554 gIR->scope() = IRScope(ifbb,elsebb); | |
2555 | |
2556 bool endifUsed = false; | |
2557 | |
2558 // do scoped statements | |
2559 ifbody->toIR(p); | |
2560 if (!gIR->scopereturned()) { | |
2561 new llvm::BranchInst(endbb,gIR->scopebegin()); | |
2562 endifUsed = true; | |
2563 } | |
2564 | |
2565 if (elsebody) { | |
2566 //assert(0); | |
2567 gIR->scope() = IRScope(elsebb,endbb); | |
2568 elsebody->toIR(p); | |
2569 if (!gIR->scopereturned()) { | |
2570 new llvm::BranchInst(endbb,gIR->scopebegin()); | |
2571 endifUsed = true; | |
2572 } | |
2573 } | |
2574 | |
2575 // rewrite the scope | |
2576 gIR->scope() = IRScope(endbb,oldend); | |
2577 } | |
2578 | |
2579 void ScopeStatement::toIR(IRState* p) | |
2580 { | |
2581 static int wsi = 0; | |
2582 Logger::println("ScopeStatement::toIR(%d): %s", wsi++, toChars()); | |
2583 LOG_SCOPE; | |
2584 | |
2585 llvm::BasicBlock* oldend = gIR->scopeend(); | |
2586 | |
2587 IRScope irs; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
2588 // remove useless branches by clearing and reusing the current basicblock |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
2589 llvm::BasicBlock* bb = gIR->scopebegin(); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
2590 if (bb->empty()) { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
2591 irs.begin = bb; |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
2592 } |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
2593 else { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
2594 irs.begin = new llvm::BasicBlock("scope", gIR->topfunc(), oldend); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
2595 new llvm::BranchInst(irs.begin, gIR->scopebegin()); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
2596 } |
1 | 2597 irs.end = new llvm::BasicBlock("endscope", gIR->topfunc(), oldend); |
2598 | |
2599 gIR->scope() = irs; | |
2600 | |
2601 statement->toIR(p); | |
2602 if (!gIR->scopereturned()) { | |
2603 new llvm::BranchInst(irs.end, gIR->scopebegin()); | |
2604 } | |
2605 | |
2606 // rewrite the scope | |
2607 gIR->scope() = IRScope(irs.end,oldend); | |
2608 } | |
2609 | |
2610 void WhileStatement::toIR(IRState* p) | |
2611 { | |
2612 static int wsi = 0; | |
2613 Logger::println("WhileStatement::toIR(%d): %s", wsi++, toChars()); | |
2614 LOG_SCOPE; | |
2615 | |
2616 // create while blocks | |
2617 llvm::BasicBlock* oldend = gIR->scopeend(); | |
2618 llvm::BasicBlock* whilebb = new llvm::BasicBlock("whilecond", gIR->topfunc(), oldend); | |
2619 llvm::BasicBlock* endbb = new llvm::BasicBlock("endwhile", gIR->topfunc(), oldend); | |
2620 | |
2621 // move into the while block | |
2622 new llvm::BranchInst(whilebb, gIR->scopebegin()); | |
2623 | |
2624 // replace current scope | |
2625 gIR->scope() = IRScope(whilebb,endbb); | |
2626 | |
2627 // create the condition | |
2628 elem* cond_e = condition->toElem(p); | |
2629 llvm::Value* cond_val = LLVM_DtoBoolean(cond_e->getValue()); | |
2630 delete cond_e; | |
2631 | |
2632 // while body block | |
2633 llvm::BasicBlock* whilebodybb = new llvm::BasicBlock("whilebody", gIR->topfunc(), endbb); | |
2634 | |
2635 // conditional branch | |
2636 llvm::Value* ifbreak = new llvm::BranchInst(whilebodybb, endbb, cond_val, whilebb); | |
2637 | |
2638 // rewrite scope | |
2639 gIR->scope() = IRScope(whilebodybb,endbb); | |
2640 | |
2641 // do while body code | |
2642 body->toIR(p); | |
2643 | |
2644 // loop | |
2645 new llvm::BranchInst(whilebb, gIR->scopebegin()); | |
2646 | |
2647 // rewrite the scope | |
2648 gIR->scope() = IRScope(endbb,oldend); | |
2649 } | |
2650 | |
2651 void DoStatement::toIR(IRState* p) | |
2652 { | |
2653 static int wsi = 0; | |
2654 Logger::println("DoStatement::toIR(%d): %s", wsi++, toChars()); | |
2655 LOG_SCOPE; | |
2656 | |
2657 // create while blocks | |
2658 llvm::BasicBlock* oldend = gIR->scopeend(); | |
2659 llvm::BasicBlock* dowhilebb = new llvm::BasicBlock("dowhile", gIR->topfunc(), oldend); | |
2660 llvm::BasicBlock* endbb = new llvm::BasicBlock("enddowhile", gIR->topfunc(), oldend); | |
2661 | |
2662 // move into the while block | |
2663 new llvm::BranchInst(dowhilebb, gIR->scopebegin()); | |
2664 | |
2665 // replace current scope | |
2666 gIR->scope() = IRScope(dowhilebb,endbb); | |
2667 | |
2668 // do do-while body code | |
2669 body->toIR(p); | |
2670 | |
2671 // create the condition | |
2672 elem* cond_e = condition->toElem(p); | |
2673 llvm::Value* cond_val = LLVM_DtoBoolean(cond_e->getValue()); | |
2674 delete cond_e; | |
2675 | |
2676 // conditional branch | |
2677 llvm::Value* ifbreak = new llvm::BranchInst(dowhilebb, endbb, cond_val, gIR->scopebegin()); | |
2678 | |
2679 // rewrite the scope | |
2680 gIR->scope() = IRScope(endbb,oldend); | |
2681 } | |
2682 | |
2683 void ForStatement::toIR(IRState* p) | |
2684 { | |
2685 static int wsi = 0; | |
2686 Logger::println("ForStatement::toIR(%d): %s", wsi++, toChars()); | |
2687 LOG_SCOPE; | |
2688 | |
2689 // create for blocks | |
2690 llvm::BasicBlock* oldend = gIR->scopeend(); | |
2691 llvm::BasicBlock* forbb = new llvm::BasicBlock("forcond", gIR->topfunc(), oldend); | |
2692 llvm::BasicBlock* forbodybb = new llvm::BasicBlock("forbody", gIR->topfunc(), oldend); | |
2693 llvm::BasicBlock* forincbb = new llvm::BasicBlock("forinc", gIR->topfunc(), oldend); | |
2694 llvm::BasicBlock* endbb = new llvm::BasicBlock("endfor", gIR->topfunc(), oldend); | |
2695 | |
2696 // init | |
2697 if (init != 0) | |
2698 init->toIR(p); | |
2699 | |
2700 // move into the for condition block, ie. start the loop | |
2701 new llvm::BranchInst(forbb, gIR->scopebegin()); | |
2702 | |
2703 IRScope loop; | |
2704 loop.begin = forincbb; | |
2705 loop.end = endbb; | |
2706 p->loopbbs.push_back(loop); | |
2707 | |
2708 // replace current scope | |
2709 gIR->scope() = IRScope(forbb,forbodybb); | |
2710 | |
2711 // create the condition | |
2712 elem* cond_e = condition->toElem(p); | |
2713 llvm::Value* cond_val = LLVM_DtoBoolean(cond_e->getValue()); | |
2714 delete cond_e; | |
2715 | |
2716 // conditional branch | |
2717 llvm::Value* ifbreak = new llvm::BranchInst(forbodybb, endbb, cond_val, forbb); | |
2718 | |
2719 // rewrite scope | |
2720 gIR->scope() = IRScope(forbodybb,forincbb); | |
2721 | |
2722 // do for body code | |
2723 body->toIR(p); | |
2724 | |
2725 // move into the for increment block | |
2726 new llvm::BranchInst(forincbb, gIR->scopebegin()); | |
2727 gIR->scope() = IRScope(forincbb, endbb); | |
2728 | |
2729 // increment | |
2730 if (increment) { | |
2731 elem* inc = increment->toElem(p); | |
2732 delete inc; | |
2733 } | |
2734 | |
2735 // loop | |
2736 new llvm::BranchInst(forbb, gIR->scopebegin()); | |
2737 | |
2738 p->loopbbs.pop_back(); | |
2739 | |
2740 // rewrite the scope | |
2741 gIR->scope() = IRScope(endbb,oldend); | |
2742 } | |
2743 | |
2744 void BreakStatement::toIR(IRState* p) | |
2745 { | |
2746 static int wsi = 0; | |
2747 Logger::println("BreakStatement::toIR(%d): %s", wsi++, toChars()); | |
2748 LOG_SCOPE; | |
2749 | |
2750 if (ident != 0) { | |
2751 Logger::println("ident = %s", ident->toChars()); | |
2752 assert(0); | |
2753 } | |
2754 else { | |
2755 new llvm::BranchInst(gIR->loopbbs.back().end, gIR->scopebegin()); | |
2756 } | |
2757 } | |
2758 | |
2759 void ContinueStatement::toIR(IRState* p) | |
2760 { | |
2761 static int wsi = 0; | |
2762 Logger::println("ContinueStatement::toIR(%d): %s", wsi++, toChars()); | |
2763 LOG_SCOPE; | |
2764 | |
2765 if (ident != 0) { | |
2766 Logger::println("ident = %s", ident->toChars()); | |
2767 assert(0); | |
2768 } | |
2769 else { | |
2770 new llvm::BranchInst(gIR->loopbbs.back().begin, gIR->scopebegin()); | |
2771 } | |
2772 } | |
2773 | |
2774 void OnScopeStatement::toIR(IRState* p) | |
2775 { | |
2776 static int wsi = 0; | |
2777 Logger::println("OnScopeStatement::toIR(%d): %s", wsi++, toChars()); | |
2778 LOG_SCOPE; | |
2779 | |
2780 assert(statement); | |
2781 //statement->toIR(p); // this seems to be redundant | |
2782 } | |
2783 | |
2784 void TryFinallyStatement::toIR(IRState* p) | |
2785 { | |
2786 static int wsi = 0; | |
2787 Logger::println("TryFinallyStatement::toIR(%d): %s", wsi++, toChars()); | |
2788 LOG_SCOPE; | |
2789 | |
2790 llvm::BasicBlock* oldend = gIR->scopeend(); | |
2791 | |
2792 llvm::BasicBlock* trybb = new llvm::BasicBlock("try", gIR->topfunc(), oldend); | |
2793 llvm::BasicBlock* finallybb = new llvm::BasicBlock("finally", gIR->topfunc(), oldend); | |
2794 llvm::BasicBlock* endbb = new llvm::BasicBlock("endtryfinally", gIR->topfunc(), oldend); | |
2795 | |
2796 // pass the previous BB into this | |
2797 new llvm::BranchInst(trybb, gIR->scopebegin()); | |
2798 | |
2799 gIR->scope() = IRScope(trybb,finallybb); | |
2800 | |
2801 assert(body); | |
2802 body->toIR(p); | |
2803 new llvm::BranchInst(finallybb, gIR->scopebegin()); | |
2804 | |
2805 // rewrite the scope | |
2806 gIR->scope() = IRScope(finallybb,endbb); | |
2807 | |
2808 assert(finalbody); | |
2809 finalbody->toIR(p); | |
2810 new llvm::BranchInst(endbb, gIR->scopebegin()); | |
2811 | |
2812 // rewrite the scope | |
2813 gIR->scope() = IRScope(endbb,oldend); | |
2814 } | |
2815 | |
2816 void TryCatchStatement::toIR(IRState* p) | |
2817 { | |
2818 static int wsi = 0; | |
2819 Logger::println("TryCatchStatement::toIR(%d): %s", wsi++, toChars()); | |
2820 LOG_SCOPE; | |
2821 | |
2822 assert(0 && "try-catch is not properly"); | |
2823 | |
2824 assert(body); | |
2825 body->toIR(p); | |
2826 | |
2827 assert(catches); | |
2828 for(size_t i=0; i<catches->dim; ++i) | |
2829 { | |
2830 Catch* c = (Catch*)catches->data[i]; | |
2831 c->handler->toIR(p); | |
2832 } | |
2833 } | |
2834 | |
2835 void ThrowStatement::toIR(IRState* p) | |
2836 { | |
2837 static int wsi = 0; | |
2838 Logger::println("ThrowStatement::toIR(%d): %s", wsi++, toChars()); | |
2839 LOG_SCOPE; | |
2840 | |
2841 assert(0 && "throw is not implemented"); | |
2842 | |
2843 assert(exp); | |
2844 elem* e = exp->toElem(p); | |
2845 delete e; | |
2846 } | |
2847 | |
2848 #define STUBST(x) void x::toIR(IRState * p) {error("Statement type "#x" not implemented: %s", toChars());fatal();} | |
2849 //STUBST(BreakStatement); | |
2850 //STUBST(ForStatement); | |
2851 STUBST(WithStatement); | |
2852 STUBST(SynchronizedStatement); | |
2853 //STUBST(ReturnStatement); | |
2854 //STUBST(ContinueStatement); | |
2855 STUBST(DefaultStatement); | |
2856 STUBST(CaseStatement); | |
2857 STUBST(SwitchStatement); | |
2858 STUBST(SwitchErrorStatement); | |
2859 STUBST(Statement); | |
2860 //STUBST(IfStatement); | |
2861 STUBST(ForeachStatement); | |
2862 //STUBST(DoStatement); | |
2863 //STUBST(WhileStatement); | |
2864 //STUBST(ExpStatement); | |
2865 //STUBST(CompoundStatement); | |
2866 //STUBST(ScopeStatement); | |
2867 STUBST(AsmStatement); | |
2868 //STUBST(TryCatchStatement); | |
2869 //STUBST(TryFinallyStatement); | |
2870 STUBST(VolatileStatement); | |
2871 STUBST(LabelStatement); | |
2872 //STUBST(ThrowStatement); | |
2873 STUBST(GotoCaseStatement); | |
2874 STUBST(GotoDefaultStatement); | |
2875 STUBST(GotoStatement); | |
2876 STUBST(UnrolledLoopStatement); | |
2877 //STUBST(OnScopeStatement); | |
2878 | |
2879 | |
2880 void | |
2881 EnumDeclaration::toDebug() | |
2882 { | |
2883 | |
2884 } | |
2885 | |
2886 int | |
2887 Dsymbol::cvMember(unsigned char*) | |
2888 { | |
2889 return 0; | |
2890 } | |
2891 int | |
2892 EnumDeclaration::cvMember(unsigned char*) | |
2893 { | |
2894 return 0; | |
2895 } | |
2896 int | |
2897 FuncDeclaration::cvMember(unsigned char*) | |
2898 { | |
2899 return 0; | |
2900 } | |
2901 int | |
2902 VarDeclaration::cvMember(unsigned char*) | |
2903 { | |
2904 return 0; | |
2905 } | |
2906 int | |
2907 TypedefDeclaration::cvMember(unsigned char*) | |
2908 { | |
2909 return 0; | |
2910 } | |
2911 | |
2912 void obj_includelib(char*){} | |
2913 | |
2914 AsmStatement::AsmStatement(Loc loc, Token *tokens) : | |
2915 Statement(loc) | |
2916 { | |
2917 } | |
2918 Statement *AsmStatement::syntaxCopy() { | |
2919 return 0; | |
2920 } | |
2921 | |
2922 Statement *AsmStatement::semantic(Scope *sc) | |
2923 { | |
2924 return Statement::semantic(sc); | |
2925 } | |
2926 | |
2927 | |
2928 void AsmStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2929 { | |
2930 Statement::toCBuffer(buf, hgs); | |
2931 } | |
2932 | |
2933 int AsmStatement::comeFrom() | |
2934 { | |
2935 return FALSE; | |
2936 } | |
2937 | |
2938 void | |
2939 backend_init() | |
2940 { | |
2941 //LLVM_D_InitRuntime(); | |
2942 // lazily loaded | |
2943 } | |
2944 | |
2945 void | |
2946 backend_term() | |
2947 { | |
2948 LLVM_D_FreeRuntime(); | |
2949 } |