File: codeprint.c

package info (click to toggle)
warzone2100 2.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 104,520 kB
  • ctags: 37,273
  • sloc: ansic: 194,169; yacc: 6,307; sh: 4,753; lex: 1,943; makefile: 1,507; cpp: 836; perl: 139
file content (339 lines) | stat: -rw-r--r-- 8,352 bytes parent folder | download
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
	This file is part of Warzone 2100.
	Copyright (C) 1999-2004  Eidos Interactive
	Copyright (C) 2005-2010  Warzone 2100 Project

	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
*/
/*
 * CodePrint.c
 *
 * Routines for displaying compiled scripts
 */

#include "lib/framework/frame.h"
#include "interpreter.h"
#include "parse.h"
#include "codeprint.h"
#include "script.h"


/* Display a value  */
void cpPrintVal(INTERP_VAL value)
{
	if (value.type & VAL_REF)
	{
		value.type &= ~VAL_REF;
		debug( LOG_NEVER, "type: %5s, value: %i (ref)",
			scriptTypeToString(value.type), value.v.ival );
		return;
	}

	switch(value.type)
	{
		case VAL_BOOL:
			debug( LOG_NEVER, "type: %5s, value: %s",
				scriptTypeToString(value.type),
				bool2string(value.v.bval) );
			break;
		case VAL_INT:
			debug( LOG_NEVER, "type: %5s, value: %d",
				scriptTypeToString(value.type),
				value.v.ival );
			break;
		case VAL_FLOAT:
			debug( LOG_NEVER, "type: %5s, value: %f",
				scriptTypeToString(value.type),
				value.v.fval );
			break;
		case VAL_STRING:
			debug( LOG_NEVER, "type: %5s, value: %s",
				scriptTypeToString(value.type),
				value.v.sval );
			break;
		case VAL_TRIGGER:
		case VAL_EVENT:
		default:
			debug( LOG_NEVER, "type: %5s, value: %d",
				scriptTypeToString(value.type),
				value.v.ival );
			break;
	}
}


/* Display a value from a program that has been packed with an opcode */
void cpPrintPackedVal(INTERP_VAL *ip)
{
	INTERP_VAL data = { (ip->v.ival & OPCODE_DATAMASK), {0} };
	data.v = (ip + 1)->v;

	cpPrintVal(data);
}


/* Print a variable access function name */
void cpPrintVarFunc(SCRIPT_VARFUNC pFunc, UDWORD index)
{
	// Search the external variable functions
	if (asScrExternalTab)
	{
		unsigned int i;
		for(i = 0; asScrExternalTab[i].pIdent != NULL; i++)
		{
			if (asScrExternalTab[i].index == index)
			{
				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;
				}
			}
		}
	}

	// Search the object functions
	if (asScrObjectVarTab)
	{
		unsigned int i;
		for(i = 0; asScrObjectVarTab[i].pIdent != NULL; i++)
		{
			if (asScrObjectVarTab[i].index == index)
			{
				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;
				}
			}
		}
	}
}


/* Print the array information */
static void cpPrintArrayInfo(INTERP_VAL *ip, SCRIPT_CODE *psProg)
{
	// get the base index of the array
	unsigned int base = (ip->v.ival & ARRAY_BASE_MASK);
	unsigned int i;

	debug( LOG_NEVER, "-> %d", base );
	for (i = 0; i < psProg->psArrayInfo[base].dimensions; i++)
	{
		debug( LOG_NEVER, "   [%d]", psProg->psArrayInfo[base].elements[i] );
	}
}


/* Display the contents of a program in readable form */
void cpPrintProgram(SCRIPT_CODE *psProg)
{
	INTERP_VAL		*ip, *end;
	OPCODE			opcode;
	UDWORD			data, i, dim;
	SCRIPT_DEBUG	*psCurrDebug=NULL;
	BOOL			debugInfo, triggerCode;
	UDWORD			jumpOffset;
	VAR_DEBUG		*psCurrVar;
	ARRAY_DATA		*psCurrArray;
	ARRAY_DEBUG		*psCurrArrayDebug;

	ASSERT( psProg != NULL,
		"cpPrintProgram: Invalid program pointer" );

	debugInfo = (psProg->psDebug != NULL);

	if (debugInfo)
	{
		// Print out the global variables
		if (psProg->numGlobals > 0)
		{
			debug( LOG_NEVER, "index  storage  type variable name\n" );
			psCurrVar = psProg->psVarDebug;

			for(i = 0; i < psProg->numGlobals; i++)
			{
				debug( LOG_NEVER, "%-6d %s  %-4d %s\n", (int)(psCurrVar - psProg->psVarDebug), psCurrVar->storage == ST_PUBLIC ? "Public " : "Private", psProg->pGlobals[i], psCurrVar->pIdent );
				psCurrVar++;
			}
		}

		if (psProg->numArrays > 0)
		{
			debug( LOG_NEVER, "\narrays\n" );
			psCurrArray = psProg->psArrayInfo;
			psCurrArrayDebug = psProg->psArrayDebug;

			for(i = 0; i < psProg->numArrays; i++)
			{
				debug( LOG_NEVER, "%-6d %s  %-4d %s", i, psCurrArrayDebug->storage == ST_PUBLIC ? "Public " : "Private", psCurrArray->type, psCurrArrayDebug->pIdent );
				for(dim=0; dim < psCurrArray->dimensions; dim += 1)
				{
					debug( LOG_NEVER, "[%d]", psCurrArray->elements[dim] );
				}
				debug( LOG_NEVER, "\n" );
				psCurrArray++;
				psCurrArrayDebug++;
			}
		}

		debug( LOG_NEVER, "\nindex line offset\n" );
		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;
	end = (INTERP_VAL*)((UBYTE*)ip + psProg->size);
	triggerCode = (psProg->numTriggers > 0);

	opcode = (ip->v.ival >> OPCODE_SHIFT);
	data = (ip->v.ival & OPCODE_DATAMASK);

	while (ip < end)
	{
		if (debugInfo)
		{
			// display a label if there is one
			if ((UDWORD)(ip - psProg->pCode) == psCurrDebug->offset &&
				psCurrDebug->pLabel != NULL)
			{
				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++;
			}
		}

		// Display the trigger/event index
		if (triggerCode)
		{
			if (ip - psProg->pCode == psProg->pTriggerTab[jumpOffset])
			{
				debug( LOG_NEVER, "jumpoffset: %-6d", jumpOffset );
				jumpOffset++;

				// 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
					triggerCode = false;
					jumpOffset = 0;
				}
			}
		}
		else
		{
			if (ip - psProg->pCode == psProg->pEventTab[jumpOffset])
			{
				debug( LOG_NEVER, "jumpoffset: %-6d", jumpOffset );
				jumpOffset++;
			}
		}

		// Display the code offset
		debug( LOG_NEVER, "offset: %-6d", (int)(ip - psProg->pCode) );
		debug( LOG_NEVER, "opcode: %s", scriptOpcodeToString(opcode) );

		switch (opcode)
		{
			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:
				debug( LOG_NEVER, "-> %s", scriptFunctionToString((ip+1)->v.pFuncExtern) );
				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:
				debug( LOG_NEVER, "-> %s", scriptOpcodeToString(data) );
				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;
		}
		debug( LOG_NEVER, "\n" );

		ip += aOpSize[opcode];
		ASSERT( (ip <= end) || ip != NULL,
			"cpPrintProgram: instruction pointer no longer valid" );

		opcode = (OPCODE)(ip->v.ival >> OPCODE_SHIFT);
		data = ip->v.ival & OPCODE_DATAMASK;
	}
}