]> git.unchartedbackwaters.co.uk Git - francis/libjit.git/commitdiff
Execute the "main" method once a Dynamic Pascal program has been compiled;
authorRhys Weatherley <rweather@southern-storm.com.au>
Thu, 6 May 2004 22:41:15 +0000 (22:41 +0000)
committerRhys Weatherley <rweather@southern-storm.com.au>
Thu, 6 May 2004 22:41:15 +0000 (22:41 +0000)
fix some calling convention problems with "call_external".

ChangeLog
dpas/dpas-function.c
dpas/dpas-internal.h
dpas/dpas-main.c
dpas/dpas-parser.y
jit/jit-interp.cpp
jit/jit-interp.h
jit/jit-opcode.c
jit/jit-rules-interp.c

index 723c49589511cec784fb60e9fc73c5951309ef1d..1f8f870fd9e02cb98a24d692ec6484cbd3265397 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,12 @@
 
+2004-05-07  Rhys Weatherley  <rweather@southern-storm.com.au>
+
+       * dpas/dpas-function.c, dpas/dpas-internal.h, dpas/dpas-main.c,
+       dpas/dpas-parser.y, jit/jit-interp.cpp, jit/jit-interp.h,
+       jit/jit-opcode.c, jit/jit-rules-interp.c: execute the "main"
+       method once a Dynamic Pascal program has been compiled;
+       fix some calling convention problems with "call_external".
+
 2004-05-06  Rhys Weatherley  <rweather@southern-storm.com.au>
 
        * dpas/Makefile.am, dpas/dpas-builtin.c, dpas/dpas-function.c,
index 73f35772eb010c977e23b665d544e1e72e2ee537..f947e71c3c58200f794c5e0b53b5fa9be1da24a1 100644 (file)
@@ -23,6 +23,8 @@
 static jit_context_t current_context;
 static jit_function_t *function_stack = 0;
 static int function_stack_size = 0;
+static jit_function_t *main_list = 0;
+static int main_list_size = 0;
 
 jit_context_t dpas_current_context(void)
 {
@@ -103,3 +105,29 @@ dpas_semvalue dpas_lvalue_to_rvalue(dpas_semvalue value)
        }
        return value;
 }
+
+void dpas_add_main_function(jit_function_t func)
+{
+       main_list = (jit_function_t *)jit_realloc
+               (main_list, sizeof(jit_function_t) * (main_list_size + 1));
+       if(!main_list)
+       {
+               dpas_out_of_memory();
+       }
+       main_list[main_list_size++] = func;
+}
+
+int dpas_run_main_functions(void)
+{
+       int index;
+       for(index = 0; index < main_list_size; ++index)
+       {
+               if(!jit_function_apply(main_list[index], 0, 0))
+               {
+                       fprintf(stderr, "Exception 0x%lx thrown past top level\n",
+                                       (long)(jit_nint)(jit_exception_get_last()));
+                       return 0;
+               }
+       }
+       return 1;
+}
index c2e4757caafeed4fd73a02b3b80ec54a340b822e..c50c0351141e61e5ae0ce932e097998f85b33f83 100644 (file)
@@ -131,6 +131,18 @@ int dpas_is_builtin(const char *name);
 dpas_semvalue dpas_expand_builtin
        (int identifier, dpas_semvalue *args, int num_args);
 
+/*
+ * Add the current function to the execution list.  Used for
+ * "main" functions within each compiled module.
+ */
+void dpas_add_main_function(jit_function_t func);
+
+/*
+ * Run the "main" functions in the order in which they were compiled.
+ * Returns zero on an exception.
+ */
+int dpas_run_main_functions(void);
+
 #ifdef __cplusplus
 };
 #endif
index f10f495bcb3f43a093108506149aaa205ff17c84..737e35524edcb4d86c02e8fceeca847159af4d6a 100644 (file)
@@ -128,7 +128,14 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       /* TODO: JIT and execute the program */
+       /* Execute the program that we just compiled */
+       if(!dpas_dump_functions)
+       {
+               if(!dpas_run_main_functions())
+               {
+                       return 1;
+               }
+       }
 
        /* Done */
        return 0;
index 98837b2debf54ee2ade2c648eb43e007d39b263f..fdd749f2056f5ee4e8f6ac862c4c89aa841b03f5 100644 (file)
@@ -929,7 +929,7 @@ ImportDeclarations
        ;
 
 ProgramBlock
-       : ProgramBlock2         {
+       : Block         {
                                /* Get the "main" function for this module */
                                jit_function_t func = dpas_current_function();
 
@@ -957,16 +957,14 @@ ProgramBlock
                                        jit_dump_function(stdout, func, "main");
                                }
 
+                               /* Add the function to the "main" list, for later execution */
+                               dpas_add_main_function(func);
+
                                /* Pop the "main" function */
                                dpas_pop_function();
                        }
        ;
 
-ProgramBlock2
-       : Block
-       | K_END
-       ;
-
 /*
  * Identifiers and lists of identifiers.
  */
@@ -1173,6 +1171,7 @@ ProcedureOrFunctionList
 StatementPart
        : K_BEGIN StatementSequence OptSemi K_END
        | K_BEGIN OptSemi K_END
+       | K_END
        | K_BEGIN error K_END
        ;
 
