Skip to content

Commit

Permalink
"standardise" printing types
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha committed Sep 24, 2023
1 parent dcb1ff7 commit 7ae15d7
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 36 deletions.
10 changes: 7 additions & 3 deletions src/dialects/builtin/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,13 @@ mod tests {
assert!(int64_0_ptr.is::<IntegerAttr>() && &int64_0_ptr != &int64_1_ptr);
let int64_0_ptr2 = IntegerAttr::create(i64_ty, ApInt::from_i64(0));
assert!(int64_0_ptr == int64_0_ptr2);
assert!(
int64_0_ptr.disp(&ctx).to_string() == "0x0: si64"
&& int64_1_ptr.disp(&ctx).to_string() == "0xf: si64"
assert_eq!(
int64_0_ptr.disp(&ctx).to_string(),
"0x0: builtin.integer <si64>"
);
assert_eq!(
int64_1_ptr.disp(&ctx).to_string(),
"0xf: builtin.integer <si64>"
);
assert!(
ApInt::from(int64_0_ptr.downcast_ref::<IntegerAttr>().unwrap().clone()).is_zero()
Expand Down
2 changes: 1 addition & 1 deletion src/dialects/builtin/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl Printable for FuncOp {
) -> core::fmt::Result {
write!(
f,
"{} @{}{} ",
"{} @{}: {} ",
self.get_opid().disp(ctx),
self.get_symbol_name(ctx),
self.get_type(ctx).disp(ctx),
Expand Down
29 changes: 16 additions & 13 deletions src/dialects/builtin/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use combine::{
choice, easy, many1,
between, choice, easy, many1,
parser::{char::digit, char::string},
ParseResult, Parser,
token, ParseResult, Parser,
};

use crate::{
Expand Down Expand Up @@ -70,7 +70,7 @@ impl Parsable for IntegerType {
let parser = choicer
.and(many1::<String, _, _>(digit()).map(|digits| digits.parse::<u64>().unwrap()));

let mut parser = spaced(parser);
let mut parser = between(spaced(token('<')), spaced(token('>')), parser);
parser
.parse_stream(&mut state_stream.stream)
.map(|(signedness, width)| IntegerType::get(state_stream.state.ctx, width, signedness))
Expand All @@ -80,15 +80,17 @@ impl Parsable for IntegerType {
impl Printable for IntegerType {
fn fmt(
&self,
_ctx: &Context,
ctx: &Context,
_state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(f, "{} <", Self::get_type_id_static().disp(ctx))?;
match &self.signedness {
Signedness::Signed => write!(f, "si{}", self.width),
Signedness::Unsigned => write!(f, "ui{}", self.width),
Signedness::Signless => write!(f, "i{}", self.width),
Signedness::Signed => write!(f, "si{}", self.width)?,
Signedness::Unsigned => write!(f, "ui{}", self.width)?,
Signedness::Signless => write!(f, "i{}", self.width)?,
}
write!(f, ">")
}
}

Expand All @@ -109,7 +111,7 @@ pub struct FunctionType {
/// Function results / outputs.
results: Vec<Ptr<TypeObj>>,
}
impl_type!(FunctionType, "Function", "builtin");
impl_type!(FunctionType, "function", "builtin");

impl FunctionType {
/// Get or create a new Function type.
Expand Down Expand Up @@ -150,7 +152,8 @@ impl Printable for FunctionType {
let sep = ListSeparator::Char(',');
write!(
f,
"({}) -> ({})",
"{} <({}) -> ({})>",
Self::get_type_id_static().disp(ctx),
self.inputs.iter().iprint(ctx, state, sep),
self.results.iter().iprint(ctx, state, sep)
)
Expand Down Expand Up @@ -232,7 +235,7 @@ mod tests {
fn test_integer_parsing() {
let mut ctx = Context::new();
let state_stream =
state_stream_from_iterator("si64".chars(), parsable::State { ctx: &mut ctx });
state_stream_from_iterator("<si64>".chars(), parsable::State { ctx: &mut ctx });

let res = IntegerType::parser()
.and(eof())
Expand All @@ -246,16 +249,16 @@ mod tests {
#[test]
fn test_integer_parsing_errs() {
let mut ctx = Context::new();
let a = "asi64".to_string();
let a = "<asi64>".to_string();
let state_stream = state_stream_from_iterator(a.chars(), parsable::State { ctx: &mut ctx });

let res = IntegerType::parser().parse(state_stream);
let err_msg = format!("{}", res.err().unwrap());

let expected_err_msg = expect![[r#"
Parse error at line: 1, column: 1
Parse error at line: 1, column: 2
Unexpected `a`
Expected whitespaces, si, ui or i
Expected whitespace, si, ui or i
"#]];
expected_err_msg.assert_eq(&err_msg);
}
Expand Down
27 changes: 18 additions & 9 deletions src/dialects/llvm/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl Printable for StructType {
_state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(f, "{} <", Self::get_type_id_static().disp(ctx))?;
use std::cell::RefCell;
// Ugly, but also the simplest way to avoid infinite recursion.
// MLIR does the same: see LLVMTypeSyntax::printStructType.
Expand All @@ -146,7 +147,7 @@ impl Printable for StructType {
if let Some(name) = &self.name {
let in_printing = IN_PRINTING.with(|f| f.borrow().contains(name));
if in_printing {
return write!(f, "{}", name.clone());
return write!(f, "{}>", name.clone());
}
IN_PRINTING.with(|f| f.borrow_mut().push(name.clone()));
s = format!("{name} {{ ");
Expand All @@ -170,7 +171,7 @@ impl Printable for StructType {
debug_assert!(IN_PRINTING.with(|f| f.borrow().last().unwrap() == name));
IN_PRINTING.with(|f| f.borrow_mut().pop());
}
write!(f, "{s}")
write!(f, "{s}>")
}
}

Expand Down Expand Up @@ -247,7 +248,12 @@ impl Printable for PointerType {
_state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(f, "{}<{}>", self.get_type_id().disp(ctx), self.to.disp(ctx))
write!(
f,
"{} <{}>",
Self::get_type_id_static().disp(ctx),
self.to.disp(ctx)
)
}
}

Expand Down Expand Up @@ -313,14 +319,14 @@ mod tests {
assert!(list_struct == list_struct_2);
assert!(StructType::get_existing_named(&ctx, "LinkedList2").is_none());

assert!(
assert_eq!(
list_struct
.deref(&ctx)
.downcast_ref::<StructType>()
.unwrap()
.disp(&ctx)
.to_string()
== "LinkedList { data: i64, next: llvm.ptr<LinkedList>, }"
.to_string(),
"llvm.struct <LinkedList { data: builtin.integer <i64>, next: llvm.ptr <llvm.struct <LinkedList>>, }>"
);

let head_fields = vec![
Expand Down Expand Up @@ -349,7 +355,10 @@ mod tests {

let int64pointer_ptr = PointerType { to: int64_ptr };
let int64pointer_ptr = Type::register_instance(int64pointer_ptr, &mut ctx);
assert!(int64pointer_ptr.disp(&ctx).to_string() == "llvm.ptr<si64>");
assert_eq!(
int64pointer_ptr.disp(&ctx).to_string(),
"llvm.ptr <builtin.integer <si64>>"
);
assert!(int64pointer_ptr == PointerType::get(&mut ctx, int64_ptr));

assert!(
Expand Down Expand Up @@ -380,11 +389,11 @@ mod tests {
dialects::llvm::register(&mut ctx);

let state_stream = state_stream_from_iterator(
"llvm.ptr <builtin.integer si64>".chars(),
"llvm.ptr <builtin.integer <si64>>".chars(),
parsable::State { ctx: &mut ctx },
);

let res = type_parser().parse(state_stream).unwrap().0;
assert_eq!(&res.disp(&ctx).to_string(), "si64");
assert_eq!(&res.disp(&ctx).to_string(), "builtin.integer <si64>");
}
}
6 changes: 3 additions & 3 deletions src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,16 +395,16 @@ mod test {
let expected_err_msg = expect![[r#"
Parse error at line: 1, column: 17
Unexpected `a`
Expected whitespaces, si, ui or i
Expected whitespaces or `<`
"#]];
expected_err_msg.assert_eq(&err_msg);

let state_stream = state_stream_from_iterator(
"builtin.integer si32".chars(),
"builtin.integer <si32>".chars(),
parsable::State { ctx: &mut ctx },
);

let parsed = type_parser().parse(state_stream).unwrap().0;
assert_eq!(parsed.disp(&ctx).to_string(), "si32");
assert_eq!(parsed.disp(&ctx).to_string(), "builtin.integer <si32>");
}
}
4 changes: 2 additions & 2 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ pub fn const_ret_in_mod(
expect![[r#"
builtin.module @bar {
block_0_0():
builtin.func @foo() -> (si64) {
builtin.func @foo: builtin.function <() -> (builtin.integer <si64>)> {
entry():
c0 = builtin.constant 0x0: si64
c0 = builtin.constant 0x0: builtin.integer <si64>
llvm.return c0
}
}"#]]
Expand Down
10 changes: 5 additions & 5 deletions tests/ir_construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ fn replace_c0_with_c1_operand() -> Result<(), CompilerError> {
expect![[r#"
builtin.module @bar {
block_0_0():
builtin.func @foo() -> (si64) {
builtin.func @foo: builtin.function <() -> (builtin.integer <si64>)> {
entry():
c0 = builtin.constant 0x0: si64
c1 = builtin.constant 0x1: si64
c0 = builtin.constant 0x0: builtin.integer <si64>
c1 = builtin.constant 0x1: builtin.integer <si64>
llvm.return c0
}
}"#]]
Expand All @@ -104,9 +104,9 @@ fn replace_c0_with_c1_operand() -> Result<(), CompilerError> {
expect![[r#"
builtin.module @bar {
block_0_0():
builtin.func @foo() -> (si64) {
builtin.func @foo: builtin.function <() -> (builtin.integer <si64>)> {
entry():
c1 = builtin.constant 0x1: si64
c1 = builtin.constant 0x1: builtin.integer <si64>
llvm.return c1
}
}"#]]
Expand Down

0 comments on commit 7ae15d7

Please sign in to comment.