120 lines
3.6 KiB
C++
120 lines
3.6 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
#include <shared_mutex>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "3rdparty/easylogging++.h"
|
|
#include "common.h"
|
|
#include "llir_type.h"
|
|
|
|
namespace antlrSysY {
|
|
|
|
class Value {
|
|
public:
|
|
std::string name;
|
|
TypePtr_t type;
|
|
Value(const std::string &name, TypePtr_t type) : name(name), type(type) {}
|
|
virtual ~Value() = default;
|
|
template <typename TT> static bool isType(std::shared_ptr<Value> ptr) {
|
|
if (ptr.get()) {
|
|
auto &r = *ptr.get();
|
|
return typeid(r) == typeid(TT);
|
|
}
|
|
LOG(WARNING) << "Comparing with nullptr";
|
|
return false;
|
|
}
|
|
virtual std::string to_string() {
|
|
return name + ": " + type->to_string();
|
|
}
|
|
};
|
|
|
|
class User : public Value {
|
|
public:
|
|
};
|
|
|
|
class BasicBlock;
|
|
class Instruction;
|
|
|
|
class FParam : public Value {
|
|
public:
|
|
FParam(const std::string &name, TypePtr_t type) : Value(name, type) {}
|
|
};
|
|
|
|
class Function : public Value {
|
|
public:
|
|
std::vector<std::shared_ptr<FParam>> fparam_list;
|
|
std::vector<std::shared_ptr<BasicBlock>> bb_list;
|
|
// we use empty basic_block to represent lib function
|
|
Function(const std::string &name, TypePtr_t ret_type) : Value(name, std::make_shared<FunctionType>(ret_type)) {}
|
|
std::shared_ptr<FunctionType> get_type() {
|
|
return std::dynamic_pointer_cast<FunctionType>(type);
|
|
}
|
|
bool is_libfunc() {
|
|
return bb_list.size() == 0;
|
|
}
|
|
virtual std::string to_string() override {
|
|
std::string params_str;
|
|
for (auto fparam : fparam_list) {
|
|
params_str += fparam->to_string() + ",";
|
|
}
|
|
return name + "(" + params_str + ") -> " + type->to_string();
|
|
}
|
|
};
|
|
|
|
class BasicBlock : public Value {
|
|
public:
|
|
std::vector<std::shared_ptr<Instruction>> inst_list;
|
|
std::shared_ptr<Function> parent;
|
|
std::vector<std::shared_ptr<BasicBlock>> successors;
|
|
std::vector<std::shared_ptr<BasicBlock>> predecessors;
|
|
BasicBlock(const std::string &name, std::shared_ptr<Function> parent) : Value(name, TypeHelper::TYPE_LABEL) {
|
|
this->parent = parent;
|
|
}
|
|
};
|
|
|
|
typedef std::shared_ptr<Value> ValuePtr_t;
|
|
typedef std::shared_ptr<BasicBlock> BasicBlockPtr_t;
|
|
typedef std::shared_ptr<Function> FunctionPtr_t;
|
|
|
|
class Constant : public Value {
|
|
public:
|
|
Constant(const std::string &name, TypePtr_t type) : Value(name, type) {}
|
|
};
|
|
|
|
class ConstantInt : public Constant {
|
|
public:
|
|
int value;
|
|
ConstantInt(const std::string &name, int value) : Constant(name, TypeHelper::TYPE_I32), value(value) {}
|
|
static std::shared_ptr<ConstantInt> make_shared(int value) {
|
|
return std::make_shared<ConstantInt>("", value);
|
|
}
|
|
};
|
|
|
|
class ConstantArr : public Constant {
|
|
public:
|
|
std::vector<ValuePtr_t> value_list;
|
|
ConstantArr(const std::string &name, const std::vector<ValuePtr_t> &value_list, std::shared_ptr<ArrayType> type)
|
|
: Constant(name, type), value_list(value_list) {}
|
|
static std::shared_ptr<ConstantArr> make_shared(const std::string &name, const std::vector<ValuePtr_t> &value_list,
|
|
std::shared_ptr<ArrayType> type) {
|
|
return std::make_shared<ConstantArr>(name, value_list, type);
|
|
}
|
|
};
|
|
|
|
typedef std::shared_ptr<Constant> ConstantPtr_t;
|
|
|
|
// emmm, actually it is more of a Instruction type
|
|
class GlobalVar : public Value {
|
|
public:
|
|
ConstantPtr_t init_value;
|
|
bool is_const;
|
|
GlobalVar(const std::string &name, ConstantPtr_t init_value, bool is_const)
|
|
: Value(name, PointerType::make_shared(init_value->type)), init_value(init_value), is_const(is_const) {}
|
|
static std::shared_ptr<GlobalVar> make_shared(const std::string &name, ConstantPtr_t init_value, bool is_const) {
|
|
return std::make_shared<GlobalVar>(name, init_value, is_const);
|
|
}
|
|
};
|
|
|
|
} // namespace antlrSysY
|