index a754b0d8acf29a6f774f5515362fa1224aea24b6..8e463a01f7e4b5802acb46b085da252314eaf460 100644 (file)
@@ -4316,6 +4316,14 @@ void _jit_run_function(jit_function_interp *func, jit_item *args,
                }
                VMBREAK;
 
+               VMCASE(JIT_OP_PUSH_RETURN_AREA_PTR):
+               {
+                       /* Push the address of "return_area" for an external call */
+                       VM_STK_PTRP = return_area;
+                       VM_MODIFY_PC_AND_STACK(1, -1);
+               }
+               VMBREAK;
+
                /******************************************************************
                 * Stack management
                 ******************************************************************/
index 5b93dc1bf8d3ef6ba38f02873d551f1745c22898..dc401349e135f80e606dc6c77acb08700abc980e 100644 (file)
@@ -166,31 +166,32 @@ public:
 #define        JIT_OP_PUSH_RETURN_FLOAT64                      (JIT_OP_NUM_OPCODES + 0x002D)
 #define        JIT_OP_PUSH_RETURN_NFLOAT                       (JIT_OP_NUM_OPCODES + 0x002E)
 #define        JIT_OP_PUSH_RETURN_SMALL_STRUCT         (JIT_OP_NUM_OPCODES + 0x002F)
+#define        JIT_OP_PUSH_RETURN_AREA_PTR                     (JIT_OP_NUM_OPCODES + 0x0030)
 
 /*
  * Nested function call handling.
  */
-#define        JIT_OP_PUSH_PARENT_LOCALS                       (JIT_OP_NUM_OPCODES + 0x0030)
-#define        JIT_OP_PUSH_PARENT_ARGS                         (JIT_OP_NUM_OPCODES + 0x0031)
+#define        JIT_OP_PUSH_PARENT_LOCALS                       (JIT_OP_NUM_OPCODES + 0x0031)
+#define        JIT_OP_PUSH_PARENT_ARGS                         (JIT_OP_NUM_OPCODES + 0x0032)
 
 /*
  * Push constant values onto the stack.
  */
-#define        JIT_OP_PUSH_CONST_INT                           (JIT_OP_NUM_OPCODES + 0x0032)
-#define        JIT_OP_PUSH_CONST_LONG                          (JIT_OP_NUM_OPCODES + 0x0033)
-#define        JIT_OP_PUSH_CONST_FLOAT32                       (JIT_OP_NUM_OPCODES + 0x0034)
-#define        JIT_OP_PUSH_CONST_FLOAT64                       (JIT_OP_NUM_OPCODES + 0x0035)
-#define        JIT_OP_PUSH_CONST_NFLOAT                        (JIT_OP_NUM_OPCODES + 0x0036)
+#define        JIT_OP_PUSH_CONST_INT                           (JIT_OP_NUM_OPCODES + 0x0033)
+#define        JIT_OP_PUSH_CONST_LONG                          (JIT_OP_NUM_OPCODES + 0x0034)
+#define        JIT_OP_PUSH_CONST_FLOAT32                       (JIT_OP_NUM_OPCODES + 0x0035)
+#define        JIT_OP_PUSH_CONST_FLOAT64                       (JIT_OP_NUM_OPCODES + 0x0036)
+#define        JIT_OP_PUSH_CONST_NFLOAT                        (JIT_OP_NUM_OPCODES + 0x0037)
 
 /*
  * Exception handling (interpreter-only).
  */
-#define        JIT_OP_CALL_FINALLY                                     (JIT_OP_NUM_OPCODES + 0x0037)
+#define        JIT_OP_CALL_FINALLY                                     (JIT_OP_NUM_OPCODES + 0x0038)
 
 /*
  * Marker opcode for the end of a function.
  */
-#define        JIT_OP_END_MARKER                                       (JIT_OP_NUM_OPCODES + 0x0038)
+#define        JIT_OP_END_MARKER                                       (JIT_OP_NUM_OPCODES + 0x0039)
 
 /*
  * Number of interpreter-specific opcodes.
index 7086ac453debbf294e05721d74d4cb9dfb99b4b9..e3a8d432dea78c974155b7db2bff508d04a44f7e 100644 (file)
@@ -583,6 +583,7 @@ jit_opcode_info_t const _jit_interp_opcodes[JIT_OP_NUM_INTERP_OPCODES] = {
        {"push_return_float64",                 0},
        {"push_return_nfloat",                  0},
        {"push_return_small_struct",    JIT_OPCODE_NINT_ARG},
+       {"push_return_area_ptr",                0},
 
        /*
         * Nested function call handling.
index 74aaa9f2ab53a8217485a0c769a9a7bbdcd987ec..2d80cb59ad65c77aa8124f20f29dd441a46ed62c 100644 (file)
@@ -1037,6 +1037,11 @@ void _jit_gen_insn(jit_gencode_t gen, jit_function_t func,
                case JIT_OP_CALL_EXTERNAL:
                {
                        /* Call a native function, whose pointer is supplied explicitly */
+                       if(!jit_type_return_via_pointer
+                                       (jit_type_get_return((jit_type_t)(insn->value2))))
+                       {
+                               jit_cache_opcode(&(gen->posn), JIT_OP_PUSH_RETURN_AREA_PTR);
+                       }
                        jit_cache_opcode(&(gen->posn), insn->opcode);
                        jit_cache_native(&(gen->posn), (jit_nint)(insn->value2));
                        jit_cache_native(&(gen->posn), (jit_nint)(insn->dest));