cargo fix + cargo fmt
This commit is contained in:
parent
3023e578b4
commit
d17d946fec
@ -1,5 +1,3 @@
|
|||||||
use crate::cpu::Word;
|
|
||||||
|
|
||||||
type RegisterMem = String;
|
type RegisterMem = String;
|
||||||
|
|
||||||
pub type ConstId = String;
|
pub type ConstId = String;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use super::{AST::*};
|
use super::AST::*;
|
||||||
use crate::cpu::{get_num, OP::*, get_memo};
|
use crate::cpu::get_num;
|
||||||
|
|
||||||
pub trait CodeFormat {
|
pub trait CodeFormat {
|
||||||
fn encode_op(op: &Operation, sy: &SymbolTable, current_pc: u16) -> Option<Self>
|
fn encode_op(op: &Operation, sy: &SymbolTable, current_pc: u16) -> Option<Self>
|
||||||
@ -57,7 +57,6 @@ impl CodeFormat for u16 {
|
|||||||
return Some((0b0011 << 12) + (r1b << 8) + (r2b << 4) + r3b);
|
return Some((0b0011 << 12) + (r1b << 8) + (r2b << 4) + r3b);
|
||||||
}
|
}
|
||||||
Operation::XOR(r1, r2, r3) => {
|
Operation::XOR(r1, r2, r3) => {
|
||||||
|
|
||||||
let (r1b, r2b, r3b) = (
|
let (r1b, r2b, r3b) = (
|
||||||
get_num(&r1)? as u16,
|
get_num(&r1)? as u16,
|
||||||
get_num(&r2)? as u16,
|
get_num(&r2)? as u16,
|
||||||
@ -112,7 +111,7 @@ impl CodeFormat for u16 {
|
|||||||
Const::C(n) => (*n) as u16,
|
Const::C(n) => (*n) as u16,
|
||||||
};
|
};
|
||||||
return Some((0b0110 << 12) + (r1b << 8) + cb);
|
return Some((0b0110 << 12) + (r1b << 8) + cb);
|
||||||
},
|
}
|
||||||
Operation::ADDI(r1, c) => {
|
Operation::ADDI(r1, c) => {
|
||||||
let r1b = get_num(&r1)? as u16;
|
let r1b = get_num(&r1)? as u16;
|
||||||
let cb = match c {
|
let cb = match c {
|
||||||
@ -120,7 +119,7 @@ impl CodeFormat for u16 {
|
|||||||
Const::C(n) => (*n) as u16,
|
Const::C(n) => (*n) as u16,
|
||||||
};
|
};
|
||||||
return Some((0b0111 << 12) + (r1b << 8) + cb);
|
return Some((0b0111 << 12) + (r1b << 8) + cb);
|
||||||
},
|
}
|
||||||
Operation::CALL(r1, c) => {
|
Operation::CALL(r1, c) => {
|
||||||
let r1b = get_num(&r1)? as u16;
|
let r1b = get_num(&r1)? as u16;
|
||||||
let cb = match c {
|
let cb = match c {
|
||||||
@ -128,7 +127,7 @@ impl CodeFormat for u16 {
|
|||||||
Const::C(n) => (*n) as u16,
|
Const::C(n) => (*n) as u16,
|
||||||
};
|
};
|
||||||
return Some((0b1110 << 12) + (r1b << 8) + cb);
|
return Some((0b1110 << 12) + (r1b << 8) + cb);
|
||||||
},
|
}
|
||||||
Operation::JAL(r1, r2, c) => {
|
Operation::JAL(r1, r2, c) => {
|
||||||
let r1b = get_num(&r1)? as u16;
|
let r1b = get_num(&r1)? as u16;
|
||||||
let r2b = get_num(&r2)? as u16;
|
let r2b = get_num(&r2)? as u16;
|
||||||
@ -137,8 +136,7 @@ impl CodeFormat for u16 {
|
|||||||
Const::C(n) => (*n) as u16,
|
Const::C(n) => (*n) as u16,
|
||||||
};
|
};
|
||||||
return Some((0b1010 << 12) + (r1b << 8) + (r2b << 4) + cb);
|
return Some((0b1010 << 12) + (r1b << 8) + (r2b << 4) + cb);
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,18 +3,15 @@ pub mod encoder;
|
|||||||
pub mod parser;
|
pub mod parser;
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
|
|
||||||
use encoder::{SymbolTable};
|
|
||||||
use encoder::CodeFormat;
|
use encoder::CodeFormat;
|
||||||
|
use encoder::SymbolTable;
|
||||||
|
|
||||||
use parser::Section;
|
use parser::Section;
|
||||||
|
|
||||||
|
|
||||||
use crate::loader::unloader::make_string;
|
use crate::loader::unloader::make_string;
|
||||||
|
|
||||||
use self::parser::SectionContent;
|
use self::parser::SectionContent;
|
||||||
|
|
||||||
|
|
||||||
impl Section {
|
impl Section {
|
||||||
fn get_size(&self) -> usize {
|
fn get_size(&self) -> usize {
|
||||||
match &self.content {
|
match &self.content {
|
||||||
@ -22,9 +19,11 @@ impl Section {
|
|||||||
SectionContent::CString(s) => {
|
SectionContent::CString(s) => {
|
||||||
let c = s.len();
|
let c = s.len();
|
||||||
if c % 2 != 0 {
|
if c % 2 != 0 {
|
||||||
(c+1)/2 + 1
|
(c + 1) / 2 + 1
|
||||||
} else {c/2 + 1}
|
} else {
|
||||||
},
|
c / 2 + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
SectionContent::CVec() => todo!(),
|
SectionContent::CVec() => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -41,22 +40,21 @@ impl Section {
|
|||||||
pc += 1;
|
pc += 1;
|
||||||
}
|
}
|
||||||
return Some(res);
|
return Some(res);
|
||||||
|
}
|
||||||
},
|
|
||||||
SectionContent::CString(s) => {
|
SectionContent::CString(s) => {
|
||||||
|
|
||||||
return Some(make_string(s));
|
return Some(make_string(s));
|
||||||
|
}
|
||||||
},
|
|
||||||
SectionContent::CVec() => todo!(),
|
SectionContent::CVec() => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sort_sections(sections: Vec<Section>) -> Option<Vec<Section>> {
|
fn sort_sections(sections: Vec<Section>) -> Option<Vec<Section>> {
|
||||||
|
|
||||||
// we start with a mock section that we'll just replace.
|
// we start with a mock section that we'll just replace.
|
||||||
let mut res: Vec<Section> = vec![Section { name: "".to_owned(), content: SectionContent::CString("".to_owned())}];
|
let mut res: Vec<Section> = vec![Section {
|
||||||
|
name: "".to_owned(),
|
||||||
|
content: SectionContent::CString("".to_owned()),
|
||||||
|
}];
|
||||||
|
|
||||||
let mut nocode: Vec<Section> = vec![];
|
let mut nocode: Vec<Section> = vec![];
|
||||||
|
|
||||||
@ -68,8 +66,7 @@ fn sort_sections(sections: Vec<Section>) -> Option<Vec<Section>> {
|
|||||||
} else {
|
} else {
|
||||||
res.push(section);
|
res.push(section);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
nocode.push(section);
|
nocode.push(section);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -80,32 +77,27 @@ fn sort_sections(sections: Vec<Section>) -> Option<Vec<Section>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn make_symbol_table<'a>(sections: &'a Vec<Section>) -> Option<SymbolTable> {
|
fn make_symbol_table<'a>(sections: &'a Vec<Section>) -> Option<SymbolTable> {
|
||||||
|
|
||||||
let mut res = vec![];
|
let mut res = vec![];
|
||||||
let mut pos : u16 = 0;
|
let mut pos: u16 = 0;
|
||||||
|
|
||||||
for sec in sections {
|
for sec in sections {
|
||||||
res.push((sec.name.clone(), pos));
|
res.push((sec.name.clone(), pos));
|
||||||
pos += sec.get_size() as u16;
|
pos += sec.get_size() as u16;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return Some(SymbolTable(res));
|
return Some(SymbolTable(res));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn to_binary(sections: Vec<Section>) -> Option<Vec<u16>> {
|
pub fn to_binary(sections: Vec<Section>) -> Option<Vec<u16>> {
|
||||||
|
|
||||||
let sorted = sort_sections(sections)?;
|
let sorted = sort_sections(sections)?;
|
||||||
println!("sorted sections: {:?}", sorted);
|
println!("sorted sections: {:?}", sorted);
|
||||||
let sy = make_symbol_table(&sorted)?;
|
let sy = make_symbol_table(&sorted)?;
|
||||||
println!("symbol table: {:?}", sy);
|
println!("symbol table: {:?}", sy);
|
||||||
let k: Vec<Vec<u16>> = sorted.iter().map(|x| x.to_binary(&sy)).collect::<Option<Vec<Vec<u16>>>>()?;
|
let k: Vec<Vec<u16>> = sorted
|
||||||
|
.iter()
|
||||||
|
.map(|x| x.to_binary(&sy))
|
||||||
|
.collect::<Option<Vec<Vec<u16>>>>()?;
|
||||||
println!("binary sections: {:?}", k);
|
println!("binary sections: {:?}", k);
|
||||||
|
|
||||||
|
|
||||||
return Some(k.into_iter().flatten().collect());
|
return Some(k.into_iter().flatten().collect());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ fn take_between(i: &str, start: &str, stop: &str) -> Option<(String, String)> {
|
|||||||
return take_alpha_till(&s1, stop);
|
return take_alpha_till(&s1, stop);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// finds special escaped characters in a string
|
/// finds special escaped characters in a string
|
||||||
/// (such as \n) and replaces them with the actual special
|
/// (such as \n) and replaces them with the actual special
|
||||||
/// character
|
/// character
|
||||||
fn escaped_codes() {}
|
fn escaped_codes() {}
|
||||||
@ -252,7 +252,6 @@ fn parse_code_line(i: &str) -> Result<Operation, ParseError> {
|
|||||||
|
|
||||||
// J-type
|
// J-type
|
||||||
match op {
|
match op {
|
||||||
|
|
||||||
"jal" => {
|
"jal" => {
|
||||||
return Ok(JAL(r1.to_owned(), r2.to_owned(), parse_const(&r3)?));
|
return Ok(JAL(r1.to_owned(), r2.to_owned(), parse_const(&r3)?));
|
||||||
}
|
}
|
||||||
|
31
src/main.rs
31
src/main.rs
@ -1,35 +1,34 @@
|
|||||||
use std::env::Args;
|
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
|
|
||||||
use dekejit::assembler::parser;
|
use dekejit::assembler::parser;
|
||||||
use dekejit::assembler::to_binary;
|
use dekejit::assembler::to_binary;
|
||||||
use dekejit::cpu::IOBuffer;
|
use dekejit::cpu::IOBuffer;
|
||||||
use dekejit::cpu::CPU;
|
use dekejit::cpu::CPU;
|
||||||
use dekejit::loader::unloader::*;
|
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let args: Vec<String> = args().collect();
|
||||||
|
|
||||||
let args : Vec<String> = args().collect();
|
if args.len() < 2 {
|
||||||
|
println!("Scialla");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if args.len() < 2 {
|
// let code = std::fs::read_to_string("./tests/assembly/hello_world.grasm").unwrap();
|
||||||
println!("Scialla");
|
let code = match std::fs::read_to_string(&args[1]) {
|
||||||
|
Ok(p) => p,
|
||||||
|
Err(_) => {
|
||||||
|
println!("Could not open file '{}'", &args[1]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// let code = std::fs::read_to_string("./tests/assembly/hello_world.grasm").unwrap();
|
let mut parser = parser::Parser::new(code);
|
||||||
let code = match std::fs::read_to_string(&args[1]) {
|
|
||||||
Ok(p) => p,
|
|
||||||
Err(_) => {println!("Could not open file '{}'", &args[1]); return;}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut parser = parser::Parser::new(code);
|
let r = parser.parse_sections().unwrap();
|
||||||
|
|
||||||
let r = parser.parse_sections().unwrap();
|
println!("Parsed sections: {:?}", r);
|
||||||
|
|
||||||
println!("Parsed sections: {:?}", r);
|
let code = to_binary(r).unwrap();
|
||||||
|
|
||||||
let code = to_binary(r).unwrap();
|
|
||||||
|
|
||||||
// let mut k = make_string("Hello world!");
|
// let mut k = make_string("Hello world!");
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user