From 0e9ffc7c78f6ef76f2673d02a16dbc8573ec33be Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Thu, 6 May 2004 22:41:15 +0000 Subject: [PATCH] Execute the "main" method once a Dynamic Pascal program has been compiled; fix some calling convention problems with "call_external". --- ChangeLog | 8 ++++++++ dpas/dpas-function.c | 28 ++++++++++++++++++++++++++++ dpas/dpas-internal.h | 12 ++++++++++++ dpas/dpas-main.c | 9 ++++++++- dpas/dpas-parser.y | 11 +++++------ jit/jit-interp.cpp | 8 ++++++++ jit/jit-interp.h | 19 ++++++++++--------- jit/jit-opcode.c | 1 + jit/jit-rules-interp.c | 5 +++++ 9 files changed, 85 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index 723c495..1f8f870 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,12 @@ +2004-05-07 Rhys Weatherley + + * 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 * dpas/Makefile.am, dpas/dpas-builtin.c, dpas/dpas-function.c, diff --git a/dpas/dpas-function.c b/dpas/dpas-function.c index 73f3577..f947e71 100644 --- a/dpas/dpas-function.c +++ b/dpas/dpas-function.c @@ -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; +} diff --git a/dpas/dpas-internal.h b/dpas/dpas-internal.h index c2e4757..c50c035 100644 --- a/dpas/dpas-internal.h +++ b/dpas/dpas-internal.h @@ -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 diff --git a/dpas/dpas-main.c b/dpas/dpas-main.c index f10f495..737e355 100644 --- a/dpas/dpas-main.c +++ b/dpas/dpas-main.c @@ -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; diff --git a/dpas/dpas-parser.y b/dpas/dpas-parser.y index 98837b2..fdd749f 100644 --- a/dpas/dpas-parser.y +++ b/dpas/dpas-parser.y @@ -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 ; diff --git a/jit/jit-interp.cpp b/jit/jit-interp.cpp index a754b0d..8e463a0 100644 --- a/jit/jit-interp.cpp +++ b/jit/jit-interp.cpp @@ -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 ******************************************************************/ diff --git a/jit/jit-interp.h b/jit/jit-interp.h index 5b93dc1..dc40134 100644 --- a/jit/jit-interp.h +++ b/jit/jit-interp.h @@ -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. diff --git a/jit/jit-opcode.c b/jit/jit-opcode.c index 7086ac4..e3a8d43 100644 --- a/jit/jit-opcode.c +++ b/jit/jit-opcode.c @@ -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. diff --git a/jit/jit-rules-interp.c b/jit/jit-rules-interp.c index 74aaa9f..2d80cb5 100644 --- a/jit/jit-rules-interp.c +++ b/jit/jit-rules-interp.c @@ -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)); -- 2.47.3