From b0f9ba02536eece20140605a814b4e65be0bef98 Mon Sep 17 00:00:00 2001 From: Robert-M-Lucas Date: Sun, 28 Jul 2024 22:50:20 +0100 Subject: [PATCH] Added null to all user types --- ..._Update_at_27_07_2024__23_06__Changes_.xml | 4 + .idea/workspace.xml | 59 ++++++-------- build/out.asm | 76 +++++++++--------- build/out.o | Bin 1088 -> 1184 bytes build/out.out | Bin 16832 -> 16912 bytes main.why | 22 ++++- src/root.rs | 4 +- src/root/compiler/assembly/mod.rs | 3 +- src/root/compiler/assembly/null.rs | 41 ++++++++++ src/root/compiler/evaluation/type_only.rs | 13 +-- src/root/name_resolver/name_resolvers.rs | 17 ++-- src/root/name_resolver/resolve_names.rs | 2 +- 12 files changed, 152 insertions(+), 89 deletions(-) create mode 100644 .idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024__23_06__Changes_.xml create mode 100644 src/root/compiler/assembly/null.rs diff --git a/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024__23_06__Changes_.xml b/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024__23_06__Changes_.xml new file mode 100644 index 0000000..1d07340 --- /dev/null +++ b/.idea/shelf/Uncommitted_changes_before_Update_at_27_07_2024__23_06__Changes_.xml @@ -0,0 +1,4 @@ + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c63b830..cf64a15 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,22 +7,12 @@ - + - - - - - - - - - - - - + + - - @@ -677,7 +666,9 @@ - diff --git a/build/out.asm b/build/out.asm index 6396549..eddb21a 100644 --- a/build/out.asm +++ b/build/out.asm @@ -5,52 +5,48 @@ section .text main: push rbp mov rbp, rsp - ; Heap Alloc - mov qword [rbp-24], 3 - mov qword [rbp-16], 4 - mov rdi, 16 - sub rsp, 24 - extern malloc - call malloc - sub rsp, 24 - mov qword [rbp-32], rax - mov rdx, qword [rbp-32] - mov rax, qword [rbp-24] - mov qword [rdx+0], rax - mov rax, qword [rbp-16] - mov qword [rdx+8], rax - mov rax, qword [rbp-32] - mov qword [rbp-8], rax - ; Unheap - mov rdx, qword [rbp-8] - mov rax, qword [rdx+0] - mov qword [rbp-48], rax - mov rax, qword [rdx+8] - mov qword [rbp-40], rax - ; Get member + sub rsp, 25 + call _4 + add rsp, 25 + mov rax, qword [rbp-25] + mov qword [rbp-9], rax + mov al, byte [rbp-17] + mov byte [rbp-1], al mov rax, rbp - add rax, -48 - mov qword [rbp-56], rax - ; Deref member - mov rdx, qword [rbp-56] - mov rax, qword [rdx+0] - mov qword [rbp-64], rax - ; Print - mov rax, qword [rbp-64] - mov qword [rbp-72], rax - mov rdi, __8_fstr - mov rsi, [rbp-72] + add rax, -9 + mov qword [rbp-33], rax + mov rdx, qword [rbp-33] + mov al, byte [rdx+0] + mov byte [rbp-34], al + mov al, byte [rbp-34] + cmp al, 0 + jz __23_0 + mov rdi, __23_t_fstr + jmp __23_1 + __23_0: + mov rdi, __23_f_fstr + __23_1: + mov rsi, 0 mov al, 0 - sub rsp, 72 + sub rsp, 34 extern printf call printf - add rsp, 72 - ; Return - mov qword [rbp-80], 7 - mov rax, qword [rbp-80] + add rsp, 34 + mov qword [rbp-42], 0 + mov rax, qword [rbp-42] + leave + ret + + +_4: + push rbp + mov rbp, rsp + mov byte [rbp+16], 1 + mov qword [rbp+17], 0 leave ret section .data_readonly - __8_fstr db `Integer: %ld\n`,0 \ No newline at end of file + __23_f_fstr db `Boolean: False`,0 + __23_t_fstr db `Boolean: True`,0 \ No newline at end of file diff --git a/build/out.o b/build/out.o index 2e0f615caba1a382fd526ddd5c17b798746db146..5ebf33fba30ef9c718815ebdc459e924a1df9d45 100644 GIT binary patch literal 1184 zcmb<-^>JfjWMqH=Mg}_u1P><4z~F#jLfH-s>HXfC|XMMIa1xzli~bvH4;HRKGO={R%*Nbh|-XK0pOIpcb+M#W;bO10)Fq z5)g_(14v`DpMeFUzy+!fMxndk04M=f3R4261E7K)P*D&Uq!-=(P>;^19?fqgU!(#h z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k-ZAxt;)Qfc05P*;Bf{41NqNTa%Pg+{Qw@m=;f z-Xi9fgobicT-d{pe^0)>=|x7)&L&jB&6p485^=#jyJD2}1S`gNdhW-M^td4Ibxv;- p=k&xTh&it}Y<+&Hg{cRzs(G6*yxj)yBc_|(e@5t*Q`L2#Gj zAM7C*6M~PN&y*Ay29GPmuQQc%{#JZZF-ok1U+RPmv$fq_flg)r^M+~=YwsCk?+^LQ zycc-Vy|VrPEI)*?MGwwqcCzodYmtSRGwE@8i4oT7P;7bVVQ`t(AlG>O)L$OBs`#h} z?kLqR8B34}*C{jld-j}VSvLwEfUG__c{7q_>oadgHl3)K*z(Nj4Nvx$|166cW;C*D z&Yi*$$(m6K)utKkDMG!9t}S%okkDK)=a7y@elVk1b2`0(W(K`Dm^Y0HY1E76lA;+{ z1k21#ENTf>&CH(W)vlZP9|^W+7@hcf8I9CswWjottsc8P0JIjeCPTr2)mMfeuCG3sZDW4;+ zN|oi06wW}rk-x3)CNOnwO-!yjW+fRKzyMQQh80*xx zdwd-hI^UtC(AcHRc5i4f(iM_TvFm1Pq3bxMb&w0|vVCL<)Egn$-Yz$c7-8y$1^m7W wchTb+GV!Z diff --git a/main.why b/main.why index d5cbfc8..4c41390 100644 --- a/main.why +++ b/main.why @@ -3,6 +3,23 @@ struct LL { first: &Node } +impl LL { + fn new() -> LL { + return LL { + has_node: false, + first: Node::null() + }; + } + + fn add(&self, val: int) { + let x: &bool = self.has_node; + if (*x) { + self.first.add(val); + } else { + self.first = Node::new(val); + }; + } +} struct Node { val: int, @@ -11,5 +28,8 @@ struct Node { } fn main() -> int { - let r: LL = LL { has_node: false, first: Node::null() }; + let r: LL = LL::new(); + printb(*(r.has_node)); + + return 0; } \ No newline at end of file diff --git a/src/root.rs b/src/root.rs index 765d838..9ea5750 100644 --- a/src/root.rs +++ b/src/root.rs @@ -13,7 +13,7 @@ use std::fs; use std::io::ErrorKind; use std::path::PathBuf; #[cfg(debug_assertions)] -pub const DEBUG_ON_ERROR: bool = false; +pub const DEBUG_ON_ERROR: bool = true; // #[cfg(target_os = "windows")] // use crate::root::runner::run; @@ -77,6 +77,8 @@ pub fn main_args(args: Args) -> Result<(), WErr> { let parsed = parse(PathBuf::from(&args.input))?; ); + println!("{:#?}", parsed); + print!("Resolving Names... "); time!( let (global_table, unprocessed_functions) = resolve(parsed)?; diff --git a/src/root/compiler/assembly/mod.rs b/src/root/compiler/assembly/mod.rs index ecd745f..35d42f7 100644 --- a/src/root/compiler/assembly/mod.rs +++ b/src/root/compiler/assembly/mod.rs @@ -1,2 +1,3 @@ pub mod utils; -pub mod heap; \ No newline at end of file +pub mod heap; +pub mod null; \ No newline at end of file diff --git a/src/root/compiler/assembly/null.rs b/src/root/compiler/assembly/null.rs new file mode 100644 index 0000000..e559cd3 --- /dev/null +++ b/src/root/compiler/assembly/null.rs @@ -0,0 +1,41 @@ +use crate::root::builtin::{BuiltinInlineFunction, InlineFunctionGenerator}; +use crate::root::name_resolver::resolve_function_signatures::FunctionSignature; +use crate::root::parser::parse_parameters::SelfType; +use crate::root::shared::common::{FunctionID, LocalAddress, TypeID}; + +pub struct NullFunction { + id: FunctionID, + parent_type: TypeID +} + +impl BuiltinInlineFunction for NullFunction { + fn id(&self) -> FunctionID { + self.id + } + + fn name(&self) -> &'static str { + "null" + } + + fn signature(&self) -> FunctionSignature { + FunctionSignature::new(SelfType::None, vec![], Some(self.parent_type.with_indirection(1))) + } + + fn inline(&self) -> InlineFunctionGenerator { + |_, return_into: Option, _, _| -> String { + let return_into = return_into.unwrap(); + format!(" mov qword {return_into}, 0\n") + } + } + + fn parent_type(&self) -> Option { + Some(self.parent_type) + } +} + +pub fn null_function(t: TypeID, f: FunctionID) -> NullFunction { + NullFunction { + id: f, + parent_type: t, + } +} \ No newline at end of file diff --git a/src/root/compiler/evaluation/type_only.rs b/src/root/compiler/evaluation/type_only.rs index d972b39..4c57c84 100644 --- a/src/root/compiler/evaluation/type_only.rs +++ b/src/root/compiler/evaluation/type_only.rs @@ -33,11 +33,14 @@ pub fn compile_evaluable_type_only( name.location().clone(), ) } - NameResult::Type(_) => { - return WErr::ne( - EvalErrs::CannotEvalStandaloneType(name.name().clone()), - name.location().clone(), - ) + NameResult::Type(t) => { + t.immediate() + // println!("> {}", name.name()); + // std::process::exit(123); + // return WErr::ne( + // EvalErrs::CannotEvalStandaloneType(name.name().clone()), + // name.location().clone(), + // ) } NameResult::Variable(address) => address.type_ref().clone(), } diff --git a/src/root/name_resolver/name_resolvers.rs b/src/root/name_resolver/name_resolvers.rs index dfbc928..5c2109c 100644 --- a/src/root/name_resolver/name_resolvers.rs +++ b/src/root/name_resolver/name_resolvers.rs @@ -17,6 +17,7 @@ use crate::root::POINTER_SIZE; use std::collections::HashMap; use std::path::PathBuf; use std::rc::Rc; +use crate::root::compiler::assembly::null::null_function; #[derive(Debug)] enum NameTreeEntry { @@ -197,8 +198,12 @@ impl GlobalDefinitionTable { } /// Adds a type definition for a previously given `TypeID` - pub fn add_type(&mut self, given_id: TypeID, definition: Box) { + pub fn add_user_type(&mut self, given_id: TypeID, definition: Box) { self.type_definitions.insert(given_id, definition); + let fid = self.id_counter; + self.id_counter += 1; + let null_function = null_function(given_id, FunctionID(fid)); + self.register_inline_function(&null_function); } /// Takes a name and resolves it to a type (or error) @@ -321,11 +326,6 @@ impl GlobalDefinitionTable { Ok(address) } - /// Returns the `FunctionSignature` of a function - pub fn get_function_signature(&self, function_id: FunctionID) -> &FunctionSignature { - self.function_signatures.get(&function_id).as_ref().unwrap() - } - /// Returns whether a main function has been defined pub fn has_main(&self) -> bool { self.function_signatures.contains_key(&FunctionID(0)) @@ -442,6 +442,11 @@ impl GlobalDefinitionTable { } } + /// Returns the `FunctionSignature` of a function + pub fn get_function_signature(&self, function_id: FunctionID) -> &FunctionSignature { + self.function_signatures.get(&function_id).as_ref().unwrap() + } + /// Returns a function specified by the `FunctionID` pub fn get_function( &self, diff --git a/src/root/name_resolver/resolve_names.rs b/src/root/name_resolver/resolve_names.rs index 6710143..1f5d8a9 100644 --- a/src/root/name_resolver/resolve_names.rs +++ b/src/root/name_resolver/resolve_names.rs @@ -182,7 +182,7 @@ pub fn resolve_names( } for (id, user_type) in final_types { - global_table.add_type(id, Box::new(user_type)); + global_table.add_user_type(id, Box::new(user_type)); } // (final_types, type_names, unprocessed_functions)