Commit 14b3ee5f authored by Kunshan Wang's avatar Kunshan Wang

[API-GEN]: Tests, CStr, comments.

Added the "localtest" directory for testing the API without touching
other modules. (TODO: It could be moved somewhere else, but should not
use src as an extern crate, or it will depend on the entire crate.)

Use CStr for incoming strings from C. CString is for C-style strings
**owned by Rust**.

Added comments at the beginning of api_c.rs and api_bridge.rs to
indicate some parts of the files are generated.
parent 20b2980c
/*!
* Stubs for api_impl.rs
*
* The muapi2rustapi.py script will always generate these stubs. Copy them into api_impl.rs as the
* template to the implementation.
* Parts of this file (between GEN:BEGIN:* and GEN:END:*) are automatically generated. Do not edit
* those parts manually because they will be overwritten. Instead, edit the muapi2rustapi.py script
* to generate the desired code.
*
* To implement these functions, copy the generated stubs to api_impl.rs and add the function body.
*/
#![allow(non_camel_case_types)]
#![allow(dead_code)]
use std::os::raw::*;
use api_c::*;
......@@ -690,7 +690,7 @@ impl MuIRBuilder {
panic!("Not implemented")
}
pub fn new_atomicrmw(&mut self, id: MuID, result_id: MuID, is_ptr: bool, ord: CMuMemOrd, optr: CMuAtomicRMWOptr, refTy: MuID, loc: MuID, opnd: MuID, exc_clause: Option<MuID>) {
pub fn new_atomicrmw(&mut self, id: MuID, result_id: MuID, is_ptr: bool, ord: CMuMemOrd, optr: CMuAtomicRMWOptr, ref_ty: MuID, loc: MuID, opnd: MuID, exc_clause: Option<MuID>) {
panic!("Not implemented")
}
......
#![allow(non_snake_case)] // It's generated code.
#![allow(dead_code)] // Seems Rust do not consider "taking the function pointer" as "use"
/**
* This file contains the bridge between the C interface and the Rust implementation.
*
* This file contains the the functions in the C-API's `MuVM`, `MuCtx` and `MuIRBuilder` structs.
* These functions will convert the low-level C-style argument types to the high-level Rust-level
* argument types, and attempt to call the methods of the same name on the corresponding Rust
* structs in api_impl.
*
* NOTE: Parts of this file (between GEN:BEGIN:* and GEN:END:*) are automatically generated. Do not
* edit those parts manually because they will be overwritten. Instead, edit the muapi2rustapi.py
* script to generate the desired code.
*/
use std::ptr;
use std::os::raw::*;
use std::ffi::CString;
use std::ffi::CStr;
use std::slice;
use super::api_c::*;
......@@ -54,11 +69,11 @@ fn from_MuName(cname: CMuName) -> MuName {
#[inline(always)]
fn from_MuCString(cstring: CMuCString) -> String {
debug_assert!(!cstring.is_null());
let ffi_cstring = unsafe {
CString::from_raw(cstring)
let ffi_cstr = unsafe {
CStr::from_ptr(cstring)
};
ffi_cstring.into_string().unwrap()
ffi_cstr.to_string_lossy().into_owned()
}
#[inline(always)]
......@@ -1891,19 +1906,19 @@ extern fn _forwarder__MuIRBuilder__new_cmpxchg(b: *mut CMuIRBuilder, id: CMuID,
};
}
extern fn _forwarder__MuIRBuilder__new_atomicrmw(b: *mut CMuIRBuilder, id: CMuID, result_id: CMuID, is_ptr: CMuBool, ord: CMuMemOrd, optr: CMuAtomicRMWOptr, refTy: CMuTypeNode, loc: CMuVarNode, opnd: CMuVarNode, exc_clause: CMuExcClause) {
extern fn _forwarder__MuIRBuilder__new_atomicrmw(b: *mut CMuIRBuilder, id: CMuID, result_id: CMuID, is_ptr: CMuBool, ord: CMuMemOrd, optr: CMuAtomicRMWOptr, ref_ty: CMuTypeNode, loc: CMuVarNode, opnd: CMuVarNode, exc_clause: CMuExcClause) {
let mut _arg_b = from_MuIRBuilder_ptr(b);
let mut _arg_id = from_MuID(id);
let mut _arg_result_id = from_MuID(result_id);
let mut _arg_is_ptr = from_MuBool(is_ptr);
let mut _arg_ord = ord;
let mut _arg_optr = optr;
let mut _arg_refTy = from_MuID(refTy);
let mut _arg_ref_ty = from_MuID(ref_ty);
let mut _arg_loc = from_MuID(loc);
let mut _arg_opnd = from_MuID(opnd);
let mut _arg_exc_clause = from_MuID_optional(exc_clause);
unsafe {
(*_arg_b).new_atomicrmw(_arg_id, _arg_result_id, _arg_is_ptr, _arg_ord, _arg_optr, _arg_refTy, _arg_loc, _arg_opnd, _arg_exc_clause)
(*_arg_b).new_atomicrmw(_arg_id, _arg_result_id, _arg_is_ptr, _arg_ord, _arg_optr, _arg_ref_ty, _arg_loc, _arg_opnd, _arg_exc_clause)
};
}
......
/*
#![allow(non_camel_case_types)] // Generated code
#![allow(dead_code)] // This is a public module that mirrors muapi.h
/**
* This file contains the C-facing interfaces.
*
* It is basically the muapi.h header written in Rust. It does not contain any
* implementation-specific code. Most codes are simply generated from muapi.h.
*
* NOTE: Parts of this file (between GEN:BEGIN:* and GEN:END:*) are automatically generated. Do not
* edit those parts manually because they will be overwritten. Instead, edit the muapi2rustapi.py
* script to generate the desired code.
*/
// This file is for interfacing with C, so it is not idiomatic Rust code.
#![allow(non_camel_case_types)]
#![allow(dead_code)]
use std::os::raw::*;
......
RUSTC=rustc
MUAPI_H_DIR=..
INCLUDE_DIR=$(MUAPI_H_DIR)
rustpart.a: *.rs
$(RUSTC) --crate-type=staticlib -o rustpart.a mock.rs
cpart.o: cpart.c cpart.h
$(CC) -c -I $(INCLUDE_DIR) -o $@ $<
cclient: cclient.c cpart.h rustpart.a cpart.o ../muapi.h
$(CC) -I $(INCLUDE_DIR) -o $@ cclient.c cpart.o rustpart.a
The purpose of this directory is to test the generated stubs in the
`src/vm/api` module without touching other modules.
Invoke `make` to build the test program.
../api_bridge.rs
\ No newline at end of file
../api_c.rs
\ No newline at end of file
This diff is collapsed.
#include <stdio.h>
#include <stdint.h>
#include <muapi.h>
#include "cpart.h"
int main() {
printf("Hello world!\n");
MuVM *mvm = new_mock_micro_vm("cclientt");
free_mock_micro_vm(mvm);
return 0;
}
#include "cpart.h"
// Currently nothing on the micro VM side need to be implemented in C.
#pragma once
#include <muapi.h>
MuVM* new_mock_micro_vm(char* name);
void free_mock_micro_vm(MuVM* mvm);
mod api_c;
mod api_bridge;
pub mod api_impl;
mod deps {
// should import from ast/src/ir.rs
pub type WPID = usize;
pub type MuID = usize;
pub type MuName = String;
pub type CName = MuName;
pub struct APIMuValue {
// stub
}
}
......@@ -693,7 +693,7 @@ struct MuIRBuilder {
MuExcClause exc_clause); /// MUAPIPARSER exc_clause:optional
void (*new_atomicrmw)(MuIRBuilder *b, MuID id, MuID result_id,
MuBool is_ptr, MuMemOrd ord, MuAtomicRMWOptr optr,
MuTypeNode refTy, MuVarNode loc, MuVarNode opnd,
MuTypeNode ref_ty, MuVarNode loc, MuVarNode opnd,
MuExcClause exc_clause); /// MUAPIPARSER exc_clause:optional
void (*new_fence )(MuIRBuilder *b, MuID id, MuMemOrd ord);
......
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