写了一个map,key是val_offset_list,value是一个struct,包含了type和width
Module* module_;
typedef struct TypeWidth {
Type* type;
int width;
TypeWidth(Type* T, int W) {
type = T;
width = W;
}
}type_width;
void structMemoryArrange(Type* val_ty, vector<int> val_list, map<vector<int>, type_width> &val_map, int &use_width);
structMemoryArrange(Type* val_ty, vector val_offset_list, map<vector,
type_width> &val_map, int &use_width),可以获取当前变量的offset、type类型、内存地址相对位置、内存地址总长度。val_offset_list是val_ty的各个变量的offset位置list。
void RemoveStruct::structMemoryArrange(Type* val_ty, vector<int> val_offset_list, map<vector<int>, type_width> &val_map, int &use_width) {
int step_width = 1;
int val_width;
int struct_val_elements = val_ty->getStructNumElements();
for (int i = 0; i < struct_val_elements; i++) {
if (val_ty->getStructElementType(i)->isIntegerTy()) {
val_width = val_ty->getStructElementType(i)->getIntegerBitWidth();
while (use_width % val_width != 0) {
use_width += step_width;
}
val_offset_list.push_back(i);
val_map.insert(map<vector<int>, type_width>::value_type
(val_offset_list, type_width(val_ty->getStructElementType(i), use_width)));
val_offset_list.pop_back();
use_width += val_width;
outs() << " " << use_width;
} else if (val_ty->getStructElementType(i)->isArrayTy()) {
if (val_ty->getStructElementType(i)->getArrayElementType()->isIntegerTy()) {
val_offset_list.push_back(i);
for (int j = 0; j < val_ty->getStructElementType(i)->getArrayNumElements(); ++j) {
val_width = val_ty->getStructElementType(i)->getArrayElementType()->getIntegerBitWidth();
while (use_width % val_width != 0) {
use_width += step_width;
}
val_offset_list.push_back(j);
val_map.insert(map<vector<int>, type_width>::value_type
(val_offset_list, type_width(val_ty->getStructElementType(i)->getArrayElementType(), use_width)));
val_offset_list.pop_back();
use_width += val_width;
outs() << " " << use_width;
}
val_offset_list.pop_back();
} else if (val_ty->getStructElementType(i)->getArrayElementType()->isArrayTy()) {
assert(0);//TODO
} else if (val_ty->getStructElementType(i)->getArrayElementType()->isStructTy()) {
assert(0);//TODO
}
} else if (val_ty->getStructElementType(i)->isStructTy()) {
int align = 0;
StructAlign(val_ty->getStructElementType(i), align);
if (use_width % align == 0) {
val_offset_list.push_back(i);
structMemoryArrange(val_ty->getStructElementType(i), val_offset_list, val_map, use_width);
val_offset_list.pop_back();
} else {
use_width += align - (use_width % align);
val_offset_list.push_back(i);
structMemoryArrange(val_ty->getStructElementType(i), val_offset_list, val_map, use_width);
val_offset_list.pop_back();
}
} else {
outs() << "This Type is not considered! " << val_ty->getStructElementType(i) << "\n";
assert(0);
}
}
int last = 0;
StructAlign(val_ty, last);
if (use_width % last != 0) {
use_width += last - (use_width % last);
}
outs() << " " << use_width;
}
#include <iostream>
#include <map>
using namespace std;
typedef struct alertInfo {
double alertUp;
double alertDown;
alertInfo(double up, double down) {
alertUp = up;
alertDown = down;
};
} alert;
int main()
{
map<int, alert> alert_map;
for (int i = 0; i < 10; i++) {
alert_map.insert(pair<int, alert>(i, alert(240 * i, 200)));
}
map<int, alert>::iterator it;
for (it = alert_map.begin(); it != alert_map.end(); ++it) {
cout << it->first << " - > " << it->second.alertUp << endl;
}
}