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);
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);
}
#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);
一維數組常量
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);
#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);
#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);