Skip to content
Snippets Groups Projects
Commit 332a843b authored by Anton Johansson's avatar Anton Johansson Committed by Anton
Browse files

Add `LibTcgInterface` struct containing func ptrs


Useful when `dlopen`ing by reducing the amount of funnctions needed to
be `dlsym`d manually. Also makes sure function prototypes are kept in
sync between users of libtcg and libtcg.

Signed-off-by: default avatarAnton Johansson <anjo@rev.ng>
parent 81c3249c
No related branches found
No related tags found
No related merge requests found
......@@ -311,6 +311,11 @@ typedef struct LibTinyCodeInstructionList {
* Lastly we have the functions we expose.
*/
/*
* TODO(anjo): We might eventually need to export these in LibTcgInterface
* as well.
*/
void libtcg_dump_instruction_to_buffer(LibTinyCodeInstruction *insn, char *buf,
size_t size);
......@@ -334,15 +339,58 @@ typedef struct LibTinyCodeDesc {
struct LibTinyCodeContext;
typedef struct LibTinyCodeContext LibTinyCodeContext;
LibTinyCodeContext *libtcg_context_create(LibTinyCodeDesc *desc);
void libtcg_context_destroy(LibTinyCodeContext *context);
/*
* Following are a bunch of macros that help in defining a function prototype
* along with a typedef of the function type.
*
* NOTE(anjo): Not really a fan of this, but it does reduce the amount of
* function prototypes you need to keep in sync. :/
*/
/* Returns the name of the function's typedef */
#define LIBTCG_FUNC_TYPE(name) \
name ## _func
/* Declares and typedefs a function */
#define LIBTCG_EXPORT(ret, name, params) \
ret name params; /* Function declaration */ \
typedef ret LIBTCG_FUNC_TYPE(name) params /* Funciton typedef */
LibTinyCodeInstructionList libtcg_translate(LibTinyCodeContext *context,
char *buffer, size_t size,
uint64_t virtual_address);
LIBTCG_EXPORT(LibTinyCodeContext *, libtcg_context_create, (LibTinyCodeDesc *desc));
LIBTCG_EXPORT(void, libtcg_context_destroy, (LibTinyCodeContext *context));
LIBTCG_EXPORT(LibTinyCodeInstructionList, libtcg_translate, (LibTinyCodeContext *context, char *buffer, size_t size, uint64_t virtual_address));
LIBTCG_EXPORT(void, libtcg_instruction_list_destroy, (LibTinyCodeContext *context, LibTinyCodeInstructionList));
void libtcg_instruction_list_destroy(LibTinyCodeContext *context,
LibTinyCodeInstructionList instruction_list);
/*
* struct to help load functions we expose,
* useful when `dlopen`ing.
*/
typedef struct LibTcgInterface {
// Functions
LIBTCG_FUNC_TYPE(libtcg_context_create) *context_create;
LIBTCG_FUNC_TYPE(libtcg_context_destroy) *context_destroy;
LIBTCG_FUNC_TYPE(libtcg_translate) *translate;
LIBTCG_FUNC_TYPE(libtcg_instruction_list_destroy) *instruction_list_destroy;
// CPUState variables
intptr_t exception_index;
intptr_t is_thumb;
intptr_t pc;
intptr_t sp;
} LibTcgInterface;
/*
* Last function we export takes care of creating/populating a LibTcgInterface.
* This is the only funciton needing to be manually loaded using `dlsym`.
*/
LIBTCG_EXPORT(LibTcgInterface, libtcg_load, (void));
#undef LIBTCG_EXPORT
/*
* NOTE(anjo): LIBTCG_FUNC_TYPE remains defined, so it can be used
* to get the typedef'd function.
*/
#ifdef __cplusplus
}
......
......@@ -423,3 +423,65 @@ void libtcg_instruction_list_destroy(LibTinyCodeContext *context,
context->desc.mem_free(instruction_list.temps);
context->desc.mem_free(instruction_list.labels);
}
LibTcgInterface libtcg_load(void) {
return (LibTcgInterface) {
// Functions
.context_create = libtcg_context_create,
.context_destroy = libtcg_context_destroy,
.translate = libtcg_translate,
.instruction_list_destroy = libtcg_instruction_list_destroy,
// CPUState variables
.exception_index = offsetof(ArchCPU, parent_obj)
+ offsetof(CPUState, exception_index),
// Target specific CPU state
#if defined(TARGET_ALPHA)
#elif defined(TARGET_ARM)
#if defined(TARGET_AARCH64)
#else
#endif
#elif defined(TARGET_AVR)
#elif defined(TARGET_CRIS)
#elif defined(TARGET_HEXAGON)
#elif defined(TARGET_HPPA)
#elif defined(TARGET_I386)
#if defined(TARGET_X86_64)
#elif
#endif
#elif defined(TARGET_M68K)
#elif defined(TARGET_MICROBLAZE)
#elif defined(TARGET_MIPS)
#if defined(TARGET_MIPS64)
#elif
#endif
#elif defined(TARGET_NIOS2)
#elif defined(TARGET_OPENRISC)
#elif defined(TARGET_PPC)
#if defined(TARGET_PPC64)
#error "lol"
#endif
#elif defined(TARGET_PPC64)
#elif defined(TARGET_RISCV32)
#if defined(TARGET_RISCV64)
#error "lol"
#endif
#elif defined(TARGET_RISCV64)
#elif defined(TARGET_RX)
#elif defined(TARGET_S390X)
#elif defined(TARGET_SH4)
#elif defined(TARGET_SPARC)
#if defined(TARGET_SPARC64)
#error "lol"
#endif
#elif defined(TARGET_SPARC64)
#elif defined(TARGET_TRICORE)
#elif defined(TARGET_XTENSA)
#endif
#if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
.is_thumb = offsetof(CPUArchState, thumb),
#endif
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment