summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xemscripten.py28
-rw-r--r--settings.py2
-rw-r--r--src/analyzer.js31
-rw-r--r--src/intertyper.js94
-rw-r--r--src/jsifier.js66
-rw-r--r--src/library.js12
-rw-r--r--src/modules.js45
-rw-r--r--src/parseTools.js33
-rw-r--r--src/preamble.js1
-rw-r--r--src/runtime.js2
-rw-r--r--src/settings.js2
-rw-r--r--tests/cases/aliasbitcast.ll37
-rw-r--r--tests/cases/gepoverflow_q1.txt1
-rw-r--r--tests/cases/storestruct_q1.txt3
-rw-r--r--tests/dlmalloc.c32
-rw-r--r--tests/runner.py102
-rw-r--r--tools/autodebugger.py19
-rw-r--r--tools/shared.py13
18 files changed, 371 insertions, 152 deletions
diff --git a/emscripten.py b/emscripten.py
index 022068c8..21abcc1d 100755
--- a/emscripten.py
+++ b/emscripten.py
@@ -11,11 +11,6 @@ from tools import shared
# Temporary files that should be deleted once the program is finished.
TEMP_FILES_TO_CLEAN = []
-# The data layout used by llvm-gcc (as opposed to clang, which doesn't have the
-# f128:128:128 part).
-GCC_DATA_LAYOUT = ('target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16'
- '-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64'
- '-v128:128:128-a0:0:64-f80:32:32-f128:128:128-n8:16:32"')
def path_from_root(*target):
@@ -98,37 +93,20 @@ def link(*objects):
return out.name
-def compile_malloc(compiler):
+def compile_malloc():
"""Compiles dlmalloc to LLVM bitcode.
- Args:
- compiler: The compiler command to use, a path to either clang or llvm-gcc.
-
Returns:
The path to the compiled dlmalloc as an LLVM bitcode (.bc) file.
"""
src = path_from_root('src', 'dlmalloc.c')
includes = '-I' + path_from_root('src', 'include')
- command = [compiler, '-c', '-g', '-emit-llvm', '-m32', '-o-', includes, src]
+ command = [shared.CLANG, '-c', '-g', '-emit-llvm', '-m32', '-o-', includes, src]
with get_temp_file('.bc') as out: ret = subprocess.call(command, stdout=out)
if ret != 0: raise RuntimeError('Could not compile dlmalloc.')
return out.name
-def determine_compiler(filepath):
- """Determines whether a given file uses llvm-gcc or clang data layout.
-
- Args:
- filepath: The .bc or .ll file containing the bitcode/assembly to test.
-
- Returns:
- The path to the compiler, either llvm-gcc or clang.
- """
- assembly = open(disassemble(filepath)).read()
- is_gcc = GCC_DATA_LAYOUT in assembly
- return shared.to_cc(shared.LLVM_GCC if is_gcc else shared.CLANG)
-
-
def has_annotations(filepath):
"""Tests whether an assembly file contains annotations.
@@ -164,7 +142,7 @@ def main(args):
if args.dlmalloc or args.optimize or not has_annotations(args.infile):
args.infile = assemble(args.infile)
if args.dlmalloc:
- malloc = compile_malloc(determine_compiler(args.infile))
+ malloc = compile_malloc()
args.infile = link(args.infile, malloc)
if args.optimize: args.infile = optimize(args.infile)
args.infile = disassemble(args.infile)
diff --git a/settings.py b/settings.py
index c6d63341..fe9297f7 100644
--- a/settings.py
+++ b/settings.py
@@ -7,8 +7,6 @@ TEMP_DIR='/dev/shm'
LLVM_ROOT=os.path.expanduser('~/Dev/llvm-2.9/cbuild/bin')
-LLVM_GCC=os.path.expanduser('~/Dev/llvm-gcc-2.9/cbuild/install/bin/llvm-g++')
-
COMPILER_OPTS = ['-m32'] # Need to build as 32bit arch, for now -
# various errors on 64bit compilation
# WARNING: '-g' here will generate llvm bitcode that lli will crash on!
diff --git a/src/analyzer.js b/src/analyzer.js
index 824e7903..9d542e2c 100644
--- a/src/analyzer.js
+++ b/src/analyzer.js
@@ -46,8 +46,10 @@ function analyzer(data) {
// Functions & labels
item.functions = [];
var currLabelFinished; // Sometimes LLVM puts a branch in the middle of a label. We need to ignore all lines after that.
+ item.items.sort(function(a, b) { return a.lineNum - b.lineNum });
for (var i = 0; i < item.items.length; i++) {
var subItem = item.items[i];
+ assert(subItem.lineNum);
if (subItem.intertype == 'function') {
item.functions.push(subItem);
subItem.endLineNum = null;
@@ -58,7 +60,7 @@ function analyzer(data) {
if (LLVM_STYLE == 'new' && item.items[i+1].intertype !== 'label') {
item.items.splice(i+1, 0, {
intertype: 'label',
- ident: '_entry',
+ ident: toNiceIdent('%0'),
lineNum: subItem.lineNum + '.5'
});
}
@@ -77,10 +79,10 @@ function analyzer(data) {
currLabelFinished = true;
}
} else {
- print('// WARNING: content after a branch in a label, line: ' + subItem.lineNum);
+ print('// WARNING: content after a branch in a label, line: ' + subItem.lineNum + '::' + dump(subItem));
}
} else {
- print("ERROR: what is this? " + JSON.stringify(subItem));
+ throw "ERROR: what is this? " + JSON.stringify(subItem);
}
}
delete item.items;
@@ -107,6 +109,8 @@ function analyzer(data) {
// check that we never allocate with this (either as a child structure
// in the analyzer, or in calcSize in alloca).
var subType = check[2];
+ addTypeInternal(subType, data); // needed for anonymous structure definitions (see below)
+
Types.types[nonPointing] = {
name_: nonPointing,
fields: range(num).map(function() { return subType }),
@@ -124,6 +128,20 @@ function analyzer(data) {
return;
}
+ // anonymous structure definition, for example |{ i32, i8*, void ()*, i32 }|
+ if (type[0] == '{' || type[0] == '<') {
+ var packed = type[0] == '<';
+ Types.types[type] = {
+ name_: type,
+ fields: splitTokenList(tokenize(type.substr(2 + packed, type.length - 4 - 2*packed)).tokens).map(function(segment) {
+ return segment[0].text;
+ }),
+ packed: packed,
+ lineNum: '?'
+ };
+ return;
+ }
+
if (isPointerType(type)) return;
if (['['].indexOf(type) != -1) return;
Types.types[type] = {
@@ -333,7 +351,7 @@ function analyzer(data) {
// Decision time
- var pointedType = removePointing(variable.type);
+ var pointedType = pointingLevels(variable.type) > 0 ? removePointing(variable.type) : null;
if (variable.origin == 'getelementptr') {
// Use our implementation that emulates pointers etc.
// TODO Can we perhaps nativize some of these? However to do so, we need to discover their
@@ -563,7 +581,7 @@ function analyzer(data) {
func.labelsDict[label.ident] = label;
func.labelIds[label.ident] = func.labelIdCounter++;
});
- func.labelIds[toNiceIdent('%entry')] = -1; // entry is always -1
+ func.labelIds[toNiceIdent('%0')] = -1; // entry is always -1
func.hasPhi = false;
func.hasIndirectBr = false;
@@ -575,8 +593,9 @@ function analyzer(data) {
var remarkableLabelId = line.value.params[i].label;
func.remarkableLabels.push(remarkableLabelId);
var remarkableLabel = func.labelsDict[remarkableLabelId];
+ assert(remarkableLabel);
var lastLine = remarkableLabel.lines.slice(-1)[0];
- if (lastLine.value) {
+ if (lastLine.intertype === 'assign') {
lastLine.value.currLabelId = remarkableLabelId;
} else {
lastLine.currLabelId = remarkableLabelId;
diff --git a/src/intertyper.js b/src/intertyper.js
index 83a49645..280b8a3f 100644
--- a/src/intertyper.js
+++ b/src/intertyper.js
@@ -2,6 +2,9 @@
// to be processed by the later stages.
var tokenizer; // TODO: Clean this up/out
+function tokenize(text) {
+ return tokenizer.processItem({ lineText: text }, true);
+}
//! @param parseFunctions We parse functions only on later passes, since we do not
//! want to parse all of them at once, and have all their
@@ -48,8 +51,11 @@ function intertyper(data, parseFunctions, baseLineNum) {
currFunctionLineNum = i + 1;
}
if (!inFunction || parseFunctions) {
- if (inContinual || new RegExp(/^\ +to.*/g).test(line)) {
- // to after invoke
+ if (inContinual || new RegExp(/^\ +to.*/g).test(line)
+ || new RegExp(/^\ +catch .*/g).test(line)
+ || new RegExp(/^\ +filter .*/g).test(line)
+ || new RegExp(/^\ +cleanup .*/g).test(line)) {
+ // to after invoke or landingpad second line
ret.slice(-1)[0].lineText += line;
if (new RegExp(/^\ +\]/g).test(line)) { // end of llvm switch
inContinual = false;
@@ -137,13 +143,15 @@ function intertyper(data, parseFunctions, baseLineNum) {
// merge certain tokens
if (lastToken && isType(lastToken.text) && isFunctionDef(token)) {
lastToken.text += ' ' + text;
- } else if (lastToken && text[text.length-1] == '}') {
+ } else if (lastToken && /^}\**$/.exec(text)) { // }, }*, etc.
var openBrace = tokens.length-1;
- while (tokens[openBrace].text != '{') openBrace --;
+ while (tokens[openBrace].text.substr(-1) != '{') openBrace --;
token = combineTokens(tokens.slice(openBrace+1));
tokens.splice(openBrace, tokens.length-openBrace+1);
tokens.push(token);
token.type = '{';
+ token.text = '{ ' + token.text + ' }';
+ while (pointingLevels(text) > pointingLevels(token.text)) token.text += '*'; // TODO: optimize
lastToken = token;
} else {
tokens.push(token);
@@ -265,6 +273,8 @@ function intertyper(data, parseFunctions, baseLineNum) {
return 'Unreachable';
if (tokensLength >= 3 && token0Text == 'indirectbr')
return 'IndirectBr';
+ if (tokensLength >= 2 && token0Text == 'resume')
+ return 'Resume';
} else if (item.indent === -1) {
if (tokensLength >= 3 &&
(token0Text == 'load' || token1Text == 'load'))
@@ -280,8 +290,12 @@ function intertyper(data, parseFunctions, baseLineNum) {
return 'Alloca';
if (tokensLength >= 3 && token0Text == 'extractvalue')
return 'ExtractValue';
+ if (tokensLength >= 3 && token0Text == 'insertvalue')
+ return 'InsertValue';
if (tokensLength >= 3 && token0Text == 'phi')
return 'Phi';
+ if (tokensLength >= 3 && token0Text == 'landingpad')
+ return 'Landingpad';
} else if (item.indent === 0) {
if ((tokensLength >= 1 && token0Text.substr(-1) == ':') || // LLVM 2.7 format, or llvm-gcc in 2.8
(tokensLength >= 3 && token1Text == '<label>'))
@@ -320,6 +334,7 @@ function intertyper(data, parseFunctions, baseLineNum) {
processItem: function(item) {
function scanConst(value, type) {
//dprint('inter-const: ' + item.lineNum + ' : ' + JSON.stringify(value) + ',' + type + '\n');
+ Types.needAnalysis[type] = 0;
if (Runtime.isNumberType(type) || pointingLevels(type) >= 1) {
return { value: toNiceIdent(value.text), type: type };
} else if (value.text in set('zeroinitializer', 'undef')) { // undef doesn't really need initting, but why not
@@ -334,16 +349,21 @@ function intertyper(data, parseFunctions, baseLineNum) {
if (segment[1].text == 'null') {
return { intertype: 'value', value: 0, type: 'i32' };
} else if (segment[1].text == 'zeroinitializer') {
+ Types.needAnalysis[segment[0].text] = 0;
return { intertype: 'emptystruct', type: segment[0].text };
} else if (segment[1].text in PARSABLE_LLVM_FUNCTIONS) {
return parseLLVMFunctionCall(segment);
} else if (segment[1].type && segment[1].type == '{') {
+ Types.needAnalysis[segment[0].text] = 0;
return { intertype: 'struct', type: segment[0].text, contents: handleSegments(segment[1].tokens) };
} else if (segment[1].type && segment[1].type == '<') {
+ Types.needAnalysis[segment[0].text] = 0;
return { intertype: 'struct', type: segment[0].text, contents: handleSegments(segment[1].item.tokens[0].tokens) };
} else if (segment[1].type && segment[1].type == '[') {
+ Types.needAnalysis[segment[0].text] = 0;
return { intertype: 'list', type: segment[0].text, contents: handleSegments(segment[1].item.tokens) };
} else if (segment.length == 2) {
+ Types.needAnalysis[segment[0].text] = 0;
return { intertype: 'value', type: segment[0].text, value: toNiceIdent(segment[1].text) };
} else if (segment[1].text === 'c') {
// string
@@ -378,15 +398,18 @@ function intertyper(data, parseFunctions, baseLineNum) {
}
if (item.tokens[2].text == 'alias') {
- cleanOutTokensSet(LLVM.LINKAGES, item.tokens, 3);
- cleanOutTokensSet(LLVM.VISIBILITIES, item.tokens, 3);
- return [{
+ cleanOutTokens(LLVM.LINKAGES, item.tokens, 3);
+ cleanOutTokens(LLVM.VISIBILITIES, item.tokens, 3);
+ var last = getTokenIndexByText(item.tokens, ';');
+ var ret = {
intertype: 'alias',
ident: toNiceIdent(item.tokens[0].text),
- aliasee: toNiceIdent(item.tokens[4].text),
- type: item.tokens[3].text,
+ value: parseLLVMSegment(item.tokens.slice(3, last)),
lineNum: item.lineNum
- }];
+ };
+ ret.type = ret.value.type;
+ Types.needAnalysis[ret.type] = 0;
+ return [ret];
}
if (item.tokens[2].text == 'type') {
var fields = [];
@@ -397,7 +420,7 @@ function intertyper(data, parseFunctions, baseLineNum) {
} else if (item.tokens[3].text != 'opaque') {
if (item.tokens[3].type == '<') {
packed = true;
- item.tokens[3] = tokenizer.processItem({ lineText: '{ ' + item.tokens[3].item.tokens[0].text + ' }' }, true).tokens[0];
+ item.tokens[3] = item.tokens[3].item.tokens[0];
}
var subTokens = item.tokens[3].tokens;
subTokens.push({text:','});
@@ -418,13 +441,13 @@ function intertyper(data, parseFunctions, baseLineNum) {
} else {
// variable
var ident = item.tokens[0].text;
- cleanOutTokensSet(LLVM.GLOBAL_MODIFIERS, item.tokens, 3);
- cleanOutTokensSet(LLVM.GLOBAL_MODIFIERS, item.tokens, 2);
+ cleanOutTokens(LLVM.GLOBAL_MODIFIERS, item.tokens, [2, 3]);
var external = false;
if (item.tokens[2].text === 'external') {
external = true;
item.tokens.splice(2, 1);
}
+ Types.needAnalysis[item.tokens[2].text] = 0;
var ret = {
intertype: 'globalVariable',
ident: toNiceIdent(ident),
@@ -515,7 +538,7 @@ function intertyper(data, parseFunctions, baseLineNum) {
substrate.addActor('Load', {
processItem: function(item) {
item.intertype = 'load';
- if (item.tokens[0].text == 'volatile') item.tokens.shift(0);
+ cleanOutTokens(LLVM.ACCESS_OPTIONS, item.tokens, [0, 1]);
item.pointerType = item.tokens[1].text;
item.valueType = item.type = removePointing(item.pointerType);
Types.needAnalysis[item.type] = 0;
@@ -537,6 +560,20 @@ function intertyper(data, parseFunctions, baseLineNum) {
this.forwardItem(item, 'Reintegrator');
}
});
+ // 'insertvalue'
+ substrate.addActor('InsertValue', {
+ processItem: function(item) {
+ var last = getTokenIndexByText(item.tokens, ';');
+ item.intertype = 'insertvalue';
+ item.type = item.tokens[1].text; // Of the origin aggregate, as well as the result
+ Types.needAnalysis[item.type] = 0;
+ item.ident = toNiceIdent(item.tokens[2].text);
+ var segments = splitTokenList(item.tokens.slice(4, last));
+ item.value = parseLLVMSegment(segments[0]);
+ item.indexes = segments.slice(1);
+ this.forwardItem(item, 'Reintegrator');
+ }
+ });
// 'bitcast'
substrate.addActor('Bitcast', {
processItem: function(item) {
@@ -607,9 +644,10 @@ function intertyper(data, parseFunctions, baseLineNum) {
}
item.ident = toNiceIdent(item.ident);
if (type === 'invoke') {
- cleanOutTokens(['alignstack', 'alwaysinline', 'inlinehint', 'naked', 'noimplicitfloat', 'noinline', 'alwaysinline attribute.', 'noredzone', 'noreturn', 'nounwind', 'optsize', 'readnone', 'readonly', 'ssp', 'sspreq'], item.tokens, 4);
- item.toLabel = toNiceIdent(item.tokens[6].text);
- item.unwindLabel = toNiceIdent(item.tokens[9].text);
+ var toIndex = findTokenText(item, 'to');
+ item.toLabel = toNiceIdent(item.tokens[toIndex+2].text);
+ item.unwindLabel = toNiceIdent(item.tokens[toIndex+5].text);
+ assert(item.toLabel && item.unwindLabel);
}
if (item.indent == 2) {
// standalone call - not in assign
@@ -629,6 +667,15 @@ function intertyper(data, parseFunctions, baseLineNum) {
return makeCall.call(this, item, 'invoke');
}
});
+ // 'landingpad' - just a stub implementation
+ substrate.addActor('Landingpad', {
+ processItem: function(item) {
+ item.intertype = 'landingpad';
+ item.type = item.tokens[1].text;
+ Types.needAnalysis[item.type] = 0;
+ this.forwardItem(item, 'Reintegrator');
+ }
+ });
// 'alloca'
var allocaPossibleVars = ['allocatedNum'];
substrate.addActor('Alloca', {
@@ -704,7 +751,7 @@ function intertyper(data, parseFunctions, baseLineNum) {
// 'store'
substrate.addActor('Store', {
processItem: function(item) {
- if (item.tokens[0].text == 'volatile') item.tokens.shift(0);
+ cleanOutTokens(LLVM.ACCESS_OPTIONS, item.tokens, [0, 1]);
var segments = splitTokenList(item.tokens.slice(1));
var ret = {
intertype: 'store',
@@ -733,7 +780,7 @@ function intertyper(data, parseFunctions, baseLineNum) {
var commaIndex = findTokenText(item, ',');
return [{
intertype: 'branch',
- condition: parseLLVMSegment(item.tokens.slice(1, commaIndex)),
+ value: parseLLVMSegment(item.tokens.slice(1, commaIndex)),
labelTrue: toNiceIdent(item.tokens[commaIndex+2].text),
labelFalse: toNiceIdent(item.tokens[commaIndex+5].text),
lineNum: item.lineNum
@@ -754,6 +801,15 @@ function intertyper(data, parseFunctions, baseLineNum) {
}];
}
});
+ // 'resume' - partial implementation
+ substrate.addActor('Resume', {
+ processItem: function(item) {
+ return [{
+ intertype: 'resume',
+ lineNum: item.lineNum
+ }];
+ }
+ });
// 'switch'
substrate.addActor('Switch', {
processItem: function(item) {
diff --git a/src/jsifier.js b/src/jsifier.js
index c19eda3a..aa1e3c60 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -26,6 +26,8 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
ident: '_' + ident
});
});
+
+ Functions.implementedFunctions = set(data.unparsedFunctions.map(function(func) { return func.ident }));
}
// Does simple 'macro' substitution, using Django-like syntax,
@@ -257,17 +259,18 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
item.intertype = 'GlobalVariableStub';
var ret = [item];
item.JS = 'var ' + item.ident + ';';
- // Set the actual value in a postset, since it may be a global variable. TODO: handle alias of alias (needs ordering)
+ // Set the actual value in a postset, since it may be a global variable. We also order by dependencies there
+ var value = finalizeLLVMParameter(item.value, true); // do *not* indexize functions here
ret.push({
intertype: 'GlobalVariablePostSet',
- JS: item.ident + ' = ' + item.aliasee + ';'
+ ident: item.ident,
+ dependencies: set([value]),
+ JS: item.ident + ' = ' + value + ';'
});
return ret;
}
});
- var moduleFunctions = set(data.unparsedFunctions.map(function(func) { return func.ident }));
-
var addedLibraryItems = {};
// functionStub
@@ -283,7 +286,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
if (ident in addedLibraryItems) return '';
// Don't replace implemented functions with library ones (which can happen when we add dependencies).
// Note: We don't return the dependencies here. Be careful not to end up where this matters
- if (('_' + ident) in moduleFunctions) return '';
+ if (('_' + ident) in Functions.implementedFunctions) return '';
addedLibraryItems[ident] = true;
var snippet = LibraryManager.library[ident];
@@ -318,6 +321,9 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
snippet = snippet.toString();
// name the function; overwrite if it's already named
snippet = snippet.replace(/function(?:\s+([^(]+))?\s*\(/, 'function _' + ident + '(');
+ if (LIBRARY_DEBUG) {
+ snippet = snippet.replace('{', '{ print("[library call:' + ident + ']"); ');
+ }
}
var postsetId = ident + '__postset';
@@ -652,10 +658,10 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
makeFuncLineActor('branch', function(item) {
if (item.stolen) return ';'; // We will appear where we were stolen to
- if (!item.condition) {
+ if (!item.value) {
return makeBranch(item.label, item.currLabelId);
} else {
- var condition = finalizeLLVMParameter(item.condition);
+ var condition = finalizeLLVMParameter(item.value);
var labelTrue = makeBranch(item.labelTrue, item.currLabelId);
var labelFalse = makeBranch(item.labelFalse, item.currLabelId);
if (labelTrue == ';' && labelFalse == ';') return ';';
@@ -685,9 +691,9 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
ret += ' ' + makeBranch(switchLabel.label, item.currLabelId || null) + '\n';
ret += '}\n';
});
- ret += 'else {\n';
+ if (item.switchLabels.length > 0) ret += 'else {\n';
ret += makeBranch(item.defaultLabel, item.currLabelId) + '\n';
- ret += '}\n';
+ if (item.switchLabels.length > 0) ret += '}\n';
if (item.value) {
ret += ' ' + toNiceIdent(item.value);
}
@@ -705,6 +711,9 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
}
return ret + ';';
});
+ makeFuncLineActor('resume', function(item) {
+ return (EXCEPTION_DEBUG ? 'print("Resuming exception");' : '') + 'throw [0,0];';
+ });
makeFuncLineActor('invoke', function(item) {
// Wrapping in a function lets us easily return values if we are
// in an assignment
@@ -720,6 +729,10 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
+ ' } else { ' + makeBranch(item.unwindLabel, item.currLabelId) + ' }';
return ret;
});
+ makeFuncLineActor('landingpad', function(item) {
+ // Just a stub
+ return '{ f0: 0, f1: 0 }';
+ });
makeFuncLineActor('load', function(item) {
var value = finalizeLLVMParameter(item.pointer);
var impl = item.ident ? getVarImpl(item.funcData, item.ident) : VAR_EMULATED;
@@ -736,6 +749,15 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
// and we emulate them using simple JS objects { f1: , f2: , } etc., for speed
return item.ident + '.f' + item.indexes[0][0].text;
});
+ makeFuncLineActor('insertvalue', function(item) {
+ assert(item.indexes.length == 1); // TODO: see extractvalue
+ var ret = '(', ident;
+ if (item.ident === 'undef') {
+ item.ident = 'tempValue';
+ ret += item.ident + ' = [' + makeEmptyStruct(item.type) + '], ';
+ }
+ return ret + item.ident + '.f' + item.indexes[0][0].text + ' = ' + finalizeLLVMParameter(item.value) + ', ' + item.ident + ')';
+ });
makeFuncLineActor('indirectbr', function(item) {
return makeBranch(finalizeLLVMParameter(item.pointer), item.currLabelId, true);
});
@@ -817,6 +839,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
}
makeFuncLineActor('getelementptr', function(item) { return finalizeLLVMFunctionCall(item) });
makeFuncLineActor('call', function(item) {
+ if (item.standalone && LibraryManager.isStubFunction(item.ident)) return ';';
return makeFunctionCall(item.ident, item.params, item.funcData) + (item.standalone ? ';' : '');
});
@@ -829,11 +852,34 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
var itemsDict = { type: [], GlobalVariableStub: [], functionStub: [], function: [], GlobalVariable: [], GlobalVariablePostSet: [] };
items.forEach(function(item) {
item.lines = null;
- var small = { intertype: item.intertype, JS: item.JS }; // Release memory
+ var small = { intertype: item.intertype, JS: item.JS, ident: item.ident, dependencies: item.dependencies }; // Release memory
itemsDict[small.intertype].push(small);
});
items = null;
+ var splitPostSets = splitter(itemsDict.GlobalVariablePostSet, function(x) { return x.ident && x.dependencies });
+ itemsDict.GlobalVariablePostSet = splitPostSets.leftIn;
+ var orderedPostSets = splitPostSets.splitOut;
+
+ var limit = orderedPostSets.length * orderedPostSets.length;
+ for (var i = 0; i < orderedPostSets.length; i++) {
+ for (var j = i+1; j < orderedPostSets.length; j++) {
+ if (orderedPostSets[j].ident in orderedPostSets[i].dependencies) {
+ var temp = orderedPostSets[i];
+ orderedPostSets[i] = orderedPostSets[j];
+ orderedPostSets[j] = temp;
+ i--;
+ limit--;
+ assert(limit > 0, 'Could not sort postsets!');
+ break;
+ }
+ }
+ }
+
+ itemsDict.GlobalVariablePostSet = itemsDict.GlobalVariablePostSet.concat(orderedPostSets);
+
+ //
+
var generated = [];
if (mainPass) {
generated = generated.concat(itemsDict.type).concat(itemsDict.GlobalVariableStub).concat(itemsDict.functionStub);
diff --git a/src/library.js b/src/library.js
index 62e4de55..66fa72ae 100644
--- a/src/library.js
+++ b/src/library.js
@@ -805,6 +805,8 @@ LibraryManager.library = {
__01fstat64_: 'fstat',
__01stat64_: 'stat',
__01lstat64_: 'lstat',
+ stat64: 'stat',
+
// TODO: Check if other aliases are needed.
// ==========================================================================
@@ -2814,6 +2816,7 @@ LibraryManager.library = {
}
},
fseeko: 'fseek',
+ fseeko64: 'fseek',
fsetpos__deps: ['$FS', 'lseek', '__setErrNo', '$ERRNO_CODES'],
fsetpos: function(stream, pos) {
// int fsetpos(FILE *stream, const fpos_t *pos);
@@ -2852,6 +2855,7 @@ LibraryManager.library = {
}
},
ftello: 'ftell',
+ ftello64: 'ftell',
fwrite__deps: ['$FS', 'write'],
fwrite: function(ptr, size, nitems, stream) {
// size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream);
@@ -3077,6 +3081,7 @@ LibraryManager.library = {
vscanf: 'scanf',
vfscanf: 'fscanf',
vsscanf: 'sscanf',
+ fopen64: 'fopen',
__01fopen64_: 'fopen',
__01fseeko64_: 'fseek',
__01ftello64_: 'ftell',
@@ -4046,6 +4051,13 @@ LibraryManager.library = {
return ret;
},
+ llvm_expect_i32: function(x, y) {
+ return x == y; // TODO: inline this
+ },
+
+ llvm_lifetime_start: function() {},
+ llvm_lifetime_end: function() {},
+
// ==========================================================================
// iostream.h
// ==========================================================================
diff --git a/src/modules.js b/src/modules.js
index f613c20b..8205e9ff 100644
--- a/src/modules.js
+++ b/src/modules.js
@@ -8,7 +8,9 @@ var LLVM = {
'weak_odr', 'externally_visible', 'dllimport', 'dllexport', 'unnamed_addr'),
VISIBILITIES: set('default', 'hidden', 'protected'),
PARAM_ATTR: set('noalias', 'signext', 'zeroext', 'inreg', 'sret', 'nocapture', 'nest'),
- CALLING_CONVENTIONS: set('ccc', 'fastcc', 'coldcc', 'cc10', 'x86_fastcallcc')
+ CALLING_CONVENTIONS: set('ccc', 'fastcc', 'coldcc', 'cc10', 'x86_fastcallcc'),
+ ACCESS_OPTIONS: set('volatile', 'atomic'),
+ INVOKE_MODIFIERS: set('alignstack', 'alwaysinline', 'inlinehint', 'naked', 'noimplicitfloat', 'noinline', 'alwaysinline attribute.', 'noredzone', 'noreturn', 'nounwind', 'optsize', 'readnone', 'readonly', 'ssp', 'sspreq')
};
LLVM.GLOBAL_MODIFIERS = set(keys(LLVM.LINKAGES).concat(['constant', 'global', 'hidden']));
@@ -27,18 +29,19 @@ var Debugging = {
var form1 = new RegExp(/^ .*, !dbg !(\d+) *$/);
var form2 = new RegExp(/^ .*, !dbg !(\d+) *; .*$/);
- var form3 = new RegExp(/^!(\d+) = metadata !{i32 (\d+), i32 \d+, metadata !(\d+), .*}$/);
- var form3a = new RegExp(/^!(\d+) = metadata !{i32 \d+, metadata !\d+, i32 \d+, i32 \d+, metadata !(\d+), i32 \d+} ; \[ DW_TAG_lexical_block \]$/);
- var form3ab = new RegExp(/^!(\d+) = metadata !{i32 \d+, i32 \d+, metadata !(\d+), .*$/);
- var form3ac = new RegExp(/^!(\d+) = metadata !{i32 \d+, metadata !\d+, metadata !"[^"]+", metadata !(\d+)[^\[]* ; \[ DW_TAG_.*$/);
- var form3b = new RegExp(/^!(\d+) = metadata !{i32 \d+, metadata !"([^"]+)", metadata !"([^"]+)", metadata !\d+} ; \[ DW_TAG_file_type \]$/);
+ var form3 = new RegExp(/^!(\d+) = metadata !{i32 (\d+), (?:i32 \d+|null), metadata !(\d+), .*}$/);
+ var form3a = new RegExp(/^!(\d+) = metadata !{i32 \d+, metadata !\d+, (?:i32 \d+|null), (?:i32 \d+|null), metadata !(\d+), (?:i32 \d+|null)}.*/);
+ var form3ab = new RegExp(/^!(\d+) = metadata !{i32 \d+, (?:i32 \d+|null), metadata !(\d+), .*$/);
+ var form3ac = new RegExp(/^!(\d+) = metadata !{i32 \d+, (?:metadata !\d+|null), metadata !"[^"]+", metadata !(\d+)[^\[]*.*$/);
+ var form3ad = new RegExp(/^!(\d+) = metadata !{i32 \d+, (?:i32 \d+|null), (?:i32 \d+|null), metadata !"[^"]*", metadata !"[^"]*", metadata !"[^"]*", metadata !(\d+),.*$/);
+ var form3b = new RegExp(/^!(\d+) = metadata !{i32 \d+, metadata !"([^"]+)", metadata !"([^"]+)", (metadata !\d+|null)}.*$/);
var form3c = new RegExp(/^!(\d+) = metadata !{\w+\d* !?(\d+)[^\d].*$/);
var form4 = new RegExp(/^!llvm.dbg.[\w\.]+ = .*$/);
var form5 = new RegExp(/^!(\d+) = metadata !{.*$/);
var form6 = new RegExp(/^ (tail )?call void \@llvm.dbg.\w+\(metadata .*$/);
- var formStruct = /^!(\d+) = metadata !\{i32 \d+, metadata !\d+, metadata !"([^"]+)", metadata !\d+, i32 \d+, i64 \d+, [^,]*, [^,]*, [^,]*, [^,]*, metadata !(\d+), .*} ; \[ DW_TAG_(?:structure|class)_type \]$/;
+ var formStruct = /^!(\d+) = metadata !\{i32 \d+, (metadata !\d+|null), metadata !"([^"]+)", metadata !(\d+), (?:i32 \d+|null), i64 \d+, [^,]*, [^,]*, [^,]*, [^,]*, metadata !(\d+), .*}.*$/;
var formStructMembers = /^!(\d+) = metadata !\{(.*)\}$/;
- var formMember = /^!(\d+) = metadata !\{i32 \d+, metadata !\d+, metadata !"([^"]+)", metadata !\d+, i32 \d+, i64 \d+, i64 \d+, i64 \d+, .+?, metadata !(\d+)} ; \[ DW_TAG_member \]$/;
+ var formMember = /^!(\d+) = metadata !\{i32 \d+, metadata !\d+, metadata !"([^"]+)", metadata !\d+, (?:i32 \d+|null), i64 \d+, i64 \d+, i64 \d+, .+?, metadata !(\d+)}.*$/;
var debugComment = new RegExp(/; +\[debug line = \d+:\d+\]/);
@@ -54,10 +57,13 @@ var Debugging = {
return line.replace(', !dbg !' + calc[1], '');
}
calc = formStruct.exec(line);
- if (calc && !(calc[2] in structToMemberMeta)) {
- structMetaToStruct[calc[1]] = calc[2];
- structToMemberMeta[calc[2]] = calc[3];
- memberMetaToStruct[calc[3]] = calc[1];
+ if (calc) {
+ metadataToParentMetadata[calc[1]] = calc[4];
+ if (!(calc[3] in structToMemberMeta)) {
+ structMetaToStruct[calc[1]] = calc[3];
+ structToMemberMeta[calc[3]] = calc[5];
+ memberMetaToStruct[calc[5]] = calc[1];
+ }
skipLine = true;
}
calc = formStructMembers.exec(line);
@@ -79,7 +85,7 @@ var Debugging = {
metadataToParentMetadata[calc[1]] = calc[3];
return ';'; // return an empty line, to keep line numbers of subsequent lines the same
}
- calc = form3a.exec(line) || form3ab.exec(line) || form3ac.exec(line);
+ calc = form3a.exec(line) || form3ab.exec(line) || form3ac.exec(line) || form3ad.exec(line);
if (calc) {
metadataToParentMetadata[calc[1]] = calc[2];
return ';';
@@ -106,9 +112,9 @@ var Debugging = {
for (var l in llvmLineToMetadata) {
var m = llvmLineToMetadata[l];
this.llvmLineToSourceLine[l] = metadataToSourceLine[m];
- //dprint('starting to recurse metadata for: ' + m);
+ dprint('metadata', 'starting to recurse metadata for: ' + m);
while (!metadataToFilename[m]) {
- //dprint('recursing metadata, at: ' + m);
+ dprint('metadata', 'recursing metadata, at: ' + m);
m = metadataToParentMetadata[m];
assert(m, 'Confused as to parent metadata for llvm #' + l + ', metadata !' + m);
}
@@ -215,6 +221,9 @@ var Functions = {
// The list of function datas which are being processed in the jsifier, currently
currFunctions: [],
+ // All functions that will be implemented in this file
+ implementedFunctions: null,
+
indexedFunctions: [0, 0], // Start at a non-0 (even, see below) value
// Mark a function as needing indexing, and returns the index
@@ -263,6 +272,12 @@ var LibraryManager = {
ret = LibraryManager.library[ret];
}
return last;
+ },
+
+ isStubFunction: function(ident) {
+ var libCall = LibraryManager.library[ident.substr(1)];
+ return typeof libCall === 'function' && libCall.toString().replace(/\s/g, '') === 'function(){}'
+ && !(ident in Functions.implementedFunctions);
}
};
diff --git a/src/parseTools.js b/src/parseTools.js
index a3347139..0e30aa5e 100644
--- a/src/parseTools.js
+++ b/src/parseTools.js
@@ -53,6 +53,7 @@ function preprocess(text, constants) {
function addPointing(type) { return type + '*' }
function removePointing(type, num) {
if (num === 0) return type;
+ assert(type.substr(type.length-(num ? num : 1)).replace(/\*/g, '') === ''); //, 'Error in removePointing with ' + [type, num, type.substr(type.length-(num ? num : 1))]);
return type.substr(0, type.length-(num ? num : 1));
}
@@ -114,6 +115,7 @@ function isStructPointerType(type) {
function isStructType(type) {
if (isPointerType(type)) return false;
if (new RegExp(/^\[\d+\ x\ (.*)\]/g).test(type)) return true; // [15 x ?] blocks. Like structs
+ if (new RegExp(/<?{ [^}]* }>?/g).test(type)) return true; // { i32, i8 } etc. - anonymous struct types
// See comment in isStructPointerType()
return !Runtime.isNumberType(type) && type[0] == '%';
}
@@ -148,7 +150,7 @@ function isFunctionType(type) {
if (pointingLevels(type) !== 1) return false;
var text = removeAllPointing(parts.slice(1).join(' '));
if (!text) return false;
- return isType(parts[0]) && isFunctionDef({ text: text, item: tokenizer.processItem({ lineText: text.substr(1, text.length-2) }, true) });
+ return isType(parts[0]) && isFunctionDef({ text: text, item: tokenize(text.substr(1, text.length-2), true) });
}
function isType(type) { // TODO!
@@ -465,15 +467,13 @@ function eatLLVMIdent(tokens) {
return ret;
}
-function cleanOutTokens(filterOut, tokens, index) {
- while (filterOut.indexOf(tokens[index].text) != -1) {
- tokens.splice(index, 1);
- }
-}
-
-function cleanOutTokensSet(filterOut, tokens, index) {
- while (tokens[index].text in filterOut) {
- tokens.splice(index, 1);
+function cleanOutTokens(filterOut, tokens, indexes) {
+ if (typeof indexes !== 'object') indexes = [indexes];
+ for (var i = indexes.length-1; i >=0; i--) {
+ var index = indexes[i];
+ while (tokens[index].text in filterOut) {
+ tokens.splice(index, 1);
+ }
}
}
@@ -979,14 +979,14 @@ function makeGetSlabs(ptr, type, allowMultiple, unsigned) {
return [];
}
-function finalizeLLVMFunctionCall(item) {
+function finalizeLLVMFunctionCall(item, noIndexizeFunctions) {
switch(item.intertype) {
case 'getelementptr': // TODO finalizeLLVMParameter on the ident and the indexes?
return makePointer(makeGetSlabs(item.ident, item.type)[0], getGetElementPtrIndexes(item), null, item.type);
case 'bitcast':
case 'inttoptr':
case 'ptrtoint':
- return finalizeLLVMParameter(item.params[0]);
+ return finalizeLLVMParameter(item.params[0], noIndexizeFunctions);
case 'icmp': case 'mul': case 'zext': case 'add': case 'sub': case 'div':
var temp = {
op: item.intertype,
@@ -1078,18 +1078,18 @@ function handleOverflow(text, bits) {
}
// From parseLLVMSegment
-function finalizeLLVMParameter(param) {
+function finalizeLLVMParameter(param, noIndexizeFunctions) {
var ret;
if (isNumber(param)) {
return param;
} else if (typeof param === 'string') {
return toNiceIdentCarefully(param);
} else if (param.intertype in PARSABLE_LLVM_FUNCTIONS) {
- ret = finalizeLLVMFunctionCall(param);
+ ret = finalizeLLVMFunctionCall(param, noIndexizeFunctions);
} else if (param.intertype == 'value') {
ret = parseNumerical(param.ident);
} else if (param.intertype == 'structvalue') {
- ret = param.values.map(finalizeLLVMParameter);
+ ret = param.values.map(function(value) { return finalizeLLVMParameter(value, noIndexizeFunctions) });
} else if (param.intertype === 'blockaddress') {
return finalizeBlockAddress(param);
} else if (param.intertype === 'type') {
@@ -1098,7 +1098,8 @@ function finalizeLLVMParameter(param) {
throw 'invalid llvm parameter: ' + param.intertype;
}
assert(param.type || (typeof param === 'string' && param.substr(0, 6) === 'CHECK_'), 'Missing type for param: ' + dump(param));
- return indexizeFunctions(ret, param.type);
+ if (!noIndexizeFunctions) ret = indexizeFunctions(ret, param.type);
+ return ret;
}
function makeSignOp(value, type, op) {
diff --git a/src/preamble.js b/src/preamble.js
index 8ab9bce8..dccf2c31 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -288,6 +288,7 @@ var __ATEXIT__ = [];
var ABORT = false;
var undef = 0;
+var tempValue;
function abort(text) {
print(text + ':\n' + (new Error).stack);
diff --git a/src/runtime.js b/src/runtime.js
index abeb0d2a..7e3c7b84 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -130,7 +130,7 @@ Runtime = {
size = Types.types[field].flatSize;
alignSize = Types.types[field].alignSize;
} else {
- dprint('Unclear type in struct: ' + field + ', in ' + type.name_);
+ dprint('Unclear type in struct: ' + field + ', in ' + type.name_ + ' :: ' + dump(Types.types[type.name_]));
assert(0);
}
alignSize = type.packed ? 1 : Math.min(alignSize, QUANTUM_SIZE);
diff --git a/src/settings.js b/src/settings.js
index ab532e67..1d67e5b4 100644
--- a/src/settings.js
+++ b/src/settings.js
@@ -69,6 +69,7 @@ SAFE_HEAP_LOG = 0; // Log out all SAFE_HEAP operations
LABEL_DEBUG = 0; // Print out labels and functions as we enter them
EXCEPTION_DEBUG = 1; // Print out exceptions in emscriptened code
+LIBRARY_DEBUG = 0; // Print out when we enter a library call (library*.js)
DISABLE_EXCEPTIONS = 0; // Disables generating code to actually catch exceptions. If the code you
// are compiling does not actually rely on catching exceptions (but the
// compiler generates code for it, maybe because of stdlibc++ stuff),
@@ -149,4 +150,5 @@ DEBUG_TAGS_SHOWING = [];
// vars
// relooping
// unparsedFunctions
+ // metadata
diff --git a/tests/cases/aliasbitcast.ll b/tests/cases/aliasbitcast.ll
new file mode 100644
index 00000000..70dc64ef
--- /dev/null
+++ b/tests/cases/aliasbitcast.ll
@@ -0,0 +1,37 @@
+; ModuleID = '/tmp/emscripten/tmp/src.cpp.o'
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
+target triple = "i386-pc-linux-gnu"
+
+@.str = private constant [14 x i8] c"hello, world!\00", align 1 ; [#uses=1]
+
+@_ZN16FormWidgetChoiceD2Ev = alias bitcast (i32 678 to i8*) ; [#uses=1]
+
+; [#uses=2]
+define void @"\01_Z5hellov"() {
+entry:
+ %0 = call i32 bitcast (i32 (i8*)* @puts to i32 (i32*)*)(i8* getelementptr inbounds ([14 x i8]* @.str, i32 0, i32 0)) ; [#uses=0]
+ br label %return
+
+return: ; preds = %entry
+ ret void
+}
+
+; [#uses=1]
+declare i32 @puts(i8*)
+
+; [#uses=0]
+define i32 @main() {
+entry:
+ %retval = alloca i32 ; [#uses=2]
+ %0 = alloca i32 ; [#uses=2]
+ %"alloca point" = bitcast i32 0 to i32 ; [#uses=0]
+ call void @"\01_Z5hellov"()
+ store i32 0, i32* %0, align 4
+ %1 = load i32* %0, align 4 ; [#uses=1]
+ store i32 %1, i32* %retval, align 4
+ br label %return
+
+return: ; preds = %entry
+ %retval1 = load i32* %retval ; [#uses=1]
+ ret i32 %retval1
+}
diff --git a/tests/cases/gepoverflow_q1.txt b/tests/cases/gepoverflow_q1.txt
new file mode 100644
index 00000000..e32e76dd
--- /dev/null
+++ b/tests/cases/gepoverflow_q1.txt
@@ -0,0 +1 @@
+skip
diff --git a/tests/cases/storestruct_q1.txt b/tests/cases/storestruct_q1.txt
new file mode 100644
index 00000000..5e18f72c
--- /dev/null
+++ b/tests/cases/storestruct_q1.txt
@@ -0,0 +1,3 @@
+*5,22*
+*7,96*
+*53,3*
diff --git a/tests/dlmalloc.c b/tests/dlmalloc.c
index 93bacc63..e6bf434d 100644
--- a/tests/dlmalloc.c
+++ b/tests/dlmalloc.c
@@ -5709,16 +5709,20 @@ int main(int ac, char **av)
char* allocations[NUM];
for (int i = 0; i < NUM/2; i++) {
allocations[i] = (char*)malloc((11*i)%1024 + x);
+ //printf("zz alloc: %d\n", (int)allocations[i]);
assert(allocations[i]);
if (i > 10 && i%4 == 1 && allocations[i-10]) {
+ //printf("zz free: %d\n", (int)allocations[i-10]);
free(allocations[i-10]);
allocations[i-10] = NULL;
}
}
for (int i = NUM/2; i < NUM; i++) {
allocations[i] = (char*)malloc(1024*(i+1));
+ //printf("zz alloc: %d\n", (int)allocations[i]);
assert(allocations[i]);
if (i > 10 && i%4 != 1 && allocations[i-10]) {
+ //printf("zz free: %d\n", (int)allocations[i-10]);
free(allocations[i-10]);
allocations[i-10] = NULL;
}
@@ -5726,14 +5730,42 @@ int main(int ac, char **av)
char* first = allocations[0];
for (int i = 0; i < NUM; i++) {
if (allocations[i]) {
+ //printf("zz free: %d\n", (int)allocations[i]);
free(allocations[i]);
}
}
char *last = (char*)malloc(512); // should be identical, as we free'd it all
+ //printf("zz last: %d\n", (int)last);
char *newer = (char*)malloc(512); // should be different
+ //printf("zz newer: %d\n", (int)newer);
c1 += first == last;
c2 += first == newer;
}
printf("*%d,%d*\n", c1, c2);
}
+/* Some debugging tools: Make JS and native code work exactly the same */
+/*
+time_t time ( time_t * timer )
+{
+ if (timer) *timer = 1;
+ return 1;
+}
+
+long sysconf(int name)
+{
+ printf("sysconf: %d (30 is page size)\n", name);
+ return 4096;
+}
+
+void *sbrk(intptr_t increment)
+{
+ static char spaace[1024*1024*1];
+ static intptr_t where = 0;
+ printf("sbrk! spaace=%d (%d,%d)\n", (int)&spaace[0], where, increment); // copy the value printed at runtime here in native code into your js
+ void *ret = &spaace[where];
+ where += increment;
+ return ret;
+}
+*/
+
diff --git a/tests/runner.py b/tests/runner.py
index 25b3eda5..04ebaa19 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -49,8 +49,8 @@ class RunnerCore(unittest.TestCase):
shutil.copy(os.path.join(self.get_dir(), name),
os.path.join(TEMP_DIR, self.id().replace('__main__.', '').replace('.test_', '.')+'.'+suff))
- def skip(self):
- print >> sys.stderr, '<skip> ',
+ def skip(self, why):
+ print >> sys.stderr, '<skipping: %s> ' % why,
def get_dir(self):
dirname = TEMP_DIR + '/tmp' # tempfile.mkdtemp(dir=TEMP_DIR)
@@ -60,13 +60,13 @@ class RunnerCore(unittest.TestCase):
# Similar to LLVM::createStandardModulePasses()
def pick_llvm_opts(self, optimization_level, optimize_size, allow_nonportable=False):
- global LLVM_OPT_OPTS, USE_TYPED_ARRAYS
+ global LLVM_OPT_OPTS, USE_TYPED_ARRAYS, QUANTUM_SIZE
#if USE_TYPED_ARRAYS == 2: # unsafe optimizations. TODO: fix all issues blocking this from being used
# LLVM_OPT_OPTS = ['-O3']
# return
- LLVM_OPT_OPTS = pick_llvm_opts(optimization_level, optimize_size, allow_nonportable)
+ LLVM_OPT_OPTS = pick_llvm_opts(optimization_level, optimize_size, allow_nonportable, quantum_size=QUANTUM_SIZE)
# Emscripten optimizations that we run on the .ll file
def do_ll_opts(self, filename):
@@ -79,6 +79,7 @@ class RunnerCore(unittest.TestCase):
if LLVM_OPTS:
shutil.move(filename + '.o', filename + '.o.pre')
output = Popen([LLVM_OPT, filename + '.o.pre'] + LLVM_OPT_OPTS + ['-o=' + filename + '.o'], stdout=PIPE, stderr=STDOUT).communicate()[0]
+ assert os.path.exists(filename + '.o'), 'Failed to run llvm optimizations: ' + output
def do_llvm_dis(self, filename):
# LLVM binary ==> LLVM assembly
@@ -86,8 +87,8 @@ class RunnerCore(unittest.TestCase):
os.remove(filename + '.o.ll')
except:
pass
- Popen([LLVM_DIS, filename + '.o'] + LLVM_DIS_OPTS + ['-o=' + filename + '.o.ll'], stdout=PIPE, stderr=STDOUT).communicate()[0]
- assert os.path.exists(filename + '.o.ll'), 'Could not create .ll file'
+ output = Popen([LLVM_DIS, filename + '.o'] + LLVM_DIS_OPTS + ['-o=' + filename + '.o.ll'], stdout=PIPE, stderr=STDOUT).communicate()[0]
+ assert os.path.exists(filename + '.o.ll'), 'Could not create .ll file: ' + output
def do_llvm_as(self, source, target):
# LLVM assembly ==> LLVM binary
@@ -95,8 +96,8 @@ class RunnerCore(unittest.TestCase):
os.remove(target)
except:
pass
- Popen([LLVM_AS, source, '-o=' + target], stdout=PIPE, stderr=STDOUT).communicate()[0]
- assert os.path.exists(target), 'Could not create bc file'
+ output = Popen([LLVM_AS, source, '-o=' + target], stdout=PIPE, stderr=STDOUT).communicate()[0]
+ assert os.path.exists(target), 'Could not create bc file: ' + output
def do_link(self, files, target):
output = Popen([LLVM_LINK] + files + ['-o', target], stdout=PIPE, stderr=STDOUT).communicate()[0]
@@ -145,6 +146,7 @@ class RunnerCore(unittest.TestCase):
# C++ => LLVM binary
os.chdir(dirname)
cwd = os.getcwd()
+
for f in [filename] + additional_files:
try:
# Make sure we notice if compilation steps failed
@@ -185,6 +187,10 @@ class RunnerCore(unittest.TestCase):
except:
pass
settings = ['-s %s=%s' % (k, json.dumps(v)) for k, v in exported_settings.items()]
+ try:
+ os.getcwd()
+ except OSError:
+ os.chdir(self.get_dir()) # ensure the current working directory is valid
compiler_output = timeout_run(Popen([EMSCRIPTEN, filename + ('.o.ll' if append_ext else ''), '-o', filename + '.o.js'] + settings + extra_args, stdout=PIPE, stderr=STDOUT), TIMEOUT, 'Compiling')
#print compiler_output
@@ -283,7 +289,6 @@ if 'benchmark' not in str(sys.argv):
# No building - just process an existing .ll file (or .bc, which we turn into .ll)
def do_ll_test(self, ll_file, expected_output=None, args=[], js_engines=None, output_nicerizer=None, post_build=None, force_recompile=False, build_ll_hook=None, extra_emscripten_args=[]):
- if COMPILER != LLVM_GCC: return self.skip() # We use existing .ll, so which compiler is unimportant
filename = os.path.join(self.get_dir(), 'src.cpp')
@@ -393,7 +398,7 @@ if 'benchmark' not in str(sys.argv):
self.do_test(src, output, force_c=True)
def test_bigint(self):
- if USE_TYPED_ARRAYS != 0: return self.skip() # Typed arrays truncate i64.
+ if USE_TYPED_ARRAYS != 0: return self.skip('Typed arrays truncate i64')
src = '''
#include <stdio.h>
int main()
@@ -1533,7 +1538,7 @@ if 'benchmark' not in str(sys.argv):
self.do_test(src, '*4,3,4*\n*6,4,6*')
def test_varargs(self):
- if QUANTUM_SIZE == 1: return self.skip() # FIXME: Add support for this
+ if QUANTUM_SIZE == 1: return self.skip('FIXME: Add support for this')
src = '''
#include <stdio.h>
@@ -1655,7 +1660,7 @@ if 'benchmark' not in str(sys.argv):
self.do_test(src, '*1,2,3,5,5,6*\n*stdin==0:0*\n*%*\n*5*\n*66.0*\n*10*\n*0*\n*-10*\n*18*\n*10*\n*0*\n*4294967286*\n*cleaned*')
def test_time(self):
- if USE_TYPED_ARRAYS == 2: return self.skip() # Typed arrays = 2 truncate i64s.
+ if USE_TYPED_ARRAYS == 2: return self.skip('Typed arrays = 2 truncate i64s')
src = open(path_from_root('tests', 'time', 'src.c'), 'r').read()
expected = open(path_from_root('tests', 'time', 'output.txt'), 'r').read()
self.do_test(src, expected)
@@ -1962,7 +1967,7 @@ if 'benchmark' not in str(sys.argv):
def test_dlfcn_data_and_fptr(self):
global LLVM_OPTS
- if LLVM_OPTS: return self.skip() # LLVM opts will optimize out parent_func
+ if LLVM_OPTS: return self.skip('LLVM opts will optimize out parent_func')
global BUILD_AS_SHARED_LIB, EXPORTED_FUNCTIONS, EXPORTED_GLOBALS
lib_src = '''
@@ -2109,7 +2114,7 @@ if 'benchmark' not in str(sys.argv):
INCLUDE_FULL_LIBRARY = 0
def test_dlfcn_varargs(self):
- if QUANTUM_SIZE == 1: return self.skip() # FIXME: Add support for this
+ if QUANTUM_SIZE == 1: return self.skip('FIXME: Add support for this')
global BUILD_AS_SHARED_LIB, EXPORTED_FUNCTIONS
lib_src = r'''
void print_ints(int n, ...);
@@ -2206,7 +2211,7 @@ if 'benchmark' not in str(sys.argv):
self.do_test(src, re.sub(r'(^|\n)\s+', r'\1', expected))
def test_strtod(self):
- if USE_TYPED_ARRAYS == 2: return self.skip() # Typed arrays = 2 truncate doubles.
+ if USE_TYPED_ARRAYS == 2: return self.skip('Typed arrays = 2 truncate doubles')
src = r'''
#include <stdio.h>
#include <stdlib.h>
@@ -2261,13 +2266,13 @@ if 'benchmark' not in str(sys.argv):
self.do_test(src, re.sub(r'\n\s+', '\n', expected))
def test_parseInt(self):
- if USE_TYPED_ARRAYS != 0: return self.skip() # Typed arrays truncate i64.
+ if USE_TYPED_ARRAYS != 0: return self.skip('Typed arrays truncate i64')
src = open(path_from_root('tests', 'parseInt', 'src.c'), 'r').read()
expected = open(path_from_root('tests', 'parseInt', 'output.txt'), 'r').read()
self.do_test(src, expected)
def test_printf(self):
- if USE_TYPED_ARRAYS != 0: return self.skip() # Typed arrays truncate i64.
+ if USE_TYPED_ARRAYS != 0: return self.skip('Typed arrays truncate i64')
src = open(path_from_root('tests', 'printf', 'test.c'), 'r').read()
expected = open(path_from_root('tests', 'printf', 'output.txt'), 'r').read()
self.do_test(src, expected)
@@ -2895,11 +2900,11 @@ if 'benchmark' not in str(sys.argv):
def test_raytrace(self):
global USE_TYPED_ARRAYS
- if USE_TYPED_ARRAYS == 2: return self.skip() # relies on double values
+ if USE_TYPED_ARRAYS == 2: return self.skip('Relies on double values')
src = open(path_from_root('tests', 'raytrace.cpp'), 'r').read()
output = open(path_from_root('tests', 'raytrace.ppm'), 'r').read()
- self.do_test(src, output, ['3', '16'])
+ self.do_test(src, output, ['3', '16'])#, build_ll_hook=self.do_autodebug)
def test_fasta(self):
results = [ (1,'''GG*ctt**tgagc*'''), (20,'''GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTT*cttBtatcatatgctaKggNcataaaSatgtaaaDcDRtBggDtctttataattcBgtcg**tacgtgtagcctagtgtttgtgttgcgttatagtctatttgtggacacagtatggtcaaa**tgacgtcttttgatctgacggcgttaacaaagatactctg*'''),
@@ -2919,8 +2924,7 @@ if 'benchmark' not in str(sys.argv):
def zzztest_gl(self):
# Switch to gcc from g++ - we don't compile properly otherwise (why?)
global COMPILER
- if COMPILER != LLVM_GCC: return self.skip()
- COMPILER = LLVM_GCC.replace('g++', 'gcc')
+ COMPILER = COMPILER.replace('++', '')
def post(filename):
src = open(filename, 'r').read().replace(
@@ -2977,6 +2981,9 @@ if 'benchmark' not in str(sys.argv):
# print opt, "FAIL"
def test_lua(self):
+ global QUANTUM_SIZE
+ if QUANTUM_SIZE == 1: return self.skip('TODO: make this work')
+
# Overflows in luaS_newlstr hash loop
global SAFE_HEAP; SAFE_HEAP = 0 # Has various warnings, with copied HEAP_HISTORY values (fixed if we copy 'null' as the type)
global CORRECT_OVERFLOWS; CORRECT_OVERFLOWS = 1
@@ -3043,9 +3050,9 @@ if 'benchmark' not in str(sys.argv):
return self.get_library('freetype', os.path.join('objs', '.libs', 'libfreetype.a.bc'))
def test_freetype(self):
- if QUANTUM_SIZE == 1: return self.skip() # TODO: Figure out and try to fix
+ if QUANTUM_SIZE == 1: return self.skip('TODO: Figure out and try to fix')
- if LLVM_OPTS or COMPILER == CLANG: global RELOOP; RELOOP = 0 # Too slow; we do care about typed arrays and OPTIMIZE though
+ if LLVM_OPTS: global RELOOP; RELOOP = 0 # Too slow; we do care about typed arrays and OPTIMIZE though
global CORRECT_SIGNS
if CORRECT_SIGNS == 0: CORRECT_SIGNS = 1 # Not sure why, but needed
@@ -3097,10 +3104,8 @@ if 'benchmark' not in str(sys.argv):
global SAFE_HEAP, SAFE_HEAP_LINES, USE_TYPED_ARRAYS, LLVM_OPTS
if LLVM_OPTS: SAFE_HEAP = 0 # Optimizations make it so we do not have debug info on the line we need to ignore
- if COMPILER == LLVM_GCC:
- global INIT_STACK; INIT_STACK = 1 # TODO: Investigate why this is necessary
- if USE_TYPED_ARRAYS == 2: return self.skip() # We have slightly different rounding here for some reason. TODO: activate this
+ if USE_TYPED_ARRAYS == 2: return self.skip('We have slightly different rounding here for some reason. TODO: activate this')
if SAFE_HEAP:
# Ignore bitfield warnings
@@ -3118,11 +3123,13 @@ if 'benchmark' not in str(sys.argv):
js_engines=[SPIDERMONKEY_ENGINE]) # V8 issue 1407
def test_poppler(self):
- if COMPILER != LLVM_GCC: return self.skip() # llvm-link failure when using clang, LLVM bug 9498
- if RELOOP or LLVM_OPTS: return self.skip() # TODO
- if QUANTUM_SIZE == 1: return self.skip() # TODO: Figure out and try to fix
+ global RELOOP, LLVM_OPTS, USE_TYPED_ARRAYS, QUANTUM_SIZE
+
+ # llvm-link failure when using clang, LLVM bug 9498, still relevant?
+ if RELOOP or LLVM_OPTS: return self.skip('TODO')
+ if USE_TYPED_ARRAYS == 2 or QUANTUM_SIZE == 1: return self.skip('TODO: Figure out and try to fix')
- global USE_TYPED_ARRAYS; USE_TYPED_ARRAYS = 0 # XXX bug - we fail with this FIXME
+ USE_TYPED_ARRAYS = 0 # XXX bug - we fail with this FIXME
global SAFE_HEAP; SAFE_HEAP = 0 # Has variable object
@@ -3202,10 +3209,7 @@ if 'benchmark' not in str(sys.argv):
else:
CORRECT_SIGNS = 2
global CORRECT_SIGNS_LINES
- if COMPILER == CLANG:
- CORRECT_SIGNS_LINES = ["mqc.c:566"]
- else:
- CORRECT_SIGNS_LINES = ["mqc.c:566", "mqc.c:317"]
+ CORRECT_SIGNS_LINES = ["mqc.c:566", "mqc.c:317"]
original_j2k = path_from_root('tests', 'openjpeg', 'syntensity_lobby_s.j2k')
@@ -3281,6 +3285,9 @@ if 'benchmark' not in str(sys.argv):
output_nicerizer=image_compare)# build_ll_hook=self.do_autodebug)
def test_python(self):
+ global QUANTUM_SIZE, USE_TYPED_ARRAYS
+ if QUANTUM_SIZE == 1 or USE_TYPED_ARRAYS == 2: return self.skip('TODO: make this work')
+
# Overflows in string_hash
global CORRECT_OVERFLOWS; CORRECT_OVERFLOWS = 1
global CHECK_OVERFLOWS; CHECK_OVERFLOWS = 0
@@ -3298,17 +3305,24 @@ if 'benchmark' not in str(sys.argv):
# They are only valid enough for us to read for test purposes, not for llvm-as
# to process.
def test_cases(self):
+ global QUANTUM_SIZE
global CHECK_OVERFLOWS; CHECK_OVERFLOWS = 0
- if LLVM_OPTS: return self.skip() # Our code is not exactly 'normal' llvm assembly
+ if LLVM_OPTS: return self.skip("Our code is not exactly 'normal' llvm assembly")
+
for name in glob.glob(path_from_root('tests', 'cases', '*.ll')):
shortname = name.replace('.ll', '')
print "Testing case '%s'..." % shortname
output_file = path_from_root('tests', 'cases', shortname + '.txt')
+ if QUANTUM_SIZE == 1:
+ q1_output_file = path_from_root('tests', 'cases', shortname + '_q1.txt')
+ if os.path.exists(q1_output_file):
+ output_file = q1_output_file
if os.path.exists(output_file):
output = open(output_file, 'r').read()
else:
output = 'hello, world!'
- self.do_ll_test(path_from_root('tests', 'cases', name), output)
+ if output.rstrip() != 'skip':
+ self.do_ll_test(path_from_root('tests', 'cases', name), output)
# Autodebug the code
def do_autodebug(self, filename):
@@ -3317,7 +3331,7 @@ if 'benchmark' not in str(sys.argv):
self.prep_ll_test(filename, filename+'.o.ll.ll', force_recompile=True) # rebuild .bc
def test_autodebug(self):
- if LLVM_OPTS: return self.skip() # They mess us up
+ if LLVM_OPTS: return self.skip('LLVM opts mess us up')
# Run a test that should work, generating some code
self.test_structs()
@@ -3327,7 +3341,6 @@ if 'benchmark' not in str(sys.argv):
# Compare to each other, and to expected output
self.do_ll_test(path_from_root('tests', filename+'.o.ll.ll'))
- self.do_ll_test(path_from_root('tests', filename+'.o.ll.ll'), 'AD:38,10\nAD:47,7008\nAD:57,7018\n')
# Test using build_ll_hook
src = '''
@@ -3605,7 +3618,7 @@ Child2:9
def test_typeinfo(self):
global RUNTIME_TYPE_INFO; RUNTIME_TYPE_INFO = 1
global QUANTUM_SIZE
- if QUANTUM_SIZE != 4: return self.skip()
+ if QUANTUM_SIZE != 4: return self.skip('We assume normal sizes in the output here')
src = '''
#include<stdio.h>
@@ -3677,8 +3690,8 @@ Child2:9
def test_safe_heap(self):
global SAFE_HEAP, SAFE_HEAP_LINES
- if not SAFE_HEAP: return self.skip()
- if LLVM_OPTS: return self.skip() # LLVM can optimize away the intermediate |x|...
+ if not SAFE_HEAP: return self.skip('We need SAFE_HEAP to test SAFE_HEAP')
+ if LLVM_OPTS: return self.skip('LLVM can optimize away the intermediate |x|')
src = '''
#include<stdio.h>
int main() {
@@ -4011,12 +4024,9 @@ TT = %s
for name, compiler, quantum, embetter, typed_arrays in [
('clang', CLANG, 1, 0, 0),
('clang', CLANG, 4, 0, 0),
- ('llvm_gcc', LLVM_GCC, 4, 0, 0),
('clang', CLANG, 1, 1, 1),
('clang', CLANG, 4, 1, 1),
- ('llvm_gcc', LLVM_GCC, 4, 1, 1),
('clang', CLANG, 4, 1, 2),
- #('llvm_gcc', LLVM_GCC, 4, 1, 2),
]:
fullname = '%s_%d_%d%s%s' % (
name, llvm_opts, embetter, '' if quantum == 4 else '_q' + str(quantum), '' if typed_arrays in [0, 1] else '_t' + str(typed_arrays)
@@ -4096,7 +4106,7 @@ else:
def do_benchmark(self, src, args=[], expected_output='FAIL', main_file=None):
global USE_TYPED_ARRAYS
- self.pick_llvm_opts(3, True, USE_TYPED_ARRAYS == 2)
+ self.pick_llvm_opts(3, True, allow_nonportable=USE_TYPED_ARRAYS == 2)
dirname = self.get_dir()
filename = os.path.join(dirname, 'src.cpp')
@@ -4236,7 +4246,7 @@ else:
if __name__ == '__main__':
sys.argv = [sys.argv[0]] + ['-v'] + sys.argv[1:] # Verbose output by default
- for cmd in [CLANG, LLVM_GCC, LLVM_DIS, SPIDERMONKEY_ENGINE[0], V8_ENGINE[0]]:
+ for cmd in [CLANG, LLVM_DIS, SPIDERMONKEY_ENGINE[0], V8_ENGINE[0]]:
if not os.path.exists(cmd):
print 'WARNING: Cannot find', cmd
unittest.main()
diff --git a/tools/autodebugger.py b/tools/autodebugger.py
index 9b51ebc9..3b10233e 100644
--- a/tools/autodebugger.py
+++ b/tools/autodebugger.py
@@ -7,6 +7,8 @@ compare that to the output when compiled using emscripten.
import os, sys, re
+ALLOW_POINTERS = False
+
POSTAMBLE = '''
@.emscripten.autodebug.str = private constant [10 x i8] c"AD:%d,%d\\0A\\00", align 1 ; [#uses=1]
@@ -138,13 +140,16 @@ if not LLVM_STYLE_OLD:
lines_added = 0
lines = data.split('\n')
for i in range(len(lines)):
- #if i == 5:
- # lines[i] += '\n
-
- m = re.match(' store (?P<type>i64|i32|i16|i8|float|double) %(?P<var>[\w.]+), .*', lines[i])
- if m and m.group('type') in ['i8', 'i16', 'i32', 'i64', 'float', 'double']:
- lines[i] += '\n call void @emscripten_autodebug_%s(i32 %d, %s %%%s)' % (m.group('type'), i+1+lines_added, m.group('type'), m.group('var'))
- lines_added += 1
+ m = re.match(' store (?P<type>i64|i32|i16|i8|float|double|%?[\w\.\*]+) %(?P<var>[\w.]+), .*', lines[i])
+ if m:
+ index = i+1+lines_added
+ if m.group('type') in ['i8', 'i16', 'i32', 'i64', 'float', 'double']:
+ lines[i] += '\n call void @emscripten_autodebug_%s(i32 %d, %s %%%s)' % (m.group('type'), index, m.group('type'), m.group('var'))
+ lines_added += 1
+ elif ALLOW_POINTERS and m.group('type').endswith('*') and m.group('type').count('*') == 1:
+ lines[i] += '\n %%ead.%d = ptrtoint %s %%%s to i32' % (index, m.group('type'), m.group('var'))
+ lines[i] += '\n call void @emscripten_autodebug_i32(i32 %d, i32 %%ead.%d)' % (index, index)
+ lines_added += 2
f = open(ofilename, 'w')
f.write('\n'.join(lines) + '\n' + POSTAMBLE + '\n')
diff --git a/tools/shared.py b/tools/shared.py
index 83c921da..cb0d0691 100644
--- a/tools/shared.py
+++ b/tools/shared.py
@@ -72,13 +72,13 @@ def limit_size(string, MAX=80*20):
if len(string) < MAX: return string
return string[0:MAX/2] + '\n[..]\n' + string[-MAX/2:]
-def pick_llvm_opts(optimization_level, optimize_size, allow_nonportable=False, use_aa=False):
+def pick_llvm_opts(optimization_level, optimize_size, allow_nonportable=False, quantum_size=4, use_aa=False):
opts = []
if optimization_level > 0:
if allow_nonportable:
opts.append('-O%d' % optimization_level)
else:
- # createStandardAliasAnalysisPasses
+ # PassManagerBuilder::populateModulePassManager
if allow_nonportable and use_aa: # ammo.js results indicate this can be nonportable
opts.append('-tbaa')
opts.append('-basicaa') # makes fannkuch slow but primes fast
@@ -114,11 +114,12 @@ def pick_llvm_opts(optimization_level, optimize_size, allow_nonportable=False, u
opts.append('-licm')
opts.append('-loop-unswitch') # XXX should depend on optimize_size
if allow_nonportable: opts.append('-instcombine')
- opts.append('-indvars')
+ if quantum_size == 4: opts.append('-indvars') # XXX this infinite-loops raytrace on q1 (loop in |new node_t[count]| has 68 hardcoded &not fixed)
if allow_nonportable: opts.append('-loop-idiom') # ?
opts.append('-loop-deletion')
opts.append('-loop-unroll')
- if allow_nonportable: opts.append('-instcombine')
+
+ ##### not in llvm-3.0. but have | #addExtensionsToPM(EP_LoopOptimizerEnd, MPM);| if allow_nonportable: opts.append('-instcombine')
# XXX Danger: Messes up Lua output for unknown reasons
# Note: this opt is of minor importance for raytrace...
@@ -131,11 +132,13 @@ def pick_llvm_opts(optimization_level, optimize_size, allow_nonportable=False, u
opts.append('-jump-threading')
opts.append('-correlated-propagation')
opts.append('-dse')
+ #addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
+
opts.append('-adce')
opts.append('-simplifycfg')
+ if allow_nonportable: opts.append('-instcombine')
opts.append('-strip-dead-prototypes')
- opts.append('-deadtypeelim')
if optimization_level > 2: opts.append('-globaldce')