C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – LLVM演员说明大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有使用fadd添加的ConstanTint和ConstantFP值.但是,我无法将ConstanTint转换为fadd将接受的浮点数.

以下是代码的摘录:

Value* left = ConstanTint::get(Type::geTint64Ty(getGlobalContext()),12,truE);
Value* right = ConstantFP::get(Type::getFloatTy(getGlobalContext()),11.6);

instruction* cast = CasTinst::Create(instruction::SIToFP,left,left->getType(),"",currentBlock());
left = cast->getOperand(0);

BinaryOperator::Create(instruction::FAdd,right,currentBlock());

currentBlock()返回BasicBlock.在尝试为此生成操作码之后,LLVM抱怨它无法添加这两个值,因为它们不相同.

我对LLVM很新,所以如果这段代码毫无意义,我会接受任何建议.

解决方法@H_607_12@
我对这些事情的常用方法是看看Clang生成了什么 – LLVM IR和C API调用(C后端).您可以使用 online instance来简化.所以,编译这个C代码
float foo(int a,float b) {
  return a + b;
}

给我这个LLVM IR:

define float @foo(i32 %a,float %b) #0 {
entry:
  %conv = sitofp i32 %a to float
  %add = fadd float %conv,%b
  ret float %add
}

这是重新创建它所需的C API调用

// Function: foo (func_foo)
 {
  Function::arg_iterator args = func_foo->arg_begin();
  Value* int32_a = args++;
  int32_a->setName("a");
  Value* floaT_B = args++;
  floaT_B->setName("b");

  BasicBlock* label_entry = BasicBlock::Create(mod->getContext(),"entry",func_foo,0);

  // Block entry (label_entry)
  CasTinst* float_conv = new SIToFPInst(int32_a,Type::getFloatTy(mod->getContext()),"conv",label_entry);
  BinaryOperator* float_add = BinaryOperator::Create(instruction::FAdd,float_conv,floaT_B,"add",label_entry);
  ReturnInst::Create(mod->getContext(),float_add,label_entry);   
 }

您可以自由调整输入C代码(即用常量替换变量等)并查看Clang / LLVM发出的内容.当您对IR和API不太熟悉时,这是找到绕过IR和API的最好/最快的方法.

大佬总结

以上是大佬教程为你收集整理的c – LLVM演员说明全部内容,希望文章能够帮你解决c – LLVM演员说明所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。