From 540066c9e2cd89bd22201b18f09b5f7d7fbcd612 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Wed, 12 Sep 2012 16:20:32 +0900 Subject: [PATCH 01/10] mrb_sym can be short integer; reduced 10KB --- include/mrbconf.h | 3 +- src/codegen.c | 57 +++++++++++---------- src/parse.y | 127 ++++++++++++++++++++++++---------------------- 3 files changed, 95 insertions(+), 92 deletions(-) diff --git a/include/mrbconf.h b/include/mrbconf.h index b4332adcd..d7ccb261d 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -62,11 +62,10 @@ typedef double mrb_float; #ifdef MRB_NAN_BOXING typedef int32_t mrb_int; -typedef int32_t mrb_sym; #else typedef int mrb_int; -typedef intptr_t mrb_sym; #endif +typedef short mrb_sym; /* define ENABLE_XXXX from DISABLE_XXX */ #ifndef DISABLE_REGEXP diff --git a/src/codegen.c b/src/codegen.c index 799ec1500..50d3e3d3b 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -466,7 +466,8 @@ node_len(node *tree) return n; } -#define lv_name(lv) ((mrb_sym)(lv)->car) +#define sym(x) ((mrb_sym)(intptr_t)(x)) +#define lv_name(lv) sym((lv)->car) static int lv_idx(codegen_scope *s, mrb_sym id) { @@ -582,7 +583,7 @@ lambda_body(codegen_scope *s, node *tree, int blk) dispatch(s, pos+i); codegen(s, opt->car->cdr, VAL); - idx = lv_idx(s, (mrb_sym)opt->car->car); + idx = lv_idx(s, (mrb_sym)(intptr_t)opt->car->car); pop(); genop_peep(s, MKOP_AB(OP_MOVE, idx, cursp()), NOVAL); i++; @@ -706,7 +707,7 @@ gen_values(codegen_scope *s, node *t) static void gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val) { - mrb_sym sym = name ? name : (mrb_sym)tree->cdr->car; + mrb_sym sym = name ? name : sym(tree->cdr->car); int idx; int n = 0, noop = 0, sendv = 0, blk = 0; @@ -796,11 +797,11 @@ gen_assignment(codegen_scope *s, node *node, int sp, int val) node = node->cdr; switch ((intptr_t)type) { case NODE_GVAR: - idx = new_sym(s, (mrb_sym)node); + idx = new_sym(s, sym(node)); genop_peep(s, MKOP_ABx(OP_SETGLOBAL, sp, idx), val); break; case NODE_LVAR: - idx = lv_idx(s, (mrb_sym)node); + idx = lv_idx(s, sym(node)); if (idx > 0) { if (idx != sp) { genop_peep(s, MKOP_AB(OP_MOVE, idx, sp), val); @@ -812,7 +813,7 @@ gen_assignment(codegen_scope *s, node *node, int sp, int val) codegen_scope *up = s->prev; while (up) { - idx = lv_idx(up, (mrb_sym)node); + idx = lv_idx(up, sym(node)); if (idx > 0) { genop_peep(s, MKOP_ABC(OP_SETUPVAR, sp, idx, lv), val); break; @@ -824,19 +825,19 @@ gen_assignment(codegen_scope *s, node *node, int sp, int val) } break; case NODE_IVAR: - idx = new_sym(s, (mrb_sym)node); + idx = new_sym(s, sym(node)); genop_peep(s, MKOP_ABx(OP_SETIV, sp, idx), val); break; case NODE_CVAR: - idx = new_sym(s, (mrb_sym)node); + idx = new_sym(s, sym(node)); genop_peep(s, MKOP_ABx(OP_SETCV, sp, idx), val); break; case NODE_CONST: - idx = new_sym(s, (mrb_sym)node); + idx = new_sym(s, sym(node)); genop_peep(s, MKOP_ABx(OP_SETCONST, sp, idx), val); break; case NODE_COLON2: - idx = new_sym(s, (mrb_sym)node->cdr); + idx = new_sym(s, sym(node->cdr)); genop_peep(s, MKOP_AB(OP_MOVE, cursp(), sp), NOVAL); push(); codegen(s, node->car, VAL); @@ -846,7 +847,7 @@ gen_assignment(codegen_scope *s, node *node, int sp, int val) case NODE_CALL: push(); - gen_call(s, node, attrsym(s, (mrb_sym)node->cdr->car), sp, val); + gen_call(s, node, attrsym(s, sym(node->cdr->car)), sp, val); val = NOVAL; /* push should have done in gen_call() */ break; @@ -1247,7 +1248,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_COLON2: { - int sym = new_sym(s, (mrb_sym)tree->cdr); + int sym = new_sym(s, sym(tree->cdr)); codegen(s, tree->car, VAL); pop(); @@ -1258,7 +1259,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_COLON3: { - int sym = new_sym(s, (mrb_sym)tree); + int sym = new_sym(s, sym(tree)); genop(s, MKOP_A(OP_OCLASS, cursp())); genop(s, MKOP_ABx(OP_GETMCNST, cursp(), sym)); @@ -1378,7 +1379,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_OP_ASGN: { - mrb_sym sym = (mrb_sym)tree->cdr->car; + mrb_sym sym = sym(tree->cdr->car); int len; const char *name = mrb_sym2name_len(s->mrb, sym, &len); int idx; @@ -1592,7 +1593,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_LVAR: if (val) { - int idx = lv_idx(s, (mrb_sym)tree); + int idx = lv_idx(s, sym(tree)); if (idx > 0) { genop(s, MKOP_AB(OP_MOVE, cursp(), idx)); @@ -1602,7 +1603,7 @@ codegen(codegen_scope *s, node *tree, int val) codegen_scope *up = s->prev; while (up) { - idx = lv_idx(up, (mrb_sym)tree); + idx = lv_idx(up, sym(tree)); if (idx > 0) { genop(s, MKOP_ABC(OP_GETUPVAR, cursp(), idx, lv)); break; @@ -1617,7 +1618,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_GVAR: { - int sym = new_sym(s, (mrb_sym)tree); + int sym = new_sym(s, sym(tree)); genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym)); push(); @@ -1626,7 +1627,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_IVAR: { - int sym = new_sym(s, (mrb_sym)tree); + int sym = new_sym(s, sym(tree)); genop(s, MKOP_ABx(OP_GETIV, cursp(), sym)); push(); @@ -1635,7 +1636,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_CVAR: { - int sym = new_sym(s, (mrb_sym)tree); + int sym = new_sym(s, sym(tree)); genop(s, MKOP_ABx(OP_GETCV, cursp(), sym)); push(); @@ -1644,7 +1645,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_CONST: { - int sym = new_sym(s, (mrb_sym)tree); + int sym = new_sym(s, sym(tree)); genop(s, MKOP_ABx(OP_GETCONST, cursp(), sym)); push(); @@ -1828,7 +1829,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_SYM: if (val) { - int sym = new_sym(s, (mrb_sym)tree); + int sym = new_sym(s, sym(tree)); genop(s, MKOP_ABx(OP_LOADSYM, cursp(), sym)); push(); @@ -1874,8 +1875,8 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_ALIAS: { - int a = new_msym(s, (mrb_sym)tree->car); - int b = new_msym(s, (mrb_sym)tree->cdr); + int a = new_msym(s, sym(tree->car)); + int b = new_msym(s, sym(tree->cdr)); int c = new_msym(s, mrb_intern(s->mrb, "alias_method")); genop(s, MKOP_A(OP_TCLASS, cursp())); @@ -1895,7 +1896,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_UNDEF: { - int sym = new_msym(s, (mrb_sym)tree); + int sym = new_msym(s, sym(tree)); int undef = new_msym(s, mrb_intern(s->mrb, "undef_method")); genop(s, MKOP_A(OP_TCLASS, cursp())); @@ -1934,7 +1935,7 @@ codegen(codegen_scope *s, node *tree, int val) push(); } pop(); pop(); - idx = new_msym(s, (mrb_sym)tree->car->cdr); + idx = new_msym(s, sym(tree->car->cdr)); genop(s, MKOP_AB(OP_CLASS, cursp(), idx)); idx = scope_body(s, tree->cdr->cdr->car); genop(s, MKOP_ABx(OP_EXEC, cursp(), idx)); @@ -1960,7 +1961,7 @@ codegen(codegen_scope *s, node *tree, int val) codegen(s, tree->car->car, VAL); } pop(); - idx = new_msym(s, (mrb_sym)tree->car->cdr); + idx = new_msym(s, sym(tree->car->cdr)); genop(s, MKOP_AB(OP_MODULE, cursp(), idx)); idx = scope_body(s, tree->cdr->car); genop(s, MKOP_ABx(OP_EXEC, cursp(), idx)); @@ -1987,7 +1988,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_DEF: { - int sym = new_msym(s, (mrb_sym)tree->car); + int sym = new_msym(s, sym(tree->car)); int idx = lambda_body(s, tree->cdr, 0); genop(s, MKOP_A(OP_TCLASS, cursp())); @@ -2004,7 +2005,7 @@ codegen(codegen_scope *s, node *tree, int val) case NODE_SDEF: { node *recv = tree->car; - int sym = new_msym(s, (mrb_sym)tree->cdr->car); + int sym = new_msym(s, sym(tree->cdr->car)); int idx = lambda_body(s, tree->cdr->cdr, 0); codegen(s, recv, VAL); diff --git a/src/parse.y b/src/parse.y index d7d0ce73e..a7f645456 100644 --- a/src/parse.y +++ b/src/parse.y @@ -58,6 +58,9 @@ typedef unsigned int stack_type; #define CMDARG_LEXPOP() BITSTACK_LEXPOP(p->cmdarg_stack) #define CMDARG_P() BITSTACK_SET_P(p->cmdarg_stack) +#define sym(x) ((mrb_sym)(intptr_t)(x)) +#define nsym(x) ((node*)(intptr_t)(x)) + static mrb_sym intern_gen(parser_state *p, const char *s) { @@ -219,7 +222,7 @@ local_var_p(parser_state *p, mrb_sym sym) while (l) { node *n = l->car; while (n) { - if ((mrb_sym)n->car == sym) return 1; + if (sym(n->car) == sym) return 1; n = n->cdr; } l = l->cdr; @@ -230,7 +233,7 @@ local_var_p(parser_state *p, mrb_sym sym) static void local_add_f(parser_state *p, mrb_sym sym) { - p->locals->car = push(p->locals->car, (node*)sym); + p->locals->car = push(p->locals->car, nsym(sym)); } static void @@ -298,7 +301,7 @@ new_false(parser_state *p) static node* new_alias(parser_state *p, mrb_sym a, mrb_sym b) { - return cons((node*)NODE_ALIAS, cons((node*)a, (node*)b)); + return cons((node*)NODE_ALIAS, cons(nsym(a), nsym(b))); } // (:if cond then else) @@ -368,14 +371,14 @@ new_self(parser_state *p) static node* new_call(parser_state *p, node *a, mrb_sym b, node *c) { - return list4((node*)NODE_CALL, a, (node*)b, c); + return list4((node*)NODE_CALL, a, nsym(b), c); } // (:fcall self mid args) static node* new_fcall(parser_state *p, mrb_sym b, node *c) { - return list4((node*)NODE_FCALL, new_self(p), (node*)b, c); + return list4((node*)NODE_FCALL, new_self(p), nsym(b), c); } #if 0 @@ -467,14 +470,14 @@ new_dot3(parser_state *p, node *a, node *b) static node* new_colon2(parser_state *p, node *b, mrb_sym c) { - return cons((node*)NODE_COLON2, cons(b, (node*)c)); + return cons((node*)NODE_COLON2, cons(b, nsym(c))); } // (:colon3 . c) static node* new_colon3(parser_state *p, mrb_sym c) { - return cons((node*)NODE_COLON3, (node*)c); + return cons((node*)NODE_COLON3, nsym(c)); } // (:and a b) @@ -516,7 +519,7 @@ new_hash(parser_state *p, node *a) static node* new_sym(parser_state *p, mrb_sym sym) { - return cons((node*)NODE_SYM, (node*)sym); + return cons((node*)NODE_SYM, nsym(sym)); } static mrb_sym @@ -532,42 +535,42 @@ new_strsym(parser_state *p, node* str) static node* new_lvar(parser_state *p, mrb_sym sym) { - return cons((node*)NODE_LVAR, (node*)sym); + return cons((node*)NODE_LVAR, nsym(sym)); } // (:gvar . a) static node* new_gvar(parser_state *p, mrb_sym sym) { - return cons((node*)NODE_GVAR, (node*)sym); + return cons((node*)NODE_GVAR, nsym(sym)); } // (:ivar . a) static node* new_ivar(parser_state *p, mrb_sym sym) { - return cons((node*)NODE_IVAR, (node*)sym); + return cons((node*)NODE_IVAR, nsym(sym)); } // (:cvar . a) static node* new_cvar(parser_state *p, mrb_sym sym) { - return cons((node*)NODE_CVAR, (node*)sym); + return cons((node*)NODE_CVAR, nsym(sym)); } // (:const . a) static node* new_const(parser_state *p, mrb_sym sym) { - return cons((node*)NODE_CONST, (node*)sym); + return cons((node*)NODE_CONST, nsym(sym)); } // (:undef a...) static node* new_undef(parser_state *p, mrb_sym sym) { - return cons((node*)NODE_UNDEF, (node*)sym); + return cons((node*)NODE_UNDEF, nsym(sym)); } // (:class class super body) @@ -595,21 +598,21 @@ new_module(parser_state *p, node *m, node *b) static node* new_def(parser_state *p, mrb_sym m, node *a, node *b) { - return list5((node*)NODE_DEF, (node*)m, p->locals->car, a, b); + return list5((node*)NODE_DEF, nsym(m), p->locals->car, a, b); } // (:sdef obj m lv (arg . body)) static node* new_sdef(parser_state *p, node *o, mrb_sym m, node *a, node *b) { - return list6((node*)NODE_SDEF, o, (node*)m, p->locals->car, a, b); + return list6((node*)NODE_SDEF, o, nsym(m), p->locals->car, a, b); } // (:arg . sym) static node* new_arg(parser_state *p, mrb_sym sym) { - return cons((node*)NODE_ARG, (node*)sym); + return cons((node*)NODE_ARG, nsym(sym)); } // (m o r m2 b) @@ -623,8 +626,8 @@ new_args(parser_state *p, node *m, node *opt, mrb_sym rest, node *m2, mrb_sym bl { node *n; - n = cons(m2, (node*)blk); - n = cons((node*)rest, n); + n = cons(m2, nsym(blk)); + n = cons(nsym(rest), n); n = cons(opt, n); return cons(m, n); } @@ -668,7 +671,7 @@ new_masgn(parser_state *p, node *a, node *b) static node* new_op_asgn(parser_state *p, node *a, mrb_sym op, node *b) { - return list4((node*)NODE_OP_ASGN, a, (node*)op, b); + return list4((node*)NODE_OP_ASGN, a, nsym(op), b); } // (:int . i) @@ -807,7 +810,7 @@ static void assignable(parser_state *p, node *lhs) { if ((int)(intptr_t)lhs->car == NODE_LVAR) { - local_add(p, (mrb_sym)lhs->cdr); + local_add(p, sym(lhs->cdr)); } } @@ -817,8 +820,8 @@ var_reference(parser_state *p, node *lhs) node *n; if ((int)(intptr_t)lhs->car == NODE_LVAR) { - if (!local_var_p(p, (mrb_sym)lhs->cdr)) { - n = new_fcall(p, (mrb_sym)lhs->cdr, 0); + if (!local_var_p(p, sym(lhs->cdr))) { + n = new_fcall(p, sym(lhs->cdr), 0); cons_free(lhs); return n; } @@ -1451,15 +1454,15 @@ cname : tIDENTIFIER cpath : tCOLON3 cname { - $$ = cons((node*)1, (node*)$2); + $$ = cons((node*)1, nsym($2)); } | cname { - $$ = cons((node*)0, (node*)$1); + $$ = cons((node*)0, nsym($1)); } | primary_value tCOLON2 cname { - $$ = cons($1, (node*)$3); + $$ = cons($1, nsym($3)); } ; @@ -2768,14 +2771,14 @@ f_arg : f_arg_item f_opt : tIDENTIFIER '=' arg_value { local_add_f(p, $1); - $$ = cons((node*)$1, $3); + $$ = cons(nsym($1), $3); } ; f_block_opt : tIDENTIFIER '=' primary_value { local_add_f(p, $1); - $$ = cons((node*)$1, $3); + $$ = cons(nsym($1), $3); } ; @@ -4706,7 +4709,7 @@ parser_update_cxt(parser_state *p, mrbc_context *cxt) cxt->syms = (mrb_sym *)mrb_realloc(p->mrb, cxt->syms, i*sizeof(mrb_sym)); cxt->slen = i; for (i=0, n=n0; n; i++,n=n->cdr) { - cxt->syms[i] = (mrb_sym)n->car; + cxt->syms[i] = sym(n->car); } } @@ -5034,7 +5037,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) while (n2) { dump_prefix(offset+2); - printf("%s=", mrb_sym2name(mrb, (mrb_sym)n2->car->car)); + printf("%s=", mrb_sym2name(mrb, sym(n2->car->car))); parser_dump(mrb, n2->car->cdr, 0); n2 = n2->cdr; } @@ -5043,7 +5046,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) n = n->cdr; if (n->car) { dump_prefix(offset+1); - printf("rest=*%s\n", mrb_sym2name(mrb, (mrb_sym)n->car)); + printf("rest=*%s\n", mrb_sym2name(mrb, sym(n->car))); } n = n->cdr; if (n->car) { @@ -5054,7 +5057,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) n = n->cdr; if (n) { dump_prefix(offset+1); - printf("blk=&%s\n", mrb_sym2name(mrb, (mrb_sym)n)); + printf("blk=&%s\n", mrb_sym2name(mrb, sym(n))); } } dump_prefix(offset+1); @@ -5177,7 +5180,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) while (n2) { if (n2->car) { if (n2 != tree->car) printf(", "); - printf("%s", mrb_sym2name(mrb, (mrb_sym)n2->car)); + printf("%s", mrb_sym2name(mrb, sym(n2->car))); } n2 = n2->cdr; } @@ -5194,7 +5197,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) parser_dump(mrb, tree->car, offset+1); dump_prefix(offset+1); printf("method='%s' (%d)\n", - mrb_sym2name(mrb, (mrb_sym)tree->cdr->car), + mrb_sym2name(mrb, sym(tree->cdr->car)), (int)(intptr_t)tree->cdr->car); tree = tree->cdr->cdr->car; if (tree) { @@ -5225,13 +5228,13 @@ parser_dump(mrb_state *mrb, node *tree, int offset) printf("NODE_COLON2:\n"); parser_dump(mrb, tree->car, offset+1); dump_prefix(offset+1); - printf("::%s\n", mrb_sym2name(mrb, (mrb_sym)tree->cdr)); + printf("::%s\n", mrb_sym2name(mrb, sym(tree->cdr))); break; case NODE_COLON3: printf("NODE_COLON3:\n"); dump_prefix(offset+1); - printf("::%s\n", mrb_sym2name(mrb, (mrb_sym)tree)); + printf("::%s\n", mrb_sym2name(mrb, sym(tree))); break; case NODE_ARRAY: @@ -5314,7 +5317,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) parser_dump(mrb, tree->car, offset+2); tree = tree->cdr; dump_prefix(offset+1); - printf("op='%s' (%d)\n", mrb_sym2name(mrb, (mrb_sym)tree->car), (int)(intptr_t)tree->car); + printf("op='%s' (%d)\n", mrb_sym2name(mrb, sym(tree->car)), (int)(intptr_t)tree->car); tree = tree->cdr; parser_dump(mrb, tree->car, offset+1); break; @@ -5366,23 +5369,23 @@ parser_dump(mrb_state *mrb, node *tree, int offset) break; case NODE_LVAR: - printf("NODE_LVAR %s\n", mrb_sym2name(mrb, (mrb_sym)tree)); + printf("NODE_LVAR %s\n", mrb_sym2name(mrb, sym(tree))); break; case NODE_GVAR: - printf("NODE_GVAR %s\n", mrb_sym2name(mrb, (mrb_sym)tree)); + printf("NODE_GVAR %s\n", mrb_sym2name(mrb, sym(tree))); break; case NODE_IVAR: - printf("NODE_IVAR %s\n", mrb_sym2name(mrb, (mrb_sym)tree)); + printf("NODE_IVAR %s\n", mrb_sym2name(mrb, sym(tree))); break; case NODE_CVAR: - printf("NODE_CVAR %s\n", mrb_sym2name(mrb, (mrb_sym)tree)); + printf("NODE_CVAR %s\n", mrb_sym2name(mrb, sym(tree))); break; case NODE_CONST: - printf("NODE_CONST %s\n", mrb_sym2name(mrb, (mrb_sym)tree)); + printf("NODE_CONST %s\n", mrb_sym2name(mrb, sym(tree))); break; case NODE_BACK_REF: @@ -5394,7 +5397,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) break; case NODE_ARG: - printf("NODE_ARG %s\n", mrb_sym2name(mrb, (mrb_sym)tree)); + printf("NODE_ARG %s\n", mrb_sym2name(mrb, sym(tree))); break; case NODE_BLOCK_ARG: @@ -5425,7 +5428,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) break; case NODE_SYM: - printf("NODE_SYM :%s\n", mrb_sym2name(mrb, (mrb_sym)tree)); + printf("NODE_SYM :%s\n", mrb_sym2name(mrb, sym(tree))); break; case NODE_SELF: @@ -5446,29 +5449,29 @@ parser_dump(mrb_state *mrb, node *tree, int offset) case NODE_ALIAS: printf("NODE_ALIAS %s %s:\n", - mrb_sym2name(mrb, (mrb_sym)tree->car), - mrb_sym2name(mrb, (mrb_sym)tree->cdr)); + mrb_sym2name(mrb, sym(tree->car)), + mrb_sym2name(mrb, sym(tree->cdr))); break; case NODE_UNDEF: printf("NODE_UNDEF %s:\n", - mrb_sym2name(mrb, (mrb_sym)tree)); + mrb_sym2name(mrb, sym(tree))); break; case NODE_CLASS: printf("NODE_CLASS:\n"); if (tree->car->car == (node*)0) { dump_prefix(offset+1); - printf(":%s\n", mrb_sym2name(mrb, (mrb_sym)tree->car->cdr)); + printf(":%s\n", mrb_sym2name(mrb, sym(tree->car->cdr))); } else if (tree->car->car == (node*)1) { dump_prefix(offset+1); - printf("::%s\n", mrb_sym2name(mrb, (mrb_sym)tree->car->cdr)); + printf("::%s\n", mrb_sym2name(mrb, sym(tree->car->cdr))); } else { parser_dump(mrb, tree->car->car, offset+1); dump_prefix(offset+1); - printf("::%s\n", mrb_sym2name(mrb, (mrb_sym)tree->car->cdr)); + printf("::%s\n", mrb_sym2name(mrb, sym(tree->car->cdr))); } if (tree->cdr->car) { dump_prefix(offset+1); @@ -5484,16 +5487,16 @@ parser_dump(mrb_state *mrb, node *tree, int offset) printf("NODE_MODULE:\n"); if (tree->car->car == (node*)0) { dump_prefix(offset+1); - printf(":%s\n", mrb_sym2name(mrb, (mrb_sym)tree->car->cdr)); + printf(":%s\n", mrb_sym2name(mrb, sym(tree->car->cdr))); } else if (tree->car->car == (node*)1) { dump_prefix(offset+1); - printf("::%s\n", mrb_sym2name(mrb, (mrb_sym)tree->car->cdr)); + printf("::%s\n", mrb_sym2name(mrb, sym(tree->car->cdr))); } else { parser_dump(mrb, tree->car->car, offset+1); dump_prefix(offset+1); - printf("::%s\n", mrb_sym2name(mrb, (mrb_sym)tree->car->cdr)); + printf("::%s\n", mrb_sym2name(mrb, sym(tree->car->cdr))); } dump_prefix(offset+1); printf("body:\n"); @@ -5511,7 +5514,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) case NODE_DEF: printf("NODE_DEF:\n"); dump_prefix(offset+1); - printf("%s\n", mrb_sym2name(mrb, (mrb_sym)tree->car)); + printf("%s\n", mrb_sym2name(mrb, sym(tree->car))); tree = tree->cdr; { node *n2 = tree->car; @@ -5523,7 +5526,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) while (n2) { if (n2->car) { if (n2 != tree->car) printf(", "); - printf("%s", mrb_sym2name(mrb, (mrb_sym)n2->car)); + printf("%s", mrb_sym2name(mrb, sym(n2->car))); } n2 = n2->cdr; } @@ -5548,7 +5551,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) while (n2) { dump_prefix(offset+2); - printf("%s=", mrb_sym2name(mrb, (mrb_sym)n2->car->car)); + printf("%s=", mrb_sym2name(mrb, sym(n2->car->car))); parser_dump(mrb, n2->car->cdr, 0); n2 = n2->cdr; } @@ -5557,7 +5560,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) n = n->cdr; if (n->car) { dump_prefix(offset+1); - printf("rest=*%s\n", mrb_sym2name(mrb, (mrb_sym)n->car)); + printf("rest=*%s\n", mrb_sym2name(mrb, sym(n->car))); } n = n->cdr; if (n->car) { @@ -5568,7 +5571,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) n = n->cdr; if (n) { dump_prefix(offset+1); - printf("blk=&%s\n", mrb_sym2name(mrb, (mrb_sym)n)); + printf("blk=&%s\n", mrb_sym2name(mrb, sym(n))); } } parser_dump(mrb, tree->cdr->car, offset+1); @@ -5579,7 +5582,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) parser_dump(mrb, tree->car, offset+1); tree = tree->cdr; dump_prefix(offset+1); - printf(":%s\n", mrb_sym2name(mrb, (mrb_sym)tree->car)); + printf(":%s\n", mrb_sym2name(mrb, sym(tree->car))); tree = tree->cdr->cdr; if (tree->car) { node *n = tree->car; @@ -5598,7 +5601,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) while (n2) { dump_prefix(offset+2); - printf("%s=", mrb_sym2name(mrb, (mrb_sym)n2->car->car)); + printf("%s=", mrb_sym2name(mrb, sym(n2->car->car))); parser_dump(mrb, n2->car->cdr, 0); n2 = n2->cdr; } @@ -5607,7 +5610,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) n = n->cdr; if (n->car) { dump_prefix(offset+1); - printf("rest=*%s\n", mrb_sym2name(mrb, (mrb_sym)n->car)); + printf("rest=*%s\n", mrb_sym2name(mrb, sym(n->car))); } n = n->cdr; if (n->car) { @@ -5618,7 +5621,7 @@ parser_dump(mrb_state *mrb, node *tree, int offset) n = n->cdr; if (n) { dump_prefix(offset+1); - printf("blk=&%s\n", mrb_sym2name(mrb, (mrb_sym)n)); + printf("blk=&%s\n", mrb_sym2name(mrb, sym(n))); } } tree = tree->cdr; From f0f113ef0edcf7a396053308d6352ee2ded7a7f3 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Thu, 13 Sep 2012 22:28:04 +0900 Subject: [PATCH 02/10] should define String#+ (non inlined); close #469 --- src/string.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/string.c b/src/string.c index 7077e9dfd..57bbbc831 100644 --- a/src/string.c +++ b/src/string.c @@ -379,7 +379,10 @@ mrb_str_plus(mrb_state *mrb, mrb_value a, mrb_value b) static mrb_value mrb_str_plus_m(mrb_state *mrb, mrb_value self) { - return mrb_nil_value(); + mrb_value str; + + mrb_get_args(mrb, "S", &str); + return mrb_str_plus(mrb, self, str); } /* From e44c3fc8aac96ad40da7bef5de30c31689efc9c9 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Sat, 15 Sep 2012 14:14:25 +0900 Subject: [PATCH 03/10] Don't check ObjectID of Kernel#to_s. Only class check of instance --- test/t/kernel.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/t/kernel.rb b/test/t/kernel.rb index 1342f0254..fb0aee310 100644 --- a/test/t/kernel.rb +++ b/test/t/kernel.rb @@ -337,5 +337,5 @@ assert('Kernel#singleton_methods', '15.3.1.3.45') do end assert('Kernel#to_s', '15.3.1.3.46') do - to_s == '#' + to_s.class == String end From 4de0a2afbfc851047e20cef28e96d9e982f88096 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Sat, 15 Sep 2012 14:48:31 +0900 Subject: [PATCH 04/10] Numeric#{upto,downto}: limit may not be an integer --- mrblib/numeric.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb index 9f65d5633..c1b6b39c0 100644 --- a/mrblib/numeric.rb +++ b/mrblib/numeric.rb @@ -10,7 +10,6 @@ class Integer # # ISO 15.2.8.3.15 def downto(num, &block) - raise TypeError, "expected Integer" unless num.kind_of? Integer i = self while(i >= num) block.call(i) @@ -38,7 +37,6 @@ class Integer # # ISO 15.2.8.3.27 def upto(num, &block) - raise TypeError, "expected Integer" unless num.kind_of? Integer i = self while(i <= num) block.call(i) From ef17231bd776b266f1fe3132bde0fb6bbef90257 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Sat, 15 Sep 2012 15:04:51 +0900 Subject: [PATCH 05/10] Integer#step added --- mrblib/numeric.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb index c1b6b39c0..1d701b1fc 100644 --- a/mrblib/numeric.rb +++ b/mrblib/numeric.rb @@ -1,6 +1,6 @@ ## # Integer -# +# # ISO 15.2.8 class Integer @@ -44,6 +44,19 @@ class Integer end self end + + ## + # Calls the given block from +self+ to +num+ + # incremented by +step+ (default 1). + # + def step(num, step=1, &block) + i = if num.kind_of? Float then self.to_f else self end + while(i <= num) + block.call(i) + i += step + end + self + end end ## From b4647224b765380e5f40d0af7927355709bce346 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Sat, 15 Sep 2012 17:13:56 +0900 Subject: [PATCH 06/10] Add test case for Integer#step --- test/t/integer.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/t/integer.rb b/test/t/integer.rb index 872723445..56ea6bf13 100644 --- a/test/t/integer.rb +++ b/test/t/integer.rb @@ -172,3 +172,19 @@ assert('Integer#upto', '15.2.8.3.27') do end a == 6 end + +# Not ISO specified + +assert('Integer#step') do + a = [] + b = [] + 1.step(3) do |i| + a << i + end + 1.step(6, 2) do |i| + b << i + end + + a == [1, 2, 3] and + b == [1, 3, 5] +end From a55ffc78e8f2a0847cfb05b1e40dda4078500d10 Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Tue, 18 Sep 2012 13:14:54 +0900 Subject: [PATCH 07/10] Generic sh-elf target support. --- cmake/Toolchain-OSX-GenericShElf.cmake.sample | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 cmake/Toolchain-OSX-GenericShElf.cmake.sample diff --git a/cmake/Toolchain-OSX-GenericShElf.cmake.sample b/cmake/Toolchain-OSX-GenericShElf.cmake.sample new file mode 100644 index 000000000..1f847830a --- /dev/null +++ b/cmake/Toolchain-OSX-GenericShElf.cmake.sample @@ -0,0 +1,37 @@ +# +# Typical usage: +# 0) install cmake version 2.8-9 or higher. +# 1) install a PizzaFactory cross compiler +# a) darwin toolchain targeting sh-elf: http://sourceforge.jp/projects/pf3gnuchains/downloads/50061/sh-pizzafactory-elf.pkg/ +# b) install pkg. +# c) export PATH=$PATH:/pizza/bin +# 2) cp cmake/Toolchain-OSX-GenericShElf.cmake.sample ~/Toolchain-OSX-GenericShElf.cmake +# 3) tweak toolchain values as needed +# 4) cd build +# 5) cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-OSX-GenericShElf.cmake .. +# 6) Run mirb on gdb +# a) sh-pizzafactory-elf-gdb tools/mirb/mirb +# b) target sim +# c) load +# d) run + +# name of the target OS on which the built artifacts will run +# and the toolchain prefix +set(CMAKE_SYSTEM_NAME Generic) +set(TOOLCHAIN_PREFIX sh-pizzafactory-elf) + +# cross compilers to use for C and C++ +set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc) +set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++) + +# target environment(s) on the build host system +# set 1st to dir with the cross compiler's C/C++ headers/libs +# set 2nd to dir containing personal cross development headers/libs +set(CMAKE_FIND_ROOT_PATH /pizza/${TOOLCHAIN_PREFIX}) + +# modify default behavior of FIND_XXX() commands to +# search for headers/libs in the target environment and +# search for programs in the build host environment +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) From fcf38cc86bf9e7e41212888830fefa0d5f1fbe38 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Tue, 18 Sep 2012 21:36:00 +0900 Subject: [PATCH 08/10] Add a contribution information file --- doc/coding_conventions.md => CONTRIBUTING.md | 35 +++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) rename doc/coding_conventions.md => CONTRIBUTING.md (57%) diff --git a/doc/coding_conventions.md b/CONTRIBUTING.md similarity index 57% rename from doc/coding_conventions.md rename to CONTRIBUTING.md index aa47fe980..fdf47c29d 100644 --- a/doc/coding_conventions.md +++ b/CONTRIBUTING.md @@ -1,14 +1,33 @@ -# Coding conventions +# How to contribute + +mruby is a young open-source project. + +We are looking forward to each pull request. + +## Pull Request + +To make it easy to review und understand your change please keep the following +things in mind for your pull request: + +* Make you change on the latest possible state of +mruby/master+ +* Test your changes before creating a pull request (+make test+) +* If possible write a test case which confirms your change +* Don't mix several features or bug-fixes in one pull request +* Create a branch which is dedicated to change +* Create a meaningful commit message +* Explain your change (i.e. with a link to the issue you are fixing) + +## Coding conventions How to style your C and Ruby code which you want to submit. -## C code +### C code The core part (parser, bytecode-interpreter, core-lib, etc.) of mruby is written in the C programming language. Please note the following hints for your C code: -### Comply with C99 (ISO/IEC 9899:1999) +#### Comply with C99 (ISO/IEC 9899:1999) mruby should be highly portable to other systems and compilers. For that it is recommended to keep your code as close as possible to the C99 standard @@ -17,19 +36,19 @@ recommended to keep your code as close as possible to the C99 standard Although we target C99, VC is also an important target for mruby, so that we avoid local variable declaration in the middle. -### Reduce library dependencies to a minimum +#### Reduce library dependencies to a minimum The dependencies to libraries should be put to an absolute minimum. This increases the portability but makes it also easier to cut away parts of mruby on-demand. -### Don't use C++ style comments +#### Don't use C++ style comments /* This is the prefered comment style */ Use C++ style comments only for temporary comment e.g. commenting out some code lines. -### Insert a break after the method return value: +#### Insert a break after the method return value: int main(void) @@ -37,12 +56,12 @@ Use C++ style comments only for temporary comment e.g. commenting out some code ... } -## Ruby code +### Ruby code Parts of the standard library of mruby is written in the Ruby programming language itself. Please note the following hints for your Ruby code: -### Comply with the Ruby standard (ISO/IEC 30170:2012) +#### Comply with the Ruby standard (ISO/IEC 30170:2012) mruby is currently targeting to execute Ruby code which complies to ISO/IEC 30170:2012 (http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=59579). From 2493915a72fcc57148a17109f9be8525fc887790 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Tue, 18 Sep 2012 21:38:15 +0900 Subject: [PATCH 09/10] Emphasis commands --- CONTRIBUTING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fdf47c29d..199def696 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -9,8 +9,8 @@ We are looking forward to each pull request. To make it easy to review und understand your change please keep the following things in mind for your pull request: -* Make you change on the latest possible state of +mruby/master+ -* Test your changes before creating a pull request (+make test+) +* Make you change on the latest possible state of **mruby/master** +* Test your changes before creating a pull request (**make test**) * If possible write a test case which confirms your change * Don't mix several features or bug-fixes in one pull request * Create a branch which is dedicated to change From 5e16c2309f95d5b9becec799e23d244390a7d230 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Tue, 18 Sep 2012 22:20:05 +0900 Subject: [PATCH 10/10] re-formulate some sentences --- CONTRIBUTING.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 199def696..08995978f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,19 +1,17 @@ # How to contribute -mruby is a young open-source project. +mruby is an open-source project which is looking forward to each contribution. -We are looking forward to each pull request. +## Your Pull Request -## Pull Request +To make it easy to review and understand your change please keep the following +things in mind before submitting your pull request: -To make it easy to review und understand your change please keep the following -things in mind for your pull request: - -* Make you change on the latest possible state of **mruby/master** +* Work on the latest possible state of **mruby/master** * Test your changes before creating a pull request (**make test**) * If possible write a test case which confirms your change * Don't mix several features or bug-fixes in one pull request -* Create a branch which is dedicated to change +* Create a branch which is dedicated to your change * Create a meaningful commit message * Explain your change (i.e. with a link to the issue you are fixing)