Skip to content

Commit 9d25f78

Browse files
committed
Implement 'this' context parsing and serialization; Other minor improvements
1 parent c9ed030 commit 9d25f78

File tree

18 files changed

+1467
-1342
lines changed

18 files changed

+1467
-1342
lines changed

dist/asc.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/asc.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/n-body/assembly/index.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@ import "allocator/arena";
33
// From The Computer Language Benchmarks Game
44
// http://benchmarksgame.alioth.debian.org
55

6-
const SOLAR_MASS = 4.0 * Math.PI * Math.PI;
7-
const DAYS_PER_YEAR = 365.24;
6+
type float = f64; // interchangeable f32/f64 for testing
7+
8+
const SOLAR_MASS = <float>(4.0 * Math.PI * Math.PI);
9+
const DAYS_PER_YEAR: float = 365.24;
810

911
class Body {
1012

1113
constructor(
12-
public x: f64,
13-
public y: f64,
14-
public z: f64,
15-
public vx: f64,
16-
public vy: f64,
17-
public vz: f64,
18-
public mass: f64
14+
public x: float,
15+
public y: float,
16+
public z: float,
17+
public vx: float,
18+
public vy: float,
19+
public vz: float,
20+
public mass: float
1921
) {}
2022

21-
offsetMomentum(px: f64, py: f64, pz: f64): this {
23+
offsetMomentum(px: float, py: float, pz: float): this {
2224
this.vx = -px / SOLAR_MASS;
2325
this.vy = -py / SOLAR_MASS;
2426
this.vz = -pz / SOLAR_MASS;
@@ -85,9 +87,9 @@ class NBodySystem {
8587
constructor(
8688
public bodies: Body[]
8789
) {
88-
var px = 0.0;
89-
var py = 0.0;
90-
var pz = 0.0;
90+
var px: float = 0.0;
91+
var py: float = 0.0;
92+
var pz: float = 0.0;
9193
var size = bodies.length;
9294
for (let i = 0; i < size; i++) {
9395
let b = unchecked(bodies[i]);
@@ -99,7 +101,7 @@ class NBodySystem {
99101
bodies[0].offsetMomentum(px, py, pz);
100102
}
101103

102-
advance(dt: f64): void {
104+
advance(dt: float): void {
103105
var bodies = this.bodies;
104106
var size: u32 = bodies.length;
105107
// var buffer = changetype<usize>(bodies.buffer_);
@@ -126,7 +128,7 @@ class NBodySystem {
126128
let dz = iz - bodyj.z;
127129

128130
let distanceSq = dx * dx + dy * dy + dz * dz;
129-
let distance = Math.sqrt(distanceSq);
131+
let distance = <float>Math.sqrt(distanceSq);
130132
let mag = dt / (distanceSq * distance);
131133

132134
let bim = bodyim * mag;
@@ -151,8 +153,8 @@ class NBodySystem {
151153
}
152154
}
153155

154-
energy(): f64 {
155-
var e = 0.0;
156+
energy(): float {
157+
var e: float = 0.0;
156158
var bodies = this.bodies;
157159

158160
for (let i: u32 = 0, size: u32 = bodies.length; i < size; ++i) {
@@ -171,11 +173,11 @@ class NBodySystem {
171173
e += 0.5 * bim * (vx * vx + vy * vy + vz * vz);
172174

173175
for (let j: u32 = i + 1; j < size; ++j) {
174-
let bodyj = bodies[j];
176+
let bodyj = unchecked(bodies[j]);
175177
let dx = ix - bodyj.x;
176178
let dy = iy - bodyj.y;
177179
let dz = iz - bodyj.z;
178-
let distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
180+
let distance = <float>Math.sqrt(dx * dx + dy * dy + dz * dz);
179181
e -= bim * bodyj.mass / distance;
180182
}
181183
}
@@ -200,7 +202,7 @@ export function getBody(index: i32): Body | null {
200202
return <u32>index < <u32>bodies.length ? bodies[index] : null;
201203
}
202204

203-
export function step(): f64 {
205+
export function step(): float {
204206
system.advance(0.01);
205207
return system.energy();
206208
}

examples/n-body/build/index.asm.js

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function asmFunc(global, env, buffer) {
1717
var Math_floor = global.Math.floor;
1818
var Math_ceil = global.Math.ceil;
1919
var Math_sqrt = global.Math.sqrt;
20-
var abort = env.abort;
20+
var $lib_env_abort = env.abort;
2121
var $lib_allocator_arena_startOffset = 0;
2222
var $lib_allocator_arena_offset = 0;
2323
var assembly_index_system = 0;
@@ -165,7 +165,7 @@ function asmFunc(global, env, buffer) {
165165
$1 = $1 | 0;
166166
var $2 = 0, $3 = 0, $4 = 0;
167167
if ($1 >>> 0 > 268435454 >>> 0) {
168-
abort(0 | 0, 8 | 0, 23 | 0, 39 | 0);
168+
$lib_env_abort(0 | 0, 8 | 0, 23 | 0, 39 | 0);
169169
abort();
170170
}
171171
$3 = $1 << 2 | 0;
@@ -189,18 +189,19 @@ function asmFunc(global, env, buffer) {
189189
$1 = $1 | 0;
190190
var $2 = 0, $3 = 0, $4 = 0.0, $5 = 0.0, $6 = 0.0, $7 = 0.0, $72 = 0, $8 = 0, $50 = 0;
191191
$8 = HEAP32[($1 + 4 | 0) >> 2] | 0;
192-
continue_0 : do {
193-
if (($2 | 0) < ($8 | 0)) {
192+
break_0 : {
193+
repeat_0 : do {
194+
if (($2 | 0) >= ($8 | 0)) break break_0;
194195
$3 = HEAPU32[(((HEAPU32[$1 >> 2] | 0) + ($2 << 2 | 0) | 0) + 8 | 0) >> 2] | 0;
195196
$4 = +HEAPF64[($3 + 48 | 0) >> 3];
196197
$5 = $5 + +HEAPF64[($3 + 24 | 0) >> 3] * $4;
197198
$6 = $6 + +HEAPF64[($3 + 32 | 0) >> 3] * $4;
198199
$7 = $7 + +HEAPF64[($3 + 40 | 0) >> 3] * $4;
199200
$2 = $2 + 1 | 0;
200-
continue continue_0;
201-
}
202-
break continue_0;
203-
} while (1);
201+
continue repeat_0;
202+
break repeat_0;
203+
} while (1);
204+
};
204205
$2 = HEAPU32[$1 >> 2] | 0;
205206
if (0 >>> 0 < ((HEAP32[$2 >> 2] | 0) >>> 2 | 0) >>> 0) $50 = HEAPU32[($2 + 8 | 0) >> 2] | 0; else abort();
206207
$2 = $50;
@@ -250,8 +251,9 @@ function asmFunc(global, env, buffer) {
250251
var $2 = 0, $3 = 0.0, $9 = 0.0, $4 = 0, $5 = 0.0, $6 = 0.0, $7 = 0.0, $8 = 0, $10 = 0.0, $11 = 0.0, $12 = 0.0, $13 = 0, $14 = 0, $15 = 0.0, $16 = 0.0, $17 = 0.0, $18 = 0.0;
251252
$13 = HEAPU32[$0 >> 2] | 0;
252253
$14 = HEAP32[($13 + 4 | 0) >> 2] | 0;
253-
continue_0 : do {
254-
if ($4 >>> 0 < $14 >>> 0) {
254+
break_0 : {
255+
repeat_0 : do {
256+
if ($4 >>> 0 >= $14 >>> 0) break break_0;
255257
$0 = HEAPU32[(((HEAPU32[$13 >> 2] | 0) + ($4 << 2 | 0) | 0) + 8 | 0) >> 2] | 0;
256258
$15 = +HEAPF64[$0 >> 3];
257259
$16 = +HEAPF64[($0 + 8 | 0) >> 3];
@@ -260,9 +262,10 @@ function asmFunc(global, env, buffer) {
260262
$6 = +HEAPF64[($0 + 32 | 0) >> 3];
261263
$7 = +HEAPF64[($0 + 40 | 0) >> 3];
262264
$18 = +HEAPF64[($0 + 48 | 0) >> 3];
263-
$8 = $4 + 1 | 0;
264-
continue_1 : do {
265-
if ($8 >>> 0 < $14 >>> 0) {
265+
break_1 : {
266+
$8 = $4 + 1 | 0;
267+
repeat_1 : do {
268+
if ($8 >>> 0 >= $14 >>> 0) break break_1;
266269
$2 = HEAPU32[(((HEAPU32[$13 >> 2] | 0) + ($8 << 2 | 0) | 0) + 8 | 0) >> 2] | 0;
267270
$10 = $15 - +HEAPF64[$2 >> 3];
268271
$11 = $16 - +HEAPF64[($2 + 8 | 0) >> 3];
@@ -278,31 +281,32 @@ function asmFunc(global, env, buffer) {
278281
HEAPF64[($2 + 32 | 0) >> 3] = +HEAPF64[($2 + 32 | 0) >> 3] + $11 * $3;
279282
HEAPF64[($2 + 40 | 0) >> 3] = +HEAPF64[($2 + 40 | 0) >> 3] + $12 * $3;
280283
$8 = $8 + 1 | 0;
281-
continue continue_1;
282-
}
283-
break continue_1;
284-
} while (1);
284+
continue repeat_1;
285+
break repeat_1;
286+
} while (1);
287+
};
285288
HEAPF64[($0 + 24 | 0) >> 3] = $5;
286289
HEAPF64[($0 + 32 | 0) >> 3] = $6;
287290
HEAPF64[($0 + 40 | 0) >> 3] = $7;
288291
HEAPF64[$0 >> 3] = +HEAPF64[$0 >> 3] + $1 * $5;
289292
HEAPF64[($0 + 8 | 0) >> 3] = +HEAPF64[($0 + 8 | 0) >> 3] + $1 * $6;
290293
HEAPF64[($0 + 16 | 0) >> 3] = +HEAPF64[($0 + 16 | 0) >> 3] + $1 * $7;
291294
$4 = $4 + 1 | 0;
292-
continue continue_0;
293-
}
294-
break continue_0;
295-
} while (1);
295+
continue repeat_0;
296+
break repeat_0;
297+
} while (1);
298+
};
296299
}
297300

298301
function assembly_index_NBodySystem_energy($0) {
299302
$0 = $0 | 0;
300-
var $1 = 0.0, $2 = 0, $3 = 0, $4 = 0, $5 = 0, $10 = 0.0, $6 = 0.0, $7 = 0.0, $8 = 0.0, $9 = 0.0, $30 = 0.0, $39 = 0.0, $45 = 0.0, $59 = 0.0, $72 = 0, $77 = 0.0, $92 = 0.0;
301-
$4 = HEAPU32[$0 >> 2] | 0;
302-
$5 = HEAP32[($4 + 4 | 0) >> 2] | 0;
303-
continue_0 : do {
304-
if ($3 >>> 0 < $5 >>> 0) {
305-
$0 = HEAPU32[(((HEAPU32[$4 >> 2] | 0) + ($3 << 2 | 0) | 0) + 8 | 0) >> 2] | 0;
303+
var $1 = 0.0, $2 = 0, $3 = 0, $4 = 0, $5 = 0, $10 = 0.0, $6 = 0.0, $7 = 0.0, $8 = 0.0, $9 = 0.0, $30 = 0.0, $39 = 0.0, $45 = 0.0, $69 = 0.0, $84 = 0.0;
304+
break_0 : {
305+
$4 = HEAPU32[$0 >> 2] | 0;
306+
$5 = HEAP32[($4 + 4 | 0) >> 2] | 0;
307+
repeat_0 : do {
308+
if ($2 >>> 0 >= $5 >>> 0) break break_0;
309+
$0 = HEAPU32[(((HEAPU32[$4 >> 2] | 0) + ($2 << 2 | 0) | 0) + 8 | 0) >> 2] | 0;
306310
$7 = +HEAPF64[$0 >> 3];
307311
$8 = +HEAPF64[($0 + 8 | 0) >> 3];
308312
$9 = +HEAPF64[($0 + 16 | 0) >> 3];
@@ -314,29 +318,27 @@ function asmFunc(global, env, buffer) {
314318
$45 = $39 + $1 * $1;
315319
$1 = +HEAPF64[($0 + 40 | 0) >> 3];
316320
$1 = $30 + .5 * $10 * ($45 + $1 * $1);
317-
$0 = $3 + 1 | 0;
318-
continue_1 : do {
319-
if ($0 >>> 0 < $5 >>> 0) {
320-
$59 = $7;
321-
$2 = HEAPU32[$4 >> 2] | 0;
322-
if ($0 >>> 0 < ((HEAP32[$2 >> 2] | 0) >>> 2 | 0) >>> 0) $72 = HEAPU32[(($2 + ($0 << 2 | 0) | 0) + 8 | 0) >> 2] | 0; else abort();
323-
$2 = $72;
324-
$6 = $59 - +HEAPF64[$2 >> 3];
325-
$77 = $1;
326-
$1 = $8 - +HEAPF64[($2 + 8 | 0) >> 3];
327-
$92 = $6 * $6 + $1 * $1;
328-
$1 = $9 - +HEAPF64[($2 + 16 | 0) >> 3];
329-
$1 = $77 - $10 * +HEAPF64[($2 + 48 | 0) >> 3] / Math_sqrt($92 + $1 * $1);
321+
break_1 : {
322+
$0 = $2 + 1 | 0;
323+
repeat_1 : do {
324+
if ($0 >>> 0 >= $5 >>> 0) break break_1;
325+
$3 = HEAPU32[(((HEAPU32[$4 >> 2] | 0) + ($0 << 2 | 0) | 0) + 8 | 0) >> 2] | 0;
326+
$6 = $7 - +HEAPF64[$3 >> 3];
327+
$69 = $1;
328+
$1 = $8 - +HEAPF64[($3 + 8 | 0) >> 3];
329+
$84 = $6 * $6 + $1 * $1;
330+
$1 = $9 - +HEAPF64[($3 + 16 | 0) >> 3];
331+
$1 = $69 - $10 * +HEAPF64[($3 + 48 | 0) >> 3] / Math_sqrt($84 + $1 * $1);
330332
$0 = $0 + 1 | 0;
331-
continue continue_1;
332-
}
333-
break continue_1;
334-
} while (1);
335-
$3 = $3 + 1 | 0;
336-
continue continue_0;
337-
}
338-
break continue_0;
339-
} while (1);
333+
continue repeat_1;
334+
break repeat_1;
335+
} while (1);
336+
};
337+
$2 = $2 + 1 | 0;
338+
continue repeat_0;
339+
break repeat_0;
340+
} while (1);
341+
};
340342
return +$1;
341343
}
342344

@@ -348,14 +350,15 @@ function asmFunc(global, env, buffer) {
348350
function assembly_index_bench($0) {
349351
$0 = $0 | 0;
350352
var $1 = 0;
351-
continue_0 : do {
352-
if ($1 >>> 0 < $0 >>> 0) {
353+
break_0 : {
354+
repeat_0 : do {
355+
if ($1 >>> 0 >= $0 >>> 0) break break_0;
353356
assembly_index_NBodySystem_advance(assembly_index_system | 0, +(.01));
354357
$1 = $1 + 1 | 0;
355-
continue continue_0;
356-
}
357-
break continue_0;
358-
} while (1);
358+
continue repeat_0;
359+
break repeat_0;
360+
} while (1);
361+
};
359362
}
360363

361364
function start() {
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)