Skip to content

Instantly share code, notes, and snippets.

@mariano
Last active October 15, 2019 21:26
Show Gist options
  • Save mariano/7c6b1c20d2f03b85699ba161d8192ac2 to your computer and use it in GitHub Desktop.
Save mariano/7c6b1c20d2f03b85699ba161d8192ac2 to your computer and use it in GitHub Desktop.
Seppo's bytecodes
function work_map(arr) {
return arr.map((item, index, array) => [item, array[index + 1]]);
}
function work_nat_mem(arr) {
var res = new Array(arr.length - 1);
for (i=0, j=0; i < arr.length; i++) {
res[i] = [arr[i], arr[i+1]];
}
return res;
}
function work_nat_for(arr) {
for (i=0, li=arr.length; i < li; i++) {
arr[i] = [arr[i], arr[i+1]];
}
return arr;
}
function work_nat_while(arr) {
var i=0;
while (i < arr.length) {
arr[i] = [arr[i], arr[++i]];
}
return arr;
}
const arr = [1, 1, 2, 3, 5, 8, 13, 21];
result = work_map(arr.slice());
console.log(result);
result = work_nat_mem(arr.slice());
console.log(result);
result = work_nat_for(arr.slice());
console.log(result);
result = work_nat_while(arr.slice());
console.log(result);
/**
BYTECODE work_nat_while
454 E> 0x1ff98553170a @ 0 : 91 StackCheck
469 S> 0x1ff98553170b @ 1 : 02 LdaZero
0x1ff98553170c @ 2 : 1e fb Star r0
488 S> 0x1ff98553170e @ 4 : 20 02 00 00 LdaNamedProperty a0, [0], [0]
482 E> 0x1ff985531712 @ 8 : 5b fb 02 TestLessThan r0, [2]
0x1ff985531715 @ 11 : 86 32 JumpIfFalse [50] (0x1ff985531747 @ 61)
473 E> 0x1ff985531717 @ 13 : 91 StackCheck
504 S> 0x1ff985531718 @ 14 : 6a 01 08 25 CreateArrayLiteral [1], [8], #37
0x1ff98553171c @ 18 : 1e f7 Star r4
0x1ff98553171e @ 20 : 02 LdaZero
0x1ff98553171f @ 21 : 1e f8 Star r3
0x1ff985531721 @ 23 : 1d fb Ldar r0
513 E> 0x1ff985531723 @ 25 : 21 02 03 LdaKeyedProperty a0, [3]
0x1ff985531726 @ 28 : 27 f7 f8 09 StaKeyedPropertySloppy r4, r3, [9]
0x1ff98553172a @ 32 : 03 01 LdaSmi [1]
0x1ff98553172c @ 34 : 1e f8 Star r3
0x1ff98553172e @ 36 : 1d fb Ldar r0
518 E> 0x1ff985531730 @ 38 : 41 05 Inc [5]
0x1ff985531732 @ 40 : 1f fb f9 Mov r0, r2
0x1ff985531735 @ 43 : 1e fb Star r0
521 E> 0x1ff985531737 @ 45 : 21 02 06 LdaKeyedProperty a0, [6]
0x1ff98553173a @ 48 : 27 f7 f8 09 StaKeyedPropertySloppy r4, r3, [9]
0x1ff98553173e @ 52 : 1d f7 Ldar r4
507 E> 0x1ff985531740 @ 54 : 27 02 f9 0b StaKeyedPropertySloppy a0, r2, [11]
0x1ff985531744 @ 58 : 77 36 00 JumpLoop [54], [0] (0x1ff98553170e @ 4)
533 S> 0x1ff985531747 @ 61 : 1d 02 Ldar a0
544 S> 0x1ff985531749 @ 63 : 95 Return
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment