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"
|
2012-05-23 21:26:15 +02:00
|
|
|
#include "gbx_api.h"
|
2018-05-26 16:50:00 +02:00
|
|
|
#include "gbx_jit.h"
|
|
|
|
|
|
|
|
static bool _component_loaded = FALSE;
|
|
|
|
static GB_FUNCTION _jit_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-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"));
|
|
|
|
if (GB_GetFunction(&_jit_func, CLASS_find_global("_Jit"), "Compile", "", "s"))
|
|
|
|
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-05-26 16:50:00 +02:00
|
|
|
ret = GB_Call(&_jit_func, 0, FALSE);
|
|
|
|
path = GB_ToZeroString((GB_STRING *)ret);
|
|
|
|
if (!*path)
|
|
|
|
ERROR_panic("Unable to compile jit source file");
|
|
|
|
|
|
|
|
fprintf(stderr, "gbx3: shared jit library is: %s\n", path);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
iface = dlsym(lib, "JIT_PTR");
|
|
|
|
if (iface)
|
|
|
|
*((void **)iface) = &GAMBAS_JitApi;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *JIT_get_function(ARCHIVE *arch, CLASS *class, int index)
|
|
|
|
{
|
|
|
|
void *lib;
|
|
|
|
void *addr;
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
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);
|
|
|
|
/*if (!addr)
|
|
|
|
ERROR_panic("Unable to find jit function %s", COMMON_buffer);*/
|
|
|
|
return addr;
|
2012-05-23 21:26:15 +02:00
|
|
|
}
|
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
void JIT_hello(void)
|
2012-05-23 21:26:15 +02:00
|
|
|
{
|
2018-05-26 16:50:00 +02:00
|
|
|
fprintf(stderr, "Hello JIT!\n");
|
2012-05-23 21:26:15 +02:00
|
|
|
}
|