Skip to content

Commit

Permalink
Basic semantic error handling; cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sbillig committed Jun 23, 2024
1 parent 33ab215 commit 0a72ef3
Show file tree
Hide file tree
Showing 30 changed files with 1,478 additions and 726 deletions.
2 changes: 1 addition & 1 deletion crates/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ keywords = ["compiler", "evm", "wasm", "smart-contract"]
[dependencies]
cranelift-entity = "0.104"
smallvec = "1.7.0"
rustc-hash = "1.1.0"
rustc-hash = "2.0.0"
sonatina-ir = { path = "../ir", version = "0.0.3-alpha" }
sonatina-triple = { path = "../triple", version = "0.0.3-alpha" }
12 changes: 8 additions & 4 deletions crates/codegen/src/critical_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ mod tests {
CriticalEdgeSplitter::new().run(func, &mut cfg);

assert_eq!(
dump_func(func),
dump_func(&module, func_ref),
"func public %test_func() -> void {
block0:
br 1.i32 block3 block1;
Expand All @@ -166,6 +166,7 @@ mod tests {
"
);

let func = &mut module.funcs[func_ref];
let mut cfg_split = ControlFlowGraph::default();
cfg_split.compute(func);
assert_eq!(cfg, cfg_split);
Expand Down Expand Up @@ -207,7 +208,7 @@ mod tests {
CriticalEdgeSplitter::new().run(func, &mut cfg);

assert_eq!(
dump_func(func),
dump_func(&module, func_ref),
"func public %test_func() -> void {
block0:
br 1.i8 block5 block1;
Expand All @@ -234,6 +235,7 @@ mod tests {
"
);

let func = &mut module.funcs[func_ref];
let mut cfg_split = ControlFlowGraph::default();
cfg_split.compute(func);
assert_eq!(cfg, cfg_split);
Expand Down Expand Up @@ -269,7 +271,7 @@ mod tests {
CriticalEdgeSplitter::new().run(func, &mut cfg);

assert_eq!(
dump_func(func),
dump_func(&module, func_ref),
"func public %test_func() -> void {
block0:
jump block1;
Expand All @@ -289,6 +291,7 @@ mod tests {
"
);

let func = &mut module.funcs[func_ref];
let mut cfg_split = ControlFlowGraph::default();
cfg_split.compute(func);
assert_eq!(cfg, cfg_split);
Expand Down Expand Up @@ -332,7 +335,7 @@ mod tests {
CriticalEdgeSplitter::new().run(func, &mut cfg);

assert_eq!(
dump_func(func),
dump_func(&module, func_ref),
"func public %test_func() -> void {
block0:
br 1.i1 block5 block6;
Expand Down Expand Up @@ -362,6 +365,7 @@ mod tests {
"
);

let func = &mut module.funcs[func_ref];
let mut cfg_split = ControlFlowGraph::default();
cfg_split.compute(func);
assert_eq!(cfg, cfg_split);
Expand Down
8 changes: 5 additions & 3 deletions crates/filecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ impl<'a> FileChecker<'a> {
func_ref: FuncRef,
) -> FileCheckResult {
let func = &mut parsed_module.module.funcs[func_ref];
let comments = &parsed_module.func_comments[func_ref];
let comments = &parsed_module.debug.func_comments[func_ref];

self.transformer.transform(func);
let func_ir = FuncWriter::new(func).dump_string().unwrap();
let func_ir = FuncWriter::new(func_ref, func, Some(&parsed_module.debug))
.dump_string()
.unwrap();

let checker = self.build_checker(comments);

Expand All @@ -171,7 +173,7 @@ impl<'a> FileChecker<'a> {
Err(errs) => {
let mut v = vec![];
for e in errs {
e.print(&mut v, self.file_path.to_str().unwrap(), &input)
e.print(&mut v, self.file_path.to_str().unwrap(), &input, true)
.unwrap()
}
Err(String::from_utf8(v).unwrap())
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ mod test {
Ok(pm) => pm.module,
Err(errs) => {
for err in errs {
eprintln!("{}", err.print_to_string("[test]", input));
eprintln!("{}", err.print_to_string("[test]", input, true));
}
panic!("parsing failed");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ keywords = ["compiler", "evm", "wasm", "smart-contract"]
primitive-types = { version = "0.12", default-features = false }
cranelift-entity = "0.104"
smallvec = "1.7.0"
rustc-hash = "1.1.0"
rustc-hash = "2.0.0"
dyn-clone = "1.0.4"
sonatina-triple = { path = "../triple", version = "0.0.3-alpha" }
indexmap = "2.0.0"
Expand Down
43 changes: 5 additions & 38 deletions crates/ir/src/builder/func_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
func_cursor::{CursorLocation, FuncCursor},
insn::{BinaryOp, CastOp, DataLocationKind, InsnData, UnaryOp},
module::FuncRef,
Block, Function, GlobalVariable, Immediate, Type, Value, ValueData,
Block, Function, GlobalVariable, Immediate, Type, Value,
};

use super::{
Expand All @@ -18,7 +18,6 @@ pub struct FunctionBuilder<C> {
func_ref: FuncRef,
pub cursor: C,
ssa_builder: SsaBuilder,
undefined: Vec<Value>,
}

macro_rules! impl_binary_insn {
Expand Down Expand Up @@ -49,7 +48,6 @@ where
func_ref,
cursor,
ssa_builder: SsaBuilder::new(),
undefined: Default::default(),
}
}

Expand All @@ -64,12 +62,9 @@ where
mut module_builder,
func,
func_ref,
undefined,
..
} = self;

debug_assert!(undefined.is_empty()); // xxx

module_builder.funcs[func_ref] = func;
module_builder
}
Expand All @@ -93,34 +88,6 @@ where
self.cursor.set_location(CursorLocation::BlockBottom(block));
}

pub fn name_value(&mut self, value: Value, name: &str) {
if let Some(v) = self.func.value_names.get_by_right(name) {
if let Some(pos) = self.undefined.iter().position(|u| u == v) {
self.func.dfg.change_to_alias(*v, value);
// self.func.dfg.values[*v] = ValueData::Alias { alias: value };
self.undefined.remove(pos);
} else {
panic!("value names must be unique");
}
}
self.func.value_names.insert(value, name.into());
}

pub fn get_named_value(&mut self, name: &str) -> Value {
if let Some(v) = self.func.value_names.get_by_right(name).copied() {
v
} else {
let v = self.func.dfg.make_value(ValueData::Immediate {
imm: Immediate::I128(424242),
ty: Type::I128,
});

self.undefined.push(v);
self.name_value(v, name);
v
}
}

pub fn make_imm_value<Imm>(&mut self, imm: Imm) -> Value
where
Imm: Into<Immediate>,
Expand Down Expand Up @@ -428,7 +395,7 @@ mod tests {
let module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
assert_eq!(
dump_func(&module.funcs[func_ref]),
dump_func(&module, func_ref),
"func public %test_func() -> void {
block0:
v2.i8 = add 1.i8 2.i8;
Expand Down Expand Up @@ -458,7 +425,7 @@ mod tests {
let module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
assert_eq!(
dump_func(&module.funcs[func_ref]),
dump_func(&module, func_ref),
"func public %test_func(v0.i32, v1.i64) -> void {
block0:
v2.i64 = sext v0;
Expand All @@ -484,7 +451,7 @@ mod tests {
let module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
assert_eq!(
dump_func(&module.funcs[func_ref]),
dump_func(&module, func_ref),
"func public %test_func() -> i32 {
block0:
return 1.i32;
Expand Down Expand Up @@ -526,7 +493,7 @@ mod tests {
let module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
assert_eq!(
dump_func(&module.funcs[func_ref]),
dump_func(&module, func_ref),
"func public %test_func(v0.i64) -> void {
block0:
br v0 block1 block2;
Expand Down
9 changes: 5 additions & 4 deletions crates/ir/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub mod test_util {
func_cursor::InsnInserter,
ir_writer::FuncWriter,
isa::{IsaBuilder, TargetIsa},
module::ModuleCtx,
Function, Linkage, Signature, Type,
module::{FuncRef, ModuleCtx},
Linkage, Module, Signature, Type,
};

pub fn build_test_isa() -> TargetIsa {
Expand All @@ -34,8 +34,9 @@ pub mod test_util {
mb.build_function(func_ref)
}

pub fn dump_func(func: &Function) -> String {
let mut writer = FuncWriter::new(func);
pub fn dump_func(module: &Module, func_ref: FuncRef) -> String {
let func = &module.funcs[func_ref];
let mut writer = FuncWriter::new(func_ref, func, None);
writer.dump_string().unwrap()
}
}
21 changes: 7 additions & 14 deletions crates/ir/src/builder/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,9 @@ mod tests {

let module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
let func = &module.funcs[func_ref];

assert_eq!(
dump_func(func),
dump_func(&module, func_ref),
"func public %test_func() -> void {
block0:
v1.i32 = add 1.i32 1.i32;
Expand Down Expand Up @@ -277,10 +276,9 @@ mod tests {

let module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
let func = &module.funcs[func_ref];

assert_eq!(
dump_func(func),
dump_func(&module, func_ref),
"func public %test_func() -> void {
block0:
br 1.i32 block2 block1;
Expand Down Expand Up @@ -358,10 +356,9 @@ mod tests {

let module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
let func = &module.funcs[func_ref];

assert_eq!(
dump_func(func),
dump_func(&module, func_ref),
"func public %test_func() -> void {
block0:
br 0.i32 block1 block2;
Expand Down Expand Up @@ -432,10 +429,9 @@ mod tests {

let module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
let func = &module.funcs[func_ref];

assert_eq!(
dump_func(func),
dump_func(&module, func_ref),
"func public %test_func() -> void {
block0:
jump block1;
Expand Down Expand Up @@ -508,10 +504,9 @@ mod tests {

let module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
let func = &module.funcs[func_ref];

assert_eq!(
dump_func(func),
dump_func(&module, func_ref),
"func public %test_func() -> void {
block0:
jump block1;
Expand Down Expand Up @@ -589,10 +584,9 @@ mod tests {

let module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
let func = &module.funcs[func_ref];

assert_eq!(
dump_func(func),
dump_func(&module, func_ref),
"func public %test_func() -> void {
block0:
jump block1;
Expand Down Expand Up @@ -668,10 +662,9 @@ mod tests {

let module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
let func = &module.funcs[func_ref];

assert_eq!(
dump_func(func),
dump_func(&module, func_ref),
"func public %test_func(v0.i32) -> i32 {
block0:
br_table v0 block4 (1.i32 block1) (2.i32 block2) (3.i32 block3);
Expand Down
19 changes: 2 additions & 17 deletions crates/ir/src/function.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
use super::{module::FuncRef, DataFlowGraph, Layout, Type, Value};
use crate::{module::ModuleCtx, types::DisplayType, Linkage};
use rustc_hash::{FxHashMap, FxHasher};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use smol_str::SmolStr;
use std::{
fmt::{self, Write},
hash::BuildHasherDefault,
};

type Bimap<K, V> = bimap::BiHashMap<K, V, BuildHasherDefault<FxHasher>>;
use std::fmt::{self, Write};

#[derive(Debug, Clone)]
pub struct Function {
Expand All @@ -18,9 +12,6 @@ pub struct Function {
pub dfg: DataFlowGraph,
pub layout: Layout,

// xxx move
pub value_names: Bimap<Value, SmolStr>,

/// Stores signatures of all functions that are called by the function.
pub callees: FxHashMap<FuncRef, Signature>,
}
Expand All @@ -43,7 +34,6 @@ impl Function {
arg_values,
dfg,
layout: Layout::default(),
value_names: Bimap::default(),
callees: FxHashMap::default(),
}
}
Expand Down Expand Up @@ -78,11 +68,6 @@ impl Signature {
self.linkage
}

// xxx remove
pub fn append_arg(&mut self, arg: Type) {
self.args.push(arg);
}

pub fn args(&self) -> &[Type] {
&self.args
}
Expand Down
Loading

0 comments on commit 0a72ef3

Please sign in to comment.