Skip to main content

synth_core/
ir.rs

1//! Intermediate Representation for synthesis
2
3use serde::{Deserialize, Serialize};
4
5/// Synthesis Intermediate Representation
6///
7/// This is a simplified IR for the PoC. In production, this would be much more
8/// sophisticated, potentially using e-graphs for optimization.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct SynthIR {
11    /// Functions in IR form
12    pub functions: Vec<IRFunction>,
13
14    /// Global data
15    pub globals: Vec<IRGlobal>,
16
17    /// Memory regions
18    pub memories: Vec<IRMemory>,
19}
20
21/// IR Function
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct IRFunction {
24    /// Function name
25    pub name: String,
26
27    /// Parameters
28    pub params: Vec<IRValue>,
29
30    /// Results
31    pub results: Vec<IRValue>,
32
33    /// Basic blocks
34    pub blocks: Vec<IRBlock>,
35}
36
37/// IR Basic Block
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct IRBlock {
40    /// Block label
41    pub label: String,
42
43    /// Instructions in this block
44    pub instructions: Vec<IRInstruction>,
45
46    /// Terminator instruction
47    pub terminator: IRTerminator,
48}
49
50/// IR Instruction
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub enum IRInstruction {
53    /// Binary operation
54    BinOp {
55        op: BinOp,
56        dest: IRValue,
57        left: IRValue,
58        right: IRValue,
59    },
60
61    /// Unary operation
62    UnOp {
63        op: UnOp,
64        dest: IRValue,
65        operand: IRValue,
66    },
67
68    /// Load from memory
69    Load {
70        dest: IRValue,
71        address: IRValue,
72        offset: i32,
73    },
74
75    /// Store to memory
76    Store {
77        address: IRValue,
78        value: IRValue,
79        offset: i32,
80    },
81
82    /// Call function
83    Call {
84        function: String,
85        args: Vec<IRValue>,
86        dest: Option<IRValue>,
87    },
88}
89
90/// IR Terminator (ends a basic block)
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub enum IRTerminator {
93    /// Return from function
94    Return(Option<IRValue>),
95
96    /// Branch to block
97    Branch(String),
98
99    /// Conditional branch
100    BranchIf {
101        condition: IRValue,
102        true_block: String,
103        false_block: String,
104    },
105
106    /// Unreachable code
107    Unreachable,
108}
109
110/// Binary operations
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
112pub enum BinOp {
113    // Integer arithmetic
114    IAdd,
115    ISub,
116    IMul,
117    IDivS,
118    IDivU,
119    IRemS,
120    IRemU,
121
122    // Integer bitwise
123    IAnd,
124    IOr,
125    IXor,
126    IShl,
127    IShrS,
128    IShrU,
129    IRotl,
130    IRotr,
131
132    // Integer comparison
133    IEq,
134    INe,
135    ILtS,
136    ILtU,
137    ILeS,
138    ILeU,
139    IGtS,
140    IGtU,
141    IGeS,
142    IGeU,
143
144    // Float arithmetic
145    FAdd,
146    FSub,
147    FMul,
148    FDiv,
149    FMin,
150    FMax,
151
152    // Float comparison
153    FEq,
154    FNe,
155    FLt,
156    FLe,
157    FGt,
158    FGe,
159}
160
161/// Unary operations
162#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
163pub enum UnOp {
164    // Integer
165    IClz,
166    ICtz,
167    IPopcnt,
168    IEqz,
169
170    // Float
171    FAbs,
172    FNeg,
173    FSqrt,
174    FCeil,
175    FFloor,
176    FTrunc,
177    FNearest,
178
179    // Conversions
180    I32WrapI64,
181    I64ExtendI32S,
182    I64ExtendI32U,
183    F32DemoteF64,
184    F64PromoteF32,
185}
186
187/// IR Value
188#[derive(Debug, Clone, Serialize, Deserialize)]
189pub enum IRValue {
190    /// Local variable
191    Local(u32),
192
193    /// Constant integer
194    ConstI32(i32),
195    ConstI64(i64),
196
197    /// Constant float
198    ConstF32(f32),
199    ConstF64(f64),
200
201    /// Global variable
202    Global(u32),
203}
204
205/// IR Global
206#[derive(Debug, Clone, Serialize, Deserialize)]
207pub struct IRGlobal {
208    /// Global index
209    pub index: u32,
210
211    /// Initial value
212    pub init: IRValue,
213
214    /// Is mutable
215    pub mutable: bool,
216}
217
218/// IR Memory
219#[derive(Debug, Clone, Serialize, Deserialize)]
220pub struct IRMemory {
221    /// Memory index
222    pub index: u32,
223
224    /// Initial size in pages
225    pub initial: u32,
226
227    /// Maximum size in pages (if limited)
228    pub maximum: Option<u32>,
229}
230
231impl SynthIR {
232    /// Create empty IR
233    pub fn new() -> Self {
234        Self {
235            functions: Vec::new(),
236            globals: Vec::new(),
237            memories: Vec::new(),
238        }
239    }
240}
241
242impl Default for SynthIR {
243    fn default() -> Self {
244        Self::new()
245    }
246}