aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/agt/disassemble.cpp
blob: cd24c48f37c9366f656ba4a7c4840289a01496cc (plain)
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
/* 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.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/agt/agility.h"

namespace Glk {
namespace AGT {

void dbgprintf(const char *fmt, ...) {
	va_list vp;
	char buff[300];

	va_start(vp, fmt);
	vsprintf(buff, fmt, vp);
	va_end(vp);

	debugout(buff);
}


static void print_msg(descr_ptr dptr) {
	int j;
	descr_line *txt;

	txt = read_descr(dptr.start, dptr.size);
	if (txt != NULL) {
		for (j = 0; txt[j] != NULL; j++) {
			dbgprintf("\n");
			debugout(txt[j]);
		}
	}
	free_descr(txt);
}


static char *getname(int inum)
/* Name should be 20 chars or less */
{
	if (inum == 0) return rstrdup("* 0 *");
	return objname(inum);
}


extern integer dobj, iobj, actor;

void print_special_obj(int i)
/* This is called by the disassembler in agtdbg.c */
/* i=0 NOUN, 1 OBJECT, 2 NAME */
{
	int dval;
	char *s;
	switch (i) {
	case 0:
		dval = dobj;
		dbgprintf("NOUN");
		break;
	case 1:
		dval = iobj;
		dbgprintf("OBJECT");
		break;
	case 2:
		dval = actor;
		dbgprintf("NAME");
		break;
	default:
		dval = 0; /* Silence compiler warnings. */
		fatal("INTERNAL ERROR: Invalid *dval* in print_special_obj.");
	}
	if (dbgflagptr == NULL)
		/* This determines whether we are linked with agtout or agil */
		return;
	s = getname(dval);
	dbgprintf("(%d:%s)", dval, s);
	rfree(s);
}

#define printval(str,index,ptr) {dbgprintf("[%s%d",str,index);\
		if (ptr==NULL) dbgprintf("]");\
		else dbgprintf("=%ld]",(long)ptr[index]);}

int argout(int dtype, int dval, int optype) {
	char *s;

	if (dtype & AGT_VAR) dtype = AGT_VAR;

	if ((optype & 3) == 1) /* variable */
		dtype = AGT_VAR;
	if (optype & 2) { /* NOUN or OBJECT */
		if (dtype >= 64 && dtype != AGT_NUM)
			dbgprintf("ILL:");
		if (optype == 2)
			print_special_obj(0); /* NOUN */
		else
			print_special_obj(1);  /* OBJECT */
		return 0;
	}

	if (!interp_arg)
		dbgprintf("%d", dval);
	else {
		if (dtype < 64) {
			if (dval == -1)
				print_special_obj(2); /* NAME */
			else {
				s = getname(dval);
				if (dtype & (AGT_ITEM | AGT_CREAT | AGT_SELF | AGT_WORN))
					dbgprintf("<%d:%s>", dval, s);
				else
					dbgprintf("{%d:%s}", dval, s);
				rfree(s);
			}
		} else if ((dtype & AGT_VAR) != 0) {
			if (dval == -1)
				print_tos();
			else
				printval("Var", dval, dbgvarptr);
		} else switch (dtype) {
			case AGT_TIME:
				dbgprintf("%2d:%2d", dval / 100, dval % 100);
				break;
			case AGT_NUM:  /* Numeric */
				dbgprintf("%d", dval);
				break;
			case AGT_FLAG:  /* Flag */
				printval("Flg", dval, dbgflagptr);
				break;
			case AGT_ROOMFLAG: /* Roomflag */
				dbgprintf("RoomFlag%d", dval);
				break;
			case AGT_QUEST:  /* Question */
				if (dval <= MaxQuestion && dval >= 1 && question != NULL) {
					dbgprintf("\nQ%d:%s\n", dval, question[dval - 1]);
					dbgprintf("[A:%s]", answer[dval - 1]);
				} else if (quest_ptr != NULL) {
					dbgprintf("\nQ%d: ", dval);
					print_msg(quest_ptr[dval - 1]);
					dbgprintf("[A:");
					print_msg(ans_ptr[dval - 1]);
				}
				break;
			case AGT_MSG: /* Message */
				if (dval > last_message || dval < 1 || msg_ptr == NULL)
					dbgprintf("ILLEGAL MESSAGE");
				else {
					dbgprintf("(Msg%d)", dval);
					if (!dbg_nomsg)
						print_msg(msg_ptr[dval - 1]);
				}
				break;
			case AGT_ERR: /* Message */
				if (dval > NUM_ERR || dval < 1 || err_ptr == NULL)
					dbgprintf("ILLEGAL MESSAGE");
				else {
					dbgprintf("(Std%d)", dval);
					if (!dbg_nomsg)
						print_msg(err_ptr[dval - 1]);
				}
				break;
			case AGT_STR: /* String */
				if (dval - 1 >= MAX_USTR || userstr == NULL)
					dbgprintf("ILLEGAL STRING");
				else
					dbgprintf("\nStr%d:%s", dval, userstr[dval]);
				break;
			case AGT_CNT: /* Counter */
				printval("Cnt", dval, dbgcntptr);
				break;
			case AGT_DIR: /* Direction */
				if (dval >= 1 && dval <= 13)
					dbgprintf("%s", exitname[dval - 1]);
				else dbgprintf("ILL_DIR(%d)", dval);
				break;
			case AGT_SUB: /* Subroutine */
				dbgprintf("Subroutine %d", dval);
				break;
			case AGT_PIC: /* Picture */
			case AGT_PIX:
				dbgprintf("Picture #%d", dval);
				break;
			case AGT_FONT: /* Font */
				dbgprintf("Font #%d", dval);
				break;
			case AGT_SONG:  /* Song */
				dbgprintf("Song #%d", dval);
				break;
			case AGT_OBJFLAG:
				dbgprintf("ObjFlag%d", dval);
				break;
			case AGT_OBJPROP:
				dbgprintf("ObjProp%d", dval);
				break;
			case AGT_ATTR:
				if (dval < 0 || dval >= NUM_ATTR)
					dbgprintf("UnkownAttr%d", dval);
				else
					dbgprintf("%s", attrlist[dval].name);
				break;
			case AGT_PROP:
				if (dval < 0 || dval >= NUM_PROP)
					dbgprintf("UnknownProp%d", dval);
				else
					dbgprintf("%s", proplist[dval].name);
				break;
			case AGT_EXIT:
				if (dval >= exitmsg_base)
					argout(AGT_MSG, dval - exitmsg_base, 0);
				else
					argout(AGT_ROOM, dval, 0);
				break;
			default:
				dbgprintf("?+%d", dval);
			}
	}
	return 1;
}


