codegen.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef CODEGEN_H
  2. #define CODEGEN_H
  3. #include "std.h"
  4. enum{
  5. IR_JUMP,IR_JUMPT,IR_JUMPF,IR_JUMPGE,
  6. IR_SEQ,IR_MOVE,IR_MEM,IR_LOCAL,IR_GLOBAL,IR_ARG,IR_CONST,
  7. IR_JSR,IR_RET,IR_AND,IR_OR,IR_XOR,IR_SHL,IR_SHR,IR_SAR,
  8. IR_CALL,IR_RETURN,IR_CAST,
  9. IR_NEG,IR_ADD,IR_SUB,IR_MUL,IR_DIV,
  10. IR_SETEQ,IR_SETNE,IR_SETLT,IR_SETGT,IR_SETLE,IR_SETGE,
  11. IR_FCALL,IR_FRETURN,IR_FCAST,
  12. IR_FNEG,IR_FADD,IR_FSUB,IR_FMUL,IR_FDIV,
  13. IR_FSETEQ,IR_FSETNE,IR_FSETLT,IR_FSETGT,IR_FSETLE,IR_FSETGE,
  14. };
  15. struct TNode{
  16. int op; //opcode
  17. TNode *l,*r; //args
  18. int iconst; //for CONST type_int
  19. string sconst; //for CONST type_string
  20. TNode( int op,TNode *l=0,TNode *r=0 ):op(op),l(l),r(r),iconst(0){}
  21. TNode( int op,TNode *l,TNode *r,int i ):op(op),l(l),r(r),iconst(i){}
  22. TNode( int op,TNode *l,TNode *r,const string &s ):op(op),l(l),r(r),iconst(0),sconst(s){}
  23. ~TNode(){ delete l;delete r; }
  24. void log();
  25. };
  26. class Codegen{
  27. public:
  28. ostream &out;
  29. bool debug;
  30. Codegen( ostream &out,bool debug ):out( out ),debug( debug ){}
  31. virtual void enter( const string &l,int frameSize )=0;
  32. virtual void code( TNode *code )=0;
  33. virtual void leave( TNode *cleanup,int pop_sz )=0;
  34. virtual void label( const string &l )=0;
  35. virtual void i_data( int i,const string &l="" )=0;
  36. virtual void s_data( const string &s,const string &l="" )=0;
  37. virtual void p_data( const string &p,const string &l="" )=0;
  38. virtual void align_data( int n )=0;
  39. virtual void flush()=0;
  40. };
  41. #endif