llvm-summary

llvm 学习总结

Type 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)

enum类型 IntegerType::get(mod->getContext(), 32)

指针和引用类型
int* int&     PointerType* PointerTy_Int_Pointer = PointerType::get(IntegerType::get(mod->getContext(), 32), 0);
long* long&     PointerType* PointerTy_Long_Pointer = PointerType::get(IntegerType::get(mod->getContext(), 64), 0);
double* double& PointerType* PointerTy_Double_Pointer = PointerType::get(Type::getDoubleTy(mod->getContext()), 0);
float* float&   PointerType* PointerTy_Double_Pointer = PointerType::get(Type::getFloatTy(mod->getContext()), 0);
char* char&      PointerType* PointerTy_Char_Pointer = PointerType::get(IntegerType::get(mod->getContext(), 8), 0);
void*            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);
}

函数参数函数返回值

格式:

一个参数:
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);

常量类型及常量定义

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';

字符串常量
	C++源码
const char*s = "hello world";
	IR码
ArrayType* ArrayTy_0 = ArrayType::get(IntegerType::get(mod->getContext(), 8), 12);
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);

// Constant Definitions
Constant *const_array_5 = ConstantDataArray::getString(mod->getContext(), "hello world", true);
std::vector<Constant*> const_ptr_7_indices;
ConstantInt* const_int32_8 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("0"), 10));
const_ptr_7_indices.push_back(const_int32_8);
const_ptr_7_indices.push_back(const_int32_8);
Constant* const_ptr_7 = ConstantExpr::getGetElementPtr(gvar_array__str, const_ptr_7_indices);
gvar_array__str->setInitializer(const_array_5);
一维数组常量
int Array_int[4] = { 1, 2, 4, 5 };

ArrayType* ArrayTy_0 = ArrayType::get(IntegerType::get(mod->getContext(), 32), 4);
GlobalVariable* gvar_array__ZZ10yiweiArrayvE9Array_int = new GlobalVariable(/*Module=*/*mod,
/*Type=*/ArrayTy_0,
/*isConstant=*/true,
/*Linkage=*/GlobalValue::PrivateLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/"_ZZ10yiweiArrayvE9Array_int");
gvar_array__ZZ10yiweiArrayvE9Array_int->setAlignment(16);

std::vector<Constant*> const_array_7_elems;
ConstantInt* const_int32_8 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("1"), 10));
const_array_7_elems.push_back(const_int32_8);
ConstantInt* const_int32_9 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("2"), 10));
const_array_7_elems.push_back(const_int32_9);
ConstantInt* const_int32_10 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("4"), 10));
const_array_7_elems.push_back(const_int32_10);
ConstantInt* const_int32_11 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("5"), 10));
const_array_7_elems.push_back(const_int32_11);

Constant* const_array_7 = ConstantArray::get(ArrayTy_0, const_array_7_elems);
Constant* const_ptr_12 = ConstantExpr::getCast(Instruction::BitCast, gvar_array__ZZ10yiweiArrayvE9Array_int, PointerTy_4);

gvar_array__ZZ10yiweiArrayvE9Array_int->setInitializer(const_array_7);
二维数组常量

int a[4][4] = {
1, 2, 3, 4,
5, 6, 7, 8,
9,10,11,12,
13,14,15,16 };
ArrayType* ArrayTy_1 = ArrayType::get(IntegerType::get(mod->getContext(), 32), 4);
ArrayType* ArrayTy_0 = ArrayType::get(ArrayTy_1, 4);

GlobalVariable* gvar_array__ZZ9fun_arrayvE1a = new GlobalVariable(/*Module=*/*mod,
/*Type=*/ArrayTy_0,
/*isConstant=*/true,
/*Linkage=*/GlobalValue::PrivateLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/"_ZZ9fun_arrayvE1a");
gvar_array__ZZ9fun_arrayvE1a->setAlignment(16);

