|
|
|
@ -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 { |
|
|
|
|