// Convert analyzed data to javascript. Everything has already been calculated
// before this stage, which just does the final conversion to JavaScript.
function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVariables) {
substrate = new Substrate('JSifyer');
var TYPES = functionsOnly ? givenTypes : data.types;
var GLOBAL_VARIABLES = functionsOnly ? givenGlobalVariables : data.globalVariables;
var FUNCTIONS = functionsOnly ? givenFunctions : {};
// Now that analysis has completed, we can get around to handling unparsedFunctions
(functionsOnly ? data.functions : data.unparsedFunctions.concat(data.functions)).forEach(function(func) {
// Save just what we need, to save memory - whether there are varargs, and the # of parameters
FUNCTIONS[func.ident] = {
hasVarArgs: func.hasVarArgs,
numParams: func.params.length
};
});
for (var i = 0; i < data.unparsedFunctions.length; i++) {
var func = data.unparsedFunctions[i];
dprint('unparsedFunctions', 'processing |' + func.ident + '|, ' + i + '/' + data.unparsedFunctions.length);
func.JS = JSify(analyzer(intertyper(func.lines, true, func.lineNum-1), TYPES), true, TYPES, FUNCTIONS, GLOBAL_VARIABLES);
delete func.lines; // clean up memory as much as possible
}
// type
substrate.addActor('Type', {
processItem: function(item) {
var type = TYPES[item.name_];
var niceName = toNiceIdent(item.name_);
// We might export all of TYPES, cleaner that way, but do not want slowdowns in accessing flatteners
item.JS = 'var ' + niceName + '___SIZE = ' + TYPES[item.name_].flatSize + '; // ' + item.name_ + '\n';
if (type.needsFlattening && !type.flatFactor) {
item.JS += 'var ' + niceName + '___FLATTENER = ' + JSON.stringify(TYPES[item.name_].flatIndexes) + ';';
}
return [item];
}
});
function makePointer(slab, pos, allocator, type) { // type is FFU
if (slab in set('HEAP', 'IHEAP', 'FHEAP')) return pos;
if (slab[0] != '[') {
slab = '[' + slab + ']';
}
return 'Pointer_make(' + slab + ', ' + (pos ? pos : 0) + (allocator ? ', ' + allocator : '') + ')';
}
function makeGetSlab(ptr, type) {
assert(type);
if (!USE_TYPED_ARRAYS) {
return 'HEAP';
} else {
if (type in Runtime.FLOAT_TYPES || type === 'int64') {
return 'FHEAP';
} else if (type in Runtime.INT_TYPES || isPointerType(type)) {
return 'IHEAP';
} else {
return 'HEAP';
}
}
}
function makeGetPos(ptr) {
return ptr;
}
function calcFastOffset(ptr, pos, noNeedFirst) {
var offset = noNeedFirst ? '0' : makeGetPos(ptr);
return getFastValue(offset, '+', pos);
}
function makeGetValue(ptr, pos, noNeedFirst, type) {
if (isStructType(type)) {
var typeData = TYPES[type];
var ret = [];
for (var i = 0; i < typeData.fields.length; i++) {
ret.push('f' + i + ': ' + makeGetValue(ptr, pos + typeData.flatIndexes[i], noNeedFirst, typeData.fields[i]));
}
return