diff options
author | alon@honor <none@none> | 2010-08-30 21:30:09 -0700 |
---|---|---|
committer | alon@honor <none@none> | 2010-08-30 21:30:09 -0700 |
commit | 50a1abf1ad3260d63f0ed0b3558e739ce5eb1f41 (patch) | |
tree | 1f491538b2388fd9a54bf773bed34b431f8dc42d | |
parent | 100294df68fd63c1a80b81264cb98d772e7e09fc (diff) |
imported patch optimize_enzymatic
-rw-r--r-- | src/enzymatic.js | 44 | ||||
-rw-r--r-- | src/parser.js | 62 | ||||
-rw-r--r-- | src/utility.js | 25 | ||||
-rw-r--r-- | tests/runner.py | 2 |
4 files changed, 87 insertions, 46 deletions
diff --git a/src/enzymatic.js b/src/enzymatic.js index ea6a4f27..41fa6e81 100644 --- a/src/enzymatic.js +++ b/src/enzymatic.js @@ -22,6 +22,12 @@ Substrate.prototype = { }, addZyme: function(zyme) { + var name_ = '?'; + if (typeof zyme == 'string') { + name_ = zyme; + zyme = arguments[1]; + } + zyme.name_ = name_; this.zymes.push(zyme); if (!zyme.select) zyme.select = Zyme.prototype.select; if (!zyme.process) zyme.process = Zyme.prototype.process; @@ -48,9 +54,10 @@ Substrate.prototype = { // Assumes list of Zymes is non-changing. var results = []; while (true) { - if (DEBUG) print("Cycle start, " + this.items.length + " items."); + dprint('enzymatic', "Cycle start, " + this.items.length + " items."); var hadProcessing = false; for (var z = 0; z < this.zymes.length; z++) { + midComment(); var zyme = this.zymes[z]; var selected = zyme.select(this.items); if (selected.length > 0) { @@ -63,10 +70,13 @@ Substrate.prototype = { } } hadProcessing = true; - this.items = this.items.filter(function(item) { return selected.indexOf(item) == -1 }); var outputs; try { + dprint('Processing using ' + zyme.name_); + PROF(true); outputs = zyme.process(selected); + PROF(); + dprint('...complete'); } catch (e) { print("Exception, current selected are: " + selected.map(dump).join('\n\n').substr(0,100)); print("Stack: " + new Error().stack); @@ -87,16 +97,38 @@ Substrate.prototype = { return outputs[0]; } results = results.concat(outputs.filter(function(output) { return !!output.__result__; })) +/* + this.items = this.items.filter(function(item) { PROF(); return selected.indexOf(item) == -1 }); outputs.filter(function(output) { return !output.__result__; }).forEach(this.addItem, this); - results.forEach(function(output) { - delete output.__result__; // Might recycle these - delete output.__uid__; - }); +*/ + var nonResults = outputs.filter(function(output) { return !output.__result__; }); + + var keptUids = {}; + nonResults.forEach(function(s) { + if (s.__uid__) { + keptUids[s.__uid__] = true; + } else { + this.addItem(s); + } + }, this); + var droppedUids = {}; + selected.forEach(function(s) { if (!keptUids[s.__uid__]) droppedUids[s.__uid__] = true }); + this.items = this.items.filter(function(item) { + if (!droppedUids[item.__uid__]) { + return true; + } else { + delete item.__uid__; + } + }); } } if (this.items.length === 0) { if (DEBUG) print("Solving complete: no remaining items"); finalComment(); + results.forEach(function(output) { + delete output.__result__; // Might recycle these + delete output.__uid__; + }); return results; } if (!hadProcessing) { diff --git a/src/parser.js b/src/parser.js index 12d7e1dd..5dcc1212 100644 --- a/src/parser.js +++ b/src/parser.js @@ -13,22 +13,6 @@ RELOOP = 1; LINEDEBUG = 0; -DEBUG_TAGS_SHOWING = ['labelbranching']; -function dcheck(tag) { - return DEBUG_TAGS_SHOWING.indexOf(arguments[0]) != -1; -} -function dprint() { - var text; - if (arguments[1]) { - if (!dcheck(arguments[0])) return; - text = arguments[1]; - } else { - text = arguments[0]; - } - text = '// ' + text; - print(text); -} - // Prep - allow this to run in both SpiderMonkey and V8 if (!this['load']) { @@ -324,7 +308,7 @@ function intertyper(data) { } // Line splitter. - substrate.addZyme({ + substrate.addZyme('LineSplitter', { selectItem: function(item) { return !!item.llvmText; }, processItem: function(item) { var lines = item.llvmText.split('\n'); @@ -354,7 +338,7 @@ function intertyper(data) { }); // Line tokenizer - substrate.addZyme({ + substrate.addZyme('Tokenizer', { selectItem: function(item) { return item.lineText; }, processItem: function(item) { //print("line: " + item.lineText); @@ -470,17 +454,17 @@ function intertyper(data) { // Line parsers to intermediate form // Comment - substrate.addZyme({ + substrate.addZyme('Comment', { selectItem: function(item) { return item.tokens && item.tokens[0].text == ';' }, processItem: function(item) { return [] }, }); // target - substrate.addZyme({ + substrate.addZyme('Target', { selectItem: function(item) { return item.tokens && item.tokens[0].text == 'target' }, processItem: function(item) { return [] }, }); // globals: type or constant - substrate.addZyme({ + substrate.addZyme('Global', { selectItem: function(item) { return item.tokens && item.tokens.length >= 3 && item.indent === 0 && item.tokens[1].text == '=' }, processItem: function(item) { if (item.tokens[2].text == 'type') { @@ -543,7 +527,7 @@ function intertyper(data) { }, }); // function header - substrate.addZyme({ + substrate.addZyme('FuncHeader', { selectItem: function(item) { return item.tokens && item.tokens.length >= 4 && item.indent === 0 && item.tokens[0].text == 'define' && item.tokens.slice(-1)[0].text == '{' }, processItem: function(item) { @@ -564,7 +548,7 @@ function intertyper(data) { }, }); // label - substrate.addZyme({ + substrate.addZyme('Label', { selectItem: function(item) { return item.tokens && item.tokens.length >= 1 && item.indent === 0 && item.tokens[0].text.substr(-1) == ':' }, processItem: function(item) { return [{ @@ -576,7 +560,7 @@ function intertyper(data) { }, }); // assignment - substrate.addZyme({ + substrate.addZyme('Assign', { selectItem: function(item) { return item.indent === 2 && item.tokens && item.tokens.length >= 3 && findTokenText(item, '=') >= 0 && !item.intertype }, processItem: function(item) { @@ -596,7 +580,7 @@ function intertyper(data) { }); // reintegration - find intermediate representation-parsed items and // place back in parents - substrate.addZyme({ + substrate.addZyme('Reintegrator', { select: function(items) { for (var i = 0; i < items.length; i++) { if (items[i].parentSlot && items[i].intertype) { @@ -619,7 +603,7 @@ function intertyper(data) { } }); // 'load' - substrate.addZyme({ + substrate.addZyme('Load', { selectItem: function(item) { return item.indent === -1 && item.tokens && item.tokens.length >= 3 && item.tokens[0].text == 'load' }, processItem: function(item) { item.intertype = 'load'; @@ -632,7 +616,7 @@ function intertyper(data) { }, }); // 'bitcast' - substrate.addZyme({ + substrate.addZyme('Bitcast', { selectItem: function(item) { return item.indent === -1 && item.tokens && item.tokens.length >= 3 && item.tokens[0].text == 'bitcast' }, processItem: function(item) { item.intertype = 'bitcast'; @@ -643,7 +627,7 @@ function intertyper(data) { }, }); // 'getelementptr' - substrate.addZyme({ + substrate.addZyme('GEP', { selectItem: function(item) { return item.indent === -1 && item.tokens && item.tokens.length >= 3 && item.tokens[0].text == 'getelementptr' }, processItem: function(item) { var last = 0; @@ -660,7 +644,7 @@ function intertyper(data) { }, }); // 'call' - substrate.addZyme({ + substrate.addZyme('Call', { selectItem: function(item) { return item.tokens && item.tokens.length >= 3 && item.tokens[0].text == 'call' && !item.intertype }, processItem: function(item) { item.intertype = 'call'; @@ -684,7 +668,7 @@ function intertyper(data) { }, }); // 'invoke' - substrate.addZyme({ + substrate.addZyme('Invoke', { selectItem: function(item) { return item.tokens && item.tokens.length >= 3 && item.tokens[0].text == 'invoke' && !item.intertype }, processItem: function(item) { item.intertype = 'invoke'; @@ -703,7 +687,7 @@ function intertyper(data) { }, }); // 'alloca' - substrate.addZyme({ + substrate.addZyme('Alloca', { selectItem: function(item) { return item.indent === -1 && item.tokens && item.tokens.length >= 3 && item.tokens[0].text == 'alloca' }, processItem: function(item) { item.intertype = 'alloca'; @@ -713,7 +697,7 @@ function intertyper(data) { }, }); // mathops - substrate.addZyme({ + substrate.addZyme('Mathops', { selectItem: function(item) { return item.indent === -1 && item.tokens && item.tokens.length >= 3 && ['add', 'sub', 'sdiv', 'mul', 'icmp', 'zext', 'urem', 'srem', 'fadd', 'fmul', 'fdiv', 'fcmp', 'uitofp', 'sitofp', 'fpext', 'fptoui', 'fptosi', 'trunc', 'sext', 'select'] .indexOf(item.tokens[0].text) != -1 && !item.intertype }, @@ -736,7 +720,7 @@ function intertyper(data) { }, }); // 'store' - substrate.addZyme({ + substrate.addZyme('Store', { selectItem: function(item) { return item.indent === 2 && item.tokens && item.tokens.length >= 5 && item.tokens[0].text == 'store' && !item.intertype }, processItem: function(item) { @@ -769,7 +753,7 @@ function intertyper(data) { }, }); // 'br' - substrate.addZyme({ + substrate.addZyme('Branch', { selectItem: function(item) { return item.indent === 2 && item.tokens && item.tokens.length >= 3 && item.tokens[0].text == 'br' && !item.intertype }, processItem: function(item) { @@ -793,7 +777,7 @@ function intertyper(data) { }, }); // 'ret' - substrate.addZyme({ + substrate.addZyme('Return', { selectItem: function(item) { return item.indent === 2 && item.tokens && item.tokens.length >= 2 && item.tokens[0].text == 'ret' && !item.intertype }, processItem: function(item) { @@ -807,7 +791,7 @@ function intertyper(data) { }, }); // 'switch' - substrate.addZyme({ + substrate.addZyme('Switch', { selectItem: function(item) { return item.indent === 2 && item.tokens && item.tokens.length >= 2 && item.tokens[0].text == 'switch' && !item.intertype }, processItem: function(item) { @@ -835,7 +819,7 @@ function intertyper(data) { }, }); // function end - substrate.addZyme({ + substrate.addZyme('FuncEnd', { selectItem: function(item) { return item.indent === 0 && item.tokens && item.tokens.length >= 1 && item.tokens[0].text == '}' && !item.intertype }, processItem: function(item) { return [{ @@ -846,7 +830,7 @@ function intertyper(data) { }, }); // external function stub - substrate.addZyme({ + substrate.addZyme('External', { selectItem: function(item) { return item.indent === 0 && item.tokens && item.tokens.length >= 4 && item.tokens[0].text == 'declare' && !item.intertype }, processItem: function(item) { @@ -861,7 +845,7 @@ function intertyper(data) { }, }); // 'unreachable' - substrate.addZyme({ + substrate.addZyme('Unreachable', { selectItem: function(item) { return item.indent === 2 && item.tokens && item.tokens[0].text == 'unreachable' && !item.intertype }, processItem: function(item) { diff --git a/src/utility.js b/src/utility.js index bcff0b65..90805bad 100644 --- a/src/utility.js +++ b/src/utility.js @@ -127,3 +127,28 @@ function splitter(array, filter) { return { leftIn: leftIn, splitOut: splitOut }; } +DEBUG_TAGS_SHOWING = ['labelbranching', 'enzymatic']; +function dcheck(tag) { + return DEBUG_TAGS_SHOWING.indexOf(arguments[0]) != -1; +} +function dprint() { + var text; + if (arguments[1]) { + if (!dcheck(arguments[0])) return; + text = arguments[1]; + } else { + text = arguments[0]; + } + text = '// ' + text; + print(text); +} + +PROF_ORIGIN = Date.now(); +PROF_TIME = PROF_ORIGIN; +function PROF(pass) { + if (!pass) { + dprint("Profiling: " + ((Date.now() - PROF_TIME)/1000) + ' seconds, total: ' + ((Date.now() - PROF_ORIGIN)/1000)); + } + PROF_TIME = Date.now(); +} + diff --git a/tests/runner.py b/tests/runner.py index 82109a34..b27d9714 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -70,7 +70,7 @@ class T(unittest.TestCase): if DEBUG: print output cwd = os.getcwd() os.chdir(path_from_root(['src'])) - output = timeout_run(Popen([PARSER_ENGINE] + PARSER_OPTS + [JS_COMPILER], stdin=open(filename + '.o.llvm', 'r'), stdout=open(filename + '.o.js', 'w'), stderr=STDOUT), 200, 'Parser') + output = timeout_run(Popen([PARSER_ENGINE] + PARSER_OPTS + [JS_COMPILER], stdin=open(filename + '.o.llvm', 'r'), stdout=open(filename + '.o.js', 'w'), stderr=STDOUT), 20, 'Parser') os.chdir(cwd) # return if DEBUG: print output |