Skip to content

Commit 7eb0814

Browse files
committed
add jump admin instructions
1 parent be45c9e commit 7eb0814

File tree

3 files changed

+80
-16
lines changed

3 files changed

+80
-16
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,55 @@
11
package io.github.charlietap.chasm.ir.instruction
22

3+
import io.github.charlietap.chasm.type.ReferenceType
4+
35
sealed interface AdminInstruction : Instruction {
46

57
data object EndBlock : AdminInstruction
68

79
data object EndFunction : AdminInstruction
10+
11+
data class Jump(
12+
val offset: Int,
13+
val adjustment: StackAdjustment,
14+
) : ControlInstruction
15+
16+
data class JumpIf(
17+
val offset: Int,
18+
val adjustment: StackAdjustment,
19+
) : ControlInstruction
20+
21+
data class JumpIfNot(
22+
val offset: Int,
23+
val adjustment: StackAdjustment,
24+
) : ControlInstruction
25+
26+
data class JumpTable(
27+
val offsets: List<Int>,
28+
val defaultOffset: Int,
29+
val adjustments: List<StackAdjustment>,
30+
) : ControlInstruction
31+
32+
data class JumpOnNull(
33+
val offset: Int,
34+
val adjustment: StackAdjustment,
35+
) : ControlInstruction
36+
37+
data class JumpOnNonNull(
38+
val offset: Int,
39+
val adjustment: StackAdjustment,
40+
) : ControlInstruction
41+
42+
data class JumpOnCast(
43+
val offset: Int,
44+
val srcReferenceType: ReferenceType,
45+
val dstReferenceType: ReferenceType,
46+
val adjustment: StackAdjustment,
47+
) : ControlInstruction
48+
49+
data class JumpOnCastFail(
50+
val offset: Int,
51+
val srcReferenceType: ReferenceType,
52+
val dstReferenceType: ReferenceType,
53+
val adjustment: StackAdjustment,
54+
) : ControlInstruction
855
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.charlietap.chasm.ir.instruction
2+
3+
import kotlin.jvm.JvmInline
4+
5+
fun StackAdjustment(
6+
depth: Int,
7+
keep: Int,
8+
): StackAdjustment {
9+
val encoded = (depth.toLong() shl 32) or (keep.toLong() and 0xFFFFFFFFL)
10+
return StackAdjustment(encoded)
11+
}
12+
13+
@JvmInline
14+
value class StackAdjustment(
15+
@PublishedApi internal val encoded: Long,
16+
) {
17+
inline val depth: Int
18+
get() = (encoded ushr 32).toInt()
19+
20+
inline val keep: Int
21+
get() = (encoded and 0xFFFFFFFF).toInt()
22+
23+
override fun toString(): String = "StackAdjustment(depth=$depth, keep=$keep)"
24+
}

predecoder/src/commonMain/kotlin/io/github/charlietap/chasm/predecoder/instruction/control/ControlInstructionPredecoder.kt

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,12 @@ package io.github.charlietap.chasm.predecoder.instruction.control
22

33
import com.github.michaelbull.result.Result
44
import com.github.michaelbull.result.binding
5+
import io.github.charlietap.chasm.ir.instruction.AdminInstruction
56
import io.github.charlietap.chasm.ir.instruction.ControlInstruction
67
import io.github.charlietap.chasm.predecoder.Predecoder
78
import io.github.charlietap.chasm.predecoder.PredecodingContext
89
import io.github.charlietap.chasm.runtime.dispatch.DispatchableInstruction
910
import io.github.charlietap.chasm.runtime.error.ModuleTrapError
10-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.Br
11-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.BrIf
12-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.BrOnCast
13-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.BrOnCastFail
14-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.BrOnNonNull
15-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.BrOnNull
16-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.BrTable
17-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.CallIndirect
18-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.CallRef
19-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.Nop
20-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.Return
21-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.ReturnCallIndirect
22-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.ReturnCallRef
23-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.Throw
24-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.ThrowRef
25-
import io.github.charlietap.chasm.runtime.instruction.ControlInstruction.Unreachable
2611

2712
internal fun ControlInstructionPredecoder(
2813
context: PredecodingContext,
@@ -104,5 +89,13 @@ internal inline fun ControlInstructionPredecoder(
10489
is ControlInstruction.ThrowRef -> throwRefInstructionPredecoder(context, instruction).bind()
10590
is ControlInstruction.TryTable -> tryTableInstructionPredecoder(context, instruction).bind()
10691
is ControlInstruction.Unreachable -> unreachableInstructionPredecoder(context, instruction).bind()
92+
is AdminInstruction.Jump -> TODO()
93+
is AdminInstruction.JumpIf -> TODO()
94+
is AdminInstruction.JumpIfNot -> TODO()
95+
is AdminInstruction.JumpOnCast -> TODO()
96+
is AdminInstruction.JumpOnCastFail -> TODO()
97+
is AdminInstruction.JumpOnNonNull -> TODO()
98+
is AdminInstruction.JumpOnNull -> TODO()
99+
is AdminInstruction.JumpTable -> TODO()
107100
}
108101
}

0 commit comments

Comments
 (0)