2011-07-08 09:53:47 +04:00
|
|
|
#!/usr/bin/python
|
2010-09-10 07:03:24 +04:00
|
|
|
|
2011-10-13 22:40:46 +04:00
|
|
|
'''
|
|
|
|
Run with -h to see usage options.
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
|
|
|
* Emscripten expects the .ll input to be formatted and annotated the way
|
|
|
|
|
|
|
|
llvm-dis -show-annotations
|
|
|
|
|
|
|
|
does. So if you get .ll from something else, you should run it through
|
|
|
|
llvm-as (to generate LLVM bitcode) and then llvm-dis -show-annotations
|
|
|
|
(to get properly formatted and annotated .ll).
|
|
|
|
'''
|
|
|
|
|
2011-07-07 11:38:35 +04:00
|
|
|
import json
|
2011-07-13 23:30:29 +04:00
|
|
|
import optparse
|
2011-07-07 11:38:35 +04:00
|
|
|
import os
|
|
|
|
import subprocess
|
2011-09-25 10:26:59 +04:00
|
|
|
import re
|
2011-07-07 11:38:35 +04:00
|
|
|
import sys
|
2011-07-13 23:30:29 +04:00
|
|
|
from tools import shared
|
2010-09-10 07:03:24 +04:00
|
|
|
|
2011-01-15 09:44:52 +03:00
|
|
|
|
2011-10-05 22:12:45 +04:00
|
|
|
__rootpath__ = os.path.abspath(os.path.dirname(__file__))
|
2011-09-23 14:43:03 +04:00
|
|
|
def path_from_root(*pathelems):
|
|
|
|
"""Returns the absolute path for which the given path elements are
|
|
|
|
relative to the emscripten root.
|
|
|
|
"""
|
2011-10-05 22:12:45 +04:00
|
|
|
return os.path.join(__rootpath__, *pathelems)
|
2011-07-07 11:38:35 +04:00
|
|
|
|
2011-12-12 03:24:04 +04:00
|
|
|
temp_files = shared.TempFiles()
|
2011-07-07 11:38:35 +04:00
|
|
|
|
|
|
|
def assemble(filepath):
|
|
|
|
"""Converts human-readable LLVM assembly to binary LLVM bitcode.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
filepath: The path to the file to assemble. If the name ends with ".bc", the
|
|
|
|
file is assumed to be in bitcode format already.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The path to the assembled file.
|
|
|
|
"""
|
|
|
|
if not filepath.endswith('.bc'):
|
2011-07-09 21:14:00 +04:00
|
|
|
command = [shared.LLVM_AS, '-o=-', filepath]
|
2011-12-12 03:24:04 +04:00
|
|
|
with temp_files.get('.bc') as out: ret = subprocess.call(command, stdout=out)
|
2011-07-07 11:38:35 +04:00
|
|
|
if ret != 0: raise RuntimeError('Could not assemble %s.' % filepath)
|
|
|
|
filepath = out.name
|
|
|
|
return filepath
|
|
|
|
|
|
|
|
|
|
|
|
def disassemble(filepath):
|
|
|
|
"""Converts binary LLVM bitcode to human-readable LLVM assembly.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
filepath: The path to the file to disassemble. If the name ends with ".ll",
|
|
|
|
the file is assumed to be in human-readable assembly format already.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The path to the disassembled file.
|
|
|
|
"""
|
|
|
|
if not filepath.endswith('.ll'):
|
|
|
|
command = [shared.LLVM_DIS, '-o=-', filepath] + shared.LLVM_DIS_OPTS
|
2011-12-12 03:24:04 +04:00
|
|
|
with temp_files.get('.ll') as out: ret = subprocess.call(command, stdout=out)
|
2011-07-07 11:38:35 +04:00
|
|
|
if ret != 0: raise RuntimeError('Could not disassemble %s.' % filepath)
|
|
|
|
filepath = out.name
|
|
|
|
return filepath
|
|
|
|
|
|
|
|
|
|
|
|
def optimize(filepath):
|
|
|
|
"""Runs LLVM's optimization passes on a given bitcode file.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
filepath: The path to the bitcode file to optimize.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The path to the optimized file.
|
|
|
|
"""
|
2011-12-07 08:36:20 +04:00
|
|
|
shared.Building.LLVM_OPTS = 1
|
|
|
|
shared.Settings.QUANTUM_SIZE = 1 # just so it isn't 4, and we assume we can do things that fail on q1
|
|
|
|
command = [shared.LLVM_OPT, '-o=-', filepath] + shared.Building.pick_llvm_opts(3, True)
|
2011-12-12 03:24:04 +04:00
|
|
|
with temp_files.get('.bc') as out: ret = subprocess.call(command, stdout=out)
|
2011-07-07 11:38:35 +04:00
|
|
|
if ret != 0: raise RuntimeError('Could not optimize %s.' % filepath)
|
|
|
|
return out.name
|
|
|
|
|
2010-09-10 07:03:24 +04:00
|
|
|
|
2011-07-07 11:38:35 +04:00
|
|
|
def link(*objects):
|
|
|
|
"""Links multiple LLVM bitcode files into a single file.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
objects: The bitcode files to link.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The path to the linked file.
|
|
|
|
"""
|
2011-07-09 21:14:00 +04:00
|
|
|
command = [shared.LLVM_LINK] + list(objects)
|
2011-12-12 03:24:04 +04:00
|
|
|
with temp_files.get('.bc') as out: ret = subprocess.call(command, stdout=out)
|
2011-07-07 11:38:35 +04:00
|
|
|
if ret != 0: raise RuntimeError('Could not link %s.' % objects)
|
|
|
|
return out.name
|
|
|
|
|
|
|
|
|
2011-09-04 21:20:02 +04:00
|
|
|
def compile_malloc():
|
2011-07-08 09:33:45 +04:00
|
|
|
"""Compiles dlmalloc to LLVM bitcode.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The path to the compiled dlmalloc as an LLVM bitcode (.bc) file.
|
|
|
|
"""
|
2011-07-07 11:38:35 +04:00
|
|
|
src = path_from_root('src', 'dlmalloc.c')
|
2011-07-08 09:33:45 +04:00
|
|
|
includes = '-I' + path_from_root('src', 'include')
|
2011-09-25 05:21:58 +04:00
|
|
|
command = [shared.CLANG, '-c', '-g', '-emit-llvm'] + shared.COMPILER_OPTS + ['-o-', includes, src]
|
2011-12-12 03:24:04 +04:00
|
|
|
with temp_files.get('.bc') as out: ret = subprocess.call(command, stdout=out)
|
2011-07-07 11:38:35 +04:00
|
|
|
if ret != 0: raise RuntimeError('Could not compile dlmalloc.')
|
|
|
|
return out.name
|
|
|
|
|
|
|
|
|
2011-07-08 08:04:38 +04:00
|
|
|
def has_annotations(filepath):
|
2011-07-08 09:33:45 +04:00
|
|
|
"""Tests whether an assembly file contains annotations.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
filepath: The .ll file containing the assembly to check.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Whether the provided file is valid assembly and has annotations.
|
|
|
|
"""
|
2011-07-08 08:04:38 +04:00
|
|
|
return filepath.endswith('.ll') and '[#uses=' in open(filepath).read()
|
|
|
|
|
|
|
|
|
2011-07-07 11:38:35 +04:00
|
|
|
def emscript(infile, settings, outfile):
|
|
|
|
"""Runs the emscripten LLVM-to-JS compiler.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
infile: The path to the input LLVM assembly file.
|
|
|
|
settings: JSON-formatted string of settings that overrides the values
|
|
|
|
defined in src/settings.js.
|
|
|
|
outfile: The file where the output is written.
|
|
|
|
"""
|
2011-12-12 03:24:04 +04:00
|
|
|
settings_file = temp_files.get('.txt').name # Save settings to a file to work around v8 issue 1579
|
2011-07-30 21:26:30 +04:00
|
|
|
s = open(settings_file, 'w')
|
|
|
|
s.write(settings)
|
|
|
|
s.close()
|
2011-07-07 11:38:35 +04:00
|
|
|
compiler = path_from_root('src', 'compiler.js')
|
2011-12-12 06:31:10 +04:00
|
|
|
shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, infile], stdout=outfile, cwd=path_from_root('src'))
|
2011-07-07 11:38:35 +04:00
|
|
|
outfile.close()
|
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
# Construct a final linked and disassembled file.
|
2011-07-08 08:04:38 +04:00
|
|
|
if args.dlmalloc or args.optimize or not has_annotations(args.infile):
|
|
|
|
args.infile = assemble(args.infile)
|
2011-07-08 09:33:45 +04:00
|
|
|
if args.dlmalloc:
|
2011-09-04 21:20:02 +04:00
|
|
|
malloc = compile_malloc()
|
2011-07-08 09:33:45 +04:00
|
|
|
args.infile = link(args.infile, malloc)
|
2011-07-08 08:04:38 +04:00
|
|
|
if args.optimize: args.infile = optimize(args.infile)
|
2011-07-07 11:38:35 +04:00
|
|
|
args.infile = disassemble(args.infile)
|
|
|
|
|
|
|
|
# Prepare settings for serialization to JSON.
|
|
|
|
settings = {}
|
|
|
|
for setting in args.settings:
|
2011-07-13 23:30:29 +04:00
|
|
|
name, value = setting.strip().split('=', 1)
|
2011-11-18 22:02:45 +04:00
|
|
|
assert name != 'OPTIMIZE', 'OPTIMIZE has been renamed MICRO_OPTS, to not confuse new users. Sorry for any inconvenience.'
|
2011-07-07 11:38:35 +04:00
|
|
|
settings[name] = json.loads(value)
|
|
|
|
|
|
|
|
# Adjust sign correction for dlmalloc.
|
|
|
|
if args.dlmalloc:
|
2011-07-08 09:33:45 +04:00
|
|
|
CORRECT_SIGNS = settings.get('CORRECT_SIGNS', 0)
|
2011-07-07 11:38:35 +04:00
|
|
|
if CORRECT_SIGNS in (0, 2):
|
|
|
|
path = path_from_root('src', 'dlmalloc.c')
|
2011-07-08 09:33:45 +04:00
|
|
|
old_lines = settings.get('CORRECT_SIGNS_LINES', [])
|
2011-07-07 11:38:35 +04:00
|
|
|
line_nums = [4816, 4191, 4246, 4199, 4205, 4235, 4227]
|
|
|
|
lines = old_lines + [path + ':' + str(i) for i in line_nums]
|
|
|
|
settings['CORRECT_SIGNS'] = 2
|
|
|
|
settings['CORRECT_SIGNS_LINES'] = lines
|
|
|
|
|
2011-09-25 10:26:59 +04:00
|
|
|
# Add header defines to settings
|
|
|
|
defines = {}
|
|
|
|
include_root = path_from_root('system', 'include')
|
2011-09-25 21:33:17 +04:00
|
|
|
headers = args.headers[0].split(',') if len(args.headers) > 0 else []
|
2011-09-26 05:56:58 +04:00
|
|
|
seen_headers = set()
|
2011-09-25 10:26:59 +04:00
|
|
|
while len(headers) > 0:
|
|
|
|
header = headers.pop(0)
|
|
|
|
if not os.path.isabs(header):
|
|
|
|
header = os.path.join(include_root, header)
|
2011-09-26 05:56:58 +04:00
|
|
|
seen_headers.add(header)
|
2011-09-25 10:26:59 +04:00
|
|
|
for line in open(header, 'r'):
|
|
|
|
line = line.replace('\t', ' ')
|
2011-09-26 05:56:58 +04:00
|
|
|
m = re.match('^ *# *define +(?P<name>[-\w_.]+) +\(?(?P<value>[-\w_.|]+)\)?.*', line)
|
2011-10-02 19:44:32 +04:00
|
|
|
if not m:
|
|
|
|
# Catch enum defines of a very limited sort
|
|
|
|
m = re.match('^ +(?P<name>[A-Z_\d]+) += +(?P<value>\d+).*', line)
|
2011-09-25 10:26:59 +04:00
|
|
|
if m:
|
2011-10-02 19:44:32 +04:00
|
|
|
if m.group('name') != m.group('value'):
|
|
|
|
defines[m.group('name')] = m.group('value')
|
|
|
|
#else:
|
|
|
|
# print 'Warning: %s #defined to itself' % m.group('name') # XXX this can happen if we are set to be equal to an enum (with the same name)
|
2011-09-26 05:56:58 +04:00
|
|
|
m = re.match('^ *# *include *["<](?P<name>[\w_.-/]+)[">].*', line)
|
2011-09-25 10:26:59 +04:00
|
|
|
if m:
|
|
|
|
# Find this file
|
|
|
|
found = False
|
|
|
|
for w in [w for w in os.walk(include_root)]:
|
|
|
|
for f in w[2]:
|
|
|
|
curr = os.path.join(w[0], f)
|
2011-09-26 05:56:58 +04:00
|
|
|
if curr.endswith(m.group('name')) and curr not in seen_headers:
|
2011-09-25 10:26:59 +04:00
|
|
|
headers.append(curr)
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if found: break
|
|
|
|
#assert found, 'Could not find header: ' + m.group('name')
|
|
|
|
if len(defines) > 0:
|
2011-09-25 12:20:34 +04:00
|
|
|
def lookup(value):
|
|
|
|
try:
|
|
|
|
while not unicode(value).isnumeric():
|
|
|
|
value = defines[value]
|
|
|
|
return value
|
|
|
|
except:
|
2011-09-25 21:28:19 +04:00
|
|
|
pass
|
|
|
|
try: # 0x300 etc.
|
|
|
|
value = eval(value)
|
|
|
|
return value
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try: # CONST1|CONST2
|
|
|
|
parts = map(lookup, value.split('|'))
|
|
|
|
value = reduce(lambda a, b: a|b, map(eval, parts))
|
|
|
|
return value
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return None
|
2011-09-25 12:20:34 +04:00
|
|
|
for key, value in defines.items():
|
|
|
|
value = lookup(value)
|
|
|
|
if value is not None:
|
|
|
|
defines[key] = str(value)
|
|
|
|
else:
|
|
|
|
del defines[key]
|
2012-01-08 23:19:33 +04:00
|
|
|
#print >> sys.stderr, 'new defs:', str(defines).replace(',', ',\n '), '\n\n'
|
|
|
|
settings.setdefault('C_DEFINES', {}).update(defines)
|
2011-09-25 10:26:59 +04:00
|
|
|
|
2011-07-07 11:38:35 +04:00
|
|
|
# Compile the assembly to Javascript.
|
|
|
|
emscript(args.infile, json.dumps(settings), args.outfile)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2011-07-13 23:30:29 +04:00
|
|
|
parser = optparse.OptionParser(
|
2011-09-25 10:26:59 +04:00
|
|
|
usage='usage: %prog [-h] [-O] [-m] [-H HEADERS] [-o OUTFILE] [-s FOO=BAR]* infile',
|
2011-07-13 23:30:29 +04:00
|
|
|
description=('Compile an LLVM assembly file to Javascript. Accepts both '
|
|
|
|
'human-readable (*.ll) and bitcode (*.bc) formats.'),
|
2011-07-07 11:38:35 +04:00
|
|
|
epilog='You should have an ~/.emscripten file set up; see settings.py.')
|
2011-07-13 23:30:29 +04:00
|
|
|
parser.add_option('-O', '--optimize',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Run LLVM optimizations on the input.')
|
|
|
|
parser.add_option('-m', '--dlmalloc',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
2011-10-10 01:48:29 +04:00
|
|
|
help='Use dlmalloc. Without, uses a dummy allocator. Warning: This will force a re-disassembly, so .ll line numbers will change.')
|
2011-09-25 10:26:59 +04:00
|
|
|
parser.add_option('-H', '--headers',
|
|
|
|
default=[],
|
|
|
|
action='append',
|
|
|
|
help='System headers (comma separated) whose #defines should be exposed to the compiled code.')
|
2011-07-13 23:30:29 +04:00
|
|
|
parser.add_option('-o', '--outfile',
|
|
|
|
default=sys.stdout,
|
|
|
|
help='Where to write the output; defaults to stdout.')
|
|
|
|
parser.add_option('-s', '--setting',
|
|
|
|
dest='settings',
|
|
|
|
default=[],
|
|
|
|
action='append',
|
|
|
|
metavar='FOO=BAR',
|
|
|
|
help=('Overrides for settings defined in settings.js. '
|
|
|
|
'May occur multiple times.'))
|
|
|
|
|
|
|
|
# Convert to the same format that argparse would have produced.
|
|
|
|
keywords, positional = parser.parse_args()
|
|
|
|
if len(positional) != 1:
|
|
|
|
raise RuntimeError('Must provide exactly one positional argument.')
|
2011-07-30 21:43:43 +04:00
|
|
|
keywords.infile = os.path.abspath(positional[0])
|
2011-07-13 23:30:29 +04:00
|
|
|
if isinstance(keywords.outfile, basestring):
|
|
|
|
keywords.outfile = open(keywords.outfile, 'w')
|
2011-07-08 08:38:42 +04:00
|
|
|
|
2011-12-12 03:24:04 +04:00
|
|
|
temp_files.run_and_clean(lambda: main(keywords))
|
|
|
|
|