// Disassembles one command from the heap, returns address of next command or 0 if a ret was encountered. reg_t disassemble(EngineState *s, reg32_t pos, reg_t objAddr, bool printBWTag, bool printBytecode) { SegmentObj *mobj = s->_segMan->getSegment(pos.getSegment(), SEG_TYPE_SCRIPT); Script *script_entity = NULL; reg_t retval; retval.setSegment(pos.getSegment()); retval.setOffset(pos.getOffset() + 1); uint16 param_value = 0xffff; // Suppress GCC warning by setting default value, chose value as invalid to getKernelName etc. uint i = 0; Kernel *kernel = g_sci->getKernel(); if (!mobj) { warning("Disassembly failed: Segment %04x non-existent or not a script", pos.getSegment()); return retval; } else script_entity = (Script *)mobj; uint scr_size = script_entity->getBufSize(); if (pos.getOffset() >= scr_size) { warning("Trying to disassemble beyond end of script"); return NULL_REG; } const byte *scr = script_entity->getBuf(); int16 opparams[4]; byte opsize; uint bytecount = readPMachineInstruction(scr + pos.getOffset(), opsize, opparams); const byte opcode = opsize >> 1; debugN("%04x:%04x: ", PRINT_REG(pos)); if (printBytecode) { if (pos.getOffset() + bytecount > scr_size) { warning("Operation arguments extend beyond end of script"); return retval; } for (i = 0; i < bytecount; i++) debugN("%02x ", scr[pos.getOffset() + i]); for (i = bytecount; i < 5; i++) debugN(" "); } opsize &= 1; // byte if true, word if false if (printBWTag) debugN("[%c] ", opsize ? 'B' : 'W'); if (opcode == op_pushSelf && opsize && g_sci->getGameId() != GID_FANMADE) { // 0x3e (62) // Debug opcode op_file debugN("file \"%s\"\n", scr + pos.getOffset() + 1); // +1: op_pushSelf size retval.incOffset(bytecount - 1); return retval; } #ifndef REDUCE_MEMORY_USAGE debugN("%-5s", opcodeNames[opcode]); #endif static const char *defaultSeparator = "\t\t; "; i = 0; while (g_sci->_opcode_formats[opcode][i]) { switch (g_sci->_opcode_formats[opcode][i++]) { case Script_Invalid: warning("-Invalid operation-"); break; case Script_SByte: case Script_Byte: param_value = scr[retval.getOffset()]; debugN("\t%02x", param_value); if (param_value > 9) { debugN("%s%u", defaultSeparator, param_value); } retval.incOffset(1); break; case Script_Word: case Script_SWord: param_value = READ_SCI11ENDIAN_UINT16(&scr[retval.getOffset()]); debugN("\t%04x", param_value); if (param_value > 9) { debugN("%s%u", defaultSeparator, param_value); } retval.incOffset(2); break; case Script_SVariable: case Script_Variable: case Script_Property: case Script_Global: case Script_Local: case Script_Temp: case Script_Param: if (opsize) { param_value = scr[retval.getOffset()]; retval.incOffset(1); } else { param_value = READ_SCI11ENDIAN_UINT16(&scr[retval.getOffset()]); retval.incOffset(2); } if (opcode == op_callk) { debugN("\t%s[%x],", (param_value < kernel->_kernelFuncs.size()) ? ((param_value < kernel->getKernelNamesSize()) ? kernel->getKernelName(param_value).c_str() : "[Unknown(postulated)]") : "<invalid>", param_value); } else if (opcode == op_class) { const reg_t classAddr = s->_segMan->getClassAddress(param_value, SCRIPT_GET_DONT_LOAD, retval.getSegment()); if (!classAddr.isNull()) { debugN("\t%s", s->_segMan->getObjectName(classAddr)); debugN(opsize ? "[%02x]" : "[%04x]", param_value); } else { debugN(opsize ? "\t%02x" : "\t%04x", param_value); } } else if (opcode == op_super) { Object *obj; if (objAddr != NULL_REG && (obj = s->_segMan->getObject(objAddr)) != nullptr) { debugN("\t%s", s->_segMan->getObjectName(obj->getSuperClassSelector())); debugN(opsize ? "[%02x]" : "[%04x]", param_value); } else { debugN(opsize ? "\t%02x" : "\t%04x", param_value); } debugN(","); #ifdef ENABLE_SCI32 } else if (getSciVersion() == SCI_VERSION_3 && ( opcode == op_pToa || opcode == op_aTop || opcode == op_pTos || opcode == op_sTop || opcode == op_ipToa || opcode == op_dpToa || opcode == op_ipTos || opcode == op_dpTos)) { const char *selectorName = "<invalid>"; if (param_value < kernel->getSelectorNamesSize()) { selectorName = kernel->getSelectorName(param_value).c_str(); } debugN("\t%s[%x]", selectorName, param_value); #endif } else { const char *separator = defaultSeparator; debugN(opsize ? "\t%02x" : "\t%04x", param_value); if (param_value > 9) { debugN("%s%u", separator, param_value); separator = ", "; } if (param_value >= 0x20 && param_value <= 0x7e) { debugN("%s'%c'", separator, param_value); separator = ", "; } if (opcode == op_pushi && param_value < kernel->getSelectorNamesSize()) { debugN("%s%s", separator, kernel->getSelectorName(param_value).c_str()); } } break; case Script_Offset: { assert(opcode == op_lofsa || opcode == op_lofss); if (opsize) { param_value = scr[retval.getOffset()]; retval.incOffset(1); } else { param_value = READ_SCI11ENDIAN_UINT16(&scr[retval.getOffset()]); retval.incOffset(2); } const uint32 offset = findOffset(param_value, script_entity, retval.getOffset()); reg_t addr; addr.setSegment(retval.getSegment()); addr.setOffset(offset); debugN("\t%s", s->_segMan->getObjectName(addr)); debugN(opsize ? "[%02x]" : "[%04x]", offset); break; } case Script_SRelative: if (opsize) { int8 offset = (int8)scr[retval.getOffset()]; retval.incOffset(1); debugN("\t%02x [%04x]", 0xff & offset, kOffsetMask & (retval.getOffset() + offset)); } else { int16 offset = (int16)READ_SCI11ENDIAN_UINT16(&scr[retval.getOffset()]); retval.incOffset(2); debugN("\t%04x [%04x]", 0xffff & offset, kOffsetMask & (retval.getOffset() + offset)); } break; case Script_End: retval = NULL_REG; break; default: error("Internal assertion failed in disassemble()"); } } if (pos == s->xs->addr.pc) { // Extra information if debugging the current opcode if ((opcode == op_pTos) || (opcode == op_sTop) || (opcode == op_pToa) || (opcode == op_aTop) || (opcode == op_dpToa) || (opcode == op_ipToa) || (opcode == op_dpTos) || (opcode == op_ipTos)) { const Object *obj = s->_segMan->getObject(s->xs->objp); if (!obj) { warning("Attempted to reference on non-object at %04x:%04x", PRINT_REG(s->xs->objp)); } else { if (getSciVersion() == SCI_VERSION_3) debugN("\t(%s)", g_sci->getKernel()->getSelectorName(param_value).c_str()); else debugN("\t(%s)", g_sci->getKernel()->getSelectorName(obj->propertyOffsetToId(s->_segMan, param_value)).c_str()); } } } debugN("\n"); if (pos == s->xs->addr.pc) { // Extra information if debugging the current opcode if (opcode == op_callk) { int stackframe = (scr[pos.getOffset() + 2] >> 1) + (s->r_rest); int argc = ((s->xs->sp)[- stackframe - 1]).getOffset(); bool oldScriptHeader = (getSciVersion() == SCI_VERSION_0_EARLY); if (!oldScriptHeader) argc += (s->r_rest); debugN(" Kernel params: ("); for (int j = 0; j < argc; j++) { debugN("%04x:%04x", PRINT_REG((s->xs->sp)[j - stackframe])); if (j + 1 < argc) debugN(", "); } debugN(")\n"); } else if ((opcode == op_send) || (opcode == op_self)) {