From b06ef50acd24c6271ed1fe32f1081aa35b5a486c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 4 Oct 2020 21:26:06 +0200 Subject: [PATCH] surround routines by far jump across --- crsn/src/asm/instr/flatten.rs | 8 ++++++-- crsn/src/runtime/program.rs | 2 +- examples/proc_skip.csn | 11 +++++++++++ examples/stacks.csn | 1 - 4 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 examples/proc_skip.csn diff --git a/crsn/src/asm/instr/flatten.rs b/crsn/src/asm/instr/flatten.rs index 7ef054b..4e6cbed 100644 --- a/crsn/src/asm/instr/flatten.rs +++ b/crsn/src/asm/instr/flatten.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::sync::atomic::AtomicU32; +use std::sync::atomic::{AtomicU32, Ordering}; use crate::asm::data::{Rd, RdData}; use crate::asm::data::literal::{Label, Value}; @@ -63,7 +63,10 @@ impl Flatten for Vec> { impl Flatten for Routine { fn flatten(self: Box, label_num: &AtomicU32) -> Result, CrsnError> { - let mut ops = vec![ + let skip_label = Label::Numbered(label_num.fetch_add(1, Ordering::Relaxed)); + + let mut ops : Vec = vec![ + BuiltinOp::FarJump(skip_label.clone()).into(), BuiltinOp::Barrier(Some(format!("Routine \"{}\" start", self.name).into())).into(), BuiltinOp::Routine(self.name.clone()).into(), ]; @@ -71,6 +74,7 @@ impl Flatten for Routine { ops.extend(self.body.flatten(label_num)?); ops.push(BuiltinOp::Barrier(Some(format!("Routine \"{}\" end", self.name).into())).into()); + ops.push(BuiltinOp::FarLabel(skip_label).into()); numbered_labels_to_skips(ops) } diff --git a/crsn/src/runtime/program.rs b/crsn/src/runtime/program.rs index af74057..d81b70f 100644 --- a/crsn/src/runtime/program.rs +++ b/crsn/src/runtime/program.rs @@ -50,7 +50,7 @@ impl Program { /// Read a program instruction at address pub fn read(&self, addr: Addr) -> &Op { if addr.0 >= self.ops.len() as u64 { - &Op::BuiltIn(BuiltinOp::Nop) + &Op::BuiltIn(BuiltinOp::Halt) } else { &self.ops[addr.0 as usize] } diff --git a/examples/proc_skip.csn b/examples/proc_skip.csn new file mode 100644 index 0000000..62b57f5 --- /dev/null +++ b/examples/proc_skip.csn @@ -0,0 +1,11 @@ +( + (ld r0 1) + + ; the procedure is surrounded by a jump and a label + (proc foo + (nop) + (ret) + ) + + (ld r0 2) +) diff --git a/examples/stacks.csn b/examples/stacks.csn index c2f5111..9c37a9b 100644 --- a/examples/stacks.csn +++ b/examples/stacks.csn @@ -11,5 +11,4 @@ (cmp r1 1 (ne? (fault))) (drop @r0) - (halt) )