make stmt a single word big

This commit is contained in:
nora 2022-04-15 19:04:43 +02:00
parent 1e1a2a277b
commit 7dd2c82fa4
4 changed files with 22 additions and 17 deletions

View file

@ -2,15 +2,15 @@ use crate::codegen::{Code, Stmt};
use std::io::{Read, Write};
use std::num::Wrapping;
const MEM_SIZE: usize = 32_000;
const MEM_SIZE: u32 = 32_000;
type Memory = [Wrapping<u8>; MEM_SIZE];
type Memory = [Wrapping<u8>; MEM_SIZE as usize];
// TODO maybe repr(C) to prevent field reordering?
struct Interpreter<'c, W, R> {
code: &'c Code<'c>,
ip: usize,
ptr: usize,
ip: u32,
ptr: u32,
stdout: W,
stdin: R,
mem: Memory,
@ -27,7 +27,7 @@ where
ptr: 0,
stdout,
stdin,
mem: [Wrapping(0u8); MEM_SIZE],
mem: [Wrapping(0u8); MEM_SIZE as usize],
};
// SAFETY: `Code` can only be produced by the `crate::codegen` module, which is trusted to not
@ -44,8 +44,8 @@ impl<'c, W: Write, R: Read> Interpreter<'c, W, R> {
// SAFETY: If the code ends with an `End` and there are no out of bounds jumps,
// `self.ip` will never be out of bounds
// Removing this bounds check speeds up execution by about 40%
debug_assert!(self.ip < stmts.len());
let instr = unsafe { *stmts.get_unchecked(self.ip) };
debug_assert!((self.ip as usize) < stmts.len());
let instr = unsafe { *stmts.get_unchecked(self.ip as usize) };
self.ip += 1;
match instr {
Stmt::Add(n) => {
@ -97,10 +97,10 @@ impl<'c, W: Write, R: Read> Interpreter<'c, W, R> {
}
fn elem_mut(&mut self) -> &mut Wrapping<u8> {
&mut self.mem[self.ptr]
&mut self.mem[self.ptr as usize]
}
fn elem(&self) -> u8 {
self.mem[self.ptr].0
self.mem[self.ptr as usize].0
}
}