]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/expprint.c
* eval.c (evaluate_subexp): Add case MULTI_SUBSCRIPT.
[thirdparty/binutils-gdb.git] / gdb / expprint.c
1 /* Print in infix form a struct expression.
2 Copyright (C) 1986, 1989, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "expression.h"
24 #include "value.h"
25 #include "language.h"
26 #include "parser-defs.h"
27
28 /* Prototypes for local functions */
29
30 static void
31 print_subexp PARAMS ((struct expression *, int *, FILE *, enum precedence));
32
33 static void
34 print_simple_m2_func PARAMS ((char *, struct expression *, int *, FILE *));
35
36 void
37 print_expression (exp, stream)
38 struct expression *exp;
39 FILE *stream;
40 {
41 int pc = 0;
42 print_subexp (exp, &pc, stream, PREC_NULL);
43 }
44
45 /* Print the subexpression of EXP that starts in position POS, on STREAM.
46 PREC is the precedence of the surrounding operator;
47 if the precedence of the main operator of this subexpression is less,
48 parentheses are needed here. */
49
50 static void
51 print_subexp (exp, pos, stream, prec)
52 register struct expression *exp;
53 register int *pos;
54 FILE *stream;
55 enum precedence prec;
56 {
57 register unsigned tem;
58 register const struct op_print *op_print_tab;
59 register int pc;
60 unsigned nargs;
61 register char *op_str;
62 int assign_modify = 0;
63 enum exp_opcode opcode;
64 enum precedence myprec;
65 /* Set to 1 for a right-associative operator. */
66 int assoc;
67 value val;
68
69 op_print_tab = exp->language_defn->la_op_print_tab;
70 pc = (*pos)++;
71 opcode = exp->elts[pc].opcode;
72 switch (opcode)
73 {
74 /* Common ops */
75
76 case OP_SCOPE:
77 myprec = PREC_PREFIX;
78 assoc = 0;
79 (*pos) += 3;
80 print_subexp (exp, pos, stream,
81 (enum precedence) ((int) myprec + assoc));
82 fputs_filtered (" :: ", stream);
83 nargs = longest_to_int (exp->elts[pc + 2].longconst);
84 (*pos) += 2 + (nargs + sizeof (union exp_element)) / sizeof (union exp_element);
85
86 fputs_filtered (&exp->elts[pc + 3].string, stream);
87 return;
88
89 case OP_LONG:
90 (*pos) += 3;
91 value_print (value_from_longest (exp->elts[pc + 1].type,
92 exp->elts[pc + 2].longconst),
93 stream, 0, Val_no_prettyprint);
94 return;
95
96 case OP_DOUBLE:
97 (*pos) += 3;
98 value_print (value_from_double (exp->elts[pc + 1].type,
99 exp->elts[pc + 2].doubleconst),
100 stream, 0, Val_no_prettyprint);
101 return;
102
103 case OP_VAR_VALUE:
104 (*pos) += 2;
105 fputs_filtered (SYMBOL_SOURCE_NAME (exp->elts[pc + 1].symbol), stream);
106 return;
107
108 case OP_LAST:
109 (*pos) += 2;
110 fprintf_filtered (stream, "$%d",
111 longest_to_int (exp->elts[pc + 1].longconst));
112 return;
113
114 case OP_REGISTER:
115 (*pos) += 2;
116 fprintf_filtered (stream, "$%s",
117 reg_names[longest_to_int (exp->elts[pc + 1].longconst)]);
118 return;
119
120 case OP_BOOL:
121 (*pos) += 2;
122 fprintf_filtered (stream, "%s",
123 longest_to_int (exp->elts[pc + 1].longconst)
124 ? "TRUE" : "FALSE");
125 return;
126
127 case OP_INTERNALVAR:
128 (*pos) += 2;
129 fprintf_filtered (stream, "$%s",
130 internalvar_name (exp->elts[pc + 1].internalvar));
131 return;
132
133 case OP_FUNCALL:
134 (*pos) += 2;
135 nargs = longest_to_int (exp->elts[pc + 1].longconst);
136 print_subexp (exp, pos, stream, PREC_SUFFIX);
137 fputs_filtered (" (", stream);
138 for (tem = 0; tem < nargs; tem++)
139 {
140 if (tem != 0)
141 fputs_filtered (", ", stream);
142 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
143 }
144 fputs_filtered (")", stream);
145 return;
146
147 case OP_STRING:
148 nargs = longest_to_int (exp -> elts[pc + 1].longconst);
149 (*pos) += 3 + (nargs + sizeof (union exp_element))
150 / sizeof (union exp_element);
151 /* LA_PRINT_STRING will print using the current repeat count threshold.
152 If necessary, we can temporarily set it to zero, or pass it as an
153 additional parameter to LA_PRINT_STRING. -fnf */
154 LA_PRINT_STRING (stream, &exp->elts[pc + 2].string, nargs, 0);
155 return;
156
157 case TERNOP_COND:
158 if ((int) prec > (int) PREC_COMMA)
159 fputs_filtered ("(", stream);
160 /* Print the subexpressions, forcing parentheses
161 around any binary operations within them.
162 This is more parentheses than are strictly necessary,
163 but it looks clearer. */
164 print_subexp (exp, pos, stream, PREC_HYPER);
165 fputs_filtered (" ? ", stream);
166 print_subexp (exp, pos, stream, PREC_HYPER);
167 fputs_filtered (" : ", stream);
168 print_subexp (exp, pos, stream, PREC_HYPER);
169 if ((int) prec > (int) PREC_COMMA)
170 fputs_filtered (")", stream);
171 return;
172
173 case STRUCTOP_STRUCT:
174 tem = longest_to_int (exp->elts[pc + 1].longconst);
175 (*pos) += 3 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
176 print_subexp (exp, pos, stream, PREC_SUFFIX);
177 fputs_filtered (".", stream);
178 fputs_filtered (&exp->elts[pc + 2].string, stream);
179 return;
180
181 /* Will not occur for Modula-2 */
182 case STRUCTOP_PTR:
183 tem = longest_to_int (exp->elts[pc + 1].longconst);
184 (*pos) += 3 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
185 print_subexp (exp, pos, stream, PREC_SUFFIX);
186 fputs_filtered ("->", stream);
187 fputs_filtered (&exp->elts[pc + 2].string, stream);
188 return;
189
190 case BINOP_SUBSCRIPT:
191 print_subexp (exp, pos, stream, PREC_SUFFIX);
192 fputs_filtered ("[", stream);
193 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
194 fputs_filtered ("]", stream);
195 return;
196
197 case UNOP_POSTINCREMENT:
198 print_subexp (exp, pos, stream, PREC_SUFFIX);
199 fputs_filtered ("++", stream);
200 return;
201
202 case UNOP_POSTDECREMENT:
203 print_subexp (exp, pos, stream, PREC_SUFFIX);
204 fputs_filtered ("--", stream);
205 return;
206
207 case UNOP_CAST:
208 (*pos) += 2;
209 if ((int) prec > (int) PREC_PREFIX)
210 fputs_filtered ("(", stream);
211 fputs_filtered ("(", stream);
212 type_print (exp->elts[pc + 1].type, "", stream, 0);
213 fputs_filtered (") ", stream);
214 print_subexp (exp, pos, stream, PREC_PREFIX);
215 if ((int) prec > (int) PREC_PREFIX)
216 fputs_filtered (")", stream);
217 return;
218
219 case UNOP_MEMVAL:
220 (*pos) += 2;
221 if ((int) prec > (int) PREC_PREFIX)
222 fputs_filtered ("(", stream);
223 if (exp->elts[pc + 1].type->code == TYPE_CODE_FUNC &&
224 exp->elts[pc + 3].opcode == OP_LONG) {
225 /* We have a minimal symbol fn, probably. It's encoded
226 as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
227 Swallow the OP_LONG (including both its opcodes); ignore
228 its type; print the value in the type of the MEMVAL. */
229 (*pos) += 4;
230 val = value_at_lazy (exp->elts[pc + 1].type,
231 (CORE_ADDR) exp->elts[pc + 5].longconst);
232 value_print (val, stream, 0, Val_no_prettyprint);
233 } else {
234 fputs_filtered ("{", stream);
235 type_print (exp->elts[pc + 1].type, "", stream, 0);
236 fputs_filtered ("} ", stream);
237 print_subexp (exp, pos, stream, PREC_PREFIX);
238 }
239 if ((int) prec > (int) PREC_PREFIX)
240 fputs_filtered (")", stream);
241 return;
242
243 case BINOP_ASSIGN_MODIFY:
244 opcode = exp->elts[pc + 1].opcode;
245 (*pos) += 2;
246 myprec = PREC_ASSIGN;
247 assoc = 1;
248 assign_modify = 1;
249 op_str = "???";
250 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
251 if (op_print_tab[tem].opcode == opcode)
252 {
253 op_str = op_print_tab[tem].string;
254 break;
255 }
256 break;
257
258 /* C++ ops */
259
260 case OP_THIS:
261 ++(*pos);
262 fputs_filtered ("this", stream);
263 return;
264
265 /* Modula-2 ops */
266
267 case MULTI_SUBSCRIPT:
268 (*pos) += 2;
269 nargs = longest_to_int (exp->elts[pc + 1].longconst);
270 print_subexp (exp, pos, stream, PREC_SUFFIX);
271 fprintf (stream, " [");
272 for (tem = 0; tem < nargs; tem++)
273 {
274 if (tem != 0)
275 fprintf (stream, ", ");
276 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
277 }
278 fprintf (stream, "]");
279 return;
280
281 case BINOP_VAL:
282 (*pos)+=2;
283 fprintf(stream,"VAL(");
284 type_print(exp->elts[pc+1].type,"",stream,0);
285 fprintf(stream,",");
286 print_subexp(exp,pos,stream,PREC_PREFIX);
287 fprintf(stream,")");
288 return;
289
290 case UNOP_CAP:
291 print_simple_m2_func("CAP",exp,pos,stream);
292 return;
293
294 case UNOP_CHR:
295 print_simple_m2_func("CHR",exp,pos,stream);
296 return;
297
298 case UNOP_ORD:
299 print_simple_m2_func("ORD",exp,pos,stream);
300 return;
301
302 case UNOP_ABS:
303 print_simple_m2_func("ABS",exp,pos,stream);
304 return;
305
306 case UNOP_FLOAT:
307 print_simple_m2_func("FLOAT",exp,pos,stream);
308 return;
309
310 case UNOP_HIGH:
311 print_simple_m2_func("HIGH",exp,pos,stream);
312 return;
313
314 case UNOP_MAX:
315 print_simple_m2_func("MAX",exp,pos,stream);
316 return;
317
318 case UNOP_MIN:
319 print_simple_m2_func("MIN",exp,pos,stream);
320 return;
321
322 case UNOP_ODD:
323 print_simple_m2_func("ODD",exp,pos,stream);
324 return;
325
326 case UNOP_TRUNC:
327 print_simple_m2_func("TRUNC",exp,pos,stream);
328 return;
329
330 case BINOP_INCL:
331 case BINOP_EXCL:
332 error("print_subexp: Not implemented.");
333
334 /* Default ops */
335
336 default:
337 op_str = "???";
338 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
339 if (op_print_tab[tem].opcode == opcode)
340 {
341 op_str = op_print_tab[tem].string;
342 myprec = op_print_tab[tem].precedence;
343 assoc = op_print_tab[tem].right_assoc;
344 break;
345 }
346 }
347
348 if ((int) myprec < (int) prec)
349 fputs_filtered ("(", stream);
350 if ((int) opcode > (int) BINOP_END)
351 {
352 /* Unary prefix operator. */
353 fputs_filtered (op_str, stream);
354 print_subexp (exp, pos, stream, PREC_PREFIX);
355 }
356 else
357 {
358 /* Binary operator. */
359 /* Print left operand.
360 If operator is right-associative,
361 increment precedence for this operand. */
362 print_subexp (exp, pos, stream,
363 (enum precedence) ((int) myprec + assoc));
364 /* Print the operator itself. */
365 if (assign_modify)
366 fprintf_filtered (stream, " %s= ", op_str);
367 else if (op_str[0] == ',')
368 fprintf_filtered (stream, "%s ", op_str);
369 else
370 fprintf_filtered (stream, " %s ", op_str);
371 /* Print right operand.
372 If operator is left-associative,
373 increment precedence for this operand. */
374 print_subexp (exp, pos, stream,
375 (enum precedence) ((int) myprec + !assoc));
376 }
377
378 if ((int) myprec < (int) prec)
379 fputs_filtered (")", stream);
380 }
381
382 /* Print out something of the form <s>(<arg>).
383 This is used to print out some builtin Modula-2
384 functions.
385 FIXME: There is probably some way to get the precedence
386 rules to do this (print a unary operand with parens around it). */
387 static void
388 print_simple_m2_func(s,exp,pos,stream)
389 char *s;
390 register struct expression *exp;
391 register int *pos;
392 FILE *stream;
393 {
394 fprintf(stream,"%s(",s);
395 print_subexp(exp,pos,stream,PREC_PREFIX);
396 fprintf(stream,")");
397 }
398
399 /* Return the operator corresponding to opcode OP as
400 a string. NULL indicates that the opcode was not found in the
401 current language table. */
402 char *
403 op_string(op)
404 enum exp_opcode op;
405 {
406 int tem;
407 register const struct op_print *op_print_tab;
408
409 op_print_tab = current_language->la_op_print_tab;
410 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
411 if (op_print_tab[tem].opcode == op)
412 return op_print_tab[tem].string;
413 return NULL;
414 }
415
416 #ifdef DEBUG_EXPRESSIONS
417
418 /* Support for dumping the raw data from expressions in a human readable
419 form. */
420
421 void
422 dump_expression (exp, stream, note)
423 struct expression *exp;
424 FILE *stream;
425 char *note;
426 {
427 int elt;
428 char *opcode_name;
429 char *eltscan;
430 int eltsize;
431
432 fprintf_filtered (stream, "Dump of expression @ 0x%x, %s:\n", exp, note);
433 fprintf_filtered (stream, "\tLanguage %s, %d elements, %d bytes each.\n",
434 exp->language_defn->la_name, exp -> nelts,
435 sizeof (union exp_element));
436 fprintf_filtered (stream, "\t%5s %20s %16s %s\n", "Index", "Opcode",
437 "Hex Value", "String Value");
438 for (elt = 0; elt < exp -> nelts; elt++)
439 {
440 fprintf_filtered (stream, "\t%5d ", elt);
441 switch (exp -> elts[elt].opcode)
442 {
443 default: opcode_name = "<unknown>"; break;
444 case OP_NULL: opcode_name = "OP_NULL"; break;
445 case BINOP_ADD: opcode_name = "BINOP_ADD"; break;
446 case BINOP_SUB: opcode_name = "BINOP_SUB"; break;
447 case BINOP_MUL: opcode_name = "BINOP_MUL"; break;
448 case BINOP_DIV: opcode_name = "BINOP_DIV"; break;
449 case BINOP_REM: opcode_name = "BINOP_REM"; break;
450 case BINOP_LSH: opcode_name = "BINOP_LSH"; break;
451 case BINOP_RSH: opcode_name = "BINOP_RSH"; break;
452 case BINOP_LOGICAL_AND: opcode_name = "BINOP_LOGICAL_AND"; break;
453 case BINOP_LOGICAL_OR: opcode_name = "BINOP_LOGICAL_OR"; break;
454 case BINOP_BITWISE_AND: opcode_name = "BINOP_BITWISE_AND"; break;
455 case BINOP_BITWISE_IOR: opcode_name = "BINOP_BITWISE_IOR"; break;
456 case BINOP_BITWISE_XOR: opcode_name = "BINOP_BITWISE_XOR"; break;
457 case BINOP_EQUAL: opcode_name = "BINOP_EQUAL"; break;
458 case BINOP_NOTEQUAL: opcode_name = "BINOP_NOTEQUAL"; break;
459 case BINOP_LESS: opcode_name = "BINOP_LESS"; break;
460 case BINOP_GTR: opcode_name = "BINOP_GTR"; break;
461 case BINOP_LEQ: opcode_name = "BINOP_LEQ"; break;
462 case BINOP_GEQ: opcode_name = "BINOP_GEQ"; break;
463 case BINOP_REPEAT: opcode_name = "BINOP_REPEAT"; break;
464 case BINOP_ASSIGN: opcode_name = "BINOP_ASSIGN"; break;
465 case BINOP_COMMA: opcode_name = "BINOP_COMMA"; break;
466 case BINOP_SUBSCRIPT: opcode_name = "BINOP_SUBSCRIPT"; break;
467 case MULTI_SUBSCRIPT: opcode_name = "MULTI_SUBSCRIPT"; break;
468 case BINOP_EXP: opcode_name = "BINOP_EXP"; break;
469 case BINOP_MIN: opcode_name = "BINOP_MIN"; break;
470 case BINOP_MAX: opcode_name = "BINOP_MAX"; break;
471 case BINOP_SCOPE: opcode_name = "BINOP_SCOPE"; break;
472 case STRUCTOP_MEMBER: opcode_name = "STRUCTOP_MEMBER"; break;
473 case STRUCTOP_MPTR: opcode_name = "STRUCTOP_MPTR"; break;
474 case BINOP_INTDIV: opcode_name = "BINOP_INTDIV"; break;
475 case BINOP_ASSIGN_MODIFY: opcode_name = "BINOP_ASSIGN_MODIFY"; break;
476 case BINOP_VAL: opcode_name = "BINOP_VAL"; break;
477 case BINOP_INCL: opcode_name = "BINOP_INCL"; break;
478 case BINOP_EXCL: opcode_name = "BINOP_EXCL"; break;
479 case BINOP_END: opcode_name = "BINOP_END"; break;
480 case TERNOP_COND: opcode_name = "TERNOP_COND"; break;
481 case OP_LONG: opcode_name = "OP_LONG"; break;
482 case OP_DOUBLE: opcode_name = "OP_DOUBLE"; break;
483 case OP_VAR_VALUE: opcode_name = "OP_VAR_VALUE"; break;
484 case OP_LAST: opcode_name = "OP_LAST"; break;
485 case OP_REGISTER: opcode_name = "OP_REGISTER"; break;
486 case OP_INTERNALVAR: opcode_name = "OP_INTERNALVAR"; break;
487 case OP_FUNCALL: opcode_name = "OP_FUNCALL"; break;
488 case OP_STRING: opcode_name = "OP_STRING"; break;
489 case UNOP_CAST: opcode_name = "UNOP_CAST"; break;
490 case UNOP_MEMVAL: opcode_name = "UNOP_MEMVAL"; break;
491 case UNOP_NEG: opcode_name = "UNOP_NEG"; break;
492 case UNOP_LOGICAL_NOT: opcode_name = "UNOP_LOGICAL_NOT"; break;
493 case UNOP_COMPLEMENT: opcode_name = "UNOP_COMPLEMENT"; break;
494 case UNOP_IND: opcode_name = "UNOP_IND"; break;
495 case UNOP_ADDR: opcode_name = "UNOP_ADDR"; break;
496 case UNOP_PREINCREMENT: opcode_name = "UNOP_PREINCREMENT"; break;
497 case UNOP_POSTINCREMENT: opcode_name = "UNOP_POSTINCREMENT"; break;
498 case UNOP_PREDECREMENT: opcode_name = "UNOP_PREDECREMENT"; break;
499 case UNOP_POSTDECREMENT: opcode_name = "UNOP_POSTDECREMENT"; break;
500 case UNOP_SIZEOF: opcode_name = "UNOP_SIZEOF"; break;
501 case UNOP_PLUS: opcode_name = "UNOP_PLUS"; break;
502 case UNOP_CAP: opcode_name = "UNOP_CAP"; break;
503 case UNOP_CHR: opcode_name = "UNOP_CHR"; break;
504 case UNOP_ORD: opcode_name = "UNOP_ORD"; break;
505 case UNOP_ABS: opcode_name = "UNOP_ABS"; break;
506 case UNOP_FLOAT: opcode_name = "UNOP_FLOAT"; break;
507 case UNOP_HIGH: opcode_name = "UNOP_HIGH"; break;
508 case UNOP_MAX: opcode_name = "UNOP_MAX"; break;
509 case UNOP_MIN: opcode_name = "UNOP_MIN"; break;
510 case UNOP_ODD: opcode_name = "UNOP_ODD"; break;
511 case UNOP_TRUNC: opcode_name = "UNOP_TRUNC"; break;
512 case OP_BOOL: opcode_name = "OP_BOOL"; break;
513 case OP_M2_STRING: opcode_name = "OP_M2_STRING"; break;
514 case STRUCTOP_STRUCT: opcode_name = "STRUCTOP_STRUCT"; break;
515 case STRUCTOP_PTR: opcode_name = "STRUCTOP_PTR"; break;
516 case OP_THIS: opcode_name = "OP_THIS"; break;
517 case OP_SCOPE: opcode_name = "OP_SCOPE"; break;
518 case OP_TYPE: opcode_name = "OP_TYPE"; break;
519 }
520 fprintf_filtered (stream, "%20s ", opcode_name);
521 fprintf_filtered (stream,
522 #if defined (LONG_LONG)
523 "%ll16x ",
524 #else
525 "%l16x ",
526 #endif
527 exp -> elts[elt].longconst);
528
529 for (eltscan = (char *) &exp->elts[elt],
530 eltsize = sizeof (union exp_element) ;
531 eltsize-- > 0;
532 eltscan++)
533 {
534 fprintf_filtered (stream, "%c",
535 isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
536 }
537 fprintf_filtered (stream, "\n");
538 }
539 }
540
541 #endif /* DEBUG_EXPRESSIONS */