Skip to contentSkip to conversation

[4406] fixed global variable handling with qemu load/ stores - initial global prologue/epilogue implementation

Qemu-devel Fabrice Bellard 2 participants 4 messages May 10, 2008

lore
Share & tools
lore
#0 lore raw

Revision: 4406

          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=4406
Author:   bellard
Date:     2008-05-10 10:52:05 +0000 (Sat, 10 May 2008)

Log Message:
-----------
fixed global variable handling with qemu load/stores - initial global prologue/epilogue implementation

Modified Paths:
--------------

    trunk/tcg/i386/tcg-target.c
    trunk/tcg/tcg.c
    trunk/tcg/tcg.h
    trunk/tcg/x86_64/tcg-target.c

Modified: trunk/tcg/i386/tcg-target.c
===================================================================

tcg/i386/tcg-target.c +51 -1
@@ -46,6 +46,8 @@
 const int tcg_target_call_iarg_regs[3] = { TCG_REG_EAX, TCG_REG_EDX, TCG_REG_ECX };
 const int tcg_target_call_oarg_regs[2] = { TCG_REG_EAX, TCG_REG_EDX };
 
+static uint8_t *tb_ret_addr;
+
 static void patch_reloc(uint8_t *code_ptr, int type, 
                         tcg_target_long value, tcg_target_long addend)
 {
@@ -879,7 +881,8 @@
     switch(opc) {
     case INDEX_op_exit_tb:
         tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_EAX, args[0]);
-        tcg_out8(s, 0xc3); /* ret */
+        tcg_out8(s, 0xe9); /* jmp tb_ret_addr */
+        tcg_out32(s, tb_ret_addr - s->code_ptr - 4);
         break;
     case INDEX_op_goto_tb:
         if (s->tb_jmp_offset) {
@@ -1145,6 +1148,53 @@
     { -1 },
 };
 
+static int tcg_target_callee_save_regs[] = {
+    /*    TCG_REG_EBP, */ /* currently used for the global env, so no
+                             need to save */
+    TCG_REG_EBX,
+    TCG_REG_ESI,
+    TCG_REG_EDI,
+};
+
+static inline void tcg_out_push(TCGContext *s, int reg)
+{
+    tcg_out_opc(s, 0x50 + reg);
+}
+
+static inline void tcg_out_pop(TCGContext *s, int reg)
+{
+    tcg_out_opc(s, 0x58 + reg);
+}
+
+/* Generate global QEMU prologue and epilogue code */
+void tcg_target_qemu_prologue(TCGContext *s)
+{
+    int i, frame_size, push_size, stack_addend;
+    
+    /* TB prologue */
+    /* save all callee saved registers */
+    for(i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
+        tcg_out_push(s, tcg_target_callee_save_regs[i]);
+    }
+    /* reserve some stack space */
+    push_size = 4 + ARRAY_SIZE(tcg_target_callee_save_regs) * 4;
+    frame_size = push_size + TCG_STATIC_CALL_ARGS_SIZE;
+    frame_size = (frame_size + TCG_TARGET_STACK_ALIGN - 1) & 
+        ~(TCG_TARGET_STACK_ALIGN - 1);
+    stack_addend = frame_size - push_size;
+    tcg_out_addi(s, TCG_REG_ESP, -stack_addend);
+
+    tcg_out_modrm(s, 0xff, 4, TCG_REG_EAX); /* jmp *%eax */
+    
+    /* TB epilogue */
+    tb_ret_addr = s->code_ptr;
+    tcg_out_addi(s, TCG_REG_ESP, stack_addend);
+    for(i = ARRAY_SIZE(tcg_target_callee_save_regs) - 1; i >= 0; i--) {
+        tcg_out_pop(s, tcg_target_callee_save_regs[i]);
+    }
+    tcg_out8(s, 0xc3); /* ret */
+}
+
 void tcg_target_init(TCGContext *s)
 {
     /* fail safe */

Modified: trunk/tcg/tcg.c
===================================================================

tcg/tcg.c +95 -54

241 lines not on this page · raw message

Modified: trunk/tcg/tcg.h
===================================================================

tcg/tcg.h +13 -2
@@ -90,6 +90,10 @@
 
 #define TCG_MAX_TEMPS 512
 
+/* when the size of the arguments of a called function is smaller than
+   this value, they are statically allocated in the TB stack frame */
+#define TCG_STATIC_CALL_ARGS_SIZE 128
+
 typedef int TCGType;
 
 #define TCG_TYPE_I32 0
@@ -285,8 +289,11 @@
 
 #define TCG_OPF_BB_END     0x01 /* instruction defines the end of a basic
                                    block */
-#define TCG_OPF_CALL_CLOBBER 0x02 /* instruction clobbers call registers */
-#define TCG_OPF_SIDE_EFFECTS 0x04 /* instruction has side effects */
+#define TCG_OPF_CALL_CLOBBER 0x02 /* instruction clobbers call registers 
+                                   and potentially update globals. */
+#define TCG_OPF_SIDE_EFFECTS 0x04 /* instruction has side effects : it
+                                     cannot be removed if its output
+                                     are not used */
 
 typedef struct TCGOpDef {
     const char *name;
@@ -305,6 +312,7 @@
 extern TCGOpDef tcg_op_defs[];
 
 void tcg_target_init(TCGContext *s);
+void tcg_target_qemu_prologue(TCGContext *s);
 
 #define tcg_abort() \
 do {\
@@ -358,3 +366,6 @@
 int64_t tcg_helper_rem_i64(int64_t arg1, int64_t arg2);
 uint64_t tcg_helper_divu_i64(uint64_t arg1, uint64_t arg2);
 uint64_t tcg_helper_remu_i64(uint64_t arg1, uint64_t arg2);
+
+extern uint8_t code_gen_prologue[];
+#define tcg_qemu_tb_exec(tb_ptr) ((long REGPARM (*)(void *))code_gen_prologue)(tb_ptr)

Modified: trunk/tcg/x86_64/tcg-target.c
===================================================================

tcg/x86_64/tcg-target.c +60 -5

95 lines not on this page · raw message

#1 lore raw

tcg/tcg.c line 1530

Quoted 3 lines · Fabrice Bellard
tcg/tcg.c +0 -3
-#ifdef HOST_HPPA
-    func_addr = (tcg_target_long)__canonicalize_funcptr_for_compare((void *)func_addr);
-#endif

This was needed on HPPA (and possibly other archs), because a function pointer can hold either the function's address, or that of a function description (plabel), but we need the address of the function itself.

How would you prefer this to be handled? Would a 'tcg_const_funcptr' macro added to tcg.h and used in tcg_gen_helper_*_*() be okay?

Signature · 1 line
Stuart Brady
#2 lore raw
Quoted 7 lines · Stuart Brady

tcg/tcg.c line 1530

Quoted 3 lines · Fabrice Bellard
tcg/tcg.c +0 -3
-#ifdef HOST_HPPA
-    func_addr = (tcg_target_long)__canonicalize_funcptr_for_compare((void *)func_addr);
-#endif

How would you prefer this to be handled?

Ouch -- I've just noticed that the right place to handle this was tcg_out_call()... which in fact already does it!

I obviously meant to remove the __canonicalize_funcptr_for_compare from tcg.c before I even submitted the code. Thanks for fixing it.

Another change I made was adding the 'func_addr' variable, and using it in place of ts->val. I can submit a patch to revert that change if you would like.

Cheers,

Signature · 1 line
Stuart Brady
#3 lore raw
Quoted 11 lines · Stuart Brady
Quoted 6 lines · Stuart Brady

tcg/tcg.c line 1530

Quoted 3 lines · Fabrice Bellard
tcg/tcg.c +0 -3
-#ifdef HOST_HPPA
-    func_addr = (tcg_target_long)__canonicalize_funcptr_for_compare((void *)func_addr);
-#endif

How would you prefer this to be handled?

Ouch -- I've just noticed that the right place to handle this was tcg_out_call()... which in fact already does it!

Quoted 6 lines

I obviously meant to remove the __canonicalize_funcptr_for_compare from tcg.c before I even submitted the code. Thanks for fixing it.

Another change I made was adding the 'func_addr' variable, and using it in place of ts->val. I can submit a patch to revert that change if you would like.

An important note: the only "officially" supported hosts for TCG are x86 and x86_64. I prefer not to support other hosts at this point because TCG is evolving. However I accept that code is commited for them provided it does not make heavy change in the TCG core.

In order to test a TCG target support, it is important to use a fully converted target such as SPARC. Testing it on non fully converted targets such as x86 only tests a small fraction of the code.

Regards,

Fabrice.