things that don't work

This commit is contained in:
nora 2022-05-23 09:52:31 +02:00
parent 04dd019806
commit b8157f2ceb
11 changed files with 368 additions and 104 deletions

245
rust2/Cargo.lock generated
View file

@ -2,6 +2,21 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "ansi_term"
version = "0.12.1"
@ -28,6 +43,21 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "base64"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "bincode"
version = "1.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
dependencies = [
"serde",
]
[[package]]
name = "bitflags"
version = "1.3.2"
@ -41,6 +71,7 @@ dependencies = [
"bumpalo",
"clap 3.1.18",
"criterion",
"dbg-pls",
"insta",
"owo-colors",
"rand",
@ -75,6 +106,12 @@ dependencies = [
"rustc_version",
]
[[package]]
name = "cc"
version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -144,6 +181,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "crc32fast"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [
"cfg-if",
]
[[package]]
name = "criterion"
version = "0.3.5"
@ -247,6 +293,35 @@ dependencies = [
"memchr",
]
[[package]]
name = "dbg-pls"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f28b65c59b1830ac65640335cf2ebef868e8c9982d1cbab01ce843ebe74d37a"
dependencies = [
"dbg-pls-derive",
"itoa 1.0.2",
"once_cell",
"prettyplease",
"proc-macro2",
"quote",
"ryu",
"syn",
"syntect",
"textwrap 0.15.0",
]
[[package]]
name = "dbg-pls-derive"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18ad0d32e09e083b0bf764cc9c2f1956518afb16b6ae0e1110c909d056f8271f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "either"
version = "1.6.1"
@ -259,6 +334,24 @@ version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]]
name = "flate2"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b39522e96686d38f4bc984b9198e3a0613264abaebaff2c5c918bfa6b6da09af"
dependencies = [
"cfg-if",
"crc32fast",
"libc",
"miniz_oxide",
]
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "getrandom"
version = "0.2.6"
@ -357,12 +450,27 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "libc"
version = "0.2.125"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b"
[[package]]
name = "line-wrap"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f30344350a2a51da54c1d53be93fade8a237e545dbcc4bdbe635413f2117cab9"
dependencies = [
"safemem",
]
[[package]]
name = "linked-hash-map"
version = "0.5.4"
@ -402,6 +510,15 @@ dependencies = [
"autocfg",
]
[[package]]
name = "miniz_oxide"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2b29bd4bc3f33391105ebee3589c19197c4271e3e5a9ec9bfe8127eeff8f082"
dependencies = [
"adler",
]
[[package]]
name = "num-traits"
version = "0.2.15"
@ -421,12 +538,43 @@ dependencies = [
"libc",
]
[[package]]
name = "num_threads"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44"
dependencies = [
"libc",
]
[[package]]
name = "once_cell"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]]
name = "onig"
version = "6.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67ddfe2c93bb389eea6e6d713306880c7f6dcc99a75b659ce145d962c861b225"
dependencies = [
"bitflags",
"lazy_static",
"libc",
"onig_sys",
]
[[package]]
name = "onig_sys"
version = "69.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dd3eee045c84695b53b20255bb7317063df090b68e18bfac0abb6c39cf7f33e"
dependencies = [
"cc",
"pkg-config",
]
[[package]]
name = "oorandom"
version = "11.1.3"
@ -451,6 +599,26 @@ version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
[[package]]
name = "pkg-config"
version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]]
name = "plist"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd39bc6cdc9355ad1dc5eeedefee696bb35c34caf21768741e81826c0bbd7225"
dependencies = [
"base64",
"indexmap",
"line-wrap",
"serde",
"time",
"xml-rs",
]
[[package]]
name = "plotters"
version = "0.3.1"
@ -485,6 +653,16 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "prettyplease"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9e07e3a46d0771a8a06b5f4441527802830b43e679ba12f44960f48dd4c6803"
dependencies = [
"proc-macro2",
"syn",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@ -587,6 +765,8 @@ version = "1.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
@ -620,6 +800,12 @@ version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695"
[[package]]
name = "safemem"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072"
[[package]]
name = "same-file"
version = "1.0.6"
@ -715,6 +901,12 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
[[package]]
name = "smawk"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f67ad224767faa3c7d8b6d91985b78e70a1324408abcb1cfcc2be4c06bc06043"
[[package]]
name = "strsim"
version = "0.10.0"
@ -732,6 +924,28 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "syntect"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b20815bbe80ee0be06e6957450a841185fcf690fe0178f14d77a05ce2caa031"
dependencies = [
"bincode",
"bitflags",
"flate2",
"fnv",
"lazy_static",
"lazycell",
"onig",
"plist",
"regex-syntax",
"serde",
"serde_derive",
"serde_json",
"walkdir",
"yaml-rust",
]
[[package]]
name = "termcolor"
version = "1.1.3"
@ -765,6 +979,11 @@ name = "textwrap"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
dependencies = [
"smawk",
"unicode-linebreak",
"unicode-width",
]
[[package]]
name = "thread_local"
@ -775,6 +994,17 @@ dependencies = [
"once_cell",
]
[[package]]
name = "time"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2702e08a7a860f005826c6815dcac101b19b5eb330c27fe4a5928fec1d20ddd"
dependencies = [
"itoa 1.0.2",
"libc",
"num_threads",
]
[[package]]
name = "tinytemplate"
version = "1.2.1"
@ -847,6 +1077,15 @@ dependencies = [
"tracing-log",
]
[[package]]
name = "unicode-linebreak"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a52dcaab0c48d931f7cc8ef826fa51690a08e1ea55117ef26f89864f532383f"
dependencies = [
"regex",
]
[[package]]
name = "unicode-width"
version = "0.1.9"
@ -983,6 +1222,12 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "xml-rs"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3"
[[package]]
name = "yaml-rust"
version = "0.4.5"

View file

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
bumpalo = { version = "3.9.1", features = ["allocator_api"] }
clap = { version = "3.1.9", features = ["derive"] }
dbg-pls = { version = "0.3.2", features = ["colors", "derive"] }
owo-colors = "3.3.0"
rand = "0.8.5"
tracing = "0.1.34"

View file

@ -1,6 +1,7 @@
use std::fmt::{Debug, Formatter};
use bumpalo::Bump;
use dbg_pls::DebugPls;
use crate::{
parse::{Ast, Instr, Span},
@ -16,7 +17,13 @@ pub struct Hir<'hir> {
impl Debug for Hir<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.stmts.fmt(f)
Debug::fmt(&self.stmts, f)
}
}
impl DebugPls for Hir<'_> {
fn fmt(&self, f: dbg_pls::Formatter<'_>) {
DebugPls::fmt(&self.stmts.iter().collect::<Vec<_>>(), f)
}
}
@ -38,11 +45,17 @@ impl<'hir> Stmt<'hir> {
impl Debug for Stmt<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.kind.fmt(f)
Debug::fmt(&self.kind, f)
}
}
#[derive(Debug, Clone)]
impl DebugPls for Stmt<'_> {
fn fmt(&self, f: dbg_pls::Formatter<'_>) {
DebugPls::fmt(&self.kind, f)
}
}
#[derive(Debug, Clone, DebugPls)]
pub enum StmtKind<'hir> {
Add(i32, u8),
Sub(i32, u8),

View file

@ -15,11 +15,13 @@ pub fn optimize<'hir>(alloc: &'hir Bump, hir: &mut Hir<'hir>) {
pass_cancel_left_right_add_sub(hir);
pass_add_sub_offset(hir);
pass_move_add_to(hir);
// pass_unroll_loops(hir);
// pass_cancel_left_right_add_sub(hir);
}
/// pass that replaces things like `Sub(1) Sub(1)` with `Sub(2)`
// TODO: This pass is really slow, speed it up please
#[tracing::instrument]
#[tracing::instrument(skip(alloc, ir_param))]
fn pass_group<'hir>(alloc: &'hir Bump, ir_param: &mut Hir<'hir>) {
let empty_ir = Hir {
stmts: Vec::new_in(alloc),
@ -85,8 +87,12 @@ fn pass_group<'hir>(alloc: &'hir Bump, ir_param: &mut Hir<'hir>) {
}
/// pass that replaces `Loop([Sub(_)])` to `SetNull`
#[tracing::instrument]
#[tracing::instrument(skip(ir))]
fn pass_find_set_null(ir: &mut Hir<'_>) {
pass_find_set_null_inner(ir)
}
fn pass_find_set_null_inner(ir: &mut Hir<'_>) {
for stmt in &mut ir.stmts {
if let Stmt {
kind: StmtKind::Loop(body),
@ -101,16 +107,19 @@ fn pass_find_set_null(ir: &mut Hir<'_>) {
trace!(?span, "Replacing Statement with SetNull");
*stmt = Stmt::new(StmtKind::SetN(0), *span);
} else {
pass_find_set_null(body);
pass_find_set_null_inner(body);
}
}
}
}
/// pass that replaces `SetN(n) Add(m)` with `SetN(n + m)`
#[tracing::instrument]
#[tracing::instrument(skip(ir))]
fn pass_set_n(ir: &mut Hir<'_>) {
window_pass(ir, pass_set_n, |[a, b]| {
pass_set_n_inner(ir)
}
fn pass_set_n_inner(ir: &mut Hir<'_>) {
window_pass(ir, pass_set_n_inner, |[a, b]| {
if let StmtKind::SetN(before) = a.kind() {
let new = match b.kind() {
StmtKind::Add(0, n) => StmtKind::SetN(before.wrapping_add(*n)),
@ -126,9 +135,13 @@ fn pass_set_n(ir: &mut Hir<'_>) {
}
/// pass that replaces `Left(5) Right(3)` with `Left(2)`
#[tracing::instrument]
#[tracing::instrument(skip(ir))]
fn pass_cancel_left_right_add_sub(ir: &mut Hir<'_>) {
window_pass(ir, pass_cancel_left_right_add_sub, |[a, b]| {
pass_cancel_left_right_add_sub_inner(ir)
}
fn pass_cancel_left_right_add_sub_inner(ir: &mut Hir<'_>) {
window_pass(ir, pass_cancel_left_right_add_sub_inner, |[a, b]| {
match (a.kind(), b.kind()) {
(StmtKind::Right(r), StmtKind::Left(l)) | (StmtKind::Left(l), StmtKind::Right(r)) => {
let new = match r.cmp(l) {
@ -159,9 +172,12 @@ fn pass_cancel_left_right_add_sub(ir: &mut Hir<'_>) {
}
/// pass that replaces `Right(9) Add(5) Left(9)` with `AddOffset(9, 5)`
#[tracing::instrument]
#[tracing::instrument(skip(ir))]
fn pass_add_sub_offset(ir: &mut Hir<'_>) {
window_pass(ir, pass_add_sub_offset, |[a, b, c]| {
pass_add_sub_offset_inner(ir)
}
fn pass_add_sub_offset_inner(ir: &mut Hir<'_>) {
window_pass(ir, pass_add_sub_offset_inner, |[a, b, c]| {
match (a.kind(), b.kind(), c.kind()) {
(StmtKind::Right(r), StmtKind::Add(0, n), StmtKind::Left(l)) if r == l => {
WindowPassAction::Merge(StmtKind::Add(i32::try_from(*r).unwrap(), *n))
@ -181,8 +197,12 @@ fn pass_add_sub_offset(ir: &mut Hir<'_>) {
}
/// pass that replaces `Loop([Sub(1) AddOffset(o, 1)])` with `MoveAddTo(o)`
#[tracing::instrument]
#[tracing::instrument(skip(ir))]
fn pass_move_add_to(ir: &mut Hir<'_>) {
pass_move_add_to_inner(ir)
}
fn pass_move_add_to_inner(ir: &mut Hir<'_>) {
for stmt in &mut ir.stmts {
if let Stmt {
kind: StmtKind::Loop(body),
@ -207,22 +227,47 @@ fn pass_move_add_to(ir: &mut Hir<'_>) {
trace!(?span, ?offset, "Replacing Statement with MoveAddTo");
*stmt = Stmt::new(StmtKind::MoveAddTo { offset: *offset }, *span);
} else {
pass_move_add_to(body);
pass_move_add_to_inner(body);
}
}
}
}
enum WindowPassAction<'hir> {
#[tracing::instrument(skip(ir))]
fn pass_unroll_loops(ir: &mut Hir<'_>) {
let alloc = Bump::new();
pass_unroll_loops_inner(&alloc, ir);
}
fn pass_unroll_loops_inner(alloc: &Bump, ir: &mut Hir<'_>) {
window_pass(ir, pass_unroll_loops, |[a, b]| {
if let (StmtKind::SetN(n), StmtKind::Loop(body)) = (a.kind(), b.kind()) {
let mut stmts_vec = BumpVec::new_in(alloc);
let stmts = std::iter::repeat(body.stmts.iter())
.take(usize::from(*n))
.flatten()
.cloned();
stmts_vec.extend(stmts);
WindowPassAction::MergeMany(stmts_vec)
} else {
WindowPassAction::None
}
})
}
enum WindowPassAction<'hir, 'pass> {
None,
Merge(StmtKind<'hir>),
MergeMany(BumpVec<'pass, Stmt<'hir>>),
RemoveAll,
}
fn window_pass<'hir, P, F, const N: usize>(ir: &mut Hir<'hir>, pass_recur: P, action: F)
fn window_pass<'hir, 'pass, P, F, const N: usize>(ir: &mut Hir<'hir>, pass_recur: P, action: F)
where
P: Fn(&mut Hir<'hir>),
F: Fn([&Stmt<'hir>; N]) -> WindowPassAction<'hir>,
F: Fn([&Stmt<'hir>; N]) -> WindowPassAction<'hir, 'pass>,
{
assert!(N > 0);
@ -262,6 +307,15 @@ where
}
stmts[i] = Stmt::new(new, merged_span);
}
WindowPassAction::MergeMany(new) => {
trace!(?elements, ?new, "Merging many");
for _ in 0..N {
stmts.remove(i);
}
for stmt in new.into_iter().rev() {
stmts.insert(i, stmt);
}
}
}
}
}

View file

@ -23,8 +23,8 @@ pub mod parse;
#[derive(clap::Parser, Default)]
#[clap(author, about)]
pub struct Args {
/// Print colored source code depending on how often it was run.
/// Makes the interpreter ~30% slower.
/// Print colored source code depending on how often it was run.
/// Makes the interpreter ~30% slower.
#[clap(short, long)]
pub profile: bool,
/// Dump the IR info (ast, hir, mir, lir)
@ -85,7 +85,7 @@ where
let optimized_hir = hir::optimized_hir(&hir_alloc, &parsed);
if let Some(DumpKind::Hir) = config.dump {
println!("{optimized_hir:#?}");
println!("{}", dbg_pls::color(&optimized_hir));
return Ok(());
}

View file

@ -1,8 +1,11 @@
use std::{cmp, fmt::Debug};
use std::{
cmp,
fmt::{Debug, Formatter},
};
use bumpalo::Bump;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub struct Span {
start: u32,
len: u32,
@ -62,6 +65,12 @@ impl Span {
}
}
impl Debug for Span {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&(self.start..(self.start + self.len)), f)
}
}
pub type Ast<'ast> = Vec<(Instr<'ast>, Span), &'ast Bump>;
#[derive(Debug, Clone, PartialEq, Eq)]

View file

@ -1,95 +1,61 @@
---
source: src/parse.rs
assertion_line: 164
expression: instrs
---
Ok(
[
(
Add,
Span {
start: 0,
len: 1,
},
0..1,
),
(
Loop(
[
(
Sub,
Span {
start: 2,
len: 1,
},
2..3,
),
(
Loop(
[
(
Sub,
Span {
start: 4,
len: 1,
},
4..5,
),
(
Loop(
[
(
Sub,
Span {
start: 6,
len: 1,
},
6..7,
),
],
),
Span {
start: 5,
len: 3,
},
5..8,
),
],
),
Span {
start: 3,
len: 6,
},
3..9,
),
(
Add,
Span {
start: 9,
len: 1,
},
9..10,
),
(
Right,
Span {
start: 10,
len: 1,
},
10..11,
),
(
Right,
Span {
start: 11,
len: 1,
},
11..12,
),
(
Right,
Span {
start: 12,
len: 1,
},
12..13,
),
],
),
Span {
start: 1,
len: 13,
},
1..14,
),
],
)

View file

@ -1,68 +1,43 @@
---
source: src/parse.rs
assertion_line: 155
expression: instrs
---
Ok(
[
(
Right,
Span {
start: 0,
len: 1,
},
0..1,
),
(
Add,
Span {
start: 1,
len: 1,
},
1..2,
),
(
Left,
Span {
start: 2,
len: 1,
},
2..3,
),
(
Add,
Span {
start: 3,
len: 1,
},
3..4,
),
(
Add,
Span {
start: 4,
len: 1,
},
4..5,
),
(
Loop(
[
(
Sub,
Span {
start: 6,
len: 1,
},
6..7,
),
],
),
Span {
start: 5,
len: 3,
},
5..8,
),
(
Out,
Span {
start: 8,
len: 1,
},
8..9,
),
],
)

2
rust2/test.bf vendored
View file

@ -1 +1 @@
[+>+..]
+++++>[-]+++[<.>]