Skip to content

Commit

Permalink
Unrandom
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jul 26, 2024
1 parent 33fd1f0 commit 64ca194
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 36 deletions.
30 changes: 19 additions & 11 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 22 additions & 7 deletions build/out.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file modified build/out.o
Binary file not shown.
Binary file modified build/out.out
Binary file not shown.
16 changes: 5 additions & 11 deletions main.why
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::collections::HashMap;
use std::{mem, ptr, slice};
use std::hash::RandomState;

mod root;

Expand Down
1 change: 1 addition & 0 deletions src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub mod compiler;
pub mod assembler;
pub mod errors;
mod ob;
mod unrandom;

pub const POINTER_SIZE: ByteSize = ByteSize(8);

Expand Down
5 changes: 3 additions & 2 deletions src/root/compiler/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunctionID, FunctionToken>) -> Result<String, WErr> {
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();

Expand Down
1 change: 1 addition & 0 deletions src/root/compiler/compile_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
8 changes: 4 additions & 4 deletions src/root/name_resolver/resolve_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -82,8 +82,8 @@ pub fn resolve_names(ast: Vec<TopLevelTokens>, global_table: &mut GlobalDefiniti
};
}

let mut unsized_final_types: HashMap<TypeID, UnsizedUserType> = HashMap::new();
let mut unprocessed_functions: HashMap<FunctionID, FunctionToken> = HashMap::new();
let mut unsized_final_types: HashMap<TypeID, UnsizedUserType> = new_hashmap();
let mut unprocessed_functions: HashMap<FunctionID, FunctionToken> = new_hashmap();

for symbol in ast {
match symbol {
Expand Down Expand Up @@ -127,7 +127,7 @@ pub fn resolve_names(ast: Vec<TopLevelTokens>, global_table: &mut GlobalDefiniti
};
}

let mut final_types: HashMap<TypeID, UserType> = HashMap::new();
let mut final_types: HashMap<TypeID, UserType> = new_hashmap();

while !unsized_final_types.is_empty() {
let next_type_id = *unsized_final_types.keys().next().unwrap();
Expand Down
1 change: 0 additions & 1 deletion src/root/parser/parse_function/parse_struct_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
65 changes: 65 additions & 0 deletions src/root/unrandom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::hash::RandomState;
use std::{mem, ptr, slice};
use std::collections::{HashMap, HashSet};

#[inline]
pub fn new_hashmap<A, B>() -> HashMap<A, B> {
#[cfg(debug_assertions)]
{
unrandom_hashmap()
}
#[cfg(not(debug_assertions))]
{
HashMap::new()
}
}

#[inline]
pub fn unrandom_hashmap<A, B>() -> HashMap<A, B> {
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::<RandomState>())
};
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<A>() -> HashSet<A> {
#[cfg(debug_assertions)]
{
unrandom_hashset()
}
#[cfg(not(debug_assertions))]
{
HashSet::new()
}
}

#[inline]
pub fn unrandom_hashset<A>() -> HashSet<A> {
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::<RandomState>())
};
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)
}

0 comments on commit 64ca194

Please sign in to comment.