types fix, frame fix to work with runtime

This commit is contained in:
ProgramSnail 2024-11-01 20:54:48 +03:00
parent 26a42d4c81
commit 39715334c7
5 changed files with 92 additions and 176 deletions

View file

@ -30,5 +30,4 @@ void s_enter_f(struct State *s, char *func_ip, size_t params_sz,
void s_exit_f(struct State *s);
union VarT **var_by_category(struct State *s, enum VarCategory category,
int id);
void **var_by_category(struct State *s, enum VarCategory category, int id);

View file

@ -1,131 +1,50 @@
#pragma once
#include "../../runtime/runtime.h"
#include "../../runtime/runtime_common.h"
#include "parser.h"
#include <stdint.h>
// ------ Var ------
// ------ General ------
enum Type {
NIL_T = 0x00000000,
INT_T = 0x00000001,
BOX_T = 0x00000002,
STR_T = 0x00000003,
CLOJURE_T = 0x00000004,
ARRAY_T = 0x00000005,
SEXP_T = 0x00000006,
FUN_T = 0x00000007
STR_T = STRING_TAG,
ARRAY_T = ARRAY_TAG,
SEXP_T = SEXP_TAG,
CLOJURE_T = CLOSURE_TAG,
};
struct NilT { // AnyVarT too
uint32_t data_header;
};
struct IntT {
uint32_t data_header;
int32_t value;
};
struct BoxT {
uint32_t data_header;
struct NilT **value;
};
struct StrT {
uint32_t data_header; // param - is not const (0 for const, 1 for not const)
const char *value;
};
struct ClojureT { // TODO
uint32_t data_header;
char *fun_ip;
struct ArrayT *vars;
};
// struct ListT {
// uint32_t data_header;
// struct NilT *value;
// struct NilT *next;
// };
struct ArrayT {
uint32_t data_header;
struct NilT **values;
};
static const size_t MAX_ARRAY_SIZE = 0x11111110;
struct SExpT {
uint32_t data_header;
const char *tag;
struct NilT **values;
};
struct FunT {
uint32_t data_header;
char *fun_ip;
};
union VarT {
struct NilT nil;
struct IntT int_t;
struct BoxT box;
struct StrT str;
struct ClojureT clojure;
// struct ListT list;
struct ArrayT array;
struct SExpT sexp;
struct FunT fun;
};
// same to TAG in runtime
static inline enum Type dh_type(int data_header) {
return (enum Type)(data_header & 0x00000007);
}
// same to LEN in runtime
static inline int dh_param(int data_header) {
return (data_header & 0xFFFFFFF8) >> 3;
}
static inline union VarT *to_var(struct NilT *var) { return (union VarT *)var; }
// ------ Frame ------
// TODO: store boxed offsets instead
struct Frame {
struct NilT *ret; // store returned value
char *rp; // ret instruction pointer
struct Frame *prev_fp; // ret function frame pointer
void **params; // store arguments
void **locals; // store locals
void **end; // store locals
void *ret; // store returned value [gc pointer]
char *rp; // ret instruction pointer [not gc pointer]
size_t to_prev_fp_box; // ret function frame pointer [boxed value, not gc
// pointer]
size_t args_sz_box; // store arguments [boxed value, not gc pointer]
size_t locals_sz_box; // store locals [boxed value, not gc pointer]
};
static inline uint64_t frame_locals_sz(struct Frame *frame) {
return frame->locals - frame->params;
}
static inline uint64_t frame_params_sz(struct Frame *frame) {
return frame->end - frame->locals;
}
size_t frame_sz();
void **f_prev_fp(struct Frame *fp);
uint64_t f_locals_sz(struct Frame *fp);
uint64_t f_args_sz(struct Frame *fp);
void **f_locals(struct Frame *fp);
void **f_args(struct Frame *fp);
// ------ State ------
union StackValue {
union VarT *var;
union VarT **var_ptr;
struct Frame frame; // ??
char *addr;
};
// static inline StackValue *to_sv(void *var) { return (StackValue *)var; }
struct State {
void **stack; // vaid**
void **vp; // stack pointer
void **sp; // stack pointer
struct Frame *fp; // function frame pointer
char *ip; // instruction pointer
char *prev_ip; // prev instruction pointer
char *prev_ip; // prev instruction pointer (to remember jmp locations)
};
struct State init_state(bytefile *bf);
@ -137,12 +56,7 @@ enum VarCategory {
VAR_GLOBAL = 0,
VAR_LOCAL = 1,
VAR_ARGUMENT = 2,
VAR_C = 3 // TODO: constant ??
VAR_C = 3 // TODO: constants ??
};
static inline enum VarCategory to_var_category(uint8_t category) {
if (category > 3) {
failure("unexpected variable category");
}
return (enum VarCategory)category;
}
enum VarCategory to_var_category(uint8_t category);