Commit 9b28c4c3 authored by qinsoon's avatar qinsoon
Browse files

[wip] unfinished work on API. Kunshan is gonna work on it

parent 815f10b1
use ast::ir::*; use ir::*;
use ast::ptr::*; use ptr::*;
use ast::types::*; use types::*;
use std::collections::HashMap; use std::collections::HashMap;
...@@ -37,13 +37,13 @@ impl MuBundle { ...@@ -37,13 +37,13 @@ impl MuBundle {
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct MuValue { pub struct APIMuValue {
pub id: MuID, pub id: MuID,
pub v: MuValueKind pub v: APIMuValueKind
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum MuValueKind { pub enum APIMuValueKind {
Int, Int,
Float, Float,
Double, Double,
...@@ -88,9 +88,9 @@ pub enum MuValueKind { ...@@ -88,9 +88,9 @@ pub enum MuValueKind {
macro_rules! handle_constructor { macro_rules! handle_constructor {
($fn_name: ident, $kind: ident) => { ($fn_name: ident, $kind: ident) => {
pub fn $fn_name(id: MuID) -> MuValue { pub fn $fn_name(id: MuID) -> APIMuValue {
MuValue{ APIMuValue{
id: id, v: MuValueKind::$kind id: id, v: APIMuValueKind::$kind
} }
} }
} }
......
...@@ -38,7 +38,7 @@ macro_rules! select_value { ...@@ -38,7 +38,7 @@ macro_rules! select_value {
#[macro_use] #[macro_use]
pub mod ir; pub mod ir;
pub mod bundle;
pub mod inst; pub mod inst;
pub mod types; pub mod types;
pub mod ir_semantics; pub mod ir_semantics;
......
use ast::ptr::*;
use ast::ir::*;
use ast::types::*;
use vm::api;
use vm::VM; use vm::VM;
use vm::bundle::*; use ast::bundle::*;
use std::mem; use std::mem;
use std::os::raw;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use std::ffi::CStr;
use std::os::raw::c_int;
use std::os::raw::c_char;
use ast::ir::MuID;
pub type APIMuName = *const c_char;
macro_rules! unimplemented_api { macro_rules! unimplemented_api {
() => { () => {
// cast fn to usize before transmute, see: https://github.com/rust-lang/rust/issues/19925 // cast fn to usize before transmute, see: https://github.com/rust-lang/rust/issues/19925
...@@ -19,7 +20,7 @@ macro_rules! unimplemented_api { ...@@ -19,7 +20,7 @@ macro_rules! unimplemented_api {
macro_rules! api { macro_rules! api {
($func: expr) => { ($func: expr) => {
unsafe {mem::transmute($func as usize)} mem::transmute($func as usize)
} }
} }
...@@ -34,46 +35,47 @@ pub struct MuVM { ...@@ -34,46 +35,47 @@ pub struct MuVM {
pub new_context: fn (mvm: *mut MuVM) -> *mut MuCtx, pub new_context: fn (mvm: *mut MuVM) -> *mut MuCtx,
id_of: fn (mvm: *const MuVM, name: MuName) -> MuID, id_of: fn (mvm: *const MuVM, name: APIMuName) -> MuID,
name_of : fn (mvm: *const MuVM, id: MuID) -> MuName, name_of : fn (mvm: *const MuVM, id: MuID) -> APIMuName,
// set_trap_handler: fn(mvm: *mut MuVM, trap_handler: MuTrapHandler, user_data: MuCPtr) // unimplemented api
set_trap_handler: fn () -> ()
} }
impl MuVM { impl MuVM {
pub fn new() -> *mut MuVM { pub fn new() -> *mut MuVM {
let vm = Box::new(MuVM { let vm = Box::new(MuVM {
internal: Arc::new(VM::new()), internal: Arc::new(VM::new()),
new_context: api!(MuVM::new_context), new_context: unsafe{api!(MuVM::new_context)},
id_of: api!(MuVM::id_of), id_of: unsafe{api!(MuVM::id_of)},
name_of: api!(MuVM::name_of) name_of: unsafe{api!(MuVM::name_of)},
set_trap_handler: unimplemented_api!()
}); });
Box::into_raw(vm) Box::into_raw(vm)
} }
pub fn new_context(&mut self) -> *mut MuCtx { pub fn new_context(*mut self) -> *mut MuCtx {
let ctx = Box::new(MuCtx::new(self.internal.clone())); let a : &mut self = self.as_mut().unwarp()
unimplemented!()
Box::into_raw(ctx)
} }
pub fn id_of(&self, name: &str) -> MuID { pub fn id_of(&self, name: APIMuName) -> MuID {
let name = unsafe {CStr::from_ptr(name)}.to_str().unwrap();
self.internal.id_of(name) self.internal.id_of(name)
} }
pub fn name_of(&self, id: MuID) -> MuName { pub fn name_of(&self, id: MuID) -> APIMuName {
self.internal.name_of(id) self.internal.name_of(id).as_ptr() as *const c_char
} }
} }
#[repr(C)] #[repr(C)]
pub struct MuCtx { pub struct MuCtx {
// void* header - current not planed to use this // void* header
internal: Box<MuCtxInternal>, internal: Arc<VM>,
}
// GENERATE_BEGIN: MuCtx
struct MuCtxInternal {
vm: Arc<VM>, // GENERATE_END: MuCtx
cur_bundles: HashMap<MuID, MuBundle>
} }
\ No newline at end of file
...@@ -3,10 +3,8 @@ ...@@ -3,10 +3,8 @@
use ast::ptr::*; use ast::ptr::*;
use ast::ir::*; use ast::ir::*;
use ast::types::*; use ast::types::*;
#[macro_use] use ast::bundle::*;
use vm::api;
use vm::VM; use vm::VM;
use vm::bundle::*;
use std::mem; use std::mem;
use std::os::raw; use std::os::raw;
...@@ -75,23 +73,23 @@ impl MuVM { ...@@ -75,23 +73,23 @@ impl MuVM {
pub type MuArraySize = usize; pub type MuArraySize = usize;
pub type MuIntValue = MuValue; pub type MuIntValue = APIMuValue;
pub type MuBundleNode = MuValue; pub type MuBundleNode = APIMuValue;
pub type MuChildNode = MuValue; pub type MuChildNode = APIMuValue;
pub type MuTypeNode = MuValue; pub type MuTypeNode = APIMuValue;
pub type MuFuncSigNode= MuValue; pub type MuFuncSigNode= APIMuValue;
pub type MuConstNode = MuValue; pub type MuConstNode = APIMuValue;
pub type MuGlobalNode = MuValue; pub type MuGlobalNode = APIMuValue;
pub type MuFuncNode = MuValue; pub type MuFuncNode = APIMuValue;
pub type MuFuncVerNode= MuValue; pub type MuFuncVerNode= APIMuValue;
pub type MuBBNode = MuValue; pub type MuBBNode = APIMuValue;
pub type MuNorParamNode = MuValue; pub type MuNorParamNode = APIMuValue;
pub type MuExcParamNode = MuValue; pub type MuExcParamNode = APIMuValue;
pub type MuInstNode = MuValue; pub type MuInstNode = APIMuValue;
pub type MuInstResNode = MuValue; pub type MuInstResNode = APIMuValue;
pub type MuLocalVarNode = MuValue; pub type MuLocalVarNode = APIMuValue;
pub type MuVarNode = MuValue; pub type MuVarNode = APIMuValue;
pub type MuFlag = usize; pub type MuFlag = usize;
pub type MuDestKind = MuFlag; pub type MuDestKind = MuFlag;
......
...@@ -2,7 +2,6 @@ mod vm; ...@@ -2,7 +2,6 @@ mod vm;
mod vm_options; mod vm_options;
mod machine_code; mod machine_code;
pub mod api; pub mod api;
pub mod bundle;
pub use vm::vm::VM; pub use vm::vm::VM;
pub use vm::vm_options::VMOptions; pub use vm::vm_options::VMOptions;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment