summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gen.h2
-rw-r--r--gen_x86.c30
-rw-r--r--m68k_core_x86.c27
-rw-r--r--z80_to_x86.c31
4 files changed, 38 insertions, 52 deletions
diff --git a/gen.h b/gen.h
index e6ed928..5087b57 100644
--- a/gen.h
+++ b/gen.h
@@ -27,5 +27,7 @@ void jmp_r(code_info *code, uint8_t dst);
void call_args(code_info *code, code_ptr fun, uint32_t num_args, ...);
//like the above, but follows other aspects of the ABI like stack alignment
void call_args_abi(code_info *code, code_ptr fun, uint32_t num_args, ...);
+void save_callee_save_regs(code_info *code);
+void restore_callee_save_regs(code_info *code);
#endif //GEN_H_
diff --git a/gen_x86.c b/gen_x86.c
index b0e209c..1af0061 100644
--- a/gen_x86.c
+++ b/gen_x86.c
@@ -2039,3 +2039,33 @@ void call_args_abi(code_info *code, code_ptr fun, uint32_t num_args, ...)
*no_adjust_rsp = code->cur - (no_adjust_rsp+1);
#endif
}
+
+void save_callee_save_regs(code_info *code)
+{
+ push_r(code, RBX);
+ push_r(code, RBP);
+#ifdef X86_64
+ push_r(code, R12);
+ push_r(code, R13);
+ push_r(code, R14);
+ push_r(code, R15);
+#else
+ push_r(code, RDI);
+ push_r(code, RSI);
+#endif
+}
+
+void restore_callee_save_regs(code_info *code)
+{
+#ifdef X86_64
+ pop_r(code, R15);
+ pop_r(code, R14);
+ pop_r(code, R13);
+ pop_r(code, R12);
+#else
+ pop_r(code, RSI);
+ pop_r(code, RDI);
+#endif
+ pop_r(code, RBP);
+ pop_r(code, RBX);
+}
diff --git a/m68k_core_x86.c b/m68k_core_x86.c
index aed617f..44f8c3e 100644
--- a/m68k_core_x86.c
+++ b/m68k_core_x86.c
@@ -2281,42 +2281,19 @@ void init_m68k_opts(m68k_options * opts, memmap_chunk * memmap, uint32_t num_chu
retn(code);
opts->start_context = (start_fun)code->cur;
+ save_callee_save_regs(code);
#ifdef X86_64
if (opts->gen.scratch2 != RDI) {
mov_rr(code, RDI, opts->gen.scratch2, SZ_PTR);
}
- //save callee save registers
- push_r(code, RBP);
- push_r(code, R12);
- push_r(code, R13);
- push_r(code, R14);
- push_r(code, R15);
#else
- //save callee save registers
- push_r(code, RBP);
- push_r(code, RBX);
- push_r(code, RSI);
- push_r(code, RDI);
-
mov_rdispr(code, RSP, 20, opts->gen.scratch2, SZ_D);
mov_rdispr(code, RSP, 24, opts->gen.context_reg, SZ_D);
#endif
call(code, opts->gen.load_context);
call_r(code, opts->gen.scratch2);
call(code, opts->gen.save_context);
-#ifdef X86_64
- //restore callee save registers
- pop_r(code, R15);
- pop_r(code, R14);
- pop_r(code, R13);
- pop_r(code, R12);
- pop_r(code, RBP);
-#else
- pop_r(code, RDI);
- pop_r(code, RSI);
- pop_r(code, RBX);
- pop_r(code, RBP);
-#endif
+ restore_callee_save_regs(code);
retn(code);
opts->native_addr = code->cur;
diff --git a/z80_to_x86.c b/z80_to_x86.c
index 83237ca..aac1ceb 100644
--- a/z80_to_x86.c
+++ b/z80_to_x86.c
@@ -2052,13 +2052,7 @@ void init_z80_opts(z80_options * options, memmap_chunk const * chunks, uint32_t
sub_ir(code, 5, RAX, SZ_PTR); //adjust return address to point to the call that got us here
mov_rrdisp(code, RBX, options->gen.context_reg, offsetof(z80_context, extra_pc), SZ_PTR);
mov_rrind(code, RAX, options->gen.context_reg, SZ_PTR);
- //restore callee saved registers
- pop_r(code, R15);
- pop_r(code, R14);
- pop_r(code, R13);
- pop_r(code, R12);
- pop_r(code, RBP);
- pop_r(code, RBX);
+ restore_callee_save_regs(code);
*no_sync = code->cur - (no_sync + 1);
//return to caller of z80_run
retn(code);
@@ -2104,13 +2098,7 @@ void init_z80_opts(z80_options * options, memmap_chunk const * chunks, uint32_t
mov_ir(code, 0x38, options->gen.scratch1, SZ_W);
call(code, options->native_addr);
mov_rrind(code, options->gen.scratch1, options->gen.context_reg, SZ_PTR);
- //restore callee saved registers
- pop_r(code, R15);
- pop_r(code, R14);
- pop_r(code, R13);
- pop_r(code, R12);
- pop_r(code, RBP);
- pop_r(code, RBX);
+ restore_callee_save_regs(code);
//return to caller of z80_run to sync
retn(code);
*skip_int = code->cur - (skip_int+1);
@@ -2121,12 +2109,7 @@ void init_z80_opts(z80_options * options, memmap_chunk const * chunks, uint32_t
call(code, options->gen.save_context);
pop_rind(code, options->gen.context_reg);
//restore callee saved registers
- pop_r(code, R15);
- pop_r(code, R14);
- pop_r(code, R13);
- pop_r(code, R12);
- pop_r(code, RBP);
- pop_r(code, RBX);
+ restore_callee_save_regs(code);
//return to caller of z80_run
*skip_sync = code->cur - (skip_sync+1);
retn(code);
@@ -2207,13 +2190,7 @@ void init_z80_opts(z80_options * options, memmap_chunk const * chunks, uint32_t
jmp_r(code, options->gen.scratch1);
options->run = (z80_run_fun)code->cur;
- //save callee save registers
- push_r(code, RBX);
- push_r(code, RBP);
- push_r(code, R12);
- push_r(code, R13);
- push_r(code, R14);
- push_r(code, R15);
+ save_callee_save_regs(code);
mov_rr(code, RDI, options->gen.context_reg, SZ_PTR);
call(code, options->load_context_scratch);
cmp_irdisp(code, 0, options->gen.context_reg, offsetof(z80_context, extra_pc), SZ_PTR);