1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - assemble.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2002 Hacktarux *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
27 #include "api/m64p_types.h"
28 #include "api/callbacks.h"
29 #include "osal/preproc.h"
30 #include "r4300/recomph.h"
31 #include "r4300/recomp.h"
32 #include "r4300/r4300.h"
34 typedef struct _jump_table
40 static jump_table *jumps_table = NULL;
41 static int jumps_number, max_jumps_number;
43 void init_assembler(void *block_jumps_table, int block_jumps_number, void *block_riprel_table, int block_riprel_number)
45 if (block_jumps_table)
47 jumps_table = (jump_table *) block_jumps_table;
48 jumps_number = block_jumps_number;
49 max_jumps_number = jumps_number;
53 jumps_table = (jump_table *) malloc(1000*sizeof(jump_table));
55 max_jumps_number = 1000;
59 void free_assembler(void **block_jumps_table, int *block_jumps_number, void **block_riprel_table, int *block_riprel_number)
61 *block_jumps_table = jumps_table;
62 *block_jumps_number = jumps_number;
63 *block_riprel_table = NULL; /* RIP-relative addressing is only for x86-64 */
64 *block_riprel_number = 0;
67 void add_jump(unsigned int pc_addr, unsigned int mi_addr)
69 if (jumps_number == max_jumps_number)
71 max_jumps_number += 1000;
72 jumps_table = (jump_table *) realloc(jumps_table, max_jumps_number*sizeof(jump_table));
74 jumps_table[jumps_number].pc_addr = pc_addr;
75 jumps_table[jumps_number].mi_addr = mi_addr;
79 void passe2(precomp_instr *dest, int start, int end, precomp_block *block)
81 unsigned int real_code_length, addr_dest;
83 build_wrappers(dest, start, end, block);
84 real_code_length = code_length;
86 for (i=0; i < jumps_number; i++)
88 code_length = jumps_table[i].pc_addr;
89 if (dest[(jumps_table[i].mi_addr - dest[0].addr)/4].reg_cache_infos.need_map)
91 addr_dest = (unsigned int)dest[(jumps_table[i].mi_addr - dest[0].addr)/4].reg_cache_infos.jump_wrapper;
92 put32(addr_dest-((unsigned int)block->code+code_length)-4);
96 addr_dest = dest[(jumps_table[i].mi_addr - dest[0].addr)/4].local_addr;
97 put32(addr_dest-code_length-4);
100 code_length = real_code_length;
103 static unsigned int g_jump_start8 = 0;
104 static unsigned int g_jump_start32 = 0;
106 void jump_start_rel8(void)
108 g_jump_start8 = code_length;
111 void jump_start_rel32(void)
113 g_jump_start32 = code_length;
116 void jump_end_rel8(void)
118 unsigned int jump_end = code_length;
119 int jump_vec = jump_end - g_jump_start8;
121 if (jump_vec > 127 || jump_vec < -128)
123 DebugMessage(M64MSG_ERROR, "8-bit relative jump too long! From %x to %x", g_jump_start8, jump_end);
124 OSAL_BREAKPOINT_INTERRUPT;
127 code_length = g_jump_start8 - 1;
129 code_length = jump_end;
132 void jump_end_rel32(void)
134 unsigned int jump_end = code_length;
135 int jump_vec = jump_end - g_jump_start32;
137 code_length = g_jump_start32 - 4;
139 code_length = jump_end;