std::vector<Constant*> const_array_9_elems;
std::vector<Constant*> const_array_10_elems;
ConstantInt* const_int32_11 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("1"), 10));
const_array_10_elems.push_back(const_int32_11);
ConstantInt* const_int32_12 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("2"), 10));
const_array_10_elems.push_back(const_int32_12);
ConstantInt* const_int32_13 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("3"), 10));
const_array_10_elems.push_back(const_int32_13);
ConstantInt* const_int32_14 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("4"), 10));
const_array_10_elems.push_back(const_int32_14);
Constant* const_array_10 = ConstantArray::get(ArrayTy_1, const_array_10_elems);
const_array_9_elems.push_back(const_array_10);
std::vector<Constant*> const_array_15_elems;
ConstantInt* const_int32_16 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("5"), 10));
const_array_15_elems.push_back(const_int32_16);
ConstantInt* const_int32_17 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("6"), 10));
const_array_15_elems.push_back(const_int32_17);
ConstantInt* const_int32_18 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("7"), 10));
const_array_15_elems.push_back(const_int32_18);
ConstantInt* const_int32_19 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("8"), 10));
const_array_15_elems.push_back(const_int32_19);
Constant* const_array_15 = ConstantArray::get(ArrayTy_1, const_array_15_elems);
const_array_9_elems.push_back(const_array_15);
std::vector<Constant*> const_array_20_elems;
ConstantInt* const_int32_21 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("9"), 10));
const_array_20_elems.push_back(const_int32_21);
ConstantInt* const_int32_22 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("10"), 10));
const_array_20_elems.push_back(const_int32_22);
ConstantInt* const_int32_23 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("11"), 10));
const_array_20_elems.push_back(const_int32_23);
ConstantInt* const_int32_24 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("12"), 10));
const_array_20_elems.push_back(const_int32_24);
Constant* const_array_20 = ConstantArray::get(ArrayTy_1, const_array_20_elems);
const_array_9_elems.push_back(const_array_20);
std::vector<Constant*> const_array_25_elems;
ConstantInt* const_int32_26 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("13"), 10));
const_array_25_elems.push_back(const_int32_26);
ConstantInt* const_int32_27 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("14"), 10));
const_array_25_elems.push_back(const_int32_27);
ConstantInt* const_int32_28 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("15"), 10));
const_array_25_elems.push_back(const_int32_28);
ConstantInt* const_int32_29 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("16"), 10));
const_array_25_elems.push_back(const_int32_29);
Constant* const_array_25 = ConstantArray::get(ArrayTy_1, const_array_25_elems);
const_array_9_elems.push_back(const_array_25);
Constant* const_array_9 = ConstantArray::get(ArrayTy_0, const_array_9_elems);
Constant* const_ptr_30 = ConstantExpr::getCast(Instruction::BitCast, gvar_array__ZZ9fun_arrayvE1a, PointerTy_5);

gvar_array__ZZ9fun_arrayvE1a->setInitializer(const_array_9);

函数类型

//函数类型有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;
其他与类型定义一致

函数声明

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);

函数定义

{
//参数获取
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);

}

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);

跳转指令

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

调用函数

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);

#12数组和struct取值和赋值
int Array_int[4] = { 1, 2, 4, 5 };
一维数组:
取值: int int_1 = Array_int[0]; //下标只与64位的有关
ConstantInt* const_int32_16 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("0"), 10));
ConstantInt* const_int64_17 = ConstantInt::get(mod->getContext(), APInt(64, StringRef("0"), 10));
std::vector<Value*> ptr_arrayidx_indices;
ptr_arrayidx_indices.push_back(const_int32_16);
ptr_arrayidx_indices.push_back(const_int64_17);//获得其指针
Instruction* ptr_arrayidx = GetElementPtrInst::Create(ptr_Array_int, ptr_arrayidx_indices, "arrayidx", label_entry);
LoadInst* int32_21 = new LoadInst(ptr_arrayidx, "", false, label_entry);
int32_21->setAlignment(4);

赋值:Array_int[1] = 4;//下标只与64位的有关
ConstantInt* const_int32_16 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("0"), 10));
ConstantInt* const_int64_18 = ConstantInt::get(mod->getContext(), APInt(64, StringRef("1"), 10));
ConstantInt* const_int32_10 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("4"), 10));
std::vector<Value*> ptr_arrayidx1_indices;
ptr_arrayidx1_indices.push_back(const_int32_16);
ptr_arrayidx1_indices.push_back(const_int64_18);//下标只与64位的有关
Instruction* ptr_arrayidx1 = GetElementPtrInst::Create(ptr_Array_int, ptr_arrayidx1_indices, "arrayidx1", label_entry);
StoreInst* void_23 = new StoreInst(const_int32_10, ptr_arrayidx1, false, label_entry);
void_23->setAlignment(4);

二维数组:
int a[4][4] = {
1, 2, 3, 4,
5, 6, 7, 8,
9,10,11,12,
13,14,15,16 };

取值:int c = a[0][0];//下标只与64位的有关

