llvm.org GIT mirror llvm / e31b144
Do some refactoring in the LLVM C API echo test to remove duplication. NFC git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265641 91177308-0d34-0410-b5e6-96231b3b80d8 Amaury Sechet 4 years ago
1 changed file(s) with 87 addition(s) and 72 deletion(s). Raw diff Collapse all Expand all
211211 return VMap;
212212 }
213213
214 LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
215 LLVMValueRef Ret;
214 static void check_value_kind(LLVMValueRef V, LLVMValueKind K) {
215 if (LLVMGetValueKind(V) != K)
216 report_fatal_error("LLVMGetValueKind returned incorrect type");
217 }
218
219 static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M);
220
221 static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) {
222 LLVMValueRef Ret = clone_constant_impl(Cst, M);
223 check_value_kind(Ret, LLVMGetValueKind(Cst));
224 return Ret;
225 }
226
227 static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
216228 if (!LLVMIsAConstant(Cst))
217229 report_fatal_error("Expected a constant");
218230
222234
223235 // Try function
224236 if (LLVMIsAFunction(Cst)) {
225 if (LLVMGetValueKind(Cst) != LLVMFunctionValueKind)
226 report_fatal_error("LLVMGetValueKind returned incorrect type");
227 Ret = LLVMGetNamedFunction(M, Name);
228 if (!Ret)
229 report_fatal_error("Could not find function");
237 check_value_kind(Cst, LLVMFunctionValueKind);
238 LLVMValueRef Dst = LLVMGetNamedFunction(M, Name);
239 if (Dst)
240 return Dst;
241 report_fatal_error("Could not find function");
242 }
243
230244 // Try global variable
231 } else if (LLVMIsAGlobalVariable(Cst)) {
232 if (LLVMGetValueKind(Cst) != LLVMGlobalVariableValueKind)
233 report_fatal_error("LLVMGetValueKind returned incorrect type");
234 Ret = LLVMGetNamedGlobal(M, Name);
235 if (!Ret)
236 report_fatal_error("Could not find function");
237 } else {
238 fprintf(stderr, "Could not find @%s\n", Name);
239 exit(-1);
240 }
245 if (LLVMIsAGlobalVariable(Cst)) {
246 check_value_kind(Cst, LLVMGlobalVariableValueKind);
247 LLVMValueRef Dst = LLVMGetNamedGlobal(M, Name);
248 if (Dst)
249 return Dst;
250 report_fatal_error("Could not find function");
251 }
252
253 fprintf(stderr, "Could not find @%s\n", Name);
254 exit(-1);
255 }
256
241257 // Try integer literal
242 } else if (LLVMIsAConstantInt(Cst)) {
243 if (LLVMGetValueKind(Cst) != LLVMConstantIntValueKind)
244 report_fatal_error("LLVMGetValueKind returned incorrect type");
245 Ret = LLVMConstInt(TypeCloner(M).Clone(Cst), LLVMConstIntGetZExtValue(Cst),
246 false);
258 if (LLVMIsAConstantInt(Cst)) {
259 check_value_kind(Cst, LLVMConstantIntValueKind);
260 return LLVMConstInt(TypeCloner(M).Clone(Cst),
261 LLVMConstIntGetZExtValue(Cst), false);
262 }
263
247264 // Try zeroinitializer
248 } else if (LLVMIsAConstantAggregateZero(Cst)) {
249 if (LLVMGetValueKind(Cst) != LLVMConstantAggregateZeroValueKind)
250 report_fatal_error("LLVMGetValueKind returned incorrect type");
251 Ret = LLVMConstNull(TypeCloner(M).Clone(Cst));
265 if (LLVMIsAConstantAggregateZero(Cst)) {
266 check_value_kind(Cst, LLVMConstantAggregateZeroValueKind);
267 return LLVMConstNull(TypeCloner(M).Clone(Cst));
268 }
269
252270 // Try constant array
253 } else if (LLVMIsAConstantArray(Cst)) {
254 if (LLVMGetValueKind(Cst) != LLVMConstantArrayValueKind)
255 report_fatal_error("LLVMGetValueKind returned incorrect type");
271 if (LLVMIsAConstantArray(Cst)) {
272 check_value_kind(Cst, LLVMConstantArrayValueKind);
256273 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
257274 unsigned EltCount = LLVMGetArrayLength(Ty);
258275 SmallVector Elts;
259276 for (unsigned i = 0; i < EltCount; i++)
260277 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
261 Ret = LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
278 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
279 }
280
262281 // Try contant data array
263 } else if (LLVMIsAConstantDataArray(Cst)) {
264 if (LLVMGetValueKind(Cst) != LLVMConstantDataArrayValueKind)
265 report_fatal_error("LLVMGetValueKind returned incorrect type");
282 if (LLVMIsAConstantDataArray(Cst)) {
283 check_value_kind(Cst, LLVMConstantDataArrayValueKind);
266284 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
267285 unsigned EltCount = LLVMGetArrayLength(Ty);
268286 SmallVector Elts;
269287 for (unsigned i = 0; i < EltCount; i++)
270288 Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
271 Ret = LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
289 return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
290 }
291
272292 // Try constant struct
273 } else if (LLVMIsAConstantStruct(Cst)) {
274 if (LLVMGetValueKind(Cst) != LLVMConstantStructValueKind)
275 report_fatal_error("LLVMGetValueKind returned incorrect type");
293 if (LLVMIsAConstantStruct(Cst)) {
294 check_value_kind(Cst, LLVMConstantStructValueKind);
276295 LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
277296 unsigned EltCount = LLVMCountStructElementTypes(Ty);
278297 SmallVector Elts;
279298 for (unsigned i = 0; i < EltCount; i++)
280299 Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
281300 if (LLVMGetStructName(Ty))
282 Ret = LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
283 else
284 Ret = LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
285 EltCount, LLVMIsPackedStruct(Ty));
301 return LLVMConstNamedStruct(Ty, Elts.data(), EltCount);
302 return LLVMConstStructInContext(LLVMGetModuleContext(M), Elts.data(),
303 EltCount, LLVMIsPackedStruct(Ty));
304 }
305
286306 // Try undef
287 } else if (LLVMIsUndef(Cst)) {
288 if (LLVMGetValueKind(Cst) != LLVMUndefValueValueKind)
289 report_fatal_error("LLVMGetValueKind returned incorrect type");
290 Ret = LLVMGetUndef(TypeCloner(M).Clone(Cst));
307 if (LLVMIsUndef(Cst)) {
308 check_value_kind(Cst, LLVMUndefValueValueKind);
309 return LLVMGetUndef(TypeCloner(M).Clone(Cst));
310 }
311
291312 // Try float literal
292 } else if (LLVMIsAConstantFP(Cst)) {
293 if (LLVMGetValueKind(Cst) != LLVMConstantFPValueKind)
294 report_fatal_error("LLVMGetValueKind returned incorrect type");
313 if (LLVMIsAConstantFP(Cst)) {
314 check_value_kind(Cst, LLVMConstantFPValueKind);
295315 report_fatal_error("ConstantFP is not supported");
316 }
317
296318 // This kind of constant is not supported
297 } else if (!LLVMIsAConstantExpr(Cst)) {
319 if (!LLVMIsAConstantExpr(Cst))
298320 report_fatal_error("Expected a constant expression");
299 // At this point, it must be a constant expression, and thus, an opcode
300 } else {
301 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
302 switch(Op) {
303 case LLVMBitCast:
304 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
305 TypeCloner(M).Clone(Cst));
306 default:
307 fprintf(stderr, "%d is not a supported opcode\n", Op);
308 exit(-1);
309 }
310 }
311 if (LLVMGetValueKind(Ret) != LLVMGetValueKind(Cst)) {
312 report_fatal_error(
313 "The ValueKind of Ret is not equal to the ValueKind of Cst");
314 }
315 return Ret;
321
322 // At this point, it must be a constant expression
323 check_value_kind(Cst, LLVMConstantExprValueKind);
324
325 LLVMOpcode Op = LLVMGetConstOpcode(Cst);
326 switch(Op) {
327 case LLVMBitCast:
328 return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
329 TypeCloner(M).Clone(Cst));
330 default:
331 fprintf(stderr, "%d is not a supported opcode\n", Op);
332 exit(-1);
333 }
316334 }
317335
318336 struct FunCloner {
346364
347365 if (!LLVMIsAInstruction(Src))
348366 report_fatal_error("Expected an instruction");
349
350 if (LLVMGetValueKind(Src) != LLVMInstructionValueKind)
351 report_fatal_error("LLVMGetValueKind returned incorrect type");
352367
353368 auto Ctx = LLVMGetModuleContext(M);
354369 auto Builder = LLVMCreateBuilderInContext(Ctx);
360375 }
361376
362377 LLVMValueRef CloneInstruction(LLVMValueRef Src, LLVMBuilderRef Builder) {
363 const char *Name = LLVMGetValueName(Src);
378 check_value_kind(Src, LLVMInstructionValueKind);
364379 if (!LLVMIsAInstruction(Src))
365380 report_fatal_error("Expected an instruction");
366381
367 if (LLVMGetValueKind(Src) != LLVMInstructionValueKind)
368 report_fatal_error("LLVMGetValueKind returned incorrect type");
382 const char *Name = LLVMGetValueName(Src);
369383
370384 // Check if this is something we already computed.
371385 {
625639 exit(-1);
626640 }
627641
642 check_value_kind(Dst, LLVMInstructionValueKind);
628643 return VMap[Src] = Dst;
629644 }
630645