diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1b712e7..2f580fb 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,18 +7,18 @@ - - + + - - - - - + + + + + @@ -633,7 +641,6 @@ - @@ -658,7 +665,8 @@ - diff --git a/build/out.asm b/build/out.asm index 5816433..b82fabb 100644 --- a/build/out.asm +++ b/build/out.asm @@ -6,15 +6,30 @@ main: push rbp mov rbp, rsp mov qword [rbp-8], 13 - ; Test Start - mov qword [rbp-32], 3 - mov qword [rbp-24], 5 - mov qword [rbp-16], 4 - ; Test End + sub rsp, 24 + call _4 + add rsp, 24 mov rax, qword [rbp-8] - mov qword [rbp-40], rax - mov rax, qword [rbp-40] + mov qword [rbp-32], rax + mov rax, qword [rbp-32] leave ret +_4: + push rbp + mov rbp, rsp + mov qword [rbp-8], 12 + mov rdi, __8_fstr + mov rsi, [rbp-8] + mov al, 0 + sub rsp, 8 + extern printf + call printf + add rsp, 8 + +leave +ret + +section .data_readonly + __8_fstr db `Integer: %ld\n`,0 \ No newline at end of file diff --git a/build/out.o b/build/out.o index a1996c6..caf45e1 100644 Binary files a/build/out.o and b/build/out.o differ diff --git a/build/out.out b/build/out.out index 067af02..bf51b38 100755 Binary files a/build/out.out and b/build/out.out differ diff --git a/main.why b/main.why index 4cb5186..366f3c1 100644 --- a/main.why +++ b/main.why @@ -8,20 +8,14 @@ struct StructB { b: int } +fn test() { + printi(12); +} + fn main() -> int { let y: int = 13; - @ Test Start - - let x: StructA = StructA { - a: 3, - b: StructB { - a: 5, - b: 4 - } - }; - - @ Test End + test(); return y; } diff --git a/src/main.rs b/src/main.rs index 4e92c5c..8209002 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +use std::collections::HashMap; +use std::{mem, ptr, slice}; +use std::hash::RandomState; mod root; diff --git a/src/root.rs b/src/root.rs index 6d61282..ae3ec12 100644 --- a/src/root.rs +++ b/src/root.rs @@ -36,6 +36,7 @@ pub mod compiler; pub mod assembler; pub mod errors; mod ob; +mod unrandom; pub const POINTER_SIZE: ByteSize = ByteSize(8); diff --git a/src/root/compiler/compile.rs b/src/root/compiler/compile.rs index 6204948..a3b2c08 100644 --- a/src/root/compiler/compile.rs +++ b/src/root/compiler/compile.rs @@ -6,14 +6,15 @@ use crate::root::errors::WErr; use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable; use crate::root::parser::parse_function::FunctionToken; use crate::root::shared::common::FunctionID; +use crate::root::unrandom::{new_hashmap, new_hashset}; /// Compiles the entire program. Returns assembly. pub fn compile(mut global_table: GlobalDefinitionTable, unprocessed_functions: HashMap) -> Result { let mut unprocessed_functions = unprocessed_functions; - let mut compiled_functions = HashMap::new(); + let mut compiled_functions = new_hashmap(); let mut compiled_len = 0usize; - let mut open_set = HashSet::new(); + let mut open_set = new_hashset(); open_set.insert(FunctionID(0)); // Start with main let mut global_tracker = GlobalTracker::default(); diff --git a/src/root/compiler/compile_function.rs b/src/root/compiler/compile_function.rs index 1b5b899..82cfadd 100644 --- a/src/root/compiler/compile_function.rs +++ b/src/root/compiler/compile_function.rs @@ -191,6 +191,7 @@ fn recursively_compile_lines(fid: FunctionID, lines: &[LineTokens], return_varia LineTokens::NoOp(et) => { contents.other(&compile_evaluable_reference(fid, et, local_variables, global_table, global_tracker)?.0); } + #[cfg(debug_assertions)] LineTokens::Marker(value) => { contents.line(&format!(";{}", value.value())); } diff --git a/src/root/name_resolver/resolve_names.rs b/src/root/name_resolver/resolve_names.rs index 285d955..a814fd0 100644 --- a/src/root/name_resolver/resolve_names.rs +++ b/src/root/name_resolver/resolve_names.rs @@ -18,7 +18,7 @@ use crate::root::parser::parse_toplevel::TopLevelTokens; use crate::root::shared::common::{LocalAddress, TypeRef}; use crate::root::shared::common::{ByteSize, FunctionID, TypeID}; use crate::root::shared::types::Type; - +use crate::root::unrandom::new_hashmap; /// A Whython-code defined type #[derive(Getters)] @@ -82,8 +82,8 @@ pub fn resolve_names(ast: Vec, global_table: &mut GlobalDefiniti }; } - let mut unsized_final_types: HashMap = HashMap::new(); - let mut unprocessed_functions: HashMap = HashMap::new(); + let mut unsized_final_types: HashMap = new_hashmap(); + let mut unprocessed_functions: HashMap = new_hashmap(); for symbol in ast { match symbol { @@ -127,7 +127,7 @@ pub fn resolve_names(ast: Vec, global_table: &mut GlobalDefiniti }; } - let mut final_types: HashMap = HashMap::new(); + let mut final_types: HashMap = new_hashmap(); while !unsized_final_types.is_empty() { let next_type_id = *unsized_final_types.keys().next().unwrap(); diff --git a/src/root/parser/parse_function/parse_struct_init.rs b/src/root/parser/parse_function/parse_struct_init.rs index 742486a..3c8898c 100644 --- a/src/root/parser/parse_function/parse_struct_init.rs +++ b/src/root/parser/parse_function/parse_struct_init.rs @@ -41,7 +41,6 @@ pub fn parse_struct_init<'a, 'b>(s: Span<'a>, containing_class: Option<&'b Simpl let (ns, _) = discard_ignored(ns)?; let (ns, to_eval) = take_until_or_end_discard_smart(ns, ",")?; - println!("{:?} - {:?}", ns.fragment(), to_eval.fragment()); let (_, eval) = parse_evaluable(to_eval, containing_class, false)?; contents.push((name, eval)); s = ns; diff --git a/src/root/unrandom.rs b/src/root/unrandom.rs new file mode 100644 index 0000000..ebd54db --- /dev/null +++ b/src/root/unrandom.rs @@ -0,0 +1,65 @@ +use std::hash::RandomState; +use std::{mem, ptr, slice}; +use std::collections::{HashMap, HashSet}; + +#[inline] +pub fn new_hashmap() -> HashMap { + #[cfg(debug_assertions)] + { + unrandom_hashmap() + } + #[cfg(not(debug_assertions))] + { + HashMap::new() + } +} + +#[inline] +pub fn unrandom_hashmap() -> HashMap { + let mut r = RandomState::new(); + let p: *mut RandomState = &mut r; + let p: *mut u8 = p as *mut u8; + let s: &mut [u8] = unsafe { + slice::from_raw_parts_mut(p, mem::size_of::()) + }; + unsafe { + for s in &mut *s { + let p: *const u8 = s; + let p: *mut u8 = p as *mut u8; + ptr::write_volatile(p, 0u8); + } + } + + HashMap::with_hasher(r) +} + +#[inline] +pub fn new_hashset() -> HashSet { + #[cfg(debug_assertions)] + { + unrandom_hashset() + } + #[cfg(not(debug_assertions))] + { + HashSet::new() + } +} + +#[inline] +pub fn unrandom_hashset() -> HashSet { + let mut r = RandomState::new(); + let p: *mut RandomState = &mut r; + let p: *mut u8 = p as *mut u8; + let s: &mut [u8] = unsafe { + slice::from_raw_parts_mut(p, mem::size_of::()) + }; + unsafe { + for s in &mut *s { + let p: *const u8 = s; + let p: *mut u8 = p as *mut u8; + ptr::write_volatile(p, 0u8); + } + } + + HashSet::with_hasher(r) +} \ No newline at end of file