Files
BlitzNext/compiler/lib/codegen.hpp
T

93 lines
1.7 KiB
C++
Raw Normal View History

2019-01-19 18:28:07 +01:00
#pragma once
#include <string>
#include <ostream>
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
enum {
IR_JUMP,
IR_JUMPT,
IR_JUMPF,
IR_JUMPGE,
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
IR_SEQ,
IR_MOVE,
IR_MEM,
IR_LOCAL,
IR_GLOBAL,
IR_ARG,
IR_CONST,
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
IR_JSR,
IR_RET,
IR_AND,
IR_OR,
IR_XOR,
IR_SHL,
IR_SHR,
IR_SAR,
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
IR_CALL,
IR_RETURN,
IR_CAST,
IR_NEG,
IR_ADD,
IR_SUB,
IR_MUL,
IR_DIV,
IR_SETEQ,
IR_SETNE,
IR_SETLT,
IR_SETGT,
IR_SETLE,
IR_SETGE,
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
IR_FCALL,
IR_FRETURN,
IR_FCAST,
IR_FNEG,
IR_FADD,
IR_FSUB,
IR_FMUL,
IR_FDIV,
IR_FSETEQ,
IR_FSETNE,
IR_FSETLT,
IR_FSETGT,
IR_FSETLE,
IR_FSETGE,
2014-01-31 08:23:00 +13:00
};
2019-01-18 17:04:57 +01:00
struct TNode {
int op; //opcode
TNode *l, *r; //args
int iconst; //for CONST type_int
2019-01-19 18:28:07 +01:00
std::string sconst; //for CONST type_string
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
TNode(int op, TNode* l = 0, TNode* r = 0) : op(op), l(l), r(r), iconst(0) {}
TNode(int op, TNode* l, TNode* r, int i) : op(op), l(l), r(r), iconst(i) {}
2019-01-19 18:28:07 +01:00
TNode(int op, TNode* l, TNode* r, const std::string& s) : op(op), l(l), r(r), iconst(0), sconst(s) {}
2019-01-18 17:04:57 +01:00
~TNode()
{
delete l;
delete r;
}
2014-01-31 08:23:00 +13:00
void log();
};
2019-01-18 17:04:57 +01:00
class Codegen {
public:
2019-01-19 18:28:07 +01:00
std::ostream& out;
2019-01-18 17:04:57 +01:00
bool debug;
2019-01-19 18:28:07 +01:00
Codegen(std::ostream& out, bool debug) : out(out), debug(debug) {}
2019-01-18 17:04:57 +01:00
2019-01-19 18:28:07 +01:00
virtual void enter(const std::string& l, int frameSize) = 0;
2019-01-18 17:04:57 +01:00
virtual void code(TNode* code) = 0;
virtual void leave(TNode* cleanup, int pop_sz) = 0;
2019-01-19 18:28:07 +01:00
virtual void label(const std::string& l) = 0;
virtual void i_data(int i, const std::string& l = "") = 0;
virtual void s_data(const std::string& s, const std::string& l = "") = 0;
virtual void p_data(const std::string& p, const std::string& l = "") = 0;
2019-01-18 17:04:57 +01:00
virtual void align_data(int n) = 0;
virtual void flush() = 0;
2014-01-31 08:23:00 +13:00
};