Repository: nexus-xyz/nexus-zkvm
File: vm/src/eval.rs (Rust)

Code before:
                SW => LW,
            };

            let (_, proof) = vm.mem.load(lop, addr)?;
            vm.read_proof = Some(proof);
            vm.write_proof = Some(vm.mem.store(sop, addr, Y)?);
        }
        ALUI { aop, rd, rs1, imm } => {
            RD = rd;
            let X = vm.get_reg(rs1);
            vm.Z = alu_op(aop, X, imm);
        }
        ALU { aop, rd, rs1, rs2 } => {
            let X = vm.get_reg(rs1);
            let Y = vm.get_reg(rs2);
            RD = rd;
            vm.Z = alu_op(aop, X, Y);
        }
        FENCE => {}
        EBREAK { .. } => {}
        ECALL { rd } => {
            RD = rd;
            vm.Z = vm.syscalls.syscall(vm.regs.pc, vm.regs.x, &vm.mem)?;

            // Collects label from syscall and starts counting cycles
            if vm.Z == (SyscallCode::ProfileCycles as u32) {
                if let Some(s) = vm.syscalls.get_label() {
                    let label = std::str::from_utf8(&s).unwrap();
                    let fn_name = label.split(':').last().unwrap().to_string();

                    match label.chars().next() {
                        Some('^') => {
                            // Start of the benchmark label
                            vm.cycle_tracker
                                .entry(fn_name)
                                .or_insert((vm.global_clk, 0))
                                .1 += 1;
                        }
                        Some('$') => {
                            // End of the benchmark label
                            // Write the total clock cycles this function takes.
                            let (clk, counter) = vm
                                .cycle_tracker
                                .get_mut(&fn_name)
                                .ok_or(NexusVMError::BenchmarkLabelError)?;
                            *counter -= 1;
                            if *counter == 0 {
                                *clk = vm.global_clk - *clk;
                            }
                        }
                        _ => (),

Review comment (refactor):
Code-placement wise, it makes it such that this syscall implementation is part of vm, and others are in `Syscalls`. This functionality should be extracted and tested, including scenarios with bad input and not following the `^$` script.

Code after:
        }
        AUIPC { rd, imm } => {
            RD = rd;
            vm.Z = add32(vm.regs.pc, imm);
        }
        JAL { rd, imm } => {
            RD = rd;
            vm.Z = add32(vm.regs.pc, 4);
            PC = add32(vm.regs.pc, imm);
        }
        JALR { rd, rs1, imm } => {
            let X = vm.get_reg(rs1);
            RD = rd;
            vm.Z = add32(vm.regs.pc, 4);
            PC = add32(X, imm);
        }
        BR { bop, rs1, rs2, imm } => {
            let X = vm.get_reg(rs1);
            let Y = vm.get_reg(rs2);

            if br_op(bop, X, Y) {
                PC = add32(vm.regs.pc, imm);
            }
        }
        LOAD { lop, rd, rs1, imm } => {
            let X = vm.get_reg(rs1);
            RD = rd;

            let addr = add32(X, imm);
            let (val, proof) = vm.mem.load(lop, addr)?;
            vm.read_proof = Some(proof);
            vm.Z = val;
        }
        STORE { sop, rs1, rs2, imm } => {
            let X = vm.get_reg(rs1);
            let Y = vm.get_reg(rs2);

            let addr = add32(X, imm);
            let lop = match sop {
                SB => LB,
                SH => LH,
                SW => LW,
            };

            let (_, proof) = vm.mem.load(lop, addr)?;
            vm.read_proof = Some(proof);
            vm.write_proof = Some(vm.mem.store(sop, addr, Y)?);
        }
        ALUI { aop, rd, rs1, imm } => {
            RD = rd;
            let X = vm.get_reg(rs1);