Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

64-bit #12

Merged
merged 9 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ jobs:
with:
repository: FluffyLabs/jamtestvectors
path: "./jamtestvectors"
ref: 0746da541814a6e1bb5da0ece4b1d249a10e5a13 # TODO Temporary 64-bit
- run: npm start ./jamtestvectors/pvm/programs/*.json
7 changes: 4 additions & 3 deletions assembly/api-generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class InitialChunk {
}

export class VmInput {
registers: u32[] = new Array<u32>(NO_OF_REGISTERS).fill(0);
registers: u64[] = new Array<u64>(NO_OF_REGISTERS).fill(0);
pc: u32 = 0;
gas: i64 = 0;
program: u8[] = [];
Expand All @@ -27,7 +27,7 @@ export class VmInput {

export class VmOutput {
status: Status = Status.OK;
registers: u32[] = [];
registers: u64[] = [];
pc: u32 = 0;
memory: InitialChunk[] = [];
gas: i64 = 0;
Expand Down Expand Up @@ -63,7 +63,7 @@ export function getAssembly(p: Program): string {

const args = decodeArguments(iData.kind, p.code.subarray(i + 1, end));
const argsArray = [args.a, args.b, args.c, args.d];
const relevantArgs = <i32>RELEVANT_ARGS[iData.kind];
const relevantArgs = RELEVANT_ARGS[iData.kind];
for (let i = 0; i < relevantArgs; i++) {
v += ` ${argsArray[i]}, `;
}
Expand All @@ -88,6 +88,7 @@ export function runVm(input: VmInput, logs: boolean = false): VmOutput {
let isOk = true;
for (;;) {
if (!isOk) {
if (logs) console.log(`REGISTERS = ${registers.join(", ")} (final)`);
if (logs) console.log(`Finished with status: ${int.status}`);
break;
}
Expand Down
8 changes: 3 additions & 5 deletions assembly/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Gas } from "./gas";
import { Interpreter, Status } from "./interpreter";
import { Access, PAGE_SIZE } from "./memory-page";
import { decodeProgram, liftBytes } from "./program";
import { NO_OF_REGISTERS, Registers } from "./registers";
import { NO_OF_REGISTERS, REG_SIZE_BYTES, Registers } from "./registers";

let interpreter: Interpreter | null = null;

Expand Down Expand Up @@ -106,8 +106,6 @@ export function setGasLeft(gas: i64): void {
}
}

const REG_SIZE_BYTES = 4;

export function getRegisters(): Uint8Array {
const flat = new Uint8Array(NO_OF_REGISTERS * REG_SIZE_BYTES).fill(0);
if (interpreter === null) {
Expand Down Expand Up @@ -167,10 +165,10 @@ function fillRegisters(registers: Registers, flat: u8[]): void {
}

for (let i = 0; i < registers.length; i++) {
let num: u32 = 0;
let num: u64 = 0;
for (let j: u8 = 0; j < <u8>REG_SIZE_BYTES; j++) {
const index = i * REG_SIZE_BYTES + j;
num |= (<u32>flat[index]) << (j * 8);
num |= (<u64>flat[index]) << (j * 8);
}
registers[i] = num;
}
Expand Down
39 changes: 30 additions & 9 deletions assembly/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ export enum Arguments {
TwoImm = 2,
OneOff = 3,
OneRegOneImm = 4,
OneRegTwoImm = 5,
OneRegOneImmOneOff = 6,
TwoReg = 7,
TwoRegOneImm = 8,
TwoRegOneOff = 9,
TwoRegTwoImm = 10,
ThreeReg = 11,
OneRegOneExtImm = 5,
OneRegTwoImm = 6,
OneRegOneImmOneOff = 7,
TwoReg = 8,
TwoRegOneImm = 9,
TwoRegOneOff = 10,
TwoRegTwoImm = 11,
ThreeReg = 12,
}

/** How many numbers in `Args` is relevant for given `Arguments`. */
export const RELEVANT_ARGS = [<u8>0, 1, 2, 1, 2, 3, 3, 2, 3, 3, 4, 3];
export const RELEVANT_ARGS = [<i32>0, 1, 2, 1, 2, 3, 3, 3, 2, 3, 3, 4, 3];
/** How many bytes is required by given `Arguments`. */
export const REQUIRED_BYTES = [<i32>0, 0, 0, 0, 1, 9, 1, 1, 1, 1, 1, 1, 2];

// @unmanaged
export class Args {
Expand Down Expand Up @@ -65,6 +68,13 @@ export const DECODERS: ArgsDecoder[] = [
(data: Uint8Array) => {
return asArgs(nibbles(data[0]).low, decodeI32(data.subarray(1)), 0, 0);
},
// DECODERS[Arguments.OneRegOneExtImm] =
(data: Uint8Array) => {
const a = nibbles(data[0]).low;
const b = decodeU32(data.subarray(1));
const c = decodeU32(data.subarray(5));
return asArgs(a, b, c, 0);
},
//DECODERS[Arguments.OneRegTwoImm] =
(data: Uint8Array) => {
const first = nibbles(data[0]);
Expand All @@ -76,7 +86,10 @@ export const DECODERS: ArgsDecoder[] = [
// DECODERS[Arguments.OneRegOneImmOneOff] =
(data: Uint8Array) => {
const n = nibbles(data[0]);
return asArgs(n.low, decodeI32(data.subarray(1, 1 + n.hig)), decodeI32(data.subarray(1 + n.hig)), 0);
const split = n.hig + 1;
const immA = decodeI32(data.subarray(1, split));
const offs = decodeI32(data.subarray(split));
return asArgs(n.low, immA, offs, 0);
},
// DECODERS[Arguments.TwoReg] =
(data: Uint8Array) => {
Expand Down Expand Up @@ -138,3 +151,11 @@ function decodeI32(data: Uint8Array): u32 {
}
return num;
}

function decodeU32(data: Uint8Array): u32 {
let num = u32(data[0]);
num |= u32(data[1]) << 8;
num |= u32(data[2]) << 16;
num |= u32(data[3]) << 24;
return num;
}
2 changes: 1 addition & 1 deletion assembly/gas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TODO [ToDr] Unions not supported.
/** Gas type. */
export type Gas = u64;

/** Create a new gas counter instance depending on the gas value. */
Expand Down
2 changes: 1 addition & 1 deletion assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { VmInput, getAssembly, runVm } from "./api-generic";
import { decodeProgram, decodeSpi, liftBytes } from "./program";

export * from "./api";
export { runVm } from "./api-generic";
export { runVm, getAssembly } from "./api-generic";

export enum InputKind {
Generic = 0,
Expand Down
Loading
Loading