aboutsummaryrefslogtreecommitdiff
path: root/src/analyzer.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/analyzer.js')
-rw-r--r--src/analyzer.js112
1 files changed, 77 insertions, 35 deletions
diff --git a/src/analyzer.js b/src/analyzer.js
index 17ad26ad..95fbccc7 100644
--- a/src/analyzer.js
+++ b/src/analyzer.js
@@ -188,7 +188,7 @@ function analyzer(data, sidePass) {
if (USE_TYPED_ARRAYS == 2) {
function getLegalVars(base, bits, allowLegal) {
bits = bits || 32; // things like pointers are all i32, but show up as 0 bits from getBits
- if (allowLegal && bits <= 32) return [{ ident: base + ('i' + bits in Runtime.INT_TYPES ? '' : '$0'), bits: bits }];
+ if (allowLegal && bits <= 32) return [{ intertype: 'value', ident: base + ('i' + bits in Runtime.INT_TYPES ? '' : '$0'), bits: bits, type: 'i' + bits }];
if (isNumber(base)) return getLegalLiterals(base, bits);
if (base[0] == '{') {
warnOnce('seeing source of illegal data ' + base + ', likely an inline struct - assuming zeroinit');
@@ -198,7 +198,7 @@ function analyzer(data, sidePass) {
var i = 0;
if (base == 'zeroinitializer' || base == 'undef') base = 0;
while (bits > 0) {
- ret[i] = { ident: base ? base + '$' + i : '0', bits: Math.min(32, bits) };
+ ret[i] = { intertype: 'value', ident: base ? base + '$' + i : '0', bits: Math.min(32, bits), type: 'i' + Math.min(32, bits) };
bits -= 32;
i++;
}
@@ -209,7 +209,7 @@ function analyzer(data, sidePass) {
var ret = new Array(Math.ceil(bits/32));
var i = 0;
while (bits > 0) {
- ret[i] = { ident: (parsed[i]|0).toString(), bits: Math.min(32, bits) }; // resign all values
+ ret[i] = { intertype: 'value', ident: (parsed[i]|0).toString(), bits: Math.min(32, bits), type: 'i' + Math.min(32, bits) }; // resign all values
bits -= 32;
i++;
}
@@ -225,7 +225,8 @@ function analyzer(data, sidePass) {
return getLegalLiterals(value.ident, bits);
} else if (value.intertype == 'structvalue') {
return getLegalStructuralParts(value).map(function(part) {
- return { ident: part.ident, bits: part.type.substr(1) };
+ part.bits = part.type.substr(1); // can be some nested IR, like LLVM calls
+ return part;
});
} else {
return getLegalVars(value.ident, bits);
@@ -550,11 +551,7 @@ function analyzer(data, sidePass) {
return {
intertype: 'phiparam',
label: param.label,
- value: {
- intertype: 'value',
- ident: values[k++][j].ident,
- type: 'i' + element.bits,
- }
+ value: values[k++][j]
};
})
});
@@ -802,27 +799,65 @@ function analyzer(data, sidePass) {
var whole = shifts >= 0 ? Math.floor(shifts/32) : Math.ceil(shifts/32);
var fraction = Math.abs(shifts % 32);
if (signed) {
- var signedFill = '(' + makeSignOp(sourceElements[sourceElements.length-1].ident, 'i' + sourceElements[sourceElements.length-1].bits, 're', 1, 1) + ' < 0 ? -1 : 0)';
- var signedKeepAlive = { intertype: 'value', ident: sourceElements[sourceElements.length-1].ident, type: 'i32' };
- }
- for (var j = 0; j < targetElements.length; j++) {
- var result = {
- intertype: 'value',
- ident: (j + whole >= 0 && j + whole < sourceElements.length) ? sourceElements[j + whole].ident : (signed ? signedFill : '0'),
- params: [(signed && j + whole > sourceElements.length) ? signedKeepAlive : null],
+ var signedFill = {
+ intertype: 'mathop',
+ op: 'select',
+ variant: 's',
type: 'i32',
+ params: [{
+ intertype: 'mathop',
+ op: 'icmp',
+ variant: 'slt',
+ type: 'i32',
+ params: [
+ { intertype: 'value', ident: sourceElements[sourceElements.length-1].ident, type: 'i' + Math.min(sourceBits, 32) },
+ { intertype: 'value', ident: '0', type: 'i32' }
+ ]
+ },
+ { intertype: 'value', ident: '-1', type: 'i32' },
+ { intertype: 'value', ident: '0', type: 'i32' },
+ ]
};
- if (j == 0 && sourceBits < 32) {
- // zext sign correction
- result.ident = makeSignOp(result.ident, 'i' + sourceBits, isUnsignedOp(value.op) ? 'un' : 're', 1, 1);
- }
- if (fraction != 0) {
- var other = {
+ }
+ for (var j = 0; j < targetElements.length; j++) {
+ var inBounds = j + whole >= 0 && j + whole < sourceElements.length;
+ var result;
+ if (inBounds || !signed) {
+ result = {
intertype: 'value',
- ident: (j + sign + whole >= 0 && j + sign + whole < sourceElements.length) ? sourceElements[j + sign + whole].ident : (signed ? signedFill : '0'),
- params: [(signed && j + sign + whole > sourceElements.length) ? signedKeepAlive : null],
- type: 'i32',
+ ident: inBounds ? sourceElements[j + whole].ident : '0',
+ type: 'i' + Math.min(sourceBits, 32),
};
+ if (j == 0 && sourceBits < 32) {
+ // zext sign correction
+ var result2 = {
+ intertype: 'mathop',
+ op: isUnsignedOp(value.op) ? 'zext' : 'sext',
+ params: [result, {
+ intertype: 'type',
+ ident: 'i32',
+ type: 'i' + sourceBits
+ }],
+ type: 'i32'
+ };
+ result = result2;
+ }
+ } else {
+ // out of bounds and signed
+ result = copy(signedFill);
+ }
+ if (fraction != 0) {
+ var other;
+ var otherInBounds = j + sign + whole >= 0 && j + sign + whole < sourceElements.length;
+ if (otherInBounds || !signed) {
+ other = {
+ intertype: 'value',
+ ident: otherInBounds ? sourceElements[j + sign + whole].ident : '0',
+ type: 'i32',
+ };
+ } else {
+ other = copy(signedFill);
+ }
other = {
intertype: 'mathop',
op: shiftOp,
@@ -872,10 +907,17 @@ function analyzer(data, sidePass) {
}
if (targetBits <= 32) {
// We are generating a normal legal type here
- legalValue = {
- intertype: 'value',
- ident: targetElements[0].ident + (targetBits < 32 ? '&' + (Math.pow(2, targetBits)-1) : ''),
- type: 'rawJS'
+ legalValue = { intertype: 'value', ident: targetElements[0].ident, type: 'i32' };
+ if (targetBits < 32) {
+ legalValue = {
+ intertype: 'mathop',
+ op: 'and',
+ type: 'i32',
+ params: [
+ legalValue,
+ { intertype: 'value', ident: (Math.pow(2, targetBits)-1).toString(), type: 'i32' }
+ ]
+ }
};
legalValue.assignTo = item.assignTo;
toAdd.push(legalValue);
@@ -1117,7 +1159,7 @@ function analyzer(data, sidePass) {
rawLinesIndex: i
};
if (variable.origin === 'alloca') {
- variable.allocatedNum = item.allocatedNum;
+ variable.allocatedNum = item.ident;
}
if (variable.origin === 'call') {
variable.type = getReturnType(variable.type);
@@ -1608,9 +1650,9 @@ function analyzer(data, sidePass) {
var lines = func.labels[0].lines;
for (var i = 0; i < lines.length; i++) {
var item = lines[i];
- if (!item.assignTo || item.intertype != 'alloca' || !isNumber(item.allocatedNum)) break;
+ if (!item.assignTo || item.intertype != 'alloca' || !isNumber(item.ident)) break;
item.allocatedSize = func.variables[item.assignTo].impl === VAR_EMULATED ?
- calcAllocatedSize(item.allocatedType)*item.allocatedNum: 0;
+ calcAllocatedSize(item.allocatedType)*item.ident: 0;
if (USE_TYPED_ARRAYS === 2) {
// We need to keep the stack aligned
item.allocatedSize = Runtime.forceAlign(item.allocatedSize, Runtime.STACK_ALIGN);
@@ -1619,7 +1661,7 @@ function analyzer(data, sidePass) {
var index = 0;
for (var i = 0; i < lines.length; i++) {
var item = lines[i];
- if (!item.assignTo || item.intertype != 'alloca' || !isNumber(item.allocatedNum)) break;
+ if (!item.assignTo || item.intertype != 'alloca' || !isNumber(item.ident)) break;
item.allocatedIndex = index;
index += item.allocatedSize;
delete item.allocatedSize;
@@ -1647,7 +1689,7 @@ function analyzer(data, sidePass) {
for (var i = 0; i < lines.length; i++) {
var item = lines[i];
- if (!finishedInitial && (!item.assignTo || item.intertype != 'alloca' || !isNumber(item.allocatedNum))) {
+ if (!finishedInitial && (!item.assignTo || item.intertype != 'alloca' || !isNumber(item.ident))) {
finishedInitial = true;
}
if (item.intertype == 'alloca' && finishedInitial) {