Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
How I spent my summer
tried to compile javascript
INGVAR STEPANYAN AKA @RREVERSER
Compilers pipeline
Compilers pipeline
function factorial(n) {
var result = 1;
for (var i = 2; i <= n; i++) {
result *= i;
}
return result;
}
‘var result = 1’
[‘v’,‘a‘,‘r’,‘ ‘,‘r’,‘e’,‘s’,‘u’,‘l’,‘t’,‘ ‘,‘=‘,‘
‘,‘1’]
Keyword Identifier Punctuator Number
[Keyword(‘var’), Identifier(‘result’), Punctuator(‘=‘), Number(‘1’)]
VarDeclaration
VarDeclarator
Identifier Expression
function factorial(n) {
var result = 1;
for (var i = 2; i <= n; i++) {
result *= i;
}
return result;
}
function factorial(n) {
var result = 1, i = 2;
while (i <= n) {
result *= i++;
}
return result;
}
function factorial(n) {
var result = 1, i = 1;
do {
result *= i++;
} while (i <= n);
return result;
}
function factorial(n) {
var result = 1;
for (var i = 2; i <= n; i++) {
result *= i;
}
return result;
}
function factorial(n) {
var n, result, i; // all vars
result = 1;
for (i = 2; i <= n; i++) {
result *= i;
}
return result;...
function factorial(n) {
var n, result, i; // all vars
result = 1;
for (i = 2; i <= n; i = i + 1) {
result = result * i;
}
...
function factorial(n) {
var n, result, i; // all vars
result = 1;
i = 2;
while (i <= n) {
result = result * i;
i = i + 1;
...
function factorial(n) {
result = undefined;
i = undefined;
result = 1;
i = 2;
while (i <= n) {
result = result * i;
i = i ...
// B1
result = undefined;
i = undefined;
result = 1;
i = 2;
goto B2;
// B4
return result;
// B3
result = result * i;
i = i...
// B1
result = undefined;
i = undefined;
result = 1;
i = 2;
cond = i <= n;
goto cond ? B2 : B3;
// B3
return result;
// B2...
// B1
r[1] = undefined;
i[1] = undefined;
r[2] = 1;
i[2] = 2;
cond[1] = i[2] <= n[1];
goto cond ? B2 : B3;
// B3
return r;...
// B1
r[1] = undefined;
i[1] = undefined;
r[2] = 1;
i[2] = 2;
cond[1] = i[2] <= n[1];
goto cond[1] ? B2 : B3;
// B3
return...
// B1
r[2] = 1;
i[2] = 2;
cond[1] = i[2] <= n[1];
goto cond[1] ? B2 : B3;
// B3
return phi(B1:r[2], B2:r[4]);
// B2
i[3] =...
// B1
cond[1] = 2 <= n[1];
goto cond[1] ? B2 : B3;
// B3
return phi(B1:1, B2:r[4]);
// B2
i[3] = phi(B1:2, B2:i[4]);
r[3] ...
// n[1]: 1
// B1
cond[1] = 2 <= n[1];
goto cond[1] ? B2 : B3;
// B3
return phi(B1:1, B2:r[4]);
// B2
i[3] = phi(B1:2, B2:i...
// n[1]: 1
// B1
cond[1] = 2 <= 1;
goto cond[1] ? B2 : B3;
// B3
return phi(B1:1, B2:r[4]);
// B2
i[3] = phi(B1:2, B2:i[4]...
// n[1]: 1
// B1
cond[1] = false;
goto cond[1] ? B2 : B3;
// B3
return phi(B1:1, B2:r[4]);
// B2
i[3] = phi(B1:2, B2:i[4])...
// n[1]: 1
// B1
goto B3;
// B3
return 1;
// B3
return 1;
// n[1]: 10
// B1
cond[1] = 2 <= 10;
goto cond[1] ? B2 : B3;
// B3
return phi(B1:1, B2:r[4]);
// B2
i[3] = phi(B1:2, B2:i[...
// n[1]: 10
// B1
cond[1] = true;
goto cond[1] ? B2 : B3;
// B3
return phi(B1:1, B2:r[4]);
// B2
i[3] = phi(B1:2, B2:i[4])...
// n[1]: 10
// B1
goto B2;
// B3
return r[4];
// B2
i[3] = phi(B1:2, B2:i[4]);
r[3] = phi(B1:1, B2:r[4]);
r[4] = r[3] * i[...
// n[1]: 10
// B1
goto B2;
// B3
return r[4];
// B2
i[3] = phi(B1:2, B2:i[4]);
r[3] = phi(B1:1, B2:r[4]);
r[4] = r[3] * i[...
// n[1]: 10
// B1
goto B2;
// B3
return r[4];
// B2
r[4] = 3628800
// B3
return 3628800;
// B1
r[1] = undefined;
i[1] = undefined;
r[2] = 1;
i[2] = 2;
cond[1] = i[2] <= n[1];
goto cond[1] ? B2 : B3;
// B3
return...
function sum(x, y) {
return x + y;
}
// B1
result = x + y;
return result;
function sum(x: ???, y: ???) {
return x + /* ??? */ y;
}
struct JSValue {
type: int8;
value: int64;
}
@readnone function runtime.binop.add(x, y);
function sum(x: JSValue, y: JSVal...
struct JSValue {
type: int8;
value: float64;
}
@readnone function runtime.binop.add(x, y);
function inc(x: JSValue) {
y = ...
function distance(x1, y1, x2, y2) {
diff1 = runtime.binop.sub(x1, y1);
diff2 = runtime.binop.sub(x2, y2);
sqrdiff1 = runti...
x1_number = runtime.unbox_to_number(x1);
y1_number = runtime.unbox_to_number(y1);
diff1 = x1_number – y1_number;
…
sqrdiff...
// TODO: More slides
How I tried to compile JavaScript
Upcoming SlideShare
Loading in …5
×

How I tried to compile JavaScript

378 views

Published on

Talk from KharkivJS 2016

Published in: Technology
  • Be the first to comment

  • Be the first to like this

How I tried to compile JavaScript

  1. 1. How I spent my summer tried to compile javascript INGVAR STEPANYAN AKA @RREVERSER
  2. 2. Compilers pipeline
  3. 3. Compilers pipeline
  4. 4. function factorial(n) { var result = 1; for (var i = 2; i <= n; i++) { result *= i; } return result; }
  5. 5. ‘var result = 1’
  6. 6. [‘v’,‘a‘,‘r’,‘ ‘,‘r’,‘e’,‘s’,‘u’,‘l’,‘t’,‘ ‘,‘=‘,‘ ‘,‘1’] Keyword Identifier Punctuator Number
  7. 7. [Keyword(‘var’), Identifier(‘result’), Punctuator(‘=‘), Number(‘1’)] VarDeclaration VarDeclarator Identifier Expression
  8. 8. function factorial(n) { var result = 1; for (var i = 2; i <= n; i++) { result *= i; } return result; }
  9. 9. function factorial(n) { var result = 1, i = 2; while (i <= n) { result *= i++; } return result; }
  10. 10. function factorial(n) { var result = 1, i = 1; do { result *= i++; } while (i <= n); return result; }
  11. 11. function factorial(n) { var result = 1; for (var i = 2; i <= n; i++) { result *= i; } return result; }
  12. 12. function factorial(n) { var n, result, i; // all vars result = 1; for (i = 2; i <= n; i++) { result *= i; } return result; }
  13. 13. function factorial(n) { var n, result, i; // all vars result = 1; for (i = 2; i <= n; i = i + 1) { result = result * i; } return result; }
  14. 14. function factorial(n) { var n, result, i; // all vars result = 1; i = 2; while (i <= n) { result = result * i; i = i + 1; } return result; }
  15. 15. function factorial(n) { result = undefined; i = undefined; result = 1; i = 2; while (i <= n) { result = result * i; i = i + 1; } return result; }
  16. 16. // B1 result = undefined; i = undefined; result = 1; i = 2; goto B2; // B4 return result; // B3 result = result * i; i = i + 1; goto B2; // B2 cond = i <= n; goto cond ? B3 : B4;
  17. 17. // B1 result = undefined; i = undefined; result = 1; i = 2; cond = i <= n; goto cond ? B2 : B3; // B3 return result; // B2 result = result * i; i = i + 1; cond = i <= n; goto cond ? B2 : B3;
  18. 18. // B1 r[1] = undefined; i[1] = undefined; r[2] = 1; i[2] = 2; cond[1] = i[2] <= n[1]; goto cond ? B2 : B3; // B3 return r; // B2 r = r * i; i = i + 1; cond = i <= n; goto cond ? B2 : B3;
  19. 19. // B1 r[1] = undefined; i[1] = undefined; r[2] = 1; i[2] = 2; cond[1] = i[2] <= n[1]; goto cond[1] ? B2 : B3; // B3 return r[4]; // B2 i[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;
  20. 20. // B1 r[2] = 1; i[2] = 2; cond[1] = i[2] <= n[1]; goto cond[1] ? B2 : B3; // B3 return phi(B1:r[2], B2:r[4]); // B2 i[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;
  21. 21. // B1 cond[1] = 2 <= n[1]; goto cond[1] ? B2 : B3; // B3 return phi(B1:1, B2:r[4]); // B2 i[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;
  22. 22. // n[1]: 1 // B1 cond[1] = 2 <= n[1]; goto cond[1] ? B2 : B3; // B3 return phi(B1:1, B2:r[4]); // B2 i[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;
  23. 23. // n[1]: 1 // B1 cond[1] = 2 <= 1; goto cond[1] ? B2 : B3; // B3 return phi(B1:1, B2:r[4]); // B2 i[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;
  24. 24. // n[1]: 1 // B1 cond[1] = false; goto cond[1] ? B2 : B3; // B3 return phi(B1:1, B2:r[4]); // B2 i[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;
  25. 25. // n[1]: 1 // B1 goto B3; // B3 return 1;
  26. 26. // B3 return 1;
  27. 27. // n[1]: 10 // B1 cond[1] = 2 <= 10; goto cond[1] ? B2 : B3; // B3 return phi(B1:1, B2:r[4]); // B2 i[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;
  28. 28. // n[1]: 10 // B1 cond[1] = true; goto cond[1] ? B2 : B3; // B3 return phi(B1:1, B2:r[4]); // B2 i[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;
  29. 29. // n[1]: 10 // B1 goto B2; // B3 return r[4]; // B2 i[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;
  30. 30. // n[1]: 10 // B1 goto B2; // B3 return r[4]; // B2 i[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;
  31. 31. // n[1]: 10 // B1 goto B2; // B3 return r[4]; // B2 r[4] = 3628800
  32. 32. // B3 return 3628800;
  33. 33. // B1 r[1] = undefined; i[1] = undefined; r[2] = 1; i[2] = 2; cond[1] = i[2] <= n[1]; goto cond[1] ? B2 : B3; // B3 return r[4]; // B2 i[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;
  34. 34. function sum(x, y) { return x + y; } // B1 result = x + y; return result;
  35. 35. function sum(x: ???, y: ???) { return x + /* ??? */ y; }
  36. 36. 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); }
  37. 37. 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); }
  38. 38. 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); }
  39. 39. 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);
  40. 40. // TODO: More slides

×