Mercurial > projects > ldc
annotate gen/tollvm.c @ 62:b86e00b938a5 trunk
[svn r66] Added support for imaginary floating point types
author | lindquist |
---|---|
date | Thu, 25 Oct 2007 09:48:36 +0200 |
parents | 2c3cd3596187 |
children | f918f3e2e99e |
rev | line source |
---|---|
1 | 1 #include <iostream> |
2 | |
40 | 3 #include "gen/llvm.h" |
1 | 4 |
5 #include "mtype.h" | |
6 #include "dsymbol.h" | |
7 #include "aggregate.h" | |
8 #include "declaration.h" | |
9 #include "init.h" | |
10 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
11 #include "gen/tollvm.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
12 #include "gen/irstate.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
13 #include "gen/logger.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
14 #include "gen/runtime.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
15 #include "gen/elem.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
16 #include "gen/arrays.h" |
1 | 17 |
40 | 18 bool LLVM_DtoIsPassedByRef(Type* type) |
19 { | |
20 TY t = type->ty; | |
21 if (t == Tstruct || t == Tarray || t == Tdelegate) | |
22 return true; | |
23 else if (t == Ttypedef) { | |
24 Type* bt = type->toBasetype(); | |
25 assert(bt); | |
26 return LLVM_DtoIsPassedByRef(bt); | |
27 } | |
28 return false; | |
29 } | |
30 | |
31 Type* LLVM_DtoDType(Type* t) | |
32 { | |
33 if (t->ty == Ttypedef) { | |
34 Type* bt = t->toBasetype(); | |
35 assert(bt); | |
36 return LLVM_DtoDType(bt); | |
37 } | |
38 return t; | |
39 } | |
40 | |
1 | 41 const llvm::Type* LLVM_DtoType(Type* t) |
42 { | |
43 assert(t); | |
44 switch (t->ty) | |
45 { | |
46 // integers | |
47 case Tint8: | |
48 case Tuns8: | |
49 case Tchar: | |
50 return (const llvm::Type*)llvm::Type::Int8Ty; | |
51 case Tint16: | |
52 case Tuns16: | |
53 case Twchar: | |
54 return (const llvm::Type*)llvm::Type::Int16Ty; | |
55 case Tint32: | |
56 case Tuns32: | |
57 case Tdchar: | |
58 return (const llvm::Type*)llvm::Type::Int32Ty; | |
59 case Tint64: | |
60 case Tuns64: | |
61 return (const llvm::Type*)llvm::Type::Int64Ty; | |
62 | |
63 case Tbool: | |
64 return (const llvm::Type*)llvm::ConstantInt::getTrue()->getType(); | |
65 | |
66 // floats | |
67 case Tfloat32: | |
62
b86e00b938a5
[svn r66] Added support for imaginary floating point types
lindquist
parents:
58
diff
changeset
|
68 case Timaginary32: |
1 | 69 return llvm::Type::FloatTy; |
70 case Tfloat64: | |
62
b86e00b938a5
[svn r66] Added support for imaginary floating point types
lindquist
parents:
58
diff
changeset
|
71 case Timaginary64: |
1 | 72 case Tfloat80: |
62
b86e00b938a5
[svn r66] Added support for imaginary floating point types
lindquist
parents:
58
diff
changeset
|
73 case Timaginary80: |
1 | 74 return llvm::Type::DoubleTy; |
75 | |
62
b86e00b938a5
[svn r66] Added support for imaginary floating point types
lindquist
parents:
58
diff
changeset
|
76 // complex |
b86e00b938a5
[svn r66] Added support for imaginary floating point types
lindquist
parents:
58
diff
changeset
|
77 case Tcomplex32: |
b86e00b938a5
[svn r66] Added support for imaginary floating point types
lindquist
parents:
58
diff
changeset
|
78 case Tcomplex64: |
b86e00b938a5
[svn r66] Added support for imaginary floating point types
lindquist
parents:
58
diff
changeset
|
79 case Tcomplex80: |
b86e00b938a5
[svn r66] Added support for imaginary floating point types
lindquist
parents:
58
diff
changeset
|
80 assert(0 && "complex number types not yet implemented"); |
b86e00b938a5
[svn r66] Added support for imaginary floating point types
lindquist
parents:
58
diff
changeset
|
81 |
1 | 82 // pointers |
83 case Tpointer: { | |
84 assert(t->next); | |
85 if (t->next->ty == Tvoid) | |
86 return (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
87 else | |
88 return (const llvm::Type*)llvm::PointerType::get(LLVM_DtoType(t->next)); | |
89 } | |
90 | |
91 // arrays | |
92 case Tarray: | |
93 return LLVM_DtoArrayType(t); | |
94 case Tsarray: | |
95 return LLVM_DtoStaticArrayType(t); | |
96 | |
97 // void | |
98 case Tvoid: | |
99 return llvm::Type::VoidTy; | |
100 | |
101 // aggregates | |
102 case Tstruct: { | |
103 if (t->llvmType == 0) | |
104 { | |
105 // recursive or cyclic declaration | |
106 if (!gIR->structs.empty()) | |
107 { | |
108 IRStruct* found = 0; | |
109 for (IRState::StructVector::iterator i=gIR->structs.begin(); i!=gIR->structs.end(); ++i) | |
110 { | |
111 if (t == i->type) | |
112 { | |
113 return i->recty.get(); | |
114 } | |
115 } | |
116 } | |
117 | |
118 // forward declaration | |
119 TypeStruct* ts = (TypeStruct*)t; | |
120 assert(ts->sym); | |
121 ts->sym->toObjFile(); | |
122 } | |
123 return t->llvmType; | |
124 } | |
125 | |
126 case Tclass: { | |
127 if (t->llvmType == 0) | |
128 { | |
6 | 129 // recursive or cyclic declaration |
130 if (!gIR->structs.empty()) | |
131 { | |
132 IRStruct* found = 0; | |
133 for (IRState::StructVector::iterator i=gIR->structs.begin(); i!=gIR->structs.end(); ++i) | |
134 { | |
135 if (t == i->type) | |
136 { | |
137 return llvm::PointerType::get(i->recty.get()); | |
138 } | |
139 } | |
140 } | |
141 | |
142 // forward declaration | |
1 | 143 TypeClass* tc = (TypeClass*)t; |
144 assert(tc->sym); | |
6 | 145 tc->sym->toObjFile(); |
1 | 146 } |
147 return llvm::PointerType::get(t->llvmType); | |
148 } | |
149 | |
150 // functions | |
151 case Tfunction: | |
152 { | |
153 if (t->llvmType == 0) { | |
154 return LLVM_DtoFunctionType(t); | |
155 } | |
156 else { | |
157 return t->llvmType; | |
158 } | |
159 } | |
6 | 160 |
1 | 161 // delegates |
162 case Tdelegate: | |
163 { | |
164 if (t->llvmType == 0) { | |
165 return LLVM_DtoDelegateType(t); | |
166 } | |
167 else { | |
168 return t->llvmType; | |
169 } | |
170 } | |
171 | |
172 // typedefs | |
52 | 173 // enum |
1 | 174 case Ttypedef: |
52 | 175 case Tenum: |
1 | 176 { |
177 Type* bt = t->toBasetype(); | |
178 assert(bt); | |
179 return LLVM_DtoType(bt); | |
180 } | |
181 | |
182 default: | |
183 printf("trying to convert unknown type with value %d\n", t->ty); | |
184 assert(0); | |
185 } | |
186 return 0; | |
187 } | |
188 | |
189 ////////////////////////////////////////////////////////////////////////////////////////// | |
190 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
191 const llvm::FunctionType* LLVM_DtoFunctionType(Type* t, const llvm::Type* thisparam) |
1 | 192 { |
193 TypeFunction* f = (TypeFunction*)t; | |
194 | |
195 // parameter types | |
196 const llvm::Type* rettype; | |
197 std::vector<const llvm::Type*> paramvec; | |
198 | |
40 | 199 if (LLVM_DtoIsPassedByRef(f->next)) { |
1 | 200 rettype = llvm::PointerType::get(LLVM_DtoType(f->next)); |
201 paramvec.push_back(rettype); | |
202 rettype = llvm::Type::VoidTy; | |
203 } | |
204 else { | |
205 Type* rt = f->next; | |
206 if (rt) | |
207 rettype = LLVM_DtoType(rt); | |
208 else | |
209 assert(0); | |
210 } | |
211 | |
212 if (thisparam) { | |
213 paramvec.push_back(thisparam); | |
214 } | |
215 | |
216 size_t n = Argument::dim(f->parameters); | |
217 for (int i=0; i < n; ++i) { | |
218 Argument* arg = Argument::getNth(f->parameters, i); | |
219 // ensure scalar | |
220 Type* argT = arg->type; | |
221 assert(argT); | |
222 paramvec.push_back(LLVM_DtoType(argT)); | |
223 } | |
224 | |
225 Logger::cout() << "Return type: " << *rettype << '\n'; | |
226 | |
227 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, paramvec, f->varargs); | |
228 return functype; | |
229 } | |
230 | |
231 ////////////////////////////////////////////////////////////////////////////////////////// | |
232 | |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
233 static const llvm::FunctionType* LLVM_DtoVaFunctionType(FuncDeclaration* fdecl) |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
234 { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
235 TypeFunction* f = (TypeFunction*)fdecl->type; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
236 assert(f != 0); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
237 |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
238 const llvm::PointerType* i8pty = llvm::PointerType::get(llvm::Type::Int8Ty); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
239 std::vector<const llvm::Type*> args; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
240 |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
241 if (fdecl->llvmInternal == LLVMva_start) { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
242 args.push_back(i8pty); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
243 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
244 else if (fdecl->llvmInternal == LLVMva_intrinsic) { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
245 size_t n = Argument::dim(f->parameters); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
246 for (size_t i=0; i<n; ++i) { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
247 args.push_back(i8pty); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
248 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
249 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
250 else |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
251 assert(0); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
252 |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
253 const llvm::FunctionType* fty = llvm::FunctionType::get(llvm::Type::VoidTy, args, false); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
254 f->llvmType = fty; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
255 return fty; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
256 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
257 |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
258 ////////////////////////////////////////////////////////////////////////////////////////// |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
259 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
260 const llvm::FunctionType* LLVM_DtoFunctionType(FuncDeclaration* fdecl) |
1 | 261 { |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
262 if ((fdecl->llvmInternal == LLVMva_start) || (fdecl->llvmInternal == LLVMva_intrinsic)) { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
263 return LLVM_DtoVaFunctionType(fdecl); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
264 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
265 |
1 | 266 TypeFunction* f = (TypeFunction*)fdecl->type; |
267 assert(f != 0); | |
268 | |
9 | 269 // type has already been resolved |
1 | 270 if (f->llvmType != 0) { |
271 return llvm::cast<llvm::FunctionType>(f->llvmType); | |
272 } | |
273 | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
274 bool typesafeVararg = false; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
275 if (f->linkage == LINKd && f->varargs == 1) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
276 assert(fdecl->v_arguments); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
277 Logger::println("v_arguments = %s", fdecl->v_arguments->toChars()); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
278 assert(fdecl->v_arguments->isParameter()); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
279 typesafeVararg = true; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
280 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
281 |
1 | 282 // return value type |
283 const llvm::Type* rettype; | |
284 const llvm::Type* actualRettype; | |
285 Type* rt = f->next; | |
286 bool retinptr = false; | |
287 bool usesthis = false; | |
288 | |
289 if (fdecl->isMain()) { | |
290 rettype = llvm::Type::Int32Ty; | |
291 actualRettype = rettype; | |
292 } | |
293 else if (rt) { | |
40 | 294 if (LLVM_DtoIsPassedByRef(rt)) { |
1 | 295 rettype = llvm::PointerType::get(LLVM_DtoType(rt)); |
296 actualRettype = llvm::Type::VoidTy; | |
297 f->llvmRetInPtr = retinptr = true; | |
298 } | |
299 else { | |
300 rettype = LLVM_DtoType(rt); | |
301 actualRettype = rettype; | |
302 } | |
303 } | |
304 else { | |
305 assert(0); | |
306 } | |
307 | |
308 // parameter types | |
309 std::vector<const llvm::Type*> paramvec; | |
310 | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
311 if (retinptr) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
312 Logger::cout() << "returning through pointer parameter: " << *rettype << '\n'; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
313 paramvec.push_back(rettype); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
314 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
315 |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
316 if (fdecl->needThis()) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
317 if (AggregateDeclaration* ad = fdecl->isMember()) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
318 Logger::print("isMember = this is: %s\n", ad->type->toChars()); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
319 const llvm::Type* thisty = LLVM_DtoType(ad->type); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
320 Logger::cout() << "this llvm type: " << *thisty << '\n'; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
321 if (llvm::isa<llvm::StructType>(thisty) || thisty == gIR->topstruct().recty.get()) |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
322 thisty = llvm::PointerType::get(thisty); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
323 paramvec.push_back(thisty); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
324 usesthis = true; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
325 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
326 else |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
327 assert(0); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
328 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
329 else if (fdecl->isNested()) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
330 paramvec.push_back(llvm::PointerType::get(llvm::Type::Int8Ty)); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
331 usesthis = true; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
332 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
333 |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
334 if (typesafeVararg) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
335 ClassDeclaration* ti = Type::typeinfo; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
336 if (!ti->llvmInitZ) |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
337 ti->toObjFile(); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
338 assert(ti->llvmInitZ); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
339 std::vector<const llvm::Type*> types; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
340 types.push_back(LLVM_DtoSize_t()); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
341 types.push_back(llvm::PointerType::get(llvm::PointerType::get(ti->llvmInitZ->getType()))); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
342 const llvm::Type* t1 = llvm::StructType::get(types); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
343 paramvec.push_back(llvm::PointerType::get(t1)); |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
344 paramvec.push_back(llvm::PointerType::get(llvm::Type::Int8Ty)); |
1 | 345 } |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
346 |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
347 size_t n = Argument::dim(f->parameters); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
348 |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
349 for (int i=0; i < n; ++i) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
350 Argument* arg = Argument::getNth(f->parameters, i); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
351 // ensure scalar |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
352 Type* argT = LLVM_DtoDType(arg->type); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
353 assert(argT); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
354 |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
355 if ((arg->storageClass & STCref) || (arg->storageClass & STCout)) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
356 //assert(arg->vardecl); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
357 //arg->vardecl->refparam = true; |
9 | 358 } |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
359 else |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
360 arg->llvmCopy = true; |
1 | 361 |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
362 const llvm::Type* at = LLVM_DtoType(argT); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
363 if (llvm::isa<llvm::StructType>(at)) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
364 Logger::println("struct param"); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
365 paramvec.push_back(llvm::PointerType::get(at)); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
366 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
367 else if (llvm::isa<llvm::ArrayType>(at)) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
368 Logger::println("sarray param"); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
369 assert(argT->ty == Tsarray); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
370 //paramvec.push_back(llvm::PointerType::get(at->getContainedType(0))); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
371 paramvec.push_back(llvm::PointerType::get(at)); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
372 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
373 else if (llvm::isa<llvm::OpaqueType>(at)) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
374 Logger::println("opaque param"); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
375 if (argT->ty == Tstruct || argT->ty == Tclass) |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
376 paramvec.push_back(llvm::PointerType::get(at)); |
40 | 377 else |
378 assert(0); | |
379 } | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
380 /*if (llvm::isa<llvm::StructType>(at) || argT->ty == Tstruct || argT->ty == Tsarray) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
381 paramvec.push_back(llvm::PointerType::get(at)); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
382 }*/ |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
383 else { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
384 if (!arg->llvmCopy) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
385 Logger::println("ref param"); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
386 at = llvm::PointerType::get(at); |
1 | 387 } |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
388 else { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
389 Logger::println("in param"); |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
390 } |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
391 paramvec.push_back(at); |
1 | 392 } |
393 } | |
394 | |
395 // construct function type | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
396 bool isvararg = !typesafeVararg && f->varargs; |
1 | 397 llvm::FunctionType* functype = llvm::FunctionType::get(actualRettype, paramvec, isvararg); |
398 | |
399 f->llvmType = functype; | |
9 | 400 f->llvmRetInPtr = retinptr; |
401 f->llvmUsesThis = usesthis; | |
1 | 402 return functype; |
403 } | |
404 | |
405 ////////////////////////////////////////////////////////////////////////////////////////// | |
406 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
407 const llvm::StructType* LLVM_DtoDelegateType(Type* t) |
1 | 408 { |
409 const llvm::Type* i8ptr = llvm::PointerType::get(llvm::Type::Int8Ty); | |
410 const llvm::Type* func = LLVM_DtoFunctionType(t->next, i8ptr); | |
411 const llvm::Type* funcptr = llvm::PointerType::get(func); | |
412 | |
413 std::vector<const llvm::Type*> types; | |
414 types.push_back(i8ptr); | |
415 types.push_back(funcptr); | |
416 return llvm::StructType::get(types); | |
417 } | |
418 | |
419 ////////////////////////////////////////////////////////////////////////////////////////// | |
420 | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
421 const llvm::Type* LLVM_DtoStructType(Type* t) |
1 | 422 { |
423 assert(0); | |
424 std::vector<const llvm::Type*> types; | |
425 return llvm::StructType::get(types); | |
426 } | |
427 | |
428 | |
429 ////////////////////////////////////////////////////////////////////////////////////////// | |
430 | |
431 static llvm::Function* LLVM_DeclareMemIntrinsic(const char* name, int bits, bool set=false) | |
432 { | |
433 assert(bits == 32 || bits == 64); | |
434 const llvm::Type* int8ty = (const llvm::Type*)llvm::Type::Int8Ty; | |
435 const llvm::Type* int32ty = (const llvm::Type*)llvm::Type::Int32Ty; | |
436 const llvm::Type* int64ty = (const llvm::Type*)llvm::Type::Int64Ty; | |
437 const llvm::Type* int8ptrty = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
438 const llvm::Type* voidty = (const llvm::Type*)llvm::Type::VoidTy; | |
439 | |
440 assert(gIR); | |
441 assert(gIR->module); | |
442 | |
443 // parameter types | |
444 std::vector<const llvm::Type*> pvec; | |
445 pvec.push_back(int8ptrty); | |
446 pvec.push_back(set?int8ty:int8ptrty); | |
447 pvec.push_back(bits==32?int32ty:int64ty); | |
448 pvec.push_back(int32ty); | |
449 llvm::FunctionType* functype = llvm::FunctionType::get(voidty, pvec, false); | |
450 return new llvm::Function(functype, llvm::GlobalValue::ExternalLinkage, name, gIR->module); | |
451 } | |
452 | |
453 ////////////////////////////////////////////////////////////////////////////////////////// | |
454 | |
455 // llvm.memset.i32 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
456 llvm::Function* LLVM_DeclareMemSet32() |
1 | 457 { |
458 static llvm::Function* _func = 0; | |
459 if (_func == 0) { | |
460 _func = LLVM_DeclareMemIntrinsic("llvm.memset.i32", 32, true); | |
461 } | |
462 return _func; | |
463 } | |
464 | |
465 ////////////////////////////////////////////////////////////////////////////////////////// | |
466 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
467 llvm::Function* LLVM_DeclareMemSet64() |
1 | 468 { |
469 static llvm::Function* _func = 0; | |
470 if (_func == 0) { | |
471 _func = LLVM_DeclareMemIntrinsic("llvm.memset.i64", 64, true); | |
472 } | |
473 return _func; | |
474 } | |
475 | |
476 ////////////////////////////////////////////////////////////////////////////////////////// | |
477 | |
478 // llvm.memcpy.i32 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
479 llvm::Function* LLVM_DeclareMemCpy32() |
1 | 480 { |
481 static llvm::Function* _func = 0; | |
482 if (_func == 0) { | |
483 _func = LLVM_DeclareMemIntrinsic("llvm.memcpy.i32", 32); | |
484 } | |
485 return _func; | |
486 } | |
487 | |
488 ////////////////////////////////////////////////////////////////////////////////////////// | |
489 | |
490 // llvm.memcpy.i64 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
491 llvm::Function* LLVM_DeclareMemCpy64() |
1 | 492 { |
493 static llvm::Function* _func = 0; | |
494 if (_func == 0) { | |
495 _func = LLVM_DeclareMemIntrinsic("llvm.memcpy.i64", 64); | |
496 } | |
497 return _func; | |
498 } | |
499 | |
500 ////////////////////////////////////////////////////////////////////////////////////////// | |
501 | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
502 llvm::Value* LLVM_DtoStructZeroInit(llvm::Value* v) |
1 | 503 { |
504 assert(gIR); | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
505 uint64_t n = gTargetData->getTypeSize(v->getType()->getContainedType(0)); |
1 | 506 //llvm::Type* sarrty = llvm::PointerType::get(llvm::ArrayType::get(llvm::Type::Int8Ty, n)); |
507 llvm::Type* sarrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
508 | |
509 llvm::Value* sarr = new llvm::BitCastInst(v,sarrty,"tmp",gIR->scopebb()); | |
510 | |
511 llvm::Function* fn = LLVM_DeclareMemSet32(); | |
512 std::vector<llvm::Value*> llargs; | |
513 llargs.resize(4); | |
514 llargs[0] = sarr; | |
515 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false); | |
516 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
517 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
518 | |
519 llvm::Value* ret = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
520 | |
521 return ret; | |
522 } | |
523 | |
524 ////////////////////////////////////////////////////////////////////////////////////////// | |
525 | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
526 llvm::Value* LLVM_DtoStructCopy(llvm::Value* dst, llvm::Value* src) |
1 | 527 { |
528 assert(dst->getType() == src->getType()); | |
529 assert(gIR); | |
530 | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
531 uint64_t n = gTargetData->getTypeSize(dst->getType()->getContainedType(0)); |
1 | 532 //llvm::Type* sarrty = llvm::PointerType::get(llvm::ArrayType::get(llvm::Type::Int8Ty, n)); |
533 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
534 | |
535 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); | |
536 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); | |
537 | |
538 llvm::Function* fn = LLVM_DeclareMemCpy32(); | |
539 std::vector<llvm::Value*> llargs; | |
540 llargs.resize(4); | |
541 llargs[0] = dstarr; | |
542 llargs[1] = srcarr; | |
543 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
544 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
545 | |
546 return new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
547 } | |
548 | |
549 ////////////////////////////////////////////////////////////////////////////////////////// | |
40 | 550 llvm::Constant* LLVM_DtoConstStructInitializer(StructInitializer* si) |
1 | 551 { |
552 llvm::StructType* structtype = llvm::cast<llvm::StructType>(si->ad->llvmType); | |
553 size_t n = structtype->getNumElements(); | |
554 | |
555 assert(si->value.dim == si->vars.dim); | |
556 | |
557 std::vector<llvm::Constant*> inits; | |
558 inits.resize(n, NULL); | |
559 for (int i = 0; i < si->value.dim; ++i) | |
560 { | |
561 Initializer* ini = (Initializer*)si->value.data[i]; | |
562 assert(ini); | |
563 | |
564 VarDeclaration* vd = (VarDeclaration*)si->vars.data[i]; | |
40 | 565 Type* vdtype = LLVM_DtoDType(vd->type); |
1 | 566 assert(vd); |
567 Logger::println("vars[%d] = %s", i, vd->toChars()); | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
568 |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
569 std::vector<unsigned> idxs; |
40 | 570 si->ad->offsetToIndex(vdtype, vd->offset, idxs); |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
571 assert(idxs.size() == 1); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
572 unsigned idx = idxs[0]; |
1 | 573 |
574 llvm::Constant* v = 0; | |
575 | |
576 if (ExpInitializer* ex = ini->isExpInitializer()) | |
577 { | |
40 | 578 v = ex->exp->toConstElem(gIR); |
1 | 579 } |
580 else if (StructInitializer* si = ini->isStructInitializer()) | |
581 { | |
40 | 582 v = LLVM_DtoConstStructInitializer(si); |
1 | 583 } |
584 else if (ArrayInitializer* ai = ini->isArrayInitializer()) | |
585 { | |
40 | 586 v = LLVM_DtoConstArrayInitializer(ai); |
1 | 587 } |
588 else if (ini->isVoidInitializer()) | |
589 { | |
590 v = llvm::UndefValue::get(structtype->getElementType(idx)); | |
591 } | |
592 else | |
593 assert(v); | |
594 | |
595 inits[idx] = v; | |
596 } | |
597 | |
598 // fill out nulls | |
599 assert(si->ad->llvmInitZ); | |
600 if (si->ad->llvmInitZ->isNullValue()) | |
601 { | |
602 for (int i = 0; i < n; ++i) | |
603 { | |
604 if (inits[i] == 0) | |
605 { | |
606 inits[i] = llvm::Constant::getNullValue(structtype->getElementType(i)); | |
607 } | |
608 } | |
609 } | |
610 else | |
611 { | |
612 for (int i = 0; i < n; ++i) | |
613 { | |
614 if (inits[i] == 0) | |
615 { | |
616 inits[i] = si->ad->llvmInitZ->getOperand(i); | |
617 } | |
618 } | |
619 } | |
620 | |
621 return llvm::ConstantStruct::get(structtype, inits); | |
622 } | |
623 | |
624 | |
625 | |
626 ////////////////////////////////////////////////////////////////////////////////////////// | |
627 | |
628 llvm::Value* LLVM_DtoNullDelegate(llvm::Value* v) | |
629 { | |
630 assert(gIR); | |
631 d_uns64 n = (global.params.is64bit) ? 16 : 8; | |
632 | |
633 llvm::Type* i8p_ty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
634 | |
635 llvm::Value* arr = new llvm::BitCastInst(v,i8p_ty,"tmp",gIR->scopebb()); | |
636 | |
637 llvm::Function* fn = LLVM_DeclareMemSet32(); | |
638 std::vector<llvm::Value*> llargs; | |
639 llargs.resize(4); | |
640 llargs[0] = arr; | |
641 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false); | |
642 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
643 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
644 | |
645 llvm::Value* ret = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
646 | |
647 return ret; | |
648 } | |
649 | |
650 ////////////////////////////////////////////////////////////////////////////////////////// | |
651 | |
652 llvm::Value* LLVM_DtoDelegateCopy(llvm::Value* dst, llvm::Value* src) | |
653 { | |
654 assert(dst->getType() == src->getType()); | |
655 assert(gIR); | |
656 | |
657 d_uns64 n = (global.params.is64bit) ? 16 : 8; | |
658 | |
659 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
660 | |
661 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); | |
662 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); | |
663 | |
664 llvm::Function* fn = LLVM_DeclareMemCpy32(); | |
665 std::vector<llvm::Value*> llargs; | |
666 llargs.resize(4); | |
667 llargs[0] = dstarr; | |
668 llargs[1] = srcarr; | |
669 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); | |
670 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | |
671 | |
672 return new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
673 } | |
674 | |
53
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
675 ////////////////////////////////////////////////////////////////////////////////////////// |
1 | 676 |
53
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
677 llvm::Value* LLVM_DtoCompareDelegate(TOK op, llvm::Value* lhs, llvm::Value* rhs) |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
678 { |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
679 llvm::ICmpInst::Predicate pred = (op == TOKequal) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE; |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
680 llvm::Value* l = gIR->ir->CreateLoad(LLVM_DtoGEPi(lhs,0,0,"tmp"),"tmp"); |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
681 llvm::Value* r = gIR->ir->CreateLoad(LLVM_DtoGEPi(rhs,0,0,"tmp"),"tmp"); |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
682 llvm::Value* b1 = gIR->ir->CreateICmp(pred,l,r,"tmp"); |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
683 l = gIR->ir->CreateLoad(LLVM_DtoGEPi(lhs,0,1,"tmp"),"tmp"); |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
684 r = gIR->ir->CreateLoad(LLVM_DtoGEPi(rhs,0,1,"tmp"),"tmp"); |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
685 llvm::Value* b2 = gIR->ir->CreateICmp(pred,l,r,"tmp"); |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
686 llvm::Value* b = gIR->ir->CreateAnd(b1,b2,"tmp"); |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
687 if (op == TOKnotequal) |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
688 return gIR->ir->CreateNot(b,"tmp"); |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
689 return b; |
06ccc817acd4
[svn r57] Added most basic TypeInfo (rebuild lphobos).
lindquist
parents:
52
diff
changeset
|
690 } |
1 | 691 |
692 ////////////////////////////////////////////////////////////////////////////////////////// | |
693 | |
694 llvm::GlobalValue::LinkageTypes LLVM_DtoLinkage(PROT prot, uint stc) | |
695 { | |
696 switch(prot) | |
697 { | |
698 case PROTprivate: | |
699 return llvm::GlobalValue::InternalLinkage; | |
700 | |
701 case PROTpublic: | |
702 case PROTpackage: | |
703 case PROTprotected: | |
704 case PROTexport: | |
705 return llvm::GlobalValue::ExternalLinkage; | |
706 | |
707 case PROTundefined: | |
708 case PROTnone: | |
709 assert(0 && "Unsupported linkage type"); | |
710 } | |
711 return llvm::GlobalValue::ExternalLinkage; | |
712 | |
713 /* ExternalLinkage = 0, LinkOnceLinkage, WeakLinkage, AppendingLinkage, | |
714 InternalLinkage, DLLImportLinkage, DLLExportLinkage, ExternalWeakLinkage, | |
715 GhostLinkage */ | |
716 } | |
717 | |
718 ////////////////////////////////////////////////////////////////////////////////////////// | |
719 | |
720 unsigned LLVM_DtoCallingConv(LINK l) | |
721 { | |
722 if (l == LINKc) | |
723 return llvm::CallingConv::C; | |
724 else if (l == LINKd || l == LINKdefault) | |
725 return llvm::CallingConv::Fast; | |
726 else if (l == LINKwindows) | |
727 return llvm::CallingConv::X86_StdCall; | |
728 else | |
729 assert(0 && "Unsupported calling convention"); | |
730 } | |
731 | |
732 ////////////////////////////////////////////////////////////////////////////////////////// | |
733 | |
734 llvm::Value* LLVM_DtoPointedType(llvm::Value* ptr, llvm::Value* val) | |
735 { | |
736 const llvm::Type* ptrTy = ptr->getType()->getContainedType(0); | |
737 const llvm::Type* valTy = val->getType(); | |
738 // ptr points to val's type | |
739 if (ptrTy == valTy) | |
740 { | |
741 return val; | |
742 } | |
743 // ptr is integer pointer | |
744 else if (ptrTy->isInteger()) | |
745 { | |
746 // val is integer | |
747 assert(valTy->isInteger()); | |
748 const llvm::IntegerType* pt = llvm::cast<const llvm::IntegerType>(ptrTy); | |
749 const llvm::IntegerType* vt = llvm::cast<const llvm::IntegerType>(valTy); | |
750 if (pt->getBitWidth() < vt->getBitWidth()) { | |
751 return new llvm::TruncInst(val, pt, "tmp", gIR->scopebb()); | |
752 } | |
753 else | |
754 assert(0); | |
755 } | |
756 // something else unsupported | |
757 else | |
758 { | |
759 Logger::cout() << *ptrTy << '|' << *valTy << '\n'; | |
760 assert(0); | |
761 } | |
762 return 0; | |
763 } | |
764 | |
765 ////////////////////////////////////////////////////////////////////////////////////////// | |
766 | |
767 llvm::Value* LLVM_DtoBoolean(llvm::Value* val) | |
768 { | |
769 const llvm::Type* t = val->getType(); | |
770 if (t->isInteger()) | |
771 { | |
772 if (t == llvm::Type::Int1Ty) | |
773 return val; | |
774 else { | |
775 llvm::Value* zero = llvm::ConstantInt::get(t, 0, false); | |
776 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb()); | |
777 } | |
778 } | |
779 else if (llvm::isa<llvm::PointerType>(t)) { | |
780 const llvm::Type* st = LLVM_DtoSize_t(); | |
781 llvm::Value* ptrasint = new llvm::PtrToIntInst(val,st,"tmp",gIR->scopebb()); | |
782 llvm::Value* zero = llvm::ConstantInt::get(st, 0, false); | |
783 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, ptrasint, zero, "tmp", gIR->scopebb()); | |
784 } | |
785 else | |
786 { | |
787 Logger::cout() << *t << '\n'; | |
788 } | |
789 assert(0); | |
790 return 0; | |
791 } | |
792 | |
793 ////////////////////////////////////////////////////////////////////////////////////////// | |
794 | |
795 const llvm::Type* LLVM_DtoSize_t() | |
796 { | |
797 if (global.params.is64bit) | |
798 return llvm::Type::Int64Ty; | |
799 else | |
800 return llvm::Type::Int32Ty; | |
801 } | |
802 | |
803 ////////////////////////////////////////////////////////////////////////////////////////// | |
804 | |
805 void LLVM_DtoMain() | |
806 { | |
807 // emit main function llvm style | |
808 // int main(int argc, char**argv, char**env); | |
809 | |
810 assert(gIR != 0); | |
811 IRState& ir = *gIR; | |
812 | |
813 assert(ir.emitMain && ir.mainFunc); | |
814 | |
815 // parameter types | |
816 std::vector<const llvm::Type*> pvec; | |
817 pvec.push_back((const llvm::Type*)llvm::Type::Int32Ty); | |
818 const llvm::Type* chPtrType = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
819 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
820 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
821 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; | |
822 | |
823 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
824 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
825 | |
826 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
827 | |
828 // call static ctors | |
829 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_ctors"); | |
830 new llvm::CallInst(fn,"",bb); | |
831 | |
832 // call user main function | |
833 llvm::CallInst* call = new llvm::CallInst(ir.mainFunc,"ret",bb); | |
834 call->setCallingConv(ir.mainFunc->getCallingConv()); | |
835 | |
836 // call static dtors | |
837 fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_dtors"); | |
838 new llvm::CallInst(fn,"",bb); | |
839 | |
840 // return | |
841 new llvm::ReturnInst(call,bb); | |
842 } | |
843 | |
844 ////////////////////////////////////////////////////////////////////////////////////////// | |
845 | |
846 void LLVM_DtoCallClassDtors(TypeClass* tc, llvm::Value* instance) | |
847 { | |
848 Array* arr = &tc->sym->dtors; | |
849 for (size_t i=0; i<arr->dim; i++) | |
850 { | |
851 FuncDeclaration* fd = (FuncDeclaration*)arr->data[i]; | |
852 assert(fd->llvmValue); | |
853 new llvm::CallInst(fd->llvmValue, instance, "", gIR->scopebb()); | |
854 } | |
855 } | |
856 | |
857 ////////////////////////////////////////////////////////////////////////////////////////// | |
858 | |
859 void LLVM_DtoInitClass(TypeClass* tc, llvm::Value* dst) | |
860 { | |
861 assert(gIR); | |
862 | |
863 assert(tc->llvmType); | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
864 uint64_t size_t_size = gTargetData->getTypeSize(LLVM_DtoSize_t()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
865 uint64_t n = gTargetData->getTypeSize(tc->llvmType) - size_t_size; |
1 | 866 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
867 // set vtable field |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
868 llvm::Value* vtblvar = LLVM_DtoGEPi(dst,0,0,"tmp",gIR->scopebb()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
869 assert(tc->sym->llvmVtbl); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
870 new llvm::StoreInst(tc->sym->llvmVtbl, vtblvar, gIR->scopebb()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
871 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
872 // copy the static initializer |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
873 if (n > 0) { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
874 assert(tc->llvmInit); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
875 assert(dst->getType() == tc->llvmInit->getType()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
876 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
877 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); |
1 | 878 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
879 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
880 dstarr = LLVM_DtoGEPi(dstarr,size_t_size,"tmp",gIR->scopebb()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
881 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
882 llvm::Value* srcarr = new llvm::BitCastInst(tc->llvmInit,arrty,"tmp",gIR->scopebb()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
883 srcarr = LLVM_DtoGEPi(srcarr,size_t_size,"tmp",gIR->scopebb()); |
1 | 884 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
885 llvm::Function* fn = LLVM_DeclareMemCpy32(); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
886 std::vector<llvm::Value*> llargs; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
887 llargs.resize(4); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
888 llargs[0] = dstarr; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
889 llargs[1] = srcarr; |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
890 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
891 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
892 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
893 new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
894 } |
1 | 895 } |
896 | |
897 ////////////////////////////////////////////////////////////////////////////////////////// | |
898 | |
40 | 899 llvm::Constant* LLVM_DtoConstInitializer(Type* type, Initializer* init) |
1 | 900 { |
21
8d45266bbabe
[svn r25] * Fixed a lot of problems with string literals
lindquist
parents:
18
diff
changeset
|
901 llvm::Constant* _init = 0; // may return zero |
1 | 902 if (!init) |
903 { | |
40 | 904 Logger::println("const default initializer for %s", type->toChars()); |
905 _init = type->defaultInit()->toConstElem(gIR); | |
1 | 906 } |
907 else if (ExpInitializer* ex = init->isExpInitializer()) | |
908 { | |
40 | 909 Logger::println("const expression initializer"); |
910 _init = ex->exp->toConstElem(gIR); | |
1 | 911 } |
912 else if (StructInitializer* si = init->isStructInitializer()) | |
913 { | |
40 | 914 Logger::println("const struct initializer"); |
915 _init = LLVM_DtoConstStructInitializer(si); | |
1 | 916 } |
917 else if (ArrayInitializer* ai = init->isArrayInitializer()) | |
918 { | |
40 | 919 Logger::println("const array initializer"); |
920 _init = LLVM_DtoConstArrayInitializer(ai); | |
1 | 921 } |
922 else if (init->isVoidInitializer()) | |
923 { | |
40 | 924 Logger::println("const void initializer"); |
1 | 925 const llvm::Type* ty = LLVM_DtoType(type); |
926 _init = llvm::Constant::getNullValue(ty); | |
927 } | |
928 else { | |
40 | 929 Logger::println("unsupported const initializer: %s", init->toChars()); |
930 } | |
931 return _init; | |
932 } | |
933 | |
934 ////////////////////////////////////////////////////////////////////////////////////////// | |
935 | |
936 void LLVM_DtoInitializer(Initializer* init) | |
937 { | |
938 if (ExpInitializer* ex = init->isExpInitializer()) | |
939 { | |
940 Logger::println("expression initializer"); | |
941 elem* e = ex->exp->toElem(gIR); | |
942 delete e; | |
943 } | |
944 else { | |
1 | 945 Logger::println("unsupported initializer: %s", init->toChars()); |
946 } | |
947 } | |
6 | 948 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
949 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
950 |
6 | 951 llvm::Value* LLVM_DtoGEP(llvm::Value* ptr, llvm::Value* i0, llvm::Value* i1, const std::string& var, llvm::BasicBlock* bb) |
952 { | |
953 std::vector<llvm::Value*> v(2); | |
954 v[0] = i0; | |
955 v[1] = i1; | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
956 Logger::cout() << "DtoGEP: " << *ptr << '\n'; |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
957 return new llvm::GetElementPtrInst(ptr, v.begin(), v.end(), var, bb?bb:gIR->scopebb()); |
6 | 958 } |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
959 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
960 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
961 |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
962 llvm::Value* LLVM_DtoGEP(llvm::Value* ptr, const std::vector<unsigned>& src, const std::string& var, llvm::BasicBlock* bb) |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
963 { |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
964 size_t n = src.size(); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
965 std::vector<llvm::Value*> dst(n); |
40 | 966 std::ostream& ostr = Logger::cout(); |
967 ostr << "indices for '" << *ptr << "':"; | |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
968 for (size_t i=0; i<n; ++i) |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
969 { |
40 | 970 ostr << ' ' << i; |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
971 dst[i] = llvm::ConstantInt::get(llvm::Type::Int32Ty, src[i], false); |
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
972 } |
40 | 973 ostr << '\n'; |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
974 return new llvm::GetElementPtrInst(ptr, dst.begin(), dst.end(), var, bb?bb:gIR->scopebb()); |
8
5e69b77a5c51
[svn r12] fixed accessing aggregate fields of aggregates
lindquist
parents:
6
diff
changeset
|
975 } |
9 | 976 |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
977 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
978 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
979 llvm::Value* LLVM_DtoGEPi(llvm::Value* ptr, unsigned i, const std::string& var, llvm::BasicBlock* bb) |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
980 { |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
981 return new llvm::GetElementPtrInst(ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false), var, bb?bb:gIR->scopebb()); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
982 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
983 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
984 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
985 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
986 llvm::Value* LLVM_DtoGEPi(llvm::Value* ptr, unsigned i0, unsigned i1, const std::string& var, llvm::BasicBlock* bb) |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
987 { |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
988 std::vector<llvm::Value*> v(2); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
989 v[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i0, false); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
990 v[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, i1, false); |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
991 return new llvm::GetElementPtrInst(ptr, v.begin(), v.end(), var, bb?bb:gIR->scopebb()); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
992 } |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
993 |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
994 ////////////////////////////////////////////////////////////////////////////////////////// |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
995 |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
996 static llvm::Function* LLVM_DtoDeclareVaFunction(FuncDeclaration* fdecl) |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
997 { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
998 TypeFunction* f = (TypeFunction*)LLVM_DtoDType(fdecl->type); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
999 const llvm::FunctionType* fty = LLVM_DtoVaFunctionType(fdecl); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1000 llvm::Constant* fn = 0; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1001 |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1002 if (fdecl->llvmInternal == LLVMva_start) { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1003 fn = gIR->module->getOrInsertFunction("llvm.va_start", fty); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1004 assert(fn); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1005 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1006 else if (fdecl->llvmInternal == LLVMva_intrinsic) { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1007 fn = gIR->module->getOrInsertFunction(fdecl->llvmInternal1, fty); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1008 assert(fn); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1009 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1010 else |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1011 assert(0); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1012 |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1013 llvm::Function* func = llvm::cast_or_null<llvm::Function>(fn); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1014 assert(func); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1015 assert(func->isIntrinsic()); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1016 fdecl->llvmValue = func; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1017 return func; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1018 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1019 |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1020 ////////////////////////////////////////////////////////////////////////////////////////// |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1021 |
9 | 1022 llvm::Function* LLVM_DtoDeclareFunction(FuncDeclaration* fdecl) |
1023 { | |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1024 if ((fdecl->llvmInternal == LLVMva_start) || (fdecl->llvmInternal == LLVMva_intrinsic)) { |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1025 return LLVM_DtoDeclareVaFunction(fdecl); |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1026 } |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1027 |
27
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1028 // mangled name |
55
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1029 char* mangled_name; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1030 if (fdecl->llvmInternal == LLVMintrinsic) |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1031 mangled_name = fdecl->llvmInternal1; |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1032 else |
0ccfae271c45
[svn r59] Added support for C-style variadic functions. Currently only works on x86, x86-64 va_arg is broken in LLVM 2.1. PPC and PPC64 unknown.
lindquist
parents:
54
diff
changeset
|
1033 mangled_name = fdecl->mangle(); |
27
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1034 |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1035 // unit test special handling |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1036 if (fdecl->isUnitTestDeclaration()) |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1037 { |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1038 assert(0 && "no unittests yet"); |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1039 /*const llvm::FunctionType* fnty = llvm::FunctionType::get(llvm::Type::VoidTy, std::vector<const llvm::Type*>(), false); |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1040 // make the function |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1041 llvm::Function* func = gIR->module->getFunction(mangled_name); |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1042 if (func == 0) |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1043 func = new llvm::Function(fnty,llvm::GlobalValue::InternalLinkage,mangled_name,gIR->module); |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1044 func->setCallingConv(llvm::CallingConv::Fast); |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1045 fdecl->llvmValue = func; |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1046 return func; |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1047 */ |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1048 } |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1049 |
92408a3a2bac
[svn r31] * Fixed returning through hidden pointer was unable to report back the return value
lindquist
parents:
24
diff
changeset
|
1050 // regular function |
40 | 1051 TypeFunction* f = (TypeFunction*)LLVM_DtoDType(fdecl->type); |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
1052 assert(f != 0); |
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
1053 |
9 | 1054 if (fdecl->llvmValue != 0) { |
18 | 1055 if (!llvm::isa<llvm::Function>(fdecl->llvmValue)) |
1056 { | |
1057 Logger::cout() << *fdecl->llvmValue << '\n'; | |
1058 assert(0); | |
1059 } | |
9 | 1060 return llvm::cast<llvm::Function>(fdecl->llvmValue); |
1061 } | |
1062 | |
18 | 1063 Logger::print("FuncDeclaration::toObjFile(%s): %s\n", fdecl->needThis()?"this":"static",fdecl->toChars()); |
9 | 1064 LOG_SCOPE; |
1065 | |
1066 if (fdecl->llvmInternal == LLVMintrinsic && fdecl->fbody) { | |
1067 error("intrinsics cannot have function bodies"); | |
1068 fatal(); | |
1069 } | |
1070 | |
1071 // construct function | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
1072 const llvm::FunctionType* functype = (f->llvmType == 0) ? LLVM_DtoFunctionType(fdecl) : llvm::cast<llvm::FunctionType>(f->llvmType); |
9 | 1073 |
1074 // make the function | |
1075 llvm::Function* func = gIR->module->getFunction(mangled_name); | |
1076 if (func == 0) { | |
1077 func = new llvm::Function(functype,LLVM_DtoLinkage(fdecl->protection, fdecl->storage_class),mangled_name,gIR->module); | |
1078 } | |
1079 | |
1080 if (fdecl->llvmInternal != LLVMintrinsic) | |
1081 func->setCallingConv(LLVM_DtoCallingConv(f->linkage)); | |
1082 | |
1083 fdecl->llvmValue = func; | |
1084 f->llvmType = functype; | |
11
d3ee9efe20e2
[svn r15] * Fixed a bunch problems with virtual calls. Seems I did some rather poor testing.
lindquist
parents:
9
diff
changeset
|
1085 assert(llvm::isa<llvm::FunctionType>(f->llvmType)); |
9 | 1086 |
1087 if (fdecl->isMain()) { | |
1088 gIR->mainFunc = func; | |
1089 } | |
1090 | |
1091 // name parameters | |
1092 llvm::Function::arg_iterator iarg = func->arg_begin(); | |
1093 int k = 0; | |
1094 if (f->llvmRetInPtr) { | |
1095 iarg->setName("retval"); | |
1096 f->llvmRetArg = iarg; | |
1097 ++iarg; | |
1098 } | |
1099 if (f->llvmUsesThis) { | |
1100 iarg->setName("this"); | |
1101 ++iarg; | |
1102 } | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1103 int varargs = -1; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1104 if (f->linkage == LINKd && f->varargs == 1) |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1105 varargs = 0; |
9 | 1106 for (; iarg != func->arg_end(); ++iarg) |
1107 { | |
1108 Argument* arg = Argument::getNth(f->parameters, k++); | |
1109 //arg->llvmValue = iarg; | |
1110 //printf("identifier: '%s' %p\n", arg->ident->toChars(), arg->ident); | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1111 if (arg && arg->ident != 0) { |
9 | 1112 if (arg->vardecl) { |
1113 arg->vardecl->llvmValue = iarg; | |
1114 } | |
1115 iarg->setName(arg->ident->toChars()); | |
1116 } | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1117 else if (!arg && varargs >= 0) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1118 if (varargs == 0) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1119 iarg->setName("_arguments"); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1120 fdecl->llvmArguments = iarg; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1121 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1122 else if (varargs == 1) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1123 iarg->setName("_argptr"); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1124 fdecl->llvmArgPtr = iarg; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1125 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1126 else |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1127 assert(0); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1128 varargs++; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1129 } |
9 | 1130 else { |
1131 iarg->setName("unnamed"); | |
1132 } | |
1133 } | |
1134 | |
1135 return func; | |
1136 } | |
29
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1137 |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1138 ////////////////////////////////////////////////////////////////////////////////////////// |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1139 |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1140 void LLVM_DtoGiveArgumentStorage(elem* l) |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1141 { |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1142 assert(l->mem == 0); |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1143 assert(l->val); |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1144 assert(llvm::isa<llvm::Argument>(l->val)); |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1145 assert(l->vardecl != 0); |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1146 |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1147 llvm::AllocaInst* allocainst = new llvm::AllocaInst(l->val->getType(), l->val->getName()+"_storage", gIR->topallocapoint()); |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1148 l->mem = allocainst; |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1149 l->vardecl->llvmValue = l->mem; |
253a5fc4033a
[svn r33] * Added support for assignment to function arguments
lindquist
parents:
27
diff
changeset
|
1150 } |
34 | 1151 |
1152 ////////////////////////////////////////////////////////////////////////////////////////// | |
1153 | |
1154 llvm::Value* LLVM_DtoRealloc(llvm::Value* ptr, const llvm::Type* ty) | |
1155 { | |
1156 /*size_t sz = gTargetData->getTypeSize(ty); | |
1157 llvm::ConstantInt* n = llvm::ConstantInt::get(LLVM_DtoSize_t(), sz, false); | |
1158 if (ptr == 0) { | |
1159 llvm::PointerType* i8pty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
1160 ptr = llvm::ConstantPointerNull::get(i8pty); | |
1161 } | |
1162 return LLVM_DtoRealloc(ptr, n);*/ | |
1163 return NULL; | |
1164 } | |
1165 | |
1166 ////////////////////////////////////////////////////////////////////////////////////////// | |
1167 | |
1168 llvm::Value* LLVM_DtoRealloc(llvm::Value* ptr, llvm::Value* n) | |
1169 { | |
1170 assert(ptr); | |
1171 assert(n); | |
1172 | |
1173 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_realloc"); | |
1174 assert(fn); | |
1175 | |
1176 llvm::Value* newptr = ptr; | |
1177 | |
1178 llvm::PointerType* i8pty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
1179 if (ptr->getType() != i8pty) { | |
1180 newptr = new llvm::BitCastInst(ptr,i8pty,"tmp",gIR->scopebb()); | |
1181 } | |
1182 | |
1183 std::vector<llvm::Value*> args; | |
1184 args.push_back(newptr); | |
1185 args.push_back(n); | |
1186 llvm::Value* ret = new llvm::CallInst(fn, args.begin(), args.end(), "tmprealloc", gIR->scopebb()); | |
1187 | |
1188 return ret->getType() == ptr->getType() ? ret : new llvm::BitCastInst(ret,ptr->getType(),"tmp",gIR->scopebb()); | |
1189 } | |
1190 | |
1191 ////////////////////////////////////////////////////////////////////////////////////////// | |
1192 | |
1193 void LLVM_DtoAssert(llvm::Value* cond, llvm::Value* loc, llvm::Value* msg) | |
1194 { | |
1195 assert(loc); | |
1196 std::vector<llvm::Value*> llargs; | |
1197 llargs.resize(3); | |
1198 llargs[0] = cond ? LLVM_DtoBoolean(cond) : llvm::ConstantInt::getFalse(); | |
1199 llargs[1] = loc; | |
1200 llargs[2] = msg ? msg : llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
1201 | |
1202 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_assert"); | |
1203 assert(fn); | |
1204 llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); | |
1205 call->setCallingConv(llvm::CallingConv::C); | |
1206 } | |
1207 | |
40 | 1208 ////////////////////////////////////////////////////////////////////////////////////////// |
34 | 1209 |
40 | 1210 llvm::Value* LLVM_DtoArgument(const llvm::Type* paramtype, Argument* fnarg, Expression* argexp) |
1211 { | |
1212 llvm::Value* retval = 0; | |
1213 | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1214 bool haslvals = !gIR->exps.empty(); |
40 | 1215 if (haslvals) |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1216 gIR->exps.push_back(IRExp(NULL,NULL,NULL)); |
40 | 1217 |
1218 elem* arg = argexp->toElem(gIR); | |
1219 | |
1220 if (haslvals) | |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1221 gIR->exps.pop_back(); |
40 | 1222 |
1223 if (arg->inplace) { | |
1224 assert(arg->mem != 0); | |
1225 retval = arg->mem; | |
1226 delete arg; | |
1227 return retval; | |
1228 } | |
1229 | |
1230 Type* realtype = LLVM_DtoDType(argexp->type); | |
1231 TY argty = realtype->ty; | |
1232 if (LLVM_DtoIsPassedByRef(realtype)) { | |
1233 if (!fnarg || !fnarg->llvmCopy) { | |
1234 retval = arg->getValue(); | |
1235 assert(retval != 0); | |
1236 } | |
1237 else { | |
1238 llvm::Value* allocaInst = 0; | |
1239 llvm::BasicBlock* entryblock = &gIR->topfunc()->front(); | |
1240 //const llvm::PointerType* pty = llvm::cast<llvm::PointerType>(arg->mem->getType()); | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1241 const llvm::Type* realtypell = LLVM_DtoType(realtype); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1242 const llvm::PointerType* pty = llvm::PointerType::get(realtypell); |
40 | 1243 if (argty == Tstruct) { |
1244 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", gIR->topallocapoint()); | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1245 LLVM_DtoStructCopy(allocaInst,arg->mem); |
40 | 1246 } |
1247 else if (argty == Tdelegate) { | |
1248 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", gIR->topallocapoint()); | |
1249 LLVM_DtoDelegateCopy(allocaInst,arg->mem); | |
1250 } | |
1251 else if (argty == Tarray) { | |
1252 if (arg->type == elem::SLICE) { | |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1253 allocaInst = new llvm::AllocaInst(realtypell, "tmpparam", gIR->topallocapoint()); |
40 | 1254 LLVM_DtoSetArray(allocaInst, arg->arg, arg->mem); |
1255 } | |
1256 else { | |
1257 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", gIR->topallocapoint()); | |
1258 LLVM_DtoArrayAssign(allocaInst,arg->mem); | |
1259 } | |
1260 } | |
1261 else | |
1262 assert(0); | |
1263 | |
1264 assert(allocaInst != 0); | |
1265 retval = allocaInst; | |
1266 } | |
1267 } | |
1268 else if (!fnarg || fnarg->llvmCopy) { | |
1269 Logger::println("regular arg"); | |
1270 assert(arg->type != elem::SLICE); | |
1271 if (arg->mem) Logger::cout() << "mem = " << *arg->mem << '\n'; | |
1272 if (arg->val) Logger::cout() << "val = " << *arg->val << '\n'; | |
1273 if (arg->arg) Logger::cout() << "arg = " << *arg->arg << '\n'; | |
1274 retval = arg->arg ? arg->arg : arg->field ? arg->mem : arg->getValue(); | |
1275 } | |
1276 else { | |
1277 Logger::println("as ptr arg"); | |
1278 retval = arg->mem ? arg->mem : arg->val; | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1279 if (paramtype && retval->getType() != paramtype) |
40 | 1280 { |
1281 assert(retval->getType() == paramtype->getContainedType(0)); | |
1282 LLVM_DtoGiveArgumentStorage(arg); | |
1283 new llvm::StoreInst(retval, arg->mem, gIR->scopebb()); | |
1284 retval = arg->mem; | |
1285 } | |
1286 } | |
1287 | |
1288 delete arg; | |
1289 | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
55
diff
changeset
|
1290 if (fnarg && paramtype && retval->getType() != paramtype) { |
40 | 1291 Logger::cout() << "got '" << *retval->getType() << "' expected '" << *paramtype << "'\n"; |
1292 assert(0 && "parameter type that was actually passed is invalid"); | |
1293 } | |
1294 return retval; | |
1295 } | |
34 | 1296 |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1297 ////////////////////////////////////////////////////////////////////////////////////////// |
34 | 1298 |
50
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1299 llvm::Value* LLVM_DtoNestedVariable(VarDeclaration* vd) |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1300 { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1301 FuncDeclaration* fd = vd->toParent()->isFuncDeclaration(); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1302 assert(fd != NULL); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1303 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1304 IRFunction* fcur = &gIR->func(); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1305 FuncDeclaration* f = fcur->decl; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1306 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1307 // on this stack |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1308 if (fd == f) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1309 return LLVM_DtoGEPi(vd->llvmValue,0,unsigned(vd->llvmNestedIndex),"tmp"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1310 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1311 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1312 // on a caller stack |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1313 llvm::Value* ptr = f->llvmThisVar; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1314 assert(ptr); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1315 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1316 f = f->toParent()->isFuncDeclaration(); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1317 assert(f); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1318 assert(f->llvmNested); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1319 const llvm::Type* nesttype = f->llvmNested->getType(); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1320 assert(nesttype); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1321 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1322 ptr = gIR->ir->CreateBitCast(ptr, nesttype, "tmp"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1323 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1324 Logger::cout() << "nested var reference:" << '\n' << *ptr << *nesttype << '\n'; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1325 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1326 while (f) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1327 if (fd == f) { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1328 return LLVM_DtoGEPi(ptr,0,vd->llvmNestedIndex,"tmp"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1329 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1330 else { |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1331 ptr = LLVM_DtoGEPi(ptr,0,0,"tmp"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1332 ptr = gIR->ir->CreateLoad(ptr,"tmp"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1333 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1334 f = f->toParent()->isFuncDeclaration(); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1335 } |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1336 |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1337 assert(0 && "nested var not found"); |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1338 return NULL; |
6fcc08a4d406
[svn r54] Added support for nested delegates referencing parent's stack variables.
lindquist
parents:
49
diff
changeset
|
1339 } |
51
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1340 |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1341 ////////////////////////////////////////////////////////////////////////////////////////// |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1342 |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1343 void LLVM_DtoAssign(Type* t, llvm::Value* lhs, llvm::Value* rhs) |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1344 { |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1345 Logger::cout() << "assignment:" << '\n' << *lhs << *rhs << '\n'; |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1346 |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1347 if (t->ty == Tstruct) { |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1348 assert(lhs->getType() == rhs->getType()); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1349 LLVM_DtoStructCopy(lhs,rhs); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1350 } |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1351 else if (t->ty == Tarray) { |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1352 assert(lhs->getType() == rhs->getType()); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1353 LLVM_DtoArrayAssign(lhs,rhs); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1354 } |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1355 else if (t->ty == Tsarray) { |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1356 assert(lhs->getType() == rhs->getType()); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1357 LLVM_DtoStaticArrayCopy(lhs,rhs); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1358 } |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1359 else if (t->ty == Tdelegate) { |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1360 assert(lhs->getType() == rhs->getType()); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1361 LLVM_DtoDelegateCopy(lhs,rhs); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1362 } |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1363 else { |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1364 assert(lhs->getType()->getContainedType(0) == rhs->getType()); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1365 gIR->ir->CreateStore(rhs, lhs); |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1366 } |
61bc1b4ad3c4
[svn r55] Foreach was always generating code as if the value variable was 'ref'
lindquist
parents:
50
diff
changeset
|
1367 } |
54
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1368 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1369 ////////////////////////////////////////////////////////////////////////////////////////// |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1370 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1371 llvm::ConstantInt* LLVM_DtoConstSize_t(size_t i) |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1372 { |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1373 return llvm::ConstantInt::get(LLVM_DtoSize_t(), i, false); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1374 } |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1375 llvm::ConstantInt* LLVM_DtoConstUint(unsigned i) |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1376 { |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1377 return llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1378 } |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1379 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1380 ////////////////////////////////////////////////////////////////////////////////////////// |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1381 |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1382 llvm::Constant* LLVM_DtoConstString(const char* str) |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1383 { |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1384 std::string s(str); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1385 llvm::Constant* init = llvm::ConstantArray::get(s, true); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1386 llvm::GlobalVariable* gvar = new llvm::GlobalVariable( |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1387 init->getType(), true,llvm::GlobalValue::InternalLinkage, init, "stringliteral", gIR->module); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1388 llvm::Constant* idxs[2] = { LLVM_DtoConstUint(0), LLVM_DtoConstUint(0) }; |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1389 return LLVM_DtoConstantSlice( |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1390 LLVM_DtoConstSize_t(s.length()), |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1391 llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2) |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1392 ); |
28e99b04a132
[svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents:
53
diff
changeset
|
1393 } |
58
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1394 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1395 ////////////////////////////////////////////////////////////////////////////////////////// |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1396 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1397 void LLVM_DtoMemCpy(llvm::Value* dst, llvm::Value* src, llvm::Value* nbytes) |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1398 { |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1399 assert(dst->getType() == src->getType()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1400 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1401 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1402 llvm::Value *dstarr, *srcarr; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1403 if (dst->getType() == arrty) |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1404 { |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1405 dstarr = dst; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1406 srcarr = src; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1407 } |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1408 else |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1409 { |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1410 dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1411 srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1412 } |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1413 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1414 llvm::Function* fn = (global.params.is64bit) ? LLVM_DeclareMemCpy64() : LLVM_DeclareMemCpy32(); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1415 std::vector<llvm::Value*> llargs; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1416 llargs.resize(4); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1417 llargs[0] = dstarr; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1418 llargs[1] = srcarr; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1419 llargs[2] = nbytes; |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1420 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1421 |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1422 new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb()); |
2c3cd3596187
[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum
lindquist
parents:
57
diff
changeset
|
1423 } |