Skip to content

Commit

Permalink
add tools to split and merge asm modules from the surrounding code
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Aug 2, 2013
1 parent ad11d72 commit 543131f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion emlink.py
Expand Up @@ -6,7 +6,7 @@
See https://github.com/kripken/emscripten/wiki/Linking
'''

import os, subprocess, sys
import sys
from tools import shared
from tools.asm_module import AsmModule

Expand Down
7 changes: 4 additions & 3 deletions tools/asm_module.py
Expand Up @@ -14,8 +14,9 @@ def __init__(self, filename):
self.end_funcs = self.js.rfind(js_optimizer.end_funcs_marker)
self.end_asm = self.js.rfind(js_optimizer.end_asm_marker)

# pre
# pre and asm
self.pre_js = self.js[:self.start_asm]
self.asm_js = self.js[self.start_asm:self.end_asm]

# heap initializer
self.staticbump = int(re.search(shared.JS.memory_staticbump_pattern, self.pre_js).group(1))
Expand Down Expand Up @@ -238,8 +239,8 @@ def parse_tables(self, js):
for part in parts:
if '=' not in part: continue
part = part.split('var ')[1]
name, data = part.split(' = ')
tables[name] = data
name, data = part.split('=')
tables[name.strip()] = data.strip()
return tables

def merge_tables(self, table, main, side, replacements, f_bases, f_sizes):
Expand Down
26 changes: 26 additions & 0 deletions tools/merge_asm.py
@@ -0,0 +1,26 @@
#!/usr/bin/env python2

'''
Splits a compiler outputted program into the asm module and the surrounding shell. This
can be useful if you want to process the shell in some manner (e.g. minifiy it) in ways
that would be harmful to asm.js code.
'''

import sys
import shared

try:
me, in_shell, in_asm, outfile = sys.argv[:4]
except:
print >> sys.stderr, 'usage: emlink.py [input file] [shell output file] [asm output file]'
sys.exit(1)

print 'Shell input:', in_shell
print 'Asm input:', in_asm
print 'Input file:', outfile

shared.try_delete(outfile)

pre, post = open(in_shell).read().split('// ASM_CODE\n')
open(outfile, 'w').write(pre + '\n' + open(in_asm).read() + '\n' + post)

30 changes: 30 additions & 0 deletions tools/split_asm.py
@@ -0,0 +1,30 @@
#!/usr/bin/env python2

'''
Splits a compiler outputted program into the asm module and the surrounding shell. This
can be useful if you want to process the shell in some manner (e.g. minifiy it) in ways
that would be harmful to asm.js code.
'''

import sys
import shared
from asm_module import AsmModule

try:
me, infile, out_shell, out_asm = sys.argv[:4]
except:
print >> sys.stderr, 'usage: emlink.py [input file] [shell output file] [asm output file]'
sys.exit(1)

print 'Input file:', infile
print 'Shell output:', out_shell
print 'Asm output:', out_asm

shared.try_delete(out_shell)
shared.try_delete(out_asm)

module = AsmModule(infile)

open(out_shell, 'w').write(module.pre_js + '\n// ASM_CODE\n' + module.post_js)
open(out_asm, 'w').write(module.asm_js)

0 comments on commit 543131f

Please sign in to comment.