Skip to content

Commit

Permalink
Cleanup 2: Electric Boogaloo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlamonster committed Dec 8, 2023
1 parent 9e36c33 commit ce8dccd
Show file tree
Hide file tree
Showing 57 changed files with 116 additions and 168 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ Arguments:
Options:
-o, --output <FILE> Specifies the path to an output file. If None, it uses the input filename. If that's also None, it defaults to "output"
-d, --display <PASS> [possible values: parse, validate, reveal, atomize, explicate, select]
-r, --run
-r, --run Optionally runs and deletes the compiled executable. Only supported on Unix systems
-d, --display <PASS> Specifies the pass to display. Supported passes are defined by the `Pass` enum [possible values: parse, validate, reveal, atomize, explicate, select]
-t, --time Print timing debug information
-h, --help Print help
-V, --version Print version
```
Expand Down Expand Up @@ -45,10 +46,10 @@ cargo run -- input.sp -o output
* Let
* If
* Functions
* Return
* Return
* Loop
* Break
* Continue
* Break
* Continue
* While
* Sequences
* Structs
Expand All @@ -67,8 +68,8 @@ cargo run -- input.sp -o output
* [ ] Add documentation where necessary.
* [x] Improve error handling for parsing pass.
* [x] Improve error handling for type checking pass.
* [ ] Make errors prettier.
* [ ] Improve algorithm for colouring the interference graph.
* [ ] Make errors prettier.
* [x] Improve algorithm for colouring the interference graph.
* [x] Add read and write functionality to the bencher to update locally.
* [x] Lots, and lots, of refactoring!
* [x] Write test input in comments.
Expand All @@ -78,8 +79,8 @@ cargo run -- input.sp -o output
* [x] Type inference.
* [x] Implement comments in code.
* [ ] Algebraic Data Types.
* [x] Structs.
* [ ] Enums.
* [x] Structs.
* [ ] Enums.
* [ ] First-class functions.
* [ ] Constants.

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/debug/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::debug::{Pass, DEBUG_ARGS};
use std::fmt::Display;

pub fn display<T: Display>(value: &T, pass: Pass) {
if let Some(display) = &DEBUG_ARGS.get().unwrap().display {
if let Some(display) = &DEBUG_ARGS.get().and_then(|args| args.display) {
if display == &pass {
print!("{value}")
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl DebugArgs {
pub static DEBUG_ARGS: OnceCell<DebugArgs> = OnceCell::new();

#[cfg(feature = "debug")]
#[derive(ValueEnum, Clone, Debug, PartialEq)]
#[derive(ValueEnum, Clone, Copy, Debug, PartialEq)]
pub enum Pass {
Parse,
Validate,
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/debug/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ static TIME: Lazy<Mutex<Time>> = Lazy::new(|| Mutex::new(Time::new()));
/// Initializes the global time tracking instance.
/// This function should be called before using the `time` function.
pub fn time_init() {
if DEBUG_ARGS.get().unwrap().time {
if let Some(true) = DEBUG_ARGS.get().map(|args| args.time) {
println!("{:>12} {:>12} {:>12}", "pass", "since prev", "since init");
Lazy::force(&TIME);
}
}

/// Tracks and prints the time elapsed since the last call to `time` or `time_init`.
pub fn time(pass: &str) {
if DEBUG_ARGS.get().unwrap().time {
if let Some(true) = DEBUG_ARGS.get().map(|args| args.time) {
let now = Instant::now();
let mut time = TIME.lock().unwrap();
let since_init = now.duration_since(time.init);
Expand Down
22 changes: 13 additions & 9 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@ use std::path::Path;
pub fn compile(program: &str, filename: &str, output: &Path) -> miette::Result<()> {
time_init!();

// todo: make a module system so we don't have to append the std-library to the source.
let program = String::leak(format!(
"{program}{}{}{}{}{}",
include_str!("../std/exit.sp"),
include_str!("../std/read.sp"),
include_str!("../std/print.sp"),
include_str!("../std/alloc.sp"),
include_str!("../std/math.sp"),
));
let program = apped_std(program);

let add_source =
|error| Report::with_source_code(error, NamedSource::new(filename, (*program).to_string()));
Expand All @@ -42,3 +34,15 @@ pub fn compile(program: &str, filename: &str, output: &Path) -> miette::Result<(

Ok(())
}

// todo: make a module system so we don't have to append the std-library to the source.
fn apped_std(program: &str) -> &str {
String::leak(format!(
"{program}{}{}{}{}{}",
include_str!("../std/exit.sp"),
include_str!("../std/read.sp"),
include_str!("../std/print.sp"),
include_str!("../std/alloc.sp"),
include_str!("../std/math.sp"),
))
}
21 changes: 8 additions & 13 deletions compiler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
mod run;

use clap::Parser;
use compiler::passes::parse::parse::PrettyParseError;
use compiler::passes::validate::error::TypeError;
use compiler::{compile, debug};
use compiler::compile;
#[cfg(feature = "debug")]
use compiler::debug::{DebugArgs, Pass};
use compiler::passes::{parse::parse::PrettyParseError, validate::error::TypeError};
use miette::{Diagnostic, IntoDiagnostic};
use std::fs;
use std::io::Read;
use std::io::{read_to_string, stdin};
use std::path::Path;
use thiserror::Error;

Expand Down Expand Up @@ -40,28 +41,22 @@ struct Args {
/// Specifies the pass to display. Supported passes are defined by the `Pass` enum.
#[cfg(feature = "debug")]
#[arg(value_enum, short, long, value_name = "PASS")]
display: Option<debug::Pass>,
display: Option<Pass>,

/// Print timing debug information.
#[cfg(feature = "debug")]
#[arg(short, long)]
time: bool,
}

fn read_from_stdin() -> Result<String, std::io::Error> {
let mut program = String::new();
std::io::stdin().read_to_string(&mut program)?;
Ok(program)
}

fn main() -> miette::Result<()> {
let args = Args::parse();

#[cfg(feature = "debug")]
debug::DebugArgs::set(args.time, args.display)?;
DebugArgs::set(args.time, args.display)?;

let (program, filename) = match args.input.as_ref() {
None => (read_from_stdin().into_diagnostic()?, "stdin"),
None => (read_to_string(stdin()).into_diagnostic()?, "stdin"),
Some(file) => (fs::read_to_string(file).into_diagnostic()?, file.as_str()),
};

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/assign/assign.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::passes::assign::{Arg, FunAssigned, X86Assigned};
use crate::passes::select::{Block, Instr, InstrSelected, VarArg, X86Selected};
use crate::time;
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use functor_derive::Functor;
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/assign/color_interference.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::passes::assign::{Arg, InterferenceGraph, LArg};
use crate::passes::select::{Reg, CALLEE_SAVED_NO_STACK};
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use binary_heap_plus::BinaryHeap;
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/assign/include_liveness.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;

use crate::passes::assign::{LArg, LBlock, LFun, LX86VarProgram};
use crate::passes::select::{
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/assign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod include_liveness;
use crate::passes::select::{
Block, FunSelected, Imm, Instr, InstrSelected, Reg, VarArg, X86Selected,
};
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use derive_more::Display;
use functor_derive::Functor;
use petgraph::graphmap::GraphMap;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/atomize/atomize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::passes::atomize::{AExpr, Atom, DefAtomized, PrgAtomized};
use crate::passes::parse::types::Type;
use crate::passes::parse::{Meta, Typed};
use crate::passes::reveal::{DefRevealed, PrgRevealed, RExpr};
use crate::utils::gen_sym::{gen_sym, UniqueSym};
use crate::utils::push_map::PushMap;
use crate::utils::unique_sym::{gen_sym, UniqueSym};
use crate::{display, time};

impl<'p> PrgRevealed<'p> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/atomize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::passes::parse::types::Type;
use crate::passes::parse::{BinaryOp, Def, Lit, Typed, UnaryOp};
use crate::passes::select::InstrSelected;
use crate::passes::validate::Int;
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use derive_more::Display;
use itertools::Itertools;
use std::collections::HashMap;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/conclude/conclude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::passes::assign::{Arg, InstrAssigned};
use crate::passes::conclude::X86Concluded;
use crate::passes::patch::X86Patched;
use crate::passes::select::{Block, Imm, Instr};
use crate::utils::gen_sym::gen_sym;
use crate::utils::unique_sym::gen_sym;
use crate::*;
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/conclude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod conclude;

use crate::passes::assign::Arg;
use crate::passes::select::Block;
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use std::collections::HashMap;

pub struct X86Concluded<'p> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/eliminate/eliminate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::passes::eliminate::eliminate_tail::eliminate_tail;
use crate::passes::eliminate::{FunEliminated, PrgEliminated};
use crate::passes::explicate::PrgExplicated;
use crate::time;
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use std::collections::HashMap;

// (Old variable name, field name) -> New variable name
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/eliminate/eliminate_params.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::passes::eliminate::eliminate::Ctx;
use crate::passes::parse::types::Type;
use crate::passes::parse::{Param, TypeDef};
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use std::collections::HashMap;

pub fn eliminate_params<'p>(
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/eliminate/eliminate_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::passes::eliminate::{ExprEliminated, TailEliminated};
use crate::passes::explicate::ExprExplicated;
use crate::passes::parse::types::Type;
use crate::passes::parse::{Meta, TypeDef, Typed};
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use std::collections::HashMap;

pub fn eliminate_seq<'p>(
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/eliminate/eliminate_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::passes::eliminate::eliminate_seq::eliminate_seq;
use crate::passes::eliminate::TailEliminated;
use crate::passes::explicate::TailExplicated;
use crate::passes::parse::TypeDef;
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use functor_derive::Functor;
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/eliminate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::passes::atomize::Atom;
use crate::passes::parse::types::Type;
use crate::passes::parse::{BinaryOp, Meta, Param, TypeDef, UnaryOp};
use crate::passes::select::InstrSelected;
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use std::collections::HashMap;

pub struct PrgEliminated<'p> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/emit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::passes::emit::push_pop::{encode_push_pop, POPQ_INFO, PUSHQ_INFO};
use crate::passes::emit::special::encode_setcc;
use crate::passes::emit::unary::{encode_unary_instr, CALLQ_INDIRECT_INFO, NEGQ_INFO};
use crate::passes::select::{Block, Cnd, Instr, Reg};
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use crate::{imm32, time};
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/explicate/explicate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::passes::atomize::{DefAtomized, PrgAtomized};
use crate::passes::explicate::explicate_tail::explicate_tail;
use crate::passes::explicate::{FunExplicated, PrgExplicated, TailExplicated};
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use crate::{display, time};
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/explicate/explicate_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::passes::explicate::explicate::Env;
use crate::passes::explicate::{explicate_pred, ExprExplicated, TailExplicated};

use crate::passes::parse::{Lit, Meta, Typed};
use crate::utils::gen_sym::{gen_sym, UniqueSym};
use crate::utils::unique_sym::{gen_sym, UniqueSym};

pub fn explicate_assign<'p>(
sym: UniqueSym<'p>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/explicate/explicate_pred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::passes::explicate::explicate_assign::explicate_assign;
use crate::passes::explicate::{ExprExplicated, TailExplicated};
use crate::passes::parse::types::Type;
use crate::passes::parse::{BinaryOp, Lit, Meta, UnaryOp};
use crate::utils::gen_sym::gen_sym;
use crate::utils::unique_sym::gen_sym;

pub fn explicate_pred<'p>(
cnd: AExpr<'p>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/explicate/explicate_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::passes::explicate::explicate_assign::explicate_assign;
use crate::passes::explicate::TailExplicated;

use crate::passes::parse::{Meta, Typed};
use crate::utils::gen_sym::gen_sym;
use crate::utils::unique_sym::gen_sym;

pub fn explicate_tail<'p>(expr: Typed<'p, AExpr<'p>>, env: &mut Env<'_, 'p>) -> TailExplicated<'p> {
let tmp = gen_sym("return");
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/explicate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::passes::atomize::Atom;
use crate::passes::parse::types::Type;
use crate::passes::parse::{BinaryOp, Param, TypeDef, Typed, UnaryOp};
use crate::passes::select::InstrSelected;
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use derive_more::Display;
use itertools::Itertools;
use std::collections::HashMap;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod types;

use crate::passes::select::{Instr, VarArg};
use crate::passes::validate::MetaConstrained;
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use derive_more::Display;
use functor_derive::Functor;
use itertools::Itertools;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/patch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod patch;

use crate::passes::assign::FunAssigned;
use crate::passes::select::X86Selected;
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use functor_derive::Functor;
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/patch/patch.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::passes::assign::{Arg, FunAssigned, X86Assigned};
use crate::passes::patch::X86Patched;
use crate::passes::select::{Block, Instr};
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use crate::{addq, movq, popq, pushq, reg, subq, time};
use functor_derive::Functor;

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/reveal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod reveal;
use crate::passes::parse::{BinaryOp, Def, Lit, Typed, UnaryOp};
use crate::passes::select::InstrSelected;
use crate::passes::validate::Int;
use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use derive_more::Display;
use itertools::Itertools;
use std::collections::HashMap;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/reveal/reveal.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::passes::parse::{Meta, Typed};
use crate::passes::reveal::{DefRevealed, PrgRevealed, RExpr};
use crate::passes::validate::{DefValidated, ExprValidated, PrgValidated};
use crate::utils::gen_sym::UniqueSym;
use crate::utils::push_map::PushMap;
use crate::utils::unique_sym::UniqueSym;
use crate::{display, time};

impl<'p> PrgValidated<'p> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod display;
pub mod macros;
pub mod select;

use crate::utils::gen_sym::UniqueSym;
use crate::utils::unique_sym::UniqueSym;
use derive_more::Display;
use functor_derive::Functor;
use itertools::Itertools;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/select/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::passes::select::{
CALLER_SAVED,
};
use crate::passes::validate::Int;
use crate::utils::gen_sym::{gen_sym, UniqueSym};
use crate::utils::unique_sym::{gen_sym, UniqueSym};
use crate::*;
use std::collections::HashMap;

Expand Down
Loading

0 comments on commit ce8dccd

Please sign in to comment.