This commit is contained in:
nora 2023-05-28 19:02:20 +02:00
parent 2b39ddebb2
commit b7108770b9
7 changed files with 28 additions and 23 deletions

View file

@ -1,22 +1,22 @@
pub mod help { pub mod help {
use crate::ir::{ConstValue, Operand, Register}; use crate::ir::{ConstValue, Operand, Register};
pub trait AsOperand { pub trait ToOperand {
fn as_operand(self) -> Operand; fn to_operand(self) -> Operand;
} }
pub fn op(o: impl AsOperand) -> Operand { pub fn op(o: impl ToOperand) -> Operand {
o.as_operand() o.to_operand()
} }
impl AsOperand for Register { impl ToOperand for Register {
fn as_operand(self) -> Operand { fn to_operand(self) -> Operand {
Operand::Reg(self) Operand::Reg(self)
} }
} }
impl AsOperand for u64 { impl ToOperand for u64 {
fn as_operand(self) -> Operand { fn to_operand(self) -> Operand {
Operand::Const(ConstValue::u64(self)) Operand::Const(ConstValue::u64(self))
} }
} }

View file

@ -3,7 +3,7 @@ use rustc_hash::FxHashSet;
use super::{BbIdx, Branch, Func, Location, Operand}; use super::{BbIdx, Branch, Func, Location, Operand};
use crate::ir::visit::Visitor; use crate::ir::visit::Visitor;
pub fn traverse_postorder<'a>(func: &'a Func<'_>) -> Vec<BbIdx> { pub fn traverse_postorder(func: &Func<'_>) -> Vec<BbIdx> {
// the final traversial, backwards. // the final traversial, backwards.
// the starting bb has to be visited last. // the starting bb has to be visited last.
let mut traversal = vec![BbIdx(0)]; let mut traversal = vec![BbIdx(0)];

View file

@ -42,7 +42,7 @@ pub fn lower_translation_unit<'cx>(
}; };
let def_id = lcx.next_def_id(); let def_id = lcx.next_def_id();
let func = lower_func(&lcx, body, def_span, ident.0, ret_ty, params)?; let func = lower_func(lcx, body, def_span, ident.0, ret_ty, params)?;
let args = &*lcx let args = &*lcx
.arena .arena
@ -450,7 +450,7 @@ fn lower_func<'cx>(
def_span, def_span,
ret_ty, ret_ty,
lcx, lcx,
params.len().try_into().unwrap(), params.len(),
), ),
lcx, lcx,
}; };

View file

@ -105,7 +105,7 @@ impl<'a, 'cx> FuncBuilder<'a, 'cx> {
} }
pub fn load(&mut self, tyl: TyLayout<'cx>, ptr: Operand, span: Span) -> Register { pub fn load(&mut self, tyl: TyLayout<'cx>, ptr: Operand, span: Span) -> Register {
let reg = self.new_reg(None, tyl.clone()); let reg = self.new_reg(None, tyl);
let stmt = StatementKind::Load { let stmt = StatementKind::Load {
result: reg, result: reg,
ptr, ptr,

View file

@ -1,3 +1,5 @@
use std::cmp::Ordering;
use parser::{ use parser::{
ast::{IntSign, IntTy, IntTyKind}, ast::{IntSign, IntTy, IntTyKind},
Error, Span, Error, Span,
@ -61,15 +63,18 @@ impl<'a, 'cx> FnLoweringCtxt<'a, 'cx> {
} else if (lhs_sign.unsigned() && rhs_prom_int.0.unsigned()) } else if (lhs_sign.unsigned() && rhs_prom_int.0.unsigned())
|| (lhs_sign.signed() && rhs_sign.signed()) || (lhs_sign.signed() && rhs_sign.signed())
{ {
if lhs_kind > rhs_kind { match lhs_kind.cmp(&rhs_kind) {
rhs_coerce.extend(self.coerce(rhs_prom, lhs_prom)?); Ordering::Greater => {
lhs rhs_coerce.extend(self.coerce(rhs_prom, lhs_prom)?);
} else if lhs_kind < rhs_kind { lhs
lhs_coerce.extend(self.coerce(lhs_prom, rhs_prom)?); }
rhs Ordering::Less => {
} else { lhs_coerce.extend(self.coerce(lhs_prom, rhs_prom)?);
unreachable!("integers must have different rank here") rhs
}
Ordering::Equal => unreachable!("integers must have different rank here"),
} }
// Otherwise, if the operand that has unsigned integer type has rank greater or // Otherwise, if the operand that has unsigned integer type has rank greater or
// equal to the rank of the type of the other operand, then the operand with // equal to the rank of the type of the other operand, then the operand with
// signed integer type is converted to the type of the operand with unsigned // signed integer type is converted to the type of the operand with unsigned

View file

@ -20,7 +20,7 @@ pub fn generate<'cx>(lcx: &'cx LoweringCx<'cx>, ir: &Ir<'cx>) -> Result<()> {
let text = obj.add_section(Vec::new(), b".text".to_vec(), object::SectionKind::Text); let text = obj.add_section(Vec::new(), b".text".to_vec(), object::SectionKind::Text);
for (_def_id, func) in &ir.funcs { for func in ir.funcs.values() {
let code = x86_64::generate_func(lcx, func)?; let code = x86_64::generate_func(lcx, func)?;
let offset = obj.append_section_data(text, &code, 8); let offset = obj.append_section_data(text, &code, 8);
@ -59,7 +59,7 @@ pub fn generate<'cx>(lcx: &'cx LoweringCx<'cx>, ir: &Ir<'cx>) -> Result<()> {
.map_err(|err| Error::new_without_span(format!("failed to spawn `cc`: {err}")))?; .map_err(|err| Error::new_without_span(format!("failed to spawn `cc`: {err}")))?;
if !output.status.success() { if !output.status.success() {
return Err(Error::new_without_span(format!("linking with `cc` failed"))); return Err(Error::new_without_span("linking with `cc` failed"));
} else { } else {
// std::fs::remove_file("main.o").map_err(|err| { // std::fs::remove_file("main.o").map_err(|err| {
// analysis::Error::new_without_span(format!( // analysis::Error::new_without_span(format!(

View file

@ -542,7 +542,7 @@ where
let cond = self.expr()?; let cond = self.expr()?;
let _paren_span = expect!(self, Tok::Punct(P::ParenClose)); let _paren_span = expect!(self, Tok::Punct(P::ParenClose));
let then = self.compount_or_single_statement()?; let then = self.compount_or_single_statement()?;
let otherwise = if let Some(_) = eat!(self, Tok::Kw(Kw::Else)) { let otherwise = if eat!(self, Tok::Kw(Kw::Else)).is_some() {
Some(self.compount_or_single_statement()?) Some(self.compount_or_single_statement()?)
} else { } else {
None None