Begin work on write_module.

This commit is contained in:
Jesse Brault 2024-11-29 19:13:30 -06:00
parent c1618ba9a2
commit 66107b4310

View File

@ -3,8 +3,13 @@ use std::collections::HashMap;
use std::io::Read; use std::io::Read;
pub const DEIMOS_MAGIC_NUMBER: u64 = 0x00_00_64_65_69_6d_6f_73; // ascii 'deimos' pub const DEIMOS_MAGIC_NUMBER: u64 = 0x00_00_64_65_69_6d_6f_73; // ascii 'deimos'
const DEIMOS_MAGIC_STRING: [u8; 6] = [0x64, 0x65, 0x69, 0x6d, 0x6f, 0x73]; // ascii 'deimos'
pub const VERSION_STRING: &str = "0.1.0";
pub struct DmModule { pub struct DmModule {
name: String,
version: String,
constants: HashMap<String, DmConstant>, constants: HashMap<String, DmConstant>,
functions: HashMap<String, DmFunction>, functions: HashMap<String, DmFunction>,
} }
@ -35,6 +40,47 @@ struct DmSymbol {
address: u32, address: u32,
} }
macro_rules! push_byte_array {
( $dest: expr, $arr: expr ) => {
for b in $arr {
$dest.push(b);
}
};
}
macro_rules! push_number_le {
( $dest: expr, $num: expr ) => {
for b in $num.to_le_bytes() {
$dest.push(b);
}
};
}
macro_rules! push_string {
( $dest: expr, $s: expr ) => {
for b in $s.len().to_le_bytes() {
$dest.push(b);
}
for b in $s.bytes() {
$dest.push(b);
}
};
}
pub fn write_module(module: DmModule) -> Vec<u8> {
// Push magic number
let mut result: Vec<u8> = Vec::new();
push_byte_array!(result, DEIMOS_MAGIC_STRING);
// Push version string
push_string!(result, module.version);
// Push module name length, little endian
push_string!(result, module.name);
result
}
pub fn load_module(bytes: &[u8]) -> Result<DmModule, String> { pub fn load_module(bytes: &[u8]) -> Result<DmModule, String> {
let mut ip: usize = 0; let mut ip: usize = 0;
// Check for magic number at bytes 0..5 // Check for magic number at bytes 0..5
@ -62,6 +108,9 @@ pub fn load_module(bytes: &[u8]) -> Result<DmModule, String> {
return Err(String::from("Invalid Deimos module version.")); return Err(String::from("Invalid Deimos module version."));
} }
// TODO: extract module name
// Holder for Symbols we will extract from the symbol table bytes // Holder for Symbols we will extract from the symbol table bytes
let mut symbols: Vec<DmSymbol> = Vec::new(); let mut symbols: Vec<DmSymbol> = Vec::new();