Skip to content

Commit

Permalink
Simple test added
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcnn committed Oct 24, 2023
1 parent dfef0f3 commit d20202e
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions hir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ miden-hir-type.workspace = true
miden-parsing = "0.1"
petgraph = "0.6"
paste.workspace = true
pretty_assertions = "1.0"
rustc-hash.workspace = true
smallvec.workspace = true
thiserror.workspace = true
Expand Down
11 changes: 10 additions & 1 deletion hir/src/parser/ast/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const INDENT: &str = " ";
/// Represents the label at the start of a basic block.
///
/// Labels must be unique within each function.
#[derive(PartialEq, Debug)]
pub struct Label {
pub name: Ident,
}
Expand All @@ -21,6 +22,7 @@ impl fmt::Display for Label {
}

/// Represents an argument for a basic block
#[derive(PartialEq, Debug)]
pub struct BlockArgument {
pub value: Value,
pub ty: Type,
Expand All @@ -37,6 +39,7 @@ impl fmt::Display for BlockArgument {
}

/// Represents the label and the arguments of a basic block
#[derive(PartialEq, Debug)]
pub struct BlockHeader {
pub label: Label,
pub args: Vec<BlockArgument>,
Expand Down Expand Up @@ -66,7 +69,7 @@ impl fmt::Display for BlockHeader {
}

/// Represents a basic block of instructions
#[derive(Spanned)]
#[derive(Spanned, Debug)]
pub struct Block {
#[span]
pub span: SourceSpan,
Expand All @@ -82,6 +85,12 @@ impl Block {
}
}
}
impl PartialEq for Block {
fn eq(&self, other: &Self) -> bool {
self.header == other.header
&& self.instructions == other.instructions
}
}
impl fmt::Display for Block {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "{}", self.header)?;
Expand Down
22 changes: 20 additions & 2 deletions hir/src/parser/ast/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::*;
use crate::{ArgumentExtension, ArgumentPurpose, CallConv, FunctionIdent, Type};

/// The possible visibilities of a function
#[derive(PartialEq, Debug)]
pub enum Visibility {
/// (Module) private visibility
Private,
Expand All @@ -11,6 +12,7 @@ pub enum Visibility {

/// A single parameter to a function.
/// Parameter names are defined in the entry block for the function.
#[derive(PartialEq, Debug)]
pub struct FunctionParameter {
/// The purpose of the parameter (default or struct return)
pub purpose: ArgumentPurpose,
Expand Down Expand Up @@ -44,6 +46,7 @@ impl fmt::Display for FunctionParameter {
}

/// A single return value from a function.
#[derive(PartialEq, Debug)]
pub struct FunctionReturn {
/// The bit extension for the parameter
pub extension: ArgumentExtension,
Expand All @@ -67,7 +70,7 @@ impl fmt::Display for FunctionReturn {
}

/// Represents the type signature of a function
#[derive(Spanned)]
#[derive(Spanned, Debug)]
pub struct FunctionSignature {
#[span]
pub span: SourceSpan,
Expand Down Expand Up @@ -96,6 +99,15 @@ impl FunctionSignature {
}
}
}
impl PartialEq for FunctionSignature {
fn eq(&self, other: &Self) -> bool {
self.visibility == other.visibility
&& self.call_convention == other.call_convention
&& self.name == other.name
&& self.params == other.params
&& self.returns == other.returns
}
}
impl fmt::Display for FunctionSignature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.visibility {
Expand Down Expand Up @@ -128,7 +140,7 @@ impl fmt::Display for FunctionSignature {
}

/// Represents the declaration of a function
#[derive(Spanned)]
#[derive(Spanned, Debug)]
pub struct FunctionDeclaration {
#[span]
pub span: SourceSpan,
Expand All @@ -144,6 +156,12 @@ impl FunctionDeclaration {
}
}
}
impl PartialEq for FunctionDeclaration {
fn eq(&self, other: &Self) -> bool {
self.signature == other.signature
&& self.blocks == other.blocks
}
}
impl fmt::Display for FunctionDeclaration {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.signature)?;
Expand Down
10 changes: 9 additions & 1 deletion hir/src/parser/ast/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl fmt::Display for GlobalVarInitializer {
}

/// This represents the declaration of a Miden IR global variable
#[derive(Spanned)]
#[derive(Spanned, Debug)]
pub struct GlobalVarDeclaration {
#[span]
pub span: SourceSpan,
Expand All @@ -60,6 +60,14 @@ impl GlobalVarDeclaration {
self.init = Some(init)
}
}
impl PartialEq for GlobalVarDeclaration {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.ty == other.ty
&& self.linkage == other.linkage
&& self.init == other.init
}
}
impl fmt::Display for GlobalVarDeclaration {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {} {}", self.name, self.ty, self.linkage)?;
Expand Down
23 changes: 22 additions & 1 deletion hir/src/parser/ast/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{FunctionIdent, Ident, Overflow, Type};
///
/// All intermediate values are named, and have an associated [Value].
/// Value identifiers must be globally unique.
#[derive(PartialEq, Debug)]
pub struct Value {
pub name: Ident,
}
Expand All @@ -20,6 +21,7 @@ impl fmt::Display for Value {
}

/// Immediates are converted at a later stage
#[derive(PartialEq, Debug)]
pub enum Immediate {
Pos(u128),
Neg(u128),
Expand All @@ -39,7 +41,7 @@ impl fmt::Display for Immediate {
/// An instruction consists of a single operation, and a number of values that
/// represent the results of the operation. Additionally, the instruction contains
/// the types of the produced results
#[derive(Spanned)]
#[derive(Spanned, Debug)]
pub struct Instruction {
#[span]
pub span: SourceSpan,
Expand All @@ -57,6 +59,13 @@ impl Instruction {
}
}
}
impl PartialEq for Instruction {
fn eq(&self, other: &Self) -> bool {
self.values == other.values
&& self.op == other.op
&& self.types == other.types
}
}
impl fmt::Display for Instruction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.values.is_empty() {
Expand All @@ -83,6 +92,7 @@ impl fmt::Display for Instruction {
}

/// Represents a operation and its arguments
#[derive(PartialEq, Debug)]
pub enum Operation {
BinaryOp(BinaryOpCode, Value, Value),
BinaryImmOp(BinaryImmOpCode, Value, Immediate),
Expand Down Expand Up @@ -179,6 +189,7 @@ impl fmt::Display for Operation {
}

/// Used to distinguish between user calls and kernel calls
#[derive(PartialEq, Debug)]
pub enum CallOp {
Call,
SysCall,
Expand All @@ -193,6 +204,7 @@ impl fmt::Display for CallOp {
}

/// Used to distinguish between binary operations
#[derive(PartialEq, Debug)]
pub enum BinaryOpCode {
Add(Overflow),
Sub(Overflow),
Expand Down Expand Up @@ -287,6 +299,7 @@ impl fmt::Display for BinaryOpCode {
}

/// Used to distinguish between immediate binary operations
#[derive(PartialEq, Debug)]
pub enum BinaryImmOpCode {
AddImm(Overflow),
SubImm(Overflow),
Expand Down Expand Up @@ -367,6 +380,7 @@ impl fmt::Display for BinaryImmOpCode {
}

/// Used to distinguish between unary operations
#[derive(PartialEq, Debug)]
pub enum UnaryOpCode {
Inv,
Incr,
Expand Down Expand Up @@ -405,6 +419,7 @@ impl fmt::Display for UnaryOpCode {
}

/// Used to distinguish between immediate unary operations
#[derive(PartialEq, Debug)]
pub enum UnaryImmOpCode {
I1,
I8,
Expand All @@ -430,6 +445,7 @@ impl fmt::Display for UnaryImmOpCode {
}

/// Used to distinguish between primary operations
#[derive(PartialEq, Debug)]
pub enum PrimOpCode {
Select,
Assert,
Expand All @@ -453,6 +469,7 @@ impl fmt::Display for PrimOpCode {

/// Memory offset for global variable reads.
/// Conversion to i32 happens during transformation to hir.
#[derive(PartialEq, Debug)]
pub enum Offset {
Pos(u128),
Neg(u128),
Expand Down Expand Up @@ -483,6 +500,7 @@ impl fmt::Display for Offset {
}

/// Used to distinguish between nested global value operations
#[derive(PartialEq, Debug)]
pub enum GlobalValueOpNested {
Symbol(Ident, Offset),
Load(Box<GlobalValueOpNested>, Offset),
Expand Down Expand Up @@ -511,6 +529,7 @@ impl fmt::Display for GlobalValueOpNested {
}

/// Used to distinguish between top-level global value operations
#[derive(PartialEq, Debug)]
pub enum GlobalValueOp {
Symbol(Ident, Offset),
Load(GlobalValueOpNested, Offset),
Expand Down Expand Up @@ -544,6 +563,7 @@ impl fmt::Display for GlobalValueOp {
}

/// The destination of a branch/jump
#[derive(PartialEq, Debug)]
pub struct Destination {
pub label: Label,
pub args: Vec<BlockArgument>,
Expand Down Expand Up @@ -572,6 +592,7 @@ impl fmt::Display for Destination {
}

/// A branch of a switch operation
#[derive(PartialEq, Debug)]
pub enum SwitchBranch {
Test(u128, Label),
Default(Label),
Expand Down
13 changes: 11 additions & 2 deletions hir/src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::Ident;
/// This is a type alias used to clarify that an identifier refers to a module
pub type ModuleId = Ident;

#[derive(Copy, Clone, PartialEq, Eq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ModuleType {
/// Kernel context module
Kernel,
Expand All @@ -35,7 +35,7 @@ impl fmt::Display for ModuleType {

/// This represents the parsed contents of a single Miden IR module
///
#[derive(Spanned)]
#[derive(Spanned, Debug)]
pub struct Module {
#[span]
pub span: SourceSpan,
Expand Down Expand Up @@ -66,6 +66,15 @@ impl Module {
}
}
}
impl PartialEq for Module {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.ty == other.ty
&& self.global_vars == other.global_vars
&& self.functions == other.functions
&& self.externals == other.externals
}
}
impl fmt::Display for Module {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "{} {}", self.ty, self.name)?;
Expand Down
3 changes: 3 additions & 0 deletions hir/src/parser/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,6 @@ impl miden_parsing::Parse for ast::Module {
}
}
}

#[cfg(test)]
mod tests;
12 changes: 12 additions & 0 deletions hir/src/parser/parser/tests/input/system.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module miden_ir_test

global_1 u32 internal = 0xCAFEBABE

pub cc(fast) fn miden_ir_test::test_func (zext u32, sext u32) -> u32 {
blk(v1 : u32, v2 : u32) : {
v3 = add.unchecked v1 v2 : u32
ret (v1, v3)
}
}

cc(kernel) fn exported::f1 (sret { u32, u32 }) -> [i8 ; 42];
Loading

0 comments on commit d20202e

Please sign in to comment.