Skip to content

misc: inline micro_code functions #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ oxidate example/hello-world.rst # Should generate example.o2
ignite hello-world.o2
```

## Testing

- To run all tests:

```bash
# Ensure you are in the root directory of rustscript repository
cargo test
```

- To run specific tests:

```bash
# Example, to run all tests on join micro code
cargo test test_join
# ^ ^
# first few characters of the testing function
```

## Project Deliverables

- **Syntax**: RustScript's syntax is a harmonious blend of Rust and TypeScript, offering a familiar yet unique coding experience.
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/apply_builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bytecode::{builtin, Value};

use crate::{Runtime, VmError};

#[inline]
pub fn apply_builtin(mut rt: Runtime, sym: &str, args: Vec<Value>) -> Result<Runtime> {
match sym {
builtin::READ_LINE_SYM => {
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{Runtime, VmError};
///
/// If the stack is empty.
/// If the symbol is not found in the environment chain.
#[inline]
pub fn assign(mut rt: Runtime, sym: Symbol) -> Result<Runtime> {
let val = rt
.current_thread
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/binop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{Runtime, VmError};
///
/// If the stack has fewer than two values or the operation is not supported
/// for the types of the values on the stack.
#[inline]
pub fn binop(mut rt: Runtime, op: BinOp) -> Result<Runtime> {
let rhs_val = rt
.current_thread
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use super::apply_builtin;
///
/// If the operand stack does not contain enough values to pop (arity + 1).
/// If the closure is not of type closure or the arity of the closure does not match the number of arguments.
#[inline]
pub fn call(mut rt: Runtime, arity: usize) -> Result<Runtime> {
let mut args = Vec::new();
args.reserve_exact(arity);
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/done.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{Runtime, VmError, MAIN_THREAD_ID};
/// # Errors
///
/// * If the current thread is not the main thread and there are no threads in the ready queue.
#[inline]
pub fn done(mut rt: Runtime) -> Result<Runtime> {
// If the current thread is the main thread, then we are done
if rt.current_thread.thread_id == MAIN_THREAD_ID {
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/enter_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{extend_environment, Runtime};
/// # Errors
///
/// Infallible.
#[inline]
pub fn enter_scope(mut rt: Runtime, syms: Vec<Symbol>) -> Result<Runtime> {
let current_env = rt.current_thread.env.clone();

Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/exit_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{Runtime, VmError};
/// # Errors
///
/// If the runtime stack is empty.
#[inline]
pub fn exit_scope(mut rt: Runtime) -> Result<Runtime> {
let prev_frame = rt
.current_thread
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/goto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use anyhow::Result;
/// # Errors
///
/// Infallible.
#[inline]
pub fn goto(mut rt: Runtime, pc: usize) -> Result<Runtime> {
rt.current_thread.pc = pc;
Ok(rt)
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/jof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{Runtime, VmError};
/// # Errors
///
/// If the stack is empty or the top of the stack is not a boolean.
#[inline]
pub fn jof(mut rt: Runtime, pc: usize) -> Result<Runtime> {
let cond = rt
.current_thread
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use super::yield_;
/// * If the thread with the given ID is not found in the thread state hashmap.
/// * If the operand stack is empty.
/// * If the value on the operand stack is not an integer.
#[inline]
pub fn join(mut rt: Runtime) -> Result<Runtime> {
let tid: i64 = rt
.current_thread
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/ld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{Runtime, VmError};
/// # Errors
///
/// If the symbol is not found.
#[inline]
pub fn ld(mut rt: Runtime, sym: Symbol) -> Result<Runtime> {
let val = rt
.current_thread
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/ldc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::Runtime;
/// # Errors
///
/// Infallible.
#[inline]
pub fn ldc(mut rt: Runtime, val: Value) -> Result<Runtime> {
rt.current_thread.operand_stack.push(val);
Ok(rt)
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/ldf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::Runtime;
/// # Errors
///
/// Infallible.
#[inline]
pub fn ldf(mut rt: Runtime, addr: usize, prms: Vec<Symbol>) -> Result<Runtime> {
let closure = Value::Closure {
fn_type: FnType::User,
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/pop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{Runtime, VmError};
/// # Errors
///
/// If the stack is empty.
#[inline]
pub fn pop(mut rt: Runtime) -> Result<Runtime> {
rt.current_thread
.operand_stack
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{Runtime, VmError};
///
/// If the stack is empty.
/// If the top value on stack is not a semaphore.
#[inline]
pub fn post(mut rt: Runtime) -> Result<Runtime> {
let sem: Semaphore = rt
.current_thread
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use bytecode::FrameType;
/// # Errors
///
/// If the runtime stack underflows. i.e. there are no frames of the given type.
#[inline]
pub fn reset(mut rt: Runtime, ft: FrameType) -> Result<Runtime> {
loop {
let frame = rt
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/sem_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::Runtime;
/// # Errors
///
/// Infallible.
#[inline]
pub fn sem_create(mut rt: Runtime) -> Result<Runtime> {
rt.current_thread
.operand_stack
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::Runtime;
/// # Errors
///
/// Infallible.
#[inline]
pub fn spawn(mut rt: Runtime, addr: usize) -> Result<Runtime> {
rt.thread_count += 1;

Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/unop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use bytecode::{type_of, UnOp, Value};
///
/// If the stack is empty or the operation is not supported for
/// the type of the value on the stack.
#[inline]
pub fn unop(mut rt: Runtime, op: UnOp) -> Result<Runtime> {
let val = rt
.current_thread
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{Runtime, VmError};
/// If the stack is empty.
/// If the top value on stack is not a semaphore.
/// If there are no threads in the ready queue when the current thread is blocked.
#[inline]
pub fn wait(mut rt: Runtime) -> Result<Runtime> {
let sem: Semaphore = rt
.current_thread
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/micro_code/yield_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{Runtime, VmError};
/// # Errors
///
/// Returns an error if there are no threads in the ready queue.
#[inline]
pub fn yield_(mut rt: Runtime) -> Result<Runtime> {
let current_thread = rt.current_thread;
rt.ready_queue.push_back(current_thread);
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/runtime/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl Runtime {
/// - Go through the runtime stack and mark all the environments and environment of closure values in
/// their respective environment, and the chain of parent environments
/// - Go through the operand stack and mark all the environments of closure values, and the chain of parent environments
#[inline]
pub fn mark_and_weep(self) -> Self {
let marked = mark(&self);
sweep(self, marked)
Expand Down
7 changes: 7 additions & 0 deletions vm/ignite/src/runtime/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl Runtime {
/// # Errors
///
/// If the program counter is out of bounds.
#[inline]
pub fn fetch_instr(&mut self) -> Result<ByteCode> {
let instr = self
.instrs
Expand All @@ -28,21 +29,25 @@ impl Runtime {
}
/// Check if the time quantum has expired.
/// The time quantum is the maximum amount of time a thread can run before it is preempted.
#[inline]
pub fn time_quantum_expired(&self) -> bool {
self.time.elapsed() >= self.time_quantum
}

#[inline]
pub fn should_garbage_collect(&self) -> bool {
self.gc_timer.elapsed() >= self.gc_interval
}

#[inline]
pub fn garbage_collect(mut self) -> Self {
self = self.mark_and_weep();
self.gc_timer = Instant::now();
self
}

/// The program is done if the current thread is the main thread and the current thread is done.
#[inline]
pub fn is_done(&self) -> bool {
self.done
}
Expand Down Expand Up @@ -75,6 +80,7 @@ impl Runtime {
/// # Errors
///
/// If an error occurs during execution.
#[inline]
pub fn run(mut rt: Runtime) -> Result<Runtime> {
loop {
if rt.is_done() {
Expand Down Expand Up @@ -117,6 +123,7 @@ pub fn run(mut rt: Runtime) -> Result<Runtime> {
/// # Errors
///
/// If an error occurs during execution.
#[inline]
pub fn execute(rt: Runtime, instr: ByteCode) -> Result<Runtime> {
match instr {
ByteCode::DONE => micro_code::done(rt),
Expand Down
1 change: 1 addition & 0 deletions vm/ignite/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl Thread {
}
}

#[inline]
pub fn extend_environment<S, V>(
mut rt: Runtime,
env: Weak<RefCell<Environment>>,
Expand Down