Commit 5d4ea841 authored by Yang's avatar Yang

add lldb Type summary

parent 04681c6c
...@@ -3,4 +3,5 @@ build/ ...@@ -3,4 +3,5 @@ build/
.vs .vs
out out
CMakePresets.json CMakePresets.json
Folder.DotSettings.user Folder.DotSettings.user
\ No newline at end of file .env
\ No newline at end of file
...@@ -16,7 +16,10 @@ ...@@ -16,7 +16,10 @@
// 程序运行的目录 // 程序运行的目录
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
// 程序运行前运行的命令(例如 build) // 程序运行前运行的命令(例如 build)
"preLaunchTask": "make cminusfc" "preLaunchTask": "make cminusfc",
"initCommands": [
"command script import ${workspaceFolder}/lldb_formatters.py"
]
}, },
{ {
"type": "lldb", "type": "lldb",
...@@ -32,7 +35,10 @@ ...@@ -32,7 +35,10 @@
"./build/1.cminus" "./build/1.cminus"
], ],
// 程序运行的目录 // 程序运行的目录
"cwd": "${workspaceFolder}" "cwd": "${workspaceFolder}",
"initCommands": [
"command script import ${workspaceFolder}/lldb_formatters.py"
]
} }
] ]
} }
\ No newline at end of file
...@@ -27,11 +27,22 @@ class BasicBlock : public Value { ...@@ -27,11 +27,22 @@ class BasicBlock : public Value {
std::list<BasicBlock *> &get_pre_basic_blocks() { return pre_bbs_; } std::list<BasicBlock *> &get_pre_basic_blocks() { return pre_bbs_; }
std::list<BasicBlock *> &get_succ_basic_blocks() { return succ_bbs_; } std::list<BasicBlock *> &get_succ_basic_blocks() { return succ_bbs_; }
void add_pre_basic_block(BasicBlock *bb) { pre_bbs_.push_back(bb); } // 自动去重
void add_succ_basic_block(BasicBlock *bb) { succ_bbs_.push_back(bb); } void add_pre_basic_block(BasicBlock *bb)
{
for (auto i : pre_bbs_) if (i == bb) return;
pre_bbs_.push_back(bb);
}
// 自动去重
void add_succ_basic_block(BasicBlock *bb)
{
for (auto i : succ_bbs_) if (i == bb) return;
succ_bbs_.push_back(bb);
}
void remove_pre_basic_block(BasicBlock *bb) { pre_bbs_.remove(bb); } void remove_pre_basic_block(BasicBlock *bb) { pre_bbs_.remove(bb); }
// 若你将 br label0, label0 的其中一个 label0 改为 label1,并且调用 remove_suc label0,那 suc 集合中也将不再包含 label0
void remove_succ_basic_block(BasicBlock *bb) { succ_bbs_.remove(bb); } void remove_succ_basic_block(BasicBlock *bb) { succ_bbs_.remove(bb); }
BasicBlock* get_entry_block_of_same_function() const; BasicBlock* get_entry_block_of_same_function() const;
// If the Block is terminated by ret/br // If the Block is terminated by ret/br
bool is_terminated() const; bool is_terminated() const;
......
...@@ -15,12 +15,13 @@ public: ...@@ -15,12 +15,13 @@ public:
Constant(Type *ty, const std::string &name = "") : User(ty, name) {} Constant(Type *ty, const std::string &name = "") : User(ty, name) {}
~Constant() override; ~Constant() override;
protected:
}; };
class ConstantInt : public Constant { class ConstantInt : public Constant {
private: private:
int value_; int value_;
ConstantInt(Type *ty, int val) : Constant(ty, ""), value_(val) {} ConstantInt(Type* ty, int val) : Constant(ty, ""), value_(val) {}
public: public:
int get_value() const { return value_; } int get_value() const { return value_; }
......
import lldb
def parseString(val : lldb.SBValue):
summary = val.GetSummary() or val.GetValue()
if summary:
# 去掉两端引号,只留裸文本(可选)
if summary.startswith('"') and summary.endswith('"'):
summary = summary[1:-1]
return summary
return ""
def TypePrinterSummary(valobj: lldb.SBValue, internal_dict):
# 调用 print 函数进行显示
name: lldb.SBValue = valobj.EvaluateExpression("print()");
return parseString(name)
def __lldb_init_module(debugger: lldb.SBDebugger, internal_dict):
types = ["Type", "IntegerType", "FunctionType", "ArrayType", "PointerType", "FloatType"]
for i in types:
debugger.HandleCommand(
f"type summary add -F lldb_formatters.TypePrinterSummary {i} -w my"
)
debugger.HandleCommand("type category enable my")
\ No newline at end of file
...@@ -34,16 +34,18 @@ Constant::~Constant() = default; ...@@ -34,16 +34,18 @@ Constant::~Constant() = default;
ConstantInt *ConstantInt::get(int val, Module *m) { ConstantInt *ConstantInt::get(int val, Module *m) {
if (cached_int.find(std::make_pair(val, m)) != cached_int.end()) if (cached_int.find(std::make_pair(val, m)) != cached_int.end())
return cached_int[std::make_pair(val, m)].get(); return cached_int[std::make_pair(val, m)].get();
return (cached_int[std::make_pair(val, m)] = std::unique_ptr<ConstantInt>( ConstantInt* ret = (cached_int[std::make_pair(val, m)] = std::unique_ptr<ConstantInt>(
new ConstantInt(m->get_int32_type(), val))) new ConstantInt(m->get_int32_type(), val)))
.get(); .get();
return ret;
} }
ConstantInt *ConstantInt::get(bool val, Module *m) { ConstantInt *ConstantInt::get(bool val, Module *m) {
if (cached_bool.find(std::make_pair(val, m)) != cached_bool.end()) if (cached_bool.find(std::make_pair(val, m)) != cached_bool.end())
return cached_bool[std::make_pair(val, m)].get(); return cached_bool[std::make_pair(val, m)].get();
return (cached_bool[std::make_pair(val, m)] = std::unique_ptr<ConstantInt>( auto ret = (cached_bool[std::make_pair(val, m)] = std::unique_ptr<ConstantInt>(
new ConstantInt(m->get_int1_type(), val ? 1 : 0))) new ConstantInt(m->get_int1_type(), val ? 1 : 0)))
.get(); .get();
return ret;
} }
std::string ConstantInt::print() { std::string ConstantInt::print() {
std::string const_ir; std::string const_ir;
...@@ -73,7 +75,8 @@ Constant *ConstantArray::get_element_value(int index) const ...@@ -73,7 +75,8 @@ Constant *ConstantArray::get_element_value(int index) const
ConstantArray *ConstantArray::get(ArrayType *ty, ConstantArray *ConstantArray::get(ArrayType *ty,
const std::vector<Constant *> &val) { const std::vector<Constant *> &val) {
return new ConstantArray(ty, val); auto ret = new ConstantArray(ty, val);
return ret;
} }
std::string ConstantArray::print() { std::string ConstantArray::print() {
...@@ -98,9 +101,10 @@ std::string ConstantArray::print() { ...@@ -98,9 +101,10 @@ std::string ConstantArray::print() {
ConstantFP *ConstantFP::get(float val, Module *m) { ConstantFP *ConstantFP::get(float val, Module *m) {
if (cached_float.find(std::make_pair(val, m)) != cached_float.end()) if (cached_float.find(std::make_pair(val, m)) != cached_float.end())
return cached_float[std::make_pair(val, m)].get(); return cached_float[std::make_pair(val, m)].get();
return (cached_float[std::make_pair(val, m)] = std::unique_ptr<ConstantFP>( auto ret = (cached_float[std::make_pair(val, m)] = std::unique_ptr<ConstantFP>(
new ConstantFP(m->get_float_type(), val))) new ConstantFP(m->get_float_type(), val)))
.get(); .get();
return ret;
} }
std::string ConstantFP::print() { std::string ConstantFP::print() {
...@@ -116,7 +120,9 @@ std::string ConstantFP::print() { ...@@ -116,7 +120,9 @@ std::string ConstantFP::print() {
ConstantZero *ConstantZero::get(Type *ty, Module *m) { ConstantZero *ConstantZero::get(Type *ty, Module *m) {
if (not cached_zero[ty]) if (not cached_zero[ty])
cached_zero[ty] = std::unique_ptr<ConstantZero>(new ConstantZero(ty)); {
cached_zero[ty] = std::unique_ptr<ConstantZero>(new ConstantZero(ty));
}
return cached_zero[ty].get(); return cached_zero[ty].get();
} }
......
...@@ -159,21 +159,6 @@ void Function::check_for_block_relation_error() const ...@@ -159,21 +159,6 @@ void Function::check_for_block_relation_error() const
assert((bbs.count(i)) && "函数 F 的基本块表中包含基本块 a, a 的前驱块表中的某基本块 b 不在 F 的基本块表中"); assert((bbs.count(i)) && "函数 F 的基本块表中包含基本块 a, a 的前驱块表中的某基本块 b 不在 F 的基本块表中");
} }
} }
// 检查基本块的前驱和后继表不包含重复的基本块
/*
for (auto& bb : basic_blocks_)
{
bbs.clear();
for(auto suc: bb.get_succ_basic_blocks()){
assert((!bbs.count(suc)) && "基本块的后继表中包含重复项目");
bbs.emplace(suc);
}
for(auto suc: bb.get_pre_basic_blocks()){
assert((!bbs.count(suc)) && "基本块的前驱表中包含重复项目");
bbs.emplace(suc);
}
}
*/
// 检查基本块基本信息 // 检查基本块基本信息
for (auto bb : basic_blocks_) for (auto bb : basic_blocks_)
{ {
......
...@@ -112,14 +112,16 @@ std::string Type::print() const { ...@@ -112,14 +112,16 @@ std::string Type::print() const {
} }
IntegerType::IntegerType(unsigned num_bits, Module *m) IntegerType::IntegerType(unsigned num_bits, Module *m)
: Type(Type::IntegerTyID, m), num_bits_(num_bits) {} : Type(Type::IntegerTyID, m), num_bits_(num_bits)
{
}
IntegerType::~IntegerType() = default; IntegerType::~IntegerType() = default;
unsigned IntegerType::get_num_bits() const { return num_bits_; } unsigned IntegerType::get_num_bits() const { return num_bits_; }
FunctionType::FunctionType(Type *result, const std::vector<Type *>& params) FunctionType::FunctionType(Type *result, const std::vector<Type *>& params)
: Type(Type::FunctionTyID, nullptr) { : Type(Type::FunctionTyID, result->get_module()) {
assert(is_valid_return_type(result) && "Invalid return type for function!"); assert(is_valid_return_type(result) && "Invalid return type for function!");
result_ = result; result_ = result;
...@@ -171,7 +173,7 @@ ArrayType *ArrayType::get(Type *contained, unsigned num_elements) { ...@@ -171,7 +173,7 @@ ArrayType *ArrayType::get(Type *contained, unsigned num_elements) {
PointerType::PointerType(Type *contained) PointerType::PointerType(Type *contained)
: Type(Type::PointerTyID, contained->get_module()), contained_(contained) { : Type(Type::PointerTyID, contained->get_module()), contained_(contained) {
static constexpr std::array allowed_elem_type = { static constexpr std::array<Type::TypeID, 4> allowed_elem_type = {
Type::IntegerTyID, Type::FloatTyID, Type::ArrayTyID, Type::PointerTyID}; Type::IntegerTyID, Type::FloatTyID, Type::ArrayTyID, Type::PointerTyID};
auto elem_type_id = contained->get_type_id(); auto elem_type_id = contained->get_type_id();
assert(std::find(allowed_elem_type.begin(), allowed_elem_type.end(), assert(std::find(allowed_elem_type.begin(), allowed_elem_type.end(),
...@@ -185,7 +187,8 @@ PointerType *PointerType::get(Type *contained) { ...@@ -185,7 +187,8 @@ PointerType *PointerType::get(Type *contained) {
return contained->get_module()->get_pointer_type(contained); return contained->get_module()->get_pointer_type(contained);
} }
FloatType::FloatType(Module *m) : Type(Type::FloatTyID, m) {} FloatType::FloatType(Module *m) : Type(Type::FloatTyID, m) {
}
FloatType::~FloatType() = default; FloatType::~FloatType() = default;
......
output_student_ast output_student_ast
\ No newline at end of file output_student
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment