2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
2010-07-26 16:36:29 -07:00
|
|
|
Copyright (C) 2005-2010 Warzone 2100 Project
|
2007-01-15 12:09:25 -08:00
|
|
|
|
|
|
|
Warzone 2100 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.
|
|
|
|
|
|
|
|
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-06-28 10:47:08 -07:00
|
|
|
/*
|
|
|
|
* CodePrint.c
|
|
|
|
*
|
|
|
|
* Routines for displaying compiled scripts
|
|
|
|
*/
|
|
|
|
|
2006-06-02 12:34:58 -07:00
|
|
|
#include "lib/framework/frame.h"
|
2009-02-10 09:23:23 -08:00
|
|
|
#include "interpreter.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
#include "parse.h"
|
|
|
|
#include "codeprint.h"
|
|
|
|
#include "script.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Display a value */
|
2008-03-25 17:45:03 -07:00
|
|
|
void cpPrintVal(INTERP_VAL value)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
if (value.type & VAL_REF)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
value.type &= ~VAL_REF;
|
|
|
|
debug( LOG_NEVER, "type: %5s, value: %i (ref)",
|
2008-03-26 07:57:35 -07:00
|
|
|
scriptTypeToString(value.type), value.v.ival );
|
2007-06-28 10:47:08 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-25 17:45:03 -07:00
|
|
|
switch(value.type)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
case VAL_BOOL:
|
|
|
|
debug( LOG_NEVER, "type: %5s, value: %s",
|
2008-03-26 07:57:35 -07:00
|
|
|
scriptTypeToString(value.type),
|
2008-03-25 17:45:03 -07:00
|
|
|
bool2string(value.v.bval) );
|
|
|
|
break;
|
|
|
|
case VAL_INT:
|
|
|
|
debug( LOG_NEVER, "type: %5s, value: %d",
|
2008-03-26 07:57:35 -07:00
|
|
|
scriptTypeToString(value.type),
|
2008-03-25 17:45:03 -07:00
|
|
|
value.v.ival );
|
|
|
|
break;
|
|
|
|
case VAL_FLOAT:
|
|
|
|
debug( LOG_NEVER, "type: %5s, value: %f",
|
2008-03-26 07:57:35 -07:00
|
|
|
scriptTypeToString(value.type),
|
2008-03-25 17:45:03 -07:00
|
|
|
value.v.fval );
|
|
|
|
break;
|
|
|
|
case VAL_STRING:
|
|
|
|
debug( LOG_NEVER, "type: %5s, value: %s",
|
2008-03-26 07:57:35 -07:00
|
|
|
scriptTypeToString(value.type),
|
2008-03-25 17:45:03 -07:00
|
|
|
value.v.sval );
|
|
|
|
break;
|
|
|
|
case VAL_TRIGGER:
|
|
|
|
case VAL_EVENT:
|
|
|
|
default:
|
|
|
|
debug( LOG_NEVER, "type: %5s, value: %d",
|
2008-03-26 07:57:35 -07:00
|
|
|
scriptTypeToString(value.type),
|
2008-03-25 17:45:03 -07:00
|
|
|
value.v.ival );
|
|
|
|
break;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Display a value from a program that has been packed with an opcode */
|
2006-11-17 03:25:03 -08:00
|
|
|
void cpPrintPackedVal(INTERP_VAL *ip)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-03-26 09:42:16 -07:00
|
|
|
INTERP_VAL data = { (ip->v.ival & OPCODE_DATAMASK), {0} };
|
|
|
|
data.v = (ip + 1)->v;
|
2006-11-17 03:25:03 -08:00
|
|
|
|
2008-03-25 17:45:03 -07:00
|
|
|
cpPrintVal(data);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Print a variable access function name */
|
|
|
|
void cpPrintVarFunc(SCRIPT_VARFUNC pFunc, UDWORD index)
|
|
|
|
{
|
|
|
|
// Search the external variable functions
|
|
|
|
if (asScrExternalTab)
|
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
unsigned int i;
|
2007-06-28 10:47:08 -07:00
|
|
|
for(i = 0; asScrExternalTab[i].pIdent != NULL; i++)
|
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
if (asScrExternalTab[i].index == index)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
if (asScrExternalTab[i].set == pFunc)
|
|
|
|
{
|
|
|
|
debug( LOG_NEVER, "%s (set)", asScrExternalTab[i].pIdent );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (asScrExternalTab[i].get == pFunc)
|
|
|
|
{
|
|
|
|
debug( LOG_NEVER, "%s (get)", asScrExternalTab[i].pIdent );
|
|
|
|
return;
|
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search the object functions
|
|
|
|
if (asScrObjectVarTab)
|
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
unsigned int i;
|
2007-06-28 10:47:08 -07:00
|
|
|
for(i = 0; asScrObjectVarTab[i].pIdent != NULL; i++)
|
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
if (asScrObjectVarTab[i].index == index)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
if (asScrObjectVarTab[i].set == pFunc)
|
|
|
|
{
|
|
|
|
debug( LOG_NEVER, "%s (set)", asScrObjectVarTab[i].pIdent );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (asScrObjectVarTab[i].get == pFunc)
|
|
|
|
{
|
|
|
|
debug( LOG_NEVER, "%s (get)", asScrObjectVarTab[i].pIdent );
|
|
|
|
return;
|
|
|
|
}
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Print the array information */
|
2008-03-25 17:45:03 -07:00
|
|
|
static void cpPrintArrayInfo(INTERP_VAL *ip, SCRIPT_CODE *psProg)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
// get the base index of the array
|
2008-03-25 17:45:03 -07:00
|
|
|
unsigned int base = (ip->v.ival & ARRAY_BASE_MASK);
|
|
|
|
unsigned int i;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-03-25 17:45:03 -07:00
|
|
|
debug( LOG_NEVER, "-> %d", base );
|
|
|
|
for (i = 0; i < psProg->psArrayInfo[base].dimensions; i++)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
debug( LOG_NEVER, " [%d]", psProg->psArrayInfo[base].elements[i] );
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Display the contents of a program in readable form */
|
|
|
|
void cpPrintProgram(SCRIPT_CODE *psProg)
|
|
|
|
{
|
2006-11-17 03:25:03 -08:00
|
|
|
INTERP_VAL *ip, *end;
|
2007-06-28 10:47:08 -07:00
|
|
|
OPCODE opcode;
|
|
|
|
UDWORD data, i, dim;
|
|
|
|
SCRIPT_DEBUG *psCurrDebug=NULL;
|
|
|
|
BOOL debugInfo, triggerCode;
|
|
|
|
UDWORD jumpOffset;
|
|
|
|
VAR_DEBUG *psCurrVar;
|
|
|
|
ARRAY_DATA *psCurrArray;
|
|
|
|
ARRAY_DEBUG *psCurrArrayDebug;
|
|
|
|
|
2007-04-03 06:20:41 -07:00
|
|
|
ASSERT( psProg != NULL,
|
2006-08-23 05:58:48 -07:00
|
|
|
"cpPrintProgram: Invalid program pointer" );
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-03-25 17:45:03 -07:00
|
|
|
debugInfo = (psProg->psDebug != NULL);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
if (debugInfo)
|
|
|
|
{
|
|
|
|
// Print out the global variables
|
|
|
|
if (psProg->numGlobals > 0)
|
|
|
|
{
|
2006-08-22 07:28:49 -07:00
|
|
|
debug( LOG_NEVER, "index storage type variable name\n" );
|
2007-06-28 10:47:08 -07:00
|
|
|
psCurrVar = psProg->psVarDebug;
|
2008-03-25 17:45:03 -07:00
|
|
|
|
|
|
|
for(i = 0; i < psProg->numGlobals; i++)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2007-03-15 16:17:09 -07:00
|
|
|
debug( LOG_NEVER, "%-6d %s %-4d %s\n", (int)(psCurrVar - psProg->psVarDebug), psCurrVar->storage == ST_PUBLIC ? "Public " : "Private", psProg->pGlobals[i], psCurrVar->pIdent );
|
2007-06-28 10:47:08 -07:00
|
|
|
psCurrVar++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (psProg->numArrays > 0)
|
|
|
|
{
|
2006-08-22 07:28:49 -07:00
|
|
|
debug( LOG_NEVER, "\narrays\n" );
|
2007-06-28 10:47:08 -07:00
|
|
|
psCurrArray = psProg->psArrayInfo;
|
|
|
|
psCurrArrayDebug = psProg->psArrayDebug;
|
2008-03-25 17:45:03 -07:00
|
|
|
|
|
|
|
for(i = 0; i < psProg->numArrays; i++)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
2006-08-22 07:28:49 -07:00
|
|
|
debug( LOG_NEVER, "%-6d %s %-4d %s", i, psCurrArrayDebug->storage == ST_PUBLIC ? "Public " : "Private", psCurrArray->type, psCurrArrayDebug->pIdent );
|
2007-06-28 10:47:08 -07:00
|
|
|
for(dim=0; dim < psCurrArray->dimensions; dim += 1)
|
|
|
|
{
|
2006-08-22 07:28:49 -07:00
|
|
|
debug( LOG_NEVER, "[%d]", psCurrArray->elements[dim] );
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2006-08-22 07:28:49 -07:00
|
|
|
debug( LOG_NEVER, "\n" );
|
2007-06-28 10:47:08 -07:00
|
|
|
psCurrArray++;
|
|
|
|
psCurrArrayDebug++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-22 07:28:49 -07:00
|
|
|
debug( LOG_NEVER, "\nindex line offset\n" );
|
2007-06-28 10:47:08 -07:00
|
|
|
psCurrDebug = psProg->psDebug;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the first trigger with code
|
|
|
|
for(jumpOffset = 0; jumpOffset < psProg->numTriggers; jumpOffset++)
|
|
|
|
{
|
|
|
|
if (psProg->psTriggerData[jumpOffset].type == TR_CODE)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ip = psProg->pCode;
|
2008-03-25 17:45:03 -07:00
|
|
|
end = (INTERP_VAL*)((UBYTE*)ip + psProg->size);
|
|
|
|
triggerCode = (psProg->numTriggers > 0);
|
|
|
|
|
|
|
|
opcode = (ip->v.ival >> OPCODE_SHIFT);
|
2006-11-17 03:25:03 -08:00
|
|
|
data = (ip->v.ival & OPCODE_DATAMASK);
|
2008-03-25 17:45:03 -07:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
while (ip < end)
|
|
|
|
{
|
|
|
|
if (debugInfo)
|
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
// display a label if there is one
|
2007-06-28 10:47:08 -07:00
|
|
|
if ((UDWORD)(ip - psProg->pCode) == psCurrDebug->offset &&
|
|
|
|
psCurrDebug->pLabel != NULL)
|
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
debug( LOG_NEVER, "label: %s\n", psCurrDebug->pLabel );
|
|
|
|
}
|
|
|
|
// Display the line number
|
|
|
|
if ((UDWORD)(ip - psProg->pCode) == psCurrDebug->offset)
|
|
|
|
{
|
|
|
|
debug( LOG_NEVER, "line: %-6d", psCurrDebug->line );
|
|
|
|
psCurrDebug++;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display the trigger/event index
|
|
|
|
if (triggerCode)
|
|
|
|
{
|
|
|
|
if (ip - psProg->pCode == psProg->pTriggerTab[jumpOffset])
|
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
debug( LOG_NEVER, "jumpoffset: %-6d", jumpOffset );
|
|
|
|
jumpOffset++;
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
// Find the next trigger with code
|
|
|
|
while(jumpOffset < psProg->numTriggers)
|
|
|
|
{
|
|
|
|
if (psProg->psTriggerData[jumpOffset].type == TR_CODE)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
jumpOffset++;
|
|
|
|
}
|
|
|
|
if (jumpOffset >= psProg->numTriggers)
|
|
|
|
{
|
|
|
|
// Got to the end of the triggers
|
2008-03-24 09:51:17 -07:00
|
|
|
triggerCode = false;
|
2007-06-28 10:47:08 -07:00
|
|
|
jumpOffset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ip - psProg->pCode == psProg->pEventTab[jumpOffset])
|
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
debug( LOG_NEVER, "jumpoffset: %-6d", jumpOffset );
|
|
|
|
jumpOffset++;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display the code offset
|
2008-03-25 17:45:03 -07:00
|
|
|
debug( LOG_NEVER, "offset: %-6d", (int)(ip - psProg->pCode) );
|
2008-03-26 07:57:35 -07:00
|
|
|
debug( LOG_NEVER, "opcode: %s", scriptOpcodeToString(opcode) );
|
2008-03-25 17:45:03 -07:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
switch (opcode)
|
|
|
|
{
|
2008-03-25 17:45:03 -07:00
|
|
|
case OP_POP:
|
|
|
|
// cpPrintVal(*stackPeek(0));
|
|
|
|
break;
|
|
|
|
case OP_PUSH:
|
|
|
|
case OP_PUSHREF:
|
|
|
|
cpPrintPackedVal(ip);
|
|
|
|
break;
|
|
|
|
case OP_POPGLOBAL:
|
|
|
|
case OP_PUSHGLOBAL:
|
|
|
|
case OP_POPLOCAL:
|
|
|
|
case OP_PUSHLOCAL:
|
|
|
|
case OP_PUSHLOCALREF:
|
|
|
|
debug( LOG_NEVER, "-> %d", data );
|
|
|
|
break;
|
|
|
|
case OP_PUSHARRAYGLOBAL:
|
|
|
|
case OP_POPARRAYGLOBAL:
|
|
|
|
cpPrintArrayInfo(ip, psProg);
|
|
|
|
break;
|
|
|
|
case OP_FUNC:
|
|
|
|
debug( LOG_NEVER, "-> %d", (ip+1)->v.ival );
|
|
|
|
break;
|
|
|
|
case OP_CALL:
|
2008-03-26 07:57:35 -07:00
|
|
|
debug( LOG_NEVER, "-> %s", scriptFunctionToString((ip+1)->v.pFuncExtern) );
|
2008-03-25 17:45:03 -07:00
|
|
|
break;
|
|
|
|
case OP_VARCALL:
|
|
|
|
cpPrintVarFunc( ((INTERP_VAL *)(ip+1))->v.pObjGetSet, data);
|
|
|
|
debug( LOG_NEVER, "-> (%d)", data );
|
|
|
|
break;
|
|
|
|
case OP_JUMP:
|
|
|
|
case OP_JUMPTRUE:
|
|
|
|
case OP_JUMPFALSE:
|
|
|
|
debug( LOG_NEVER, "-> %d (%d)", (SWORD)data, (int)(ip - psProg->pCode + (SWORD)data) );
|
|
|
|
break;
|
|
|
|
case OP_BINARYOP:
|
|
|
|
case OP_UNARYOP:
|
2008-03-26 07:57:35 -07:00
|
|
|
debug( LOG_NEVER, "-> %s", scriptOpcodeToString(data) );
|
2008-03-25 17:45:03 -07:00
|
|
|
break;
|
|
|
|
case OP_EXIT:
|
|
|
|
break;
|
|
|
|
case OP_PAUSE:
|
|
|
|
debug( LOG_NEVER, "-> %d", data );
|
|
|
|
break;
|
|
|
|
case OP_TO_FLOAT:
|
|
|
|
case OP_TO_INT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT( false,"cpPrintProgram: Unknown opcode: %x", ip->type );
|
|
|
|
break;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2008-03-25 17:45:03 -07:00
|
|
|
debug( LOG_NEVER, "\n" );
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-03-25 17:45:03 -07:00
|
|
|
ip += aOpSize[opcode];
|
2007-04-03 06:20:41 -07:00
|
|
|
ASSERT( (ip <= end) || ip != NULL,
|
2006-08-23 05:58:48 -07:00
|
|
|
"cpPrintProgram: instruction pointer no longer valid" );
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2006-11-17 03:25:03 -08:00
|
|
|
opcode = (OPCODE)(ip->v.ival >> OPCODE_SHIFT);
|
|
|
|
data = ip->v.ival & OPCODE_DATAMASK;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|