lang/ir.h

104 lines
1.4 KiB
C
Raw Normal View History

2021-12-21 01:18:22 +01:00
#ifndef __IR_H__
#define __IR_H__
#include "tok.h"
2021-12-22 16:09:52 +01:00
typedef struct BuiltinFunc {
char *name;
bool side_effects : 1;
size_t n_args;
Value (*func)(Value *args);
size_t fid; /* function ID, assigned automatically */
} BuiltinFunc;
2021-12-21 01:18:22 +01:00
enum IRInstr {
IRSet,
IRNeg,
IRAdd,
IRSub,
IRMul,
IRDiv,
2021-12-23 21:06:49 +01:00
IREq,
2021-12-26 12:19:54 +01:00
IRNeq,
2021-12-23 21:06:49 +01:00
IRLt,
IRLe,
IRNot,
2021-12-23 21:42:09 +01:00
IRAnd,
IROr,
2021-12-21 13:59:08 +01:00
IRJmp,
2021-12-21 01:18:22 +01:00
IRJnz,
2021-12-22 16:09:52 +01:00
IRCallInternal,
2021-12-21 01:18:22 +01:00
IRInstrEnumSize,
};
typedef enum IRInstr IRInstr;
extern const char *irinstr_str[IRInstrEnumSize];
typedef struct IRParam {
enum {
IRParamNull = 0,
IRParamLiteral,
IRParamAddr,
} kind;
union {
Value Literal;
size_t Addr;
};
} IRParam;
typedef struct IRArgs {
struct IRArgs *next;
IRParam param;
} IRArgs;
typedef struct IRTok {
size_t ln, col;
IRInstr instr;
union {
struct {
size_t addr;
IRParam val;
} Unary;
struct {
size_t addr;
IRParam lhs, rhs;
2021-12-23 20:10:02 +01:00
} Binary;
2021-12-21 01:18:22 +01:00
2021-12-21 13:59:08 +01:00
struct {
size_t iaddr;
} Jmp;
2021-12-21 01:18:22 +01:00
struct {
size_t iaddr;
IRParam condition;
} CJmp;
2021-12-22 16:09:52 +01:00
struct {
size_t ret_addr;
size_t fid;
IRParam *args;
} CallI;
2021-12-21 01:18:22 +01:00
};
} IRTok;
typedef struct IRToks {
size_t len, cap;
IRTok *toks;
} IRToks;
void irtoks_init_long(IRToks *v);
void irtoks_init_short(IRToks *v);
2021-12-21 01:18:22 +01:00
void irtoks_term(IRToks *v);
void irtoks_app(IRToks *v, IRTok t);
void irtoks_eat_irtoks(IRToks *v, IRToks *other, size_t jmp_offset);
2021-12-21 01:18:22 +01:00
2021-12-22 16:09:52 +01:00
void print_ir(IRToks *v, const BuiltinFunc *builtin_funcs);
2021-12-21 01:18:22 +01:00
void optimize_ir(IRToks *v);
2021-12-21 01:18:22 +01:00
#endif /* IR_H */