how i tried to compile javascript
TRANSCRIPT
![Page 1: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/1.jpg)
How I spent my summer
tried to compile javascript
INGVAR STEPANYAN AKA @RREVERSER
![Page 2: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/2.jpg)
Compilers pipeline
![Page 3: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/3.jpg)
Compilers pipeline
![Page 4: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/4.jpg)
function factorial(n) { var result = 1; for (var i = 2; i <= n; i++) { result *= i; } return result;}
![Page 5: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/5.jpg)
‘var result = 1’
![Page 6: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/6.jpg)
[‘v’,‘a‘,‘r’,‘ ‘,‘r’,‘e’,‘s’,‘u’,‘l’,‘t’,‘ ‘,‘=‘,‘ ‘,‘1’]
Keyword Identifier Punctuator Number
![Page 7: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/7.jpg)
[Keyword(‘var’), Identifier(‘result’), Punctuator(‘=‘), Number(‘1’)]
VarDeclaration
VarDeclarator
Identifier Expression
![Page 8: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/8.jpg)
function factorial(n) { var result = 1; for (var i = 2; i <= n; i++) { result *= i; } return result;}
![Page 9: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/9.jpg)
function factorial(n) { var result = 1, i = 2; while (i <= n) { result *= i++; } return result;}
![Page 10: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/10.jpg)
function factorial(n) { var result = 1, i = 1; do { result *= i++; } while (i <= n); return result;}
![Page 11: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/11.jpg)
function factorial(n) { var result = 1; for (var i = 2; i <= n; i++) { result *= i; } return result;}
![Page 12: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/12.jpg)
function factorial(n) { var n, result, i; // all vars result = 1; for (i = 2; i <= n; i++) { result *= i; } return result;}
![Page 13: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/13.jpg)
function factorial(n) { var n, result, i; // all vars result = 1; for (i = 2; i <= n; i = i + 1) { result = result * i; } return result;}
![Page 14: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/14.jpg)
function factorial(n) { var n, result, i; // all vars result = 1; i = 2; while (i <= n) { result = result * i; i = i + 1; } return result;}
![Page 15: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/15.jpg)
function factorial(n) { result = undefined; i = undefined; result = 1; i = 2; while (i <= n) { result = result * i; i = i + 1; } return result;}
![Page 16: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/16.jpg)
// B1result = undefined;i = undefined;result = 1;i = 2;goto B2;
// B4return result;
// B3result = result * i;i = i + 1;goto B2;
// B2cond = i <= n;goto cond ? B3 : B4;
![Page 17: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/17.jpg)
// B1result = undefined;i = undefined;result = 1;i = 2;cond = i <= n;goto cond ? B2 : B3;
// B3return result;
// B2result = result * i;i = i + 1;cond = i <= n;goto cond ? B2 : B3;
![Page 18: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/18.jpg)
// B1r[1] = undefined;i[1] = undefined;r[2] = 1;i[2] = 2;cond[1] = i[2] <= n[1];goto cond ? B2 : B3;
// B3return r;
// B2r = r * i;i = i + 1;cond = i <= n;goto cond ? B2 : B3;
![Page 19: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/19.jpg)
// B1r[1] = undefined;i[1] = undefined;r[2] = 1;i[2] = 2;cond[1] = i[2] <= n[1];goto cond[1] ? B2 : B3;
// B3return r[4];
// B2i[3] = phi(B1:i[2], B2:i[4]);r[3] = phi(B1:r[2], B2:r[4]);r[4] = r[3] * i[3];i[4] = i[3] + 1;cond[2] = i[4] <= n[1];goto cond[2] ? B2 : B3;
![Page 20: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/20.jpg)
// B1r[2] = 1;i[2] = 2;cond[1] = i[2] <= n[1];goto cond[1] ? B2 : B3;
// B3return phi(B1:r[2], B2:r[4]);
// B2i[3] = phi(B1:i[2], B2:i[4]);r[3] = phi(B1:r[2], B2:r[4]);r[4] = r[3] * i[3];i[4] = i[3] + 1;cond[2] = i[4] <= n[1];goto cond[2] ? B2 : B3;
![Page 21: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/21.jpg)
// B1cond[1] = 2 <= n[1];goto cond[1] ? B2 : B3;
// B3return phi(B1:1, B2:r[4]);
// B2i[3] = phi(B1:2, B2:i[4]);r[3] = phi(B1:1, B2:r[4]);r[4] = r[3] * i[3];i[4] = i[3] + 1;cond[2] = i[4] <= n[1];goto cond[2] ? B2 : B3;
![Page 22: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/22.jpg)
// n[1]: 1// B1cond[1] = 2 <= n[1];goto cond[1] ? B2 : B3;
// B3return phi(B1:1, B2:r[4]);
// B2i[3] = phi(B1:2, B2:i[4]);r[3] = phi(B1:1, B2:r[4]);r[4] = r[3] * i[3];i[4] = i[3] + 1;cond[2] = i[4] <= n[1];goto cond[2] ? B2 : B3;
![Page 23: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/23.jpg)
// n[1]: 1// B1cond[1] = 2 <= 1;goto cond[1] ? B2 : B3;
// B3return phi(B1:1, B2:r[4]);
// B2i[3] = phi(B1:2, B2:i[4]);r[3] = phi(B1:1, B2:r[4]);r[4] = r[3] * i[3];i[4] = i[3] + 1;cond[2] = i[4] <= 1;goto cond[2] ? B2 : B3;
![Page 24: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/24.jpg)
// n[1]: 1// B1cond[1] = false;goto cond[1] ? B2 : B3;
// B3return phi(B1:1, B2:r[4]);
// B2i[3] = phi(B1:2, B2:i[4]);r[3] = phi(B1:1, B2:r[4]);r[4] = r[3] * i[3];i[4] = i[3] + 1;cond[2] = i[4] <= 1;goto cond[2] ? B2 : B3;
![Page 25: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/25.jpg)
// n[1]: 1// B1goto B3;
// B3return 1;
![Page 26: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/26.jpg)
// B3return 1;
![Page 27: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/27.jpg)
// n[1]: 10// B1cond[1] = 2 <= 10;goto cond[1] ? B2 : B3;
// B3return phi(B1:1, B2:r[4]);
// B2i[3] = phi(B1:2, B2:i[4]);r[3] = phi(B1:1, B2:r[4]);r[4] = r[3] * i[3];i[4] = i[3] + 1;cond[2] = i[4] <= 10;goto cond[2] ? B2 : B3;
![Page 28: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/28.jpg)
// n[1]: 10// B1cond[1] = true;goto cond[1] ? B2 : B3;
// B3return phi(B1:1, B2:r[4]);
// B2i[3] = phi(B1:2, B2:i[4]);r[3] = phi(B1:1, B2:r[4]);r[4] = r[3] * i[3];i[4] = i[3] + 1;cond[2] = i[4] <= 10;goto cond[2] ? B2 : B3;
![Page 29: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/29.jpg)
// n[1]: 10// B1goto B2;
// B3return r[4];
// B2i[3] = phi(B1:2, B2:i[4]);r[3] = phi(B1:1, B2:r[4]);r[4] = r[3] * i[3];i[4] = i[3] + 1;cond[2] = i[4] <= 10;goto cond[2] ? B2 : B3;
![Page 30: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/30.jpg)
// n[1]: 10// B1goto B2;
// B3return r[4];
// B2i[3] = phi(B1:2, B2:i[4]);r[3] = phi(B1:1, B2:r[4]);r[4] = r[3] * i[3];i[4] = i[3] + 1;cond[2] = i[4] <= 10;goto cond[2] ? B2 : B3;
![Page 31: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/31.jpg)
// n[1]: 10// B1goto B2;
// B3return r[4];
// B2r[4] = 3628800
![Page 32: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/32.jpg)
// B3return 3628800;
![Page 33: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/33.jpg)
// B1r[1] = undefined;i[1] = undefined;r[2] = 1;i[2] = 2;cond[1] = i[2] <= n[1];goto cond[1] ? B2 : B3;
// B3return r[4];
// B2i[3] = phi(B1:i[2], B2:i[4]);r[3] = phi(B1:r[2], B2:r[4]);r[4] = r[3] * i[3];i[4] = i[3] + 1;cond[2] = i[4] <= n[1];goto cond[2] ? B2 : B3;
![Page 34: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/34.jpg)
function sum(x, y) { return x + y;}
// B1result = x + y;return result;
![Page 35: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/35.jpg)
function sum(x: ???, y: ???) { return x + /* ??? */ y;}
![Page 36: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/36.jpg)
struct JSValue { type: int8; value: int64;}
@readnone function runtime.binop.add(x, y);
function sum(x: JSValue, y: JSValue) { return runtime.binop.add(x, y);}
![Page 37: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/37.jpg)
struct JSValue { type: int8; value: float64;}
@readnone function runtime.binop.add(x, y);
function inc(x: JSValue) { y = JSValue { type: 3 /* number */, value: 1. }; return runtime.binop.add(x, y);}
![Page 38: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/38.jpg)
function distance(x1, y1, x2, y2) { diff1 = runtime.binop.sub(x1, y1); diff2 = runtime.binop.sub(x2, y2);
sqrdiff1 = runtime.binop.mul(diff1, diff1); sqrdiff2 = runtime.binop.mul(diff2, diff2);
sum = runtime.binop.add(sqrdiff1, sqrdiff2); return runtime.unop.sqrt(sum);}
![Page 39: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/39.jpg)
x1_number = runtime.unbox_to_number(x1);y1_number = runtime.unbox_to_number(y1);diff1 = x1_number – y1_number;…sqrdiff1 = diff1 * diff1;…sum = sqrdiff1 + sqrdiff2;result = sqrt(sum);return runtime.box_number(result);
![Page 40: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/40.jpg)
![Page 41: How I tried to compile JavaScript](https://reader036.vdocuments.us/reader036/viewer/2022062522/5881012f1a28abc3368b750f/html5/thumbnails/41.jpg)
// TODO: More slides