Skip to content

Commit

Permalink
misc compiler tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
azakai committed Mar 13, 2011
1 parent f534ade commit 6c22a66
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 31 deletions.
30 changes: 16 additions & 14 deletions src/jsifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
if (type !== 'null') type = '"' + safeQuote(type) + '"';
return 'SAFE_HEAP_STORE(' + offset + ', ' + value + ', ' + type + ');';
} else {
return makeGetSlabs(ptr, type, true).map(function(slab) { return slab + '[' + offset + '] = ' + value }).join('; ') + ';';
return makeGetSlabs(ptr, type, true).map(function(slab) { return slab + '[' + offset + ']=' + value }).join('; ') + ';';
}
}

Expand Down Expand Up @@ -312,7 +312,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {

return ret.concat({
intertype: 'GlobalVariable',
JS: item.ident + ' = ' + constant + ';',
JS: item.ident + '=' + constant + ';',
});
}
}
Expand Down Expand Up @@ -369,7 +369,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
}

var deps = Library[ident + '__deps'];
return 'var _' + ident + ' = ' + snippet + (deps ? '\n' + deps.map(addFromLibrary).join('\n') : '');
return 'var _' + ident + '=' + snippet + (deps ? '\n' + deps.map(addFromLibrary).join('\n') : '');
}
item.JS = addFromLibrary(shortident);
} else {
Expand Down Expand Up @@ -594,7 +594,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
default: throw 'zz unknown impl: ' + impl;
}
if (value)
item.JS += ' = ' + value;
item.JS += '=' + value;
item.JS += ';';

