Skip to content
This repository
Browse code

Fix type bugs with arrays and pointer subtraction

- 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...
commit 4db5ee41fb1171f9fa9e41cb463891cc644502f3 1 parent cc01593
Shu-yu Guo syg authored

Showing 3 changed files with 16 additions and 10 deletions. Show diff stats Hide diff stats

  1. +12 5 src/compiler.js
  2. +0 1  src/types.js
  3. +4 4 test/arrays-stack.ljs
17 src/compiler.js
@@ -277,6 +277,8 @@
277 277
278 278 ArrayType.prototype.lint = function () {
279 279 check(this.base, "array without element type");
  280 + this.base.lint();
  281 + this.align = this.base.align;
280 282 };
281 283
282 284 StructType.prototype.lint = function () {
@@ -294,8 +296,8 @@
294 296 this.fields.push(field = members[i]);
295 297 type = field.type;
296 298
297   - // Recursively lint inline structs.
298   - if (type instanceof StructType) {
  299 + // Recursively lint field types.
  300 + if (type) {
299 301 type.lint();
300 302 }
301 303
@@ -843,9 +845,10 @@
843 845 if (rty instanceof PrimitiveType && rty.integral) {
844 846 ty = lty;
845 847 } else if (rty instanceof PointerType && op === "-") {
846   - if (lty.assignableFrom(rty)) {
847   - ty = Types.i32ty;
848   - }
  848 + check(lty.base.size === rty.base.size,
  849 + "subtraction with incompatible pointer types " +
  850 + quote(lty) + " and " + quote(rty));
  851 + ty = Types.i32ty;
849 852 }
850 853 } else if (BINOP_COMPARISON.indexOf(op) >= 0) {
851 854 if (lty instanceof PointerType && isNull(this.right)) {
@@ -1142,6 +1145,10 @@
1142 1145 return expr;
1143 1146 }
1144 1147
  1148 + if (rty && rty.numeric) {
  1149 + return expr;
  1150 + }
  1151 +
1145 1152 if (!this.integral) {
1146 1153 return new CallExpression(new Identifier("Number"), [expr], expr.loc);
1147 1154 }
1  src/types.js
@@ -183,7 +183,6 @@
183 183 };
184 184
185 185 PointerType.prototype.align = u32ty;
186   - ArrayType.prototype.align = u32ty;
187 186
188 187 exports.TypeAlias = TypeAlias;
189 188 exports.PrimitiveType = PrimitiveType;
8 test/arrays-stack.ljs
@@ -49,7 +49,7 @@ describe('Arrays-Stack', function () {
49 49 assert (arr2[1] === 10);
50 50
51 51 let ST s;
52   - assert (&s - arr === 126);
  52 + assert (&s - (ST *)(arr) === 126);
53 53 });
54 54
55 55 it('allocates a struct with an inline array', function () {
@@ -57,7 +57,7 @@ describe('Arrays-Stack', function () {
57 57 s.arr[5] = 10;
58 58
59 59 let ST s2;
60   - assert (&s2 - &s === 100);
  60 + assert (&s2 - (ST *)(&s) === 100);
61 61 });
62 62
63 63 it('allocates a struct with an inline array of structs with arrays', function () {
@@ -65,6 +65,6 @@ describe('Arrays-Stack', function () {
65 65 s.arr[5].arr[5] = 5;
66 66
67 67 let ST s2;
68   - assert (&s2 - &s === 100 * 100);
  68 + assert (&s2 - (ST *)(&s) === 100 * 100);
69 69 });
70   -});
  70 +});

0 comments on commit 4db5ee4

Please sign in to comment.
Something went wrong with that request. Please try again.