fix some calling convention problems with "call_external".
+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,
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)
{
}
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;
+}
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
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;
;
ProgramBlock
- : ProgramBlock2 {
+ : Block {
/* Get the "main" function for this module */
jit_function_t func = dpas_current_function();
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.
*/
StatementPart
: K_BEGIN StatementSequence OptSemi K_END
| K_BEGIN OptSemi K_END
+ | K_END
| K_BEGIN error K_END
;
}
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
******************************************************************/
#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.
{"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.
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));