larpcc/ir.h
2026-11-16 16:34:17 +01:00

86 lines
1.5 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_Fn,
IrVal_Array,
} IrValTy;
typedef enum {
IrInst_Val,
IrInst_Call,
} IrInstTy;
typedef struct IrVal IrVal;
typedef struct IrInst IrInst;
typedef struct IrFn IrFn;
struct IrVal {
IrValTy ty;
union {
int8_t i8_val;
IrFn* fn;
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