Bump Rust and add skeleton code for irgen.

This commit is contained in:
Janggun Lee
2022-12-19 18:50:22 +09:00
parent 6ae0e26197
commit c5946611a3
26 changed files with 913 additions and 176 deletions

View File

@@ -413,7 +413,7 @@ impl TryFrom<BaseDtype> for Dtype {
ast::TypeSpecifier::Int => Self::INT,
ast::TypeSpecifier::Float => Self::FLOAT,
ast::TypeSpecifier::Double => Self::DOUBLE,
_ => panic!("Dtype::try_from::<BaseDtype>: {:?} is not a scalar type", t),
_ => panic!("Dtype::try_from::<BaseDtype>: {t:?} is not a scalar type"),
}
} else {
Self::default()
@@ -452,10 +452,9 @@ impl TryFrom<BaseDtype> for Dtype {
let is_signed = match signed_option {
ast::TypeSpecifier::Signed => true,
ast::TypeSpecifier::Unsigned => false,
_ => panic!(
"Dtype::try_from::<BaseDtype>: {:?} is not a signed option",
signed_option
),
_ => {
panic!("Dtype::try_from::<BaseDtype>: {signed_option:?} is not a signed option")
}
};
if dtype.get_int_width().is_none() {
@@ -941,7 +940,7 @@ impl Dtype {
let struct_type = structs
.get(name)
.ok_or_else(|| DtypeError::Misc {
message: format!("unknown struct name `{}`", name),
message: format!("unknown struct name `{name}`"),
})?
.as_ref()
.expect("`struct_type` must have its definition");
@@ -1223,7 +1222,7 @@ impl Dtype {
let dtype = typedefs
.get(&name)
.ok_or_else(|| DtypeError::Misc {
message: format!("unknown type name `{}`", name),
message: format!("unknown type name `{name}`"),
})?
.clone();
let is_const = dtype.is_const() || is_const;
@@ -1288,7 +1287,7 @@ impl Dtype {
} else {
let tempid = *tempid_counter;
*tempid_counter += 1;
format!("%t{}", tempid)
format!("%t{tempid}")
};
let resolved_struct = Self::structure(Some(name.clone()), Some(fields));
let filled_struct =
@@ -1297,7 +1296,7 @@ impl Dtype {
if let Some(prev_dtype) = structs.insert(name.clone(), Some(filled_struct)) {
if prev_dtype.is_some() {
return Err(DtypeError::Misc {
message: format!("redefinition of {}", name),
message: format!("redefinition of {name}"),
});
}
}
@@ -1306,11 +1305,11 @@ impl Dtype {
} else {
let name = name.expect("`name` must exist");
let struct_type = structs.get(&name).ok_or_else(|| DtypeError::Misc {
message: format!("unknown struct name `{}`", name),
message: format!("unknown struct name `{name}`"),
})?;
if struct_type.is_none() {
return Err(DtypeError::Misc {
message: format!("variable has incomplete type 'struct {}'", name),
message: format!("variable has incomplete type 'struct {name}'"),
});
}
@@ -1355,7 +1354,7 @@ impl fmt::Display for Dtype {
Self::Pointer { inner, is_const } => {
write!(f, "{}*{}", inner, if *is_const { "const" } else { "" })
}
Self::Array { inner, size, .. } => write!(f, "[{} x {}]", size, inner,),
Self::Array { inner, size, .. } => write!(f, "[{size} x {inner}]",),
Self::Struct {
name,
fields,
@@ -1374,7 +1373,7 @@ impl fmt::Display for Dtype {
field.deref()
))
});
format!(":<{}>", fields)
format!(":<{fields}>")
} else {
"".to_string()
};

View File

@@ -770,8 +770,7 @@ mod calculator {
(false, Dtype::SIZE_OF_DOUBLE) => value as f64,
_ => panic!(
"calculate_typecast: not supported case \
typecast int to float when `width` is {}",
width
typecast int to float when `width` is {width}"
),
};
Ok(Value::float(casted_value, width))
@@ -782,8 +781,7 @@ mod calculator {
} else {
panic!(
"calculate_typecast: not support case \
typecast int to pointer when `value` is {}",
value
typecast int to pointer when `value` is {value}"
)
}
}
@@ -1023,7 +1021,7 @@ impl Byte {
let value_bits: u128 = match size {
Dtype::SIZE_OF_FLOAT => (float_value.into_inner() as f32).to_bits() as u128,
Dtype::SIZE_OF_DOUBLE => (float_value.into_inner()).to_bits() as u128,
_ => panic!("value_to_bytes: {} is not a valid float size", size),
_ => panic!("value_to_bytes: {size} is not a valid float size"),
};
Self::u128_to_bytes(value_bits, size)
@@ -1206,8 +1204,7 @@ impl<'i> State<'i> {
func_name: self.stack_frame.func_name.clone(),
pc: self.stack_frame.pc,
msg: format!(
"fail to translate `Initializer` and `{}` to `Value`",
dtype
"fail to translate `Initializer` and `{dtype}` to `Value`"
),
},
)?
@@ -1430,8 +1427,7 @@ impl<'i> State<'i> {
func_name: self.stack_frame.func_name.clone(),
pc: self.stack_frame.pc,
msg: format!(
"fail to store {:?} into memory with bid: {}, offset: {}",
value, bid, offset,
"fail to store {value:?} into memory with bid: {bid}, offset: {offset}",
),
})?;
Value::Unit

View File

@@ -313,25 +313,23 @@ impl fmt::Display for Instruction {
Instruction::UnaryOp { op, operand, .. } => {
write!(f, "{} {}", op.write_operation(), operand)
}
Instruction::Store { ptr, value } => {
write!(f, "store {} {}", value, ptr)
}
Instruction::Load { ptr } => write!(f, "load {}", ptr),
Instruction::Store { ptr, value } => write!(f, "store {value} {ptr}"),
Instruction::Load { ptr } => write!(f, "load {ptr}"),
Instruction::Call { callee, args, .. } => {
write!(
f,
"call {}({})",
callee,
args.iter()
.format_with(", ", |operand, f| f(&format_args!("{}", operand)))
.format_with(", ", |operand, f| f(&format_args!("{operand}")))
)
}
Instruction::TypeCast {
value,
target_dtype,
} => write!(f, "typecast {} to {}", value, target_dtype),
} => write!(f, "typecast {value} to {target_dtype}"),
Instruction::GetElementPtr { ptr, offset, .. } => {
write!(f, "getelementptr {} offset {}", ptr, offset)
write!(f, "getelementptr {ptr} offset {offset}")
}
}
}
@@ -385,12 +383,12 @@ impl BlockExit {
impl fmt::Display for BlockExit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BlockExit::Jump { arg } => write!(f, "j {}", arg),
BlockExit::Jump { arg } => write!(f, "j {arg}"),
BlockExit::ConditionalJump {
condition,
arg_then,
arg_else,
} => write!(f, "br {}, {}, {}", condition, arg_then, arg_else),
} => write!(f, "br {condition}, {arg_then}, {arg_else}"),
BlockExit::Switch {
value,
default,
@@ -407,7 +405,7 @@ impl fmt::Display for BlockExit {
b
)))
),
BlockExit::Return { value } => write!(f, "ret {}", value),
BlockExit::Return { value } => write!(f, "ret {value}"),
BlockExit::Unreachable => write!(f, "<unreachable>\t\t\t\t; error state"),
}
}
@@ -482,7 +480,7 @@ impl fmt::Display for Operand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Constant(value) => write!(f, "{}:{}", value, value.dtype()),
Self::Register { rid, dtype } => write!(f, "{}:{}", rid, dtype),
Self::Register { rid, dtype } => write!(f, "{rid}:{dtype}"),
}
}
}
@@ -548,9 +546,9 @@ impl RegisterId {
impl fmt::Display for RegisterId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Local { aid } => write!(f, "%l{}", aid),
Self::Arg { bid, aid } => write!(f, "%{}:p{}", bid, aid),
Self::Temp { bid, iid } => write!(f, "%{}:i{}", bid, iid),
Self::Local { aid } => write!(f, "%l{aid}"),
Self::Arg { bid, aid } => write!(f, "%{bid}:p{aid}"),
Self::Temp { bid, iid } => write!(f, "%{bid}:i{iid}"),
}
}
}
@@ -678,8 +676,7 @@ impl TryFrom<&ast::Constant> for Constant {
}
_ => panic!(
"Constant::try_from::<&ast::Constant>: \
{:?} is not a pattern of `pat`",
pat
{pat:?} is not a pattern of `pat`"
),
};
(Dtype::FLOAT, value)
@@ -694,8 +691,7 @@ impl TryFrom<&ast::Constant> for Constant {
}
_ => panic!(
"Constant::try_from::<&ast::Constant>: \
{:?} is not a pattern of `pat`",
pat
{pat:?} is not a pattern of `pat`"
),
};
(Dtype::DOUBLE, value)
@@ -942,8 +938,8 @@ impl fmt::Display for Constant {
value.to_string()
}
),
Self::Float { value, .. } => write!(f, "{}", value),
Self::GlobalVariable { name, .. } => write!(f, "@{}", name),
Self::Float { value, .. } => write!(f, "{value}"),
Self::GlobalVariable { name, .. } => write!(f, "@{name}"),
}
}
}

