Skip to content

Commit bce1d87

Browse files
committed
constants
Signed-off-by: andreypfau <[email protected]>
1 parent 3b10a12 commit bce1d87

File tree

7 files changed

+482
-84
lines changed

7 files changed

+482
-84
lines changed

vm/src/Continuation.kt

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
package org.ton.kotlin.tvm
22

3+
import org.ton.cell.CellSlice
4+
5+
public sealed interface TvmContinuation {
6+
public data class Ordinary(
7+
val code: CellSlice,
8+
) : TvmContinuation
9+
10+
public companion object {
11+
public fun ordinary(code: CellSlice): TvmContinuation = Ordinary(code)
12+
}
13+
}
14+
15+
//public class ControlData(
16+
// public val nargs: Int? = null,
17+
// public val stack: Stack? = null,
18+
// val stackDepth: Int = 0,
19+
// val save: ControlRegs = ControlRegs(),
20+
// val cp: Int = 0
21+
//)
22+
//
23+
//public class ControlRegs(
24+
// public val c: Array<TvmContinuation?> = arrayOfNulls(4),
25+
// public val d: Array<Cell?> = arrayOfNulls(2),
26+
// public val c7: List<Any?> = emptyList()
27+
//)
28+
329
/*
4-
class ControlRegs(
5-
val c: Array<TvmContinuation?> = arrayOfNulls(4),
6-
val d: Array<Cell?> = arrayOfNulls(2),
7-
val c7: List<Any?> = emptyList()
8-
)
9-
10-
class ControlData(
11-
val stack: Stack = Stack(),
12-
val stackDepth: Int = 0,
13-
val save: ControlRegs = ControlRegs(),
14-
val nargs: Int = 0,
15-
val cp: Int = 0
16-
)
30+
31+
1732
1833
class TvmContinuation private constructor(
1934
val tag: Byte,

vm/src/OpCodes.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,53 @@ public object OpCodes {
172172
public const val UNTUPLE_14: Int = 0x2E
173173
public const val UNTUPLE_15: Int = 0x2F
174174

175+
// A.4.1. Integer and boolean constants.
176+
public const val PUSHINT_0: Int = 0x70
177+
public const val PUSHINT_1: Int = 0x71
178+
public const val PUSHINT_2: Int = 0x72
179+
public const val PUSHINT_3: Int = 0x73
180+
public const val PUSHINT_4: Int = 0x74
181+
public const val PUSHINT_5: Int = 0x75
182+
public const val PUSHINT_6: Int = 0x76
183+
public const val PUSHINT_7: Int = 0x77
184+
public const val PUSHINT_8: Int = 0x78
185+
public const val PUSHINT_9: Int = 0x79
186+
public const val PUSHINT_10: Int = 0x7A
187+
public const val PUSHINT_MINUS_5: Int = 0x7B
188+
public const val PUSHINT_MINUS_4: Int = 0x7C
189+
public const val PUSHINT_MINUS_3: Int = 0x7D
190+
public const val PUSHINT_MINUS_2: Int = 0x7E
191+
public const val PUSHINT_MINUS_1: Int = 0x7F
192+
public const val PUSHINT_XX: Int = 0x80
193+
public const val PUSHINT_XXXX: Int = 0x81
194+
public const val PUSHINT_LONG: Int = 0x82
195+
public const val PUSHPOW2: Int = 0x83
196+
public const val PUSHPOW2DEC: Int = 0x84
197+
public const val PUSHNEGPOW2: Int = 0x85
198+
199+
// A.4.2. Constant slices, continuations, cells, and references.
200+
public const val PUSHREF: Int = 0x88
201+
public const val PUSHREFSLICE: Int = 0x89
202+
public const val PUSHREFCONT: Int = 0x8A
203+
public const val PUSHSLICE_SHORT: Int = 0x8B
204+
public const val PUSHSLICE: Int = 0x8C
205+
public const val PUSHSLICE_LONG: Int = 0x8D
206+
public const val PUSHCONT_REF_0_3: Int = 0x8E
207+
public const val PUSHCONT_REF_4: Int = 0x8F
208+
public const val PUSHCONT_0: Int = 0x90
209+
public const val PUSHCONT_1: Int = 0x91
210+
public const val PUSHCONT_2: Int = 0x92
211+
public const val PUSHCONT_3: Int = 0x93
212+
public const val PUSHCONT_4: Int = 0x94
213+
public const val PUSHCONT_5: Int = 0x95
214+
public const val PUSHCONT_6: Int = 0x96
215+
public const val PUSHCONT_7: Int = 0x97
216+
public const val PUSHCONT_8: Int = 0x98
217+
public const val PUSHCONT_9: Int = 0x99
218+
public const val PUSHCONT_10: Int = 0x9A
219+
public const val PUSHCONT_11: Int = 0x9B
220+
public const val PUSHCONT_12: Int = 0x9C
221+
public const val PUSHCONT_13: Int = 0x9D
222+
public const val PUSHCONT_14: Int = 0x9E
223+
public const val PUSHCONT_15: Int = 0x9F
175224
}

vm/src/Stack.kt

Lines changed: 83 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package org.ton.kotlin.tvm
33
import org.ton.bigint.BigInt
44
import org.ton.bigint.sign
55
import org.ton.bigint.toBigInt
6-
import org.ton.kotlin.tvm.exception.StackOverflowException
6+
import org.ton.cell.Cell
7+
import org.ton.cell.CellSlice
78
import org.ton.kotlin.tvm.exception.StackUnderflowException
89

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

1920
public class Stack {
2021
private var elements = arrayOfNulls<Any?>(INCREMENT)
21-
public var top: Int = -1
22-
23-
public val depth: Int get() = top + 1
24-
25-
public operator fun get(offset: Int): Any? {
26-
if (offset < 0 || offset > depth) {
27-
throw StackUnderflowException()
28-
}
29-
return elements[top - offset]
30-
}
3122

32-
public operator fun set(offset: Int, operand: Any) {
33-
if (offset < 0) {
34-
throw StackUnderflowException()
35-
} else if (offset > top) {
36-
throw StackOverflowException()
37-
}
38-
elements[top - offset] = operand
39-
}
23+
public var depth: Int = 0
4024

4125
public fun pop(): Any {
42-
if (top < 0) {
26+
val currentDepth = depth
27+
if (currentDepth == 0) {
4328
throw StackUnderflowException()
4429
}
4530
val elements = elements
46-
val removed = elements[top]
47-
elements[top--] = null
31+
val newDepth = currentDepth - 1
32+
val removed = elements[newDepth]
33+
elements[newDepth] = null
34+
depth = newDepth
4835
return removed as Any
4936
}
5037

@@ -56,33 +43,48 @@ public class Stack {
5643
pushElement(value)
5744
}
5845

46+
public fun pushCell(value: Cell) {
47+
pushElement(value)
48+
}
49+
50+
public fun pushSlice(value: CellSlice) {
51+
pushElement(value)
52+
}
53+
54+
public fun pushContinuation(value: TvmContinuation) {
55+
pushElement(value)
56+
}
57+
5958
public fun pushElement(operand: Any) {
60-
val nextTop = top + 1
59+
val currentDepth = depth
6160
var elements = elements
6261
val currentCapacity = elements.size
63-
if (nextTop >= currentCapacity) {
62+
val nextDepth = currentDepth + 1
63+
if (nextDepth > currentCapacity) {
6464
elements = elements.copyOf(currentCapacity + INCREMENT)
6565
this.elements = elements
6666
}
67-
elements[nextTop] = operand
68-
top = nextTop
67+
elements[currentDepth] = operand
68+
depth = nextDepth
6969
}
7070

7171
public fun pushCopy(offset: Int) {
7272
var elements = elements
73-
val value = elements[top - offset]
74-
val nextTop = top + 1
73+
val currentDepth = depth
74+
val value = elements[currentDepth - 1 - offset]
75+
val nextDepth = currentDepth + 1
7576
val currentCapacity = elements.size
76-
if (nextTop >= currentCapacity) {
77+
if (nextDepth > currentCapacity) {
7778
elements = elements.copyOf(currentCapacity + INCREMENT)
7879
this.elements = elements
7980
}
80-
elements[nextTop] = value
81-
top = nextTop
81+
elements[currentDepth] = value
82+
depth = nextDepth
8283
}
8384

8485
public fun reverse(fromOffset: Int, toOffset: Int) {
8586
val length = toOffset - fromOffset
87+
val top = depth - 1
8688
val fromIndex = top - fromOffset - 1
8789
val toIndex = top - toOffset
8890
val elements = elements
@@ -97,45 +99,57 @@ public class Stack {
9799
}
98100

99101
public fun dropTop(count: Int) {
100-
elements.fill(null, top - count + 1, top + 1)
101-
top -= count
102+
val currentDepth = depth
103+
val newDepth = currentDepth - count
104+
elements.fill(null, newDepth, currentDepth)
105+
depth = newDepth
102106
}
103107

104108
public fun onlyTop(count: Int) {
105109
val elements = elements
106-
val top = top
107-
val toIndex = top + 1
108-
elements.copyInto(elements, 0, top - count + 1, toIndex)
109-
elements.fill(null, count, toIndex)
110-
this.top = count - 1
110+
val currentDepth = depth
111+
elements.copyInto(elements, 0, currentDepth - count, currentDepth)
112+
elements.fill(null, count, currentDepth)
113+
depth = count
111114
}
112115

113116
public fun rot() {
114117
val elements = elements
115-
val a = elements[top - 2]
116-
val b = elements[top - 1]
117-
val c = elements[top]
118-
elements[top - 2] = b
119-
elements[top - 1] = c
120-
elements[top] = a
118+
val depth = depth
119+
val aIndex = depth - 3
120+
val bIndex = depth - 2
121+
val cIndex = depth - 1
122+
val a = elements[aIndex]
123+
val b = elements[bIndex]
124+
val c = elements[cIndex]
125+
elements[aIndex] = b
126+
elements[bIndex] = c
127+
elements[cIndex] = a
121128
}
122129

123130
public fun rotRev() {
124131
val elements = elements
125-
val a = elements[top - 2]
126-
val b = elements[top - 1]
127-
val c = elements[top]
128-
elements[top - 2] = c
129-
elements[top - 1] = a
130-
elements[top] = b
132+
val depth = depth
133+
val aIndex = depth - 3
134+
val bIndex = depth - 2
135+
val cIndex = depth - 1
136+
val a = elements[aIndex]
137+
val b = elements[bIndex]
138+
val c = elements[cIndex]
139+
elements[aIndex] = c
140+
elements[bIndex] = a
141+
elements[cIndex] = b
131142
}
132143

133144
public fun swap(first: Int, second: Int) {
134145
val elements = elements
135-
val firstValue = elements[top - first]
136-
val secondValue = elements[top - second]
137-
elements[top - first] = secondValue
138-
elements[top - second] = firstValue
146+
val depth = depth
147+
val firstIndex = depth - 1 - first
148+
val secondIndex = depth - 1 - second
149+
val firstValue = elements[firstIndex]
150+
val secondValue = elements[secondIndex]
151+
elements[firstIndex] = secondValue
152+
elements[secondIndex] = firstValue
139153
}
140154

141155
/**
@@ -144,16 +158,19 @@ public class Stack {
144158
*/
145159
public fun blockSwap(i: Int, j: Int) {
146160
val elements = elements
147-
val lastElements = elements.copyOfRange(top - j + 1, top + 1)
148-
elements.copyInto(elements, top - i + 1, top - j - i + 1, top - j + 1)
149-
lastElements.copyInto(elements, top - i - j + 1)
161+
val currentDepth = depth
162+
val lastElements = elements.copyOfRange(currentDepth - j, currentDepth)
163+
elements.copyInto(elements, currentDepth - i, currentDepth - j - i, currentDepth - j)
164+
lastElements.copyInto(elements, currentDepth - i - j)
150165
}
151166

