2009-06-04 11:45:17 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* 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.
|
2014-02-18 02:34:24 +01:00
|
|
|
*
|
2009-06-04 11:45:17 +00:00
|
|
|
* 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.
|
2014-02-18 02:34:24 +01:00
|
|
|
*
|
2009-06-04 11:45:17 +00:00
|
|
|
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common/endian.h"
|
|
|
|
|
|
|
|
#include "sci/sci.h"
|
2010-11-17 11:15:52 +00:00
|
|
|
#include "sci/engine/kernel.h"
|
2010-05-19 08:50:24 +00:00
|
|
|
#include "sci/engine/features.h"
|
2010-11-19 08:18:24 +00:00
|
|
|
#include "sci/engine/object.h"
|
2010-05-30 21:49:07 +00:00
|
|
|
#include "sci/engine/script.h" // for SCI_OBJ_EXPORTS and SCI_OBJ_SYNONYMS
|
2009-09-17 13:22:46 +00:00
|
|
|
#include "sci/engine/segment.h"
|
2009-06-04 11:45:17 +00:00
|
|
|
#include "sci/engine/seg_manager.h"
|
|
|
|
#include "sci/engine/state.h"
|
|
|
|
|
|
|
|
namespace Sci {
|
|
|
|
|
2009-09-17 00:44:22 +00:00
|
|
|
//#define GC_DEBUG // Debug garbage collection
|
|
|
|
//#define GC_DEBUG_VERBOSE // Debug garbage verbosely
|
|
|
|
|
2009-09-17 00:45:12 +00:00
|
|
|
SegmentObj *SegmentObj::createSegmentObj(SegmentType type) {
|
|
|
|
SegmentObj *mem = 0;
|
2009-06-04 11:45:17 +00:00
|
|
|
switch (type) {
|
2009-09-17 00:45:12 +00:00
|
|
|
case SEG_TYPE_SCRIPT:
|
2009-06-04 11:45:17 +00:00
|
|
|
mem = new Script();
|
|
|
|
break;
|
2009-09-17 00:45:12 +00:00
|
|
|
case SEG_TYPE_CLONES:
|
2009-06-04 11:45:17 +00:00
|
|
|
mem = new CloneTable();
|
|
|
|
break;
|
2009-09-17 00:45:12 +00:00
|
|
|
case SEG_TYPE_LOCALS:
|
2009-06-04 11:45:17 +00:00
|
|
|
mem = new LocalVariables();
|
|
|
|
break;
|
2009-09-17 00:45:12 +00:00
|
|
|
case SEG_TYPE_STACK:
|
2009-06-04 11:45:17 +00:00
|
|
|
mem = new DataStack();
|
|
|
|
break;
|
2009-09-17 00:45:12 +00:00
|
|
|
case SEG_TYPE_HUNK:
|
2009-06-04 11:45:17 +00:00
|
|
|
mem = new HunkTable();
|
|
|
|
break;
|
2009-09-17 00:45:12 +00:00
|
|
|
case SEG_TYPE_LISTS:
|
2009-06-04 11:45:17 +00:00
|
|
|
mem = new ListTable();
|
|
|
|
break;
|
2009-09-17 00:45:12 +00:00
|
|
|
case SEG_TYPE_NODES:
|
2009-06-04 11:45:17 +00:00
|
|
|
mem = new NodeTable();
|
|
|
|
break;
|
2009-09-17 00:45:12 +00:00
|
|
|
case SEG_TYPE_DYNMEM:
|
2009-06-04 11:45:17 +00:00
|
|
|
mem = new DynMem();
|
|
|
|
break;
|
2009-12-21 14:32:54 +00:00
|
|
|
#ifdef ENABLE_SCI32
|
|
|
|
case SEG_TYPE_ARRAY:
|
|
|
|
mem = new ArrayTable();
|
|
|
|
break;
|
2016-07-29 15:48:14 -05:00
|
|
|
case SEG_TYPE_BITMAP:
|
|
|
|
mem = new BitmapTable();
|
|
|
|
break;
|
2009-12-21 14:32:54 +00:00
|
|
|
#endif
|
2009-06-04 11:45:17 +00:00
|
|
|
default:
|
2009-09-17 00:45:12 +00:00
|
|
|
error("Unknown SegmentObj type %d", type);
|
2009-06-04 11:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(mem);
|
2009-09-22 00:35:46 +00:00
|
|
|
assert(mem->_type == type);
|
2009-06-04 11:45:17 +00:00
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2009-09-22 00:36:24 +00:00
|
|
|
SegmentRef SegmentObj::dereference(reg_t pointer) {
|
2009-06-04 11:45:17 +00:00
|
|
|
error("Error: Trying to dereference pointer %04x:%04x to inappropriate segment",
|
|
|
|
PRINT_REG(pointer));
|
2009-09-22 00:36:24 +00:00
|
|
|
return SegmentRef();
|
2009-06-04 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------- clones --------------------
|
|
|
|
|
2010-06-28 11:22:41 +00:00
|
|
|
Common::Array<reg_t> CloneTable::listAllOutgoingReferences(reg_t addr) const {
|
|
|
|
Common::Array<reg_t> tmp;
|
2009-06-04 11:45:17 +00:00
|
|
|
// assert(addr.segment == _segId);
|
|
|
|
|
2012-06-18 05:21:59 +03:00
|
|
|
if (!isValidEntry(addr.getOffset())) {
|
2009-09-12 17:31:29 +00:00
|
|
|
error("Unexpected request for outgoing references from clone at %04x:%04x", PRINT_REG(addr));
|
2009-06-04 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 19:00:12 +01:00
|
|
|
const Clone *clone = &at(addr.getOffset());
|
2009-06-04 11:45:17 +00:00
|
|
|
|
|
|
|
// Emit all member variables (including references to the 'super' delegate)
|
2009-10-10 15:58:51 +00:00
|
|
|
for (uint i = 0; i < clone->getVarCount(); i++)
|
2010-06-28 11:22:41 +00:00
|
|
|
tmp.push_back(clone->getVariable(i));
|
2009-06-04 11:45:17 +00:00
|
|
|
|
|
|
|
// Note that this also includes the 'base' object, which is part of the script and therefore also emits the locals.
|
2010-06-28 11:22:41 +00:00
|
|
|
tmp.push_back(clone->getPos());
|
2011-01-01 12:48:12 +00:00
|
|
|
//debugC(kDebugLevelGC, "[GC] Reporting clone-pos %04x:%04x", PRINT_REG(clone->pos));
|
2010-06-28 11:22:41 +00:00
|
|
|
|
|
|
|
return tmp;
|
2009-06-04 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2009-09-06 12:57:42 +00:00
|
|
|
void CloneTable::freeAtAddress(SegManager *segMan, reg_t addr) {
|
2009-09-17 00:44:22 +00:00
|
|
|
#ifdef GC_DEBUG
|
2016-03-23 19:00:12 +01:00
|
|
|
Object *victim_obj = &at(addr.getOffset());
|
2009-06-04 11:45:17 +00:00
|
|
|
|
2009-09-21 21:35:43 +00:00
|
|
|
if (!(victim_obj->_flags & OBJECT_FLAG_FREED))
|
2009-07-06 10:39:22 +00:00
|
|
|
warning("[GC] Clone %04x:%04x not reachable and not freed (freeing now)", PRINT_REG(addr));
|
2009-06-04 11:45:17 +00:00
|
|
|
#ifdef GC_DEBUG_VERBOSE
|
|
|
|
else
|
2009-07-06 10:39:22 +00:00
|
|
|
warning("[GC-DEBUG] Clone %04x:%04x: Freeing", PRINT_REG(addr));
|
2011-03-08 14:46:03 +02:00
|
|
|
|
|
|
|
warning("[GC] Clone had pos %04x:%04x", PRINT_REG(victim_obj->pos));
|
2009-06-04 11:45:17 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2011-03-08 14:46:03 +02:00
|
|
|
|
2012-06-18 05:21:59 +03:00
|
|
|
freeEntry(addr.getOffset());
|
2009-06-04 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------- locals --------------------
|
2011-03-08 20:13:08 +02:00
|
|
|
|
|
|
|
SegmentRef LocalVariables::dereference(reg_t pointer) {
|
|
|
|
SegmentRef ret;
|
|
|
|
ret.isRaw = false; // reg_t based data!
|
2012-06-18 05:21:59 +03:00
|
|
|
ret.maxSize = (_locals.size() - pointer.getOffset() / 2) * 2;
|
2011-03-08 20:13:08 +02:00
|
|
|
|
2012-06-18 05:21:59 +03:00
|
|
|
if (pointer.getOffset() & 1) {
|
2011-03-08 20:13:08 +02:00
|
|
|
ret.maxSize -= 1;
|
|
|
|
ret.skipByte = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret.maxSize > 0) {
|
2012-06-18 05:21:59 +03:00
|
|
|
ret.reg = &_locals[pointer.getOffset() / 2];
|
2011-03-08 20:13:08 +02:00
|
|
|
} else {
|
2011-11-03 22:44:11 +02:00
|
|
|
if ((g_sci->getEngineState()->currentRoomNumber() == 160 ||
|
|
|
|
g_sci->getEngineState()->currentRoomNumber() == 220)
|
2011-03-08 20:13:08 +02:00
|
|
|
&& g_sci->getGameId() == GID_LAURABOW2) {
|
2011-11-03 22:44:11 +02:00
|
|
|
// WORKAROUND: Happens in two places during the intro of LB2CD, both
|
|
|
|
// from kMemory(peek):
|
2011-03-08 20:13:08 +02:00
|
|
|
// - room 160: Heap 160 has 83 local variables (0-82), and the game
|
|
|
|
// asks for variables at indices 83 - 90 too.
|
|
|
|
// - room 220: Heap 220 has 114 local variables (0-113), and the
|
|
|
|
// game asks for variables at indices 114-120 too.
|
|
|
|
} else {
|
|
|
|
error("LocalVariables::dereference: Offset at end or out of bounds %04x:%04x", PRINT_REG(pointer));
|
|
|
|
}
|
|
|
|
ret.reg = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-26 18:11:17 +00:00
|
|
|
reg_t LocalVariables::findCanonicAddress(SegManager *segMan, reg_t addr) const {
|
2009-06-04 11:45:17 +00:00
|
|
|
// Reference the owning script
|
2009-09-06 12:58:16 +00:00
|
|
|
SegmentId owner_seg = segMan->getScriptSegment(script_id);
|
|
|
|
assert(owner_seg > 0);
|
2009-06-04 11:45:17 +00:00
|
|
|
return make_reg(owner_seg, 0);
|
|
|
|
}
|
|
|
|
|
2010-06-28 11:22:41 +00:00
|
|
|
Common::Array<reg_t> LocalVariables::listAllOutgoingReferences(reg_t addr) const {
|
|
|
|
Common::Array<reg_t> tmp;
|
2009-06-04 11:45:17 +00:00
|
|
|
for (uint i = 0; i < _locals.size(); i++)
|
2010-06-28 11:22:41 +00:00
|
|
|
tmp.push_back(_locals[i]);
|
|
|
|
|
|
|
|
return tmp;
|
2009-06-04 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------- stack --------------------
|
2011-03-08 20:13:08 +02:00
|
|
|
|
|
|
|
SegmentRef DataStack::dereference(reg_t pointer) {
|
|
|
|
SegmentRef ret;
|
|
|
|
ret.isRaw = false; // reg_t based data!
|
2012-06-18 05:21:59 +03:00
|
|
|
ret.maxSize = (_capacity - pointer.getOffset() / 2) * 2;
|
2011-03-08 20:13:08 +02:00
|
|
|
|
2012-06-18 05:21:59 +03:00
|
|
|
if (pointer.getOffset() & 1) {
|
2011-03-08 20:13:08 +02:00
|
|
|
ret.maxSize -= 1;
|
|
|
|
ret.skipByte = true;
|
|
|
|
}
|
|
|
|
|
2012-06-18 05:21:59 +03:00
|
|
|
ret.reg = &_entries[pointer.getOffset() / 2];
|
2011-03-08 20:13:08 +02:00
|
|
|
return ret;
|
2009-06-04 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2010-06-28 11:22:41 +00:00
|
|
|
Common::Array<reg_t> DataStack::listAllOutgoingReferences(reg_t object) const {
|
|
|
|
Common::Array<reg_t> tmp;
|
2017-02-18 16:17:11 -06:00
|
|
|
for (uint i = 0; i < _capacity; i++)
|
2010-06-28 11:22:41 +00:00
|
|
|
tmp.push_back(_entries[i]);
|
|
|
|
|
|
|
|
return tmp;
|
2009-06-04 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------- lists --------------------
|
|
|
|
|
2010-06-28 11:22:41 +00:00
|
|
|
Common::Array<reg_t> ListTable::listAllOutgoingReferences(reg_t addr) const {
|
|
|
|
Common::Array<reg_t> tmp;
|
2012-06-18 05:21:59 +03:00
|
|
|
if (!isValidEntry(addr.getOffset())) {
|
2010-06-17 23:50:28 +00:00
|
|
|
error("Invalid list referenced for outgoing references: %04x:%04x", PRINT_REG(addr));
|
2009-06-04 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 19:00:12 +01:00
|
|
|
const List *list = &at(addr.getOffset());
|
2009-06-04 11:45:17 +00:00
|
|
|
|
2010-06-28 11:22:41 +00:00
|
|
|
tmp.push_back(list->first);
|
|
|
|
tmp.push_back(list->last);
|
2009-06-04 11:45:17 +00:00
|
|
|
// We could probably get away with just one of them, but
|
|
|
|
// let's be conservative here.
|
2010-06-28 11:22:41 +00:00
|
|
|
|
|
|
|
return tmp;
|
2009-06-04 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------- nodes --------------------
|
|
|
|
|
2010-06-28 11:22:41 +00:00
|
|
|
Common::Array<reg_t> NodeTable::listAllOutgoingReferences(reg_t addr) const {
|
|
|
|
Common::Array<reg_t> tmp;
|
2012-06-18 05:21:59 +03:00
|
|
|
if (!isValidEntry(addr.getOffset())) {
|
2010-06-17 23:50:28 +00:00
|
|
|
error("Invalid node referenced for outgoing references: %04x:%04x", PRINT_REG(addr));
|
2009-06-04 11:45:17 +00:00
|
|
|
}
|
2016-03-23 19:00:12 +01:00
|
|
|
const Node *node = &at(addr.getOffset());
|
2009-06-04 11:45:17 +00:00
|
|
|
|
|
|
|
// We need all four here. Can't just stick with 'pred' OR 'succ' because node operations allow us
|
|
|
|
// to walk around from any given node
|
2010-06-28 11:22:41 +00:00
|
|
|
tmp.push_back(node->pred);
|
|
|
|
tmp.push_back(node->succ);
|
|
|
|
tmp.push_back(node->key);
|
|
|
|
tmp.push_back(node->value);
|
|
|
|
|
|
|
|
return tmp;
|
2009-06-04 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------- dynamic memory --------------------
|
|
|
|
|
2011-03-08 20:13:08 +02:00
|
|
|
SegmentRef DynMem::dereference(reg_t pointer) {
|
|
|
|
SegmentRef ret;
|
|
|
|
ret.isRaw = true;
|
2012-06-18 05:21:59 +03:00
|
|
|
ret.maxSize = _size - pointer.getOffset();
|
|
|
|
ret.raw = _buf + pointer.getOffset();
|
2011-03-08 20:13:08 +02:00
|
|
|
return ret;
|
2009-06-04 11:45:17 +00:00
|
|
|
}
|
|
|
|
|
2009-12-21 14:32:54 +00:00
|
|
|
#ifdef ENABLE_SCI32
|
|
|
|
|
2009-12-30 16:00:56 +00:00
|
|
|
SegmentRef ArrayTable::dereference(reg_t pointer) {
|
|
|
|
SegmentRef ret;
|
|
|
|
|
2016-09-04 16:30:47 -05:00
|
|
|
SciArray &array = at(pointer.getOffset());
|
2016-10-03 16:02:59 -05:00
|
|
|
const bool isRaw = array.getType() == kArrayTypeByte || array.getType() == kArrayTypeString;
|
2016-09-04 16:30:47 -05:00
|
|
|
|
|
|
|
ret.isRaw = isRaw;
|
2016-10-03 16:02:59 -05:00
|
|
|
ret.maxSize = array.byteSize();
|
2016-09-04 16:30:47 -05:00
|
|
|
if (isRaw) {
|
|
|
|
ret.raw = (byte *)array.getRawData();
|
|
|
|
} else {
|
|
|
|
ret.reg = (reg_t *)array.getRawData();
|
|
|
|
}
|
|
|
|
return ret;
|
2010-02-08 15:51:00 +00:00
|
|
|
}
|
|
|
|
|
2010-06-28 11:22:41 +00:00
|
|
|
Common::Array<reg_t> ArrayTable::listAllOutgoingReferences(reg_t addr) const {
|
2016-09-04 16:30:47 -05:00
|
|
|
Common::Array<reg_t> refs;
|
2012-06-18 05:21:59 +03:00
|
|
|
if (!isValidEntry(addr.getOffset())) {
|
2016-09-04 16:30:47 -05:00
|
|
|
// Scripts may still hold references to array memory that has been
|
|
|
|
// explicitly freed; ignore these references
|
|
|
|
return refs;
|
2010-02-08 15:51:00 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 16:30:47 -05:00
|
|
|
SciArray &array = const_cast<SciArray &>(at(addr.getOffset()));
|
2016-10-11 19:38:43 -05:00
|
|
|
if (array.getType() == kArrayTypeID || array.getType() == kArrayTypeInt16) {
|
2016-09-04 16:30:47 -05:00
|
|
|
for (uint16 i = 0; i < array.size(); ++i) {
|
|
|
|
const reg_t value = array.getAsID(i);
|
|
|
|
if (value.isPointer()) {
|
|
|
|
refs.push_back(value);
|
|
|
|
}
|
|
|
|
}
|
2010-02-08 15:51:00 +00:00
|
|
|
}
|
2010-06-28 11:22:41 +00:00
|
|
|
|
2016-09-04 16:30:47 -05:00
|
|
|
return refs;
|
2009-12-21 14:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2009-06-04 11:45:17 +00:00
|
|
|
|
|
|
|
} // End of namespace Sci
|