From c9f5e4faeb93d3cf315ba5bae30ce3ed108680ff Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Fri, 9 Aug 2019 19:30:15 +0300 Subject: [PATCH] Replace symbol_t with struct --- c/builtins.c | 15 ++++++++------- c/cvalues.h | 4 ++-- c/equal.h | 2 +- c/flisp.c | 33 +++++++++++++++++---------------- c/flisp.h | 22 +++++++++++----------- c/types.h | 4 ++-- 6 files changed, 41 insertions(+), 39 deletions(-) diff --git a/c/builtins.c b/c/builtins.c index f3e40cf..4e09d81 100644 --- a/c/builtins.c +++ b/c/builtins.c @@ -148,14 +148,15 @@ static value_t fl_symbol(value_t *args, u_int32_t nargs) static value_t fl_keywordp(value_t *args, u_int32_t nargs) { argcount("keyword?", nargs, 1); - return (issymbol(args[0]) && iskeyword((symbol_t *)ptr(args[0]))) ? FL_T - : FL_F; + return (issymbol(args[0]) && iskeyword((struct symbol *)ptr(args[0]))) + ? FL_T + : FL_F; } static value_t fl_top_level_value(value_t *args, u_int32_t nargs) { argcount("top-level-value", nargs, 1); - symbol_t *sym = tosymbol(args[0], "top-level-value"); + struct symbol *sym = tosymbol(args[0], "top-level-value"); if (sym->binding == UNBOUND) fl_raise(fl_list2(UnboundError, args[0])); return sym->binding; @@ -164,13 +165,13 @@ static value_t fl_top_level_value(value_t *args, u_int32_t nargs) static value_t fl_set_top_level_value(value_t *args, u_int32_t nargs) { argcount("set-top-level-value!", nargs, 2); - symbol_t *sym = tosymbol(args[0], "set-top-level-value!"); + struct symbol *sym = tosymbol(args[0], "set-top-level-value!"); if (!isconstant(sym)) sym->binding = args[1]; return args[1]; } -static void global_env_list(symbol_t *root, value_t *pv) +static void global_env_list(struct symbol *root, value_t *pv) { while (root != NULL) { if (root->name[0] != ':' && (root->binding != UNBOUND)) { @@ -181,7 +182,7 @@ static void global_env_list(symbol_t *root, value_t *pv) } } -extern symbol_t *symtab; +extern struct symbol *symtab; value_t fl_global_env(value_t *args, u_int32_t nargs) { @@ -200,7 +201,7 @@ static value_t fl_constantp(value_t *args, u_int32_t nargs) { argcount("constant?", nargs, 1); if (issymbol(args[0])) - return (isconstant((symbol_t *)ptr(args[0])) ? FL_T : FL_F); + return (isconstant((struct symbol *)ptr(args[0])) ? FL_T : FL_F); if (iscons(args[0])) { if (car_(args[0]) == QUOTE) return FL_T; diff --git a/c/cvalues.h b/c/cvalues.h index 8966bca..899548f 100644 --- a/c/cvalues.h +++ b/c/cvalues.h @@ -867,7 +867,7 @@ static value_t cvalue_array_aset(value_t *args) value_t fl_builtin(value_t *args, u_int32_t nargs) { argcount("builtin", nargs, 1); - symbol_t *name = tosymbol(args[0], "builtin"); + struct symbol *name = tosymbol(args[0], "builtin"); cvalue_t *cv; if (ismanaged(args[0]) || (cv = name->dlcache) == NULL) { lerrorf(ArgError, "builtin: function %s not found", name->name); @@ -884,7 +884,7 @@ value_t cbuiltin(char *name, builtin_t f) *(void **)cv->data = f; value_t sym = symbol(name); - ((symbol_t *)ptr(sym))->dlcache = cv; + ((struct symbol *)ptr(sym))->dlcache = cv; ptrhash_put(&reverse_dlsym_lookup_table, cv, (void *)sym); return tagptr(cv, TAG_CVALUE); diff --git a/c/equal.h b/c/equal.h index e1ba059..c679c85 100644 --- a/c/equal.h +++ b/c/equal.h @@ -327,7 +327,7 @@ static uptrint_t bounded_hash(value_t a, int bound, int *oob) oob); return inthash(a); case TAG_SYM: - return ((symbol_t *)ptr(a))->hash; + return ((struct symbol *)ptr(a))->hash; case TAG_CPRIM: cp = (struct cprim *)ptr(a); data = cp_data(cp); diff --git a/c/flisp.c b/c/flisp.c index 73cf1fe..d2855a5 100644 --- a/c/flisp.c +++ b/c/flisp.c @@ -250,7 +250,7 @@ void bounds_error(char *fname, value_t arr, value_t ind) type_error(fname, #type, v); \ } SAFECAST_OP(cons, struct cons *, ptr) -SAFECAST_OP(symbol, symbol_t *, ptr) +SAFECAST_OP(symbol, struct symbol *, ptr) SAFECAST_OP(fixnum, fixnum_t, numval) SAFECAST_OP(cvalue, cvalue_t *, ptr) SAFECAST_OP(string, char *, cvalue_data) @@ -259,19 +259,20 @@ SAFECAST_OP(string, char *, cvalue_data) // symbol table // --------------------------------------------------------------- -symbol_t *symtab = NULL; +struct symbol *symtab = NULL; int fl_is_keyword_name(char *str, size_t len) { return ((str[0] == ':' || str[len - 1] == ':') && str[1] != '\0'); } -static symbol_t *mk_symbol(char *str) +static struct symbol *mk_symbol(char *str) { - symbol_t *sym; + struct symbol *sym; size_t len = strlen(str); - sym = (symbol_t *)malloc(sizeof(symbol_t) - sizeof(void *) + len + 1); + sym = + (struct symbol *)malloc(sizeof(struct symbol) - sizeof(void *) + len + 1); assert(((uptrint_t)sym & 0x7) == 0); // make sure malloc aligns 8 sym->left = sym->right = NULL; sym->flags = 0; @@ -288,7 +289,7 @@ static symbol_t *mk_symbol(char *str) return sym; } -static symbol_t **symtab_lookup(symbol_t **ptree, char *str) +static struct symbol **symtab_lookup(struct symbol **ptree, char *str) { int x; @@ -306,7 +307,7 @@ static symbol_t **symtab_lookup(symbol_t **ptree, char *str) value_t symbol(char *str) { - symbol_t **pnode; + struct symbol **pnode; pnode = symtab_lookup(&symtab, str); if (*pnode == NULL) @@ -350,7 +351,7 @@ char *symbol_name(value_t v) *(--n) = 'g'; return n; } - return ((symbol_t *)ptr(v))->name; + return ((struct symbol *)ptr(v))->name; } // conses @@ -540,7 +541,7 @@ static value_t relocate(value_t v) value_t relocate_lispvalue(value_t v) { return relocate(v); } -static void trace_globals(symbol_t *root) +static void trace_globals(struct symbol *root) { while (root != NULL) { if (root->binding != UNBOUND) @@ -874,7 +875,7 @@ static uint32_t process_keys(value_t kwtable, uint32_t nreq, uint32_t nkw, args[i] = UNBOUND; for (i = nreq; i < nargs; i++) { v = Stack[bp + i]; - if (issymbol(v) && iskeyword((symbol_t *)ptr(v))) + if (issymbol(v) && iskeyword((struct symbol *)ptr(v))) break; if (a >= nopt) goto no_kw; @@ -889,7 +890,7 @@ static uint32_t process_keys(value_t kwtable, uint32_t nreq, uint32_t nkw, if (i >= nargs) lerrorf(ArgError, "keyword %s requires an argument", symbol_name(v)); - value_t hv = fixnum(((symbol_t *)ptr(v))->hash); + value_t hv = fixnum(((struct symbol *)ptr(v))->hash); uptrint_t x = 2 * (labs(numval(hv)) % n); if (vector_elt(kwtable, x) == v) { uptrint_t idx = numval(vector_elt(kwtable, x + 1)); @@ -906,7 +907,7 @@ static uint32_t process_keys(value_t kwtable, uint32_t nreq, uint32_t nkw, if (i >= nargs) break; v = Stack[bp + i]; - } while (issymbol(v) && iskeyword((symbol_t *)ptr(v))); + } while (issymbol(v) && iskeyword((struct symbol *)ptr(v))); no_kw: nrestargs = nargs - i; if (!va && nrestargs > 0) @@ -979,7 +980,7 @@ static value_t apply_cl(uint32_t nargs) uint32_t op; #endif uint32_t i; - symbol_t *sym; + struct symbol *sym; static struct cons *c; static value_t *pv; static int64_t accum; @@ -1696,7 +1697,7 @@ apply_cl_top: ip++; do_loadg: assert(issymbol(v)); - sym = (symbol_t *)ptr(v); + sym = (struct symbol *)ptr(v); if (sym->binding == UNBOUND) fl_raise(fl_list2(UnboundError, v)); PUSH(sym->binding); @@ -1714,7 +1715,7 @@ apply_cl_top: ip++; do_setg: assert(issymbol(v)); - sym = (symbol_t *)ptr(v); + sym = (struct symbol *)ptr(v); v = Stack[SP - 1]; if (!isconstant(sym)) sym->binding = v; @@ -2602,7 +2603,7 @@ int fl_load_system_image(value_t sys_image_iostream) { value_t e; int saveSP; - symbol_t *sym; + struct symbol *sym; PUSH(sys_image_iostream); saveSP = SP; diff --git a/c/flisp.h b/c/flisp.h index d27f79c..d44ed6b 100644 --- a/c/flisp.h +++ b/c/flisp.h @@ -15,20 +15,20 @@ struct cons { value_t cdr; }; -typedef struct _symbol_t { +struct symbol { uptrint_t flags; value_t binding; // global value binding struct _fltype_t *type; uint32_t hash; void *dlcache; // dlsym address // below fields are private - struct _symbol_t *left; - struct _symbol_t *right; + struct symbol *left; + struct symbol *right; union { char name[1]; void *_pad; // ensure field aligned to pointer size }; -} symbol_t; +}; struct gensym { value_t isconst; @@ -97,15 +97,15 @@ struct gensym { #define fn_env(f) (((value_t *)ptr(f))[2]) #define fn_name(f) (((value_t *)ptr(f))[3]) -#define set(s, v) (((symbol_t *)ptr(s))->binding = (v)) -#define setc(s, v) \ - do { \ - ((symbol_t *)ptr(s))->flags |= 1; \ - ((symbol_t *)ptr(s))->binding = (v); \ +#define set(s, v) (((struct symbol *)ptr(s))->binding = (v)) +#define setc(s, v) \ + do { \ + ((struct symbol *)ptr(s))->flags |= 1; \ + ((struct symbol *)ptr(s))->binding = (v); \ } while (0) #define isconstant(s) ((s)->flags & 0x1) #define iskeyword(s) ((s)->flags & 0x2) -#define symbol_value(s) (((symbol_t *)ptr(s))->binding) +#define symbol_value(s) (((struct symbol *)ptr(s))->binding) #define ismanaged(v) \ ((((unsigned char *)ptr(v)) >= fromspace) && \ (((unsigned char *)ptr(v)) < fromspace + heapsize)) @@ -157,7 +157,7 @@ int isnumtok_base(char *tok, value_t *pval, int base); /* safe casts */ struct cons *tocons(value_t v, char *fname); -symbol_t *tosymbol(value_t v, char *fname); +struct symbol *tosymbol(value_t v, char *fname); fixnum_t tofixnum(value_t v, char *fname); char *tostring(value_t v, char *fname); diff --git a/c/types.h b/c/types.h index 09d10a0..c56fae9 100644 --- a/c/types.h +++ b/c/types.h @@ -2,7 +2,7 @@ fltype_t *get_type(value_t t) { fltype_t *ft; if (issymbol(t)) { - ft = ((symbol_t *)ptr(t))->type; + ft = ((struct symbol *)ptr(t))->type; if (ft != NULL) return ft; } @@ -24,7 +24,7 @@ fltype_t *get_type(value_t t) ft->type = t; if (issymbol(t)) { ft->numtype = sym_to_numtype(t); - ((symbol_t *)ptr(t))->type = ft; + ((struct symbol *)ptr(t))->type = ft; } else { ft->numtype = N_NUMTYPES; }