Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 6 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler/core/codegen.c
r331 r439 9 9 #include <stdlib.h> 10 10 #include <string.h> 11 #include <math.h> 11 12 #include <mruby.h> 12 13 #include <mruby/compile.h> … … 24 25 #endif 25 26 27 #define MAXARG_S (1<<16) 28 26 29 typedef mrb_ast_node node; 27 30 typedef struct mrb_parser_state parser_state; … … 37 40 struct loopinfo { 38 41 enum looptype type; 39 int pc 1, pc2, pc3, acc;42 int pc0, pc1, pc2, pc3, acc; 40 43 int ensure_level; 41 44 struct loopinfo *prev; … … 51 54 node *lv; 52 55 53 int sp; 54 int pc; 55 int lastlabel; 56 uint16_t sp; 57 uint16_t pc; 58 uint16_t lastpc; 59 uint16_t lastlabel; 56 60 int ainfo:15; 57 61 mrb_bool mscope:1; … … 59 63 struct loopinfo *loop; 60 64 int ensure_level; 61 char const *filename;65 mrb_sym filename_sym; 62 66 uint16_t lineno; 63 67 64 68 mrb_code *iseq; 65 69 uint16_t *lines; 66 int icapa;70 uint32_t icapa; 67 71 68 72 mrb_irep *irep; 69 size_t pcapa; 70 size_t scapa; 71 size_t rcapa; 73 uint32_t pcapa, scapa, rcapa; 72 74 73 75 uint16_t nlocals; … … 101 103 codegen_scope *tmp = s->prev; 102 104 mrb_free(s->mrb, s->iseq); 105 mrb_free(s->mrb, s->lines); 103 106 mrb_pool_close(s->mpool); 104 107 s = tmp; 105 108 } 106 109 #ifndef MRB_DISABLE_STDIO 107 if (s->filename && s->lineno) { 108 fprintf(stderr, "codegen error:%s:%d: %s\n", s->filename, s->lineno, message); 110 if (s->filename_sym && s->lineno) { 111 const char *filename = mrb_sym_name_len(s->mrb, s->filename_sym, NULL); 112 fprintf(stderr, "codegen error:%s:%d: %s\n", filename, s->lineno, message); 109 113 } 110 114 else { … … 125 129 126 130 static void* 127 codegen_malloc(codegen_scope *s, size_t len)128 {129 void *p = mrb_malloc_simple(s->mrb, len);130 131 if (!p) codegen_error(s, "mrb_malloc");132 return p;133 }134 135 static void*136 131 codegen_realloc(codegen_scope *s, void *p, size_t len) 137 132 { … … 145 140 new_label(codegen_scope *s) 146 141 { 147 s->lastlabel = s->pc; 148 return s->pc; 149 } 150 151 static inline int 152 genop(codegen_scope *s, mrb_code i) 153 { 154 if (s->pc == s->icapa) { 142 return s->lastlabel = s->pc; 143 } 144 145 static void 146 emit_B(codegen_scope *s, uint32_t pc, uint8_t i) 147 { 148 if (pc >= MAXARG_S || s->icapa >= MAXARG_S) { 149 codegen_error(s, "too big code block"); 150 } 151 if (pc >= s->icapa) { 155 152 s->icapa *= 2; 153 if (s->icapa > MAXARG_S) { 154 s->icapa = MAXARG_S; 155 } 156 156 s->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->icapa); 157 157 if (s->lines) { 158 s->lines = (uint16_t*)codegen_realloc(s, s->lines, sizeof(short)*s->icapa); 159 s->irep->lines = s->lines; 160 } 161 } 162 s->iseq[s->pc] = i; 158 s->lines = (uint16_t*)codegen_realloc(s, s->lines, sizeof(uint16_t)*s->icapa); 159 } 160 } 163 161 if (s->lines) { 164 s->lines[s->pc] = s->lineno; 165 } 166 return s->pc++; 162 if (s->lineno > 0 || pc == 0) 163 s->lines[pc] = s->lineno; 164 else 165 s->lines[pc] = s->lines[pc-1]; 166 } 167 s->iseq[pc] = i; 168 } 169 170 static void 171 emit_S(codegen_scope *s, int pc, uint16_t i) 172 { 173 uint8_t hi = i>>8; 174 uint8_t lo = i&0xff; 175 176 emit_B(s, pc, hi); 177 emit_B(s, pc+1, lo); 178 } 179 180 static void 181 gen_B(codegen_scope *s, uint8_t i) 182 { 183 emit_B(s, s->pc, i); 184 s->pc++; 185 } 186 187 static void 188 gen_S(codegen_scope *s, uint16_t i) 189 { 190 emit_S(s, s->pc, i); 191 s->pc += 2; 192 } 193 194 static void 195 genop_0(codegen_scope *s, mrb_code i) 196 { 197 s->lastpc = s->pc; 198 gen_B(s, i); 199 } 200 201 static void 202 genop_1(codegen_scope *s, mrb_code i, uint16_t a) 203 { 204 s->lastpc = s->pc; 205 if (a > 0xff) { 206 gen_B(s, OP_EXT1); 207 gen_B(s, i); 208 gen_S(s, a); 209 } 210 else { 211 gen_B(s, i); 212 gen_B(s, (uint8_t)a); 213 } 214 } 215 216 static void 217 genop_2(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b) 218 { 219 s->lastpc = s->pc; 220 if (a > 0xff && b > 0xff) { 221 gen_B(s, OP_EXT3); 222 gen_B(s, i); 223 gen_S(s, a); 224 gen_S(s, b); 225 } 226 else if (b > 0xff) { 227 gen_B(s, OP_EXT2); 228 gen_B(s, i); 229 gen_B(s, (uint8_t)a); 230 gen_S(s, b); 231 } 232 else if (a > 0xff) { 233 gen_B(s, OP_EXT1); 234 gen_B(s, i); 235 gen_S(s, a); 236 gen_B(s, (uint8_t)b); 237 } 238 else { 239 gen_B(s, i); 240 gen_B(s, (uint8_t)a); 241 gen_B(s, (uint8_t)b); 242 } 243 } 244 245 static void 246 genop_3(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b, uint8_t c) 247 { 248 genop_2(s, i, a, b); 249 gen_B(s, c); 250 } 251 252 static void 253 genop_2S(codegen_scope *s, mrb_code i, uint16_t a, uint16_t b) 254 { 255 genop_1(s, i, a); 256 gen_S(s, b); 257 } 258 259 static void 260 genop_W(codegen_scope *s, mrb_code i, uint32_t a) 261 { 262 uint8_t a1 = (a>>16) & 0xff; 263 uint8_t a2 = (a>>8) & 0xff; 264 uint8_t a3 = a & 0xff; 265 266 s->lastpc = s->pc; 267 gen_B(s, i); 268 gen_B(s, a1); 269 gen_B(s, a2); 270 gen_B(s, a3); 167 271 } 168 272 … … 178 282 } 179 283 180 static int 181 genop_peep(codegen_scope *s, mrb_code i, int val) 182 { 183 /* peephole optimization */ 184 if (!no_optimize(s) && s->lastlabel != s->pc && s->pc > 0) { 185 mrb_code i0 = s->iseq[s->pc-1]; 186 int c1 = GET_OPCODE(i); 187 int c0 = GET_OPCODE(i0); 188 189 switch (c1) { 284 static 285 mrb_bool 286 on_eval(codegen_scope *s) 287 { 288 if (s && s->parser && s->parser->on_eval) 289 return TRUE; 290 return FALSE; 291 } 292 293 struct mrb_insn_data 294 mrb_decode_insn(const mrb_code *pc) 295 { 296 struct mrb_insn_data data = { 0 }; 297 mrb_code insn = READ_B(); 298 uint16_t a = 0; 299 uint16_t b = 0; 300 uint8_t c = 0; 301 302 switch (insn) { 303 #define FETCH_Z() /* empty */ 304 #define OPCODE(i,x) case OP_ ## i: FETCH_ ## x (); break; 305 #include "mruby/ops.h" 306 #undef OPCODE 307 } 308 switch (insn) { 309 case OP_EXT1: 310 insn = READ_B(); 311 switch (insn) { 312 #define OPCODE(i,x) case OP_ ## i: FETCH_ ## x ## _1 (); break; 313 #include "mruby/ops.h" 314 #undef OPCODE 315 } 316 break; 317 case OP_EXT2: 318 insn = READ_B(); 319 switch (insn) { 320 #define OPCODE(i,x) case OP_ ## i: FETCH_ ## x ## _2 (); break; 321 #include "mruby/ops.h" 322 #undef OPCODE 323 } 324 break; 325 case OP_EXT3: 326 insn = READ_B(); 327 switch (insn) { 328 #define OPCODE(i,x) case OP_ ## i: FETCH_ ## x ## _3 (); break; 329 #include "mruby/ops.h" 330 #undef OPCODE 331 } 332 break; 333 default: 334 break; 335 } 336 data.insn = insn; 337 data.a = a; 338 data.b = b; 339 data.c = c; 340 return data; 341 } 342 343 static struct mrb_insn_data 344 mrb_last_insn(codegen_scope *s) 345 { 346 if (s->pc == s->lastpc) { 347 struct mrb_insn_data data; 348 349 data.insn = OP_NOP; 350 return data; 351 } 352 return mrb_decode_insn(&s->iseq[s->lastpc]); 353 } 354 355 static mrb_bool 356 no_peephole(codegen_scope *s) 357 { 358 return no_optimize(s) || s->lastlabel == s->pc || s->pc == 0 || s->pc == s->lastpc; 359 } 360 361 static uint16_t 362 genjmp(codegen_scope *s, mrb_code i, uint16_t pc) 363 { 364 uint16_t pos; 365 366 s->lastpc = s->pc; 367 gen_B(s, i); 368 pos = s->pc; 369 gen_S(s, pc); 370 return pos; 371 } 372 373 static uint16_t 374 genjmp2(codegen_scope *s, mrb_code i, uint16_t a, int pc, int val) 375 { 376 uint16_t pos; 377 378 if (!no_peephole(s) && !val) { 379 struct mrb_insn_data data = mrb_last_insn(s); 380 381 if (data.insn == OP_MOVE && data.a == a) { 382 s->pc = s->lastpc; 383 a = data.b; 384 } 385 } 386 387 s->lastpc = s->pc; 388 if (a > 0xff) { 389 gen_B(s, OP_EXT1); 390 gen_B(s, i); 391 gen_S(s, a); 392 pos = s->pc; 393 gen_S(s, pc); 394 } 395 else { 396 gen_B(s, i); 397 gen_B(s, (uint8_t)a); 398 pos = s->pc; 399 gen_S(s, pc); 400 } 401 return pos; 402 } 403 404 static void 405 gen_move(codegen_scope *s, uint16_t dst, uint16_t src, int nopeep) 406 { 407 if (no_peephole(s)) { 408 normal: 409 genop_2(s, OP_MOVE, dst, src); 410 if (on_eval(s)) { 411 genop_0(s, OP_NOP); 412 } 413 return; 414 } 415 else { 416 struct mrb_insn_data data = mrb_last_insn(s); 417 418 switch (data.insn) { 190 419 case OP_MOVE: 191 if (GETARG_A(i) == GETARG_B(i)) { 192 /* skip useless OP_MOVE */ 193 return 0; 194 } 195 if (val) break; 196 switch (c0) { 197 case OP_MOVE: 198 if (GETARG_A(i) == GETARG_A(i0)) { 199 /* skip overriden OP_MOVE */ 200 s->pc--; 201 s->iseq[s->pc] = i; 202 } 203 if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i) == GETARG_B(i0)) { 204 /* skip swapping OP_MOVE */ 205 return 0; 206 } 207 if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) { 208 s->pc--; 209 return genop_peep(s, MKOP_AB(OP_MOVE, GETARG_A(i), GETARG_B(i0)), val); 210 } 211 break; 212 case OP_LOADI: 213 if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) { 214 s->iseq[s->pc-1] = MKOP_AsBx(OP_LOADI, GETARG_A(i), GETARG_sBx(i0)); 215 return 0; 216 } 217 break; 218 case OP_ARRAY: 219 case OP_HASH: 220 case OP_RANGE: 221 case OP_AREF: 222 case OP_GETUPVAR: 223 if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) { 224 s->iseq[s->pc-1] = MKOP_ABC(c0, GETARG_A(i), GETARG_B(i0), GETARG_C(i0)); 225 return 0; 226 } 227 break; 228 case OP_LOADSYM: 229 case OP_GETGLOBAL: 230 case OP_GETIV: 231 case OP_GETCV: 232 case OP_GETCONST: 233 case OP_GETSPECIAL: 234 case OP_LOADL: 235 case OP_STRING: 236 if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) { 237 s->iseq[s->pc-1] = MKOP_ABx(c0, GETARG_A(i), GETARG_Bx(i0)); 238 return 0; 239 } 240 break; 241 case OP_SCLASS: 242 if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) { 243 s->iseq[s->pc-1] = MKOP_AB(c0, GETARG_A(i), GETARG_B(i0)); 244 return 0; 245 } 246 break; 247 case OP_LOADNIL: 248 case OP_LOADSELF: 249 case OP_LOADT: 250 case OP_LOADF: 251 case OP_OCLASS: 252 if (GETARG_B(i) == GETARG_A(i0) && GETARG_A(i0) >= s->nlocals) { 253 s->iseq[s->pc-1] = MKOP_A(c0, GETARG_A(i)); 254 return 0; 255 } 256 break; 257 default: 258 break; 259 } 420 if (dst == src) return; /* remove useless MOVE */ 421 if (data.b == dst && data.a == src) /* skip swapping MOVE */ 422 return; 423 goto normal; 424 case OP_LOADNIL: case OP_LOADSELF: case OP_LOADT: case OP_LOADF: 425 case OP_LOADI__1: 426 case OP_LOADI_0: case OP_LOADI_1: case OP_LOADI_2: case OP_LOADI_3: 427 case OP_LOADI_4: case OP_LOADI_5: case OP_LOADI_6: case OP_LOADI_7: 428 if (nopeep || data.a != src || data.a < s->nlocals) goto normal; 429 s->pc = s->lastpc; 430 genop_1(s, data.insn, dst); 260 431 break; 261 case OP_SETIV: 262 case OP_SETCV: 263 case OP_SETCONST: 264 case OP_SETMCNST: 265 case OP_SETGLOBAL: 266 if (val) break; 267 if (c0 == OP_MOVE) { 268 if (GETARG_A(i) == GETARG_A(i0)) { 269 s->iseq[s->pc-1] = MKOP_ABx(c1, GETARG_B(i0), GETARG_Bx(i)); 270 return 0; 271 } 272 } 273 break; 274 case OP_SETUPVAR: 275 if (val) break; 276 if (c0 == OP_MOVE) { 277 if (GETARG_A(i) == GETARG_A(i0)) { 278 s->iseq[s->pc-1] = MKOP_ABC(c1, GETARG_B(i0), GETARG_B(i), GETARG_C(i)); 279 return 0; 280 } 281 } 282 break; 283 case OP_EPOP: 284 if (c0 == OP_EPOP) { 285 s->iseq[s->pc-1] = MKOP_A(OP_EPOP, GETARG_A(i0)+GETARG_A(i)); 286 return 0; 287 } 288 break; 289 case OP_POPERR: 290 if (c0 == OP_POPERR) { 291 s->iseq[s->pc-1] = MKOP_A(OP_POPERR, GETARG_A(i0)+GETARG_A(i)); 292 return 0; 293 } 294 break; 295 case OP_RETURN: 296 switch (c0) { 297 case OP_RETURN: 298 return 0; 299 case OP_MOVE: 300 if (GETARG_A(i0) >= s->nlocals) { 301 s->iseq[s->pc-1] = MKOP_AB(OP_RETURN, GETARG_B(i0), OP_R_NORMAL); 302 return 0; 303 } 304 break; 305 case OP_SETIV: 306 case OP_SETCV: 307 case OP_SETCONST: 308 case OP_SETMCNST: 309 case OP_SETUPVAR: 310 case OP_SETGLOBAL: 311 s->pc--; 312 genop_peep(s, i0, NOVAL); 313 i0 = s->iseq[s->pc-1]; 314 return genop(s, MKOP_AB(OP_RETURN, GETARG_A(i0), OP_R_NORMAL)); 315 #if 0 316 case OP_SEND: 317 if (GETARG_B(i) == OP_R_NORMAL && GETARG_A(i) == GETARG_A(i0)) { 318 s->iseq[s->pc-1] = MKOP_ABC(OP_TAILCALL, GETARG_A(i0), GETARG_B(i0), GETARG_C(i0)); 319 return; 320 } 321 break; 322 #endif 323 default: 324 break; 325 } 326 break; 327 case OP_ADD: 328 case OP_SUB: 329 if (c0 == OP_LOADI) { 330 int c = GETARG_sBx(i0); 331 332 if (c1 == OP_SUB) c = -c; 333 if (c > 127 || c < -127) break; 334 if (0 <= c) 335 s->iseq[s->pc-1] = MKOP_ABC(OP_ADDI, GETARG_A(i), GETARG_B(i), c); 336 else 337 s->iseq[s->pc-1] = MKOP_ABC(OP_SUBI, GETARG_A(i), GETARG_B(i), -c); 338 return 0; 339 } 340 case OP_STRCAT: 341 if (c0 == OP_STRING) { 342 mrb_value v = s->irep->pool[GETARG_Bx(i0)]; 343 344 if (mrb_string_p(v) && RSTRING_LEN(v) == 0) { 345 s->pc--; 346 return 0; 347 } 348 } 349 if (c0 == OP_LOADNIL) { 350 if (GETARG_B(i) == GETARG_A(i0)) { 351 s->pc--; 352 return 0; 353 } 354 } 355 break; 356 case OP_JMPIF: 357 case OP_JMPNOT: 358 if (c0 == OP_MOVE && GETARG_A(i) == GETARG_A(i0)) { 359 s->iseq[s->pc-1] = MKOP_AsBx(c1, GETARG_B(i0), GETARG_sBx(i)); 360 return s->pc-1; 361 } 432 case OP_LOADI: case OP_LOADINEG: case OP_LOADL: case OP_LOADSYM: 433 case OP_GETGV: case OP_GETSV: case OP_GETIV: case OP_GETCV: 434 case OP_GETCONST: case OP_STRING: 435 case OP_LAMBDA: case OP_BLOCK: case OP_METHOD: case OP_BLKPUSH: 436 if (nopeep || data.a != src || data.a < s->nlocals) goto normal; 437 s->pc = s->lastpc; 438 genop_2(s, data.insn, dst, data.b); 362 439 break; 363 440 default: 441 goto normal; 442 } 443 } 444 } 445 446 static void 447 gen_return(codegen_scope *s, uint8_t op, uint16_t src) 448 { 449 if (no_peephole(s)) { 450 genop_1(s, op, src); 451 } 452 else { 453 struct mrb_insn_data data = mrb_last_insn(s); 454 455 if (data.insn == OP_MOVE && src == data.a) { 456 s->pc = s->lastpc; 457 genop_1(s, op, data.b); 458 } 459 else if (data.insn != OP_RETURN) { 460 genop_1(s, op, src); 461 } 462 } 463 } 464 465 static void 466 gen_addsub(codegen_scope *s, uint8_t op, uint16_t dst) 467 { 468 if (no_peephole(s)) { 469 normal: 470 genop_1(s, op, dst); 471 return; 472 } 473 else { 474 struct mrb_insn_data data = mrb_last_insn(s); 475 476 switch (data.insn) { 477 case OP_LOADI__1: 478 if (op == OP_ADD) op = OP_SUB; 479 else op = OP_ADD; 480 data.b = 1; 481 goto replace; 482 case OP_LOADI_0: case OP_LOADI_1: case OP_LOADI_2: case OP_LOADI_3: 483 case OP_LOADI_4: case OP_LOADI_5: case OP_LOADI_6: case OP_LOADI_7: 484 data.b = data.insn - OP_LOADI_0; 485 /* fall through */ 486 case OP_LOADI: 487 replace: 488 if (data.b >= 128) goto normal; 489 s->pc = s->lastpc; 490 if (op == OP_ADD) { 491 genop_2(s, OP_ADDI, dst, (uint8_t)data.b); 492 } 493 else { 494 genop_2(s, OP_SUBI, dst, (uint8_t)data.b); 495 } 364 496 break; 365 } 366 } 367 return genop(s, i); 497 default: 498 goto normal; 499 } 500 } 501 } 502 503 static int 504 dispatch(codegen_scope *s, uint16_t pos0) 505 { 506 uint16_t newpos; 507 508 s->lastlabel = s->pc; 509 newpos = PEEK_S(s->iseq+pos0); 510 emit_S(s, pos0, s->pc); 511 return newpos; 368 512 } 369 513 370 514 static void 371 scope_error(codegen_scope *s) 372 { 373 exit(EXIT_FAILURE); 374 } 375 376 static inline void 377 dispatch(codegen_scope *s, int pc) 378 { 379 int diff = s->pc - pc; 380 mrb_code i = s->iseq[pc]; 381 int c = GET_OPCODE(i); 382 383 s->lastlabel = s->pc; 384 switch (c) { 385 case OP_JMP: 386 case OP_JMPIF: 387 case OP_JMPNOT: 388 case OP_ONERR: 389 break; 390 default: 391 #ifndef MRB_DISABLE_STDIO 392 fprintf(stderr, "bug: dispatch on non JMP op\n"); 393 #endif 394 scope_error(s); 395 break; 396 } 397 if (diff > MAXARG_sBx) { 398 codegen_error(s, "too distant jump address"); 399 } 400 s->iseq[pc] = MKOP_AsBx(c, GETARG_A(i), diff); 401 } 402 403 static void 404 dispatch_linked(codegen_scope *s, int pc) 405 { 406 mrb_code i; 407 int pos; 408 409 if (!pc) return; 515 dispatch_linked(codegen_scope *s, uint16_t pos) 516 { 517 if (pos==0) return; 410 518 for (;;) { 411 i = s->iseq[pc]; 412 pos = GETARG_sBx(i); 413 dispatch(s, pc); 414 if (!pos) break; 415 pc = pos; 519 pos = dispatch(s, pos); 520 if (pos==0) break; 416 521 } 417 522 } … … 419 524 #define nregs_update do {if (s->sp > s->nregs) s->nregs = s->sp;} while (0) 420 525 static void 421 push_(codegen_scope *s) 422 { 423 if (s->sp > 511) { 424 codegen_error(s, "too complex expression"); 425 } 426 s->sp++; 427 nregs_update; 428 } 429 430 static void 431 push_n_(codegen_scope *s, size_t n) 432 { 433 if (s->sp+n > 511) { 526 push_n_(codegen_scope *s, int n) 527 { 528 if (s->sp+n >= 0xffff) { 434 529 codegen_error(s, "too complex expression"); 435 530 } … … 438 533 } 439 534 440 #define push() push_(s) 535 static void 536 pop_n_(codegen_scope *s, int n) 537 { 538 if ((int)s->sp-n < 0) { 539 codegen_error(s, "stack pointer underflow"); 540 } 541 s->sp-=n; 542 } 543 544 #define push() push_n_(s,1) 441 545 #define push_n(n) push_n_(s,n) 442 #define pop_(s) ((s)->sp--) 443 #define pop() pop_(s) 444 #define pop_n(n) (s->sp-=(n)) 546 #define pop() pop_n_(s,1) 547 #define pop_n(n) pop_n_(s,n) 445 548 #define cursp() (s->sp) 446 549 … … 448 551 new_lit(codegen_scope *s, mrb_value val) 449 552 { 450 size_t i;553 int i; 451 554 mrb_value *pv; 452 555 … … 457 560 pv = &s->irep->pool[i]; 458 561 459 if ( mrb_type(*pv) != MRB_TT_STRING) continue;562 if (!mrb_string_p(*pv)) continue; 460 563 if ((len = RSTRING_LEN(*pv)) != RSTRING_LEN(val)) continue; 461 564 if (memcmp(RSTRING_PTR(*pv), RSTRING_PTR(val), len) == 0) … … 463 566 } 464 567 break; 568 #ifndef MRB_WITHOUT_FLOAT 465 569 case MRB_TT_FLOAT: 466 570 for (i=0; i<s->irep->plen; i++) { 571 mrb_float f1, f2; 467 572 pv = &s->irep->pool[i]; 468 if (mrb_type(*pv) != MRB_TT_FLOAT) continue; 469 if (mrb_float(*pv) == mrb_float(val)) return i; 470 } 471 break; 573 if (!mrb_float_p(*pv)) continue; 574 f1 = mrb_float(*pv); 575 f2 = mrb_float(val); 576 if (f1 == f2 && !signbit(f1) == !signbit(f2)) return i; 577 } 578 break; 579 #endif 472 580 case MRB_TT_FIXNUM: 473 581 for (i=0; i<s->irep->plen; i++) { … … 492 600 switch (mrb_type(val)) { 493 601 case MRB_TT_STRING: 494 *pv = mrb_str_pool(s->mrb, val); 495 break; 496 602 *pv = mrb_str_pool(s->mrb, RSTRING_PTR(val), RSTRING_LEN(val), RSTR_NOFREE_P(RSTRING(val))); 603 break; 604 605 #ifndef MRB_WITHOUT_FLOAT 497 606 case MRB_TT_FLOAT: 498 607 #ifdef MRB_WORD_BOXING … … 500 609 break; 501 610 #endif 611 #endif 502 612 case MRB_TT_FIXNUM: 503 613 *pv = val; … … 511 621 } 512 622 513 /* method symbols should be fit in 9 bits */514 #define MAXMSYMLEN 512515 623 /* maximum symbol numbers */ 516 #define MAXSYMLEN 65536624 #define MAXSYMLEN 0x10000 517 625 518 626 static int 519 new_ msym(codegen_scope *s, mrb_sym sym)520 { 521 size_t i, len;627 new_sym(codegen_scope *s, mrb_sym sym) 628 { 629 int i, len; 522 630 523 631 mrb_assert(s->irep); 524 632 525 633 len = s->irep->slen; 526 if (len > MAXMSYMLEN) len = MAXMSYMLEN;527 634 for (i=0; i<len; i++) { 528 635 if (s->irep->syms[i] == sym) return i; 529 if (s->irep->syms[i] == 0) break; 530 } 531 if (i == MAXMSYMLEN) { 532 codegen_error(s, "too many symbols (max " MRB_STRINGIZE(MAXMSYMLEN) ")"); 533 } 534 s->irep->syms[i] = sym; 535 if (i == s->irep->slen) s->irep->slen++; 536 return i; 537 } 538 539 static int 540 new_sym(codegen_scope *s, mrb_sym sym) 541 { 542 size_t i; 543 544 for (i=0; i<s->irep->slen; i++) { 545 if (s->irep->syms[i] == sym) return i; 546 } 547 if (s->irep->slen == MAXSYMLEN) { 548 codegen_error(s, "too many symbols (max " MRB_STRINGIZE(MAXSYMLEN) ")"); 549 } 550 551 if (s->irep->slen > MAXMSYMLEN/2 && s->scapa == MAXMSYMLEN) { 552 s->scapa = MAXSYMLEN; 553 s->irep->syms = (mrb_sym *)codegen_realloc(s, s->irep->syms, sizeof(mrb_sym)*MAXSYMLEN); 554 for (i = s->irep->slen; i < MAXMSYMLEN; i++) { 555 static const mrb_sym mrb_sym_zero = { 0 }; 556 s->irep->syms[i] = mrb_sym_zero; 557 } 558 s->irep->slen = MAXMSYMLEN; 636 } 637 if (s->irep->slen >= s->scapa) { 638 s->scapa *= 2; 639 s->irep->syms = (mrb_sym*)codegen_realloc(s, s->irep->syms, sizeof(mrb_sym)*s->scapa); 559 640 } 560 641 s->irep->syms[s->irep->slen] = sym; … … 574 655 } 575 656 576 #define sym(x) ((mrb_sym)(intptr_t)(x)) 577 #define lv_name(lv) sym((lv)->car) 657 #define nint(x) ((int)(intptr_t)(x)) 658 #define nchar(x) ((char)(intptr_t)(x)) 659 #define nsym(x) ((mrb_sym)(intptr_t)(x)) 660 661 #define lv_name(lv) nsym((lv)->car) 662 578 663 static int 579 664 lv_idx(codegen_scope *s, mrb_sym id) … … 597 682 struct loopinfo *lp; 598 683 node *n2; 599 mrb_code c;600 684 601 685 /* generate receiver */ … … 603 687 /* generate loop-block */ 604 688 s = scope_new(s->mrb, s, NULL); 605 if (s == NULL) {606 raise_error(prev, "unexpected scope");607 }608 689 609 690 push(); /* push for a block parameter */ … … 611 692 /* generate loop variable */ 612 693 n2 = tree->car; 613 genop (s, MKOP_Ax(OP_ENTER, 0x40000));694 genop_W(s, OP_ENTER, 0x40000); 614 695 if (n2->car && !n2->car->cdr && !n2->cdr) { 615 696 gen_assignment(s, n2->car->car, 1, NOVAL); … … 625 706 codegen(s, tree->cdr->cdr->car, VAL); 626 707 pop(); 627 if (s->pc > 0) { 628 c = s->iseq[s->pc-1]; 629 if (GET_OPCODE(c) != OP_RETURN || GETARG_B(c) != OP_R_NORMAL || s->pc == s->lastlabel) 630 genop_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL); 631 } 708 gen_return(s, OP_RETURN, cursp()); 632 709 loop_pop(s, NOVAL); 633 710 scope_finish(s); 634 711 s = prev; 635 genop(s, MKOP_Abc(OP_LAMBDA, cursp(), s->irep->rlen-1, OP_L_BLOCK)); 712 genop_2(s, OP_BLOCK, cursp(), s->irep->rlen-1); 713 push();pop(); /* space for a block */ 636 714 pop(); 637 idx = new_ msym(s, mrb_intern_lit(s->mrb, "each"));638 genop (s, MKOP_ABC(OP_SENDB, cursp(), idx, 0));715 idx = new_sym(s, mrb_intern_lit(s->mrb, "each")); 716 genop_3(s, OP_SENDB, cursp(), idx, 0); 639 717 } 640 718 … … 642 720 lambda_body(codegen_scope *s, node *tree, int blk) 643 721 { 644 mrb_code c;645 722 codegen_scope *parent = s; 646 723 s = scope_new(s->mrb, s, tree->car); 647 if (s == NULL) {648 raise_error(parent, "unexpected scope");649 }650 724 651 725 s->mscope = !blk; … … 653 727 if (blk) { 654 728 struct loopinfo *lp = loop_push(s, LOOP_BLOCK); 655 lp->pc 1= new_label(s);729 lp->pc0 = new_label(s); 656 730 } 657 731 tree = tree->cdr; 658 if (tree->car) { 732 if (tree->car == NULL) { 733 genop_W(s, OP_ENTER, 0); 734 } 735 else { 659 736 mrb_aspec a; 660 737 int ma, oa, ra, pa, ka, kd, ba; 661 738 int pos, i; 662 node *n, *opt; 663 739 node *opt; 740 node *margs, *pargs; 741 node *tail; 742 743 /* mandatory arguments */ 664 744 ma = node_len(tree->car->car); 665 n= tree->car->car;666 while (n) {667 n = n->cdr; 668 }745 margs = tree->car->car; 746 tail = tree->car->cdr->cdr->cdr->cdr; 747 748 /* optional arguments */ 669 749 oa = node_len(tree->car->cdr->car); 750 /* rest argument? */ 670 751 ra = tree->car->cdr->cdr->car ? 1 : 0; 752 /* mandatory arugments after rest argument */ 671 753 pa = node_len(tree->car->cdr->cdr->cdr->car); 672 ka = kd = 0; 673 ba = tree->car->cdr->cdr->cdr->cdr ? 1 : 0; 754 pargs = tree->car->cdr->cdr->cdr->car; 755 /* keyword arguments */ 756 ka = tail? node_len(tail->cdr->car) : 0; 757 /* keyword dictionary? */ 758 kd = tail && tail->cdr->cdr->car? 1 : 0; 759 /* block argument? */ 760 ba = tail && tail->cdr->cdr->cdr->car ? 1 : 0; 674 761 675 762 if (ma > 0x1f || oa > 0x1f || pa > 0x1f || ka > 0x1f) { 676 763 codegen_error(s, "too many formal arguments"); 677 764 } 678 a = ((mrb_aspec)(ma & 0x1f) << 18) 679 | ((mrb_aspec)(oa & 0x1f) << 13) 680 | ((ra & 1) << 12) 681 | ((pa & 0x1f) << 7) 682 | ((ka & 0x1f) << 2) 683 | ((kd & 1)<< 1) 684 | (ba & 1); 685 s->ainfo = (((ma+oa) & 0x3f) << 6) /* (12bits = 6:1:5) */ 686 | ((ra & 1) << 5) 687 | (pa & 0x1f); 688 genop(s, MKOP_Ax(OP_ENTER, a)); 765 a = MRB_ARGS_REQ(ma) 766 | MRB_ARGS_OPT(oa) 767 | (ra? MRB_ARGS_REST() : 0) 768 | MRB_ARGS_POST(pa) 769 | MRB_ARGS_KEY(ka, kd) 770 | (ba? MRB_ARGS_BLOCK() : 0); 771 s->ainfo = (((ma+oa) & 0x3f) << 7) /* (12bits = 5:1:5:1) */ 772 | ((ra & 0x1) << 6) 773 | ((pa & 0x1f) << 1) 774 | ((ka | kd) != 0 ? 0x01 : 0x00); 775 genop_W(s, OP_ENTER, a); 776 /* generate jump table for optional arguments initializer */ 689 777 pos = new_label(s); 690 778 for (i=0; i<oa; i++) { 691 779 new_label(s); 692 gen op(s, MKOP_sBx(OP_JMP, 0));780 genjmp(s, OP_JMP, 0); 693 781 } 694 782 if (oa > 0) { 695 gen op(s, MKOP_sBx(OP_JMP, 0));783 genjmp(s, OP_JMP, 0); 696 784 } 697 785 opt = tree->car->cdr->car; … … 700 788 int idx; 701 789 702 dispatch(s, pos+i );790 dispatch(s, pos+i*3+1); 703 791 codegen(s, opt->car->cdr, VAL); 704 idx = lv_idx(s, (mrb_sym)(intptr_t)opt->car->car);705 792 pop(); 706 genop_peep(s, MKOP_AB(OP_MOVE, idx, cursp()), NOVAL); 793 idx = lv_idx(s, nsym(opt->car->car)); 794 gen_move(s, idx, cursp(), 0); 707 795 i++; 708 796 opt = opt->cdr; 709 797 } 710 798 if (oa > 0) { 711 dispatch(s, pos+i); 712 } 713 } 799 dispatch(s, pos+i*3+1); 800 } 801 802 /* keyword arguments */ 803 if (tail) { 804 node *kwds = tail->cdr->car; 805 int kwrest = 0; 806 807 if (tail->cdr->cdr->car) { 808 kwrest = 1; 809 } 810 mrb_assert(nint(tail->car) == NODE_ARGS_TAIL); 811 mrb_assert(node_len(tail) == 4); 812 813 while (kwds) { 814 int jmpif_key_p, jmp_def_set = -1; 815 node *kwd = kwds->car, *def_arg = kwd->cdr->cdr->car; 816 mrb_sym kwd_sym = nsym(kwd->cdr->car); 817 818 mrb_assert(nint(kwd->car) == NODE_KW_ARG); 819 820 if (def_arg) { 821 genop_2(s, OP_KEY_P, lv_idx(s, kwd_sym), new_sym(s, kwd_sym)); 822 jmpif_key_p = genjmp2(s, OP_JMPIF, lv_idx(s, kwd_sym), 0, 0); 823 codegen(s, def_arg, VAL); 824 pop(); 825 gen_move(s, lv_idx(s, kwd_sym), cursp(), 0); 826 jmp_def_set = genjmp(s, OP_JMP, 0); 827 dispatch(s, jmpif_key_p); 828 } 829 genop_2(s, OP_KARG, lv_idx(s, kwd_sym), new_sym(s, kwd_sym)); 830 if (jmp_def_set != -1) { 831 dispatch(s, jmp_def_set); 832 } 833 i++; 834 835 kwds = kwds->cdr; 836 } 837 if (tail->cdr->car && !kwrest) { 838 genop_0(s, OP_KEYEND); 839 } 840 } 841 842 /* argument destructuring */ 843 if (margs) { 844 node *n = margs; 845 846 pos = 1; 847 while (n) { 848 if (nint(n->car->car) == NODE_MASGN) { 849 gen_vmassignment(s, n->car->cdr->car, pos, NOVAL); 850 } 851 pos++; 852 n = n->cdr; 853 } 854 } 855 if (pargs) { 856 node *n = margs; 857 858 pos = ma+oa+ra+1; 859 while (n) { 860 if (nint(n->car->car) == NODE_MASGN) { 861 gen_vmassignment(s, n->car->cdr->car, pos, NOVAL); 862 } 863 pos++; 864 n = n->cdr; 865 } 866 } 867 } 868 714 869 codegen(s, tree->cdr->car, VAL); 715 870 pop(); 716 871 if (s->pc > 0) { 717 c = s->iseq[s->pc-1]; 718 if (GET_OPCODE(c) != OP_RETURN || GETARG_B(c) != OP_R_NORMAL || s->pc == s->lastlabel) { 719 if (s->nregs == 0) { 720 genop(s, MKOP_A(OP_LOADNIL, 0)); 721 genop(s, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL)); 722 } 723 else { 724 genop_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL); 725 } 726 } 872 gen_return(s, OP_RETURN, cursp()); 727 873 } 728 874 if (blk) { … … 737 883 { 738 884 codegen_scope *scope = scope_new(s->mrb, s, tree->car); 739 if (scope == NULL) {740 raise_error(s, "unexpected scope");741 }742 885 743 886 codegen(scope, tree->cdr, VAL); 887 gen_return(scope, OP_RETURN, scope->sp-1); 744 888 if (!s->iseq) { 745 genop(scope, MKOP_A(OP_STOP, 0)); 746 } 747 else if (!val) { 748 genop(scope, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL)); 749 } 750 else { 751 if (scope->nregs == 0) { 752 genop(scope, MKOP_A(OP_LOADNIL, 0)); 753 genop(scope, MKOP_AB(OP_RETURN, 0, OP_R_NORMAL)); 754 } 755 else { 756 genop_peep(scope, MKOP_AB(OP_RETURN, scope->sp-1, OP_R_NORMAL), NOVAL); 757 } 889 genop_0(scope, OP_STOP); 758 890 } 759 891 scope_finish(scope); … … 769 901 { 770 902 while (t) { 771 if ( (intptr_t)t->car->car== NODE_SPLAT) return FALSE;903 if (nint(t->car->car) == NODE_SPLAT) return FALSE; 772 904 t = t->cdr; 773 905 } … … 782 914 char *name2; 783 915 784 name = mrb_sym 2name_len(s->mrb, a, &len);916 name = mrb_sym_name_len(s->mrb, a, &len); 785 917 name2 = (char *)codegen_palloc(s, 786 918 (size_t)len … … 805 937 806 938 while (t) { 807 is_splat = (intptr_t)t->car->car== NODE_SPLAT; /* splat mode */939 is_splat = nint(t->car->car) == NODE_SPLAT; /* splat mode */ 808 940 if ( 809 941 n+extra >= CALL_MAXARGS - 1 /* need to subtract one because vm.c expects an array if n == CALL_MAXARGS */ 810 942 || is_splat) { 811 943 if (val) { 812 if (is_splat && n == 0 && (intptr_t)t->car->cdr->car== NODE_ARRAY) {944 if (is_splat && n == 0 && nint(t->car->cdr->car) == NODE_ARRAY) { 813 945 codegen(s, t->car->cdr, VAL); 814 946 pop(); … … 816 948 else { 817 949 pop_n(n); 818 genop(s, MKOP_ABC(OP_ARRAY, cursp(), cursp(), n)); 950 if (n == 0 && is_splat) { 951 genop_1(s, OP_LOADNIL, cursp()); 952 } 953 else { 954 genop_2(s, OP_ARRAY, cursp(), n); 955 } 819 956 push(); 820 957 codegen(s, t->car, VAL); 821 958 pop(); pop(); 822 959 if (is_splat) { 823 genop (s, MKOP_AB(OP_ARYCAT, cursp(), cursp()+1));960 genop_1(s, OP_ARYCAT, cursp()); 824 961 } 825 962 else { 826 genop (s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1));963 genop_1(s, OP_ARYPUSH, cursp()); 827 964 } 828 965 } … … 832 969 codegen(s, t->car, VAL); 833 970 pop(); pop(); 834 if ( (intptr_t)t->car->car== NODE_SPLAT) {835 genop (s, MKOP_AB(OP_ARYCAT, cursp(), cursp()+1));971 if (nint(t->car->car) == NODE_SPLAT) { 972 genop_1(s, OP_ARYCAT, cursp()); 836 973 } 837 974 else { 838 genop (s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1));975 genop_1(s, OP_ARYPUSH, cursp()); 839 976 } 840 977 t = t->cdr; … … 860 997 gen_call(codegen_scope *s, node *tree, mrb_sym name, int sp, int val, int safe) 861 998 { 862 mrb_sym sym = name ? name : sym(tree->cdr->car);863 int idx,skip = 0;999 mrb_sym sym = name ? name : nsym(tree->cdr->car); 1000 int skip = 0; 864 1001 int n = 0, noop = 0, sendv = 0, blk = 0; 865 1002 … … 867 1004 if (safe) { 868 1005 int recv = cursp()-1; 869 genop(s, MKOP_A(OP_LOADNIL, cursp())); 870 push(); 871 genop(s, MKOP_AB(OP_MOVE, cursp(), recv)); 872 push(); pop(); /* space for a block */ 873 pop(); 874 idx = new_msym(s, mrb_intern_lit(s->mrb, "==")); 875 genop(s, MKOP_ABC(OP_EQ, cursp(), idx, 1)); 876 skip = genop(s, MKOP_AsBx(OP_JMPIF, cursp(), 0)); 877 } 878 idx = new_msym(s, sym); 1006 gen_move(s, cursp(), recv, 1); 1007 skip = genjmp2(s, OP_JMPNIL, cursp(), 0, val); 1008 } 879 1009 tree = tree->cdr->cdr->car; 880 1010 if (tree) { … … 885 1015 } 886 1016 } 887 if (sp) { 1017 if (sp) { /* last argument pushed (attr=) */ 888 1018 if (sendv) { 1019 gen_move(s, cursp(), sp, 0); 889 1020 pop(); 890 genop (s, MKOP_AB(OP_ARYPUSH, cursp(), sp));1021 genop_1(s, OP_ARYPUSH, cursp()); 891 1022 push(); 892 1023 } 893 1024 else { 894 gen op(s, MKOP_AB(OP_MOVE, cursp(), sp));1025 gen_move(s, cursp(), sp, 0); 895 1026 push(); 896 1027 n++; … … 901 1032 codegen(s, tree->cdr, VAL); 902 1033 pop(); 903 } 904 else { 905 blk = cursp(); 1034 blk = 1; 906 1035 } 907 1036 push();pop(); … … 909 1038 { 910 1039 mrb_int symlen; 911 const char *symname = mrb_sym 2name_len(s->mrb, sym, &symlen);1040 const char *symname = mrb_sym_name_len(s->mrb, sym, &symlen); 912 1041 913 1042 if (!noop && symlen == 1 && symname[0] == '+' && n == 1) { 914 gen op_peep(s, MKOP_ABC(OP_ADD, cursp(), idx, n), val);1043 gen_addsub(s, OP_ADD, cursp()); 915 1044 } 916 1045 else if (!noop && symlen == 1 && symname[0] == '-' && n == 1) { 917 gen op_peep(s, MKOP_ABC(OP_SUB, cursp(), idx, n), val);1046 gen_addsub(s, OP_SUB, cursp()); 918 1047 } 919 1048 else if (!noop && symlen == 1 && symname[0] == '*' && n == 1) { 920 genop (s, MKOP_ABC(OP_MUL, cursp(), idx, n));1049 genop_1(s, OP_MUL, cursp()); 921 1050 } 922 1051 else if (!noop && symlen == 1 && symname[0] == '/' && n == 1) { 923 genop (s, MKOP_ABC(OP_DIV, cursp(), idx, n));1052 genop_1(s, OP_DIV, cursp()); 924 1053 } 925 1054 else if (!noop && symlen == 1 && symname[0] == '<' && n == 1) { 926 genop (s, MKOP_ABC(OP_LT, cursp(), idx, n));1055 genop_1(s, OP_LT, cursp()); 927 1056 } 928 1057 else if (!noop && symlen == 2 && symname[0] == '<' && symname[1] == '=' && n == 1) { 929 genop (s, MKOP_ABC(OP_LE, cursp(), idx, n));1058 genop_1(s, OP_LE, cursp()); 930 1059 } 931 1060 else if (!noop && symlen == 1 && symname[0] == '>' && n == 1) { 932 genop (s, MKOP_ABC(OP_GT, cursp(), idx, n));1061 genop_1(s, OP_GT, cursp()); 933 1062 } 934 1063 else if (!noop && symlen == 2 && symname[0] == '>' && symname[1] == '=' && n == 1) { 935 genop (s, MKOP_ABC(OP_GE, cursp(), idx, n));1064 genop_1(s, OP_GE, cursp()); 936 1065 } 937 1066 else if (!noop && symlen == 2 && symname[0] == '=' && symname[1] == '=' && n == 1) { 938 genop (s, MKOP_ABC(OP_EQ, cursp(), idx, n));1067 genop_1(s, OP_EQ, cursp()); 939 1068 } 940 1069 else { 941 if (sendv) n = CALL_MAXARGS; 942 if (blk > 0) { /* no block */ 943 genop(s, MKOP_ABC(OP_SEND, cursp(), idx, n)); 1070 int idx = new_sym(s, sym); 1071 1072 if (sendv) { 1073 genop_2(s, blk ? OP_SENDVB : OP_SENDV, cursp(), idx); 944 1074 } 945 1075 else { 946 genop (s, MKOP_ABC(OP_SENDB, cursp(), idx, n));1076 genop_3(s, blk ? OP_SENDB : OP_SEND, cursp(), idx, n); 947 1077 } 948 1078 } … … 960 1090 { 961 1091 int idx; 962 int type = (intptr_t)tree->car;1092 int type = nint(tree->car); 963 1093 964 1094 tree = tree->cdr; 965 1095 switch (type) { 966 1096 case NODE_GVAR: 967 idx = new_sym(s, sym(tree)); 968 genop_peep(s, MKOP_ABx(OP_SETGLOBAL, sp, idx), val); 969 break; 1097 idx = new_sym(s, nsym(tree)); 1098 genop_2(s, OP_SETGV, sp, idx); 1099 break; 1100 case NODE_ARG: 970 1101 case NODE_LVAR: 971 idx = lv_idx(s, sym(tree));1102 idx = lv_idx(s, nsym(tree)); 972 1103 if (idx > 0) { 973 1104 if (idx != sp) { 974 genop_peep(s, MKOP_AB(OP_MOVE, idx, sp), val); 1105 gen_move(s, idx, sp, val); 1106 if (val && on_eval(s)) genop_0(s, OP_NOP); 975 1107 } 976 1108 break; … … 981 1113 982 1114 while (up) { 983 idx = lv_idx(up, sym(tree));1115 idx = lv_idx(up, nsym(tree)); 984 1116 if (idx > 0) { 985 genop_ peep(s, MKOP_ABC(OP_SETUPVAR, sp, idx, lv), val);1117 genop_3(s, OP_SETUPVAR, sp, idx, lv); 986 1118 break; 987 1119 } … … 991 1123 } 992 1124 break; 1125 case NODE_NVAR: 1126 idx = nint(tree); 1127 codegen_error(s, "Can't assign to numbered parameter"); 1128 break; 993 1129 case NODE_IVAR: 994 idx = new_sym(s, sym(tree));995 genop_ peep(s, MKOP_ABx(OP_SETIV, sp, idx), val);1130 idx = new_sym(s, nsym(tree)); 1131 genop_2(s, OP_SETIV, sp, idx); 996 1132 break; 997 1133 case NODE_CVAR: 998 idx = new_sym(s, sym(tree));999 genop_ peep(s, MKOP_ABx(OP_SETCV, sp, idx), val);1134 idx = new_sym(s, nsym(tree)); 1135 genop_2(s, OP_SETCV, sp, idx); 1000 1136 break; 1001 1137 case NODE_CONST: 1002 idx = new_sym(s, sym(tree));1003 genop_ peep(s, MKOP_ABx(OP_SETCONST, sp, idx), val);1138 idx = new_sym(s, nsym(tree)); 1139 genop_2(s, OP_SETCONST, sp, idx); 1004 1140 break; 1005 1141 case NODE_COLON2: 1006 idx = new_sym(s, sym(tree->cdr)); 1007 genop_peep(s, MKOP_AB(OP_MOVE, cursp(), sp), NOVAL); 1142 gen_move(s, cursp(), sp, 0); 1008 1143 push(); 1009 1144 codegen(s, tree->car, VAL); 1010 1145 pop_n(2); 1011 genop_peep(s, MKOP_ABx(OP_SETMCNST, cursp(), idx), val); 1146 idx = new_sym(s, nsym(tree->cdr)); 1147 genop_2(s, OP_SETMCNST, sp, idx); 1012 1148 break; 1013 1149 … … 1015 1151 case NODE_SCALL: 1016 1152 push(); 1017 gen_call(s, tree, attrsym(s, sym(tree->cdr->car)), sp, NOVAL,1153 gen_call(s, tree, attrsym(s, nsym(tree->cdr->car)), sp, NOVAL, 1018 1154 type == NODE_SCALL); 1019 1155 pop(); 1020 1156 if (val) { 1021 gen op_peep(s, MKOP_AB(OP_MOVE, cursp(), sp), val);1157 gen_move(s, cursp(), sp, 0); 1022 1158 } 1023 1159 break; … … 1050 1186 n = 0; 1051 1187 while (t) { 1052 genop(s, MKOP_ABC(OP_AREF, cursp(), rhs, n)); 1053 gen_assignment(s, t->car, cursp(), NOVAL); 1188 int sp = cursp(); 1189 1190 genop_3(s, OP_AREF, sp, rhs, n); 1191 push(); 1192 gen_assignment(s, t->car, sp, NOVAL); 1193 pop(); 1054 1194 n++; 1055 1195 t = t->cdr; … … 1065 1205 } 1066 1206 } 1067 if (val) { 1068 genop(s, MKOP_AB(OP_MOVE, cursp(), rhs)); 1069 } 1070 else { 1071 pop(); 1072 } 1073 push_n(post); 1074 pop_n(post); 1075 genop(s, MKOP_ABC(OP_APOST, cursp(), n, post)); 1207 gen_move(s, cursp(), rhs, val); 1208 push_n(post+1); 1209 pop_n(post+1); 1210 genop_3(s, OP_APOST, cursp(), n, post); 1076 1211 n = 1; 1077 if (t->car ) {/* rest */1212 if (t->car && t->car != (node*)-1) { /* rest */ 1078 1213 gen_assignment(s, t->car, cursp(), NOVAL); 1079 1214 } … … 1086 1221 } 1087 1222 } 1088 if ( !val) {1089 push();1223 if (val) { 1224 gen_move(s, cursp(), rhs, 0); 1090 1225 } 1091 1226 } … … 1093 1228 1094 1229 static void 1095 gen_ send_intern(codegen_scope *s)1230 gen_intern(codegen_scope *s) 1096 1231 { 1097 1232 pop(); 1098 genop (s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "intern")), 0));1233 genop_1(s, OP_INTERN, cursp()); 1099 1234 push(); 1100 1235 } 1236 1101 1237 static void 1102 1238 gen_literal_array(codegen_scope *s, node *tree, mrb_bool sym, int val) … … 1106 1242 1107 1243 while (tree) { 1108 switch ( (intptr_t)tree->car->car) {1244 switch (nint(tree->car->car)) { 1109 1245 case NODE_STR: 1110 if ((tree->cdr == NULL) && ( (intptr_t)tree->car->cdr->cdr== 0))1246 if ((tree->cdr == NULL) && (nint(tree->car->cdr->cdr) == 0)) 1111 1247 break; 1112 1248 /* fall through */ … … 1121 1257 ++i; 1122 1258 if (sym) 1123 gen_ send_intern(s);1259 gen_intern(s); 1124 1260 } 1125 1261 break; 1126 1262 } 1127 if(j >= 2) {1263 while (j >= 2) { 1128 1264 pop(); pop(); 1129 genop_ peep(s, MKOP_AB(OP_STRCAT, cursp(), cursp()+1), VAL);1265 genop_1(s, OP_STRCAT, cursp()); 1130 1266 push(); 1131 j = 1;1267 j--; 1132 1268 } 1133 1269 tree = tree->cdr; … … 1136 1272 ++i; 1137 1273 if (sym) 1138 gen_ send_intern(s);1274 gen_intern(s); 1139 1275 } 1140 1276 pop_n(i); 1141 genop (s, MKOP_ABC(OP_ARRAY, cursp(), cursp(), i));1277 genop_2(s, OP_ARRAY, cursp(), i); 1142 1278 push(); 1143 1279 } 1144 1280 else { 1145 1281 while (tree) { 1146 switch ( (intptr_t)tree->car->car) {1282 switch (nint(tree->car->car)) { 1147 1283 case NODE_BEGIN: case NODE_BLOCK: 1148 1284 codegen(s, tree->car, NOVAL); … … 1158 1294 int idx = new_lit(s, mrb_str_new_cstr(s->mrb, msg)); 1159 1295 1160 genop(s, MKOP_ABx(OP_ERR, 1, idx)); 1161 } 1162 1296 genop_1(s, OP_ERR, idx); 1297 } 1298 1299 #ifndef MRB_WITHOUT_FLOAT 1163 1300 static double 1164 1301 readint_float(codegen_scope *s, const char *p, int base) … … 1186 1323 return f; 1187 1324 } 1325 #endif 1188 1326 1189 1327 static mrb_int … … 1233 1371 gen_retval(codegen_scope *s, node *tree) 1234 1372 { 1235 if ((intptr_t)tree->car == NODE_SPLAT) { 1236 genop(s, MKOP_ABC(OP_ARRAY, cursp(), cursp(), 0)); 1237 push(); 1373 if (nint(tree->car) == NODE_SPLAT) { 1238 1374 codegen(s, tree, VAL); 1239 pop(); pop();1240 genop (s, MKOP_AB(OP_ARYCAT, cursp(), cursp()+1));1375 pop(); 1376 genop_1(s, OP_ARYDUP, cursp()); 1241 1377 } 1242 1378 else { … … 1254 1390 if (!tree) { 1255 1391 if (val) { 1256 genop (s, MKOP_A(OP_LOADNIL, cursp()));1392 genop_1(s, OP_LOADNIL, cursp()); 1257 1393 push(); 1258 1394 } … … 1265 1401 } 1266 1402 if (s->irep && s->filename_index != tree->filename_index) { 1267 s->irep->filename = mrb_parser_get_filename(s->parser, s->filename_index); 1268 mrb_debug_info_append_file(s->mrb, s->irep, s->debug_start_pos, s->pc); 1403 mrb_sym fname = mrb_parser_get_filename(s->parser, s->filename_index); 1404 const char *filename = mrb_sym_name_len(s->mrb, fname, NULL); 1405 1406 mrb_debug_info_append_file(s->mrb, s->irep->debug_info, 1407 filename, s->lines, s->debug_start_pos, s->pc); 1269 1408 s->debug_start_pos = s->pc; 1270 1409 s->filename_index = tree->filename_index; 1271 s->filename = mrb_parser_get_filename(s->parser, tree->filename_index);1272 } 1273 1274 nt = (intptr_t)tree->car;1410 s->filename_sym = mrb_parser_get_filename(s->parser, tree->filename_index); 1411 } 1412 1413 nt = nint(tree->car); 1275 1414 s->lineno = tree->lineno; 1276 1415 tree = tree->cdr; … … 1278 1417 case NODE_BEGIN: 1279 1418 if (val && !tree) { 1280 genop (s, MKOP_A(OP_LOADNIL, cursp()));1419 genop_1(s, OP_LOADNIL, cursp()); 1281 1420 push(); 1282 1421 } … … 1289 1428 case NODE_RESCUE: 1290 1429 { 1291 int onerr,noexc, exend, pos1, pos2, tmp;1430 int noexc, exend, pos1, pos2, tmp; 1292 1431 struct loopinfo *lp; 1293 1432 1294 1433 if (tree->car == NULL) goto exit; 1295 onerr = genop(s, MKOP_Bx(OP_ONERR, 0));1296 1434 lp = loop_push(s, LOOP_BEGIN); 1297 lp->pc1 = onerr; 1435 lp->pc0 = new_label(s); 1436 lp->pc1 = genjmp(s, OP_ONERR, 0); 1298 1437 codegen(s, tree->car, VAL); 1299 1438 pop(); 1300 1439 lp->type = LOOP_RESCUE; 1301 noexc = gen op(s, MKOP_Bx(OP_JMP, 0));1302 dispatch(s, onerr);1440 noexc = genjmp(s, OP_JMP, 0); 1441 dispatch(s, lp->pc1); 1303 1442 tree = tree->cdr; 1304 1443 exend = 0; … … 1308 1447 int exc = cursp(); 1309 1448 1310 genop (s, MKOP_ABC(OP_RESCUE, exc, 0, 0));1449 genop_1(s, OP_EXCEPT, exc); 1311 1450 push(); 1312 1451 while (n2) { … … 1317 1456 pos2 = 0; 1318 1457 do { 1319 if (n4 && n4->car && (intptr_t)n4->car->car== NODE_SPLAT) {1458 if (n4 && n4->car && nint(n4->car->car) == NODE_SPLAT) { 1320 1459 codegen(s, n4->car, VAL); 1321 genop(s, MKOP_AB(OP_MOVE, cursp(), exc)); 1460 gen_move(s, cursp(), exc, 0); 1461 push_n(2); pop_n(2); /* space for one arg and a block */ 1322 1462 pop(); 1323 genop (s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "__case_eqq")), 1));1463 genop_3(s, OP_SEND, cursp(), new_sym(s, mrb_intern_lit(s->mrb, "__case_eqq")), 1); 1324 1464 } 1325 1465 else { … … 1328 1468 } 1329 1469 else { 1330 genop (s, MKOP_ABx(OP_GETCONST, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "StandardError"))));1470 genop_2(s, OP_GETCONST, cursp(), new_sym(s, mrb_intern_lit(s->mrb, "StandardError"))); 1331 1471 push(); 1332 1472 } 1333 1473 pop(); 1334 genop (s, MKOP_ABC(OP_RESCUE, exc, cursp(), 1));1474 genop_2(s, OP_RESCUE, exc, cursp()); 1335 1475 } 1336 tmp = gen op(s, MKOP_AsBx(OP_JMPIF, cursp(), pos2));1476 tmp = genjmp2(s, OP_JMPIF, cursp(), pos2, val); 1337 1477 pos2 = tmp; 1338 1478 if (n4) { … … 1340 1480 } 1341 1481 } while (n4); 1342 pos1 = gen op(s, MKOP_sBx(OP_JMP, 0));1482 pos1 = genjmp(s, OP_JMP, 0); 1343 1483 dispatch_linked(s, pos2); 1344 1484 … … 1351 1491 if (val) pop(); 1352 1492 } 1353 tmp = gen op(s, MKOP_sBx(OP_JMP, exend));1493 tmp = genjmp(s, OP_JMP, exend); 1354 1494 exend = tmp; 1355 1495 n2 = n2->cdr; … … 1358 1498 if (pos1) { 1359 1499 dispatch(s, pos1); 1360 genop (s, MKOP_A(OP_RAISE, exc));1500 genop_1(s, OP_RAISE, exc); 1361 1501 } 1362 1502 } … … 1364 1504 tree = tree->cdr; 1365 1505 dispatch(s, noexc); 1366 genop (s, MKOP_A(OP_POPERR, 1));1506 genop_1(s, OP_POPERR, 1); 1367 1507 if (tree->car) { 1368 1508 codegen(s, tree->car, val); … … 1378 1518 case NODE_ENSURE: 1379 1519 if (!tree->cdr || !tree->cdr->cdr || 1380 ( (intptr_t)tree->cdr->cdr->car== NODE_BEGIN &&1520 (nint(tree->cdr->cdr->car) == NODE_BEGIN && 1381 1521 tree->cdr->cdr->cdr)) { 1382 1522 int idx; 1383 int epush = s->pc; 1384 1385 genop(s, MKOP_Bx(OP_EPUSH, 0)); 1523 1386 1524 s->ensure_level++; 1525 idx = scope_body(s, tree->cdr, NOVAL); 1526 genop_1(s, OP_EPUSH, idx); 1387 1527 codegen(s, tree->car, val); 1388 idx = scope_body(s, tree->cdr, NOVAL);1389 s->iseq[epush] = MKOP_Bx(OP_EPUSH, idx);1390 1528 s->ensure_level--; 1391 genop_ peep(s, MKOP_A(OP_EPOP, 1), NOVAL);1529 genop_1(s, OP_EPOP, 1); 1392 1530 } 1393 1531 else { /* empty ensure ignored */ … … 1400 1538 int idx = lambda_body(s, tree, 1); 1401 1539 1402 genop (s, MKOP_Abc(OP_LAMBDA, cursp(), idx, OP_L_LAMBDA));1540 genop_2(s, OP_LAMBDA, cursp(), idx); 1403 1541 push(); 1404 1542 } … … 1409 1547 int idx = lambda_body(s, tree, 1); 1410 1548 1411 genop (s, MKOP_Abc(OP_LAMBDA, cursp(), idx, OP_L_BLOCK));1549 genop_2(s, OP_BLOCK, cursp(), idx); 1412 1550 push(); 1413 1551 } … … 1416 1554 case NODE_IF: 1417 1555 { 1418 int pos1, pos2 ;1419 node *e = tree->cdr->cdr->car;1556 int pos1, pos2, nil_p = FALSE; 1557 node *elsepart = tree->cdr->cdr->car; 1420 1558 1421 1559 if (!tree->car) { 1422 codegen(s, e , val);1560 codegen(s, elsepart, val); 1423 1561 goto exit; 1424 1562 } 1425 switch ( (intptr_t)tree->car->car) {1563 switch (nint(tree->car->car)) { 1426 1564 case NODE_TRUE: 1427 1565 case NODE_INT: … … 1431 1569 case NODE_FALSE: 1432 1570 case NODE_NIL: 1433 codegen(s, e , val);1571 codegen(s, elsepart, val); 1434 1572 goto exit; 1435 } 1436 codegen(s, tree->car, VAL); 1573 case NODE_CALL: 1574 { 1575 node *n = tree->car->cdr; 1576 mrb_sym mid = nsym(n->cdr->car); 1577 mrb_sym mnil = mrb_intern_lit(s->mrb, "nil?"); 1578 if (mid == mnil && n->cdr->cdr->car == NULL) { 1579 nil_p = TRUE; 1580 codegen(s, n->car, VAL); 1581 } 1582 } 1583 break; 1584 } 1585 if (!nil_p) { 1586 codegen(s, tree->car, VAL); 1587 } 1437 1588 pop(); 1438 pos1 = genop_peep(s, MKOP_AsBx(OP_JMPNOT, cursp(), 0), NOVAL); 1439 1440 codegen(s, tree->cdr->car, val); 1441 if (e) { 1589 if (val || tree->cdr->car) { 1590 if (nil_p) { 1591 pos2 = genjmp2(s, OP_JMPNIL, cursp(), 0, val); 1592 pos1 = genjmp(s, OP_JMP, 0); 1593 dispatch(s, pos2); 1594 } 1595 else { 1596 pos1 = genjmp2(s, OP_JMPNOT, cursp(), 0, val); 1597 } 1598 codegen(s, tree->cdr->car, val); 1442 1599 if (val) pop(); 1443 pos2 = genop(s, MKOP_sBx(OP_JMP, 0)); 1444 dispatch(s, pos1); 1445 codegen(s, e, val); 1446 dispatch(s, pos2); 1447 } 1448 else { 1449 if (val) { 1450 pop(); 1451 pos2 = genop(s, MKOP_sBx(OP_JMP, 0)); 1600 if (elsepart || val) { 1601 pos2 = genjmp(s, OP_JMP, 0); 1452 1602 dispatch(s, pos1); 1453 genop(s, MKOP_A(OP_LOADNIL, cursp()));1603 codegen(s, elsepart, val); 1454 1604 dispatch(s, pos2); 1455 push();1456 1605 } 1457 1606 else { 1458 1607 dispatch(s, pos1); 1608 } 1609 } 1610 else { /* empty then-part */ 1611 if (elsepart) { 1612 if (nil_p) { 1613 pos1 = genjmp2(s, OP_JMPNIL, cursp(), 0, val); 1614 } 1615 else { 1616 pos1 = genjmp2(s, OP_JMPIF, cursp(), 0, val); 1617 } 1618 codegen(s, elsepart, val); 1619 dispatch(s, pos1); 1620 } 1621 else if (val && !nil_p) { 1622 genop_1(s, OP_LOADNIL, cursp()); 1623 push(); 1459 1624 } 1460 1625 } … … 1468 1633 codegen(s, tree->car, VAL); 1469 1634 pop(); 1470 pos = gen op(s, MKOP_AsBx(OP_JMPNOT, cursp(), 0));1635 pos = genjmp2(s, OP_JMPNOT, cursp(), 0, val); 1471 1636 codegen(s, tree->cdr, val); 1472 1637 dispatch(s, pos); … … 1480 1645 codegen(s, tree->car, VAL); 1481 1646 pop(); 1482 pos = gen op(s, MKOP_AsBx(OP_JMPIF, cursp(), 0));1647 pos = genjmp2(s, OP_JMPIF, cursp(), 0, val); 1483 1648 codegen(s, tree->cdr, val); 1484 1649 dispatch(s, pos); … … 1490 1655 struct loopinfo *lp = loop_push(s, LOOP_NORMAL); 1491 1656 1492 lp->pc1 = genop(s, MKOP_sBx(OP_JMP, 0)); 1657 lp->pc0 = new_label(s); 1658 lp->pc1 = genjmp(s, OP_JMP, 0); 1493 1659 lp->pc2 = new_label(s); 1494 1660 codegen(s, tree->cdr, NOVAL); … … 1496 1662 codegen(s, tree->car, VAL); 1497 1663 pop(); 1498 gen op(s, MKOP_AsBx(OP_JMPIF, cursp(), lp->pc2 - s->pc));1664 genjmp2(s, OP_JMPIF, cursp(), lp->pc2, NOVAL); 1499 1665 1500 1666 loop_pop(s, val); … … 1506 1672 struct loopinfo *lp = loop_push(s, LOOP_NORMAL); 1507 1673 1508 lp->pc1 = genop(s, MKOP_sBx(OP_JMP, 0)); 1674 lp->pc0 = new_label(s); 1675 lp->pc1 = genjmp(s, OP_JMP, 0); 1509 1676 lp->pc2 = new_label(s); 1510 1677 codegen(s, tree->cdr, NOVAL); … … 1512 1679 codegen(s, tree->car, VAL); 1513 1680 pop(); 1514 gen op(s, MKOP_AsBx(OP_JMPNOT, cursp(), lp->pc2 - s->pc));1681 genjmp2(s, OP_JMPNOT, cursp(), lp->pc2, NOVAL); 1515 1682 1516 1683 loop_pop(s, val); … … 1541 1708 codegen(s, n->car, VAL); 1542 1709 if (head) { 1543 gen op(s, MKOP_AB(OP_MOVE, cursp(), head));1544 p op();1545 if ( (intptr_t)n->car->car== NODE_SPLAT) {1546 genop (s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "__case_eqq")), 1));1710 gen_move(s, cursp(), head, 0); 1711 push(); push(); pop(); pop(); pop(); 1712 if (nint(n->car->car) == NODE_SPLAT) { 1713 genop_3(s, OP_SEND, cursp(), new_sym(s, mrb_intern_lit(s->mrb, "__case_eqq")), 1); 1547 1714 } 1548 1715 else { 1549 genop (s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "===")), 1));1716 genop_3(s, OP_SEND, cursp(), new_sym(s, mrb_intern_lit(s->mrb, "===")), 1); 1550 1717 } 1551 1718 } … … 1553 1720 pop(); 1554 1721 } 1555 tmp = gen op(s, MKOP_AsBx(OP_JMPIF, cursp(), pos2));1722 tmp = genjmp2(s, OP_JMPIF, cursp(), pos2, NOVAL); 1556 1723 pos2 = tmp; 1557 1724 n = n->cdr; 1558 1725 } 1559 1726 if (tree->car->car) { 1560 pos1 = gen op(s, MKOP_sBx(OP_JMP, 0));1727 pos1 = genjmp(s, OP_JMP, 0); 1561 1728 dispatch_linked(s, pos2); 1562 1729 } 1563 1730 codegen(s, tree->car->cdr, val); 1564 1731 if (val) pop(); 1565 tmp = gen op(s, MKOP_sBx(OP_JMP, pos3));1732 tmp = genjmp(s, OP_JMP, pos3); 1566 1733 pos3 = tmp; 1567 1734 if (pos1) dispatch(s, pos1); … … 1570 1737 if (val) { 1571 1738 int pos = cursp(); 1572 genop (s, MKOP_A(OP_LOADNIL, cursp()));1739 genop_1(s, OP_LOADNIL, cursp()); 1573 1740 if (pos3) dispatch_linked(s, pos3); 1574 1741 if (head) pop(); 1575 1742 if (cursp() != pos) { 1576 gen op(s, MKOP_AB(OP_MOVE, cursp(), pos));1743 gen_move(s, cursp(), pos, 0); 1577 1744 } 1578 1745 push(); … … 1606 1773 if (val) { 1607 1774 pop(); pop(); 1608 genop (s, MKOP_ABC(OP_RANGE, cursp(), cursp(), FALSE));1775 genop_1(s, OP_RANGE_INC, cursp()); 1609 1776 push(); 1610 1777 } … … 1616 1783 if (val) { 1617 1784 pop(); pop(); 1618 genop (s, MKOP_ABC(OP_RANGE, cursp(), cursp(), TRUE));1785 genop_1(s, OP_RANGE_EXC, cursp()); 1619 1786 push(); 1620 1787 } … … 1623 1790 case NODE_COLON2: 1624 1791 { 1625 int sym = new_sym(s, sym(tree->cdr));1792 int sym = new_sym(s, nsym(tree->cdr)); 1626 1793 1627 1794 codegen(s, tree->car, VAL); 1628 1795 pop(); 1629 genop (s, MKOP_ABx(OP_GETMCNST, cursp(), sym));1796 genop_2(s, OP_GETMCNST, cursp(), sym); 1630 1797 if (val) push(); 1631 1798 } … … 1634 1801 case NODE_COLON3: 1635 1802 { 1636 int sym = new_sym(s, sym(tree));1637 1638 genop (s, MKOP_A(OP_OCLASS, cursp()));1639 genop (s, MKOP_ABx(OP_GETMCNST, cursp(), sym));1803 int sym = new_sym(s, nsym(tree)); 1804 1805 genop_1(s, OP_OCLASS, cursp()); 1806 genop_2(s, OP_GETMCNST, cursp(), sym); 1640 1807 if (val) push(); 1641 1808 } … … 1650 1817 if (val) { 1651 1818 pop_n(n); 1652 genop (s, MKOP_ABC(OP_ARRAY, cursp(), cursp(), n));1819 genop_2(s, OP_ARRAY, cursp(), n); 1653 1820 push(); 1654 1821 } … … 1661 1828 1662 1829 case NODE_HASH: 1830 case NODE_KW_HASH: 1663 1831 { 1664 1832 int len = 0; … … 1666 1834 1667 1835 while (tree) { 1668 codegen(s, tree->car->car, val); 1669 codegen(s, tree->car->cdr, val); 1670 len++; 1836 if (nint(tree->car->car->car) == NODE_KW_REST_ARGS) { 1837 if (len > 0) { 1838 pop_n(len*2); 1839 if (!update) { 1840 genop_2(s, OP_HASH, cursp(), len); 1841 } 1842 else { 1843 pop(); 1844 genop_2(s, OP_HASHADD, cursp(), len); 1845 } 1846 push(); 1847 } 1848 codegen(s, tree->car->cdr, VAL); 1849 if (len > 0 || update) { 1850 pop(); pop(); 1851 genop_1(s, OP_HASHCAT, cursp()); 1852 push(); 1853 } 1854 update = TRUE; 1855 len = 0; 1856 } 1857 else { 1858 codegen(s, tree->car->car, val); 1859 codegen(s, tree->car->cdr, val); 1860 len++; 1861 } 1671 1862 tree = tree->cdr; 1672 if (val && len == 126) {1863 if (val && len == 255) { 1673 1864 pop_n(len*2); 1674 genop(s, MKOP_ABC(OP_HASH, cursp(), cursp(), len)); 1675 if (update) { 1865 if (!update) { 1866 genop_2(s, OP_HASH, cursp(), len); 1867 } 1868 else { 1676 1869 pop(); 1677 genop (s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "__update")), 1));1870 genop_2(s, OP_HASHADD, cursp(), len); 1678 1871 } 1679 1872 push(); … … 1684 1877 if (val) { 1685 1878 pop_n(len*2); 1686 genop(s, MKOP_ABC(OP_HASH, cursp(), cursp(), len)); 1687 if (update) { 1879 if (!update) { 1880 genop_2(s, OP_HASH, cursp(), len); 1881 } 1882 else { 1688 1883 pop(); 1689 genop(s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "__update")), 1)); 1884 if (len > 0) { 1885 genop_2(s, OP_HASHADD, cursp(), len); 1886 } 1690 1887 } 1691 1888 push(); … … 1710 1907 int rhs = cursp(); 1711 1908 1712 if ( (intptr_t)t->car== NODE_ARRAY && t->cdr && nosplat(t->cdr)) {1909 if (nint(t->car) == NODE_ARRAY && t->cdr && nosplat(t->cdr)) { 1713 1910 /* fixed rhs */ 1714 1911 t = t->cdr; … … 1728 1925 } 1729 1926 else { 1730 genop (s, MKOP_A(OP_LOADNIL, rhs+n));1927 genop_1(s, OP_LOADNIL, rhs+n); 1731 1928 gen_assignment(s, t->car, rhs+n, NOVAL); 1732 1929 } … … 1752 1949 rn = len - post - n; 1753 1950 } 1754 genop (s, MKOP_ABC(OP_ARRAY, cursp(), rhs+n, rn));1951 genop_3(s, OP_ARRAY2, cursp(), rhs+n, rn); 1755 1952 gen_assignment(s, t->car, cursp(), NOVAL); 1756 1953 n += rn; … … 1767 1964 pop_n(len); 1768 1965 if (val) { 1769 genop (s, MKOP_ABC(OP_ARRAY, rhs, rhs, len));1966 genop_2(s, OP_ARRAY, rhs, len); 1770 1967 push(); 1771 1968 } … … 1784 1981 case NODE_OP_ASGN: 1785 1982 { 1786 mrb_sym sym = sym(tree->cdr->car);1983 mrb_sym sym = nsym(tree->cdr->car); 1787 1984 mrb_int len; 1788 const char *name = mrb_sym 2name_len(s->mrb, sym, &len);1985 const char *name = mrb_sym_name_len(s->mrb, sym, &len); 1789 1986 int idx, callargs = -1, vsp = -1; 1790 1987 1791 1988 if ((len == 2 && name[0] == '|' && name[1] == '|') && 1792 ( (intptr_t)tree->car->car== NODE_CONST ||1793 (intptr_t)tree->car->car== NODE_CVAR)) {1989 (nint(tree->car->car) == NODE_CONST || 1990 nint(tree->car->car) == NODE_CVAR)) { 1794 1991 int onerr, noexc, exc; 1795 1992 struct loopinfo *lp; 1796 1993 1797 onerr = gen op(s, MKOP_Bx(OP_ONERR, 0));1994 onerr = genjmp(s, OP_ONERR, 0); 1798 1995 lp = loop_push(s, LOOP_BEGIN); 1799 1996 lp->pc1 = onerr; … … 1801 1998 codegen(s, tree->car, VAL); 1802 1999 lp->type = LOOP_RESCUE; 1803 genop (s, MKOP_A(OP_POPERR, 1));1804 noexc = gen op(s, MKOP_Bx(OP_JMP, 0));2000 genop_1(s, OP_POPERR, 1); 2001 noexc = genjmp(s, OP_JMP, 0); 1805 2002 dispatch(s, onerr); 1806 genop (s, MKOP_ABC(OP_RESCUE, exc, 0, 0));1807 genop (s, MKOP_A(OP_LOADF, exc));2003 genop_1(s, OP_EXCEPT, exc); 2004 genop_1(s, OP_LOADF, exc); 1808 2005 dispatch(s, noexc); 1809 2006 loop_pop(s, NOVAL); 1810 2007 } 1811 else if ( (intptr_t)tree->car->car== NODE_CALL) {2008 else if (nint(tree->car->car) == NODE_CALL) { 1812 2009 node *n = tree->car->cdr; 2010 int base, i, nargs = 0; 2011 callargs = 0; 1813 2012 1814 2013 if (val) { … … 1817 2016 } 1818 2017 codegen(s, n->car, VAL); /* receiver */ 1819 idx = new_msym(s, sym(n->cdr->car)); 2018 idx = new_sym(s, nsym(n->cdr->car)); 2019 base = cursp()-1; 1820 2020 if (n->cdr->cdr->car) { 1821 int base = cursp()-1; 1822 int nargs = gen_values(s, n->cdr->cdr->car->car, VAL, 1); 1823 1824 /* copy receiver and arguments */ 2021 nargs = gen_values(s, n->cdr->cdr->car->car, VAL, 1); 1825 2022 if (nargs >= 0) { 1826 int i;1827 1828 genop(s, MKOP_AB(OP_MOVE, cursp(), base));1829 for (i=0; i<nargs; i++) {1830 genop(s, MKOP_AB(OP_MOVE, cursp()+i+1, base+i+1));1831 }1832 push_n(nargs+1);1833 pop_n(nargs+1);1834 2023 callargs = nargs; 1835 2024 } 1836 else { 1837 /* varargs */ 2025 else { /* varargs */ 1838 2026 push(); 1839 genop(s, MKOP_AB(OP_MOVE, cursp(), base)); 1840 genop(s, MKOP_AB(OP_MOVE, cursp()+1, base+1)); 2027 nargs = 1; 1841 2028 callargs = CALL_MAXARGS; 1842 2029 } 1843 genop(s, MKOP_ABC(OP_SEND, cursp(), idx, callargs)); 1844 } 1845 else { 1846 genop(s, MKOP_AB(OP_MOVE, cursp(), cursp()-1)); 1847 genop(s, MKOP_ABC(OP_SEND, cursp(), idx, 0)); 1848 callargs = 0; 1849 } 2030 } 2031 /* copy receiver and arguments */ 2032 gen_move(s, cursp(), base, 1); 2033 for (i=0; i<nargs; i++) { 2034 gen_move(s, cursp()+i+1, base+i+1, 1); 2035 } 2036 push_n(nargs+2);pop_n(nargs+2); /* space for receiver, arguments and a block */ 2037 genop_3(s, OP_SEND, cursp(), idx, callargs); 1850 2038 push(); 1851 2039 } … … 1861 2049 if (val) { 1862 2050 if (vsp >= 0) { 1863 gen op(s, MKOP_AB(OP_MOVE, vsp, cursp()));1864 } 1865 pos = gen op(s, MKOP_AsBx(name[0]=='|'?OP_JMPIF:OP_JMPNOT, cursp(), 0));2051 gen_move(s, vsp, cursp(), 1); 2052 } 2053 pos = genjmp2(s, name[0]=='|'?OP_JMPIF:OP_JMPNOT, cursp(), 0, val); 1866 2054 } 1867 2055 else { 1868 pos = gen op_peep(s, MKOP_AsBx(name[0]=='|'?OP_JMPIF:OP_JMPNOT, cursp(), 0), NOVAL);2056 pos = genjmp2(s, name[0]=='|'?OP_JMPIF:OP_JMPNOT, cursp(), 0, val); 1869 2057 } 1870 2058 codegen(s, tree->cdr->cdr->car, VAL); 1871 2059 pop(); 1872 2060 if (val && vsp >= 0) { 1873 genop(s, MKOP_AB(OP_MOVE, vsp, cursp())); 1874 } 1875 if ((intptr_t)tree->car->car == NODE_CALL) { 1876 mrb_sym m = sym(tree->car->cdr->cdr->car); 1877 mrb_sym m2 = attrsym(s, m); 1878 1879 idx = new_msym(s, m2); 1880 pop(); 2061 gen_move(s, vsp, cursp(), 1); 2062 } 2063 if (nint(tree->car->car) == NODE_CALL) { 1881 2064 if (callargs == CALL_MAXARGS) { 1882 genop(s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1));1883 2065 pop(); 1884 genop (s, MKOP_ABC(OP_SEND, cursp(), idx, callargs));2066 genop_1(s, OP_ARYPUSH, cursp()); 1885 2067 } 1886 2068 else { 1887 2069 pop_n(callargs); 1888 genop(s, MKOP_ABC(OP_SEND, cursp(), idx, callargs+1)); 1889 } 2070 callargs++; 2071 } 2072 pop(); 2073 idx = new_sym(s, attrsym(s, nsym(tree->car->cdr->cdr->car))); 2074 genop_3(s, OP_SEND, cursp(), idx, callargs); 1890 2075 } 1891 2076 else { … … 1899 2084 pop(); pop(); 1900 2085 1901 idx = new_msym(s, sym);1902 2086 if (len == 1 && name[0] == '+') { 1903 gen op_peep(s, MKOP_ABC(OP_ADD, cursp(), idx, 1), val);2087 gen_addsub(s, OP_ADD, cursp()); 1904 2088 } 1905 2089 else if (len == 1 && name[0] == '-') { 1906 gen op_peep(s, MKOP_ABC(OP_SUB, cursp(), idx, 1), val);2090 gen_addsub(s, OP_SUB, cursp()); 1907 2091 } 1908 2092 else if (len == 1 && name[0] == '*') { 1909 genop (s, MKOP_ABC(OP_MUL, cursp(), idx, 1));2093 genop_1(s, OP_MUL, cursp()); 1910 2094 } 1911 2095 else if (len == 1 && name[0] == '/') { 1912 genop (s, MKOP_ABC(OP_DIV, cursp(), idx, 1));2096 genop_1(s, OP_DIV, cursp()); 1913 2097 } 1914 2098 else if (len == 1 && name[0] == '<') { 1915 genop (s, MKOP_ABC(OP_LT, cursp(), idx, 1));2099 genop_1(s, OP_LT, cursp()); 1916 2100 } 1917 2101 else if (len == 2 && name[0] == '<' && name[1] == '=') { 1918 genop (s, MKOP_ABC(OP_LE, cursp(), idx, 1));2102 genop_1(s, OP_LE, cursp()); 1919 2103 } 1920 2104 else if (len == 1 && name[0] == '>') { 1921 genop (s, MKOP_ABC(OP_GT, cursp(), idx, 1));2105 genop_1(s, OP_GT, cursp()); 1922 2106 } 1923 2107 else if (len == 2 && name[0] == '>' && name[1] == '=') { 1924 genop (s, MKOP_ABC(OP_GE, cursp(), idx, 1));2108 genop_1(s, OP_GE, cursp()); 1925 2109 } 1926 2110 else { 1927 genop(s, MKOP_ABC(OP_SEND, cursp(), idx, 1)); 2111 idx = new_sym(s, sym); 2112 genop_3(s, OP_SEND, cursp(), idx, 1); 1928 2113 } 1929 2114 if (callargs < 0) { … … 1932 2117 else { 1933 2118 if (val && vsp >= 0) { 1934 gen op(s, MKOP_AB(OP_MOVE, vsp, cursp()));2119 gen_move(s, vsp, cursp(), 0); 1935 2120 } 1936 2121 if (callargs == CALL_MAXARGS) { 1937 2122 pop(); 1938 genop (s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1));2123 genop_1(s, OP_ARYPUSH, cursp()); 1939 2124 } 1940 2125 else { … … 1943 2128 } 1944 2129 pop(); 1945 idx = new_ msym(s, attrsym(s,sym(tree->car->cdr->cdr->car)));1946 genop (s, MKOP_ABC(OP_SEND, cursp(), idx, callargs));2130 idx = new_sym(s, attrsym(s,nsym(tree->car->cdr->cdr->car))); 2131 genop_3(s, OP_SEND, cursp(), idx, callargs); 1947 2132 } 1948 2133 } … … 1961 2146 if (!s2) break; 1962 2147 } 1963 genop (s, MKOP_ABx(OP_ARGARY, cursp(), (lv & 0xf)));2148 genop_2S(s, OP_ARGARY, cursp(), (lv & 0xf)); 1964 2149 push(); push(); /* ARGARY pushes two values */ 1965 2150 pop(); pop(); … … 1979 2164 } 1980 2165 else { 1981 genop (s, MKOP_A(OP_LOADNIL, cursp()));2166 genop_1(s, OP_LOADNIL, cursp()); 1982 2167 push(); pop(); 1983 2168 } 1984 2169 pop_n(n+1); 1985 2170 if (sendv) n = CALL_MAXARGS; 1986 genop (s, MKOP_ABC(OP_SUPER, cursp(), 0, n));2171 genop_2(s, OP_SUPER, cursp(), n); 1987 2172 if (val) push(); 1988 2173 } … … 2001 2186 } 2002 2187 if (s2) ainfo = s2->ainfo; 2003 genop (s, MKOP_ABx(OP_ARGARY, cursp(), (ainfo<<4)|(lv & 0xf)));2188 genop_2S(s, OP_ARGARY, cursp(), (ainfo<<4)|(lv & 0xf)); 2004 2189 push(); push(); pop(); /* ARGARY pushes two values */ 2005 2190 if (tree && tree->cdr) { … … 2008 2193 } 2009 2194 pop(); pop(); 2010 genop (s, MKOP_ABC(OP_SUPER, cursp(), 0, CALL_MAXARGS));2195 genop_2(s, OP_SUPER, cursp(), CALL_MAXARGS); 2011 2196 if (val) push(); 2012 2197 } … … 2018 2203 } 2019 2204 else { 2020 genop (s, MKOP_A(OP_LOADNIL, cursp()));2205 genop_1(s, OP_LOADNIL, cursp()); 2021 2206 } 2022 2207 if (s->loop) { 2023 gen op(s, MKOP_AB(OP_RETURN, cursp(), OP_R_RETURN));2208 gen_return(s, OP_RETURN_BLK, cursp()); 2024 2209 } 2025 2210 else { 2026 gen op_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL);2211 gen_return(s, OP_RETURN, cursp()); 2027 2212 } 2028 2213 if (val) push(); … … 2049 2234 } 2050 2235 } 2236 push();pop(); /* space for a block */ 2051 2237 pop_n(n+1); 2052 genop (s, MKOP_ABx(OP_BLKPUSH, cursp(), (ainfo<<4)|(lv & 0xf)));2238 genop_2S(s, OP_BLKPUSH, cursp(), (ainfo<<4)|(lv & 0xf)); 2053 2239 if (sendv) n = CALL_MAXARGS; 2054 genop (s, MKOP_ABC(OP_SEND, cursp(), new_msym(s, mrb_intern_lit(s->mrb, "call")), n));2240 genop_3(s, OP_SEND, cursp(), new_sym(s, mrb_intern_lit(s->mrb, "call")), n); 2055 2241 if (val) push(); 2056 2242 } … … 2068 2254 else if (s->loop->type == LOOP_NORMAL) { 2069 2255 if (s->ensure_level > s->loop->ensure_level) { 2070 genop_ peep(s, MKOP_A(OP_EPOP, s->ensure_level - s->loop->ensure_level), NOVAL);2256 genop_1(s, OP_EPOP, s->ensure_level - s->loop->ensure_level); 2071 2257 } 2072 2258 codegen(s, tree, NOVAL); 2073 gen op(s, MKOP_sBx(OP_JMP, s->loop->pc1 - s->pc));2259 genjmp(s, OP_JMP, s->loop->pc0); 2074 2260 } 2075 2261 else { … … 2079 2265 } 2080 2266 else { 2081 genop (s, MKOP_A(OP_LOADNIL, cursp()));2082 } 2083 gen op_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL);2267 genop_1(s, OP_LOADNIL, cursp()); 2268 } 2269 gen_return(s, OP_RETURN, cursp()); 2084 2270 } 2085 2271 if (val) push(); … … 2092 2278 else { 2093 2279 if (s->ensure_level > s->loop->ensure_level) { 2094 genop_ peep(s, MKOP_A(OP_EPOP, s->ensure_level - s->loop->ensure_level), NOVAL);2095 } 2096 gen op(s, MKOP_sBx(OP_JMP, s->loop->pc2 - s->pc));2280 genop_1(s, OP_EPOP, s->ensure_level - s->loop->ensure_level); 2281 } 2282 genjmp(s, OP_JMP, s->loop->pc2); 2097 2283 } 2098 2284 if (val) push(); … … 2121 2307 else { 2122 2308 if (n > 0) { 2123 while (n--) { 2124 genop_peep(s, MKOP_A(OP_POPERR, 1), NOVAL); 2125 } 2309 genop_1(s, OP_POPERR, n); 2126 2310 } 2127 2311 if (s->ensure_level > lp->ensure_level) { 2128 genop_ peep(s, MKOP_A(OP_EPOP, s->ensure_level - lp->ensure_level), NOVAL);2129 } 2130 gen op(s, MKOP_sBx(OP_JMP, lp->pc1 - s->pc));2312 genop_1(s, OP_EPOP, s->ensure_level - lp->ensure_level); 2313 } 2314 genjmp(s, OP_JMP, lp->pc0); 2131 2315 } 2132 2316 } … … 2137 2321 case NODE_LVAR: 2138 2322 if (val) { 2139 int idx = lv_idx(s, sym(tree));2323 int idx = lv_idx(s, nsym(tree)); 2140 2324 2141 2325 if (idx > 0) { 2142 genop_peep(s, MKOP_AB(OP_MOVE, cursp(), idx), NOVAL); 2326 gen_move(s, cursp(), idx, val); 2327 if (val && on_eval(s)) genop_0(s, OP_NOP); 2143 2328 } 2144 2329 else { … … 2147 2332 2148 2333 while (up) { 2149 idx = lv_idx(up, sym(tree));2334 idx = lv_idx(up, nsym(tree)); 2150 2335 if (idx > 0) { 2151 genop (s, MKOP_ABC(OP_GETUPVAR, cursp(), idx, lv));2336 genop_3(s, OP_GETUPVAR, cursp(), idx, lv); 2152 2337 break; 2153 2338 } … … 2160 2345 break; 2161 2346 2347 case NODE_NVAR: 2348 if (val) { 2349 int idx = nint(tree); 2350 2351 gen_move(s, cursp(), idx, val); 2352 if (val && on_eval(s)) genop_0(s, OP_NOP); 2353 2354 push(); 2355 } 2356 break; 2357 2162 2358 case NODE_GVAR: 2163 if (val){2164 int sym = new_sym(s, sym(tree));2165 2166 genop (s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym));2167 push();2359 { 2360 int sym = new_sym(s, nsym(tree)); 2361 2362 genop_2(s, OP_GETGV, cursp(), sym); 2363 if (val) push(); 2168 2364 } 2169 2365 break; 2170 2366 2171 2367 case NODE_IVAR: 2172 if (val){2173 int sym = new_sym(s, sym(tree));2174 2175 genop (s, MKOP_ABx(OP_GETIV, cursp(), sym));2176 push();2368 { 2369 int sym = new_sym(s, nsym(tree)); 2370 2371 genop_2(s, OP_GETIV, cursp(), sym); 2372 if (val) push(); 2177 2373 } 2178 2374 break; 2179 2375 2180 2376 case NODE_CVAR: 2181 if (val){2182 int sym = new_sym(s, sym(tree));2183 2184 genop (s, MKOP_ABx(OP_GETCV, cursp(), sym));2185 push();2377 { 2378 int sym = new_sym(s, nsym(tree)); 2379 2380 genop_2(s, OP_GETCV, cursp(), sym); 2381 if (val) push(); 2186 2382 } 2187 2383 break; … … 2189 2385 case NODE_CONST: 2190 2386 { 2191 int sym = new_sym(s, sym(tree)); 2192 2193 genop(s, MKOP_ABx(OP_GETCONST, cursp(), sym)); 2194 if (val) { 2195 push(); 2196 } 2197 } 2198 break; 2199 2200 case NODE_DEFINED: 2201 codegen(s, tree, VAL); 2387 int sym = new_sym(s, nsym(tree)); 2388 2389 genop_2(s, OP_GETCONST, cursp(), sym); 2390 if (val) push(); 2391 } 2202 2392 break; 2203 2393 2204 2394 case NODE_BACK_REF: 2205 2395 if (val) { 2206 char buf[3]; 2207 int sym; 2208 2209 buf[0] = '$'; 2210 buf[1] = (char)(intptr_t)tree; 2211 buf[2] = 0; 2212 sym = new_sym(s, mrb_intern_cstr(s->mrb, buf)); 2213 genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym)); 2396 char buf[] = {'$', nchar(tree)}; 2397 int sym = new_sym(s, mrb_intern(s->mrb, buf, sizeof(buf))); 2398 2399 genop_2(s, OP_GETGV, cursp(), sym); 2214 2400 push(); 2215 2401 } … … 2222 2408 int sym; 2223 2409 2224 str = mrb_format(mrb, "$% S", mrb_fixnum_value((mrb_int)(intptr_t)tree));2410 str = mrb_format(mrb, "$%d", nint(tree)); 2225 2411 sym = new_sym(s, mrb_intern_str(mrb, str)); 2226 genop (s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym));2412 genop_2(s, OP_GETGV, cursp(), sym); 2227 2413 push(); 2228 2414 } … … 2234 2420 2235 2421 case NODE_BLOCK_ARG: 2236 codegen(s, tree, VAL);2422 codegen(s, tree, val); 2237 2423 break; 2238 2424 … … 2240 2426 if (val) { 2241 2427 char *p = (char*)tree->car; 2242 int base = (intptr_t)tree->cdr->car;2428 int base = nint(tree->cdr->car); 2243 2429 mrb_int i; 2244 mrb_code co;2245 2430 mrb_bool overflow; 2246 2431 2247 2432 i = readint_mrb_int(s, p, base, FALSE, &overflow); 2433 #ifndef MRB_WITHOUT_FLOAT 2248 2434 if (overflow) { 2249 2435 double f = readint_float(s, p, base); 2250 2436 int off = new_lit(s, mrb_float_value(s->mrb, f)); 2251 2437 2252 genop(s, MKOP_ABx(OP_LOADL, cursp(), off)); 2253 } 2254 else { 2255 if (i < MAXARG_sBx && i > -MAXARG_sBx) { 2256 co = MKOP_AsBx(OP_LOADI, cursp(), i); 2257 } 2438 genop_2(s, OP_LOADL, cursp(), off); 2439 } 2440 else 2441 #endif 2442 { 2443 if (i == -1) genop_1(s, OP_LOADI__1, cursp()); 2444 else if (i < 0) genop_2(s, OP_LOADINEG, cursp(), (uint16_t)-i); 2445 else if (i < 8) genop_1(s, OP_LOADI_0 + (uint8_t)i, cursp()); 2446 else if (i <= 0xffff) genop_2(s, OP_LOADI, cursp(), (uint16_t)i); 2258 2447 else { 2259 2448 int off = new_lit(s, mrb_fixnum_value(i)); 2260 co = MKOP_ABx(OP_LOADL, cursp(), off); 2261 } 2262 genop(s, co); 2449 genop_2(s, OP_LOADL, cursp(), off); 2450 } 2263 2451 } 2264 2452 push(); … … 2266 2454 break; 2267 2455 2456 #ifndef MRB_WITHOUT_FLOAT 2268 2457 case NODE_FLOAT: 2269 2458 if (val) { … … 2272 2461 int off = new_lit(s, mrb_float_value(s->mrb, f)); 2273 2462 2274 genop (s, MKOP_ABx(OP_LOADL, cursp(), off));2463 genop_2(s, OP_LOADL, cursp(), off); 2275 2464 push(); 2276 2465 } 2277 2466 break; 2467 #endif 2278 2468 2279 2469 case NODE_NEGATE: 2280 2470 { 2281 nt = (intptr_t)tree->car; 2282 tree = tree->cdr; 2471 nt = nint(tree->car); 2283 2472 switch (nt) { 2473 #ifndef MRB_WITHOUT_FLOAT 2284 2474 case NODE_FLOAT: 2285 2475 if (val) { 2286 char *p = (char*)tree ;2476 char *p = (char*)tree->cdr; 2287 2477 mrb_float f = mrb_float_read(p, NULL); 2288 2478 int off = new_lit(s, mrb_float_value(s->mrb, -f)); 2289 2479 2290 genop (s, MKOP_ABx(OP_LOADL, cursp(), off));2480 genop_2(s, OP_LOADL, cursp(), off); 2291 2481 push(); 2292 2482 } 2293 2483 break; 2484 #endif 2294 2485 2295 2486 case NODE_INT: 2296 2487 if (val) { 2297 char *p = (char*)tree->c ar;2298 int base = (intptr_t)tree->cdr->car;2488 char *p = (char*)tree->cdr->car; 2489 int base = nint(tree->cdr->cdr->car); 2299 2490 mrb_int i; 2300 mrb_code co;2301 2491 mrb_bool overflow; 2302 2492 2303 2493 i = readint_mrb_int(s, p, base, TRUE, &overflow); 2494 #ifndef MRB_WITHOUT_FLOAT 2304 2495 if (overflow) { 2305 2496 double f = readint_float(s, p, base); 2306 2497 int off = new_lit(s, mrb_float_value(s->mrb, -f)); 2307 2498 2308 genop (s, MKOP_ABx(OP_LOADL, cursp(), off));2499 genop_2(s, OP_LOADL, cursp(), off); 2309 2500 } 2310 2501 else { 2311 if (i < MAXARG_sBx && i > -MAXARG_sBx) { 2312 co = MKOP_AsBx(OP_LOADI, cursp(), i); 2502 #endif 2503 if (i == -1) genop_1(s, OP_LOADI__1, cursp()); 2504 else if (i >= -0xffff) { 2505 genop_2(s, OP_LOADINEG, cursp(), (uint16_t)-i); 2313 2506 } 2314 2507 else { 2315 2508 int off = new_lit(s, mrb_fixnum_value(i)); 2316 co = MKOP_ABx(OP_LOADL, cursp(), off);2509 genop_2(s, OP_LOADL, cursp(), off); 2317 2510 } 2318 genop(s, co); 2319 } 2511 #ifndef MRB_WITHOUT_FLOAT 2512 } 2513 #endif 2320 2514 push(); 2321 2515 } … … 2324 2518 default: 2325 2519 if (val) { 2326 int sym = new_msym(s, mrb_intern_lit(s->mrb, "-")); 2327 2328 genop(s, MKOP_ABx(OP_LOADI, cursp(), 0)); 2520 int sym = new_sym(s, mrb_intern_lit(s->mrb, "-@")); 2521 codegen(s, tree, VAL); 2522 pop(); 2523 genop_3(s, OP_SEND, cursp(), sym, 0); 2329 2524 push(); 2330 codegen(s, tree, VAL);2331 pop(); pop();2332 genop(s, MKOP_ABC(OP_SUB, cursp(), sym, 2));2333 2525 } 2334 2526 else { … … 2348 2540 2349 2541 mrb_gc_arena_restore(s->mrb, ai); 2350 genop (s, MKOP_ABx(OP_STRING, cursp(), off));2542 genop_2(s, OP_STRING, cursp(), off); 2351 2543 push(); 2352 2544 } … … 2361 2553 2362 2554 if (!n) { 2363 genop (s, MKOP_A(OP_LOADNIL, cursp()));2555 genop_1(s, OP_LOADNIL, cursp()); 2364 2556 push(); 2365 2557 break; … … 2370 2562 codegen(s, n->car, VAL); 2371 2563 pop(); pop(); 2372 genop_ peep(s, MKOP_AB(OP_STRCAT, cursp(), cursp()+1), VAL);2564 genop_1(s, OP_STRCAT, cursp()); 2373 2565 push(); 2374 2566 n = n->cdr; … … 2379 2571 2380 2572 while (n) { 2381 if ( (intptr_t)n->car->car!= NODE_STR) {2573 if (nint(n->car->car) != NODE_STR) { 2382 2574 codegen(s, n->car, NOVAL); 2383 2575 } … … 2401 2593 int sym = new_sym(s, mrb_intern_lit(s->mrb, "Kernel")); 2402 2594 2403 genop (s, MKOP_A(OP_LOADSELF, cursp()));2595 genop_1(s, OP_LOADSELF, cursp()); 2404 2596 push(); 2405 2597 codegen(s, tree->car, VAL); 2406 2598 n = tree->cdr; 2407 2599 while (n) { 2408 if ( (intptr_t)n->car->car== NODE_XSTR) {2600 if (nint(n->car->car) == NODE_XSTR) { 2409 2601 n->car->car = (struct mrb_ast_node*)(intptr_t)NODE_STR; 2410 2602 mrb_assert(!n->cdr); /* must be the end */ … … 2412 2604 codegen(s, n->car, VAL); 2413 2605 pop(); pop(); 2414 genop_ peep(s, MKOP_AB(OP_STRCAT, cursp(), cursp()+1), VAL);2606 genop_1(s, OP_STRCAT, cursp()); 2415 2607 push(); 2416 2608 n = n->cdr; … … 2419 2611 pop_n(3); 2420 2612 sym = new_sym(s, mrb_intern_lit(s->mrb, "`")); 2421 genop (s, MKOP_ABC(OP_SEND, cursp(), sym, 1));2613 genop_3(s, OP_SEND, cursp(), sym, 1); 2422 2614 if (val) push(); 2423 2615 mrb_gc_arena_restore(s->mrb, ai); … … 2433 2625 int sym; 2434 2626 2435 genop (s, MKOP_A(OP_LOADSELF, cursp()));2627 genop_1(s, OP_LOADSELF, cursp()); 2436 2628 push(); 2437 genop (s, MKOP_ABx(OP_STRING, cursp(), off));2629 genop_2(s, OP_STRING, cursp(), off); 2438 2630 push(); push(); 2439 2631 pop_n(3); 2440 2632 sym = new_sym(s, mrb_intern_lit(s->mrb, "`")); 2441 genop (s, MKOP_ABC(OP_SEND, cursp(), sym, 1));2633 genop_3(s, OP_SEND, cursp(), sym, 1); 2442 2634 if (val) push(); 2443 2635 mrb_gc_arena_restore(s->mrb, ai); … … 2455 2647 int argc = 1; 2456 2648 2457 genop (s, MKOP_A(OP_OCLASS, cursp()));2458 genop (s, MKOP_ABx(OP_GETMCNST, cursp(), sym));2649 genop_1(s, OP_OCLASS, cursp()); 2650 genop_2(s, OP_GETMCNST, cursp(), sym); 2459 2651 push(); 2460 genop(s, MKOP_ABx(OP_STRING, cursp(), off)); 2652 genop_2(s, OP_STRING, cursp(), off); 2653 push(); 2461 2654 if (p2 || p3) { 2655 if (p2) { /* opt */ 2656 off = new_lit(s, mrb_str_new_cstr(s->mrb, p2)); 2657 genop_2(s, OP_STRING, cursp(), off); 2658 } 2659 else { 2660 genop_1(s, OP_LOADNIL, cursp()); 2661 } 2462 2662 push(); 2463 if (p2) {2464 off = new_lit(s, mrb_str_new_cstr(s->mrb, p2));2465 genop(s, MKOP_ABx(OP_STRING, cursp(), off));2466 }2467 else {2468 genop(s, MKOP_A(OP_LOADNIL, cursp()));2469 }2470 2663 argc++; 2471 if (p3) { 2664 if (p3) { /* enc */ 2665 off = new_lit(s, mrb_str_new(s->mrb, p3, 1)); 2666 genop_2(s, OP_STRING, cursp(), off); 2472 2667 push(); 2473 off = new_lit(s, mrb_str_new(s->mrb, p3, 1));2474 genop(s, MKOP_ABx(OP_STRING, cursp(), off));2475 2668 argc++; 2476 pop(); 2477 } 2478 pop(); 2479 } 2480 pop(); 2669 } 2670 } 2671 push(); /* space for a block */ 2672 pop_n(argc+2); 2481 2673 sym = new_sym(s, mrb_intern_lit(s->mrb, "compile")); 2482 genop (s, MKOP_ABC(OP_SEND, cursp(), sym, argc));2674 genop_3(s, OP_SEND, cursp(), sym, argc); 2483 2675 mrb_gc_arena_restore(s->mrb, ai); 2484 2676 push(); … … 2495 2687 char *p; 2496 2688 2497 genop (s, MKOP_A(OP_OCLASS, cursp()));2498 genop (s, MKOP_ABx(OP_GETMCNST, cursp(), sym));2689 genop_1(s, OP_OCLASS, cursp()); 2690 genop_2(s, OP_GETMCNST, cursp(), sym); 2499 2691 push(); 2500 2692 codegen(s, n->car, VAL); … … 2503 2695 codegen(s, n->car, VAL); 2504 2696 pop(); pop(); 2505 genop_ peep(s, MKOP_AB(OP_STRCAT, cursp(), cursp()+1), VAL);2697 genop_1(s, OP_STRCAT, cursp()); 2506 2698 push(); 2507 2699 n = n->cdr; 2508 2700 } 2509 2701 n = tree->cdr->cdr; 2510 if (n->car) { 2702 if (n->car) { /* tail */ 2511 2703 p = (char*)n->car; 2512 2704 off = new_lit(s, mrb_str_new_cstr(s->mrb, p)); 2513 2705 codegen(s, tree->car, VAL); 2514 genop (s, MKOP_ABx(OP_STRING, cursp(), off));2706 genop_2(s, OP_STRING, cursp(), off); 2515 2707 pop(); 2516 genop_peep(s, MKOP_AB(OP_STRCAT, cursp(), cursp()+1), VAL); 2517 } 2518 if (n->cdr->car) { 2708 genop_1(s, OP_STRCAT, cursp()); 2709 push(); 2710 } 2711 if (n->cdr->car) { /* opt */ 2519 2712 char *p2 = (char*)n->cdr->car; 2520 2713 off = new_lit(s, mrb_str_new_cstr(s->mrb, p2)); 2714 genop_2(s, OP_STRING, cursp(), off); 2521 2715 push(); 2716 argc++; 2717 } 2718 if (n->cdr->cdr) { /* enc */ 2719 char *p2 = (char*)n->cdr->cdr; 2522 2720 off = new_lit(s, mrb_str_new_cstr(s->mrb, p2)); 2523 genop(s, MKOP_ABx(OP_STRING, cursp(), off)); 2721 genop_2(s, OP_STRING, cursp(), off); 2722 push(); 2524 2723 argc++; 2525 2724 } 2526 if (n->cdr->cdr) { 2527 char *p2 = (char*)n->cdr->cdr; 2528 2529 push(); 2530 off = new_lit(s, mrb_str_new_cstr(s->mrb, p2)); 2531 genop(s, MKOP_ABx(OP_STRING, cursp(), off)); 2532 argc++; 2533 } 2534 pop_n(argc); 2725 push(); /* space for a block */ 2726 pop_n(argc+2); 2535 2727 sym = new_sym(s, mrb_intern_lit(s->mrb, "compile")); 2536 genop (s, MKOP_ABC(OP_SEND, cursp(), sym, argc));2728 genop_3(s, OP_SEND, cursp(), sym, argc); 2537 2729 mrb_gc_arena_restore(s->mrb, ai); 2538 2730 push(); … … 2542 2734 2543 2735 while (n) { 2544 if ( (intptr_t)n->car->car!= NODE_STR) {2736 if (nint(n->car->car) != NODE_STR) { 2545 2737 codegen(s, n->car, NOVAL); 2546 2738 } … … 2552 2744 case NODE_SYM: 2553 2745 if (val) { 2554 int sym = new_sym(s, sym(tree));2555 2556 genop (s, MKOP_ABx(OP_LOADSYM, cursp(), sym));2746 int sym = new_sym(s, nsym(tree)); 2747 2748 genop_2(s, OP_LOADSYM, cursp(), sym); 2557 2749 push(); 2558 2750 } … … 2562 2754 codegen(s, tree, val); 2563 2755 if (val) { 2564 gen_ send_intern(s);2756 gen_intern(s); 2565 2757 } 2566 2758 break; … … 2568 2760 case NODE_SELF: 2569 2761 if (val) { 2570 genop (s, MKOP_A(OP_LOADSELF, cursp()));2762 genop_1(s, OP_LOADSELF, cursp()); 2571 2763 push(); 2572 2764 } … … 2575 2767 case NODE_NIL: 2576 2768 if (val) { 2577 genop (s, MKOP_A(OP_LOADNIL, cursp()));2769 genop_1(s, OP_LOADNIL, cursp()); 2578 2770 push(); 2579 2771 } … … 2582 2774 case NODE_TRUE: 2583 2775 if (val) { 2584 genop (s, MKOP_A(OP_LOADT, cursp()));2776 genop_1(s, OP_LOADT, cursp()); 2585 2777 push(); 2586 2778 } … … 2589 2781 case NODE_FALSE: 2590 2782 if (val) { 2591 genop (s, MKOP_A(OP_LOADF, cursp()));2783 genop_1(s, OP_LOADF, cursp()); 2592 2784 push(); 2593 2785 } … … 2596 2788 case NODE_ALIAS: 2597 2789 { 2598 int a = new_msym(s, sym(tree->car)); 2599 int b = new_msym(s, sym(tree->cdr)); 2600 int c = new_msym(s, mrb_intern_lit(s->mrb, "alias_method")); 2601 2602 genop(s, MKOP_A(OP_TCLASS, cursp())); 2603 push(); 2604 genop(s, MKOP_ABx(OP_LOADSYM, cursp(), a)); 2605 push(); 2606 genop(s, MKOP_ABx(OP_LOADSYM, cursp(), b)); 2607 push(); 2608 genop(s, MKOP_A(OP_LOADNIL, cursp())); 2609 push(); 2610 pop_n(4); 2611 genop(s, MKOP_ABC(OP_SEND, cursp(), c, 2)); 2790 int a = new_sym(s, nsym(tree->car)); 2791 int b = new_sym(s, nsym(tree->cdr)); 2792 2793 genop_2(s, OP_ALIAS, a, b); 2612 2794 if (val) { 2795 genop_1(s, OP_LOADNIL, cursp()); 2613 2796 push(); 2614 2797 } … … 2618 2801 case NODE_UNDEF: 2619 2802 { 2620 int undef = new_msym(s, mrb_intern_lit(s->mrb, "undef_method"));2621 int num = 0;2622 2803 node *t = tree; 2623 2804 2624 genop(s, MKOP_A(OP_TCLASS, cursp()));2625 push();2626 2805 while (t) { 2627 int symbol; 2628 if (num >= CALL_MAXARGS - 1) { 2629 pop_n(num); 2630 genop(s, MKOP_ABC(OP_ARRAY, cursp(), cursp(), num)); 2631 while (t) { 2632 symbol = new_msym(s, sym(t->car)); 2633 push(); 2634 genop(s, MKOP_ABx(OP_LOADSYM, cursp(), symbol)); 2635 pop(); 2636 genop(s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1)); 2637 t = t->cdr; 2638 } 2639 num = CALL_MAXARGS; 2640 break; 2641 } 2642 symbol = new_msym(s, sym(t->car)); 2643 genop(s, MKOP_ABx(OP_LOADSYM, cursp(), symbol)); 2644 push(); 2806 int symbol = new_sym(s, nsym(t->car)); 2807 genop_1(s, OP_UNDEF, symbol); 2645 2808 t = t->cdr; 2646 num++; 2647 } 2648 pop(); 2649 if (num < CALL_MAXARGS) { 2650 pop_n(num); 2651 } 2652 genop(s, MKOP_ABC(OP_SEND, cursp(), undef, num)); 2809 } 2653 2810 if (val) { 2811 genop_1(s, OP_LOADNIL, cursp()); 2654 2812 push(); 2655 2813 } … … 2660 2818 { 2661 2819 int idx; 2820 node *body; 2662 2821 2663 2822 if (tree->car->car == (node*)0) { 2664 genop (s, MKOP_A(OP_LOADNIL, cursp()));2823 genop_1(s, OP_LOADNIL, cursp()); 2665 2824 push(); 2666 2825 } 2667 2826 else if (tree->car->car == (node*)1) { 2668 genop (s, MKOP_A(OP_OCLASS, cursp()));2827 genop_1(s, OP_OCLASS, cursp()); 2669 2828 push(); 2670 2829 } … … 2676 2835 } 2677 2836 else { 2678 genop (s, MKOP_A(OP_LOADNIL, cursp()));2837 genop_1(s, OP_LOADNIL, cursp()); 2679 2838 push(); 2680 2839 } 2681 2840 pop(); pop(); 2682 idx = new_msym(s, sym(tree->car->cdr)); 2683 genop(s, MKOP_AB(OP_CLASS, cursp(), idx)); 2684 idx = scope_body(s, tree->cdr->cdr->car, val); 2685 genop(s, MKOP_ABx(OP_EXEC, cursp(), idx)); 2841 idx = new_sym(s, nsym(tree->car->cdr)); 2842 genop_2(s, OP_CLASS, cursp(), idx); 2843 body = tree->cdr->cdr->car; 2844 if (nint(body->cdr->car) == NODE_BEGIN && body->cdr->cdr == NULL) { 2845 genop_1(s, OP_LOADNIL, cursp()); 2846 } 2847 else { 2848 idx = scope_body(s, body, val); 2849 genop_2(s, OP_EXEC, cursp(), idx); 2850 } 2686 2851 if (val) { 2687 2852 push(); … … 2695 2860 2696 2861 if (tree->car->car == (node*)0) { 2697 genop (s, MKOP_A(OP_LOADNIL, cursp()));2862 genop_1(s, OP_LOADNIL, cursp()); 2698 2863 push(); 2699 2864 } 2700 2865 else if (tree->car->car == (node*)1) { 2701 genop (s, MKOP_A(OP_OCLASS, cursp()));2866 genop_1(s, OP_OCLASS, cursp()); 2702 2867 push(); 2703 2868 } … … 2706 2871 } 2707 2872 pop(); 2708 idx = new_msym(s, sym(tree->car->cdr)); 2709 genop(s, MKOP_AB(OP_MODULE, cursp(), idx)); 2710 idx = scope_body(s, tree->cdr->car, val); 2711 genop(s, MKOP_ABx(OP_EXEC, cursp(), idx)); 2873 idx = new_sym(s, nsym(tree->car->cdr)); 2874 genop_2(s, OP_MODULE, cursp(), idx); 2875 if (nint(tree->cdr->car->cdr->car) == NODE_BEGIN && 2876 tree->cdr->car->cdr->cdr == NULL) { 2877 genop_1(s, OP_LOADNIL, cursp()); 2878 } 2879 else { 2880 idx = scope_body(s, tree->cdr->car, val); 2881 genop_2(s, OP_EXEC, cursp(), idx); 2882 } 2712 2883 if (val) { 2713 2884 push(); … … 2722 2893 codegen(s, tree->car, VAL); 2723 2894 pop(); 2724 genop(s, MKOP_AB(OP_SCLASS, cursp(), cursp())); 2725 idx = scope_body(s, tree->cdr->car, val); 2726 genop(s, MKOP_ABx(OP_EXEC, cursp(), idx)); 2895 genop_1(s, OP_SCLASS, cursp()); 2896 if (nint(tree->cdr->car->cdr->car) == NODE_BEGIN && 2897 tree->cdr->car->cdr->cdr == NULL) { 2898 genop_1(s, OP_LOADNIL, cursp()); 2899 } 2900 else { 2901 idx = scope_body(s, tree->cdr->car, val); 2902 genop_2(s, OP_EXEC, cursp(), idx); 2903 } 2727 2904 if (val) { 2728 2905 push(); … … 2733 2910 case NODE_DEF: 2734 2911 { 2735 int sym = new_ msym(s,sym(tree->car));2912 int sym = new_sym(s, nsym(tree->car)); 2736 2913 int idx = lambda_body(s, tree->cdr, 0); 2737 2914 2738 genop (s, MKOP_A(OP_TCLASS, cursp()));2915 genop_1(s, OP_TCLASS, cursp()); 2739 2916 push(); 2740 genop (s, MKOP_Abc(OP_LAMBDA, cursp(), idx, OP_L_METHOD));2917 genop_2(s, OP_METHOD, cursp(), idx); 2741 2918 push(); pop(); 2742 2919 pop(); 2743 genop (s, MKOP_AB(OP_METHOD, cursp(), sym));2920 genop_2(s, OP_DEF, cursp(), sym); 2744 2921 if (val) { 2745 genop (s, MKOP_ABx(OP_LOADSYM, cursp(), sym));2922 genop_2(s, OP_LOADSYM, cursp(), sym); 2746 2923 push(); 2747 2924 } … … 2752 2929 { 2753 2930 node *recv = tree->car; 2754 int sym = new_ msym(s,sym(tree->cdr->car));2931 int sym = new_sym(s, nsym(tree->cdr->car)); 2755 2932 int idx = lambda_body(s, tree->cdr->cdr, 0); 2756 2933 2757 2934 codegen(s, recv, VAL); 2758 2935 pop(); 2759 genop (s, MKOP_AB(OP_SCLASS, cursp(), cursp()));2936 genop_1(s, OP_SCLASS, cursp()); 2760 2937 push(); 2761 genop (s, MKOP_Abc(OP_LAMBDA, cursp(), idx, OP_L_METHOD));2938 genop_2(s, OP_METHOD, cursp(), idx); 2762 2939 pop(); 2763 genop (s, MKOP_AB(OP_METHOD, cursp(), sym));2940 genop_2(s, OP_DEF, cursp(), sym); 2764 2941 if (val) { 2765 genop (s, MKOP_ABx(OP_LOADSYM, cursp(), sym));2942 genop_2(s, OP_LOADSYM, cursp(), sym); 2766 2943 push(); 2767 2944 } … … 2802 2979 codegen_scope *p = (codegen_scope *)mrb_pool_alloc(pool, sizeof(codegen_scope)); 2803 2980 2804 if (!p) return NULL; 2981 if (!p) { 2982 if (prev) 2983 codegen_error(prev, "unexpected scope"); 2984 return NULL; 2985 } 2805 2986 *p = codegen_scope_zero; 2806 2987 p->mrb = mrb; … … 2825 3006 p->irep->plen = 0; 2826 3007 2827 p->scapa = MAXMSYMLEN;3008 p->scapa = 256; 2828 3009 p->irep->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*p->scapa); 2829 3010 p->irep->slen = 0; … … 2850 3031 p->ai = mrb_gc_arena_save(mrb); 2851 3032 2852 p->filename = prev->filename;2853 if (p->filename ) {3033 p->filename_sym = prev->filename_sym; 3034 if (p->filename_sym) { 2854 3035 p->lines = (uint16_t*)mrb_malloc(mrb, sizeof(short)*p->icapa); 2855 3036 } … … 2858 3039 /* debug setting */ 2859 3040 p->debug_start_pos = 0; 2860 if (p->filename ) {3041 if (p->filename_sym) { 2861 3042 mrb_debug_info_alloc(mrb, p->irep); 2862 p->irep->filename = p->filename;2863 p->irep->lines = p->lines;2864 3043 } 2865 3044 else { … … 2879 3058 mrb_state *mrb = s->mrb; 2880 3059 mrb_irep *irep = s->irep; 2881 size_t fname_len; 2882 char *fname; 2883 3060 3061 if (s->nlocals >= 0x3ff) { 3062 codegen_error(s, "too many local variables"); 3063 } 2884 3064 irep->flags = 0; 2885 3065 if (s->iseq) { 2886 3066 irep->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc); 2887 3067 irep->ilen = s->pc; 2888 if (s->lines) {2889 irep->lines = (uint16_t *)codegen_realloc(s, s->lines, sizeof(uint16_t)*s->pc);2890 }2891 else {2892 irep->lines = 0;2893 }2894 3068 } 2895 3069 irep->pool = (mrb_value*)codegen_realloc(s, irep->pool, sizeof(mrb_value)*irep->plen); 2896 3070 irep->syms = (mrb_sym*)codegen_realloc(s, irep->syms, sizeof(mrb_sym)*irep->slen); 2897 3071 irep->reps = (mrb_irep**)codegen_realloc(s, irep->reps, sizeof(mrb_irep*)*irep->rlen); 2898 if (s->filename) { 2899 irep->filename = mrb_parser_get_filename(s->parser, s->filename_index); 2900 mrb_debug_info_append_file(mrb, irep, s->debug_start_pos, s->pc); 2901 2902 fname_len = strlen(s->filename); 2903 fname = (char*)codegen_malloc(s, fname_len + 1); 2904 memcpy(fname, s->filename, fname_len); 2905 fname[fname_len] = '\0'; 2906 irep->filename = fname; 2907 irep->own_filename = TRUE; 2908 } 3072 if (s->filename_sym) { 3073 mrb_sym fname = mrb_parser_get_filename(s->parser, s->filename_index); 3074 const char *filename = mrb_sym_name_len(s->mrb, fname, NULL); 3075 3076 mrb_debug_info_append_file(s->mrb, s->irep->debug_info, 3077 filename, s->lines, s->debug_start_pos, s->pc); 3078 } 3079 mrb_free(s->mrb, s->lines); 2909 3080 2910 3081 irep->nlocals = s->nlocals; … … 2921 3092 2922 3093 p->type = t; 2923 p->pc 1 = p->pc2 = p->pc3 = 0;3094 p->pc0 = p->pc1 = p->pc2 = p->pc3 = 0; 2924 3095 p->prev = s->loop; 2925 3096 p->ensure_level = s->ensure_level; … … 2939 3110 else { 2940 3111 struct loopinfo *loop; 3112 int n = 0; 2941 3113 2942 3114 if (tree) { … … 2945 3117 2946 3118 loop = s->loop; 2947 while (loop && loop->type == LOOP_BEGIN) { 2948 genop_peep(s, MKOP_A(OP_POPERR, 1), NOVAL); 2949 loop = loop->prev; 2950 } 2951 while (loop && loop->type == LOOP_RESCUE) { 2952 loop = loop->prev; 3119 while (loop) { 3120 if (loop->type == LOOP_BEGIN) { 3121 n++; 3122 loop = loop->prev; 3123 } 3124 else if (loop->type == LOOP_RESCUE) { 3125 loop = loop->prev; 3126 } 3127 else{ 3128 break; 3129 } 2953 3130 } 2954 3131 if (!loop) { … … 2956 3133 return; 2957 3134 } 3135 if (n > 0) { 3136 genop_1(s, OP_POPERR, n); 3137 } 2958 3138 2959 3139 if (loop->type == LOOP_NORMAL) { … … 2961 3141 2962 3142 if (s->ensure_level > s->loop->ensure_level) { 2963 genop_ peep(s, MKOP_A(OP_EPOP, s->ensure_level - s->loop->ensure_level), NOVAL);3143 genop_1(s, OP_EPOP, s->ensure_level - s->loop->ensure_level); 2964 3144 } 2965 3145 if (tree) { 2966 gen op_peep(s, MKOP_AB(OP_MOVE, loop->acc, cursp()), NOVAL);2967 } 2968 tmp = gen op(s, MKOP_sBx(OP_JMP, loop->pc3));3146 gen_move(s, loop->acc, cursp(), 0); 3147 } 3148 tmp = genjmp(s, OP_JMP, loop->pc3); 2969 3149 loop->pc3 = tmp; 2970 3150 } 2971 3151 else { 2972 3152 if (!tree) { 2973 genop (s, MKOP_A(OP_LOADNIL, cursp()));2974 } 2975 gen op(s, MKOP_AB(OP_RETURN, cursp(), OP_R_BREAK));3153 genop_1(s, OP_LOADNIL, cursp()); 3154 } 3155 gen_return(s, OP_BREAK, cursp()); 2976 3156 } 2977 3157 } … … 2981 3161 loop_pop(codegen_scope *s, int val) 2982 3162 { 3163 if (val) { 3164 genop_1(s, OP_LOADNIL, cursp()); 3165 } 2983 3166 dispatch_linked(s, s->loop->pc3); 2984 if (val) {2985 genop(s, MKOP_A(OP_LOADNIL, cursp()));2986 }2987 3167 s->loop = s->loop->prev; 2988 3168 if (val) push(); 2989 3169 } 2990 3170 2991 MRB_APIstruct RProc*2992 mrb_generate_code(mrb_state *mrb, parser_state *p)3171 static struct RProc* 3172 generate_code(mrb_state *mrb, parser_state *p, int val) 2993 3173 { 2994 3174 codegen_scope *scope = scope_new(mrb, 0, 0); … … 2996 3176 struct mrb_jmpbuf *prev_jmp = mrb->jmp; 2997 3177 2998 if (!scope) {2999 return NULL;3000 }3001 3178 scope->mrb = mrb; 3002 3179 scope->parser = p; 3003 scope->filename = p->filename;3180 scope->filename_sym = p->filename_sym; 3004 3181 scope->filename_index = p->current_filename_index; 3005 3182 3006 3183 MRB_TRY(&scope->jmp) { 3007 mrb->jmp = &scope->jmp; 3184 mrb->jmp = &scope->jmp; 3008 3185 /* prepare irep */ 3009 codegen(scope, p->tree, NOVAL);3186 codegen(scope, p->tree, val); 3010 3187 proc = mrb_proc_new(mrb, scope->irep); 3011 3188 mrb_irep_decref(mrb, scope->irep); 3012 3189 mrb_pool_close(scope->mpool); 3013 3190 proc->c = NULL; 3191 if (mrb->c->cibase && mrb->c->cibase->proc == proc->upper) { 3192 proc->upper = NULL; 3193 } 3014 3194 mrb->jmp = prev_jmp; 3015 3195 return proc; … … 3023 3203 MRB_END_EXC(&scope->jmp); 3024 3204 } 3205 3206 MRB_API struct RProc* 3207 mrb_generate_code(mrb_state *mrb, parser_state *p) 3208 { 3209 return generate_code(mrb, p, VAL); 3210 } 3211 3212 void 3213 mrb_irep_remove_lv(mrb_state *mrb, mrb_irep *irep) 3214 { 3215 int i; 3216 3217 if (irep->lv) { 3218 mrb_free(mrb, irep->lv); 3219 irep->lv = NULL; 3220 } 3221 3222 for (i = 0; i < irep->rlen; ++i) { 3223 mrb_irep_remove_lv(mrb, irep->reps[i]); 3224 } 3225 } 3226 3227 #undef OPCODE 3228 #define Z 1 3229 #define S 3 3230 #define W 4 3231 /* instruction sizes */ 3232 uint8_t mrb_insn_size[] = { 3233 #define B 2 3234 #define BB 3 3235 #define BBB 4 3236 #define BS 4 3237 #define SB 4 3238 #define OPCODE(_,x) x, 3239 #include "mruby/ops.h" 3240 #undef OPCODE 3241 #undef B 3242 #undef BB 3243 #undef BS 3244 #undef SB 3245 #undef BBB 3246 }; 3247 /* EXT1 instruction sizes */ 3248 uint8_t mrb_insn_size1[] = { 3249 #define B 3 3250 #define BB 4 3251 #define BBB 5 3252 #define BS 5 3253 #define SB 5 3254 #define OPCODE(_,x) x, 3255 #include "mruby/ops.h" 3256 #undef OPCODE 3257 #undef B 3258 }; 3259 /* EXT2 instruction sizes */ 3260 uint8_t mrb_insn_size2[] = { 3261 #define B 2 3262 #define OPCODE(_,x) x, 3263 #include "mruby/ops.h" 3264 #undef OPCODE 3265 #undef BB 3266 #undef BBB 3267 #undef BS 3268 #undef SB 3269 }; 3270 /* EXT3 instruction sizes */ 3271 #define BB 5 3272 #define BBB 6 3273 #define BS 4 3274 #define SB 5 3275 uint8_t mrb_insn_size3[] = { 3276 #define OPCODE(_,x) x, 3277 #include "mruby/ops.h" 3278 }; -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler/core/keywords
r270 r439 1 1 %{ 2 2 struct kwtable {const char *name; int id[2]; enum mrb_lex_state_enum state;}; 3 const struct kwtable *mrb_reserved_word(const char *, unsigned int);4 static const struct kwtable *reserved_word(const char *, unsigned int);5 #define mrb_reserved_word(str, len) reserved_word(str, len)6 3 %} 7 4 -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler/core/lex.def
r331 r439 1 /* ANSI-C code produced by gperf version 3. 0.4*/1 /* ANSI-C code produced by gperf version 3.1 */ 2 2 /* Command-line: gperf -L ANSI-C -C -p -j1 -i 1 -g -o -t -N mrb_reserved_word -k'1,3,$' /home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords */ 3 3 … … 26 26 && ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126)) 27 27 /* The character set is not based on ISO-646. */ 28 #error "gperf generated tables don't work with this execution character set. Please report a bug to <bug-g nu-gperf@gnu.org>."28 #error "gperf generated tables don't work with this execution character set. Please report a bug to <bug-gperf@gnu.org>." 29 29 #endif 30 30 … … 32 32 33 33 struct kwtable {const char *name; int id[2]; enum mrb_lex_state_enum state;}; 34 const struct kwtable *mrb_reserved_word(const char *, unsigned int); 35 static const struct kwtable *reserved_word(const char *, unsigned int); 36 #define mrb_reserved_word(str, len) reserved_word(str, len) 37 #line 8 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 34 #line 5 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 38 35 struct kwtable; 39 36 … … 53 50 #endif 54 51 static unsigned int 55 hash (register const char *str, register unsigned int len)52 hash (register const char *str, register size_t len) 56 53 { 57 54 static const unsigned char asso_values[] = … … 84 81 51, 51, 51, 51, 51, 51 85 82 }; 86 register int hval = len;83 register unsigned int hval = len; 87 84 88 85 switch (hval) … … 99 96 } 100 97 101 #ifdef __GNUC__102 __inline103 #if defined __GNUC_STDC_INLINE__ || defined __GNUC_GNU_INLINE__104 __attribute__ ((__gnu_inline__))105 #endif106 #endif107 98 const struct kwtable * 108 mrb_reserved_word (register const char *str, register unsigned int len)99 mrb_reserved_word (register const char *str, register size_t len) 109 100 { 110 101 static const struct kwtable wordlist[] = 111 102 { 112 103 {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, 104 #line 15 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 105 {"break", {keyword_break, keyword_break}, EXPR_MID}, 106 #line 20 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 107 {"else", {keyword_else, keyword_else}, EXPR_BEG}, 108 #line 30 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 109 {"nil", {keyword_nil, keyword_nil}, EXPR_END}, 110 #line 23 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 111 {"ensure", {keyword_ensure, keyword_ensure}, EXPR_BEG}, 112 #line 22 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 113 {"end", {keyword_end, keyword_end}, EXPR_END}, 114 #line 39 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 115 {"then", {keyword_then, keyword_then}, EXPR_BEG}, 116 #line 31 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 117 {"not", {keyword_not, keyword_not}, EXPR_ARG}, 118 #line 24 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 119 {"false", {keyword_false, keyword_false}, EXPR_END}, 120 #line 37 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 121 {"self", {keyword_self, keyword_self}, EXPR_END}, 122 #line 21 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 123 {"elsif", {keyword_elsif, keyword_elsif}, EXPR_VALUE}, 124 #line 34 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 125 {"rescue", {keyword_rescue, modifier_rescue}, EXPR_MID}, 126 #line 40 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 127 {"true", {keyword_true, keyword_true}, EXPR_END}, 128 #line 43 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 129 {"until", {keyword_until, modifier_until}, EXPR_VALUE}, 130 #line 42 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 131 {"unless", {keyword_unless, modifier_unless}, EXPR_VALUE}, 132 #line 36 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 133 {"return", {keyword_return, keyword_return}, EXPR_MID}, 113 134 #line 18 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 114 {"break", {keyword_break, keyword_break}, EXPR_MID}, 115 #line 23 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 116 {"else", {keyword_else, keyword_else}, EXPR_BEG}, 135 {"def", {keyword_def, keyword_def}, EXPR_FNAME}, 136 #line 13 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 137 {"and", {keyword_and, keyword_and}, EXPR_VALUE}, 138 #line 19 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 139 {"do", {keyword_do, keyword_do}, EXPR_BEG}, 140 #line 46 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 141 {"yield", {keyword_yield, keyword_yield}, EXPR_ARG}, 142 #line 25 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 143 {"for", {keyword_for, keyword_for}, EXPR_VALUE}, 144 #line 41 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 145 {"undef", {keyword_undef, keyword_undef}, EXPR_FNAME}, 146 #line 32 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 147 {"or", {keyword_or, keyword_or}, EXPR_VALUE}, 148 #line 27 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 149 {"in", {keyword_in, keyword_in}, EXPR_VALUE}, 150 #line 44 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 151 {"when", {keyword_when, keyword_when}, EXPR_VALUE}, 152 #line 35 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 153 {"retry", {keyword_retry, keyword_retry}, EXPR_END}, 154 #line 26 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 155 {"if", {keyword_if, modifier_if}, EXPR_VALUE}, 156 #line 16 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 157 {"case", {keyword_case, keyword_case}, EXPR_VALUE}, 117 158 #line 33 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 118 {"nil", {keyword_nil, keyword_nil}, EXPR_END}, 119 #line 26 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 120 {"ensure", {keyword_ensure, keyword_ensure}, EXPR_BEG}, 121 #line 25 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 122 {"end", {keyword_end, keyword_end}, EXPR_END}, 123 #line 42 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 124 {"then", {keyword_then, keyword_then}, EXPR_BEG}, 125 #line 34 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 126 {"not", {keyword_not, keyword_not}, EXPR_ARG}, 127 #line 27 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 128 {"false", {keyword_false, keyword_false}, EXPR_END}, 129 #line 40 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 130 {"self", {keyword_self, keyword_self}, EXPR_END}, 131 #line 24 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 132 {"elsif", {keyword_elsif, keyword_elsif}, EXPR_VALUE}, 133 #line 37 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 134 {"rescue", {keyword_rescue, modifier_rescue}, EXPR_MID}, 135 #line 43 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 136 {"true", {keyword_true, keyword_true}, EXPR_END}, 137 #line 46 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 138 {"until", {keyword_until, modifier_until}, EXPR_VALUE}, 139 #line 45 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 140 {"unless", {keyword_unless, modifier_unless}, EXPR_VALUE}, 141 #line 39 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 142 {"return", {keyword_return, keyword_return}, EXPR_MID}, 143 #line 21 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 144 {"def", {keyword_def, keyword_def}, EXPR_FNAME}, 145 #line 16 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 146 {"and", {keyword_and, keyword_and}, EXPR_VALUE}, 147 #line 22 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 148 {"do", {keyword_do, keyword_do}, EXPR_BEG}, 149 #line 49 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 150 {"yield", {keyword_yield, keyword_yield}, EXPR_ARG}, 159 {"redo", {keyword_redo, keyword_redo}, EXPR_END}, 160 #line 29 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 161 {"next", {keyword_next, keyword_next}, EXPR_MID}, 162 #line 38 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 163 {"super", {keyword_super, keyword_super}, EXPR_ARG}, 151 164 #line 28 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 152 {"for", {keyword_for, keyword_for}, EXPR_VALUE},153 #line 44 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"154 {"undef", {keyword_undef, keyword_undef}, EXPR_FNAME},155 #line 35 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"156 {"or", {keyword_or, keyword_or}, EXPR_VALUE},157 #line 30 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"158 {"in", {keyword_in, keyword_in}, EXPR_VALUE},159 #line 47 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"160 {"when", {keyword_when, keyword_when}, EXPR_VALUE},161 #line 38 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"162 {"retry", {keyword_retry, keyword_retry}, EXPR_END},163 #line 29 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"164 {"if", {keyword_if, modifier_if}, EXPR_VALUE},165 #line 19 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"166 {"case", {keyword_case, keyword_case}, EXPR_VALUE},167 #line 36 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"168 {"redo", {keyword_redo, keyword_redo}, EXPR_END},169 #line 32 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"170 {"next", {keyword_next, keyword_next}, EXPR_MID},171 #line 41 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"172 {"super", {keyword_super, keyword_super}, EXPR_ARG},173 #line 31 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"174 165 {"module", {keyword_module, keyword_module}, EXPR_VALUE}, 175 #line 1 7"/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"166 #line 14 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 176 167 {"begin", {keyword_begin, keyword_begin}, EXPR_BEG}, 168 #line 9 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 169 {"__LINE__", {keyword__LINE__, keyword__LINE__}, EXPR_END}, 170 #line 8 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 171 {"__FILE__", {keyword__FILE__, keyword__FILE__}, EXPR_END}, 172 #line 7 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 173 {"__ENCODING__", {keyword__ENCODING__, keyword__ENCODING__}, EXPR_END}, 174 #line 11 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 175 {"END", {keyword_END, keyword_END}, EXPR_END}, 177 176 #line 12 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 178 {"__LINE__", {keyword__LINE__, keyword__LINE__}, EXPR_END}, 179 #line 11 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 180 {"__FILE__", {keyword__FILE__, keyword__FILE__}, EXPR_END}, 177 {"alias", {keyword_alias, keyword_alias}, EXPR_FNAME}, 181 178 #line 10 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 182 {"__ENCODING__", {keyword__ENCODING__, keyword__ENCODING__}, EXPR_END},183 #line 14 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"184 {"END", {keyword_END, keyword_END}, EXPR_END},185 #line 15 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"186 {"alias", {keyword_alias, keyword_alias}, EXPR_FNAME},187 #line 13 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"188 179 {"BEGIN", {keyword_BEGIN, keyword_BEGIN}, EXPR_END}, 189 180 {""}, 190 #line 20"/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"181 #line 17 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 191 182 {"class", {keyword_class, keyword_class}, EXPR_CLASS}, 192 183 {""}, {""}, 193 #line 4 8"/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"184 #line 45 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 194 185 {"while", {keyword_while, modifier_while}, EXPR_VALUE} 195 186 }; … … 197 188 if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH) 198 189 { 199 register int key = hash (str, len);200 201 if (key <= MAX_HASH_VALUE && key >= 0)190 register unsigned int key = hash (str, len); 191 192 if (key <= MAX_HASH_VALUE) 202 193 { 203 194 register const char *s = wordlist[key].name; … … 209 200 return 0; 210 201 } 211 #line 50"/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords"212 202 #line 47 "/home/matz/work/mruby/mrbgems/mruby-compiler/core/keywords" 203 -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler/core/node.h
r331 r439 10 10 enum node_type { 11 11 NODE_METHOD, 12 NODE_FBODY,13 NODE_CFUNC,14 12 NODE_SCOPE, 15 13 NODE_BLOCK, … … 17 15 NODE_CASE, 18 16 NODE_WHEN, 19 NODE_OPT_N,20 17 NODE_WHILE, 21 18 NODE_UNTIL, … … 41 38 NODE_SCALL, 42 39 NODE_FCALL, 43 NODE_VCALL,44 40 NODE_SUPER, 45 41 NODE_ZSUPER, … … 47 43 NODE_ZARRAY, 48 44 NODE_HASH, 45 NODE_KW_HASH, 49 46 NODE_RETURN, 50 47 NODE_YIELD, … … 55 52 NODE_CONST, 56 53 NODE_CVAR, 54 NODE_NVAR, 57 55 NODE_NTH_REF, 58 56 NODE_BACK_REF, 59 57 NODE_MATCH, 60 NODE_MATCH2,61 NODE_MATCH3,62 58 NODE_INT, 63 59 NODE_FLOAT, … … 72 68 NODE_DREGX, 73 69 NODE_DREGX_ONCE, 74 NODE_LIST,75 70 NODE_ARG, 76 NODE_ARGSCAT, 77 NODE_ARGSPUSH, 71 NODE_ARGS_TAIL, 72 NODE_KW_ARG, 73 NODE_KW_REST_ARGS, 78 74 NODE_SPLAT, 79 75 NODE_TO_ARY, … … 89 85 NODE_COLON2, 90 86 NODE_COLON3, 91 NODE_CREF,92 87 NODE_DOT2, 93 88 NODE_DOT3, 94 NODE_FLIP2,95 NODE_FLIP3,96 NODE_ATTRSET,97 89 NODE_SELF, 98 90 NODE_NIL, … … 100 92 NODE_FALSE, 101 93 NODE_DEFINED, 102 NODE_NEWLINE,103 94 NODE_POSTEXE, 104 NODE_ALLOCA,105 NODE_DMETHOD,106 NODE_BMETHOD,107 NODE_MEMO,108 NODE_IFUNC,109 95 NODE_DSYM, 110 NODE_ATTRASGN,111 96 NODE_HEREDOC, 112 97 NODE_LITERAL_DELIM, -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler/core/parse.y
r331 r439 11 11 #endif 12 12 #define YYERROR_VERBOSE 1 13 /* 14 * Force yacc to use our memory management. This is a little evil because 15 * the macros assume that "parser_state *p" is in scope 16 */ 17 #define YYMALLOC(n) mrb_malloc(p->mrb, (n)) 18 #define YYFREE(o) mrb_free(p->mrb, (o)) 19 #define YYSTACK_USE_ALLOCA 0 13 #define YYSTACK_USE_ALLOCA 1 20 14 21 15 #include <ctype.h> … … 28 22 #include <mruby/error.h> 29 23 #include <mruby/throw.h> 24 #include <mruby/string.h> 30 25 #include "node.h" 31 26 … … 77 72 #define intn(x) ((int)(intptr_t)(x)) 78 73 74 #define NUM_SUFFIX_R (1<<0) 75 #define NUM_SUFFIX_I (1<<1) 76 79 77 static inline mrb_sym 80 78 intern_cstr_gen(parser_state *p, const char *s) … … 91 89 #define intern(s,len) intern_gen(p,(s),(len)) 92 90 93 static inline mrb_sym 94 intern_gen_c(parser_state *p, const char c) 95 { 96 return mrb_intern(p->mrb, &c, 1); 97 } 98 #define intern_c(c) intern_gen_c(p,(c)) 91 #define intern_lit(s) mrb_intern_lit(p->mrb, s) 99 92 100 93 static void … … 134 127 c->lineno = p->lineno; 135 128 c->filename_index = p->current_filename_index; 129 /* beginning of next partial file; need to point the previous file */ 130 if (p->lineno == 0 && p->current_filename_index > 0) { 131 c->filename_index-- ; 132 } 136 133 return c; 137 134 } … … 186 183 187 184 if (!a) return b; 185 if (!b) return a; 188 186 while (c->cdr) { 189 187 c = c->cdr; 190 188 } 191 if (b) { 192 c->cdr = b; 193 } 189 c->cdr = b; 194 190 return a; 195 191 } … … 216 212 #undef strdup 217 213 #define strdup(s) parser_strdup(p, s) 214 215 static void 216 dump_int(uint16_t i, char *s) 217 { 218 char *p = s; 219 char *t = s; 220 221 while (i > 0) { 222 *p++ = (i % 10)+'0'; 223 i /= 10; 224 } 225 if (p == s) *p++ = '0'; 226 *p = 0; 227 p--; /* point the last char */ 228 while (t < p) { 229 char c = *t; 230 *t++ = *p; 231 *p-- = c; 232 } 233 } 218 234 219 235 /* xxx ----------------------------- */ … … 280 296 } 281 297 298 static void 299 local_add_blk(parser_state *p, mrb_sym blk) 300 { 301 /* allocate register for block */ 302 local_add_f(p, blk ? blk : mrb_intern_lit(p->mrb, "&")); 303 } 304 305 static void 306 local_add_kw(parser_state *p, mrb_sym kwd) 307 { 308 /* allocate register for keywords hash */ 309 local_add_f(p, kwd ? kwd : mrb_intern_lit(p->mrb, "**")); 310 } 311 282 312 static node* 283 313 locals_node(parser_state *p) 284 314 { 285 315 return p->locals ? p->locals->car : NULL; 316 } 317 318 static void 319 nvars_nest(parser_state *p) 320 { 321 p->nvars = cons(nint(0), p->nvars); 322 } 323 324 static void 325 nvars_block(parser_state *p) 326 { 327 p->nvars = cons(nint(-2), p->nvars); 328 } 329 330 static void 331 nvars_unnest(parser_state *p) 332 { 333 p->nvars = p->nvars->cdr; 286 334 } 287 335 … … 569 617 } 570 618 619 /* (:kw_hash (k . v) (k . v)...) */ 620 static node* 621 new_kw_hash(parser_state *p, node *a) 622 { 623 return cons((node*)NODE_KW_HASH, a); 624 } 625 571 626 /* (:sym . a) */ 572 627 static node* … … 613 668 } 614 669 670 /* (:nvar . a) */ 671 static node* 672 new_nvar(parser_state *p, int num) 673 { 674 int nvars = intn(p->nvars->car); 675 676 p->nvars->car = nint(nvars > num ? nvars : num); 677 return cons((node*)NODE_NVAR, nint(num)); 678 } 679 615 680 /* (:const . a) */ 616 681 static node* … … 672 737 } 673 738 674 /* (m o r m2 b) */ 739 static void 740 local_add_margs(parser_state *p, node *n) 741 { 742 while (n) { 743 if (n->car->car == (node*)NODE_MASGN) { 744 node *t = n->car->cdr->cdr; 745 746 n->car->cdr->cdr = NULL; 747 while (t) { 748 local_add_f(p, sym(t->car)); 749 t = t->cdr; 750 } 751 local_add_margs(p, n->car->cdr->car->car); 752 local_add_margs(p, n->car->cdr->car->cdr->cdr->car); 753 } 754 n = n->cdr; 755 } 756 } 757 758 static void 759 local_add_lv(parser_state *p, node *lv) 760 { 761 while (lv) { 762 local_add_f(p, sym(lv->car)); 763 lv = lv->cdr; 764 } 765 } 766 767 /* (m o r m2 tail) */ 675 768 /* m: (a b c) */ 676 769 /* o: ((a . e1) (b . e2)) */ … … 679 772 /* b: a */ 680 773 static node* 681 new_args(parser_state *p, node *m, node *opt, mrb_sym rest, node *m2, mrb_sym blk)774 new_args(parser_state *p, node *m, node *opt, mrb_sym rest, node *m2, node *tail) 682 775 { 683 776 node *n; 684 777 685 n = cons(m2, nsym(blk)); 778 local_add_margs(p, m); 779 local_add_margs(p, m2); 780 n = cons(m2, tail); 686 781 n = cons(nsym(rest), n); 687 782 n = cons(opt, n); 783 while (opt) { 784 /* opt: (sym . (opt . lv)) -> (sym . opt) */ 785 local_add_lv(p, opt->car->cdr->cdr); 786 opt->car->cdr = opt->car->cdr->car; 787 opt = opt->cdr; 788 } 688 789 return cons(m, n); 689 790 } 690 791 792 /* (:args_tail keywords rest_keywords_sym block_sym) */ 793 static node* 794 new_args_tail(parser_state *p, node *kws, node *kwrest, mrb_sym blk) 795 { 796 node *k; 797 798 if (kws || kwrest) { 799 local_add_kw(p, (kwrest && kwrest->cdr)? sym(kwrest->cdr) : 0); 800 } 801 802 local_add_blk(p, blk); 803 804 /* allocate register for keywords arguments */ 805 /* order is for Proc#parameters */ 806 for (k = kws; k; k = k->cdr) { 807 if (!k->car->cdr->cdr->car) { /* allocate required keywords */ 808 local_add_f(p, sym(k->car->cdr->car)); 809 } 810 } 811 for (k = kws; k; k = k->cdr) { 812 if (k->car->cdr->cdr->car) { /* allocate keywords with default */ 813 local_add_lv(p, k->car->cdr->cdr->car->cdr); 814 k->car->cdr->cdr->car = k->car->cdr->cdr->car->car; 815 local_add_f(p, sym(k->car->cdr->car)); 816 } 817 } 818 819 return list4((node*)NODE_ARGS_TAIL, kws, kwrest, nsym(blk)); 820 } 821 822 /* (:kw_arg kw_sym def_arg) */ 823 static node* 824 new_kw_arg(parser_state *p, mrb_sym kw, node *def_arg) 825 { 826 mrb_assert(kw); 827 return list3((node*)NODE_KW_ARG, nsym(kw), def_arg); 828 } 829 691 830 /* (:block_arg . a) */ 692 831 static node* … … 696 835 } 697 836 837 static node* 838 setup_numparams(parser_state *p, node *a) 839 { 840 int nvars = intn(p->nvars->car); 841 if (nvars > 0) { 842 int i; 843 mrb_sym sym; 844 // m || opt || rest || tail 845 if (a && (a->car || (a->cdr && a->cdr->car) || (a->cdr->cdr && a->cdr->cdr->car) || (a->cdr->cdr->cdr->cdr && a->cdr->cdr->cdr->cdr->car))) { 846 yyerror(p, "ordinary parameter is defined"); 847 } 848 else if (p->locals) { 849 /* p->locals should not be NULL unless error happens before the point */ 850 node* args = 0; 851 for (i = nvars; i > 0; i--) { 852 char buf[3]; 853 854 buf[0] = '_'; 855 buf[1] = i+'0'; 856 buf[2] = '\0'; 857 sym = intern_cstr(buf); 858 args = cons(new_arg(p, sym), args); 859 p->locals->car = cons(nsym(sym), p->locals->car); 860 } 861 a = new_args(p, args, 0, 0, 0, 0); 862 } 863 } 864 return a; 865 } 866 698 867 /* (:block arg body) */ 699 868 static node* 700 869 new_block(parser_state *p, node *a, node *b) 701 870 { 871 a = setup_numparams(p, a); 702 872 return list4((node*)NODE_BLOCK, locals_node(p), a, b); 703 873 } … … 726 896 } 727 897 898 /* (:masgn mlhs mrhs) no check */ 899 static node* 900 new_masgn_param(parser_state *p, node *a, node *b) 901 { 902 return cons((node*)NODE_MASGN, cons(a, b)); 903 } 904 728 905 /* (:asgn lhs rhs) */ 729 906 static node* … … 734 911 } 735 912 913 static node* 914 new_imaginary(parser_state *p, node *imaginary) 915 { 916 return new_call(p, new_const(p, intern_lit("Kernel")), intern_lit("Complex"), list1(list2(list3((node*)NODE_INT, (node*)strdup("0"), nint(10)), imaginary)), 1); 917 } 918 919 static node* 920 new_rational(parser_state *p, node *rational) 921 { 922 return new_call(p, new_const(p, intern_lit("Kernel")), intern_lit("Rational"), list1(list1(rational)), 1); 923 } 924 736 925 /* (:int . i) */ 737 926 static node* 738 new_int(parser_state *p, const char *s, int base) 739 { 740 return list3((node*)NODE_INT, (node*)strdup(s), nint(base)); 741 } 742 927 new_int(parser_state *p, const char *s, int base, int suffix) 928 { 929 node* result = list3((node*)NODE_INT, (node*)strdup(s), nint(base)); 930 if (suffix & NUM_SUFFIX_R) { 931 result = new_rational(p, result); 932 } 933 if (suffix & NUM_SUFFIX_I) { 934 result = new_imaginary(p, result); 935 } 936 return result; 937 } 938 939 #ifndef MRB_WITHOUT_FLOAT 743 940 /* (:float . i) */ 744 941 static node* 745 new_float(parser_state *p, const char *s) 746 { 747 return cons((node*)NODE_FLOAT, (node*)strdup(s)); 748 } 942 new_float(parser_state *p, const char *s, int suffix) 943 { 944 node* result = cons((node*)NODE_FLOAT, (node*)strdup(s)); 945 if (suffix & NUM_SUFFIX_R) { 946 result = new_rational(p, result); 947 } 948 if (suffix & NUM_SUFFIX_I) { 949 result = new_imaginary(p, result); 950 } 951 return result; 952 } 953 #endif 749 954 750 955 /* (:str . (s . len)) */ 751 956 static node* 752 new_str(parser_state *p, const char *s, int len)957 new_str(parser_state *p, const char *s, size_t len) 753 958 { 754 959 return cons((node*)NODE_STR, cons((node*)strndup(s, len), nint(len))); … … 762 967 } 763 968 969 static int 970 string_node_p(node *n) 971 { 972 return (int)((enum node_type)(intptr_t)n->car == NODE_STR); 973 } 974 975 static node* 976 composite_string_node(parser_state *p, node *a, node *b) 977 { 978 size_t newlen = (size_t)a->cdr + (size_t)b->cdr; 979 char *str = (char*)mrb_pool_realloc(p->pool, a->car, (size_t)a->cdr + 1, newlen + 1); 980 memcpy(str + (size_t)a->cdr, b->car, (size_t)b->cdr); 981 str[newlen] = '\0'; 982 a->car = (node*)str; 983 a->cdr = (node*)newlen; 984 cons_free(b); 985 return a; 986 } 987 988 static node* 989 concat_string(parser_state *p, node *a, node *b) 990 { 991 if (string_node_p(a)) { 992 if (string_node_p(b)) { 993 /* a == NODE_STR && b == NODE_STR */ 994 composite_string_node(p, a->cdr, b->cdr); 995 cons_free(b); 996 return a; 997 } 998 else { 999 /* a == NODE_STR && b == NODE_DSTR */ 1000 1001 if (string_node_p(b->cdr->car)) { 1002 /* a == NODE_STR && b->[NODE_STR, ...] */ 1003 composite_string_node(p, a->cdr, b->cdr->car->cdr); 1004 cons_free(b->cdr->car); 1005 b->cdr->car = a; 1006 return b; 1007 } 1008 } 1009 } 1010 else { 1011 node *c; /* last node of a */ 1012 for (c = a; c->cdr != NULL; c = c->cdr) ; 1013 1014 if (string_node_p(b)) { 1015 /* a == NODE_DSTR && b == NODE_STR */ 1016 if (string_node_p(c->car)) { 1017 /* a->[..., NODE_STR] && b == NODE_STR */ 1018 composite_string_node(p, c->car->cdr, b->cdr); 1019 cons_free(b); 1020 return a; 1021 } 1022 1023 push(a, b); 1024 return a; 1025 } 1026 else { 1027 /* a == NODE_DSTR && b == NODE_DSTR */ 1028 if (string_node_p(c->car) && string_node_p(b->cdr->car)) { 1029 /* a->[..., NODE_STR] && b->[NODE_STR, ...] */ 1030 node *d = b->cdr; 1031 cons_free(b); 1032 composite_string_node(p, c->car->cdr, d->car->cdr); 1033 cons_free(d->car); 1034 c->cdr = d->cdr; 1035 cons_free(d); 1036 return a; 1037 } 1038 else { 1039 c->cdr = b->cdr; 1040 cons_free(b); 1041 return a; 1042 } 1043 } 1044 } 1045 1046 return new_dstr(p, list2(a, b)); 1047 } 1048 764 1049 /* (:str . (s . len)) */ 765 1050 static node* … … 780 1065 new_dsym(parser_state *p, node *a) 781 1066 { 782 return cons((node*)NODE_DSYM, new_dstr(p, a));1067 return cons((node*)NODE_DSYM, a); 783 1068 } 784 1069 … … 1024 1309 if (p->parsing_heredoc == NULL) { 1025 1310 p->lstate = EXPR_BEG; 1026 p->cmd_start = TRUE;1027 1311 end_strterm(p); 1028 1312 p->lex_strterm = p->lex_strterm_before_heredoc; 1029 1313 p->lex_strterm_before_heredoc = NULL; 1030 p->heredoc_end_now = TRUE;1031 1314 } 1032 1315 else { … … 1105 1388 keyword__ENCODING__ 1106 1389 1107 %token <id> tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL 1390 %token <id> tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL_TAG 1108 1391 %token <nd> tINTEGER tFLOAT tCHAR tXSTRING tREGEXP 1109 %token <nd> tSTRING tSTRING_PART tSTRING_MID tLABEL_END1392 %token <nd> tSTRING tSTRING_PART tSTRING_MID 1110 1393 %token <nd> tNTH_REF tBACK_REF 1111 1394 %token <num> tREGEXP_END 1112 1113 %type <nd> singleton string string_rep string_interp xstring regexp 1395 %token <num> tNUMPARAM 1396 1397 %type <nd> singleton string string_fragment string_rep string_interp xstring regexp 1114 1398 %type <nd> literal numeric cpath symbol 1115 1399 %type <nd> top_compstmt top_stmts top_stmt … … 1122 1406 %type <nd> command_asgn command_rhs mrhs superclass block_call block_command 1123 1407 %type <nd> f_block_optarg f_block_opt 1124 %type <nd> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs1408 %type <nd> f_arglist f_args f_arg f_arg_item f_optarg f_margs 1125 1409 %type <nd> assoc_list assocs assoc undef_list backref for_var 1126 1410 %type <nd> block_param opt_block_param block_param_def f_opt … … 1132 1416 %type <nd> heredoc words symbols 1133 1417 %type <num> call_op call_op2 /* 0:'&.', 1:'.', 2:'::' */ 1418 1419 %type <nd> args_tail opt_args_tail f_kwarg f_kw f_kwrest 1420 %type <nd> f_block_kwarg f_block_kw block_args_tail opt_block_args_tail 1421 %type <id> f_label 1134 1422 1135 1423 %token tUPLUS /* unary+ */ … … 1158 1446 %token tLBRACE_ARG /* { */ 1159 1447 %token tSTAR /* * */ 1448 %token tDSTAR /* ** */ 1160 1449 %token tAMPER /* & */ 1161 1450 %token tLAMBDA /* -> */ … … 1179 1468 %right '=' tOP_ASGN 1180 1469 %left modifier_rescue 1181 %right '?' ':' 1470 %right '?' ':' tLABEL_TAG 1182 1471 %nonassoc tDOT2 tDOT3 1183 1472 %left tOROP … … 1237 1526 { 1238 1527 $<nd>$ = local_switch(p); 1528 nvars_block(p); 1239 1529 } 1240 1530 '{' top_compstmt '}' … … 1242 1532 yyerror(p, "BEGIN not supported"); 1243 1533 local_resume(p, $<nd>2); 1534 nvars_unnest(p); 1244 1535 $$ = 0; 1245 1536 } … … 1359 1650 $$ = new_op_asgn(p, $1, $2, $3); 1360 1651 } 1361 | primary_value '[' opt_call_args rbrackettOP_ASGN command_rhs1362 { 1363 $$ = new_op_asgn(p, new_call(p, $1, intern ("[]",2), $3, '.'), $5, $6);1652 | primary_value '[' opt_call_args ']' tOP_ASGN command_rhs 1653 { 1654 $$ = new_op_asgn(p, new_call(p, $1, intern_lit("[]"), $3, '.'), $5, $6); 1364 1655 } 1365 1656 | primary_value call_op tIDENTIFIER tOP_ASGN command_rhs … … 1385 1676 $$ = new_begin(p, 0); 1386 1677 } 1387 1678 ; 1388 1679 1389 1680 command_rhs : command_call %prec tOP_ASGN … … 1439 1730 { 1440 1731 local_nest(p); 1732 nvars_nest(p); 1441 1733 } 1442 1734 opt_block_param … … 1446 1738 $$ = new_block(p, $3, $4); 1447 1739 local_unnest(p); 1740 nvars_unnest(p); 1448 1741 } 1449 1742 ; … … 1588 1881 assignable(p, $1); 1589 1882 } 1590 | primary_value '[' opt_call_args rbracket1591 { 1592 $$ = new_call(p, $1, intern ("[]",2), $3, '.');1883 | primary_value '[' opt_call_args ']' 1884 { 1885 $$ = new_call(p, $1, intern_lit("[]"), $3, '.'); 1593 1886 } 1594 1887 | primary_value call_op tIDENTIFIER … … 1627 1920 assignable(p, $1); 1628 1921 } 1629 | primary_value '[' opt_call_args rbracket1630 { 1631 $$ = new_call(p, $1, intern ("[]",2), $3, '.');1922 | primary_value '[' opt_call_args ']' 1923 { 1924 $$ = new_call(p, $1, intern_lit("[]"), $3, '.'); 1632 1925 } 1633 1926 | primary_value call_op tIDENTIFIER … … 1659 1952 backref_error(p, $1); 1660 1953 $$ = 0; 1954 } 1955 | tNUMPARAM 1956 { 1957 yyerror(p, "can't assign to numbered parameter"); 1661 1958 } 1662 1959 ; … … 1713 2010 ; 1714 2011 1715 op : '|' { $$ = intern_c('|'); } 1716 | '^' { $$ = intern_c('^'); } 1717 | '&' { $$ = intern_c('&'); } 1718 | tCMP { $$ = intern("<=>",3); } 1719 | tEQ { $$ = intern("==",2); } 1720 | tEQQ { $$ = intern("===",3); } 1721 | tMATCH { $$ = intern("=~",2); } 1722 | tNMATCH { $$ = intern("!~",2); } 1723 | '>' { $$ = intern_c('>'); } 1724 | tGEQ { $$ = intern(">=",2); } 1725 | '<' { $$ = intern_c('<'); } 1726 | tLEQ { $$ = intern("<=",2); } 1727 | tNEQ { $$ = intern("!=",2); } 1728 | tLSHFT { $$ = intern("<<",2); } 1729 | tRSHFT { $$ = intern(">>",2); } 1730 | '+' { $$ = intern_c('+'); } 1731 | '-' { $$ = intern_c('-'); } 1732 | '*' { $$ = intern_c('*'); } 1733 | tSTAR { $$ = intern_c('*'); } 1734 | '/' { $$ = intern_c('/'); } 1735 | '%' { $$ = intern_c('%'); } 1736 | tPOW { $$ = intern("**",2); } 1737 | '!' { $$ = intern_c('!'); } 1738 | '~' { $$ = intern_c('~'); } 1739 | tUPLUS { $$ = intern("+@",2); } 1740 | tUMINUS { $$ = intern("-@",2); } 1741 | tAREF { $$ = intern("[]",2); } 1742 | tASET { $$ = intern("[]=",3); } 1743 | '`' { $$ = intern_c('`'); } 2012 op : '|' { $$ = intern_lit("|"); } 2013 | '^' { $$ = intern_lit("^"); } 2014 | '&' { $$ = intern_lit("&"); } 2015 | tCMP { $$ = intern_lit("<=>"); } 2016 | tEQ { $$ = intern_lit("=="); } 2017 | tEQQ { $$ = intern_lit("==="); } 2018 | tMATCH { $$ = intern_lit("=~"); } 2019 | tNMATCH { $$ = intern_lit("!~"); } 2020 | '>' { $$ = intern_lit(">"); } 2021 | tGEQ { $$ = intern_lit(">="); } 2022 | '<' { $$ = intern_lit("<"); } 2023 | tLEQ { $$ = intern_lit("<="); } 2024 | tNEQ { $$ = intern_lit("!="); } 2025 | tLSHFT { $$ = intern_lit("<<"); } 2026 | tRSHFT { $$ = intern_lit(">>"); } 2027 | '+' { $$ = intern_lit("+"); } 2028 | '-' { $$ = intern_lit("-"); } 2029 | '*' { $$ = intern_lit("*"); } 2030 | tSTAR { $$ = intern_lit("*"); } 2031 | '/' { $$ = intern_lit("/"); } 2032 | '%' { $$ = intern_lit("%"); } 2033 | tPOW { $$ = intern_lit("**"); } 2034 | tDSTAR { $$ = intern_lit("**"); } 2035 | '!' { $$ = intern_lit("!"); } 2036 | '~' { $$ = intern_lit("~"); } 2037 | tUPLUS { $$ = intern_lit("+@"); } 2038 | tUMINUS { $$ = intern_lit("-@"); } 2039 | tAREF { $$ = intern_lit("[]"); } 2040 | tASET { $$ = intern_lit("[]="); } 2041 | '`' { $$ = intern_lit("`"); } 1744 2042 ; 1745 2043 … … 1766 2064 $$ = new_op_asgn(p, $1, $2, $3); 1767 2065 } 1768 | primary_value '[' opt_call_args rbrackettOP_ASGN arg_rhs1769 { 1770 $$ = new_op_asgn(p, new_call(p, $1, intern ("[]",2), $3, '.'), $5, $6);2066 | primary_value '[' opt_call_args ']' tOP_ASGN arg_rhs 2067 { 2068 $$ = new_op_asgn(p, new_call(p, $1, intern_lit("[]"), $3, '.'), $5, $6); 1771 2069 } 1772 2070 | primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs … … 1925 2223 $$ = new_if(p, cond($1), $3, $6); 1926 2224 } 2225 | arg '?' arg opt_nl tLABEL_TAG arg 2226 { 2227 $$ = new_if(p, cond($1), $3, $6); 2228 } 1927 2229 | primary 1928 2230 { … … 1939 2241 | args comma assocs trailer 1940 2242 { 1941 $$ = push($1, new_ hash(p, $3));2243 $$ = push($1, new_kw_hash(p, $3)); 1942 2244 } 1943 2245 | assocs trailer 1944 2246 { 1945 $$ = cons(new_ hash(p, $1), 0);2247 $$ = cons(new_kw_hash(p, $1), 0); 1946 2248 NODE_LINENO($$, $1); 1947 2249 } … … 1960 2262 ; 1961 2263 1962 paren_args : '(' opt_call_args rparen2264 paren_args : '(' opt_call_args ')' 1963 2265 { 1964 2266 $$ = $2; … … 1971 2273 1972 2274 opt_call_args : none 1973 | call_args 1974 | args ','2275 | call_args opt_terms 2276 | args comma 1975 2277 { 1976 2278 $$ = cons($1,0); 1977 2279 NODE_LINENO($$, $1); 1978 2280 } 1979 | args comma assocs ','1980 { 1981 $$ = cons(push($1, new_ hash(p, $3)), 0);2281 | args comma assocs comma 2282 { 2283 $$ = cons(push($1, new_kw_hash(p, $3)), 0); 1982 2284 NODE_LINENO($$, $1); 1983 2285 } 1984 | assocs ','1985 { 1986 $$ = cons(list1(new_ hash(p, $1)), 0);2286 | assocs comma 2287 { 2288 $$ = cons(list1(new_kw_hash(p, $1)), 0); 1987 2289 NODE_LINENO($$, $1); 1988 2290 } … … 2002 2304 | assocs opt_block_arg 2003 2305 { 2004 $$ = cons(list1(new_ hash(p, $1)), $2);2306 $$ = cons(list1(new_kw_hash(p, $1)), $2); 2005 2307 NODE_LINENO($$, $1); 2006 2308 } 2007 2309 | args comma assocs opt_block_arg 2008 2310 { 2009 $$ = cons(push($1, new_ hash(p, $3)), $4);2311 $$ = cons(push($1, new_kw_hash(p, $3)), $4); 2010 2312 NODE_LINENO($$, $1); 2011 2313 } … … 2045 2347 2046 2348 comma : ',' 2047 | ',' heredoc_bodies2349 | ',' opt_nl heredoc_bodies 2048 2350 ; 2049 2351 … … 2096 2398 | var_ref 2097 2399 | backref 2400 | tNUMPARAM 2401 { 2402 $$ = new_nvar(p, $1); 2403 } 2098 2404 | tFID 2099 2405 { … … 2248 2554 yyerror(p, "class definition in method body"); 2249 2555 $<nd>$ = local_switch(p); 2556 nvars_block(p); 2250 2557 } 2251 2558 bodystmt … … 2255 2562 SET_LINENO($$, $1); 2256 2563 local_resume(p, $<nd>4); 2564 nvars_unnest(p); 2257 2565 } 2258 2566 | keyword_class … … 2265 2573 { 2266 2574 $<nd>$ = cons(local_switch(p), nint(p->in_single)); 2575 nvars_block(p); 2267 2576 p->in_single = 0; 2268 2577 } … … 2273 2582 SET_LINENO($$, $1); 2274 2583 local_resume(p, $<nd>6->car); 2584 nvars_unnest(p); 2275 2585 p->in_def = $<num>4; 2276 2586 p->in_single = intn($<nd>6->cdr); … … 2282 2592 yyerror(p, "module definition in method body"); 2283 2593 $<nd>$ = local_switch(p); 2594 nvars_block(p); 2284 2595 } 2285 2596 bodystmt … … 2289 2600 SET_LINENO($$, $1); 2290 2601 local_resume(p, $<nd>3); 2602 nvars_unnest(p); 2291 2603 } 2292 2604 | keyword_def fname … … 2298 2610 p->in_def++; 2299 2611 $<nd>$ = local_switch(p); 2612 nvars_block(p); 2300 2613 } 2301 2614 f_arglist … … 2306 2619 SET_LINENO($$, $1); 2307 2620 local_resume(p, $<nd>4); 2621 nvars_unnest(p); 2308 2622 p->in_def--; 2309 2623 p->cmdarg_stack = $<stack>3; … … 2320 2634 p->lstate = EXPR_ENDFN; /* force for args */ 2321 2635 $<nd>$ = local_switch(p); 2636 nvars_block(p); 2322 2637 } 2323 2638 f_arglist … … 2328 2643 SET_LINENO($$, $1); 2329 2644 local_resume(p, $<nd>6); 2645 nvars_unnest(p); 2330 2646 p->in_single--; 2331 2647 p->cmdarg_stack = $<stack>4; … … 2388 2704 ; 2389 2705 2390 f_marg : f_norm_arg 2391 { 2392 $$ = new_arg(p, $1); 2393 } 2394 | tLPAREN f_margs rparen 2395 { 2396 $$ = new_masgn(p, $2, 0); 2397 } 2398 ; 2399 2400 f_marg_list : f_marg 2401 { 2402 $$ = list1($1); 2403 } 2404 | f_marg_list ',' f_marg 2405 { 2406 $$ = push($1, $3); 2407 } 2408 ; 2409 2410 f_margs : f_marg_list 2706 f_margs : f_arg 2411 2707 { 2412 2708 $$ = list3($1,0,0); 2413 2709 } 2414 | f_ marg_list',' tSTAR f_norm_arg2710 | f_arg ',' tSTAR f_norm_arg 2415 2711 { 2416 2712 $$ = list3($1, new_arg(p, $4), 0); 2417 2713 } 2418 | f_ marg_list ',' tSTAR f_norm_arg ',' f_marg_list2714 | f_arg ',' tSTAR f_norm_arg ',' f_arg 2419 2715 { 2420 2716 $$ = list3($1, new_arg(p, $4), $6); 2421 2717 } 2422 | f_marg_list ',' tSTAR 2423 { 2718 | f_arg ',' tSTAR 2719 { 2720 local_add_f(p, 0); 2424 2721 $$ = list3($1, (node*)-1, 0); 2425 2722 } 2426 | f_ marg_list ',' tSTAR ',' f_marg_list2723 | f_arg ',' tSTAR ',' f_arg 2427 2724 { 2428 2725 $$ = list3($1, (node*)-1, $5); … … 2432 2729 $$ = list3(0, new_arg(p, $2), 0); 2433 2730 } 2434 | tSTAR f_norm_arg ',' f_ marg_list2731 | tSTAR f_norm_arg ',' f_arg 2435 2732 { 2436 2733 $$ = list3(0, new_arg(p, $2), $4); … … 2438 2735 | tSTAR 2439 2736 { 2737 local_add_f(p, 0); 2440 2738 $$ = list3(0, (node*)-1, 0); 2441 2739 } 2442 | tSTAR ',' f_marg_list 2443 { 2444 $$ = list3(0, (node*)-1, $3); 2445 } 2446 ; 2447 2448 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_f_block_arg 2740 | tSTAR ',' 2741 { 2742 local_add_f(p, 0); 2743 } 2744 f_arg 2745 { 2746 $$ = list3(0, (node*)-1, $4); 2747 } 2748 ; 2749 2750 block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg 2751 { 2752 $$ = new_args_tail(p, $1, $3, $4); 2753 } 2754 | f_block_kwarg opt_f_block_arg 2755 { 2756 $$ = new_args_tail(p, $1, 0, $2); 2757 } 2758 | f_kwrest opt_f_block_arg 2759 { 2760 $$ = new_args_tail(p, 0, $1, $2); 2761 } 2762 | f_block_arg 2763 { 2764 $$ = new_args_tail(p, 0, 0, $1); 2765 } 2766 ; 2767 2768 opt_block_args_tail : ',' block_args_tail 2769 { 2770 $$ = $2; 2771 } 2772 | /* none */ 2773 { 2774 $$ = new_args_tail(p, 0, 0, 0); 2775 } 2776 ; 2777 2778 block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail 2449 2779 { 2450 2780 $$ = new_args(p, $1, $3, $5, 0, $6); 2451 2781 } 2452 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_ f_block_arg2782 | f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail 2453 2783 { 2454 2784 $$ = new_args(p, $1, $3, $5, $7, $8); 2455 2785 } 2456 | f_arg ',' f_block_optarg opt_ f_block_arg2786 | f_arg ',' f_block_optarg opt_block_args_tail 2457 2787 { 2458 2788 $$ = new_args(p, $1, $3, 0, 0, $4); 2459 2789 } 2460 | f_arg ',' f_block_optarg ',' f_arg opt_ f_block_arg2790 | f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail 2461 2791 { 2462 2792 $$ = new_args(p, $1, $3, 0, $5, $6); 2463 2793 } 2464 | f_arg ',' f_rest_arg opt_ f_block_arg2794 | f_arg ',' f_rest_arg opt_block_args_tail 2465 2795 { 2466 2796 $$ = new_args(p, $1, 0, $3, 0, $4); 2467 2797 } 2468 | f_arg ',' 2469 { 2470 $$ = new_args(p, $1, 0, 0, 0, 0);2471 } 2472 | f_arg ',' f_rest_arg ',' f_arg opt_ f_block_arg2798 | f_arg ',' opt_block_args_tail 2799 { 2800 $$ = new_args(p, $1, 0, 0, 0, $3); 2801 } 2802 | f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail 2473 2803 { 2474 2804 $$ = new_args(p, $1, 0, $3, $5, $6); 2475 2805 } 2476 | f_arg opt_ f_block_arg2806 | f_arg opt_block_args_tail 2477 2807 { 2478 2808 $$ = new_args(p, $1, 0, 0, 0, $2); 2479 2809 } 2480 | f_block_optarg ',' f_rest_arg opt_ f_block_arg2810 | f_block_optarg ',' f_rest_arg opt_block_args_tail 2481 2811 { 2482 2812 $$ = new_args(p, 0, $1, $3, 0, $4); 2483 2813 } 2484 | f_block_optarg ',' f_rest_arg ',' f_arg opt_ f_block_arg2814 | f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail 2485 2815 { 2486 2816 $$ = new_args(p, 0, $1, $3, $5, $6); 2487 2817 } 2488 | f_block_optarg opt_ f_block_arg2818 | f_block_optarg opt_block_args_tail 2489 2819 { 2490 2820 $$ = new_args(p, 0, $1, 0, 0, $2); 2491 2821 } 2492 | f_block_optarg ',' f_arg opt_ f_block_arg2822 | f_block_optarg ',' f_arg opt_block_args_tail 2493 2823 { 2494 2824 $$ = new_args(p, 0, $1, 0, $3, $4); 2495 2825 } 2496 | f_rest_arg opt_ f_block_arg2826 | f_rest_arg opt_block_args_tail 2497 2827 { 2498 2828 $$ = new_args(p, 0, 0, $1, 0, $2); 2499 2829 } 2500 | f_rest_arg ',' f_arg opt_ f_block_arg2830 | f_rest_arg ',' f_arg opt_block_args_tail 2501 2831 { 2502 2832 $$ = new_args(p, 0, 0, $1, $3, $4); 2503 2833 } 2504 | f_block_arg2834 | block_args_tail 2505 2835 { 2506 2836 $$ = new_args(p, 0, 0, 0, 0, $1); … … 2509 2839 2510 2840 opt_block_param : none 2841 { 2842 local_add_blk(p, 0); 2843 $$ = 0; 2844 } 2511 2845 | block_param_def 2512 2846 { 2513 2847 p->cmd_start = TRUE; 2514 2848 $$ = $1; … … 2516 2850 ; 2517 2851 2518 block_param_def : '|' opt_bv_decl '|'2852 block_param_def : '|' {local_add_blk(p, 0);} opt_bv_decl '|' 2519 2853 { 2520 2854 $$ = 0; … … 2522 2856 | tOROP 2523 2857 { 2858 local_add_blk(p, 0); 2524 2859 $$ = 0; 2525 2860 } … … 2567 2902 $$ = $2; 2568 2903 } 2569 | keyword_do_LAMBDA compstmt keyword_end2904 | keyword_do_LAMBDA bodystmt keyword_end 2570 2905 { 2571 2906 $$ = $2; … … 2576 2911 { 2577 2912 local_nest(p); 2913 nvars_nest(p); 2578 2914 } 2579 2915 opt_block_param 2580 compstmt2916 bodystmt 2581 2917 keyword_end 2582 2918 { 2583 2919 $$ = new_block(p,$3,$4); 2584 2920 local_unnest(p); 2921 nvars_unnest(p); 2585 2922 } 2586 2923 ; … … 2630 2967 | primary_value call_op paren_args 2631 2968 { 2632 $$ = new_call(p, $1, intern ("call",4), $3, $2);2969 $$ = new_call(p, $1, intern_lit("call"), $3, $2); 2633 2970 } 2634 2971 | primary_value tCOLON2 paren_args 2635 2972 { 2636 $$ = new_call(p, $1, intern ("call",4), $3, tCOLON2);2973 $$ = new_call(p, $1, intern_lit("call"), $3, tCOLON2); 2637 2974 } 2638 2975 | keyword_super paren_args … … 2644 2981 $$ = new_zsuper(p); 2645 2982 } 2646 | primary_value '[' opt_call_args rbracket2647 { 2648 $$ = new_call(p, $1, intern ("[]",2), $3, '.');2983 | primary_value '[' opt_call_args ']' 2984 { 2985 $$ = new_call(p, $1, intern_lit("[]"), $3, '.'); 2649 2986 } 2650 2987 ; … … 2653 2990 { 2654 2991 local_nest(p); 2992 nvars_nest(p); 2655 2993 $<num>$ = p->lineno; 2656 2994 } … … 2661 2999 SET_LINENO($$, $<num>2); 2662 3000 local_unnest(p); 3001 nvars_unnest(p); 2663 3002 } 2664 3003 | keyword_do 2665 3004 { 2666 3005 local_nest(p); 3006 nvars_nest(p); 2667 3007 $<num>$ = p->lineno; 2668 3008 } 2669 3009 opt_block_param 2670 compstmt keyword_end3010 bodystmt keyword_end 2671 3011 { 2672 3012 $$ = new_block(p,$3,$4); 2673 3013 SET_LINENO($$, $<num>2); 2674 3014 local_unnest(p); 3015 nvars_unnest(p); 2675 3016 } 2676 3017 ; … … 2734 3075 ; 2735 3076 2736 string : tCHAR 3077 string : string_fragment 3078 | string string_fragment 3079 { 3080 $$ = concat_string(p, $1, $2); 3081 } 3082 ; 3083 3084 string_fragment : tCHAR 2737 3085 | tSTRING 2738 3086 | tSTRING_BEG tSTRING … … 2854 3202 symbol : basic_symbol 2855 3203 { 3204 p->lstate = EXPR_ENDARG; 2856 3205 $$ = new_sym(p, $1); 2857 3206 } 2858 3207 | tSYMBEG tSTRING_BEG string_rep tSTRING 2859 3208 { 2860 p->lstate = EXPR_END ;2861 $$ = new_dsym(p, push($3, $4));3209 p->lstate = EXPR_ENDARG; 3210 $$ = new_dsym(p, new_dstr(p, push($3, $4))); 2862 3211 } 2863 3212 ; … … 2865 3214 basic_symbol : tSYMBEG sym 2866 3215 { 2867 p->lstate = EXPR_END;2868 3216 $$ = $2; 2869 3217 } … … 2932 3280 assignable(p, $1); 2933 3281 } 3282 | tNUMPARAM 3283 { 3284 yyerror(p, "can't assign to numbered parameter"); 3285 } 2934 3286 ; 2935 3287 … … 2956 3308 | keyword__FILE__ 2957 3309 { 2958 const char *fn = p->filename;3310 const char *fn = mrb_sym_name_len(p->mrb, p->filename_sym, NULL); 2959 3311 if (!fn) { 2960 3312 fn = "(null)"; … … 2966 3318 char buf[16]; 2967 3319 2968 snprintf(buf, sizeof(buf), "%d", p->lineno); 2969 $$ = new_int(p, buf, 10); 3320 dump_int(p->lineno, buf); 3321 $$ = new_int(p, buf, 10, 0); 3322 } 3323 | keyword__ENCODING__ 3324 { 3325 #ifdef MRB_UTF8_STRING 3326 const char *enc = "UTF-8"; 3327 #else 3328 const char *enc = "ASCII-8BIT"; 3329 #endif 3330 $$ = new_str(p, enc, strlen(enc)); 2970 3331 } 2971 3332 ; … … 3007 3368 ; 3008 3369 3009 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_f_block_arg 3370 f_label : tIDENTIFIER tLABEL_TAG 3371 { 3372 local_nest(p); 3373 } 3374 ; 3375 3376 f_kw : f_label arg 3377 { 3378 void_expr_error(p, $2); 3379 $$ = new_kw_arg(p, $1, cons($2, locals_node(p))); 3380 local_unnest(p); 3381 } 3382 | f_label 3383 { 3384 $$ = new_kw_arg(p, $1, 0); 3385 local_unnest(p); 3386 } 3387 ; 3388 3389 f_block_kw : f_label primary_value 3390 { 3391 $$ = new_kw_arg(p, $1, cons($2, locals_node(p))); 3392 local_unnest(p); 3393 } 3394 | f_label 3395 { 3396 $$ = new_kw_arg(p, $1, 0); 3397 local_unnest(p); 3398 } 3399 ; 3400 3401 f_block_kwarg : f_block_kw 3402 { 3403 $$ = list1($1); 3404 } 3405 | f_block_kwarg ',' f_block_kw 3406 { 3407 $$ = push($1, $3); 3408 } 3409 ; 3410 3411 f_kwarg : f_kw 3412 { 3413 $$ = list1($1); 3414 } 3415 | f_kwarg ',' f_kw 3416 { 3417 $$ = push($1, $3); 3418 } 3419 ; 3420 3421 kwrest_mark : tPOW 3422 | tDSTAR 3423 ; 3424 3425 f_kwrest : kwrest_mark tIDENTIFIER 3426 { 3427 $$ = cons((node*)NODE_KW_REST_ARGS, nsym($2)); 3428 } 3429 | kwrest_mark 3430 { 3431 $$ = cons((node*)NODE_KW_REST_ARGS, 0); 3432 } 3433 ; 3434 3435 args_tail : f_kwarg ',' f_kwrest opt_f_block_arg 3436 { 3437 $$ = new_args_tail(p, $1, $3, $4); 3438 } 3439 | f_kwarg opt_f_block_arg 3440 { 3441 $$ = new_args_tail(p, $1, 0, $2); 3442 } 3443 | f_kwrest opt_f_block_arg 3444 { 3445 $$ = new_args_tail(p, 0, $1, $2); 3446 } 3447 | f_block_arg 3448 { 3449 $$ = new_args_tail(p, 0, 0, $1); 3450 } 3451 ; 3452 3453 opt_args_tail : ',' args_tail 3454 { 3455 $$ = $2; 3456 } 3457 | /* none */ 3458 { 3459 $$ = new_args_tail(p, 0, 0, 0); 3460 } 3461 ; 3462 3463 f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail 3010 3464 { 3011 3465 $$ = new_args(p, $1, $3, $5, 0, $6); 3012 3466 } 3013 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_ f_block_arg3467 | f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail 3014 3468 { 3015 3469 $$ = new_args(p, $1, $3, $5, $7, $8); 3016 3470 } 3017 | f_arg ',' f_optarg opt_ f_block_arg3471 | f_arg ',' f_optarg opt_args_tail 3018 3472 { 3019 3473 $$ = new_args(p, $1, $3, 0, 0, $4); 3020 3474 } 3021 | f_arg ',' f_optarg ',' f_arg opt_ f_block_arg3475 | f_arg ',' f_optarg ',' f_arg opt_args_tail 3022 3476 { 3023 3477 $$ = new_args(p, $1, $3, 0, $5, $6); 3024 3478 } 3025 | f_arg ',' f_rest_arg opt_ f_block_arg3479 | f_arg ',' f_rest_arg opt_args_tail 3026 3480 { 3027 3481 $$ = new_args(p, $1, 0, $3, 0, $4); 3028 3482 } 3029 | f_arg ',' f_rest_arg ',' f_arg opt_ f_block_arg3483 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail 3030 3484 { 3031 3485 $$ = new_args(p, $1, 0, $3, $5, $6); 3032 3486 } 3033 | f_arg opt_ f_block_arg3487 | f_arg opt_args_tail 3034 3488 { 3035 3489 $$ = new_args(p, $1, 0, 0, 0, $2); 3036 3490 } 3037 | f_optarg ',' f_rest_arg opt_ f_block_arg3491 | f_optarg ',' f_rest_arg opt_args_tail 3038 3492 { 3039 3493 $$ = new_args(p, 0, $1, $3, 0, $4); 3040 3494 } 3041 | f_optarg ',' f_rest_arg ',' f_arg opt_ f_block_arg3495 | f_optarg ',' f_rest_arg ',' f_arg opt_args_tail 3042 3496 { 3043 3497 $$ = new_args(p, 0, $1, $3, $5, $6); 3044 3498 } 3045 | f_optarg opt_ f_block_arg3499 | f_optarg opt_args_tail 3046 3500 { 3047 3501 $$ = new_args(p, 0, $1, 0, 0, $2); 3048 3502 } 3049 | f_optarg ',' f_arg opt_ f_block_arg3503 | f_optarg ',' f_arg opt_args_tail 3050 3504 { 3051 3505 $$ = new_args(p, 0, $1, 0, $3, $4); 3052 3506 } 3053 | f_rest_arg opt_ f_block_arg3507 | f_rest_arg opt_args_tail 3054 3508 { 3055 3509 $$ = new_args(p, 0, 0, $1, 0, $2); 3056 3510 } 3057 | f_rest_arg ',' f_arg opt_ f_block_arg3511 | f_rest_arg ',' f_arg opt_args_tail 3058 3512 { 3059 3513 $$ = new_args(p, 0, 0, $1, $3, $4); 3060 3514 } 3061 | f_block_arg3515 | args_tail 3062 3516 { 3063 3517 $$ = new_args(p, 0, 0, 0, 0, $1); … … 3065 3519 | /* none */ 3066 3520 { 3067 local_add_f(p, 0);3521 local_add_f(p, mrb_intern_lit(p->mrb, "&")); 3068 3522 $$ = new_args(p, 0, 0, 0, 0, 0); 3069 3523 } … … 3090 3544 $$ = 0; 3091 3545 } 3546 | tNUMPARAM 3547 { 3548 yyerror(p, "formal argument cannot be a numbered parameter"); 3549 $$ = 0; 3550 } 3092 3551 ; 3093 3552 … … 3107 3566 $$ = new_arg(p, $1); 3108 3567 } 3109 | tLPAREN f_margs rparen 3110 { 3111 $$ = new_masgn(p, $2, 0); 3568 | tLPAREN 3569 { 3570 $<nd>$ = local_switch(p); 3571 } 3572 f_margs rparen 3573 { 3574 $$ = new_masgn_param(p, $3, p->locals->car); 3575 local_resume(p, $<nd>2); 3576 local_add_f(p, 0); 3112 3577 } 3113 3578 ; … … 3126 3591 { 3127 3592 local_add_f(p, $1); 3593 local_nest(p); 3128 3594 $$ = $1; 3129 3595 } … … 3133 3599 { 3134 3600 void_expr_error(p, $2); 3135 $$ = cons(nsym($1), $2); 3601 $$ = cons(nsym($1), cons($2, locals_node(p))); 3602 local_unnest(p); 3136 3603 } 3137 3604 ; … … 3140 3607 { 3141 3608 void_expr_error(p, $2); 3142 $$ = cons(nsym($1), $2); 3609 $$ = cons(nsym($1), cons($2, locals_node(p))); 3610 local_unnest(p); 3143 3611 } 3144 3612 ; … … 3175 3643 | restarg_mark 3176 3644 { 3177 local_add_f(p, 0);3645 local_add_f(p, mrb_intern_lit(p->mrb, "*")); 3178 3646 $$ = -1; 3179 3647 } … … 3186 3654 f_block_arg : blkarg_mark tIDENTIFIER 3187 3655 { 3188 local_add_f(p, $2);3189 3656 $$ = $2; 3190 3657 } … … 3197 3664 | none 3198 3665 { 3199 local_add_f(p, 0);3200 3666 $$ = 0; 3201 3667 } … … 3244 3710 NODE_LINENO($$, $1); 3245 3711 } 3246 | assocs ','assoc3712 | assocs comma assoc 3247 3713 { 3248 3714 $$ = push($1, $3); 3249 3715 } 3716 ; 3717 3718 label_tag : tLABEL_TAG 3719 | tLABEL_TAG heredoc_bodies 3250 3720 ; 3251 3721 … … 3256 3726 $$ = cons($1, $3); 3257 3727 } 3258 | tLABEL arg 3728 | tIDENTIFIER label_tag arg 3729 { 3730 void_expr_error(p, $3); 3731 $$ = cons(new_sym(p, $1), $3); 3732 } 3733 | string_fragment label_tag arg 3734 { 3735 void_expr_error(p, $3); 3736 if ($1->car == (node*)NODE_DSTR) { 3737 $$ = cons(new_dsym(p, $1), $3); 3738 } 3739 else { 3740 $$ = cons(new_sym(p, new_strsym(p, $1)), $3); 3741 } 3742 } 3743 | tDSTAR arg 3259 3744 { 3260 3745 void_expr_error(p, $2); 3261 $$ = cons(new_sym(p, $1), $2); 3262 } 3263 | tLABEL_END arg 3264 { 3265 void_expr_error(p, $2); 3266 $$ = cons(new_sym(p, new_strsym(p, $1)), $2); 3267 } 3268 | tSTRING_BEG tLABEL_END arg 3269 { 3270 void_expr_error(p, $3); 3271 $$ = cons(new_sym(p, new_strsym(p, $2)), $3); 3272 } 3273 | tSTRING_BEG string_rep tLABEL_END arg 3274 { 3275 void_expr_error(p, $4); 3276 $$ = cons(new_dsym(p, push($2, $3)), $4); 3746 $$ = cons(cons((node*)NODE_KW_REST_ARGS, 0), $2); 3277 3747 } 3278 3748 ; … … 3323 3793 ; 3324 3794 3325 rparen : opt_nl ')' 3326 ; 3327 3328 rbracket : opt_nl ']' 3795 rparen : opt_terms ')' 3329 3796 ; 3330 3797 3331 3798 trailer : /* none */ 3332 | nl3799 | terms 3333 3800 | comma 3334 3801 ; … … 3362 3829 { 3363 3830 char* c; 3364 int n;3831 size_t n; 3365 3832 3366 3833 if (! p->capture_errors) { 3367 3834 #ifndef MRB_DISABLE_STDIO 3368 if (p->filename) { 3369 fprintf(stderr, "%s:%d:%d: %s\n", p->filename, p->lineno, p->column, s); 3835 if (p->filename_sym) { 3836 const char *filename = mrb_sym_name_len(p->mrb, p->filename_sym, NULL); 3837 fprintf(stderr, "%s:%d:%d: %s\n", filename, p->lineno, p->column, s); 3370 3838 } 3371 3839 else { … … 3386 3854 3387 3855 static void 3388 yyerror_ i(parser_state *p, const char *fmt, int i)3856 yyerror_c(parser_state *p, const char *msg, char c) 3389 3857 { 3390 3858 char buf[256]; 3391 3859 3392 snprintf(buf, sizeof(buf), fmt, i); 3860 strncpy(buf, msg, sizeof(buf) - 2); 3861 buf[sizeof(buf) - 2] = '\0'; 3862 strncat(buf, &c, 1); 3393 3863 yyerror(p, buf); 3394 3864 } … … 3398 3868 { 3399 3869 char* c; 3400 int n;3870 size_t n; 3401 3871 3402 3872 if (! p->capture_errors) { 3403 3873 #ifndef MRB_DISABLE_STDIO 3404 if (p->filename) { 3405 fprintf(stderr, "%s:%d:%d: %s\n", p->filename, p->lineno, p->column, s); 3874 if (p->filename_sym) { 3875 const char *filename = mrb_sym_name_len(p->mrb, p->filename_sym, NULL); 3876 fprintf(stderr, "%s:%d:%d: warning: %s\n", filename, p->lineno, p->column, s); 3406 3877 } 3407 3878 else { 3408 fprintf(stderr, "line %d:%d: %s\n", p->lineno, p->column, s);3879 fprintf(stderr, "line %d:%d: warning: %s\n", p->lineno, p->column, s); 3409 3880 } 3410 3881 #endif … … 3428 3899 3429 3900 static void 3430 yywarning_s(parser_state *p, const char * fmt, const char *s)3901 yywarning_s(parser_state *p, const char *msg, const char *s) 3431 3902 { 3432 3903 char buf[256]; 3433 3904 3434 snprintf(buf, sizeof(buf), fmt, s); 3905 strncpy(buf, msg, sizeof(buf) - 1); 3906 buf[sizeof(buf) - 1] = '\0'; 3907 strncat(buf, ": ", sizeof(buf) - strlen(buf) - 1); 3908 strncat(buf, s, sizeof(buf) - strlen(buf) - 1); 3435 3909 yywarning(p, buf); 3436 3910 } … … 3441 3915 int c; 3442 3916 3443 c = (int)(intptr_t)n->car;3917 c = intn(n->car); 3444 3918 3445 3919 if (c == NODE_NTH_REF) { 3446 yyerror_ i(p, "can't set variable $%" MRB_PRId, (mrb_int)(intptr_t)n->cdr);3920 yyerror_c(p, "can't set variable $", (char)intn(n->cdr)+'0'); 3447 3921 } 3448 3922 else if (c == NODE_BACK_REF) { 3449 yyerror_ i(p, "can't set variable $%c", (int)(intptr_t)n->cdr);3923 yyerror_c(p, "can't set variable $", (char)intn(n->cdr)); 3450 3924 } 3451 3925 else { 3452 mrb_bug(p->mrb, "Internal error in backref_error() : n=>car == % S", mrb_fixnum_value(c));3926 mrb_bug(p->mrb, "Internal error in backref_error() : n=>car == %d", c); 3453 3927 } 3454 3928 } … … 3460 3934 3461 3935 if (n == NULL) return; 3462 c = (int)(intptr_t)n->car;3936 c = intn(n->car); 3463 3937 switch (c) { 3464 3938 case NODE_BREAK: … … 3471 3945 case NODE_AND: 3472 3946 case NODE_OR: 3473 void_expr_error(p, n->cdr->car); 3474 void_expr_error(p, n->cdr->cdr); 3947 if (n->cdr) { 3948 void_expr_error(p, n->cdr->car); 3949 void_expr_error(p, n->cdr->cdr); 3950 } 3475 3951 break; 3476 3952 case NODE_BEGIN: … … 3492 3968 3493 3969 static inline int 3970 nextc0(parser_state *p) 3971 { 3972 int c; 3973 #ifndef MRB_DISABLE_STDIO 3974 if (p->f) { 3975 if (feof(p->f)) return -1; 3976 c = fgetc(p->f); 3977 if (c == EOF) return -1; 3978 } 3979 else 3980 #endif 3981 if (!p->s || p->s >= p->send) { 3982 return -1; 3983 } 3984 else { 3985 c = (unsigned char)*p->s++; 3986 } 3987 return c; 3988 } 3989 3990 static inline int 3494 3991 nextc(parser_state *p) 3495 3992 { … … 3499 3996 node *tmp; 3500 3997 3501 c = (int)(intptr_t)p->pb->car;3998 c = intn(p->pb->car); 3502 3999 tmp = p->pb; 3503 4000 p->pb = p->pb->cdr; … … 3505 4002 } 3506 4003 else { 3507 #ifndef MRB_DISABLE_STDIO 3508 if (p->f) { 3509 if (feof(p->f)) goto eof; 3510 c = fgetc(p->f); 3511 if (c == EOF) goto eof; 3512 } 3513 else 3514 #endif 3515 if (!p->s || p->s >= p->send) { 3516 goto eof; 3517 } 3518 else { 3519 c = (unsigned char)*p->s++; 3520 } 4004 c = nextc0(p); 4005 if (c < 0) goto eof; 3521 4006 } 3522 4007 if (c >= 0) { … … 3524 4009 } 3525 4010 if (c == '\r') { 3526 c = nextc(p); 3527 if (c != '\n') { 3528 pushback(p, c); 3529 return '\r'; 3530 } 3531 return c; 4011 const int lf = nextc0(p); 4012 if (lf == '\n') { 4013 return '\n'; 4014 } 4015 if (lf > 0) pushback(p, lf); 3532 4016 } 3533 4017 return c; … … 3548 4032 p->column--; 3549 4033 } 3550 p->pb = cons( (node*)(intptr_t)c, p->pb);4034 p->pb = cons(nint(c), p->pb); 3551 4035 } 3552 4036 … … 3573 4057 if (c0 == -1) return c0; /* do not skip partial EOF */ 3574 4058 if (c0 >= 0) --p->column; 3575 list = push(list, (node*)(intptr_t)c0);4059 list = push(list, nint(c0)); 3576 4060 } while(n--); 3577 4061 if (p->pb) { … … 3594 4078 peeks(parser_state *p, const char *s) 3595 4079 { 3596 int len = strlen(s);4080 size_t len = strlen(s); 3597 4081 3598 4082 #ifndef MRB_DISABLE_STDIO … … 3630 4114 s++; 3631 4115 if (peeks(p, s)) { 3632 int len = strlen(s);4116 size_t len = strlen(s); 3633 4117 3634 4118 while (len--) { … … 3752 4236 #define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1)) 3753 4237 3754 static int 4238 static int32_t 3755 4239 scan_oct(const int *start, int len, int *retlen) 3756 4240 { 3757 4241 const int *s = start; 3758 int retval = 0;4242 int32_t retval = 0; 3759 4243 3760 4244 /* mrb_assert(len <= 3) */ … … 3763 4247 retval |= *s++ - '0'; 3764 4248 } 3765 *retlen = s - start;4249 *retlen = (int)(s - start); 3766 4250 3767 4251 return retval; … … 3769 4253 3770 4254 static int32_t 3771 scan_hex( const int *start, int len, int *retlen)4255 scan_hex(parser_state *p, const int *start, int len, int *retlen) 3772 4256 { 3773 4257 static const char hexdigit[] = "0123456789abcdef0123456789ABCDEF"; 3774 4258 const int *s = start; 3775 int32_t retval = 0;4259 uint32_t retval = 0; 3776 4260 char *tmp; 3777 4261 … … 3782 4266 s++; 3783 4267 } 3784 *retlen = s - start;3785 3786 return retval;4268 *retlen = (int)(s - start); 4269 4270 return (int32_t)retval; 3787 4271 } 3788 4272 … … 3790 4274 read_escape_unicode(parser_state *p, int limit) 3791 4275 { 3792 int32_t c;3793 4276 int buf[9]; 3794 4277 int i; 4278 int32_t hex; 3795 4279 3796 4280 /* Look for opening brace */ 3797 4281 i = 0; 3798 4282 buf[0] = nextc(p); 3799 if (buf[0] < 0) goto eof; 4283 if (buf[0] < 0) { 4284 eof: 4285 yyerror(p, "invalid escape character syntax"); 4286 return -1; 4287 } 3800 4288 if (ISXDIGIT(buf[0])) { 3801 4289 /* \uxxxx form */ … … 3812 4300 pushback(p, buf[0]); 3813 4301 } 3814 c = scan_hex(buf, i, &i); 3815 if (i == 0) { 3816 eof: 3817 yyerror(p, "Invalid escape character syntax"); 4302 hex = scan_hex(p, buf, i, &i); 4303 if (i == 0 || hex > 0x10FFFF || (hex & 0xFFFFF800) == 0xD800) { 4304 yyerror(p, "invalid Unicode code point"); 3818 4305 return -1; 3819 4306 } 3820 if (c < 0 || c > 0x10FFFF || (c & 0xFFFFF800) == 0xD800) { 3821 yyerror(p, "Invalid Unicode code point"); 3822 return -1; 3823 } 3824 return c; 4307 return hex; 3825 4308 } 3826 4309 … … 3888 4371 } 3889 4372 } 3890 c = scan_hex(buf, i, &i);3891 4373 if (i == 0) { 3892 yyerror(p, " Invalid escape character syntax");3893 return 0;3894 } 3895 }3896 return c;4374 yyerror(p, "invalid hex escape"); 4375 return -1; 4376 } 4377 return scan_hex(p, buf, i, &i); 4378 } 3897 4379 3898 4380 case 'u': /* Unicode */ … … 3961 4443 int c; 3962 4444 string_type type = (string_type)(intptr_t)p->lex_strterm->car; 3963 int nest_level = (intptr_t)p->lex_strterm->cdr->car;3964 int beg = (intptr_t)p->lex_strterm->cdr->cdr->car;3965 int end = (intptr_t)p->lex_strterm->cdr->cdr->cdr;4445 int nest_level = intn(p->lex_strterm->cdr->car); 4446 int beg = intn(p->lex_strterm->cdr->cdr->car); 4447 int end = intn(p->lex_strterm->cdr->cdr->cdr); 3966 4448 parser_heredoc_info *hinf = (type & STR_FUNC_HEREDOC) ? parsing_heredoc_inf(p) : NULL; 3967 int cmd_state = p->cmd_start;3968 4449 3969 4450 if (beg == 0) beg = -3; /* should never happen */ … … 3990 4471 } 3991 4472 if ((len-1 == hinf->term_len) && (strncmp(s, hinf->term, len-1) == 0)) { 3992 if (c < 0) { 3993 p->parsing_heredoc = NULL; 3994 } 3995 else { 3996 return tHEREDOC_END; 3997 } 4473 return tHEREDOC_END; 3998 4474 } 3999 4475 } 4000 4476 if (c < 0) { 4001 4477 char buf[256]; 4002 snprintf(buf, sizeof(buf), "can't find heredoc delimiter \"%s\" anywhere before EOF", hinf->term); 4003 yyerror(p, buf); 4478 const char s1[] = "can't find heredoc delimiter \""; 4479 const char s2[] = "\" anywhere before EOF"; 4480 4481 if (sizeof(s1)+sizeof(s2)+strlen(hinf->term)+1 >= sizeof(buf)) { 4482 yyerror(p, "can't find heredoc delimiter anywhere before EOF"); 4483 } else { 4484 strcpy(buf, s1); 4485 strcat(buf, hinf->term); 4486 strcat(buf, s2); 4487 yyerror(p, buf); 4488 } 4004 4489 return 0; 4005 4490 } … … 4013 4498 else if (c == beg) { 4014 4499 nest_level++; 4015 p->lex_strterm->cdr->car = (node*)(intptr_t)nest_level;4500 p->lex_strterm->cdr->car = nint(nest_level); 4016 4501 } 4017 4502 else if (c == end) { 4018 4503 nest_level--; 4019 p->lex_strterm->cdr->car = (node*)(intptr_t)nest_level;4504 p->lex_strterm->cdr->car = nint(nest_level); 4020 4505 } 4021 4506 else if (c == '\\') { … … 4119 4604 4120 4605 tokfix(p); 4121 p->lstate = EXPR_END ;4606 p->lstate = EXPR_ENDARG; 4122 4607 end_strterm(p); 4123 4608 … … 4145 4630 case 'u': f |= 16; break; 4146 4631 case 'n': f |= 32; break; 4632 case 'o': break; 4147 4633 default: tokadd(p, re_opt); break; 4148 4634 } … … 4151 4637 if (toklen(p)) { 4152 4638 char msg[128]; 4639 4640 strcpy(msg, "unknown regexp option"); 4153 4641 tokfix(p); 4154 snprintf(msg, sizeof(msg), "unknown regexp option%s - %s", 4155 toklen(p) > 1 ? "s" : "", tok(p)); 4642 if (toklen(p) > 1) { 4643 strcat(msg, "s"); 4644 } 4645 strcat(msg, " - "); 4646 strncat(msg, tok(p), sizeof(msg) - strlen(msg) - 1); 4156 4647 yyerror(p, msg); 4157 4648 } … … 4180 4671 } 4181 4672 pylval.nd = new_str(p, tok(p), toklen(p)); 4182 if (IS_LABEL_POSSIBLE()) {4183 if (IS_LABEL_SUFFIX(0)) {4184 p->lstate = EXPR_BEG;4185 nextc(p);4186 return tLABEL_END;4187 }4188 }4189 4673 4190 4674 return tSTRING; 4191 4675 } 4192 4676 4677 static int 4678 number_literal_suffix(parser_state *p) 4679 { 4680 int c, result = 0; 4681 node *list = 0; 4682 int column = p->column; 4683 int mask = NUM_SUFFIX_R|NUM_SUFFIX_I; 4684 4685 while ((c = nextc(p)) != -1) { 4686 list = push(list, (node*)(intptr_t)c); 4687 4688 if ((mask & NUM_SUFFIX_I) && c == 'i') { 4689 result |= (mask & NUM_SUFFIX_I); 4690 mask &= ~NUM_SUFFIX_I; 4691 /* r after i, rational of complex is disallowed */ 4692 mask &= ~NUM_SUFFIX_R; 4693 continue; 4694 } 4695 if ((mask & NUM_SUFFIX_R) && c == 'r') { 4696 result |= (mask & NUM_SUFFIX_R); 4697 mask &= ~NUM_SUFFIX_R; 4698 continue; 4699 } 4700 if (!ISASCII(c) || ISALPHA(c) || c == '_') { 4701 p->column = column; 4702 if (p->pb) { 4703 p->pb = append((node*)list, p->pb); 4704 } 4705 else { 4706 p->pb = list; 4707 } 4708 return 0; 4709 } 4710 pushback(p, c); 4711 break; 4712 } 4713 return result; 4714 } 4193 4715 4194 4716 static int … … 4311 4833 case -2: /* end of a file */ 4312 4834 case '\n': 4313 4835 maybe_heredoc: 4314 4836 heredoc_treat_nextline(p); 4315 switch (p->lstate) {4316 case EXPR_BEG:4317 case EXPR_FNAME:4318 case EXPR_DOT:4319 case EXPR_CLASS:4320 case EXPR_VALUE:4321 p->lineno++;4322 4837 p->column = 0; 4838 switch (p->lstate) { 4839 case EXPR_BEG: 4840 case EXPR_FNAME: 4841 case EXPR_DOT: 4842 case EXPR_CLASS: 4843 case EXPR_VALUE: 4844 p->lineno++; 4845 if (p->parsing_heredoc != NULL) { 4846 if (p->lex_strterm) { 4847 return parse_string(p); 4848 } 4849 } 4850 goto retry; 4851 default: 4852 break; 4853 } 4323 4854 if (p->parsing_heredoc != NULL) { 4324 if (p->lex_strterm) { 4325 return parse_string(p); 4326 } 4327 } 4328 goto retry; 4329 default: 4330 break; 4331 } 4332 if (p->parsing_heredoc != NULL) { 4855 return '\n'; 4856 } 4857 while ((c = nextc(p))) { 4858 switch (c) { 4859 case ' ': case '\t': case '\f': case '\r': 4860 case '\13': /* '\v' */ 4861 space_seen = 1; 4862 break; 4863 case '#': /* comment as a whitespace */ 4864 pushback(p, '#'); 4865 p->lineno++; 4866 goto retry; 4867 case '.': 4868 if (!peek(p, '.')) { 4869 pushback(p, '.'); 4870 p->lineno++; 4871 goto retry; 4872 } 4873 pushback(p, c); 4874 goto normal_newline; 4875 case '&': 4876 if (peek(p, '.')) { 4877 pushback(p, '&'); 4878 p->lineno++; 4879 goto retry; 4880 } 4881 pushback(p, c); 4882 goto normal_newline; 4883 case -1: /* EOF */ 4884 case -2: /* end of a file */ 4885 goto normal_newline; 4886 default: 4887 pushback(p, c); 4888 goto normal_newline; 4889 } 4890 } 4891 normal_newline: 4892 p->cmd_start = TRUE; 4893 p->lstate = EXPR_BEG; 4333 4894 return '\n'; 4334 }4335 while ((c = nextc(p))) {4336 switch (c) {4337 case ' ': case '\t': case '\f': case '\r':4338 case '\13': /* '\v' */4339 space_seen = 1;4340 break;4341 case '.':4342 if ((c = nextc(p)) != '.') {4343 pushback(p, c);4344 pushback(p, '.');4345 goto retry;4346 }4347 case -1: /* EOF */4348 case -2: /* end of a file */4349 goto normal_newline;4350 default:4351 pushback(p, c);4352 goto normal_newline;4353 }4354 }4355 normal_newline:4356 p->cmd_start = TRUE;4357 p->lstate = EXPR_BEG;4358 return '\n';4359 4895 4360 4896 case '*': 4361 4897 if ((c = nextc(p)) == '*') { 4362 4898 if ((c = nextc(p)) == '=') { 4363 pylval.id = intern ("**",2);4899 pylval.id = intern_lit("**"); 4364 4900 p->lstate = EXPR_BEG; 4365 4901 return tOP_ASGN; 4366 4902 } 4367 4903 pushback(p, c); 4368 c = tPOW; 4904 if (IS_SPCARG(c)) { 4905 yywarning(p, "'**' interpreted as argument prefix"); 4906 c = tDSTAR; 4907 } 4908 else if (IS_BEG()) { 4909 c = tDSTAR; 4910 } 4911 else { 4912 c = tPOW; /* "**", "argument prefix" */ 4913 } 4369 4914 } 4370 4915 else { 4371 4916 if (c == '=') { 4372 pylval.id = intern_ c('*');4917 pylval.id = intern_lit("*"); 4373 4918 p->lstate = EXPR_BEG; 4374 4919 return tOP_ASGN; … … 4486 5031 if (c == '<') { 4487 5032 if ((c = nextc(p)) == '=') { 4488 pylval.id = intern ("<<",2);5033 pylval.id = intern_lit("<<"); 4489 5034 p->lstate = EXPR_BEG; 4490 5035 return tOP_ASGN; … … 4508 5053 if (c == '>') { 4509 5054 if ((c = nextc(p)) == '=') { 4510 pylval.id = intern (">>",2);5055 pylval.id = intern_lit(">>"); 4511 5056 p->lstate = EXPR_BEG; 4512 5057 return tOP_ASGN; … … 4579 5124 if (c2) { 4580 5125 char buf[256]; 4581 snprintf(buf, sizeof(buf), "invalid character syntax; use ?\\%c", c2); 5126 char cc[] = { (char)c2, '\0' }; 5127 5128 strcpy(buf, "invalid character syntax; use ?\\"); 5129 strncat(buf, cc, 2); 4582 5130 yyerror(p, buf); 4583 5131 } … … 4590 5138 newtok(p); 4591 5139 /* need support UTF-8 if configured */ 4592 if (( isalnum(c) || c == '_')) {5140 if ((ISALNUM(c) || c == '_')) { 4593 5141 int c2 = nextc(p); 4594 5142 pushback(p, c2); 4595 if (( isalnum(c2) || c2 == '_')) {5143 if ((ISALNUM(c2) || c2 == '_')) { 4596 5144 goto ternary; 4597 5145 } … … 4606 5154 tokfix(p); 4607 5155 pylval.nd = new_str(p, tok(p), toklen(p)); 4608 p->lstate = EXPR_END ;5156 p->lstate = EXPR_ENDARG; 4609 5157 return tCHAR; 4610 5158 … … 4613 5161 p->lstate = EXPR_BEG; 4614 5162 if ((c = nextc(p)) == '=') { 4615 pylval.id = intern ("&&",2);5163 pylval.id = intern_lit("&&"); 4616 5164 p->lstate = EXPR_BEG; 4617 5165 return tOP_ASGN; … … 4625 5173 } 4626 5174 else if (c == '=') { 4627 pylval.id = intern_ c('&');5175 pylval.id = intern_lit("&"); 4628 5176 p->lstate = EXPR_BEG; 4629 5177 return tOP_ASGN; … … 4652 5200 p->lstate = EXPR_BEG; 4653 5201 if ((c = nextc(p)) == '=') { 4654 pylval.id = intern ("||",2);5202 pylval.id = intern_lit("||"); 4655 5203 p->lstate = EXPR_BEG; 4656 5204 return tOP_ASGN; … … 4660 5208 } 4661 5209 if (c == '=') { 4662 pylval.id = intern_ c('|');5210 pylval.id = intern_lit("|"); 4663 5211 p->lstate = EXPR_BEG; 4664 5212 return tOP_ASGN; … … 4684 5232 } 4685 5233 if (c == '=') { 4686 pylval.id = intern_ c('+');5234 pylval.id = intern_lit("+"); 4687 5235 p->lstate = EXPR_BEG; 4688 5236 return tOP_ASGN; … … 4712 5260 } 4713 5261 if (c == '=') { 4714 pylval.id = intern_ c('-');5262 pylval.id = intern_lit("-"); 4715 5263 p->lstate = EXPR_BEG; 4716 5264 return tOP_ASGN; … … 4753 5301 { 4754 5302 int is_float, seen_point, seen_e, nondigit; 5303 int suffix = 0; 4755 5304 4756 5305 is_float = seen_point = seen_e = nondigit = 0; 4757 p->lstate = EXPR_END ;5306 p->lstate = EXPR_ENDARG; 4758 5307 newtok(p); 4759 5308 if (c == '-' || c == '+') { … … 4786 5335 } 4787 5336 else if (nondigit) goto trailing_uc; 4788 pylval.nd = new_int(p, tok(p), 16); 5337 suffix = number_literal_suffix(p); 5338 pylval.nd = new_int(p, tok(p), 16, suffix); 4789 5339 return tINTEGER; 4790 5340 } … … 4810 5360 } 4811 5361 else if (nondigit) goto trailing_uc; 4812 pylval.nd = new_int(p, tok(p), 2); 5362 suffix = number_literal_suffix(p); 5363 pylval.nd = new_int(p, tok(p), 2, suffix); 4813 5364 return tINTEGER; 4814 5365 } … … 4834 5385 } 4835 5386 else if (nondigit) goto trailing_uc; 4836 pylval.nd = new_int(p, tok(p), 10); 5387 suffix = number_literal_suffix(p); 5388 pylval.nd = new_int(p, tok(p), 10, suffix); 4837 5389 return tINTEGER; 4838 5390 } … … 4867 5419 tokfix(p); 4868 5420 if (nondigit) goto trailing_uc; 4869 pylval.nd = new_int(p, tok(p), 8); 5421 suffix = number_literal_suffix(p); 5422 pylval.nd = new_int(p, tok(p), 8, suffix); 4870 5423 return tINTEGER; 4871 5424 } … … 4884 5437 else { 4885 5438 pushback(p, c); 4886 pylval.nd = new_int(p, "0", 10); 5439 suffix = number_literal_suffix(p); 5440 pylval.nd = new_int(p, "0", 10, suffix); 4887 5441 return tINTEGER; 4888 5442 } … … 4952 5506 if (nondigit) { 4953 5507 trailing_uc: 4954 yyerror_ i(p, "trailing '%c' in number",nondigit);5508 yyerror_c(p, "trailing non digit in number: ", (char)nondigit); 4955 5509 } 4956 5510 tokfix(p); 4957 5511 if (is_float) { 5512 #ifdef MRB_WITHOUT_FLOAT 5513 yywarning_s(p, "floating point numbers are not supported", tok(p)); 5514 pylval.nd = new_int(p, "0", 10, 0); 5515 return tINTEGER; 5516 #else 4958 5517 double d; 4959 5518 char *endp; … … 4962 5521 d = mrb_float_read(tok(p), &endp); 4963 5522 if (d == 0 && endp == tok(p)) { 4964 yywarning_s(p, "corrupted float value %s", tok(p));5523 yywarning_s(p, "corrupted float value", tok(p)); 4965 5524 } 4966 5525 else if (errno == ERANGE) { 4967 yywarning_s(p, "float %sout of range", tok(p));5526 yywarning_s(p, "float out of range", tok(p)); 4968 5527 errno = 0; 4969 5528 } 4970 pylval.nd = new_float(p, tok(p)); 5529 suffix = number_literal_suffix(p); 5530 pylval.nd = new_float(p, tok(p), suffix); 4971 5531 return tFLOAT; 4972 } 4973 pylval.nd = new_int(p, tok(p), 10); 5532 #endif 5533 } 5534 suffix = number_literal_suffix(p); 5535 pylval.nd = new_int(p, tok(p), 10, suffix); 4974 5536 return tINTEGER; 4975 5537 } … … 4985 5547 p->lstate = EXPR_ENDFN; 4986 5548 else 4987 p->lstate = EXPR_END ARG;5549 p->lstate = EXPR_END; 4988 5550 return c; 4989 5551 … … 4998 5560 return tCOLON2; 4999 5561 } 5000 if ( IS_END() || ISSPACE(c)) {5562 if (!space_seen && IS_END()) { 5001 5563 pushback(p, c); 5002 5564 p->lstate = EXPR_BEG; 5003 return ':'; 5565 return tLABEL_TAG; 5566 } 5567 if (!ISSPACE(c) || IS_BEG()) { 5568 pushback(p, c); 5569 p->lstate = EXPR_FNAME; 5570 return tSYMBEG; 5004 5571 } 5005 5572 pushback(p, c); 5006 p->lstate = EXPR_ FNAME;5007 return tSYMBEG;5573 p->lstate = EXPR_BEG; 5574 return ':'; 5008 5575 5009 5576 case '/': … … 5013 5580 } 5014 5581 if ((c = nextc(p)) == '=') { 5015 pylval.id = intern_ c('/');5582 pylval.id = intern_lit("/"); 5016 5583 p->lstate = EXPR_BEG; 5017 5584 return tOP_ASGN; … … 5032 5599 case '^': 5033 5600 if ((c = nextc(p)) == '=') { 5034 pylval.id = intern_ c('^');5601 pylval.id = intern_lit("^"); 5035 5602 p->lstate = EXPR_BEG; 5036 5603 return tOP_ASGN; … … 5070 5637 } 5071 5638 else if (IS_SPCARG(-1)) { 5639 c = tLPAREN_ARG; 5640 } 5641 else if (p->lstate == EXPR_END && space_seen) { 5072 5642 c = tLPAREN_ARG; 5073 5643 } … … 5147 5717 else { 5148 5718 term = nextc(p); 5149 if ( isalnum(term)) {5719 if (ISALNUM(term)) { 5150 5720 yyerror(p, "unknown type of %string"); 5151 5721 return 0; … … 5206 5776 } 5207 5777 if ((c = nextc(p)) == '=') { 5208 pylval.id = intern_ c('%');5778 pylval.id = intern_lit("%"); 5209 5779 p->lstate = EXPR_BEG; 5210 5780 return tOP_ASGN; … … 5260 5830 tokadd(p, c); 5261 5831 tokfix(p); 5262 pylval.id = intern _cstr(tok(p));5832 pylval.id = intern(tok(p), toklen(p)); 5263 5833 return tGVAR; 5264 5834 … … 5270 5840 gvar: 5271 5841 tokfix(p); 5272 pylval.id = intern _cstr(tok(p));5842 pylval.id = intern(tok(p), toklen(p)); 5273 5843 return tGVAR; 5274 5844 … … 5291 5861 tokadd(p, c); 5292 5862 c = nextc(p); 5293 } while (c >= 0 && isdigit(c));5863 } while (c >= 0 && ISDIGIT(c)); 5294 5864 pushback(p, c); 5295 5865 if (last_state == EXPR_FNAME) goto gvar; … … 5298 5868 unsigned long n = strtoul(tok(p), NULL, 10); 5299 5869 if (n > INT_MAX) { 5300 yyerror _i(p, "capture group index must be <= %d", INT_MAX);5870 yyerror(p, "capture group index must be <= " MRB_STRINGIZE(INT_MAX)); 5301 5871 return 0; 5302 5872 } … … 5333 5903 return 0; 5334 5904 } 5335 else if ( isdigit(c)) {5905 else if (ISDIGIT(c)) { 5336 5906 if (p->tidx == 1) { 5337 yyerror_ i(p, "'@%c' is not allowed as an instance variable name", c);5907 yyerror_c(p, "wrong instance variable name: @", c); 5338 5908 } 5339 5909 else { 5340 yyerror_ i(p, "'@@%c' is not allowed as a class variable name", c);5910 yyerror_c(p, "wrong class variable name: @@", c); 5341 5911 } 5342 5912 return 0; … … 5354 5924 default: 5355 5925 if (!identchar(c)) { 5356 yyerror_i(p, "Invalid char '\\x%02X' in expression", c); 5926 char buf[36]; 5927 const char s[] = "Invalid char in expression: 0x"; 5928 const char hexdigits[] = "0123456789ABCDEF"; 5929 5930 strcpy(buf, s); 5931 buf[sizeof(s)-1] = hexdigits[(c & 0xf0) >> 4]; 5932 buf[sizeof(s)] = hexdigits[(c & 0x0f)]; 5933 buf[sizeof(s)+1] = 0; 5934 yyerror(p, buf); 5357 5935 goto retry; 5358 5936 } … … 5400 5978 break; 5401 5979 5980 case '_': 5981 if (toklen(p) == 2 && ISDIGIT(tok(p)[1]) && p->nvars) { 5982 int n = tok(p)[1] - '0'; 5983 int nvar; 5984 5985 if (n > 0) { 5986 node *nvars = p->nvars->cdr; 5987 5988 while (nvars) { 5989 nvar = intn(nvars->car); 5990 if (nvar == -2) break; /* top of the scope */ 5991 if (nvar > 0) { 5992 yywarning(p, "numbered parameter used in outer block"); 5993 break; 5994 } 5995 nvars->car = nint(-1); 5996 nvars = nvars->cdr; 5997 } 5998 nvar = intn(p->nvars->car); 5999 if (nvar == -1) { 6000 yywarning(p, "numbered parameter used in inner block"); 6001 } 6002 if (nvar >= -1) { 6003 pylval.num = n; 6004 p->lstate = EXPR_END; 6005 return tNUMPARAM; 6006 } 6007 else { 6008 yywarning(p, "identifier for numbered parameter; consider another name"); 6009 } 6010 } 6011 } 6012 /* fall through */ 5402 6013 default: 5403 6014 if (toklast(p) == '!' || toklast(p) == '?') { … … 5406 6017 else { 5407 6018 if (p->lstate == EXPR_FNAME) { 6019 if ((c = nextc(p)) == '=' && !peek(p, '~') && !peek(p, '>') && 6020 (!peek(p, '=') || (peek_n(p, '>', 1)))) { 6021 result = tIDENTIFIER; 6022 tokadd(p, c); 6023 tokfix(p); 6024 } 6025 else { 6026 pushback(p, c); 6027 } 5408 6028 if ((c = nextc(p)) == '=' && !peek(p, '~') && !peek(p, '>') && 5409 6029 (!peek(p, '=') || (peek_n(p, '>', 1)))) { … … 5426 6046 if (IS_LABEL_POSSIBLE()) { 5427 6047 if (IS_LABEL_SUFFIX(0)) { 5428 p->lstate = EXPR_BEG; 5429 nextc(p); 6048 p->lstate = EXPR_END; 5430 6049 tokfix(p); 5431 pylval.id = intern _cstr(tok(p));5432 return t LABEL;6050 pylval.id = intern(tok(p), toklen(p)); 6051 return tIDENTIFIER; 5433 6052 } 5434 6053 } … … 5488 6107 } 5489 6108 { 5490 mrb_sym ident = intern _cstr(tok(p));6109 mrb_sym ident = intern(tok(p), toklen(p)); 5491 6110 5492 6111 pylval.id = ident; 5493 #if 0 5494 if (last_state != EXPR_DOT && islower(tok(p)[0]) && lvar_defined(ident)) { 6112 if (last_state != EXPR_DOT && ISLOWER(tok(p)[0]) && local_var_p(p, ident)) { 5495 6113 p->lstate = EXPR_END; 5496 6114 } 5497 #endif5498 6115 } 5499 6116 return result; … … 5524 6141 p->capture_errors = cxt->capture_errors; 5525 6142 p->no_optimize = cxt->no_optimize; 6143 p->on_eval = cxt->on_eval; 5526 6144 if (cxt->partial_hook) { 5527 6145 p->cxt = cxt; … … 5536 6154 5537 6155 if (!cxt) return; 5538 if ( (int)(intptr_t)p->tree->car!= NODE_SCOPE) return;6156 if (intn(p->tree->car) != NODE_SCOPE) return; 5539 6157 n0 = n = p->tree->cdr->car; 5540 6158 while (n) { … … 5559 6177 5560 6178 MRB_TRY(p->jmp) { 5561 int n ;6179 int n = 1; 5562 6180 5563 6181 p->cmd_start = TRUE; … … 5675 6293 { 5676 6294 if (s) { 5677 int len = strlen(s);6295 size_t len = strlen(s); 5678 6296 char *p = (char *)mrb_malloc(mrb, len + 1); 5679 6297 … … 5702 6320 5703 6321 sym = mrb_intern_cstr(p->mrb, f); 5704 p->filename = mrb_sym2name_len(p->mrb, sym, NULL);6322 p->filename_sym = sym; 5705 6323 p->lineno = (p->filename_table_length > 0)? 0 : 1; 5706 6324 5707 6325 for (i = 0; i < p->filename_table_length; ++i) { 5708 6326 if (p->filename_table[i] == sym) { 5709 p->current_filename_index = i;6327 p->current_filename_index = (int)i; 5710 6328 return; 5711 6329 } 5712 6330 } 5713 6331 6332 if (p->filename_table_length == UINT16_MAX) { 6333 yyerror(p, "too many files to compile"); 6334 return; 6335 } 5714 6336 p->current_filename_index = p->filename_table_length++; 5715 6337 5716 6338 new_table = (mrb_sym*)parser_palloc(p, sizeof(mrb_sym) * p->filename_table_length); 5717 6339 if (p->filename_table) { 5718 memmove(new_table, p->filename_table, sizeof(mrb_sym) * p-> filename_table_length);6340 memmove(new_table, p->filename_table, sizeof(mrb_sym) * p->current_filename_index); 5719 6341 } 5720 6342 p->filename_table = new_table; … … 5722 6344 } 5723 6345 5724 MRB_API char const*6346 MRB_API mrb_sym 5725 6347 mrb_parser_get_filename(struct mrb_parser_state* p, uint16_t idx) { 5726 if (idx >= p->filename_table_length) { return NULL; }6348 if (idx >= p->filename_table_length) return 0; 5727 6349 else { 5728 return mrb_sym2name_len(p->mrb, p->filename_table[idx], NULL);6350 return p->filename_table[idx]; 5729 6351 } 5730 6352 } … … 5747 6369 5748 6370 MRB_API parser_state* 5749 mrb_parse_nstring(mrb_state *mrb, const char *s, int len, mrbc_context *c)6371 mrb_parse_nstring(mrb_state *mrb, const char *s, size_t len, mrbc_context *c) 5750 6372 { 5751 6373 parser_state *p; … … 5778 6400 } 5779 6401 if (!p->tree || p->nerr) { 6402 if (c) c->parser_nerr = p->nerr; 5780 6403 if (p->capture_errors) { 5781 6404 char buf[256]; 5782 int n; 5783 5784 n = snprintf(buf, sizeof(buf), "line %d: %s\n", 5785 p->error_buffer[0].lineno, p->error_buffer[0].message); 5786 mrb->exc = mrb_obj_ptr(mrb_exc_new(mrb, E_SYNTAX_ERROR, buf, n)); 6405 6406 strcpy(buf, "line "); 6407 dump_int(p->error_buffer[0].lineno, buf+5); 6408 strcat(buf, ": "); 6409 strncat(buf, p->error_buffer[0].message, sizeof(buf) - strlen(buf) - 1); 6410 mrb->exc = mrb_obj_ptr(mrb_exc_new(mrb, E_SYNTAX_ERROR, buf, strlen(buf))); 5787 6411 mrb_parser_free(p); 5788 6412 return mrb_undef_value(); … … 5817 6441 } 5818 6442 } 5819 proc->target_class = target;6443 MRB_PROC_SET_TARGET_CLASS(proc, target); 5820 6444 if (mrb->c->ci) { 5821 6445 mrb->c->ci->target_class = target; … … 5841 6465 5842 6466 MRB_API mrb_value 5843 mrb_load_nstring_cxt(mrb_state *mrb, const char *s, int len, mrbc_context *c)6467 mrb_load_nstring_cxt(mrb_state *mrb, const char *s, size_t len, mrbc_context *c) 5844 6468 { 5845 6469 return mrb_load_exec(mrb, mrb_parse_nstring(mrb, s, len, c), c); … … 5847 6471 5848 6472 MRB_API mrb_value 5849 mrb_load_nstring(mrb_state *mrb, const char *s, int len)6473 mrb_load_nstring(mrb_state *mrb, const char *s, size_t len) 5850 6474 { 5851 6475 return mrb_load_nstring_cxt(mrb, s, len, NULL); … … 5885 6509 } 5886 6510 6511 static void 6512 dump_args(mrb_state *mrb, node *n, int offset) 6513 { 6514 if (n->car) { 6515 dump_prefix(n, offset+1); 6516 printf("mandatory args:\n"); 6517 dump_recur(mrb, n->car, offset+2); 6518 } 6519 n = n->cdr; 6520 if (n->car) { 6521 dump_prefix(n, offset+1); 6522 printf("optional args:\n"); 6523 { 6524 node *n2 = n->car; 6525 6526 while (n2) { 6527 dump_prefix(n2, offset+2); 6528 printf("%s=\n", mrb_sym_name(mrb, sym(n2->car->car))); 6529 mrb_parser_dump(mrb, n2->car->cdr, offset+3); 6530 n2 = n2->cdr; 6531 } 6532 } 6533 } 6534 n = n->cdr; 6535 if (n->car) { 6536 dump_prefix(n, offset+1); 6537 printf("rest=*%s\n", mrb_sym_name(mrb, sym(n->car))); 6538 } 6539 n = n->cdr; 6540 if (n->car) { 6541 dump_prefix(n, offset+1); 6542 printf("post mandatory args:\n"); 6543 dump_recur(mrb, n->car, offset+2); 6544 } 6545 6546 n = n->cdr; 6547 if (n) { 6548 mrb_assert(intn(n->car) == NODE_ARGS_TAIL); 6549 mrb_parser_dump(mrb, n, offset); 6550 } 6551 } 6552 6553 /* 6554 * This function restores the GC arena on return. 6555 * For this reason, if a process that further generates an object is 6556 * performed at the caller, the string pointer returned as the return 6557 * value may become invalid. 6558 */ 6559 static const char* 6560 str_dump(mrb_state *mrb, const char *str, int len) 6561 { 6562 mrb_int ai = mrb_gc_arena_save(mrb); 6563 mrb_value s; 6564 # if INT_MAX > MRB_INT_MAX / 4 6565 /* check maximum length with "\xNN" charactor */ 6566 if (len > MRB_INT_MAX / 4) { 6567 len = MRB_INT_MAX / 4; 6568 } 6569 # endif 6570 s = mrb_str_new(mrb, str, (mrb_int)len); 6571 s = mrb_str_dump(mrb, s); 6572 mrb_gc_arena_restore(mrb, ai); 6573 return RSTRING_PTR(s); 6574 } 5887 6575 #endif 5888 6576 … … 5896 6584 again: 5897 6585 dump_prefix(tree, offset); 5898 nodetype = (int)(intptr_t)tree->car;6586 nodetype = intn(tree->car); 5899 6587 tree = tree->cdr; 5900 6588 switch (nodetype) { … … 5956 6644 5957 6645 case NODE_LAMBDA: 5958 printf("NODE_BLOCK:\n"); 6646 printf("NODE_LAMBDA:\n"); 6647 dump_prefix(tree, offset); 5959 6648 goto block; 5960 6649 … … 5964 6653 tree = tree->cdr; 5965 6654 if (tree->car) { 5966 node *n = tree->car; 5967 5968 if (n->car) { 5969 dump_prefix(n, offset+1); 5970 printf("mandatory args:\n"); 5971 dump_recur(mrb, n->car, offset+2); 5972 } 5973 n = n->cdr; 5974 if (n->car) { 5975 dump_prefix(n, offset+1); 5976 printf("optional args:\n"); 5977 { 5978 node *n2 = n->car; 5979 5980 while (n2) { 5981 dump_prefix(n2, offset+2); 5982 printf("%s=", mrb_sym2name(mrb, sym(n2->car->car))); 5983 mrb_parser_dump(mrb, n2->car->cdr, 0); 5984 n2 = n2->cdr; 5985 } 5986 } 5987 } 5988 n = n->cdr; 5989 if (n->car) { 5990 dump_prefix(n, offset+1); 5991 printf("rest=*%s\n", mrb_sym2name(mrb, sym(n->car))); 5992 } 5993 n = n->cdr; 5994 if (n->car) { 5995 dump_prefix(n, offset+1); 5996 printf("post mandatory args:\n"); 5997 dump_recur(mrb, n->car, offset+2); 5998 } 5999 if (n->cdr) { 6000 dump_prefix(n, offset+1); 6001 printf("blk=&%s\n", mrb_sym2name(mrb, sym(n->cdr))); 6002 } 6655 dump_args(mrb, tree->car, offset+1); 6003 6656 } 6004 6657 dump_prefix(tree, offset+1); … … 6123 6776 if (n2->car) { 6124 6777 if (!first_lval) printf(", "); 6125 printf("%s", mrb_sym 2name(mrb, sym(n2->car)));6778 printf("%s", mrb_sym_name(mrb, sym(n2->car))); 6126 6779 first_lval = FALSE; 6127 6780 } … … 6151 6804 dump_prefix(tree, offset+1); 6152 6805 printf("method='%s' (%d)\n", 6153 mrb_sym 2name(mrb, sym(tree->cdr->car)),6154 (int)(intptr_t)tree->cdr->car);6806 mrb_sym_dump(mrb, sym(tree->cdr->car)), 6807 intn(tree->cdr->car)); 6155 6808 tree = tree->cdr->cdr->car; 6156 6809 if (tree) { … … 6182 6835 mrb_parser_dump(mrb, tree->car, offset+1); 6183 6836 dump_prefix(tree, offset+1); 6184 printf("::%s\n", mrb_sym 2name(mrb, sym(tree->cdr)));6837 printf("::%s\n", mrb_sym_name(mrb, sym(tree->cdr))); 6185 6838 break; 6186 6839 6187 6840 case NODE_COLON3: 6188 printf("NODE_COLON3: ::%s\n", mrb_sym 2name(mrb, sym(tree)));6841 printf("NODE_COLON3: ::%s\n", mrb_sym_name(mrb, sym(tree))); 6189 6842 break; 6190 6843 … … 6196 6849 case NODE_HASH: 6197 6850 printf("NODE_HASH:\n"); 6851 while (tree) { 6852 dump_prefix(tree, offset+1); 6853 printf("key:\n"); 6854 mrb_parser_dump(mrb, tree->car->car, offset+2); 6855 dump_prefix(tree, offset+1); 6856 printf("value:\n"); 6857 mrb_parser_dump(mrb, tree->car->cdr, offset+2); 6858 tree = tree->cdr; 6859 } 6860 break; 6861 6862 case NODE_KW_HASH: 6863 printf("NODE_KW_HASH:\n"); 6198 6864 while (tree) { 6199 6865 dump_prefix(tree, offset+1); … … 6269 6935 tree = tree->cdr; 6270 6936 dump_prefix(tree, offset+1); 6271 printf("op='%s' (%d)\n", mrb_sym 2name(mrb, sym(tree->car)), (int)(intptr_t)tree->car);6937 printf("op='%s' (%d)\n", mrb_sym_name(mrb, sym(tree->car)), intn(tree->car)); 6272 6938 tree = tree->cdr; 6273 6939 mrb_parser_dump(mrb, tree->car, offset+1); … … 6321 6987 6322 6988 case NODE_LVAR: 6323 printf("NODE_LVAR %s\n", mrb_sym 2name(mrb, sym(tree)));6989 printf("NODE_LVAR %s\n", mrb_sym_name(mrb, sym(tree))); 6324 6990 break; 6325 6991 6326 6992 case NODE_GVAR: 6327 printf("NODE_GVAR %s\n", mrb_sym 2name(mrb, sym(tree)));6993 printf("NODE_GVAR %s\n", mrb_sym_name(mrb, sym(tree))); 6328 6994 break; 6329 6995 6330 6996 case NODE_IVAR: 6331 printf("NODE_IVAR %s\n", mrb_sym 2name(mrb, sym(tree)));6997 printf("NODE_IVAR %s\n", mrb_sym_name(mrb, sym(tree))); 6332 6998 break; 6333 6999 6334 7000 case NODE_CVAR: 6335 printf("NODE_CVAR %s\n", mrb_sym2name(mrb, sym(tree))); 7001 printf("NODE_CVAR %s\n", mrb_sym_name(mrb, sym(tree))); 7002 break; 7003 7004 case NODE_NVAR: 7005 printf("NODE_NVAR %d\n", intn(tree)); 6336 7006 break; 6337 7007 6338 7008 case NODE_CONST: 6339 printf("NODE_CONST %s\n", mrb_sym 2name(mrb, sym(tree)));7009 printf("NODE_CONST %s\n", mrb_sym_name(mrb, sym(tree))); 6340 7010 break; 6341 7011 … … 6351 7021 6352 7022 case NODE_BACK_REF: 6353 printf("NODE_BACK_REF: $%c\n", (int)(intptr_t)tree);7023 printf("NODE_BACK_REF: $%c\n", intn(tree)); 6354 7024 break; 6355 7025 6356 7026 case NODE_NTH_REF: 6357 printf("NODE_NTH_REF: $% " MRB_PRId "\n", (mrb_int)(intptr_t)tree);7027 printf("NODE_NTH_REF: $%d\n", intn(tree)); 6358 7028 break; 6359 7029 6360 7030 case NODE_ARG: 6361 printf("NODE_ARG %s\n", mrb_sym 2name(mrb, sym(tree)));7031 printf("NODE_ARG %s\n", mrb_sym_name(mrb, sym(tree))); 6362 7032 break; 6363 7033 … … 6368 7038 6369 7039 case NODE_INT: 6370 printf("NODE_INT %s base %d\n", (char*)tree->car, (int)(intptr_t)tree->cdr->car);7040 printf("NODE_INT %s base %d\n", (char*)tree->car, intn(tree->cdr->car)); 6371 7041 break; 6372 7042 … … 6376 7046 6377 7047 case NODE_NEGATE: 6378 printf("NODE_NEGATE \n");7048 printf("NODE_NEGATE:\n"); 6379 7049 mrb_parser_dump(mrb, tree, offset+1); 6380 7050 break; 6381 7051 6382 7052 case NODE_STR: 6383 printf("NODE_STR \"%s\" len %d\n", (char*)tree->car, (int)(intptr_t)tree->cdr);7053 printf("NODE_STR %s len %d\n", str_dump(mrb, (char*)tree->car, intn(tree->cdr)), intn(tree->cdr)); 6384 7054 break; 6385 7055 6386 7056 case NODE_DSTR: 6387 printf("NODE_DSTR \n");7057 printf("NODE_DSTR:\n"); 6388 7058 dump_recur(mrb, tree, offset+1); 6389 7059 break; 6390 7060 6391 7061 case NODE_XSTR: 6392 printf("NODE_XSTR \"%s\" len %d\n", (char*)tree->car, (int)(intptr_t)tree->cdr);7062 printf("NODE_XSTR %s len %d\n", str_dump(mrb, (char*)tree->car, intn(tree->cdr)), intn(tree->cdr)); 6393 7063 break; 6394 7064 6395 7065 case NODE_DXSTR: 6396 printf("NODE_DXSTR \n");7066 printf("NODE_DXSTR:\n"); 6397 7067 dump_recur(mrb, tree, offset+1); 6398 7068 break; … … 6403 7073 6404 7074 case NODE_DREGX: 6405 printf("NODE_DREGX \n");7075 printf("NODE_DREGX:\n"); 6406 7076 dump_recur(mrb, tree->car, offset+1); 6407 7077 dump_prefix(tree, offset); … … 6418 7088 6419 7089 case NODE_SYM: 6420 printf("NODE_SYM :%s (%d)\n", mrb_sym2name(mrb, sym(tree)), 6421 (int)(intptr_t)tree); 7090 printf("NODE_SYM :%s (%d)\n", mrb_sym_dump(mrb, sym(tree)), 7091 intn(tree)); 7092 break; 7093 7094 case NODE_DSYM: 7095 printf("NODE_DSYM:\n"); 7096 mrb_parser_dump(mrb, tree, offset+1); 7097 break; 7098 7099 case NODE_WORDS: 7100 printf("NODE_WORDS:\n"); 7101 dump_recur(mrb, tree, offset+1); 7102 break; 7103 7104 case NODE_SYMBOLS: 7105 printf("NODE_SYMBOLS:\n"); 7106 dump_recur(mrb, tree, offset+1); 7107 break; 7108 7109 case NODE_LITERAL_DELIM: 7110 printf("NODE_LITERAL_DELIM\n"); 6422 7111 break; 6423 7112 … … 6440 7129 case NODE_ALIAS: 6441 7130 printf("NODE_ALIAS %s %s:\n", 6442 mrb_sym 2name(mrb, sym(tree->car)),6443 mrb_sym 2name(mrb, sym(tree->cdr)));7131 mrb_sym_dump(mrb, sym(tree->car)), 7132 mrb_sym_dump(mrb, sym(tree->cdr))); 6444 7133 break; 6445 7134 … … 6449 7138 node *t = tree; 6450 7139 while (t) { 6451 printf(" %s", mrb_sym 2name(mrb, sym(t->car)));7140 printf(" %s", mrb_sym_dump(mrb, sym(t->car))); 6452 7141 t = t->cdr; 6453 7142 } … … 6460 7149 if (tree->car->car == (node*)0) { 6461 7150 dump_prefix(tree, offset+1); 6462 printf(":%s\n", mrb_sym 2name(mrb, sym(tree->car->cdr)));7151 printf(":%s\n", mrb_sym_name(mrb, sym(tree->car->cdr))); 6463 7152 } 6464 7153 else if (tree->car->car == (node*)1) { 6465 7154 dump_prefix(tree, offset+1); 6466 printf("::%s\n", mrb_sym 2name(mrb, sym(tree->car->cdr)));7155 printf("::%s\n", mrb_sym_name(mrb, sym(tree->car->cdr))); 6467 7156 } 6468 7157 else { 6469 7158 mrb_parser_dump(mrb, tree->car->car, offset+1); 6470 7159 dump_prefix(tree, offset+1); 6471 printf("::%s\n", mrb_sym 2name(mrb, sym(tree->car->cdr)));7160 printf("::%s\n", mrb_sym_name(mrb, sym(tree->car->cdr))); 6472 7161 } 6473 7162 if (tree->cdr->car) { … … 6485 7174 if (tree->car->car == (node*)0) { 6486 7175 dump_prefix(tree, offset+1); 6487 printf(":%s\n", mrb_sym 2name(mrb, sym(tree->car->cdr)));7176 printf(":%s\n", mrb_sym_name(mrb, sym(tree->car->cdr))); 6488 7177 } 6489 7178 else if (tree->car->car == (node*)1) { 6490 7179 dump_prefix(tree, offset+1); 6491 printf("::%s\n", mrb_sym 2name(mrb, sym(tree->car->cdr)));7180 printf("::%s\n", mrb_sym_name(mrb, sym(tree->car->cdr))); 6492 7181 } 6493 7182 else { 6494 7183 mrb_parser_dump(mrb, tree->car->car, offset+1); 6495 7184 dump_prefix(tree, offset+1); 6496 printf("::%s\n", mrb_sym 2name(mrb, sym(tree->car->cdr)));7185 printf("::%s\n", mrb_sym_name(mrb, sym(tree->car->cdr))); 6497 7186 } 6498 7187 dump_prefix(tree, offset+1); … … 6512 7201 printf("NODE_DEF:\n"); 6513 7202 dump_prefix(tree, offset+1); 6514 printf("%s\n", mrb_sym 2name(mrb, sym(tree->car)));7203 printf("%s\n", mrb_sym_dump(mrb, sym(tree->car))); 6515 7204 tree = tree->cdr; 6516 7205 { … … 6525 7214 if (n2->car) { 6526 7215 if (!first_lval) printf(", "); 6527 printf("%s", mrb_sym 2name(mrb, sym(n2->car)));7216 printf("%s", mrb_sym_name(mrb, sym(n2->car))); 6528 7217 first_lval = FALSE; 6529 7218 } … … 6535 7224 tree = tree->cdr; 6536 7225 if (tree->car) { 6537 node *n = tree->car; 6538 6539 if (n->car) { 6540 dump_prefix(n, offset+1); 6541 printf("mandatory args:\n"); 6542 dump_recur(mrb, n->car, offset+2); 6543 } 6544 n = n->cdr; 6545 if (n->car) { 6546 dump_prefix(n, offset+1); 6547 printf("optional args:\n"); 6548 { 6549 node *n2 = n->car; 6550 6551 while (n2) { 6552 dump_prefix(n2, offset+2); 6553 printf("%s=", mrb_sym2name(mrb, sym(n2->car->car))); 6554 mrb_parser_dump(mrb, n2->car->cdr, 0); 6555 n2 = n2->cdr; 6556 } 6557 } 6558 } 6559 n = n->cdr; 6560 if (n->car) { 6561 dump_prefix(n, offset+1); 6562 printf("rest=*%s\n", mrb_sym2name(mrb, sym(n->car))); 6563 } 6564 n = n->cdr; 6565 if (n->car) { 6566 dump_prefix(n, offset+1); 6567 printf("post mandatory args:\n"); 6568 dump_recur(mrb, n->car, offset+2); 6569 } 6570 if (n->cdr) { 6571 dump_prefix(n, offset+1); 6572 printf("blk=&%s\n", mrb_sym2name(mrb, sym(n->cdr))); 6573 } 7226 dump_args(mrb, tree->car, offset); 6574 7227 } 6575 7228 mrb_parser_dump(mrb, tree->cdr->car, offset+1); … … 6581 7234 tree = tree->cdr; 6582 7235 dump_prefix(tree, offset+1); 6583 printf(":%s\n", mrb_sym 2name(mrb, sym(tree->car)));7236 printf(":%s\n", mrb_sym_dump(mrb, sym(tree->car))); 6584 7237 tree = tree->cdr->cdr; 6585 7238 if (tree->car) { 6586 node *n = tree->car; 6587 6588 if (n->car) { 6589 dump_prefix(n, offset+1); 6590 printf("mandatory args:\n"); 6591 dump_recur(mrb, n->car, offset+2); 6592 } 6593 n = n->cdr; 6594 if (n->car) { 6595 dump_prefix(n, offset+1); 6596 printf("optional args:\n"); 6597 { 6598 node *n2 = n->car; 6599 6600 while (n2) { 6601 dump_prefix(n2, offset+2); 6602 printf("%s=", mrb_sym2name(mrb, sym(n2->car->car))); 6603 mrb_parser_dump(mrb, n2->car->cdr, 0); 6604 n2 = n2->cdr; 6605 } 6606 } 6607 } 6608 n = n->cdr; 6609 if (n->car) { 6610 dump_prefix(n, offset+1); 6611 printf("rest=*%s\n", mrb_sym2name(mrb, sym(n->car))); 6612 } 6613 n = n->cdr; 6614 if (n->car) { 6615 dump_prefix(n, offset+1); 6616 printf("post mandatory args:\n"); 6617 dump_recur(mrb, n->car, offset+2); 6618 } 6619 n = n->cdr; 6620 if (n) { 6621 dump_prefix(n, offset+1); 6622 printf("blk=&%s\n", mrb_sym2name(mrb, sym(n))); 6623 } 7239 dump_args(mrb, tree->car, offset+1); 6624 7240 } 6625 7241 tree = tree->cdr; … … 6637 7253 break; 6638 7254 7255 case NODE_ARGS_TAIL: 7256 printf("NODE_ARGS_TAIL:\n"); 7257 { 7258 node *kws = tree->car; 7259 7260 while (kws) { 7261 mrb_parser_dump(mrb, kws->car, offset+1); 7262 kws = kws->cdr; 7263 } 7264 } 7265 tree = tree->cdr; 7266 if (tree->car) { 7267 mrb_assert(intn(tree->car->car) == NODE_KW_REST_ARGS); 7268 mrb_parser_dump(mrb, tree->car, offset+1); 7269 } 7270 tree = tree->cdr; 7271 if (tree->car) { 7272 dump_prefix(tree, offset+1); 7273 printf("block='%s'\n", mrb_sym_name(mrb, sym(tree->car))); 7274 } 7275 break; 7276 7277 case NODE_KW_ARG: 7278 printf("NODE_KW_ARG %s:\n", mrb_sym_name(mrb, sym(tree->car))); 7279 mrb_parser_dump(mrb, tree->cdr->car, offset + 1); 7280 break; 7281 7282 case NODE_KW_REST_ARGS: 7283 printf("NODE_KW_REST_ARGS %s\n", mrb_sym_name(mrb, sym(tree))); 7284 break; 7285 6639 7286 default: 6640 7287 printf("node type: %d (0x%x)\n", nodetype, (unsigned)nodetype); -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-compiler/mrbgem.rake
r331 r439 24 24 end 25 25 end 26 file objfile("#{current_build_dir}/core/y.tab") => lex_def27 26 28 27 # Parser 29 file "#{current_build_dir}/core/y.tab.c" => ["#{current_dir}/core/parse.y"] do |t| 28 file "#{current_build_dir}/core/y.tab.c" => ["#{current_dir}/core/parse.y", lex_def] do |t| 29 mkdir_p File.dirname t.name 30 30 yacc.run t.name, t.prerequisites.first 31 31 end … … 36 36 end 37 37 38 file libfile("#{build.build_dir}/lib/libmruby_core")=> core_objs38 file build.libmruby_core_static => core_objs 39 39 build.libmruby << core_objs 40 40 end
Note:
See TracChangeset
for help on using the changeset viewer.