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