/[gxemul]/trunk/src/debugger/debugger_expr.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/src/debugger/debugger_expr.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 44 - (show annotations)
Mon Oct 8 16:22:56 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 9984 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1632 2007/09/11 21:46:35 debug Exp $
20070616	Implementing the MIPS32/64 revision 2 "ror" instruction.
20070617	Adding a struct for each physpage which keeps track of which
		ranges within that page (base offset, length) that are
		continuously translatable. When running with native code
		generation enabled (-b), a range is added after each read-
		ahead loop.
		Experimenting with using the physical program counter sample
		data (implemented 20070608) together with the "translatable
		range" information, to figure out which physical address ranges
		would be worth translating to native code (if the number of
		samples falling within a range is above a certain threshold).
20070618	Adding automagic building of .index comment files for
		src/file/, src/promemul/, src src/useremul/ as well.
		Adding a "has been translated" bit to the ranges, so that only
		not-yet-translated ranges will be sampled.
20070619	Moving src/cpu.c and src/memory_rw.c into src/cpus/,
		src/device.c into src/devices/, and src/machine.c into
		src/machines/.
		Creating a skeleton cc/ld native backend module; beginning on
		the function which will detect cc command line, etc.
20070620	Continuing on the native code generation infrastructure.
20070621	Moving src/x11.c and src/console.c into a new src/console/
		subdir (for everything that is console or framebuffer related).
		Moving src/symbol*.c into a new src/symbol/, which should
		contain anything that is symbol handling related.
20070624	Making the program counter sampling threshold a "settings
		variable" (sampling_threshold), i.e. it can now be changed
		during runtime.
		Switching the RELEASE notes format from plain text to HTML.
		If the TMPDIR environment variable is set, it is used instead
		of "/tmp" for temporary files.
		Continuing on the cc/ld backend: simple .c code is generated,
		the compiler and linker are called, etc.
		Adding detection of host architecture to the configure script
		(again), and adding icache invalidation support (only
		implemented for Alpha hosts so far).
20070625	Simplifying the program counter sampling mechanism.
20070626	Removing the cc/ld native code generation stuff, program
		counter sampling, etc; it would not have worked well in the
		general case.
20070627	Removing everything related to native code generation.
20070629	Removing the (practically unusable) support for multiple
		emulations. (The single emulation allowed now still supports
		multiple simultaneous machines, as before.)
		Beginning on PCCTWO and M88K interrupts.
20070723	Adding a dummy skeleton for emulation of M32R processors.
20070901	Fixing a warning found by "gcc version 4.3.0 20070817
		(experimental)" on amd64.
20070905	Removing some more traces of the old "multiple emulations"
		code.
		Also looking in /usr/local/include and /usr/local/lib for
		X11 libs, when running configure.
20070909	Minor updates to the guest OS install instructions, in
		preparation for the NetBSD 4.0 release.
20070918	More testing of NetBSD 4.0 RC1.

