llvm学习笔记-2015-11-6

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

时间: 2024-12-05 18:29:22

llvm学习笔记-2015-11-6的相关文章

【视频编解码&#183;学习笔记】11. 提取SPS信息程序

一.准备工作: 回到之前SimpleH264Analyzer程序,找到SPS信息,并对其做解析 调整项目目录结构: 修改Global.h文件中代码,添加新数据类型UINT16,之前编写的工程中,UINT8和UINT32都为小写表示,为了更符合编程规范,将其改为全大写(可使用ctrl+H在整个解决方案内进行替换). typedef unsigned char UINT8; typedef unsigned short UINT16; typedef unsigned int UINT32; 之后编

Java程序员的JavaScript学习笔记(11——jQuery-在“对象”层面扩展)

计划按如下顺序完成这篇笔记: 1.    理念. 2.    属性复制和继承. 3.    this/call/apply. 4.    闭包/getter/setter. 5.    prototype. 6.    面向对象模拟. 7.    jQuery基本机制. 8.    jQuery选择器. 9.    jQuery工具方法. 10.    jQuery-在"类"层面扩展. 11.    jQuery-在"对象"层面扩展. 12.    jQuery-扩

SQLServer学习笔记系列11

一.写在前面的话 身体是革命的本钱,这句放在嘴边常说的话,还是拿出来一起共勉,提醒一起奋斗的同僚们,保证睡眠,注意身体!偶尔加个班,也许不曾感觉到身体发出的讯号,长期晚睡真心扛不住!自己也制定计划,敦促自己按照作息时间来上班学习生活!虽然自己每星期运动,还是觉得晚睡带来的身体压力,无法承受!程序猿兄弟们,我们早上起来的时候,可以看看自己的眼睛,如果充满血丝,那我们就该需要调养,好好休息了!没了身体,Coding的世界即将一去不复返!好好休息,保重身体!善待朋友,真爱家人,迎接每一天美丽的日出!共

linux学习笔记(11)df命令

linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命令功能: 显示指定磁盘文件的可用空间.如果没有文件名被指定,则所有当前被挂载的文件系统的可用空间将被显示.默认情况下,磁盘空间将以 1KB 为单位进行显示,除非环境变量 POSIXLY_CORRECT 被指定,那样将以512字节为单位进行显示 3.命令参数: 必要参数: -a 全部文件系统列表 -h

《Effective C++ 》学习笔记——条款11

***************************************转载请注明出处:http://blog.csdn.net/lttree******************************************** 二.Constructors,Destructors and Assignment Operators Rule 11:Handle assignment to self in operator = 规则11:在 operator= 中处理"自我赋值"

HTML第一天学习笔记2015 10 01

如需在 HTML5 中显示视频,您所有需要的是: <video src="movie.ogg" controls="controls"> </video> <html> 与 </html> 之间的文本描述网页 <body> 与 </body> 之间的文本是可见的页面内容 <h1> 与 </h1> 之间的文本被显示为标题 <p> 与 </p> 之间的

锋利的jQuery第2版学习笔记8~11章

第8章,用jQuery打造个性网站 网站结构 文件结构 images文件夹用于存放将要用到的图片 styles文件夹用于存放CSS样式表,个人更倾向于使用CSS文件夹 scripts文件夹用于存放jQuery脚本,个人更倾向于使用JS文件夹存放所有的js及jQuery脚本 编写CSS样式 推荐首先编写全局样式,接着编写可大范围内重用的样式,最后编写细节样式,这样根据CSS最近优先原则,可以较容易地对网站进行从整体到细节样式的定义 第9章,jQuery Mobile jQuery Mobile主要

&lt;&lt;Python基础教程&gt;&gt;学习笔记 | 第11章 | 文件和素材

打开文件 open(name[mode[,buffing]) name: 是强制选项,模式和缓冲是可选的 #如果文件不在,会报下面错误: >>> f = open(r'D:\text.txt','r') Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 2] No such file or directory: 'D:\\

ECMAScript5学习笔记-第11章

11.表达式 11.1  主值表达式  语法:PrimaryExpression : this:执行为当前执行环境的ThisBinding Identifier :执行遵循 标识符解析 的标识符查找.标识符执行的结果总是一个 Reference 类型的值.  Literal:  ArrayLiteral:一个零个或者多个表达式的序列,其中每一个表示一个数组元素,并且用方括号括起来 当元素列表中的一个逗号没有被 AssignmentExpression 优先处理(如,一个逗号在另一个逗号之前.)的