diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..32fccd9 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,29 @@ +get_filename_component(_EXEC_NAME "${CMAKE_CURRENT_SOURCE_DIR}" NAME) +add_subdirectory("myjit") + +foreach(i RANGE 1 3) + set(EXEC_NAME "${_EXEC_NAME}_demo_${i}") + + set(Source_Files "demo${i}.c") + source_group("${EXEC_NAME} Source Files" FILES "${Source_Files}") + + add_executable("${EXEC_NAME}" "${Source_Files}") + set_target_properties( + "${EXEC_NAME}" + PROPERTIES + LINKER_LANGUAGE + C + ) + target_link_libraries("${EXEC_NAME}" PRIVATE "myjit") + + # install rules + + set(installable_libs "${EXEC_NAME}" "${PROJECT_NAME}_compiler_flags") + + if (TARGET "${DEPENDANT_LIBRARY}") + list(APPEND installable_libs "${DEPENDANT_LIBRARY}") + endif () + install(TARGETS ${installable_libs} + DESTINATION "bin" + EXPORT "${EXEC_NAME}Targets") +endforeach () diff --git a/myjit/CMakeLists.txt b/myjit/CMakeLists.txt new file mode 100644 index 0000000..90a5453 --- /dev/null +++ b/myjit/CMakeLists.txt @@ -0,0 +1,68 @@ +get_filename_component(LIBRARY_NAME "${CMAKE_CURRENT_SOURCE_DIR}" NAME) + +set(Header_Files + "amd64-codegen.h" + "amd64-specific.h" + "arm32-codegen.h" + "arm32-specific.h" + "common86-codegen.h" + "common86-specific.h" + "cpu-detect.h" + "flow-analysis.h" + "jitlib-core.h" + "jitlib-debug.h" + "jitlib.h" + "llrb.h" + "reg-allocator.h" + "rmap.h" + "set.h" + "sparc-codegen.h" + "sparc-specific.h" + "sse2-specific.h" + "util.h" + "x86-codegen.h" + "x86-specific.h" +) +source_group("Header Files" FILES "${Header_Files}") + +set(Source_Files + "code-check.c" + "jitlib-core.c" + "jitlib-debug.c" + "llrb.c" + "x86-common-stuff.c" + ) +source_group("Source Files" FILES "${Source_Files}") + +add_library("${LIBRARY_NAME}" "${LIBRARY_TYPE_FLAG}" "${Header_Files}" "${Source_Files}") +target_include_directories( + "${LIBRARY_NAME}" + PUBLIC + "$" + "$" +) +set_target_properties( + "${LIBRARY_NAME}" + PROPERTIES + LINKER_LANGUAGE + C +) + +# install rules +include(GenerateExportHeader) +set(_export_file "${CMAKE_CURRENT_SOURCE_DIR}/${LIBRARY_NAME}_export.h") +generate_export_header("${LIBRARY_NAME}" EXPORT_FILE_NAME "${_export_file}") + +# setup the version numbering +set_property(TARGET "${LIBRARY_NAME}" PROPERTY VERSION "1.0.0") +set_property(TARGET "${LIBRARY_NAME}" PROPERTY SOVERSION "1") + +set(installable_libs "${LIBRARY_NAME}" "${PROJECT_NAME}_compiler_flags") +install(FILES "${Header_Files}" DESTINATION "include") + +if (TARGET "${DEPENDANT_LIBRARY}") + list(APPEND installable_libs "${DEPENDANT_LIBRARY}") +endif () +install(TARGETS ${installable_libs} + DESTINATION "lib" + EXPORT "${LIBRARY_NAME}Targets") diff --git a/myjit/code-check.c b/myjit/code-check.c index 918846d..5461b24 100644 --- a/myjit/code-check.c +++ b/myjit/code-check.c @@ -1,3 +1,10 @@ +#include +#include +#include +#include "jitlib.h" +#include "jitlib-core.h" +#include "set.h" + /* * MyJIT * Copyright (C) 2015 Petr Krajca, diff --git a/myjit/jitlib-core.h b/myjit/jitlib-core.h index af7aa3b..3c29252 100644 --- a/myjit/jitlib-core.h +++ b/myjit/jitlib-core.h @@ -26,8 +26,9 @@ #include #include #include + #include "jitlib.h" -#include "llrb.c" +#include "llrb.h" #define FR_IMM (jit_mkreg(JIT_RTYPE_FLOAT, JIT_RTYPE_IMM, 0)) @@ -94,7 +95,7 @@ typedef struct jit_prepared_args { int gp_args; // number of prepared GP arguments int fp_args; // number od prepared FP arguments int stack_size; // size of stack occupied by passed arguments - jit_op * op; // corresponding ``PREPARE'' operation + struct jit_op * op; // corresponding ``PREPARE'' operation struct jit_out_arg {// array of arguments union { long generic; @@ -175,9 +176,9 @@ int jit_optimize_join_addmul(struct jit * jit); int jit_optimize_join_addimm(struct jit * jit); void jit_optimize_frame_ptr(struct jit * jit); void jit_optimize_unused_assignments(struct jit * jit); -static int is_cond_branch_op(jit_op *op); // FIXME: rename to: jit_op_is_cond_branch +static int is_cond_branch_op(struct jit_op *op); // FIXME: rename to: jit_op_is_cond_branch static inline void jit_set_free(jit_set * s); -void jit_trace_callback(struct jit *jit, jit_op *op, int verbosity, int trace); +void jit_trace_callback(struct jit *jit, struct jit_op *op, int verbosity, int trace); /** * Initialize argpos-th argument. @@ -191,11 +192,11 @@ void jit_init_arg_params(struct jit * jit, struct jit_func_info * info, int argp void jit_assign_regs(struct jit * jit); struct jit_reg_allocator * jit_reg_allocator_create(); void jit_reg_allocator_free(struct jit_reg_allocator * a); -void jit_gen_op(struct jit * jit, jit_op * op); +void jit_gen_op(struct jit * jit, struct jit_op * op); char * jit_reg_allocator_get_hwreg_name(struct jit_reg_allocator * al, int reg); -int jit_reg_in_use(jit_op * op, int reg, int fp); -jit_hw_reg * jit_get_unused_reg(struct jit_reg_allocator * al, jit_op * op, int fp); -jit_hw_reg * jit_get_unused_reg_with_index(struct jit_reg_allocator * al, jit_op * op, int fp, int index); +int jit_reg_in_use(struct jit_op * op, int reg, int fp); +jit_hw_reg * jit_get_unused_reg(struct jit_reg_allocator * al, struct jit_op * op, int fp); +jit_hw_reg * jit_get_unused_reg_with_index(struct jit_reg_allocator * al, struct jit_op * op, int fp, int index); void rmap_free(jit_rmap * regmap); void jit_allocator_hints_free(jit_tree *); diff --git a/myjit/jitlib-debug.c b/myjit/jitlib-debug.c index cfb1298..c63ff78 100644 --- a/myjit/jitlib-debug.c +++ b/myjit/jitlib-debug.c @@ -30,7 +30,7 @@ #define ABS(x) ((x) < 0 ? - (x) : x) -#include "llrb.c" +#include "llrb.h" #define OUTPUT_BUF_SIZE (8192) //#define print_padding(buf, size) while (strlen((buf)) < (size)) { strcat((buf), " "); } @@ -158,23 +158,6 @@ static void compiler_based_debugger(struct jit * jit) unlink(obj_file_name); } -typedef struct jit_disasm { - char *indent_template; - char *reg_template; - char *freg_template; - char *arg_template; - char *farg_template; - char *reg_fp_template; - char *reg_out_template; - char *reg_imm_template; - char *reg_fimm_template; - char *reg_unknown_template; - char *label_template; - char *label_forward_template; - char *generic_addr_template; - char *generic_value_template; -} jit_disasm; - struct jit_disasm jit_disasm_general = { .indent_template = " ", .reg_template = "r%i", diff --git a/myjit/jitlib-debug.h b/myjit/jitlib-debug.h new file mode 100644 index 0000000..724df33 --- /dev/null +++ b/myjit/jitlib-debug.h @@ -0,0 +1,46 @@ +/* + * MyJIT + * Copyright (C) 2010, 2015, 2017, 2018 Petr Krajca, + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef JITLIB_DEBUG_H +#define JITLIB_DEBUG_H + +#include "jitlib.h" +#include "llrb.h" +#include "jitlib-core.h" + +typedef struct jit_disasm { + char *indent_template; + char *reg_template; + char *freg_template; + char *arg_template; + char *farg_template; + char *reg_fp_template; + char *reg_out_template; + char *reg_imm_template; + char *reg_fimm_template; + char *reg_unknown_template; + char *label_template; + char *label_forward_template; + char *generic_addr_template; + char *generic_value_template; +} jit_disasm; + +int print_op(FILE *, struct jit_disasm *, struct jit_op *, jit_tree *, int); + +#endif /* JITLIB_DEBUG_H */ diff --git a/myjit/jitlib.h b/myjit/jitlib.h index 0752a78..4fe7661 100644 --- a/myjit/jitlib.h +++ b/myjit/jitlib.h @@ -26,6 +26,7 @@ #include #include #include "cpu-detect.h" +#include "jitlib-debug.h" /* * Memory management @@ -650,4 +651,5 @@ static inline jit_op *jit_data_bytes(struct jit *jit, jit_value count, unsigned int jit_regs_active_count(jit_op *op); void jit_regs_active(jit_op *op, jit_value *dest); -#endif + +#endif /* JITLIB_H */ diff --git a/myjit/llrb.c b/myjit/llrb.c index cb9594c..68bed50 100644 --- a/myjit/llrb.c +++ b/myjit/llrb.c @@ -17,24 +17,13 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ - -#ifndef LLRB_C -#define LLRB_C +#include +#include "jitlib.h" +#include "llrb.h" #define RED (1) #define BLACK (0) -typedef jit_value jit_tree_key; -typedef void * jit_tree_value; - -typedef struct jit_tree { - struct jit_tree * left; - struct jit_tree * right; - int color; - jit_tree_key key; - jit_tree_value value; -} jit_tree; - static inline int is_red(jit_tree * n) { @@ -277,5 +266,3 @@ static int jit_tree_size(jit_tree *h) if (h == NULL) return 0; return jit_tree_size(h->left) + jit_tree_size(h->right) + 1; } - -#endif diff --git a/myjit/llrb.h b/myjit/llrb.h new file mode 100644 index 0000000..5d3eb84 --- /dev/null +++ b/myjit/llrb.h @@ -0,0 +1,34 @@ +/* + * MyJIT + * Copyright (C) 2010, 2015 Petr Krajca, + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef LLRB_H +#define LLRB_H + +typedef jit_value jit_tree_key; +typedef void * jit_tree_value; + +typedef struct jit_tree { + struct jit_tree * left; + struct jit_tree * right; + int color; + jit_tree_key key; + jit_tree_value value; +} jit_tree; + +#endif /* LLRB_H */