scmno****@osdn*****
scmno****@osdn*****
Sun Jun 10 12:59:44 JST 2018
changeset ae8d402ece24 in quipu/quipu details: http://hg.osdn.jp/view/quipu/quipu?cmd=changeset;node=ae8d402ece24 user: Agustina Arzille <avarz****@riseu*****> date: Sun Jun 10 03:59:07 2018 +0000 description: Use BP-relative access, discard locals cache diffstat: bytecode.cpp | 149 +++++++++++----------- bytecode.h | 5 +- compiler.cpp | 379 +++++++++++++++++++++++++++------------------------------ defs.h | 20 +- misc.cpp | 22 --- utils/opnames | 5 +- 6 files changed, 267 insertions(+), 313 deletions(-) diffs (truncated from 1125 to 300 lines): diff -r 9d4c985e1b60 -r ae8d402ece24 bytecode.cpp --- a/bytecode.cpp Sat Jun 09 05:36:20 2018 +0000 +++ b/bytecode.cpp Sun Jun 10 03:59:07 2018 +0000 @@ -17,15 +17,15 @@ static const char OPC_NAMES[] = "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\0loadap1\0mkcont\0captenv\0closure\0tryend\0" - "argc\0argc.l\0vargc\0vargc.l\0jmp\0jmp.l\0brt\0brt.l\0brn\0brn.l\0" - "brneq\0brneq.l\0tcall\0tcall.l\0call\0call.l\0setc\0setc.l\0seta\0" - "seta.l\0setap\0setap.l\0setg\0setg.l\0setb\0setb.l\0loadc\0loadc.l\0" - "loada\0loada.l\0loadap\0loadap.l\0loadg\0loadg.l\0loadv\0loadv.l\0" - "loadx\0loadx.l\0loadb\0loadb.l\0bind\0bind.l\0recur\0recur.l\0trecur\0" - "trecur.l\0mkframe\0mkframe.l\0unwind\0unwind.l\0trybegin\0trybegin.l\0" - "setapop\0setapop.l\0irtjmp\0irtjmp.l\0optargs\0optargs.l\0brbound\0" - "brbound.l\0kwargs\0kwargs.l"; + "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"; #define F_(n) 1 << (BC_FLG_FBIT + n) @@ -63,73 +63,70 @@ { 115, 0 | BC_LOAD_FORM | BC_PURE_FORM }, // loada1 { 122, 0 | BC_LOAD_FORM | BC_PURE_FORM }, // loadc00 { 130, 0 | BC_LOAD_FORM | BC_PURE_FORM }, // loadc01 - { 138, 1 | BC_LOAD_FORM | BC_PURE_FORM }, // loadap1 - { 146, 1 | BC_LOAD_FORM }, // mkcont - { 153, 0 }, // captenv - { 161, 0 }, // closure - { 169, 0 }, // tryend - { 176, 1 }, // argc - { 181, 1 | BC_LONG_FORM }, // argc.l - { 188, 1 }, // vargc - { 194, 1 | BC_LONG_FORM }, // vargc.l - { 202, 1 | BC_BRANCH_FORM }, // jmp - { 206, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // jmp.l - { 212, 1 | BC_BRANCH_FORM }, // brt - { 216, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // brt.l - { 222, 1 | BC_BRANCH_FORM }, // brn - { 226, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // brn.l - { 232, 1 | BC_BRANCH_FORM }, // brneq - { 238, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // brneq.l - { 246, 1 | BC_CALL_FORM }, // tcall - { 252, 1 | BC_CALL_FORM | BC_LONG_FORM }, // tcall.l - { 260, 1 | BC_CALL_FORM }, // call - { 265, 1 | BC_CALL_FORM | BC_LONG_FORM }, // call.l - { 272, 2 }, // setc - { 277, 2 | BC_LONG_FORM }, // setc.l - { 284, 1 }, // seta - { 289, 1 | BC_LONG_FORM }, // seta.l - { 296, 2 }, // setap - { 302, 2 | BC_LONG_FORM }, // setap.l - { 310, 1 }, // setg - { 315, 1 | BC_LONG_FORM }, // setg.l - { 322, 1 }, // setb - { 327, 1 | BC_LONG_FORM }, // setb.l - { 334, 2 | BC_LOAD_FORM | BC_PURE_FORM }, // loadc - { 340, 2 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loadc.l - { 348, 1 | BC_LOAD_FORM | BC_PURE_FORM }, // loada - { 354, 1 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loada.l - { 362, 2 | BC_LOAD_FORM | BC_PURE_FORM }, // loadap - { 369, 2 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loadap.l - { 378, 1 | BC_LOAD_FORM }, // loadg - { 384, 1 | BC_LOAD_FORM | BC_LONG_FORM }, // loadg.l - { 392, 1 | BC_LOAD_FORM | BC_PURE_FORM }, // loadv - { 398, 1 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loadv.l - { 406, 1 | BC_LOAD_FORM }, // loadx - { 412, 1 | BC_LOAD_FORM | BC_LONG_FORM }, // loadx.l - { 420, 1 | BC_LOAD_FORM | BC_PURE_FORM }, // loadb - { 426, 1 | BC_LOAD_FORM | BC_PURE_FORM | BC_LONG_FORM }, // loadb.l - { 434, 1 }, // bind - { 439, 1 | BC_LONG_FORM }, // bind.l - { 446, 1 | BC_CALL_FORM }, // recur - { 452, 1 | BC_CALL_FORM | BC_LONG_FORM }, // recur.l - { 460, 1 | BC_CALL_FORM }, // trecur - { 467, 1 | BC_CALL_FORM | BC_LONG_FORM }, // trecur.l - { 476, 1 }, // mkframe - { 484, 1 | BC_LONG_FORM }, // mkframe.l - { 494, 1 }, // unwind - { 501, 1 | BC_LONG_FORM }, // unwind.l - { 510, 1 | BC_BRANCH_FORM }, // trybegin - { 519, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // trybegin.l - { 530, 1 }, // setapop - { 538, 1 | BC_LONG_FORM }, // setapop.l - { 548, 1 | BC_BRANCH_FORM }, // irtjmp - { 555, 1 | BC_BRANCH_FORM | BC_LONG_FORM }, // irtjmp.l - { 564, 2 }, // optargs - { 572, 2 | BC_LONG_FORM }, // optargs.l - { 582, 1 }, // brbound - { 590, 1 | BC_LONG_FORM }, // brbound.l - { 600, 3 }, // kwargs - { 607, 3 | BC_LONG_FORM }, // kwargs.l + { 138, 1 | BC_LOAD_FORM | BC_PURE_FORM }, // loadap0 + { 146, 1 | BC_LOAD_FORM | BC_PURE_FORM }, // loadap1 + { 154, 1 | BC_LOAD_FORM }, // mkcont + { 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 { 0, 0 } }; diff -r 9d4c985e1b60 -r ae8d402ece24 bytecode.h --- a/bytecode.h Sat Jun 09 05:36:20 2018 +0000 +++ b/bytecode.h Sun Jun 10 03:59:07 2018 +0000 @@ -31,6 +31,7 @@ OP_LOADA1, OP_LOADC00, OP_LOADC01, + OP_LOADAP0, OP_LOADAP1, OP_MKCONT, OP_CAPTENV, @@ -61,8 +62,6 @@ OP_SETAPL, OP_SETG, OP_SETGL, - OP_SETB, - OP_SETBL, OP_LOADC, OP_LOADCL, OP_LOADA, @@ -75,8 +74,6 @@ OP_LOADVL, OP_LOADX, OP_LOADXL, - OP_LOADB, - OP_LOADBL, OP_BIND, OP_BINDL, OP_RECUR, diff -r 9d4c985e1b60 -r ae8d402ece24 compiler.cpp --- a/compiler.cpp Sat Jun 09 05:36:20 2018 +0000 +++ b/compiler.cpp Sun Jun 10 03:59:07 2018 +0000 @@ -15,6 +15,9 @@ // Special instruction used to refer to labels in the code. static const int OP_LABEL = 0x100; +// Placeholder for variable captures. +static const int OP_FAKECAPT = 0x101; + #define OPX_(opc) (EXTRA_BIT | (intobj ((int)OP_##opc))) /* Opcodes that follow OP_ARGC are defined as pairs, the second one being @@ -35,7 +38,6 @@ } class whl_block; -class clref; class xcmp_call { @@ -48,6 +50,15 @@ } }; +class whl_block +{ +public: + int top_lbl; + int end_lbl; + int stkpos; + whl_block *next; +}; + /* Definition for the type responsible for emitting bytecode, * label book-keeeping and maintaining the symbol enviroment. * Compilation is divided into 3 well-defined steps: @@ -70,11 +81,22 @@ { public: typedef sorted_list<xcmp_call> ctable_t; - typedef std::vector<object> codevec_t; + + class frame_data + { + public: + int nargs; + int stkdisp; + int acc; + bool done; + + frame_data (bool d) : nargs (0), stkdisp (interpreter::frame_size), + acc (0), done (d) {} + }; interpreter *interp; ctable_t ctable; - codevec_t code; + std::vector<object> code; int nconst = 0; int cdepth = INT_MAX; int lbl_cnt = 0; @@ -91,16 +113,43 @@ unsigned char sn[2]; } xdo; - int let_cnt = 0; - int inlet = 0; - clref *clrefs = nullptr; whl_block *whl = nullptr; + std::vector<frame_data> frames; + bool outer_ref = false; + bool captured = false; + bool emitted_captenv = false; + + int cur_stkpos () const + { + const auto& f = this->frames.back (); + return (f.acc + f.nargs + f.stkdisp); + } + + void push_f (bool done = true) + { + frame_data fr (done); + if (!this->frames.empty ()) + fr.acc = this->cur_stkpos (); + + this->frames.push_back (fr); + } + + void pop_f () + { + this->frames.pop_back (); + } + + frame_data& cur_f () + { + return (this->frames.back ()); + } bc_emitter (interpreter *ip) : interp (ip), sp (ip->stkdiff ()) { this->xdo.expr.car = UNBOUND; this->ct_env = NIL; this->ctable.cmp.ip = ip;