scmno****@osdn*****
scmno****@osdn*****
Wed Jun 20 03:24:44 JST 2018
changeset dd746fac3d09 in quipu/quipu details: http://hg.osdn.jp/view/quipu/quipu?cmd=changeset;node=dd746fac3d09 user: Agustina Arzille <avarz****@riseu*****> date: Tue Jun 19 15:24:34 2018 -0300 description: Implement throwing exceptions in bytecode diffstat: bytecode.cpp | 134 +++++++++++++++++++++++++++++---------------------------- bytecode.h | 2 + compiler.cpp | 53 +++++++++++++++++----- utils/opnames | 2 + 4 files changed, 112 insertions(+), 79 deletions(-) diffs (266 lines): diff -r 7ded2ec411d9 -r dd746fac3d09 bytecode.cpp --- a/bytecode.cpp Tue Jun 19 13:39:55 2018 -0300 +++ b/bytecode.cpp Tue Jun 19 15:24:34 2018 -0300 @@ -18,14 +18,14 @@ "nop\0dup\0pop\0ret\0is\0not\0cons\0list\0car\0cdr\0cadr\0nputcar\0" "nputcdr\0apply\0tapply\0loadt\0loadnil\0load0\0load1\0loadi8\0loada0\0" "loada1\0loadc00\0loadc01\0loadap0\0loadap1\0mkcont\0captenv\0closure\0" - "tryend\0argc\0argc.l\0vargc\0vargc.l\0jmp\0jmp.l\0brt\0brt.l\0brn\0" - "brn.l\0brneq\0brneq.l\0tcall\0tcall.l\0call\0call.l\0setc\0setc.l\0" - "seta\0seta.l\0setap\0setap.l\0setg\0setg.l\0loadc\0loadc.l\0loada\0" - "loada.l\0loadap\0loadap.l\0loadg\0loadg.l\0loadv\0loadv.l\0loadx\0" - "loadx.l\0bind\0bind.l\0recur\0recur.l\0trecur\0trecur.l\0mkframe\0" - "mkframe.l\0unwind\0unwind.l\0trybegin\0trybegin.l\0setapop\0setapop.l\0" - "irtjmp\0irtjmp.l\0optargs\0optargs.l\0brbound\0brbound.l\0kwargs\0" - "kwargs.l"; + "tryend\0raise\0raise2\0argc\0argc.l\0vargc\0vargc.l\0jmp\0jmp.l\0" + "brt\0brt.l\0brn\0brn.l\0brneq\0brneq.l\0tcall\0tcall.l\0call\0call.l\0" + "setc\0setc.l\0seta\0seta.l\0setap\0setap.l\0setg\0setg.l\0loadc\0" + "loadc.l\0loada\0loada.l\0loadap\0loadap.l\0loadg\0loadg.l\0loadv\0" + "loadv.l\0loadx\0loadx.l\0bind\0bind.l\0recur\0recur.l\0trecur\0trecur.l\0" + "mkframe\0mkframe.l\0unwind\0unwind.l\0trybegin\0trybegin.l\0setapop\0" + "setapop.l\0irtjmp\0irtjmp.l\0optargs\0optargs.l\0brbound\0brbound.l\0" + "kwargs\0kwargs.l"; #define F_(n) 1 << (BC_FLG_FBIT + n) @@ -69,64 +69,66 @@ { 161, 0 }, // captenv { 169, 0 }, // closure { 177, 0 }, // tryend - { 184, 1 }, // argc - { 189, 1 | BC_LONG_FORM }, // argc.l - { 196, 1 }, // vargc - { 202, 1 | BC_LONG_FORM }, // vargc.l - { 210, 1 | BC_BRANCH_FORM }, // jmp - { 214, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // jmp.l - { 220, 1 | BC_BRANCH_FORM }, // brt - { 224, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // brt.l - { 230, 1 | BC_BRANCH_FORM }, // brn - { 234, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // brn.l - { 240, 1 | BC_BRANCH_FORM }, // brneq - { 246, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // brneq.l - { 254, 1 | BC_CALL_FORM }, // tcall - { 260, 1 | BC_CALL_FORM | BC_LONG_FORM }, // tcall.l - { 268, 1 | BC_CALL_FORM }, // call - { 273, 1 | BC_CALL_FORM | BC_LONG_FORM }, // call.l - { 280, 2 }, // setc - { 285, 2 | BC_LONG_FORM }, // setc.l - { 292, 1 }, // seta - { 297, 1 | BC_LONG_FORM }, // seta.l - { 304, 2 }, // setap - { 310, 2 | BC_LONG_FORM }, // setap.l - { 318, 1 }, // setg - { 323, 1 | BC_LONG_FORM }, // setg.l - { 330, 2 | BC_LOAD_FORM | BC_PURE_FORM }, // loadc - { 336, 2 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loadc.l - { 344, 1 | BC_LOAD_FORM | BC_PURE_FORM }, // loada - { 350, 1 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loada.l - { 358, 2 | BC_LOAD_FORM | BC_PURE_FORM }, // loadap - { 365, 2 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loadap.l - { 374, 1 | BC_LOAD_FORM }, // loadg - { 380, 1 | BC_LOAD_FORM | BC_LONG_FORM }, // loadg.l - { 388, 1 | BC_LOAD_FORM | BC_PURE_FORM }, // loadv - { 394, 1 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loadv.l - { 402, 1 | BC_LOAD_FORM }, // loadx - { 408, 1 | BC_LOAD_FORM | BC_LONG_FORM }, // loadx.l - { 416, 1 }, // bind - { 421, 1 | BC_LONG_FORM }, // bind.l - { 428, 1 | BC_CALL_FORM }, // recur - { 434, 1 | BC_CALL_FORM | BC_LONG_FORM }, // recur.l - { 442, 1 | BC_CALL_FORM }, // trecur - { 449, 1 | BC_CALL_FORM | BC_LONG_FORM }, // trecur.l - { 458, 1 }, // mkframe - { 466, 1 | BC_LONG_FORM }, // mkframe.l - { 476, 1 }, // unwind - { 483, 1 | BC_LONG_FORM }, // unwind.l - { 492, 1 | BC_BRANCH_FORM }, // trybegin - { 501, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // trybegin.l - { 512, 1 }, // setapop - { 520, 1 | BC_LONG_FORM }, // setapop.l - { 530, 1 | BC_BRANCH_FORM }, // irtjmp - { 537, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // irtjmp.l - { 546, 2 }, // optargs - { 554, 2 | BC_LONG_FORM }, // optargs.l - { 564, 1 }, // brbound - { 572, 1 | BC_LONG_FORM }, // brbound.l - { 582, 3 }, // kwargs - { 589, 3 | BC_LONG_FORM }, // kwargs.l + { 184, 0 }, // raise + { 190, 0 }, // raise2 + { 197, 1 }, // argc + { 202, 1 | BC_LONG_FORM }, // argc.l + { 209, 1 }, // vargc + { 215, 1 | BC_LONG_FORM }, // vargc.l + { 223, 1 | BC_BRANCH_FORM }, // jmp + { 227, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // jmp.l + { 233, 1 | BC_BRANCH_FORM }, // brt + { 237, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // brt.l + { 243, 1 | BC_BRANCH_FORM }, // brn + { 247, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // brn.l + { 253, 1 | BC_BRANCH_FORM }, // brneq + { 259, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // brneq.l + { 267, 1 | BC_CALL_FORM }, // tcall + { 273, 1 | BC_CALL_FORM | BC_LONG_FORM }, // tcall.l + { 281, 1 | BC_CALL_FORM }, // call + { 286, 1 | BC_CALL_FORM | BC_LONG_FORM }, // call.l + { 293, 2 }, // setc + { 298, 2 | BC_LONG_FORM }, // setc.l + { 305, 1 }, // seta + { 310, 1 | BC_LONG_FORM }, // seta.l + { 317, 2 }, // setap + { 323, 2 | BC_LONG_FORM }, // setap.l + { 331, 1 }, // setg + { 336, 1 | BC_LONG_FORM }, // setg.l + { 343, 2 | BC_LOAD_FORM | BC_PURE_FORM }, // loadc + { 349, 2 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loadc.l + { 357, 1 | BC_LOAD_FORM | BC_PURE_FORM }, // loada + { 363, 1 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loada.l + { 371, 2 | BC_LOAD_FORM | BC_PURE_FORM }, // loadap + { 378, 2 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loadap.l + { 387, 1 | BC_LOAD_FORM }, // loadg + { 393, 1 | BC_LOAD_FORM | BC_LONG_FORM }, // loadg.l + { 401, 1 | BC_LOAD_FORM | BC_PURE_FORM }, // loadv + { 407, 1 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loadv.l + { 415, 1 | BC_LOAD_FORM }, // loadx + { 421, 1 | BC_LOAD_FORM | BC_LONG_FORM }, // loadx.l + { 429, 1 }, // bind + { 434, 1 | BC_LONG_FORM }, // bind.l + { 441, 1 | BC_CALL_FORM }, // recur + { 447, 1 | BC_CALL_FORM | BC_LONG_FORM }, // recur.l + { 455, 1 | BC_CALL_FORM }, // trecur + { 462, 1 | BC_CALL_FORM | BC_LONG_FORM }, // trecur.l + { 471, 1 }, // mkframe + { 479, 1 | BC_LONG_FORM }, // mkframe.l + { 489, 1 }, // unwind + { 496, 1 | BC_LONG_FORM }, // unwind.l + { 505, 1 | BC_BRANCH_FORM }, // trybegin + { 514, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // trybegin.l + { 525, 1 }, // setapop + { 533, 1 | BC_LONG_FORM }, // setapop.l + { 543, 1 | BC_BRANCH_FORM }, // irtjmp + { 550, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // irtjmp.l + { 559, 2 }, // optargs + { 567, 2 | BC_LONG_FORM }, // optargs.l + { 577, 1 }, // brbound + { 585, 1 | BC_LONG_FORM }, // brbound.l + { 595, 3 }, // kwargs + { 602, 3 | BC_LONG_FORM }, // kwargs.l { 0, 0 } }; diff -r 7ded2ec411d9 -r dd746fac3d09 bytecode.h --- a/bytecode.h Tue Jun 19 13:39:55 2018 -0300 +++ b/bytecode.h Tue Jun 19 15:24:34 2018 -0300 @@ -37,6 +37,8 @@ OP_CAPTENV, OP_CLOSURE, OP_TRYEND, + OP_RAISE, + OP_RAISE2, // Opcodes with long forms. OP_ARGC, OP_ARGCL, diff -r 7ded2ec411d9 -r dd746fac3d09 compiler.cpp --- a/compiler.cpp Tue Jun 19 13:39:55 2018 -0300 +++ b/compiler.cpp Tue Jun 19 15:24:34 2018 -0300 @@ -1104,7 +1104,8 @@ SF_LET, SF_RECUR, SF_YIELD, - SF_CALLCC + SF_CALLCC, + SF_RAISE }; static const struct @@ -1129,7 +1130,8 @@ { "let", 3, SF_LET }, { "recur", 5, SF_RECUR }, { "yield", 5, SF_YIELD }, - { "call/cc", 7, SF_CALLCC } + { "call/cc", 7, SF_CALLCC }, + { "raise", 5, SF_RAISE } }; static inline int @@ -1421,6 +1423,22 @@ this->compile_try (env, tail, expr); break; + case SF_RAISE: + if (xcdr (expr) == NIL) + this->code.push_back (OPX_(RAISE)); + else + { + if (!xcons_p (xcdr (expr))) + specform_error (this->interp, "raise", SPECFORM_DOTTED); + else if (xcddr (expr) != NIL) + specform_error (this->interp, "raise", SPECFORM_TOOMANY); + + this->compile_in (env, false, xcadr (expr)); + this->code.push_back (OPX_(RAISE2)); + } + + break; + default: this->compile_app (env, tail, expr); } @@ -2173,17 +2191,18 @@ P_(CAR), P_(CDR), P_(CADR), P_(NPUTCAR), P_(NPUTCDR), P_(APPLY), P_(TAPPLY), P_(LOADT), P_(LOADNIL), P_(LOAD0), P_(LOAD1), P_(LOADI8), P_(LOADA0), P_(LOADA1), P_(LOADC00), P_(LOADC01), P_(LOADAP0), - P_(LOADAP1), P_(MKCONT), P_(CAPTENV), P_(CLOSURE), P_(TRYEND), P_(ARGC), - P_(ARGCL), P_(VARGC), P_(VARGCL), P_(JMP), P_(JMPL), P_(BRT), P_(BRTL), - P_(BRN), P_(BRNL), P_(BRNEQ), P_(BRNEQL), P_(TCALL), P_(TCALLL), P_(CALL), - P_(CALLL), P_(SETC), P_(SETCL), P_(SETA), P_(SETAL), P_(SETAP), - P_(SETAPL), P_(SETG), P_(SETGL), P_(LOADC), P_(LOADCL), P_(LOADA), - P_(LOADAL), P_(LOADAP), P_(LOADAPL), P_(LOADG), P_(LOADGL), P_(LOADV), - P_(LOADVL), P_(LOADX), P_(LOADXL), P_(BIND), P_(BINDL), P_(RECUR), - P_(RECURL), P_(TRECUR), P_(TRECURL), P_(MKFRAME), P_(MKFRAMEL), - P_(UNWIND), P_(UNWINDL), P_(TRYBEGIN), P_(TRYBEGINL), P_(SETAPOP), - P_(SETAPOPL), P_(IRTJMP), P_(IRTJMPL), P_(OPTARGS), P_(OPTARGSL), - P_(BRBOUND), P_(BRBOUNDL), P_(KWARGS), P_(KWARGSL) + P_(LOADAP1), P_(MKCONT), P_(CAPTENV), P_(CLOSURE), P_(TRYEND), P_(RAISE), + P_(RAISE2), P_(ARGC), P_(ARGCL), P_(VARGC), P_(VARGCL), P_(JMP), + P_(JMPL), P_(BRT), P_(BRTL), P_(BRN), P_(BRNL), P_(BRNEQ), P_(BRNEQL), + P_(TCALL), P_(TCALLL), P_(CALL), P_(CALLL), P_(SETC), P_(SETCL), + P_(SETA), P_(SETAL), P_(SETAP), P_(SETAPL), P_(SETG), P_(SETGL), + P_(LOADC), P_(LOADCL), P_(LOADA), P_(LOADAL), P_(LOADAP), P_(LOADAPL), + P_(LOADG), P_(LOADGL), P_(LOADV), P_(LOADVL), P_(LOADX), P_(LOADXL), + P_(BIND), P_(BINDL), P_(RECUR), P_(RECURL), P_(TRECUR), P_(TRECURL), + P_(MKFRAME), P_(MKFRAMEL), P_(UNWIND), P_(UNWINDL), P_(TRYBEGIN), + P_(TRYBEGINL), P_(SETAPOP), P_(SETAPOPL), P_(IRTJMP), P_(IRTJMPL), + P_(OPTARGS), P_(OPTARGSL), P_(BRBOUND), P_(BRBOUNDL), P_(KWARGS), + P_(KWARGSL) }; # undef P_ @@ -2788,6 +2807,14 @@ NEXT_OP; + OP_(RAISE): + if (interp->lasterr == UNBOUND) + interp->raise2 ("runtime-error", "no exception to reraise"); + interp->raise (interp->lasterr); + + OP_(RAISE2): + interp->raise (interp->pop ()); + #ifndef GOTO_LABELS default: goto dispatch; diff -r 7ded2ec411d9 -r dd746fac3d09 utils/opnames --- a/utils/opnames Tue Jun 19 13:39:55 2018 -0300 +++ b/utils/opnames Tue Jun 19 15:24:34 2018 -0300 @@ -28,6 +28,8 @@ captenv 0 0 closure 0 0 tryend 0 0 +raise 0 0 +raise2 0 0 argc 1 0 argc.l 1 long vargc 1 0