Mercurial > projects > ldc
annotate gen/toir.c @ 32:a86fe7496b58 trunk
[svn r36] * Fixed a bug where passing a regular argument to a ref argument did not allocate storage
author | lindquist |
---|---|
date | Thu, 04 Oct 2007 18:24:05 +0200 |
parents | 881158a93592 |
children | 4648206ca213 |
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; |
32
a86fe7496b58
[svn r36] * Fixed a bug where passing a regular argument to a ref argument did not allocate storage
lindquist
parents:
30
diff
changeset
|
989 if (llargs[j]->getType() != llfnty->getParamType(j)) |
a86fe7496b58
[svn r36] * Fixed a bug where passing a regular argument to a ref argument did not allocate storage
lindquist
parents:
30
diff
changeset
|
990 { |
a86fe7496b58
[svn r36] * Fixed a bug where passing a regular argument to a ref argument did not allocate storage
lindquist
parents:
30
diff
changeset
|
991 assert(llargs[j]->getType() == llfnty->getParamType(j)->getContainedType(0)); |
a86fe7496b58
[svn r36] * Fixed a bug where passing a regular argument to a ref argument did not allocate storage
lindquist
parents:
30
diff
changeset
|
992 LLVM_DtoGiveArgumentStorage(arg); |
a86fe7496b58
[svn r36] * Fixed a bug where passing a regular argument to a ref argument did not allocate storage
lindquist
parents:
30
diff
changeset
|
993 new llvm::StoreInst(llargs[j], arg->mem, p->scopebb()); |
a86fe7496b58
[svn r36] * Fixed a bug where passing a regular argument to a ref argument did not allocate storage
lindquist
parents:
30
diff
changeset
|
994 llargs[j] = arg->mem; |
a86fe7496b58
[svn r36] * Fixed a bug where passing a regular argument to a ref argument did not allocate storage
lindquist
parents:
30
diff
changeset
|
995 } |
1 | 996 assert(llargs[j] != 0); |
997 } | |
998 | |
999 delete arg; | |
1000 } | |
1001 | |
1002 // void returns cannot not be named | |
1003 const char* varname = ""; | |
1004 if (llfnty->getReturnType() != llvm::Type::VoidTy) | |
1005 varname = "tmp"; | |
1006 | |
1007 Logger::println("%d params passed", n); | |
1008 for (int i=0; i<n; ++i) | |
1009 { | |
1010 Logger::cout() << *llargs[i] << '\n'; | |
1011 } | |
1012 | |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
1013 Logger::cout() << "Calling: " << *funcval->getType() << '\n'; |
1 | 1014 |
1015 // call the function | |
1016 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
|
1017 if (retinptr) |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
26
diff
changeset
|
1018 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
|
1019 else |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
26
diff
changeset
|
1020 e->val = call; |
1 | 1021 |
1022 // set calling convention | |
1023 if ((fn->funcdecl && (fn->funcdecl->llvmInternal != LLVMintrinsic)) || delegateCall) | |
1024 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
|
1025 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
|
1026 call->setCallingConv(fn->callconv); |
1 | 1027 |
1028 delete fn; | |
1029 return e; | |
1030 } | |
1031 | |
1032 ////////////////////////////////////////////////////////////////////////////////////////// | |
1033 | |
1034 elem* CastExp::toElem(IRState* p) | |
1035 { | |
1036 Logger::print("CastExp::toElem: %s\n", toChars()); | |
1037 LOG_SCOPE; | |
1038 elem* e = new elem; | |
1039 elem* u = e1->toElem(p); | |
1040 const llvm::Type* totype = LLVM_DtoType(to); | |
1041 Type* from = e1->type; | |
1042 int lsz = from->size(); | |
1043 int rsz = to->size(); | |
1044 | |
1045 // this makes sure the strange lvalue casts don't screw things up | |
1046 e->mem = u->mem; | |
1047 | |
1048 if (from->isintegral()) { | |
1049 if (to->isintegral()) { | |
1050 if (lsz < rsz) { | |
1051 Logger::cout() << *totype << '\n'; | |
1052 if (from->isunsigned() || from->ty == Tbool) { | |
1053 e->val = new llvm::ZExtInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1054 } else { | |
1055 e->val = new llvm::SExtInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1056 } | |
1057 } | |
1058 else if (lsz > rsz) { | |
1059 e->val = new llvm::TruncInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1060 } | |
1061 else { | |
1062 e->val = new llvm::BitCastInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1063 } | |
1064 } | |
1065 else if (to->isfloating()) { | |
1066 if (from->isunsigned()) { | |
1067 e->val = new llvm::UIToFPInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1068 } | |
1069 else { | |
1070 e->val = new llvm::SIToFPInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1071 } | |
1072 } | |
1073 else { | |
1074 assert(0); | |
1075 } | |
1076 //e->storeVal = u->storeVal ? u->storeVal : u->val; | |
1077 e->type = elem::VAL; | |
1078 } | |
1079 else if (from->isfloating()) { | |
1080 if (to->isfloating()) { | |
1081 if ((from->ty == Tfloat80 || from->ty == Tfloat64) && (to->ty == Tfloat80 || to->ty == Tfloat64)) { | |
1082 e->val = u->getValue(); | |
1083 } | |
1084 else if (lsz < rsz) { | |
1085 e->val = new llvm::FPExtInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1086 } | |
1087 else if (lsz > rsz) { | |
1088 e->val = new llvm::FPTruncInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1089 } | |
1090 else { | |
1091 assert(0); | |
1092 } | |
1093 } | |
1094 else if (to->isintegral()) { | |
1095 if (to->isunsigned()) { | |
1096 e->val = new llvm::FPToUIInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1097 } | |
1098 else { | |
1099 e->val = new llvm::FPToSIInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1100 } | |
1101 } | |
1102 else { | |
1103 assert(0); | |
1104 } | |
1105 e->type = elem::VAL; | |
1106 } | |
1107 else if (from->ty == Tclass) { | |
1108 //assert(to->ty == Tclass); | |
1109 e->val = new llvm::BitCastInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1110 e->type = elem::VAL; | |
1111 } | |
1112 else if (from->ty == Tarray || from->ty == Tsarray) { | |
1113 Logger::cout() << "from array or sarray" << '\n'; | |
1114 if (to->ty == Tpointer) { | |
1115 Logger::cout() << "to pointer" << '\n'; | |
1116 assert(from->next == to->next); | |
1117 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1118 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
6 | 1119 llvm::Value* ptr = LLVM_DtoGEP(u->getValue(),zero,one,"tmp",p->scopebb()); |
1 | 1120 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb()); |
1121 e->type = elem::VAL; | |
1122 } | |
1123 else if (to->ty == Tarray) { | |
1124 Logger::cout() << "to array" << '\n'; | |
1125 assert(from->next->size() == to->next->size()); | |
1126 const llvm::Type* ptrty = LLVM_DtoType(to->next); | |
1127 if (ptrty == llvm::Type::VoidTy) | |
1128 ptrty = llvm::Type::Int8Ty; | |
1129 ptrty = llvm::PointerType::get(ptrty); | |
1130 | |
1131 if (u->type == elem::SLICE) { | |
1132 e->mem = new llvm::BitCastInst(u->mem, ptrty, "tmp", p->scopebb()); | |
1133 e->arg = u->arg; | |
1134 } | |
1135 else { | |
1136 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
|
1137 if (from->ty == Tsarray) { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1138 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
|
1139 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
|
1140 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
|
1141 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
|
1142 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
|
1143 } |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1144 else { |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1145 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
|
1146 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); |
6 | 1147 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
|
1148 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
|
1149 |
6 | 1150 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
|
1151 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
|
1152 //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
|
1153 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
|
1154 } |
1 | 1155 } |
1156 e->type = elem::SLICE; | |
1157 } | |
1158 else if (to->ty == Tsarray) { | |
1159 Logger::cout() << "to sarray" << '\n'; | |
1160 assert(0); | |
1161 } | |
1162 else { | |
1163 assert(0); | |
1164 } | |
1165 } | |
1166 else if (from->ty == Tpointer) { | |
1167 if (to->ty == Tpointer || to->ty == Tclass) { | |
1168 llvm::Value* src = u->getValue(); | |
1169 //Logger::cout() << *src << '|' << *totype << '\n'; | |
1170 e->val = new llvm::BitCastInst(src, totype, "tmp", p->scopebb()); | |
1171 } | |
1172 else if (to->isintegral()) { | |
1173 e->val = new llvm::PtrToIntInst(u->getValue(), totype, "tmp", p->scopebb()); | |
1174 } | |
1175 else | |
1176 assert(0); | |
1177 e->type = elem::VAL; | |
1178 } | |
1179 else { | |
1180 assert(0); | |
1181 } | |
1182 delete u; | |
1183 return e; | |
1184 } | |
1185 | |
1186 ////////////////////////////////////////////////////////////////////////////////////////// | |
1187 | |
1188 elem* SymOffExp::toElem(IRState* p) | |
1189 { | |
1190 Logger::print("SymOffExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1191 LOG_SCOPE; | |
1192 elem* e = 0; | |
1193 if (VarDeclaration* vd = var->isVarDeclaration()) | |
1194 { | |
1195 Logger::println("VarDeclaration"); | |
23 | 1196 assert(vd->llvmValue); |
1 | 1197 if (vd->type->ty == Tstruct && !(type->ty == Tpointer && type->next == vd->type)) { |
1198 TypeStruct* vdt = (TypeStruct*)vd->type; | |
1199 e = new elem; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1200 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
|
1201 vdt->sym->offsetToIndex(type->next, offset, dst); |
1 | 1202 llvm::Value* ptr = vd->llvmValue; |
1203 assert(ptr); | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1204 e->mem = LLVM_DtoGEP(ptr,dst,"tmp",p->scopebb()); |
1 | 1205 e->type = elem::VAL; |
1206 e->field = true; | |
1207 } | |
1208 else if (vd->type->ty == Tsarray) { | |
1209 /*e = new elem; | |
1210 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1211 e->val = new llvm::GetElementPtrInst(vd->llvmValue,idx0,idx0,"tmp",p->scopebb());*/ | |
1212 e = new elem; | |
1213 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1214 //llvm::Value* idx1 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
6 | 1215 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
|
1216 e->arg = vd->llvmValue; |
1 | 1217 e->type = elem::VAL; |
1218 } | |
1219 else if (offset == 0) { | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1220 /*if (!vd->llvmValue) |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1221 vd->toObjFile();*/ |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
1222 assert(vd->llvmValue); |
1 | 1223 e = new elem; |
1224 e->mem = vd->llvmValue; | |
1225 //e->vardecl = vd; | |
1226 e->type = elem::VAL; | |
1227 } | |
1228 else { | |
1229 assert(0); | |
1230 } | |
1231 } | |
1232 else if (FuncDeclaration* fd = var->isFuncDeclaration()) | |
1233 { | |
1234 Logger::println("FuncDeclaration"); | |
1235 e = new elem; | |
1236 if (fd->llvmValue == 0) | |
1237 fd->toObjFile(); | |
1238 e->val = fd->llvmValue; | |
1239 //e->aspointer = true; | |
1240 e->type = elem::FUNC; | |
1241 } | |
1242 assert(e != 0); | |
1243 assert(e->type != elem::NONE); | |
1244 return e; | |
1245 } | |
1246 | |
1247 ////////////////////////////////////////////////////////////////////////////////////////// | |
1248 | |
1249 elem* PtrExp::toElem(IRState* p) | |
1250 { | |
1251 Logger::print("PtrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1252 LOG_SCOPE; | |
1253 elem* e = new elem; | |
1254 elem* a = e1->toElem(p); | |
1255 | |
1256 if (a->mem) | |
1257 Logger::cout() << "mem: " << *a->mem << '\n'; | |
1258 if (a->val) | |
1259 Logger::cout() << "val: " << *a->val << '\n'; | |
1260 | |
1261 if (a->field) | |
1262 e->mem = a->mem; | |
1263 else | |
1264 e->mem = a->getValue(); | |
1265 e->type = elem::VAR; | |
1266 | |
1267 delete a; | |
1268 return e; | |
1269 } | |
1270 | |
1271 ////////////////////////////////////////////////////////////////////////////////////////// | |
1272 | |
1273 elem* DotVarExp::toElem(IRState* p) | |
1274 { | |
1275 Logger::print("DotVarExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1276 LOG_SCOPE; | |
1277 elem* e = new elem; | |
1278 | |
1279 elem* l = e1->toElem(p); | |
1280 | |
1281 Logger::print("e1->type=%s\n", e1->type->toChars()); | |
1282 | |
1283 if (VarDeclaration* vd = var->isVarDeclaration()) { | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1284 std::vector<unsigned> vdoffsets(1,0); |
1 | 1285 llvm::Value* src = 0; |
1286 if (e1->type->ty == Tpointer) { | |
1287 assert(e1->type->next->ty == Tstruct); | |
1288 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
|
1289 ts->sym->offsetToIndex(vd->type, vd->offset, vdoffsets); |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1290 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
|
1291 src = l->val ? l->val : l->mem; |
1 | 1292 } |
1293 else if (e1->type->ty == Tclass) { | |
1294 TypeClass* tc = (TypeClass*)e1->type; | |
1295 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
|
1296 tc->sym->offsetToIndex(vd->type, vd->offset, vdoffsets); |
1 | 1297 src = l->getValue(); |
1298 } | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1299 assert(vdoffsets.size() != 1); |
1 | 1300 assert(src != 0); |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
1301 llvm::Value* arrptr = LLVM_DtoGEP(src,vdoffsets,"tmp",p->scopebb()); |
1 | 1302 e->mem = arrptr; |
1303 Logger::cout() << "mem: " << *e->mem << '\n'; | |
1304 e->type = elem::VAR; | |
1305 } | |
1306 else if (FuncDeclaration* fdecl = var->isFuncDeclaration()) | |
1307 { | |
1308 if (fdecl->llvmValue == 0) | |
1309 { | |
1310 fdecl->toObjFile(); | |
1311 } | |
1312 | |
1313 llvm::Value* funcval = fdecl->llvmValue; | |
1314 e->arg = l->getValue(); | |
1315 | |
1316 // virtual call | |
1317 if (fdecl->isVirtual()) { | |
1318 assert(fdecl->vtblIndex > 0); | |
1319 assert(e1->type->ty == Tclass); | |
1320 | |
1321 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1322 llvm::Value* vtblidx = llvm::ConstantInt::get(llvm::Type::Int32Ty, (size_t)fdecl->vtblIndex, false); | |
6 | 1323 funcval = LLVM_DtoGEP(e->arg, zero, zero, "tmp", p->scopebb()); |
1 | 1324 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); |
6 | 1325 funcval = LLVM_DtoGEP(funcval, zero, vtblidx, "tmp", p->scopebb()); |
1 | 1326 funcval = new llvm::LoadInst(funcval,"tmp",p->scopebb()); |
9 | 1327 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
|
1328 e->callconv = LLVM_DtoCallingConv(fdecl->linkage); |
1 | 1329 } |
1330 e->val = funcval; | |
1331 e->type = elem::VAL; | |
1332 } | |
1333 else { | |
1334 printf("unknown: %s\n", var->toChars()); | |
1335 assert(0); | |
1336 } | |
1337 | |
1338 delete l; | |
1339 | |
1340 return e; | |
1341 } | |
1342 | |
1343 ////////////////////////////////////////////////////////////////////////////////////////// | |
1344 | |
1345 elem* ThisExp::toElem(IRState* p) | |
1346 { | |
1347 Logger::print("ThisExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1348 LOG_SCOPE; | |
1349 elem* e = new elem; | |
1350 | |
1351 if (VarDeclaration* vd = var->isVarDeclaration()) { | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1352 /*assert(vd->llvmValue == 0); |
1 | 1353 |
1354 llvm::Function* fn = p->topfunc(); | |
1355 assert(fn); | |
1356 | |
1357 TypeFunction* tf = p->topfunctype(); | |
1358 assert(tf); | |
1359 | |
1360 llvm::Value* v = 0; | |
1361 if (tf->llvmRetInPtr) | |
1362 v = ++fn->arg_begin(); | |
1363 else | |
1364 v = fn->arg_begin(); | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1365 assert(v);*/ |
1 | 1366 |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1367 llvm::Value* v = p->funcdecls.back()->llvmThisVar; |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1368 if (llvm::isa<llvm::AllocaInst>(v)) |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1369 v = new llvm::LoadInst(v, "tmp", p->scopebb()); |
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1370 e->mem = v; |
1 | 1371 e->type = elem::VAL; |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1372 e->isthis = true; |
1 | 1373 } |
1374 else { | |
1375 assert(0); | |
1376 } | |
1377 | |
1378 return e; | |
1379 } | |
1380 | |
1381 ////////////////////////////////////////////////////////////////////////////////////////// | |
1382 | |
1383 elem* AddrExp::toElem(IRState* p) | |
1384 { | |
1385 Logger::print("AddrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1386 LOG_SCOPE; | |
1387 elem* e = e1->toElem(p); | |
1388 e->field = true; | |
1389 return e; | |
1390 } | |
1391 | |
1392 ////////////////////////////////////////////////////////////////////////////////////////// | |
1393 | |
1394 elem* StructLiteralExp::toElem(IRState* p) | |
1395 { | |
1396 Logger::print("StructLiteralExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1397 LOG_SCOPE; | |
1398 elem* e = new elem; | |
1399 | |
30
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1400 llvm::Value* sptr = 0; |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1401 |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1402 // if there is no lval, this is probably a temporary struct literal. correct? |
1 | 1403 if (p->lvals.empty()) |
1404 { | |
30
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1405 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
|
1406 e->mem = sptr; |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1407 e->type = elem::VAR; |
1 | 1408 } |
30
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1409 // already has memory |
1 | 1410 else |
1411 { | |
30
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1412 sptr = p->toplval(); |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1413 } |
1 | 1414 |
30
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1415 assert(sptr); |
1 | 1416 |
30
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1417 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
|
1418 |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1419 unsigned n = elements->dim; |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1420 for (unsigned i=0; i<n; ++i) |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1421 { |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1422 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
|
1423 llvm::Value* arrptr = LLVM_DtoGEP(sptr,zero,offset,"tmp",p->scopebb()); |
1 | 1424 |
30
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1425 Expression* vx = (Expression*)elements->data[i]; |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1426 if (vx != 0) { |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1427 elem* ve = vx->toElem(p); |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1428 llvm::Value* val = ve->getValue(); |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1429 Logger::cout() << *val << " | " << *arrptr << '\n'; |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1430 if (vx->type->ty == Tstruct) { |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1431 TypeStruct* ts = (TypeStruct*)vx->type; |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1432 LLVM_DtoStructCopy(ts,arrptr,val); |
1 | 1433 } |
30
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1434 else |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1435 new llvm::StoreInst(val, arrptr, p->scopebb()); |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1436 delete ve; |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1437 } |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1438 else { |
881158a93592
[svn r34] * Fixed passing a struct literal as function argument
lindquist
parents:
29
diff
changeset
|
1439 assert(0); |
1 | 1440 } |
1441 } | |
1442 | |
1443 e->inplace = true; | |
1444 | |
1445 return e; | |
1446 } | |
1447 | |
1448 ////////////////////////////////////////////////////////////////////////////////////////// | |
1449 | |
1450 elem* IndexExp::toElem(IRState* p) | |
1451 { | |
1452 Logger::print("IndexExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1453 LOG_SCOPE; | |
1454 | |
1455 elem* e = new elem; | |
1456 | |
1457 elem* l = e1->toElem(p); | |
1458 | |
1459 p->arrays.push_back(l->mem); // if $ is used it must be an array so this is fine. | |
1460 elem* r = e2->toElem(p); | |
1461 p->arrays.pop_back(); | |
1462 | |
1463 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1464 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1465 | |
1466 llvm::Value* arrptr = 0; | |
1467 if (e1->type->ty == Tpointer) { | |
1468 arrptr = new llvm::GetElementPtrInst(l->getValue(),r->getValue(),"tmp",p->scopebb()); | |
1469 } | |
1470 else if (e1->type->ty == Tsarray) { | |
6 | 1471 arrptr = LLVM_DtoGEP(l->mem, zero, r->getValue(),"tmp",p->scopebb()); |
1 | 1472 } |
1473 else if (e1->type->ty == Tarray) { | |
6 | 1474 arrptr = LLVM_DtoGEP(l->mem,zero,one,"tmp",p->scopebb()); |
1 | 1475 arrptr = new llvm::LoadInst(arrptr,"tmp",p->scopebb()); |
1476 arrptr = new llvm::GetElementPtrInst(arrptr,r->getValue(),"tmp",p->scopebb()); | |
1477 } | |
1478 assert(arrptr); | |
1479 | |
1480 e->mem = arrptr; | |
1481 e->type = elem::VAR; | |
1482 | |
1483 delete l; | |
1484 delete r; | |
1485 | |
1486 return e; | |
1487 } | |
1488 | |
1489 ////////////////////////////////////////////////////////////////////////////////////////// | |
1490 | |
1491 elem* SliceExp::toElem(IRState* p) | |
1492 { | |
1493 Logger::print("SliceExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1494 LOG_SCOPE; | |
1495 | |
1496 elem* v = e1->toElem(p); | |
1497 | |
1498 elem* e = new elem; | |
1499 assert(v->mem); | |
1500 e->type = elem::SLICE; | |
1501 | |
1502 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1503 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1504 | |
1505 // partial slice | |
1506 if (lwr) | |
1507 { | |
1508 assert(upr); | |
1509 p->arrays.push_back(v->mem); | |
1510 elem* lo = lwr->toElem(p); | |
1511 | |
1512 bool lwr_is_zero = false; | |
1513 if (lo->type == elem::CONST) | |
1514 { | |
1515 assert(lo->val); | |
1516 assert(llvm::isa<llvm::ConstantInt>(lo->val)); | |
1517 | |
1518 if (e1->type->ty == Tpointer) { | |
1519 e->mem = v->getValue(); | |
1520 } | |
1521 else if (e1->type->ty == Tarray) { | |
6 | 1522 llvm::Value* tmp = LLVM_DtoGEP(v->mem,zero,one,"tmp",p->scopebb()); |
1 | 1523 e->mem = new llvm::LoadInst(tmp,"tmp",p->scopebb()); |
1524 } | |
1525 else | |
1526 assert(e->mem); | |
1527 | |
1528 llvm::ConstantInt* c = llvm::cast<llvm::ConstantInt>(lo->val); | |
1529 if (!(lwr_is_zero = c->isZero())) { | |
1530 e->mem = new llvm::GetElementPtrInst(e->mem,lo->val,"tmp",p->scopebb()); | |
1531 } | |
1532 } | |
1533 else | |
1534 { | |
6 | 1535 llvm::Value* tmp = LLVM_DtoGEP(v->mem,zero,one,"tmp",p->scopebb()); |
1 | 1536 tmp = new llvm::LoadInst(tmp,"tmp",p->scopebb()); |
1537 e->mem = new llvm::GetElementPtrInst(tmp,lo->getValue(),"tmp",p->scopebb()); | |
1538 } | |
1539 | |
1540 elem* up = upr->toElem(p); | |
1541 p->arrays.pop_back(); | |
1542 | |
1543 if (up->type == elem::CONST) | |
1544 { | |
1545 assert(up->val); | |
1546 assert(llvm::isa<llvm::ConstantInt>(up->val)); | |
1547 if (lwr_is_zero) { | |
1548 e->arg = up->val; | |
1549 } | |
1550 else { | |
1551 if (lo->type == elem::CONST) { | |
1552 llvm::Constant* clo = llvm::cast<llvm::Constant>(lo->val); | |
1553 llvm::Constant* cup = llvm::cast<llvm::Constant>(up->val); | |
1554 e->arg = llvm::ConstantExpr::getSub(cup, clo); | |
1555 } | |
1556 else { | |
1557 e->arg = llvm::BinaryOperator::createSub(up->val, lo->getValue(), "tmp", p->scopebb()); | |
1558 } | |
1559 } | |
1560 } | |
1561 else | |
1562 { | |
1563 if (lwr_is_zero) { | |
1564 e->arg = up->getValue(); | |
1565 } | |
1566 else { | |
1567 e->arg = llvm::BinaryOperator::createSub(up->getValue(), lo->getValue(), "tmp", p->scopebb()); | |
1568 } | |
1569 } | |
1570 | |
1571 delete lo; | |
1572 delete up; | |
1573 } | |
1574 // full slice | |
1575 else | |
1576 { | |
1577 e->mem = v->mem; | |
1578 } | |
1579 | |
1580 delete v; | |
1581 | |
1582 return e; | |
1583 } | |
1584 | |
1585 ////////////////////////////////////////////////////////////////////////////////////////// | |
1586 | |
1587 elem* CmpExp::toElem(IRState* p) | |
1588 { | |
1589 Logger::print("CmpExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1590 LOG_SCOPE; | |
1591 | |
1592 elem* e = new elem; | |
1593 | |
1594 elem* l = e1->toElem(p); | |
1595 elem* r = e2->toElem(p); | |
1596 | |
1597 assert(e1->type == e2->type); | |
1598 | |
1599 Type* t = e1->type; | |
1600 | |
1601 if (t->isintegral()) | |
1602 { | |
1603 llvm::ICmpInst::Predicate cmpop; | |
1604 switch(op) | |
1605 { | |
1606 case TOKlt: | |
1607 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_ULT : llvm::ICmpInst::ICMP_SLT; | |
1608 break; | |
1609 case TOKle: | |
1610 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_ULE : llvm::ICmpInst::ICMP_SLE; | |
1611 break; | |
1612 case TOKgt: | |
1613 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_UGT : llvm::ICmpInst::ICMP_SGT; | |
1614 break; | |
1615 case TOKge: | |
1616 cmpop = t->isunsigned() ? llvm::ICmpInst::ICMP_UGE : llvm::ICmpInst::ICMP_SGE; | |
1617 break; | |
1618 default: | |
1619 assert(0); | |
1620 } | |
1621 e->val = new llvm::ICmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1622 } | |
1623 else if (t->isfloating()) | |
1624 { | |
1625 llvm::FCmpInst::Predicate cmpop; | |
1626 switch(op) | |
1627 { | |
1628 case TOKlt: | |
1629 cmpop = llvm::FCmpInst::FCMP_OLT;break; | |
1630 case TOKle: | |
1631 cmpop = llvm::FCmpInst::FCMP_OLE;break; | |
1632 case TOKgt: | |
1633 cmpop = llvm::FCmpInst::FCMP_OGT;break; | |
1634 case TOKge: | |
1635 cmpop = llvm::FCmpInst::FCMP_OGE;break; | |
1636 case TOKunord: | |
1637 cmpop = llvm::FCmpInst::FCMP_UNO;break; | |
1638 case TOKule: | |
1639 cmpop = llvm::FCmpInst::FCMP_ULE;break; | |
1640 case TOKul: | |
1641 cmpop = llvm::FCmpInst::FCMP_ULT;break; | |
1642 case TOKuge: | |
1643 cmpop = llvm::FCmpInst::FCMP_UGE;break; | |
1644 case TOKug: | |
1645 cmpop = llvm::FCmpInst::FCMP_UGT;break; | |
1646 case TOKue: | |
1647 cmpop = llvm::FCmpInst::FCMP_UEQ;break; | |
1648 case TOKlg: | |
1649 cmpop = llvm::FCmpInst::FCMP_ONE;break; | |
1650 case TOKleg: | |
1651 cmpop = llvm::FCmpInst::FCMP_ORD;break; | |
1652 | |
1653 default: | |
1654 assert(0); | |
1655 } | |
1656 e->val = new llvm::FCmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1657 } | |
1658 else | |
1659 { | |
1660 assert(0 && "Unsupported CmpExp type"); | |
1661 } | |
1662 | |
1663 delete l; | |
1664 delete r; | |
1665 | |
1666 e->type = elem::VAL; | |
1667 | |
1668 return e; | |
1669 } | |
1670 | |
1671 ////////////////////////////////////////////////////////////////////////////////////////// | |
1672 | |
1673 elem* EqualExp::toElem(IRState* p) | |
1674 { | |
1675 Logger::print("EqualExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1676 LOG_SCOPE; | |
1677 | |
1678 elem* e = new elem; | |
1679 | |
1680 elem* l = e1->toElem(p); | |
1681 elem* r = e2->toElem(p); | |
1682 | |
1683 assert(e1->type == e2->type); | |
1684 | |
1685 Type* t = e1->type; | |
1686 | |
1687 if (t->isintegral() || t->ty == Tpointer) | |
1688 { | |
1689 llvm::ICmpInst::Predicate cmpop; | |
1690 switch(op) | |
1691 { | |
1692 case TOKequal: | |
1693 cmpop = llvm::ICmpInst::ICMP_EQ; | |
1694 break; | |
1695 case TOKnotequal: | |
1696 cmpop = llvm::ICmpInst::ICMP_NE; | |
1697 break; | |
1698 default: | |
1699 assert(0); | |
1700 } | |
1701 e->val = new llvm::ICmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1702 } | |
1703 else if (t->isfloating()) | |
1704 { | |
1705 llvm::FCmpInst::Predicate cmpop; | |
1706 switch(op) | |
1707 { | |
1708 case TOKequal: | |
1709 cmpop = llvm::FCmpInst::FCMP_OEQ; | |
1710 break; | |
1711 case TOKnotequal: | |
1712 cmpop = llvm::FCmpInst::FCMP_UNE; | |
1713 break; | |
1714 default: | |
1715 assert(0); | |
1716 } | |
1717 e->val = new llvm::FCmpInst(cmpop, l->getValue(), r->getValue(), "tmp", p->scopebb()); | |
1718 } | |
1719 else if (t->ty == Tarray) | |
1720 { | |
1721 // array comparison invokes the typeinfo runtime | |
1722 assert(0); | |
1723 } | |
1724 else | |
1725 { | |
1726 assert(0 && "Unsupported EqualExp type"); | |
1727 } | |
1728 | |
1729 delete l; | |
1730 delete r; | |
1731 | |
1732 e->type = elem::VAL; | |
1733 | |
1734 return e; | |
1735 } | |
1736 | |
1737 ////////////////////////////////////////////////////////////////////////////////////////// | |
1738 | |
1739 elem* PostExp::toElem(IRState* p) | |
1740 { | |
1741 Logger::print("PostExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1742 LOG_SCOPE; | |
1743 | |
1744 elem* l = e1->toElem(p); | |
1745 elem* r = e2->toElem(p); | |
1746 | |
1747 elem* e = new elem; | |
1748 e->mem = l->mem; | |
1749 e->val = l->getValue(); | |
1750 e->type = elem::VAL; | |
1751 | |
1752 llvm::Value* val = e->val; | |
1753 llvm::Value* post = 0; | |
1754 | |
1755 if (e1->type->isintegral()) | |
1756 { | |
1757 assert(e2->type->isintegral()); | |
1758 llvm::Value* one = llvm::ConstantInt::get(val->getType(), 1, !e2->type->isunsigned()); | |
1759 if (op == TOKplusplus) { | |
1760 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb()); | |
1761 } | |
1762 else if (op == TOKminusminus) { | |
1763 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb()); | |
1764 } | |
1765 } | |
1766 else if (e1->type->ty == Tpointer) | |
1767 { | |
1768 assert(e2->type->isintegral()); | |
1769 llvm::Constant* minusone = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)-1,true); | |
1770 llvm::Constant* plusone = llvm::ConstantInt::get(LLVM_DtoSize_t(),(uint64_t)1,false); | |
1771 llvm::Constant* whichone = (op == TOKplusplus) ? plusone : minusone; | |
1772 post = new llvm::GetElementPtrInst(val, whichone, "tmp", p->scopebb()); | |
1773 } | |
1774 else if (e1->type->isfloating()) | |
1775 { | |
1776 assert(e2->type->isfloating()); | |
1777 llvm::Value* one = llvm::ConstantFP::get(val->getType(), 1.0f); | |
1778 if (op == TOKplusplus) { | |
1779 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb()); | |
1780 } | |
1781 else if (op == TOKminusminus) { | |
1782 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb()); | |
1783 } | |
1784 } | |
1785 else | |
1786 assert(post); | |
1787 | |
1788 //llvm::Value* tostore = l->storeVal ? l->storeVal : l->val; | |
1789 new llvm::StoreInst(post,l->mem,p->scopebb()); | |
1790 | |
1791 delete l; | |
1792 delete r; | |
1793 | |
1794 return e; | |
1795 } | |
1796 | |
1797 ////////////////////////////////////////////////////////////////////////////////////////// | |
1798 | |
1799 elem* NewExp::toElem(IRState* p) | |
1800 { | |
1801 Logger::print("NewExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1802 LOG_SCOPE; | |
1803 | |
1804 assert(!thisexp); | |
1805 assert(!newargs); | |
1806 assert(newtype); | |
1807 //assert(!arguments); | |
1808 //assert(!member); | |
1809 assert(!allocator); | |
1810 | |
1811 elem* e = new elem; | |
1812 | |
1813 const llvm::Type* t = LLVM_DtoType(newtype); | |
1814 | |
1815 if (onstack) { | |
1816 assert(newtype->ty == Tclass); | |
1817 e->mem = new llvm::AllocaInst(t->getContainedType(0),"tmp",p->topallocapoint()); | |
1818 } | |
1819 else { | |
1820 if (newtype->ty == Tclass) { | |
1821 e->mem = new llvm::MallocInst(t->getContainedType(0),"tmp",p->scopebb()); | |
1822 } | |
1823 else if (newtype->ty == Tarray) { | |
1824 t = LLVM_DtoType(newtype->next); | |
1825 assert(arguments); | |
1826 if (arguments->dim == 1) { | |
1827 elem* sz = ((Expression*)arguments->data[0])->toElem(p); | |
1828 llvm::Value* dimval = sz->getValue(); | |
1829 llvm::Value* usedimval = dimval; | |
1830 if (dimval->getType() != llvm::Type::Int32Ty) | |
1831 usedimval = new llvm::TruncInst(dimval, llvm::Type::Int32Ty,"tmp",p->scopebb()); | |
1832 e->mem = new llvm::MallocInst(t,usedimval,"tmp",p->scopebb()); | |
1833 | |
1834 LLVM_DtoSetArray(p->toplval(), dimval, e->mem); | |
1835 delete sz; | |
1836 } | |
1837 else { | |
1838 assert(0); | |
1839 } | |
1840 } | |
1841 else { | |
1842 e->mem = new llvm::MallocInst(t,"tmp",p->scopebb()); | |
1843 } | |
1844 } | |
1845 | |
1846 if (newtype->ty == Tclass) { | |
1847 // first apply the static initializer | |
1848 assert(e->mem); | |
1849 LLVM_DtoInitClass((TypeClass*)newtype, e->mem); | |
1850 | |
1851 // then call constructor | |
1852 if (arguments) { | |
1853 std::vector<llvm::Value*> ctorargs; | |
1854 ctorargs.push_back(e->mem); | |
1855 for (size_t i=0; i<arguments->dim; ++i) | |
1856 { | |
1857 Expression* ex = (Expression*)arguments->data[i]; | |
1858 Logger::println("arg=%s", ex->toChars()); | |
1859 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
|
1860 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
|
1861 assert(v); |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
26
diff
changeset
|
1862 ctorargs.push_back(v); |
1 | 1863 delete exe; |
1864 } | |
1865 assert(member); | |
1866 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
|
1867 e->mem = new llvm::CallInst(member->llvmValue, ctorargs.begin(), ctorargs.end(), "tmp", p->scopebb()); |
1 | 1868 } |
1869 } | |
1870 else if (newtype->ty == Tstruct) { | |
1871 TypeStruct* ts = (TypeStruct*)newtype; | |
1872 if (ts->isZeroInit()) { | |
1873 LLVM_DtoStructZeroInit(ts,e->mem); | |
1874 } | |
1875 else { | |
1876 LLVM_DtoStructCopy(ts,e->mem,ts->llvmInit); | |
1877 } | |
1878 } | |
1879 | |
1880 e->inplace = true; | |
1881 e->type = elem::VAR; | |
15
37a4fdab33fc
[svn r19] * Added support for reassigning 'this' inside class constructors.
lindquist
parents:
14
diff
changeset
|
1882 |
1 | 1883 return e; |
1884 } | |
1885 | |
1886 ////////////////////////////////////////////////////////////////////////////////////////// | |
1887 | |
1888 elem* DeleteExp::toElem(IRState* p) | |
1889 { | |
1890 Logger::print("DeleteExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1891 LOG_SCOPE; | |
1892 | |
1893 //assert(e1->type->ty != Tclass); | |
1894 | |
1895 elem* v = e1->toElem(p); | |
1896 llvm::Value* val = v->getValue(); | |
1897 llvm::Value* ldval = 0; | |
1898 | |
1899 const llvm::Type* t = val->getType(); | |
1900 llvm::Constant* z = llvm::Constant::getNullValue(t); | |
1901 | |
1902 if (e1->type->ty == Tpointer) { | |
1903 ldval = v->getValue(); | |
1904 new llvm::FreeInst(ldval, p->scopebb()); | |
1905 | |
1906 Logger::cout() << *z << '\n'; | |
1907 Logger::cout() << *val << '\n'; | |
1908 new llvm::StoreInst(z, v->mem, p->scopebb()); | |
1909 } | |
1910 else if (e1->type->ty == Tclass) { | |
1911 TypeClass* tc = (TypeClass*)e1->type; | |
1912 LLVM_DtoCallClassDtors(tc, val); | |
1913 | |
1914 if (v->vardecl && !v->vardecl->onstack) { | |
1915 new llvm::FreeInst(val, p->scopebb()); | |
1916 } | |
1917 new llvm::StoreInst(z, v->mem, p->scopebb()); | |
1918 } | |
1919 else if (e1->type->ty == Tarray) { | |
1920 // must be on the heap (correct?) | |
1921 ldval = v->getValue(); | |
1922 | |
1923 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
1924 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
1925 | |
6 | 1926 llvm::Value* ptr = LLVM_DtoGEP(ldval,zero,one,"tmp",p->scopebb()); |
1 | 1927 ptr = new llvm::LoadInst(ptr,"tmp",p->scopebb()); |
1928 new llvm::FreeInst(ptr, p->scopebb()); | |
1929 LLVM_DtoNullArray(val); | |
1930 } | |
1931 else { | |
1932 assert(0); | |
1933 } | |
1934 | |
1935 delete v; | |
1936 | |
1937 // this expression produces no useful data | |
1938 return 0; | |
1939 } | |
1940 | |
1941 ////////////////////////////////////////////////////////////////////////////////////////// | |
1942 | |
1943 elem* ArrayLengthExp::toElem(IRState* p) | |
1944 { | |
1945 Logger::print("ArrayLengthExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1946 LOG_SCOPE; | |
1947 | |
1948 elem* e = new elem; | |
1949 elem* u = e1->toElem(p); | |
1950 | |
1951 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
6 | 1952 llvm::Value* ptr = LLVM_DtoGEP(u->mem,zero,zero,"tmp",p->scopebb()); |
1 | 1953 e->val = new llvm::LoadInst(ptr, "tmp", p->scopebb()); |
1954 e->type = elem::VAL; | |
1955 | |
1956 delete u; | |
1957 | |
1958 return e; | |
1959 } | |
1960 | |
1961 ////////////////////////////////////////////////////////////////////////////////////////// | |
1962 | |
1963 elem* AssertExp::toElem(IRState* p) | |
1964 { | |
1965 Logger::print("AssertExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1966 LOG_SCOPE; | |
1967 | |
1968 elem* e = new elem; | |
1969 elem* u = e1->toElem(p); | |
1970 elem* m = msg ? msg->toElem(p) : 0; | |
1971 | |
1972 std::vector<llvm::Value*> llargs; | |
1973 llargs.resize(3); | |
1974 llargs[0] = LLVM_DtoBoolean(u->getValue()); | |
1975 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false); | |
1976 llargs[2] = m ? m->val : llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
1977 | |
1978 delete m; | |
1979 delete u; | |
1980 | |
1981 //Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n'; | |
1982 | |
1983 llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert"); | |
1984 assert(fn); | |
1985 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb()); | |
1986 call->setCallingConv(llvm::CallingConv::C); | |
1987 | |
1988 return e; | |
1989 } | |
1990 | |
1991 ////////////////////////////////////////////////////////////////////////////////////////// | |
1992 | |
1993 elem* NotExp::toElem(IRState* p) | |
1994 { | |
1995 Logger::print("NotExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
1996 LOG_SCOPE; | |
1997 | |
1998 elem* e = new elem; | |
1999 elem* u = e1->toElem(p); | |
2000 | |
2001 llvm::Value* b = LLVM_DtoBoolean(u->getValue()); | |
2002 | |
2003 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, true); | |
2004 e->val = new llvm::ICmpInst(llvm::ICmpInst::ICMP_EQ,b,zero,"tmp",p->scopebb()); | |
2005 e->type = elem::VAL; | |
2006 | |
2007 delete u; | |
2008 | |
2009 return e; | |
2010 } | |
2011 | |
2012 ////////////////////////////////////////////////////////////////////////////////////////// | |
2013 | |
2014 elem* AndAndExp::toElem(IRState* p) | |
2015 { | |
2016 Logger::print("AndAndExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2017 LOG_SCOPE; | |
2018 | |
2019 // allocate a temporary for the final result. failed to come up with a better way :/ | |
2020 llvm::Value* resval = 0; | |
2021 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
2022 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"andandtmp",p->topallocapoint()); | |
2023 | |
2024 elem* e = new elem; | |
2025 elem* u = e1->toElem(p); | |
2026 | |
2027 llvm::BasicBlock* oldend = p->scopeend(); | |
2028 llvm::BasicBlock* andand = new llvm::BasicBlock("andand", gIR->topfunc(), oldend); | |
2029 llvm::BasicBlock* andandend = new llvm::BasicBlock("andandend", gIR->topfunc(), oldend); | |
2030 | |
2031 llvm::Value* ubool = LLVM_DtoBoolean(u->getValue()); | |
2032 new llvm::StoreInst(ubool,resval,p->scopebb()); | |
2033 new llvm::BranchInst(andand,andandend,ubool,p->scopebb()); | |
2034 | |
2035 p->scope() = IRScope(andand, andandend); | |
2036 elem* v = e2->toElem(p); | |
2037 | |
2038 llvm::Value* vbool = LLVM_DtoBoolean(v->getValue()); | |
2039 llvm::Value* uandvbool = llvm::BinaryOperator::create(llvm::BinaryOperator::And, ubool, vbool,"tmp",p->scopebb()); | |
2040 new llvm::StoreInst(uandvbool,resval,p->scopebb()); | |
2041 new llvm::BranchInst(andandend,p->scopebb()); | |
2042 | |
2043 delete u; | |
2044 delete v; | |
2045 | |
2046 p->scope() = IRScope(andandend, oldend); | |
2047 | |
2048 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
2049 e->type = elem::VAL; | |
2050 | |
2051 return e; | |
2052 } | |
2053 | |
2054 ////////////////////////////////////////////////////////////////////////////////////////// | |
2055 | |
2056 elem* OrOrExp::toElem(IRState* p) | |
2057 { | |
2058 Logger::print("OrOrExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2059 LOG_SCOPE; | |
2060 | |
2061 // allocate a temporary for the final result. failed to come up with a better way :/ | |
2062 llvm::Value* resval = 0; | |
2063 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
2064 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"orortmp",p->topallocapoint()); | |
2065 | |
2066 elem* e = new elem; | |
2067 elem* u = e1->toElem(p); | |
2068 | |
2069 llvm::BasicBlock* oldend = p->scopeend(); | |
2070 llvm::BasicBlock* oror = new llvm::BasicBlock("oror", gIR->topfunc(), oldend); | |
2071 llvm::BasicBlock* ororend = new llvm::BasicBlock("ororend", gIR->topfunc(), oldend); | |
2072 | |
2073 llvm::Value* ubool = LLVM_DtoBoolean(u->getValue()); | |
2074 new llvm::StoreInst(ubool,resval,p->scopebb()); | |
2075 new llvm::BranchInst(ororend,oror,ubool,p->scopebb()); | |
2076 | |
2077 p->scope() = IRScope(oror, ororend); | |
2078 elem* v = e2->toElem(p); | |
2079 | |
2080 llvm::Value* vbool = LLVM_DtoBoolean(v->getValue()); | |
2081 new llvm::StoreInst(vbool,resval,p->scopebb()); | |
2082 new llvm::BranchInst(ororend,p->scopebb()); | |
2083 | |
2084 delete u; | |
2085 delete v; | |
2086 | |
2087 p->scope() = IRScope(ororend, oldend); | |
2088 | |
2089 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
2090 e->type = elem::VAL; | |
2091 | |
2092 return e; | |
2093 } | |
2094 | |
2095 ////////////////////////////////////////////////////////////////////////////////////////// | |
2096 | |
2097 #define BinBitExp(X,Y) \ | |
2098 elem* X##Exp::toElem(IRState* p) \ | |
2099 { \ | |
2100 Logger::print("%sExp::toElem: %s | %s\n", #X, toChars(), type->toChars()); \ | |
2101 LOG_SCOPE; \ | |
2102 elem* e = new elem; \ | |
2103 elem* u = e1->toElem(p); \ | |
2104 elem* v = e2->toElem(p); \ | |
2105 e->val = llvm::BinaryOperator::create(llvm::Instruction::Y, u->getValue(), v->getValue(), "tmp", p->scopebb()); \ | |
2106 e->type = elem::VAL; \ | |
2107 delete u; \ | |
2108 delete v; \ | |
2109 return e; \ | |
2110 } \ | |
2111 \ | |
2112 elem* X##AssignExp::toElem(IRState* p) \ | |
2113 { \ | |
2114 Logger::print("%sAssignExp::toElem: %s | %s\n", #X, toChars(), type->toChars()); \ | |
2115 LOG_SCOPE; \ | |
2116 elem* u = e1->toElem(p); \ | |
2117 elem* v = e2->toElem(p); \ | |
2118 llvm::Value* tmp = llvm::BinaryOperator::create(llvm::Instruction::Y, u->getValue(), v->getValue(), "tmp", p->scopebb()); \ | |
2119 Logger::cout() << *tmp << '|' << *u->mem << '\n'; \ | |
29
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
28
diff
changeset
|
2120 if (u->mem == 0) \ |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
28
diff
changeset
|
2121 LLVM_DtoGiveArgumentStorage(u); \ |
1 | 2122 new llvm::StoreInst(LLVM_DtoPointedType(u->mem, tmp), u->mem, p->scopebb()); \ |
2123 delete u; \ | |
2124 delete v; \ | |
2125 elem* e = new elem; \ | |
2126 e->mem = u->mem; \ | |
2127 e->type = elem::VAR; \ | |
2128 return e; \ | |
2129 } | |
2130 | |
2131 BinBitExp(And,And); | |
2132 BinBitExp(Or,Or); | |
2133 BinBitExp(Xor,Xor); | |
2134 BinBitExp(Shl,Shl); | |
2135 BinBitExp(Shr,AShr); | |
2136 BinBitExp(Ushr,LShr); | |
2137 | |
2138 ////////////////////////////////////////////////////////////////////////////////////////// | |
2139 | |
2140 elem* HaltExp::toElem(IRState* p) | |
2141 { | |
2142 Logger::print("HaltExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2143 LOG_SCOPE; | |
2144 | |
2145 std::vector<llvm::Value*> llargs; | |
2146 llargs.resize(3); | |
2147 llargs[0] = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, false); | |
2148 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false); | |
2149 llargs[2] = llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
2150 | |
2151 //Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n'; | |
2152 | |
2153 llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert"); | |
2154 assert(fn); | |
2155 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb()); | |
2156 call->setCallingConv(llvm::CallingConv::C); | |
2157 | |
2158 //new llvm::UnreachableInst(p->scopebb()); | |
2159 | |
2160 return 0; | |
2161 } | |
2162 | |
2163 ////////////////////////////////////////////////////////////////////////////////////////// | |
2164 | |
2165 elem* DelegateExp::toElem(IRState* p) | |
2166 { | |
2167 Logger::print("DelegateExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2168 LOG_SCOPE; | |
2169 | |
2170 elem* e = new elem; | |
2171 elem* u = e1->toElem(p); | |
2172 | |
2173 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
2174 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | |
2175 | |
2176 const llvm::Type* int8ptrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
2177 | |
2178 llvm::Value* lval = p->toplval(); | |
2179 | |
6 | 2180 llvm::Value* context = LLVM_DtoGEP(lval,zero,zero,"tmp",p->scopebb()); |
1 | 2181 llvm::Value* castcontext = new llvm::BitCastInst(u->getValue(),int8ptrty,"tmp",p->scopebb()); |
2182 new llvm::StoreInst(castcontext, context, p->scopebb()); | |
2183 | |
6 | 2184 llvm::Value* fptr = LLVM_DtoGEP(lval,zero,one,"tmp",p->scopebb()); |
1 | 2185 |
2186 assert(func->llvmValue); | |
2187 llvm::Value* castfptr = new llvm::BitCastInst(func->llvmValue,fptr->getType()->getContainedType(0),"tmp",p->scopebb()); | |
2188 new llvm::StoreInst(castfptr, fptr, p->scopebb()); | |
2189 | |
2190 e->inplace = true; | |
2191 | |
2192 delete u; | |
2193 return e; | |
2194 } | |
2195 | |
2196 ////////////////////////////////////////////////////////////////////////////////////////// | |
2197 | |
2198 elem* IdentityExp::toElem(IRState* p) | |
2199 { | |
2200 Logger::print("IdentityExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2201 LOG_SCOPE; | |
2202 | |
2203 elem* u = e1->toElem(p); | |
2204 elem* v = e2->toElem(p); | |
2205 | |
2206 elem* e = new elem; | |
2207 | |
2208 llvm::Value* l = u->getValue(); | |
2209 llvm::Value* r = 0; | |
2210 if (v->type == elem::NUL) | |
2211 r = llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(l->getType())); | |
2212 else | |
2213 r = v->getValue(); | |
2214 | |
2215 llvm::ICmpInst::Predicate pred = (op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE; | |
2216 e->val = new llvm::ICmpInst(pred, l, r, "tmp", p->scopebb()); | |
2217 e->type = elem::VAL; | |
2218 | |
2219 delete u; | |
2220 delete v; | |
2221 | |
2222 return e; | |
2223 } | |
2224 | |
2225 ////////////////////////////////////////////////////////////////////////////////////////// | |
2226 | |
2227 elem* CommaExp::toElem(IRState* p) | |
2228 { | |
2229 Logger::print("CommaExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2230 LOG_SCOPE; | |
2231 | |
2232 elem* u = e1->toElem(p); | |
2233 elem* v = e2->toElem(p); | |
2234 delete u; | |
2235 return v; | |
2236 } | |
2237 | |
2238 ////////////////////////////////////////////////////////////////////////////////////////// | |
2239 | |
2240 elem* CondExp::toElem(IRState* p) | |
2241 { | |
2242 Logger::print("CondExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2243 LOG_SCOPE; | |
2244 | |
2245 const llvm::Type* resty = LLVM_DtoType(type); | |
2246 | |
2247 // allocate a temporary for the final result. failed to come up with a better way :/ | |
2248 llvm::BasicBlock* entryblock = &p->topfunc()->front(); | |
2249 llvm::Value* resval = new llvm::AllocaInst(resty,"condtmp",p->topallocapoint()); | |
2250 | |
2251 llvm::BasicBlock* oldend = p->scopeend(); | |
2252 llvm::BasicBlock* condtrue = new llvm::BasicBlock("condtrue", gIR->topfunc(), oldend); | |
2253 llvm::BasicBlock* condfalse = new llvm::BasicBlock("condfalse", gIR->topfunc(), oldend); | |
2254 llvm::BasicBlock* condend = new llvm::BasicBlock("condend", gIR->topfunc(), oldend); | |
2255 | |
2256 elem* c = econd->toElem(p); | |
2257 llvm::Value* cond_val = LLVM_DtoBoolean(c->getValue()); | |
2258 delete c; | |
2259 new llvm::BranchInst(condtrue,condfalse,cond_val,p->scopebb()); | |
2260 | |
2261 p->scope() = IRScope(condtrue, condfalse); | |
2262 elem* u = e1->toElem(p); | |
2263 new llvm::StoreInst(u->getValue(),resval,p->scopebb()); | |
2264 new llvm::BranchInst(condend,p->scopebb()); | |
2265 delete u; | |
2266 | |
2267 p->scope() = IRScope(condfalse, condend); | |
2268 elem* v = e2->toElem(p); | |
2269 new llvm::StoreInst(v->getValue(),resval,p->scopebb()); | |
2270 new llvm::BranchInst(condend,p->scopebb()); | |
2271 delete v; | |
2272 | |
2273 p->scope() = IRScope(condend, oldend); | |
2274 | |
2275 elem* e = new elem; | |
2276 e->val = new llvm::LoadInst(resval,"tmp",p->scopebb()); | |
2277 e->type = elem::VAL; | |
2278 return e; | |
2279 } | |
2280 | |
2281 ////////////////////////////////////////////////////////////////////////////////////////// | |
2282 | |
2283 elem* ComExp::toElem(IRState* p) | |
2284 { | |
2285 Logger::print("ComExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2286 LOG_SCOPE; | |
2287 | |
2288 elem* e = new elem; | |
2289 elem* u = e1->toElem(p); | |
2290 | |
2291 llvm::Value* value = u->getValue(); | |
2292 llvm::Value* minusone = llvm::ConstantInt::get(value->getType(), -1, true); | |
2293 e->val = llvm::BinaryOperator::create(llvm::Instruction::Xor, value, minusone, "tmp", p->scopebb()); | |
2294 | |
2295 delete u; | |
2296 | |
2297 e->type = elem::VAL; | |
2298 | |
2299 return e; | |
2300 } | |
2301 | |
2302 ////////////////////////////////////////////////////////////////////////////////////////// | |
2303 | |
23 | 2304 elem* NegExp::toElem(IRState* p) |
2305 { | |
2306 Logger::print("NegExp::toElem: %s | %s\n", toChars(), type->toChars()); | |
2307 LOG_SCOPE; | |
2308 elem* e = new elem; | |
2309 elem* l = e1->toElem(p); | |
2310 llvm::Value* val = l->getValue(); | |
2311 delete l; | |
2312 | |
2313 llvm::Value* zero = 0; | |
2314 if (type->isintegral()) | |
2315 zero = llvm::ConstantInt::get(val->getType(), 0, true); | |
2316 else if (type->isfloating()) { | |
2317 if (type->ty == Tfloat32) | |
2318 zero = llvm::ConstantFP::get(val->getType(), float(0)); | |
2319 else if (type->ty == Tfloat64 || type->ty == Tfloat80) | |
2320 zero = llvm::ConstantFP::get(val->getType(), double(0)); | |
2321 else | |
2322 assert(0); | |
2323 } | |
2324 else | |
2325 assert(0); | |
2326 | |
2327 e->val = llvm::BinaryOperator::createSub(zero,val,"tmp",p->scopebb()); | |
2328 e->type = elem::VAL; | |
2329 | |
2330 return e; | |
2331 } | |
2332 | |
2333 ////////////////////////////////////////////////////////////////////////////////////////// | |
2334 | |
1 | 2335 #define STUB(x) elem *x::toElem(IRState * p) {error("Exp type "#x" not implemented: %s", toChars()); fatal(); return 0; } |
2336 //STUB(IdentityExp); | |
2337 //STUB(CondExp); | |
2338 //STUB(EqualExp); | |
2339 STUB(InExp); | |
2340 //STUB(CmpExp); | |
2341 //STUB(AndAndExp); | |
2342 //STUB(OrOrExp); | |
2343 //STUB(AndExp); | |
2344 //STUB(AndAssignExp); | |
2345 //STUB(OrExp); | |
2346 //STUB(OrAssignExp); | |
2347 //STUB(XorExp); | |
2348 //STUB(XorAssignExp); | |
2349 //STUB(ShrExp); | |
2350 //STUB(ShrAssignExp); | |
2351 //STUB(ShlExp); | |
2352 //STUB(ShlAssignExp); | |
2353 //STUB(UshrExp); | |
2354 //STUB(UshrAssignExp); | |
2355 //STUB(DivExp); | |
2356 //STUB(DivAssignExp); | |
2357 //STUB(MulExp); | |
2358 //STUB(MulAssignExp); | |
2359 //STUB(ModExp); | |
2360 //STUB(ModAssignExp); | |
2361 STUB(CatExp); | |
2362 STUB(CatAssignExp); | |
2363 //STUB(AddExp); | |
2364 //STUB(AddAssignExp); | |
2365 STUB(Expression); | |
2366 //STUB(MinExp); | |
2367 //STUB(MinAssignExp); | |
2368 //STUB(PostExp); | |
2369 //STUB(NullExp); | |
2370 //STUB(ThisExp); | |
2371 //STUB(CallExp); | |
2372 STUB(DotTypeExp); | |
2373 STUB(TypeDotIdExp); | |
2374 //STUB(DotVarExp); | |
2375 //STUB(AssertExp); | |
2376 STUB(FuncExp); | |
2377 //STUB(DelegateExp); | |
2378 //STUB(VarExp); | |
2379 //STUB(DeclarationExp); | |
2380 //STUB(NewExp); | |
2381 //STUB(SymOffExp); | |
2382 STUB(ScopeExp); | |
2383 //STUB(AssignExp); | |
2384 | |
2385 STUB(TypeExp); | |
2386 //STUB(RealExp); | |
2387 STUB(ComplexExp); | |
2388 //STUB(StringExp); | |
2389 //STUB(IntegerExp); | |
2390 STUB(BoolExp); | |
2391 | |
2392 //STUB(NotExp); | |
2393 //STUB(ComExp); | |
23 | 2394 //STUB(NegExp); |
1 | 2395 //STUB(PtrExp); |
2396 //STUB(AddrExp); | |
2397 //STUB(SliceExp); | |
2398 //STUB(CastExp); | |
2399 //STUB(DeleteExp); | |
2400 //STUB(IndexExp); | |
2401 //STUB(CommaExp); | |
2402 //STUB(ArrayLengthExp); | |
2403 //STUB(HaltExp); | |
2404 STUB(RemoveExp); | |
2405 STUB(ArrayLiteralExp); | |
2406 STUB(AssocArrayLiteralExp); | |
2407 //STUB(StructLiteralExp); | |
2408 | |
2409 unsigned Type::totym() { return 0; } | |
2410 | |
2411 type * | |
2412 Type::toCtype() { | |
2413 return 0; | |
2414 } | |
2415 | |
2416 type * Type::toCParamtype() | |
2417 { | |
2418 return 0; | |
2419 } | |
2420 Symbol * Type::toSymbol() | |
2421 { | |
2422 return 0; | |
2423 } | |
2424 | |
2425 type * | |
2426 TypeTypedef::toCtype() | |
2427 { | |
2428 return 0; | |
2429 } | |
2430 | |
2431 type * | |
2432 TypeTypedef::toCParamtype() | |
2433 { | |
2434 return 0; | |
2435 } | |
2436 | |
2437 void | |
2438 TypedefDeclaration::toDebug() | |
2439 { | |
2440 } | |
2441 | |
2442 | |
2443 type * | |
2444 TypeEnum::toCtype() | |
2445 { | |
2446 return 0; | |
2447 } | |
2448 | |
2449 type * | |
2450 TypeStruct::toCtype() | |
2451 { | |
2452 return 0; | |
2453 } | |
2454 | |
2455 void | |
2456 StructDeclaration::toDebug() | |
2457 { | |
2458 } | |
2459 | |
2460 Symbol * TypeClass::toSymbol() | |
2461 { | |
2462 return 0; | |
2463 } | |
2464 | |
2465 unsigned TypeFunction::totym() | |
2466 { | |
2467 return 0; | |
2468 } | |
2469 | |
2470 type * | |
2471 TypeFunction::toCtype() | |
2472 { | |
2473 return 0; | |
2474 } | |
2475 | |
2476 type * | |
2477 TypeSArray::toCtype() | |
2478 { | |
2479 return 0; | |
2480 } | |
2481 | |
2482 type *TypeSArray::toCParamtype() { return 0; } | |
2483 | |
2484 type * | |
2485 TypeDArray::toCtype() | |
2486 { | |
2487 return 0; | |
2488 } | |
2489 | |
2490 type * | |
2491 TypeAArray::toCtype() | |
2492 { | |
2493 return 0; | |
2494 } | |
2495 | |
2496 type * | |
2497 TypePointer::toCtype() | |
2498 { | |
2499 return 0; | |
2500 } | |
2501 | |
2502 type * | |
2503 TypeDelegate::toCtype() | |
2504 { | |
2505 return 0; | |
2506 } | |
2507 | |
2508 type * | |
2509 TypeClass::toCtype() | |
2510 { | |
2511 return 0; | |
2512 } | |
2513 | |
2514 void | |
2515 ClassDeclaration::toDebug() | |
2516 { | |
2517 } | |
2518 | |
14
0e86428ee567
[svn r18] * Initial support for switch statements - No string switches yet.
lindquist
parents:
11
diff
changeset
|
2519 ////////////////////////////////////////////////////////////////////////////// |
1 | 2520 |
2521 void | |
2522 EnumDeclaration::toDebug() | |
2523 { | |
2524 | |
2525 } | |
2526 | |
2527 int | |
2528 Dsymbol::cvMember(unsigned char*) | |
2529 { | |
2530 return 0; | |
2531 } | |
2532 int | |
2533 EnumDeclaration::cvMember(unsigned char*) | |
2534 { | |
2535 return 0; | |
2536 } | |
2537 int | |
2538 FuncDeclaration::cvMember(unsigned char*) | |
2539 { | |
2540 return 0; | |
2541 } | |
2542 int | |
2543 VarDeclaration::cvMember(unsigned char*) | |
2544 { | |
2545 return 0; | |
2546 } | |
2547 int | |
2548 TypedefDeclaration::cvMember(unsigned char*) | |
2549 { | |
2550 return 0; | |
2551 } | |
2552 | |
2553 void obj_includelib(char*){} | |
2554 | |
2555 AsmStatement::AsmStatement(Loc loc, Token *tokens) : | |
2556 Statement(loc) | |
2557 { | |
2558 } | |
2559 Statement *AsmStatement::syntaxCopy() { | |
2560 return 0; | |
2561 } | |
2562 | |
2563 Statement *AsmStatement::semantic(Scope *sc) | |
2564 { | |
2565 return Statement::semantic(sc); | |
2566 } | |
2567 | |
2568 | |
2569 void AsmStatement::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2570 { | |
2571 Statement::toCBuffer(buf, hgs); | |
2572 } | |
2573 | |
2574 int AsmStatement::comeFrom() | |
2575 { | |
2576 return FALSE; | |
2577 } | |
2578 | |
2579 void | |
2580 backend_init() | |
2581 { | |
2582 //LLVM_D_InitRuntime(); | |
2583 // lazily loaded | |
2584 } | |
2585 | |
2586 void | |
2587 backend_term() | |
2588 { | |
2589 LLVM_D_FreeRuntime(); | |
2590 } |