CompilerSysY/include/llir_value.h
2023-05-05 21:24:02 +08:00

186 lines
4.9 KiB
C++

#pragma once
#include <memory>
#include <shared_mutex>
#include <string>
#include <vector>
#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;
};
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;
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);
}
};
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;
}
};
class ConstantInt : public Value {
public:
int value;
ConstantInt(const std::string &name, int value) : Value(name, TypeHelper::TYPE_I32), value(value) {}
};
class ConstantArr : public Value {};
class GlobalVarInt : public Value {
public:
int init_value;
GlobalVarInt(const std::string &name, int init_value) : Value(name, TypeHelper::TYPE_I32), init_value(init_value) {}
};
enum class InstTag {
Add,
Sub,
Mod,
Mul,
Div,
Lt,
Le,
Ge,
Gt,
Eq,
Ne,
And,
Or,
Br,
Call,
Ret,
Alloca,
Load,
Store,
GEP,
Zext,
Phi,
MemPhi,
LoadDep,
InsertEle,
ExtractEle
};
typedef std::shared_ptr<Value> ValuePtr_t;
typedef std::shared_ptr<BasicBlock> BasicBlockPtr_t;
typedef std::shared_ptr<Function> FunctionPtr_t;
class Instruction : public Value {
public:
InstTag tag;
std::shared_ptr<BasicBlock> parent_bb;
std::vector<std::shared_ptr<Value>> operand_list;
Instruction(InstTag inst_tag, TypePtr_t type, std::shared_ptr<BasicBlock> parent_bb)
: Value("", type), tag(inst_tag), parent_bb(parent_bb) {}
};
class InstAlloca : public Instruction {
public:
bool isInit = false;
InstAlloca(TypePtr_t alloc_type, bool isInit, std::shared_ptr<BasicBlock> parent_bb)
: Instruction(InstTag::Alloca, std::make_shared<PointerType>(alloc_type), parent_bb), isInit(isInit) {}
};
class InstStore : public Instruction {
public:
InstStore(std::shared_ptr<Value> value, std::shared_ptr<Value> pointer, std::shared_ptr<BasicBlock> parent_bb)
: Instruction(InstTag::Store, TypeHelper::TYPE_VOID, parent_bb) {
operand_list.push_back(value);
operand_list.push_back(pointer);
}
};
class InstLoad : public Instruction {
public:
InstLoad(std::shared_ptr<Value> value, TypePtr_t type, std::shared_ptr<BasicBlock> parent_bb)
: Instruction(InstTag::Load, type, parent_bb) {
operand_list.push_back(value);
}
};
class InstBinary : public Instruction {
public:
InstBinary(InstTag inst_tag, TypePtr_t val_type, std::shared_ptr<Value> op1, std::shared_ptr<Value> op2,
std::shared_ptr<BasicBlock> parent_bb)
: Instruction(inst_tag, val_type, parent_bb) {
operand_list.push_back(op1);
operand_list.push_back(op2);
}
};
class InstZext : public Instruction {
public:
InstZext(std::shared_ptr<Value> op, std::shared_ptr<BasicBlock> parent_bb)
: Instruction(InstTag::Zext, TypeHelper::TYPE_I32, parent_bb) {
operand_list.push_back(op);
}
};
class InstBranch : public Instruction {
public:
// conditional branch
InstBranch(ValuePtr_t cond, BasicBlockPtr_t true_block, BasicBlockPtr_t false_block, BasicBlockPtr_t parent_bb)
: Instruction(InstTag::Br, TypeHelper::TYPE_VOID, parent_bb) {
this->operand_list.push_back(cond);
this->operand_list.push_back(true_block);
this->operand_list.push_back(false_block);
}
// unconditional branch
InstBranch(BasicBlockPtr_t target_block, BasicBlockPtr_t parent_bb)
: Instruction(InstTag::Br, TypeHelper::TYPE_VOID, parent_bb) {
this->operand_list.push_back(target_block);
}
};
class InstReturn : public Instruction {
public:
InstReturn(ValuePtr_t ret_val, BasicBlockPtr_t parent_bb)
: Instruction(InstTag::Ret, TypeHelper::TYPE_VOID, parent_bb) {
this->operand_list.push_back(ret_val);
}
InstReturn(BasicBlockPtr_t parent_bb) : Instruction(InstTag::Ret, TypeHelper::TYPE_VOID, parent_bb) {}
};
class InstCall : public Instruction {
public:
InstCall(FunctionPtr_t func, const std::vector<ValuePtr_t> &args, BasicBlockPtr_t parent_bb)
: Instruction(InstTag::Call, func->get_type()->return_type, parent_bb) {
operand_list.push_back(func);
for (auto arg : args) {
operand_list.push_back(arg);
}
}
};
} // namespace antlrSysY