Skip to content

Commit

Permalink
add pvm instructions 5.10, 5.11, 5.12 (#84)
Browse files Browse the repository at this point in the history
* fix name

* add some instructions

* add A.5.12 instructions

* add to table

* fix

* add Pvm Config protocol

* Update PolkaVM/Sources/PolkaVM/Instructions.swift

Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>

* fix

---------

Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>
  • Loading branch information
qiweiii and xlc authored Aug 29, 2024
1 parent f3f0501 commit 624316d
Show file tree
Hide file tree
Showing 8 changed files with 716 additions and 33 deletions.
2 changes: 2 additions & 0 deletions Blockchain/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let package = Package(
.package(path: "../Codec"),
.package(path: "../Utils"),
.package(path: "../TracingUtils"),
.package(path: "../PolkaVM"),
.package(url: "https://github.com/apple/swift-testing.git", branch: "0.10.0"),
],
targets: [
Expand All @@ -29,6 +30,7 @@ let package = Package(
dependencies: [
"Codec",
"Utils",
"PolkaVM",
"TracingUtils",
]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension Ref where T == ProtocolConfig {
maxEncodedWorkReportSize: 96 * 1 << 10,
erasureCodedSegmentSize: 6,
ticketSubmissionEndSlot: 500,
pvmDynamicAddressAlignmentFactor: 4,
pvmDynamicAddressAlignmentFactor: 2,
pvmProgramInitInputDataSize: 1 << 24,
pvmProgramInitPageSize: 1 << 14,
pvmProgramInitSegmentSize: 1 << 16
Expand Down Expand Up @@ -70,7 +70,7 @@ extension Ref where T == ProtocolConfig {
maxEncodedWorkReportSize: 96 * 1 << 10,
erasureCodedSegmentSize: 6,
ticketSubmissionEndSlot: 500,
pvmDynamicAddressAlignmentFactor: 4,
pvmDynamicAddressAlignmentFactor: 2,
pvmProgramInitInputDataSize: 1 << 24,
pvmProgramInitPageSize: 1 << 14,
pvmProgramInitSegmentSize: 1 << 16
Expand Down
5 changes: 4 additions & 1 deletion Blockchain/Sources/Blockchain/Config/ProtocolConfig.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import PolkaVM
import Utils

// constants defined in the graypaper
Expand Down Expand Up @@ -94,7 +95,7 @@ public struct ProtocolConfig: Sendable {
// Y = 500: The number of slots into an epoch at which ticket-submission ends.
public var ticketSubmissionEndSlot: Int

// ZA = 4: The pvm dynamic address alignment factor.
// ZA = 2: The pvm dynamic address alignment factor.
public var pvmDynamicAddressAlignmentFactor: Int

// ZI = 2^24: The standard pvm program initialization input data size.
Expand Down Expand Up @@ -181,6 +182,8 @@ public struct ProtocolConfig: Sendable {

public typealias ProtocolConfigRef = Ref<ProtocolConfig>

extension ProtocolConfig: PvmConfig {}

extension ProtocolConfig {
public enum AuditTranchePeriod: ReadInt {
public typealias TConfig = ProtocolConfigRef
Expand Down
13 changes: 13 additions & 0 deletions PolkaVM/Sources/PolkaVM/Config.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public protocol PvmConfig {
// ZA = 2: The pvm dynamic address alignment factor.
var pvmDynamicAddressAlignmentFactor: Int { get }

// ZI = 2^24: The standard pvm program initialization input data size.
var pvmProgramInitInputDataSize: Int { get }

// ZP = 2^14: The standard pvm program initialization page size.
var pvmProgramInitPageSize: Int { get }

// ZQ = 2^16: The standard pvm program initialization segment size.
var pvmProgramInitSegmentSize: Int { get }
}
6 changes: 0 additions & 6 deletions PolkaVM/Sources/PolkaVM/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ public class Engine {

let res = inst.execute(state: state, skip: skip)

if state.pc == Constants.exitAddress {
// TODO: GP only defined this for `djump` but not `branch`
// so need to confirm this is correct
return .exit(.halt)
}

return res
}
}
57 changes: 57 additions & 0 deletions PolkaVM/Sources/PolkaVM/InstructionTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,63 @@ public class InstructionTable {
Instructions.BranchLeSImm.self,
Instructions.BranchGeSImm.self,
Instructions.BranchGtSImm.self,
Instructions.MoveReg.self,
Instructions.Sbrk.self,
Instructions.StoreIndU8.self,
Instructions.StoreIndU16.self,
Instructions.StoreIndU32.self,
Instructions.LoadIndU8.self,
Instructions.LoadIndI8.self,
Instructions.LoadIndU16.self,
Instructions.LoadIndI16.self,
Instructions.LoadIndU32.self,
Instructions.AddImm.self,
Instructions.AndImm.self,
Instructions.XorImm.self,
Instructions.OrImm.self,
Instructions.MulImm.self,
Instructions.MulUpperSSImm.self,
Instructions.MulUpperUUImm.self,
Instructions.SetLtUImm.self,
Instructions.SetLtSImm.self,
Instructions.ShloLImm.self,
Instructions.ShloRImm.self,
Instructions.SharRImm.self,
Instructions.NegAddImm.self,
Instructions.SetGtUImm.self,
Instructions.SetGtSImm.self,
Instructions.ShloLImmAlt.self,
Instructions.ShloRImmAlt.self,
Instructions.SharRImmAlt.self,
Instructions.CmovIzImm.self,
Instructions.CmovNzImm.self,
Instructions.BranchEq.self,
Instructions.BranchNe.self,
Instructions.BranchLtU.self,
Instructions.BranchLtS.self,
Instructions.BranchGeU.self,
Instructions.BranchGeS.self,
Instructions.LoadImmJumpInd.self,
Instructions.Add.self,
Instructions.Sub.self,
Instructions.And.self,
Instructions.Xor.self,
Instructions.Or.self,
Instructions.Mul.self,
Instructions.MulUpperSS.self,
Instructions.MulUpperUU.self,
Instructions.MulUpperSU.self,
Instructions.DivU.self,
Instructions.DivS.self,
Instructions.RemU.self,
Instructions.RemS.self,
Instructions.SetLtU.self,
Instructions.SetLtS.self,
Instructions.ShloL.self,
Instructions.ShloR.self,
Instructions.SharR.self,
Instructions.CmovIz.self,
Instructions.CmovNz.self,
]
var table: [Instruction.Type?] = Array(repeating: nil, count: 256)
for i in 0 ..< insts.count {
Expand Down
Loading

0 comments on commit 624316d

Please sign in to comment.