LLVM Study notes-2015-11-6

Source: Internet
Author: User

LLVM Study Summary
#1Type define
int type integertype::get (Mod->getcontext (), 32)

Long Type Integertype::get (Mod->getcontext (), 64)

Double Type type::getdoublety (Mod->getcontext ())

Float type Type::getfloatty (Mod->getcontext ())

Char type Integertype::get (Mod->getcontext (), 8)

Pointer type
intpointertype* 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);
charpointertype* pointerty_char_pointer = Pointertype::get (Integertype::get (Mod->getcontext (), 8), 0);

struct type
struct struct_1
{
int int_1;
Long long_1;
Double double_1;
Char char_1;
};
No pointers
Structtype *structty_struct = Mod->gettypebyname ("Struct1");//struct name
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;
};

with pointers
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);//Array

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 type

Class Class_1
{
int int_1;
Long long_1;
Double double_1;
Char char_1;
};

No function
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);
}

Has a function
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 function parameter function return value
#格式:

One parameter:
int fun_int_1 (int a);

std::vector<type*>functy_0_args;
Functy_0_args.push_back (Integertype::get (Mod->getcontext (), 32));
Two parameters:
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));
Three parameters:
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));

function return value
When the function return value is 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 (
The return value of the/*result=*/pointerty_2,//function is pointerty_2
/*params=*/functy_3_args,
/*isvararg=*/false);

#3常量类型及常量定义
int type constantint* Const_int32 = Constantint::get (Mod->getcontext (), Apint (+, Stringref (")"),//int int_1 = 100;

Long type constantint* Const_int64 = Constantint::get (Mod->getcontext (), Apint (+, Stringref ("100000"),//long); long_1 = 100000;

Float type constantfp* const_float_4 = Constantfp::get (Mod->getcontext (), Apfloat (4.342340e+02f)); float float_1 = 434.234

Double type constantfp* const_double = Constantfp::get (Mod->getcontext (), Apfloat (1.344000e+02)); Double double_1 = 134.4

Char type constantint* const_int8 = Constantint::get (Mod->getcontext (), Apint (8, Stringref (")");//char char_1 = ' A ';

String constants
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::P rivatelinkage,
/*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 (+, 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函数类型
function type has 3 parameters @1 Result return value type
@2 Params parameter types
@3 whether Isvararg is a variable parameter/bool
functiontype* functy_0 = Functiontype::get (
/*result=*/integertype::get (Mod->getcontext (), 32),
/*params=*/functy_0_args,
/*isvararg=*/false);
Result type
void Type Type::getvoidty (Mod->getcontext ())
Other consistent with type definition
Params type
void Type std::vector<type*>functy_0_args;
Other consistent with type definition

#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 Language library function 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函数定义
{
Parameter acquisition
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 (), +), "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 Definition
If
{}
Else
{}
Basicblock has 4 parameters: @1:mod->getcontext (), @2: "string"//char*, @3: function name//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);//Direct jump by Label_entry >>> Label_for_cond

Branchinst::create (Label_for_body, Label_for_end, int1_cmp, Label_for_cond);//conditional Jump Label_for_cond >>> > label_for_body or Label_for_end
//determined by int_cmp, not equal to 0 jump Label_for_body
equals 0 jump label_for_end
#9结束指令
Returninst::create (Mod->getcontext (), int32_17, label_for_end);
Three Parameters @1:mod->getcontext (), @2: return value, @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;//parameter
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);//Invoke instruction
// 4 Parameters: @1: Function name//function* @2: Parameter @3: string @4 where Basicblock
Int32_call->setcallingconv (callingconv::c);
Int32_ Call->settailcall (FALSE);
AttributeSet Int32_call_pal;
Int32_call->setattributes (int32_call_pal);

#11指令
Allocainst instruction request Pointer 3 parameters @1: Type @2: string difference @3 where Basicblock
allocainst* ptr_int_1 = new Allocainst (Integertype::get (Mod->getcontext (), +), "int_1", label_entry);
Ptr_int_1->setalignment (4);

Storeinst instruction Ptr_int_1 Point const_int32_10 4 parameters: @1: Value @2: pointer @3: @4: Where Basicblock
storeinst* void_16 = new Storeinst (Const_int32_10, ptr_int_1, False, label_entry);
Void_16->setalignment (4);

The value of Loadinst instruction ptr_81 is the value that ptr_a_addr_79 points to 4 parameters: @1: pointer @2: String @3:bool @4: where Basicblock
loadinst* ptr_81 = new Loadinst (ptr_a_addr_79, "", false, label_entry_78);
Ptr_81->setalignment (8);

LLVM Study notes-2015-11-6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.