ConstantInt* const_int64_34 = ConstantInt::get(mod->getContext(), APInt(64, StringRef("0"), 10));
std::vector<Value*> ptr_arrayidx_indices;
ptr_arrayidx_indices.push_back(const_int32_33);
ptr_arrayidx_indices.push_back(const_int64_34);//下标只与64位的有关
Instruction* ptr_arrayidx = GetElementPtrInst::Create(ptr_a, ptr_arrayidx_indices, "arrayidx", label_entry);
std::vector<Value*> ptr_arrayidx1_indices;
ptr_arrayidx1_indices.push_back(const_int32_33);
ptr_arrayidx1_indices.push_back(const_int64_34);//下标只与64位的有关
Instruction* ptr_arrayidx1 = GetElementPtrInst::Create(ptr_arrayidx, ptr_arrayidx1_indices, "arrayidx1", label_entry);
LoadInst* int32_39 = new LoadInst(ptr_arrayidx1, "", false, label_entry);
int32_39->setAlignment(4);
StoreInst* void_40 = new StoreInst(int32_39, ptr_c, false, label_entry);
void_40->setAlignment(4);

赋值: a[2][3] = 5;
ConstantInt* const_int32_33 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("0"), 10));
ConstantInt* const_int64_35 = ConstantInt::get(mod->getContext(), APInt(64, StringRef("2"), 10));
ConstantInt* const_int64_36 = ConstantInt::get(mod->getContext(), APInt(64, StringRef("3"), 10));
std::vector<Value*> ptr_arrayidx2_indices;
ptr_arrayidx2_indices.push_back(const_int32_33);
ptr_arrayidx2_indices.push_back(const_int64_35);//下标只与64位的有关
Instruction* ptr_arrayidx2 = GetElementPtrInst::Create(ptr_a, ptr_arrayidx2_indices, "arrayidx2", label_entry);
std::vector<Value*> ptr_arrayidx3_indices;
ptr_arrayidx3_indices.push_back(const_int32_33);
ptr_arrayidx3_indices.push_back(const_int64_36);//下标只与64位的有关
Instruction* ptr_arrayidx3 = GetElementPtrInst::Create(ptr_arrayidx2, ptr_arrayidx3_indices, "arrayidx3", label_entry);
StoreInst* void_41 = new StoreInst(const_int32_16, ptr_arrayidx3, false, label_entry);
void_41->setAlignment(4);

13.结构体的取值和赋值
typedef struct struct_1
{
int int_1;
struct struct_1* next;
}struct_1;

void fun_struct()
{
struct_1 st_1 = {1,2,3, 0};
int i = st_1.int_1;
struct_1 *st_2 = st_1.next;

st_1.int_1 = 2;
st_1.next = &st_1;
}

取值:
int i = st_1.int_1;
struct_1 *st_2 = st_1.next;

ConstantInt* const_int32_14 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("0"), 10));
ConstantInt* const_int32_8 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("1"), 10));
std::vector<Value*> ptr_int_1_indices;
ptr_int_1_indices.push_back(const_int32_14);
ptr_int_1_indices.push_back(const_int32_14);//只与二个有关
Instruction* ptr_int_1 = GetElementPtrInst::Create(ptr_st_1, ptr_int_1_indices, "int_1", label_entry);
LoadInst* int32_18 = new LoadInst(ptr_int_1, "", false, label_entry);
int32_18->setAlignment(4);
StoreInst* void_19 = new StoreInst(int32_18, ptr_i, false, label_entry);
void_19->setAlignment(4);
std::vector<Value*> ptr_next_indices;
ptr_next_indices.push_back(const_int32_14);
ptr_next_indices.push_back(const_int32_8);//只与二个有关
Instruction* ptr_next = GetElementPtrInst::Create(ptr_st_1, ptr_next_indices, "next", label_entry);
LoadInst* ptr_20 = new LoadInst(ptr_next, "", false, label_entry);
ptr_20->setAlignment(8);
StoreInst* void_21 = new StoreInst(ptr_20, ptr_st_2, false, label_entry);
void_21->setAlignment(8);

赋值:
st_1.int_1 = 2;
st_1.next = &st_1;

ConstantInt* const_int32_14 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("0"), 10));
ConstantInt* const_int32_8 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("1"), 10));
std::vector<Value*> ptr_int_11_indices;
ptr_int_11_indices.push_back(const_int32_14);
ptr_int_11_indices.push_back(const_int32_14);//只与二个有关
Instruction* ptr_int_11 = GetElementPtrInst::Create(ptr_st_1, ptr_int_11_indices, "int_11", label_entry);
StoreInst* void_22 = new StoreInst(const_int32_15, ptr_int_11, false, label_entry);
void_22->setAlignment(4);
std::vector<Value*> ptr_next2_indices;
ptr_next2_indices.push_back(const_int32_14);
ptr_next2_indices.push_back(const_int32_8);//只与二个有关
Instruction* ptr_next2 = GetElementPtrInst::Create(ptr_st_1, ptr_next2_indices, "next2", label_entry);
StoreInst* void_23 = new StoreInst(ptr_st_1, ptr_next2, false, label_entry);
void_23->setAlignment(8);
posted @ 2016-01-08 12:33  John-Y  阅读(883)  评论(0编辑  收藏  举报