llvm 学习总结
#1Type define
int类型 IntegerType::get(mod->getContext(), 32)
long类型 IntegerType::get(mod->getContext(), 64)
double类型 Type::getDoubleTy(mod->getContext())
float类型 Type::getFloatTy(mod->getContext())
char类型 IntegerType::get(mod->getContext(), 8)
pointer类型
int PointerType* PointerTy_Int_Pointer = PointerType::get(IntegerType::get(mod->getContext(), 32), 0);
long PointerType* PointerTy_Long_Pointer = PointerType::get(IntegerType::get(mod->getContext(), 64), 0);
double PointerType* PointerTy_Double_Pointer = PointerType::get(Type::getDoubleTy(mod->getContext()), 0);
float PointerType* PointerTy_Double_Pointer = PointerType::get(Type::getFloatTy(mod->getContext()), 0);
char PointerType* PointerTy_Char_Pointer = PointerType::get(IntegerType::get(mod->getContext(), 8), 0);
结构体类型
struct struct_1
{
int int_1;
long long_1;
double double_1;
char char_1;
};
无指针
StructType *StructTy_struct = mod->getTypeByName("struct1");//结构体名
if (!StructTy_struct_struct_1) {
StructTy_struct = StructType::create(mod->getContext(), "struct1");
}
std::vector<Type*>StructTy_struct_1_fields;
StructTy_struct_1_fields.push_back(IntegerType::get(mod->getContext(), 32));
StructTy_struct_1_fields.push_back(IntegerType::get(mod->getContext(), 64));
StructTy_struct_1_fields.push_back(Type::getDoubleTy(mod->getContext()));
StructTy_struct_1_fields.push_back(IntegerType::get(mod->getContext(), 8));
if (StructTy_struct->isOpaque()) {
StructTy_struct->setBody(StructTy_struct_1_fields, /*isPacked=*/false);
}
struct struct_2
{
int *int_ptr;
long *long_ptr;
double *double_ptr;
int int_1;
long long_1;
double double_1;
int int_array[100];
int **int_2;
};
有指针
PointerType* PointerTy_Int = PointerType::get(IntegerType::get(mod->getContext(), 32), 0);
PointerType* PointerTy_Long = PointerType::get(IntegerType::get(mod->getContext(), 64), 0);
PointerType* PointerTy_Double = PointerType::get(Type::getDoubleTy(mod->getContext()), 0);
PointerType* PointerTy_Int_Pointer = PointerType::get(PointerTy_Int, 0);
StructType *StructTy_struct_struct_2 = mod->getTypeByName("struct.struct_2");
if (!StructTy_struct_struct_2) {
StructTy_struct_struct_2 = StructType::create(mod->getContext(), "struct.struct_2");
}
std::vector<Type*>StructTy_struct_struct_2_fields;
StructTy_struct_struct_2_fields.push_back(PointerTy_Int);
StructTy_struct_struct_2_fields.push_back(PointerTy_Long);
StructTy_struct_struct_2_fields.push_back(PointerTy_Double);
StructTy_struct_struct_2_fields.push_back(IntegerType::get(mod->getContext(), 32));
StructTy_struct_struct_2_fields.push_back(IntegerType::get(mod->getContext(), 64));
StructTy_struct_struct_2_fields.push_back(Type::getDoubleTy(mod->getContext()));
ArrayType* ArrayTy = ArrayType::get(IntegerType::get(mod->getContext(), 32), 100);//数组
StructTy_struct_struct_2_fields.push_back(ArrayTy);
StructTy_struct_struct_2_fields.push_back(PointerTy_Int_Pointer);
if (StructTy_struct_struct_2->isOpaque()) {
StructTy_struct_struct_2->setBody(StructTy_struct_struct_2_fields, /*isPacked=*/false);
}
类类型
class class_1
{
int int_1;
long long_1;
double double_1;
char char_1;
};
无函数
StructType *StructTy_class_class_1 = mod->getTypeByName("class.class_1");
if (!StructTy_class_class_1) {
StructTy_class_class_1 = StructType::create(mod->getContext(), "class.class_1");
}
std::vector<Type*>StructTy_class_class_1_fields;
StructTy_class_class_1_fields.push_back(IntegerType::get(mod->getContext(), 32));
StructTy_class_class_1_fields.push_back(IntegerType::get(mod->getContext(), 64));
StructTy_class_class_1_fields.push_back(Type::getDoubleTy(mod->getContext()));
StructTy_class_class_1_fields.push_back(IntegerType::get(mod->getContext(), 8));
if (StructTy_class_class_1->isOpaque()) {
StructTy_class_class_1->setBody(StructTy_class_class_1_fields, /*isPacked=*/false);
}
有函数
class class_2
{
int int_1;
int *int_ptr;
class_2(){}
void fun(int int_arg)
{
int_1 =int_arg;
int_ptr = &int_arg;
}
};
PointerType* PointerTy_Int_Pointer = PointerType::get(IntegerType::get(mod->getContext(), 32), 0);
StructType *StructTy_class_class_2 = mod->getTypeByName("class.class_2");
if (!StructTy_class_class_2) {
StructTy_class_class_2 = StructType::create(mod->getContext(), "class.class_2");
}
std::vector<Type*>StructTy_class_class_2_fields;
StructTy_class_class_2_fields.push_back(IntegerType::get(mod->getContext(), 32));
StructTy_class_class_2_fields.push_back(PointerTy_Int_Pointer);
if (StructTy_class_class_2->isOpaque()) {
StructTy_class_class_2->setBody(StructTy_class_class_2_fields, /*isPacked=*/false);
}
#2 函数参数函数返回值
#格式:
一个参数:
int fun_int_1(int a);
std::vector<Type*>FuncTy_0_args;
FuncTy_0_args.push_back(IntegerType::get(mod->getContext(), 32));
两个参数:
int fun_int_2(int a, int b);
std::vector<Type*>FuncTy_8_args;
FuncTy_8_args.push_back(IntegerType::get(mod->getContext(), 32));
FuncTy_8_args.push_back(IntegerType::get(mod->getContext(), 32));
三个参数:
int fun_int_3(int a, int b, int c);
std::vector<Type*>FuncTy_9_args;
FuncTy_9_args.push_back(IntegerType::get(mod->getContext(), 32));
FuncTy_9_args.push_back(IntegerType::get(mod->getContext(), 32));
FuncTy_9_args.push_back(IntegerType::get(mod->getContext(), 32));
函数返回值
当函数返回值为char*时
ArrayType* ArrayTy_0 = ArrayType::get(IntegerType::get(mod->getContext(), 8), 12);
PointerType* PointerTy_2 = PointerType::get(IntegerType::get(mod->getContext(), 8), 0);
std::vector<Type*>FuncTy_3_args;
FunctionType* FuncTy_3 = FunctionType::get(
/*Result=*/PointerTy_2,//函数的返回值为PointerTy_2
/*Params=*/FuncTy_3_args,
/*isVarArg=*/false);
#3常量类型及常量定义
int类型 ConstantInt* const_int32 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("100"), 10)); //int int_1 = 100;
long类型 ConstantInt* const_int64 = ConstantInt::get(mod->getContext(), APInt(64, StringRef("100000"), 10)); //long long_1 = 100000;
float类型 ConstantFP* const_float_4 = ConstantFP::get(mod->getContext(), APFloat(4.342340e+02f)); //float float_1 = 434.234
double类型 ConstantFP* const_double = ConstantFP::get(mod->getContext(), APFloat(1.344000e+02)); // double double_1 = 134.4
char类型 ConstantInt* const_int8 = ConstantInt::get(mod->getContext(), APInt(8, StringRef("97"), 10)); // char char_1 = ‘a‘;
字符串常量
const char* cstring_1 = "134312abc";
Constant *const_array_8 = ConstantDataArray::getString(mod->getContext(), "134312abc", true);
ArrayType* ArrayTy_0 = ArrayType::get(IntegerType::get(mod->getContext(), 8), 10);
//Global Variable Declarations
GlobalVariable* gvar_array__str = new GlobalVariable(/*Module=*/*mod,
/*Type=*/ArrayTy_0,
/*isConstant=*/true,
/*Linkage=*/GlobalValue::PrivateLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/".str");
gvar_array__str->setAlignment(1);
std::vector<Constant*> const_ptr_13_indices;
ConstantInt* const_int32_14 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("0"), 10));
const_ptr_13_indices.push_back(const_int32_14);
const_ptr_13_indices.push_back(const_int32_14);
Constant* const_ptr_13 = ConstantExpr::getGetElementPtr(gvar_array__str, const_ptr_13_indices);
// Global Variable Definitions
gvar_array__str->setInitializer(const_array_8);
#4函数类型
//函数类型有3个参数 @1 Result 返回值类型
@2 Params 参数类型
@3 isVarArg 是否是可变参数 /bool
FunctionType* FuncTy_0 = FunctionType::get(
/*Result=*/IntegerType::get(mod->getContext(), 32),
/*Params=*/FuncTy_0_args,
/*isVarArg=*/false);
Result类型
void类型 Type::getVoidTy(mod->getContext())
其他与类型定义一致
Params类型
void类型 std::vector<Type*>FuncTy_0_args;
其他与类型定义一致
#5函数声明
Function* func__Z9fun_int_1i = mod->getFunction("_Z9fun_int_1i");
if (!func__Z9fun_int_1i) {
func__Z9fun_int_1i = Function::Create(
/*Type=*/FuncTy_0,
/*Linkage=*/GlobalValue::ExternalLinkage,
/*Name=*/"_Z9fun_int_1i", mod);
func__Z9fun_int_1i->setCallingConv(CallingConv::C);
}
AttributeSet func__Z9fun_int_1i_PAL;
{
SmallVector<AttributeSet, 4> Attrs;
AttributeSet PAS;
{
AttrBuilder B;
B.addAttribute(Attribute::NoUnwind);
B.addAttribute(Attribute::UWTable);//C语言库函数Attribute::ReadNone
PAS = AttributeSet::get(mod->getContext(), ~0U, B);
}
Attrs.push_back(PAS);
func__Z9fun_int_1i_PAL = AttributeSet::get(mod->getContext(), Attrs);
}
func__Z9fun_int_1i->setAttributes(func__Z9fun_int_1i_PAL);
#6函数定义
{
//参数获取
Function::arg_iterator args = func__Z9fun_int_1i->arg_begin();
Value* int32_a = args++;
int32_a->setName("a");
BasicBlock* label_entry = BasicBlock::Create(mod->getContext(), "entry",func__Z9fun_int_1i,0);
// Block entry (label_entry)
AllocaInst* ptr_a_addr = new AllocaInst(IntegerType::get(mod->getContext(), 32), "a.addr", label_entry);
ptr_a_addr->setAlignment(4);
StoreInst* void_39 = new StoreInst(int32_a, ptr_a_addr, false, label_entry);
void_39->setAlignment(4);
LoadInst* int32_40 = new LoadInst(ptr_a_addr, "", false, label_entry);
int32_40->setAlignment(4);
ReturnInst::Create(mod->getContext(), int32_40, label_entry);
}
#7 BasicBlock定义
if
{}
else
{}
//BasicBlock有4个参数: @1:mod->getContext(), @2:"字符串"//char*, @3:函数名//Function*, @4:0
BasicBlock* label_entry_70 = BasicBlock::Create(mod->getContext(), "entry",func__Z4dayuv,0);
BasicBlock* label_if_then = BasicBlock::Create(mod->getContext(), "if.then",func__Z4dayuv,0);
BasicBlock* label_if_else = BasicBlock::Create(mod->getContext(), "if.else",func__Z4dayuv,0);
BasicBlock* label_if_end = BasicBlock::Create(mod->getContext(), "if.end",func__Z4dayuv,0);
for
{}
BasicBlock* label_entry = BasicBlock::Create(mod->getContext(), "entry",func__Z5cyclev,0);
BasicBlock* label_for_cond = BasicBlock::Create(mod->getContext(), "for.cond",func__Z5cyclev,0);
BasicBlock* label_for_body = BasicBlock::Create(mod->getContext(), "for.body",func__Z5cyclev,0);
BasicBlock* label_for_inc = BasicBlock::Create(mod->getContext(), "for.inc",func__Z5cyclev,0);
BasicBlock* label_for_end = BasicBlock::Create(mod->getContext(), "for.end",func__Z5cyclev,0);
while
{}
BasicBlock* label_entry = BasicBlock::Create(mod->getContext(), "entry",func__Z3funf,0);
BasicBlock* label_while_cond = BasicBlock::Create(mod->getContext(), "while.cond",func__Z3funf,0);
BasicBlock* label_while_body = BasicBlock::Create(mod->getContext(), "while.body",func__Z3funf,0);
BasicBlock* label_while_end = BasicBlock::Create(mod->getContext(), "while.end",func__Z3funf,0);
#8跳转指令
BranchInst::Create(label_for_cond, label_entry);//直接跳转由label_entry >>> label_for_cond
BranchInst::Create(label_for_body, label_for_end, int1_cmp, label_for_cond); //条件跳转 label_for_cond >>>> label_for_body or label_for_end
//由int_cmp 决定, 不等于0 跳 label_for_body
等于0 跳 label_for_end
#9结束指令
ReturnInst::Create(mod->getContext(), int32_17, label_for_end);
三个参数@1: mod->getContext(), @2: 返回值 , @3:BasicBlock
#10调用函数
LoadInst* int32_12 = new LoadInst(ptr_x_addr, "", false, label_entry_9);
int32_12->setAlignment(4);
LoadInst* int32_13 = new LoadInst(ptr_y_addr, "", false, label_entry_9);
int32_13->setAlignment(4);
std::vector<Value*> int32_call_params;//参数
int32_call_params.push_back(int32_12);
int32_call_params.push_back(int32_13);
CallInst* int32_call = CallInst::Create(func__Z3addii, int32_call_params, "call", label_entry_9);//调用指令
//4个参数:@1:函数名//Function* @2:参数 @3:字符串 @4所在BasicBlock
int32_call->setCallingConv(CallingConv::C);
int32_call->setTailCall(false);
AttributeSet int32_call_PAL;
int32_call->setAttributes(int32_call_PAL);
#11指令
//AllocaInst指令 申请指针 3个参数 @1:类型 @2:字符串区别 @3所在BasicBlock
AllocaInst* ptr_int_1 = new AllocaInst(IntegerType::get(mod->getContext(), 32), "int_1", label_entry);
ptr_int_1->setAlignment(4);
//StoreInst指令 ptr_int_1指向const_int32_10 4个参数:@1:值 @2:指针 @3 : @4:所在BasicBlock
StoreInst* void_16 = new StoreInst(const_int32_10, ptr_int_1, false, label_entry);
void_16->setAlignment(4);
//LoadInst指令 ptr_81的值为ptr_a_addr_79所指向的值 4个参数: @1:指针 @2: 字符串 @3: bool @4:所在BasicBlock
LoadInst* ptr_81 = new LoadInst(ptr_a_addr_79, "", false, label_entry_78);
ptr_81->setAlignment(8);