Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSA draft #11

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
7 changes: 4 additions & 3 deletions src/codegen/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::codegen::ssa::{BasicBlock, SSABuilder};
use crate::lexer::Token;
use crate::parser::{Block, Expr, FnDeclaration, Program, Statement, VarDeclaration};
use std::collections::HashMap;
use std::mem::uninitialized;

#[derive(Debug)]
pub enum AbstractAssemblyInstruction {
Expand Down Expand Up @@ -81,6 +81,8 @@ pub struct Context {
label_counter: usize,
/// Given a variable name, get the associated temp
var_to_temp: HashMap<String, usize>,
/// For converting to SSA form
ssa_builder: SSABuilder,
}

impl Context {
Expand All @@ -90,9 +92,8 @@ impl Context {
instructions: Vec::new(),
temp_counter: 0,
label_counter: 0,
/// TODO: if we're converting to SSA, then we'd want to create a new version of each variable
/// for each assignment, as well as for each branch. Also some way of placing phi nodes
var_to_temp: HashMap::new(),
ssa_builder: SSABuilder::new(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::path::PathBuf;

mod context;
use context::Context;

mod emit;
mod ssa;

pub enum Target {
AbstractAssembly,
Expand Down
40 changes: 40 additions & 0 deletions src/codegen/ssa.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::codegen::context::{AbstractAssemblyInstruction, AsmLabel, Context};
use std::collections::HashMap;

pub struct SSABuilder {
cfg: ControlFlowGraph,
/// Track current block for SSABuilder
current_block: BasicBlock,
}

pub struct ControlFlowGraph {
blocks: HashMap<BasicBlockId, BasicBlock>,
edges: HashMap<BasicBlockId, (Option<BasicBlockId>, Option<BasicBlockId>)>,
}

#[derive(Debug)]
pub struct BasicBlock {
id: BasicBlockId,
label: Option<AsmLabel>,
instructions: Vec<AbstractAssemblyInstruction>,
}

#[derive(Debug)]
pub struct BasicBlockId(usize);

impl SSABuilder {
pub fn new() -> Self {}

pub fn convert_to_ssa(context: &Context) -> Context {
// 1. Build CFG from context.instructions
// 2. Compute dominance frontiers
// 3. Insert phi nodes at dominance frontiers
// 4. Rename variables
}

fn compute_dominance_frontiers(&mut self) {}

fn insert_phi_nodes(&mut self, context: &mut Context) {}

fn rename_variables(&mut self, context: &mut Context) {}
}