View File

@@ -677,8 +677,7 @@ impl<P: AsRef<Path>> Translate<P> for Parse {
fn translate(&mut self, source: &P) -> Result<Self::Target, Self::Error> {
let ir = fs::read_to_string(source).map_err(Error::Io)?;
let ir = ir_parse::translation_unit(&ir).map_err(Error::Parse)?;
Ok(ir)
ir_parse::translation_unit(&ir).map_err(Error::Parse)
}
}

View File

@@ -50,7 +50,7 @@ impl Translate<TranslationUnit> for Visualizer {
let from = self.translate_instruction_node(name, *bid, iid);
let to = self.translate_callee(name, callee)?;
edges.push(format!("{} -> {};", from, to));
edges.push(format!("{from} -> {to};"));
}
}
}
@@ -59,7 +59,7 @@ impl Translate<TranslationUnit> for Visualizer {
let inner = vec![subgraphs, edges].concat().join("\n");
Ok(format!("digraph G {{\n{}\n}}", inner))
Ok(format!("digraph G {{\n{inner}\n}}"))
}
}
@@ -82,12 +82,12 @@ impl Visualizer {
#[inline]
fn translate_instruction_node(&self, name: &str, bid: BlockId, iid: usize) -> String {
format!("\"{}:{}:i{}\"", name, bid, iid)
format!("\"{name}:{bid}:i{iid}\"")
}
#[inline]
fn translate_block_exit_node(&self, name: &str, bid: BlockId) -> String {
format!("\"{}:{}:exit\"", name, bid)
format!("\"{name}:{bid}:exit\"")
}
#[inline]
@@ -178,7 +178,7 @@ impl Visualizer {
// TODO: Add init information (bid_init, allocations)
let inner = vec![subgraphs, vec![label], edges].concat().join("\n");
Ok(format!("subgraph \"cluster.{}\" {{\n{}\n}}", name, inner))
Ok(format!("subgraph \"cluster.{name}\" {{\n{inner}\n}}"))
}
fn translate_block(&mut self, name: &str, bid: &BlockId, block: &Block) -> Result<String, ()> {
@@ -186,7 +186,7 @@ impl Visualizer {
header.push("style=filled;".to_string());
header.push("color=lightgrey;".to_string());
header.push("node [shape=record];".to_string());
header.push(format!("label=\"{}\";", bid));
header.push(format!("label=\"{bid}\";"));
let mut nodes = Vec::new();
@@ -223,9 +223,6 @@ impl Visualizer {
let inner = vec![header, nodes, vec![edges]].concat().join("\n");
Ok(format!(
"subgraph \"cluster.{}.{}\" {{\n{}\n}}",
name, bid, inner
))
Ok(format!("subgraph \"cluster.{name}.{bid}\" {{\n{inner}\n}}"))
}
}

View File

@@ -28,12 +28,12 @@ impl WriteLine for TranslationUnit {
))
});
format!("{{ {} }}", fields)
format!("{{ {fields} }}")
} else {
"opaque".to_string()
};
writeln!(write, "struct {} : {}", name, definition)?;
writeln!(write, "struct {name} : {definition}")?;
}
for (name, decl) in &self.decls {
@@ -93,7 +93,7 @@ impl WriteLine for (&String, &Declaration) {
i,
a.deref(),
if let Some(name) = a.name() {
format!(":{}", name)
format!(":{name}")
} else {
"".into()
}
@@ -101,7 +101,7 @@ impl WriteLine for (&String, &Declaration) {
)?;
for (id, block) in &definition.blocks {
writeln!(write, "\nblock {}:", id)?;
writeln!(write, "\nblock {id}:")?;
(id, block).write_line(indent + 1, write)?;
}
@@ -128,7 +128,7 @@ impl WriteLine for (&BlockId, &Block) {
RegisterId::arg(*self.0, i),
phi.deref(),
if let Some(name) = phi.name() {
format!(":{}", name)
format!(":{name}")
} else {
"".into()
}
@@ -143,7 +143,7 @@ impl WriteLine for (&BlockId, &Block) {
RegisterId::temp(*self.0, i),
instr.dtype(),
if let Some(name) = instr.name() {
format!(":{}", name)
format!(":{name}")
} else {
"".into()
},
@@ -160,18 +160,18 @@ impl WriteLine for (&BlockId, &Block) {
impl WriteString for Instruction {
fn write_string(&self) -> String {
format!("{}", self)
format!("{self}")
}
}
impl WriteString for Operand {
fn write_string(&self) -> String {
format!("{}", self)
format!("{self}")
}
}
impl WriteString for BlockExit {
fn write_string(&self) -> String {
format!("{}", self)
format!("{self}")
}
}