1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/*
* ucdisasm.cc - Disassembled usecode trace
*
* Copyright (C) 1999 Jeffrey S. Freedman
* Copyright (C) 2000-2022 The Exult Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "game.h"
#include "ignore_unused_variable_warning.h"
#include "span.h"
#include "stackframe.h"
#include "ucfunction.h"
#include "ucinternal.h"
#include "uctools.h"
#include "useval.h"
#include <cstdio>
#include <cstring>
#include <iostream>
using std::cout;
using std::strlen;
int Usecode_internal::get_opcode_length(int opcode) {
if (opcode >= 0 && static_cast<unsigned>(opcode) < opcode_table.size()) {
return opcode_table[opcode].nbytes + 1;
}
return 0;
}
void Usecode_internal::uc_trace_disasm(Stack_frame* frame) {
uc_trace_disasm(
frame->locals, frame->num_args + frame->num_vars,
frame->function->statics, frame->data, frame->externs, frame->code,
frame->ip);
}
void Usecode_internal::uc_trace_disasm(
Usecode_value* locals, int num_locals,
std::vector<Usecode_value>& locstatics, const uint8* data,
const uint8* externals, const uint8* code, const uint8* ip) {
ignore_unused_variable_warning(num_locals);
const int func_ip = static_cast<int>(ip - code);
const int opcode = Read1(ip);
// const uint8 *param_ip = ip;
const opcode_desc* pdesc = nullptr;
if (opcode >= 0 && static_cast<unsigned>(opcode) < opcode_table.size()) {
pdesc = &(opcode_table[opcode]);
}
std::printf(" %04X: ", func_ip);
if (pdesc && pdesc->mnemonic) {
std::printf("%s", pdesc->mnemonic);
if (strlen(pdesc->mnemonic) < 4) {
std::printf("\t");
}
} else {
std::printf("<unknown>");
}
if (pdesc && pdesc->nbytes > 0) {
uint16 varref;
signed short immed;
int offset;
switch (pdesc->type) {
case op_byte:
std::printf("\t%02x", Read1(ip));
break;
case op_immed:
// Print immediate operand
immed = little_endian::Read2(ip);
std::printf("\t%04hXH\t\t; %d", immed, immed);
break;
case op_immed32: {
const int immed32 = little_endian::Read4s(ip);
std::printf("\t%08XH\t\t; %d", immed32, immed32);
break;
}
case op_data_string:
case op_data_string32: {
const char* pstr;
size_t len;
// Print data string operand
if (pdesc->type == op_data_string) {
offset = little_endian::Read2(ip);
} else {
offset = little_endian::Read4s(ip);
}
pstr = reinterpret_cast<const char*>(data + offset);
len = strlen(pstr);
if (len > 20) {
len = 20 - 3;
}
std::printf("\tL%04X\t\t; ", offset);
for (unsigned i = 0; i < len; i++) {
std::printf("%c", pstr[i]);
}
if (len < strlen(pstr)) {
// String truncated
std::printf("...");
}
} break;
case op_relative_jump:
// Print jump desination
offset = little_endian::Read2(ip);
std::printf(
"\t%04X", (offset + func_ip + 1 + pdesc->nbytes) & 0xFFFF);
break;
case op_relative_jump32:
offset = little_endian::Read4s(ip);
std::printf("\t%04X", offset + func_ip + 1 + pdesc->nbytes);
break;
case op_sloop:
if (pdesc->nbytes == 11) {
ip++;
}
little_endian::Read2(ip);
little_endian::Read2(ip);
little_endian::Read2(ip);
varref = little_endian::Read2(ip);
offset = little_endian::Read2(ip);
std::printf(
"\t[%04X], %04X\t= ", varref,
(offset + func_ip + 1 + pdesc->nbytes) & 0xFFFF);
locals[varref].print(cout, true); // print value (short format)
break;
case op_sloop32:
if (pdesc->nbytes == 13) {
ip++;
}
little_endian::Read2(ip);
little_endian::Read2(ip);
little_endian::Read2(ip);
varref = little_endian::Read2(ip);
offset = little_endian::Read4s(ip);
std::printf(
"\t[%04X], %04X\t= ", varref,
offset + func_ip + 1 + pdesc->nbytes);
locals[varref].print(cout, true); // print value (short format)
break;
case op_immed_and_relative_jump:
immed = little_endian::Read2(ip);
offset = little_endian::Read2(ip);
std::printf(
"\t%04hXH, %04X", immed,
(offset + func_ip + 1 + pdesc->nbytes) & 0xFFFF);
break;
case op_immedreljump32:
immed = little_endian::Read2(ip);
offset = little_endian::Read4s(ip);
std::printf(
"\t%04hXH, %04X", immed,
offset + func_ip + 1 + pdesc->nbytes);
break;
case op_call: {
const unsigned short func = little_endian::Read2(ip);
immed = Read1(ip);
tcb::span<const std::string_view> func_table;
if (Game::get_game_type() == SERPENT_ISLE) {
if (Game::is_si_beta()) {
func_table = sibeta_intrinsic_table;
} else {
func_table = si_intrinsic_table;
}
} else {
func_table = bg_intrinsic_table;
}
std::printf(
"\t_%s@%d\t; %04X", func_table[func].data(), immed, func);
} break;
case op_extcall: {
// Print extern call
offset = little_endian::Read2(ip);
std::printf(
"\t[%04X]\t\t; %04XH", offset,
externals[2 * offset] + 256 * externals[2 * offset + 1]);
break;
}
case op_varref:
// Print variable reference
varref = little_endian::Read2(ip);
std::printf("\t[%04X]\t\t= ", varref);
locals[varref].print(cout, true); // print value (short)
break;
case op_flgref:
// Print global flag reference
offset = little_endian::Read2(ip);
std::printf("\tflag:[%04X]\t= ", offset);
if (gflags[offset]) {
std::printf("set");
} else {
std::printf("unset");
}
break;
case op_staticref: {
const sint16 staticref = little_endian::Read2(ip);
std::printf("[%04X]\t= ", static_cast<uint16>(staticref));
if (staticref < 0) {
// global
if (static_cast<unsigned>(-staticref) < statics.size()) {
statics[-staticref].print(cout, true);
} else {
std::printf("<out of range>");
}
} else {
if (static_cast<unsigned>(staticref) < locstatics.size()) {
locstatics[staticref].print(cout, true);
} else {
std::printf("<out of range>");
}
}
break;
}
default:
// Unknown type
break;
}
}
// special cases:
switch (opcode & 0x7f) {
case 0x05: { // JNE/JNE32.
Usecode_value val;
if (sp <= stack) {
val = Usecode_value(0);
} else {
val = *(sp - 1);
}
if (val.is_false()) {
std::printf("\t\t(jump taken)");
} else {
std::printf("\t\t(jump not taken)");
}
break;
}
/* maybe predict this?
case 0x07: // CMPS/CMPS32.
{
int cnt = little_endian::Read2(param_ip); // # strings.
bool matched = false;
// only try to match if we haven't found an answer yet
while (!matched && !found_answer && cnt-- > 0) {
Usecode_value s;
if (sp + cnt <= stack)
s = Usecode_value(0);
else
s = *(sp - cnt - 1);
const char *str = s.get_str_value();
if (str && strcmp(str, user_choice) == 0) {
matched = true;
found_answer = true;
}
}
if (val.is_false())
std::printf("\t\t(jump taken)");
else
std::printf("\t\t(jump not taken)");
break;
}
break;
*/
}
std::printf("\n");
}
|