this.forwardItem(item, 'FunctionReconstructor');
Expand All @@ -621,13 +621,13 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
}
switch (impl) {
case VAR_NATIVIZED:
return item.ident + ' = ' + value + ';'; // We have the actual value here
return item.ident + '=' + value + ';'; // We have the actual value here
break;
case VAR_EMULATED:
if (item.pointer.intertype == 'value') {
return makeSetValue(item.ident, 0, value, item.valueType) + ';';
return makeSetValue(item.ident, 0, value, item.valueType);
} else {
return makeSetValue(0, indexizeFunctions(finalizeLLVMParameter(item.pointer)), value, item.valueType) + ';';
return makeSetValue(0, indexizeFunctions(finalizeLLVMParameter(item.pointer)), value, item.valueType);
}
break;
default:
Expand Down Expand Up @@ -1115,7 +1115,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
// Optimized intertypes

makeFuncLineActor('fastgetelementptrload', function(item) {
return 'var ' + item.ident + ' = ' + makeGetValue(parseNumerical(item.value.ident), getGetElementPtrIndexes(item.value), item.value.valueType, true) + ';';
return 'var ' + item.ident + '=' + makeGetValue(parseNumerical(item.value.ident), getGetElementPtrIndexes(item.value), item.value.valueType, true) + ';';
});
makeFuncLineActor('fastgetelementptrstore', function(item) {
return makeSetValue(item.value.ident, getGetElementPtrIndexes(item.value), parseNumerical(item.ident), item.type, true) + ';';
Expand Down Expand Up @@ -1145,16 +1145,18 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {

if (functionsOnly) return ret;

var body = preprocess(read('preamble.js').replace('{{RUNTIME}}', getRuntime()) + ret + read('postamble.js'), CONSTANTS);
var pre = preprocess(read('preamble.js').replace('{{RUNTIME}}', getRuntime()), CONSTANTS);
var post = preprocess(read('postamble.js'), CONSTANTS);
ret = pre + ret + post;
var globalVars = itemsDict.GlobalVariable.map(function(item) { return item.JS }).join('\n');
var globalVarsPostSets = itemsDict.GlobalVariablePostSet.map(function(item) { return item.JS }).join('\n');
body = indentify(body, 2);
// body may be a very large string at this point - we may not be able to allocate two of it. So must be careful in these last steps
ret = indentify(ret, 2);
// ret may be a very large string at this point - we may not be able to allocate two of it. So must be careful in these last steps
var shellParts = read('shell.js').split('{{BODY}}');
body = shellParts[0] + body + shellParts[1];
ret = shellParts[0] + ret + shellParts[1];
globalVars = indentify(globalVars+'\n\n\n'+globalVarsPostSets, 4);
body = body.replace('{{GLOBAL_VARS}}', globalVars);
return processMacros(body);
ret = ret.replace('{{GLOBAL_VARS}}', globalVars);
return processMacros(ret);
}

// Data
Expand Down
6 changes: 3 additions & 3 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ function __initializeRuntime__() {
}
}

var base = intArrayFromString('(null)').concat(0); // So printing %s of NULL gives '(null)'
// Also this ensures we leave 0 as an invalid address, 'NULL'
var base = intArrayFromString('(null)'); // So printing %s of NULL gives '(null)'
// Also this ensures we leave 0 as an invalid address, 'NULL'
for (var i = 0; i < base.length; i++) {
{{{ makeSetValue(0, 'i', 'base[i]', 'i8') }}}
}
Expand Down Expand Up @@ -360,7 +360,7 @@ function jrint(label, obj) { // XXX manual debugging
print(label + JSON.stringify(obj));
}

// This processes a 'normal' string into a C-line array of numbers.
// This processes a JS string into a C-line array of numbers, 0-terminated.
// For LLVM-originating strings, see parser.js:parseLLVMString function
function intArrayFromString(stringy) {
var ret = [];
Expand Down
50 changes: 41 additions & 9 deletions tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def do_llvm_dis(self, filename):

def do_link(self, files, target):
output = Popen([LLVM_LINK] + files + ['-o', target], stdout=PIPE, stderr=STDOUT).communicate()[0]
assert 'Could not open input file' not in output, 'Linking error: ' + output
assert output is None or 'Could not open input file' not in output, 'Linking error: ' + output

# Build JavaScript code from source code
def build(self, src, dirname, filename, output_processor=None, main_file=None, additional_files=[], libraries=[], includes=[], build_ll_hook=None):
Expand Down Expand Up @@ -1529,6 +1529,20 @@ def test_libcxx(self):
'june -> 30\nPrevious (in alphabetical order) is july\nNext (in alphabetical order) is march',
main_file='main.cpp', additional_files=['hash.cpp'])

# This will fail without using libcxx, as libstdc++ (gnu c++ lib) will use but not link in
# __ZSt29_Rb_tree_insert_and_rebalancebPSt18_Rb_tree_node_baseS0_RS_
# So a way to avoid that problem is to include libcxx, as done here
self.do_test('''
#include <set>
#include <stdio.h>
int main() {
std::set<int> *fetchOriginatorNums = new std::set<int>();
fetchOriginatorNums->insert(171);
printf("hello world\\n");
return 1;
}
''', 'hello world', includes=[path_from_root('tests', 'libcxx', 'include')]);

def test_cubescript(self):
# XXX Warning: Running this in SpiderMonkey can lead to an extreme amount of memory being
# used, see Mozilla bug 593659.
Expand Down Expand Up @@ -1586,14 +1600,14 @@ def get_library(self, name, generated_libs, configure=['./configure'], configure

temp_dir = self.get_building_dir()
project_dir = os.path.join(temp_dir, name)
shutil.copytree(path_from_root('tests', name), project_dir)
shutil.copytree(path_from_root('tests', name), project_dir) # Useful in debugging sometimes to comment this out
os.chdir(project_dir)
env = os.environ.copy()
env['RANLIB'] = env['AR'] = env['CXX'] = env['CC'] = env['LIBTOOL'] = EMMAKEN
env['EMMAKEN_COMPILER'] = COMPILER
env['EMSCRIPTEN_TOOLS'] = path_from_root('tools')
env['CFLAGS'] = env['EMMAKEN_CFLAGS'] = ' '.join(COMPILER_OPTS + COMPILER_TEST_OPTS) # Normal CFLAGS is ignored by some configure's.
if configure:
if configure: # Useful in debugging sometimes to comment this out (and 2 lines below)
Popen(configure + configure_args, stdout=PIPE, stderr=STDOUT, env=env).communicate()[0]
Popen(make + make_args, stdout=PIPE, stderr=STDOUT, env=env).communicate()[0]
bc_file = os.path.join(project_dir, 'bc.bc')
Expand Down Expand Up @@ -1646,6 +1660,10 @@ def test_zlib(self):

def zzztest_poppler(self):
global SAFE_HEAP; SAFE_HEAP = 0 # Has variable object
global CORRECT_SIGNS; CORRECT_SIGNS = 1 # isdigit does -ord('0') and then <= 9, assuming unsigned
global CORRECT_OVERFLOWS; CORRECT_OVERFLOWS = 1

global COMPILER_TEST_OPTS; COMPILER_TEST_OPTS = ['-I' + path_from_root('tests', 'libcxx', 'include')] # Avoid libstdc++ linking issue, see libcxx test

def post(filename):
src = open(filename, 'r').read().replace(
Expand All @@ -1656,14 +1674,28 @@ def post(filename):
)
open(filename, 'w').write(src)

fontconfig = self.get_library('fontconfig', [os.path.join('src', '.libs', 'libfontconfig.a')])

poppler = self.get_library('poppler',
[os.path.join('poppler', '.libs', 'libpoppler.so.13.0.0'),
os.path.join('utils', 'pdftoppm.o')])
#freetype = ... link it
self.do_ll_test(poppler,
os.path.join('goo', '.libs', 'libgoo.a'),
os.path.join('fofi', '.libs', 'libfofi.a'),
os.path.join('splash', '.libs', 'libsplash.a'),
#os.path.join('poppler', 'SplashOutputDev.o'),
os.path.join('utils', 'pdftoppm.o'),
os.path.join('utils', 'parseargs.o')],
configure_args=['--disable-libjpeg', '--disable-libpng'])

# Combine libraries
# TODO: FreeType XXX DO THIS

combined = os.path.join(self.get_building_dir(), 'combined.bc')
self.do_link([fontconfig, poppler], combined)

self.do_ll_test(combined,
'halp',#open(path_from_root('tests', 'poppler', 'ref.txt'), 'r').read(),
args='-png -scale-to 512 paper.pdf filename'.split(' '),
post_build=post)
args='-scale-to 512 paper.pdf filename'.split(' '),
post_build=post, build_ll_hook=self.do_autodebug)

def test_openjpeg(self):
global SAFE_HEAP; SAFE_HEAP = 0 # Very slow
Expand Down Expand Up @@ -2055,7 +2087,7 @@ def setUp(self):
if LLVM_OPTS:
self.pick_llvm_opts(3, True)
COMPILER_TEST_OPTS = []
shutil.rmtree(self.get_dir())
shutil.rmtree(self.get_dir()) # Useful in debugging sometimes to comment this out
self.get_dir() # make sure it exists
TT = %s
''' % (fullname, compiler['path'], compiler['quantum_size'], llvm_opts, embetter, fullname))
Expand Down
14 changes: 9 additions & 5 deletions tools/exec_llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
http://llvm.org/bugs/show_bug.cgi?id=6981
So instead we compile the bitcode into native code.
So we must get around that.
To use this, change the Makefile so that instead of
running
Expand All @@ -32,8 +32,12 @@ def path_from_root(*pathelems):
return os.path.join(os.path.sep, *(abspath.split(os.sep)[:-1] + list(pathelems)))
exec(open(path_from_root('tools', 'shared.py'), 'r').read())

print 'EXEC_LLVM: ', sys.argv
Popen([LLVM_COMPILER, '-march=c', sys.argv[1]]).communicate()[0]
Popen(['gcc', sys.argv[1]+'.cbe.c']).communicate()[0]
Popen(['./a.out'] + sys.argv[2:]).communicate()[0]
print '// EXEC_LLVM: ', sys.argv

Popen([LLVM_OPT, sys.argv[1], '-strip-debug', '-o=' + sys.argv[1]+'.clean.bc']).communicate()[0]
Popen([LLVM_INTERPRETER, sys.argv[1]+'.clean.bc'] + sys.argv[2:]).communicate()[0]

#Popen([LLVM_COMPILER, '-march=c', sys.argv[1], '-o=' + sys.argv[1]+'.cbe.c']).communicate()[0]
#Popen(['gcc', sys.argv[1]+'.cbe.c', '-lstdc++']).communicate()[0]
#Popen(['./a.out'] + sys.argv[2:]).communicate()[0]

0 comments on commit 6c22a66

Please sign in to comment.