From 62e4b45143e4ff24a1758a2ccd3af5dfe69706ec Mon Sep 17 00:00:00 2001 From: WormHeamer Date: Sun, 3 Aug 2025 18:48:20 -0400 Subject: factor out node stuff into ir.c & ir.h --- ir.h | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 ir.h (limited to 'ir.h') diff --git a/ir.h b/ir.h new file mode 100644 index 0000000..dd2e496 --- /dev/null +++ b/ir.h @@ -0,0 +1,105 @@ +#ifndef IR_H +#define IR_H + +#include "arena.h" +#include "dynarr.h" +#include "lex.h" /* for error reporting only */ +#include "str.h" + +/* nodes */ + +typedef enum { + N_START, + N_RETURN, + N_KEEPALIVE, + N_LIT, + N_OP_ADD, N_OP_SUB, N_OP_MUL, N_OP_DIV, + N_OP_AND, N_OP_OR, N_OP_XOR, + N_OP_SHL, N_OP_SHR, + N_OP_NEG, N_OP_NOT, + N_VALUE +} NodeType; + +const char *node_type_name(NodeType t); + +typedef enum { + T_BOT, + T_TOP, + T_CONST, + T_INT +} Type; + +typedef struct { + Type type; + union { + int64_t i; + uint64_t u; + }; +} Value; + +typedef struct Node { + union { + struct Node *prev_free; + struct { + int id, refs; + int walked; + NodeType type; + LexSpan src_pos; + DYNARR(struct Node *) in, out; + Value val; + }; + }; +} Node; + +/* procedure graph */ + +typedef struct NameBinding { + struct NameBinding *prev; + LexSpan src_pos; + Str name; + Node *node; +} NameBinding; + +typedef struct ScopeFrame { + struct ScopeFrame *prev; + NameBinding *latest; +} ScopeFrame; + +typedef struct { + ScopeFrame *tail, *free_scope; + NameBinding *free_bind; +} Scope; + +typedef struct { + Arena arena; + Str name; + Node *start, *stop, *keepalive; + Node *free_list; + Scope scope; +} Proc; + +void node_kill(Node *n, Proc *p); +void node_die(Node *n, Proc *p); +void node_del_out(Node *n, Node *p); +void node_del_in(Node *n, Node *p); +void node_kill(Node *n, Proc *p); +void node_add(Proc *p, Node *src, Node *dest); +void node_remove(Proc *p, Node *src, Node *dest); +Node *node_new_empty(Proc *p, NodeType t); +Node *node_newv(Proc *p, NodeType t, ...); +Node *node_dedup_lit(Proc *p, Value v); +Node *node_new_lit_i64(Proc *p, int64_t i); +Value node_compute(Node *n, Lexer *l); +Node *node_peephole(Node *n, Proc *p, Lexer *l); + +#define node_new(...) node_newv(__VA_ARGS__, NULL) + +void proc_init(Proc *proc, Str name); +void proc_free(Proc *proc); + +ScopeFrame *scope_push(Scope *scope, Proc *proc); +ScopeFrame *scope_pop(Scope *scope, Proc *proc); +NameBinding *scope_find(Scope *scope, Str name); +NameBinding *scope_bind(Scope *scope, Str name, Node *value, LexSpan pos, Proc *proc); + +#endif -- cgit v1.2.3