Skip to content

Commit 0282e2e

Browse files
[wasm] Add CiWasmTarget.functionId (#496)
1 parent 95ddf21 commit 0282e2e

File tree

12 files changed

+194
-91
lines changed

12 files changed

+194
-91
lines changed

aeneas/src/core/Eval.v3

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,13 @@ def evalOp(op: Operator, args: Arguments) -> Result {
951951
if (sys == null) prog.system = sys = SystemCallState.new();
952952
return syscall.eval(sys, args);
953953
}
954+
//----------------------------------------------------------------------------
955+
WasmFunctionId => {
956+
var prog = args.getProgram();
957+
var del = Closure.!(args.vals[0]);
958+
if (del == null) return args.throw(V3Exception.NullCheck, null);
959+
return Int.box(prog.makeFunctionId(del.memberRef));
960+
}
954961
_ => ;
955962
}
956963
return args.throw("EvalUnimplemented", op.opcode.name);

aeneas/src/core/Opcode.v3

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ type Opcode {
175175
case Alloc;
176176
// Wasm target
177177
case WasmFunctionTypeId;
178+
case WasmFunctionId;
178179
// Call
179180
case CallAddress(p: Mach_FuncRep);
180181
case CallKernel(kernel: Kernel);

aeneas/src/core/Operator.v3

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ component V3Op {
569569
var ta = [paramType, resultType];
570570
return newOp0(Opcode.WasmFunctionTypeId, ta, TypeUtil.NO_TYPES, Int.TYPE);
571571
}
572+
def newWasmFunctionId(paramType: Type, resultType: Type) -> Operator {
573+
var ta = [paramType, resultType];
574+
var funcType = Function.newType(paramType, resultType);
575+
return newOp0(Opcode.WasmFunctionId, ta, [funcType], Int.TYPE);
576+
}
572577
//----------------------------------------------------------------------------
573578
def bestCallVirtual(spec: IrSpec) -> Operator {
574579
if (spec.receiver.typeCon.kind == Kind.CLASS) {

aeneas/src/core/Program.v3

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ class Program {
2222
var mainRootIndex = -1;
2323
var explicitName: string;
2424
var tprog: TargetProgram;
25+
var numImports = 0;
2526

2627
// dynamic portion of the program, including initialized state
2728
var initState: Array<InitState>;
2829
var compRecords: Array<Record>;
2930
var strRecords: Array<Record>;
3031
var recordCount: int;
3132
var system: SystemCallState;
33+
var wasmFuncId_imported = 0;
34+
var wasmFuncId_internal = 0;
3235

3336
new() {
3437
opBuilder = IrOpMethodBuilder.new(this);
@@ -193,6 +196,22 @@ class Program {
193196
if (trt != frt) requestMapper(frt, trt); // results are covariant
194197
}
195198
}
199+
def makeFunctionId(meth: IrSpec) -> int {
200+
// XXX: linear search for function id
201+
for (i < ir.roots.length) {
202+
var r = ir.roots[i];
203+
if (r.spec.equals(meth)) {
204+
if (r.functionId < 0) r.functionId = assignFunctionId(meth);
205+
return r.functionId;
206+
}
207+
}
208+
var r = IrRoot.new(null, meth);
209+
ir.roots.put(r);
210+
return r.functionId = assignFunctionId(meth);
211+
}
212+
def assignFunctionId(meth: IrSpec) -> int {
213+
return if(meth.asMethod().isImported(), wasmFuncId_imported++, wasmFuncId_internal++);
214+
}
196215
}
197216
// Representation of the program appropriate for the target, e.g. a machine.
198217
class TargetProgram(prog: Program) {

aeneas/src/ir/Ir.v3

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ class IrMethod extends IrMember {
100100
def getMethodType() -> Type {
101101
return sig.funcType();
102102
}
103+
def isImported() -> bool {
104+
return source != null && source.importName != null;
105+
}
103106
}
104107
// a virtual method selector consisting of a method and a vtable index
105108
// (the vtable index may differ from the method's in the case of partial specialization)
@@ -250,7 +253,9 @@ class IrSpec(receiver: Type, typeArgs: Array<Type>, member: IrMember) {
250253
}
251254
}
252255

253-
class IrRoot(name: string, spec: IrSpec) { }
256+
class IrRoot(name: string, spec: IrSpec) {
257+
var functionId = -1;
258+
}
254259

255260
// Utility methods for dealing with Ir classes, methods, and fields
256261
component IrUtil {
@@ -263,7 +268,7 @@ component IrUtil {
263268
return HashMap<IrItem, T>.new(IrItem.uid, IrItem.==);
264269
}
265270
}
266-
// representation of a module, including classes, components, methods, etc.
271+
// Representation of a module, including classes, components, methods, etc.
267272
class IrModule {
268273
def classMap = TypeUtil.newTypeMap<IrClass>();
269274
def classes = Vector<IrClass>.new();

aeneas/src/ir/Normalization.v3

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,9 @@ class ReachabilityNormalizer(config: NormalizerConfig, ra: ReachabilityAnalyzer)
504504
newIr.roots.length = old.length;
505505
for (i < old.length) {
506506
var o = old[i];
507-
newIr.roots[i] = IrRoot.new(o.name, normalizeMethodRef(o.spec).1);
507+
var r = IrRoot.new(o.name, normalizeMethodRef(o.spec).1);
508+
r.functionId = o.functionId;
509+
newIr.roots[i] = r;
508510
}
509511
ra.prog.ir = newIr;
510512
// do remaining work; normalize record instances

aeneas/src/vst/Verifier.v3

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Verifier(compiler: Compiler, prog: Program) {
1212
def ir = prog.ir = IrModule.new();
1313
var exportMap: PartialMap<string, ExportDecl>;
1414
var methodOverrideChecks: List<(VstCompoundVerifier, VstMethod)>;
15+
var numImports = 0;
1516

1617
def forAll<T>(vec: Vector<T>, do: T -> void) {
1718
for (i < vec.length) {
@@ -55,6 +56,8 @@ class Verifier(compiler: Compiler, prog: Program) {
5556
for (r in vst.fieldRedefs.asArray()) redefineField(r, tc);
5657
}
5758
if (ERROR.noErrors) forAll(vst.exports, addExport);
59+
60+
prog.wasmFuncId_internal = numImports;
5861
}
5962
def buildFile(file: VstFile) {
6063
if (file == null) return;
@@ -629,6 +632,7 @@ class VstCompoundVerifier {
629632
if (decl.importName == null) decl.importName = decl.token;
630633
if (decl.typeParams != null) errAtDecl(decl).ImportError("imported methods cannot have type parameters");
631634
}
635+
verifier.numImports++;
632636
}
633637
typeEnv = compound.typeEnv;
634638
if (decl.isIndexed() && VstComponent.?(compound)) {

aeneas/src/wasm/WasmCodeGen.v3

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,14 @@ class WasmCodeGen extends SsaMachGen {
15501550
_ => context.fail1("unimplemented main parameter type %q", t.render);
15511551
}
15521552
}
1553+
def emitUnassignedFunction() {
1554+
def b = w.putb;
1555+
1556+
b(/*body_size=*/3);
1557+
b(/*locals_count=*/0);
1558+
b(WasmOp.UNREACHABLE.opcode);
1559+
b(WasmOp.END.opcode);
1560+
}
15531561
def emitAllocationStub() {
15541562
var gcmeth = rt.getRiGc();
15551563
def b = w.putb;

aeneas/src/wasm/WasmGcTarget.v3

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,9 +1447,8 @@ class WasmGcProgram extends WasmProgram {
14471447
if (m.machIndex < 0) context.fail1("method not assigned an index: %q", m.renderLong);
14481448
requestIndirectAdapter(m);
14491449
}
1450-
def addIrMethods() -> Vector<IrMethod> {
1450+
def addIrMethods() -> void {
14511451
context.fail("addIrMethods called for WasmGc target");
1452-
return null;
14531452
}
14541453
def addIrMethodsGc(infos: Vector<FunctionInfo>) -> Vector<IrMethod> {
14551454
var methods = mach.prog.ir.methods;
@@ -1506,8 +1505,9 @@ class WasmGcProgram extends WasmProgram {
15061505
Arrays.copyInto(sorted, methods.array, 0); // update the IrModule's method vector.
15071506
return methods;
15081507
}
1509-
def addMethod(m: IrMethod) {
1508+
def addMethod(m: IrMethod, wf: WasmFuncKind) -> int {
15101509
context.fail("addMethod called for WasmGc target");
1510+
return -1;
15111511
}
15121512
def addMethodGc(m: IrMethod, infos: Vector<FunctionInfo>, imported: bool) {
15131513
var sigIdx = addSig(m.ssa.params[0].vtype, m.sig);

0 commit comments

Comments
 (0)