aboutsummaryrefslogtreecommitdiff
path: root/src/experimental/simplifyGeneratedFunctionsDetection.diff
blob: 09e0ebcd911bb23e0adcc637ce03a2f0e9492d84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
diff --git a/emscripten.py b/emscripten.py
index b698654..a7843c7 100755
--- a/emscripten.py
+++ b/emscripten.py
@@ -294,8 +294,6 @@ def emscript(infile, settings, outfile, libraries=[], compiler_engine=None,
   outfile.write(blockaddrsize(indexize(pre)))
   pre = None
 
-  #if DEBUG: outfile.write('// funcs\n')
-
   # forward
   forwarded_data = json.dumps(forwarded_json)
   forwarded_file = temp_files.get('.2.json').name
@@ -483,9 +481,14 @@ Runtime.stackRestore = function(top) { asm.stackRestore(top) };
   else:
     function_tables_defs = '\n'.join([table for table in last_forwarded_json['Functions']['tables'].itervalues()])
     outfile.write(function_tables_defs)
+
+  outfile.write('// EMSCRIPTEN_START_FUNCS\n')
+
   outfile.write(blockaddrsize(indexize(funcs_js)))
   funcs_js = None
 
+  outfile.write('// EMSCRIPTEN_END_FUNCS\n')
+
   outfile.write(indexize(post))
   if DEBUG: print >> sys.stderr, '  emscript: phase 3 took %s seconds' % (time.time() - t)
 
diff --git a/src/jsifier.js b/src/jsifier.js
index ff58ece..d6aa110 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -1587,12 +1587,6 @@ function JSify(data, functionsOnly, givenFunctions) {
 
     var shellParts = read(shellFile).split('{{BODY}}');
     print(shellParts[1]);
-    // Print out some useful metadata (for additional optimizations later, like the eliminator)
-    if (EMIT_GENERATED_FUNCTIONS) {
-      print('// EMSCRIPTEN_GENERATED_FUNCTIONS: ' + JSON.stringify(keys(Functions.implementedFunctions).filter(function(func) {
-        return IGNORED_FUNCTIONS.indexOf(func.ident) < 0;
-      })) + '\n');
-    }
 
     PassManager.serialize();
 
diff --git a/src/settings.js b/src/settings.js
index 1bfcf92..99c83f4 100644
--- a/src/settings.js
+++ b/src/settings.js
@@ -336,8 +336,6 @@ var EXPLICIT_ZEXT = 0; // If 1, generate an explicit conversion of zext i1 to i3
 
 var NECESSARY_BLOCKADDRS = []; // List of (function, block) for all block addresses that are taken.
 
-var EMIT_GENERATED_FUNCTIONS = 0; // whether to emit the list of generated functions, needed for external JS optimization passes
-
 // Compiler debugging options
 var DEBUG_TAGS_SHOWING = [];
   // Some useful items:
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index f2dc516..9fa038c 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -140,16 +140,6 @@ var UNDEFINED_NODE = ['unary-prefix', 'void', ['num', 0]];
 var TRUE_NODE = ['unary-prefix', '!', ['num', 0]];
 var FALSE_NODE = ['unary-prefix', '!', ['num', 1]];
 
-var GENERATED_FUNCTIONS_MARKER = '// EMSCRIPTEN_GENERATED_FUNCTIONS:';
-var generatedFunctions = null;
-function setGeneratedFunctions(metadata) {
-  var start = metadata.indexOf(GENERATED_FUNCTIONS_MARKER);
-  generatedFunctions = set(eval(metadata.substr(start + GENERATED_FUNCTIONS_MARKER.length)));
-}
-function isGenerated(ident) {
-  return ident in generatedFunctions;
-}
-
 function srcToAst(src) {
   return uglify.parser.parse(src);
 }
@@ -212,21 +202,9 @@ function traverse(node, pre, post, stack) {
   return result;
 }
 
-// Only walk through the generated functions
-function traverseGenerated(ast, pre, post, stack) {
-  assert(generatedFunctions);
-  traverse(ast, function(node) {
-    if (node[0] == 'defun' && isGenerated(node[1])) {
-      traverse(node, pre, post, stack);
-      return null;
-    }
-  });
-}
-
-function traverseGeneratedFunctions(ast, callback) {
-  assert(generatedFunctions);
+function traverseFunctions(ast, callback) {
   traverse(ast, function(node) {
-    if (node[0] == 'defun' && isGenerated(node[1])) {
+    if (node[0] == 'defun') {
       callback(node);
       return null;
     }
@@ -418,7 +396,7 @@ function simplifyExpressionsPre(ast) {
     var rerun = true;
     while (rerun) {
       rerun = false;
-      traverseGenerated(ast, function process(node, type, stack) {
+      traverse(ast, function process(node, type, stack) {
         if (type == 'binary' && node[1] == '|') {
           if (node[2][0] == 'num' && node[3][0] == 'num') {
             return ['num', node[2][1] | node[3][1]];
@@ -455,7 +433,7 @@ function simplifyExpressionsPre(ast) {
     }
 
     // &-related optimizations
-    traverseGenerated(ast, function(node, type) {
+    traverse(ast, function(node, type) {
       if (type == 'binary' && node[1] == '&' && node[3][0] == 'num') {
         if (node[2][0] == 'num') return ['num', node[2][1] & node[3][1]];
         var input = node[2];
@@ -489,7 +467,7 @@ function simplifyExpressionsPre(ast) {
 
     if (asm) {
       // optimize num >> num, in asm we need this here since we do not run optimizeShifts
-      traverseGenerated(ast, function(node, type) {
+      traverse(ast, function(node, type) {
         if (type == 'binary' && node[1] == '>>' && node[2][0] == 'num' && node[3][0] == 'num') {
           node[0] = 'num';
           node[1] = node[2][1] >> node[3][1];
@@ -505,7 +483,7 @@ function simplifyExpressionsPre(ast) {
     var rerun = true;
     while (rerun) {
       rerun = false;
-      traverseGenerated(ast, function(node, type) {
+      traverse(ast, function(node, type) {
         if (type == 'binary' && node[1] == '+') {
           if (node[2][0] == 'num' && node[3][0] == 'num') {
             rerun = true;
@@ -528,7 +506,7 @@ function simplifyExpressionsPre(ast) {
 
   // if (x == 0) can be if (!x), etc.
   function simplifyZeroComp(ast) {
-    traverseGenerated(ast, function(node, type) {
+    traverse(ast, function(node, type) {
       var binary;
       if (type == 'if' && (binary = node[1])[0] == 'binary') {
         if ((binary[1] == '!=' || binary[1] == '!==') && binary[3][0] == 'num' && binary[3][1] == 0) {
@@ -554,7 +532,7 @@ function simplifyExpressionsPre(ast) {
 // TODO: when shifting a variable, if there are other uses, keep an unshifted version too, to prevent slowdowns?
 function optimizeShiftsInternal(ast, conservative) {
   var MAX_SHIFTS = 3;
-  traverseGeneratedFunctions(ast, function(fun) {
+  traverseFunctions(ast, function(fun) {
     var funMore = true;
     var funFinished = {};
     while (funMore) {
@@ -999,7 +977,7 @@ function vacuum(ast) {
       } break;
     }
   }
-  traverseGeneratedFunctions(ast, function(node) {
+  traverseFunctions(ast, function(node) {
     vacuumInternal(node);
     simplifyNotComps(node);
   });
@@ -1021,7 +999,7 @@ function getStatements(node) {
 //   if (condition) { label == x } else ..
 // We can hoist the multiple block into the condition, thus removing code and one 'if' check
 function hoistMultiples(ast) {
-  traverseGeneratedFunctions(ast, function(node) {
+  traverseFunctions(ast, function(node) {
     traverse(node, function(node, type) {
       var statements = getStatements(node);
       if (!statements) return;
@@ -1135,7 +1113,7 @@ function hoistMultiples(ast) {
   //    if (..) { .. break|continue } else { .. }
   // to
   //    if (..) { .. break|continue } ..
-  traverseGenerated(ast, function(container, type) {
+  traverse(ast, function(container, type) {
     var statements = getStatements(container);
     if (!statements) return;
     for (var i = 0; i < statements.length; i++) {
@@ -1168,7 +1146,7 @@ function loopOptimizer(ast) {
   function passTwo(ast) {
     var neededDos = [];
     // Find unneeded labels
-    traverseGenerated(ast, function(node, type, stack) {
+    traverse(ast, function(node, type, stack) {
       if (type == 'label' && node[2][0] in LOOP) {
         // this is a labelled loop. we don't know if it's needed yet. Mark its label for removal for now now.
         stack.push(node);
@@ -1212,7 +1190,7 @@ function loopOptimizer(ast) {
     // We return whether another pass is necessary
     var more = false;
     // Remove unneeded labels
-    traverseGenerated(ast, function(node, type) {
+    traverse(ast, function(node, type) {
       if (type == 'label' && node[1][0] == '+') {
         more = true;
         var ident = node[1].substr(1);
@@ -1227,13 +1205,13 @@ function loopOptimizer(ast) {
     });
     // Remove unneeded one-time loops. We need such loops if (1) they have a label, or (2) they have a direct break so they are in neededDos.
     // First, add all labeled loops of this nature to neededDos
-    traverseGenerated(ast, function(node, type) {
+    traverse(ast, function(node, type) {
       if (type == 'label' && node[2][0] == 'do') {
         neededDos.push(node[2]);
       }
     });
     // Remove unneeded dos, we know who they are now
-    traverseGenerated(ast, function(node, type) {
+    traverse(ast, function(node, type) {
       if (type == 'do' && neededDos.indexOf(node) < 0) {
         assert(jsonCompare(node[1], ['num', 0]), 'Trying to remove a one-time do loop that is not one of our generated ones.;');
         more = true;
@@ -1407,7 +1385,7 @@ function denormalizeAsm(func, data) {
 //       we still need the eliminator? Closure? And in what order? Perhaps just
 //       closure simple?
 function registerize(ast) {
-  traverseGeneratedFunctions(ast, function(fun) {
+  traverseFunctions(ast, function(fun) {
     if (asm) var asmData = normalizeAsm(fun);
     // Add parameters as a first (fake) var (with assignment), so they get taken into consideration
     var params = {}; // note: params are special, they can never share a register between them (see later)
@@ -1671,7 +1649,7 @@ var ABORTING_ELIMINATOR_SCAN_NODES = set('new', 'object', 'function', 'defun', '
 
 function eliminate(ast, memSafe) {
   // Find variables that have a single use, and if they can be eliminated, do so
-  traverseGeneratedFunctions(ast, function(func, type) {
+  traverseFunctions(ast, function(func, type) {
     if (asm) var asmData = normalizeAsm(func);
     //printErr('eliminate in ' + func[1]);
 
@@ -2218,9 +2196,6 @@ var passes = {
 var src = read(arguments_[0]);
 var ast = srcToAst(src);
 //printErr(JSON.stringify(ast)); throw 1;
-var metadata = src.split('\n').filter(function(line) { return line.indexOf(GENERATED_FUNCTIONS_MARKER) >= 0 })[0];
-//assert(metadata, 'Must have EMSCRIPTEN_GENERATED_FUNCTIONS metadata');
-if (metadata) setGeneratedFunctions(metadata);
 
 arguments_.slice(1).forEach(function(arg) {
   passes[arg](ast);
diff --git a/tools/js_optimizer.py b/tools/js_optimizer.py
index 2fd2211..a253afb 100644
--- a/tools/js_optimizer.py
+++ b/tools/js_optimizer.py
@@ -41,17 +41,17 @@ def run_on_js(filename, passes, js_engine, jcache):
   if os.linesep != '\n':
     js = js.replace(os.linesep, '\n') # we assume \n in the splitting code
 
-  # Find suffix
-  suffix_marker = '// EMSCRIPTEN_GENERATED_FUNCTIONS'
-  suffix_start = js.find(suffix_marker)
-  suffix = ''
-  if suffix_start >= 0:
-    suffix = js[suffix_start:js.find('\n', suffix_start)] + '\n'
-    # if there is metadata, we will run only on the generated functions. If there isn't, we will run on everything.
-    generated = set(eval(suffix[len(suffix_marker)+1:]))
-
-  if not suffix and jcache:
-    # JCache cannot be used without metadata, since it might reorder stuff, and that's dangerous since only generated can be reordered
+  # Find markers
+  start_marker = '// EMSCRIPTEN_START_FUNCS\n'
+  end_marker = '// EMSCRIPTEN_END_FUNCS\n'
+  start = js.find(start_marker)
+  end = js.find(end_marker)
+  assert (start >= 0) == (end >= 0), 'must have both markers or neither'
+  have_markers = start >= 0
+  # if there are markers, we will run only on the generated functions. If there isn't, we will run on everything.
+
+  if not have_markers and jcache:
+    # JCache cannot be used without markers, since it might reorder stuff, and that's dangerous since only generated can be reordered
     # This means jcache does not work after closure compiler runs, for example. But you won't get much benefit from jcache with closure
     # anyhow (since closure is likely the longest part of the build).
     if DEBUG: print >>sys.stderr, 'js optimizer: no metadata, so disabling jcache'
@@ -59,27 +59,10 @@ def run_on_js(filename, passes, js_engine, jcache):
 
   # If we process only generated code, find that and save the rest on the side
   func_sig = re.compile('( *)function (_[\w$]+)\(')
-  if suffix:
-    pos = 0
-    gen_start = 0
-    gen_end = 0
-    while 1:
-      m = func_sig.search(js, pos)
-      if not m: break
-      pos = m.end()
-      indent = m.group(1)
-      ident = m.group(2)
-      if ident in generated:
-        if not gen_start:
-          gen_start = m.start()
-          assert gen_start
-        gen_end = js.find('\n%s}\n' % indent, m.end()) + (3 + len(indent))
-    assert gen_end > gen_start
-    pre = js[:gen_start]
-    post = js[gen_end:]
-    if 'last' in passes:
-      post = post.replace(suffix, '') # no need to write out the metadata - nothing after us needs it
-    js = js[gen_start:gen_end]
+  if have_markers:
+    pre = js[:start + len(start_marker)] # includes start marker
+    post = js[end:] # includes end marker
+    js = js[start + len(start_marker):end]
   else:
     pre = ''
     post = ''
@@ -95,7 +78,7 @@ def run_on_js(filename, passes, js_engine, jcache):
     if m:
       ident = m.group(2)
     else:
-      if suffix: continue # ignore whitespace
+      if have_markers: continue # ignore whitespace
       ident = 'anon_%d' % i
     assert ident
     funcs.append((ident, func))
@@ -131,7 +114,6 @@ def run_on_js(filename, passes, js_engine, jcache):
       temp_file = temp_files.get('.jsfunc_%d.js' % i).name
       f = open(temp_file, 'w')
       f.write(chunk)
-      f.write(suffix)
       f.close()
       return temp_file
     filenames = [write_chunk(chunks[i], i) for i in range(len(chunks))]
@@ -169,7 +151,6 @@ def run_on_js(filename, passes, js_engine, jcache):
       f.write(cached); # TODO: preserve order
       f.write('\n')
   f.write(post);
-  # No need to write suffix: if there was one, it is inside post which exists when suffix is there
   f.write('\n')
   f.close()