Mercurial > projects > ldc
annotate gen/toir.c @ 18:c05ef76f1c20 trunk
[svn r22] * Forgot to add std.stdio
author | lindquist |
---|---|
date | Thu, 04 Oct 2007 01:47:53 +0200 |
parents | 37a4fdab33fc |
children | 8d45266bbabe |
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(); | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
387 Logger::cout() << "tmp: " << *tmp << " ||| " << *l->mem << '\n'; |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
388 // assignment to this in constructor special case |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
389 if (l->isthis) { |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
390 FuncDeclaration* fdecl = p->funcdecls.back(); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
391 // respecify the this param |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
392 if (!llvm::isa<llvm::AllocaInst>(fdecl->llvmThisVar)) |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
393 fdecl->llvmThisVar = new llvm::AllocaInst(tmp->getType(), "newthis", p->topallocapoint()); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
394 new llvm::StoreInst(tmp, fdecl->llvmThisVar, p->scopebb()); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
395 } |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
396 // regular class ref -> class ref assignment |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
397 else { |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
398 new llvm::StoreInst(tmp, l->mem, p->scopebb()); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
399 } |
1 | 400 } |
401 else | |
402 assert(0); | |
403 } | |
404 else if (e1ty == Tdelegate) { | |
405 Logger::println("Assigning to delegate"); | |
406 if (e2ty == Tdelegate) { | |
407 if (r->type == elem::NUL) { | |
408 llvm::Constant* c = llvm::cast<llvm::Constant>(r->val); | |
409 if (c->isNullValue()) { | |
410 LLVM_DtoNullDelegate(l->mem); | |
411 } | |
412 else | |
413 assert(0); | |
414 } | |
415 else if (r->inplace) { | |
416 // do nothing | |
417 e->inplace = true; | |
418 } | |
419 else | |
420 assert(0); | |
421 } | |
422 else | |
423 assert(0); | |
424 } | |
425 // !struct && !array && !pointer && !class | |
426 else { | |
427 Logger::cout() << *l->mem << '\n'; | |
428 new llvm::StoreInst(r->getValue(),l->mem,p->scopebb()); | |
429 } | |
430 | |
431 delete r; | |
432 delete l; | |
433 return e; | |
434 } | |
435 | |
436 ////////////////////////////////////////////////////////////////////////////////////////// | |
437 | |
438 elem* AddExp::toElem(IRState* p) | |
439 { | |
440 Logger::print("AddExp::toElem: %s\n", toChars()); | |
441 LOG_SCOPE; | |
442 elem* e = new elem; | |
443 elem* l = e1->toElem(p); | |
444 elem* r = e2->toElem(p); | |
445 | |
446 if (e1->type != e2->type) { | |
447 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
|
448 //assert(l->field); |
1 | 449 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); |
450 assert(r->type == elem::CONST); | |
451 llvm::ConstantInt* cofs = llvm::cast<llvm::ConstantInt>(r->val); | |
452 | |
453 TypeStruct* ts = (TypeStruct*)e1->type->next; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
454 std::vector<unsigned> offsets(1,0); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
455 ts->sym->offsetToIndex(cofs->getZExtValue(), offsets); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
456 e->mem = LLVM_DtoGEP(l->getValue(), offsets, "tmp", p->scopebb()); |
1 | 457 e->type = elem::VAR; |
458 e->field = true; | |
459 } | |
460 else if (e1->type->ty == Tpointer) { | |
461 e->val = new llvm::GetElementPtrInst(l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
462 e->type = elem::VAR; | |
463 } | |
464 else { | |
465 assert(0); | |
466 } | |
467 } | |
468 else { | |
469 e->val = llvm::BinaryOperator::createAdd(l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
470 e->type = elem::VAL; | |
471 } | |
472 delete l; | |
473 delete r; | |
474 return e; | |
475 } | |
476 | |
477 ////////////////////////////////////////////////////////////////////////////////////////// | |
478 | |
479 elem* AddAssignExp::toElem(IRState* p) | |
480 { | |
481 Logger::print("AddAssignExp::toElem: %s\n", toChars()); | |
482 LOG_SCOPE; | |
483 | |
484 elem* l = e1->toElem(p); | |
485 elem* r = e2->toElem(p); | |
486 | |
487 elem* e = new elem; | |
488 llvm::Value* val = 0; | |
489 if (e1->type->ty == Tpointer) { | |
490 val = e->mem = new llvm::GetElementPtrInst(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
491 } | |
492 else { | |
493 val = e->val = llvm::BinaryOperator::createAdd(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
494 } | |
495 | |
496 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
497 if (llvm::isa<llvm::PointerType>(storeVal->getType()) && storeVal->getType()->getContainedType(0) != tmp->getType()) | |
498 { | |
499 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
500 }*/ | |
501 | |
502 new llvm::StoreInst(val,l->mem,p->scopebb()); | |
503 e->type = elem::VAR; | |
504 | |
505 delete l; | |
506 delete r; | |
507 return e; | |
508 } | |
509 | |
510 ////////////////////////////////////////////////////////////////////////////////////////// | |
511 | |
512 elem* MinExp::toElem(IRState* p) | |
513 { | |
514 Logger::print("MinExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
515 LOG_SCOPE; | |
516 elem* e = new elem; | |
517 elem* l = e1->toElem(p); | |
518 elem* r = e2->toElem(p); | |
519 | |
520 llvm::Value* left = l->getValue(); | |
521 if (llvm::isa<llvm::PointerType>(left->getType())) | |
522 left = new llvm::PtrToIntInst(left,LLVM_DtoSize_t(),"tmp",p->scopebb()); | |
523 | |
524 llvm::Value* right = r->getValue(); | |
525 if (llvm::isa<llvm::PointerType>(right->getType())) | |
526 right = new llvm::PtrToIntInst(right,LLVM_DtoSize_t(),"tmp",p->scopebb()); | |
527 | |
528 e->val = llvm::BinaryOperator::createSub(left,right,"tmp",p->scopebb()); | |
529 e->type = elem::VAL; | |
530 | |
531 const llvm::Type* totype = LLVM_DtoType(type); | |
532 if (e->val->getType() != totype) { | |
533 assert(0); | |
534 assert(llvm::isa<llvm::PointerType>(e->val->getType())); | |
535 assert(llvm::isa<llvm::IntegerType>(totype)); | |
536 e->val = new llvm::IntToPtrInst(e->val,totype,"tmp",p->scopebb()); | |
537 } | |
538 | |
539 delete l; | |
540 delete r; | |
541 return e; | |
542 } | |
543 | |
544 ////////////////////////////////////////////////////////////////////////////////////////// | |
545 | |
546 elem* MinAssignExp::toElem(IRState* p) | |
547 { | |
548 Logger::print("MinAssignExp::toElem: %s\n", toChars()); | |
549 LOG_SCOPE; | |
550 | |
551 elem* l = e1->toElem(p); | |
552 elem* r = e2->toElem(p); | |
553 | |
554 llvm::Value* tmp = 0; | |
555 if (e1->type->ty == Tpointer) { | |
556 tmp = r->getValue(); | |
557 llvm::Value* zero = llvm::ConstantInt::get(tmp->getType(),0,false); | |
558 tmp = llvm::BinaryOperator::createSub(zero,tmp,"tmp",p->scopebb()); | |
559 tmp = new llvm::GetElementPtrInst(l->getValue(),tmp,"tmp",p->scopebb()); | |
560 } | |
561 else { | |
562 tmp = llvm::BinaryOperator::createSub(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
563 } | |
564 | |
565 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
566 if (storeVal->getType()->getContainedType(0) != tmp->getType()) | |
567 { | |
568 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
569 }*/ | |
570 | |
571 new llvm::StoreInst(tmp, l->mem, p->scopebb()); | |
572 | |
573 delete l; | |
574 delete r; | |
575 | |
576 elem* e = new elem; | |
577 e->val = tmp; | |
578 e->type = elem::VAR; | |
579 return e; | |
580 } | |
581 | |
582 ////////////////////////////////////////////////////////////////////////////////////////// | |
583 | |
584 elem* MulExp::toElem(IRState* p) | |
585 { | |
586 Logger::print("MulExp::toElem: %s\n", toChars()); | |
587 LOG_SCOPE; | |
588 elem* e = new elem; | |
589 elem* l = e1->toElem(p); | |
590 elem* r = e2->toElem(p); | |
591 llvm::Value* vl = l->getValue(); | |
592 llvm::Value* vr = r->getValue(); | |
593 Logger::cout() << "mul: " << *vl << ", " << *vr << '\n'; | |
594 e->val = llvm::BinaryOperator::createMul(vl,vr,"tmp",p->scopebb()); | |
595 e->type = elem::VAL; | |
596 delete l; | |
597 delete r; | |
598 return e; | |
599 } | |
600 | |
601 ////////////////////////////////////////////////////////////////////////////////////////// | |
602 | |
603 elem* MulAssignExp::toElem(IRState* p) | |
604 { | |
605 Logger::print("MulAssignExp::toElem: %s\n", toChars()); | |
606 LOG_SCOPE; | |
607 | |
608 elem* l = e1->toElem(p); | |
609 elem* r = e2->toElem(p); | |
610 llvm::Value* vl = l->getValue(); | |
611 llvm::Value* vr = r->getValue(); | |
612 Logger::cout() << "mulassign: " << *vl << ", " << *vr << '\n'; | |
613 llvm::Value* tmp = llvm::BinaryOperator::createMul(vl,vr,"tmp",p->scopebb()); | |
614 | |
615 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
616 if (storeVal->getType()->getContainedType(0) != tmp->getType()) | |
617 { | |
618 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
619 }*/ | |
620 | |
621 new llvm::StoreInst(tmp,l->mem,p->scopebb()); | |
622 | |
623 delete l; | |
624 delete r; | |
625 | |
626 elem* e = new elem; | |
627 e->val = tmp; | |
628 e->type = elem::VAR; | |
629 return e; | |
630 } | |
631 | |
632 ////////////////////////////////////////////////////////////////////////////////////////// | |
633 | |
634 elem* DivExp::toElem(IRState* p) | |
635 { | |
636 Logger::print("DivExp::toElem: %s\n", toChars()); | |
637 LOG_SCOPE; | |
638 elem* e = new elem; | |
639 elem* l = e1->toElem(p); | |
640 elem* r = e2->toElem(p); | |
641 | |
642 if (type->isunsigned()) | |
643 e->val = llvm::BinaryOperator::createUDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
644 else if (type->isintegral()) | |
645 e->val = llvm::BinaryOperator::createSDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
646 else if (type->isfloating()) | |
647 e->val = llvm::BinaryOperator::createFDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
648 else | |
649 assert(0); | |
650 e->type = elem::VAL; | |
651 delete l; | |
652 delete r; | |
653 return e; | |
654 } | |
655 | |
656 ////////////////////////////////////////////////////////////////////////////////////////// | |
657 | |
658 elem* DivAssignExp::toElem(IRState* p) | |
659 { | |
660 Logger::print("DivAssignExp::toElem: %s\n", toChars()); | |
661 LOG_SCOPE; | |
662 | |
663 elem* l = e1->toElem(p); | |
664 elem* r = e2->toElem(p); | |
665 | |
666 llvm::Value* tmp; | |
667 if (type->isunsigned()) | |
668 tmp = llvm::BinaryOperator::createUDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
669 else if (type->isintegral()) | |
670 tmp = llvm::BinaryOperator::createSDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
671 else if (type->isfloating()) | |
672 tmp = llvm::BinaryOperator::createFDiv(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
673 else | |
674 assert(0); | |
675 | |
676 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
677 if (storeVal->getType()->getContainedType(0) != tmp->getType()) | |
678 { | |
679 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
680 }*/ | |
681 | |
682 new llvm::StoreInst(tmp,l->mem,p->scopebb()); | |
683 | |
684 delete l; | |
685 delete r; | |
686 | |
687 elem* e = new elem; | |
688 e->val = tmp; | |
689 e->type = elem::VAR; | |
690 return e; | |
691 } | |
692 | |
693 ////////////////////////////////////////////////////////////////////////////////////////// | |
694 | |
695 elem* ModExp::toElem(IRState* p) | |
696 { | |
697 Logger::print("ModExp::toElem: %s\n", toChars()); | |
698 LOG_SCOPE; | |
699 elem* e = new elem; | |
700 elem* l = e1->toElem(p); | |
701 elem* r = e2->toElem(p); | |
702 | |
703 if (type->isunsigned()) | |
704 e->val = llvm::BinaryOperator::createURem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
705 else if (type->isintegral()) | |
706 e->val = llvm::BinaryOperator::createSRem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
707 else if (type->isfloating()) | |
708 e->val = llvm::BinaryOperator::createFRem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
709 else | |
710 assert(0); | |
711 e->type = elem::VAL; | |
712 delete l; | |
713 delete r; | |
714 return e; | |
715 } | |
716 | |
717 ////////////////////////////////////////////////////////////////////////////////////////// | |
718 | |
719 elem* ModAssignExp::toElem(IRState* p) | |
720 { | |
721 Logger::print("ModAssignExp::toElem: %s\n", toChars()); | |
722 LOG_SCOPE; | |
723 | |
724 elem* l = e1->toElem(p); | |
725 elem* r = e2->toElem(p); | |
726 | |
727 llvm::Value* tmp; | |
728 if (type->isunsigned()) | |
729 tmp = llvm::BinaryOperator::createURem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
730 else if (type->isintegral()) | |
731 tmp = llvm::BinaryOperator::createSRem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
732 else if (type->isfloating()) | |
733 tmp = llvm::BinaryOperator::createFRem(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
734 else | |
735 assert(0); | |
736 | |
737 /*llvm::Value* storeVal = l->storeVal ? l->storeVal : l->val; | |
738 if (storeVal->getType()->getContainedType(0) != tmp->getType()) | |
739 { | |
740 tmp = LLVM_DtoPointedType(storeVal, tmp); | |
741 }*/ | |
742 | |
743 new llvm::StoreInst(tmp,l->mem,p->scopebb()); | |
744 | |
745 delete l; | |
746 delete r; | |
747 | |
748 elem* e = new elem; | |
749 e->val = tmp; | |
750 e->type = elem::VAR; | |
751 return e; | |
752 } | |
753 | |
754 ////////////////////////////////////////////////////////////////////////////////////////// | |
755 | |
756 elem* CallExp::toElem(IRState* p) | |
757 { | |
758 Logger::print("CallExp::toElem: %s\n", toChars()); | |
759 LOG_SCOPE; | |
760 elem* e = new elem; | |
761 elem* fn = e1->toElem(p); | |
762 LINK dlink = LINKdefault; | |
18 | 763 |
1 | 764 bool delegateCall = false; |
765 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false); | |
766 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty,1,false); | |
767 | |
768 // hidden struct return parameter handling | |
769 bool retinptr = false; | |
18 | 770 |
1 | 771 TypeFunction* tf = 0; |
18 | 772 |
1 | 773 // regular functions |
774 if (e1->type->ty == Tfunction) { | |
775 tf = (TypeFunction*)e1->type; | |
776 if (tf->llvmRetInPtr) { | |
777 retinptr = true; | |
778 } | |
779 dlink = tf->linkage; | |
780 } | |
18 | 781 |
1 | 782 // delegates |
783 else if (e1->type->ty == Tdelegate) { | |
784 Logger::println("delegateTy = %s\n", e1->type->toChars()); | |
785 assert(e1->type->next->ty == Tfunction); | |
786 tf = (TypeFunction*)e1->type->next; | |
787 if (tf->llvmRetInPtr) { | |
788 retinptr = true; | |
789 } | |
790 dlink = tf->linkage; | |
791 delegateCall = true; | |
792 } | |
18 | 793 |
1 | 794 // invalid |
795 else { | |
796 assert(tf); | |
797 } | |
798 | |
799 size_t n = arguments->dim; | |
800 if (fn->arg || delegateCall) n++; | |
801 if (retinptr) n++; | |
802 | |
803 llvm::Value* funcval = fn->getValue(); | |
18 | 804 assert(funcval != 0); |
1 | 805 std::vector<llvm::Value*> llargs(n, 0); |
806 | |
807 const llvm::FunctionType* llfnty = 0; | |
18 | 808 |
1 | 809 // normal function call |
810 if (llvm::isa<llvm::FunctionType>(funcval->getType())) { | |
811 llfnty = llvm::cast<llvm::FunctionType>(funcval->getType()); | |
812 } | |
813 // pointer to something | |
814 else if (llvm::isa<llvm::PointerType>(funcval->getType())) { | |
815 // pointer to function pointer - I think this not really supposed to happen, but does :/ | |
816 // seems like sometimes we get a func* other times a func** | |
817 if (llvm::isa<llvm::PointerType>(funcval->getType()->getContainedType(0))) { | |
818 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); | |
819 } | |
820 // function pointer | |
821 if (llvm::isa<llvm::FunctionType>(funcval->getType()->getContainedType(0))) { | |
822 //Logger::cout() << "function pointer type:\n" << *funcval << '\n'; | |
823 llfnty = llvm::cast<llvm::FunctionType>(funcval->getType()->getContainedType(0)); | |
824 } | |
825 // struct pointer - delegate | |
826 else if (llvm::isa<llvm::StructType>(funcval->getType()->getContainedType(0))) { | |
6 | 827 funcval = LLVM_DtoGEP(funcval,zero,one,"tmp",p->scopebb()); |
1 | 828 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); |
829 const llvm::Type* ty = funcval->getType()->getContainedType(0); | |
830 llfnty = llvm::cast<llvm::FunctionType>(ty); | |
831 } | |
832 // unknown | |
833 else { | |
834 Logger::cout() << "what kind of pointer are we calling? : " << *funcval->getType() << '\n'; | |
835 } | |
836 } | |
837 else { | |
838 Logger::cout() << "what are we calling? : " << *funcval << '\n'; | |
839 } | |
840 assert(llfnty); | |
841 Logger::cout() << "Function LLVM type: " << *llfnty << '\n'; | |
842 | |
843 // argument handling | |
844 llvm::FunctionType::param_iterator argiter = llfnty->param_begin(); | |
845 int j = 0; | |
846 | |
847 // hidden struct return parameter | |
848 if (retinptr) { | |
849 if (!p->lvals.empty()) { | |
850 assert(llvm::isa<llvm::StructType>(p->toplval()->getType()->getContainedType(0))); | |
851 llargs[j] = p->toplval(); | |
852 TY Dty = tf->next->ty; | |
853 if (Dty == Tstruct || Dty == Tdelegate || Dty == Tarray) { | |
854 e->inplace = true; | |
855 } | |
856 else | |
857 assert(0); | |
858 } | |
859 else { | |
860 llargs[j] = new llvm::AllocaInst(argiter->get()->getContainedType(0),"rettmp",p->topallocapoint()); | |
861 } | |
862 ++j; | |
863 ++argiter; | |
864 e->type = elem::VAR; | |
865 } | |
866 else { | |
867 e->type = elem::VAL; | |
868 } | |
869 | |
870 // this parameter | |
871 if (fn->arg) { | |
872 Logger::println("This Call"); | |
873 if (fn->arg->getType() != argiter->get()) { | |
874 //Logger::cout() << *fn->thisparam << '|' << *argiter->get() << '\n'; | |
875 llargs[j] = new llvm::BitCastInst(fn->arg, argiter->get(), "tmp", p->scopebb()); | |
876 } | |
877 else { | |
878 llargs[j] = fn->arg; | |
879 } | |
880 ++j; | |
881 ++argiter; | |
882 } | |
883 // delegate context parameter | |
884 else if (delegateCall) { | |
885 Logger::println("Delegate Call"); | |
6 | 886 llvm::Value* contextptr = LLVM_DtoGEP(fn->mem,zero,zero,"tmp",p->scopebb()); |
1 | 887 llargs[j] = new llvm::LoadInst(contextptr,"tmp",p->scopebb()); |
888 ++j; | |
889 ++argiter; | |
890 } | |
891 | |
892 // regular parameters | |
893 for (int i=0; i<arguments->dim; i++,j++) | |
894 { | |
895 Expression* argexp = (Expression*)arguments->data[i]; | |
896 elem* arg = argexp->toElem(p); | |
897 | |
898 Argument* fnarg = Argument::getNth(tf->parameters, i); | |
899 | |
900 TY argty = argexp->type->ty; | |
901 if (argty == Tstruct || argty == Tdelegate || argty == Tarray) { | |
902 if (!fnarg || !fnarg->llvmCopy) { | |
903 llargs[j] = arg->getValue(); | |
904 assert(llargs[j] != 0); | |
905 } | |
906 else { | |
907 llvm::Value* allocaInst = 0; | |
908 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
909 const llvm::PointerType* pty = llvm::cast<llvm::PointerType>(arg->mem->getType()); | |
910 if (argty == Tstruct) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
911 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", p->topallocapoint()); |
1 | 912 TypeStruct* ts = (TypeStruct*)argexp->type; |
913 LLVM_DtoStructCopy(ts,allocaInst,arg->mem); | |
914 } | |
915 else if (argty == Tdelegate) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
916 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", p->topallocapoint()); |
1 | 917 LLVM_DtoDelegateCopy(allocaInst,arg->mem); |
918 } | |
919 else if (argty == Tarray) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
920 if (arg->type == elem::SLICE) { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
921 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
|
922 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
|
923 } |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
924 else { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
925 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
|
926 LLVM_DtoArrayAssign(allocaInst,arg->mem); |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
927 } |
1 | 928 } |
929 else | |
930 assert(0); | |
931 | |
932 llargs[j] = allocaInst; | |
933 assert(llargs[j] != 0); | |
934 } | |
935 } | |
936 else if (!fnarg || fnarg->llvmCopy) { | |
937 llargs[j] = arg->getValue(); | |
938 assert(llargs[j] != 0); | |
939 } | |
940 else { | |
5
3d60e549b0c2
[svn r9] added a preliminary rebuild profile. llvmdc-posix - very handy :)
lindquist
parents:
4
diff
changeset
|
941 llargs[j] = arg->mem ? arg->mem : arg->val; |
1 | 942 assert(llargs[j] != 0); |
943 } | |
944 | |
945 delete arg; | |
946 } | |
947 | |
948 // void returns cannot not be named | |
949 const char* varname = ""; | |
950 if (llfnty->getReturnType() != llvm::Type::VoidTy) | |
951 varname = "tmp"; | |
952 | |
953 Logger::println("%d params passed", n); | |
954 for (int i=0; i<n; ++i) | |
955 { | |
956 Logger::cout() << *llargs[i] << '\n'; | |
957 } | |
958 | |
18 | 959 //Logger::cout() << "Calling: " << *funcval->getType() << '\n'; |
1 | 960 |
961 // call the function | |
962 llvm::CallInst* call = new llvm::CallInst(funcval, llargs.begin(), llargs.end(), varname, p->scopebb()); | |
963 e->val = call; | |
964 | |
965 // set calling convention | |
966 if ((fn->funcdecl && (fn->funcdecl->llvmInternal != LLVMintrinsic)) || delegateCall) | |
967 call->setCallingConv(LLVM_DtoCallingConv(dlink)); | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
968 else if (fn->callconv != (unsigned)-1) |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
969 call->setCallingConv(fn->callconv); |
1 | 970 |
971 delete fn; | |
972 return e; | |
973 } | |
974 | |
975 ////////////////////////////////////////////////////////////////////////////////////////// | |
976 | |
977 elem* CastExp::toElem(IRState* p) | |
978 { | |
979 Logger::print("CastExp::toElem: %s\n", toChars()); | |
980 LOG_SCOPE; | |
981 elem* e = new elem; | |
982 elem* u = e1->toElem(p); | |
983 const llvm::Type* totype = LLVM_DtoType(to); | |
984 Type* from = e1->type; | |
985 int lsz = from->size(); | |
986 int rsz = to->size(); | |
987 | |
988 // this makes sure the strange lvalue casts don't screw things up | |
989 e->mem = u->mem; | |
990 | |
991 if (from->isintegral()) { | |
992 if (to->isintegral()) { | |
993 if (lsz < rsz) { | |
994 Logger::cout() << *totype << '\n'; | |
995 if (from->isunsigned() || from->ty == Tbool) { | |
996 e->val = new llvm::ZExtInst(u->getValue(), totype, "tmp", p->scopebb()); | |
997 } else { | |
998 e->val = new llvm::SExtInst(u->getValue(), totype, "tmp", p->scopebb()); | |
999 } | |
1000 } | |
1001 else if (lsz > rsz) { | |
1002 e->val = new llvm::TruncInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1003 } | |
1004 else { | |
1005 e->val = new llvm::BitCastInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1006 } | |
1007 } | |
1008 else if (to->isfloating()) { | |
1009 if (from->isunsigned()) { | |
1010 e->val = new llvm::UIToFPInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1011 } | |
1012 else { | |
1013 e->val = new llvm::SIToFPInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1014 } | |
1015 } | |
1016 else { | |
1017 assert(0); | |
1018 } | |
1019 //e->storeVal = u->storeVal ? u->storeVal : u->val; | |
1020 e->type = elem::VAL; | |
1021 } | |
1022 else if (from->isfloating()) { | |
1023 if (to->isfloating()) { | |
1024 if ((from->ty == Tfloat80 || from->ty == Tfloat64) && (to->ty == Tfloat80 || to->ty == Tfloat64)) { | |
1025 e->val = u->getValue(); | |
1026 } | |
1027 else if (lsz < rsz) { | |
1028 e->val = new llvm::FPExtInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1029 } | |
1030 else if (lsz > rsz) { | |
1031 e->val = new llvm::FPTruncInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1032 } | |
1033 else { | |
1034 assert(0); | |
1035 } | |
1036 } | |
1037 else if (to->isintegral()) { | |
1038 if (to->isunsigned()) { | |
1039 e->val = new llvm::FPToUIInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1040 } | |
1041 else { | |
1042 e->val = new llvm::FPToSIInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1043 } | |
1044 } | |
1045 else { | |
1046 assert(0); | |
1047 } | |
1048 e->type = elem::VAL; | |
1049 } | |
1050 else if (from->ty == Tclass) { | |
1051 //assert(to->ty == Tclass); | |
1052 e->val = new llvm::BitCastInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1053 e->type = elem::VAL; | |
1054 } | |
1055 else if (from->ty == Tarray || from->ty == Tsarray) { | |
1056 Logger::cout() << "from array or sarray" << '\n'; | |
1057 if (to->ty == Tpointer) { | |
1058 Logger::cout() << "to pointer" << '\n'; | |
1059 assert(from->next == to->next); | |
1060 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1061 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
6 | 1062 llvm::Value* ptr = LLVM_DtoGEP(u->getValue(),zero,one,"tmp",p->scopebb()); |
1 | 1063 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb()); |
1064 e->type = elem::VAL; | |
1065 } | |
1066 else if (to->ty == Tarray) { | |
1067 Logger::cout() << "to array" << '\n'; | |
1068 assert(from->next->size() == to->next->size()); | |
1069 const llvm::Type* ptrty = LLVM_DtoType(to->next); | |
1070 if (ptrty == llvm::Type::VoidTy) | |
1071 ptrty = llvm::Type::Int8Ty; | |
1072 ptrty = llvm::PointerType::get(ptrty); | |
1073 | |
1074 if (u->type == elem::SLICE) { | |
1075 e->mem = new llvm::BitCastInst(u->mem, ptrty, "tmp", p->scopebb()); | |
1076 e->arg = u->arg; | |
1077 } | |
1078 else { | |
1079 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
|
1080 if (from->ty == Tsarray) { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1081 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
|
1082 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
|
1083 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
|
1084 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
|
1085 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
|
1086 } |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1087 else { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1088 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
|
1089 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); |
6 | 1090 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
|
1091 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
|
1092 |
6 | 1093 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
|
1094 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
|
1095 //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
|
1096 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
|
1097 } |
1 | 1098 } |
1099 e->type = elem::SLICE; | |
1100 } | |
1101 else if (to->ty == Tsarray) { | |
1102 Logger::cout() << "to sarray" << '\n'; | |
1103 assert(0); | |
1104 } | |
1105 else { | |
1106 assert(0); | |
1107 } | |
1108 } | |
1109 else if (from->ty == Tpointer) { | |
1110 if (to->ty == Tpointer || to->ty == Tclass) { | |
1111 llvm::Value* src = u->getValue(); | |
1112 //Logger::cout() << *src << '|' << *totype << '\n'; | |
1113 e->val = new llvm::BitCastInst(src, totype, "tmp", p->scopebb()); | |
1114 } | |
1115 else if (to->isintegral()) { | |
1116 e->val = new llvm::PtrToIntInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1117 } | |
1118 else | |
1119 assert(0); | |
1120 e->type = elem::VAL; | |
1121 } | |
1122 else { | |
1123 assert(0); | |
1124 } | |
1125 delete u; | |
1126 return e; | |
1127 } | |
1128 | |
1129 ////////////////////////////////////////////////////////////////////////////////////////// | |
1130 | |
1131 elem* SymOffExp::toElem(IRState* p) | |
1132 { | |
1133 Logger::print("SymOffExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1134 LOG_SCOPE; | |
1135 elem* e = 0; | |
1136 if (VarDeclaration* vd = var->isVarDeclaration()) | |
1137 { | |
1138 Logger::println("VarDeclaration"); | |
1139 if (vd->type->ty == Tstruct && !(type->ty == Tpointer && type->next == vd->type)) { | |
1140 TypeStruct* vdt = (TypeStruct*)vd->type; | |
1141 e = new elem; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1142 std::vector<unsigned> dst(1,0); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1143 vdt->sym->offsetToIndex(offset, dst); |
1 | 1144 llvm::Value* ptr = vd->llvmValue; |
1145 assert(ptr); | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1146 e->mem = LLVM_DtoGEP(ptr,dst,"tmp",p->scopebb()); |
1 | 1147 e->type = elem::VAL; |
1148 e->field = true; | |
1149 } | |
1150 else if (vd->type->ty == Tsarray) { | |
1151 /*e = new elem; | |
1152 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1153 e->val = new llvm::GetElementPtrInst(vd->llvmValue,idx0,idx0,"tmp",p->scopebb());*/ | |
1154 e = new elem; | |
1155 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1156 //llvm::Value* idx1 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
6 | 1157 e->mem = LLVM_DtoGEP(vd->llvmValue,idx0,idx0,"tmp",p->scopebb()); |
1 | 1158 e->type = elem::VAL; |
1159 } | |
1160 else if (offset == 0) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1161 /*if (!vd->llvmValue) |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1162 vd->toObjFile();*/ |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1163 assert(vd->llvmValue); |
1 | 1164 e = new elem; |
1165 e->mem = vd->llvmValue; | |
1166 //e->vardecl = vd; | |
1167 e->type = elem::VAL; | |
1168 } | |
1169 else { | |
1170 assert(0); | |
1171 } | |
1172 } | |
1173 else if (FuncDeclaration* fd = var->isFuncDeclaration()) | |
1174 { | |
1175 Logger::println("FuncDeclaration"); | |
1176 e = new elem; | |
1177 if (fd->llvmValue == 0) | |
1178 fd->toObjFile(); | |
1179 e->val = fd->llvmValue; | |
1180 //e->aspointer = true; | |
1181 e->type = elem::FUNC; | |
1182 } | |
1183 assert(e != 0); | |
1184 assert(e->type != elem::NONE); | |
1185 return e; | |
1186 } | |
1187 | |
1188 ////////////////////////////////////////////////////////////////////////////////////////// | |
1189 | |
1190 elem* PtrExp::toElem(IRState* p) | |
1191 { | |
1192 Logger::print("PtrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1193 LOG_SCOPE; | |
1194 elem* e = new elem; | |
1195 elem* a = e1->toElem(p); | |
1196 | |
1197 if (a->mem) | |
1198 Logger::cout() << "mem: " << *a->mem << '\n'; | |
1199 if (a->val) | |
1200 Logger::cout() << "val: " << *a->val << '\n'; | |
1201 | |
1202 if (a->field) | |
1203 e->mem = a->mem; | |
1204 else | |
1205 e->mem = a->getValue(); | |
1206 e->type = elem::VAR; | |
1207 | |
1208 delete a; | |
1209 return e; | |
1210 } | |
1211 | |
1212 ////////////////////////////////////////////////////////////////////////////////////////// | |
1213 | |
1214 elem* DotVarExp::toElem(IRState* p) | |
1215 { | |
1216 Logger::print("DotVarExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1217 LOG_SCOPE; | |
1218 elem* e = new elem; | |
1219 | |
1220 elem* l = e1->toElem(p); | |
1221 | |
1222 Logger::print("e1->type=%s\n", e1->type->toChars()); | |
1223 | |
1224 if (VarDeclaration* vd = var->isVarDeclaration()) { | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1225 std::vector<unsigned> vdoffsets(1,0); |
1 | 1226 llvm::Value* src = 0; |
1227 if (e1->type->ty == Tpointer) { | |
1228 assert(e1->type->next->ty == Tstruct); | |
1229 TypeStruct* ts = (TypeStruct*)e1->type->next; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1230 ts->sym->offsetToIndex(vd->offset, vdoffsets); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1231 Logger::println("Struct member offset:%d", vd->offset); |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1232 src = l->val ? l->val : l->mem; |
1 | 1233 } |
1234 else if (e1->type->ty == Tclass) { | |
1235 TypeClass* tc = (TypeClass*)e1->type; | |
1236 Logger::println("Class member offset: %d", vd->offset); | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1237 tc->sym->offsetToIndex(vd->offset, vdoffsets); |
1 | 1238 src = l->getValue(); |
1239 } | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1240 assert(vdoffsets.size() != 1); |
1 | 1241 assert(src != 0); |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1242 llvm::Value* arrptr = LLVM_DtoGEP(src,vdoffsets,"tmp",p->scopebb()); |
1 | 1243 e->mem = arrptr; |
1244 Logger::cout() << "mem: " << *e->mem << '\n'; | |
1245 e->type = elem::VAR; | |
1246 } | |
1247 else if (FuncDeclaration* fdecl = var->isFuncDeclaration()) | |
1248 { | |
1249 if (fdecl->llvmValue == 0) | |
1250 { | |
1251 fdecl->toObjFile(); | |
1252 } | |
1253 | |
1254 llvm::Value* funcval = fdecl->llvmValue; | |
1255 e->arg = l->getValue(); | |
1256 | |
1257 // virtual call | |
1258 if (fdecl->isVirtual()) { | |
1259 assert(fdecl->vtblIndex > 0); | |
1260 assert(e1->type->ty == Tclass); | |
1261 | |
1262 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1263 llvm::Value* vtblidx = llvm::ConstantInt::get(llvm::Type::Int32Ty, (size_t)fdecl->vtblIndex, false); | |
6 | 1264 funcval = LLVM_DtoGEP(e->arg, zero, zero, "tmp", p->scopebb()); |
1 | 1265 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); |
6 | 1266 funcval = LLVM_DtoGEP(funcval, zero, vtblidx, "tmp", p->scopebb()); |
1 | 1267 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); |
9 | 1268 assert(funcval->getType() == fdecl->llvmValue->getType()); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
1269 e->callconv = LLVM_DtoCallingConv(fdecl->linkage); |
1 | 1270 } |
1271 e->val = funcval; | |
1272 e->type = elem::VAL; | |
1273 } | |
1274 else { | |
1275 printf("unknown: %s\n", var->toChars()); | |
1276 assert(0); | |
1277 } | |
1278 | |
1279 delete l; | |
1280 | |
1281 return e; | |
1282 } | |
1283 | |
1284 ////////////////////////////////////////////////////////////////////////////////////////// | |
1285 | |
1286 elem* ThisExp::toElem(IRState* p) | |
1287 { | |
1288 Logger::print("ThisExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1289 LOG_SCOPE; | |
1290 elem* e = new elem; | |
1291 | |
1292 if (VarDeclaration* vd = var->isVarDeclaration()) { | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1293 /*assert(vd->llvmValue == 0); |
1 | 1294 |
1295 llvm::Function* fn = p->topfunc(); | |
1296 assert(fn); | |
1297 | |
1298 TypeFunction* tf = p->topfunctype(); | |
1299 assert(tf); | |
1300 | |
1301 llvm::Value* v = 0; | |
1302 if (tf->llvmRetInPtr) | |
1303 v = ++fn->arg_begin(); | |
1304 else | |
1305 v = fn->arg_begin(); | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1306 assert(v);*/ |
1 | 1307 |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1308 llvm::Value* v = p->funcdecls.back()->llvmThisVar; |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1309 if (llvm::isa<llvm::AllocaInst>(v)) |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1310 v = new llvm::LoadInst(v, "tmp", p->scopebb()); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1311 e->mem = v; |
1 | 1312 e->type = elem::VAL; |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1313 e->isthis = true; |
1 | 1314 } |
1315 else { | |
1316 assert(0); | |
1317 } | |
1318 | |
1319 return e; | |
1320 } | |
1321 | |
1322 ////////////////////////////////////////////////////////////////////////////////////////// | |
1323 | |
1324 elem* AddrExp::toElem(IRState* p) | |
1325 { | |
1326 Logger::print("AddrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1327 LOG_SCOPE; | |
1328 elem* e = e1->toElem(p); | |
1329 e->field = true; | |
1330 return e; | |
1331 } | |
1332 | |
1333 ////////////////////////////////////////////////////////////////////////////////////////// | |
1334 | |
1335 elem* StructLiteralExp::toElem(IRState* p) | |
1336 { | |
1337 Logger::print("StructLiteralExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1338 LOG_SCOPE; | |
1339 elem* e = new elem; | |
1340 | |
1341 // if there is no lval, this must be a static initializer for a global. correct? | |
1342 if (p->lvals.empty()) | |
1343 { | |
1344 // TODO | |
1345 assert(0); | |
1346 } | |
1347 // otherwise write directly in the lvalue | |
1348 else | |
1349 { | |
1350 llvm::Value* sptr = p->toplval(); | |
1351 assert(sptr); | |
1352 | |
1353 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1354 | |
1355 unsigned n = elements->dim; | |
1356 for (unsigned i=0; i<n; ++i) | |
1357 { | |
1358 llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false); | |
6 | 1359 llvm::Value* arrptr = LLVM_DtoGEP(sptr,zero,offset,"tmp",p->scopebb()); |
1 | 1360 |
1361 Expression* vx = (Expression*)elements->data[i]; | |
1362 if (vx != 0) { | |
1363 elem* ve = vx->toElem(p); | |
1364 //Logger::cout() << *ve->val << " | " << *arrptr << '\n'; | |
1365 new llvm::StoreInst(ve->getValue(), arrptr, p->scopebb()); | |
1366 delete ve; | |
1367 } | |
1368 else { | |
1369 assert(0); | |
1370 } | |
1371 } | |
1372 } | |
1373 | |
1374 e->inplace = true; | |
1375 | |
1376 return e; | |
1377 } | |
1378 | |
1379 ////////////////////////////////////////////////////////////////////////////////////////// | |
1380 | |
1381 elem* IndexExp::toElem(IRState* p) | |
1382 { | |
1383 Logger::print("IndexExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1384 LOG_SCOPE; | |
1385 | |
1386 elem* e = new elem; | |
1387 | |
1388 elem* l = e1->toElem(p); | |
1389 | |
1390 p->arrays.push_back(l->mem); // if $ is used it must be an array so this is fine. | |
1391 elem* r = e2->toElem(p); | |
1392 p->arrays.pop_back(); | |
1393 | |
1394 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1395 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1396 | |
1397 llvm::Value* arrptr = 0; | |
1398 if (e1->type->ty == Tpointer) { | |
1399 arrptr = new llvm::GetElementPtrInst(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
1400 } | |
1401 else if (e1->type->ty == Tsarray) { | |
6 | 1402 arrptr = LLVM_DtoGEP(l->mem, zero, r->getValue(),"tmp",p->scopebb()); |
1 | 1403 } |
1404 else if (e1->type->ty == Tarray) { | |
6 | 1405 arrptr = LLVM_DtoGEP(l->mem,zero,one,"tmp",p->scopebb()); |
1 | 1406 arrptr = new llvm::LoadInst(arrptr,"tmp",p->scopebb()); |
1407 arrptr = new llvm::GetElementPtrInst(arrptr,r->getValue(),"tmp",p->scopebb()); | |
1408 } | |
1409 assert(arrptr); | |
1410 | |
1411 e->mem = arrptr; | |
1412 e->type = elem::VAR; | |
1413 | |
1414 delete l; | |
1415 delete r; | |
1416 | |
1417 return e; | |
1418 } | |
1419 | |
1420 ////////////////////////////////////////////////////////////////////////////////////////// | |
1421 | |
1422 elem* SliceExp::toElem(IRState* p) | |
1423 { | |
1424 Logger::print("SliceExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1425 LOG_SCOPE; | |
1426 | |
1427 elem* v = e1->toElem(p); | |
1428 | |
1429 elem* e = new elem; | |
1430 assert(v->mem); | |
1431 e->type = elem::SLICE; | |
1432 | |
1433 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1434 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1435 | |
1436 // partial slice | |
1437 if (lwr) | |
1438 { | |
1439 assert(upr); | |
1440 p->arrays.push_back(v->mem); | |
1441 elem* lo = lwr->toElem(p); | |
1442 | |
1443 bool lwr_is_zero = false; | |
1444 if (lo->type == elem::CONST) | |
1445 { | |
1446 assert(lo->val); | |
1447 assert(llvm::isa<llvm::ConstantInt>(lo->val)); | |
1448 | |
1449 if (e1->type->ty == Tpointer) { | |
1450 e->mem = v->getValue(); | |
1451 } | |
1452 else if (e1->type->ty == Tarray) { | |
6 | 1453 llvm::Value* tmp = LLVM_DtoGEP(v->mem,zero,one,"tmp",p->scopebb()); |
1 | 1454 e->mem = new llvm::LoadInst(tmp,"tmp",p->scopebb()); |
1455 } | |
1456 else | |
1457 assert(e->mem); | |
1458 | |
1459 llvm::ConstantInt* c = llvm::cast<llvm::ConstantInt>(lo->val); | |
1460 if (!(lwr_is_zero = c->isZero())) { | |
1461 e->mem = new llvm::GetElementPtrInst(e->mem,lo->val,"tmp",p->scopebb()); | |
1462 } | |
1463 } | |
1464 else | |
1465 { | |
6 | 1466 llvm::Value* tmp = LLVM_DtoGEP(v->mem,zero,one,"tmp",p->scopebb()); |
1 | 1467 tmp = new llvm::LoadInst(tmp,"tmp",p->scopebb()); |
1468 e->mem = new llvm::GetElementPtrInst(tmp,lo->getValue(),"tmp",p->scopebb()); | |
1469 } | |
1470 | |
1471 elem* up = upr->toElem(p); | |
1472 p->arrays.pop_back(); | |
1473 | |
1474 if (up->type == elem::CONST) | |
1475 { | |
1476 assert(up->val); | |
1477 assert(llvm::isa<llvm::ConstantInt>(up->val)); | |
1478 if (lwr_is_zero) { | |
1479 e->arg = up->val; | |
1480 } | |
1481 else { | |
1482 if (lo->type == elem::CONST) { | |
1483 llvm::Constant* clo = llvm::cast<llvm::Constant>(lo->val); | |
1484 llvm::Constant* cup = llvm::cast<llvm::Constant>(up->val); | |
1485 e->arg = llvm::ConstantExpr::getSub(cup, clo); | |
1486 } | |
1487 else { | |
1488 e->arg = llvm::BinaryOperator::createSub(up->val, lo->getValue(), "tmp", p->scopebb()); | |
1489 } | |
1490 } | |
1491 } | |
1492 else | |
1493 { | |
1494 if (lwr_is_zero) { | |
1495 e->arg = up->getValue(); | |
1496 } | |
1497 else { | |
1498 e->arg = llvm::BinaryOperator::createSub(up->getValue(), lo->getValue(), "tmp", p->scopebb()); | |
1499 } | |
1500 } | |
1501 | |
1502 delete lo; | |
1503 delete up; | |
1504 } | |
1505 // full slice | |
1506 else | |
1507 { | |
1508 e->mem = v->mem; | |
1509 } | |
1510 | |
1511 delete v; | |
1512 | |
1513 return e; | |
1514 } | |
1515 | |
1516 ////////////////////////////////////////////////////////////////////////////////////////// | |
1517 | |
1518 elem* CmpExp::toElem(IRState* p) | |
1519 { | |
1520 Logger::print("CmpExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1521 LOG_SCOPE; | |
1522 | |
1523 elem* e = new elem; | |
1524 | |
1525 elem* l = e1->toElem(p); | |
1526 elem* r = e2->toElem(p); | |
1527 | |
1528 assert(e1->type == e2->type); | |
1529 | |
1530 Type* t = e1->type; | |
1531 | |
1532 if (t->isintegral()) | |
1533 { | |
1534 llvm::ICmpInst::Predicate cmpop; | |
1535 switch(op) | |
1536 { | |
1537 case TOKlt: | |
1538 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_ULT : llvm::ICmpInst::ICMP_SLT; | |
1539 break; | |
1540 case TOKle: | |
1541 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_ULE : llvm::ICmpInst::ICMP_SLE; | |
1542 break; | |
1543 case TOKgt: | |
1544 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_UGT : llvm::ICmpInst::ICMP_SGT; | |
1545 break; | |
1546 case TOKge: | |
1547 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_UGE : llvm::ICmpInst::ICMP_SGE; | |
1548 break; | |
1549 default: | |
1550 assert(0); | |
1551 } | |
1552 e->val = new llvm::ICmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1553 } | |
1554 else if (t->isfloating()) | |
1555 { | |
1556 llvm::FCmpInst::Predicate cmpop; | |
1557 switch(op) | |
1558 { | |
1559 case TOKlt: | |
1560 cmpop = llvm::FCmpInst::FCMP_OLT;break; | |
1561 case TOKle: | |
1562 cmpop = llvm::FCmpInst::FCMP_OLE;break; | |
1563 case TOKgt: | |
1564 cmpop = llvm::FCmpInst::FCMP_OGT;break; | |
1565 case TOKge: | |
1566 cmpop = llvm::FCmpInst::FCMP_OGE;break; | |
1567 case TOKunord: | |
1568 cmpop = llvm::FCmpInst::FCMP_UNO;break; | |
1569 case TOKule: | |
1570 cmpop = llvm::FCmpInst::FCMP_ULE;break; | |
1571 case TOKul: | |
1572 cmpop = llvm::FCmpInst::FCMP_ULT;break; | |
1573 case TOKuge: | |
1574 cmpop = llvm::FCmpInst::FCMP_UGE;break; | |
1575 case TOKug: | |
1576 cmpop = llvm::FCmpInst::FCMP_UGT;break; | |
1577 case TOKue: | |
1578 cmpop = llvm::FCmpInst::FCMP_UEQ;break; | |
1579 case TOKlg: | |
1580 cmpop = llvm::FCmpInst::FCMP_ONE;break; | |
1581 case TOKleg: | |
1582 cmpop = llvm::FCmpInst::FCMP_ORD;break; | |
1583 | |
1584 default: | |
1585 assert(0); | |
1586 } | |
1587 e->val = new llvm::FCmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1588 } | |
1589 else | |
1590 { | |
1591 assert(0 && "Unsupported CmpExp type"); | |
1592 } | |
1593 | |
1594 delete l; | |
1595 delete r; | |
1596 | |
1597 e->type = elem::VAL; | |
1598 | |
1599 return e; | |
1600 } | |
1601 | |
1602 ////////////////////////////////////////////////////////////////////////////////////////// | |
1603 | |
1604 elem* EqualExp::toElem(IRState* p) | |
1605 { | |
1606 Logger::print("EqualExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1607 LOG_SCOPE; | |
1608 | |
1609 elem* e = new elem; | |
1610 | |
1611 elem* l = e1->toElem(p); | |
1612 elem* r = e2->toElem(p); | |
1613 | |
1614 assert(e1->type == e2->type); | |
1615 | |
1616 Type* t = e1->type; | |
1617 | |
1618 if (t->isintegral() || t->ty == Tpointer) | |
1619 { | |
1620 llvm::ICmpInst::Predicate cmpop; | |
1621 switch(op) | |
1622 { | |
1623 case TOKequal: | |
1624 cmpop = llvm::ICmpInst::ICMP_EQ; | |
1625 break; | |
1626 case TOKnotequal: | |
1627 cmpop = llvm::ICmpInst::ICMP_NE; | |
1628 break; | |
1629 default: | |
1630 assert(0); | |
1631 } | |
1632 e->val = new llvm::ICmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1633 } | |
1634 else if (t->isfloating()) | |
1635 { | |
1636 llvm::FCmpInst::Predicate cmpop; | |
1637 switch(op) | |
1638 { | |
1639 case TOKequal: | |
1640 cmpop = llvm::FCmpInst::FCMP_OEQ; | |
1641 break; | |
1642 case TOKnotequal: | |
1643 cmpop = llvm::FCmpInst::FCMP_UNE; | |
1644 break; | |
1645 default: | |
1646 assert(0); | |
1647 } | |
1648 e->val = new llvm::FCmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1649 } | |
1650 else if (t->ty == Tarray) | |
1651 { | |
1652 // array comparison invokes the typeinfo runtime | |
1653 assert(0); | |
1654 } | |
1655 else | |
1656 { | |
1657 assert(0 && "Unsupported EqualExp type"); | |
1658 } | |
1659 | |
1660 delete l; | |
1661 delete r; | |
1662 | |
1663 e->type = elem::VAL; | |
1664 | |
1665 return e; | |
1666 } | |
1667 | |
1668 ////////////////////////////////////////////////////////////////////////////////////////// | |
1669 | |
1670 elem* PostExp::toElem(IRState* p) | |
1671 { | |
1672 Logger::print("PostExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1673 LOG_SCOPE; | |
1674 | |
1675 elem* l = e1->toElem(p); | |
1676 elem* r = e2->toElem(p); | |
1677 | |
1678 elem* e = new elem; | |
1679 e->mem = l->mem; | |
1680 e->val = l->getValue(); | |
1681 e->type = elem::VAL; | |
1682 | |
1683 llvm::Value* val = e->val; | |
1684 llvm::Value* post = 0; | |
1685 | |
1686 if (e1->type->isintegral()) | |
1687 { | |
1688 assert(e2->type->isintegral()); | |
1689 llvm::Value* one = llvm::ConstantInt::get(val->getType(), 1, !e2->type->isunsigned()); | |
1690 if (op == TOKplusplus) { | |
1691 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb()); | |
1692 } | |
1693 else if (op == TOKminusminus) { | |
1694 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb()); | |
1695 } | |
1696 } | |
1697 else if (e1->type->ty == Tpointer) | |
1698 { | |
1699 assert(e2->type->isintegral()); | |
1700 llvm::Constant* minusone = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)-1,true); | |
1701 llvm::Constant* plusone = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)1,false); | |
1702 llvm::Constant* whichone = (op == TOKplusplus) ? plusone : minusone; | |
1703 post = new llvm::GetElementPtrInst(val, whichone, "tmp", p->scopebb()); | |
1704 } | |
1705 else if (e1->type->isfloating()) | |
1706 { | |
1707 assert(e2->type->isfloating()); | |
1708 llvm::Value* one = llvm::ConstantFP::get(val->getType(), 1.0f); | |
1709 if (op == TOKplusplus) { | |
1710 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb()); | |
1711 } | |
1712 else if (op == TOKminusminus) { | |
1713 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb()); | |
1714 } | |
1715 } | |
1716 else | |
1717 assert(post); | |
1718 | |
1719 //llvm::Value* tostore = l->storeVal ? l->storeVal : l->val; | |
1720 new llvm::StoreInst(post,l->mem,p->scopebb()); | |
1721 | |
1722 delete l; | |
1723 delete r; | |
1724 | |
1725 return e; | |
1726 } | |
1727 | |
1728 ////////////////////////////////////////////////////////////////////////////////////////// | |
1729 | |
1730 elem* NewExp::toElem(IRState* p) | |
1731 { | |
1732 Logger::print("NewExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1733 LOG_SCOPE; | |
1734 | |
1735 assert(!thisexp); | |
1736 assert(!newargs); | |
1737 assert(newtype); | |
1738 //assert(!arguments); | |
1739 //assert(!member); | |
1740 assert(!allocator); | |
1741 | |
1742 elem* e = new elem; | |
1743 | |
1744 const llvm::Type* t = LLVM_DtoType(newtype); | |
1745 | |
1746 if (onstack) { | |
1747 assert(newtype->ty == Tclass); | |
1748 e->mem = new llvm::AllocaInst(t->getContainedType(0),"tmp",p->topallocapoint()); | |
1749 } | |
1750 else { | |
1751 if (newtype->ty == Tclass) { | |
1752 e->mem = new llvm::MallocInst(t->getContainedType(0),"tmp",p->scopebb()); | |
1753 } | |
1754 else if (newtype->ty == Tarray) { | |
1755 t = LLVM_DtoType(newtype->next); | |
1756 assert(arguments); | |
1757 if (arguments->dim == 1) { | |
1758 elem* sz = ((Expression*)arguments->data[0])->toElem(p); | |
1759 llvm::Value* dimval = sz->getValue(); | |
1760 llvm::Value* usedimval = dimval; | |
1761 if (dimval->getType() != llvm::Type::Int32Ty) | |
1762 usedimval = new llvm::TruncInst(dimval, llvm::Type::Int32Ty,"tmp",p->scopebb()); | |
1763 e->mem = new llvm::MallocInst(t,usedimval,"tmp",p->scopebb()); | |
1764 | |
1765 LLVM_DtoSetArray(p->toplval(), dimval, e->mem); | |
1766 delete sz; | |
1767 } | |
1768 else { | |
1769 assert(0); | |
1770 } | |
1771 } | |
1772 else { | |
1773 e->mem = new llvm::MallocInst(t,"tmp",p->scopebb()); | |
1774 } | |
1775 } | |
1776 | |
1777 if (newtype->ty == Tclass) { | |
1778 // first apply the static initializer | |
1779 assert(e->mem); | |
1780 LLVM_DtoInitClass((TypeClass*)newtype, e->mem); | |
1781 | |
1782 // then call constructor | |
1783 if (arguments) { | |
1784 std::vector<llvm::Value*> ctorargs; | |
1785 ctorargs.push_back(e->mem); | |
1786 for (size_t i=0; i<arguments->dim; ++i) | |
1787 { | |
1788 Expression* ex = (Expression*)arguments->data[i]; | |
1789 Logger::println("arg=%s", ex->toChars()); | |
1790 elem* exe = ex->toElem(p); | |
1791 assert(exe->getValue()); | |
1792 ctorargs.push_back(exe->getValue()); | |
1793 delete exe; | |
1794 } | |
1795 assert(member); | |
1796 assert(member->llvmValue); | |
1797 new llvm::CallInst(member->llvmValue, ctorargs.begin(), ctorargs.end(), "", p->scopebb()); | |
1798 } | |
1799 } | |
1800 else if (newtype->ty == Tstruct) { | |
1801 TypeStruct* ts = (TypeStruct*)newtype; | |
1802 if (ts->isZeroInit()) { | |
1803 LLVM_DtoStructZeroInit(ts,e->mem); | |
1804 } | |
1805 else { | |
1806 LLVM_DtoStructCopy(ts,e->mem,ts->llvmInit); | |
1807 } | |
1808 } | |
1809 | |
1810 e->inplace = true; | |
1811 e->type = elem::VAR; | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1812 |
1 | 1813 return e; |
1814 } | |
1815 | |
1816 ////////////////////////////////////////////////////////////////////////////////////////// | |
1817 | |
1818 elem* DeleteExp::toElem(IRState* p) | |
1819 { | |
1820 Logger::print("DeleteExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1821 LOG_SCOPE; | |
1822 | |
1823 //assert(e1->type->ty != Tclass); | |
1824 | |
1825 elem* v = e1->toElem(p); | |
1826 llvm::Value* val = v->getValue(); | |
1827 llvm::Value* ldval = 0; | |
1828 | |
1829 const llvm::Type* t = val->getType(); | |
1830 llvm::Constant* z = llvm::Constant::getNullValue(t); | |
1831 | |
1832 if (e1->type->ty == Tpointer) { | |
1833 ldval = v->getValue(); | |
1834 new llvm::FreeInst(ldval, p->scopebb()); | |
1835 | |
1836 Logger::cout() << *z << '\n'; | |
1837 Logger::cout() << *val << '\n'; | |
1838 new llvm::StoreInst(z, v->mem, p->scopebb()); | |
1839 } | |
1840 else if (e1->type->ty == Tclass) { | |
1841 TypeClass* tc = (TypeClass*)e1->type; | |
1842 LLVM_DtoCallClassDtors(tc, val); | |
1843 | |
1844 if (v->vardecl && !v->vardecl->onstack) { | |
1845 new llvm::FreeInst(val, p->scopebb()); | |
1846 } | |
1847 new llvm::StoreInst(z, v->mem, p->scopebb()); | |
1848 } | |
1849 else if (e1->type->ty == Tarray) { | |
1850 // must be on the heap (correct?) | |
1851 ldval = v->getValue(); | |
1852 | |
1853 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1854 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1855 | |
6 | 1856 llvm::Value* ptr = LLVM_DtoGEP(ldval,zero,one,"tmp",p->scopebb()); |
1 | 1857 ptr = new llvm::LoadInst(ptr,"tmp",p->scopebb()); |
1858 new llvm::FreeInst(ptr, p->scopebb()); | |
1859 LLVM_DtoNullArray(val); | |
1860 } | |
1861 else { | |
1862 assert(0); | |
1863 } | |
1864 | |
1865 delete v; | |
1866 | |
1867 // this expression produces no useful data | |
1868 return 0; | |
1869 } | |
1870 | |
1871 ////////////////////////////////////////////////////////////////////////////////////////// | |
1872 | |
1873 elem* ArrayLengthExp::toElem(IRState* p) | |
1874 { | |
1875 Logger::print("ArrayLengthExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1876 LOG_SCOPE; | |
1877 | |
1878 elem* e = new elem; | |
1879 elem* u = e1->toElem(p); | |
1880 | |
1881 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
6 | 1882 llvm::Value* ptr = LLVM_DtoGEP(u->mem,zero,zero,"tmp",p->scopebb()); |
1 | 1883 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb()); |
1884 e->type = elem::VAL; | |
1885 | |
1886 delete u; | |
1887 | |
1888 return e; | |
1889 } | |
1890 | |
1891 ////////////////////////////////////////////////////////////////////////////////////////// | |
1892 | |
1893 elem* AssertExp::toElem(IRState* p) | |
1894 { | |
1895 Logger::print("AssertExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1896 LOG_SCOPE; | |
1897 | |
1898 elem* e = new elem; | |
1899 elem* u = e1->toElem(p); | |
1900 elem* m = msg ? msg->toElem(p) : 0; | |
1901 | |
1902 std::vector<llvm::Value*> llargs; | |
1903 llargs.resize(3); | |
1904 llargs[0] = LLVM_DtoBoolean(u->getValue()); | |
1905 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false); | |
1906 llargs[2] = m ? m->val : llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
1907 | |
1908 delete m; | |
1909 delete u; | |
1910 | |
1911 //Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n'; | |
1912 | |
1913 llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert"); | |
1914 assert(fn); | |
1915 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb()); | |
1916 call->setCallingConv(llvm::CallingConv::C); | |
1917 | |
1918 return e; | |
1919 } | |
1920 | |
1921 ////////////////////////////////////////////////////////////////////////////////////////// | |
1922 | |
1923 elem* NotExp::toElem(IRState* p) | |
1924 { | |
1925 Logger::print("NotExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1926 LOG_SCOPE; | |
1927 | |
1928 elem* e = new elem; | |
1929 elem* u = e1->toElem(p); | |
1930 | |
1931 llvm::Value* b = LLVM_DtoBoolean(u->getValue()); | |
1932 | |
1933 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, true); | |
1934 e->val = new llvm::ICmpInst(llvm::ICmpInst::ICMP_EQ,b,zero,"tmp",p->scopebb()); | |
1935 e->type = elem::VAL; | |
1936 | |
1937 delete u; | |
1938 | |
1939 return e; | |
1940 } | |
1941 | |
1942 ////////////////////////////////////////////////////////////////////////////////////////// | |
1943 | |
1944 elem* AndAndExp::toElem(IRState* p) | |
1945 { | |
1946 Logger::print("AndAndExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1947 LOG_SCOPE; | |
1948 | |
1949 // allocate a temporary for the final result. failed to come up with a better way :/ | |
1950 llvm::Value* resval = 0; | |
1951 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
1952 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"andandtmp",p->topallocapoint()); | |
1953 | |
1954 elem* e = new elem; | |
1955 elem* u = e1->toElem(p); | |
1956 | |
1957 llvm::BasicBlock* oldend = p->scopeend(); | |
1958 llvm::BasicBlock* andand = new llvm::BasicBlock("andand", gIR->topfunc(), oldend); | |
1959 llvm::BasicBlock* andandend = new llvm::BasicBlock("andandend", gIR->topfunc(), oldend); | |
1960 | |
1961 llvm::Value* ubool = LLVM_DtoBoolean(u->getValue()); | |
1962 new llvm::StoreInst(ubool,resval,p->scopebb()); | |
1963 new llvm::BranchInst(andand,andandend,ubool,p->scopebb()); | |
1964 | |
1965 p->scope() = IRScope(andand, andandend); | |
1966 elem* v = e2->toElem(p); | |
1967 | |
1968 llvm::Value* vbool = LLVM_DtoBoolean(v->getValue()); | |
1969 llvm::Value* uandvbool = llvm::BinaryOperator::create(llvm::BinaryOperator::And, ubool, vbool,"tmp",p->scopebb()); | |
1970 new llvm::StoreInst(uandvbool,resval,p->scopebb()); | |
1971 new llvm::BranchInst(andandend,p->scopebb()); | |
1972 | |
1973 delete u; | |
1974 delete v; | |
1975 | |
1976 p->scope() = IRScope(andandend, oldend); | |
1977 | |
1978 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
1979 e->type = elem::VAL; | |
1980 | |
1981 return e; | |
1982 } | |
1983 | |
1984 ////////////////////////////////////////////////////////////////////////////////////////// | |
1985 | |
1986 elem* OrOrExp::toElem(IRState* p) | |
1987 { | |
1988 Logger::print("OrOrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1989 LOG_SCOPE; | |
1990 | |
1991 // allocate a temporary for the final result. failed to come up with a better way :/ | |
1992 llvm::Value* resval = 0; | |
1993 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
1994 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"orortmp",p->topallocapoint()); | |
1995 | |
1996 elem* e = new elem; | |
1997 elem* u = e1->toElem(p); | |
1998 | |
1999 llvm::BasicBlock* oldend = p->scopeend(); | |
2000 llvm::BasicBlock* oror = new llvm::BasicBlock("oror", gIR->topfunc(), oldend); | |
2001 llvm::BasicBlock* ororend = new llvm::BasicBlock("ororend", gIR->topfunc(), oldend); | |
2002 | |
2003 llvm::Value* ubool = LLVM_DtoBoolean(u->getValue()); | |
2004 new llvm::StoreInst(ubool,resval,p->scopebb()); | |
2005 new llvm::BranchInst(ororend,oror,ubool,p->scopebb()); | |
2006 | |
2007 p->scope() = IRScope(oror, ororend); | |
2008 elem* v = e2->toElem(p); | |
2009 | |
2010 llvm::Value* vbool = LLVM_DtoBoolean(v->getValue()); | |
2011 new llvm::StoreInst(vbool,resval,p->scopebb()); | |
2012 new llvm::BranchInst(ororend,p->scopebb()); | |
2013 | |
2014 delete u; | |
2015 delete v; | |
2016 | |
2017 p->scope() = IRScope(ororend, oldend); | |
2018 | |
2019 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
2020 e->type = elem::VAL; | |
2021 | |
2022 return e; | |
2023 } | |
2024 | |
2025 ////////////////////////////////////////////////////////////////////////////////////////// | |
2026 | |
2027 #define BinBitExp(X,Y) \ | |
2028 elem* X##Exp::toElem(IRState* p) \ | |
2029 { \ | |
2030 Logger::print("%sExp::toElem: %s | %s\n", #X, toChars(), type->toChars()); \ | |
2031 LOG_SCOPE; \ | |
2032 elem* e = new elem; \ | |
2033 elem* u = e1->toElem(p); \ | |
2034 elem* v = e2->toElem(p); \ | |
2035 e->val = llvm::BinaryOperator::create(llvm::Instruction::Y, u->getValue(), v->getValue(), "tmp", p->scopebb()); \ | |
2036 e->type = elem::VAL; \ | |
2037 delete u; \ | |
2038 delete v; \ | |
2039 return e; \ | |
2040 } \ | |
2041 \ | |
2042 elem* X##AssignExp::toElem(IRState* p) \ | |
2043 { \ | |
2044 Logger::print("%sAssignExp::toElem: %s | %s\n", #X, toChars(), type->toChars()); \ | |
2045 LOG_SCOPE; \ | |
2046 elem* u = e1->toElem(p); \ | |
2047 elem* v = e2->toElem(p); \ | |
2048 llvm::Value* tmp = llvm::BinaryOperator::create(llvm::Instruction::Y, u->getValue(), v->getValue(), "tmp", p->scopebb()); \ | |
2049 Logger::cout() << *tmp << '|' << *u->mem << '\n'; \ | |
2050 new llvm::StoreInst(LLVM_DtoPointedType(u->mem, tmp), u->mem, p->scopebb()); \ | |
2051 delete u; \ | |
2052 delete v; \ | |
2053 elem* e = new elem; \ | |
2054 e->mem = u->mem; \ | |
2055 e->type = elem::VAR; \ | |
2056 return e; \ | |
2057 } | |
2058 | |
2059 BinBitExp(And,And); | |
2060 BinBitExp(Or,Or); | |
2061 BinBitExp(Xor,Xor); | |
2062 BinBitExp(Shl,Shl); | |
2063 BinBitExp(Shr,AShr); | |
2064 BinBitExp(Ushr,LShr); | |
2065 | |
2066 ////////////////////////////////////////////////////////////////////////////////////////// | |
2067 | |
2068 elem* HaltExp::toElem(IRState* p) | |
2069 { | |
2070 Logger::print("HaltExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2071 LOG_SCOPE; | |
2072 | |
2073 std::vector<llvm::Value*> llargs; | |
2074 llargs.resize(3); | |
2075 llargs[0] = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, false); | |
2076 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false); | |
2077 llargs[2] = llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
2078 | |
2079 //Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n'; | |
2080 | |
2081 llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert"); | |
2082 assert(fn); | |
2083 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb()); | |
2084 call->setCallingConv(llvm::CallingConv::C); | |
2085 | |
2086 //new llvm::UnreachableInst(p->scopebb()); | |
2087 | |
2088 return 0; | |
2089 } | |
2090 | |
2091 ////////////////////////////////////////////////////////////////////////////////////////// | |
2092 | |
2093 elem* DelegateExp::toElem(IRState* p) | |
2094 { | |
2095 Logger::print("DelegateExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2096 LOG_SCOPE; | |
2097 | |
2098 elem* e = new elem; | |
2099 elem* u = e1->toElem(p); | |
2100 | |
2101 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
2102 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
2103 | |
2104 const llvm::Type* int8ptrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
2105 | |
2106 llvm::Value* lval = p->toplval(); | |
2107 | |
6 | 2108 llvm::Value* context = LLVM_DtoGEP(lval,zero,zero,"tmp",p->scopebb()); |
1 | 2109 llvm::Value* castcontext = new llvm::BitCastInst(u->getValue(),int8ptrty,"tmp",p->scopebb()); |
2110 new llvm::StoreInst(castcontext, context, p->scopebb()); | |
2111 | |
6 | 2112 llvm::Value* fptr = LLVM_DtoGEP(lval,zero,one,"tmp",p->scopebb()); |
1 | 2113 |
2114 assert(func->llvmValue); | |
2115 llvm::Value* castfptr = new llvm::BitCastInst(func->llvmValue,fptr->getType()->getContainedType(0),"tmp",p->scopebb()); | |
2116 new llvm::StoreInst(castfptr, fptr, p->scopebb()); | |
2117 | |
2118 e->inplace = true; | |
2119 | |
2120 delete u; | |
2121 return e; | |
2122 } | |
2123 | |
2124 ////////////////////////////////////////////////////////////////////////////////////////// | |
2125 | |
2126 elem* IdentityExp::toElem(IRState* p) | |
2127 { | |
2128 Logger::print("IdentityExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2129 LOG_SCOPE; | |
2130 | |
2131 elem* u = e1->toElem(p); | |
2132 elem* v = e2->toElem(p); | |
2133 | |
2134 elem* e = new elem; | |
2135 | |
2136 llvm::Value* l = u->getValue(); | |
2137 llvm::Value* r = 0; | |
2138 if (v->type == elem::NUL) | |
2139 r = llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(l->getType())); | |
2140 else | |
2141 r = v->getValue(); | |
2142 | |
2143 llvm::ICmpInst::Predicate pred = (op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE; | |
2144 e->val = new llvm::ICmpInst(pred, l, r, "tmp", p->scopebb()); | |
2145 e->type = elem::VAL; | |
2146 | |
2147 delete u; | |
2148 delete v; | |
2149 | |
2150 return e; | |
2151 } | |
2152 | |
2153 ////////////////////////////////////////////////////////////////////////////////////////// | |
2154 | |
2155 elem* CommaExp::toElem(IRState* p) | |
2156 { | |
2157 Logger::print("CommaExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2158 LOG_SCOPE; | |
2159 | |
2160 elem* u = e1->toElem(p); | |
2161 elem* v = e2->toElem(p); | |
2162 delete u; | |
2163 return v; | |
2164 } | |
2165 | |
2166 ////////////////////////////////////////////////////////////////////////////////////////// | |
2167 | |
2168 elem* CondExp::toElem(IRState* p) | |
2169 { | |
2170 Logger::print("CondExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2171 LOG_SCOPE; | |
2172 | |
2173 const llvm::Type* resty = LLVM_DtoType(type); | |
2174 | |
2175 // allocate a temporary for the final result. failed to come up with a better way :/ | |
2176 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
2177 llvm::Value* resval = new llvm::AllocaInst(resty,"condtmp",p->topallocapoint()); | |
2178 | |
2179 llvm::BasicBlock* oldend = p->scopeend(); | |
2180 llvm::BasicBlock* condtrue = new llvm::BasicBlock("condtrue", gIR->topfunc(), oldend); | |
2181 llvm::BasicBlock* condfalse = new llvm::BasicBlock("condfalse", gIR->topfunc(), oldend); | |
2182 llvm::BasicBlock* condend = new llvm::BasicBlock("condend", gIR->topfunc(), oldend); | |
2183 | |
2184 elem* c = econd->toElem(p); | |
2185 llvm::Value* cond_val = LLVM_DtoBoolean(c->getValue()); | |
2186 delete c; | |
2187 new llvm::BranchInst(condtrue,condfalse,cond_val,p->scopebb()); | |
2188 | |
2189 p->scope() = IRScope(condtrue, condfalse); | |
2190 elem* u = e1->toElem(p); | |
2191 Logger::cout() << *u->val << '|' << *resval << '\n'; \ | |
2192 new llvm::StoreInst(u->getValue(),resval,p->scopebb()); | |
2193 new llvm::BranchInst(condend,p->scopebb()); | |
2194 delete u; | |
2195 | |
2196 p->scope() = IRScope(condfalse, condend); | |
2197 elem* v = e2->toElem(p); | |
2198 new llvm::StoreInst(v->getValue(),resval,p->scopebb()); | |
2199 new llvm::BranchInst(condend,p->scopebb()); | |
2200 delete v; | |
2201 | |
2202 p->scope() = IRScope(condend, oldend); | |
2203 | |
2204 elem* e = new elem; | |
2205 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
2206 e->type = elem::VAL; | |
2207 return e; | |
2208 } | |
2209 | |
2210 ////////////////////////////////////////////////////////////////////////////////////////// | |
2211 | |
2212 elem* ComExp::toElem(IRState* p) | |
2213 { | |
2214 Logger::print("ComExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2215 LOG_SCOPE; | |
2216 | |
2217 elem* e = new elem; | |
2218 elem* u = e1->toElem(p); | |
2219 | |
2220 llvm::Value* value = u->getValue(); | |
2221 llvm::Value* minusone = llvm::ConstantInt::get(value->getType(), -1, true); | |
2222 e->val = llvm::BinaryOperator::create(llvm::Instruction::Xor, value, minusone, "tmp", p->scopebb()); | |
2223 | |
2224 delete u; | |
2225 | |
2226 e->type = elem::VAL; | |
2227 | |
2228 return e; | |
2229 } | |
2230 | |
2231 ////////////////////////////////////////////////////////////////////////////////////////// | |
2232 | |
2233 #define STUB(x) elem *x::toElem(IRState * p) {error("Exp type "#x" not implemented: %s", toChars()); fatal(); return 0; } | |
2234 //STUB(IdentityExp); | |
2235 //STUB(CondExp); | |
2236 //STUB(EqualExp); | |
2237 STUB(InExp); | |
2238 //STUB(CmpExp); | |
2239 //STUB(AndAndExp); | |
2240 //STUB(OrOrExp); | |
2241 //STUB(AndExp); | |
2242 //STUB(AndAssignExp); | |
2243 //STUB(OrExp); | |
2244 //STUB(OrAssignExp); | |
2245 //STUB(XorExp); | |
2246 //STUB(XorAssignExp); | |
2247 //STUB(ShrExp); | |
2248 //STUB(ShrAssignExp); | |
2249 //STUB(ShlExp); | |
2250 //STUB(ShlAssignExp); | |
2251 //STUB(UshrExp); | |
2252 //STUB(UshrAssignExp); | |
2253 //STUB(DivExp); | |
2254 //STUB(DivAssignExp); | |
2255 //STUB(MulExp); | |
2256 //STUB(MulAssignExp); | |
2257 //STUB(ModExp); | |
2258 //STUB(ModAssignExp); | |
2259 STUB(CatExp); | |
2260 STUB(CatAssignExp); | |
2261 //STUB(AddExp); | |
2262 //STUB(AddAssignExp); | |
2263 STUB(Expression); | |
2264 //STUB(MinExp); | |
2265 //STUB(MinAssignExp); | |
2266 //STUB(PostExp); | |
2267 //STUB(NullExp); | |
2268 //STUB(ThisExp); | |
2269 //STUB(CallExp); | |
2270 STUB(DotTypeExp); | |
2271 STUB(TypeDotIdExp); | |
2272 //STUB(DotVarExp); | |
2273 //STUB(AssertExp); | |
2274 STUB(FuncExp); | |
2275 //STUB(DelegateExp); | |
2276 //STUB(VarExp); | |
2277 //STUB(DeclarationExp); | |
2278 //STUB(NewExp); | |
2279 //STUB(SymOffExp); | |
2280 STUB(ScopeExp); | |
2281 //STUB(AssignExp); | |
2282 | |
2283 STUB(TypeExp); | |
2284 //STUB(RealExp); | |
2285 STUB(ComplexExp); | |
2286 //STUB(StringExp); | |
2287 //STUB(IntegerExp); | |
2288 STUB(BoolExp); | |
2289 | |
2290 //STUB(NotExp); | |
2291 //STUB(ComExp); | |
2292 STUB(NegExp); | |
2293 //STUB(PtrExp); | |
2294 //STUB(AddrExp); | |
2295 //STUB(SliceExp); | |
2296 //STUB(CastExp); | |
2297 //STUB(DeleteExp); | |
2298 //STUB(IndexExp); | |
2299 //STUB(CommaExp); | |
2300 //STUB(ArrayLengthExp); | |
2301 //STUB(HaltExp); | |
2302 STUB(RemoveExp); | |
2303 STUB(ArrayLiteralExp); | |
2304 STUB(AssocArrayLiteralExp); | |
2305 //STUB(StructLiteralExp); | |
2306 | |
2307 unsigned Type::totym() { return 0; } | |
2308 | |
2309 type * | |
2310 Type::toCtype() { | |
2311 return 0; | |
2312 } | |
2313 | |
2314 type * Type::toCParamtype() | |
2315 { | |
2316 return 0; | |
2317 } | |
2318 Symbol * Type::toSymbol() | |
2319 { | |
2320 return 0; | |
2321 } | |
2322 | |
2323 type * | |
2324 TypeTypedef::toCtype() | |
2325 { | |
2326 return 0; | |
2327 } | |
2328 | |
2329 type * | |
2330 TypeTypedef::toCParamtype() | |
2331 { | |
2332 return 0; | |
2333 } | |
2334 | |
2335 void | |
2336 TypedefDeclaration::toDebug() | |
2337 { | |
2338 } | |
2339 | |
2340 | |
2341 type * | |
2342 TypeEnum::toCtype() | |
2343 { | |
2344 return 0; | |
2345 } | |
2346 | |
2347 type * | |
2348 TypeStruct::toCtype() | |
2349 { | |
2350 return 0; | |
2351 } | |
2352 | |
2353 void | |
2354 StructDeclaration::toDebug() | |
2355 { | |
2356 } | |
2357 | |
2358 Symbol * TypeClass::toSymbol() | |
2359 { | |
2360 return 0; | |
2361 } | |
2362 | |
2363 unsigned TypeFunction::totym() | |
2364 { | |
2365 return 0; | |
2366 } | |
2367 | |
2368 type * | |
2369 TypeFunction::toCtype() | |
2370 { | |
2371 return 0; | |
2372 } | |
2373 | |
2374 type * | |
2375 TypeSArray::toCtype() | |
2376 { | |
2377 return 0; | |
2378 } | |
2379 | |
2380 type *TypeSArray::toCParamtype() { return 0; } | |
2381 | |
2382 type * | |
2383 TypeDArray::toCtype() | |
2384 { | |
2385 return 0; | |
2386 } | |
2387 | |
2388 type * | |
2389 TypeAArray::toCtype() | |
2390 { | |
2391 return 0; | |
2392 } | |
2393 | |
2394 type * | |
2395 TypePointer::toCtype() | |
2396 { | |
2397 return 0; | |
2398 } | |
2399 | |
2400 type * | |
2401 TypeDelegate::toCtype() | |
2402 { | |
2403 return 0; | |
2404 } | |
2405 | |
2406 type * | |
2407 TypeClass::toCtype() | |
2408 { | |
2409 return 0; | |
2410 } | |
2411 | |
2412 void | |
2413 ClassDeclaration::toDebug() | |
2414 { | |
2415 } | |
2416 | |
14
0e86428ee567
[svn r18] * Initial support for switch statements - No string switches yet.
lindquist
parents:
11
diff
changeset
|
2417 ////////////////////////////////////////////////////////////////////////////// |
1 | 2418 |
2419 void | |
2420 EnumDeclaration::toDebug() | |
2421 { | |
2422 | |
2423 } | |
2424 | |
2425 int | |
2426 Dsymbol::cvMember(unsigned char*) | |
2427 { | |
2428 return 0; | |
2429 } | |
2430 int | |
2431 EnumDeclaration::cvMember(unsigned char*) | |
2432 { | |
2433 return 0; | |
2434 } | |
2435 int | |
2436 FuncDeclaration::cvMember(unsigned char*) | |
2437 { | |
2438 return 0; | |
2439 } | |
2440 int | |
2441 VarDeclaration::cvMember(unsigned char*) | |
2442 { | |
2443 return 0; | |
2444 } | |
2445 int | |
2446 TypedefDeclaration::cvMember(unsigned char*) | |
2447 { | |
2448 return 0; | |
2449 } | |
2450 | |
2451 void obj_includelib(char*){} | |
2452 | |
2453 AsmStatement::AsmStatement(Loc loc, Token *tokens) : | |
2454 Statement(loc) | |
2455 { | |
2456 } | |
2457 Statement *AsmStatement::syntaxCopy() { | |
2458 return 0; | |
2459 } | |
2460 | |
2461 Statement *AsmStatement::semantic(Scope *sc) | |
2462 { | |
2463 return Statement::semantic(sc); | |
2464 } | |
2465 | |
2466 | |
2467 void AsmStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2468 { | |
2469 Statement::toCBuffer(buf, hgs); | |
2470 } | |
2471 | |
2472 int AsmStatement::comeFrom() | |
2473 { | |
2474 return FALSE; | |
2475 } | |
2476 | |
2477 void | |
2478 backend_init() | |
2479 { | |
2480 //LLVM_D_InitRuntime(); | |
2481 // lazily loaded | |
2482 } | |
2483 | |
2484 void | |
2485 backend_term() | |
2486 { | |
2487 LLVM_D_FreeRuntime(); | |
2488 } |