]> git.unchartedbackwaters.co.uk Git - francis/libjit.git/commitdiff
Add the "jit_insn_alloca" instruction.
authorRhys Weatherley <rweather@southern-storm.com.au>
Fri, 21 May 2004 04:58:27 +0000 (04:58 +0000)
committerRhys Weatherley <rweather@southern-storm.com.au>
Fri, 21 May 2004 04:58:27 +0000 (04:58 +0000)
ChangeLog
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

index a2c03857584a89b5ce12d6ff14c7cb924bc21741..8e1fdd0cbfb181524d7ce2bb0bd42957717a3950 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
        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  <rweather@southern-storm.com.au>
 
        * include/jit/jit-value.h, jit/jit-insn.c, jit/jit-value.c:
index 01b85e8a6e8914279d382a8e28775f7e36329eb3..692c7810f10172f01f474edc0e167f2273716474 100644 (file)
@@ -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);
index 2b08ecd9408cf80cb84bd6d493650c640eeda51b..52eb1befd123900a013c41ec31456e88a290a355 100644 (file)
@@ -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.
index 7288b891e5830bb5b44369f3deaa7568fb0d50ab..919e918c13621f6ecafbe7bb343c523fe8a7bf33 100644 (file)
@@ -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;
index 85635d65ad85896851ffbb5dbe792d16af0380f8..190f1e3c96faf01d30d384977367d6b1b9ef4148 100644 (file)
@@ -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
index 6e5d5a283129bd6ffeedda9d13ffbbfd9d761e69..6579e4b33bedf4de6d0bbf68b3f4f26aab2b5bca 100644 (file)
@@ -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.
                 ******************************************************************/
index 591ac0d05272a53446b3647565df859974c63228..cb5ff337a0c6715dbc5f1d35a3c2f2587a354080 100644 (file)
@@ -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)
index 9b64830619bf47b9c7d1f10843adc78c930dd1d0..2a574c5169c1dd7c8f9d7f43ccf03fbbb9384edf 100644 (file)
@@ -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);