2012-05-23 21:26:15 +02:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
gbx_jit.c
|
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
(c) 2018 Benoît Minisini <g4mba5@gmail.com>
|
2012-05-23 21:26:15 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_JIT_C
|
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
#include "gb_common_case.h"
|
|
|
|
#include "gbx_component.h"
|
2018-05-28 03:18:44 +02:00
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_object.h"
|
2012-05-23 21:26:15 +02:00
|
|
|
#include "gbx_api.h"
|
2018-05-26 16:50:00 +02:00
|
|
|
#include "gbx_jit.h"
|
|
|
|
|
2018-06-01 03:50:42 +02:00
|
|
|
typedef
|
|
|
|
struct {
|
|
|
|
JIT_FUNC addr;
|
|
|
|
PCODE *code;
|
|
|
|
}
|
|
|
|
JIT_FUNCTION;
|
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
static bool _component_loaded = FALSE;
|
2018-06-01 03:50:42 +02:00
|
|
|
static GB_FUNCTION _jit_compile_func;
|
2012-05-23 21:26:15 +02:00
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
static bool _no_jit = FALSE;
|
|
|
|
static void *_jit_library = NULL;
|
2012-05-23 21:26:15 +02:00
|
|
|
|
2018-06-01 03:50:42 +02:00
|
|
|
static JIT_FUNCTION *_jit_func = NULL;
|
|
|
|
|
|
|
|
void JIT_exit(void)
|
|
|
|
{
|
|
|
|
ARRAY_delete(&_jit_func);
|
|
|
|
}
|
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
bool JIT_compile(ARCHIVE *arch)
|
2012-05-24 01:39:02 +02:00
|
|
|
{
|
2018-05-26 16:50:00 +02:00
|
|
|
GB_VALUE *ret;
|
|
|
|
char *path;
|
|
|
|
void *lib;
|
|
|
|
void **iface;
|
2012-05-23 21:26:15 +02:00
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
if (_no_jit)
|
2012-05-23 21:26:15 +02:00
|
|
|
return TRUE;
|
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
if (arch)
|
|
|
|
{
|
|
|
|
if (arch->jit_library)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_jit_library)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-05-23 21:26:15 +02:00
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
if (!_component_loaded)
|
2012-05-23 21:26:15 +02:00
|
|
|
{
|
2018-05-26 16:50:00 +02:00
|
|
|
char *var = getenv("GB_NO_JIT");
|
2012-05-26 00:24:13 +02:00
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
_component_loaded = TRUE;
|
2012-05-23 21:26:15 +02:00
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
if (var && var[0] && !(var[0] == '0' && var[1] == 0))
|
|
|
|
{
|
|
|
|
_no_jit = TRUE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-07-03 00:38:29 +02:00
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
fprintf(stderr, "gbx3: loading gb.jit component\n");
|
|
|
|
COMPONENT_load(COMPONENT_create("gb.jit"));
|
2018-06-01 03:50:42 +02:00
|
|
|
if (GB_GetFunction(&_jit_compile_func, CLASS_find_global("_Jit"), "Compile", "", "s"))
|
2018-05-26 16:50:00 +02:00
|
|
|
ERROR_panic("Unable to find _Jit.Compile method");
|
2012-05-23 21:26:15 +02:00
|
|
|
}
|
2012-06-01 02:18:38 +02:00
|
|
|
|
2018-06-01 03:50:42 +02:00
|
|
|
ret = GB_Call(&_jit_compile_func, 0, FALSE);
|
2018-05-26 16:50:00 +02:00
|
|
|
path = GB_ToZeroString((GB_STRING *)ret);
|
|
|
|
if (!*path)
|
|
|
|
ERROR_panic("Unable to compile jit source file");
|
|
|
|
|
2018-05-28 03:18:44 +02:00
|
|
|
//fprintf(stderr, "gbx3: shared jit library is: %s\n", path);
|
2018-05-26 16:50:00 +02:00
|
|
|
|
|
|
|
lib = dlopen(path, RTLD_NOW);
|
|
|
|
if (!lib)
|
|
|
|
ERROR_panic("Unable to load jit library: %s", dlerror());
|
|
|
|
|
|
|
|
if (arch)
|
|
|
|
arch->jit_library = lib;
|
|
|
|
else
|
|
|
|
_jit_library = lib;
|
|
|
|
|
2018-05-28 03:18:44 +02:00
|
|
|
iface = dlsym(lib, "GB_PTR");
|
|
|
|
if (iface) *((void **)iface) = &GAMBAS_Api;
|
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
iface = dlsym(lib, "JIT_PTR");
|
2018-05-28 03:18:44 +02:00
|
|
|
if (iface) *((void **)iface) = &GAMBAS_JitApi;
|
2018-05-26 16:50:00 +02:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-06-01 03:50:42 +02:00
|
|
|
void JIT_create_function(ARCHIVE *arch, CLASS *class, int index)
|
2018-05-26 16:50:00 +02:00
|
|
|
{
|
2018-06-01 03:50:42 +02:00
|
|
|
FUNCTION *func;
|
|
|
|
JIT_FUNCTION *jit;
|
2018-05-26 16:50:00 +02:00
|
|
|
void *lib;
|
|
|
|
void *addr;
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
|
2018-06-01 03:50:42 +02:00
|
|
|
func = &class->load->func[index];
|
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
if (!arch)
|
|
|
|
lib = _jit_library;
|
|
|
|
else
|
|
|
|
lib = arch->jit_library;
|
|
|
|
|
|
|
|
len = sprintf(COMMON_buffer, "%s_%d", class->name, index);
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
COMMON_buffer[i] = tolower(COMMON_buffer[i]);
|
2012-05-23 21:26:15 +02:00
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
addr = dlsym(lib, COMMON_buffer);
|
2018-06-01 03:50:42 +02:00
|
|
|
if (!addr)
|
|
|
|
{
|
|
|
|
func->fast = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (func->debug)
|
|
|
|
fprintf(stderr, "gbx3: loading jit function: %s.%s\n", class->name, func->debug->name);
|
|
|
|
|
|
|
|
if (!_jit_func)
|
|
|
|
ARRAY_create(&_jit_func);
|
|
|
|
|
|
|
|
jit = (JIT_FUNCTION *)ARRAY_add(&_jit_func);
|
|
|
|
|
|
|
|
jit->addr = addr;
|
|
|
|
jit->code = func->code;
|
|
|
|
|
|
|
|
func->code = (PCODE *)jit;
|
2012-05-23 21:26:15 +02:00
|
|
|
}
|
|
|
|
|
2018-05-28 03:18:44 +02:00
|
|
|
|
|
|
|
void JIT_exec(void)
|
|
|
|
{
|
|
|
|
CLASS *class = CP;
|
|
|
|
void *object = OP;
|
|
|
|
VALUE *sp = SP;
|
2018-06-01 03:50:42 +02:00
|
|
|
JIT_FUNCTION *jit;
|
2018-05-28 03:18:44 +02:00
|
|
|
|
|
|
|
CP = EXEC.class;
|
|
|
|
OP = (void *)EXEC.object;
|
|
|
|
|
2018-06-01 03:50:42 +02:00
|
|
|
jit = (JIT_FUNCTION *)(EXEC.func->code);
|
|
|
|
PC = jit->code;
|
|
|
|
|
|
|
|
(*(jit->addr))(EXEC.nparam);
|
2018-05-28 03:18:44 +02:00
|
|
|
|
|
|
|
if (SP != sp)
|
2018-05-29 03:43:23 +02:00
|
|
|
fprintf(stderr, "SP: %+ld (%ld) !\n", (SP - sp) / sizeof(VALUE), SP - sp);
|
2018-05-28 03:18:44 +02:00
|
|
|
|
|
|
|
CP = class;
|
|
|
|
OP = object;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *JIT_get_dynamic_addr(int index)
|
|
|
|
{
|
|
|
|
CLASS_VAR *var = &CP->load->dyn[index];
|
|
|
|
return &OP[var->pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
void *JIT_get_static_addr(int index)
|
|
|
|
{
|
|
|
|
CLASS_VAR *var = &CP->load->stat[index];
|
|
|
|
return &((char *)CP->stat)[var->pos];
|
|
|
|
}
|
|
|
|
|
2018-05-29 23:25:57 +02:00
|
|
|
void *JIT_get_class_ref(int index)
|
|
|
|
{
|
|
|
|
return CP->load->class_ref[index];
|
|
|
|
}
|
|
|
|
|
2018-05-28 03:18:44 +02:00
|
|
|
CLASS_CONST *JIT_get_constant(int index)
|
2012-05-23 21:26:15 +02:00
|
|
|
{
|
2018-05-28 03:18:44 +02:00
|
|
|
return &CP->load->cst[index];
|
2012-05-23 21:26:15 +02:00
|
|
|
}
|
2018-05-29 03:43:23 +02:00
|
|
|
|
|
|
|
void JIT_debug(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vfprintf(stderr, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|