diff options
author | alon@honor <none@none> | 2010-09-30 23:04:47 -0700 |
---|---|---|
committer | alon@honor <none@none> | 2010-09-30 23:04:47 -0700 |
commit | f57c6002edde6e92f9aa3dde44e10e9e0bf09023 (patch) | |
tree | c19180dba573bd576b936393db28681571e70dbe | |
parent | c6a3fcbbe4b95bb3312d919c2be68e4dee7e5f14 (diff) |
use parse/finalizeLLVM in mathops
-rw-r--r-- | src/intertyper.js | 13 | ||||
-rw-r--r-- | src/jsifier.js | 73 | ||||
-rw-r--r-- | src/parseTools.js | 19 |
3 files changed, 60 insertions, 45 deletions
diff --git a/src/intertyper.js b/src/intertyper.js index 75d8bce8..ed670e65 100644 --- a/src/intertyper.js +++ b/src/intertyper.js @@ -477,12 +477,13 @@ function intertyper(data) { item.variant = item.tokens[1].text; item.tokens.splice(1, 1); } - item.type = item.tokens[1]; - item.ident = item.tokens[2].text; - item.ident2 = item.tokens[4].text; - item.ident3 = item.tokens[5] ? item.tokens[5].text : null; - item.ident4 = item.tokens[8] ? item.tokens[8].text : null; - dprint('mathop', item.op + ',' + item.variant + ',' + item.ident + ',' + item.value); + var segments = splitTokenList(item.tokens.slice(1)); + for (var i = 1; i <= 4; i++) { + if (segments[i-1]) { + item['param'+i] = parseLLVMSegment(segments[i-1]); + } + } + item.type = { text: item.param1.type }; // TODO: unobject this this.forwardItem(item, 'Reintegrator'); }, }); diff --git a/src/jsifier.js b/src/jsifier.js index 9a5d7121..7f5a86cd 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -527,62 +527,65 @@ function JSify(data) { makeFuncLineZyme('mathop', function(item) { with(item) { dprint('mathop', 'mathop: ' + dump(item)); - ident = parseNumerical(ident); - ident2 = parseNumerical(ident2); + for (var i = 1; i <= 4; i++) { + if (item['param'+i]) { + item['ident'+i] = finalizeLLVMParameter(item['param'+i]); + } + } if (GUARD_SIGNS) { if (op[0] == 'u' || (variant && variant[0] == 'u')) { - ident = makeUnSign(ident, type.text); + ident1 = makeUnSign(ident1, type.text); ident2 = makeUnSign(ident2, type.text); } } switch (op) { - case 'add': return ident + ' + ' + ident2; - case 'sub': return ident + ' - ' + ident2; - case 'sdiv': case 'udiv': return 'Math.floor(' + ident + ' / ' + ident2 + ')'; - case 'mul': return ident + ' * ' + ident2; - case 'urem': case 'srem': return 'Math.floor(' + ident + ' % ' + ident2 + ')'; - case 'or': return ident + ' | ' + ident2; - case 'and': return ident + ' & ' + ident2; - case 'xor': return ident + ' ^ ' + ident2; - case 'shl': case 'ashl': case 'lshl': return ident + ' << ' + ident2; - case 'shr': case 'ashr': case 'lshr': return ident + ' >> ' + ident2; - case 'fadd': return ident + ' + ' + ident2; - case 'fsub': return ident + ' - ' + ident2; - case 'fdiv': return ident + ' / ' + ident2; - case 'fmul': return ident + ' * ' + ident2; - case 'uitofp': case 'sitofp': return ident; - case 'fptoui': case 'fptosi': return 'Math.floor(' + ident + ')'; + case 'add': return ident1 + ' + ' + ident2; + case 'sub': return ident1 + ' - ' + ident2; + case 'sdiv': case 'udiv': return 'Math.floor(' + ident1 + ' / ' + ident2 + ')'; + case 'mul': return ident1 + ' * ' + ident2; + case 'urem': case 'srem': return 'Math.floor(' + ident1 + ' % ' + ident2 + ')'; + case 'or': return ident1 + ' | ' + ident2; + case 'and': return ident1 + ' & ' + ident2; + case 'xor': return ident1 + ' ^ ' + ident2; + case 'shl': case 'ashl': case 'lshl': return ident1 + ' << ' + ident2; + case 'shr': case 'ashr': case 'lshr': return ident1 + ' >> ' + ident2; + case 'fadd': return ident1 + ' + ' + ident2; + case 'fsub': return ident1 + ' - ' + ident2; + case 'fdiv': return ident1 + ' / ' + ident2; + case 'fmul': return ident1 + ' * ' + ident2; + case 'uitofp': case 'sitofp': return ident1; + case 'fptoui': case 'fptosi': return 'Math.floor(' + ident1 + ')'; case 'icmp': { switch (variant) { - case 'uge': case 'sge': return '0+(' + ident + ' >= ' + ident2 + ')'; - case 'ule': case 'sle': return '0+(' + ident + ' <= ' + ident2 + ')'; - case 'ugt': case 'sgt': return '0+(' + ident + ' > ' + ident2 + ')'; - case 'ult': case 'slt': return '0+(' + ident + ' < ' + ident2 + ')'; - case 'ne': case 'une': return '0+(' + ident + ' != ' + ident2 + ')'; - case 'eq': return '0+(' + ident + ' == ' + ident2 + ')'; + case 'uge': case 'sge': return '0+(' + ident1 + ' >= ' + ident2 + ')'; + case 'ule': case 'sle': return '0+(' + ident1 + ' <= ' + ident2 + ')'; + case 'ugt': case 'sgt': return '0+(' + ident1 + ' > ' + ident2 + ')'; + case 'ult': case 'slt': return '0+(' + ident1 + ' < ' + ident2 + ')'; + case 'ne': case 'une': return '0+(' + ident1 + ' != ' + ident2 + ')'; + case 'eq': return '0+(' + ident1 + ' == ' + ident2 + ')'; default: throw 'Unknown icmp variant: ' + variant } } case 'fcmp': { switch (variant) { - case 'uge': case 'oge': return '0+(' + ident + ' >= ' + ident2 + ')'; - case 'ule': case 'ole': return '0+(' + ident + ' <= ' + ident2 + ')'; - case 'ugt': case 'ogt': return '0+(' + ident + ' > ' + ident2 + ')'; - case 'ult': case 'olt': return '0+(' + ident + ' < ' + ident2 + ')'; - case 'une': case 'one': return '0+(' + ident + ' != ' + ident2 + ')'; - case 'ueq': case 'oeq': return '0+(' + ident + ' == ' + ident2 + ')'; + case 'uge': case 'oge': return '0+(' + ident1 + ' >= ' + ident2 + ')'; + case 'ule': case 'ole': return '0+(' + ident1 + ' <= ' + ident2 + ')'; + case 'ugt': case 'ogt': return '0+(' + ident1 + ' > ' + ident2 + ')'; + case 'ult': case 'olt': return '0+(' + ident1 + ' < ' + ident2 + ')'; + case 'une': case 'one': return '0+(' + ident1 + ' != ' + ident2 + ')'; + case 'ueq': case 'oeq': return '0+(' + ident1 + ' == ' + ident2 + ')'; default: throw 'Unknown fcmp variant: ' + variant } } - case 'zext': case 'fpext': case 'trunc': case 'sext': case 'fptrunc': return ident; - case 'select': return '(' + ident + ' ? ' + ident3 + ' : ' + ident4 + ')'; + case 'zext': case 'fpext': case 'trunc': case 'sext': case 'fptrunc': return ident1; + case 'select': return '(' + ident1 + ' ? ' + ident2 + ' : ' + ident3 + ')'; case 'ptrtoint': { if (type.text != 'i8*') print('// XXX Warning: Risky ptrtoint operation on line ' + lineNum); - return ident; + return ident1; } case 'inttoptr': { print('// XXX Warning: inttoptr operation on line ' + lineNum); - return ident; + return ident1; } default: throw 'Unknown mathcmp op: ' + item.op } diff --git a/src/parseTools.js b/src/parseTools.js index 7cf0fa0b..51988753 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -156,14 +156,19 @@ function splitTokenList(tokens) { if (tokens.slice(-1)[0].text != ',') tokens.push({text:','}); var ret = []; var seg = []; - tokens.forEach(function(token) { - if (token.text == ',') { + var SPLITTERS = searchable(',', 'to'); // 'to' can separate parameters as well... + for (var i = 0; i < tokens.length; i++) { + var token = tokens[i]; + if (token.text in SPLITTERS) { ret.push(seg); seg = []; + } else if (token.text == ';') { + ret.push(seg); + break; } else { seg.push(token); } - }); + } return ret; } @@ -269,7 +274,13 @@ function parseParamTokens(params) { // Segment ==> Parameter function parseLLVMSegment(segment) { - if (segment[1].text in PARSABLE_LLVM_FUNCTIONS) { + if (segment.length == 1) { + return { + intertype: 'value', + ident: segment[0].text, + type: '?', + }; + } else if (segment[1].text in PARSABLE_LLVM_FUNCTIONS) { return parseLLVMFunctionCall(segment); } else { return { |