Skip to content

Commit

Permalink
Add function_ module temporarily for the inst transition
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Sep 18, 2024
1 parent 63a3b10 commit 05005ea
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
21 changes: 19 additions & 2 deletions crates/ir/src/dfg_.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! This module contains Sonatine IR data flow graph.
use std::collections::BTreeSet;

use cranelift_entity::{packed_option::PackedOption, PrimaryMap, SecondaryMap};
use cranelift_entity::{entity_impl, packed_option::PackedOption, PrimaryMap, SecondaryMap};
use rustc_hash::FxHashMap;

use crate::{inst::InstId, module::ModuleCtx, Block, BlockId, GlobalVariable, Inst};
use crate::{inst::InstId, module::ModuleCtx, GlobalVariable, Inst};

use super::{
value_::{Immediate, Value, ValueId},
Expand Down Expand Up @@ -138,3 +138,20 @@ impl DataFlowGraph {
self.inst_results[inst_id].expand()
}
}

/// An opaque reference to [`Block`]
#[derive(Clone, PartialEq, Eq, Copy, Hash, PartialOrd, Ord)]
pub struct BlockId(pub u32);
entity_impl!(BlockId, "block");

/// A block data definition.
/// A Block data doesn't hold any information for layout of a program. It is managed by
/// [`super::layout::Layout`].
#[derive(Debug, Clone, Default)]
pub struct Block {}

impl Block {
pub fn new() -> Self {
Self::default()
}
}
81 changes: 81 additions & 0 deletions crates/ir/src/function_.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use super::{dfg_::DataFlowGraph, module::FuncRef, value_::ValueId, Layout, Type};
use crate::{module::ModuleCtx, Linkage};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;

pub struct Function {
/// Signature of the function.
pub sig: Signature,
pub arg_values: smallvec::SmallVec<[ValueId; 8]>,
pub dfg: DataFlowGraph,
pub layout: Layout,

/// Stores signatures of all functions that are called by the function.
pub callees: FxHashMap<FuncRef, Signature>,
}

impl Function {
pub fn new(ctx: &ModuleCtx, sig: Signature) -> Self {
let mut dfg = DataFlowGraph::new(ctx.clone());
let arg_values = sig
.args()
.iter()
.enumerate()
.map(|(idx, arg_ty)| {
let value = dfg.make_arg_value(*arg_ty, idx);
dfg.make_value(value)
})
.collect();

Self {
sig,
arg_values,
dfg,
layout: Layout::default(),
callees: FxHashMap::default(),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Signature {
/// Name of the function.
name: String,

/// Linkage of the function.
linkage: Linkage,

args: SmallVec<[Type; 8]>,
ret_ty: Type,
}

impl Signature {
pub fn new(name: &str, linkage: Linkage, args: &[Type], ret_ty: Type) -> Self {
Self {
name: name.to_string(),
linkage,
args: args.into(),
ret_ty,
}
}
pub fn name(&self) -> &str {
&self.name
}

pub fn linkage(&self) -> Linkage {
self.linkage
}

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

pub fn ret_ty(&self) -> Type {
self.ret_ty
}

#[doc(hidden)]
pub fn set_ret_ty(&mut self, ty: Type) {
self.ret_ty = ty;
}
}
1 change: 1 addition & 0 deletions crates/ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod dfg;
pub mod dfg_;
pub mod func_cursor;
pub mod function;
pub mod function_;
pub mod global_variable;
pub mod graphviz;
pub mod insn;
Expand Down

0 comments on commit 05005ea

Please sign in to comment.