Skip to content

Commit

Permalink
wip pest parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sbillig committed Jun 20, 2024
1 parent ac1eb92 commit cec6a13
Show file tree
Hide file tree
Showing 108 changed files with 3,940 additions and 901 deletions.
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
[workspace]
resolver = "2"
members = ["crates/ir", "crates/codegen", "crates/object", "crates/parser", "crates/filecheck", "crates/triple", "crates/interpreter"]
members = [
"crates/ir",
"crates/codegen",
"crates/object",
"crates/parser",
"crates/parser2",
"crates/filecheck",
"crates/triple",
"crates/interpreter",
]
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"
fxhash = "0.2.1"
rustc-hash = "1.1.0"
sonatina-ir = { path = "../ir", version = "0.0.3-alpha" }
sonatina-triple = { path = "../triple", version = "0.0.3-alpha" }
45 changes: 19 additions & 26 deletions crates/codegen/src/critical_edge.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use sonatina_ir::func_cursor::FuncCursor;
use sonatina_ir::ControlFlowGraph;

use sonatina_ir::{
func_cursor::{CursorLocation, FuncCursor, InsnInserter},
func_cursor::{CursorLocation, InsnInserter},
insn::InsnData,
Block, Function, Insn,
};
Expand Down Expand Up @@ -65,10 +66,10 @@ impl CriticalEdgeSplitter {
// critical edge.
let inserted_dest = func.dfg.make_block();
let jump = func.dfg.make_insn(InsnData::jump(original_dest));
let mut cursor = InsnInserter::new(func, CursorLocation::BlockTop(original_dest));
cursor.append_block(inserted_dest);
cursor.set_loc(CursorLocation::BlockTop(inserted_dest));
cursor.append_insn(jump);
let mut cursor = InsnInserter::at_location(CursorLocation::BlockTop(original_dest));
cursor.append_block(func, inserted_dest);
cursor.set_location(CursorLocation::BlockTop(inserted_dest));
cursor.append_insn(func, jump);

// Rewrite branch destination to the new block.
func.dfg
Expand Down Expand Up @@ -123,8 +124,7 @@ mod tests {

#[test]
fn critical_edge_basic() {
let mut test_module_builder = TestModuleBuilder::new();
let mut builder = test_module_builder.func_builder(&[], Type::Void);
let mut builder = test_func_builder(&[], Type::Void);

let a = builder.append_block();
let b = builder.append_block();
Expand All @@ -141,9 +141,8 @@ mod tests {
builder.ret(None);

builder.seal_all();
let func_ref = builder.finish();

let mut module = test_module_builder.build();
let mut module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
let func = &mut module.funcs[func_ref];
let mut cfg = ControlFlowGraph::default();
cfg.compute(func);
Expand Down Expand Up @@ -175,8 +174,7 @@ mod tests {
#[test]
#[allow(clippy::many_single_char_names)]
fn critical_edge_to_same_block() {
let mut test_module_builder = TestModuleBuilder::new();
let mut builder = test_module_builder.func_builder(&[], Type::Void);
let mut builder = test_func_builder(&[], Type::Void);

let a = builder.append_block();
let b = builder.append_block();
Expand All @@ -201,9 +199,8 @@ mod tests {
builder.ret(None);

builder.seal_all();
let func_ref = builder.finish();

let mut module = test_module_builder.build();
let mut module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
let func = &mut module.funcs[func_ref];
let mut cfg = ControlFlowGraph::default();
cfg.compute(func);
Expand Down Expand Up @@ -243,8 +240,7 @@ mod tests {

#[test]
fn critical_edge_phi() {
let mut test_module_builder = TestModuleBuilder::new();
let mut builder = test_module_builder.func_builder(&[], Type::Void);
let mut builder = test_func_builder(&[], Type::Void);

let a = builder.append_block();
let b = builder.append_block();
Expand All @@ -255,7 +251,7 @@ mod tests {
builder.jump(b);

builder.switch_to_block(b);
let phi_value = builder.phi(&[(v1, a)]);
let phi_value = builder.phi(Type::I8, &[(v1, a)]);
let v2 = builder.add(phi_value, v1);
builder.append_phi_arg(phi_value, v2, b);
builder.br(phi_value, c, b);
Expand All @@ -264,9 +260,8 @@ mod tests {
builder.ret(None);

builder.seal_all();
let func_ref = builder.finish();

let mut module = test_module_builder.build();
let mut module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
let func = &mut module.funcs[func_ref];
let mut cfg = ControlFlowGraph::default();
cfg.compute(func);
Expand Down Expand Up @@ -299,8 +294,7 @@ mod tests {

#[test]
fn critical_edge_br_table() {
let mut test_module_builder = TestModuleBuilder::new();
let mut builder = test_module_builder.func_builder(&[], Type::Void);
let mut builder = test_func_builder(&[], Type::Void);

let a = builder.append_block();
let b = builder.append_block();
Expand Down Expand Up @@ -328,9 +322,8 @@ mod tests {
builder.ret(None);

builder.seal_all();
let func_ref = builder.finish();

let mut module = test_module_builder.build();
let mut module = builder.finish().build();
let func_ref = module.iter_functions().next().unwrap();
let func = &mut module.funcs[func_ref];
let mut cfg = ControlFlowGraph::default();
cfg.compute(func);
Expand Down
30 changes: 13 additions & 17 deletions crates/codegen/src/domtree.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module contains dominantor tree related structs.
//!
//! The algorithm is based on Keith D. Cooper., Timothy J. Harvey., and Ken Kennedy.: A Simple, Fast Dominance Algorithm:
//! The algorithm is based on Keith D. Cooper., Timothy J. Harvey., and Ken Kennedy.: A Simple, Fast Dominance Algorithm:
//! <https://www.cs.rice.edu/~keith/EMBED/dom.pdf>
use std::collections::BTreeSet;
Expand Down Expand Up @@ -230,8 +230,7 @@ mod tests {

#[test]
fn dom_tree_if_else() {
let mut test_module_builder = TestModuleBuilder::new();
let mut builder = test_module_builder.func_builder(&[], Type::Void);
let mut builder = test_func_builder(&[], Type::Void);

let entry_block = builder.append_block();
let then_block = builder.append_block();
Expand All @@ -252,9 +251,9 @@ mod tests {
builder.ret(None);

builder.seal_all();
let func_ref = builder.finish();

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

let (dom_tree, df) = calc_dom(func);
Expand All @@ -271,8 +270,7 @@ mod tests {

#[test]
fn unreachable_edge() {
let mut test_module_builder = TestModuleBuilder::new();
let mut builder = test_module_builder.func_builder(&[], Type::Void);
let mut builder = test_func_builder(&[], Type::Void);

let a = builder.append_block();
let b = builder.append_block();
Expand All @@ -297,9 +295,9 @@ mod tests {
builder.ret(None);

builder.seal_all();
let func_ref = builder.finish();

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

let (dom_tree, df) = calc_dom(func);
Expand All @@ -319,8 +317,7 @@ mod tests {

#[test]
fn dom_tree_complex() {
let mut test_module_builder = TestModuleBuilder::new();
let mut builder = test_module_builder.func_builder(&[], Type::Void);
let mut builder = test_func_builder(&[], Type::Void);

let a = builder.append_block();
let b = builder.append_block();
Expand Down Expand Up @@ -377,9 +374,9 @@ mod tests {
builder.ret(None);

builder.seal_all();
let func_ref = builder.finish();

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

let (dom_tree, df) = calc_dom(func);
Expand Down Expand Up @@ -412,8 +409,7 @@ mod tests {

#[test]
fn dom_tree_br_table() {
let mut test_module_builder = TestModuleBuilder::new();
let mut builder = test_module_builder.func_builder(&[], Type::Void);
let mut builder = test_func_builder(&[], Type::Void);

let a = builder.append_block();
let b = builder.append_block();
Expand Down Expand Up @@ -445,9 +441,9 @@ mod tests {
builder.ret(None);

builder.seal_all();
let func_ref = builder.finish();

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

let (dom_tree, df) = calc_dom(func);
Expand Down
34 changes: 15 additions & 19 deletions crates/codegen/src/loop_analysis.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cranelift_entity::{entity_impl, packed_option::PackedOption, PrimaryMap, SecondaryMap};
use fxhash::FxHashMap;
use rustc_hash::FxHashMap;
use smallvec::SmallVec;

use crate::domtree::DomTree;
Expand Down Expand Up @@ -261,8 +261,7 @@ mod tests {

#[test]
fn simple_loop() {
let mut test_module_builder = TestModuleBuilder::new();
let mut builder = test_module_builder.func_builder(&[], Type::Void);
let mut builder = test_func_builder(&[], Type::Void);

let b0 = builder.append_block();
let b1 = builder.append_block();
Expand All @@ -274,7 +273,7 @@ mod tests {
builder.jump(b1);

builder.switch_to_block(b1);
let v1 = builder.phi(&[(v0, b0)]);
let v1 = builder.phi(Type::I32, &[(v0, b0)]);
let c0 = builder.make_imm_value(10i32);
let v2 = builder.eq(v1, c0);
builder.br(v2, b3, b2);
Expand All @@ -289,9 +288,9 @@ mod tests {
builder.ret(None);

builder.seal_all();
let func_ref = builder.finish();

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

Expand All @@ -307,8 +306,7 @@ mod tests {

#[test]
fn continue_loop() {
let mut test_module_builder = TestModuleBuilder::new();
let mut builder = test_module_builder.func_builder(&[], Type::Void);
let mut builder = test_func_builder(&[], Type::Void);

let b0 = builder.append_block();
let b1 = builder.append_block();
Expand All @@ -323,7 +321,7 @@ mod tests {
builder.jump(b1);

builder.switch_to_block(b1);
let v1 = builder.phi(&[(v0, b0)]);
let v1 = builder.phi(Type::I32, &[(v0, b0)]);
let c0 = builder.make_imm_value(10i32);
let v2 = builder.eq(v1, c0);
builder.br(v2, b5, b2);
Expand Down Expand Up @@ -352,9 +350,9 @@ mod tests {
builder.ret(None);

builder.seal_all();
let func_ref = builder.finish();

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

Expand All @@ -374,8 +372,7 @@ mod tests {

#[test]
fn single_block_loop() {
let mut test_module_builder = TestModuleBuilder::new();
let mut builder = test_module_builder.func_builder(&[Type::I1], Type::Void);
let mut builder = test_func_builder(&[Type::I1], Type::Void);
let b0 = builder.append_block();
let b1 = builder.append_block();
let b2 = builder.append_block();
Expand All @@ -392,9 +389,9 @@ mod tests {
builder.ret(None);

builder.seal_all();
let func_ref = builder.finish();

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

Expand All @@ -408,8 +405,7 @@ mod tests {

#[test]
fn nested_loop() {
let mut test_module_builder = TestModuleBuilder::new();
let mut builder = test_module_builder.func_builder(&[Type::I1], Type::Void);
let mut builder = test_func_builder(&[Type::I1], Type::Void);

let b0 = builder.append_block();
let b1 = builder.append_block();
Expand Down Expand Up @@ -463,9 +459,9 @@ mod tests {
builder.ret(None);

builder.seal_all();
let func_ref = builder.finish();

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

Expand Down
Loading

0 comments on commit cec6a13

Please sign in to comment.