152167
public fun blockDrop(i: Int, j: Int) {
153168
val elements = elements
154-
elements.copyInto(elements, top - j - i + 1, top - j + 1, top + 1)
155-
elements.fill(null, top - i + 1, top + 1)
156-
top = top - i
169+
val currentDepth = depth
170+
val newDepth = currentDepth - i
171+
elements.copyInto(elements, newDepth - j, currentDepth - j, currentDepth)
172+
elements.fill(null, newDepth, currentDepth)
173+
depth = newDepth
157174
}
158175

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

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

186+
public fun popSlice(): CellSlice = pop() as CellSlice
187+
169188
override fun toString(): String = buildString {
170189
val elements = elements
171190
append("[ ")
172-
for (i in 0..top) {
191+
for (i in 0 until depth) {
173192
append(elements[i])
174193
append(" ")
175194
}
176-
append("]")
195+
append("] | ${elements.copyOf(16).contentToString()}")
177196
}
178197

179198
override fun equals(other: Any?): Boolean {
180199
if (this === other) return true
181200
if (other == null || this::class != other::class) return false
182201
other as Stack
183-
if (top != other.top) return false
202+
if (depth != other.depth) return false
184203
if (!elements.contentEquals(other.elements)) return false
185204
return true
186205
}
187206

188207
override fun hashCode(): Int {
189-
var result = top
208+
var result = depth
190209
result = 31 * result + elements.contentHashCode()
191210
return result
192211
}

0 commit comments

Comments
 (0)