Commit c396901a authored by Yang's avatar Yang

add names api

parent a6d37a3d
...@@ -81,11 +81,14 @@ class BasicBlock : public Value { ...@@ -81,11 +81,14 @@ class BasicBlock : public Value {
// 用于 lldb 调试生成 summary // 用于 lldb 调试生成 summary
std::string safe_print() const; std::string safe_print() const;
private: private:
explicit BasicBlock(Module *m, const std::string &name, Function *parent); explicit BasicBlock(const Module *m, const std::string &name, Function *parent);
std::list<BasicBlock *> pre_bbs_; std::list<BasicBlock *> pre_bbs_;
std::list<BasicBlock *> succ_bbs_; std::list<BasicBlock *> succ_bbs_;
std::list<Instruction*> instr_list_; std::list<Instruction*> instr_list_;
Function *parent_; Function *parent_;
}; };
extern Names GLOBAL_BASICBLOCK_NAMES_;
\ No newline at end of file
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include <list> #include <list>
#include <memory> #include <memory>
#include "Names.hpp"
class Module; class Module;
class Argument; class Argument;
class Type; class Type;
...@@ -49,6 +51,11 @@ class Function : public Value { ...@@ -49,6 +51,11 @@ class Function : public Value {
// 用于 lldb 调试生成 summary // 用于 lldb 调试生成 summary
std::string safe_print() const; std::string safe_print() const;
// 用于给 basicblock 分配名称
Names names4blocks_;
Names names4insts_;
private: private:
std::list<BasicBlock*> basic_blocks_; std::list<BasicBlock*> basic_blocks_;
std::list<Argument> arguments_; std::list<Argument> arguments_;
......
...@@ -105,9 +105,9 @@ class IRBuilder { ...@@ -105,9 +105,9 @@ class IRBuilder {
return LoadInst::create_load(ptr, this->BB_); return LoadInst::create_load(ptr, this->BB_);
} }
AllocaInst *create_alloca(Type *ty) const AllocaInst *create_alloca(Type *ty, const std::string& name = "") const
{ {
return AllocaInst::create_alloca(ty, this->BB_->get_entry_block_of_same_function()); return AllocaInst::create_alloca(ty, this->BB_->get_entry_block_of_same_function(), name);
} }
ZextInst *create_zext(Value *val, Type *ty) const ZextInst *create_zext(Value *val, Type *ty) const
......
This diff is collapsed.
#ifndef NAMES_HPP
#define NAMES_HPP
#include <string>
#include <unordered_map>
class Names
{
public:
explicit Names(bool use_underline, std::string prefix, std::string append)
: use_underline_(use_underline), default_prefix_used_count_(0), default_prefix_(std::move(prefix)),
appended_prefix_(std::move(append))
{
}
// 获得一个 Names 内唯一的名称,会保持唯一的同时尽可能的与 names 类似
std::string get_name(std::string name);
// 获得一个 Names 内唯一的名称
std::string get_name();
private:
bool use_underline_;
int default_prefix_used_count_;
std::string default_prefix_;
std::string appended_prefix_;
std::unordered_map<std::string, int> allocated_;
};
#endif // !NAMES_HPP
...@@ -91,7 +91,7 @@ Value* CminusfBuilder::visit(ASTVarDeclaration &node) { ...@@ -91,7 +91,7 @@ Value* CminusfBuilder::visit(ASTVarDeclaration &node) {
auto nowBB = builder->get_insert_block(); auto nowBB = builder->get_insert_block();
auto entryBB = context.func->get_entry_block(); auto entryBB = context.func->get_entry_block();
builder->set_insert_point(entryBB); builder->set_insert_point(entryBB);
auto *var = builder->create_alloca(var_type); auto *var = builder->create_alloca(var_type, node.id);
builder->set_insert_point(nowBB); builder->set_insert_point(nowBB);
scope.push(node.id, var); scope.push(node.id, var);
} else { } else {
...@@ -99,7 +99,7 @@ Value* CminusfBuilder::visit(ASTVarDeclaration &node) { ...@@ -99,7 +99,7 @@ Value* CminusfBuilder::visit(ASTVarDeclaration &node) {
auto entryBB = context.func->get_entry_block(); auto entryBB = context.func->get_entry_block();
builder->set_insert_point(entryBB); builder->set_insert_point(entryBB);
auto *array_type = ArrayType::get(var_type, node.num->i_val); auto *array_type = ArrayType::get(var_type, node.num->i_val);
auto *var = builder->create_alloca(array_type); auto *var = builder->create_alloca(array_type, node.id);
builder->set_insert_point(nowBB); builder->set_insert_point(nowBB);
scope.push(node.id, var); scope.push(node.id, var);
} }
...@@ -149,18 +149,18 @@ Value* CminusfBuilder::visit(ASTFunDeclaration &node) { ...@@ -149,18 +149,18 @@ Value* CminusfBuilder::visit(ASTFunDeclaration &node) {
if (node.params[i]->isarray) { if (node.params[i]->isarray) {
Value *array_alloc; Value *array_alloc;
if (node.params[i]->type == TYPE_INT) { if (node.params[i]->type == TYPE_INT) {
array_alloc = builder->create_alloca(INT32PTR_T); array_alloc = builder->create_alloca(INT32PTR_T, node.params[i]->id);
} else { } else {
array_alloc = builder->create_alloca(FLOATPTR_T); array_alloc = builder->create_alloca(FLOATPTR_T, node.params[i]->id);
} }
builder->create_store(args[i], array_alloc); builder->create_store(args[i], array_alloc);
scope.push(node.params[i]->id, array_alloc); scope.push(node.params[i]->id, array_alloc);
} else { } else {
Value *alloc; Value *alloc;
if (node.params[i]->type == TYPE_INT) { if (node.params[i]->type == TYPE_INT) {
alloc = builder->create_alloca(INT32_T); alloc = builder->create_alloca(INT32_T, node.params[i]->id);
} else { } else {
alloc = builder->create_alloca(FLOAT_T); alloc = builder->create_alloca(FLOAT_T, node.params[i]->id);
} }
builder->create_store(args[i], alloc); builder->create_store(args[i], alloc);
scope.push(node.params[i]->id, alloc); scope.push(node.params[i]->id, alloc);
......
...@@ -7,9 +7,13 @@ ...@@ -7,9 +7,13 @@
#include <cassert> #include <cassert>
BasicBlock::BasicBlock(Module *m, const std::string &name = "", #include "Names.hpp"
Function *parent = nullptr)
: Value(m->get_label_type(), name), parent_(parent) { BasicBlock::BasicBlock(const Module* m, const std::string& name = "",
Function* parent = nullptr)
: Value(m->get_label_type(),
parent == nullptr ? GLOBAL_BASICBLOCK_NAMES_.get_name(name) : parent->names4blocks_.get_name(name))
, parent_(parent) {
assert(parent && "currently parent should not be nullptr"); assert(parent && "currently parent should not be nullptr");
parent_->add_basic_block(this); parent_->add_basic_block(this);
} }
...@@ -105,12 +109,9 @@ std::string BasicBlock::print() { ...@@ -105,12 +109,9 @@ std::string BasicBlock::print() {
std::string BasicBlock::safe_print() const std::string BasicBlock::safe_print() const
{ {
Function* parent = parent_;
auto parentName = ((parent != nullptr) ? parent->get_name() : "f<null>");
auto name = get_name(); auto name = get_name();
if (name.empty()) name = "b" + ptr_to_str(this); if (name.empty()) name = "b" + ptr_to_str(this);
if (parentName.empty()) parentName = "f" + ptr_to_str(parent); return name + " " + std::to_string(instr_list_.size()) + "inst pre " + std::to_string(pre_bbs_.size()) + "b suc " + std::to_string(succ_bbs_.size()) + "b";
return parentName + ":" + name + " " + std::to_string(instr_list_.size()) + "inst pre " + std::to_string(pre_bbs_.size()) + "b suc " + std::to_string(succ_bbs_.size()) + "b";
} }
BasicBlock::~BasicBlock() BasicBlock::~BasicBlock()
...@@ -123,3 +124,5 @@ BasicBlock* BasicBlock::get_entry_block_of_same_function() const ...@@ -123,3 +124,5 @@ BasicBlock* BasicBlock::get_entry_block_of_same_function() const
assert(parent_ != nullptr && "bb have no parent function"); assert(parent_ != nullptr && "bb have no parent function");
return parent_->get_entry_block(); return parent_->get_entry_block();
} }
Names GLOBAL_BASICBLOCK_NAMES_{false, "label", ""};
\ No newline at end of file
...@@ -10,7 +10,7 @@ add_library( ...@@ -10,7 +10,7 @@ add_library(
Instruction.cpp Instruction.cpp
Module.cpp Module.cpp
IRprinter.cpp IRprinter.cpp
) Names.cpp)
target_link_libraries( target_link_libraries(
IR_lib IR_lib
......
...@@ -6,8 +6,17 @@ ...@@ -6,8 +6,17 @@
#include <unordered_set> #include <unordered_set>
#include <queue> #include <queue>
namespace
{
std::string chopName(std::string name)
{
if (name.size() > 4) return { name.begin(), name.begin() + 4 };
return name;
}
}
Function::Function(FunctionType* ty, const std::string& name, Module* parent) Function::Function(FunctionType* ty, const std::string& name, Module* parent)
: Value(ty, name), parent_(parent), seq_cnt_(0) { : Value(ty, name), names4blocks_(false, "label", chopName(name)), names4insts_(true, "op", ""), parent_(parent), seq_cnt_(0) {
// num_args_ = ty->getNumParams(); // num_args_ = ty->getNumParams();
parent->add_function(this); parent->add_function(this);
// build args // build args
......
This diff is collapsed.
#include "Names.hpp"
std::string Names::get_name(std::string name)
{
int ed = static_cast<int>(name.size());
while (ed > 0)
{
char ch = name[ed - 1];
if ((use_underline_ && ch == '_') || (ch >= '0' && ch <= '9')) ed--;
else break;
}
if (ed == 0) return get_name();
std::string name1 = {name.begin(), name.begin() + ed};
if (name1 == default_prefix_) return get_name();
auto get = appended_prefix_ + name1;
auto idx = allocated_[name1]++;
if (idx == 0) return get;
if (use_underline_) get += "_";
return get + std::to_string(idx);
}
std::string Names::get_name()
{
auto get = appended_prefix_ + default_prefix_;
return get + std::to_string(++default_prefix_used_count_);
}
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