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