Skip to content

Commit

Permalink
Added opcode generator
Browse files Browse the repository at this point in the history
Added generation opcode, opcode name, creator of classes in Graph
  • Loading branch information
techie-mike committed Sep 27, 2023
1 parent 28af16f commit e3d68d4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
25 changes: 10 additions & 15 deletions src/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,18 @@ class Graph
void SetMethodName(const std::string& name);
void Dump(std::ostream &out);

/*
* Functions to create instructions
*/
auto* CreateConstant() {
auto inst = new ConstantInst();
inst->SetId(all_inst_.size());
all_inst_.push_back(inst);
return inst;
#define CREATE_CREATORS(OPCODE) \
template <typename... Args> \
auto* Create##OPCODE##Inst(Args&&... args) { \
auto inst = new OPCODE##Inst(std::forward<Args>(args)...); \
inst->SetId(all_inst_.size()); \
all_inst_.push_back(inst); \
return inst; \
}

template <typename... Args>
auto* CreateAddInst(Args&&... args) {
auto inst = new AddInst(std::forward<Args>(args)...);
inst->SetId(all_inst_.size());
all_inst_.push_back(inst);
return inst;
}
OPCODE_LIST(CREATE_CREATORS)

#undef CREATE_CREATORS

private:
std::vector<Inst *> all_inst_;
Expand Down
4 changes: 2 additions & 2 deletions src/inst.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ class AddInst : public FixedInputs<2>
{
public:
AddInst(Type type, Inst *input0, Inst *input1):
FixedInputs<2>(Opcode::ADD, type, {{input0, input1}}) {}
FixedInputs<2>(Opcode::Add, type, {{input0, input1}}) {}

};

class ConstantInst : public Inst, public ImmidiateProperty
{
public:
ConstantInst():
Inst(Opcode::CONSTANT, Type::INT64),
Inst(Opcode::Constant, Type::INT64),
ImmidiateProperty(0) {}

void DumpInputs(std::ostream &out) const override {
Expand Down
31 changes: 20 additions & 11 deletions src/opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@

namespace compiler {

#define OPCODE_LIST(ACTION) \
ACTION( Add ) \
ACTION( Constant )

enum class Opcode {
NONE = 0,
ADD,
SUB,
MUL,
DIV,
CONSTANT,

#define CREATE_OPCODE(OPCODE) \
OPCODE,

OPCODE_LIST(CREATE_OPCODE)

#undef CREATE_OPCODE

NUM_OPCODES
};

constexpr std::array<const char *const, static_cast<size_t>(Opcode::NUM_OPCODES)> OPCODE_NAME {
"NONE",
"Add",
"Sub",
"Mul",
"Div",
"Constant"
"INVALID",

#define CREATE_NAMES(OPCODE) \
#OPCODE,

OPCODE_LIST(CREATE_NAMES)

#undef CREATE_NAMES
};

enum class Type {
Expand Down
6 changes: 3 additions & 3 deletions tests/graph_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TEST(GraphTest, DumpEmptyGraph) {
TEST(GraphTest, CreateConstant) {
Graph graph;
graph.SetMethodName("Only const");
auto *cnst = graph.CreateConstant();
auto *cnst = graph.CreateConstantInst();
cnst->SetImm(1);
std::ostringstream dump_out;
std::string output =
Expand All @@ -34,9 +34,9 @@ TEST(GraphTest, CreateConstant) {

TEST(GraphTest, CreateAdd) {
Graph graph;
auto *cnst0 = graph.CreateConstant();
auto *cnst0 = graph.CreateConstantInst();
cnst0->SetImm(0);
auto *cnst1 = graph.CreateConstant();
auto *cnst1 = graph.CreateConstantInst();
cnst1->SetImm(1);
auto *inst = graph.CreateAddInst(Type::INT64, cnst0, cnst1);
std::ostringstream dump_out;
Expand Down

0 comments on commit e3d68d4

Please sign in to comment.