Skip to content

Commit

Permalink
inline more small functions from hll_value
Browse files Browse the repository at this point in the history
  • Loading branch information
Holodome committed Oct 17, 2023
1 parent ce7bb57 commit 2e8e2d4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 114 deletions.
85 changes: 0 additions & 85 deletions hololisp/hll_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,6 @@ static void register_gc_obj(hll_vm *vm, hll_obj *obj) {
vm->gc->all_objs = obj;
}

hll_value hll_nil(void) { return nan_box_singleton(HLL_VALUE_NIL); }
hll_value hll_true(void) { return nan_box_singleton(HLL_VALUE_TRUE); }

hll_value hll_num(double num) {
hll_value value;
memcpy(&value, &num, sizeof(hll_value));
return value;
}

hll_value hll_new_symbol(hll_vm *vm, const char *symbol, size_t length) {
assert(symbol != NULL);
assert(length != 0);
Expand Down Expand Up @@ -138,82 +129,6 @@ hll_value hll_new_func(hll_vm *vm, hll_value params, hll_bytecode *bytecode) {
return nan_box_ptr(obj);
}

hll_obj_cons *hll_unwrap_cons(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_CONS);
return (hll_obj_cons *)obj->as;
}

const char *hll_unwrap_zsymb(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_SYMB);
return ((hll_obj_symb *)obj->as)->symb;
}

hll_obj_symb *hll_unwrap_symb(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_SYMB);
return (hll_obj_symb *)obj->as;
}

hll_value hll_unwrap_cdr(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_CONS);
return ((hll_obj_cons *)obj->as)->cdr;
}

hll_value hll_unwrap_car(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_CONS);
return ((hll_obj_cons *)obj->as)->car;
}

hll_obj_bind *hll_unwrap_bind(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_BIND);
return (hll_obj_bind *)obj->as;
}

hll_obj_env *hll_unwrap_env(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_ENV);
return (hll_obj_env *)obj->as;
}

hll_obj_func *hll_unwrap_func(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_FUNC);
return (hll_obj_func *)obj->as;
}

double hll_unwrap_num(hll_value value) {
assert(hll_is_num(value));
double result;
memcpy(&result, &value, sizeof(hll_value));
return result;
}

hll_obj *hll_unwrap_obj(hll_value value) {
assert(hll_is_obj(value));
return nan_unbox_ptr(value);
}

void hll_setcar(hll_value cons, hll_value car) {
hll_unwrap_cons(cons)->car = car;
}

void hll_setcdr(hll_value cons, hll_value cdr) {
hll_unwrap_cons(cons)->cdr = cdr;
}

