91 lines
1.6 KiB
C
91 lines
1.6 KiB
C
#ifndef IR_H
|
|
#define IR_H
|
|
|
|
#include "ast.h"
|
|
#include "collection.h"
|
|
#include "sema.h"
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
|
|
typedef enum {
|
|
IrVal_I8,
|
|
IrVal_I32,
|
|
IrVal_Fn,
|
|
IrVal_Array,
|
|
IrVal_AddrOf,
|
|
} IrValTy;
|
|
|
|
typedef enum {
|
|
IrInst_Val,
|
|
IrInst_Call,
|
|
IrInst_Ret,
|
|
} IrInstTy;
|
|
|
|
typedef struct IrVal IrVal;
|
|
typedef struct IrInst IrInst;
|
|
typedef struct IrFn IrFn;
|
|
|
|
struct IrVal {
|
|
IrValTy ty;
|
|
union {
|
|
int8_t i8_val;
|
|
int32_t i32_val;
|
|
IrFn* fn;
|
|
IrVal* irval;
|
|
struct {
|
|
IrVal** elems;
|
|
size_t elems_size;
|
|
size_t size_of_elem;
|
|
} array;
|
|
};
|
|
};
|
|
|
|
struct IrInst {
|
|
IrInstTy ty;
|
|
union {
|
|
IrVal* val;
|
|
struct {
|
|
IrInst* expr;
|
|
IrInst** args;
|
|
size_t args_size;
|
|
} call;
|
|
};
|
|
};
|
|
|
|
typedef struct {
|
|
ARRAY_FIELD(IrInst*, insts);
|
|
} IrBlock;
|
|
|
|
struct IrFn {
|
|
char* ident;
|
|
ARRAY_FIELD(IrBlock*, blocks);
|
|
bool defined;
|
|
};
|
|
|
|
typedef struct {
|
|
Arena arena;
|
|
ARRAY_FIELD(IrFn*, fns);
|
|
ARRAY_FIELD(IrVal*, readonly_vals);
|
|
} IrCx;
|
|
|
|
typedef struct {
|
|
IrCx cx;
|
|
Arena* a;
|
|
Sema* sema;
|
|
IrFn* curr_fn;
|
|
IrBlock* curr_block;
|
|
Keymap sym_map;
|
|
} AstLowerer;
|
|
|
|
void astlowerer_init(AstLowerer* l, Sema* sema);
|
|
void astlowerer_deinit(AstLowerer* l);
|
|
|
|
void astlowerer_lower_top_stmt(AstLowerer* l, AstStmt* stmt);
|
|
|
|
void irval_dump(const IrVal* val, FILE* file, Keymap* inst_ids);
|
|
void irinst_dump(const IrInst* val, FILE* file, Keymap* inst_ids);
|
|
void irfn_dump(const IrFn* val, FILE* file);
|
|
|
|
#endif
|