It is common to represent a function as a String, and previously that required re-validating the UTF-8 in a Vec<u8>. The fmt::Write trait writes UTF-8 directly into a String, so no extra checking is required. This also means we can implement Display for Function which gives it a to_string() method. This makes the function_to_string() method redundant, so delete it. The functions in the write module are no longer generally useful, so make the module private. The Display trait on Function is all we need.
75 lines
2.2 KiB
Rust
75 lines
2.2 KiB
Rust
//! Intermediate representation of a function.
|
|
//!
|
|
//! The `Function` struct defined in this module owns all of its extended basic blocks and
|
|
//! instructions.
|
|
|
|
use ir::{FunctionName, Signature, StackSlot, StackSlotData, JumpTable, JumpTableData,
|
|
DataFlowGraph, Layout};
|
|
use entity_map::{EntityMap, PrimaryEntityData};
|
|
use std::fmt::{self, Display, Debug, Formatter};
|
|
use write::write_function;
|
|
|
|
/// A function.
|
|
///
|
|
/// Functions can be cloned, but it is not a very fast operation.
|
|
/// The clone will have all the same entity numbers as the original.
|
|
#[derive(Clone)]
|
|
pub struct Function {
|
|
/// Name of this function. Mostly used by `.cton` files.
|
|
pub name: FunctionName,
|
|
|
|
/// Signature of this function.
|
|
signature: Signature,
|
|
|
|
/// Stack slots allocated in this function.
|
|
pub stack_slots: EntityMap<StackSlot, StackSlotData>,
|
|
|
|
/// Jump tables used in this function.
|
|
pub jump_tables: EntityMap<JumpTable, JumpTableData>,
|
|
|
|
/// Data flow graph containing the primary definition of all instructions, EBBs and values.
|
|
pub dfg: DataFlowGraph,
|
|
|
|
/// Layout of EBBs and instructions in the function body.
|
|
pub layout: Layout,
|
|
}
|
|
|
|
impl PrimaryEntityData for StackSlotData {}
|
|
impl PrimaryEntityData for JumpTableData {}
|
|
|
|
impl Function {
|
|
/// Create a function with the given name and signature.
|
|
pub fn with_name_signature(name: FunctionName, sig: Signature) -> Function {
|
|
Function {
|
|
name: name,
|
|
signature: sig,
|
|
stack_slots: EntityMap::new(),
|
|
jump_tables: EntityMap::new(),
|
|
dfg: DataFlowGraph::new(),
|
|
layout: Layout::new(),
|
|
}
|
|
}
|
|
|
|
/// Create a new empty, anomymous function.
|
|
pub fn new() -> Function {
|
|
Self::with_name_signature(FunctionName::new(), Signature::new())
|
|
}
|
|
|
|
/// Get the signature of this function.
|
|
pub fn own_signature(&self) -> &Signature {
|
|
&self.signature
|
|
}
|
|
}
|
|
|
|
impl Display for Function {
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
write_function(fmt, self)
|
|
}
|
|
}
|
|
|
|
impl Debug for Function {
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
write_function(fmt, self)
|
|
}
|
|
}
|