Skip to content

Commit 8e439fe

Browse files
committed
dataify
1 parent bb4cea3 commit 8e439fe

File tree

6 files changed

+102
-101
lines changed

6 files changed

+102
-101
lines changed

src/builtins.ts

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,6 @@ export namespace BuiltinNames {
577577
export const INFO = "~lib/diagnostics/INFO";
578578

579579
// std/memory.ts
580-
export const memory = "~lib/memory/memory";
581580
export const memory_size = "~lib/memory/memory.size";
582581
export const memory_grow = "~lib/memory/memory.grow";
583582
export const memory_copy = "~lib/memory/memory.copy";
@@ -2476,67 +2475,6 @@ builtins.set(BuiltinNames.unreachable, builtin_unreachable);
24762475

24772476
// === Memory =================================================================================
24782477

2479-
// memory(size[, align]) -> usize
2480-
function builtin_memory(ctx: BuiltinContext): ExpressionRef {
2481-
var compiler = ctx.compiler;
2482-
var module = compiler.module;
2483-
compiler.currentType = Type.i32;
2484-
if (
2485-
checkTypeAbsent(ctx) |
2486-
checkArgsOptional(ctx, 1, 2)
2487-
) return module.unreachable();
2488-
var operands = ctx.operands;
2489-
var numOperands = operands.length;
2490-
var usizeType = compiler.options.usizeType;
2491-
var arg0 = compiler.precomputeExpression(operands[0], Type.i32, Constraints.CONV_IMPLICIT);
2492-
compiler.currentType = usizeType;
2493-
if (getExpressionId(arg0) != ExpressionId.Const) {
2494-
compiler.error(
2495-
DiagnosticCode.Expression_must_be_a_compile_time_constant,
2496-
operands[0].range
2497-
);
2498-
return module.unreachable();
2499-
}
2500-
var size = getConstValueI32(arg0);
2501-
if (size < 1) {
2502-
compiler.error(
2503-
DiagnosticCode._0_must_be_a_value_between_1_and_2_inclusive,
2504-
operands[0].range, "1", i32.MAX_VALUE.toString()
2505-
);
2506-
return module.unreachable();
2507-
}
2508-
var align = 16;
2509-
if (numOperands == 2) {
2510-
align = evaluateImmediateOffset(operands[1], compiler);
2511-
compiler.currentType = usizeType;
2512-
if (align < 0) {
2513-
return module.unreachable();
2514-
}
2515-
if (align < 1 || align > 16) {
2516-
compiler.error(
2517-
DiagnosticCode._0_must_be_a_value_between_1_and_2_inclusive,
2518-
operands[1].range, "Alignment", "1", "16"
2519-
);
2520-
return module.unreachable();
2521-
}
2522-
if (!isPowerOf2(align)) {
2523-
compiler.error(
2524-
DiagnosticCode._0_must_be_a_power_of_two,
2525-
operands[1].range, "Alignment"
2526-
);
2527-
return module.unreachable();
2528-
}
2529-
}
2530-
// FIXME: what if recompiles happen? recompiles are bad.
2531-
var offset = compiler.addMemorySegment(new Uint8Array(size), align).offset;
2532-
if (usizeType == Type.usize32) {
2533-
assert(!i64_high(offset));
2534-
return module.i32(i64_low(offset));
2535-
}
2536-
return module.i64(i64_low(offset), i64_high(offset));
2537-
}
2538-
builtins.set(BuiltinNames.memory, builtin_memory);
2539-
25402478
// memory.size() -> i32
25412479
function builtin_memory_size(ctx: BuiltinContext): ExpressionRef {
25422480
var compiler = ctx.compiler;
@@ -2618,6 +2556,67 @@ function builtin_memory_fill(ctx: BuiltinContext): ExpressionRef {
26182556
}
26192557
builtins.set(BuiltinNames.memory_fill, builtin_memory_fill);
26202558

2559+
// memory.data(size[, align]) -> usize
2560+
function builtin_memory_data(ctx: BuiltinContext): ExpressionRef {
2561+
var compiler = ctx.compiler;
2562+
var module = compiler.module;
2563+
compiler.currentType = Type.i32;
2564+
if (
2565+
checkTypeAbsent(ctx) |
2566+
checkArgsOptional(ctx, 1, 2)
2567+
) return module.unreachable();
2568+
var operands = ctx.operands;
2569+
var numOperands = operands.length;
2570+
var usizeType = compiler.options.usizeType;
2571+
var arg0 = compiler.precomputeExpression(operands[0], Type.i32, Constraints.CONV_IMPLICIT);
2572+
compiler.currentType = usizeType;
2573+
if (getExpressionId(arg0) != ExpressionId.Const) {
2574+
compiler.error(
2575+
DiagnosticCode.Expression_must_be_a_compile_time_constant,
2576+
operands[0].range
2577+
);
2578+
return module.unreachable();
2579+
}
2580+
var size = getConstValueI32(arg0);
2581+
if (size < 1) {
2582+
compiler.error(
2583+
DiagnosticCode._0_must_be_a_value_between_1_and_2_inclusive,
2584+
operands[0].range, "1", i32.MAX_VALUE.toString()
2585+
);
2586+
return module.unreachable();
2587+
}
2588+
var align = 16;
2589+
if (numOperands == 2) {
2590+
align = evaluateImmediateOffset(operands[1], compiler);
2591+
compiler.currentType = usizeType;
2592+
if (align < 0) {
2593+
return module.unreachable();
2594+
}
2595+
if (align < 1 || align > 16) {
2596+
compiler.error(
2597+
DiagnosticCode._0_must_be_a_value_between_1_and_2_inclusive,
2598+
operands[1].range, "Alignment", "1", "16"
2599+
);
2600+
return module.unreachable();
2601+
}
2602+
if (!isPowerOf2(align)) {
2603+
compiler.error(
2604+
DiagnosticCode._0_must_be_a_power_of_two,
2605+
operands[1].range, "Alignment"
2606+
);
2607+
return module.unreachable();
2608+
}
2609+
}
2610+
// FIXME: what if recompiles happen? recompiles are bad.
2611+
var offset = compiler.addMemorySegment(new Uint8Array(size), align).offset;
2612+
if (usizeType == Type.usize32) {
2613+
assert(!i64_high(offset));
2614+
return module.i32(i64_low(offset));
2615+
}
2616+
return module.i64(i64_low(offset), i64_high(offset));
2617+
}
2618+
builtins.set(BuiltinNames.memory_data, builtin_memory_data);
2619+
26212620
// === Helpers ================================================================================
26222621

26232622
// changetype<T!>(value: *) -> T

std/assembly/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,8 +1126,6 @@ declare function bswap16<T = i8 | u8 | i16 | u16 | i32 | u32>(value: T): T;
11261126

11271127
// Standard library
11281128

1129-
/** Gets a pointer to a static chunk of memory of the given size. */
1130-
declare function memory(size: i32, align?: i32): usize;
11311129
/** Memory operations. */
11321130
declare namespace memory {
11331131
/** Whether the memory managed interface is implemented. */
@@ -1148,6 +1146,8 @@ declare namespace memory {
11481146
export function drop(segmentIndex: u32): void;
11491147
/** Compares two chunks of memory. Returns `0` if equal, otherwise the difference of the first differing bytes. */
11501148
export function compare(vl: usize, vr: usize, n: usize): i32;
1149+
/** Gets a pointer to a static chunk of memory of the given size. */
1150+
export function data(size: i32, align?: i32): usize;
11511151
}
11521152

11531153
/** Garbage collector interface. */

std/assembly/memory.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { memcmp, memmove, memset } from "./util/memory";
22
import { E_NOTIMPLEMENTED } from "./util/error";
33

4-
/** Gets a pointer to a static chunk of memory of the given size. */
5-
// @ts-ignore: decorator
6-
@builtin
7-
export declare function memory(size: i32, align?: i32): usize;
8-
94
/** Memory manager interface. */
105
export namespace memory {
116

@@ -65,4 +60,9 @@ export namespace memory {
6560
export function compare(vl: usize, vr: usize, n: usize): i32 {
6661
return memcmp(vl, vr, n);
6762
}
63+
64+
/** Gets a pointer to a static chunk of memory of the given size. */
65+
// @ts-ignore: decorator
66+
@builtin
67+
export declare function data(size: i32, align?: i32): usize;
6868
}

tests/compiler/memory.optimized.wat

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
if
2626
i32.const 0
2727
i32.const 1056
28-
i32.const 10
28+
i32.const 12
2929
i32.const 1
3030
call $~lib/builtins/abort
3131
unreachable
@@ -36,7 +36,7 @@
3636
if
3737
i32.const 0
3838
i32.const 1056
39-
i32.const 11
39+
i32.const 13
4040
i32.const 1
4141
call $~lib/builtins/abort
4242
unreachable
@@ -47,7 +47,7 @@
4747
if
4848
i32.const 0
4949
i32.const 1056
50-
i32.const 12
50+
i32.const 14
5151
i32.const 1
5252
call $~lib/builtins/abort
5353
unreachable
@@ -62,7 +62,7 @@
6262
if
6363
i32.const 0
6464
i32.const 1056
65-
i32.const 17
65+
i32.const 19
6666
i32.const 1
6767
call $~lib/builtins/abort
6868
unreachable
@@ -77,7 +77,7 @@
7777
if
7878
i32.const 0
7979
i32.const 1056
80-
i32.const 18
80+
i32.const 20
8181
i32.const 1
8282
call $~lib/builtins/abort
8383
unreachable
@@ -92,7 +92,7 @@
9292
if
9393
i32.const 0
9494
i32.const 1056
95-
i32.const 19
95+
i32.const 21
9696
i32.const 1
9797
call $~lib/builtins/abort
9898
unreachable
@@ -107,7 +107,7 @@
107107
if
108108
i32.const 0
109109
i32.const 1056
110-
i32.const 20
110+
i32.const 22
111111
i32.const 1
112112
call $~lib/builtins/abort
113113
unreachable
@@ -122,7 +122,7 @@
122122
if
123123
i32.const 0
124124
i32.const 1056
125-
i32.const 21
125+
i32.const 23
126126
i32.const 1
127127
call $~lib/builtins/abort
128128
unreachable
@@ -145,7 +145,7 @@
145145
if
146146
i32.const 0
147147
i32.const 1056
148-
i32.const 34
148+
i32.const 36
149149
i32.const 1
150150
call $~lib/builtins/abort
151151
unreachable
@@ -160,7 +160,7 @@
160160
if
161161
i32.const 0
162162
i32.const 1056
163-
i32.const 35
163+
i32.const 37
164164
i32.const 1
165165
call $~lib/builtins/abort
166166
unreachable

tests/compiler/memory.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
// === memory.data ===
2+
13
// Should be static
24

35
function test(): i32 {
4-
const ptr = memory(4);
6+
const ptr = memory.data(4);
57
var value = load<i32>(ptr);
68
store<i32>(ptr, value + 1);
79
return value;
@@ -13,21 +15,21 @@ assert(test() == 2);
1315

1416
// Should be properly aligned
1517

16-
var ptr = memory(16, 16);
17-
assert(ptr + 16 == (ptr = memory(1, 16)));
18-
assert(ptr + 8 == (ptr = memory(1, 8)));
19-
assert(ptr + 4 == (ptr = memory(1, 4)));
20-
assert(ptr + 2 == (ptr = memory(1, 2)));
21-
assert(ptr + 1 == (ptr = memory(1, 1)));
18+
var ptr = memory.data(16, 16);
19+
assert(ptr + 16 == (ptr = memory.data(1, 16)));
20+
assert(ptr + 8 == (ptr = memory.data(1, 8)));
21+
assert(ptr + 4 == (ptr = memory.data(1, 4)));
22+
assert(ptr + 2 == (ptr = memory.data(1, 2)));
23+
assert(ptr + 1 == (ptr = memory.data(1, 1)));
2224

2325
// Should be static and properly aligned per generic instance
2426

2527
function testGeneric<T>(): usize {
26-
const ptr = memory(1, 1 << alignof<T>());
28+
const ptr = memory.data(1, 1 << alignof<T>());
2729
return ptr;
2830
}
2931

30-
ptr = memory(16, 16);
32+
ptr = memory.data(16, 16);
3133
assert(ptr + 16 == (ptr = testGeneric<v128>()));
3234
assert(ptr + 8 == (ptr = testGeneric<i64>()));
3335
assert(ptr + 4 == (ptr = testGeneric<i32>()));

0 commit comments

Comments
 (0)