Skip to content
Snippets Groups Projects
Commit 59d6a895 authored by Russel Arbore's avatar Russel Arbore
Browse files

Skeleton IR parsing code

parent 55718b78
No related branches found
No related tags found
No related merge requests found
pub struct FunctionID(u32);
pub struct NodeID(u32);
const NULL_NODE: NodeID = NodeID(0xFFFFFFFF);
pub struct ConstantID(u32);
pub struct TypeID(u32);
#[derive(Clone)]
pub struct Module {
functions: Vec<Function>,
typed: Vec<Type>,
constants: Vec<Constant>,
pub functions: Vec<Function>,
pub types: Vec<Type>,
pub constants: Vec<Constant>,
}
#[derive(Clone)]
pub struct Function {
name: String,
nodes: Vec<Node>,
pub name: String,
pub nodes: Vec<Node>,
}
#[derive(Clone)]
pub enum Type {
Control,
Control(u64),
Integer8,
Integer16,
Integer32,
......@@ -28,6 +22,7 @@ pub enum Type {
Float64,
}
#[derive(Clone)]
pub enum Constant {
Integer8(u8),
Integer16(u16),
......@@ -37,6 +32,7 @@ pub enum Constant {
Float64(f64),
}
#[derive(Clone)]
pub enum Node {
Start,
Region {
......@@ -46,6 +42,14 @@ pub enum Node {
control: NodeID,
cond: NodeID,
},
Fork {
control: NodeID,
factor: u64,
},
Join {
control: NodeID,
factor: u64,
},
Phi {
control: NodeID,
data: Box<[NodeID]>,
......@@ -54,6 +58,9 @@ pub enum Node {
control: NodeID,
value: NodeID,
},
Parameter {
index: u64,
},
Constant {
id: ConstantID,
},
......@@ -82,56 +89,38 @@ pub enum Node {
},
}
impl From<u32> for FunctionID {
fn from(v: u32) -> Self {
FunctionID(v)
}
}
impl From<u64> for FunctionID {
fn from(v: u64) -> Self {
FunctionID(v as u32)
}
}
#[derive(Clone)]
pub struct FunctionID(u32);
impl From<usize> for FunctionID {
fn from(v: usize) -> Self {
FunctionID(v as u32)
impl FunctionID {
pub fn idx(&self) -> usize {
self.0 as usize
}
}
impl From<u32> for NodeID {
fn from(v: u32) -> Self {
NodeID(v)
}
}
#[derive(Clone)]
pub struct NodeID(u32);
impl From<u64> for NodeID {
fn from(v: u64) -> Self {
NodeID(v as u32)
impl NodeID {
pub fn idx(&self) -> usize {
self.0 as usize
}
}
impl From<usize> for NodeID {
fn from(v: usize) -> Self {
NodeID(v as u32)
}
}
#[derive(Clone)]
pub struct ConstantID(u32);
impl From<u32> for ConstantID {
fn from(v: u32) -> Self {
ConstantID(v)
impl ConstantID {
pub fn idx(&self) -> usize {
self.0 as usize
}
}
impl From<u64> for ConstantID {
fn from(v: u64) -> Self {
ConstantID(v as u32)
}
}
#[derive(Clone)]
pub struct TypeID(u32);
impl From<usize> for ConstantID {
fn from(v: usize) -> Self {
ConstantID(v as u32)
impl TypeID {
pub fn idx(&self) -> usize {
self.0 as usize
}
}
pub mod ir;
pub mod parse;
pub use crate::ir::*;
pub use crate::parse::*;
extern crate nom;
use std::collections::HashMap;
use crate::*;
fn parse(ir_test: &str) -> Module {
parse_module(ir_test, Context::default()).unwrap().1
}
#[derive(Default)]
struct Context<'a> {
function_ids: HashMap<&'a str, FunctionID>,
node_ids: HashMap<&'a str, NodeID>,
interned_types: HashMap<Type, TypeID>,
interned_constants: HashMap<Constant, ConstantID>,
}
fn parse_module<'a>(ir_text: &'a str, mut context: Context<'a>) -> nom::IResult<&'a str, Module> {
let (rest, functions) = nom::multi::many0(|x| parse_function(x, &mut context))(ir_text)?;
nom::combinator::eof(rest)?;
let mut types = vec![Type::Control(0); context.interned_types.len()];
for (ty, id) in context.interned_types {
types[id.idx()] = ty;
}
let mut constants = vec![Constant::Integer8(0); context.interned_constants.len()];
for (constant, id) in context.interned_constants {
constants[id.idx()] = constant;
}
Ok((
rest,
Module {
functions,
types,
constants,
},
))
}
fn parse_function<'a>(
ir_text: &'a str,
context: &mut Context<'a>,
) -> nom::IResult<&'a str, Function> {
todo!()
}
mod tests {
#[allow(unused_imports)]
use super::*;
#[test]
fn parse_ir1() {
parse("fn add(x: i32, y: i32) -> i32 return(z) z = add(start, x, y)");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment