Skip to content

Commit

Permalink
replace pop value or null with optimistic version that skips branch
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieTap committed Jan 15, 2025
1 parent bfb1f1b commit 4461ad0
Show file tree
Hide file tree
Showing 50 changed files with 115 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import io.github.charlietap.chasm.ast.type.VectorType
import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.runtime.error.InvocationError
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.instance.FunctionInstance
import io.github.charlietap.chasm.executor.runtime.instance.HostFunctionContext
import io.github.charlietap.chasm.executor.runtime.value.ExecutionValue
Expand All @@ -29,7 +28,7 @@ internal fun HostFunctionCall(
val type = function.functionType

val params = List(type.params.types.size) {
stack.popValue().bind()
stack.popValue()
}.asReversed()

val functionContext = HostFunctionContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.invoker.ext.grow
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.default
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.instance.FunctionInstance
import io.github.charlietap.chasm.executor.runtime.value.ExecutionValue

Expand All @@ -21,7 +20,7 @@ internal inline fun ReturnWasmFunctionCall(
val locals = frame.locals
locals.grow(params + instance.function.locals.size, ExecutionValue.Uninitialised)
for (i in (params - 1) downTo 0) {
locals[i] = stack.popValue().bind()
locals[i] = stack.popValue()
}
for (local in instance.function.locals) {
locals[params++] = local.type.default().bind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.github.charlietap.chasm.executor.runtime.Stack
import io.github.charlietap.chasm.executor.runtime.dispatch.DispatchableInstruction
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.default
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.instance.FunctionInstance
import io.github.charlietap.chasm.executor.runtime.stack.ActivationFrame
import io.github.charlietap.chasm.executor.runtime.value.ExecutionValue
Expand Down Expand Up @@ -39,7 +38,7 @@ internal inline fun WasmFunctionCall(

val locals = MutableList<ExecutionValue>(params + instance.function.locals.size) { ExecutionValue.Uninitialised }
for (i in (params - 1) downTo 0) {
locals[i] = stack.popValue().bind()
locals[i] = stack.popValue()
}
var idx = params
for (local in instance.function.locals) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import io.github.charlietap.chasm.executor.runtime.ext.arrayType
import io.github.charlietap.chasm.executor.runtime.ext.definedType
import io.github.charlietap.chasm.executor.runtime.ext.popArrayReference
import io.github.charlietap.chasm.executor.runtime.ext.popI32
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.instruction.AggregateInstruction
import io.github.charlietap.chasm.type.expansion.DefinedTypeExpander

Expand All @@ -32,7 +31,7 @@ internal inline fun ArrayFillExecutor(

val (stack) = context
val elementsToFill = stack.popI32().bind()
val fillValue = stack.popValue().bind()
val fillValue = stack.popValue()
val arrayElementOffset = stack.popI32().bind()
val arrayReference = stack.popArrayReference().bind()
val arrayInstance = arrayReference.instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.arrayType
import io.github.charlietap.chasm.executor.runtime.ext.definedType
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.ext.pushValue
import io.github.charlietap.chasm.executor.runtime.instance.ArrayInstance
import io.github.charlietap.chasm.executor.runtime.instruction.AggregateInstruction
Expand Down Expand Up @@ -39,7 +38,7 @@ internal inline fun ArrayNewFixedExecutor(
val arrayType = definedTypeExpander(definedType).arrayType().bind()

val fields = MutableList(size.toInt()) { _ ->
val value = stack.popValue().bind()
val value = stack.popValue()
fieldPacker(value, arrayType.fieldType).bind()
}.asReversed()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import io.github.charlietap.chasm.executor.runtime.ext.arrayType
import io.github.charlietap.chasm.executor.runtime.ext.definedType
import io.github.charlietap.chasm.executor.runtime.ext.popArrayReference
import io.github.charlietap.chasm.executor.runtime.ext.popI32
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.instruction.AggregateInstruction
import io.github.charlietap.chasm.type.expansion.DefinedTypeExpander

Expand Down Expand Up @@ -39,7 +38,7 @@ internal inline fun ArraySetExecutor(

val arrayType = definedTypeExpander(definedType).arrayType().bind()

val value = stack.popValue().bind()
val value = stack.popValue()

val fieldIndex = stack.popI32().bind()
val arrayReference = stack.popArrayReference().bind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.github.charlietap.chasm.executor.invoker.instruction.aggregate
import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.definedType
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.ext.pushValue
import io.github.charlietap.chasm.executor.runtime.ext.structType
import io.github.charlietap.chasm.executor.runtime.instance.StructInstance
Expand Down Expand Up @@ -40,7 +39,7 @@ internal inline fun StructNewExecutor(
val fields = structType.fields
.asReversed()
.map { fieldType ->
val value = stack.popValue().bind()
val value = stack.popValue()
fieldPacker(value, fieldType).bind()
}.asReversed()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.definedType
import io.github.charlietap.chasm.executor.runtime.ext.field
import io.github.charlietap.chasm.executor.runtime.ext.popStructReference
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.ext.structType
import io.github.charlietap.chasm.executor.runtime.instruction.AggregateInstruction
import io.github.charlietap.chasm.type.expansion.DefinedTypeExpander
Expand Down Expand Up @@ -38,7 +37,7 @@ internal inline fun StructSetExecutor(
val structType = definedTypeExpander(definedType).structType().bind()
val fieldType = structType.field(instruction.fieldIndex).bind()

val executionValue = stack.popValue().bind()
val executionValue = stack.popValue()

val structRef = stack.popStructReference().bind()
val structInstance = structRef.instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.github.charlietap.chasm.executor.invoker.dispatch.Dispatcher
import io.github.charlietap.chasm.executor.invoker.dispatch.control.ThrowRefDispatcher
import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.ext.pushValue
import io.github.charlietap.chasm.executor.runtime.ext.tag
import io.github.charlietap.chasm.executor.runtime.ext.tagAddress
Expand Down Expand Up @@ -38,7 +37,7 @@ internal inline fun ThrowExecutor(
val functionType = instance.type.type

val params = List(functionType.params.types.size) {
stack.popValue().bind()
stack.popValue()
}

val exceptionInstance = ExceptionInstance(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package io.github.charlietap.chasm.executor.invoker.instruction.parametric

import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.instruction.ParametricInstruction

@Suppress("UNUSED_PARAMETER")
internal inline fun DropExecutor(
context: ExecutionContext,
instruction: ParametricInstruction.Drop,
) {
context.stack.popValue().bind()
context.stack.popValue()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.github.charlietap.chasm.executor.invoker.instruction.parametric
import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.popI32
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.instruction.ParametricInstruction

@Suppress("UNUSED_PARAMETER")
Expand All @@ -14,10 +13,10 @@ internal inline fun SelectExecutor(
val (stack) = context
val select = stack.popI32().bind()

val value2 = stack.popValue().bind()
val value2 = stack.popValue()

if (select == 0) {
stack.popValue().bind()
stack.popValue()
stack.push(value2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package io.github.charlietap.chasm.executor.invoker.instruction.parametric
import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.popI32
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.instruction.ParametricInstruction

@Suppress("UNUSED_PARAMETER")
Expand All @@ -16,10 +15,10 @@ internal inline fun SelectWithTypeExecutor(
val (stack) = context
val select = stack.popI32().bind()

val value2 = stack.popValue().bind()
val value2 = stack.popValue()

if (select == 0) {
stack.popValue().bind()
stack.popValue()
stack.push(value2)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.github.charlietap.chasm.executor.invoker.instruction.reference

import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.ext.pushValue
import io.github.charlietap.chasm.executor.runtime.instruction.ReferenceInstruction
import io.github.charlietap.chasm.executor.runtime.value.NumberValue
Expand All @@ -15,8 +13,8 @@ internal inline fun RefEqExecutor(

val (stack) = context

val referenceValue1 = stack.popValue().bind()
val referenceValue2 = stack.popValue().bind()
val referenceValue1 = stack.popValue()
val referenceValue2 = stack.popValue()

val equal = when {
referenceValue1 is ReferenceValue.Array && referenceValue2 is ReferenceValue.Array -> referenceValue1.instance === referenceValue2.instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.github.michaelbull.result.Err
import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.runtime.error.InvocationError
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.instruction.ReferenceInstruction
import io.github.charlietap.chasm.executor.runtime.value.NumberValue
import io.github.charlietap.chasm.executor.runtime.value.ReferenceValue
Expand All @@ -15,7 +14,7 @@ internal inline fun RefIsNullExecutor(
) {

val (stack) = context
val value = stack.popValue().bind()
val value = stack.popValue()

if (value is ReferenceValue) {
if (value is ReferenceValue.Null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package io.github.charlietap.chasm.executor.invoker.instruction.variable

import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.instruction.VariableInstruction

internal inline fun GlobalSetExecutor(
context: ExecutionContext,
instruction: VariableInstruction.GlobalSet,
) {
val value = context.stack.popValue().bind()
val value = context.stack.popValue()
instruction.global.value = value
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.github.charlietap.chasm.executor.invoker.instruction.variable

import io.github.charlietap.chasm.executor.invoker.ext.bind
import io.github.charlietap.chasm.executor.runtime.execution.ExecutionContext
import io.github.charlietap.chasm.executor.runtime.ext.popValue
import io.github.charlietap.chasm.executor.runtime.instruction.VariableInstruction

internal inline fun LocalSetExecutor(
Expand All @@ -11,7 +9,7 @@ internal inline fun LocalSetExecutor(
) {
val (stack) = context

val value = stack.popValue().bind()
val value = stack.popValue()
val frame = stack.peekFrame()

frame.locals[instruction.localIdx] = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class HostFunctionCallTest {
assertEquals(Unit, actual)
assertEquals(1, stack.framesDepth())
assertEquals(2, stack.valuesDepth())
assertEquals(i64(118), stack.popValueOrNull())
assertEquals(i32(117), stack.popValueOrNull())
assertEquals(i64(118), stack.popValue())
assertEquals(i32(117), stack.popValue())
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AnyConvertExternExecutorTest {
val actual = AnyConvertExternExecutor(context, AggregateInstruction.AnyConvertExtern)

assertEquals(Unit, actual)
assertEquals(ReferenceValue.Null(AbstractHeapType.Any), stack.popValueOrNull())
assertEquals(ReferenceValue.Null(AbstractHeapType.Any), stack.popValue())
}

@Test
Expand All @@ -42,6 +42,6 @@ class AnyConvertExternExecutorTest {
val actual = AnyConvertExternExecutor(context, AggregateInstruction.AnyConvertExtern)

assertEquals(Unit, actual)
assertEquals(referenceValue, stack.popValueOrNull())
assertEquals(referenceValue, stack.popValue())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ class ArrayGetExecutorTest {

assertEquals(Unit, actual)
assertEquals(1, stack.valuesDepth())
assertEquals(expected, stack.popValueOrNull())
assertEquals(expected, stack.popValue())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class ArrayLenExecutorTest {
val actual = ArrayLenExecutor(context, AggregateInstruction.ArrayLen)

assertEquals(Unit, actual)
assertEquals(i32(3), stack.popValueOrNull())
assertEquals(i32(3), stack.popValue())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class ArrayNewDataExecutorTest {

assertEquals(Unit, actual)
assertEquals(2, stack.valuesDepth())
assertEquals(i32(arrayElem2), stack.popValueOrNull())
assertEquals(i32(arrayElem1), stack.popValueOrNull())
assertEquals(i32(arrayElem2), stack.popValue())
assertEquals(i32(arrayElem1), stack.popValue())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ArrayNewDefaultExecutorTest {

assertEquals(Unit, actual)
assertEquals(2, stack.valuesDepth())
assertEquals(default, stack.popValueOrNull())
assertEquals(default, stack.popValueOrNull())
assertEquals(default, stack.popValue())
assertEquals(default, stack.popValue())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ArrayNewElementExecutorTest {

assertEquals(Unit, actual)
assertEquals(2, stack.valuesDepth())
assertEquals(arrayElem2, stack.popValueOrNull())
assertEquals(arrayElem1, stack.popValueOrNull())
assertEquals(arrayElem2, stack.popValue())
assertEquals(arrayElem1, stack.popValue())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ArrayNewExecutorTest {

assertEquals(Unit, actual)
assertEquals(2, stack.valuesDepth())
assertEquals(executionValue, stack.popValueOrNull())
assertEquals(executionValue, stack.popValueOrNull())
assertEquals(executionValue, stack.popValue())
assertEquals(executionValue, stack.popValue())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ class ArrayNewFixedExecutorTest {

assertEquals(Unit, actual)
assertEquals(1, stack.valuesDepth())
assertEquals(expected, stack.popValueOrNull())
assertEquals(expected, stack.popValue())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ExternConvertAnyExecutorTest {
val actual = ExternConvertAnyExecutor(context, AggregateInstruction.ExternConvertAny)

assertEquals(Unit, actual)
assertEquals(ReferenceValue.Null(AbstractHeapType.Extern), stack.popValueOrNull())
assertEquals(ReferenceValue.Null(AbstractHeapType.Extern), stack.popValue())
}

@Test
Expand All @@ -52,7 +52,7 @@ class ExternConvertAnyExecutorTest {
val actual = ExternConvertAnyExecutor(context, AggregateInstruction.ExternConvertAny)

assertEquals(Unit, actual)
assertEquals(ReferenceValue.Extern(referenceValue), stack.popValueOrNull())
assertEquals(ReferenceValue.Extern(referenceValue), stack.popValue())
}
}
}
Loading

0 comments on commit 4461ad0

Please sign in to comment.