2020-10-07 15:48:24 -07:00
|
|
|
use crate::{obj::prelude::*, vm::inst::Inst};
|
2020-10-20 16:21:50 -07:00
|
|
|
use std::fmt::{self, Debug, Formatter};
|
2020-10-07 15:48:24 -07:00
|
|
|
use std::{collections::BTreeMap, sync::Arc};
|
2020-09-14 16:32:00 -07:00
|
|
|
|
2020-10-07 15:48:24 -07:00
|
|
|
pub type FrameBindings = BTreeMap<usize, ObjRef>;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum Frame {
|
|
|
|
|
User(UserFrame),
|
|
|
|
|
Native(NativeFrame),
|
2020-09-14 16:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
2020-10-07 15:48:24 -07:00
|
|
|
impl Frame {
|
|
|
|
|
pub fn user_frame(&self) -> Option<&UserFrame> {
|
|
|
|
|
if let Frame::User(frame) = self {
|
|
|
|
|
Some(frame)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn user_frame_mut(&mut self) -> Option<&mut UserFrame> {
|
|
|
|
|
if let Frame::User(frame) = self {
|
|
|
|
|
Some(frame)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn native_frame(&self) -> Option<&NativeFrame> {
|
|
|
|
|
if let Frame::Native(frame) = self {
|
|
|
|
|
Some(frame)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
2020-09-14 16:32:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-07 15:48:24 -07:00
|
|
|
pub fn native_frame_mut(&mut self) -> Option<&mut NativeFrame> {
|
|
|
|
|
if let Frame::Native(frame) = self {
|
|
|
|
|
Some(frame)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-14 16:32:00 -07:00
|
|
|
|
2020-10-07 15:48:24 -07:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct UserFrame {
|
|
|
|
|
callee: ObjRef,
|
|
|
|
|
bindings: FrameBindings,
|
|
|
|
|
last_pc: usize,
|
|
|
|
|
code: Arc<Vec<Inst>>,
|
2020-09-14 16:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
2020-10-07 15:48:24 -07:00
|
|
|
impl UserFrame {
|
2020-10-20 16:21:50 -07:00
|
|
|
pub fn new(
|
|
|
|
|
callee: ObjRef,
|
|
|
|
|
bindings: FrameBindings,
|
|
|
|
|
last_pc: usize,
|
|
|
|
|
code: Arc<Vec<Inst>>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
callee,
|
|
|
|
|
bindings,
|
|
|
|
|
last_pc,
|
|
|
|
|
code,
|
|
|
|
|
}
|
2020-09-14 16:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
2020-10-07 15:48:24 -07:00
|
|
|
pub fn bindings(&self) -> &FrameBindings {
|
|
|
|
|
&self.bindings
|
2020-09-14 16:32:00 -07:00
|
|
|
}
|
|
|
|
|
|
2020-10-07 15:48:24 -07:00
|
|
|
pub fn bindings_mut(&mut self) -> &mut FrameBindings {
|
|
|
|
|
&mut self.bindings
|
2020-09-14 16:32:00 -07:00
|
|
|
}
|
2020-10-07 15:48:24 -07:00
|
|
|
|
|
|
|
|
pub fn last_pc(&self) -> usize {
|
|
|
|
|
self.last_pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn code(&self) -> &Arc<Vec<Inst>> {
|
|
|
|
|
&self.code
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn callee(&self) -> &ObjRef {
|
|
|
|
|
&self.callee
|
2020-09-14 16:32:00 -07:00
|
|
|
}
|
2020-09-01 17:32:48 -07:00
|
|
|
}
|
2020-09-27 19:33:18 -07:00
|
|
|
|
2020-10-07 15:48:24 -07:00
|
|
|
pub struct NativeFrame {
|
|
|
|
|
callee: ObjRef,
|
|
|
|
|
fun_ptr: NativeFunPtr,
|
|
|
|
|
args: Vec<ObjRef>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl NativeFrame {
|
|
|
|
|
pub fn new(callee: ObjRef, fun_ptr: NativeFunPtr, args: Vec<ObjRef>) -> Self {
|
2020-10-20 16:21:50 -07:00
|
|
|
Self {
|
|
|
|
|
callee,
|
|
|
|
|
fun_ptr,
|
|
|
|
|
args,
|
|
|
|
|
}
|
2020-10-07 15:48:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn fun_ptr(&self) -> &NativeFunPtr {
|
|
|
|
|
&self.fun_ptr
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-07 17:21:01 -07:00
|
|
|
pub fn args(&self) -> &Vec<ObjRef> {
|
|
|
|
|
&self.args
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-07 15:48:24 -07:00
|
|
|
pub fn callee(&self) -> &ObjRef {
|
|
|
|
|
&self.callee
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Debug for NativeFrame {
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
|
|
|
|
|
fmt.debug_struct("NativeFrame")
|
2020-10-20 16:21:50 -07:00
|
|
|
.field(
|
|
|
|
|
"fun_ptr",
|
|
|
|
|
&format!("{:#x}", &self.fun_ptr as *const _ as usize),
|
|
|
|
|
)
|
2020-10-07 15:48:24 -07:00
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|