Path: blob/master/libmupen64plus/mupen64plus-core/src/r4300/x86/assemble.c
2 views
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *1* Mupen64plus - assemble.c *2* Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *3* Copyright (C) 2002 Hacktarux *4* *5* This program is free software; you can redistribute it and/or modify *6* it under the terms of the GNU General Public License as published by *7* the Free Software Foundation; either version 2 of the License, or *8* (at your option) any later version. *9* *10* This program is distributed in the hope that it will be useful, *11* but WITHOUT ANY WARRANTY; without even the implied warranty of *12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *13* GNU General Public License for more details. *14* *15* You should have received a copy of the GNU General Public License *16* along with this program; if not, write to the *17* Free Software Foundation, Inc., *18* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *19* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */2021#include <stdlib.h>22#include <stdio.h>2324#include "assemble.h"2526#include "api/m64p_types.h"27#include "api/callbacks.h"28#include "osal/preproc.h"29#include "r4300/recomph.h"30#include "r4300/recomp.h"31#include "r4300/r4300.h"3233typedef struct _jump_table34{35unsigned int mi_addr;36unsigned int pc_addr;37} jump_table;3839static jump_table *jumps_table = NULL;40static int jumps_number, max_jumps_number;4142void init_assembler(void *block_jumps_table, int block_jumps_number, void *block_riprel_table, int block_riprel_number)43{44if (block_jumps_table)45{46jumps_table = (jump_table *) block_jumps_table;47jumps_number = block_jumps_number;48max_jumps_number = jumps_number;49}50else51{52jumps_table = (jump_table *) malloc(1000*sizeof(jump_table));53jumps_number = 0;54max_jumps_number = 1000;55}56}5758void free_assembler(void **block_jumps_table, int *block_jumps_number, void **block_riprel_table, int *block_riprel_number)59{60*block_jumps_table = jumps_table;61*block_jumps_number = jumps_number;62*block_riprel_table = NULL; /* RIP-relative addressing is only for x86-64 */63*block_riprel_number = 0;64}6566void add_jump(unsigned int pc_addr, unsigned int mi_addr)67{68if (jumps_number == max_jumps_number)69{70max_jumps_number += 1000;71jumps_table = (jump_table *) realloc(jumps_table, max_jumps_number*sizeof(jump_table));72}73jumps_table[jumps_number].pc_addr = pc_addr;74jumps_table[jumps_number].mi_addr = mi_addr;75jumps_number++;76}7778void passe2(precomp_instr *dest, int start, int end, precomp_block *block)79{80unsigned int real_code_length, addr_dest;81int i;82build_wrappers(dest, start, end, block);83real_code_length = code_length;8485for (i=0; i < jumps_number; i++)86{87code_length = jumps_table[i].pc_addr;88if (dest[(jumps_table[i].mi_addr - dest[0].addr)/4].reg_cache_infos.need_map)89{90addr_dest = (unsigned int)dest[(jumps_table[i].mi_addr - dest[0].addr)/4].reg_cache_infos.jump_wrapper;91put32(addr_dest-((unsigned int)block->code+code_length)-4);92}93else94{95addr_dest = dest[(jumps_table[i].mi_addr - dest[0].addr)/4].local_addr;96put32(addr_dest-code_length-4);97}98}99code_length = real_code_length;100}101102static unsigned int g_jump_start8 = 0;103static unsigned int g_jump_start32 = 0;104105void jump_start_rel8(void)106{107g_jump_start8 = code_length;108}109110void jump_start_rel32(void)111{112g_jump_start32 = code_length;113}114115void jump_end_rel8(void)116{117unsigned int jump_end = code_length;118int jump_vec = jump_end - g_jump_start8;119120if (jump_vec > 127 || jump_vec < -128)121{122DebugMessage(M64MSG_ERROR, "8-bit relative jump too long! From %x to %x", g_jump_start8, jump_end);123OSAL_BREAKPOINT_INTERRUPT;124}125126code_length = g_jump_start8 - 1;127put8(jump_vec);128code_length = jump_end;129}130131void jump_end_rel32(void)132{133unsigned int jump_end = code_length;134int jump_vec = jump_end - g_jump_start32;135136code_length = g_jump_start32 - 4;137put32(jump_vec);138code_length = jump_end;139}140141142