1 /*
2 * Copyright (C) 2004-2007 Anders Gavare. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *
28 * $Id: debugger_expr.c,v 1.12 2007/06/28 14:58:38 debug Exp $
29 *
30 * Expression evaluator.
31 *
32 *
33 * TODO:
34 * Sign-extension only on MIPS?
35 * SPECIAL IMPORTANT CASE: Clear the delay_slot flag when writing
36 * to the pc register.
37 * TAB completion? :-)
38 */
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44
45 #include "cpu.h"
46 #include "debugger.h"
47 #include "machine.h"
48 #include "misc.h"
49 #include "settings.h"
50
51
52 extern struct settings *global_settings;
53
54 extern int debugger_cur_cpu;
55 extern int debugger_cur_machine;
56
57
58 /*
59 * debugger_parse_name():
60 *
61 * This function takes a string as input, and tries to match it to a register
62 * name or a more general "setting", a hexadecimal or decimal numeric value,
63 * or a registered symbol.
64 *
65 * Some examples:
66 *
67 * Settings (including register names):
68 * verbose
69 * pc
70 * r5
71 *
72 * Numeric values:
73 * 12345
74 * 0x7fff1234
75 *
76 * Symbols:
77 * memcpy
78 *
79 * To force detection of different types, a character can be added in front of
80 * the name: "$" for numeric values, "#" for registers or other settings,
81 * and "@" for symbols.
82 *
83 * Return value is:
84 *
85 * PARSE_NOMATCH no match
86 * PARSE_MULTIPLE multiple matches
87 *
88 * or one of these (and then *valuep is read or written, depending on
89 * the writeflag):
90 *
91 * PARSE_SETTINGS a setting (e.g. a register)
92 * PARSE_NUMBER a hex number
93 * PARSE_SYMBOL a symbol
94 */
95 int debugger_parse_name(struct machine *m, char *name, int writeflag,
96 uint64_t *valuep)
97 {
98 int match_settings = 0, match_symbol = 0, match_numeric = 0;
99 int skip_settings, skip_numeric, skip_symbol;
100
101 if (m == NULL || name == NULL) {
102 fprintf(stderr, "debugger_parse_name(): NULL ptr\n");
103 exit(1);
104 }
105
106 while (name[0] == '\t' || name[0] == ' ')
107 name ++;
108
109 /* Warn about non-signextended values: */
110 if (writeflag) {
111 if (m->cpus[0]->is_32bit) {
112 /* Automagically sign-extend. TODO: Is this good? */
113 if (((*valuep) >> 32) == 0 && (*valuep) & 0x80000000ULL)
114 (*valuep) |= 0xffffffff00000000ULL;
115 } else {
116 if (((*valuep) >> 32) == 0 && (*valuep) & 0x80000000ULL)
117 printf("WARNING: The value is not sign-extende"
118 "d. Is this what you intended?\n");
119 }
120 }
121
122 skip_settings = name[0] == '$' || name[0] == '@';
123 skip_numeric = name[0] == '#' || name[0] == '@';
124 skip_symbol = name[0] == '$' || name[0] == '#';
125
126 if (!skip_settings) {
127 char setting_name[400];
128 int res;
129
130 res = settings_access(global_settings, name, writeflag, valuep);
131 if (res == SETTINGS_OK)
132 match_settings = 1;
133
134 if (!match_settings) {
135 snprintf(setting_name, sizeof(setting_name),
136 GLOBAL_SETTINGS_NAME".%s", name);
137 res = settings_access(global_settings, setting_name,
138 writeflag, valuep);
139 if (res == SETTINGS_OK)
140 match_settings = 1;
141 }
142
143 if (!match_settings) {
144 snprintf(setting_name, sizeof(setting_name),
145 GLOBAL_SETTINGS_NAME".emul.%s", name);
146 res = settings_access(global_settings, setting_name,
147 writeflag, valuep);
148 if (res == SETTINGS_OK)
149 match_settings = 1;
150 }
151
152 if (!match_settings) {
153 snprintf(setting_name, sizeof(setting_name),
154 GLOBAL_SETTINGS_NAME".emul.machine[%i].%s",
155 debugger_cur_machine, name);
156 res = settings_access(global_settings, setting_name,
157 writeflag, valuep);
158 if (res == SETTINGS_OK)
159 match_settings = 1;
160 }
161
162 if (!match_settings) {
163 snprintf(setting_name, sizeof(setting_name),
164 GLOBAL_SETTINGS_NAME".emul.machine[%i]."
165 "cpu[%i].%s", debugger_cur_machine,
166 debugger_cur_cpu, name);
167 res = settings_access(global_settings, setting_name,
168 writeflag, valuep);
169 if (res == SETTINGS_OK)
170 match_settings = 1;
171 }
172 }
173
174 /* Check for a number match: */
175 if (!skip_numeric && isdigit((int)name[0])) {
176 uint64_t x;
177 x = strtoull(name, NULL, 0);
178 if (writeflag)
179 printf("You cannot assign like that.\n");
180 else
181 *valuep = x;
182 match_numeric = 1;
183 }
184
185 /* Check for a symbol match: */
186 if (!skip_symbol) {
187 uint64_t newaddr;
188 if (get_symbol_addr(&m->symbol_context, name, &newaddr)) {
189 if (writeflag)
190 printf("You cannot assign like that.\n");
191 else
192 *valuep = newaddr;
193 match_symbol = 1;
194 }
195 }
196
197 if (match_settings + match_symbol + match_numeric > 1)
198 return PARSE_MULTIPLE;
199
200 if (match_settings)
201 return PARSE_SETTINGS;
202 if (match_numeric)
203 return PARSE_NUMBER;
204 if (match_symbol)
205 return PARSE_SYMBOL;
206
207 return PARSE_NOMATCH;
208 }
209
210
211 /*
212 * debugger_parse_expression()
213 *
214 * Input:
215 * writeflag = 0: expr = an expression to evaluate. The result is
216 * returned in *valuep.
217 *
218 * writeflag = 1: expr = an lvalue name. *valuep is written to that
219 * lvalue, using debugger_parse_name().
220 *
221 * Parentheses always have precedence.
222 * * / and % have second highest precedence.
223 * + - & | ^ have lowest precedence.
224 *
225 * Return value on failure is:
226 *
227 * PARSE_NOMATCH one or more words in the expression didn't
228 * match any known symbol/register/number
229 * PARSE_MULTIPLE multiple matches within the expression
230 *
231 * Return value on success is PARSE_NUMBER (for now).
232 *
233 *
234 * TODO: BETTER RETURN VALUE!
235 *
236 * NOTE: This is a quick hack, but hopefully it should work. The internal
237 * mechanism is to split the expression into a left half and a right
238 * half around an operator. This operator should be the operator
239 * in the string which has the lowest precedence (except those that
240 * are inside parentheses sub-expressions). E.g. if the expression
241 * is a * (b + c * d) / e then the operator with the lowest
242 * precedence is the first multiplication sign, and the split will
243 * be: left = a
244 * right = (b+c*d)/e
245 */
246 int debugger_parse_expression(struct machine *m, char *expr, int writeflag,
247 uint64_t *valuep)
248 {
249 int prec, res, i, nest;
250 char *copy;
251
252 if (writeflag)
253 return debugger_parse_name(m, expr, writeflag, valuep);
254
255 while (expr[0] == '\t' || expr[0] == ' ')
256 expr ++;
257
258 CHECK_ALLOCATION(copy = strdup(expr));
259
260 while (copy[0] && copy[strlen(copy)-1] == ' ')
261 copy[strlen(copy)-1] = '\0';
262
263 /* Find the lowest operator precedence: */
264 i = 0; prec = 2; nest = 0;
265 while (copy[i] != '\0') {
266 switch (copy[i]) {
267 case '(':
268 nest ++;
269 break;
270 case ')':
271 nest --;
272 break;
273 case '+':
274 case '-':
275 case '^':
276 case '&':
277 case '|':
278 if (nest == 0)
279 prec = 0;
280 break;
281 case '*':
282 case '/':
283 case '%':
284 if (nest == 0 && prec > 1)
285 prec = 1;
286 break;
287 }
288
289 i++;
290 }
291
292 if (nest != 0) {
293 printf("Unmatching parentheses.\n");
294 return PARSE_NOMATCH;
295 }
296
297 if (prec == 2 && copy[0] == '(' && copy[strlen(copy)-1] == ')') {
298 int res;
299 copy[strlen(copy)-1] = '\0';
300 res = debugger_parse_expression(m, copy+1, 0, valuep);
301 free(copy);
302 return res;
303 }
304
305 /* Split according to the first lowest priority operator: */
306 i = 0; nest = 0;
307 while (copy[i] != '\0') {
308 switch (copy[i]) {
309 case '(':
310 nest ++;
311 break;
312 case ')':
313 nest --;
314 break;
315 case '*':
316 case '/':
317 case '%':
318 if (prec == 0)
319 break;
320 /* Fallthrough. */
321 case '+':
322 case '-':
323 case '^':
324 case '&':
325 case '|':
326 if (nest == 0) {
327 uint64_t left, right;
328 int res1, res2, j;
329 char op = copy[i];
330
331 copy[i] = '\0';
332 j = i;
333 while (j>0 && copy[j-1] == ' ') {
334 copy[j-1] = '\0';
335 j --;
336 }
337
338 res1 = debugger_parse_expression(
339 m, copy, 0, &left);
340 res2 = debugger_parse_expression(
341 m, copy + i + 1, 0, &right);
342
343 if (res1 == PARSE_NOMATCH ||
344 res2 == PARSE_NOMATCH) {
345 res = PARSE_NOMATCH;
346 goto return_failure;
347 }
348
349 if (res1 == PARSE_MULTIPLE ||
350 res2 == PARSE_MULTIPLE) {
351 res = PARSE_MULTIPLE;
352 goto return_failure;
353 }
354
355 switch (op) {
356 case '+':
357 (*valuep) = left + right;
358 break;
359 case '-':
360 (*valuep) = left - right;
361 break;
362 case '^':
363 (*valuep) = left ^ right;
364 break;
365 case '&':
366 (*valuep) = left & right;
367 break;
368 case '|':
369 (*valuep) = left | right;
370 break;
371 case '*':
372 (*valuep) = left * right;
373 break;
374 case '/':
375 (*valuep) = left / right;
376 break;
377 case '%':
378 (*valuep) = left % right;
379 break;
380 }
381
382 goto return_ok;
383 }
384 break;
385 }
386
387 i ++;
388 }
389
390 res = debugger_parse_name(m, expr, writeflag, valuep);
391 if (res == PARSE_NOMATCH || res == PARSE_MULTIPLE)
392 goto return_failure;
393
394 return_ok:
395 free(copy);
396 return PARSE_NUMBER;
397
398 return_failure:
399 free(copy);
400 return res;
401 }
402

  ViewVC Help
Powered by ViewVC 1.1.26