refactor(compiler)!: migrate SDK to minimal storage ISA
Replace retired guards and publication opcodes with SDK checks, neutral TRAP and sequential List/Linked mutations with explicit failure phases. Preserve snapshot cursors and import schema 2 metadata with the delivered provider pin. Include common/source/installed conformance, debug and release evidence, synchronized specs and the consolidated lesson. Runtime checkout remains unchanged.
This commit is contained in:
+4
-78
@@ -1617,89 +1617,15 @@ final class PbsExecutableBodyLowerer {
|
||||
var operation=p.studio.compiler.pbs.PbsSdkCollections.argument(binding,"name");
|
||||
var span=function.span();
|
||||
switch(operation) {
|
||||
case "publishFields" -> {
|
||||
var owners = new java.util.ArrayList<IRRegistry.TypeRef>();
|
||||
int parameter = 0, offset = 0, pops = 0;
|
||||
while (parameter < function.parameters().size()) {
|
||||
var receiver = PbsRuntimeTypes.unwrap(function.parameters().get(parameter++).typeRef());
|
||||
var owner = receiver.kind() == PbsAst.TypeRefKind.OPTIONAL
|
||||
? PbsRuntimeTypes.unwrap(receiver.inner()) : receiver;
|
||||
var declaration = c.types().structs.get(owner.name());
|
||||
if (declaration == null) throw new IllegalArgumentException("Publication destination must be Struct");
|
||||
owners.add(c.types().registryType(owner));
|
||||
if (receiver.kind() == PbsAst.TypeRefKind.OPTIONAL) {
|
||||
var absent = c.nextLabel("absent_publication_destination");
|
||||
var ready = c.nextLabel("publication_destination_ready");
|
||||
optionalPresence(offset, receiver, span, c);
|
||||
emitJump(IRBackendExecutableFunction.InstructionKind.JMP_IF_FALSE, absent, span, c);
|
||||
unwrapOptional(offset, receiver, span, c);
|
||||
emitJump(IRBackendExecutableFunction.InstructionKind.JMP, ready, span, c);
|
||||
emitLabel(absent, span, c);
|
||||
emitDefault(c.types().registryType(owner), 1, span, c);
|
||||
emitLabel(ready, span, c);
|
||||
} else emitGetLocal(offset, span, c);
|
||||
offset += c.types().slots(receiver);
|
||||
pops++;
|
||||
for (var field : declaration.fields()) {
|
||||
if (parameter >= function.parameters().size()) throw new IllegalArgumentException("Missing publication field");
|
||||
var value = function.parameters().get(parameter++).typeRef();
|
||||
if (!c.types().key(value).equals(c.types().key(field.typeRef())))
|
||||
throw new IllegalArgumentException("Publication field type mismatch");
|
||||
int width = c.types().slots(value);
|
||||
emitLoadLocalSlots(offset, width, span, c);
|
||||
packStorage(value, span, c);
|
||||
offset += width;
|
||||
pops += storageWidth(value, c);
|
||||
}
|
||||
}
|
||||
registry(FIELDS_PUBLISH, c.types().publicationSchema(owners), 0, pops, 0, span, c);
|
||||
}
|
||||
case "indexGuard" -> {
|
||||
emitLoadLocalSlots(0,2,span,c);
|
||||
registry(INDEX_GUARD,null,0,2,0,span,c);
|
||||
}
|
||||
case "checkLength" -> {
|
||||
emitGetLocal(0,span,c);
|
||||
registry(ARRAY_CHECK_LENGTH,c.types().arrayStorage(),0,1,0,span,c);
|
||||
case "fail" -> {
|
||||
if (!function.parameters().isEmpty()) throw new IllegalArgumentException("SDK failure binding takes no operands");
|
||||
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.TRAP,"",null,null,span));
|
||||
return;
|
||||
}
|
||||
case "copy" -> {
|
||||
emitLoadLocalSlots(0,5,span,c);
|
||||
registry(ARRAY_COPY,c.types().arrayStorage(),0,5,0,span,c);
|
||||
}
|
||||
case "publish" -> {
|
||||
var parameters=function.parameters();
|
||||
var optional=PbsRuntimeTypes.unwrap(parameters.get(7).typeRef());
|
||||
var payload=optional.inner();
|
||||
var owner=parameters.get(8).typeRef();
|
||||
var declaration=c.types().structs.get(owner.name());
|
||||
var firstName=p.studio.compiler.pbs.PbsSdkCollections.argument(binding,"first");
|
||||
var secondName=p.studio.compiler.pbs.PbsSdkCollections.argument(binding,"second");
|
||||
int first=-1,second=-1;
|
||||
for(int i=0;i<declaration.fields().size();i++) {
|
||||
if(declaration.fields().get(i).name().equals(firstName))first=i;
|
||||
if(declaration.fields().get(i).name().equals(secondName))second=i;
|
||||
}
|
||||
if(first<0||second<0)throw new IllegalArgumentException("Unresolved SDK storage fields");
|
||||
emitLoadLocalSlots(0,7,span,c);
|
||||
optionalPresence(7,optional,span,c);
|
||||
var absent=c.nextLabel("empty_storage_fill");var ready=c.nextLabel("storage_fill_ready");
|
||||
emitJump(IRBackendExecutableFunction.InstructionKind.JMP_IF_FALSE,absent,span,c);
|
||||
unwrapOptional(7,optional,span,c);
|
||||
packStorage(payload,span,c);
|
||||
registry(DYN_PACK,c.types().registryType(payload),0,storageWidth(payload,c),2,span,c);
|
||||
emitJump(IRBackendExecutableFunction.InstructionKind.JMP,ready,span,c);
|
||||
emitLabel(absent,span,c);emitDefault(c.types().dynamic(),2,span,c);
|
||||
emitLabel(ready,span,c);
|
||||
int offset=8,pops=9;
|
||||
for(int i=8;i<parameters.size();i++) {
|
||||
var type=parameters.get(i).typeRef();int width=c.types().slots(type);
|
||||
emitLoadLocalSlots(offset,width,span,c);packStorage(type,span,c);
|
||||
offset+=width;pops+=storageWidth(type,c);
|
||||
}
|
||||
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.REGISTRY,
|
||||
null,"",null,null,null,"","",pops,0,span,
|
||||
new IRRegistryInstruction(STORAGE_PUBLISH,c.types().arrayStorage(),c.types().registryType(owner),null,first,second)));
|
||||
}
|
||||
default -> throw new IllegalArgumentException("Unknown private storage binding: "+operation);
|
||||
}
|
||||
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.RET,"",null,null,span));
|
||||
|
||||
+2
-1
@@ -87,7 +87,7 @@ final class PbsExecutableStackAnalyzer {
|
||||
}
|
||||
outHeight = outHeight - argSlots + retSlots;
|
||||
}
|
||||
case HALT, LABEL, JMP, RET -> {
|
||||
case HALT, TRAP, LABEL, JMP, RET -> {
|
||||
}
|
||||
case JMP_IF_TRUE, JMP_IF_FALSE -> {
|
||||
if (outHeight <= 0) {
|
||||
@@ -99,6 +99,7 @@ final class PbsExecutableStackAnalyzer {
|
||||
|
||||
maxHeight = Math.max(maxHeight, outHeight);
|
||||
if (instruction.kind() == IRBackendExecutableFunction.InstructionKind.RET
|
||||
|| instruction.kind() == IRBackendExecutableFunction.InstructionKind.TRAP
|
||||
|| instruction.kind() == IRBackendExecutableFunction.InstructionKind.HALT) {
|
||||
continue;
|
||||
}
|
||||
|
||||
-9
@@ -138,15 +138,6 @@ final class PbsRuntimeTypes {
|
||||
used.putIfAbsent(ref.declarationKey(), new IRRegistry.TypeDeclaration(ref, null, new IRRegistry.Array(element)));
|
||||
return ref;
|
||||
}
|
||||
IRRegistry.TypeRef publicationSchema(List<IRRegistry.TypeRef> owners) {
|
||||
var ref = new IRRegistry.TypeRef("pbs:field-publication:" + owners.stream()
|
||||
.map(IRRegistry.TypeRef::declarationKey).map(k -> k.length() + ":" + k)
|
||||
.collect(java.util.stream.Collectors.joining()));
|
||||
used.putIfAbsent(ref.declarationKey(), new IRRegistry.TypeDeclaration(ref, null,
|
||||
new IRRegistry.Struct(ReadOnlyList.wrap(owners.stream()
|
||||
.map(owner -> new IRRegistry.NamedType(owner, null)).toList()))));
|
||||
return ref;
|
||||
}
|
||||
IRRegistry.TypeRef builtin(int id) {
|
||||
var ref = IRRegistry.TypeRef.builtin(id);
|
||||
used.putIfAbsent(ref.declarationKey(),new IRRegistry.TypeDeclaration(ref,null,new IRRegistry.Builtin(id)));
|
||||
|
||||
+68
-72
@@ -1,19 +1,24 @@
|
||||
// Source algorithms own state, ranges, capacity policy and snapshot topology.
|
||||
// Only these private, origin-checked helpers bind generic storage instructions.
|
||||
[StoragePrimitive(name = "indexGuard")]
|
||||
fn _bounds(index: long, limit: long) {}
|
||||
[StoragePrimitive(name = "fail")]
|
||||
fn _fail() {}
|
||||
|
||||
[StoragePrimitive(name = "checkLength")]
|
||||
fn _checkLength(length: long) {}
|
||||
fn _bounds(index: long, limit: long) {
|
||||
if index < (0 as long) || index >= limit { _fail(); }
|
||||
}
|
||||
|
||||
fn _insertionBounds(index: long, length: long) {
|
||||
if index < (0 as long) || index > length { _fail(); }
|
||||
}
|
||||
|
||||
fn _checkLength(length: long) {
|
||||
let maximum: long = 4294967295;
|
||||
if length < (0 as long) || length > maximum { _fail(); }
|
||||
}
|
||||
|
||||
[StoragePrimitive(name = "copy")]
|
||||
fn _copy<T>(source: array<T>, sourceStart: long, target: array<T>, targetStart: long, count: long) {}
|
||||
|
||||
[StoragePrimitive(name = "publish", first = "storage", second = "length")]
|
||||
fn _publish<T>(source: array<T>, sourceStart: long, target: array<T>, targetStart: long,
|
||||
count: long, fillStart: long, fillCount: long, fill: optional T,
|
||||
owner: list<T>, storage: array<T>, length: long) {}
|
||||
|
||||
fn _grownCapacity(capacity: long, required: long) -> long {
|
||||
_checkLength(required);
|
||||
let maximum: long = 4294967295;
|
||||
@@ -32,6 +37,7 @@ declare struct list<T>(storage: array<T>, length: long) {
|
||||
this.length = 0;
|
||||
}
|
||||
ctor make(capacity: long) {
|
||||
_checkLength(capacity);
|
||||
this.storage = new array<T>(capacity);
|
||||
this.length = 0;
|
||||
}
|
||||
@@ -51,58 +57,75 @@ declare struct list<T>(storage: array<T>, length: long) {
|
||||
|
||||
fn add(value: T) { this.insert(this.length, value); }
|
||||
fn insert(index: long, value: T) {
|
||||
_bounds(index, this.length + 1);
|
||||
_insertionBounds(index, this.length);
|
||||
let required: long = this.length + 1;
|
||||
_checkLength(required);
|
||||
let target: array<T> = this.storage;
|
||||
if required > target.length {
|
||||
target = new array<T>(_grownCapacity(target.length, required));
|
||||
_copy<T>(this.storage, 0, target, 0, this.length);
|
||||
if required > this.storage.length {
|
||||
let target: array<T> = new array<T>(_grownCapacity(this.storage.length, required));
|
||||
_copy<T>(this.storage, 0, target, 0, index);
|
||||
_copy<T>(this.storage, index, target, index + 1, this.length - index);
|
||||
target[index] = value;
|
||||
this.storage = target;
|
||||
} else {
|
||||
// Prepare one complete stored group before shifting observable slots.
|
||||
let prepared: array<T> = new array<T>(1);
|
||||
prepared[0] = value;
|
||||
_copy<T>(this.storage, index, this.storage, index + 1, this.length - index);
|
||||
_copy<T>(prepared, 0, this.storage, index, 1);
|
||||
}
|
||||
_publish<T>(target, index, target, index + 1, this.length - index,
|
||||
index, 1, some(value), this, target, required);
|
||||
this.length = required;
|
||||
}
|
||||
fn clearAt(index: long) {
|
||||
_bounds(index, this.length);
|
||||
_publish<T>(this.storage, 0, this.storage, 0, 0,
|
||||
index, 1, none, this, this.storage, this.length);
|
||||
this.storage.clear(index);
|
||||
}
|
||||
fn removeAt(index: long) -> optional T {
|
||||
_bounds(index, this.length);
|
||||
let previous: optional T = this.storage[index];
|
||||
_publish<T>(this.storage, index + 1, this.storage, index, this.length - index - 1,
|
||||
this.length - 1, 1, none, this, this.storage, this.length - 1);
|
||||
let length: long = this.length - 1;
|
||||
_copy<T>(this.storage, index + 1, this.storage, index, length - index);
|
||||
this.storage.clear(length);
|
||||
this.length = length;
|
||||
return previous;
|
||||
}
|
||||
fn clear() {
|
||||
_publish<T>(this.storage, 0, this.storage, 0, 0,
|
||||
0, this.length, none, this, this.storage, 0);
|
||||
let index: long = 0;
|
||||
while index < this.length {
|
||||
this.storage.clear(index);
|
||||
index = index + 1;
|
||||
}
|
||||
this.length = 0;
|
||||
}
|
||||
fn resize(length: long) {
|
||||
_checkLength(length);
|
||||
let target: array<T> = this.storage;
|
||||
if length > target.length {
|
||||
target = new array<T>(_grownCapacity(target.length, length));
|
||||
if length > this.storage.length {
|
||||
let target: array<T> = new array<T>(_grownCapacity(this.storage.length, length));
|
||||
_copy<T>(this.storage, 0, target, 0, this.length);
|
||||
this.storage = target;
|
||||
} else {
|
||||
let start: long = this.length;
|
||||
let end: long = length;
|
||||
if length < this.length { start = length; end = this.length; }
|
||||
while start < end {
|
||||
this.storage.clear(start);
|
||||
start = start + 1;
|
||||
}
|
||||
}
|
||||
let start: long = this.length;
|
||||
let count: long = length - this.length;
|
||||
if length < this.length { start = length; count = this.length - length; }
|
||||
_publish<T>(target, 0, target, 0, 0, start, count, none, this, target, length);
|
||||
this.length = length;
|
||||
}
|
||||
fn reserve(capacity: long) {
|
||||
_checkLength(capacity);
|
||||
if capacity > this.storage.length {
|
||||
let target: array<T> = new array<T>(capacity);
|
||||
_copy<T>(this.storage, 0, target, 0, this.length);
|
||||
_publish<T>(target, 0, target, 0, 0, 0, 0, none, this, target, this.length);
|
||||
this.storage = target;
|
||||
}
|
||||
}
|
||||
fn trim() {
|
||||
if this.storage.length != this.length {
|
||||
let target: array<T> = new array<T>(this.length);
|
||||
_copy<T>(this.storage, 0, target, 0, this.length);
|
||||
_publish<T>(target, 0, target, 0, 0, 0, 0, none, this, target, this.length);
|
||||
this.storage = target;
|
||||
}
|
||||
}
|
||||
fn cursor() -> Cursor<T> {
|
||||
@@ -155,18 +178,6 @@ implements<T> Cursor<T> for _SnapshotCursor<T> using snapshot {
|
||||
fn current() -> optional T { return snapshot.current(); }
|
||||
}
|
||||
|
||||
// Complete-object publication is neutral; this source chooses the node topology.
|
||||
[StoragePrimitive(name = "publishFields")]
|
||||
fn _linkedPublish<T>(owner: linked<T>, head: optional _LinkedNode<T>, tail: optional _LinkedNode<T>, length: long,
|
||||
previous: optional _LinkedNode<T>, previousValue: T, previousPrevious: optional _LinkedNode<T>, previousNext: optional _LinkedNode<T>,
|
||||
next: optional _LinkedNode<T>, nextValue: T, nextPrevious: optional _LinkedNode<T>, nextNext: optional _LinkedNode<T>) {}
|
||||
|
||||
[StoragePrimitive(name = "publishFields")]
|
||||
fn _linkedEmpty<T>(owner: linked<T>, head: optional _LinkedNode<T>, tail: optional _LinkedNode<T>, length: long) {}
|
||||
|
||||
[StoragePrimitive(name = "publishFields")]
|
||||
fn _linkedReplace<T>(owner: _LinkedNode<T>, value: T, previous: optional _LinkedNode<T>, next: optional _LinkedNode<T>) {}
|
||||
|
||||
fn _linkedNextSize(length: long) -> long {
|
||||
// Check before adding; Linked does not inherit Array's u32 structural bound.
|
||||
let maximum: long = 9223372036854775807;
|
||||
@@ -228,13 +239,13 @@ declare struct linked<T>(head: optional _LinkedNode<T>, tail: optional _LinkedNo
|
||||
[SdkPrivate]
|
||||
fn _put(index: long, value: T) {
|
||||
if let node = this._node(index) {
|
||||
_linkedReplace<T>(node, value, node.previous, node.next);
|
||||
node.value = value;
|
||||
}
|
||||
}
|
||||
fn add(value: T) { this.insert(this.length, value); }
|
||||
fn addFirst(value: T) { this.insert(0, value); }
|
||||
fn insert(index: long, value: T) {
|
||||
if index != this.length { _bounds(index, this.length); }
|
||||
_insertionBounds(index, this.length);
|
||||
let required: long = _linkedNextSize(this.length);
|
||||
let next: optional _LinkedNode<T> = none;
|
||||
let previous: optional _LinkedNode<T> = this.tail;
|
||||
@@ -243,38 +254,19 @@ declare struct linked<T>(head: optional _LinkedNode<T>, tail: optional _LinkedNo
|
||||
if let node = next { previous = node.previous; }
|
||||
}
|
||||
let inserted: optional _LinkedNode<T> = some(new _LinkedNode<T>(value, previous, next));
|
||||
let head: optional _LinkedNode<T> = this.head;
|
||||
let tail: optional _LinkedNode<T> = this.tail;
|
||||
if index == (0 as long) { head = inserted; }
|
||||
if index == this.length { tail = inserted; }
|
||||
let previousValue: T = value;
|
||||
let previousPrevious: optional _LinkedNode<T> = none;
|
||||
let nextValue: T = value;
|
||||
let nextNext: optional _LinkedNode<T> = none;
|
||||
if let node = previous { previousValue = node.value; previousPrevious = node.previous; }
|
||||
if let node = next { nextValue = node.value; nextNext = node.next; }
|
||||
_linkedPublish<T>(this, head, tail, required,
|
||||
previous, previousValue, previousPrevious, inserted,
|
||||
next, nextValue, inserted, nextNext);
|
||||
if let node = previous { node.next = inserted; } else { this.head = inserted; }
|
||||
if let node = next { node.previous = inserted; } else { this.tail = inserted; }
|
||||
this.length = required;
|
||||
}
|
||||
fn removeAt(index: long) -> optional T {
|
||||
if let removed = this._node(index) {
|
||||
let removedValue: optional T = some(removed.value);
|
||||
let previous: optional _LinkedNode<T> = removed.previous;
|
||||
let next: optional _LinkedNode<T> = removed.next;
|
||||
let head: optional _LinkedNode<T> = this.head;
|
||||
let tail: optional _LinkedNode<T> = this.tail;
|
||||
if previous.hasNone() { head = next; }
|
||||
if next.hasNone() { tail = previous; }
|
||||
let previousValue: T = removed.value;
|
||||
let previousPrevious: optional _LinkedNode<T> = none;
|
||||
let nextValue: T = removed.value;
|
||||
let nextNext: optional _LinkedNode<T> = none;
|
||||
if let node = previous { previousValue = node.value; previousPrevious = node.previous; }
|
||||
if let node = next { nextValue = node.value; nextNext = node.next; }
|
||||
_linkedPublish<T>(this, head, tail, this.length - 1,
|
||||
previous, previousValue, previousPrevious, next,
|
||||
next, nextValue, previous, nextNext);
|
||||
let length: long = this.length - 1;
|
||||
if let node = previous { node.next = next; } else { this.head = next; }
|
||||
if let node = next { node.previous = previous; } else { this.tail = previous; }
|
||||
this.length = length;
|
||||
return removedValue;
|
||||
}
|
||||
return none;
|
||||
@@ -287,7 +279,11 @@ declare struct linked<T>(head: optional _LinkedNode<T>, tail: optional _LinkedNo
|
||||
if this.length == (0 as long) { return none; }
|
||||
return this.removeAt(this.length - 1);
|
||||
}
|
||||
fn clear() { _linkedEmpty<T>(this, none, none, 0); }
|
||||
fn clear() {
|
||||
this.head = none;
|
||||
this.tail = none;
|
||||
this.length = 0;
|
||||
}
|
||||
fn cursor() -> Cursor<T> {
|
||||
let captured: optional _SnapshotNode<T> = none;
|
||||
let source: optional _LinkedNode<T> = this.tail;
|
||||
|
||||
+2
-2
@@ -84,7 +84,7 @@ class PbsGateUSdkInterfaceConformanceTest {
|
||||
var copied=DiagnosticSink.empty();
|
||||
homonym.compileFile(new FileId(201),"""
|
||||
[SdkList] declare struct list<T>(value:T) {}
|
||||
[StoragePrimitive(name="indexGuard")] fn fake(index:long,limit:long) {}
|
||||
[StoragePrimitive(name="fail")] fn fake() {}
|
||||
""",copied,SourceKind.PROJECT);
|
||||
assertTrue(copied.hasErrors(),"Project attributes cannot acquire SDK bindings");
|
||||
}
|
||||
@@ -119,7 +119,7 @@ class PbsGateUSdkInterfaceConformanceTest {
|
||||
var copied=DiagnosticSink.empty();
|
||||
new PbsFrontendCompiler().compileFile(new FileId(203),"""
|
||||
[SdkLinked] declare struct linked<T>(value:T) {}
|
||||
[StoragePrimitive(name="publishFields")] fn fake() {}
|
||||
[StoragePrimitive(name="copy")] fn fake() {}
|
||||
""",copied,SourceKind.PROJECT);
|
||||
assertTrue(copied.hasErrors());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user