mirror of https://github.com/svaarala/duktape.git
Browse Source
Pre/post inc/dec are an important fast path for fastints. Reimplement these operations as atomic opcodes so that fast pathing is easier. Opcodes emitted for typical loop constructs are also reduced by the change. Several opcodes were moved to extraops to make space for the 12 pre/post inc/dec reg/var/prop variants, and the compiler was changed to support two-argument (dest, dest, src) extraop arithmetic for its ispec/ivalue. Example of bytecode change, in Duktape 1.1.0: function foo() { var x = 10; print(x++, x++, x--, x--, x--); print(x); } BC 0000: LDINT 0, 10, 256 ; 0x80028003 op=3 (LDINT) a=0 b=10 c=256 BC 0001: CSVAR 1, 256, 0 ; 0x00400052 op=18 (CSVAR) a=1 b=256 c=0 BC 0002: LDREG 3, 0, 0 ; 0x000000c0 op=0 (LDREG) a=3 b=0 c=0 BC 0003: TONUM 3, 3 ; 0x0180c2fe op=62 (EXTRA) a=11 b=3 c=3 BC 0004: INC 0, 3 ; 0x0180067e op=62 (EXTRA) a=25 b=0 c=3 BC 0005: LDREG 4, 0, 0 ; 0x00000100 op=0 (LDREG) a=4 b=0 c=0 BC 0006: TONUM 4, 4 ; 0x020102fe op=62 (EXTRA) a=11 b=4 c=4 BC 0007: INC 0, 4 ; 0x0200067e op=62 (EXTRA) a=25 b=0 c=4 BC 0008: LDREG 5, 0, 0 ; 0x00000140 op=0 (LDREG) a=5 b=0 c=0 BC 0009: TONUM 5, 5 ; 0x028142fe op=62 (EXTRA) a=11 b=5 c=5 BC 0010: DEC 0, 5 ; 0x028006be op=62 (EXTRA) a=26 b=0 c=5 BC 0011: LDREG 6, 0, 0 ; 0x00000180 op=0 (LDREG) a=6 b=0 c=0 BC 0012: TONUM 6, 6 ; 0x030182fe op=62 (EXTRA) a=11 b=6 c=6 BC 0013: DEC 0, 6 ; 0x030006be op=62 (EXTRA) a=26 b=0 c=6 BC 0014: LDREG 7, 0, 0 ; 0x000001c0 op=0 (LDREG) a=7 b=0 c=0 BC 0015: TONUM 7, 7 ; 0x0381c2fe op=62 (EXTRA) a=11 b=7 c=7 BC 0016: DEC 0, 7 ; 0x038006be op=62 (EXTRA) a=26 b=0 c=7 BC 0017: CALL 0, 1, 5 ; 0x02804034 op=52 (CALL) a=0 b=1 c=5 BC 0018: CSVAR 1, 256, 0 ; 0x00400052 op=18 (CSVAR) a=1 b=256 c=0 BC 0019: LDREG 3, 0, 0 ; 0x000000c0 op=0 (LDREG) a=3 b=0 c=0 BC 0020: CALL 0, 1, 1 ; 0x00804034 op=52 (CALL) a=0 b=1 c=1 BC 0021: RETURN 1, 0, 0 ; 0x00000073 op=51 (RETURN) a=1 b=0 c=0 After this commit: function foo() { var x = 10; print(x++, x++, x--, x--, x--); print(x); } BC 0000: LDINT 0, 10, 256 ; 0x80028003 op=3 (LDINT) a=0 b=10 c=256 BC 0001: CSVAR 1, 256, 0 ; 0x00400052 op=18 (CSVAR) a=1 b=256 c=0 BC 0002: POSTINC 3, 0, 0 ; 0x000000f9 op=57 (POSTINC) a=3 b=0 c=0 BC 0003: POSTINC 4, 0, 0 ; 0x00000139 op=57 (POSTINC) a=4 b=0 c=0 BC 0004: POSTDEC 5, 0, 0 ; 0x0000017c op=60 (POSTDEC) a=5 b=0 c=0 BC 0005: POSTDEC 6, 0, 0 ; 0x000001bc op=60 (POSTDEC) a=6 b=0 c=0 BC 0006: POSTDEC 7, 0, 0 ; 0x000001fc op=60 (POSTDEC) a=7 b=0 c=0 BC 0007: CALL 0, 1, 5 ; 0x02804030 op=48 (CALL) a=0 b=1 c=5 BC 0008: CSVAR 1, 256, 0 ; 0x00400052 op=18 (CSVAR) a=1 b=256 c=0 BC 0009: LDREG 3, 0, 0 ; 0x000000c0 op=0 (LDREG) a=3 b=0 c=0 BC 0010: CALL 0, 1, 1 ; 0x00804030 op=48 (CALL) a=0 b=1 c=1 BC 0011: RETURN 1, 0, 0 ; 0x0000006f op=47 (RETURN) a=1 b=0 c=0 For an empty for-loop, in Duktape 1.1.0: function foo() { for (var i = 0; i < 1000; i++) {} } BC 0000: LABEL 0, 0, 0 ; 0x00000036 op=54 (LABEL) a=0 b=0 c=0 BC 0001: JUMP 11 (to pc+12) ; 0x800002f2 op=50 (JUMP) a=11 b=0 c=256 BC 0002: JUMP 5 (to pc+6) ; 0x80000172 op=50 (JUMP) a=5 b=0 c=256 BC 0003: LDINT 0, 0, 256 ; 0x80000003 op=3 (LDINT) a=0 b=0 c=256 BC 0004: LT 1, 0, 256 ; 0x8000006d op=45 (LT) a=1 b=0 c=256 BC 0005: IF 0, 1, 0 ; 0x0000402f op=47 (IF) a=0 b=1 c=0 BC 0006: JUMP 1 (to pc+2) ; 0x80000072 op=50 (JUMP) a=1 b=0 c=256 BC 0007: JUMP 5 (to pc+6) ; 0x80000172 op=50 (JUMP) a=5 b=0 c=256 BC 0008: LDREG 1, 0, 0 ; 0x00000040 op=0 (LDREG) a=1 b=0 c=0 BC 0009: TONUM 1, 1 ; 0x008042fe op=62 (EXTRA) a=11 b=1 c=1 BC 0010: INC 0, 1 ; 0x0080067e op=62 (EXTRA) a=25 b=0 c=1 BC 0011: JUMP -8 (to pc-7) ; 0x7ffffe32 op=50 (JUMP) a=248 b=511 c=255 BC 0012: JUMP -5 (to pc-4) ; 0x7ffffef2 op=50 (JUMP) a=251 b=511 c=255 BC 0013: ENDLABEL 0, 0, 0 ; 0x00000037 op=55 (ENDLABEL) a=0 b=0 c=0 BC 0014: RETURN 1, 0, 0 ; 0x00000073 op=51 (RETURN) a=1 b=0 c=0 After this commit: function foo() { for (var i = 0; i < 1000; i++) {} } BC 0000: LABEL 0, 0 ; 0x0000083f op=63 (EXTRA) a=32 b=0 c=0 BC 0001: JUMP 9 (to pc+10) ; 0x8000026e op=46 (JUMP) a=9 b=0 c=256 BC 0002: JUMP 5 (to pc+6) ; 0x8000016e op=46 (JUMP) a=5 b=0 c=256 BC 0003: LDINT 0, 0, 256 ; 0x80000003 op=3 (LDINT) a=0 b=0 c=256 BC 0004: LT 1, 0, 256 ; 0x8000006b op=43 (LT) a=1 b=0 c=256 BC 0005: IF 0, 1, 0 ; 0x0000402d op=45 (IF) a=0 b=1 c=0 BC 0006: JUMP 1 (to pc+2) ; 0x8000006e op=46 (JUMP) a=1 b=0 c=256 BC 0007: JUMP 3 (to pc+4) ; 0x800000ee op=46 (JUMP) a=3 b=0 c=256 BC 0008: POSTINC 1, 0, 0 ; 0x00000079 op=57 (POSTINC) a=1 b=0 c=0 BC 0009: JUMP -6 (to pc-5) ; 0x7ffffeae op=46 (JUMP) a=250 b=511 c=255 BC 0010: JUMP -3 (to pc-2) ; 0x7fffff6e op=46 (JUMP) a=253 b=511 c=255 BC 0011: ENDLABEL 0, 0 ; 0x0000087f op=63 (EXTRA) a=33 b=0 c=0 BC 0012: RETURN 1, 0, 0 ; 0x0000006f op=47 (RETURN) a=1 b=0 c=0pull/156/head
Sami Vaarala
10 years ago
6 changed files with 467 additions and 359 deletions
Loading…
Reference in new issue