Build Wasm record members where their parent keeps them
zx2wasm reads a large record's fields where the record lies instead of copying it whole, gives record parameters and results the caller's storage, and leaves out constructor stores that are overwritten before use. A member, call result or returned record is now built directly in the parent field, caller result or join where it ends up, and a returned load copies once, from its pointer. Kryon's default theme was copied record by record on every lookup; its constructors now copy nothing. A host call that only reads a []T argument no longer copies its elements back when they are unchanged. The Wasm fixture covers snapshots across stores, calls and edges, by-value aliasing, members that read the field they replace, a parent made after its member, joins, and read-only host views.
This commit is contained in:
1 parent
ebd19223fa
commit
6a066abdb2
6 files changed
+821
-37
No files matched your search
@@ -4,6 +4,9 @@
|
||||
|
||||
- Native x86-64 code builds the record a function returns where its caller keeps the result instead of in a frame object it then copies: when every return gives the same record, that record and the members built in it lie in the caller's result memory, which the function keeps the address of in `r14` for its whole body (saved and never allocated), so the return has nothing to copy. A record returned on two different paths, a parameter, or a parameter with a member changed keeps its copy. The machine IR gains a `result_register` flag on functions and placement into a function's `ResultAddress` object, and both verifiers check them.
|
||||
- Native x86-64 code builds a record member where its parent keeps it instead of in a temporary it then copies from: members of a nested literal, a member inserted into a built record, one replaced through a constant index and a call result used as a member take frame space inside the parent's, and a member that is also read, shared, updated afterwards or whose parent is read while it is built keeps its own. An aggregate build whose members are all replaced before it is read emits nothing, and the optimizer folds f64-to-f32 and f32-to-f64 constant conversions and float negations to the hardware's patterns (NaNs stay). A nested shadow-token builder shaped like Kryon's `DefaultShadows` shrinks from 1759 to 661 bytes of code. The machine IR gains placed stack objects (`placed_in`, `placed_at`), and the allocated verifier checks each lies inside its parent.
|
||||
- Wasm code reads a large record's fields where the record lies instead of copying it whole, gives record parameters and results the caller's storage, builds a member, call result or returned record where its parent or caller keeps it, and leaves out constructor stores that are overwritten before use. Kryon's widget scenarios no longer copy their theme records on every lookup.
|
||||
- A host can inspect a slice of records containing strings or nested slices under Wasm without requiring a copy back when its values are unchanged. Mutable byte slices still copy their changes back.
|
||||
|
||||
## [0.6.0] - 2026-11-16
|
||||
|
||||
- Ziran is manually safe like C by default: native x86-64, AArch64 and ARMv7 code, Wasm, generated C and the checks Ziran writes into Go carry no defensive runtime checks, so pointers are addresses and indexes are raw. Build with `--checks` to get every bounds check, pointer token and trap as before, or write `#checks` at the top of a module, before a block or statement, or before one index or slice access to check exactly there. The ZX header records how a bundle was built. The portable engine stays checked.
|
||||
|
||||
+61
-1
@@ -1224,6 +1224,59 @@ exec_to_image(ExecHosts *hosts, ExecImage *image, int node_id, int32_t type, con
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Compare value contents, not image offsets: encoding a nested view may
|
||||
* place its unchanged elements elsewhere in the new image. */
|
||||
static int
|
||||
exec_same_image_value(const ExecBundle *bundle, int node_id, int32_t type,
|
||||
const unsigned char *left, int64_t left_size, int64_t left_at,
|
||||
const unsigned char *right, int64_t right_size, int64_t right_at, int depth)
|
||||
{
|
||||
const ExecNode *node = &bundle->nodes[node_id];
|
||||
const LirType *shape = exec_type(bundle, type);
|
||||
int64_t width = exec_size(bundle, type);
|
||||
if(depth > EXEC_DEPTH || width < 0 || left_at < 0 || right_at < 0 ||
|
||||
left_at > left_size || right_at > right_size ||
|
||||
width > left_size - left_at || width > right_size - right_at) return 0;
|
||||
if(node->class == NODE_SCALAR || node->class == NODE_POINTER)
|
||||
return memcmp(left + left_at, right + right_at, (size_t)width) == 0;
|
||||
if(node->class == NODE_RECORD) {
|
||||
if(shape->kind != LirTypeKind_Record || shape->fields.length != node->field_count) return 0;
|
||||
int64_t offset = 0;
|
||||
for(int at = 0; at < node->field_count; at++) {
|
||||
int32_t field = ((const int32_t *)shape->fields.data)[at];
|
||||
if(!exec_same_image_value(bundle, node->field_nodes[at], field,
|
||||
left, left_size, left_at + offset, right, right_size, right_at + offset, depth + 1)) return 0;
|
||||
offset += exec_size(bundle, field);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if(node->class == NODE_ARRAY) {
|
||||
if(shape->kind != LirTypeKind_Array) return 0;
|
||||
int64_t element = exec_size(bundle, shape->element);
|
||||
for(int64_t at = 0; at < shape->count; at++)
|
||||
if(!exec_same_image_value(bundle, node->child, shape->element,
|
||||
left, left_size, left_at + at * element,
|
||||
right, right_size, right_at + at * element, depth + 1)) return 0;
|
||||
return 1;
|
||||
}
|
||||
if((node->class != NODE_STRING && node->class != NODE_SLICE) || width != 16) return 0;
|
||||
int32_t element = exec_view_element(bundle, type);
|
||||
int64_t element_width = element >= 0 ? exec_size(bundle, element) : 0;
|
||||
if(element_width <= 0) return 0;
|
||||
uint64_t a = exec_read(left + left_at, 8), b = exec_read(right + right_at, 8);
|
||||
uint64_t count = exec_read(left + left_at + 8, 8);
|
||||
if(count != exec_read(right + right_at + 8, 8) || a > (uint64_t)left_size ||
|
||||
b > (uint64_t)right_size || count > (uint64_t)((left_size - (int64_t)a) / element_width) ||
|
||||
count > (uint64_t)((right_size - (int64_t)b) / element_width)) return 0;
|
||||
if(node->class == NODE_STRING)
|
||||
return memcmp(left + a, right + b, (size_t)count) == 0;
|
||||
for(uint64_t at = 0; at < count; at++)
|
||||
if(!exec_same_image_value(bundle, node->child, element,
|
||||
left, left_size, (int64_t)(a + at * (uint64_t)element_width),
|
||||
right, right_size, (int64_t)(b + at * (uint64_t)element_width), depth + 1)) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* The new image of a []T argument the host may have changed, appended to
|
||||
* hosts->back: the image it was given with each element written again, as
|
||||
* the semantic VM copies a host's slice elements back. */
|
||||
@@ -1311,7 +1364,14 @@ ExecHostsCall(ExecHosts *hosts, size_t index, ExecHostArgument *arguments, size_
|
||||
for(size_t at = 0; at < count; at++)
|
||||
if(bundle->nodes[host->parameters[at]].class == NODE_SLICE) {
|
||||
if(!exec_copy_back(hosts, host->parameters[at], &arguments[at], &values[at], &backs[at])) goto done;
|
||||
arguments[at].changed = 1;
|
||||
/* Reading a slice does not change its elements. In particular,
|
||||
* a view inside an element needs no copy-back when the host kept
|
||||
* it intact. Report only images that actually differ. */
|
||||
size_t size = hosts->back.length - backs[at];
|
||||
arguments[at].changed = !exec_same_image_value(bundle, host->parameters[at], arguments[at].type,
|
||||
arguments[at].bytes, (int64_t)arguments[at].size, 0,
|
||||
hosts->back.bytes + backs[at], (int64_t)size, 0, 0);
|
||||
if(!arguments[at].changed) hosts->back.length = backs[at];
|
||||
}
|
||||
const LirFunction *declared = &((const LirFunction *)bundle->program.functions.data)[host->function];
|
||||
int32_t type = declared->result;
|
||||
|
||||
+566
-3
@@ -791,8 +791,10 @@ typedef struct Fn {
|
||||
* slot. */
|
||||
Addr *view;
|
||||
int64_t *view_root;
|
||||
/* An insert of a view back where it was read: nothing to do. */
|
||||
/* An insert of a view back where it was read, or an unused constructor. */
|
||||
unsigned char *elided;
|
||||
/* Initial members overwritten along a single-use insert chain. */
|
||||
unsigned char **omitted;
|
||||
int64_t *alloca_slot;
|
||||
int64_t frame;
|
||||
int64_t scratch;
|
||||
@@ -1009,7 +1011,8 @@ offset_fits(Fn *fn, int64_t offset)
|
||||
static void
|
||||
copy_fixed(Fn *fn, Addr destination, Addr source, int64_t size)
|
||||
{
|
||||
if(size <= 0)
|
||||
if(size <= 0 || (destination.kind == source.kind && destination.local == source.local &&
|
||||
destination.offset == source.offset))
|
||||
return;
|
||||
if(size > 64 || !offset_fits(fn, destination.offset + size) || !offset_fits(fn, source.offset + size)) {
|
||||
push_full(fn, destination);
|
||||
@@ -1503,6 +1506,99 @@ same_member(const LirInstruction *left, int64_t left_fixed, const LirInstruction
|
||||
return left->immediate == right->immediate;
|
||||
}
|
||||
|
||||
/* A load used only for member reads can borrow its pointer instead of
|
||||
* materializing the whole record. Track nested extracts too: a read after
|
||||
* any possible memory effect needs the original snapshot. This deliberately
|
||||
* stays within one block and keeps checked loads and escaping values. */
|
||||
static int
|
||||
plan_load_views(Fn *fn, const unsigned char *aggregate)
|
||||
{
|
||||
int64_t count = fn->function->values.length;
|
||||
size_t items = (size_t)(count ? count : 1);
|
||||
int32_t *root = malloc(items * sizeof(*root));
|
||||
int64_t *home = malloc(items * sizeof(*home)), *stamp = calloc(items, sizeof(*stamp));
|
||||
unsigned char *valid = calloc(items, 1), *used = calloc(items, 1);
|
||||
int ok = root != NULL && home != NULL && stamp != NULL && valid != NULL && used != NULL;
|
||||
if(!ok) {
|
||||
DiagnosticOutOfMemory();
|
||||
fn->lower->failed = 1;
|
||||
goto done;
|
||||
}
|
||||
for(int64_t value = 0; value < count; value++) root[value] = -1;
|
||||
const LirBlock *blocks = fn->function->blocks.data;
|
||||
for(int64_t block = 0; block < fn->block_count; block++) {
|
||||
const LirInstruction *instructions = blocks[block].instructions.data;
|
||||
for(int64_t item = 0; item < blocks[block].instructions.length; item++) {
|
||||
const LirInstruction *instruction = &instructions[item];
|
||||
int32_t value = instruction->result;
|
||||
if(instruction->op == LirOp_Load && value_valid(fn, value) && aggregate[value] &&
|
||||
instruction->operands.length == 1 && !instruction->checked) {
|
||||
root[value] = value;
|
||||
home[value] = block;
|
||||
valid[value] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int64_t block = 0; block < fn->block_count; block++) {
|
||||
const LirInstruction *instructions = blocks[block].instructions.data;
|
||||
int64_t version = 0;
|
||||
for(int64_t item = 0; item < blocks[block].instructions.length; item++) {
|
||||
const LirInstruction *instruction = &instructions[item];
|
||||
int32_t result = instruction->result;
|
||||
if(value_valid(fn, result) && root[result] == result) stamp[result] = version;
|
||||
const int32_t *operands = instruction->operands.data;
|
||||
for(int64_t at = 0; at < instruction->operands.length; at++) {
|
||||
int32_t value = operands[at];
|
||||
if(!value_valid(fn, value) || root[value] < 0) continue;
|
||||
int32_t base = root[value];
|
||||
if(instruction->op != LirOp_Extract || at != 0 || home[base] != block || stamp[base] != version) {
|
||||
valid[base] = 0;
|
||||
continue;
|
||||
}
|
||||
used[base] = 1;
|
||||
if(value_valid(fn, result) && aggregate[result] && fn->view[result].kind >= 0)
|
||||
root[result] = base;
|
||||
}
|
||||
const LirEdge *edges = instruction->edges.data;
|
||||
for(int64_t edge = 0; edge < instruction->edges.length; edge++) {
|
||||
const int32_t *arguments = edges[edge].arguments.data;
|
||||
for(int64_t at = 0; at < edges[edge].arguments.length; at++)
|
||||
if(value_valid(fn, arguments[at]) && root[arguments[at]] >= 0)
|
||||
valid[root[arguments[at]]] = 0;
|
||||
}
|
||||
switch(instruction->op) {
|
||||
case LirOp_Store: case LirOp_Copy: case LirOp_Zero: case LirOp_Resize:
|
||||
case LirOp_Allocate: case LirOp_Call: case LirOp_CallIndirect:
|
||||
case LirOp_HostCall: case LirOp_Insert: version++; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int64_t block = 0; block < fn->block_count; block++) {
|
||||
const LirInstruction *instructions = blocks[block].instructions.data;
|
||||
for(int64_t item = 0; item < blocks[block].instructions.length; item++) {
|
||||
const LirInstruction *instruction = &instructions[item];
|
||||
int32_t value = instruction->result;
|
||||
if(instruction->op != LirOp_Load || !value_valid(fn, value) ||
|
||||
root[value] != value || !valid[value] || !used[value]) continue;
|
||||
int32_t pointer = ((const int32_t *)instruction->operands.data)[0];
|
||||
int64_t slot = fn->value_slot[value];
|
||||
fn->view[value] = (Addr){ADDR_LOCAL, (uint32_t)fn->value_local[pointer], 0};
|
||||
fn->view_root[value] = -1 - value;
|
||||
for(int64_t nested = 0; nested < count; nested++) {
|
||||
if(fn->view[nested].kind != ADDR_FRAME || fn->view_root[nested] != slot) continue;
|
||||
fn->view[nested].kind = ADDR_LOCAL;
|
||||
fn->view[nested].local = (uint32_t)fn->value_local[pointer];
|
||||
fn->view[nested].offset -= slot;
|
||||
fn->view_root[nested] = -1 - value;
|
||||
}
|
||||
}
|
||||
}
|
||||
done:
|
||||
free(root); free(home); free(stamp); free(valid); free(used);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* Views: an aggregate extract read only by extracts in its own block,
|
||||
* while nothing writes the slot it reads from, is read in place there
|
||||
* instead of copied out. One that a single insert then changes, put back
|
||||
@@ -1639,6 +1735,453 @@ done:
|
||||
return ok && !fn->lower->failed;
|
||||
}
|
||||
|
||||
/* Change a coalesced frame slot's base without disturbing the offsets of
|
||||
* its nested read views. Dynamic views compute their address at the extract. */
|
||||
static void
|
||||
place_slot(Fn *fn, const unsigned char *aggregate, int64_t slot, Addr place)
|
||||
{
|
||||
for(int64_t value = 0; value < fn->function->values.length; value++) {
|
||||
if(!aggregate[value]) continue;
|
||||
if(fn->view[value].kind < 0 && fn->value_slot[value] == slot) {
|
||||
fn->view[value] = place;
|
||||
fn->view_root[value] = slot;
|
||||
} else if(fn->view[value].kind == ADDR_FRAME && fn->view_root[value] == slot) {
|
||||
fn->view[value].kind = place.kind;
|
||||
fn->view[value].local = place.local;
|
||||
fn->view[value].offset += place.offset - slot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Arguments are caller-owned SSA snapshots. A parameter whose coalesced
|
||||
* slot is never written can borrow that snapshot for this call. Aggregate
|
||||
* results with a single destination class can be built in the caller's
|
||||
* result area; calls always keep their result separate from their inputs. */
|
||||
static void
|
||||
plan_call_places(Fn *fn, const unsigned char *aggregate)
|
||||
{
|
||||
const LirBlock *blocks = fn->function->blocks.data;
|
||||
const LirBlock *entry = &blocks[fn->function->entry];
|
||||
const int32_t *parameters = entry->parameters.data;
|
||||
uint32_t first = type_aggregate(fn->lower, fn->function->result) ? 1 : 0;
|
||||
for(int64_t at = 0; at < entry->parameters.length; at++) {
|
||||
int32_t parameter = parameters[at];
|
||||
if(!aggregate[parameter]) continue;
|
||||
int64_t slot = fn->value_slot[parameter];
|
||||
int written = 0;
|
||||
for(int64_t block = 0; block < fn->block_count && !written; block++) {
|
||||
const int32_t *inputs = blocks[block].parameters.data;
|
||||
for(int64_t input = 0; input < blocks[block].parameters.length; input++) {
|
||||
int32_t value = inputs[input];
|
||||
if(value != parameter && aggregate[value] && fn->value_slot[value] == slot) written = 1;
|
||||
}
|
||||
const LirInstruction *instructions = blocks[block].instructions.data;
|
||||
for(int64_t item = 0; item < blocks[block].instructions.length && !written; item++) {
|
||||
int32_t value = instructions[item].result;
|
||||
if(value_valid(fn, value) && aggregate[value] && fn->value_slot[value] == slot &&
|
||||
fn->view[value].kind < 0) written = 1;
|
||||
}
|
||||
}
|
||||
if(!written) place_slot(fn, aggregate, slot, (Addr){ADDR_LOCAL, first + (uint32_t)at, 0});
|
||||
}
|
||||
if(!first) return;
|
||||
int64_t slot = -1;
|
||||
for(int64_t block = 0; block < fn->block_count; block++) {
|
||||
const LirInstruction *instructions = blocks[block].instructions.data;
|
||||
for(int64_t item = 0; item < blocks[block].instructions.length; item++) {
|
||||
const LirInstruction *instruction = &instructions[item];
|
||||
if(instruction->op != LirOp_Return || instruction->operands.length != 1) continue;
|
||||
int32_t value = ((const int32_t *)instruction->operands.data)[0];
|
||||
if(!aggregate[value] || fn->view[value].kind >= 0 ||
|
||||
(slot >= 0 && slot != fn->value_slot[value])) return;
|
||||
slot = fn->value_slot[value];
|
||||
}
|
||||
}
|
||||
if(slot < 0) return;
|
||||
/* Loads and extracts use view[] to mean an elided read, whereas a
|
||||
* constructor still writes its destination. Keep read results in their
|
||||
* own slots so placement cannot turn a required snapshot into a view. */
|
||||
for(int64_t block = 0; block < fn->block_count; block++) {
|
||||
const LirInstruction *instructions = blocks[block].instructions.data;
|
||||
for(int64_t item = 0; item < blocks[block].instructions.length; item++) {
|
||||
const LirInstruction *instruction = &instructions[item];
|
||||
int32_t value = instruction->result;
|
||||
if((instruction->op == LirOp_Load || instruction->op == LirOp_Extract) &&
|
||||
value_valid(fn, value) && aggregate[value] && fn->value_slot[value] == slot) return;
|
||||
}
|
||||
}
|
||||
place_slot(fn, aggregate, slot, (Addr){ADDR_LOCAL, fn->result_address, 0});
|
||||
}
|
||||
|
||||
/* Constructor fields overwritten before any other use need no initial
|
||||
* store. Follow only single-use, fixed-member inserts: a snapshot, extract
|
||||
* or block argument ends the chain. Prune nested constructors whose only
|
||||
* uses were those omitted stores, keeping calls and checked operations. */
|
||||
static int
|
||||
plan_initial_stores(Fn *fn)
|
||||
{
|
||||
int64_t count = fn->function->values.length;
|
||||
size_t items = (size_t)(count ? count : 1);
|
||||
const LirInstruction **definitions = calloc(items, sizeof(*definitions));
|
||||
const LirInstruction **only = calloc(items, sizeof(*only));
|
||||
int64_t *uses = calloc(items, sizeof(*uses));
|
||||
int32_t *queue = malloc(items * sizeof(*queue));
|
||||
fn->omitted = calloc(items, sizeof(*fn->omitted));
|
||||
int ok = definitions != NULL && only != NULL && uses != NULL && queue != NULL && fn->omitted != NULL;
|
||||
if(!ok) goto failed;
|
||||
const LirBlock *blocks = fn->function->blocks.data;
|
||||
for(int64_t block = 0; block < fn->block_count; block++) {
|
||||
const LirInstruction *instructions = blocks[block].instructions.data;
|
||||
for(int64_t item = 0; item < blocks[block].instructions.length; item++) {
|
||||
const LirInstruction *instruction = &instructions[item];
|
||||
if(value_valid(fn, instruction->result)) definitions[instruction->result] = instruction;
|
||||
const int32_t *operands = instruction->operands.data;
|
||||
for(int64_t at = 0; at < instruction->operands.length; at++) {
|
||||
int32_t value = operands[at];
|
||||
if(value_valid(fn, value)) only[value] = ++uses[value] == 1 ? instruction : NULL;
|
||||
}
|
||||
const LirEdge *edges = instruction->edges.data;
|
||||
for(int64_t edge = 0; edge < instruction->edges.length; edge++) {
|
||||
const int32_t *arguments = edges[edge].arguments.data;
|
||||
for(int64_t at = 0; at < edges[edge].arguments.length; at++) {
|
||||
int32_t value = arguments[at];
|
||||
if(value_valid(fn, value)) { uses[value]++; only[value] = NULL; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int64_t value = 0; value < count; value++) {
|
||||
const LirInstruction *definition = definitions[value];
|
||||
if(definition == NULL || definition->op != LirOp_Aggregate) continue;
|
||||
int32_t current = (int32_t)value;
|
||||
for(int64_t walk = 0; walk < count && uses[current] == 1 && only[current] != NULL; walk++) {
|
||||
const LirInstruction *use = only[current];
|
||||
const int32_t *operands = use->operands.data;
|
||||
if(use->op != LirOp_Insert || use->operands.length != 2 || operands[0] != current ||
|
||||
use->immediate < 0 || use->immediate >= definition->operands.length ||
|
||||
!value_valid(fn, use->result)) break;
|
||||
if(fn->omitted[value] == NULL) {
|
||||
fn->omitted[value] = calloc((size_t)definition->operands.length, 1);
|
||||
if(fn->omitted[value] == NULL) { ok = 0; goto failed; }
|
||||
}
|
||||
fn->omitted[value][use->immediate] = 1;
|
||||
current = use->result;
|
||||
}
|
||||
if(fn->omitted[value] != NULL) {
|
||||
const int32_t *operands = definition->operands.data;
|
||||
for(int64_t at = 0; at < definition->operands.length; at++)
|
||||
if(fn->omitted[value][at] && value_valid(fn, operands[at])) uses[operands[at]]--;
|
||||
}
|
||||
}
|
||||
{
|
||||
int64_t head = 0, tail = 0;
|
||||
for(int64_t value = 0; value < count; value++)
|
||||
if(uses[value] == 0 && definitions[value] != NULL && definitions[value]->op == LirOp_Aggregate) {
|
||||
fn->elided[value] = 1;
|
||||
queue[tail++] = (int32_t)value;
|
||||
}
|
||||
while(head < tail) {
|
||||
int32_t value = queue[head++];
|
||||
const LirInstruction *definition = definitions[value];
|
||||
const int32_t *operands = definition->operands.data;
|
||||
for(int64_t at = 0; at < definition->operands.length; at++) {
|
||||
int32_t source = operands[at];
|
||||
if((fn->omitted[value] != NULL && fn->omitted[value][at]) || !value_valid(fn, source)) continue;
|
||||
if(--uses[source] == 0 && !fn->elided[source] && definitions[source] != NULL &&
|
||||
definitions[source]->op == LirOp_Aggregate) {
|
||||
fn->elided[source] = 1;
|
||||
queue[tail++] = source;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
failed:
|
||||
if(!ok) { DiagnosticOutOfMemory(); fn->lower->failed = 1; }
|
||||
free(definitions); free(only); free(uses); free(queue);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* The storage a value occupies: its frame slot, or the root of the place
|
||||
* it views. Results placed for the caller use MEMBER_RESULT_ROOT. */
|
||||
#define MEMBER_RESULT_ROOT INT64_MIN
|
||||
|
||||
static int64_t
|
||||
storage_root(Fn *fn, int32_t value)
|
||||
{
|
||||
return fn->view[value].kind >= 0 ? fn->view_root[value] : fn->value_slot[value];
|
||||
}
|
||||
|
||||
static int
|
||||
same_place(Addr left, Addr right)
|
||||
{
|
||||
return left.kind == right.kind && left.local == right.local && left.offset == right.offset;
|
||||
}
|
||||
|
||||
/* Checks instructions [from, to) of one block before a member class moves
|
||||
* into `root`. Class values may only feed in-place inserts of the class
|
||||
* (counted in *chained) or, when `join` names a block, argument `parameter`
|
||||
* of an edge there (counted in *tails). No other instruction may read the
|
||||
* root's storage or define a value living in it. */
|
||||
static int
|
||||
member_range(Fn *fn, const LirInstruction *instructions, int64_t from, int64_t to, int32_t class, int64_t root,
|
||||
const unsigned char *aggregate, const int32_t *rep, int64_t join, int64_t parameter,
|
||||
int64_t *chained, int64_t *tails)
|
||||
{
|
||||
for(int64_t item = from; item < to; item++) {
|
||||
const LirInstruction *between = &instructions[item];
|
||||
int32_t result = between->result;
|
||||
if(value_valid(fn, result) && aggregate[result] && rep[result] != class && storage_root(fn, result) == root)
|
||||
return 0;
|
||||
const int32_t *inputs = between->operands.data;
|
||||
for(int64_t at = 0; at < between->operands.length; at++) {
|
||||
int32_t value = inputs[at];
|
||||
if(!value_valid(fn, value) || !aggregate[value]) continue;
|
||||
if(rep[value] == class) {
|
||||
if(between->op != LirOp_Insert || at != 0 || !value_valid(fn, result) || rep[result] != class)
|
||||
return 0;
|
||||
(*chained)++;
|
||||
} else if(storage_root(fn, value) == root) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
const LirEdge *edges = between->edges.data;
|
||||
for(int64_t edge = 0; edge < between->edges.length; edge++) {
|
||||
const int32_t *arguments = edges[edge].arguments.data;
|
||||
for(int64_t at = 0; at < edges[edge].arguments.length; at++) {
|
||||
int32_t value = arguments[at];
|
||||
if(!value_valid(fn, value) || !aggregate[value]) continue;
|
||||
if(rep[value] == class) {
|
||||
if(join < 0 || edges[edge].block != join || at != parameter)
|
||||
return 0;
|
||||
(*tails)++;
|
||||
} else if(storage_root(fn, value) == root) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Builds a member where its parent keeps it, as native code does. A class
|
||||
* of values (a constructor or call and the inserts that update it in place)
|
||||
* whose last value only fills one member of a parent, by an in-place insert
|
||||
* or the parent's constructor, or is the return value, is written at that
|
||||
* destination directly. The whole class lives in the block before that use,
|
||||
* and nothing in between reads the destination's storage, so writing early
|
||||
* is unobservable. Loads and extracts keep their own slots, because a view
|
||||
* on them means an elided read; a returned load instead reads from its
|
||||
* pointer, so the return copies once. */
|
||||
static int
|
||||
plan_member_places(Fn *fn, const unsigned char *aggregate, const int32_t *rep)
|
||||
{
|
||||
if(fn->async)
|
||||
return 1;
|
||||
int64_t count = fn->function->values.length;
|
||||
size_t items = (size_t)(count ? count : 1);
|
||||
const LirInstruction **definition = calloc(items, sizeof(*definition));
|
||||
int64_t *uses = calloc(items, sizeof(*uses)), *home = malloc(items * sizeof(*home));
|
||||
int64_t *position = malloc(items * sizeof(*position));
|
||||
int32_t *next = malloc(items * sizeof(*next)), *head = malloc(items * sizeof(*head));
|
||||
int ok = definition != NULL && uses != NULL && home != NULL && position != NULL && next != NULL && head != NULL;
|
||||
if(!ok) {
|
||||
DiagnosticOutOfMemory();
|
||||
fn->lower->failed = 1;
|
||||
goto done;
|
||||
}
|
||||
for(int64_t value = 0; value < count; value++) {
|
||||
head[value] = next[value] = -1;
|
||||
home[value] = position[value] = -1;
|
||||
}
|
||||
for(int64_t value = count - 1; value >= 0; value--)
|
||||
if(aggregate[value]) {
|
||||
next[value] = head[rep[value]];
|
||||
head[rep[value]] = (int32_t)value;
|
||||
}
|
||||
const LirBlock *blocks = fn->function->blocks.data;
|
||||
for(int64_t block = 0; block < fn->block_count; block++) {
|
||||
const LirInstruction *instructions = blocks[block].instructions.data;
|
||||
for(int64_t item = 0; item < blocks[block].instructions.length; item++) {
|
||||
const LirInstruction *instruction = &instructions[item];
|
||||
if(value_valid(fn, instruction->result)) {
|
||||
definition[instruction->result] = instruction;
|
||||
home[instruction->result] = block;
|
||||
position[instruction->result] = item;
|
||||
}
|
||||
const int32_t *operands = instruction->operands.data;
|
||||
for(int64_t at = 0; at < instruction->operands.length; at++)
|
||||
if(value_valid(fn, operands[at])) uses[operands[at]]++;
|
||||
const LirEdge *edges = instruction->edges.data;
|
||||
for(int64_t edge = 0; edge < instruction->edges.length; edge++) {
|
||||
const int32_t *arguments = edges[edge].arguments.data;
|
||||
for(int64_t at = 0; at < edges[edge].arguments.length; at++)
|
||||
if(value_valid(fn, arguments[at])) uses[arguments[at]]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
int returns_aggregate = type_aggregate(fn->lower, fn->function->result);
|
||||
for(int64_t block = 0; block < fn->block_count; block++) {
|
||||
const LirInstruction *instructions = blocks[block].instructions.data;
|
||||
for(int64_t use = 0; use < blocks[block].instructions.length; use++) {
|
||||
const LirInstruction *instruction = &instructions[use];
|
||||
const int32_t *operands = instruction->operands.data;
|
||||
int32_t members[64];
|
||||
Addr places[64];
|
||||
int64_t roots[64];
|
||||
int64_t candidates = 0;
|
||||
if(instruction->op == LirOp_Insert && instruction->operands.length == 2) {
|
||||
int32_t base = operands[0], member = operands[1], result = instruction->result;
|
||||
int64_t offset;
|
||||
int32_t member_type;
|
||||
if(!value_valid(fn, base) || !value_valid(fn, member) || !value_valid(fn, result) ||
|
||||
!aggregate[base] || !aggregate[member] || rep[base] == rep[member] ||
|
||||
!same_place(addr_of_slot(fn, result), addr_of_slot(fn, base)) ||
|
||||
storage_root(fn, result) != storage_root(fn, base) || storage_root(fn, result) < 0 ||
|
||||
!member_offset(fn, value_type(fn, base), instruction->immediate, &offset, &member_type))
|
||||
continue;
|
||||
places[0] = addr_of_slot(fn, result);
|
||||
places[0].offset += offset;
|
||||
roots[0] = storage_root(fn, result);
|
||||
members[candidates++] = member;
|
||||
} else if(instruction->op == LirOp_Aggregate && value_valid(fn, instruction->result) &&
|
||||
aggregate[instruction->result] && storage_root(fn, instruction->result) >= 0) {
|
||||
int32_t result = instruction->result;
|
||||
for(int64_t at = 0; at < instruction->operands.length && candidates < 64; at++) {
|
||||
int32_t member = operands[at];
|
||||
int64_t offset;
|
||||
int32_t member_type;
|
||||
if(!value_valid(fn, member) || !aggregate[member] || rep[member] == rep[result] ||
|
||||
!member_offset(fn, value_type(fn, result), (uint64_t)at, &offset, &member_type))
|
||||
continue;
|
||||
places[candidates] = addr_of_slot(fn, result);
|
||||
places[candidates].offset += offset;
|
||||
roots[candidates] = storage_root(fn, result);
|
||||
members[candidates++] = member;
|
||||
}
|
||||
} else if(instruction->op == LirOp_Return && returns_aggregate && instruction->operands.length == 1 &&
|
||||
value_valid(fn, operands[0]) && aggregate[operands[0]] && fn->view[operands[0]].kind < 0) {
|
||||
int32_t member = operands[0];
|
||||
const LirInstruction *loaded = definition[member];
|
||||
/* A returned load reads from its pointer: one copy. */
|
||||
if(loaded != NULL && loaded->op == LirOp_Load) {
|
||||
int32_t pointer = loaded->operands.length == 1 ? ((const int32_t *)loaded->operands.data)[0] : -1;
|
||||
if(!fn->lower->program->checks && !loaded->checked && uses[member] == 1 &&
|
||||
home[member] == block && position[member] == use - 1 &&
|
||||
head[rep[member]] == member && next[member] < 0 &&
|
||||
value_valid(fn, pointer) && value_scalar(fn, pointer) && fn->value_local[pointer] >= 0) {
|
||||
fn->view[member] = (Addr){ADDR_LOCAL, (uint32_t)fn->value_local[pointer], 0};
|
||||
fn->view_root[member] = -1 - member;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
places[0] = (Addr){ADDR_LOCAL, fn->result_address, 0};
|
||||
roots[0] = MEMBER_RESULT_ROOT;
|
||||
members[candidates++] = member;
|
||||
}
|
||||
for(int64_t candidate = 0; candidate < candidates; candidate++) {
|
||||
int32_t member = members[candidate];
|
||||
Addr place = places[candidate];
|
||||
int64_t root = roots[candidate];
|
||||
if(fn->view[member].kind >= 0 || uses[member] != 1 || !offset_fits(fn, place.offset))
|
||||
continue;
|
||||
int32_t class = rep[member];
|
||||
int64_t slot = fn->value_slot[member], first = use, size = 0, join = -1, outside = 0;
|
||||
const int32_t *parameters = blocks[block].parameters.data;
|
||||
int good = 1;
|
||||
for(int32_t value = head[class]; value >= 0 && good; value = next[value]) {
|
||||
const LirInstruction *defined = definition[value];
|
||||
const int32_t *inputs = defined != NULL ? defined->operands.data : NULL;
|
||||
size++;
|
||||
good = fn->view[value].kind < 0 && !fn->elided[value] && (value == member || uses[value] == 1);
|
||||
if(!good)
|
||||
break;
|
||||
if(defined == NULL) {
|
||||
/* One parameter of this block may join the values
|
||||
* its predecessors build. */
|
||||
int64_t found = -1;
|
||||
for(int64_t at = 0; at < blocks[block].parameters.length; at++)
|
||||
if(parameters[at] == value) found = at;
|
||||
good = found >= 0 && join < 0;
|
||||
join = found;
|
||||
continue;
|
||||
}
|
||||
good = defined->op == LirOp_Aggregate || defined->op == LirOp_Call ||
|
||||
defined->op == LirOp_CallIndirect ||
|
||||
(defined->op == LirOp_Insert && defined->operands.length >= 2 &&
|
||||
value_valid(fn, inputs[0]) && rep[inputs[0]] == class);
|
||||
if(good && home[value] == block) {
|
||||
good = position[value] < use;
|
||||
if(position[value] < first) first = position[value];
|
||||
} else {
|
||||
outside++;
|
||||
}
|
||||
}
|
||||
if(!good || (outside > 0 && join < 0))
|
||||
continue;
|
||||
/* Members already built inside this class move with it,
|
||||
* so they start its range too. */
|
||||
for(int64_t value = 0; value < count && good; value++)
|
||||
if(aggregate[value] && fn->view[value].kind == ADDR_FRAME && fn->view_root[value] == slot) {
|
||||
good = join < 0 && home[value] == block && position[value] < use;
|
||||
if(good && position[value] < first) first = position[value];
|
||||
}
|
||||
int64_t chained = 0, tails = 0;
|
||||
if(good && join >= 0) {
|
||||
/* Each edge here is its block's only edge and carries a
|
||||
* class value built in that block; the parameter exists
|
||||
* from this block's start. */
|
||||
int64_t covered = 0;
|
||||
first = 0;
|
||||
for(int64_t from = 0; from < fn->block_count && good; from++) {
|
||||
const LirInstruction *code = blocks[from].instructions.data;
|
||||
int64_t length = blocks[from].instructions.length, start = length, enters = 0;
|
||||
for(int64_t item = 0; item < length; item++) {
|
||||
const LirEdge *edges = code[item].edges.data;
|
||||
for(int64_t edge = 0; edge < code[item].edges.length; edge++)
|
||||
if(edges[edge].block == block)
|
||||
enters = enters >= 0 && item == length - 1 && code[item].edges.length == 1 ? 1 : -1;
|
||||
}
|
||||
if(enters == 0)
|
||||
continue;
|
||||
const LirEdge *edge = code[length - 1].edges.data;
|
||||
good = enters > 0 && from != block && edge->arguments.length > join;
|
||||
if(!good)
|
||||
break;
|
||||
int32_t argument = ((const int32_t *)edge->arguments.data)[join];
|
||||
good = value_valid(fn, argument) && rep[argument] == class && home[argument] == from;
|
||||
for(int32_t value = head[class]; value >= 0 && good; value = next[value])
|
||||
if(definition[value] != NULL && home[value] == from) {
|
||||
covered++;
|
||||
if(position[value] < start) start = position[value];
|
||||
}
|
||||
good = good && member_range(fn, code, start, length, class, root, aggregate, rep,
|
||||
block, join, &chained, &tails);
|
||||
}
|
||||
good = good && covered == outside;
|
||||
}
|
||||
good = good && member_range(fn, instructions, first, use, class, root, aggregate, rep,
|
||||
-1, -1, &chained, &tails);
|
||||
if(!good || chained + tails != size - 1)
|
||||
continue;
|
||||
for(int64_t value = 0; value < count; value++)
|
||||
if(aggregate[value] && fn->view[value].kind == ADDR_FRAME && fn->view_root[value] == slot) {
|
||||
fn->view[value].kind = place.kind;
|
||||
fn->view[value].local = place.local;
|
||||
fn->view[value].offset += place.offset - slot;
|
||||
fn->view_root[value] = root;
|
||||
}
|
||||
for(int32_t value = head[class]; value >= 0; value = next[value]) {
|
||||
fn->view[value] = place;
|
||||
fn->view_root[value] = root;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
done:
|
||||
free(definition); free(uses); free(home); free(position); free(next); free(head);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static void lower_instruction(Fn *fn, int64_t block, const LirInstruction *instruction, int64_t slot);
|
||||
|
||||
/* Emits positions start..last: a loop when `loop`, with a block label for
|
||||
@@ -2126,7 +2669,8 @@ lower_aggregate(Fn *fn, const LirInstruction *instruction)
|
||||
for(int64_t index = 0; index < instruction->operands.length; index++) {
|
||||
Addr place = base;
|
||||
place.offset += at;
|
||||
store_value(fn, place, operands[index]);
|
||||
if(fn->omitted[instruction->result] == NULL || !fn->omitted[instruction->result][index])
|
||||
store_value(fn, place, operands[index]);
|
||||
at += type_size(fn->lower, value_type(fn, operands[index]));
|
||||
}
|
||||
}
|
||||
@@ -2222,6 +2766,7 @@ lower_memory(Fn *fn, const LirInstruction *instruction)
|
||||
if(instruction->op == LirOp_Load) {
|
||||
if(!operand_count(fn, instruction, 1, 1)) return;
|
||||
if(!value_scalar(fn, operands[0])) { WasmGap(fn->lower, "wasm.gap.load", "address is not a pointer value"); return; }
|
||||
if(fn->view[instruction->result].kind >= 0) return;
|
||||
load_value(fn, (Addr){ADDR_LOCAL, (uint32_t)fn->value_local[operands[0]], 0}, instruction->result);
|
||||
return;
|
||||
}
|
||||
@@ -2640,6 +3185,7 @@ lower_instruction(Fn *fn, int64_t block, const LirInstruction *instruction, int6
|
||||
{
|
||||
Lower *lower = fn->lower;
|
||||
fn->instruction = instruction;
|
||||
if(value_valid(fn, instruction->result) && fn->elided[instruction->result]) return;
|
||||
const int32_t *operands = instruction->operands.data;
|
||||
const LirEdge *edges = instruction->edges.data;
|
||||
switch(instruction->op) {
|
||||
@@ -3087,6 +3633,20 @@ lower_function(Lower *lower, int64_t index, Buf *output)
|
||||
if(aggregate[value] && rep[value] != value)
|
||||
fn.value_slot[value] = fn.value_slot[rep[value]];
|
||||
if(!plan_views(&fn, aggregate)) goto done;
|
||||
if(!lower->program->checks && !plan_load_views(&fn, aggregate)) goto done;
|
||||
plan_call_places(&fn, aggregate);
|
||||
if(!plan_member_places(&fn, aggregate, rep)) goto done;
|
||||
if(!plan_initial_stores(&fn)) goto done;
|
||||
/* Borrowed loads, parameters and results need no stack reservation.
|
||||
* Keep offsets stable, but discard unused trailing slots; a procedure
|
||||
* whose aggregates all live elsewhere then needs no frame at all. */
|
||||
fn.frame = 0;
|
||||
for(int64_t value = 0; value < value_count; value++) {
|
||||
if(!aggregate[value]) continue;
|
||||
Addr place = addr_of_slot(&fn, (int32_t)value);
|
||||
int64_t end = place.offset + type_size(lower, values[value]);
|
||||
if(place.kind == ADDR_FRAME && end > fn.frame) fn.frame = end;
|
||||
}
|
||||
for(int64_t value = 0; value < value_count; value++)
|
||||
fn.proven[value] = compiler_lowered_proven_LirProvenIndex(&lower->proven, index, (int32_t)value);
|
||||
int64_t at = 0;
|
||||
@@ -3289,6 +3849,9 @@ done:
|
||||
free(fn.view);
|
||||
free(fn.view_root);
|
||||
free(fn.elided);
|
||||
if(fn.omitted != NULL)
|
||||
for(int64_t value = 0; value < value_count; value++) free(fn.omitted[value]);
|
||||
free(fn.omitted);
|
||||
free(fn.proven);
|
||||
free(fn.alloca_slot);
|
||||
free(aggregate);
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// A read-only host call with strings inside slice elements must work on both engines.
|
||||
host_api :: #system_library "host_api";
|
||||
Node :: struct {
|
||||
value: s32
|
||||
children: []Node
|
||||
}
|
||||
Grow :: (tree: Node) -> Node #foreign host_api;
|
||||
Label :: struct { name: string; weight: s32; tags: []string }
|
||||
InspectRows :: (rows: []Label, marks: []u8) -> s32 #foreign host_api;
|
||||
labels: [2]Label;
|
||||
tags: [2]string = .["alpha", "beta"];
|
||||
tree: Node;
|
||||
leaves: [2]Node;
|
||||
ring: [1]Node;
|
||||
#program_export
|
||||
Answer :: () -> s32 {
|
||||
labels[0] = Label.{"left", 2, tags[0:1]}
|
||||
labels[1] = Label.{"right", 3, tags[1:2]}
|
||||
marks: [4]u8
|
||||
if InspectRows(labels[:], marks[1:3]) != 5 { return 8 }
|
||||
if marks[0] != 0 || marks[1] != 88 || marks[2] != 89 || marks[3] != 0 { return 10 }
|
||||
if labels[0].name != "left" || labels[1].name != "right" || labels[0].weight != 2 || labels[1].weight != 3 { return 9 }
|
||||
if labels[0].tags.count != 1 || labels[1].tags.count != 1 || labels[0].tags[0] != "alpha" || labels[1].tags[0] != "beta" { return 11 }
|
||||
leaves[0].value = 2
|
||||
leaves[1].value = 3
|
||||
tree.value = 1
|
||||
tree.children = leaves[:]
|
||||
tree = Grow(tree)
|
||||
if tree.value != 6 || tree.children.count != 2 { return 1 }
|
||||
if tree.children[0].value != 100 || tree.children[1].children.count != 1 { return 2 }
|
||||
return tree.children[1].children[0].value
|
||||
}
|
||||
#program_export
|
||||
Cycle :: () -> s32 {
|
||||
ring[0].value = 1
|
||||
ring[0].children = ring[:]
|
||||
return Grow(ring[0]).value
|
||||
}
|
||||
@@ -28,8 +28,137 @@ Pair :: struct {
|
||||
tag: u8
|
||||
}
|
||||
|
||||
ConstructorViews :: () -> bool {
|
||||
partial := Point.{x = 17}
|
||||
snapshot := partial
|
||||
partial.x = 31
|
||||
swapped := Point.{x = snapshot.y, y = snapshot.x}
|
||||
nested := Pair.{a = Point.{x = swapped.y, y = 23}, tag = 5}
|
||||
return partial.x == 31 && partial.y == 0 && snapshot.x == 17 && snapshot.y == 0 &&
|
||||
swapped.x == 0 && nested.a.x == 17 && nested.a.y == 23 && nested.tag == 5
|
||||
}
|
||||
|
||||
// Members built where their parent keeps them: call results, nested
|
||||
// constructors and returned values. A member that reads the parent it
|
||||
// replaces, or a call given the parent, must still see the old contents.
|
||||
Layer :: struct { offset: float32; blur: float32; spread: float32; alpha: float32 }
|
||||
Layers :: struct { rest: Layer; raised: [2]Layer }
|
||||
Tokens :: struct { dark: bool; layers: Layers; corner: Pair; series: [3]u32 }
|
||||
member_tokens: Tokens;
|
||||
member_ready: bool;
|
||||
MakeLayers :: (dark: bool) -> Layers {
|
||||
layers: Layers
|
||||
layers.rest = Layer.{1.0, 2.0, 0.0, 0.25}
|
||||
if dark { layers.raised[0] = Layer.{8.0, 22.0, 0.0, 0.5} }
|
||||
else { layers.raised[1] = Layer.{6.0, 18.0, 0.0, 0.75} }
|
||||
return layers
|
||||
}
|
||||
MakeCorner :: (seed: Pair) -> Pair { return Pair.{a = Point.{x = seed.a.y, y = seed.a.x}, tag = seed.tag + 1} }
|
||||
MakeTokens :: (dark: bool) -> Tokens {
|
||||
tokens: Tokens
|
||||
tokens.dark = dark
|
||||
tokens.layers = MakeLayers(dark)
|
||||
tokens.corner = MakeCorner(Pair.{a = Point.{x = 3, y = 4}, tag = 6})
|
||||
tokens.series = .{cast(u32)7, cast(u32)8, cast(u32)9}
|
||||
return tokens
|
||||
}
|
||||
CurrentTokens :: () -> Tokens {
|
||||
if !member_ready { return MakeTokens(false) }
|
||||
return member_tokens
|
||||
}
|
||||
MemberPlaces :: () -> bool {
|
||||
first := CurrentTokens()
|
||||
if first.dark || first.layers.rest.alpha != 0.25 || first.layers.raised[1].blur != 18.0 ||
|
||||
first.layers.raised[0].blur != 0.0 || first.corner.a.x != 4 || first.corner.a.y != 3 ||
|
||||
first.corner.tag != 7 || first.series[2] != 9 { return false }
|
||||
member_tokens = MakeTokens(true)
|
||||
member_ready = true
|
||||
second := CurrentTokens()
|
||||
if !second.dark || second.layers.raised[0].alpha != 0.5 || second.layers.raised[1].alpha != 0.0 { return false }
|
||||
// The replacement reads the member it replaces.
|
||||
second.corner = MakeCorner(second.corner)
|
||||
if second.corner.a.x != 3 || second.corner.a.y != 4 || second.corner.tag != 8 { return false }
|
||||
second.corner = Pair.{a = Point.{x = second.corner.a.y, y = second.corner.a.x}, tag = second.corner.tag}
|
||||
if second.corner.a.x != 4 || second.corner.a.y != 3 || second.corner.tag != 8 { return false }
|
||||
second.layers.raised[1] = second.layers.raised[0]
|
||||
second.layers.raised[0] = Layer.{second.layers.raised[1].blur, second.layers.rest.blur, 1.0, 0.125}
|
||||
if second.layers.raised[0].offset != 22.0 || second.layers.raised[0].blur != 2.0 ||
|
||||
second.layers.raised[1].offset != 8.0 { return false }
|
||||
// A parent made after its member, and also kept as a snapshot.
|
||||
inner := Point.{x = 5, y = 6}
|
||||
outer: Pair
|
||||
kept := outer
|
||||
outer.a = inner
|
||||
if outer.a.x != 5 || outer.a.y != 6 || kept.a.x != 0 || kept.a.y != 0 { return false }
|
||||
// Members chosen by a condition join in the block that stores them,
|
||||
// including one that reads the member it replaces.
|
||||
joined: Pair
|
||||
joined.a = ifx second.dark then MixPoints(inner, outer.a) else Point.{x = 1, y = 2}
|
||||
if joined.a.x != 11 || joined.a.y != 11 { return false }
|
||||
joined.a = ifx second.dark then MixPoints(joined.a, inner) else joined.a
|
||||
if joined.a.x != 17 || joined.a.y != 16 { return false }
|
||||
member_ready = false
|
||||
return member_tokens.dark && member_tokens.corner.tag == 7 && CurrentTokens().series[0] == 7
|
||||
}
|
||||
|
||||
// Pointer-backed aggregate member reads must preserve snapshots across
|
||||
// stores, calls, block edges and nested extracts. Independent pointers must
|
||||
// retain distinct bases even when their aggregate frame slots coalesce.
|
||||
FieldRecord :: struct { padding: [32768]s32; value: s32 }
|
||||
field_left: FieldRecord;
|
||||
field_right: FieldRecord;
|
||||
FieldRead :: (record: *FieldRecord) -> s32 { return record.value }
|
||||
FieldPair :: (left: *FieldRecord, right: *FieldRecord, index: s64) -> s32 {
|
||||
return left.padding[index] + right.padding[index] + left.value + right.value
|
||||
}
|
||||
FieldWrite :: () { field_left.value = 23 }
|
||||
FieldByValue :: (record: FieldRecord) -> FieldRecord {
|
||||
record.value += 3
|
||||
return record
|
||||
}
|
||||
FieldNestedByValue :: (record: FieldRecord) -> s32 {
|
||||
record.padding[42] += 4
|
||||
return record.padding[42]
|
||||
}
|
||||
FieldObserve :: (record: FieldRecord, alias: *FieldRecord) -> s32 {
|
||||
alias.value = 29
|
||||
return record.value
|
||||
}
|
||||
FieldSnapshot :: (record: *FieldRecord, choose: bool) -> s32 {
|
||||
snapshot := <<record
|
||||
record.value = 19
|
||||
if choose { return snapshot.value }
|
||||
return snapshot.padding[42]
|
||||
}
|
||||
FieldViews :: () -> bool {
|
||||
field_left.value = 7
|
||||
field_right.value = 5
|
||||
field_left.padding[42] = 3
|
||||
field_right.padding[42] = 10
|
||||
if FieldRead(*field_left) != 7 || FieldPair(*field_left, *field_right, 42) != 25 { return false }
|
||||
argument := field_left
|
||||
changed_parameter := FieldByValue(argument)
|
||||
if changed_parameter.value != 10 || argument.value != 7 || field_left.value != 7 { return false }
|
||||
if FieldNestedByValue(argument) != 7 || argument.padding[42] != 3 { return false }
|
||||
if FieldObserve(field_left, *field_left) != 7 || field_left.value != 29 { return false }
|
||||
field_left.value = 7
|
||||
snapshot := field_left
|
||||
nested := snapshot.padding
|
||||
field_left.padding[42] = 17
|
||||
if nested[42] != 3 { return false }
|
||||
call_snapshot := field_left
|
||||
FieldWrite()
|
||||
if call_snapshot.value != 7 || field_left.value != 23 { return false }
|
||||
if FieldSnapshot(*field_left, true) != 23 { return false }
|
||||
if FieldSnapshot(*field_left, false) != 17 { return false }
|
||||
changed := field_left
|
||||
changed.padding[42] = 31
|
||||
return changed.padding[42] == 31 && field_left.padding[42] == 17
|
||||
}
|
||||
|
||||
#program_export
|
||||
Answer :: () -> s64 {
|
||||
if !FieldViews() || !ConstructorViews() || !MemberPlaces() { return -1 }
|
||||
total: s64 = 0
|
||||
|
||||
u: u16 = 65535
|
||||
|
||||
+24
-33
@@ -315,36 +315,8 @@ ZI
|
||||
"$zi2zx" --root "$work" --entry state:Main -o "$work/state.zx" "$work/state.zi"
|
||||
# A record that holds itself through a slice crosses both ways; one
|
||||
# that holds itself stops at the nesting bound.
|
||||
cat >"$work/tree.zi" <<'ZI'
|
||||
host_api :: #system_library "host_api";
|
||||
Node :: struct {
|
||||
value: s32
|
||||
children: []Node
|
||||
}
|
||||
Grow :: (tree: Node) -> Node #foreign host_api;
|
||||
tree: Node;
|
||||
leaves: [2]Node;
|
||||
ring: [1]Node;
|
||||
#program_export
|
||||
Answer :: () -> s32 {
|
||||
leaves[0].value = 2
|
||||
leaves[1].value = 3
|
||||
tree.value = 1
|
||||
tree.children = leaves[:]
|
||||
tree = Grow(tree)
|
||||
if tree.value != 6 || tree.children.count != 2 { return 1 }
|
||||
if tree.children[0].value != 100 || tree.children[1].children.count != 1 { return 2 }
|
||||
return tree.children[1].children[0].value
|
||||
}
|
||||
#program_export
|
||||
Cycle :: () -> s32 {
|
||||
ring[0].value = 1
|
||||
ring[0].children = ring[:]
|
||||
return Grow(ring[0]).value
|
||||
}
|
||||
ZI
|
||||
"$ziran" bundle --root "$work" --entry tree:Answer -o "$work/tree.zx" "$work/tree.zi"
|
||||
"$ziran" bundle --root "$work" --entry tree:Cycle -o "$work/cycle.zx" "$work/tree.zi"
|
||||
"$ziran" bundle --root "$repo/tests/spec" --entry zx_wasm_host_views:Answer -o "$work/tree.zx" "$repo/tests/spec/zx_wasm_host_views.zi"
|
||||
"$ziran" bundle --root "$repo/tests/spec" --entry zx_wasm_host_views:Cycle -o "$work/cycle.zx" "$repo/tests/spec/zx_wasm_host_views.zi"
|
||||
cat >"$work/tier.c" <<'C'
|
||||
#include "ziran_host.h"
|
||||
#include "zir_zx_wasm_platform.h"
|
||||
@@ -403,6 +375,22 @@ static int call(void *context, const char *module, const char *function,
|
||||
result->kind = HOST_INTEGER; result->integer = 7;
|
||||
} else if(!strcmp(function, "AddHost")) {
|
||||
result->kind = HOST_INTEGER; result->integer = args[0].integer + 1000;
|
||||
} else if(!strcmp(function, "InspectRows")) {
|
||||
if(count != 2 || args[0].kind != HOST_SLICE || args[0].length != 2 ||
|
||||
args[1].kind != HOST_SLICE || args[1].length != 2) return 0;
|
||||
long long sum = 0;
|
||||
const char *names[2] = {"left", "right"};
|
||||
for(size_t at = 0; at < 2; at++) {
|
||||
const HostValue *row = &args[0].elements[at];
|
||||
if(row->kind != HOST_RECORD || row->field_count != 3) return 0;
|
||||
const HostValue *name = &row->fields[0].value;
|
||||
if(name->kind != HOST_STRING || name->length != strlen(names[at]) ||
|
||||
memcmp(name->data, names[at], name->length)) return 0;
|
||||
sum += row->fields[1].value.integer;
|
||||
}
|
||||
for(size_t at = 0; at < 2; at++)
|
||||
args[1].elements[at].bits = (uint64_t)(args[1].elements[at].integer = 88 + (int64_t)at);
|
||||
result->kind = HOST_INTEGER; result->integer = sum;
|
||||
} else if(!strcmp(function, "Grow")) {
|
||||
/* The given tree's sum over leaves 100 and 200, the second with a
|
||||
* leaf 7 of its own. */
|
||||
@@ -468,9 +456,12 @@ static void scenario(const char *engine, BundleBackendPolicy policy,
|
||||
BundleInstanceClose(moved);
|
||||
BundleInstanceClose(beside);
|
||||
BundleInstanceClose(first);
|
||||
HostBinding grow = {.module = "tree", .function = "Grow", .call = call};
|
||||
BundleInstance *grown = BundleInstantiateWithOptions(tree, &grow, 1, &options);
|
||||
BundleInstance *ring = BundleInstantiateWithOptions(cycle, &grow, 1, &options);
|
||||
HostBinding forest[2] = {
|
||||
{.module = "zx_wasm_host_views", .function = "Grow", .call = call},
|
||||
{.module = "zx_wasm_host_views", .function = "InspectRows", .call = call},
|
||||
};
|
||||
BundleInstance *grown = BundleInstantiateWithOptions(tree, forest, 2, &options);
|
||||
BundleInstance *ring = BundleInstantiateWithOptions(cycle, forest, 2, &options);
|
||||
long long spent = -1;
|
||||
int has = 0, cycled = ring != NULL && BundleInstanceRun(ring, &spent, &has);
|
||||
printf("%s tree %lld cycle %s\n", engine, run(grown), cycled ? "ran" : "failed");
|
||||
|
||||
Reference in new issue
Block a user