void debug_newline(integer op, rbool first_nl) {
	rbool early_nl;

	if (!dbg_nomsg) return;
	early_nl = (op == 1008 || op == 1027 || op == 1083 || op == 1105
	            || (op >= 1126 && op <= 1131));
	if (early_nl == first_nl)
		debugout("\n");
}


void debug_cmd_out(int ip, integer op, int arg1, int arg2, int optype) {
	int j;
	const opdef *opdata;
	rbool save_dbg_nomsg;

	dbgprintf("  %2d:", ip);
	save_dbg_nomsg = 0; /* Just to silence compiler warnings. */

	opdata = get_opdef(op);
	if (opdata == &illegal_def)
		dbgprintf("ILLEGAL %d\n", op);
	else {
		if (op >= END_ACT) dbgprintf("!"); /* "Terminal" Actions */
		else if (op <= MAX_COND) dbgprintf("?"); /* Condition */
		if (op == 1063) { /* RandomMessage needs special handling */
			save_dbg_nomsg = dbg_nomsg;
			dbg_nomsg = 1;
		}
		dbgprintf("%s", opdata->opcode);
		for (j = 0; j < opdata->argnum; j++) {
			dbgprintf("\t");
			argout(j == 0 ? opdata->arg1 : opdata->arg2 , j == 0 ? arg1 : arg2,
			       optype >> 2);
			optype <<= 2;
		}
		if (op == 1063)
			dbg_nomsg = save_dbg_nomsg;
	}
	debug_newline(op, 1);
}


void debug_head(int i) {
	int v, w, a;

	v = verb_code(command[i].verbcmd);
	if (v >= BASE_VERB && v < BASE_VERB + DUMB_VERB && syntbl[synlist[v]] != 0)
		w = syntbl[synlist[v]];
	else w = command[i].verbcmd;
	if (command[i].actor > 0) {
		dbgprintf("CMD %d: ", i);
		a = command[i].actor;
	} else {
		dbgprintf("REDIR: ");
		a = -command[i].actor;
	}

	if (a == 2)
		dbgprintf("anybody, ");
	else if (a > 2) {
		char *name;
		name = objname(a);
		name[0] = toupper(name[0]);
		dbgprintf("%s, ", name);
		rfree(name);
	}

	dbgprintf("%s ", w == 0 ? "any" : dict[w]);
	if (command[i].noun_adj != 0)
		dbgprintf("%s ", gdict(command[i].noun_adj));
	dbgprintf("%s %s ", gdict(command[i].nouncmd),
	          (ver == 3) ? gdict(command[i].prep) : "->");
	if (command[i].obj_adj != 0)
		dbgprintf("%s ", gdict(command[i].obj_adj));
	dbgprintf("%s\n", gdict(command[i].objcmd));

}

} // End of namespace AGT
} // End of namespace Glk