Skip to content

Commit

Permalink
Fix type bugs with arrays and pointer subtraction
Browse files Browse the repository at this point in the history
- Remove superfluous Number cast when the right-hand side is already a numeric
  type.
- Fix pointer subtraction. Force both operands to have bases of equal size.
- Fix alignment of array types.
  • Loading branch information
syg committed Aug 21, 2012
1 parent cc01593 commit 4db5ee4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
17 changes: 12 additions & 5 deletions src/compiler.js
Expand Up @@ -277,6 +277,8 @@

ArrayType.prototype.lint = function () {
check(this.base, "array without element type");
this.base.lint();
this.align = this.base.align;
};

StructType.prototype.lint = function () {
Expand All @@ -294,8 +296,8 @@
this.fields.push(field = members[i]);
type = field.type;

// Recursively lint inline structs.
if (type instanceof StructType) {
// Recursively lint field types.
if (type) {
type.lint();
}

Expand Down Expand Up @@ -843,9 +845,10 @@
if (rty instanceof PrimitiveType && rty.integral) {
ty = lty;
} else if (rty instanceof PointerType && op === "-") {
if (lty.assignableFrom(rty)) {
ty = Types.i32ty;
}
check(lty.base.size === rty.base.size,
"subtraction with incompatible pointer types " +
quote(lty) + " and " + quote(rty));
ty = Types.i32ty;
}
} else if (BINOP_COMPARISON.indexOf(op) >= 0) {
if (lty instanceof PointerType && isNull(this.right)) {
Expand Down Expand Up @@ -1142,6 +1145,10 @@
return expr;
}

if (rty && rty.numeric) {
return expr;
}

if (!this.integral) {
return new CallExpression(new Identifier("Number"), [expr], expr.loc);
}
Expand Down
1 change: 0 additions & 1 deletion src/types.js
Expand Up @@ -183,7 +183,6 @@
};

PointerType.prototype.align = u32ty;
ArrayType.prototype.align = u32ty;

exports.TypeAlias = TypeAlias;
exports.PrimitiveType = PrimitiveType;
Expand Down
8 changes: 4 additions & 4 deletions test/arrays-stack.ljs
Expand Up @@ -49,22 +49,22 @@ describe('Arrays-Stack', function () {
assert (arr2[1] === 10);

let ST s;
assert (&s - arr === 126);
assert (&s - (ST *)(arr) === 126);
});

it('allocates a struct with an inline array', function () {
let InlineArrayST s;
s.arr[5] = 10;

let ST s2;
assert (&s2 - &s === 100);
assert (&s2 - (ST *)(&s) === 100);
});

it('allocates a struct with an inline array of structs with arrays', function () {
let InlineStructArrayST s;
s.arr[5].arr[5] = 5;

let ST s2;
assert (&s2 - &s === 100 * 100);
assert (&s2 - (ST *)(&s) === 100 * 100);
});
});
});

0 comments on commit 4db5ee4

Please sign in to comment.