Mercurial > projects > ldc
comparison gen/arrays.cpp @ 338:0c90d816394f trunk
[svn r359] Try to fix DtoArrayInit. Unfinished, breaks for arrays of primitive types.
author | ChristianK |
---|---|
date | Sat, 12 Jul 2008 23:56:56 +0200 |
parents | 20446d22f832 |
children | 385a18242485 |
comparison
equal
deleted
inserted
replaced
337:d03d748a9b5f | 338:0c90d816394f |
---|---|
106 | 106 |
107 ////////////////////////////////////////////////////////////////////////////////////////// | 107 ////////////////////////////////////////////////////////////////////////////////////////// |
108 | 108 |
109 typedef const LLType* constLLVMTypeP; | 109 typedef const LLType* constLLVMTypeP; |
110 | 110 |
111 static size_t checkRectArrayInit(const LLType* pt, constLLVMTypeP& finalty) | 111 static size_t checkRectArrayInit(const LLType* pt, const LLType* t) |
112 { | 112 { |
113 if (const LLArrayType* arrty = isaArray(pt)) { | 113 const LLArrayType* arrty = isaArray(pt); |
114 size_t n = checkRectArrayInit(arrty->getElementType(), finalty); | 114 if (pt != t && arrty) { |
115 size_t n = checkRectArrayInit(arrty->getElementType(), t); | |
115 size_t ne = arrty->getNumElements(); | 116 size_t ne = arrty->getNumElements(); |
116 if (n) return n * ne; | 117 if (n) return n * ne; |
117 return ne; | 118 return ne; |
118 } | 119 } |
119 finalty = pt; | 120 |
120 return 0; | 121 return 0; |
121 } | 122 } |
122 | 123 |
123 void DtoArrayInit(DValue* array, DValue* value) | 124 void DtoArrayInit(DValue* array, DValue* value) |
124 { | 125 { |
131 | 132 |
132 Logger::cout() << "llvm values:\n" << " ptr: " << *ptr << " dim: " << *dim << " val: " << *val << '\n'; | 133 Logger::cout() << "llvm values:\n" << " ptr: " << *ptr << " dim: " << *dim << " val: " << *val << '\n'; |
133 | 134 |
134 const LLType* pt = ptr->getType()->getContainedType(0); | 135 const LLType* pt = ptr->getType()->getContainedType(0); |
135 const LLType* t = val->getType(); | 136 const LLType* t = val->getType(); |
136 const LLType* finalTy; | |
137 | 137 |
138 size_t aggrsz = 0; | 138 size_t aggrsz = 0; |
139 Type* valtype = value->getType()->toBasetype(); | 139 Type* valtype = value->getType()->toBasetype(); |
140 | 140 |
141 // handle rectangular init | 141 const char* funcname = NULL; |
142 if (size_t arrsz = checkRectArrayInit(pt, finalTy)) | 142 |
143 { | 143 LLSmallVector<LLValue*, 4> args; |
144 assert(finalTy == t); | 144 args.push_back(ptr); |
145 LLConstant* c = isaConstant(dim); | 145 args.push_back(dim); |
146 assert(c); | 146 args.push_back(val); |
147 dim = llvm::ConstantExpr::getMul(c, DtoConstSize_t(arrsz)); | 147 |
148 ptr = gIR->ir->CreateBitCast(ptr, getPtrToType(finalTy), "tmp"); | 148 // if t is a primitive type, use the corresponding runtime function |
149 if (t == LLType::Int1Ty) { | |
150 funcname = "_d_array_init_i1"; | |
151 } | |
152 else if (t == LLType::Int8Ty) { | |
153 funcname = "_d_array_init_i8"; | |
154 } | |
155 else if (t == LLType::Int16Ty) { | |
156 funcname = "_d_array_init_i16"; | |
157 } | |
158 else if (t == LLType::Int32Ty) { | |
159 funcname = "_d_array_init_i32"; | |
160 } | |
161 else if (t == LLType::Int64Ty) { | |
162 funcname = "_d_array_init_i64"; | |
163 } | |
164 else if (t == LLType::FloatTy) { | |
165 funcname = "_d_array_init_float"; | |
166 } | |
167 else if (t == LLType::DoubleTy) { | |
168 funcname = "_d_array_init_double"; | |
169 } | |
170 else if (t == getPtrToType(LLType::Int8Ty)) { | |
171 funcname = "_d_array_init_pointer"; | |
172 | |
173 const LLType* dstty = getPtrToType(getVoidPtrType()); | |
174 if (args[0]->getType() != dstty) | |
175 args[0] = DtoBitCast(args[0],dstty); | |
176 | |
177 const LLType* valty = getVoidPtrType(); | |
178 if (args[2]->getType() != valty) | |
179 args[2] = DtoBitCast(args[2],valty); | |
180 } | |
181 // handle array rhs | |
182 else if (value->getType()->ty == Tarray || value->getType()->ty == Tsarray) | |
183 { | |
184 const LLArrayType* dstarrty = isaArray(pt); | |
185 assert(dstarrty); | |
186 const LLPointerType* srcty = isaPointer(t); | |
187 assert(dstarrty == srcty->getElementType()); | |
188 | |
189 funcname = "_d_array_init_mem"; | |
190 | |
191 args[0] = gIR->ir->CreateBitCast(ptr, getVoidPtrType(), "tmp"); | |
192 args[2] = gIR->ir->CreateBitCast(val, getVoidPtrType(), "tmp"); | |
193 | |
194 size_t n_inner = getABITypeSize(srcty->getElementType()); | |
195 args.push_back(DtoConstSize_t(n_inner)); | |
149 } | 196 } |
150 // handle null aggregate | 197 // handle null aggregate |
151 else if (isaStruct(t)) | 198 else if (isaStruct(t)) |
152 { | 199 { |
153 aggrsz = getABITypeSize(t); | 200 aggrsz = getABITypeSize(t); |
162 return; | 209 return; |
163 } | 210 } |
164 // handle general aggregate case | 211 // handle general aggregate case |
165 else if (DtoIsPassedByRef(valtype)) | 212 else if (DtoIsPassedByRef(valtype)) |
166 { | 213 { |
214 funcname = "_d_array_init_mem"; | |
215 | |
216 args[0] = gIR->ir->CreateBitCast(ptr, getVoidPtrType(), "tmp"); | |
217 args[2] = gIR->ir->CreateBitCast(val, getVoidPtrType(), "tmp"); | |
218 | |
167 aggrsz = getABITypeSize(pt); | 219 aggrsz = getABITypeSize(pt); |
168 ptr = gIR->ir->CreateBitCast(ptr, getVoidPtrType(), "tmp"); | 220 args.push_back(DtoConstSize_t(aggrsz)); |
169 val = gIR->ir->CreateBitCast(val, getVoidPtrType(), "tmp"); | |
170 } | 221 } |
171 else | 222 else |
172 { | 223 { |
173 Logger::cout() << "no special handling" << '\n'; | |
174 assert(t == pt); | |
175 } | |
176 | |
177 Logger::cout() << "array: " << *ptr << " dim: " << *dim << " val: " << *val << '\n'; | |
178 | |
179 LLSmallVector<LLValue*, 4> args; | |
180 args.push_back(ptr); | |
181 args.push_back(dim); | |
182 args.push_back(val); | |
183 | |
184 const char* funcname = NULL; | |
185 | |
186 if (aggrsz) { | |
187 funcname = "_d_array_init_mem"; | |
188 args.push_back(DtoConstSize_t(aggrsz)); | |
189 } | |
190 else if (isaPointer(t)) { | |
191 funcname = "_d_array_init_pointer"; | |
192 | |
193 const LLType* dstty = getPtrToType(getPtrToType(LLType::Int8Ty)); | |
194 if (args[0]->getType() != dstty) | |
195 args[0] = DtoBitCast(args[0],dstty); | |
196 | |
197 const LLType* valty = getPtrToType(LLType::Int8Ty); | |
198 if (args[2]->getType() != valty) | |
199 args[2] = DtoBitCast(args[2],valty); | |
200 } | |
201 else if (t == LLType::Int1Ty) { | |
202 funcname = "_d_array_init_i1"; | |
203 } | |
204 else if (t == LLType::Int8Ty) { | |
205 funcname = "_d_array_init_i8"; | |
206 } | |
207 else if (t == LLType::Int16Ty) { | |
208 funcname = "_d_array_init_i16"; | |
209 } | |
210 else if (t == LLType::Int32Ty) { | |
211 funcname = "_d_array_init_i32"; | |
212 } | |
213 else if (t == LLType::Int64Ty) { | |
214 funcname = "_d_array_init_i64"; | |
215 } | |
216 else if (t == LLType::FloatTy) { | |
217 funcname = "_d_array_init_float"; | |
218 } | |
219 else if (t == LLType::DoubleTy) { | |
220 funcname = "_d_array_init_double"; | |
221 } | |
222 else { | |
223 Logger::cout() << *ptr->getType() << " = " << *val->getType() << '\n'; | 224 Logger::cout() << *ptr->getType() << " = " << *val->getType() << '\n'; |
224 assert(0); | 225 assert(0); |
225 } | 226 } |
226 | 227 |
227 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, funcname); | 228 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, funcname); |