Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
rikhuijzer committed Dec 21, 2024
1 parent 3c06de6 commit f70dc80
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 25 deletions.
7 changes: 4 additions & 3 deletions xrcf/src/convert/experimental_to_mlir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::shared::Shared;
use crate::shared::SharedExt;
use anyhow::Result;
use dialect::experimental::PrintfOp;
use std::str::FromStr;
use std::sync::Arc;

struct PrintLowering;
Expand Down Expand Up @@ -50,7 +51,7 @@ impl PrintLowering {
fn len_specifier(parent: &Shared<Block>, len: usize) -> Shared<dyn Op> {
let mut operation = Operation::default();
operation.set_parent(Some(parent.clone()));
let typ = IntegerType::from_str("i16");
let typ = IntegerType::from_str("i16").unwrap();
let name = parent.rd().unique_value_name("%");
let result_type = Shared::new(typ.into());
let result = operation.add_new_op_result(&name, result_type);
Expand Down Expand Up @@ -117,7 +118,7 @@ impl PrintLowering {
let var = var.expect("expected vararg");
operation.set_operand(1, var);
}
let typ = IntegerType::from_str("i32");
let typ = IntegerType::from_str("i32").unwrap();
let name = parent.rd().unique_value_name("%");
let result_type = Shared::new(typ.into());
let result = operation.add_new_op_result(&name, result_type);
Expand Down Expand Up @@ -171,7 +172,7 @@ impl PrintLowering {
fn printf_func_def(parent: Shared<Block>, set_varargs: bool) -> Result<Shared<dyn Op>> {
let mut operation = Operation::default();
operation.set_parent(Some(parent.clone()));
let result_type = crate::ir::IntegerType::from_str("i32");
let result_type = IntegerType::from_str("i32").unwrap();
let result_type = Shared::new(result_type.into());
operation.set_anonymous_result(result_type)?;

Expand Down
3 changes: 2 additions & 1 deletion xrcf/src/dialect/func/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::shared::Shared;
use crate::shared::SharedExt;
use anyhow::Result;
use std::fmt::Formatter;
use std::str::FromStr;
use std::sync::Arc;

const TOKEN_KIND: TokenKind = TokenKind::PercentIdentifier;
Expand Down Expand Up @@ -469,7 +470,7 @@ impl<T: ParserDispatch> Parser<T> {
operation.set_operands(self.parse_op_operands(parent.clone().unwrap(), TOKEN_KIND)?);
self.expect(TokenKind::Colon)?;
let return_type = self.expect(TokenKind::IntType)?;
let return_type = IntegerType::from_str(&return_type.lexeme);
let return_type = IntegerType::from_str(&return_type.lexeme).unwrap();
let result_type = Shared::new(return_type.into());
operation.set_anonymous_result(result_type)?;
}
Expand Down
11 changes: 6 additions & 5 deletions xrcf/src/dialect/llvm/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::shared::SharedExt;
use anyhow::Result;
use std::fmt::Display;
use std::fmt::Formatter;
use std::str::FromStr;

/// Represent an integer type such as i32 or i64.
///
Expand All @@ -31,7 +32,7 @@ impl ArrayType {
};
let (num_elements, element_type) = s.split_once('x').unwrap();
let num_elements = num_elements.parse::<u32>().unwrap();
let element_type = IntegerType::from_str(element_type);
let element_type = IntegerType::from_str(element_type).unwrap();
let element_type = Shared::new(element_type.into());
Self {
num_elements,
Expand All @@ -43,7 +44,7 @@ impl ArrayType {
let text = s.to_string();
let text = text.trim_matches('"');
let num_elements = text.as_bytes().len() as u32;
let element_type = IntegerType::from_str("i8");
let element_type = IntegerType::from_str("i8").unwrap();
let element_type = Shared::new(element_type.into());
Self {
num_elements,
Expand All @@ -52,7 +53,7 @@ impl ArrayType {
}
pub fn for_bytes(bytes: &Vec<u8>) -> Self {
let num_elements = bytes.len() as u32;
let element_type = IntegerType::from_str("i8");
let element_type = IntegerType::from_str("i8").unwrap();
let element_type = Shared::new(element_type.into());
Self {
num_elements,
Expand Down Expand Up @@ -107,9 +108,9 @@ impl FunctionType {
pub fn from_str(s: &str) -> Self {
assert!(s.starts_with("!llvm.func<"));
let s = s.strip_prefix("!llvm.func<").unwrap();
let (return_types, arguments) = s.split_once('(').unwrap();
let (ret_types, arguments) = s.split_once('(').unwrap();

let return_type = IntegerType::from_str(return_types.trim());
let return_type = IntegerType::from_str(ret_types.trim()).unwrap();
let return_type = Shared::new(return_type.into());
let return_types = Types::from_vec(vec![return_type]);

Expand Down
3 changes: 2 additions & 1 deletion xrcf/src/frontend/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::ir::Values;
use crate::shared::Shared;
use crate::shared::SharedExt;
use anyhow::Result;
use std::str::FromStr;
use std::sync::Arc;

/// Interface to add custom operations to the parser.
Expand Down Expand Up @@ -104,7 +105,7 @@ pub fn default_dispatch<T: ParserDispatch>(
pub fn default_parse_type<T: ParserDispatch>(parser: &mut Parser<T>) -> Result<Shared<dyn Type>> {
if parser.check(TokenKind::IntType) {
let typ = parser.advance();
let typ = IntegerType::from_str(&typ.lexeme);
let typ = IntegerType::from_str(&typ.lexeme).unwrap();
return Ok(Shared::new(typ.into()));
}
let text = parser.parse_type_text()?;
Expand Down
5 changes: 3 additions & 2 deletions xrcf/src/ir/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use anyhow::Result;
use std::collections::HashMap;
use std::fmt::Display;
use std::fmt::Formatter;
use std::str::FromStr;
use std::sync::Arc;

/// Attributes are known-constant values of operations (a variable is not allowed).
Expand Down Expand Up @@ -63,7 +64,7 @@ impl Attribute for BooleanAttr {
Self { value }
}
fn typ(&self) -> Shared<dyn Type> {
Shared::new(IntegerType::from_str("i1").into())
Shared::new(IntegerType::from_str("i1").unwrap().into())
}
fn as_any(&self) -> &dyn std::any::Any {
self
Expand Down Expand Up @@ -282,7 +283,7 @@ impl<T: ParserDispatch> Parser<T> {
let _colon = self.expect(TokenKind::Colon)?;

let num_bits = self.expect(TokenKind::IntType)?;
let typ = IntegerType::from_str(&num_bits.lexeme);
let typ = IntegerType::from_str(&num_bits.lexeme).unwrap();
let value = APInt::from_str(&num_bits.lexeme, &value);
let integer = IntegerAttr::new(typ, value);
Ok(integer)
Expand Down
4 changes: 2 additions & 2 deletions xrcf/src/ir/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Region {
/// will point to blocks that occur later. If we would refresh the name during
/// printing of the block (like we do with ssa variables), then the operands
/// would print outdated names.
fn set_fresh_block_labels(blocks: &Vec<Shared<Block>>) {
fn set_fresh_block_labels(blocks: &[Shared<Block>]) {
let mut label_index: usize = 1;
for block in blocks.iter() {
let block = block.rd();
Expand Down Expand Up @@ -102,7 +102,7 @@ impl Region {
self.parent = parent;
}
pub fn display(&self, f: &mut Formatter<'_>, indent: i32) -> std::fmt::Result {
write!(f, " {{\n")?;
writeln!(f, " {{")?;
let blocks = self.blocks();
let blocks = blocks.vec();
let blocks = blocks.rd();
Expand Down
20 changes: 9 additions & 11 deletions xrcf/src/ir/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::shared::SharedExt;
use anyhow::Result;
use std::fmt::Display;
use std::fmt::Formatter;

use std::str::FromStr;
pub trait Type {
/// Display the type.
///
Expand Down Expand Up @@ -88,10 +88,14 @@ impl IntegerType {
pub fn new(num_bits: u64) -> Self {
Self { num_bits }
}
pub fn from_str(s: &str) -> Self {
}

impl std::str::FromStr for IntegerType {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.strip_prefix("i").expect("no i prefix");
let num_bits = s.parse::<u64>().unwrap();
Self { num_bits }
Ok(Self { num_bits })
}
}

Expand Down Expand Up @@ -145,7 +149,7 @@ impl APInt {
}
}
pub fn from_str(typ: &str, value: &str) -> Self {
let typ = IntegerType::from_str(typ);
let typ = IntegerType::from_str(typ).unwrap();
let value = value.parse::<u64>().unwrap();
Self::new(typ.num_bits, value, true)
}
Expand Down Expand Up @@ -175,7 +179,7 @@ impl Display for APInt {
/// A collection of `Type`s.
///
/// Provides some convenience methods around [Type]s.
#[derive(Clone)]
#[derive(Clone, Default)]
pub struct Types {
types: Vec<Shared<dyn Type>>,
}
Expand All @@ -197,12 +201,6 @@ impl Types {
}
}

impl Default for Types {
fn default() -> Self {
Self { types: vec![] }
}
}

impl Display for Types {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let joined = self
Expand Down

0 comments on commit f70dc80

Please sign in to comment.