aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--emlink.py29
-rwxr-xr-xtests/runner.py26
-rw-r--r--tools/shared.py4
3 files changed, 36 insertions, 23 deletions
diff --git a/emlink.py b/emlink.py
index 02d1fd89..8270291a 100644
--- a/emlink.py
+++ b/emlink.py
@@ -61,20 +61,9 @@ class AsmModule():
self.pre_js = self.js[:self.start_asm]
# heap initializer
- mem_init = re.search(shared.JS.memory_initializer_pattern, self.pre_js)
- if mem_init:
- self.mem_init_full_js = mem_init.group(0)
- self.mem_init_js = mem_init.groups(0)[0][:-2]
- self.mem_init_size = self.mem_init_js.count(',') + self.mem_init_js.count('concat') + 1 # XXX add testing for large and small ones
- pad = 8 - (self.mem_init_size % 8)
- #print >> sys.stderr, 'pad', self.mem_init_size, pad
- if pad < 8:
- self.mem_init_js += '.concat([' + ','.join(['0']*pad) + '])'
- self.mem_init_size += pad
- else:
- self.mem_init_js = ''
- self.mem_init_size = 0
- #print >> sys.stderr, self.mem_init_js
+ self.staticbump = int(re.search(shared.JS.memory_staticbump_pattern, self.pre_js).group(1))
+ if self.staticbump:
+ self.mem_init_js = re.search(shared.JS.memory_initializer_pattern, self.pre_js).group(0)
# global initializers
global_inits = re.search(shared.JS.global_initializers_pattern, self.pre_js)
@@ -110,13 +99,9 @@ class AsmModule():
def relocate_into(self, main):
# heap initializer
- concat = '.concat(' if main.mem_init_js and self.mem_init_js else ''
- end = ')' if main.mem_init_js and self.mem_init_js else ''
- allocation = main.mem_init_js + concat + self.mem_init_js + end
- if allocation:
- full_allocation = '/* memory initializer */ allocate(' + allocation + ', "i8", ALLOC_NONE, Runtime.GLOBAL_BASE)'
- main.pre_js = re.sub(shared.JS.memory_initializer_pattern if main.mem_init_js else shared.JS.no_memory_initializer_pattern, full_allocation, main.pre_js, count=1)
- main.pre_js = re.sub('STATICTOP = STATIC_BASE \+ (\d+);', 'STATICTOP = STATIC_BASE + %d' % (main.mem_init_size + side.mem_init_size), main.pre_js, count=1)
+ if self.staticbump > 0:
+ new_mem_init = self.mem_init_js[:self.mem_init_js.rfind(', ')] + ', Runtime.GLOBAL_BASE+%d)' % main.staticbump
+ main.pre_js = re.sub(shared.JS.memory_staticbump_pattern, 'STATICTOP = STATIC_BASE + %d;\n' % (main.staticbump + side.staticbump) + new_mem_init, main.pre_js, count=1)
# Find function name replacements TODO: do not rename duplicate names with duplicate contents, just merge them
main_funcs = set(main.funcs)
@@ -157,7 +142,7 @@ class AsmModule():
temp = shared.Building.js_optimizer(self.filename, ['asm', 'relocate'], extra_info={
'replacements': replacements,
'fBases': f_bases,
- 'hBase': main.mem_init_size
+ 'hBase': main.staticbump
})
#print >> sys.stderr, 'relocated side into', temp
relocated_funcs = AsmModule(temp)
diff --git a/tests/runner.py b/tests/runner.py
index bd8d3cec..32bfd8ac 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -10702,6 +10702,32 @@ f.close()
return # TODO
+ test('clibs', r'''
+ #include <stdlib.h>
+ #include <string.h>
+ char *side(const char *data);
+ ''', r'''
+ #include <stdio.h>
+ #include "header.h"
+ int main() {
+ char *temp = side("hello through side\n");
+ char *ret = (char*)malloc(strlen(temp)+1);
+ strcpy(ret, temp);
+ temp[1] = 'x';
+ puts(ret);
+ return 0;
+ }
+ ''', r'''
+ #include "header.h"
+ char *side(const char *data) {
+ return (char*)data;
+// char *ret = (char*)malloc(strlen(data)+1);
+ // strcpy(ret, data);
+ // return ret;
+ }
+ ''', ['hello through side\n'])
+ return
+
# C library usage
test('clibs', r'''
#include <iostream>
diff --git a/tools/shared.py b/tools/shared.py
index 3e7bac9d..42dc3d76 100644
--- a/tools/shared.py
+++ b/tools/shared.py
@@ -1351,9 +1351,11 @@ JCache = cache.JCache(Cache)
chunkify = cache.chunkify
class JS:
- memory_initializer_pattern = '/\* memory initializer \*/ allocate\(([\d,\.concat\(\)\[\]\\n ]+)"i8", ALLOC_NONE, Runtime\.GLOBAL_BASE\)'
+ memory_initializer_pattern = '/\* memory initializer \*/ allocate\(([\d,\.concat\(\)\[\]\\n ]+)"i8", ALLOC_NONE, ([\dRuntime\.GLOBAL_BASE+]+)\)'
no_memory_initializer_pattern = '/\* no memory initializer \*/'
+ memory_staticbump_pattern = 'STATICTOP = STATIC_BASE \+ (\d+);'
+
global_initializers_pattern = '/\* global initializers \*/ __ATINIT__.push\((.+)\);'
@staticmethod