From a300002e3c9aeb153463e0700a4b0898c8069a0e Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Fri, 21 May 2004 04:58:27 +0000 Subject: [PATCH] Add the "jit_insn_alloca" instruction. --- ChangeLog | 5 +++++ include/jit/jit-insn.h | 2 ++ include/jit/jit-opcode.h | 7 ++++++- include/jit/jit-plus.h | 1 + jit/jit-insn.c | 20 ++++++++++++++++++++ jit/jit-interp.c | 25 +++++++++++++++++++++++++ jit/jit-opcode.c | 6 ++++++ jitplus/jit-plus-function.cpp | 6 ++++++ 8 files changed, 71 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index a2c0385..8e1fdd0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -15,6 +15,11 @@ remove the last remaining C++ code from libjit.so so that it is now a pure C library. + * include/jit/jit-insn.h, include/jit/jit-opcode.h, + include/jit/jit-plus.h, jit/jit-insn.c, jit/jit-interp.c, + jit/jit-opcode.c, jitplus/jit-plus-function.cpp: + add the "jit_insn_alloca" instruction. + 2004-05-20 Rhys Weatherley * include/jit/jit-value.h, jit/jit-insn.c, jit/jit-value.c: diff --git a/include/jit/jit-insn.h b/include/jit/jit-insn.h index 01b85e8..692c781 100644 --- a/include/jit/jit-insn.h +++ b/include/jit/jit-insn.h @@ -279,6 +279,8 @@ int jit_insn_memmove int jit_insn_memset (jit_function_t func, jit_value_t dest, jit_value_t value, jit_value_t size) JIT_NOTHROW; +jit_value_t jit_insn_alloca + (jit_function_t func, jit_value_t size) JIT_NOTHROW; int jit_insn_move_blocks (jit_function_t func, jit_label_t from_label, jit_label_t to_label); diff --git a/include/jit/jit-opcode.h b/include/jit/jit-opcode.h index 2b08ecd..52eb1be 100644 --- a/include/jit/jit-opcode.h +++ b/include/jit/jit-opcode.h @@ -499,10 +499,15 @@ extern "C" { #define JIT_OP_MEMMOVE 0x0196 #define JIT_OP_MEMSET 0x0197 +/* + * Allocate memory from the stack. + */ +#define JIT_OP_ALLOCA 0x0198 + /* * The number of opcodes in the above list. */ -#define JIT_OP_NUM_OPCODES 0x0198 +#define JIT_OP_NUM_OPCODES 0x0199 /* * Opcode information. diff --git a/include/jit/jit-plus.h b/include/jit/jit-plus.h index 7288b89..919e918 100644 --- a/include/jit/jit-plus.h +++ b/include/jit/jit-plus.h @@ -315,6 +315,7 @@ public: (const jit_value& dest, const jit_value& src, const jit_value& size); void insn_memset (const jit_value& dest, const jit_value& value, const jit_value& size); + jit_value insn_alloca(const jit_value& size); private: jit_function_t func; diff --git a/jit/jit-insn.c b/jit/jit-insn.c index 85635d6..190f1e3 100644 --- a/jit/jit-insn.c +++ b/jit/jit-insn.c @@ -6629,6 +6629,26 @@ int jit_insn_memset return apply_ternary(func, JIT_OP_MEMSET, dest, value, size); } +/*@ + * @deftypefun jit_value_t jit_insn_alloca (jit_function_t func, jit_value_t size) + * Allocate @code{size} bytes of memory from the stack. + * @end deftypefun +@*/ +jit_value_t jit_insn_alloca(jit_function_t func, jit_value_t size) +{ + /* Round the size to the best alignment boundary on this platform */ + size = jit_insn_convert(func, size, jit_type_nuint, 0); + size = jit_insn_add + (func, size, jit_value_create_nint_constant + (func, jit_type_nuint, JIT_BEST_ALIGNMENT - 1)); + size = jit_insn_and + (func, size, jit_value_create_nint_constant + (func, jit_type_nuint, ~((jit_nint)(JIT_BEST_ALIGNMENT - 1)))); + + /* Allocate "size" bytes of memory from the stack */ + return apply_unary(func, JIT_OP_ALLOCA, size, jit_type_void_ptr); +} + /*@ * @deftypefun int jit_insn_move_blocks (jit_function_t func, jit_label_t from_label, jit_label_t to_label) * Move all of the blocks between @code{from_label} (inclusive) and diff --git a/jit/jit-interp.c b/jit/jit-interp.c index 6e5d5a2..6579e4b 100644 --- a/jit/jit-interp.c +++ b/jit/jit-interp.c @@ -3948,6 +3948,31 @@ void _jit_run_function(jit_function_interp_t func, jit_item *args, } VMBREAK; + /****************************************************************** + * Allocate memory from the stack. + ******************************************************************/ + + VMCASE(JIT_OP_ALLOCA): + { + /* Allocate memory from the stack */ + VM_STK_PTR0 = (void *)alloca(VM_STK_NUINT0); + VM_MODIFY_PC_AND_STACK(1, 0); + + /* We need to reset the "setjmp" point for this function + because the saved stack pointer is no longer the same. + If we don't do this, then an exception throw will pop + the alloca'ed memory, causing dangling pointer problems */ + if(jbuf) + { + if(setjmp(jbuf->buf)) + { + exception_object = jit_exception_get_last_and_clear(); + goto handle_exception; + } + } + } + VMBREAK; + /****************************************************************** * Argument variable access opcodes. ******************************************************************/ diff --git a/jit/jit-opcode.c b/jit/jit-opcode.c index 591ac0d..cb5ff33 100644 --- a/jit/jit-opcode.c +++ b/jit/jit-opcode.c @@ -514,6 +514,12 @@ jit_opcode_info_t const jit_opcodes[JIT_OP_NUM_OPCODES] = { {"memcpy", F_(PTR, PTR, PTR)}, {"memmove", F_(PTR, PTR, PTR)}, {"memset", F_(PTR, INT, PTR)}, + + /* + * Allocate memory from the stack. + */ + {"alloca", F_(PTR, PTR, EMPTY)}, + }; #if defined(JIT_BACKEND_INTERP) diff --git a/jitplus/jit-plus-function.cpp b/jitplus/jit-plus-function.cpp index 9b64830..2a574c5 100644 --- a/jitplus/jit-plus-function.cpp +++ b/jitplus/jit-plus-function.cpp @@ -644,6 +644,7 @@ jit_value jit_function::get_struct_pointer() * @deftypemethodx jit_function void insn_memcpy ({const jit_value&} dest, {const jit_value&} src, {const jit_value&} size) * @deftypemethodx void insn_memmove ({const jit_value&} dest, {const jit_value&} src, {const jit_value&} size) * @deftypemethodx void jit_insn_memset ({const jit_value&} dest, {const jit_value&} value, {const jit_value&} size) + * @deftypemethodx jit_value jit_insn_alloca ({const jit_value&} size) * Create instructions of various kinds. @xref{Instructions}, for more * information on the individual instructions and their arguments. * @end deftypemethod @@ -1187,6 +1188,11 @@ void jit_function::insn_memset } } +jit_value jit_function::insn_alloca(const jit_value& size) +{ + value_wrap(jit_insn_alloca(func, size.raw())); +} + void jit_function::register_on_demand() { jit_function_set_on_demand_compiler(func, on_demand_compiler); -- 2.47.3