#![allow(
dead_code,
clippy::unwrap_used,
reason = "test-support code: each test crate uses a subset, and a bad value should fail loudly"
)]
use codegen_lang::{Op, Program};
use linker_lang::Object;
#[must_use]
pub fn encode(program: &Program) -> Vec<u8> {
program.ops().iter().map(opcode).collect()
}
fn opcode(op: &Op) -> u8 {
match op {
Op::Const { .. } => 0x01,
Op::Bin { .. } => 0x02,
Op::Un { .. } => 0x03,
Op::Move { .. } => 0x04,
Op::Jump { .. } => 0x05,
Op::JumpUnless { .. } => 0x06,
Op::Return { .. } => 0x07,
}
}
#[must_use]
pub fn object_for(program: &Program) -> Object {
let mut object = Object::new(program.name());
object.section(".text", encode(program));
object.define(program.name(), ".text", 0);
object
}
#[must_use]
pub fn read_u64(bytes: &[u8], at: usize) -> u64 {
u64::from_le_bytes(bytes[at..at + 8].try_into().unwrap())
}
#[must_use]
pub fn read_u32(bytes: &[u8], at: usize) -> u32 {
u32::from_le_bytes(bytes[at..at + 4].try_into().unwrap())
}