Browse Source

general FFI improvements

master
Alison Watson 11 months ago
parent
commit
905d709e05
  1. 3
      fw/hal.rs
  2. 41
      fw/hal/sdl.rs
  3. 4
      fw/types/ffi.rs
  4. 100
      fw/vire/llvm.rs

3
fw/hal.rs

@ -2,8 +2,7 @@ pub mod ctx;
pub mod evt;
pub mod win;
#[doc(hidden)]
pub mod sdl;
mod sdl;
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]

41
fw/hal/sdl.rs

@ -4,11 +4,11 @@ use crate::{
};
use std::os::raw;
pub const WINDOW_POS_UNDEF: raw::c_int = 0x1FFF0000;
pub(super) const WINDOW_POS_UNDEF: raw::c_int = 0x1FFF0000;
bitflags::bitflags! {
#[repr(transparent)]
pub struct InitFlags: u32 {
pub(super) struct InitFlags: u32 {
const TIMER = 0x01;
const AUDIO = 0x10;
const VIDEO = 0x20;
@ -20,7 +20,7 @@ bitflags::bitflags! {
}
#[repr(transparent)]
pub struct WindowFlags: u32 {
pub(super) struct WindowFlags: u32 {
const FULLSCREEN = 0x00000001;
const OPENGL = 0x00000002;
const SHOWN = 0x00000004;
@ -49,7 +49,8 @@ bitflags::bitflags! {
#[repr(u32)]
#[derive(Clone, Copy)]
#[non_exhaustive]
pub enum EventType {
#[allow(dead_code)]
pub(super) enum EventType {
Quit = 0x100,
AppTerminating,
@ -122,59 +123,59 @@ pub enum EventType {
#[repr(C)]
#[derive(Copy, Clone)]
pub struct CommonEvent {
pub ty: EventType,
pub time_stamp: u32,
pub(super) struct CommonEvent {
pub(super) ty: EventType,
pub(super) time_stamp: u32,
}
#[repr(C)]
pub union Event {
pub ty: EventType,
pub common: CommonEvent,
pub(super) union Event {
pub(super) ty: EventType,
pub(super) common: CommonEvent,
_forced_size: [u8; 56],
}
opaque_struct!(pub Window);
opaque_struct!(pub(super) Window);
#[link(name = "SDL2")]
extern "C" {
// initialization
#[link_name = "SDL_SetMainReady"]
pub fn set_main_ready();
pub(super) fn set_main_ready();
#[link_name = "SDL_Init"]
pub fn init(flags: InitFlags) -> raw::c_int;
pub(super) fn init(flags: InitFlags) -> raw::c_int;
#[link_name = "SDL_Quit"]
pub fn quit();
pub(super) fn quit();
// errors
#[link_name = "SDL_GetError"]
pub fn get_error() -> Nts;
pub(super) fn get_error() -> Nts;
// events
#[link_name = "SDL_PollEvent"]
pub fn poll_event(event: *mut Event) -> raw::c_int;
pub(super) fn poll_event(event: *mut Event) -> raw::c_int;
// windows
#[link_name = "SDL_CreateWindow"]
pub fn create_window(
pub(super) fn create_window(
title: Nts, x: raw::c_int, y: raw::c_int, w: raw::c_int, h: raw::c_int,
flags: WindowFlags,
) -> *mut Window;
#[link_name = "SDL_DestroyWindow"]
pub fn destroy_window(window: *mut Window);
pub(super) fn destroy_window(window: *mut Window);
// vulkan
#[link_name = "SDL_Vulkan_GetInstanceExtensions"]
pub fn vulkan_get_instance_extensions(
pub(super) fn vulkan_get_instance_extensions(
window: *mut Window, p_count: *mut raw::c_uint, p_names: *mut Nts,
) -> Bool;
#[link_name = "SDL_Vulkan_CreateSurface"]
pub fn vulkan_create_surface(
pub(super) fn vulkan_create_surface(
window: *mut Window, instance: ash::vk::Instance,
surface: *mut ash::vk::SurfaceKHR,
) -> Bool;

4
fw/types/ffi.rs

@ -15,9 +15,9 @@ macro_rules! c_str {
#[macro_export]
macro_rules! opaque_struct {
(pub $s:ident) => {
($v:vis $s:ident) => {
#[repr(C)]
pub struct $s {
$v struct $s {
_private: [u8; 0],
}
};

100
fw/vire/llvm.rs

@ -1,150 +1,158 @@
#![allow(dead_code)]
use crate::{
ffi::{Bool, Nts, NtsMut},
opaque_struct,
types::ffi::{Bool, Nts, NtsMut},
};
use std::os::raw;
#[derive(thiserror::Error, Debug)]
#[error("{}", .0)]
pub struct Err(String);
pub(super) struct Err(String);
opaque_struct!(pub BasicBlock);
opaque_struct!(pub Builder);
opaque_struct!(pub Context);
opaque_struct!(pub Error);
opaque_struct!(pub Jit);
opaque_struct!(pub Module);
opaque_struct!(pub Type);
opaque_struct!(pub Value);
opaque_struct!(pub(super) BasicBlock);
opaque_struct!(pub(super) Builder);
opaque_struct!(pub(super) Context);
opaque_struct!(pub(super) Error);
opaque_struct!(pub(super) Jit);
opaque_struct!(pub(super) Module);
opaque_struct!(pub(super) Type);
opaque_struct!(pub(super) Value);
extern "C" {
// linking
pub fn target_init();
pub(super) fn target_init();
// messages
#[link_name = "LLVMCreateMessage"]
pub fn message_new(msg: Nts) -> NtsMut;
pub(super) fn message_new(msg: Nts) -> NtsMut;
#[link_name = "LLVMDisposeMessage"]
pub fn message_drop(msg: NtsMut);
pub(super) fn message_drop(msg: NtsMut);
// errors
#[link_name = "LLVMConsumeError"]
pub fn error_drop(err: *mut Error);
pub(super) fn error_drop(err: *mut Error);
#[must_use]
#[link_name = "LLVMGetErrorMessage"]
pub fn error_drop_get_message(err: *mut Error) -> NtsMut;
pub(super) fn error_drop_get_message(err: *mut Error) -> NtsMut;
#[link_name = "LLVMDisposeErrorMessage"]
pub fn error_message_drop(msg: NtsMut);
pub(super) fn error_message_drop(msg: NtsMut);
// contexts
#[link_name = "LLVMContextCreate"]
pub fn context_new() -> *mut Context;
pub(super) fn context_new() -> *mut Context;
#[link_name = "LLVMContextDispose"]
pub fn context_drop(contx: *mut Context);
pub(super) fn context_drop(contx: *mut Context);
#[link_name = "LLVMAppendBasicBlockInContext"]
pub fn context_append_basic_block(
pub(super) fn context_append_basic_block(
contx: *mut Context, func: *mut Value, name: Nts,
) -> *mut BasicBlock;
// modules
#[link_name = "LLVMModuleCreateWithNameInContext"]
pub fn module_new_with_name_in_context(
pub(super) fn module_new_with_name_in_context(
module_id: Nts, contx: *mut Context,
) -> *mut Module;
#[link_name = "LLVMDisposeModule"]
pub fn module_drop(modul: *mut Module);
pub(super) fn module_drop(modul: *mut Module);
#[link_name = "LLVMAddFunction"]
pub fn module_add_function(
pub(super) fn module_add_function(
modul: *mut Module, name: Nts, func_ty: *mut Type,
) -> *mut Value;
#[link_name = "LLVMGetNamedFunction"]
pub fn module_get_named_function(
pub(super) fn module_get_named_function(
modul: *mut Module, name: Nts,
) -> *mut Value;
// builders
#[link_name = "LLVMCreateBuilderInContext"]
pub fn builder_new_in_context(contx: *mut Context) -> *mut Builder;
pub(super) fn builder_new_in_context(contx: *mut Context) -> *mut Builder;
#[link_name = "LLVMDisposeBuilder"]
pub fn builder_drop(build: *mut Builder);
pub(super) fn builder_drop(build: *mut Builder);
#[link_name = "LLVMBuildCall2"]
pub fn builder_call(
pub(super) fn builder_call(
build: *mut Builder, ftyp: *mut Type, func: *mut Value,
args: *mut *mut Value, count: raw::c_uint, name: Nts,
) -> *mut Value;
#[link_name = "LLVMBuildRetVoid"]
pub fn builder_ret_void(build: *mut Builder) -> *mut Value;
pub(super) fn builder_ret_void(build: *mut Builder) -> *mut Value;
#[link_name = "LLVMBuildRet"]
pub fn builder_ret(build: *mut Builder, retn: *mut Value) -> *mut Value;
pub(super) fn builder_ret(
build: *mut Builder, retn: *mut Value,
) -> *mut Value;
#[link_name = "LLVMPositionBuilderAtEnd"]
pub fn builder_position_at_end(build: *mut Builder, block: *mut BasicBlock);
pub(super) fn builder_position_at_end(
build: *mut Builder, block: *mut BasicBlock,
);
// types
#[link_name = "LLVMPrintTypeToString"]
pub fn type_print_to_string(handle: *mut Type) -> NtsMut;
pub(super) fn type_print_to_string(handle: *mut Type) -> NtsMut;
#[link_name = "LLVMArrayType"]
pub fn type_array(element: *mut Type, count: raw::c_uint) -> *mut Type;
pub(super) fn type_array(
element: *mut Type, count: raw::c_uint,
) -> *mut Type;
#[link_name = "LLVMFunctionType"]
pub fn type_function(
pub(super) fn type_function(
retn: *mut Type, params: *mut *mut Type, count: raw::c_uint,
is_var_arg: Bool,
) -> *mut Type;
#[link_name = "LLVMVoidType"]
pub fn type_void() -> *mut Type;
pub(super) fn type_void() -> *mut Type;
#[link_name = "LLVMInt1Type"]
pub fn type_int1() -> *mut Type;
pub(super) fn type_int1() -> *mut Type;
#[link_name = "LLVMInt8Type"]
pub fn type_int8() -> *mut Type;
pub(super) fn type_int8() -> *mut Type;
#[link_name = "LLVMInt16Type"]
pub fn type_int16() -> *mut Type;
pub(super) fn type_int16() -> *mut Type;
#[link_name = "LLVMInt32Type"]
pub fn type_int32() -> *mut Type;
pub(super) fn type_int32() -> *mut Type;
#[link_name = "LLVMInt64Type"]
pub fn type_int64() -> *mut Type;
pub(super) fn type_int64() -> *mut Type;
// values
#[link_name = "LLVMGetUndef"]
pub fn value_undef(ty: *mut Type) -> *mut Value;
pub(super) fn value_undef(ty: *mut Type) -> *mut Value;
#[link_name = "LLVMConstInt"]
pub fn value_const_int(
pub(super) fn value_const_int(
ty: *mut Type, num: raw::c_ulonglong, sign_extend: Bool,
) -> *mut Value;
#[link_name = "LLVMTypeOf"]
pub fn value_type(value: *mut Value) -> *mut Type;
pub(super) fn value_type(value: *mut Value) -> *mut Type;
#[must_use]
pub fn value_verify_func(func: *mut Value) -> NtsMut;
pub(super) fn value_verify_func(func: *mut Value) -> NtsMut;
// jit
pub fn jit_new(jit: *mut *mut Jit) -> *mut Error;
pub fn jit_drop(jit: *mut Jit);
pub(super) fn jit_new(jit: *mut *mut Jit) -> *mut Error;
pub(super) fn jit_drop(jit: *mut Jit);
}
impl Err {
pub fn check(err: *mut Error) -> Result<(), Self> {
pub(super) fn check(err: *mut Error) -> Result<(), Self> {
if err.is_null() {
Ok(())
} else {
@ -155,7 +163,7 @@ impl Err {
}
}
pub unsafe fn get_msg_and_drop(
pub(super) unsafe fn get_msg_and_drop(
msg: NtsMut, f: unsafe extern "C" fn(msg: NtsMut),
) -> String {
unsafe {

Loading…
Cancel
Save