Skip to content

Commit a13224e

Browse files
author
jonas.ong
committed
add comments to constants
1 parent 0cc98ee commit a13224e

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/wasm-compiler/constants.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,22 @@ export const HEAP_PTR = "$_heap_pointer";
3131
export const CURR_ENV = "$_current_env";
3232

3333
// boxing functions
34+
35+
// store directly in payload
3436
export const MAKE_INT_FX = wasm
3537
.func("$_make_int")
3638
.params({ $value: i64 })
3739
.results(i32, i64)
3840
.body(i32.const(TYPE_TAG.INT), local.get("$value"));
3941

42+
// reinterpret bits as int
4043
export const MAKE_FLOAT_FX = wasm
4144
.func("$_make_float")
4245
.params({ $value: f64 })
4346
.results(i32, i64)
4447
.body(i32.const(TYPE_TAG.FLOAT), i64.reinterpret_f64(local.get("$value")));
4548

49+
// upper 32: pointer to f64 real part; lower 32: pointer to f64 imaginary part
4650
export const MAKE_COMPLEX_FX = wasm
4751
.func("$_make_complex")
4852
.params({ $real: f64, $img: f64 })
@@ -57,6 +61,7 @@ export const MAKE_COMPLEX_FX = wasm
5761
global.set(HEAP_PTR, i32.add(global.get(HEAP_PTR), i32.const(16)))
5862
);
5963

64+
// store directly as i32
6065
export const MAKE_BOOL_FX = wasm
6166
.func("$_make_bool")
6267
.params({ $value: i32 })
@@ -96,6 +101,8 @@ export const MAKE_CLOSURE_FX = wasm
96101
export const MAKE_NONE_FX = wasm.func("$_make_none").results(i32, i64).body(i32.const(TYPE_TAG.NONE), i64.const(0));
97102

98103
// pair-related functions
104+
105+
// upper 32: pointer to head; lower 32: pointer to tail
99106
export const MAKE_PAIR_FX = wasm
100107
.func("$_make_pair")
101108
.params({ $tag1: i32, $val1: i64, $tag2: i32, $val2: i64 })
@@ -273,6 +280,7 @@ export const ARITHMETIC_OP_FX = wasm
273280
.results(i32, i64)
274281
.locals({ $a: f64, $b: f64, $c: f64, $d: f64, $denom: f64 })
275282
.body(
283+
// if adding, check if both are strings
276284
wasm
277285
.if(
278286
i32.and(
@@ -284,7 +292,7 @@ export const ARITHMETIC_OP_FX = wasm
284292
)
285293
)
286294
.then(
287-
global.get(HEAP_PTR),
295+
global.get(HEAP_PTR), // starting address of new string
288296

289297
memory.copy(
290298
global.get(HEAP_PTR),
@@ -303,9 +311,11 @@ export const ARITHMETIC_OP_FX = wasm
303311
wasm.return(wasm.call(MAKE_STRING_FX).args())
304312
),
305313

314+
// if either's bool, convert to int
306315
wasm.if(i32.eq(local.get("$x_tag"), i32.const(TYPE_TAG.BOOL))).then(local.set("$x_tag", i32.const(TYPE_TAG.INT))),
307316
wasm.if(i32.eq(local.get("$y_tag"), i32.const(TYPE_TAG.BOOL))).then(local.set("$y_tag", i32.const(TYPE_TAG.INT))),
308317

318+
// if both int, use int instr (except for division: use float)
309319
wasm
310320
.if(
311321
i32.and(
@@ -327,6 +337,7 @@ export const ARITHMETIC_OP_FX = wasm
327337
)
328338
),
329339

340+
// else, if either's int, convert to float and set float locals
330341
wasm
331342
.if(i32.eq(local.get("$x_tag"), i32.const(TYPE_TAG.INT)))
332343
.then(local.set("$a", f64.convert_i64_s(local.get("$x_val"))), local.set("$x_tag", i32.const(TYPE_TAG.FLOAT)))
@@ -337,6 +348,7 @@ export const ARITHMETIC_OP_FX = wasm
337348
.then(local.set("$c", f64.convert_i64_s(local.get("$y_val"))), local.set("$y_tag", i32.const(TYPE_TAG.FLOAT)))
338349
.else(local.set("$c", f64.reinterpret_i64(local.get("$y_val")))),
339350

351+
// if both float, use float instr
340352
wasm
341353
.if(
342354
i32.and(
@@ -354,14 +366,14 @@ export const ARITHMETIC_OP_FX = wasm
354366
)
355367
),
356368

369+
// else, if either's complex, load from mem, set locals (default 0)
357370
wasm
358371
.if(i32.eq(local.get("$x_tag"), i32.const(TYPE_TAG.FLOAT)))
359372
.then(local.set("$x_tag", i32.const(TYPE_TAG.COMPLEX)))
360373
.else(
361374
local.set("$a", f64.load(i32.wrap_i64(local.get("$x_val")))),
362375
local.set("$b", f64.load(i32.add(i32.wrap_i64(local.get("$x_val")), i32.const(8))))
363376
),
364-
365377
wasm
366378
.if(i32.eq(local.get("$y_tag"), i32.const(TYPE_TAG.FLOAT)))
367379
.then(local.set("$y_tag", i32.const(TYPE_TAG.COMPLEX)))
@@ -370,6 +382,7 @@ export const ARITHMETIC_OP_FX = wasm
370382
local.set("$d", f64.load(i32.add(i32.wrap_i64(local.get("$y_val")), i32.const(8))))
371383
),
372384

385+
// if both complex, perform complex operations
373386
wasm
374387
.if(
375388
i32.and(
@@ -390,6 +403,7 @@ export const ARITHMETIC_OP_FX = wasm
390403
.call(MAKE_COMPLEX_FX)
391404
.args(f64.sub(local.get("$a"), local.get("$c")), f64.sub(local.get("$b"), local.get("$d")))
392405
),
406+
// (a+bi)*(c+di) = (ac-bd) + (ad+bc)i
393407
wasm.return(
394408
wasm
395409
.call(MAKE_COMPLEX_FX)
@@ -398,6 +412,7 @@ export const ARITHMETIC_OP_FX = wasm
398412
f64.add(f64.mul(local.get("$b"), local.get("$c")), f64.mul(local.get("$a"), local.get("$d")))
399413
)
400414
),
415+
// (a+bi)/(c+di) = (ac+bd)/(c^2+d^2) + (bc-ad)/(c^2+d^2)i
401416
wasm.return(
402417
wasm
403418
.call(MAKE_COMPLEX_FX)
@@ -418,6 +433,7 @@ export const ARITHMETIC_OP_FX = wasm
418433
)
419434
),
420435

436+
wasm.call("$_log_error").args(i32.const(ERROR_MAP.ARITH_OP_UNKNOWN_TYPE[0])),
421437
wasm.unreachable()
422438
);
423439

0 commit comments

Comments
 (0)