2012-04-25 01:47:33 +04:00
|
|
|
#!/usr/bin/env python
|
2010-09-10 07:03:24 +04:00
|
|
|
|
2011-10-13 22:40:46 +04:00
|
|
|
'''
|
2012-01-22 05:16:50 +04:00
|
|
|
You should normally never use this! Use emcc instead.
|
2011-10-13 22:40:46 +04:00
|
|
|
|
2012-01-22 05:16:50 +04:00
|
|
|
This is a small wrapper script around the core JS compiler. This calls that
|
|
|
|
compiler with the settings given to it. It can also read data from C/C++
|
|
|
|
header files (so that the JS compiler can see the constants in those
|
|
|
|
headers, for the libc implementation in JS).
|
2011-10-13 22:40:46 +04:00
|
|
|
'''
|
|
|
|
|
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
|
2010-09-10 07:03:24 +04:00
|
|
|
|
2012-02-05 22:37:40 +04:00
|
|
|
if not os.environ.get('EMSCRIPTEN_SUPPRESS_USAGE_WARNING'):
|
|
|
|
print >> sys.stderr, '''
|
|
|
|
==============================================================
|
|
|
|
WARNING: You should normally never use this! Use emcc instead.
|
|
|
|
==============================================================
|
|
|
|
'''
|
|
|
|
|
|
|
|
from tools import shared
|
2011-01-15 09:44:52 +03:00
|
|
|
|
2012-11-06 00:24:46 +04:00
|
|
|
DEBUG = os.environ.get('EMCC_DEBUG')
|
|
|
|
|
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
|
|
|
|
2011-07-08 08:04:38 +04:00
|
|
|
|
2012-11-06 00:24:46 +04:00
|
|
|
def scan(ll, settings):
|
|
|
|
# blockaddress(@main, %23)
|
|
|
|
blockaddrs = []
|
|
|
|
for blockaddr in re.findall('blockaddress\([^)]*\)', ll):
|
|
|
|
b = blockaddr.split('(')[1][:-1].split(', ')
|
|
|
|
blockaddrs.append(b)
|
|
|
|
if len(blockaddrs) > 0:
|
|
|
|
settings['NECESSARY_BLOCKADDRS'] = blockaddrs
|
|
|
|
|
2012-04-14 05:57:41 +04:00
|
|
|
def emscript(infile, settings, outfile, libraries=[]):
|
2012-11-06 00:24:46 +04:00
|
|
|
"""Runs the emscripten LLVM-to-JS compiler. We parallelize as much as possible
|
2011-07-07 11:38:35 +04:00
|
|
|
|
|
|
|
Args:
|
|
|
|
infile: The path to the input LLVM assembly file.
|
2012-11-06 00:24:46 +04:00
|
|
|
settings: JSON-formatted settings that override the values
|
2011-07-07 11:38:35 +04:00
|
|
|
defined in src/settings.js.
|
|
|
|
outfile: The file where the output is written.
|
|
|
|
"""
|
2012-11-06 00:24:46 +04:00
|
|
|
|
|
|
|
compiler = path_from_root('src', 'compiler.js')
|
|
|
|
|
|
|
|
# Parallelization: We run 3 passes:
|
|
|
|
# 1 aka 'pre' : Process types and metadata and so forth, and generate the preamble.
|
|
|
|
# 2 aka 'funcs': Process functions. We can parallelize this, working on each function independently.
|
|
|
|
# 3 aka 'post' : Process globals, generate postamble and finishing touches.
|
|
|
|
|
|
|
|
# Pre-scan ll and alter settings as necessary
|
|
|
|
ll = open(infile).read()
|
|
|
|
scan(ll, settings)
|
|
|
|
ll = None # allow collection
|
|
|
|
|
|
|
|
# Split input into the relevant parts for each phase
|
|
|
|
pre = ''
|
|
|
|
funcs = [] # split up functions here, for parallelism later
|
|
|
|
meta = '' # needed by each function XXX
|
|
|
|
post = ''
|
|
|
|
|
|
|
|
in_func = False
|
|
|
|
ll_lines = open(infile).readlines()
|
|
|
|
for line in ll_lines:
|
|
|
|
if in_func:
|
|
|
|
funcs[-1] += line
|
|
|
|
if line.startswith('}'):
|
|
|
|
in_func = False
|
|
|
|
#pre += line # XXX pre needs function defs?
|
|
|
|
else:
|
|
|
|
if line.startswith('define '):
|
|
|
|
in_func = True
|
|
|
|
funcs.append(line)
|
|
|
|
#pre += line # XXX pre needs function defs?
|
|
|
|
elif line.find(' = type { ') > 0:
|
|
|
|
pre += line # type
|
|
|
|
elif line.startswith('!'):
|
|
|
|
meta += line # metadata
|
|
|
|
else:
|
|
|
|
post += line # global
|
|
|
|
pre += line # pre needs it to, so we know about globals in pre and funcs
|
|
|
|
ll_lines = None
|
|
|
|
|
|
|
|
#print '========= pre ================\n'
|
|
|
|
#print pre
|
|
|
|
#print '========== funcs ===============\n'
|
|
|
|
#for func in funcs:
|
|
|
|
# print '\n// ===\n\n', func
|
|
|
|
#print '========== post ==============\n'
|
|
|
|
#print post
|
|
|
|
#print '=========================\n'
|
|
|
|
|
|
|
|
# Save settings to a file to work around v8 issue 1579
|
|
|
|
settings_file = temp_files.get('.txt').name
|
2011-07-30 21:26:30 +04:00
|
|
|
s = open(settings_file, 'w')
|
2012-11-06 00:24:46 +04:00
|
|
|
s.write(json.dumps(settings))
|
2011-07-30 21:26:30 +04:00
|
|
|
s.close()
|
2012-11-06 00:24:46 +04:00
|
|
|
|
|
|
|
# Pass 1
|
|
|
|
if DEBUG: print >> sys.stderr, 'phase 1'
|
|
|
|
pre_file = temp_files.get('.ll').name
|
|
|
|
open(pre_file, 'w').write(pre)
|
|
|
|
out = shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, pre_file, 'pre'] + libraries, stdout=subprocess.PIPE, cwd=path_from_root('src'))
|
|
|
|
js, forwarded_data = out.split('//FORWARDED_DATA:')
|
|
|
|
#print 'js', js
|
|
|
|
#print >> sys.stderr, 'FORWARDED_DATA 1:', forwarded_data, type(forwarded_data)
|
|
|
|
forwarded_file = temp_files.get('.json').name
|
|
|
|
open(forwarded_file, 'w').write(forwarded_data)
|
|
|
|
|
|
|
|
# Pass 2
|
2012-11-06 22:36:23 +04:00
|
|
|
# XXX must coordinate function indexixing data when parallelizing
|
2012-11-06 00:24:46 +04:00
|
|
|
if DEBUG: print >> sys.stderr, 'phase 2'
|
|
|
|
funcs_file = temp_files.get('.ll').name
|
|
|
|
open(funcs_file, 'w').write('\n'.join(funcs) + '\n' + meta)
|
|
|
|
out = shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, funcs_file, 'funcs', forwarded_file] + libraries, stdout=subprocess.PIPE, cwd=path_from_root('src'))
|
|
|
|
funcs_js, forwarded_data = out.split('//FORWARDED_DATA:')
|
|
|
|
forwarded_file += '2'
|
|
|
|
open(forwarded_file, 'w').write(forwarded_data)
|
|
|
|
js += funcs_js
|
|
|
|
|
|
|
|
# Pass 3
|
|
|
|
if DEBUG: print >> sys.stderr, 'phase 3'
|
|
|
|
post_file = temp_files.get('.ll').name
|
|
|
|
open(post_file, 'w').write(post)
|
|
|
|
out = shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, post_file, 'post', forwarded_file] + libraries, stdout=subprocess.PIPE, cwd=path_from_root('src'))
|
|
|
|
js += out
|
|
|
|
|
|
|
|
outfile.write(js)
|
2011-07-07 11:38:35 +04:00
|
|
|
outfile.close()
|
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
# 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-07-07 11:38:35 +04:00
|
|
|
settings[name] = json.loads(value)
|
|
|
|
|
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
|
|
|
|
2012-04-14 05:57:41 +04:00
|
|
|
# libraries
|
|
|
|
libraries = args.libraries[0].split(',') if len(args.libraries) > 0 else []
|
|
|
|
|
2011-07-07 11:38:35 +04:00
|
|
|
# Compile the assembly to Javascript.
|
2012-11-06 00:24:46 +04:00
|
|
|
emscript(args.infile, settings, args.outfile, libraries)
|
2011-07-07 11:38:35 +04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2011-07-13 23:30:29 +04:00
|
|
|
parser = optparse.OptionParser(
|
2012-06-20 11:17:46 +04:00
|
|
|
usage='usage: %prog [-h] [-H HEADERS] [-o OUTFILE] [-s FOO=BAR]* infile',
|
2012-01-22 05:16:50 +04:00
|
|
|
description=('You should normally never use this! Use emcc instead. '
|
|
|
|
'This is a wrapper around the JS compiler, converting .ll to .js.'),
|
|
|
|
epilog='')
|
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.')
|
2012-04-14 05:57:41 +04:00
|
|
|
parser.add_option('-L', '--libraries',
|
|
|
|
default=[],
|
|
|
|
action='append',
|
|
|
|
help='Library files (comma separated) to use in addition to those in emscripten src/library_*.')
|
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))
|
|
|
|
|