1 /// Copyright: Copyright (c) 2022 Andrey Penechko. 2 /// License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 3 /// Authors: Andrey Penechko. 4 5 /// C API bindings 6 module libvox; 7 8 // SliceString that are passed to Vox compiler must remain valid until vox_free 9 10 extern(C) nothrow { 11 // -- Init libvox 12 VoxCompiler* vox_init(); 13 // -- Free libvox 14 void vox_free(VoxCompiler* compiler); 15 16 // -- Reset function. Needs to be run before compiling a new program 17 // Previously compiled program will be lost 18 void vox_begin_compilation(VoxCompiler* compiler); 19 20 // -- Setup functions 21 // Should be run after vox_init -> vox_begin_compilation and before `vox_compile` 22 void vox_set_passes_exe(VoxCompiler* compiler); 23 void vox_set_passes_jit(VoxCompiler* compiler); 24 void vox_set_target_os(VoxCompiler* compiler, TargetOs os); 25 void vox_set_output_filename(VoxCompiler* compiler, SliceString outFile); 26 Identifier vox_id_get_or_reg(VoxCompiler* compiler, SliceString hostModuleName); 27 SliceString vox_id_get_string(VoxCompiler* compiler, Identifier id); 28 Identifier vox_id_get_parent(VoxCompiler* compiler, Identifier id); 29 LinkIndex vox_get_or_create_external_module(VoxCompiler* compiler, Identifier modId); 30 // hostModuleIndex is the one returned by `vox_get_or_create_external_module` function 31 void vox_reg_host_symbol(VoxCompiler* compiler, LinkIndex hostModuleIndex, Identifier symId, void* ptr); 32 void vox_add_module(VoxCompiler* compiler, SliceString fileName, SliceString filedata); 33 // -- Setup functions end 34 35 // -- Runs compilation 36 // Should be run after vox_init -> vox_begin_compilation -> setup 37 // 0 means no errors, non-zero indicates errors 38 int vox_compile(VoxCompiler* compiler); 39 40 // -- Those functions can be called after successfull `vox_compile` 41 void vox_make_code_executable(VoxCompiler* compiler); 42 void* vox_find_function(VoxCompiler* compiler, Identifier funcId); 43 } 44 45 struct VoxCompiler; 46 struct Slice(T) { 47 this(T[] data) { 48 ptr = data.ptr; 49 length = data.length; 50 } 51 ulong length; 52 T* ptr; 53 T[] slice() { return ptr[0..length]; } 54 alias slice this; 55 } 56 alias SliceString = Slice!(const(char)); 57 alias Identifier = uint; 58 alias LinkIndex = uint; 59 60 enum TargetOs : ubyte { 61 windows, 62 linux, 63 macos, 64 }