Skip to content

Commit

Permalink
attempt - use BaseInstruction
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Aug 19, 2024
1 parent cb8172b commit 9479ded
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 113 deletions.
10 changes: 5 additions & 5 deletions aot/src/main/java/com/dylibso/chicory/aot/AotContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import static com.dylibso.chicory.aot.AotUtil.stackSize;

import com.dylibso.chicory.aot.AotUtil.StackSize;
import com.dylibso.chicory.wasm.types.BaseInstruction;
import com.dylibso.chicory.wasm.types.FunctionBody;
import com.dylibso.chicory.wasm.types.FunctionType;
import com.dylibso.chicory.wasm.types.Instruction;
import com.dylibso.chicory.wasm.types.ValueType;
import java.util.ArrayDeque;
import java.util.ArrayList;
Expand All @@ -33,7 +33,7 @@ final class AotContext {
private final int instanceSlot;
private final int tempSlot;
private final Deque<Deque<StackSize>> stackSizesStack = new ArrayDeque<>();
private final Map<Instruction, Integer> scopeStackSize = new HashMap<>();
private final Map<BaseInstruction, Integer> scopeStackSize = new HashMap<>();
private final Deque<Deque<StackSize>> restoreStackSize = new ArrayDeque<>();

public AotContext(
Expand Down Expand Up @@ -147,7 +147,7 @@ public void popStackSizesStack() {
stackSizesStack.pop();
}

public void enterScope(Instruction scope, FunctionType scopeType) {
public void enterScope(BaseInstruction scope, FunctionType scopeType) {
scopeStackSize.put(scope, stackSizes().size());

// stack sizes when exiting "polymorphic" blocks after unconditional control transfer
Expand All @@ -161,12 +161,12 @@ public void enterScope(Instruction scope, FunctionType scopeType) {
restoreStackSize.push(stack);
}

public void exitScope(Instruction scope) {
public void exitScope(BaseInstruction scope) {
scopeStackSize.remove(scope);
restoreStackSize.pop();
}

public int scopeStackSize(Instruction scope) {
public int scopeStackSize(BaseInstruction scope) {
return scopeStackSize.get(scope);
}

Expand Down
14 changes: 7 additions & 7 deletions aot/src/main/java/com/dylibso/chicory/aot/AotMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.dylibso.chicory.runtime.exceptions.WASMRuntimeException;
import com.dylibso.chicory.wasm.Module;
import com.dylibso.chicory.wasm.exceptions.ChicoryException;
import com.dylibso.chicory.wasm.types.BaseInstruction;
import com.dylibso.chicory.wasm.types.ExternalType;
import com.dylibso.chicory.wasm.types.FunctionBody;
import com.dylibso.chicory.wasm.types.FunctionImport;
Expand Down Expand Up @@ -79,8 +80,8 @@
public final class AotMachine implements Machine {

public static final String DEFAULT_CLASS_NAME = "com.dylibso.chicory.$gen.CompiledModule";
private static final Instruction FUNCTION_SCOPE =
Instruction.builder()
private static final BaseInstruction FUNCTION_SCOPE =
BaseInstruction.builder()
.withAddress(-1)
.withOpcode(OpCode.NOP)
.withOperands(new long[0])
Expand Down Expand Up @@ -691,7 +692,7 @@ private void compileBody(
break;
case BLOCK:
case LOOP:
ctx.enterScope(ins.scope().get(), blockType(ins));
ctx.enterScope(ins.scope().get(), blockType((int) ins.operands()[0]));
break;
case END:
ctx.exitScope(ins.scope().get());
Expand All @@ -707,7 +708,7 @@ private void compileBody(
break;
case IF:
ctx.popStackSize();
ctx.enterScope(ins.scope().get(), blockType(ins));
ctx.enterScope(ins.scope().get(), blockType((int) ins.operands()[0]));
asm.visitJumpInsn(Opcodes.IFEQ, labels.get(ins.labelFalse().get()));
// use the same starting stack sizes for both sides of the branch
if (body.instructions().get(ins.labelFalse().get() - 1).opcode()
Expand Down Expand Up @@ -829,7 +830,7 @@ private void emitUnwindStack(
scope = FUNCTION_SCOPE;
blockType = functionType;
} else {
blockType = blockType(scope);
blockType = blockType((int) scope.operands()[0]);
}

var types = forward ? blockType.returns() : blockType.params();
Expand Down Expand Up @@ -868,8 +869,7 @@ private void emitUnwindStack(
}
}

private FunctionType blockType(Instruction ins) {
int typeId = (int) ins.operands()[0];
private FunctionType blockType(int typeId) {
if (typeId == 0x40) {
return FunctionType.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.dylibso.chicory.runtime;

import com.dylibso.chicory.wasm.exceptions.InvalidException;
import com.dylibso.chicory.wasm.types.Instruction;
import com.dylibso.chicory.wasm.types.BaseInstruction;
import com.dylibso.chicory.wasm.types.MutabilityType;
import com.dylibso.chicory.wasm.types.Value;
import com.dylibso.chicory.wasm.types.ValueType;
import java.util.Arrays;
import java.util.List;

public class ConstantEvaluators {
public static Value computeConstantValue(Instance instance, Instruction[] expr) {
public static Value computeConstantValue(Instance instance, BaseInstruction[] expr) {
return computeConstantValue(instance, Arrays.asList(expr));
}

public static Value computeConstantValue(Instance instance, List<Instruction> expr) {
public static Value computeConstantValue(Instance instance, List<BaseInstruction> expr) {
Value tos = null;
for (Instruction instruction : expr) {
for (var instruction : expr) {
switch (instruction.opcode()) {
case F32_CONST:
{
Expand Down Expand Up @@ -95,8 +95,8 @@ public static Value computeConstantValue(Instance instance, List<Instruction> ex
return tos;
}

public static Instance computeConstantInstance(Instance instance, List<Instruction> expr) {
for (Instruction instruction : expr) {
public static Instance computeConstantInstance(Instance instance, List<BaseInstruction> expr) {
for (var instruction : expr) {
switch (instruction.opcode()) {
case GLOBAL_GET:
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.dylibso.chicory.wasm.exceptions.UnlinkableException;
import com.dylibso.chicory.wasm.types.ActiveDataSegment;
import com.dylibso.chicory.wasm.types.ActiveElement;
import com.dylibso.chicory.wasm.types.BaseInstruction;
import com.dylibso.chicory.wasm.types.DataSegment;
import com.dylibso.chicory.wasm.types.DeclarativeElement;
import com.dylibso.chicory.wasm.types.Element;
Expand Down Expand Up @@ -126,13 +127,13 @@ public Instance initialize(boolean start) {
throw new InvalidException(
"type mismatch, invalid offset type in element " + offset.type());
}
List<List<Instruction>> initializers = ae.initializers();
List<List<BaseInstruction>> initializers = ae.initializers();
if (offset.asInt() > table.limits().min()
|| (offset.asInt() + initializers.size() - 1) >= table.size()) {
throw new UninstantiableException("out of bounds table access");
}
for (int i = 0; i < initializers.size(); i++) {
final List<Instruction> init = initializers.get(i);
final List<BaseInstruction> init = initializers.get(i);
var index = offset.asInt() + i;
if (init.stream().filter(e -> e.opcode() != OpCode.END).count() > 1l) {
throw new InvalidException(
Expand Down Expand Up @@ -165,7 +166,7 @@ public Instance initialize(boolean start) {
}
}
} else if (el instanceof DeclarativeElement) {
for (List<Instruction> init : el.initializers()) {
for (var init : el.initializers()) {
computeConstantValue(this, init);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1927,9 +1927,6 @@ private static void BR_IF(StackFrame frame, MStack stack, Instruction instructio
frame.jumpTo(instruction.labelFalse().get());
} else {
ctrlJump(frame, stack, (int) instruction.operands()[0]);
if (instruction.labelTrue().isEmpty()) {
System.out.println("debug me");
}
frame.jumpTo(instruction.labelTrue().get());
}
}
Expand Down
58 changes: 29 additions & 29 deletions wasm/src/main/java/com/dylibso/chicory/wasm/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.dylibso.chicory.wasm.exceptions.MalformedException;
import com.dylibso.chicory.wasm.types.ActiveDataSegment;
import com.dylibso.chicory.wasm.types.ActiveElement;
import com.dylibso.chicory.wasm.types.BaseInstruction;
import com.dylibso.chicory.wasm.types.CodeSection;
import com.dylibso.chicory.wasm.types.CustomSection;
import com.dylibso.chicory.wasm.types.DataCountSection;
Expand Down Expand Up @@ -648,7 +649,7 @@ private static Element parseSingleElement(ByteBuffer buffer) {

// the table index is assumed to be zero
int tableIdx = 0;
List<Instruction> offset = List.of();
List<BaseInstruction> offset = List.of();

if (active) {
if (hasTableIdx) {
Expand All @@ -672,7 +673,7 @@ private static Element parseSingleElement(ByteBuffer buffer) {
type = ValueType.refTypeForId(Math.toIntExact(readVarUInt32(buffer)));
}
int initCnt = Math.toIntExact(readVarUInt32(buffer));
List<List<Instruction>> inits = new ArrayList<>(initCnt);
List<List<BaseInstruction>> inits = new ArrayList<>(initCnt);
if (exprInit) {
// read the expressions directly from the stream
for (int i = 0; i < initCnt; i++) {
Expand All @@ -683,12 +684,12 @@ private static Element parseSingleElement(ByteBuffer buffer) {
for (int i = 0; i < initCnt; i++) {
inits.add(
List.of(
Instruction.builder()
BaseInstruction.builder()
.withAddress(-1)
.withOpcode(OpCode.REF_FUNC)
.withOperands(new long[] {readVarUInt32(buffer)})
.build(),
Instruction.builder()
BaseInstruction.builder()
.withAddress(-1)
.withOpcode(OpCode.END)
.withOperands(new long[0])
Expand Down Expand Up @@ -731,7 +732,7 @@ private static CodeSection parseCodeSection(ByteBuffer buffer) {

// Parse individual function bodies in the code section
for (int i = 0; i < funcBodyCount; i++) {
var blockScope = new ArrayDeque<Instruction>();
var blockScope = new ArrayDeque<BaseInstruction>();
var depth = 0;
var funcEndPoint = readVarUInt32(buffer) + buffer.position();
var locals = parseCodeSectionLocalTypes(buffer);
Expand All @@ -740,28 +741,29 @@ private static CodeSection parseCodeSection(ByteBuffer buffer) {
ControlTree currentControlFlow = null;

do {
var instruction = parseInstruction(buffer);
var baseInstruction = parseInstruction(buffer);
var instruction = Instruction.builder().from(baseInstruction);
lastInstruction = buffer.position() >= funcEndPoint;
if (instructions.isEmpty()) {
currentControlFlow = root.spawn(0, instruction);
}

// https://webassembly.github.io/spec/core/binary/modules.html#data-count-section
switch (instruction.opcode()) {
switch (baseInstruction.opcode()) {
case MEMORY_INIT:
case DATA_DROP:
codeSection.setRequiresDataCount(true);
}

// depth control
switch (instruction.opcode()) {
switch (baseInstruction.opcode()) {
case BLOCK:
case LOOP:
case IF:
{
depth++;
instruction.withDepth(depth);
blockScope.push(instruction.build());
blockScope.push(baseInstruction);
instruction.withScope(blockScope.peek());
break;
}
Expand All @@ -770,7 +772,7 @@ private static CodeSection parseCodeSection(ByteBuffer buffer) {
instruction.withDepth(depth);
depth--;
instruction.withScope(
blockScope.isEmpty() ? instruction.build() : blockScope.pop());
blockScope.isEmpty() ? baseInstruction : blockScope.pop());
break;
}
default:
Expand All @@ -781,7 +783,7 @@ private static CodeSection parseCodeSection(ByteBuffer buffer) {
}

// control-flow
switch (instruction.opcode()) {
switch (baseInstruction.opcode()) {
case BLOCK:
case LOOP:
{
Expand All @@ -798,10 +800,7 @@ private static CodeSection parseCodeSection(ByteBuffer buffer) {
currentControlFlow.addCallback(
end -> {
// check that there is no "else" branch
if (instruction.labelFalse().isPresent()
&& instruction.labelFalse().get() == defaultJmp) {
instruction.withLabelFalse(end);
}
instruction.updateLabelFalse(end);
});

// defaults
Expand All @@ -811,7 +810,6 @@ private static CodeSection parseCodeSection(ByteBuffer buffer) {
}
case ELSE:
{
assert (currentControlFlow.instruction().opcode() == OpCode.IF);
currentControlFlow
.instruction()
.withLabelFalse(instructions.size() + 1);
Expand All @@ -827,7 +825,7 @@ private static CodeSection parseCodeSection(ByteBuffer buffer) {
// fallthrough
case BR:
{
var offset = (int) instruction.operands()[0];
var offset = (int) baseInstruction.operands()[0];
ControlTree reference = currentControlFlow;
while (offset > 0) {
if (reference == null) {
Expand All @@ -841,11 +839,11 @@ private static CodeSection parseCodeSection(ByteBuffer buffer) {
}
case BR_TABLE:
{
var length = instruction.operands().length;
var length = baseInstruction.operands().length;
var labelTable = new ArrayList<Integer>();
for (var idx = 0; idx < length; idx++) {
labelTable.add(null);
var offset = (int) instruction.operands()[idx];
var offset = (int) baseInstruction.operands()[idx];
ControlTree reference = currentControlFlow;
while (offset > 0) {
if (reference == null) {
Expand All @@ -869,7 +867,7 @@ private static CodeSection parseCodeSection(ByteBuffer buffer) {
if (lastInstruction && instructions.size() > 1) {
var former = instructions.get(instructions.size() - 1);
if (former.opcode() == OpCode.END) {
instruction.withScope(former.scope());
instruction.withScope(former.scope().get());
}
}
break;
Expand Down Expand Up @@ -929,7 +927,7 @@ private static DataCountSection parseDataCountSection(ByteBuffer buffer) {
return DataCountSection.builder().withDataCount((int) dataCount).build();
}

private static Instruction.Builder parseInstruction(ByteBuffer buffer) {
private static BaseInstruction parseInstruction(ByteBuffer buffer) {

var address = buffer.position();
var b = (int) readByte(buffer) & 0xff;
Expand Down Expand Up @@ -958,10 +956,11 @@ private static Instruction.Builder parseInstruction(ByteBuffer buffer) {
break;
}
if (signature.length == 0) {
return Instruction.builder()
return BaseInstruction.builder()
.withAddress(address)
.withOpcode(op)
.withOperands(new long[] {});
.withOperands(new long[] {})
.build();
}
var operands = new ArrayList<Long>();
for (var sig : signature) {
Expand Down Expand Up @@ -996,10 +995,11 @@ private static Instruction.Builder parseInstruction(ByteBuffer buffer) {
operandsArray[i] = operands.get(i);
}
verifyAlignment(op, operandsArray);
return Instruction.builder()
return BaseInstruction.builder()
.withAddress(address)
.withOpcode(op)
.withOperands(operandsArray);
.withOperands(operandsArray)
.build();
}

private static void verifyAlignment(OpCode op, long[] operands) {
Expand Down Expand Up @@ -1043,16 +1043,16 @@ private static void verifyAlignment(OpCode op, long[] operands) {
}
}

private static Instruction[] parseExpression(ByteBuffer buffer) {
var expr = new ArrayList<Instruction>();
private static BaseInstruction[] parseExpression(ByteBuffer buffer) {
var expr = new ArrayList<BaseInstruction>();
while (true) {
var i = parseInstruction(buffer);
if (i.opcode() == OpCode.END) {
break;
}
expr.add(i.build());
expr.add(i);
}
return expr.toArray(new Instruction[0]);
return expr.toArray(new BaseInstruction[0]);
}

// https://webassembly.github.io/spec/core/syntax/values.html#integers
Expand Down
Loading

0 comments on commit 9479ded

Please sign in to comment.