forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcir.rs
More file actions
220 lines (197 loc) · 6.24 KB
/
Copy pathcir.rs
File metadata and controls
220 lines (197 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! Concurrency Intermediate Representation (CIR) for WasmFX backend.
//!
//! CIR is a structured CFG extracted from MIR that makes suspension boundaries
//! explicit. Only functions containing at least one suspension point get a CIR
//! representation; non-suspending functions stay on the existing `mir_wasm.rs`
//! path.
//!
//! All types are gated behind `#[cfg(feature = "wasmfx-backend")]`.
use std::collections::HashMap;
// ---------------------------------------------------------------------------
// IDs
// ---------------------------------------------------------------------------
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct VarId(pub u32);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct BlockId(pub u32);
// ---------------------------------------------------------------------------
// CIR Function
// ---------------------------------------------------------------------------
#[derive(Debug, Clone)]
pub struct CirFunction {
pub name: String,
pub locals: Vec<CirLocal>,
pub blocks: Vec<CirBlock>,
pub entry_block: BlockId,
}
#[derive(Debug, Clone)]
pub struct CirLocal {
pub id: VarId,
}
// ---------------------------------------------------------------------------
// CIR Block
// ---------------------------------------------------------------------------
#[derive(Debug, Clone)]
pub struct CirBlock {
pub id: BlockId,
pub stmts: Vec<CirStmt>,
pub terminator: CirTerminator,
}
// ---------------------------------------------------------------------------
// Statements
// ---------------------------------------------------------------------------
#[derive(Debug, Clone)]
pub enum CirStmt {
/// Pure computation (no side effects): dst = src
Assign { dst: VarId, src: CirExpr },
/// Fire-and-forget effect (non-suspending): ActorSend, IO.print, log
Emit {
effect: EffectKind,
args: Vec<CirExpr>,
},
/// Allocate continuation frame for upcoming SuspendAndYield.
/// Emitted at the end of the block before SuspendAndYield.
SaveFrame {
vars: Vec<VarId>,
offsets: Vec<usize>,
frame_ptr: VarId,
},
/// Restore variables from frame on resume path.
/// Emitted at the top of the resume block.
RestoreFrame {
vars: Vec<VarId>,
offsets: Vec<usize>,
frame_ptr: VarId,
},
}
// ---------------------------------------------------------------------------
// Expressions
// ---------------------------------------------------------------------------
#[derive(Debug, Clone)]
pub enum CirExpr {
Var(VarId),
ConstI64(i64),
ConstF64(f64),
ConstBool(bool),
ConstNil,
ConstUnit,
/// String constant; interned into the data segment at codegen time.
/// The Wasm value is `TAG_STRING | offset`.
ConstString(String),
BinaryOp {
op: BinaryOp,
lhs: Box<CirExpr>,
rhs: Box<CirExpr>,
},
UnaryOp {
op: UnaryOp,
operand: Box<CirExpr>,
},
Call {
func_idx: u32,
args: Vec<CirExpr>,
},
ArrayLen {
arr: Box<CirExpr>,
},
ArrayLoad {
arr: Box<CirExpr>,
idx: Box<CirExpr>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
Add,
Sub,
Mul,
Div,
Mod,
Eq,
Neq,
Lt,
Lte,
Gt,
Gte,
And,
Or,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
Neg,
Not,
}
// ---------------------------------------------------------------------------
// Terminators
// ---------------------------------------------------------------------------
#[derive(Debug, Clone)]
pub enum CirTerminator {
Return(Option<CirExpr>),
Jump(BlockId),
Branch {
cond: CirExpr,
then_block: BlockId,
else_block: BlockId,
},
/// Yield to host via WasmFX suspend. Resumes at resume_block with
/// resume_var holding the host-provided result (tagged i64).
SuspendAndYield {
effect: EffectKind,
args: Vec<CirExpr>,
resume_block: BlockId,
resume_var: VarId,
live_vars: Vec<VarId>,
},
/// User-defined effect handler resume (Terminator::Resume in MIR).
Resume(CirExpr),
}
// ---------------------------------------------------------------------------
// Effect kinds
// ---------------------------------------------------------------------------
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EffectKind {
/// Non-blocking send to actor mailbox: RValue::Send
ActorSend,
/// Blocking dequeue: RValue::ReceiveWait, ReceiveMatch
MailboxDequeue,
/// RValue::SignalWait
SignalWait,
/// RValue::Perform with effect "LLM.ask"
LlmAsk,
/// Stmt::PerformAsync — async effect emission
PerformAsync,
/// Built-in or user-defined effect dispatched to host
HostEffect { module: String, name: String },
}
// ---------------------------------------------------------------------------
// Frame layout (shared between cir_frame and cir_analysis)
// ---------------------------------------------------------------------------
/// Layout of a continuation frame in Wasm linear memory.
///
/// ```text
/// Offset 0..4: state_id (u32) — BlockId of the resume block
/// Offset 4..8: frame_size (u32) — total frame size in bytes
/// Offset 8..12: parent_ptr (u32) — pointer to parent frame (0 if none)
/// Offset 12..16: [padding to 8-byte boundary]
/// Offset 16..: live_vars[0] (i64), live_vars[1] (i64), ...
/// ```
pub struct FrameLayout {
pub total_size: usize,
/// Offset for each live variable in the frame.
pub var_offsets: HashMap<VarId, usize>,
}
/// Fixed header size before live variables (16 bytes with alignment padding).
pub const FRAME_HEADER_SIZE: usize = 16;
/// Compute frame layout for a set of live variables.
///
/// All values are i64 (8 bytes, 8-byte aligned). The header is 16 bytes
/// (12-byte real header + 4 bytes padding to align variables at 8 bytes).
pub fn compute_frame_layout(live_vars: &[VarId]) -> FrameLayout {
let mut var_offsets = HashMap::new();
for (i, &var) in live_vars.iter().enumerate() {
var_offsets.insert(var, FRAME_HEADER_SIZE + i * 8);
}
FrameLayout {
total_size: FRAME_HEADER_SIZE + live_vars.len() * 8,
var_offsets,
}
}