From de362c8f37340cf0f2c6e93b8339498a623b54a7 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 26 Sep 2014 16:47:23 +0900 Subject: [PATCH 01/27] strict error check on vector manipulation --- vector.c | 56 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/vector.c b/vector.c index 2a3099f7..775895d3 100644 --- a/vector.c +++ b/vector.c @@ -26,11 +26,11 @@ struct pic_vector * pic_make_vec_from_list(pic_state *pic, pic_value data) { struct pic_vector *vec; - size_t i, len; + int len, i; len = pic_length(pic, data); - vec = pic_make_vec(pic, len); + vec = pic_make_vec(pic, (size_t)len); for (i = 0; i < len; ++i) { vec->data[i] = pic_car(pic, data); data = pic_cdr(pic, data); @@ -77,7 +77,11 @@ pic_vec_make_vector(pic_state *pic) n = pic_get_args(pic, "i|o", &k, &v); - vec = pic_make_vec(pic, k); + if (k < 0) { + pic_errorf(pic, "make-vector: vector length must be non-negative"); + } + + vec = pic_make_vec(pic, (size_t)k); if (n == 2) { for (i = 0; i < (size_t)k; ++i) { vec->data[i] = v; @@ -93,7 +97,7 @@ pic_vec_vector_length(pic_state *pic) pic_get_args(pic, "v", &v); - return pic_int_value(v->len); + return pic_int_value((int)v->len); } static pic_value @@ -138,7 +142,7 @@ pic_vec_vector_copy_i(pic_state *pic) case 3: start = 0; case 4: - end = from->len; + end = (int)from->len; } if (to == from && (start <= at && at < end)) { @@ -169,10 +173,14 @@ pic_vec_vector_copy(pic_state *pic) case 1: start = 0; case 2: - end = vec->len; + end = (int)vec->len; } - to = pic_make_vec(pic, end - start); + if (end - start < 0) { + pic_errorf(pic, "vector-copy: end index must not be less than start index"); + } + + to = pic_make_vec(pic, (size_t)(end - start)); while (start < end) { to->data[i++] = vec->data[start++]; } @@ -221,7 +229,7 @@ pic_vec_vector_fill_i(pic_state *pic) case 2: start = 0; case 3: - end = vec->len; + end = (int)vec->len; } while (start < end) { @@ -300,7 +308,7 @@ pic_vec_list_to_vector(pic_state *pic) pic_get_args(pic, "o", &list); - vec = pic_make_vec(pic, pic_length(pic, list)); + vec = pic_make_vec(pic, (size_t)pic_length(pic, list)); data = vec->data; @@ -323,7 +331,7 @@ pic_vec_vector_to_list(pic_state *pic) case 1: start = 0; case 2: - end = vec->len; + end = (int)vec->len; } list = pic_nil_value(); @@ -348,10 +356,14 @@ pic_vec_vector_to_string(pic_state *pic) case 1: start = 0; case 2: - end = vec->len; + end = (int)vec->len; } - buf = pic_alloc(pic, end - start); + if (end - start < 0) { + pic_errorf(pic, "vector->string: end index must not be less than start index"); + } + + buf = pic_alloc(pic, (size_t)(end - start)); for (i = start; i < end; ++i) { pic_assert_type(pic, vec->data[i], char); @@ -359,7 +371,7 @@ pic_vec_vector_to_string(pic_state *pic) buf[i - start] = pic_char(vec->data[i]); } - str = pic_make_str(pic, buf, end - start); + str = pic_make_str(pic, buf, (size_t)(end - start)); pic_free(pic, buf); return pic_obj_value(str); @@ -369,7 +381,8 @@ static pic_value pic_vec_string_to_vector(pic_state *pic) { pic_str *str; - int n, start, end, i; + int n, start, end; + size_t i; pic_vec *vec; n = pic_get_args(pic, "s|ii", &str, &start, &end); @@ -378,13 +391,20 @@ pic_vec_string_to_vector(pic_state *pic) case 1: start = 0; case 2: - end = pic_strlen(str); + end = (int)pic_strlen(str); } - vec = pic_make_vec(pic, end - start); + if (start < 0) { + pic_errorf(pic, "string->vector: index must non-negative"); + } + if (end - start < 0) { + pic_errorf(pic, "string->vector: end index must not be less than start index"); + } - for (i = start; i < end; ++i) { - vec->data[i - start] = pic_char_value(pic_str_ref(pic, str, i)); + vec = pic_make_vec(pic, (size_t)(end - start)); + + for (i = 0; i < (size_t)(end - start); ++i) { + vec->data[i] = pic_char_value(pic_str_ref(pic, str, i + (size_t)start)); } return pic_obj_value(vec); } From a75ff9f2b0790d3398f969cc3a4079b69f10967a Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 26 Sep 2014 17:04:20 +0900 Subject: [PATCH 02/27] argc should be int --- blob.c | 5 +++-- bool.c | 2 +- char.c | 3 +-- cont.c | 4 ++-- dict.c | 2 +- error.c | 4 ++-- lib.c | 8 ++++---- number.c | 9 +++------ pair.c | 10 ++++------ proc.c | 2 +- string.c | 14 +++++++------- symbol.c | 2 +- vector.c | 12 +++++++----- vm.c | 4 ++-- 14 files changed, 39 insertions(+), 42 deletions(-) diff --git a/blob.c b/blob.c index e927f4ff..d87afc83 100644 --- a/blob.c +++ b/blob.c @@ -33,7 +33,7 @@ static pic_value pic_blob_bytevector(pic_state *pic) { pic_value *argv; - size_t argc, i; + int argc, i; pic_blob *blob; unsigned char *data; @@ -176,7 +176,8 @@ pic_blob_bytevector_copy(pic_state *pic) static pic_value pic_blob_bytevector_append(pic_state *pic) { - size_t argc, i, j, len; + size_t j, len; + int argc, i; pic_value *argv; pic_blob *blob; diff --git a/bool.c b/bool.c index 8f8c75f1..ff5528e9 100644 --- a/bool.c +++ b/bool.c @@ -172,7 +172,7 @@ pic_bool_boolean_p(pic_state *pic) static pic_value pic_bool_boolean_eq_p(pic_state *pic) { - size_t argc, i; + int argc, i; pic_value *argv; pic_get_args(pic, "*", &argc, &argv); diff --git a/char.c b/char.c index 98eed6f7..f08fcdd0 100644 --- a/char.c +++ b/char.c @@ -42,9 +42,8 @@ pic_char_integer_to_char(pic_state *pic) static pic_value \ pic_char_##name##_p(pic_state *pic) \ { \ - size_t argc; \ + int argc, i; \ pic_value *argv; \ - size_t i; \ char c, d; \ \ pic_get_args(pic, "cc*", &c, &d, &argc, &argv); \ diff --git a/cont.c b/cont.c index f010f532..39ec7861 100644 --- a/cont.c +++ b/cont.c @@ -96,7 +96,7 @@ pic_load_point(pic_state *pic, struct pic_escape *escape) noreturn static pic_value escape_call(pic_state *pic) { - size_t argc; + int argc; pic_value *argv; struct pic_data *e; @@ -251,7 +251,7 @@ pic_cont_dynamic_wind(pic_state *pic) static pic_value pic_cont_values(pic_state *pic) { - size_t argc; + int argc; pic_value *argv; pic_get_args(pic, "*", &argc, &argv); diff --git a/dict.c b/dict.c index 13200c08..2a8fcd41 100644 --- a/dict.c +++ b/dict.c @@ -142,7 +142,7 @@ pic_dict_dictionary(pic_state *pic) { struct pic_dict *dict; pic_value *argv; - size_t argc, i; + int argc, i; pic_get_args(pic, "*", &argc, &argv); diff --git a/error.c b/error.c index b25cbb42..568e523b 100644 --- a/error.c +++ b/error.c @@ -243,7 +243,7 @@ noreturn static pic_value pic_error_error(pic_state *pic) { const char *str; - size_t argc; + int argc; pic_value *argv; pic_get_args(pic, "z*", &str, &argc, &argv); @@ -257,7 +257,7 @@ pic_error_make_error_object(pic_state *pic) struct pic_error *e; pic_sym type; pic_str *msg; - size_t argc; + int argc; pic_value *argv; pic_get_args(pic, "ms*", &type, &msg, &argc, &argv); diff --git a/lib.c b/lib.c index 37cba2bd..cd760f18 100644 --- a/lib.c +++ b/lib.c @@ -253,7 +253,7 @@ static pic_value pic_lib_condexpand(pic_state *pic) { pic_value *clauses; - size_t argc, i; + int argc, i; pic_get_args(pic, "*", &argc, &clauses); @@ -269,7 +269,7 @@ pic_lib_condexpand(pic_state *pic) static pic_value pic_lib_import(pic_state *pic) { - size_t argc, i; + int argc, i; pic_value *argv; pic_get_args(pic, "*", &argc, &argv); @@ -284,7 +284,7 @@ pic_lib_import(pic_state *pic) static pic_value pic_lib_export(pic_state *pic) { - size_t argc, i; + int argc, i; pic_value *argv; pic_get_args(pic, "*", &argc, &argv); @@ -300,7 +300,7 @@ static pic_value pic_lib_define_library(pic_state *pic) { struct pic_lib *prev = pic->lib; - size_t argc, i; + int argc, i; pic_value spec, *argv; pic_get_args(pic, "o*", &spec, &argc, &argv); diff --git a/number.c b/number.c index 2ed93a79..c72f58ca 100644 --- a/number.c +++ b/number.c @@ -162,9 +162,8 @@ pic_number_nan_p(pic_state *pic) static pic_value \ pic_number_##name(pic_state *pic) \ { \ - size_t argc; \ + int argc, i; \ pic_value *argv; \ - size_t i; \ double f,g; \ \ pic_get_args(pic, "ff*", &f, &g, &argc, &argv); \ @@ -198,9 +197,8 @@ DEFINE_ARITH_CMP(>=, ge) static pic_value \ pic_number_##name(pic_state *pic) \ { \ - size_t argc; \ + int argc, i; \ pic_value *argv; \ - size_t i; \ double f; \ bool e = true; \ \ @@ -230,9 +228,8 @@ DEFINE_ARITH_OP(*, mul, 1) static pic_value \ pic_number_##name(pic_state *pic) \ { \ - size_t argc; \ + int argc, i; \ pic_value *argv; \ - size_t i; \ double f; \ bool e; \ \ diff --git a/pair.c b/pair.c index ee2263c7..b6def852 100644 --- a/pair.c +++ b/pair.c @@ -544,7 +544,7 @@ pic_pair_make_list(pic_state *pic) static pic_value pic_pair_list(pic_state *pic) { - size_t argc; + int argc; pic_value *argv; pic_get_args(pic, "*", &argc, &argv); @@ -565,7 +565,7 @@ pic_pair_length(pic_state *pic) static pic_value pic_pair_append(pic_state *pic) { - size_t argc; + int argc; pic_value *args, list; pic_get_args(pic, "*", &argc, &args); @@ -641,9 +641,8 @@ static pic_value pic_pair_map(pic_state *pic) { struct pic_proc *proc; - size_t argc; + int argc, i; pic_value *args; - int i; pic_value cars, ret; pic_get_args(pic, "l*", &proc, &argc, &args); @@ -670,9 +669,8 @@ static pic_value pic_pair_for_each(pic_state *pic) { struct pic_proc *proc; - size_t argc; + int argc, i; pic_value *args; - int i; pic_value cars; pic_get_args(pic, "l*", &proc, &argc, &args); diff --git a/proc.c b/proc.c index 210f157d..0d67ddd7 100644 --- a/proc.c +++ b/proc.c @@ -61,7 +61,7 @@ pic_proc_apply(pic_state *pic) { struct pic_proc *proc; pic_value *args; - size_t argc; + int argc; pic_value arg_list; pic_get_args(pic, "l*", &proc, &argc, &args); diff --git a/string.c b/string.c index b53055f4..2372ea31 100644 --- a/string.c +++ b/string.c @@ -233,11 +233,10 @@ pic_str_string_p(pic_state *pic) static pic_value pic_str_string(pic_state *pic) { - size_t argc; + int argc, i; pic_value *argv; pic_str *str; char *buf; - size_t i; pic_get_args(pic, "*", &argc, &argv); @@ -290,9 +289,8 @@ pic_str_string_ref(pic_state *pic) static pic_value \ pic_str_string_##name(pic_state *pic) \ { \ - size_t argc; \ + int argc, i; \ pic_value *argv; \ - size_t i; \ \ pic_get_args(pic, "*", &argc, &argv); \ \ @@ -338,7 +336,7 @@ pic_str_string_copy(pic_state *pic) static pic_value pic_str_string_append(pic_state *pic) { - size_t argc, i; + int argc, i; pic_value *argv; pic_str *str; @@ -358,8 +356,9 @@ static pic_value pic_str_string_map(pic_state *pic) { struct pic_proc *proc; - size_t argc, i, len, j; + int argc, i; pic_value *argv, vals, val; + size_t len, j; pic_get_args(pic, "l*", &proc, &argc, &argv); @@ -396,7 +395,8 @@ static pic_value pic_str_string_for_each(pic_state *pic) { struct pic_proc *proc; - size_t argc, i, len, j; + int argc, i; + size_t len, j; pic_value *argv, vals, val; pic_get_args(pic, "l*", &proc, &argc, &argv); diff --git a/symbol.c b/symbol.c index 0cbbf78a..d6feab13 100644 --- a/symbol.c +++ b/symbol.c @@ -108,7 +108,7 @@ pic_symbol_symbol_p(pic_state *pic) static pic_value pic_symbol_symbol_eq_p(pic_state *pic) { - size_t argc, i; + int argc, i; pic_value *argv; pic_get_args(pic, "*", &argc, &argv); diff --git a/vector.c b/vector.c index 775895d3..4378e66c 100644 --- a/vector.c +++ b/vector.c @@ -51,10 +51,9 @@ pic_vec_vector_p(pic_state *pic) static pic_value pic_vec_vector(pic_state *pic) { - size_t argc; + int argc, i; pic_value *argv; pic_vec *vec; - size_t i; pic_get_args(pic, "*", &argc, &argv); @@ -191,8 +190,9 @@ pic_vec_vector_copy(pic_state *pic) static pic_value pic_vec_vector_append(pic_state *pic) { - size_t argc, i, j, len; + int argc, i; pic_value *argv; + size_t j, len; pic_vec *vec; pic_get_args(pic, "*", &argc, &argv); @@ -243,8 +243,9 @@ static pic_value pic_vec_vector_map(pic_state *pic) { struct pic_proc *proc; - size_t argc, i, len, j; + int argc, i; pic_value *argv, vals; + size_t len, j; pic_vec *vec; pic_get_args(pic, "l*", &proc, &argc, &argv); @@ -275,8 +276,9 @@ static pic_value pic_vec_vector_for_each(pic_state *pic) { struct pic_proc *proc; - size_t argc, i, len, j; + int argc, i; pic_value *argv, vals; + size_t len, j; pic_get_args(pic, "l*", &proc, &argc, &argv); diff --git a/vm.c b/vm.c index 92af1ce7..a996742e 100644 --- a/vm.c +++ b/vm.c @@ -386,10 +386,10 @@ pic_get_args(pic_state *pic, const char *format, ...) } } if ('*' == c) { - size_t *n; + int *n; pic_value **argv; - n = va_arg(ap, size_t *); + n = va_arg(ap, int *); argv = va_arg(ap, pic_value **); if (i <= argc) { *n = argc - i; From 7ab9708ed067588ca7451ae93703e82bf421fa12 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 26 Sep 2014 17:09:52 +0900 Subject: [PATCH 03/27] passing array with a pair of int and pic_value * --- cont.c | 14 +++++++------- include/picrin/cont.h | 4 ++-- include/picrin/pair.h | 2 +- pair.c | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cont.c b/cont.c index 39ec7861..4b980e4a 100644 --- a/cont.c +++ b/cont.c @@ -184,9 +184,9 @@ pic_values5(pic_state *pic, pic_value arg1, pic_value arg2, pic_value arg3, pic_ } pic_value -pic_values_by_array(pic_state *pic, size_t argc, pic_value *argv) +pic_values_by_array(pic_state *pic, int argc, pic_value *argv) { - size_t i; + int i; for (i = 0; i < argc; ++i) { pic->sp[i] = argv[i]; @@ -200,7 +200,7 @@ pic_value pic_values_by_list(pic_state *pic, pic_value list) { pic_value v; - size_t i; + int i; i = 0; pic_for_each (v, list) { @@ -211,11 +211,11 @@ pic_values_by_list(pic_state *pic, pic_value list) return pic_nil_p(list) ? pic_none_value() : pic->sp[0]; } -size_t -pic_receive(pic_state *pic, size_t n, pic_value *argv) +int +pic_receive(pic_state *pic, int n, pic_value *argv) { pic_callinfo *ci; - size_t i, retc; + int i, retc; /* take info from discarded frame */ ci = pic->ci + 1; @@ -263,7 +263,7 @@ static pic_value pic_cont_call_with_values(pic_state *pic) { struct pic_proc *producer, *consumer; - size_t argc; + int argc; pic_value args[256]; pic_get_args(pic, "ll", &producer, &consumer); diff --git a/include/picrin/cont.h b/include/picrin/cont.h index 645e6d9c..01e86e11 100644 --- a/include/picrin/cont.h +++ b/include/picrin/cont.h @@ -40,9 +40,9 @@ pic_value pic_values2(pic_state *, pic_value, pic_value); pic_value pic_values3(pic_state *, pic_value, pic_value, pic_value); pic_value pic_values4(pic_state *, pic_value, pic_value, pic_value, pic_value); pic_value pic_values5(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value); -pic_value pic_values_by_array(pic_state *, size_t, pic_value *); +pic_value pic_values_by_array(pic_state *, int, pic_value *); pic_value pic_values_by_list(pic_state *, pic_value); -size_t pic_receive(pic_state *, size_t, pic_value *); +int pic_receive(pic_state *, int, pic_value *); pic_value pic_escape(pic_state *, struct pic_proc *); diff --git a/include/picrin/pair.h b/include/picrin/pair.h index 16c61863..49e3f73f 100644 --- a/include/picrin/pair.h +++ b/include/picrin/pair.h @@ -56,7 +56,7 @@ pic_value pic_list4(pic_state *, pic_value, pic_value, pic_value, pic_value); pic_value pic_list5(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value); pic_value pic_list6(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value, pic_value); pic_value pic_list7(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value, pic_value, pic_value); -pic_value pic_list_by_array(pic_state *, size_t, pic_value *); +pic_value pic_list_by_array(pic_state *, int, pic_value *); pic_value pic_make_list(pic_state *, int, pic_value); #define pic_for_each(var, list) \ diff --git a/pair.c b/pair.c index b6def852..6a9687d8 100644 --- a/pair.c +++ b/pair.c @@ -160,7 +160,7 @@ pic_list7(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_va } pic_value -pic_list_by_array(pic_state *pic, size_t c, pic_value *vs) +pic_list_by_array(pic_state *pic, int c, pic_value *vs) { pic_value v; From 7b0ec3adde85bbaab2350493248f5a4aaad63eab Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 26 Sep 2014 17:13:17 +0900 Subject: [PATCH 04/27] adjustment. amending for argc type change --- blob.c | 2 +- vector.c | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/blob.c b/blob.c index d87afc83..da3907c3 100644 --- a/blob.c +++ b/blob.c @@ -39,7 +39,7 @@ pic_blob_bytevector(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); - blob = pic_make_blob(pic, argc); + blob = pic_make_blob(pic, (size_t)argc); data = blob->data; diff --git a/vector.c b/vector.c index 4378e66c..6ae7f136 100644 --- a/vector.c +++ b/vector.c @@ -57,7 +57,7 @@ pic_vec_vector(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); - vec = pic_make_vec(pic, argc); + vec = pic_make_vec(pic, (size_t)argc); for (i = 0; i < argc; ++i) { vec->data[i] = argv[i]; @@ -243,14 +243,13 @@ static pic_value pic_vec_vector_map(pic_state *pic) { struct pic_proc *proc; - int argc, i; + int argc, i, len, j; pic_value *argv, vals; - size_t len, j; pic_vec *vec; pic_get_args(pic, "l*", &proc, &argc, &argv); - len = SIZE_MAX; + len = INT_MAX; for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], vec); @@ -276,13 +275,12 @@ static pic_value pic_vec_vector_for_each(pic_state *pic) { struct pic_proc *proc; - int argc, i; + int argc, i, len, j; pic_value *argv, vals; - size_t len, j; pic_get_args(pic, "l*", &proc, &argc, &argv); - len = SIZE_MAX; + len = INT_MAX; for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], vec); From cb54f0e065db793256340be80ddaff8824cbdec9 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 26 Sep 2014 17:36:00 +0900 Subject: [PATCH 05/27] int and size_t --- codegen.c | 11 +++++------ vector.c | 14 ++++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/codegen.c b/codegen.c index 55bb8587..050f18c2 100644 --- a/codegen.c +++ b/codegen.c @@ -490,7 +490,6 @@ analyze_if(analyze_state *state, pic_value obj, bool tailpos) switch (pic_length(pic, obj)) { default: pic_errorf(pic, "syntax error"); - break; case 4: if_false = pic_list_ref(pic, obj, 3); FALLTHROUGH; @@ -956,7 +955,7 @@ create_activation(codegen_context *cxt) if ((n = xh_val(xh_get_int(®s, *var), size_t)) <= xv_size(&cxt->args) || (cxt->varg && n == xv_size(&cxt->args) + 1)) { /* copy arguments to capture variable area */ cxt->code[cxt->clen].insn = OP_LREF; - cxt->code[cxt->clen].u.i = n; + cxt->code[cxt->clen].u.i = (int)n; cxt->clen++; } else { /* otherwise, just extend the stack */ @@ -1030,9 +1029,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 = xv_size(&state->cxt->args) + 1; - irep->localc = xv_size(&state->cxt->locals); - irep->capturec = xv_size(&state->cxt->captures); + 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->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); @@ -1067,7 +1066,7 @@ index_capture(codegen_state *state, pic_sym sym, int depth) for (i = 0; i < xv_size(&cxt->captures); ++i) { var = xv_get(&cxt->captures, i); if (*var == sym) - return i; + return (int)i; } return -1; } diff --git a/vector.c b/vector.c index 6ae7f136..ba0dc7c8 100644 --- a/vector.c +++ b/vector.c @@ -243,14 +243,15 @@ static pic_value pic_vec_vector_map(pic_state *pic) { struct pic_proc *proc; - int argc, i, len, j; + int argc; + size_t i, len, j; pic_value *argv, vals; pic_vec *vec; pic_get_args(pic, "l*", &proc, &argc, &argv); len = INT_MAX; - for (i = 0; i < argc; ++i) { + for (i = 0; i < (size_t)argc; ++i) { pic_assert_type(pic, argv[i], vec); len = len < pic_vec_ptr(argv[i])->len @@ -262,7 +263,7 @@ pic_vec_vector_map(pic_state *pic) for (i = 0; i < len; ++i) { vals = pic_nil_value(); - for (j = 0; j < argc; ++j) { + for (j = 0; j < (size_t)argc; ++j) { pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals); } vec->data[i] = pic_apply(pic, proc, vals); @@ -275,13 +276,14 @@ static pic_value pic_vec_vector_for_each(pic_state *pic) { struct pic_proc *proc; - int argc, i, len, j; + int argc; + size_t i, len, j; pic_value *argv, vals; pic_get_args(pic, "l*", &proc, &argc, &argv); len = INT_MAX; - for (i = 0; i < argc; ++i) { + for (i = 0; i < (size_t)argc; ++i) { pic_assert_type(pic, argv[i], vec); len = len < pic_vec_ptr(argv[i])->len @@ -291,7 +293,7 @@ pic_vec_vector_for_each(pic_state *pic) for (i = 0; i < len; ++i) { vals = pic_nil_value(); - for (j = 0; j < argc; ++j) { + for (j = 0; j < (size_t)argc; ++j) { pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals); } pic_apply(pic, proc, vals); From 315c17c8d41a53c832d2e883cb578dff39dbeb12 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 26 Sep 2014 17:36:27 +0900 Subject: [PATCH 06/27] move stack overflow checker --- vm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/vm.c b/vm.c index a996742e..58e5967f 100644 --- a/vm.c +++ b/vm.c @@ -581,7 +581,7 @@ pic_apply5(pic_state *pic, struct pic_proc *proc, pic_value arg1, pic_value arg2 # define VM_LOOP_END } } #endif -#define PUSH(v) ((pic->sp >= pic->stend) ? abort() : (*pic->sp++ = (v))) +#define PUSH(v) (*pic->sp++ = (v)) #define POP() (*--pic->sp) #define PUSHCI() (++pic->ci) @@ -843,6 +843,10 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) VM_CALL_PRINT; + if (pic->sp >= pic->stend) { + pic_panic(pic, "VM stack overflow"); + } + ci = PUSHCI(); ci->argc = c.u.i; ci->retc = 1; From 490472af2f662f51fe7918e1d907804b999f8d49 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 26 Sep 2014 17:36:47 +0900 Subject: [PATCH 07/27] resolve VM's varaible shadows --- vm.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/vm.c b/vm.c index 58e5967f..1eac9f91 100644 --- a/vm.c +++ b/vm.c @@ -650,11 +650,10 @@ pic_apply5(pic_state *pic, struct pic_proc *proc, pic_value arg1, pic_value arg2 #endif pic_value -pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) +pic_apply(pic_state *pic, struct pic_proc *proc, pic_value args) { pic_code c; size_t ai = pic_gc_arena_preserve(pic); - size_t argc, i; pic_code boot[2]; #if PIC_DIRECT_THREADED_VM @@ -674,26 +673,29 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) pic_callinfo *cibase; #endif - if (! pic_list_p(argv)) { + if (! pic_list_p(args)) { pic_errorf(pic, "argv must be a proper list"); } + else { + int argc, i; - argc = pic_length(pic, argv) + 1; + argc = pic_length(pic, args) + 1; - VM_BOOT_PRINT; + VM_BOOT_PRINT; - PUSH(pic_obj_value(proc)); - for (i = 1; i < argc; ++i) { - PUSH(pic_car(pic, argv)); - argv = pic_cdr(pic, argv); + PUSH(pic_obj_value(proc)); + for (i = 1; i < argc; ++i) { + PUSH(pic_car(pic, args)); + args = pic_cdr(pic, args); + } + + /* boot! */ + boot[0].insn = OP_CALL; + boot[0].u.i = argc; + boot[1].insn = OP_STOP; + pic->ip = boot; } - /* boot! */ - boot[0].insn = OP_CALL; - boot[0].u.i = argc; - boot[1].insn = OP_STOP; - pic->ip = boot; - VM_LOOP { CASE(OP_NOP) { NEXT; @@ -827,7 +829,6 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) CASE(OP_CALL) { pic_value x, v; pic_callinfo *ci; - struct pic_proc *proc; if (c.u.i == -1) { pic->sp += pic->ci[1].retc - 1; @@ -958,7 +959,6 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) CASE(OP_LAMBDA) { pic_value self; struct pic_irep *irep; - struct pic_proc *proc; self = pic->ci->fp[0]; if (! pic_proc_p(self)) { From 5cfa89ba2344cf949ba7f4614438250e60a9a3c7 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 26 Sep 2014 17:38:58 +0900 Subject: [PATCH 08/27] starndard style initializer --- vm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm.c b/vm.c index 1eac9f91..832fb2ce 100644 --- a/vm.c +++ b/vm.c @@ -1094,7 +1094,7 @@ pic_value pic_apply_trampoline(pic_state *pic, struct pic_proc *proc, pic_value args) { static const pic_code iseq[2] = { - { OP_NOP, {} }, + { OP_NOP, { .i = 0 } }, { OP_TAILCALL, { .i = -1 } } }; From 36328c154b918d36a18c97db61690b8462ab649d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 26 Sep 2014 17:39:11 +0900 Subject: [PATCH 09/27] more fix on misc functions in vm.c --- vm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vm.c b/vm.c index 832fb2ce..7cc84d96 100644 --- a/vm.c +++ b/vm.c @@ -280,14 +280,14 @@ pic_get_args(pic_state *pic, const char *format, ...) break; } case 'c': { - char *c; + char *k; pic_value v; - c = va_arg(ap, char *); + k = va_arg(ap, char *); if (i < argc) { v = GET_OPERAND(pic,i); if (pic_char_p(v)) { - *c = pic_char(v); + *k = pic_char(v); } else { pic_errorf(pic, "pic_get_args: expected char, but got ~s", v); @@ -488,7 +488,7 @@ vm_push_env(pic_state *pic) { pic_callinfo *ci = pic->ci; - ci->env = (struct pic_env *)pic_obj_alloc(pic, offsetof(struct pic_env, storage) + sizeof(pic_value) * ci->regc, PIC_TT_ENV); + ci->env = (struct pic_env *)pic_obj_alloc(pic, offsetof(struct pic_env, storage) + sizeof(pic_value) * (size_t)(ci->regc), PIC_TT_ENV); ci->env->up = ci->up; ci->env->regc = ci->regc; ci->env->regs = ci->regs; From 9906865932edbaf4cddac6127bb83070bce9aef9 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 26 Sep 2014 23:24:17 +0900 Subject: [PATCH 10/27] explicit int to size_t cast --- codegen.c | 6 +++--- string.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/codegen.c b/codegen.c index 050f18c2..fec7ad43 100644 --- a/codegen.c +++ b/codegen.c @@ -1075,17 +1075,17 @@ static int index_local(codegen_state *state, pic_sym sym) { codegen_context *cxt = state->cxt; - size_t i, offset; + int i, offset; pic_sym *var; offset = 1; - for (i = 0; i < xv_size(&cxt->args); ++i) { + for (i = 0; i < (int)xv_size(&cxt->args); ++i) { var = xv_get(&cxt->args, i); if (*var == sym) return i + offset; } offset += i; - for (i = 0; i < xv_size(&cxt->locals); ++i) { + for (i = 0; i < (int)xv_size(&cxt->locals); ++i) { var = xv_get(&cxt->locals, i); if (*var == sym) return i + offset; diff --git a/string.c b/string.c index 2372ea31..3bad3daf 100644 --- a/string.c +++ b/string.c @@ -463,7 +463,7 @@ pic_str_string_to_list(pic_state *pic) case 1: start = 0; case 2: - end = pic_strlen(str); + end = (int)pic_strlen(str); } list = pic_nil_value(); From f8a32d7d60ff18b7e7851b6b40d23d3f2c64be4d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 26 Sep 2014 23:24:26 +0900 Subject: [PATCH 11/27] use ptrdiff to extend exception handler stack --- error.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/error.c b/error.c index 568e523b..0f3ac651 100644 --- a/error.c +++ b/error.c @@ -90,7 +90,8 @@ void pic_push_try(pic_state *pic, struct pic_escape *escape) { struct pic_proc *cont, *handler; - size_t xp_len, xp_offset; + size_t xp_len; + ptrdiff_t xp_offset; cont = pic_make_econt(pic, escape); @@ -99,7 +100,7 @@ pic_push_try(pic_state *pic, struct pic_escape *escape) pic_attr_set(pic, pic_obj_value(handler), "@@escape", pic_obj_value(cont)); if (pic->xp >= pic->xpend) { - xp_len = (pic->xpend - pic->xpbase) * 2; + xp_len = (size_t)(pic->xpend - pic->xpbase) * 2; xp_offset = pic->xp - pic->xpbase; pic->xpbase = pic_realloc(pic, pic->xpbase, sizeof(struct pic_proc *) * xp_len); pic->xp = pic->xpbase + xp_offset; @@ -198,12 +199,13 @@ pic_error_with_exception_handler(pic_state *pic) { struct pic_proc *handler, *thunk; pic_value val; - size_t xp_len, xp_offset; + size_t xp_len; + ptrdiff_t xp_offset; pic_get_args(pic, "ll", &handler, &thunk); if (pic->xp >= pic->xpend) { - xp_len = (pic->xpend - pic->xpbase) * 2; + xp_len = (size_t)(pic->xpend - pic->xpbase) * 2; xp_offset = pic->xp - pic->xpbase; pic->xpbase = pic_realloc(pic, pic->xpbase, sizeof(struct pic_proc *) * xp_len); pic->xp = pic->xpbase + xp_offset; From 7350f7e71e10b7813b88be1e6360bbad148bc028 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 17:18:11 +0900 Subject: [PATCH 12/27] int to size_t conversion --- codegen.c | 26 +++++++++++++------------- include/picrin/irep.h | 4 ++-- string.c | 4 ++-- symbol.c | 2 +- system.c | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/codegen.c b/codegen.c index fec7ad43..c4e3121e 100644 --- a/codegen.c +++ b/codegen.c @@ -1075,20 +1075,20 @@ static int index_local(codegen_state *state, pic_sym sym) { codegen_context *cxt = state->cxt; - int i, offset; + size_t i, offset; pic_sym *var; offset = 1; - for (i = 0; i < (int)xv_size(&cxt->args); ++i) { + for (i = 0; i < xv_size(&cxt->args); ++i) { var = xv_get(&cxt->args, i); if (*var == sym) - return i + offset; + return (int)(i + offset); } offset += i; - for (i = 0; i < (int)xv_size(&cxt->locals); ++i) { + for (i = 0; i < xv_size(&cxt->locals); ++i) { var = xv_get(&cxt->locals, i); if (*var == sym) - return i + offset; + return (int)(i + offset); } return -1; } @@ -1126,7 +1126,7 @@ codegen(codegen_state *state, pic_value obj) name = pic_sym(pic_list_ref(pic, obj, 1)); if ((i = index_capture(state, name, 0)) != -1) { cxt->code[cxt->clen].insn = OP_LREF; - cxt->code[cxt->clen].u.i = i + xv_size(&cxt->args) + xv_size(&cxt->locals) + 1; + cxt->code[cxt->clen].u.i = i + (int)xv_size(&cxt->args) + (int)xv_size(&cxt->locals) + 1; cxt->clen++; return; } @@ -1172,7 +1172,7 @@ codegen(codegen_state *state, pic_value obj) name = pic_sym(pic_list_ref(pic, var, 1)); if ((i = index_capture(state, name, 0)) != -1) { cxt->code[cxt->clen].insn = OP_LSET; - cxt->code[cxt->clen].u.i = i + xv_size(&cxt->args) + xv_size(&cxt->locals) + 1; + cxt->code[cxt->clen].u.i = i + (int)xv_size(&cxt->args) + (int)xv_size(&cxt->locals) + 1; cxt->clen++; cxt->code[cxt->clen].insn = OP_PUSHNONE; cxt->clen++; @@ -1193,7 +1193,7 @@ codegen(codegen_state *state, pic_value obj) cxt->icapa *= 2; cxt->irep = pic_realloc(pic, cxt->irep, sizeof(struct pic_irep *) * cxt->icapa); } - k = cxt->ilen++; + k = (int)cxt->ilen++; cxt->code[cxt->clen].insn = OP_LAMBDA; cxt->code[cxt->clen].u.i = k; cxt->clen++; @@ -1207,18 +1207,18 @@ codegen(codegen_state *state, pic_value obj) codegen(state, pic_list_ref(pic, obj, 1)); cxt->code[cxt->clen].insn = OP_JMPIF; - s = cxt->clen++; + s = (int)cxt->clen++; /* if false branch */ codegen(state, pic_list_ref(pic, obj, 3)); cxt->code[cxt->clen].insn = OP_JMP; - t = cxt->clen++; + t = (int)cxt->clen++; - cxt->code[s].u.i = cxt->clen - s; + cxt->code[s].u.i = (int)cxt->clen - s; /* if true branch */ codegen(state, pic_list_ref(pic, obj, 2)); - cxt->code[t].u.i = cxt->clen - t; + cxt->code[t].u.i = (int)cxt->clen - t; return; } else if (sym == pic->sBEGIN) { @@ -1266,7 +1266,7 @@ codegen(codegen_state *state, pic_value obj) cxt->pcapa *= 2; cxt->pool = pic_realloc(pic, cxt->pool, sizeof(pic_value) * cxt->pcapa); } - pidx = cxt->plen++; + pidx = (int)cxt->plen++; cxt->pool[pidx] = obj; cxt->code[cxt->clen].insn = OP_PUSHCONST; cxt->code[cxt->clen].u.i = pidx; diff --git a/include/picrin/irep.h b/include/picrin/irep.h index c6e5befb..fe924bbc 100644 --- a/include/picrin/irep.h +++ b/include/picrin/irep.h @@ -52,8 +52,8 @@ struct pic_code { int i; char c; struct { - short depth; - short idx; + int depth; + int idx; } r; } u; }; diff --git a/string.c b/string.c index 3bad3daf..7f03cfae 100644 --- a/string.c +++ b/string.c @@ -240,14 +240,14 @@ pic_str_string(pic_state *pic) pic_get_args(pic, "*", &argc, &argv); - buf = pic_alloc(pic, argc); + buf = pic_alloc(pic, (size_t)argc); for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], char); buf[i] = pic_char(argv[i]); } - str = pic_make_str(pic, buf, argc); + str = pic_make_str(pic, buf, (size_t)argc); pic_free(pic, buf); return pic_obj_value(str); diff --git a/symbol.c b/symbol.c index d6feab13..7e9f1b52 100644 --- a/symbol.c +++ b/symbol.c @@ -80,7 +80,7 @@ pic_ungensym(pic_state *pic, pic_sym base) if ((occr = strrchr(name, '@')) == NULL) { pic_panic(pic, "logic flaw"); } - return pic_intern(pic, name, occr - name); + return pic_intern(pic, name, (size_t)(occr - name)); } bool diff --git a/system.c b/system.c index 4c54b905..e9ef1aa9 100644 --- a/system.c +++ b/system.c @@ -110,7 +110,7 @@ pic_system_getenvs(pic_state *pic) for (i = 0; (*envp)[i] != '='; ++i) ; - key = pic_make_str(pic, *envp, i); + key = pic_make_str(pic, *envp, (size_t)i); val = pic_make_str_cstr(pic, getenv(pic_str_cstr(key))); /* push */ From 186bad0503977d9cdf1e4533812986166288fc3f Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 17:19:16 +0900 Subject: [PATCH 13/27] pair functions must use size_t for index, not int --- include/picrin/pair.h | 10 +++++----- pair.c | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/picrin/pair.h b/include/picrin/pair.h index 49e3f73f..d6cf779d 100644 --- a/include/picrin/pair.h +++ b/include/picrin/pair.h @@ -57,7 +57,7 @@ pic_value pic_list5(pic_state *, pic_value, pic_value, pic_value, pic_value, pic pic_value pic_list6(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value, pic_value); pic_value pic_list7(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value, pic_value, pic_value); pic_value pic_list_by_array(pic_state *, int, pic_value *); -pic_value pic_make_list(pic_state *, int, pic_value); +pic_value pic_make_list(pic_state *, size_t, pic_value); #define pic_for_each(var, list) \ pic_for_each_helper_(var, GENSYM(tmp), list) @@ -69,7 +69,7 @@ pic_value pic_make_list(pic_state *, int, pic_value); #define pic_push(pic, item, place) (place = pic_cons(pic, item, place)) #define pic_pop(pic, place) (place = pic_cdr(pic, place)) -int pic_length(pic_state *, pic_value); +size_t pic_length(pic_state *, pic_value); pic_value pic_reverse(pic_state *, pic_value); pic_value pic_append(pic_state *, pic_value, pic_value); @@ -88,9 +88,9 @@ pic_value pic_cadr(pic_state *, pic_value); pic_value pic_cdar(pic_state *, pic_value); pic_value pic_cddr(pic_state *, pic_value); -pic_value pic_list_tail(pic_state *, pic_value, int); -pic_value pic_list_ref(pic_state *, pic_value, int); -void pic_list_set(pic_state *, pic_value, int, pic_value); +pic_value pic_list_tail(pic_state *, pic_value, size_t); +pic_value pic_list_ref(pic_state *, pic_value, size_t); +void pic_list_set(pic_state *, pic_value, size_t, pic_value); pic_value pic_list_copy(pic_state *, pic_value); #if defined(__cplusplus) diff --git a/pair.c b/pair.c index 6a9687d8..c2a775dc 100644 --- a/pair.c +++ b/pair.c @@ -172,10 +172,10 @@ pic_list_by_array(pic_state *pic, int c, pic_value *vs) } pic_value -pic_make_list(pic_state *pic, int k, pic_value fill) +pic_make_list(pic_state *pic, size_t k, pic_value fill) { pic_value list; - int i; + size_t i; list = pic_nil_value(); for (i = 0; i < k; ++i) { @@ -185,10 +185,10 @@ pic_make_list(pic_state *pic, int k, pic_value fill) return list; } -int +size_t pic_length(pic_state *pic, pic_value obj) { - int c = 0; + size_t c = 0; if (! pic_list_p(obj)) { pic_errorf(pic, "length: expected list, but got ~s", obj); @@ -375,7 +375,7 @@ pic_cddr(pic_state *pic, pic_value v) } pic_value -pic_list_tail(pic_state *pic, pic_value list, int i) +pic_list_tail(pic_state *pic, pic_value list, size_t i) { while (i-- > 0) { list = pic_cdr(pic, list); @@ -384,13 +384,13 @@ pic_list_tail(pic_state *pic, pic_value list, int i) } pic_value -pic_list_ref(pic_state *pic, pic_value list, int i) +pic_list_ref(pic_state *pic, pic_value list, size_t i) { return pic_car(pic, pic_list_tail(pic, list, i)); } void -pic_list_set(pic_state *pic, pic_value list, int i, pic_value obj) +pic_list_set(pic_state *pic, pic_value list, size_t i, pic_value obj) { pic_pair_ptr(pic_list_tail(pic, list, i))->car = obj; } From 4ac6c2b7ab6a6b813d5f1d2712ba6a496b6f4cc3 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 17:33:49 +0900 Subject: [PATCH 14/27] refine description of pic_get_args --- vm.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/vm.c b/vm.c index 7cc84d96..7599c919 100644 --- a/vm.c +++ b/vm.c @@ -35,26 +35,26 @@ pic_get_proc(pic_state *pic) } /** - * char type - * ---- ---- - * o object - * i int - * I int with exactness - * f float - * F float with exactness - * s string object - * z c string - * m symbol - * v vector object - * b bytevector object - * c char - * l lambda object - * p port object - * d dictionary object - * e error object + * char type desc. + * ---- ---- ---- + * o pic_value * object + * i int * int + * I int *, bool * int with exactness + * f double * float + * F double *, bool * float with exactness + * s pic_str ** string object + * z char ** c string + * m pic_sym * symbol + * v pic_vec ** vector object + * b pic_blob ** bytevector object + * c char * char + * l struct pic_proc ** lambda object + * p struct pic_port ** port object + * d struct pic_dict ** dictionary object + * e struct pic_error ** error object * - * | optional operator - * * variable length operator + * | optional operator + * * int *, pic_value ** variable length operator */ int From bfc45a228ba166416f97e6355c2c7147757f9ab7 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 17:47:47 +0900 Subject: [PATCH 15/27] add 'k' specifier to pic_get_args format --- vm.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/vm.c b/vm.c index 7599c919..97e21bf6 100644 --- a/vm.c +++ b/vm.c @@ -40,6 +40,7 @@ pic_get_proc(pic_state *pic) * o pic_value * object * i int * int * I int *, bool * int with exactness + * k size_t * size_t implicitly converted from int * f double * float * F double *, bool * float with exactness * s pic_str ** string object @@ -196,6 +197,33 @@ pic_get_args(pic_state *pic, const char *format, ...) } break; } + case 'k': { + size_t *k; + + k = va_arg(ap, size_t *); + if (i < argc) { + pic_value v; + int x; + + v = GET_OPERAND(pic, i); + switch (pic_type(v)) { + case PIC_TT_INT: + x = pic_int(v); + if (x < 0) { + pic_errorf(pic, "pic_get_args: expected non-negative int, but got ~s", v); + } + if (sizeof(unsigned) > sizeof(size_t) && (unsigned)x > (unsigned)SIZE_MAX) { + pic_errorf(pic, "pic_get_args: int unrepresentable with size_t ~s", v); + } + *k = (size_t)x; + break; + default: + pic_errorf(pic, "pic_get_args: expected int, but got ~s", v); + } + i++; + } + break; + } case 's': { pic_str **str; pic_value v; From 8bdf6230546644b89a94722a8ac2d5154730e2e5 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 17:52:56 +0900 Subject: [PATCH 16/27] use k format specifier in blob.c --- blob.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/blob.c b/blob.c index da3907c3..509b295a 100644 --- a/blob.c +++ b/blob.c @@ -60,17 +60,15 @@ static pic_value pic_blob_make_bytevector(pic_state *pic) { pic_blob *blob; - int k, b = 0, i; + size_t k, i; + int b = 0; - pic_get_args(pic, "i|i", &k, &b); + pic_get_args(pic, "k|i", &k, &b); if (b < 0 || b > 255) pic_errorf(pic, "byte out of range"); - if (k < 0) - pic_errorf(pic, "make-bytevector: cannot create a bytevector of length %d", k); - - blob = pic_make_blob(pic, (size_t)k); + blob = pic_make_blob(pic, k); for (i = 0; i < k; ++i) { blob->data[i] = (unsigned char)b; } @@ -118,15 +116,16 @@ static pic_value pic_blob_bytevector_copy_i(pic_state *pic) { pic_blob *to, *from; - int n, at, start, end; + int n; + size_t at, start, end; - n = pic_get_args(pic, "bib|ii", &to, &at, &from, &start, &end); + n = pic_get_args(pic, "bkb|kk", &to, &at, &from, &start, &end); switch (n) { case 3: start = 0; case 4: - end = (int)from->len; + end = from->len; } if (to == from && (start <= at && at < end)) { @@ -149,23 +148,23 @@ static pic_value pic_blob_bytevector_copy(pic_state *pic) { pic_blob *from, *to; - int n, start, end, k, i = 0; + int n; + size_t start, end, i = 0; - n = pic_get_args(pic, "b|ii", &from, &start, &end); + n = pic_get_args(pic, "b|kk", &from, &start, &end); switch (n) { case 1: start = 0; case 2: - end = (int)from->len; + end = from->len; } - k = end - start; + if (end < start) { + pic_errorf(pic, "make-bytevector: end index must not be less than start index"); + } - if (k < 0) - pic_errorf(pic, "make-bytevector: cannot create a bytevector of length %d", k); - - to = pic_make_blob(pic, (size_t)k); + to = pic_make_blob(pic, end - start); while (start < end) { to->data[i++] = from->data[start++]; } @@ -211,7 +210,7 @@ pic_blob_list_to_bytevector(pic_state *pic) pic_get_args(pic, "o", &list); - blob = pic_make_blob(pic, (size_t)pic_length(pic, list)); + blob = pic_make_blob(pic, pic_length(pic, list)); data = blob->data; @@ -231,15 +230,16 @@ pic_blob_bytevector_to_list(pic_state *pic) { pic_blob *blob; pic_value list; - int n, start, end, i; + int n; + size_t start, end, i; - n = pic_get_args(pic, "b|ii", &blob, &start, &end); + n = pic_get_args(pic, "b|kk", &blob, &start, &end); switch (n) { case 1: start = 0; case 2: - end = (int)blob->len; + end = blob->len; } list = pic_nil_value(); From d34cbccf12062d0279df192a36f12a39c89321a2 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 18:10:14 +0900 Subject: [PATCH 17/27] use k format specifier in vector.c --- vector.c | 95 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/vector.c b/vector.c index ba0dc7c8..86cd3c9b 100644 --- a/vector.c +++ b/vector.c @@ -26,11 +26,11 @@ struct pic_vector * pic_make_vec_from_list(pic_state *pic, pic_value data) { struct pic_vector *vec; - int len, i; + size_t len, i; len = pic_length(pic, data); - vec = pic_make_vec(pic, (size_t)len); + vec = pic_make_vec(pic, len); for (i = 0; i < len; ++i) { vec->data[i] = pic_car(pic, data); data = pic_cdr(pic, data); @@ -70,19 +70,15 @@ static pic_value pic_vec_make_vector(pic_state *pic) { pic_value v; - int n, k; - size_t i; + int n; + size_t k, i; struct pic_vector *vec; - n = pic_get_args(pic, "i|o", &k, &v); + n = pic_get_args(pic, "k|o", &k, &v); - if (k < 0) { - pic_errorf(pic, "make-vector: vector length must be non-negative"); - } - - vec = pic_make_vec(pic, (size_t)k); + vec = pic_make_vec(pic, k); if (n == 2) { - for (i = 0; i < (size_t)k; ++i) { + for (i = 0; i < k; ++i) { vec->data[i] = v; } } @@ -103,11 +99,11 @@ static pic_value pic_vec_vector_ref(pic_state *pic) { struct pic_vector *v; - int k; + size_t k; - pic_get_args(pic, "vi", &v, &k); + pic_get_args(pic, "vk", &v, &k); - if (k < 0 || v->len <= (size_t)k) { + if (v->len <= k) { pic_errorf(pic, "vector-ref: index out of range"); } return v->data[k]; @@ -117,12 +113,12 @@ static pic_value pic_vec_vector_set(pic_state *pic) { struct pic_vector *v; - int k; + size_t k; pic_value o; - pic_get_args(pic, "vio", &v, &k, &o); + pic_get_args(pic, "vko", &v, &k, &o); - if (k < 0 || v->len <= (size_t)k) { + if (v->len <= k) { pic_errorf(pic, "vector-set!: index out of range"); } v->data[k] = o; @@ -133,15 +129,16 @@ static pic_value pic_vec_vector_copy_i(pic_state *pic) { pic_vec *to, *from; - int n, at, start, end; + int n; + size_t at, start, end; - n = pic_get_args(pic, "viv|ii", &to, &at, &from, &start, &end); + n = pic_get_args(pic, "vkv|kk", &to, &at, &from, &start, &end); switch (n) { case 3: start = 0; case 4: - end = (int)from->len; + end = from->len; } if (to == from && (start <= at && at < end)) { @@ -164,22 +161,23 @@ static pic_value pic_vec_vector_copy(pic_state *pic) { pic_vec *vec, *to; - int n, start, end, i = 0; + int n; + size_t start, end, i = 0; - n = pic_get_args(pic, "v|ii", &vec, &start, &end); + n = pic_get_args(pic, "v|kk", &vec, &start, &end); switch (n) { case 1: start = 0; case 2: - end = (int)vec->len; + end = vec->len; } - if (end - start < 0) { + if (end < start) { pic_errorf(pic, "vector-copy: end index must not be less than start index"); } - to = pic_make_vec(pic, (size_t)(end - start)); + to = pic_make_vec(pic, end - start); while (start < end) { to->data[i++] = vec->data[start++]; } @@ -221,15 +219,16 @@ pic_vec_vector_fill_i(pic_state *pic) { pic_vec *vec; pic_value obj; - int n, start, end; + int n; + size_t start, end; - n = pic_get_args(pic, "vo|ii", &vec, &obj, &start, &end); + n = pic_get_args(pic, "vo|kk", &vec, &obj, &start, &end); switch (n) { case 2: start = 0; case 3: - end = (int)vec->len; + end = vec->len; } while (start < end) { @@ -310,7 +309,7 @@ pic_vec_list_to_vector(pic_state *pic) pic_get_args(pic, "o", &list); - vec = pic_make_vec(pic, (size_t)pic_length(pic, list)); + vec = pic_make_vec(pic, pic_length(pic, list)); data = vec->data; @@ -325,15 +324,16 @@ pic_vec_vector_to_list(pic_state *pic) { struct pic_vector *vec; pic_value list; - int n, start, end, i; + int n; + size_t start, end, i; - n = pic_get_args(pic, "v|ii", &vec, &start, &end); + n = pic_get_args(pic, "v|kk", &vec, &start, &end); switch (n) { case 1: start = 0; case 2: - end = (int)vec->len; + end = vec->len; } list = pic_nil_value(); @@ -349,23 +349,24 @@ pic_vec_vector_to_string(pic_state *pic) { pic_vec *vec; char *buf; - int n, start, end, i; + int n; + size_t start, end, i; pic_str *str; - n = pic_get_args(pic, "v|ii", &vec, &start, &end); + n = pic_get_args(pic, "v|kk", &vec, &start, &end); switch (n) { case 1: start = 0; case 2: - end = (int)vec->len; + end = vec->len; } - if (end - start < 0) { + if (end < start) { pic_errorf(pic, "vector->string: end index must not be less than start index"); } - buf = pic_alloc(pic, (size_t)(end - start)); + buf = pic_alloc(pic, end - start); for (i = start; i < end; ++i) { pic_assert_type(pic, vec->data[i], char); @@ -373,7 +374,7 @@ pic_vec_vector_to_string(pic_state *pic) buf[i - start] = pic_char(vec->data[i]); } - str = pic_make_str(pic, buf, (size_t)(end - start)); + str = pic_make_str(pic, buf, end - start); pic_free(pic, buf); return pic_obj_value(str); @@ -383,30 +384,28 @@ static pic_value pic_vec_string_to_vector(pic_state *pic) { pic_str *str; - int n, start, end; + int n; + size_t start, end; size_t i; pic_vec *vec; - n = pic_get_args(pic, "s|ii", &str, &start, &end); + n = pic_get_args(pic, "s|kk", &str, &start, &end); switch (n) { case 1: start = 0; case 2: - end = (int)pic_strlen(str); + end = pic_strlen(str); } - if (start < 0) { - pic_errorf(pic, "string->vector: index must non-negative"); - } - if (end - start < 0) { + if (end < start) { pic_errorf(pic, "string->vector: end index must not be less than start index"); } - vec = pic_make_vec(pic, (size_t)(end - start)); + vec = pic_make_vec(pic, end - start); - for (i = 0; i < (size_t)(end - start); ++i) { - vec->data[i] = pic_char_value(pic_str_ref(pic, str, i + (size_t)start)); + for (i = 0; i < end - start; ++i) { + vec->data[i] = pic_char_value(pic_str_ref(pic, str, i + start)); } return pic_obj_value(vec); } From de51c221348b1e6673719baa91242f69a3688413 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 18:16:27 +0900 Subject: [PATCH 18/27] use k format specifier in pair.c --- pair.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pair.c b/pair.c index c2a775dc..763458f9 100644 --- a/pair.c +++ b/pair.c @@ -533,10 +533,10 @@ pic_pair_list_p(pic_state *pic) static pic_value pic_pair_make_list(pic_state *pic) { - int i; + size_t i; pic_value fill = pic_none_value(); - pic_get_args(pic, "i|o", &i, &fill); + pic_get_args(pic, "k|o", &i, &fill); return pic_make_list(pic, i, fill); } @@ -596,9 +596,9 @@ static pic_value pic_pair_list_tail(pic_state *pic) { pic_value list; - int i; + size_t i; - pic_get_args(pic, "oi", &list, &i); + pic_get_args(pic, "ok", &list, &i); return pic_list_tail(pic, list, i); } @@ -607,9 +607,9 @@ static pic_value pic_pair_list_ref(pic_state *pic) { pic_value list; - int i; + size_t i; - pic_get_args(pic, "oi", &list, &i); + pic_get_args(pic, "ok", &list, &i); return pic_list_ref(pic, list, i); } @@ -618,9 +618,9 @@ static pic_value pic_pair_list_set(pic_state *pic) { pic_value list, obj; - int i; + size_t i; - pic_get_args(pic, "oio", &list, &i, &obj); + pic_get_args(pic, "oko", &list, &i, &obj); pic_list_set(pic, list, i, obj); From b3def908b6ac15eca0dee2721d9268a664ef16b9 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 18:48:20 +0900 Subject: [PATCH 19/27] use k format specifier in string.c --- string.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/string.c b/string.c index 7f03cfae..6ef7e745 100644 --- a/string.c +++ b/string.c @@ -256,10 +256,10 @@ pic_str_string(pic_state *pic) static pic_value pic_str_make_string(pic_state *pic) { - int len; + size_t len; char c = ' '; - pic_get_args(pic, "i|c", &len, &c); + pic_get_args(pic, "k|c", &len, &c); return pic_obj_value(pic_make_str_fill(pic, len, c)); } @@ -278,9 +278,9 @@ static pic_value pic_str_string_ref(pic_state *pic) { pic_str *str; - int k; + size_t k; - pic_get_args(pic, "si", &str, &k); + pic_get_args(pic, "sk", &str, &k); return pic_char_value(pic_str_ref(pic, str, k)); } @@ -319,9 +319,10 @@ static pic_value pic_str_string_copy(pic_state *pic) { pic_str *str; - int n, start, end; + int n; + size_t start, end; - n = pic_get_args(pic, "s|ii", &str, &start, &end); + n = pic_get_args(pic, "s|kk", &str, &start, &end); switch (n) { case 1: @@ -356,14 +357,14 @@ static pic_value pic_str_string_map(pic_state *pic) { struct pic_proc *proc; - int argc, i; + int argc; pic_value *argv, vals, val; - size_t len, j; + size_t i, len, j; pic_get_args(pic, "l*", &proc, &argc, &argv); len = SIZE_MAX; - for (i = 0; i < argc; ++i) { + for (i = 0; i < (size_t)argc; ++i) { pic_assert_type(pic, argv[i], str); len = len < pic_strlen(pic_str_ptr(argv[i])) @@ -378,7 +379,7 @@ pic_str_string_map(pic_state *pic) for (i = 0; i < len; ++i) { vals = pic_nil_value(); - for (j = 0; j < argc; ++j) { + for (j = 0; j < (size_t)argc; ++j) { pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } val = pic_apply(pic, proc, vals); @@ -395,14 +396,14 @@ static pic_value pic_str_string_for_each(pic_state *pic) { struct pic_proc *proc; - int argc, i; - size_t len, j; + int argc; + size_t len, i, j; pic_value *argv, vals, val; pic_get_args(pic, "l*", &proc, &argc, &argv); len = SIZE_MAX; - for (i = 0; i < argc; ++i) { + for (i = 0; i < (size_t)argc; ++i) { pic_assert_type(pic, argv[i], str); len = len < pic_strlen(pic_str_ptr(argv[i])) @@ -415,7 +416,7 @@ pic_str_string_for_each(pic_state *pic) for (i = 0; i < len; ++i) { vals = pic_nil_value(); - for (j = 0; j < argc; ++j) { + for (j = 0; j < (size_t)argc; ++j) { pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } val = pic_apply(pic, proc, vals); @@ -429,7 +430,7 @@ pic_str_list_to_string(pic_state *pic) { pic_str *str; pic_value list, e; - int i = 0; + size_t i = 0; pic_get_args(pic, "o", &list); @@ -455,15 +456,16 @@ pic_str_string_to_list(pic_state *pic) { pic_str *str; pic_value list; - int n, start, end, i; + int n; + size_t start, end, i; - n = pic_get_args(pic, "s|ii", &str, &start, &end); + n = pic_get_args(pic, "s|kk", &str, &start, &end); switch (n) { case 1: start = 0; case 2: - end = (int)pic_strlen(str); + end = pic_strlen(str); } list = pic_nil_value(); From cf9d48a8653cb2dfb2826eca1cc4423de01849e9 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 19:40:10 +0900 Subject: [PATCH 20/27] add pic_size_value --- blob.c | 2 +- dict.c | 2 +- include/picrin/value.h | 12 ++++++++++++ pair.c | 2 +- string.c | 2 +- vector.c | 2 +- 6 files changed, 17 insertions(+), 5 deletions(-) diff --git a/blob.c b/blob.c index 509b295a..ae0f2fb7 100644 --- a/blob.c +++ b/blob.c @@ -83,7 +83,7 @@ pic_blob_bytevector_length(pic_state *pic) pic_get_args(pic, "b", &bv); - return pic_int_value((int)bv->len); + return pic_size_value(bv->len); } static pic_value diff --git a/dict.c b/dict.c index 2a8fcd41..c5387706 100644 --- a/dict.c +++ b/dict.c @@ -213,7 +213,7 @@ pic_dict_dictionary_size(pic_state *pic) pic_get_args(pic, "d", &dict); - return pic_int_value((int)pic_dict_size(pic, dict)); + return pic_size_value(pic_dict_size(pic, dict)); } static pic_value diff --git a/include/picrin/value.h b/include/picrin/value.h index 37dd58c0..d0c1dbe3 100644 --- a/include/picrin/value.h +++ b/include/picrin/value.h @@ -189,6 +189,7 @@ static inline pic_value pic_undef_value(); static inline pic_value pic_obj_value(void *); static inline pic_value pic_float_value(double); static inline pic_value pic_int_value(int); +static inline pic_value pic_size_value(size_t); static inline pic_value pic_sym_value(pic_sym); static inline pic_value pic_char_value(char c); static inline pic_value pic_none_value(); @@ -323,6 +324,17 @@ pic_bool_value(bool b) return v; } +static inline pic_value +pic_size_value(size_t s) +{ + if (sizeof(unsigned) < sizeof(size_t)) { + if (s > (size_t)INT_MAX) { + return pic_float_value(s); + } + } + return pic_int_value((int)s); +} + #if PIC_NAN_BOXING static inline pic_value diff --git a/pair.c b/pair.c index 763458f9..184182cc 100644 --- a/pair.c +++ b/pair.c @@ -559,7 +559,7 @@ pic_pair_length(pic_state *pic) pic_get_args(pic, "o", &list); - return pic_int_value(pic_length(pic, list)); + return pic_size_value(pic_length(pic, list)); } static pic_value diff --git a/string.c b/string.c index 6ef7e745..cd4b3660 100644 --- a/string.c +++ b/string.c @@ -271,7 +271,7 @@ pic_str_string_length(pic_state *pic) pic_get_args(pic, "s", &str); - return pic_int_value(pic_strlen(str)); + return pic_size_value(pic_strlen(str)); } static pic_value diff --git a/vector.c b/vector.c index 86cd3c9b..a7dbd81d 100644 --- a/vector.c +++ b/vector.c @@ -92,7 +92,7 @@ pic_vec_vector_length(pic_state *pic) pic_get_args(pic, "v", &v); - return pic_int_value((int)v->len); + return pic_size_value(v->len); } static pic_value From 7f68fd3e2a6938c6633fc200605b17ce4d019b83 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 19:48:58 +0900 Subject: [PATCH 21/27] use k format specifier in port.c --- port.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/port.c b/port.c index 5cbcb1b8..ec61d984 100644 --- a/port.c +++ b/port.c @@ -521,20 +521,15 @@ pic_port_read_blob(pic_state *pic) { struct pic_port *port = pic_stdin(pic); pic_blob *blob; - int k; - size_t i; + size_t k, i; - pic_get_args(pic, "i|p", &k, &port); + pic_get_args(pic, "k|p", &k, &port); assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector"); - if (k < 0) { - pic_errorf(pic, "read-bytevector: index must be non-negative %d", k); - } + blob = pic_make_blob(pic, k); - blob = pic_make_blob(pic, (size_t)k); - - i = xfread(blob->data, sizeof(char), (size_t)k, port->file); + i = xfread(blob->data, sizeof(char), k, port->file); if (i == 0) { return pic_eof_object(); } @@ -550,27 +545,27 @@ pic_port_read_blob_ip(pic_state *pic) { struct pic_port *port; struct pic_blob *bv; - int n, start, end; + int n; char *buf; - size_t i, len; + size_t start, end, i, len; - n = pic_get_args(pic, "b|pii", &bv, &port, &start, &end); + n = pic_get_args(pic, "b|pkk", &bv, &port, &start, &end); switch (n) { case 1: port = pic_stdin(pic); case 2: start = 0; case 3: - end = (int)bv->len; + end = bv->len; } assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector!"); - if (end - start < 0) { + if (end < start) { pic_errorf(pic, "read-bytevector!: end index must be greater than or equal to start index"); } - len = (size_t)(end - start); + len = end - start; buf = pic_calloc(pic, len, sizeof(char)); i = xfread(buf, sizeof(char), len, port->file); @@ -581,7 +576,7 @@ pic_port_read_blob_ip(pic_state *pic) return pic_eof_object(); } else { - return pic_int_value((int)i); + return pic_size_value(i); } } @@ -656,16 +651,17 @@ pic_port_write_blob(pic_state *pic) { struct pic_blob *blob; struct pic_port *port; - int start, end, n, i; + int n; + size_t start, end, i; - n = pic_get_args(pic, "b|pii", &blob, &port, &start, &end); + n = pic_get_args(pic, "b|pkk", &blob, &port, &start, &end); switch (n) { case 1: port = pic_stdout(pic); case 2: start = 0; case 3: - end = (int)blob->len; + end = blob->len; } assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, PIC_PORT_OPEN, "write-bytevector"); From d165b6ea6fbe995b7fb5c68023a32a290ff9344c Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 19:49:32 +0900 Subject: [PATCH 22/27] avoid explicit casts if possible (small refacotring on dict.c, system.c) --- dict.c | 10 ++++++---- system.c | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/dict.c b/dict.c index c5387706..890af074 100644 --- a/dict.c +++ b/dict.c @@ -12,11 +12,13 @@ xh_value_hash(const void *key, void *data) { union { double f; int i; } u; pic_value val = *(pic_value *)key; - int hash; + int hash, vtype; UNUSED(data); - switch (pic_vtype(val)) { + vtype = pic_vtype(val); + + switch (vtype) { default: hash = 0; break; @@ -31,11 +33,11 @@ xh_value_hash(const void *key, void *data) hash = pic_int(val); break; case PIC_VTYPE_HEAP: - hash = (int)pic_ptr(val); + hash = (int)(intptr_t)pic_ptr(val); break; } - return hash + (int)pic_vtype(val); + return hash + vtype; } static int diff --git a/system.c b/system.c index e9ef1aa9..1b251661 100644 --- a/system.c +++ b/system.c @@ -105,12 +105,12 @@ pic_system_getenvs(pic_state *pic) for (envp = pic->envp; *envp; ++envp) { pic_str *key, *val; - int i; + size_t i; for (i = 0; (*envp)[i] != '='; ++i) ; - key = pic_make_str(pic, *envp, (size_t)i); + key = pic_make_str(pic, *envp, i); val = pic_make_str_cstr(pic, getenv(pic_str_cstr(key))); /* push */ From 552ee7444fc6e1519a84eb5d8cc5cbc6ae15cf91 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 19:58:04 +0900 Subject: [PATCH 23/27] more fix --- codegen.c | 4 ++-- time.c | 2 +- vm.c | 10 ++++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/codegen.c b/codegen.c index c4e3121e..f6183278 100644 --- a/codegen.c +++ b/codegen.c @@ -1375,7 +1375,7 @@ codegen(codegen_state *state, pic_value obj) return; } else if (sym == state->sCALL || sym == state->sTAILCALL) { - int len = pic_length(pic, obj); + int len = (int)pic_length(pic, obj); pic_value elt; pic_for_each (elt, pic_cdr(pic, obj)) { @@ -1401,7 +1401,7 @@ codegen(codegen_state *state, pic_value obj) return; } else if (sym == state->sRETURN) { - int len = pic_length(pic, obj); + int len = (int)pic_length(pic, obj); pic_value elt; pic_for_each (elt, pic_cdr(pic, obj)) { diff --git a/time.c b/time.c index a0a1ffb6..83716db8 100644 --- a/time.c +++ b/time.c @@ -27,7 +27,7 @@ pic_current_jiffy(pic_state *pic) pic_get_args(pic, ""); c = clock(); - return pic_int_value((int)c); + return pic_int_value((int)c); /* The year 2038 problem :-| */ } static pic_value diff --git a/vm.c b/vm.c index 97e21bf6..fbdd6c2b 100644 --- a/vm.c +++ b/vm.c @@ -212,8 +212,10 @@ pic_get_args(pic_state *pic, const char *format, ...) if (x < 0) { pic_errorf(pic, "pic_get_args: expected non-negative int, but got ~s", v); } - if (sizeof(unsigned) > sizeof(size_t) && (unsigned)x > (unsigned)SIZE_MAX) { - pic_errorf(pic, "pic_get_args: int unrepresentable with size_t ~s", v); + if (sizeof(unsigned) > sizeof(size_t)) { + if ((unsigned)x > (unsigned)SIZE_MAX) { + pic_errorf(pic, "pic_get_args: int unrepresentable with size_t ~s", v); + } } *k = (size_t)x; break; @@ -707,7 +709,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value args) else { int argc, i; - argc = pic_length(pic, args) + 1; + argc = (int)pic_length(pic, args) + 1; VM_BOOT_PRINT; @@ -1139,7 +1141,7 @@ pic_apply_trampoline(pic_state *pic, struct pic_proc *proc, pic_value args) ci = PUSHCI(); ci->ip = (pic_code *)iseq; ci->fp = pic->sp; - ci->retc = pic_length(pic, args); + ci->retc = (int)pic_length(pic, args); if (ci->retc == 0) { return pic_none_value(); From c808b34a677815886f784c4c89d4fbf821d9f397 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 20:15:47 +0900 Subject: [PATCH 24/27] refactor map and for-each --- pair.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/pair.c b/pair.c index 184182cc..cbb4de70 100644 --- a/pair.c +++ b/pair.c @@ -643,23 +643,24 @@ pic_pair_map(pic_state *pic) struct pic_proc *proc; int argc, i; pic_value *args; - pic_value cars, ret; + pic_value arg, ret; pic_get_args(pic, "l*", &proc, &argc, &args); ret = pic_nil_value(); do { - cars = pic_nil_value(); - for (i = argc - 1; i >= 0; --i) { + arg = pic_nil_value(); + for (i = 0; i < argc; ++i) { if (! pic_pair_p(args[i])) { break; } - cars = pic_cons(pic, pic_car(pic, args[i]), cars); + pic_push(pic, pic_car(pic, args[i]), arg); args[i] = pic_cdr(pic, args[i]); } - if (i >= 0) + if (i != argc) { break; - ret = pic_cons(pic, pic_apply(pic, proc, cars), ret); + } + pic_push(pic, pic_apply(pic, proc, pic_reverse(pic, arg)), ret); } while (1); return pic_reverse(pic, ret); @@ -671,22 +672,23 @@ pic_pair_for_each(pic_state *pic) struct pic_proc *proc; int argc, i; pic_value *args; - pic_value cars; + pic_value arg; pic_get_args(pic, "l*", &proc, &argc, &args); do { - cars = pic_nil_value(); - for (i = argc - 1; i >= 0; --i) { + arg = pic_nil_value(); + for (i = 0; i < argc; ++i) { if (! pic_pair_p(args[i])) { break; } - cars = pic_cons(pic, pic_car(pic, args[i]), cars); + pic_push(pic, pic_car(pic, args[i]), arg); args[i] = pic_cdr(pic, args[i]); } - if (i >= 0) + if (i != argc) { break; - pic_apply(pic, proc, cars); + } + pic_apply(pic, proc, pic_reverse(pic, arg)); } while (1); return pic_none_value(); From 186c468c165cdfabc8ead6f85a39efbe4041dd4d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 20:17:02 +0900 Subject: [PATCH 25/27] revert: argc must be size_t type --- blob.c | 7 +++---- bool.c | 2 +- char.c | 2 +- cont.c | 4 ++-- dict.c | 2 +- error.c | 4 ++-- lib.c | 8 ++++---- number.c | 6 +++--- pair.c | 8 ++++---- proc.c | 2 +- string.c | 20 +++++++++----------- symbol.c | 2 +- vector.c | 19 ++++++++----------- vm.c | 6 +++--- 14 files changed, 43 insertions(+), 49 deletions(-) diff --git a/blob.c b/blob.c index ae0f2fb7..3e5b7723 100644 --- a/blob.c +++ b/blob.c @@ -33,13 +33,13 @@ static pic_value pic_blob_bytevector(pic_state *pic) { pic_value *argv; - int argc, i; + size_t argc, i; pic_blob *blob; unsigned char *data; pic_get_args(pic, "*", &argc, &argv); - blob = pic_make_blob(pic, (size_t)argc); + blob = pic_make_blob(pic, argc); data = blob->data; @@ -175,8 +175,7 @@ pic_blob_bytevector_copy(pic_state *pic) static pic_value pic_blob_bytevector_append(pic_state *pic) { - size_t j, len; - int argc, i; + size_t argc, i, j, len; pic_value *argv; pic_blob *blob; diff --git a/bool.c b/bool.c index ff5528e9..8f8c75f1 100644 --- a/bool.c +++ b/bool.c @@ -172,7 +172,7 @@ pic_bool_boolean_p(pic_state *pic) static pic_value pic_bool_boolean_eq_p(pic_state *pic) { - int argc, i; + size_t argc, i; pic_value *argv; pic_get_args(pic, "*", &argc, &argv); diff --git a/char.c b/char.c index f08fcdd0..d9c675e7 100644 --- a/char.c +++ b/char.c @@ -42,7 +42,7 @@ pic_char_integer_to_char(pic_state *pic) static pic_value \ pic_char_##name##_p(pic_state *pic) \ { \ - int argc, i; \ + size_t argc, i; \ pic_value *argv; \ char c, d; \ \ diff --git a/cont.c b/cont.c index 4b980e4a..41a0fdb8 100644 --- a/cont.c +++ b/cont.c @@ -96,7 +96,7 @@ pic_load_point(pic_state *pic, struct pic_escape *escape) noreturn static pic_value escape_call(pic_state *pic) { - int argc; + size_t argc; pic_value *argv; struct pic_data *e; @@ -251,7 +251,7 @@ pic_cont_dynamic_wind(pic_state *pic) static pic_value pic_cont_values(pic_state *pic) { - int argc; + size_t argc; pic_value *argv; pic_get_args(pic, "*", &argc, &argv); diff --git a/dict.c b/dict.c index 890af074..81bdea68 100644 --- a/dict.c +++ b/dict.c @@ -144,7 +144,7 @@ pic_dict_dictionary(pic_state *pic) { struct pic_dict *dict; pic_value *argv; - int argc, i; + size_t argc, i; pic_get_args(pic, "*", &argc, &argv); diff --git a/error.c b/error.c index 0f3ac651..3b462969 100644 --- a/error.c +++ b/error.c @@ -245,7 +245,7 @@ noreturn static pic_value pic_error_error(pic_state *pic) { const char *str; - int argc; + size_t argc; pic_value *argv; pic_get_args(pic, "z*", &str, &argc, &argv); @@ -259,7 +259,7 @@ pic_error_make_error_object(pic_state *pic) struct pic_error *e; pic_sym type; pic_str *msg; - int argc; + size_t argc; pic_value *argv; pic_get_args(pic, "ms*", &type, &msg, &argc, &argv); diff --git a/lib.c b/lib.c index cd760f18..37cba2bd 100644 --- a/lib.c +++ b/lib.c @@ -253,7 +253,7 @@ static pic_value pic_lib_condexpand(pic_state *pic) { pic_value *clauses; - int argc, i; + size_t argc, i; pic_get_args(pic, "*", &argc, &clauses); @@ -269,7 +269,7 @@ pic_lib_condexpand(pic_state *pic) static pic_value pic_lib_import(pic_state *pic) { - int argc, i; + size_t argc, i; pic_value *argv; pic_get_args(pic, "*", &argc, &argv); @@ -284,7 +284,7 @@ pic_lib_import(pic_state *pic) static pic_value pic_lib_export(pic_state *pic) { - int argc, i; + size_t argc, i; pic_value *argv; pic_get_args(pic, "*", &argc, &argv); @@ -300,7 +300,7 @@ static pic_value pic_lib_define_library(pic_state *pic) { struct pic_lib *prev = pic->lib; - int argc, i; + size_t argc, i; pic_value spec, *argv; pic_get_args(pic, "o*", &spec, &argc, &argv); diff --git a/number.c b/number.c index c72f58ca..88db88d6 100644 --- a/number.c +++ b/number.c @@ -162,7 +162,7 @@ pic_number_nan_p(pic_state *pic) static pic_value \ pic_number_##name(pic_state *pic) \ { \ - int argc, i; \ + size_t argc, i; \ pic_value *argv; \ double f,g; \ \ @@ -197,7 +197,7 @@ DEFINE_ARITH_CMP(>=, ge) static pic_value \ pic_number_##name(pic_state *pic) \ { \ - int argc, i; \ + size_t argc, i; \ pic_value *argv; \ double f; \ bool e = true; \ @@ -228,7 +228,7 @@ DEFINE_ARITH_OP(*, mul, 1) static pic_value \ pic_number_##name(pic_state *pic) \ { \ - int argc, i; \ + size_t argc, i; \ pic_value *argv; \ double f; \ bool e; \ diff --git a/pair.c b/pair.c index cbb4de70..f2803f1a 100644 --- a/pair.c +++ b/pair.c @@ -544,7 +544,7 @@ pic_pair_make_list(pic_state *pic) static pic_value pic_pair_list(pic_state *pic) { - int argc; + size_t argc; pic_value *argv; pic_get_args(pic, "*", &argc, &argv); @@ -565,7 +565,7 @@ pic_pair_length(pic_state *pic) static pic_value pic_pair_append(pic_state *pic) { - int argc; + size_t argc; pic_value *args, list; pic_get_args(pic, "*", &argc, &args); @@ -641,7 +641,7 @@ static pic_value pic_pair_map(pic_state *pic) { struct pic_proc *proc; - int argc, i; + size_t argc, i; pic_value *args; pic_value arg, ret; @@ -670,7 +670,7 @@ static pic_value pic_pair_for_each(pic_state *pic) { struct pic_proc *proc; - int argc, i; + size_t argc, i; pic_value *args; pic_value arg; diff --git a/proc.c b/proc.c index 0d67ddd7..210f157d 100644 --- a/proc.c +++ b/proc.c @@ -61,7 +61,7 @@ pic_proc_apply(pic_state *pic) { struct pic_proc *proc; pic_value *args; - int argc; + size_t argc; pic_value arg_list; pic_get_args(pic, "l*", &proc, &argc, &args); diff --git a/string.c b/string.c index cd4b3660..f9f15a59 100644 --- a/string.c +++ b/string.c @@ -233,7 +233,7 @@ pic_str_string_p(pic_state *pic) static pic_value pic_str_string(pic_state *pic) { - int argc, i; + size_t argc, i; pic_value *argv; pic_str *str; char *buf; @@ -289,7 +289,7 @@ pic_str_string_ref(pic_state *pic) static pic_value \ pic_str_string_##name(pic_state *pic) \ { \ - int argc, i; \ + size_t argc, i; \ pic_value *argv; \ \ pic_get_args(pic, "*", &argc, &argv); \ @@ -337,7 +337,7 @@ pic_str_string_copy(pic_state *pic) static pic_value pic_str_string_append(pic_state *pic) { - int argc, i; + size_t argc, i; pic_value *argv; pic_str *str; @@ -357,14 +357,13 @@ static pic_value pic_str_string_map(pic_state *pic) { struct pic_proc *proc; - int argc; pic_value *argv, vals, val; - size_t i, len, j; + size_t argc, i, len, j; pic_get_args(pic, "l*", &proc, &argc, &argv); len = SIZE_MAX; - for (i = 0; i < (size_t)argc; ++i) { + for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], str); len = len < pic_strlen(pic_str_ptr(argv[i])) @@ -379,7 +378,7 @@ pic_str_string_map(pic_state *pic) for (i = 0; i < len; ++i) { vals = pic_nil_value(); - for (j = 0; j < (size_t)argc; ++j) { + for (j = 0; j < argc; ++j) { pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } val = pic_apply(pic, proc, vals); @@ -396,14 +395,13 @@ static pic_value pic_str_string_for_each(pic_state *pic) { struct pic_proc *proc; - int argc; - size_t len, i, j; + size_t argc, len, i, j; pic_value *argv, vals, val; pic_get_args(pic, "l*", &proc, &argc, &argv); len = SIZE_MAX; - for (i = 0; i < (size_t)argc; ++i) { + for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], str); len = len < pic_strlen(pic_str_ptr(argv[i])) @@ -416,7 +414,7 @@ pic_str_string_for_each(pic_state *pic) for (i = 0; i < len; ++i) { vals = pic_nil_value(); - for (j = 0; j < (size_t)argc; ++j) { + for (j = 0; j < argc; ++j) { pic_push(pic, pic_char_value(pic_str_ref(pic, pic_str_ptr(argv[j]), i)), vals); } val = pic_apply(pic, proc, vals); diff --git a/symbol.c b/symbol.c index 7e9f1b52..115582c8 100644 --- a/symbol.c +++ b/symbol.c @@ -108,7 +108,7 @@ pic_symbol_symbol_p(pic_state *pic) static pic_value pic_symbol_symbol_eq_p(pic_state *pic) { - int argc, i; + size_t argc, i; pic_value *argv; pic_get_args(pic, "*", &argc, &argv); diff --git a/vector.c b/vector.c index a7dbd81d..33070d24 100644 --- a/vector.c +++ b/vector.c @@ -51,7 +51,7 @@ pic_vec_vector_p(pic_state *pic) static pic_value pic_vec_vector(pic_state *pic) { - int argc, i; + size_t argc, i; pic_value *argv; pic_vec *vec; @@ -188,9 +188,8 @@ pic_vec_vector_copy(pic_state *pic) static pic_value pic_vec_vector_append(pic_state *pic) { - int argc, i; pic_value *argv; - size_t j, len; + size_t argc, i, j, len; pic_vec *vec; pic_get_args(pic, "*", &argc, &argv); @@ -242,15 +241,14 @@ static pic_value pic_vec_vector_map(pic_state *pic) { struct pic_proc *proc; - int argc; - size_t i, len, j; + size_t argc, i, len, j; pic_value *argv, vals; pic_vec *vec; pic_get_args(pic, "l*", &proc, &argc, &argv); len = INT_MAX; - for (i = 0; i < (size_t)argc; ++i) { + for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], vec); len = len < pic_vec_ptr(argv[i])->len @@ -262,7 +260,7 @@ pic_vec_vector_map(pic_state *pic) for (i = 0; i < len; ++i) { vals = pic_nil_value(); - for (j = 0; j < (size_t)argc; ++j) { + for (j = 0; j < argc; ++j) { pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals); } vec->data[i] = pic_apply(pic, proc, vals); @@ -275,14 +273,13 @@ static pic_value pic_vec_vector_for_each(pic_state *pic) { struct pic_proc *proc; - int argc; - size_t i, len, j; + size_t argc, i, len, j; pic_value *argv, vals; pic_get_args(pic, "l*", &proc, &argc, &argv); len = INT_MAX; - for (i = 0; i < (size_t)argc; ++i) { + for (i = 0; i < argc; ++i) { pic_assert_type(pic, argv[i], vec); len = len < pic_vec_ptr(argv[i])->len @@ -292,7 +289,7 @@ pic_vec_vector_for_each(pic_state *pic) for (i = 0; i < len; ++i) { vals = pic_nil_value(); - for (j = 0; j < (size_t)argc; ++j) { + for (j = 0; j < argc; ++j) { pic_push(pic, pic_vec_ptr(argv[j])->data[i], vals); } pic_apply(pic, proc, vals); diff --git a/vm.c b/vm.c index fbdd6c2b..accb47ce 100644 --- a/vm.c +++ b/vm.c @@ -416,13 +416,13 @@ pic_get_args(pic_state *pic, const char *format, ...) } } if ('*' == c) { - int *n; + size_t *n; pic_value **argv; - n = va_arg(ap, int *); + n = va_arg(ap, size_t *); argv = va_arg(ap, pic_value **); if (i <= argc) { - *n = argc - i; + *n = (size_t)(argc - i); *argv = &GET_OPERAND(pic, i); i = argc; } From 0403ca5570c81004f5477cfd97f2ccd2ba9fe499 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 20:21:54 +0900 Subject: [PATCH 26/27] more fixes --- cont.c | 16 ++++++++-------- include/picrin/cont.h | 4 ++-- include/picrin/pair.h | 2 +- pair.c | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cont.c b/cont.c index 41a0fdb8..4e38e8c6 100644 --- a/cont.c +++ b/cont.c @@ -184,14 +184,14 @@ pic_values5(pic_state *pic, pic_value arg1, pic_value arg2, pic_value arg3, pic_ } pic_value -pic_values_by_array(pic_state *pic, int argc, pic_value *argv) +pic_values_by_array(pic_state *pic, size_t argc, pic_value *argv) { - int i; + size_t i; for (i = 0; i < argc; ++i) { pic->sp[i] = argv[i]; } - pic->ci->retc = argc; + pic->ci->retc = (int)argc; return argc == 0 ? pic_none_value() : pic->sp[0]; } @@ -211,15 +211,15 @@ pic_values_by_list(pic_state *pic, pic_value list) return pic_nil_p(list) ? pic_none_value() : pic->sp[0]; } -int -pic_receive(pic_state *pic, int n, pic_value *argv) +size_t +pic_receive(pic_state *pic, size_t n, pic_value *argv) { pic_callinfo *ci; - int i, retc; + size_t i, retc; /* take info from discarded frame */ ci = pic->ci + 1; - retc = ci->retc; + retc = (size_t)ci->retc; for (i = 0; i < retc && i < n; ++i) { argv[i] = ci->fp[i]; @@ -263,7 +263,7 @@ static pic_value pic_cont_call_with_values(pic_state *pic) { struct pic_proc *producer, *consumer; - int argc; + size_t argc; pic_value args[256]; pic_get_args(pic, "ll", &producer, &consumer); diff --git a/include/picrin/cont.h b/include/picrin/cont.h index 01e86e11..645e6d9c 100644 --- a/include/picrin/cont.h +++ b/include/picrin/cont.h @@ -40,9 +40,9 @@ pic_value pic_values2(pic_state *, pic_value, pic_value); pic_value pic_values3(pic_state *, pic_value, pic_value, pic_value); pic_value pic_values4(pic_state *, pic_value, pic_value, pic_value, pic_value); pic_value pic_values5(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value); -pic_value pic_values_by_array(pic_state *, int, pic_value *); +pic_value pic_values_by_array(pic_state *, size_t, pic_value *); pic_value pic_values_by_list(pic_state *, pic_value); -int pic_receive(pic_state *, int, pic_value *); +size_t pic_receive(pic_state *, size_t, pic_value *); pic_value pic_escape(pic_state *, struct pic_proc *); diff --git a/include/picrin/pair.h b/include/picrin/pair.h index d6cf779d..d489b765 100644 --- a/include/picrin/pair.h +++ b/include/picrin/pair.h @@ -56,7 +56,7 @@ pic_value pic_list4(pic_state *, pic_value, pic_value, pic_value, pic_value); pic_value pic_list5(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value); pic_value pic_list6(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value, pic_value); pic_value pic_list7(pic_state *, pic_value, pic_value, pic_value, pic_value, pic_value, pic_value, pic_value); -pic_value pic_list_by_array(pic_state *, int, pic_value *); +pic_value pic_list_by_array(pic_state *, size_t, pic_value *); pic_value pic_make_list(pic_state *, size_t, pic_value); #define pic_for_each(var, list) \ diff --git a/pair.c b/pair.c index f2803f1a..b662534a 100644 --- a/pair.c +++ b/pair.c @@ -160,7 +160,7 @@ pic_list7(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_va } pic_value -pic_list_by_array(pic_state *pic, int c, pic_value *vs) +pic_list_by_array(pic_state *pic, size_t c, pic_value *vs) { pic_value v; From 1b371026761b7f65ac41a669ebc28eb18b4e7a07 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 20:43:31 +0900 Subject: [PATCH 27/27] suppress warning in compiling read.c --- read.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/read.c b/read.c index 7deb5eb2..be160c0d 100644 --- a/read.c +++ b/read.c @@ -209,7 +209,7 @@ read_symbol(pic_state *pic, struct pic_port *port, const char *str) for (i = 0; i < len; ++i) { if (pic->reader->typecase == PIC_CASE_FOLD) { - buf[i] = tolower(str[i]); + buf[i] = (char)tolower(str[i]); } else { buf[i] = str[i]; } @@ -222,7 +222,7 @@ read_symbol(pic_state *pic, struct pic_port *port, const char *str) } len += 1; buf = pic_realloc(pic, buf, len + 1); - buf[len - 1] = c; + buf[len - 1] = (char)c; } sym = pic_intern(pic, buf, len); @@ -240,9 +240,9 @@ read_uinteger(pic_state *pic, struct pic_port *port, int c, char buf[]) read_error(pic, "expected one or more digits"); } - buf[i++] = c; + buf[i++] = (char)c; while (isdigit(c = peek(port))) { - buf[i++] = next(port); + buf[i++] = (char)next(port); } buf[i] = '\0'; @@ -262,12 +262,12 @@ read_suffix(pic_state *pic, struct pic_port *port, char buf[]) return i; } - buf[i++] = next(port); + buf[i++] = (char)next(port); switch ((c = next(port))) { case '-': case '+': - buf[i++] = c; + buf[i++] = (char)c; c = next(port); default: return i + read_uinteger(pic, port, c, buf + i); @@ -284,14 +284,14 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) switch (peek(port)) { case '.': - buf[i++] = next(port); + buf[i++] = (char)next(port); i += read_uinteger(pic, port, next(port), buf + i); read_suffix(pic, port, buf + i); return pic_float_value(atof(buf)); default: read_suffix(pic, port, buf + i); - return pic_int_value((int)atof(buf)); + return pic_int_value((int)(atof(buf))); } } @@ -404,7 +404,7 @@ read_char(pic_state *pic, struct pic_port *port, const char *str) } } - return pic_char_value(c); + return pic_char_value((char)c); fail: read_error(pic, "unexpected character while reading character literal"); @@ -436,7 +436,7 @@ read_string(pic_state *pic, struct pic_port *port, const char *name) case 'r': c = '\r'; break; } } - buf[cnt++] = c; + buf[cnt++] = (char)c; if (cnt >= size) { buf = pic_realloc(pic, buf, size *= 2); } @@ -474,15 +474,15 @@ read_pipe(pic_state *pic, struct pic_port *port, const char *str) case 'r': c = '\r'; break; case 'x': i = 0; - while ((HEX_BUF[i++] = next(port)) != ';') { + while ((HEX_BUF[i++] = (char)next(port)) != ';') { if (i >= sizeof HEX_BUF) read_error(pic, "expected ';'"); } - c = strtol(HEX_BUF, NULL, 16); + c = (char)strtol(HEX_BUF, NULL, 16); break; } } - buf[cnt++] = c; + buf[cnt++] = (char)c; if (cnt >= size) { buf = pic_realloc(pic, buf, size *= 2); } @@ -500,7 +500,8 @@ read_blob(pic_state *pic, struct pic_port *port, const char *str) { int nbits, n, c; size_t len, i; - char *dat, buf[256]; + char buf[256]; + unsigned char *dat; pic_blob *blob; UNUSED(str); @@ -530,7 +531,7 @@ read_blob(pic_state *pic, struct pic_port *port, const char *str) } len += 1; dat = pic_realloc(pic, dat, len); - dat[len - 1] = n; + dat[len - 1] = (unsigned char)n; c = next(port); } @@ -710,7 +711,7 @@ read_nullable(pic_state *pic, struct pic_port *port, int c) read_error(pic, "invalid character at the seeker head"); } - buf[i++] = c; + buf[i++] = (char)c; while (i < sizeof buf) { trie = trie->table[c]; @@ -721,7 +722,7 @@ read_nullable(pic_state *pic, struct pic_port *port, int c) if (trie->table[c] == NULL) { break; } - buf[i++] = next(port); + buf[i++] = (char)next(port); } if (i == sizeof buf) { read_error(pic, "too long dispatch string");