Skip to content

Commit

Permalink
A.5.1. Addition, subtraction, multiplication.
Browse files Browse the repository at this point in the history
Signed-off-by: andreypfau <[email protected]>
  • Loading branch information
andreypfau committed Mar 1, 2025
1 parent bce1d87 commit a9e02f9
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 36 deletions.
2 changes: 2 additions & 0 deletions bigint/src/BigInt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public expect operator fun BigInt.times(other: BigInt): BigInt
public expect operator fun BigInt.div(other: BigInt): BigInt
public expect operator fun BigInt.unaryMinus(): BigInt
public expect operator fun BigInt.rem(other: BigInt): BigInt
public expect operator fun BigInt.inc(): BigInt
public expect operator fun BigInt.dec(): BigInt
public expect infix fun BigInt.shr(shr: Int): BigInt
public expect infix fun BigInt.shl(shl: Int): BigInt
public expect infix fun BigInt.and(and: BigInt): BigInt
Expand Down
4 changes: 4 additions & 0 deletions bigint/src@jvm/BigIntJvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public operator fun BigInt.div(long: Long): BigInt = divide(long.toBigInt())

public actual operator fun BigInt.unaryMinus(): BigInt = negate()

public actual operator fun BigInt.inc(): BigInt = add(BigInteger.ONE)

public actual operator fun BigInt.dec(): BigInt = subtract(BigInteger.ONE)

public actual infix fun BigInt.shr(shr: Int): BigInt = shiftRight(shr)

public actual infix fun BigInt.shl(shl: Int): BigInt = shiftLeft(shl)
Expand Down
6 changes: 6 additions & 0 deletions bigint/src@native/BigInt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public actual operator fun BigInt.unaryMinus(): BigInt =
public actual operator fun BigInt.rem(other: BigInt): BigInt =
BigInt(value % other.value)

public actual operator fun BigInt.inc(): BigInt =
BigInt(value.inc())

public actual operator fun BigInt.dec(): BigInt =
BigInt(value.dec())

public actual infix fun BigInt.shr(shr: Int): BigInt =
BigInt(value shr shr)

Expand Down
35 changes: 35 additions & 0 deletions vm/src/GasConsumer.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
package org.ton.kotlin.tvm

import org.ton.cell.Cell
import kotlin.math.max

public interface GasCalculator {
public fun calculateOperationCost(bitCount: Int): Long

public fun calculateRollCost(elements: Int): Long = max(elements - 255L, 0L)

public fun calculateCellLoad(cell: Cell): Long

public fun calculateCellReload(cell: Cell): Long

public fun calculateCellCreate(cell: Cell): Long

public fun calculateException(): Long

public fun calculateImplicitReturn(): Long
}

public object FreeGasCalculator : GasCalculator {
override fun calculateOperationCost(bitCount: Int): Long = 0

override fun calculateCellLoad(cell: Cell): Long = 0

override fun calculateCellReload(cell: Cell): Long = 0

override fun calculateCellCreate(cell: Cell): Long = 0

override fun calculateException(): Long = 0

override fun calculateImplicitReturn(): Long = 0
}

public object DefaultGasCalculator : GasCalculator {
override fun calculateOperationCost(bitCount: Int): Long = 10 + bitCount.toLong()

override fun calculateCellLoad(cell: Cell): Long = 100

override fun calculateCellReload(cell: Cell): Long = 25

override fun calculateCellCreate(cell: Cell): Long = 500

override fun calculateException(): Long = 50

override fun calculateImplicitReturn(): Long = 5
}
11 changes: 11 additions & 0 deletions vm/src/OpCodes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,15 @@ public object OpCodes {
public const val PUSHCONT_13: Int = 0x9D
public const val PUSHCONT_14: Int = 0x9E
public const val PUSHCONT_15: Int = 0x9F

// A.5.1. Addition, subtraction, multiplication.
public const val ADD: Int = 0xA0
public const val SUB: Int = 0xA1
public const val SUBR: Int = 0xA2
public const val NEGATE: Int = 0xA3
public const val INC: Int = 0xA4
public const val DEC: Int = 0xA5
public const val ADDCONST: Int = 0xA6
public const val MULCONST: Int = 0xA7
public const val MUL: Int = 0xA8
}
14 changes: 13 additions & 1 deletion vm/src/Stack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ private const val INCREMENT = 256
public fun stackOf(vararg elements: Any): Stack {
val stack = Stack()
for (element in elements) {
stack.pushElement(element)
when (element) {
is BigInt -> stack.pushInt(element)
is Number -> stack.pushInt(element.toLong())
is Boolean -> stack.pushBoolean(element)
is Cell -> stack.pushCell(element)
is CellSlice -> stack.pushSlice(element)
is TvmContinuation -> stack.pushContinuation(element)
else -> throw IllegalArgumentException("Unsupported type ${element::class}")
}
}
return stack
}
Expand Down Expand Up @@ -43,6 +51,10 @@ public class Stack {
pushElement(value)
}

public fun pushBoolean(value: Boolean) {
pushElement(if (value) (-1).toBigInt() else 0.toBigInt())
}

public fun pushCell(value: Cell) {
pushElement(value)
}
Expand Down
Loading

0 comments on commit a9e02f9

Please sign in to comment.