From f39f7063ef51d60365b84250a4f25a3978096138 Mon Sep 17 00:00:00 2001 From: gambas Date: Thu, 17 May 2018 01:13:56 +0200 Subject: [PATCH] Start working on the new jit system. [COMPILER] * NEW: Start working on the new jit system. [GB.JIT] * NEW: Rename the component as 'gb.jit.llvm'. --- Makefile.am | 2 +- configure.ac | 2 +- {gb.jit => gb.jit.llvm}/AUTHORS | 0 {gb.jit => gb.jit.llvm}/COPYING | 0 {gb.jit => gb.jit.llvm}/ChangeLog | 0 {gb.jit => gb.jit.llvm}/INSTALL | 0 {gb.jit => gb.jit.llvm}/Makefile.am | 2 +- {gb.jit => gb.jit.llvm}/NEWS | 0 {gb.jit => gb.jit.llvm}/README | 0 {gb.jit => gb.jit.llvm}/acinclude.m4 | 0 {gb.jit => gb.jit.llvm}/component.am | 0 {gb.jit => gb.jit.llvm}/configure.ac | 10 +- {gb.jit => gb.jit.llvm}/gambas.h | 0 {gb.jit => gb.jit.llvm}/gb_common.h | 0 {gb.jit => gb.jit.llvm}/m4 | 0 {gb.jit => gb.jit.llvm}/reconf | 0 gb.jit.llvm/src/Makefile.am | 34 ++++++ {gb.jit => gb.jit.llvm}/src/gb.jit.h | 0 gb.jit.llvm/src/gb.jit.llvm.component | 5 + {gb.jit => gb.jit.llvm}/src/jit.h | 0 {gb.jit => gb.jit.llvm}/src/jit_api.cpp | 0 {gb.jit => gb.jit.llvm}/src/jit_codegen.cpp | 0 .../src/jit_codegen_conv.h | 0 {gb.jit => gb.jit.llvm}/src/jit_compile.cpp | 0 {gb.jit => gb.jit.llvm}/src/jit_conv.cpp | 0 .../src/jit_expressions.cpp | 0 .../src/jit_gambas_pass.cpp | 0 {gb.jit => gb.jit.llvm}/src/jit_gambas_pass.h | 0 {gb.jit => gb.jit.llvm}/src/jit_read.cpp | 0 {gb.jit => gb.jit.llvm}/src/jit_runtime.c | 0 {gb.jit => gb.jit.llvm}/src/jit_runtime.h | 0 {gb.jit => gb.jit.llvm}/src/main.cpp | 0 {gb.jit => gb.jit.llvm}/src/main.h | 0 gb.jit/src/Makefile.am | 34 ------ gb.jit/src/gb.jit.component | 5 - main/gbc/Makefile.am | 1 + main/gbc/gb_str.c | 32 +++++ main/gbc/gb_str.h | 2 + main/gbc/gbc_class.h | 3 +- main/gbc/gbc_header.c | 5 +- main/gbc/gbc_jit.c | 112 ++++++++++++++++++ main/gbc/gbc_jit.h | 38 ++++++ main/gbc/gbc_output.c | 2 - main/gbc/gbc_trans_code.c | 82 ++++++++----- main/gbx/gbx_jit.h | 2 +- 45 files changed, 292 insertions(+), 81 deletions(-) rename {gb.jit => gb.jit.llvm}/AUTHORS (100%) rename {gb.jit => gb.jit.llvm}/COPYING (100%) rename {gb.jit => gb.jit.llvm}/ChangeLog (100%) rename {gb.jit => gb.jit.llvm}/INSTALL (100%) rename {gb.jit => gb.jit.llvm}/Makefile.am (74%) rename {gb.jit => gb.jit.llvm}/NEWS (100%) rename {gb.jit => gb.jit.llvm}/README (100%) rename {gb.jit => gb.jit.llvm}/acinclude.m4 (100%) rename {gb.jit => gb.jit.llvm}/component.am (100%) rename {gb.jit => gb.jit.llvm}/configure.ac (87%) rename {gb.jit => gb.jit.llvm}/gambas.h (100%) rename {gb.jit => gb.jit.llvm}/gb_common.h (100%) rename {gb.jit => gb.jit.llvm}/m4 (100%) rename {gb.jit => gb.jit.llvm}/reconf (100%) create mode 100644 gb.jit.llvm/src/Makefile.am rename {gb.jit => gb.jit.llvm}/src/gb.jit.h (100%) create mode 100644 gb.jit.llvm/src/gb.jit.llvm.component rename {gb.jit => gb.jit.llvm}/src/jit.h (100%) rename {gb.jit => gb.jit.llvm}/src/jit_api.cpp (100%) rename {gb.jit => gb.jit.llvm}/src/jit_codegen.cpp (100%) rename {gb.jit => gb.jit.llvm}/src/jit_codegen_conv.h (100%) rename {gb.jit => gb.jit.llvm}/src/jit_compile.cpp (100%) rename {gb.jit => gb.jit.llvm}/src/jit_conv.cpp (100%) rename {gb.jit => gb.jit.llvm}/src/jit_expressions.cpp (100%) rename {gb.jit => gb.jit.llvm}/src/jit_gambas_pass.cpp (100%) rename {gb.jit => gb.jit.llvm}/src/jit_gambas_pass.h (100%) rename {gb.jit => gb.jit.llvm}/src/jit_read.cpp (100%) rename {gb.jit => gb.jit.llvm}/src/jit_runtime.c (100%) rename {gb.jit => gb.jit.llvm}/src/jit_runtime.h (100%) rename {gb.jit => gb.jit.llvm}/src/main.cpp (100%) rename {gb.jit => gb.jit.llvm}/src/main.h (100%) delete mode 100644 gb.jit/src/Makefile.am delete mode 100644 gb.jit/src/gb.jit.component create mode 100644 main/gbc/gbc_jit.c create mode 100644 main/gbc/gbc_jit.h diff --git a/Makefile.am b/Makefile.am index cf0952c50..e43ad5584 100644 --- a/Makefile.am +++ b/Makefile.am @@ -34,7 +34,7 @@ SUBDIRS = \ @gmp_dir@ \ @ncurses_dir@ \ @media_dir@ \ - @jit_dir@ \ + @jitllvm_dir@ \ @httpd_dir@ \ @openssl_dir@ \ @openal_dir@ \ diff --git a/configure.ac b/configure.ac index 37fa65043..f19ca32d8 100644 --- a/configure.ac +++ b/configure.ac @@ -46,7 +46,7 @@ GB_CONFIG_SUBDIRS(gsl, gb.gsl) GB_CONFIG_SUBDIRS(gmp, gb.gmp) GB_CONFIG_SUBDIRS(ncurses, gb.ncurses) GB_CONFIG_SUBDIRS(media, gb.media) -GB_CONFIG_SUBDIRS(jit, gb.jit) +GB_CONFIG_SUBDIRS(jitllvm, gb.jit.llvm) GB_CONFIG_SUBDIRS(httpd, gb.httpd) GB_CONFIG_SUBDIRS(openssl, gb.openssl) GB_CONFIG_SUBDIRS(openal, gb.openal) diff --git a/gb.jit/AUTHORS b/gb.jit.llvm/AUTHORS similarity index 100% rename from gb.jit/AUTHORS rename to gb.jit.llvm/AUTHORS diff --git a/gb.jit/COPYING b/gb.jit.llvm/COPYING similarity index 100% rename from gb.jit/COPYING rename to gb.jit.llvm/COPYING diff --git a/gb.jit/ChangeLog b/gb.jit.llvm/ChangeLog similarity index 100% rename from gb.jit/ChangeLog rename to gb.jit.llvm/ChangeLog diff --git a/gb.jit/INSTALL b/gb.jit.llvm/INSTALL similarity index 100% rename from gb.jit/INSTALL rename to gb.jit.llvm/INSTALL diff --git a/gb.jit/Makefile.am b/gb.jit.llvm/Makefile.am similarity index 74% rename from gb.jit/Makefile.am rename to gb.jit.llvm/Makefile.am index c22c42f4c..ed93e642d 100644 --- a/gb.jit/Makefile.am +++ b/gb.jit.llvm/Makefile.am @@ -1,3 +1,3 @@ ACLOCAL_AMFLAGS = -I m4 --install -SUBDIRS = @JIT_DIR@ +SUBDIRS = @JITLLVM_DIR@ EXTRA_DIST = reconf gambas.h gb*.h diff --git a/gb.jit/NEWS b/gb.jit.llvm/NEWS similarity index 100% rename from gb.jit/NEWS rename to gb.jit.llvm/NEWS diff --git a/gb.jit/README b/gb.jit.llvm/README similarity index 100% rename from gb.jit/README rename to gb.jit.llvm/README diff --git a/gb.jit/acinclude.m4 b/gb.jit.llvm/acinclude.m4 similarity index 100% rename from gb.jit/acinclude.m4 rename to gb.jit.llvm/acinclude.m4 diff --git a/gb.jit/component.am b/gb.jit.llvm/component.am similarity index 100% rename from gb.jit/component.am rename to gb.jit.llvm/component.am diff --git a/gb.jit/configure.ac b/gb.jit.llvm/configure.ac similarity index 87% rename from gb.jit/configure.ac rename to gb.jit.llvm/configure.ac index 6f8cb5a77..8f3e7b8ba 100644 --- a/gb.jit/configure.ac +++ b/gb.jit.llvm/configure.ac @@ -1,10 +1,10 @@ dnl ---- configure.ac for gb.jit m4_include([../version.m4]) -AC_INIT(gambas3-gb-jit, GB_VERSION, GB_MAIL, [], GB_URL) +AC_INIT(gambas3-gb-jit-llvm, GB_VERSION, GB_MAIL, [], GB_URL) AC_CONFIG_MACRO_DIR([m4]) -GB_INIT(gb.jit) +GB_INIT(gb.jit.llvm) AC_PROG_LIBTOOL min_llvm_version=3.1 @@ -17,7 +17,7 @@ if test "x$LLVM_CONFIG" = x; then fi if test "x$LLVM_CONFIG" = xno; then - touch DISABLED DISABLED.gb.jit + touch DISABLED DISABLED.gb.jit.llvm else AC_MSG_CHECKING([for LLVM, version between $min_llvm_version and $max_llvm_version]) @@ -35,14 +35,14 @@ else else AC_MSG_RESULT(no) - touch DISABLED DISABLED.gb.jit + touch DISABLED DISABLED.gb.jit.llvm fi fi dnl [GB_FIND(libLLVM-$LLVM_VERSION.$SHLIBEXT, `$LLVM_CONFIG --prefix` /usr/lib/llvm* /usr/local /usr, lib)], GB_COMPONENT( - jit, JIT, gb.jit, [src], + jitllvm, JITLLVM, gb.jit.llvm, [src], [GB_FIND(llvm/ExecutionEngine/JIT.h llvm/Config/llvm-config.h llvm-c/Core.h, `$LLVM_CONFIG --prefix` /usr/lib/llvm* /usr/local /usr, include)], [], [$C_LIB `$LLVM_CONFIG --ldflags` $LLVM_LIBS], diff --git a/gb.jit/gambas.h b/gb.jit.llvm/gambas.h similarity index 100% rename from gb.jit/gambas.h rename to gb.jit.llvm/gambas.h diff --git a/gb.jit/gb_common.h b/gb.jit.llvm/gb_common.h similarity index 100% rename from gb.jit/gb_common.h rename to gb.jit.llvm/gb_common.h diff --git a/gb.jit/m4 b/gb.jit.llvm/m4 similarity index 100% rename from gb.jit/m4 rename to gb.jit.llvm/m4 diff --git a/gb.jit/reconf b/gb.jit.llvm/reconf similarity index 100% rename from gb.jit/reconf rename to gb.jit.llvm/reconf diff --git a/gb.jit.llvm/src/Makefile.am b/gb.jit.llvm/src/Makefile.am new file mode 100644 index 000000000..dee551c14 --- /dev/null +++ b/gb.jit.llvm/src/Makefile.am @@ -0,0 +1,34 @@ +COMPONENT = gb.jit.llvm +include $(top_srcdir)/component.am + +gblib_LTLIBRARIES = gb.jit.llvm.la +noinst_LTLIBRARIES = libjit.llvm.la + +libjit_llvm_la_LIBADD = +libjit_llvm_la_LDFLAGS = -module @LD_FLAGS@ +libjit_llvm_la_CPPFLAGS = @JITLLVM_INC@ +libjit_llvm_la_CXXFLAGS = $(AM_CFLAGS) -std=gnu++0x -fno-exceptions -fno-rtti + +gb_jit_llvm_la_LIBADD = libjit.la @JITLLVM_LIB@ +gb_jit_llvm_la_LDFLAGS = -module @LD_FLAGS@ @JITLLVM_LDFLAGS@ +gb_jit_llvm_la_CPPFLAGS = @JITLLVM_INC@ +gb_jit_llvm_la_CXXFLAGS = $(AM_CXXFLAGS) -std=gnu++0x -fno-exceptions + +libjit_llvm_la_SOURCES = \ + jit_gambas_pass.cpp \ + jit_gambas_pass.h + +gb_jit_llvm_la_SOURCES = \ + gb.jit.h \ + jit_api.cpp \ + jit_codegen_conv.h \ + jit_codegen.cpp \ + jit_compile.cpp \ + jit_conv.cpp \ + jit_expressions.cpp \ + jit.h \ + jit_read.cpp \ + jit_runtime.c \ + jit_runtime.h \ + main.cpp \ + main.h diff --git a/gb.jit/src/gb.jit.h b/gb.jit.llvm/src/gb.jit.h similarity index 100% rename from gb.jit/src/gb.jit.h rename to gb.jit.llvm/src/gb.jit.h diff --git a/gb.jit.llvm/src/gb.jit.llvm.component b/gb.jit.llvm/src/gb.jit.llvm.component new file mode 100644 index 000000000..5d62b136d --- /dev/null +++ b/gb.jit.llvm/src/gb.jit.llvm.component @@ -0,0 +1,5 @@ +[Component] +Key=gb.jit.llvm +Name=Gambas JIT Compiler based on LLVM +Author=Emil Lenngren +Hidden=True diff --git a/gb.jit/src/jit.h b/gb.jit.llvm/src/jit.h similarity index 100% rename from gb.jit/src/jit.h rename to gb.jit.llvm/src/jit.h diff --git a/gb.jit/src/jit_api.cpp b/gb.jit.llvm/src/jit_api.cpp similarity index 100% rename from gb.jit/src/jit_api.cpp rename to gb.jit.llvm/src/jit_api.cpp diff --git a/gb.jit/src/jit_codegen.cpp b/gb.jit.llvm/src/jit_codegen.cpp similarity index 100% rename from gb.jit/src/jit_codegen.cpp rename to gb.jit.llvm/src/jit_codegen.cpp diff --git a/gb.jit/src/jit_codegen_conv.h b/gb.jit.llvm/src/jit_codegen_conv.h similarity index 100% rename from gb.jit/src/jit_codegen_conv.h rename to gb.jit.llvm/src/jit_codegen_conv.h diff --git a/gb.jit/src/jit_compile.cpp b/gb.jit.llvm/src/jit_compile.cpp similarity index 100% rename from gb.jit/src/jit_compile.cpp rename to gb.jit.llvm/src/jit_compile.cpp diff --git a/gb.jit/src/jit_conv.cpp b/gb.jit.llvm/src/jit_conv.cpp similarity index 100% rename from gb.jit/src/jit_conv.cpp rename to gb.jit.llvm/src/jit_conv.cpp diff --git a/gb.jit/src/jit_expressions.cpp b/gb.jit.llvm/src/jit_expressions.cpp similarity index 100% rename from gb.jit/src/jit_expressions.cpp rename to gb.jit.llvm/src/jit_expressions.cpp diff --git a/gb.jit/src/jit_gambas_pass.cpp b/gb.jit.llvm/src/jit_gambas_pass.cpp similarity index 100% rename from gb.jit/src/jit_gambas_pass.cpp rename to gb.jit.llvm/src/jit_gambas_pass.cpp diff --git a/gb.jit/src/jit_gambas_pass.h b/gb.jit.llvm/src/jit_gambas_pass.h similarity index 100% rename from gb.jit/src/jit_gambas_pass.h rename to gb.jit.llvm/src/jit_gambas_pass.h diff --git a/gb.jit/src/jit_read.cpp b/gb.jit.llvm/src/jit_read.cpp similarity index 100% rename from gb.jit/src/jit_read.cpp rename to gb.jit.llvm/src/jit_read.cpp diff --git a/gb.jit/src/jit_runtime.c b/gb.jit.llvm/src/jit_runtime.c similarity index 100% rename from gb.jit/src/jit_runtime.c rename to gb.jit.llvm/src/jit_runtime.c diff --git a/gb.jit/src/jit_runtime.h b/gb.jit.llvm/src/jit_runtime.h similarity index 100% rename from gb.jit/src/jit_runtime.h rename to gb.jit.llvm/src/jit_runtime.h diff --git a/gb.jit/src/main.cpp b/gb.jit.llvm/src/main.cpp similarity index 100% rename from gb.jit/src/main.cpp rename to gb.jit.llvm/src/main.cpp diff --git a/gb.jit/src/main.h b/gb.jit.llvm/src/main.h similarity index 100% rename from gb.jit/src/main.h rename to gb.jit.llvm/src/main.h diff --git a/gb.jit/src/Makefile.am b/gb.jit/src/Makefile.am deleted file mode 100644 index 4771b31a1..000000000 --- a/gb.jit/src/Makefile.am +++ /dev/null @@ -1,34 +0,0 @@ -COMPONENT = gb.jit -include $(top_srcdir)/component.am - -gblib_LTLIBRARIES = gb.jit.la -noinst_LTLIBRARIES = libjit.la - -libjit_la_LIBADD = -libjit_la_LDFLAGS = -module @LD_FLAGS@ -libjit_la_CPPFLAGS = @JIT_INC@ -libjit_la_CXXFLAGS = $(AM_CFLAGS) -std=gnu++0x -fno-exceptions -fno-rtti - -gb_jit_la_LIBADD = libjit.la @JIT_LIB@ -gb_jit_la_LDFLAGS = -module @LD_FLAGS@ @JIT_LDFLAGS@ -gb_jit_la_CPPFLAGS = @JIT_INC@ -gb_jit_la_CXXFLAGS = $(AM_CXXFLAGS) -std=gnu++0x -fno-exceptions - -libjit_la_SOURCES = \ - jit_gambas_pass.cpp \ - jit_gambas_pass.h - -gb_jit_la_SOURCES = \ - gb.jit.h \ - jit_api.cpp \ - jit_codegen_conv.h \ - jit_codegen.cpp \ - jit_compile.cpp \ - jit_conv.cpp \ - jit_expressions.cpp \ - jit.h \ - jit_read.cpp \ - jit_runtime.c \ - jit_runtime.h \ - main.cpp \ - main.h diff --git a/gb.jit/src/gb.jit.component b/gb.jit/src/gb.jit.component deleted file mode 100644 index 965200b59..000000000 --- a/gb.jit/src/gb.jit.component +++ /dev/null @@ -1,5 +0,0 @@ -[Component] -Key=gb.jit -Name=Gambas JIT Compiler -Author=Emil Lenngren -Hidden=True \ No newline at end of file diff --git a/main/gbc/Makefile.am b/main/gbc/Makefile.am index 7ad0cc10e..62433c62c 100644 --- a/main/gbc/Makefile.am +++ b/main/gbc/Makefile.am @@ -36,6 +36,7 @@ gbc3_SOURCES = \ gbc_pcode.c \ gb_file.h gb_file.c \ gbc_form.h gbc_form.c gbc_form_webpage.c \ + gbc_jit.h gbc_jit.c \ gb_str.h gb_str.c \ gbc_chown.h gbc_chown.c \ gb_common.c \ diff --git a/main/gbc/gb_str.c b/main/gbc/gb_str.c index d33ba31aa..79aa89f02 100644 --- a/main/gbc/gb_str.c +++ b/main/gbc/gb_str.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "gb_common.h" #include "gb_error.h" @@ -96,4 +97,35 @@ char *STR_cat(const char *str, ...) return cpy; } +char *STR_upper(const char *str) +{ + char *s; + char *p; + + p = s = STR_copy(str); + while (*p) + { + *p = toupper(*p); + p++; + } + + return s; +} + + +char *STR_lower(const char *str) +{ + char *s; + char *p; + + p = s = STR_copy(str); + while (*p) + { + *p = tolower(*p); + p++; + } + + return s; +} + diff --git a/main/gbc/gb_str.h b/main/gbc/gb_str.h index c5e8b8b21..42ca5a87e 100644 --- a/main/gbc/gb_str.h +++ b/main/gbc/gb_str.h @@ -30,6 +30,8 @@ char *STR_copy(const char *str); char *STR_copy_len(const char *str, int len); char *STR_cat(const char *str, ...); char *STR_add(char *d, const char *s); +char *STR_upper(const char *str); +char *STR_lower(const char *str); #define STR_free(_str) IFREE(_str) diff --git a/main/gbc/gbc_class.h b/main/gbc/gbc_class.h index 4adf6c0af..6afb7010e 100644 --- a/main/gbc/gbc_class.h +++ b/main/gbc/gbc_class.h @@ -202,7 +202,8 @@ typedef unsigned nocreate : 1; // class cannot be instantiated unsigned all_fast : 1; // all methods have the Fast option (JIT) unsigned has_static : 1; // has static methods, properties or variables - unsigned _reserved : 10; + unsigned has_fast : 1; // has at least one fast method + unsigned _reserved : 9; VARIABLE *stat; // static variables VARIABLE *dyn; // dynamic variables CONSTANT *constant; // constants diff --git a/main/gbc/gbc_header.c b/main/gbc/gbc_header.c index dc124b288..6bfa3dd39 100644 --- a/main/gbc/gbc_header.c +++ b/main/gbc/gbc_header.c @@ -688,7 +688,9 @@ static bool header_function(TRANS_FUNC *func) if (is_static) TYPE_set_flag(&func->type, TF_STATIC); if (is_public) TYPE_set_flag(&func->type, TF_PUBLIC); - func->fast = is_fast; + func->fast = is_fast || JOB->class->all_fast; + if (func->fast) + JOB->class->has_fast = TRUE; // Check special methods @@ -887,7 +889,6 @@ static bool header_option(void) { JOB->current++; JOB->class->all_fast = TRUE; - return TRUE; } diff --git a/main/gbc/gbc_jit.c b/main/gbc/gbc_jit.c new file mode 100644 index 000000000..dbbc029c0 --- /dev/null +++ b/main/gbc/gbc_jit.c @@ -0,0 +1,112 @@ +/*************************************************************************** + + gbc_jit.c + + (c) 2000-2018 Benoît Minisini + + 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 __GBC_JIT_C + +#include +#include +#include +#include +#include + +#include "gb_file.h" +#include "gb_str.h" +#include "gb_error.h" +#include "gbc_compile.h" +#include "gbc_chown.h" +#include "gbc_class.h" +#include "gbc_jit.h" + +static FILE *_file = NULL; +static char *_prefix; + +void JIT_begin(void) +{ + const char *path; + + if (_file) + return; + + if (mkdir(".jit", 0777) == 0) + FILE_set_owner(".jit", COMP_project); + + _prefix = STR_lower(JOB->class->name); + path = FILE_cat(".jit", _prefix, NULL); + path = FILE_set_ext(path, "c"); + + _file = fopen(path, "w"); + if (!_file) + THROW("Cannot create file: &1", path); +} + +void JIT_end(void) +{ + if (!_file) + return; + + fclose(_file); + _file = NULL; + STR_free(_prefix); +} + +void JIT_declare_func(FUNCTION *func) +{ + char *name = STR_lower(TABLE_get_symbol_name(JOB->class->table, func->name)); + + if (!TYPE_is_public(func->type)) + JIT_print("static "); + JIT_print("void %s_%s();\n", _prefix, name); + + STR_free(name); +} + +void JIT_begin_func(FUNCTION *func) +{ + const char *fname = TABLE_get_symbol_name(JOB->class->table, func->name); + char *name = STR_lower(fname); + + JIT_section(fname); + + if (!TYPE_is_public(func->type)) + JIT_print("static "); + JIT_print("void %s_%s(ushort code)\n{\n", _prefix, name); + + STR_free(name); +} + +void JIT_end_func(void) +{ + JIT_print("}\n"); +} + +void JIT_print(const char *str, ...) +{ + va_list args; + va_start(args, str); + vfprintf(_file, str, args); +} + +void JIT_section(const char *str) +{ + JIT_print("\n// %s\n\n", str); +} diff --git a/main/gbc/gbc_jit.h b/main/gbc/gbc_jit.h new file mode 100644 index 000000000..9ee4b13c8 --- /dev/null +++ b/main/gbc/gbc_jit.h @@ -0,0 +1,38 @@ +/*************************************************************************** + + gbc_jit.h + + (c) 2000-2018 Benoît Minisini + + 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. + +***************************************************************************/ + +#ifndef __GBC_JIT_H +#define __GBC_JIT_H + +#include "gbc_class.h" + +void JIT_begin(void); +void JIT_end(void); +void JIT_declare_func(FUNCTION *func); +void JIT_begin_func(FUNCTION *func); +void JIT_end_func(void); +void JIT_print(const char *str, ...); +void JIT_section(const char *str); + +#endif + diff --git a/main/gbc/gbc_output.c b/main/gbc/gbc_output.c index f944097f3..e7480689f 100644 --- a/main/gbc/gbc_output.c +++ b/main/gbc/gbc_output.c @@ -1151,10 +1151,8 @@ char *OUTPUT_get_file(const char *file) { char *output; char *p; - //char *dir; char *name; - //dir = STR_copy(FILE_get_dir(file)); name = STR_copy(FILE_get_name(file)); for (p = name; *p; p++) diff --git a/main/gbc/gbc_trans_code.c b/main/gbc/gbc_trans_code.c index 3aec624f4..1368d25fd 100644 --- a/main/gbc/gbc_trans_code.c +++ b/main/gbc/gbc_trans_code.c @@ -31,12 +31,13 @@ #include "gb_error.h" #include "gbc_compile.h" #include "gbc_trans.h" +#include "gbc_jit.h" #include "gb_code.h" #include "gb_limit.h" /*#define DEBUG*/ -static FUNCTION *func; +static FUNCTION *_func; static CLASS_SYMBOL *add_local(int sym_index, TYPE type, int value, bool used) { @@ -44,10 +45,10 @@ static CLASS_SYMBOL *add_local(int sym_index, TYPE type, int value, bool used) PARAM *loc; bool warnings = JOB->warnings; - if (ARRAY_count(func->local) >= MAX_LOCAL_SYMBOL) + if (ARRAY_count(_func->local) >= MAX_LOCAL_SYMBOL) THROW("Too many local variables"); - loc = ARRAY_add(&func->local); + loc = ARRAY_add(&_func->local); loc->index = sym_index; loc->type = type; loc->value = value; @@ -72,10 +73,10 @@ static void create_local_from_param() JOB->line--; // For line number of declaration - for (i = 0; i < func->nparam; i++) + for (i = 0; i < _func->nparam; i++) { - if (TYPE_get_id(func->param[i].type) != T_VOID) - add_local(func->param[i].index, func->param[i].type, (i - func->nparam), func->param[i].ignore); + if (TYPE_get_id(_func->param[i].type) != T_VOID) + add_local(_func->param[i].index, _func->param[i].type, (i - _func->nparam), _func->param[i].ignore); } JOB->line++; @@ -86,9 +87,9 @@ static void remove_local() int i; CLASS_SYMBOL *sym; - for (i = 0; i < ARRAY_count(func->local); i++) + for (i = 0; i < ARRAY_count(_func->local); i++) { - sym = CLASS_get_symbol(JOB->class, func->local[i].index); + sym = CLASS_get_symbol(JOB->class, _func->local[i].index); if (!sym->local_used) { @@ -169,7 +170,7 @@ static bool TRANS_local(void) no_warning = FALSE; sym_index = PATTERN_index(*pattern); - sym = add_local(sym_index, decl.type, func->nlocal, FALSE); + sym = add_local(sym_index, decl.type, _func->nlocal, FALSE); pattern++; if (no_warning) @@ -178,11 +179,11 @@ static bool TRANS_local(void) JOB->warnings = save_warnings; } - func->nlocal++; + _func->nlocal++; if (TRANS_init_var(&decl)) { - CODE_pop_local(func->nlocal - 1); + CODE_pop_local(_func->nlocal - 1); sym->local_assigned = TRUE; } @@ -197,7 +198,7 @@ static bool TRANS_local(void) /*if (!many) { if (TRANS_init_var(&decl)) - CODE_pop_local(func->nlocal - 1); + CODE_pop_local(_func->nlocal - 1); }*/ if (!TRANS_is(RS_COMMA)) @@ -310,7 +311,7 @@ void TRANS_statement(void) static void translate_body() { PATTERN *look; - bool is_proc = (TYPE_get_id(func->type) == T_VOID); + bool is_proc = (TYPE_get_id(_func->type) == T_VOID); bool test_newline; //int line = JOB->line - 1; bool just_got_select = FALSE; @@ -545,28 +546,44 @@ static void trans_call(const char *name, int nparam) void TRANS_code(void) { int i; + bool fast; + if (JOB->class->has_fast) + { + JIT_begin(); + + JIT_section("Declarations"); + for (i = 0; i < ARRAY_count(JOB->class->function); i++) + { + _func = &JOB->class->function[i]; + if (!_func->start || !_func->fast) + continue; + JIT_declare_func(_func); + } + } + for (i = 0; i < ARRAY_count(JOB->class->function); i++) { - func = &JOB->class->function[i]; - - CODE_begin_function(func); + _func = &JOB->class->function[i]; + fast = _func->fast; + + CODE_begin_function(_func); if (JOB->verbose) - printf("Compiling %s()...\n", TABLE_get_symbol_name(JOB->class->table, func->name)); + printf("Compiling %s()...\n", TABLE_get_symbol_name(JOB->class->table, _func->name)); /* Do not debug implicit or generated functions */ - if (!func->start || func->name == NO_SYMBOL || TABLE_get_symbol_name(JOB->class->table, func->name)[0] == '@') + if (!_func->start || _func->name == NO_SYMBOL || TABLE_get_symbol_name(JOB->class->table, _func->name)[0] == '@') JOB->nobreak = TRUE; else JOB->nobreak = FALSE; - JOB->line = func->line; - JOB->current = func->start; - JOB->func = func; + JOB->line = _func->line; + JOB->current = _func->start; + JOB->func = _func; /* fonction implicite ? */ - if (!func->start) + if (!_func->start) { if ((i == FUNC_INIT_DYNAMIC) && (JOB->form != NULL)) { @@ -578,9 +595,12 @@ void TRANS_code(void) FUNCTION_add_last_pos_line(); CODE_op(C_RETURN, 0, 0, TRUE); if (JOB->verbose) - CODE_dump(func->code, func->ncode); + CODE_dump(_func->code, _func->ncode); continue; } + + if (fast) + JIT_begin_func(_func); create_local_from_param(); @@ -588,22 +608,28 @@ void TRANS_code(void) CODE_return(2); // Return from function, ignore Gosub stack - CODE_end_function(func); + CODE_end_function(_func); FUNCTION_add_last_pos_line(); - func->stack = func->nlocal + func->nctrl + CODE_stack_usage; + _func->stack = _func->nlocal + _func->nctrl + CODE_stack_usage; if (JOB->verbose) { - CODE_dump(func->code, func->ncode); - printf("%d local(s) %d control(s) ", func->nlocal, func->nctrl); - printf("%d stack\n", func->stack); + CODE_dump(_func->code, _func->ncode); + printf("%d local(s) %d control(s) ", _func->nlocal, _func->nctrl); + printf("%d stack\n", _func->stack); printf("\n"); } remove_local(); + + if (fast) + JIT_end_func(); } + if (fast) + JIT_end(); + CLASS_check_properties(JOB->class); CLASS_check_unused_global(JOB->class); diff --git a/main/gbx/gbx_jit.h b/main/gbx/gbx_jit.h index 0e28243c4..453a9db47 100644 --- a/main/gbx/gbx_jit.h +++ b/main/gbx/gbx_jit.h @@ -31,7 +31,7 @@ #include "gbx_object.h" #include "gbx_exec.h" -#include "../../gb.jit/src/gb.jit.h" +#include "../../gb.jit.llvm/src/gb.jit.h" #ifndef __GBX_JIT_C