add support for - input

This commit is contained in:
nora 2023-05-28 18:59:27 +02:00
parent d0be270de2
commit 2b39ddebb2
8 changed files with 50 additions and 27 deletions

View file

@ -4,7 +4,7 @@ use std::{
};
use parser::{
ast::{self, IntSign, IntTyKind, IntTy},
ast::{self, IntSign, IntTy, IntTyKind},
Symbol,
};
use rustc_hash::{FxHashMap, FxHashSet};

View file

@ -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};

View file

@ -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;

View file

@ -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<BbIdx> {
// the final traversial, backwards.

View file

@ -65,7 +65,11 @@ impl<W: Write> PrettyPrinter<W> {
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={}",

View file

@ -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);
}
},
}
}
}

View file

@ -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<Item = (Token<'_>, Span)> + '_ {
let pre_tokens = pre::preprocess_tokens(src);
token::pre_tokens_to_tokens(pre_tokens)

View file

@ -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);
});
("<stdin>".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) -> ! {