size_t hll_list_length(hll_value value) {
size_t length = 0;
while (hll_is_cons(value)) {
Expand Down
115 changes: 86 additions & 29 deletions hololisp/hll_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>

#include "hll_hololisp.h"

Expand Down Expand Up @@ -64,9 +66,6 @@ typedef struct hll_obj_symb {
// Functions that create new values.
//

HLL_PUB hll_value hll_nil(void);
HLL_PUB hll_value hll_true(void);
HLL_PUB hll_value hll_num(double value);
HLL_PUB hll_value hll_new_symbol(struct hll_vm *vm, const char *symbol,
size_t length);
HLL_PUB hll_value hll_new_symbolz(struct hll_vm *vm, const char *symbol);
Expand All @@ -78,39 +77,13 @@ HLL_PUB hll_value hll_new_bind(struct hll_vm *vm,
HLL_PUB hll_value hll_new_func(struct hll_vm *vm, hll_value params,
struct hll_bytecode *bytecode);

//
// Unwrapper functions.
// Unwrappers are used to get internal contents of value of expected type.
// If value type is not equal to expected, panic.
//

HLL_PUB double hll_unwrap_num(hll_value value);
HLL_PUB hll_value hll_unwrap_cdr(hll_value value);
HLL_PUB hll_value hll_unwrap_car(hll_value value);
HLL_PUB void hll_setcar(hll_value cons, hll_value car);
HLL_PUB void hll_setcdr(hll_value cons, hll_value cdr);
HLL_PUB hll_obj_cons *hll_unwrap_cons(hll_value value)
__attribute__((returns_nonnull));
HLL_PUB hll_obj_env *hll_unwrap_env(hll_value value)
__attribute__((returns_nonnull));
HLL_PUB const char *hll_unwrap_zsymb(hll_value value)
__attribute__((returns_nonnull));
HLL_PUB hll_obj_symb *hll_unwrap_symb(hll_value value)
__attribute__((returns_nonnull));
HLL_PUB hll_obj_bind *hll_unwrap_bind(hll_value value)
__attribute__((returns_nonnull));
HLL_PUB hll_obj_func *hll_unwrap_func(hll_value value)
__attribute__((returns_nonnull));

hll_obj *hll_unwrap_obj(hll_value value);
void hll_free_obj(struct hll_vm *vm, hll_obj *obj);

HLL_PUB size_t hll_list_length(hll_value value);

#define HLL_SIGN_BIT ((uint64_t)1 << 63)
#define HLL_QNAN ((uint64_t)0x7ffc000000000000)


static inline hll_value nan_box_singleton(uint8_t kind) {
return HLL_QNAN | kind;
}
Expand All @@ -127,6 +100,14 @@ static inline hll_obj *nan_unbox_ptr(hll_value value) {
return (hll_obj *)(uintptr_t)(value & ~(HLL_SIGN_BIT | HLL_QNAN));
}

static inline hll_value hll_nil(void) { return nan_box_singleton(HLL_VALUE_NIL); }
static inline hll_value hll_true(void) { return nan_box_singleton(HLL_VALUE_TRUE); }
static inline hll_value hll_num(double num) {
hll_value value;
memcpy(&value, &num, sizeof(hll_value));
return value;
}

static inline bool hll_is_num(hll_value value) { return (value & HLL_QNAN) != HLL_QNAN; }
static inline bool hll_is_obj(hll_value value) {
return (((value) & (HLL_QNAN | HLL_SIGN_BIT)) == (HLL_QNAN | HLL_SIGN_BIT));
Expand All @@ -150,4 +131,80 @@ static inline bool hll_is_list(hll_value value) {
return hll_is_cons(value) || hll_is_nil(value);
}

static inline hll_obj_cons *hll_unwrap_cons(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_CONS);
return (hll_obj_cons *)obj->as;
}

static inline const char *hll_unwrap_zsymb(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_SYMB);
return ((hll_obj_symb *)obj->as)->symb;
}

static inline hll_obj_symb *hll_unwrap_symb(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_SYMB);
return (hll_obj_symb *)obj->as;
}

static inline hll_value hll_unwrap_cdr(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_CONS);
return ((hll_obj_cons *)obj->as)->cdr;
}

static inline hll_value hll_unwrap_car(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_CONS);
return ((hll_obj_cons *)obj->as)->car;
}

static inline hll_obj_bind *hll_unwrap_bind(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_BIND);
return (hll_obj_bind *)obj->as;
}

static inline hll_obj_env *hll_unwrap_env(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_ENV);
return (hll_obj_env *)obj->as;
}

static inline hll_obj_func *hll_unwrap_func(hll_value value) {
assert(hll_is_obj(value));
hll_obj *obj = nan_unbox_ptr(value);
assert(obj->kind == HLL_VALUE_FUNC);
return (hll_obj_func *)obj->as;
}

static inline double hll_unwrap_num(hll_value value) {
assert(hll_is_num(value));
double result;
memcpy(&result, &value, sizeof(hll_value));
return result;
}

static inline hll_obj *hll_unwrap_obj(hll_value value) {
assert(hll_is_obj(value));
return nan_unbox_ptr(value);
}

static inline void hll_setcar(hll_value cons, hll_value car) {
hll_unwrap_cons(cons)->car = car;
}

static inline void hll_setcdr(hll_value cons, hll_value cdr) {
hll_unwrap_cons(cons)->cdr = cdr;
}

#endif

0 comments on commit 2e8e2d4

Please sign in to comment.