Skip to content

Commit

Permalink
constants
Browse files Browse the repository at this point in the history
Signed-off-by: andreypfau <[email protected]>
  • Loading branch information
andreypfau committed Feb 28, 2025
1 parent 3b10a12 commit bce1d87
Show file tree
Hide file tree
Showing 7 changed files with 482 additions and 84 deletions.
41 changes: 28 additions & 13 deletions vm/src/Continuation.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
package org.ton.kotlin.tvm

import org.ton.cell.CellSlice

public sealed interface TvmContinuation {
public data class Ordinary(
val code: CellSlice,
) : TvmContinuation

public companion object {
public fun ordinary(code: CellSlice): TvmContinuation = Ordinary(code)
}
}

//public class ControlData(
// public val nargs: Int? = null,
// public val stack: Stack? = null,
// val stackDepth: Int = 0,
// val save: ControlRegs = ControlRegs(),
// val cp: Int = 0
//)
//
//public class ControlRegs(
// public val c: Array<TvmContinuation?> = arrayOfNulls(4),
// public val d: Array<Cell?> = arrayOfNulls(2),
// public val c7: List<Any?> = emptyList()
//)

/*
class ControlRegs(
val c: Array<TvmContinuation?> = arrayOfNulls(4),
val d: Array<Cell?> = arrayOfNulls(2),
val c7: List<Any?> = emptyList()
)
class ControlData(
val stack: Stack = Stack(),
val stackDepth: Int = 0,
val save: ControlRegs = ControlRegs(),
val nargs: Int = 0,
val cp: Int = 0
)
class TvmContinuation private constructor(
val tag: Byte,
Expand Down
49 changes: 49 additions & 0 deletions vm/src/OpCodes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,53 @@ public object OpCodes {
public const val UNTUPLE_14: Int = 0x2E
public const val UNTUPLE_15: Int = 0x2F

// A.4.1. Integer and boolean constants.
public const val PUSHINT_0: Int = 0x70
public const val PUSHINT_1: Int = 0x71
public const val PUSHINT_2: Int = 0x72
public const val PUSHINT_3: Int = 0x73
public const val PUSHINT_4: Int = 0x74
public const val PUSHINT_5: Int = 0x75
public const val PUSHINT_6: Int = 0x76
public const val PUSHINT_7: Int = 0x77
public const val PUSHINT_8: Int = 0x78
public const val PUSHINT_9: Int = 0x79
public const val PUSHINT_10: Int = 0x7A
public const val PUSHINT_MINUS_5: Int = 0x7B
public const val PUSHINT_MINUS_4: Int = 0x7C
public const val PUSHINT_MINUS_3: Int = 0x7D
public const val PUSHINT_MINUS_2: Int = 0x7E
public const val PUSHINT_MINUS_1: Int = 0x7F
public const val PUSHINT_XX: Int = 0x80
public const val PUSHINT_XXXX: Int = 0x81
public const val PUSHINT_LONG: Int = 0x82
public const val PUSHPOW2: Int = 0x83
public const val PUSHPOW2DEC: Int = 0x84
public const val PUSHNEGPOW2: Int = 0x85

// A.4.2. Constant slices, continuations, cells, and references.
public const val PUSHREF: Int = 0x88
public const val PUSHREFSLICE: Int = 0x89
public const val PUSHREFCONT: Int = 0x8A
public const val PUSHSLICE_SHORT: Int = 0x8B
public const val PUSHSLICE: Int = 0x8C
public const val PUSHSLICE_LONG: Int = 0x8D
public const val PUSHCONT_REF_0_3: Int = 0x8E
public const val PUSHCONT_REF_4: Int = 0x8F
public const val PUSHCONT_0: Int = 0x90
public const val PUSHCONT_1: Int = 0x91
public const val PUSHCONT_2: Int = 0x92
public const val PUSHCONT_3: Int = 0x93
public const val PUSHCONT_4: Int = 0x94
public const val PUSHCONT_5: Int = 0x95
public const val PUSHCONT_6: Int = 0x96
public const val PUSHCONT_7: Int = 0x97
public const val PUSHCONT_8: Int = 0x98
public const val PUSHCONT_9: Int = 0x99
public const val PUSHCONT_10: Int = 0x9A
public const val PUSHCONT_11: Int = 0x9B
public const val PUSHCONT_12: Int = 0x9C
public const val PUSHCONT_13: Int = 0x9D
public const val PUSHCONT_14: Int = 0x9E
public const val PUSHCONT_15: Int = 0x9F
}
147 changes: 83 additions & 64 deletions vm/src/Stack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package org.ton.kotlin.tvm
import org.ton.bigint.BigInt
import org.ton.bigint.sign
import org.ton.bigint.toBigInt
import org.ton.kotlin.tvm.exception.StackOverflowException
import org.ton.cell.Cell
import org.ton.cell.CellSlice
import org.ton.kotlin.tvm.exception.StackUnderflowException

private const val INCREMENT = 256
Expand All @@ -18,33 +19,19 @@ public fun stackOf(vararg elements: Any): Stack {

public class Stack {
private var elements = arrayOfNulls<Any?>(INCREMENT)
public var top: Int = -1

public val depth: Int get() = top + 1

public operator fun get(offset: Int): Any? {
if (offset < 0 || offset > depth) {
throw StackUnderflowException()
}
return elements[top - offset]
}

public operator fun set(offset: Int, operand: Any) {
if (offset < 0) {
throw StackUnderflowException()
} else if (offset > top) {
throw StackOverflowException()
}
elements[top - offset] = operand
}
public var depth: Int = 0

public fun pop(): Any {
if (top < 0) {
val currentDepth = depth
if (currentDepth == 0) {
throw StackUnderflowException()
}
val elements = elements
val removed = elements[top]
elements[top--] = null
val newDepth = currentDepth - 1
val removed = elements[newDepth]
elements[newDepth] = null
depth = newDepth
return removed as Any
}

Expand All @@ -56,33 +43,48 @@ public class Stack {
pushElement(value)
}

public fun pushCell(value: Cell) {
pushElement(value)
}

public fun pushSlice(value: CellSlice) {
pushElement(value)
}

public fun pushContinuation(value: TvmContinuation) {
pushElement(value)
}

public fun pushElement(operand: Any) {
val nextTop = top + 1
val currentDepth = depth
var elements = elements
val currentCapacity = elements.size
if (nextTop >= currentCapacity) {
val nextDepth = currentDepth + 1
if (nextDepth > currentCapacity) {
elements = elements.copyOf(currentCapacity + INCREMENT)
this.elements = elements
}
elements[nextTop] = operand
top = nextTop
elements[currentDepth] = operand
depth = nextDepth
}

public fun pushCopy(offset: Int) {
var elements = elements
val value = elements[top - offset]
val nextTop = top + 1
val currentDepth = depth
val value = elements[currentDepth - 1 - offset]
val nextDepth = currentDepth + 1
val currentCapacity = elements.size
if (nextTop >= currentCapacity) {
if (nextDepth > currentCapacity) {
elements = elements.copyOf(currentCapacity + INCREMENT)
this.elements = elements
}
elements[nextTop] = value
top = nextTop
elements[currentDepth] = value
depth = nextDepth
}

public fun reverse(fromOffset: Int, toOffset: Int) {
val length = toOffset - fromOffset
val top = depth - 1
val fromIndex = top - fromOffset - 1
val toIndex = top - toOffset
val elements = elements
Expand All @@ -97,45 +99,57 @@ public class Stack {
}

public fun dropTop(count: Int) {
elements.fill(null, top - count + 1, top + 1)
top -= count
val currentDepth = depth
val newDepth = currentDepth - count
elements.fill(null, newDepth, currentDepth)
depth = newDepth
}

public fun onlyTop(count: Int) {
val elements = elements
val top = top
val toIndex = top + 1
elements.copyInto(elements, 0, top - count + 1, toIndex)
elements.fill(null, count, toIndex)
this.top = count - 1
val currentDepth = depth
elements.copyInto(elements, 0, currentDepth - count, currentDepth)
elements.fill(null, count, currentDepth)
depth = count
}

public fun rot() {
val elements = elements
val a = elements[top - 2]
val b = elements[top - 1]
val c = elements[top]
elements[top - 2] = b
elements[top - 1] = c
elements[top] = a
val depth = depth
val aIndex = depth - 3
val bIndex = depth - 2
val cIndex = depth - 1
val a = elements[aIndex]
val b = elements[bIndex]
val c = elements[cIndex]
elements[aIndex] = b
elements[bIndex] = c
elements[cIndex] = a
}

public fun rotRev() {
val elements = elements
val a = elements[top - 2]
val b = elements[top - 1]
val c = elements[top]
elements[top - 2] = c
elements[top - 1] = a
elements[top] = b
val depth = depth
val aIndex = depth - 3
val bIndex = depth - 2
val cIndex = depth - 1
val a = elements[aIndex]
val b = elements[bIndex]
val c = elements[cIndex]
elements[aIndex] = c
elements[bIndex] = a
elements[cIndex] = b
}

public fun swap(first: Int, second: Int) {
val elements = elements
val firstValue = elements[top - first]
val secondValue = elements[top - second]
elements[top - first] = secondValue
elements[top - second] = firstValue
val depth = depth
val firstIndex = depth - 1 - first
val secondIndex = depth - 1 - second
val firstValue = elements[firstIndex]
val secondValue = elements[secondIndex]
elements[firstIndex] = secondValue
elements[secondIndex] = firstValue
}

/**
Expand All @@ -144,16 +158,19 @@ public class Stack {
*/
public fun blockSwap(i: Int, j: Int) {
val elements = elements
val lastElements = elements.copyOfRange(top - j + 1, top + 1)
elements.copyInto(elements, top - i + 1, top - j - i + 1, top - j + 1)
lastElements.copyInto(elements, top - i - j + 1)
val currentDepth = depth
val lastElements = elements.copyOfRange(currentDepth - j, currentDepth)
elements.copyInto(elements, currentDepth - i, currentDepth - j - i, currentDepth - j)
lastElements.copyInto(elements, currentDepth - i - j)
}

public fun blockDrop(i: Int, j: Int) {
val elements = elements
elements.copyInto(elements, top - j - i + 1, top - j + 1, top + 1)
elements.fill(null, top - i + 1, top + 1)
top = top - i
val currentDepth = depth
val newDepth = currentDepth - i
elements.copyInto(elements, newDepth - j, currentDepth - j, currentDepth)
elements.fill(null, newDepth, currentDepth)
depth = newDepth
}

public fun copy(srcSlot: Int, dstSlot: Int) {
Expand All @@ -166,27 +183,29 @@ public class Stack {

public fun popBoolean(): Boolean = popInt().sign != 0

public fun popSlice(): CellSlice = pop() as CellSlice

override fun toString(): String = buildString {
val elements = elements
append("[ ")
for (i in 0..top) {
for (i in 0 until depth) {
append(elements[i])
append(" ")
}
append("]")
append("] | ${elements.copyOf(16).contentToString()}")
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as Stack
if (top != other.top) return false
if (depth != other.depth) return false
if (!elements.contentEquals(other.elements)) return false
return true
}

override fun hashCode(): Int {
var result = top
var result = depth
result = 31 * result + elements.contentHashCode()
return result
}
Expand Down
Loading

0 comments on commit bce1d87

Please sign in to comment.