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 {
use crate::ir::{ConstValue, Operand, Register};
pub trait AsOperand {
fn as_operand(self) -> Operand;
pub trait ToOperand {
fn to_operand(self) -> Operand;
}
pub fn op(o: impl AsOperand) -> Operand {
o.as_operand()
pub fn op(o: impl ToOperand) -> Operand {
o.to_operand()
}
impl AsOperand for Register {
fn as_operand(self) -> Operand {
impl ToOperand for Register {
fn to_operand(self) -> Operand {
Operand::Reg(self)
}
}
impl AsOperand for u64 {
fn as_operand(self) -> Operand {
impl ToOperand for u64 {
fn to_operand(self) -> Operand {
Operand::Const(ConstValue::u64(self))
}
}

View file

@ -3,7 +3,7 @@ use rustc_hash::FxHashSet;
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(func: &Func<'_>) -> Vec<BbIdx> {
// the final traversial, backwards.
// the starting bb has to be visited last.
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 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
.arena
@ -450,7 +450,7 @@ fn lower_func<'cx>(
def_span,
ret_ty,
lcx,
params.len().try_into().unwrap(),
params.len(),
),
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 {
let reg = self.new_reg(None, tyl.clone());
let reg = self.new_reg(None, tyl);
let stmt = StatementKind::Load {
result: reg,
ptr,

View file

@ -1,3 +1,5 @@
use std::cmp::Ordering;
use parser::{
ast::{IntSign, IntTy, IntTyKind},
Error, Span,
@ -61,15 +63,18 @@ impl<'a, 'cx> FnLoweringCtxt<'a, 'cx> {
} else if (lhs_sign.unsigned() && rhs_prom_int.0.unsigned())
|| (lhs_sign.signed() && rhs_sign.signed())
{
if lhs_kind > rhs_kind {
rhs_coerce.extend(self.coerce(rhs_prom, lhs_prom)?);
lhs
} else if lhs_kind < rhs_kind {
lhs_coerce.extend(self.coerce(lhs_prom, rhs_prom)?);
rhs
} else {
unreachable!("integers must have different rank here")
match lhs_kind.cmp(&rhs_kind) {
Ordering::Greater => {
rhs_coerce.extend(self.coerce(rhs_prom, lhs_prom)?);
lhs
}
Ordering::Less => {
lhs_coerce.extend(self.coerce(lhs_prom, rhs_prom)?);
rhs
}
Ordering::Equal => unreachable!("integers must have different rank here"),
}
// 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
// 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);
for (_def_id, func) in &ir.funcs {
for func in ir.funcs.values() {
let code = x86_64::generate_func(lcx, func)?;
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}")))?;
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 {
// std::fs::remove_file("main.o").map_err(|err| {
// analysis::Error::new_without_span(format!(

View file

@ -542,7 +542,7 @@ where
let cond = self.expr()?;
let _paren_span = expect!(self, Tok::Punct(P::ParenClose));
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()?)
} else {
None