diff --git a/extlib/benz/codegen.c b/extlib/benz/codegen.c index 845febd2..721505a8 100644 --- a/extlib/benz/codegen.c +++ b/extlib/benz/codegen.c @@ -330,9 +330,9 @@ pic_expand(pic_state *pic, pic_value expr, struct pic_env *env) return v; } -typedef xvect_t(pic_sym *) xvect; +typedef kvec_t(pic_sym *) svec_t; -#define xv_push_sym(v, x) xv_push(pic_sym *, (v), (x)) +#define kv_push_sym(v, x) kv_push(pic_sym *, (v), (x)) /** * scope object @@ -341,7 +341,7 @@ typedef xvect_t(pic_sym *) xvect; typedef struct analyze_scope { int depth; bool varg; - xvect args, locals, captures; /* rest args variable is counted as a local */ + svec_t args, locals, captures; /* rest args variable is counted as a local */ pic_value defer; struct analyze_scope *up; } analyze_scope; @@ -373,7 +373,7 @@ new_analyze_state(pic_state *pic) push_scope(state, pic_nil_value()); pic_dict_for_each (sym, pic->globals, it) { - xv_push_sym(state->scope->locals, sym); + kv_push_sym(state->scope->locals, sym); } return state; @@ -387,7 +387,7 @@ destroy_analyze_state(analyze_state *state) } static bool -analyze_args(pic_state *pic, pic_value formals, bool *varg, xvect *args, xvect *locals) +analyze_args(pic_state *pic, pic_value formals, bool *varg, svec_t *args, svec_t *locals) { pic_value v, t; pic_sym *sym; @@ -398,7 +398,7 @@ analyze_args(pic_state *pic, pic_value formals, bool *varg, xvect *args, xvect * return false; } sym = pic_sym_ptr(t); - xv_push_sym(*args, sym); + kv_push_sym(*args, sym); } if (pic_nil_p(v)) { *varg = false; @@ -406,7 +406,7 @@ analyze_args(pic_state *pic, pic_value formals, bool *varg, xvect *args, xvect * else if (pic_sym_p(v)) { *varg = true; sym = pic_sym_ptr(v); - xv_push_sym(*locals, sym); + kv_push_sym(*locals, sym); } else { return false; @@ -422,9 +422,9 @@ push_scope(analyze_state *state, pic_value formals) analyze_scope *scope = pic_malloc(pic, sizeof(analyze_scope)); bool varg; - xv_init(scope->args); - xv_init(scope->locals); - xv_init(scope->captures); + kv_init(scope->args); + kv_init(scope->locals); + kv_init(scope->captures); if (analyze_args(pic, formals, &varg, &scope->args, &scope->locals)) { scope->up = state->scope; @@ -437,9 +437,9 @@ push_scope(analyze_state *state, pic_value formals) return true; } else { - xv_destroy(scope->args); - xv_destroy(scope->locals); - xv_destroy(scope->captures); + kv_destroy(scope->args); + kv_destroy(scope->locals); + kv_destroy(scope->captures); pic_free(pic, scope); return false; } @@ -452,9 +452,9 @@ pop_scope(analyze_state *state) analyze_scope *scope; scope = state->scope; - xv_destroy(scope->args); - xv_destroy(scope->locals); - xv_destroy(scope->captures); + kv_destroy(scope->args); + kv_destroy(scope->locals); + kv_destroy(scope->captures); scope = scope->up; pic_free(state->pic, state->scope); @@ -467,13 +467,13 @@ lookup_scope(analyze_scope *scope, pic_sym *sym) size_t i; /* args */ - for (i = 0; i < xv_size(scope->args); ++i) { - if (xv_A(scope->args, i) == sym) + for (i = 0; i < kv_size(scope->args); ++i) { + if (kv_A(scope->args, i) == sym) return true; } /* locals */ - for (i = 0; i < xv_size(scope->locals); ++i) { - if (xv_A(scope->locals, i) == sym) + for (i = 0; i < kv_size(scope->locals); ++i) { + if (kv_A(scope->locals, i) == sym) return true; } return false; @@ -484,13 +484,13 @@ capture_var(pic_state *pic, analyze_scope *scope, pic_sym *sym) { size_t i; - for (i = 0; i < xv_size(scope->captures); ++i) { - if (xv_A(scope->captures, i) == sym) { + for (i = 0; i < kv_size(scope->captures); ++i) { + if (kv_A(scope->captures, i) == sym) { break; } } - if (i == xv_size(scope->captures)) { - xv_push_sym(scope->captures, sym); + if (i == kv_size(scope->captures)) { + kv_push_sym(scope->captures, sym); } } @@ -524,7 +524,7 @@ define_var(analyze_state *state, pic_sym *sym) return; } - xv_push_sym(scope->locals, sym); + kv_push_sym(scope->locals, sym); } static pic_value analyze_node(analyze_state *, pic_value, bool); @@ -648,8 +648,8 @@ analyze_procedure(analyze_state *state, pic_value name, pic_value formals, pic_v size_t i; args = pic_nil_value(); - for (i = xv_size(scope->args); i > 0; --i) { - pic_push(pic, pic_obj_value(xv_A(scope->args, i - 1)), args); + for (i = kv_size(scope->args); i > 0; --i) { + pic_push(pic, pic_obj_value(kv_A(scope->args, i - 1)), args); } varg = scope->varg @@ -662,13 +662,13 @@ analyze_procedure(analyze_state *state, pic_value name, pic_value formals, pic_v analyze_deferred(state); locals = pic_nil_value(); - for (i = xv_size(scope->locals); i > 0; --i) { - pic_push(pic, pic_obj_value(xv_A(scope->locals, i - 1)), locals); + for (i = kv_size(scope->locals); i > 0; --i) { + pic_push(pic, pic_obj_value(kv_A(scope->locals, i - 1)), locals); } captures = pic_nil_value(); - for (i = xv_size(scope->captures); i > 0; --i) { - pic_push(pic, pic_obj_value(xv_A(scope->captures, i - 1)), captures); + for (i = kv_size(scope->captures); i > 0; --i) { + pic_push(pic, pic_obj_value(kv_A(scope->captures, i - 1)), captures); } pop_scope(state); @@ -1141,7 +1141,7 @@ typedef struct codegen_context { pic_sym *name; /* rest args variable is counted as a local */ bool varg; - xvect args, locals, captures; + svec_t args, locals, captures; /* actual bit code sequence */ pic_code *code; size_t clen, ccapa; @@ -1268,19 +1268,19 @@ create_activation(codegen_state *state) xh_init_ptr(®s, sizeof(size_t)); offset = 1; - for (i = 0; i < xv_size(cxt->args); ++i) { + for (i = 0; i < kv_size(cxt->args); ++i) { n = i + offset; - xh_put_ptr(®s, xv_A(cxt->args, i), &n); + xh_put_ptr(®s, kv_A(cxt->args, i), &n); } offset += i; - for (i = 0; i < xv_size(cxt->locals); ++i) { + for (i = 0; i < kv_size(cxt->locals); ++i) { n = i + offset; - xh_put_ptr(®s, xv_A(cxt->locals, i), &n); + xh_put_ptr(®s, kv_A(cxt->locals, i), &n); } - for (i = 0; i < xv_size(cxt->captures); ++i) { - n = xh_val(xh_get_ptr(®s, xv_A(cxt->captures, i)), size_t); - if (n <= xv_size(cxt->args) || (cxt->varg && n == xv_size(cxt->args) + 1)) { + for (i = 0; i < kv_size(cxt->captures); ++i) { + n = xh_val(xh_get_ptr(®s, kv_A(cxt->captures, i)), size_t); + if (n <= kv_size(cxt->args) || (cxt->varg && n == kv_size(cxt->args) + 1)) { /* copy arguments to capture variable area */ emit_i(state, OP_LREF, (int)n); } else { @@ -1308,18 +1308,18 @@ push_codegen_context(codegen_state *state, pic_value name, pic_value args, pic_v : pic_sym_ptr(name); cxt->varg = varg; - xv_init(cxt->args); - xv_init(cxt->locals); - xv_init(cxt->captures); + kv_init(cxt->args); + kv_init(cxt->locals); + kv_init(cxt->captures); pic_for_each (var, args, it) { - xv_push_sym(cxt->args, pic_sym_ptr(var)); + kv_push_sym(cxt->args, pic_sym_ptr(var)); } pic_for_each (var, locals, it) { - xv_push_sym(cxt->locals, pic_sym_ptr(var)); + kv_push_sym(cxt->locals, pic_sym_ptr(var)); } pic_for_each (var, captures, it) { - xv_push_sym(cxt->captures, pic_sym_ptr(var)); + kv_push_sym(cxt->captures, pic_sym_ptr(var)); } cxt->code = pic_calloc(pic, PIC_ISEQ_SIZE, sizeof(pic_code)); @@ -1354,9 +1354,9 @@ pop_codegen_context(codegen_state *state) irep = (struct pic_irep *)pic_obj_alloc(pic, sizeof(struct pic_irep), PIC_TT_IREP); irep->name = state->cxt->name; irep->varg = state->cxt->varg; - irep->argc = (int)xv_size(state->cxt->args) + 1; - irep->localc = (int)xv_size(state->cxt->locals); - irep->capturec = (int)xv_size(state->cxt->captures); + irep->argc = (int)kv_size(state->cxt->args) + 1; + irep->localc = (int)kv_size(state->cxt->locals); + irep->capturec = (int)kv_size(state->cxt->captures); irep->code = pic_realloc(pic, state->cxt->code, sizeof(pic_code) * state->cxt->clen); irep->clen = state->cxt->clen; irep->irep = pic_realloc(pic, state->cxt->irep, sizeof(struct pic_irep *) * state->cxt->ilen); @@ -1367,9 +1367,9 @@ pop_codegen_context(codegen_state *state) irep->slen = state->cxt->slen; /* finalize */ - xv_destroy(cxt->args); - xv_destroy(cxt->locals); - xv_destroy(cxt->captures); + kv_destroy(cxt->args); + kv_destroy(cxt->locals); + kv_destroy(cxt->captures); /* destroy context */ cxt = cxt->up; @@ -1389,8 +1389,8 @@ index_capture(codegen_state *state, pic_sym *sym, int depth) cxt = cxt->up; } - for (i = 0; i < xv_size(cxt->captures); ++i) { - if (xv_A(cxt->captures, i) == sym) + for (i = 0; i < kv_size(cxt->captures); ++i) { + if (kv_A(cxt->captures, i) == sym) return (int)i; } return -1; @@ -1403,13 +1403,13 @@ index_local(codegen_state *state, pic_sym *sym) size_t i, offset; offset = 1; - for (i = 0; i < xv_size(cxt->args); ++i) { - if (xv_A(cxt->args, i) == sym) + for (i = 0; i < kv_size(cxt->args); ++i) { + if (kv_A(cxt->args, i) == sym) return (int)(i + offset); } offset += i; - for (i = 0; i < xv_size(cxt->locals); ++i) { - if (xv_A(cxt->locals, i) == sym) + for (i = 0; i < kv_size(cxt->locals); ++i) { + if (kv_A(cxt->locals, i) == sym) return (int)(i + offset); } return -1; @@ -1462,7 +1462,7 @@ codegen(codegen_state *state, pic_value obj) name = pic_sym_ptr(pic_list_ref(pic, obj, 1)); if ((i = index_capture(state, name, 0)) != -1) { - emit_i(state, OP_LREF, i + (int)xv_size(cxt->args) + (int)xv_size(cxt->locals) + 1); + emit_i(state, OP_LREF, i + (int)kv_size(cxt->args) + (int)kv_size(cxt->locals) + 1); return; } emit_i(state, OP_LREF, index_local(state, name)); @@ -1497,7 +1497,7 @@ codegen(codegen_state *state, pic_value obj) name = pic_sym_ptr(pic_list_ref(pic, var, 1)); if ((i = index_capture(state, name, 0)) != -1) { - emit_i(state, OP_LSET, i + (int)xv_size(cxt->args) + (int)xv_size(cxt->locals) + 1); + emit_i(state, OP_LSET, i + (int)kv_size(cxt->args) + (int)kv_size(cxt->locals) + 1); emit_n(state, OP_PUSHUNDEF); return; } diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index a4f33668..9205abe2 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -745,25 +745,17 @@ static void gc_sweep_symbols(pic_state *pic) { xh_entry *it; - xvect_t(xh_entry *) xv; - size_t i; char *cstr; - xv_init(xv); - for (it = xh_begin(&pic->syms); it != NULL; it = xh_next(it)) { if (! gc_obj_is_marked((struct pic_object *)xh_val(it, pic_sym *))) { - xv_push(xh_entry *, xv, it); + cstr = xh_key(it, char *); + + xh_del_str(&pic->syms, cstr); + + pic_free(pic, cstr); } } - - for (i = 0; i < xv_size(xv); ++i) { - cstr = xh_key(xv_A(xv, i), char *); - - xh_del_str(&pic->syms, cstr); - - pic_free(pic, cstr); - } } static void diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index e5419c24..54c8bf14 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -35,7 +35,7 @@ extern "C" { #include "picrin/config.h" #include "picrin/compat.h" -#include "picrin/xvect.h" +#include "picrin/kvec.h" #include "picrin/xhash.h" #include "picrin/value.h" diff --git a/extlib/benz/include/picrin/kvec.h b/extlib/benz/include/picrin/kvec.h new file mode 100644 index 00000000..cea48ee4 --- /dev/null +++ b/extlib/benz/include/picrin/kvec.h @@ -0,0 +1,67 @@ +/* The MIT License + + Copyright (c) 2015, by Yuichi Nishiwaki + Copyright (c) 2008, by Attractive Chaos + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +#ifndef AC_KVEC_H +#define AC_KVEC_H + +#define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) + +#define kvec_t(type) struct { size_t n, m; type *a; } +#define kv_init(v) ((v).n = (v).m = 0, (v).a = 0) +#define kv_destroy(v) pic_free((pic), (v).a) +#define kv_A(v, i) ((v).a[(i)]) +#define kv_pop(v) ((v).a[--(v).n]) +#define kv_size(v) ((v).n) +#define kv_max(v) ((v).m) + +#define kv_resize(type, v, s) ((v).m = (s), (v).a = (type*)pic_realloc((pic), (v).a, sizeof(type) * (v).m)) + +#define kv_copy(type, v1, v0) do { \ + if ((v1).m < (v0).n) kv_resize((pic), type, v1, (v0).n); \ + (v1).n = (v0).n; \ + memcpy((v1).a, (v0).a, sizeof(type) * (v0).n); \ + } while (0) \ + +#define kv_push(type, v, x) do { \ + if ((v).n == (v).m) { \ + (v).m = (v).m? (v).m<<1 : 2; \ + (v).a = (type*)pic_realloc((pic), (v).a, sizeof(type) * (v).m); \ + } \ + (v).a[(v).n++] = (x); \ + } while (0) + +#define kv_pushp(type, v) \ + (((v).n == (v).m)? \ + ((v).m = ((v).m? (v).m<<1 : 2), \ + (v).a = (type*)pic_realloc((pic), (v).a, sizeof(type) * (v).m), 0) \ + : 0), ((v).a + ((v).n++)) + +#define kv_a(type, v, i) \ + (((v).m <= (size_t)(i)? \ + ((v).m = (v).n = (i) + 1, kv_roundup32((v).m), \ + (v).a = (type*)pic_realloc((pic), (v).a, sizeof(type) * (v).m), 0) \ + : (v).n <= (size_t)(i)? (v).n = (i) + 1 \ + : 0), (v).a[(i)]) + +#endif diff --git a/extlib/benz/include/picrin/xvect.h b/extlib/benz/include/picrin/xvect.h deleted file mode 100644 index 44db4d8e..00000000 --- a/extlib/benz/include/picrin/xvect.h +++ /dev/null @@ -1,76 +0,0 @@ -#ifndef XVECT_H__ -#define XVECT_H__ - -/* The MIT License - - Copyright (c) 2008, by Attractive Chaos - Copyright (c) 2014, by Yuichi Nishiwaki - - Permission is hereby granted, free of charge, to any person obtaining - a copy of this software and associated documentation files (the - "Software"), to deal in the Software without restriction, including - without limitation the rights to use, copy, modify, merge, publish, - distribute, sublicense, and/or sell copies of the Software, and to - permit persons to whom the Software is furnished to do so, subject to - the following conditions: - - The above copyright notice and this permission notice shall be - included in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ - -#define xv_realloc(P,Z) pic_realloc(pic,P,Z) -#define xv_free(P) pic_free(pic,P) - -#define xv_roundup32(x) \ - (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) - -#define xvect_t(type) struct { size_t n, m; type *a; } -#define xv_init(v) ((v).n = (v).m = 0, (v).a = 0) -#define xv_destroy(v) xv_free((v).a) -#define xv_A(v, i) ((v).a[(i)]) -#define xv_pop(v) ((v).a[--(v).n]) -#define xv_size(v) ((v).n) -#define xv_max(v) ((v).m) - -#define xv_resize(type, v, s) \ - ((v).m = (s), (v).a = (type*)xv_realloc((v).a, sizeof(type) * (v).m)) - -#define xv_copy(type, v1, v0) \ - do { \ - if ((v1).m < (v0).n) xv_resize(type, v1, (v0).n); \ - (v1).n = (v0).n; \ - memcpy((v1).a, (v0).a, sizeof(type) * (v0).n); \ - } while (0) \ - -#define xv_push(type, v, x) \ - do { \ - if ((v).n == (v).m) { \ - (v).m = (v).m? (v).m<<1 : (size_t)2; \ - (v).a = (type*)xv_realloc((v).a, sizeof(type) * (v).m); \ - } \ - (v).a[(v).n++] = (x); \ - } while (0) - -#define xv_pushp(type, v) \ - (((v).n == (v).m)? \ - ((v).m = ((v).m? (v).m<<1 : (size_t)2), \ - (v).a = (type*)xv_realloc((v).a, sizeof(type) * (v).m), 0) \ - : 0), ((v).a + ((v).n++)) - -#define xv_a(type, v, i) \ - (((v).m <= (size_t)(i)? \ - ((v).m = (v).n = (i) + 1, xv_roundup32((v).m), \ - (v).a = (type*)xv_realloc((v).a, sizeof(type) * (v).m), 0) \ - : (v).n <= (size_t)(i)? (v).n = (i) + 1 \ - : (size_t)0), (v).a[(i)]) - -#endif