Skip to content

Commit

Permalink
Fixed imports to work in JS. Also fixed a few missing references.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Crane committed Jul 25, 2012
1 parent d9d0ff4 commit a7fdb2b
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 87 deletions.
5 changes: 4 additions & 1 deletion docs/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,14 @@ <h2>K&R Malloc vs. JS Object Allocation</h2>
<script src="vendor/cm/mode/javascript/javascript.js"></script>
<script src="vendor/jquery-1.7.2.min.js"></script>

<script src="lljs/estransform.js"></script>
<script src="lljs/types.js"></script>
<script src="lljs/util.js"></script>
<script src="lljs/scope.js"></script>
<script src="lljs/esprima.js"></script>
<script src="lljs/escodegen.js"></script>
<script src="lljs/estransform.js"></script>
<script src="lljs/compiler.js"></script>
<script src="lljs/memcheck.js"></script>
<script src="lljs/memory.js"></script>
<script src="lljs/ljc.js"></script>

Expand Down
4 changes: 3 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,12 @@ <h4>Acknowledgements</h4>
<script src="vendor/cm/mode/javascript/javascript.js"></script>
<script src="vendor/jquery-1.7.2.min.js"></script>

<script src="lljs/estransform.js"></script>
<script src="lljs/types.js"></script>
<script src="lljs/util.js"></script>
<script src="lljs/scope.js"></script>
<script src="lljs/esprima.js"></script>
<script src="lljs/escodegen.js"></script>
<script src="lljs/estransform.js"></script>
<script src="lljs/compiler.js"></script>
<script src="lljs/memcheck.js"></script>
<script src="lljs/memory.js"></script>
Expand Down
99 changes: 23 additions & 76 deletions src/compiler.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
(function (exports) {
var util, T, S, Types;
if (typeof process !== "undefined") {
var util = require("./util.js");
var T = require("./estransform.js");
var S = require("./scope.js");
var Types = require("./types.js");
} else {
var util = this.util;
var T = estransform;
util = require("./util.js");
T = require("./estransform.js");
S = require("./scope.js");
Types = require("./types.js");
} else if (typeof snarf !== "undefined") {
util = this.util;
T = estransform;
load("./scope.js");
var S = scope;
S = scope;
load("./types.js");
} else {
util = this.util;
T = estransform;
S = scope;
Types = this.Types;
}


/**
* Import nodes.
*/
Expand Down Expand Up @@ -50,6 +57,14 @@
const clone = util.clone;
const extend = util.extend;
const cast = util.cast;
const isInteger = util.isInteger;
const isPowerOfTwo = util.isPowerOfTwo;
const log2 = util.log2;
const div4 = util.div4;
const isAlignedTo = util.isAlignedTo;
const alignTo = util.alignTo;
const dereference = util.dereference;
const realign = util.realign;

/**
* Import scopes.
Expand All @@ -73,32 +88,6 @@
* Misc utility functions.
*/

function isInteger(x) {
return (parseInt(x) | 0) === Number(x);
}

function isPowerOfTwo(x) {
return x && ((x & (x - 1)) === 0);
}

function log2(x) {
assert (isPowerOfTwo(x), "Value " + x + " is not a power of two.");
return Math.log(x) / Math.LN2;
}

function div4(x) {
assert (x % 4 === 0, "Value " + x + " is not divisible by four.");
return x / 4;
}

function isAlignedTo(offset, alignment) {
return offset & ~(alignment - 1);
}

function alignTo(offset, alignment) {
return (offset + (alignment - 1)) & ~(alignment - 1);
}

function check(condition, message, warn) {
if (!condition) {
if (warn) {
Expand Down Expand Up @@ -1084,48 +1073,6 @@
return expr;
};

function realign(expr, lalign) {
assert(expr.ty instanceof PointerType);
var ralign = expr.ty.base.align.size;

if (lalign === ralign) {
return expr;
}

var ratio, op;
if (lalign < ralign) {
ratio = ralign / lalign;
op = "<<";
} else {
ratio = lalign / ralign;
op = ">>";
}

return new BinaryExpression(op, expr, new Literal(log2(ratio)), expr.loc);
}

function alignAddress(base, byteOffset, ty) {
var address = realign(base, ty.align.size);
if (byteOffset !== 0) {
assert(isAlignedTo(byteOffset, ty.align.size), "unaligned byte offset " + byteOffset +
" for type " + quote(ty) + " with alignment " + ty.align.size);
var offset = byteOffset / ty.align.size;
address = new BinaryExpression("+", address, new Literal(offset), address.loc);
}
// Remember (coerce) the type of the address for realign, but *do not* cast.
address.ty = new PointerType(ty);
return address;
}

function dereference(address, byteOffset, ty, scope, loc) {
assert(scope);
address = alignAddress(address, byteOffset, ty);
var expr = new MemberExpression(scope.getView(ty), address, true, loc);
// Remember (coerce) the type so we can realign, but *do not* cast.
expr.ty = ty;
return expr;
}

function createPrologue(node, o) {
assert(node.frame);

Expand Down
16 changes: 9 additions & 7 deletions src/scope.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(function (exports) {

var util, T, Types;
if (typeof process !== "undefined") {
var util = require("./util.js");
var T = require("./estransform.js");
var Types = require("./types.js");
util = require("./util.js");
T = require("./estransform.js");
Types = require("./types.js");
} else {
var util = this.util;
var T = this.T;
var Types = this.Types;
util = this.util;
T = estransform;
Types = this.Types;
}

/**
Expand Down Expand Up @@ -45,6 +45,8 @@
*/
const assert = util.assert;
const cast = util.cast;
const alignTo = util.alignTo;
const dereference = util.dereference;

/**
* Import types.
Expand Down
88 changes: 86 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,87 @@
(function (exports) {
var T, Types;
if (typeof process !== "undefined") {
var T = require("./estransform.js");
T = require("./estransform.js");
Types = require("./types.js");
} else {
var T = estransform;
T = estransform;
Types = this.Types;
}

const CastExpression = T.CastExpression;
const BinaryExpression = T.BinaryExpression;
const Literal = T.Literal;
const MemberExpression = T.MemberExpression;


function realign(expr, lalign) {
assert(expr.ty instanceof Types.PointerType);
var ralign = expr.ty.base.align.size;

if (lalign === ralign) {
return expr;
}

var ratio, op;
if (lalign < ralign) {
ratio = ralign / lalign;
op = "<<";
} else {
ratio = lalign / ralign;
op = ">>";
}

return new BinaryExpression(op, expr, new Literal(log2(ratio)), expr.loc);
}

function alignAddress(base, byteOffset, ty) {
var address = realign(base, ty.align.size);
if (byteOffset !== 0) {
assert(isAlignedTo(byteOffset, ty.align.size), "unaligned byte offset " + byteOffset +
" for type " + quote(ty) + " with alignment " + ty.align.size);
var offset = byteOffset / ty.align.size;
address = new BinaryExpression("+", address, new Literal(offset), address.loc);
}
// Remember (coerce) the type of the address for realign, but *do not* cast.
address.ty = new Types.PointerType(ty);
return address;
}

function dereference(address, byteOffset, ty, scope, loc) {
assert(scope);
address = alignAddress(address, byteOffset, ty);
var expr = new MemberExpression(scope.getView(ty), address, true, loc);
// Remember (coerce) the type so we can realign, but *do not* cast.
expr.ty = ty;
return expr;
}


function isInteger(x) {
return (parseInt(x) | 0) === Number(x);
}

function isPowerOfTwo(x) {
return x && ((x & (x - 1)) === 0);
}

function log2(x) {
assert (isPowerOfTwo(x), "Value " + x + " is not a power of two.");
return Math.log(x) / Math.LN2;
}

function div4(x) {
assert (x % 4 === 0, "Value " + x + " is not divisible by four.");
return x / 4;
}

function isAlignedTo(offset, alignment) {
return offset & ~(alignment - 1);
}

function alignTo(offset, alignment) {
return (offset + (alignment - 1)) & ~(alignment - 1);
}

function assert(condition, message) {
if (!condition) {
Expand Down Expand Up @@ -438,5 +514,13 @@
exports.clone = clone;
exports.extend = extend;
exports.cast = cast;
exports.isInteger = isInteger;
exports.isPowerOfTwo = isPowerOfTwo;
exports.log2 = log2;
exports.div4 = div4;
exports.isAlignedTo = isAlignedTo;
exports.alignTo = alignTo;
exports.dereference = dereference;
exports.realign = realign;

}(typeof exports === 'undefined' ? (util = {}) : exports));

0 comments on commit a7fdb2b

Please sign in to comment.