Skip to content

Commit

Permalink
Whython 8 compiles for the first time
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 3, 2024
1 parent 84b46a2 commit fc6c3c1
Show file tree
Hide file tree
Showing 35 changed files with 290 additions and 230 deletions.
40 changes: 40 additions & 0 deletions .idea/workspace.xml

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

Binary file removed build/a.out
Binary file not shown.
9 changes: 0 additions & 9 deletions build/out.asm

This file was deleted.

11 changes: 0 additions & 11 deletions build/test.asm

This file was deleted.

Binary file removed build/test.o
Binary file not shown.
Binary file removed libs/kernel32.lib
Binary file not shown.
Binary file removed libs/legacy_stdio_definitions.lib
Binary file not shown.
Binary file removed libs/legacy_stdio_wide_specifiers.lib
Binary file not shown.
Binary file removed libs/msvcrt.lib
Binary file not shown.
Binary file removed libs/ucrt.lib
Binary file not shown.
Binary file removed libs/vcruntime.lib
Binary file not shown.
11 changes: 1 addition & 10 deletions main.why
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
struct A {
b: int,
c: int
}

fn check() -> A {

}

fn main() -> int {
printb(true);
return 12;
}
File renamed without changes.
62 changes: 21 additions & 41 deletions src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use std::io::ErrorKind;
use std::path::PathBuf;
use crate::root::compiler::compile::compile;
use crate::root::name_resolver::resolve::resolve;
use crate::root::shared::types::ByteSize;
use shared::common::ByteSize;
use crate::root::runner::{assemble, link_gcc, run};

// #[cfg(target_os = "windows")]
// use crate::root::runner::run;
Expand Down Expand Up @@ -84,47 +85,26 @@ pub fn main_args(args: Args) {
fs::write(PathBuf::from(format!("{}.asm", &args.output)), assembly.as_bytes()).unwrap();
);

// print!("Compiling... ");
// time!(generate_assembly(&args.output, functions););
//
// print!("Assembling (NASM)... ");
// time!(if assemble(&args.output).is_err() {
// return Err(AnyError::Other);
// });

// #[cfg(target_os = "windows")]
// {
// println!("Linking (MSVC - link.exe)... ");
// time!(if link(&args.output).is_err() {
// return Err(AnyError::Other);
// });
// if args.build {
// println!("Skipping execution")
// } else {
// println!("Executing... ");
// run(&args.output);
// }
// }
// #[cfg(target_os = "linux")]
// {
// cprintln!("<yellow,bold>Compilation and execution on Linux may be buggy!</>");
// println!("Linking (gcc)... ");
// time!(
// let res = link_gcc_experimental(&args.output);
// if res.is_err() {
// return Err(AnyError::Other);
// }
// );
//
// if args.build {
// println!("Skipping execution")
// } else {
// println!("Executing (wine)... ");
// if run_wine_experimental(&args.output).is_err() {
// return Err(AnyError::Other);
// }
// }
// }
print!("Assembling (NASM)... ");
time!(
assemble(&args.output).unwrap();
);

#[cfg(target_os = "linux")]
{
println!("Linking (gcc)... ");
time!(
link_gcc(&args.output).unwrap();
);

if args.build {
println!("Skipping execution")
} else {
println!("Executing...");
run(&args.output);
}
}

cprintln!("<g,bold>Done!</>");
}
22 changes: 21 additions & 1 deletion src/root/builtin/int.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use unique_type_id::UniqueTypeId;
use crate::root::shared::types::{ByteSize, Type, TypeID};
use crate::root::compiler::assembly::utils::get_qword_stack_pointer;
use crate::root::parser::parse_function::parse_literal::{LiteralToken, LiteralTokens};
use crate::root::shared::common::{AddressedTypeRef, ByteSize, LocalAddress, TypeID};
use crate::root::shared::types::Type;

#[derive(UniqueTypeId)]
#[UniqueTypeIdType = "u16"]
Expand All @@ -13,4 +16,21 @@ impl Type for IntType {
fn size(&self) -> ByteSize {
ByteSize(8)
}

fn instantiate_from_literal(&self, location: &LocalAddress, literal: &LiteralToken) -> String {
let location = get_qword_stack_pointer(location);
match literal.literal() {
LiteralTokens::Bool(value) => {
if *value {
format!("\tmov {location}, 0")
}
else {
format!("\tmov {location}, 1")
}
}
LiteralTokens::Int(value) => {
format!("\tmov {location}, {value}")
}
}
}
}
2 changes: 1 addition & 1 deletion src/root/builtin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod int;
pub mod int;

use crate::root::builtin::int::IntType;
use crate::root::name_resolver::name_resolvers::{GlobalDefinitionTable, ImplNode};
Expand Down
12 changes: 11 additions & 1 deletion src/root/compiler/assembly/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::root::shared::types::{ByteSize, FunctionID};
use crate::root::shared::common::{ByteSize, FunctionID, LocalAddress};

pub fn get_function_tag(id: FunctionID) -> String {
if id.is_main() {
Expand Down Expand Up @@ -44,4 +44,14 @@ pub fn align_16_bytes_plus_8(bytes: ByteSize) -> ByteSize {
} else {
ByteSize(bytes + (16 % (24 - (bytes % 16))))
}
}

pub fn get_qword_stack_pointer(address: &LocalAddress) -> String {
let address = address.0;

if address >= 0 {
format!("qword [rbp+{address}]")
} else {
format!("qword [rbp{address}]")
}
}
2 changes: 1 addition & 1 deletion src/root/compiler/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};
use crate::root::compiler::compile_function::compile_function;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::parser::parse_function::FunctionToken;
use crate::root::shared::types::FunctionID;
use crate::root::shared::common::FunctionID;

pub fn compile(global_table: GlobalDefinitionTable, unprocessed_functions: HashMap<FunctionID, FunctionToken>) -> String {
let mut unprocessed_functions = unprocessed_functions;
Expand Down
37 changes: 37 additions & 0 deletions src/root/compiler/compile_evaluable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::collections::HashSet;
use crate::root::compiler::local_variable_table::LocalVariableTable;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::parser::parse_function::parse_evaluable::{EvaluableToken, EvaluableTokens};
use crate::root::shared::common::{FunctionID, TypeRef};
use crate::root::shared::common::AddressedTypeRef;

pub fn compile_evaluable(fid: FunctionID, et: &EvaluableToken, target: Option<AddressedTypeRef>, local_variables: &mut LocalVariableTable, global_table: &GlobalDefinitionTable, function_calls: &mut HashSet<FunctionID>) -> (String, Option<AddressedTypeRef>) {
let et = et.token();

match et {
EvaluableTokens::Name(_) => todo!(),
EvaluableTokens::Literal(literal) => {
let (address, t, tid) = if let Some(target) = target {
let (address, tid) = target.dissolve();
if tid.indirection().has_indirection() {
todo!()
}
let t = global_table.type_definitions().get(tid.type_id()).unwrap();
(address, t, tid)
}
else {
let tid = literal.literal().default_type();
if tid.indirection().has_indirection() {
todo!()
}
let t = global_table.type_definitions().get(tid.type_id()).unwrap();
let address = local_variables.add_new_unnamed(t.size());
(address, t, tid)
};

(t.instantiate_from_literal(&address, literal), Some(AddressedTypeRef::new(address, tid)))
}
EvaluableTokens::InfixOperator(_, _, _) => todo!(),
EvaluableTokens::PrefixOperator(_, _) => todo!()
}
}
Loading

0 comments on commit fc6c3c1

Please sign in to comment.