pr 10
This commit is contained in:
@@ -37,4 +37,16 @@ impl ConstPool {
|
||||
pub fn get(&self, id: ConstId) -> Option<&ConstantValue> {
|
||||
self.constants.get(id.0 as usize)
|
||||
}
|
||||
|
||||
pub fn add_int(&mut self, value: i64) -> ConstId {
|
||||
self.insert(ConstantValue::Int(value))
|
||||
}
|
||||
|
||||
pub fn add_float(&mut self, value: f64) -> ConstId {
|
||||
self.insert(ConstantValue::Float(value))
|
||||
}
|
||||
|
||||
pub fn add_string(&mut self, value: String) -> ConstId {
|
||||
self.insert(ConstantValue::String(value))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use super::ids::FunctionId;
|
||||
use super::block::Block;
|
||||
use super::types::Type;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct Param {
|
||||
pub name: String,
|
||||
pub ty: Type,
|
||||
}
|
||||
|
||||
/// A function within a module, composed of basic blocks forming a CFG.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct Function {
|
||||
pub id: FunctionId,
|
||||
pub name: String,
|
||||
pub params: Vec<Param>,
|
||||
pub return_type: Type,
|
||||
pub blocks: Vec<Block>,
|
||||
}
|
||||
|
||||
@@ -10,4 +10,31 @@ pub enum Instr {
|
||||
Call(FunctionId, u32),
|
||||
/// Host calls (syscalls).
|
||||
Syscall(u32),
|
||||
/// Variable access.
|
||||
GetLocal(u32),
|
||||
SetLocal(u32),
|
||||
/// Stack operations.
|
||||
Pop,
|
||||
Dup,
|
||||
/// Arithmetic.
|
||||
Add,
|
||||
Sub,
|
||||
Mul,
|
||||
Div,
|
||||
Neg,
|
||||
/// Logical/Comparison.
|
||||
Eq,
|
||||
Neq,
|
||||
Lt,
|
||||
Lte,
|
||||
Gt,
|
||||
Gte,
|
||||
And,
|
||||
Or,
|
||||
Not,
|
||||
/// HIP operations.
|
||||
Alloc,
|
||||
Free, // Not used in v0 but good to have in Core IR
|
||||
ReadGate,
|
||||
WriteGate,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod ids;
|
||||
pub mod const_pool;
|
||||
pub mod types;
|
||||
pub mod program;
|
||||
pub mod module;
|
||||
pub mod function;
|
||||
@@ -9,6 +10,7 @@ pub mod terminator;
|
||||
|
||||
pub use ids::*;
|
||||
pub use const_pool::*;
|
||||
pub use types::*;
|
||||
pub use program::*;
|
||||
pub use module::*;
|
||||
pub use function::*;
|
||||
|
||||
@@ -7,4 +7,10 @@ pub enum Terminator {
|
||||
Return,
|
||||
/// Unconditional jump to another block (by index/ID).
|
||||
Jump(u32),
|
||||
/// Conditional jump: pops a bool, if false jumps to target, else continues to next block?
|
||||
/// Actually, in a CFG, we usually have two targets for a conditional jump.
|
||||
JumpIfFalse {
|
||||
target: u32,
|
||||
else_target: u32,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum Type {
|
||||
Void,
|
||||
Int,
|
||||
Float,
|
||||
Bool,
|
||||
String,
|
||||
Optional(Box<Type>),
|
||||
Result(Box<Type>, Box<Type>),
|
||||
Struct(String),
|
||||
Service(String),
|
||||
Contract(String),
|
||||
ErrorType(String),
|
||||
Function {
|
||||
params: Vec<Type>,
|
||||
return_type: Box<Type>,
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user