WORKING ASM FUNCTION CALLS

This commit is contained in:
2024-10-12 02:24:48 -04:00
parent de79445ede
commit cf9f3469ae
14 changed files with 376 additions and 287 deletions

View File

@@ -1,53 +1,35 @@
use std::collections::HashMap;
pub struct Program {
data: Vec<u8>,
ro_map: HashMap<String, usize>,
}
impl Program {
pub fn new(data: HashMap<String, Vec<u8>>) -> Self {
let mut ro_data = Vec::new();
let mut ro_map = HashMap::new();
for (key, val) in data {
ro_map.insert(key, ro_data.len());
ro_data.extend(val);
}
Self {
data: ro_data,
ro_map,
}
}
}
pub fn create_program<I: Instr>(
ro_data: HashMap<String, Vec<u8>>,
functions: Vec<Function<I>>,
) -> Vec<u8> {
) -> (Vec<u8>, Option<u64>) {
let mut data = Vec::new();
let mut ro_map = HashMap::new();
let mut sym_map = HashMap::new();
for (key, val) in ro_data {
ro_map.insert(key, data.len());
sym_map.insert(key, data.len() as u64);
data.extend(val);
}
// let mut fn_map = HashMap::new();
let mut start = None;
for fun in functions {
if fun.label == "_start" {
start = Some(data.len() as u64);
}
sym_map.insert(fun.label, data.len() as u64);
for i in fun.instructions {
data.extend(i.to_le_bytes());
let pos = data.len() as u64;
i.push(&mut data, &sym_map, pos);
}
}
data
(data, start)
}
pub struct Function<I: Instr> {
label: String,
instructions: Vec<I>,
pub label: String,
pub instructions: Vec<I>,
}
pub trait Instr {
fn to_le_bytes(&self) -> impl IntoIterator<Item = u8>;
fn push(&self, data: &mut Vec<u8>, ro_map: &HashMap<String, u64>, pos: u64) -> Option<String>;
}
struct SymbolInstr {
i: usize
}