Skip to content

Commit 7e8fb2d

Browse files
committed
feat(interpreter): add and lower trap instructions
1 parent aa73e27 commit 7e8fb2d

3 files changed

Lines changed: 52 additions & 5 deletions

File tree

interpreter/src/ir.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,14 @@ pub enum Instruction {
6868
UserCall { dest: Reg, start: Reg, end: Reg, name: NonLocal },
6969
IndirectCall { dest: Reg, start: Reg, end: Reg, name: Arg, ty: ArgTy },
7070
Jump { to: Label },
71-
Return { arg: Arg, ty: ArgTy },
7271
Branch { then_label: Label, else_label: Label, condition: Reg },
72+
73+
// Traps
74+
Exit { arg: Arg, ty: ArgTy },
75+
Return { arg: Arg, ty: ArgTy },
76+
ReturnUnassigned,
77+
Next,
78+
NextFile,
7379
}
7480

7581
const _: () = const { assert!(size_of::<Instruction>() <= size_of::<u128>()) };
@@ -187,7 +193,7 @@ impl Display for Instruction {
187193
Self::Jump { to } => {
188194
write!(f, "{op} {to}")
189195
}
190-
Self::Return { arg, ty } => {
196+
Self::Return { arg, ty } | Self::Exit { arg, ty } => {
191197
write!(f, "{op}")?;
192198
fmt_arg(f, arg, ty, " ")
193199
}
@@ -208,6 +214,9 @@ impl Display for Instruction {
208214
Self::UserCall { dest, start, end, name } => {
209215
write!(f, "{dest} <- {op} {name}, {start}..{end}")
210216
}
217+
Self::Next | Self::NextFile | Self::ReturnUnassigned => {
218+
write!(f, "{op}")
219+
}
211220
}
212221
}
213222
}
@@ -244,8 +253,11 @@ impl Instruction {
244253
Self::IndirectCall { .. } => "vcall",
245254
Self::OutputCall { .. } => "out",
246255
Self::Jump { .. } => "jmp",
247-
Self::Return { .. } => "ret",
256+
Self::Return { .. } | Self::ReturnUnassigned => "ret",
248257
Self::Branch { .. } => "brif",
258+
Self::Exit { .. } => "exit",
259+
Self::Next => "next",
260+
Self::NextFile => "nextf",
249261
}
250262
}
251263
}

interpreter/src/ir/lower.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,40 @@ impl<'a> CodeGen<'a> {
185185
self.bc
186186
.emit(Instruction::OutputCall { start, end, cmd: *name, redir });
187187
}
188+
Statement::Simple(SimpleStatement::Delete(..)) => todo!(),
188189
Statement::Switch { scrutinee, branches, default } => {
189190
self.lower_switch(scrutinee, branches, default.as_ref());
190191
}
191-
_ => todo!(),
192+
Statement::ForEach { .. } => todo!(),
193+
Statement::Break => todo!(),
194+
Statement::Continue => todo!(),
195+
Statement::Exit(Some(expr)) => {
196+
let dest = self.alloc_reg();
197+
self.lower_expr_into(expr, *dest);
198+
self.bc
199+
.emit(Instruction::Exit { arg: Arg { reg: *dest }, ty: ArgTy::Reg });
200+
self.free_reg(dest);
201+
}
202+
Statement::Exit(None) => {
203+
self.bc
204+
.emit(Instruction::Exit { arg: Arg { imm: 0 }, ty: ArgTy::Imm });
205+
}
206+
Statement::Return(Some(expr)) => {
207+
let dest = self.alloc_reg();
208+
self.lower_expr_into(expr, *dest);
209+
self.bc
210+
.emit(Instruction::Return { arg: Arg { reg: *dest }, ty: ArgTy::Reg });
211+
self.free_reg(dest);
212+
}
213+
Statement::Return(None) => {
214+
self.bc.emit(Instruction::ReturnUnassigned);
215+
}
216+
Statement::Next => {
217+
self.bc.emit(Instruction::Next);
218+
}
219+
Statement::NextFile => {
220+
self.bc.emit(Instruction::NextFile);
221+
}
192222
}
193223
}
194224

interpreter/src/vm.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ impl Interpreter<'_> {
281281
self.program_counter = label as _;
282282
continue;
283283
}
284-
Instruction::Return { arg: _, ty: _ } => return Ok(Signal::Return),
285284
Instruction::Branch { then_label, else_label, condition } => {
286285
if self.registers.get(condition).to_bool() {
287286
self.program_counter = then_label.0 as _;
@@ -290,6 +289,12 @@ impl Interpreter<'_> {
290289
}
291290
continue;
292291
}
292+
// TODO resolve return/exit args.
293+
Instruction::Exit { arg: _arg, ty: _ty } => return Ok(Signal::Exit),
294+
Instruction::Return { arg: _, ty: _ } => return Ok(Signal::Return),
295+
Instruction::ReturnUnassigned => return Ok(Signal::Return),
296+
Instruction::Next => return Ok(Signal::Next),
297+
Instruction::NextFile => return Ok(Signal::NextFile),
293298
}
294299
self.program_counter += 1;
295300
}

0 commit comments

Comments
 (0)