travel fn path for compilation
This commit is contained in:
@@ -1,3 +1 @@
|
||||
pub mod riscv64;
|
||||
use super::*;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{compiler::program::{Addr, Instr, SymTable}, ir::AddrID};
|
||||
use crate::{compiler::program::{Addr, Instr, SymTable}, ir::Symbol};
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -12,10 +12,10 @@ pub enum LinkerInstruction {
|
||||
Sd { src: Reg, offset: i32, base: Reg },
|
||||
Ld { dest: Reg, offset: i32, base: Reg },
|
||||
Mv { dest: Reg, src: Reg },
|
||||
La { dest: Reg, src: AddrID },
|
||||
La { dest: Reg, src: Symbol },
|
||||
Jal { dest: Reg, offset: i32 },
|
||||
Call(AddrID),
|
||||
J(AddrID),
|
||||
Call(Symbol),
|
||||
J(Symbol),
|
||||
Ret,
|
||||
Ecall,
|
||||
Li { dest: Reg, imm: i64 },
|
||||
@@ -28,7 +28,7 @@ impl Instr for LinkerInstruction {
|
||||
sym_map: &SymTable,
|
||||
pos: Addr,
|
||||
missing: bool,
|
||||
) -> Option<AddrID> {
|
||||
) -> Option<Symbol> {
|
||||
let last = match self {
|
||||
Self::Add { dest, src1, src2 } => add(*dest, *src1, *src2),
|
||||
Self::Addi { dest, src, imm } => addi(*dest, *src, BitsI32::new(*imm)),
|
||||
|
||||
@@ -13,11 +13,10 @@ use super::{LinkerInstruction as LI, *};
|
||||
pub fn compile(program: IRLProgram) -> (Vec<u8>, Option<Addr>) {
|
||||
let mut fns = Vec::new();
|
||||
let mut data = Vec::new();
|
||||
for d in program.data {
|
||||
data.push((d.data, d.addr));
|
||||
for (sym, d) in program.ro_data() {
|
||||
data.push((d.clone(), *sym));
|
||||
}
|
||||
let mut start = None;
|
||||
for f in program.fns {
|
||||
for (sym, f) in program.fns() {
|
||||
let mut v = Vec::new();
|
||||
let mut stack = HashMap::new();
|
||||
let mut stack_len = 0;
|
||||
@@ -106,12 +105,10 @@ pub fn compile(program: IRLProgram) -> (Vec<u8>, Option<Addr>) {
|
||||
IRI::Ret { src } => todo!(),
|
||||
}
|
||||
}
|
||||
if f.name == "start" {
|
||||
start = Some(f.addr);
|
||||
} else {
|
||||
if *sym != program.entry() {
|
||||
v.push(LI::Ret);
|
||||
}
|
||||
fns.push((v, f.addr));
|
||||
fns.push((v, *sym));
|
||||
}
|
||||
create_program(fns, data, start)
|
||||
create_program(fns, data, Some(program.entry()))
|
||||
}
|
||||
|
||||
@@ -1,63 +1,17 @@
|
||||
mod asm;
|
||||
mod base;
|
||||
mod compile;
|
||||
mod funct;
|
||||
mod opcode;
|
||||
mod reg;
|
||||
mod single;
|
||||
mod compile;
|
||||
|
||||
use crate::util::BitsI32;
|
||||
pub use asm::*;
|
||||
use base::*;
|
||||
use funct::{op::*, width};
|
||||
use opcode::*;
|
||||
pub use reg::*;
|
||||
pub use compile::*;
|
||||
|
||||
use single::*;
|
||||
|
||||
pub fn gen() -> Vec<u8> {
|
||||
// use asm::LinkerInstruction as I;
|
||||
// let mut table = SymMap::new();
|
||||
// let (msg, len) = table.push_ro_data_size(b"Hello world!\n".to_vec());
|
||||
// let (msg2, len2) = table.push_ro_data_size(b"IT WORKS!!!!\n".to_vec());
|
||||
// let print_stuff = table.reserve();
|
||||
// let start = table.push_fn(vec![
|
||||
// I::Call(*print_stuff),
|
||||
// I::Li { dest: a0, imm: 0 },
|
||||
// I::Li { dest: a7, imm: 93 },
|
||||
// I::Ecall,
|
||||
// I::Jal {
|
||||
// dest: zero,
|
||||
// offset: 0,
|
||||
// },
|
||||
// ]);
|
||||
// table.write_fn(
|
||||
// print_stuff,
|
||||
// vec![
|
||||
// I::Li { dest: a0, imm: 1 },
|
||||
// I::La { dest: a1, src: msg },
|
||||
// I::Li {
|
||||
// dest: a2,
|
||||
// imm: len as i64,
|
||||
// },
|
||||
// I::Li { dest: a7, imm: 64 },
|
||||
// I::Ecall,
|
||||
// I::Li { dest: a0, imm: 1 },
|
||||
// I::La {
|
||||
// dest: a1,
|
||||
// src: msg2,
|
||||
// },
|
||||
// I::Li {
|
||||
// dest: a2,
|
||||
// imm: len2 as i64,
|
||||
// },
|
||||
// I::Li { dest: a7, imm: 64 },
|
||||
// I::Ecall,
|
||||
// I::Ret,
|
||||
// ],
|
||||
// );
|
||||
// let (program, start) = create_program(table, Some(start));
|
||||
// elf::create(program, start.expect("no start!"))
|
||||
todo!("remove this");
|
||||
}
|
||||
pub use asm::*;
|
||||
pub use compile::*;
|
||||
pub use reg::*;
|
||||
|
||||
@@ -1,10 +1,3 @@
|
||||
use std::{
|
||||
fs::{create_dir_all, OpenOptions},
|
||||
os::unix::fs::OpenOptionsExt,
|
||||
path::Path,
|
||||
process::Command,
|
||||
};
|
||||
|
||||
pub mod arch;
|
||||
mod elf;
|
||||
mod program;
|
||||
@@ -16,58 +9,5 @@ use crate::ir::IRLProgram;
|
||||
|
||||
pub fn compile(program: IRLProgram) -> Vec<u8> {
|
||||
let (compiled, start) = arch::riscv64::compile(program);
|
||||
let binary = elf::create(compiled, start.expect("no start method found"));
|
||||
binary
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
use std::io::prelude::*;
|
||||
let dir = Path::new("./build");
|
||||
create_dir_all(dir).expect("Failed to create or confirm build directory");
|
||||
let name = Path::new("test");
|
||||
let path = dir.join(name);
|
||||
let path = path.as_os_str();
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.mode(0o750)
|
||||
.open(path)
|
||||
.expect("Failed to create file");
|
||||
file.write_all(&arch::riscv64::gen())
|
||||
.expect("Failed to write to file");
|
||||
file.sync_all().expect("Failed to sync file");
|
||||
let mut p = Command::new("qemu-riscv64");
|
||||
let run_gdb = std::env::args().nth(1).is_some_and(|a| a == "d");
|
||||
let proc = if run_gdb {
|
||||
p.arg("-g").arg("1234").arg(path).spawn()
|
||||
} else {
|
||||
p.arg(path).spawn()
|
||||
};
|
||||
if let Ok(mut process) = proc {
|
||||
let mut print_exit = true;
|
||||
if run_gdb {
|
||||
match Command::new("gdb")
|
||||
.arg("-q")
|
||||
.arg("-ex")
|
||||
.arg("target remote :1234")
|
||||
.arg(path)
|
||||
.spawn()
|
||||
{
|
||||
Ok(mut gdb) => {
|
||||
gdb.wait().expect("xd");
|
||||
}
|
||||
Err(e) => {
|
||||
print_exit = false;
|
||||
println!("gdb error: {e:?}");
|
||||
process.kill().expect("uh oh");
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Ok(status) = process.wait() {
|
||||
if print_exit && status.code().is_none_or(|c| c != 0) {
|
||||
println!("{}", status);
|
||||
}
|
||||
}
|
||||
}
|
||||
elf::create(compiled, start.expect("no start method found"))
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
use std::{collections::HashMap, ops::Deref};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::ir::AddrID;
|
||||
use crate::ir::Symbol;
|
||||
|
||||
pub fn create_program<I: Instr>(
|
||||
fns: Vec<(Vec<I>, AddrID)>,
|
||||
ro_data: Vec<(Vec<u8>, AddrID)>,
|
||||
start: Option<AddrID>,
|
||||
fns: Vec<(Vec<I>, Symbol)>,
|
||||
ro_data: Vec<(Vec<u8>, Symbol)>,
|
||||
start: Option<Symbol>,
|
||||
) -> (Vec<u8>, Option<Addr>) {
|
||||
let mut data = Vec::new();
|
||||
let mut sym_table = SymTable::new(fns.len() + ro_data.len());
|
||||
let mut missing = HashMap::<AddrID, Vec<(Addr, I)>>::new();
|
||||
let mut missing = HashMap::<Symbol, Vec<(Addr, I)>>::new();
|
||||
for (val, id) in ro_data {
|
||||
sym_table.insert(id, Addr(data.len() as u64));
|
||||
data.extend(val);
|
||||
@@ -45,7 +45,7 @@ pub fn create_program<I: Instr>(
|
||||
|
||||
pub trait Instr {
|
||||
fn push(&self, data: &mut Vec<u8>, syms: &SymTable, pos: Addr, missing: bool)
|
||||
-> Option<AddrID>;
|
||||
-> Option<Symbol>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
@@ -57,75 +57,16 @@ impl Addr {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
|
||||
pub struct Symbol(usize);
|
||||
/// intentionally does not have copy or clone;
|
||||
/// this should only be consumed once
|
||||
pub struct WritableSymbol(Symbol);
|
||||
|
||||
impl Deref for WritableSymbol {
|
||||
type Target = Symbol;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SymMap<I> {
|
||||
i: usize,
|
||||
ro_data: Vec<(Vec<u8>, Symbol)>,
|
||||
functions: Vec<(Vec<I>, Symbol)>,
|
||||
}
|
||||
|
||||
impl<I> SymMap<I> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
i: 0,
|
||||
ro_data: Vec::new(),
|
||||
functions: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn push_ro_data(&mut self, data: Vec<u8>) -> Symbol {
|
||||
let sym = self.reserve();
|
||||
self.write_ro_data(sym, data.into())
|
||||
}
|
||||
pub fn push_ro_data_size(&mut self, data: Vec<u8>) -> (Symbol, usize) {
|
||||
let sym = self.reserve();
|
||||
let len = data.len();
|
||||
(self.write_ro_data(sym, data), len)
|
||||
}
|
||||
pub fn push_fn(&mut self, instructions: Vec<I>) -> Symbol {
|
||||
let sym = self.reserve();
|
||||
self.write_fn(sym, instructions)
|
||||
}
|
||||
pub fn write_ro_data(&mut self, sym: WritableSymbol, data: Vec<u8>) -> Symbol {
|
||||
let data = data.into();
|
||||
self.ro_data.push((data, *sym));
|
||||
*sym
|
||||
}
|
||||
pub fn write_fn(&mut self, sym: WritableSymbol, instructions: Vec<I>) -> Symbol {
|
||||
self.functions.push((instructions, *sym));
|
||||
*sym
|
||||
}
|
||||
pub fn reserve(&mut self) -> WritableSymbol {
|
||||
let val = self.i;
|
||||
self.i += 1;
|
||||
WritableSymbol(Symbol(val))
|
||||
}
|
||||
pub fn len(&self) -> usize {
|
||||
self.functions.len() + self.ro_data.len()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SymTable(Vec<Addr>);
|
||||
impl SymTable {
|
||||
pub fn new(len: usize) -> Self {
|
||||
Self(vec![Addr::NONE; len])
|
||||
}
|
||||
pub fn insert(&mut self, sym: AddrID, addr: Addr) {
|
||||
self.0[sym.0] = addr;
|
||||
pub fn insert(&mut self, sym: Symbol, addr: Addr) {
|
||||
self.0[*sym] = addr;
|
||||
}
|
||||
pub fn get(&self, sym: AddrID) -> Option<Addr> {
|
||||
match self.0[sym.0] {
|
||||
pub fn get(&self, sym: Symbol) -> Option<Addr> {
|
||||
match self.0[*sym] {
|
||||
Addr::NONE => None,
|
||||
addr => Some(addr),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user