From 2b39ddebb22412322f95e49dbdc133c0775b030f Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 28 May 2023 18:59:27 +0200 Subject: [PATCH] add support for - input --- analysis/src/ctxt.rs | 2 +- analysis/src/ir.rs | 5 ++--- analysis/src/ir/custom.rs | 2 +- analysis/src/ir/info.rs | 3 +-- analysis/src/ir/pretty.rs | 6 +++++- analysis/src/ir/visit.rs | 26 +++++++++++++++++--------- parser/src/lib.rs | 3 --- src/main.rs | 30 +++++++++++++++++++++++------- 8 files changed, 50 insertions(+), 27 deletions(-) diff --git a/analysis/src/ctxt.rs b/analysis/src/ctxt.rs index 5da7966..5ec8a21 100644 --- a/analysis/src/ctxt.rs +++ b/analysis/src/ctxt.rs @@ -4,7 +4,7 @@ use std::{ }; use parser::{ - ast::{self, IntSign, IntTyKind, IntTy}, + ast::{self, IntSign, IntTy, IntTyKind}, Symbol, }; use rustc_hash::{FxHashMap, FxHashSet}; diff --git a/analysis/src/ir.rs b/analysis/src/ir.rs index b288878..8ac98d7 100644 --- a/analysis/src/ir.rs +++ b/analysis/src/ir.rs @@ -37,12 +37,11 @@ mod pretty; mod validate; mod visit; -#[doc(hidden)] -pub use custom::help as custom_help; - use std::fmt::{Debug, Display}; pub use custom::define_ir_func; +#[doc(hidden)] +pub use custom::help as custom_help; use either::Either; use parser::{ast, Span, Symbol}; pub use pretty::{func_to_string, ir_to_string}; diff --git a/analysis/src/ir/custom.rs b/analysis/src/ir/custom.rs index 3db0943..9216ecf 100644 --- a/analysis/src/ir/custom.rs +++ b/analysis/src/ir/custom.rs @@ -1,5 +1,5 @@ pub mod help { - use crate::ir::{Operand, Register, ConstValue}; + use crate::ir::{ConstValue, Operand, Register}; pub trait AsOperand { fn as_operand(self) -> Operand; diff --git a/analysis/src/ir/info.rs b/analysis/src/ir/info.rs index 1a98e87..ab63aa1 100644 --- a/analysis/src/ir/info.rs +++ b/analysis/src/ir/info.rs @@ -1,8 +1,7 @@ use rustc_hash::FxHashSet; -use crate::ir::visit::Visitor; - use super::{BbIdx, Branch, Func, Location, Operand}; +use crate::ir::visit::Visitor; pub fn traverse_postorder<'a>(func: &'a Func<'_>) -> Vec { // the final traversial, backwards. diff --git a/analysis/src/ir/pretty.rs b/analysis/src/ir/pretty.rs index 22a416a..4893b1b 100644 --- a/analysis/src/ir/pretty.rs +++ b/analysis/src/ir/pretty.rs @@ -65,7 +65,11 @@ impl PrettyPrinter { for stmt in &bb.statements { match stmt.kind { - StatementKind::Alloca { result: reg, size, align } => { + StatementKind::Alloca { + result: reg, + size, + align, + } => { writeln!( self.out, " {} = alloca, size={}, align={}", diff --git a/analysis/src/ir/visit.rs b/analysis/src/ir/visit.rs index c37fc60..882dfd5 100644 --- a/analysis/src/ir/visit.rs +++ b/analysis/src/ir/visit.rs @@ -38,7 +38,7 @@ pub trait Visitor { self.visit_reg(result); self.visit_operand(size); self.visit_operand(align); - }, + } StatementKind::Store { ptr, value, @@ -49,7 +49,7 @@ pub trait Visitor { self.visit_operand(value); self.visit_operand(size); self.visit_operand(align); - }, + } StatementKind::Load { result, ptr, @@ -60,7 +60,7 @@ pub trait Visitor { self.visit_operand(ptr); self.visit_operand(size); self.visit_operand(align); - }, + } StatementKind::BinOp { kind: _, lhs, @@ -70,11 +70,15 @@ pub trait Visitor { self.visit_reg(result); self.visit_operand(lhs); self.visit_operand(rhs); - }, - StatementKind::UnaryOperation { rhs, kind: _, result } => { + } + StatementKind::UnaryOperation { + rhs, + kind: _, + result, + } => { self.visit_reg(result); self.visit_operand(rhs); - }, + } StatementKind::PtrOffset { result, ptr, @@ -83,14 +87,18 @@ pub trait Visitor { self.visit_reg(result); self.visit_operand(ptr); self.visit_operand(amount); - }, - StatementKind::Call { result, func, ref args } => { + } + StatementKind::Call { + result, + func, + ref args, + } => { self.visit_reg(result); self.visit_operand(func); for &arg in args { self.visit_operand(arg); } - }, + } } } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 47bc477..c129137 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -63,7 +63,6 @@ impl Debug for Span { } } - #[derive(Debug)] pub struct Error { pub msg: String, @@ -103,7 +102,6 @@ impl Error { } } - impl DebugPls for Error { fn fmt(&self, f: dbg_pls::Formatter<'_>) { f.debug_struct("Error") @@ -113,7 +111,6 @@ impl DebugPls for Error { } } - fn lex_and_pre(src: &str) -> impl Iterator, Span)> + '_ { let pre_tokens = pre::preprocess_tokens(src); token::pre_tokens_to_tokens(pre_tokens) diff --git a/src/main.rs b/src/main.rs index 58cb978..83db157 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,32 @@ +use std::io::Read; + use analysis::LoweringCx; use parser::Error; fn main() { let input_file = std::env::args().nth(1).expect("first argument"); - let src = std::fs::read_to_string(&input_file).unwrap_or_else(|err| { - eprintln!("failed to read file {input_file}: {err}"); - std::process::exit(1); - }); + + let (filename, src) = if input_file == "-" { + let mut buf = String::new(); + std::io::stdin() + .lock() + .read_to_string(&mut buf) + .unwrap_or_else(|err| { + eprintln!("failed to read file {input_file}: {err}"); + std::process::exit(1); + }); + ("".into(), buf) + } else { + let src = std::fs::read_to_string(&input_file).unwrap_or_else(|err| { + eprintln!("failed to read file {input_file}: {err}"); + std::process::exit(1); + }); + (input_file, src) + }; let ast = parser::parse_file(&src); // dbg_pls::color!(&ast); - let ast = ast.unwrap_or_else(|err| report_fatal(&input_file, &src, err)); + let ast = ast.unwrap_or_else(|err| report_fatal(&filename, &src, err)); let mut printer = parser::pretty::PrettyPrinter::new(std::io::stdout().lock(), false); println!("-------- AST pretty"); printer.translation_unit(&ast).unwrap(); @@ -20,10 +36,10 @@ fn main() { println!("-------- IR"); let ir = analysis::lower_translation_unit(&mut lcx, &ast) - .unwrap_or_else(|err| report_fatal(&input_file, &src, err)); + .unwrap_or_else(|err| report_fatal(&filename, &src, err)); println!("-------- ASM"); - codegen::generate(&lcx, &ir).unwrap_or_else(|err| report_fatal(&input_file, &src, err)); + codegen::generate(&lcx, &ir).unwrap_or_else(|err| report_fatal(&filename, &src, err)); } fn report_fatal(filename: &str, source: &str, error: Error) -> ! {