mirror of
https://github.com/Noratrieb/uwucc.git
synced 2026-01-14 16:45:07 +01:00
add support for - input
This commit is contained in:
parent
d0be270de2
commit
2b39ddebb2
8 changed files with 50 additions and 27 deletions
|
|
@ -4,7 +4,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use parser::{
|
use parser::{
|
||||||
ast::{self, IntSign, IntTyKind, IntTy},
|
ast::{self, IntSign, IntTy, IntTyKind},
|
||||||
Symbol,
|
Symbol,
|
||||||
};
|
};
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
|
|
|
||||||
|
|
@ -37,12 +37,11 @@ mod pretty;
|
||||||
mod validate;
|
mod validate;
|
||||||
mod visit;
|
mod visit;
|
||||||
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub use custom::help as custom_help;
|
|
||||||
|
|
||||||
use std::fmt::{Debug, Display};
|
use std::fmt::{Debug, Display};
|
||||||
|
|
||||||
pub use custom::define_ir_func;
|
pub use custom::define_ir_func;
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub use custom::help as custom_help;
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use parser::{ast, Span, Symbol};
|
use parser::{ast, Span, Symbol};
|
||||||
pub use pretty::{func_to_string, ir_to_string};
|
pub use pretty::{func_to_string, ir_to_string};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
pub mod help {
|
pub mod help {
|
||||||
use crate::ir::{Operand, Register, ConstValue};
|
use crate::ir::{ConstValue, Operand, Register};
|
||||||
|
|
||||||
pub trait AsOperand {
|
pub trait AsOperand {
|
||||||
fn as_operand(self) -> Operand;
|
fn as_operand(self) -> Operand;
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
use crate::ir::visit::Visitor;
|
|
||||||
|
|
||||||
use super::{BbIdx, Branch, Func, Location, Operand};
|
use super::{BbIdx, Branch, Func, Location, Operand};
|
||||||
|
use crate::ir::visit::Visitor;
|
||||||
|
|
||||||
pub fn traverse_postorder<'a>(func: &'a Func<'_>) -> Vec<BbIdx> {
|
pub fn traverse_postorder<'a>(func: &'a Func<'_>) -> Vec<BbIdx> {
|
||||||
// the final traversial, backwards.
|
// the final traversial, backwards.
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,11 @@ impl<W: Write> PrettyPrinter<W> {
|
||||||
|
|
||||||
for stmt in &bb.statements {
|
for stmt in &bb.statements {
|
||||||
match stmt.kind {
|
match stmt.kind {
|
||||||
StatementKind::Alloca { result: reg, size, align } => {
|
StatementKind::Alloca {
|
||||||
|
result: reg,
|
||||||
|
size,
|
||||||
|
align,
|
||||||
|
} => {
|
||||||
writeln!(
|
writeln!(
|
||||||
self.out,
|
self.out,
|
||||||
" {} = alloca, size={}, align={}",
|
" {} = alloca, size={}, align={}",
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ pub trait Visitor {
|
||||||
self.visit_reg(result);
|
self.visit_reg(result);
|
||||||
self.visit_operand(size);
|
self.visit_operand(size);
|
||||||
self.visit_operand(align);
|
self.visit_operand(align);
|
||||||
},
|
}
|
||||||
StatementKind::Store {
|
StatementKind::Store {
|
||||||
ptr,
|
ptr,
|
||||||
value,
|
value,
|
||||||
|
|
@ -49,7 +49,7 @@ pub trait Visitor {
|
||||||
self.visit_operand(value);
|
self.visit_operand(value);
|
||||||
self.visit_operand(size);
|
self.visit_operand(size);
|
||||||
self.visit_operand(align);
|
self.visit_operand(align);
|
||||||
},
|
}
|
||||||
StatementKind::Load {
|
StatementKind::Load {
|
||||||
result,
|
result,
|
||||||
ptr,
|
ptr,
|
||||||
|
|
@ -60,7 +60,7 @@ pub trait Visitor {
|
||||||
self.visit_operand(ptr);
|
self.visit_operand(ptr);
|
||||||
self.visit_operand(size);
|
self.visit_operand(size);
|
||||||
self.visit_operand(align);
|
self.visit_operand(align);
|
||||||
},
|
}
|
||||||
StatementKind::BinOp {
|
StatementKind::BinOp {
|
||||||
kind: _,
|
kind: _,
|
||||||
lhs,
|
lhs,
|
||||||
|
|
@ -70,11 +70,15 @@ pub trait Visitor {
|
||||||
self.visit_reg(result);
|
self.visit_reg(result);
|
||||||
self.visit_operand(lhs);
|
self.visit_operand(lhs);
|
||||||
self.visit_operand(rhs);
|
self.visit_operand(rhs);
|
||||||
},
|
}
|
||||||
StatementKind::UnaryOperation { rhs, kind: _, result } => {
|
StatementKind::UnaryOperation {
|
||||||
|
rhs,
|
||||||
|
kind: _,
|
||||||
|
result,
|
||||||
|
} => {
|
||||||
self.visit_reg(result);
|
self.visit_reg(result);
|
||||||
self.visit_operand(rhs);
|
self.visit_operand(rhs);
|
||||||
},
|
}
|
||||||
StatementKind::PtrOffset {
|
StatementKind::PtrOffset {
|
||||||
result,
|
result,
|
||||||
ptr,
|
ptr,
|
||||||
|
|
@ -83,14 +87,18 @@ pub trait Visitor {
|
||||||
self.visit_reg(result);
|
self.visit_reg(result);
|
||||||
self.visit_operand(ptr);
|
self.visit_operand(ptr);
|
||||||
self.visit_operand(amount);
|
self.visit_operand(amount);
|
||||||
},
|
}
|
||||||
StatementKind::Call { result, func, ref args } => {
|
StatementKind::Call {
|
||||||
|
result,
|
||||||
|
func,
|
||||||
|
ref args,
|
||||||
|
} => {
|
||||||
self.visit_reg(result);
|
self.visit_reg(result);
|
||||||
self.visit_operand(func);
|
self.visit_operand(func);
|
||||||
for &arg in args {
|
for &arg in args {
|
||||||
self.visit_operand(arg);
|
self.visit_operand(arg);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,6 @@ impl Debug for Span {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
pub msg: String,
|
pub msg: String,
|
||||||
|
|
@ -103,7 +102,6 @@ impl Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl DebugPls for Error {
|
impl DebugPls for Error {
|
||||||
fn fmt(&self, f: dbg_pls::Formatter<'_>) {
|
fn fmt(&self, f: dbg_pls::Formatter<'_>) {
|
||||||
f.debug_struct("Error")
|
f.debug_struct("Error")
|
||||||
|
|
@ -113,7 +111,6 @@ impl DebugPls for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn lex_and_pre(src: &str) -> impl Iterator<Item = (Token<'_>, Span)> + '_ {
|
fn lex_and_pre(src: &str) -> impl Iterator<Item = (Token<'_>, Span)> + '_ {
|
||||||
let pre_tokens = pre::preprocess_tokens(src);
|
let pre_tokens = pre::preprocess_tokens(src);
|
||||||
token::pre_tokens_to_tokens(pre_tokens)
|
token::pre_tokens_to_tokens(pre_tokens)
|
||||||
|
|
|
||||||
30
src/main.rs
30
src/main.rs
|
|
@ -1,16 +1,32 @@
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
use analysis::LoweringCx;
|
use analysis::LoweringCx;
|
||||||
use parser::Error;
|
use parser::Error;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input_file = std::env::args().nth(1).expect("first argument");
|
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}");
|
let (filename, src) = if input_file == "-" {
|
||||||
std::process::exit(1);
|
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);
|
let ast = parser::parse_file(&src);
|
||||||
// dbg_pls::color!(&ast);
|
// 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);
|
let mut printer = parser::pretty::PrettyPrinter::new(std::io::stdout().lock(), false);
|
||||||
println!("-------- AST pretty");
|
println!("-------- AST pretty");
|
||||||
printer.translation_unit(&ast).unwrap();
|
printer.translation_unit(&ast).unwrap();
|
||||||
|
|
@ -20,10 +36,10 @@ fn main() {
|
||||||
|
|
||||||
println!("-------- IR");
|
println!("-------- IR");
|
||||||
let ir = analysis::lower_translation_unit(&mut lcx, &ast)
|
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");
|
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) -> ! {
|
fn report_fatal(filename: &str, source: &str, error: Error) -> ! {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue