Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"crates/analyzer",
"crates/emitter",
"crates/formatter",
"crates/ir",
"crates/languageserver",
"crates/mdbook",
"crates/metadata",
Expand Down Expand Up @@ -42,6 +43,8 @@ handlebars = "6.3"
log = "0.4.28"
mdbook = "0.4.51"
miette = {version = "7.6"}
num-bigint = "0.4.6"
num-traits = "0.2.19"
once_cell = "1.21"
parol_runtime = "4.0"
pulldown-cmark = "0.13.0"
Expand Down
4 changes: 2 additions & 2 deletions crates/analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ daggy = "0.9.0"
fxhash = {workspace = true}
itertools = "0.14.0"
log = {workspace = true}
num-bigint = "0.4.6"
num-traits = "0.2.19"
num-bigint = {workspace = true}
num-traits = {workspace = true}
smallvec = {workspace = true}
strnum_bitwidth = {workspace = true}
thiserror = {workspace = true}
Expand Down
16 changes: 16 additions & 0 deletions crates/ir/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "veryl-ir"
version = "0.16.5"
authors.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
license.workspace = true
readme.workspace = true
description.workspace = true
edition.workspace = true

[dependencies]
num-bigint = {workspace = true}
num-traits = {workspace = true}
veryl-parser = {version = "0.16.5", path = "../parser"}
8 changes: 8 additions & 0 deletions crates/ir/src/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::variable::{VarId, Variable};
use std::collections::HashMap;
use veryl_parser::resource_table::StrId;

pub struct Block {
name: StrId,
variables: HashMap<VarId, Variable>,
}
256 changes: 256 additions & 0 deletions crates/ir/src/expression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
use crate::variable::{Value, VarId, Variable};
use num_bigint::BigUint;
use std::collections::HashMap;
use std::fmt;

pub enum Expression {
Term(Factor),
Unary(Op, Box<Expression>),
Binary(Box<Expression>, Op, Box<Expression>),
Concatenation(Vec<Expression>),
}

impl Expression {
pub fn eval(&self, context_width: Option<usize>, map: &HashMap<VarId, Variable>) -> Value {
match self {
Expression::Term(x) => match x {
Factor::Variable(x) => map.get(x).unwrap().value.clone(),
Factor::Value(x) => x.clone(),
},
Expression::Unary(op, x) => {
let mut ret = x.eval(context_width, map);
match op {
Op::BitAnd => {
ret.payload = reduction(ret.payload, ret.width, |x, y| x & y);
}
Op::BitOr => {
ret.payload = reduction(ret.payload, ret.width, |x, y| x | y);
}
Op::BitXor => {
ret.payload = reduction(ret.payload, ret.width, |x, y| x ^ y);
}
Op::BitXnor => {
ret.payload = reduction(ret.payload, ret.width, |x, y| {
((x ^ y) == 0u32.into()).into()
});
}
Op::BitNand => {
ret.payload = reduction(ret.payload, ret.width, |x, y| {
((x & y) == 0u32.into()).into()
});
}
Op::BitNor => {
ret.payload = reduction(ret.payload, ret.width, |x, y| {
((x | y) == 0u32.into()).into()
});
}
Op::BitNot => {
ret.payload = BigUint::from_slice(
&ret.payload
.to_u32_digits()
.iter()
.map(|x| !x)
.collect::<Vec<_>>(),
);
}
Op::LogicNot => {
ret.payload = (ret.payload == 0u32.into()).into();
ret.width = 1;
ret.signed = false;
}
_ => unreachable!(),
}
ret
}
Expression::Binary(x, op, y) => {
let x = x.eval(context_width, map);
let y = y.eval(context_width, map);
match op {
Op::Pow => {
let (width, payload) = binary_op(
(x.width, x.payload),
(y.width, y.payload),
context_width,
|x, _, z| x.max(z.unwrap_or(0)),
|x, y| y.try_into().map(|y| x.pow(y)).unwrap(),
);
Value {
payload,
width,
signed: false,
}
}
Op::Div => {
let (width, payload) = binary_op(
(x.width, x.payload),
(y.width, y.payload),
context_width,
|x, y, z| x.max(y).max(z.unwrap_or(0)),
|x, y| x / y,
);
Value {
payload,
width,
signed: false,
}
}
Op::Rem => {
let (width, payload) = binary_op(
(x.width, x.payload),
(y.width, y.payload),
context_width,
|x, y, z| x.max(y).max(z.unwrap_or(0)),
|x, y| x % y,
);
Value {
payload,
width,
signed: false,
}
}
Op::Mul => todo!(),
Op::Add => todo!(),
Op::Sub => todo!(),
Op::ArithShiftL => todo!(),
Op::ArithShiftR => todo!(),
Op::LogicShiftL => todo!(),
Op::LogicShiftR => todo!(),
Op::LessEq => todo!(),
Op::GreaterEq => todo!(),
Op::Less => todo!(),
Op::Greater => todo!(),
Op::Eq => todo!(),
Op::Ne => todo!(),
Op::LogicAnd => todo!(),
Op::LogicOr => todo!(),
Op::LogicNot => todo!(),
Op::BitAnd => todo!(),
Op::BitOr => todo!(),
Op::BitXor => todo!(),
Op::BitXnor => todo!(),
Op::BitNand => todo!(),
Op::BitNor => todo!(),
Op::BitNot => todo!(),
}
}
_ => todo!(),
}
}
}

fn reduction<T: Fn(BigUint, BigUint) -> BigUint>(value: BigUint, width: usize, func: T) -> BigUint {
let mut tmp = value;
let mut ret = tmp.clone() & BigUint::from(1u32);
for _ in 0..width {
tmp >>= 1;
ret = func(ret, tmp.clone() & BigUint::from(1u32));
}
ret
}

fn binary_op<T: Fn(usize, usize, Option<usize>) -> usize, U: Fn(BigUint, BigUint) -> BigUint>(
x: (usize, BigUint),
y: (usize, BigUint),
context_width: Option<usize>,
calc_width: T,
calc_value: U,
) -> (usize, BigUint) {
let width = calc_width(x.0, y.0, context_width);
let value = calc_value(x.1, y.1);
(width, value)
}

pub enum Factor {
Variable(VarId),
Value(Value),
}

pub enum Op {
/// **
Pow,
/// /
Div,
/// %
Rem,
/// *
Mul,
/// +
Add,
/// -
Sub,
/// <<<
ArithShiftL,
/// >>>
ArithShiftR,
/// <<
LogicShiftL,
/// >>
LogicShiftR,
/// <=
LessEq,
/// >=
GreaterEq,
/// <:
Less,
/// >:
Greater,
/// ==
Eq,
/// !=
Ne,
/// &&
LogicAnd,
/// ||
LogicOr,
/// !
LogicNot,
/// &
BitAnd,
/// |
BitOr,
/// ^
BitXor,
/// ~^ ^~
BitXnor,
/// ~&
BitNand,
/// ~|
BitNor,
/// ~
BitNot,
}

impl fmt::Display for Op {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let str = match self {
Op::Pow => "**",
Op::Div => "/",
Op::Rem => "%",
Op::Mul => "*",
Op::Add => "+",
Op::Sub => "-",
Op::ArithShiftL => "<<<",
Op::ArithShiftR => ">>>",
Op::LogicShiftL => "<<",
Op::LogicShiftR => ">>",
Op::LessEq => "<=",
Op::GreaterEq => ">=",
Op::Less => "<:",
Op::Greater => ">:",
Op::Eq => "==",
Op::Ne => "!=",
Op::LogicAnd => "&&",
Op::LogicOr => "||",
Op::LogicNot => "!",
Op::BitAnd => "&",
Op::BitOr => "|",
Op::BitXor => "^",
Op::BitXnor => "^~",
Op::BitNand => "~&",
Op::BitNor => "~|",
Op::BitNot => "~",
};

str.fmt(f)
}
}
3 changes: 3 additions & 0 deletions crates/ir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod block;
mod expression;
mod variable;
Loading
Loading