1 /* Print in infix form a struct expression.
2 Copyright (C) 1986, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
23 #include "expression.h"
26 #include "parser-defs.h"
28 /* Prototypes for local functions */
31 print_subexp PARAMS ((struct expression *, int *, FILE *, enum precedence));
34 print_simple_m2_func PARAMS ((char *, struct expression *, int *, FILE *));
37 print_expression (exp, stream)
38 struct expression *exp;
42 print_subexp (exp, &pc, stream, PREC_NULL);
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. */
51 print_subexp (exp, pos, stream, prec)
52 register struct expression *exp;
57 register unsigned tem;
58 register const struct op_print *op_print_tab;
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. */
70 op_print_tab = exp->language_defn->la_op_print_tab;
72 opcode = exp->elts[pc].opcode;
80 fputs_filtered (type_name_no_tag (exp->elts[pc + 1].type), stream);
81 fputs_filtered ("::", stream);
82 nargs = longest_to_int (exp->elts[pc + 2].longconst);
83 (*pos) += 4 + BYTES_TO_EXP_ELEM (nargs + 1);
84 fputs_filtered (&exp->elts[pc + 3].string, stream);
89 value_print (value_from_longest (exp->elts[pc + 1].type,
90 exp->elts[pc + 2].longconst),
91 stream, 0, Val_no_prettyprint);
96 value_print (value_from_double (exp->elts[pc + 1].type,
97 exp->elts[pc + 2].doubleconst),
98 stream, 0, Val_no_prettyprint);
103 fputs_filtered (SYMBOL_SOURCE_NAME (exp->elts[pc + 1].symbol), stream);
108 fprintf_filtered (stream, "$%d",
109 longest_to_int (exp->elts[pc + 1].longconst));
114 fprintf_filtered (stream, "$%s",
115 reg_names[longest_to_int (exp->elts[pc + 1].longconst)]);
120 fprintf_filtered (stream, "%s",
121 longest_to_int (exp->elts[pc + 1].longconst)
127 fprintf_filtered (stream, "$%s",
128 internalvar_name (exp->elts[pc + 1].internalvar));
133 nargs = longest_to_int (exp->elts[pc + 1].longconst);
134 print_subexp (exp, pos, stream, PREC_SUFFIX);
135 fputs_filtered (" (", stream);
136 for (tem = 0; tem < nargs; tem++)
139 fputs_filtered (", ", stream);
140 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
142 fputs_filtered (")", stream);
146 nargs = longest_to_int (exp -> elts[pc + 1].longconst);
147 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
148 /* LA_PRINT_STRING will print using the current repeat count threshold.
149 If necessary, we can temporarily set it to zero, or pass it as an
150 additional parameter to LA_PRINT_STRING. -fnf */
151 LA_PRINT_STRING (stream, &exp->elts[pc + 2].string, nargs, 0);
155 error ("support for OP_BITSTRING unimplemented");
160 nargs = longest_to_int (exp->elts[pc + 2].longconst);
161 nargs -= longest_to_int (exp->elts[pc + 1].longconst);
164 if (exp->elts[pc + 4].opcode == OP_LONG
165 && exp->elts[pc + 5].type == builtin_type_char
166 && exp->language_defn->la_language == language_c)
168 /* Attempt to print C character arrays using string syntax.
169 Walk through the args, picking up one character from each
170 of the OP_LONG expression elements. If any array element
171 does not match our expection of what we should find for
172 a simple string, revert back to array printing. Note that
173 the last expression element is an explicit null terminator
174 byte, which doesn't get printed. */
175 tempstr = alloca (nargs);
179 if (exp->elts[pc].opcode != OP_LONG
180 || exp->elts[pc + 1].type != builtin_type_char)
182 /* Not a simple array of char, use regular array printing. */
189 longest_to_int (exp->elts[pc + 2].longconst);
196 LA_PRINT_STRING (stream, tempstr, nargs - 1, 0);
201 fputs_filtered (" {", stream);
202 for (tem = 0; tem < nargs; tem++)
206 fputs_filtered (", ", stream);
208 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
210 fputs_filtered ("}", stream);
215 if ((int) prec > (int) PREC_COMMA)
216 fputs_filtered ("(", stream);
217 /* Print the subexpressions, forcing parentheses
218 around any binary operations within them.
219 This is more parentheses than are strictly necessary,
220 but it looks clearer. */
221 print_subexp (exp, pos, stream, PREC_HYPER);
222 fputs_filtered (" ? ", stream);
223 print_subexp (exp, pos, stream, PREC_HYPER);
224 fputs_filtered (" : ", stream);
225 print_subexp (exp, pos, stream, PREC_HYPER);
226 if ((int) prec > (int) PREC_COMMA)
227 fputs_filtered (")", stream);
230 case STRUCTOP_STRUCT:
231 tem = longest_to_int (exp->elts[pc + 1].longconst);
232 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
233 print_subexp (exp, pos, stream, PREC_SUFFIX);
234 fputs_filtered (".", stream);
235 fputs_filtered (&exp->elts[pc + 2].string, stream);
238 /* Will not occur for Modula-2 */
240 tem = longest_to_int (exp->elts[pc + 1].longconst);
241 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
242 print_subexp (exp, pos, stream, PREC_SUFFIX);
243 fputs_filtered ("->", stream);
244 fputs_filtered (&exp->elts[pc + 2].string, stream);
247 case BINOP_SUBSCRIPT:
248 print_subexp (exp, pos, stream, PREC_SUFFIX);
249 fputs_filtered ("[", stream);
250 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
251 fputs_filtered ("]", stream);
254 case UNOP_POSTINCREMENT:
255 print_subexp (exp, pos, stream, PREC_SUFFIX);
256 fputs_filtered ("++", stream);
259 case UNOP_POSTDECREMENT:
260 print_subexp (exp, pos, stream, PREC_SUFFIX);
261 fputs_filtered ("--", stream);
266 if ((int) prec > (int) PREC_PREFIX)
267 fputs_filtered ("(", stream);
268 fputs_filtered ("(", stream);
269 type_print (exp->elts[pc + 1].type, "", stream, 0);
270 fputs_filtered (") ", stream);
271 print_subexp (exp, pos, stream, PREC_PREFIX);
272 if ((int) prec > (int) PREC_PREFIX)
273 fputs_filtered (")", stream);
278 if ((int) prec > (int) PREC_PREFIX)
279 fputs_filtered ("(", stream);
280 if (exp->elts[pc + 1].type->code == TYPE_CODE_FUNC &&
281 exp->elts[pc + 3].opcode == OP_LONG) {
282 /* We have a minimal symbol fn, probably. It's encoded
283 as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
284 Swallow the OP_LONG (including both its opcodes); ignore
285 its type; print the value in the type of the MEMVAL. */
287 val = value_at_lazy (exp->elts[pc + 1].type,
288 (CORE_ADDR) exp->elts[pc + 5].longconst);
289 value_print (val, stream, 0, Val_no_prettyprint);
291 fputs_filtered ("{", stream);
292 type_print (exp->elts[pc + 1].type, "", stream, 0);
293 fputs_filtered ("} ", stream);
294 print_subexp (exp, pos, stream, PREC_PREFIX);
296 if ((int) prec > (int) PREC_PREFIX)
297 fputs_filtered (")", stream);
300 case BINOP_ASSIGN_MODIFY:
301 opcode = exp->elts[pc + 1].opcode;
303 myprec = PREC_ASSIGN;
307 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
308 if (op_print_tab[tem].opcode == opcode)
310 op_str = op_print_tab[tem].string;
313 if (op_print_tab[tem].opcode != opcode)
314 /* Not found; don't try to keep going because we don't know how
315 to interpret further elements. */
316 error ("Invalid expression");
323 fputs_filtered ("this", stream);
328 case MULTI_SUBSCRIPT:
330 nargs = longest_to_int (exp->elts[pc + 1].longconst);
331 print_subexp (exp, pos, stream, PREC_SUFFIX);
332 fprintf (stream, " [");
333 for (tem = 0; tem < nargs; tem++)
336 fprintf (stream, ", ");
337 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
339 fprintf (stream, "]");
344 fprintf(stream,"VAL(");
345 type_print(exp->elts[pc+1].type,"",stream,0);
347 print_subexp(exp,pos,stream,PREC_PREFIX);
352 print_simple_m2_func("CAP",exp,pos,stream);
356 print_simple_m2_func("CHR",exp,pos,stream);
360 print_simple_m2_func("ORD",exp,pos,stream);
364 print_simple_m2_func("ABS",exp,pos,stream);
368 print_simple_m2_func("FLOAT",exp,pos,stream);
372 print_simple_m2_func("HIGH",exp,pos,stream);
376 print_simple_m2_func("MAX",exp,pos,stream);
380 print_simple_m2_func("MIN",exp,pos,stream);
384 print_simple_m2_func("ODD",exp,pos,stream);
388 print_simple_m2_func("TRUNC",exp,pos,stream);
393 error("print_subexp: Not implemented.");
399 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
400 if (op_print_tab[tem].opcode == opcode)
402 op_str = op_print_tab[tem].string;
403 myprec = op_print_tab[tem].precedence;
404 assoc = op_print_tab[tem].right_assoc;
407 if (op_print_tab[tem].opcode != opcode)
408 /* Not found; don't try to keep going because we don't know how
409 to interpret further elements. For example, this happens
410 if opcode is OP_TYPE. */
411 error ("Invalid expression");
414 if ((int) myprec < (int) prec)
415 fputs_filtered ("(", stream);
416 if ((int) opcode > (int) BINOP_END)
418 /* Unary prefix operator. */
419 fputs_filtered (op_str, stream);
420 print_subexp (exp, pos, stream, PREC_PREFIX);
424 /* Binary operator. */
425 /* Print left operand.
426 If operator is right-associative,
427 increment precedence for this operand. */
428 print_subexp (exp, pos, stream,
429 (enum precedence) ((int) myprec + assoc));
430 /* Print the operator itself. */
432 fprintf_filtered (stream, " %s= ", op_str);
433 else if (op_str[0] == ',')
434 fprintf_filtered (stream, "%s ", op_str);
436 fprintf_filtered (stream, " %s ", op_str);
437 /* Print right operand.
438 If operator is left-associative,
439 increment precedence for this operand. */
440 print_subexp (exp, pos, stream,
441 (enum precedence) ((int) myprec + !assoc));
444 if ((int) myprec < (int) prec)
445 fputs_filtered (")", stream);
448 /* Print out something of the form <s>(<arg>).
449 This is used to print out some builtin Modula-2
451 FIXME: There is probably some way to get the precedence
452 rules to do this (print a unary operand with parens around it). */
454 print_simple_m2_func(s,exp,pos,stream)
456 register struct expression *exp;
460 fprintf(stream,"%s(",s);
461 print_subexp(exp,pos,stream,PREC_PREFIX);
465 /* Return the operator corresponding to opcode OP as
466 a string. NULL indicates that the opcode was not found in the
467 current language table. */
473 register const struct op_print *op_print_tab;
475 op_print_tab = current_language->la_op_print_tab;
476 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
477 if (op_print_tab[tem].opcode == op)
478 return op_print_tab[tem].string;
482 #ifdef DEBUG_EXPRESSIONS
484 /* Support for dumping the raw data from expressions in a human readable
488 dump_expression (exp, stream, note)
489 struct expression *exp;
498 fprintf_filtered (stream, "Dump of expression @ 0x%x, %s:\n", exp, note);
499 fprintf_filtered (stream, "\tLanguage %s, %d elements, %d bytes each.\n",
500 exp->language_defn->la_name, exp -> nelts,
501 sizeof (union exp_element));
502 fprintf_filtered (stream, "\t%5s %20s %16s %s\n", "Index", "Opcode",
503 "Hex Value", "String Value");
504 for (elt = 0; elt < exp -> nelts; elt++)
506 fprintf_filtered (stream, "\t%5d ", elt);
507 switch (exp -> elts[elt].opcode)
509 default: opcode_name = "<unknown>"; break;
510 case OP_NULL: opcode_name = "OP_NULL"; break;
511 case BINOP_ADD: opcode_name = "BINOP_ADD"; break;
512 case BINOP_SUB: opcode_name = "BINOP_SUB"; break;
513 case BINOP_MUL: opcode_name = "BINOP_MUL"; break;
514 case BINOP_DIV: opcode_name = "BINOP_DIV"; break;
515 case BINOP_REM: opcode_name = "BINOP_REM"; break;
516 case BINOP_MOD: opcode_name = "BINOP_MOD"; break;
517 case BINOP_LSH: opcode_name = "BINOP_LSH"; break;
518 case BINOP_RSH: opcode_name = "BINOP_RSH"; break;
519 case BINOP_LOGICAL_AND: opcode_name = "BINOP_LOGICAL_AND"; break;
520 case BINOP_LOGICAL_OR: opcode_name = "BINOP_LOGICAL_OR"; break;
521 case BINOP_BITWISE_AND: opcode_name = "BINOP_BITWISE_AND"; break;
522 case BINOP_BITWISE_IOR: opcode_name = "BINOP_BITWISE_IOR"; break;
523 case BINOP_BITWISE_XOR: opcode_name = "BINOP_BITWISE_XOR"; break;
524 case BINOP_EQUAL: opcode_name = "BINOP_EQUAL"; break;
525 case BINOP_NOTEQUAL: opcode_name = "BINOP_NOTEQUAL"; break;
526 case BINOP_LESS: opcode_name = "BINOP_LESS"; break;
527 case BINOP_GTR: opcode_name = "BINOP_GTR"; break;
528 case BINOP_LEQ: opcode_name = "BINOP_LEQ"; break;
529 case BINOP_GEQ: opcode_name = "BINOP_GEQ"; break;
530 case BINOP_REPEAT: opcode_name = "BINOP_REPEAT"; break;
531 case BINOP_ASSIGN: opcode_name = "BINOP_ASSIGN"; break;
532 case BINOP_COMMA: opcode_name = "BINOP_COMMA"; break;
533 case BINOP_SUBSCRIPT: opcode_name = "BINOP_SUBSCRIPT"; break;
534 case MULTI_SUBSCRIPT: opcode_name = "MULTI_SUBSCRIPT"; break;
535 case BINOP_EXP: opcode_name = "BINOP_EXP"; break;
536 case BINOP_MIN: opcode_name = "BINOP_MIN"; break;
537 case BINOP_MAX: opcode_name = "BINOP_MAX"; break;
538 case BINOP_SCOPE: opcode_name = "BINOP_SCOPE"; break;
539 case STRUCTOP_MEMBER: opcode_name = "STRUCTOP_MEMBER"; break;
540 case STRUCTOP_MPTR: opcode_name = "STRUCTOP_MPTR"; break;
541 case BINOP_INTDIV: opcode_name = "BINOP_INTDIV"; break;
542 case BINOP_ASSIGN_MODIFY: opcode_name = "BINOP_ASSIGN_MODIFY"; break;
543 case BINOP_VAL: opcode_name = "BINOP_VAL"; break;
544 case BINOP_INCL: opcode_name = "BINOP_INCL"; break;
545 case BINOP_EXCL: opcode_name = "BINOP_EXCL"; break;
546 case BINOP_CONCAT: opcode_name = "BINOP_CONCAT"; break;
547 case BINOP_END: opcode_name = "BINOP_END"; break;
548 case TERNOP_COND: opcode_name = "TERNOP_COND"; break;
549 case OP_LONG: opcode_name = "OP_LONG"; break;
550 case OP_DOUBLE: opcode_name = "OP_DOUBLE"; break;
551 case OP_VAR_VALUE: opcode_name = "OP_VAR_VALUE"; break;
552 case OP_LAST: opcode_name = "OP_LAST"; break;
553 case OP_REGISTER: opcode_name = "OP_REGISTER"; break;
554 case OP_INTERNALVAR: opcode_name = "OP_INTERNALVAR"; break;
555 case OP_FUNCALL: opcode_name = "OP_FUNCALL"; break;
556 case OP_STRING: opcode_name = "OP_STRING"; break;
557 case OP_BITSTRING: opcode_name = "OP_BITSTRING"; break;
558 case OP_ARRAY: opcode_name = "OP_ARRAY"; break;
559 case UNOP_CAST: opcode_name = "UNOP_CAST"; break;
560 case UNOP_MEMVAL: opcode_name = "UNOP_MEMVAL"; break;
561 case UNOP_NEG: opcode_name = "UNOP_NEG"; break;
562 case UNOP_LOGICAL_NOT: opcode_name = "UNOP_LOGICAL_NOT"; break;
563 case UNOP_COMPLEMENT: opcode_name = "UNOP_COMPLEMENT"; break;
564 case UNOP_IND: opcode_name = "UNOP_IND"; break;
565 case UNOP_ADDR: opcode_name = "UNOP_ADDR"; break;
566 case UNOP_PREINCREMENT: opcode_name = "UNOP_PREINCREMENT"; break;
567 case UNOP_POSTINCREMENT: opcode_name = "UNOP_POSTINCREMENT"; break;
568 case UNOP_PREDECREMENT: opcode_name = "UNOP_PREDECREMENT"; break;
569 case UNOP_POSTDECREMENT: opcode_name = "UNOP_POSTDECREMENT"; break;
570 case UNOP_SIZEOF: opcode_name = "UNOP_SIZEOF"; break;
571 case UNOP_PLUS: opcode_name = "UNOP_PLUS"; break;
572 case UNOP_CAP: opcode_name = "UNOP_CAP"; break;
573 case UNOP_CHR: opcode_name = "UNOP_CHR"; break;
574 case UNOP_ORD: opcode_name = "UNOP_ORD"; break;
575 case UNOP_ABS: opcode_name = "UNOP_ABS"; break;
576 case UNOP_FLOAT: opcode_name = "UNOP_FLOAT"; break;
577 case UNOP_HIGH: opcode_name = "UNOP_HIGH"; break;
578 case UNOP_MAX: opcode_name = "UNOP_MAX"; break;
579 case UNOP_MIN: opcode_name = "UNOP_MIN"; break;
580 case UNOP_ODD: opcode_name = "UNOP_ODD"; break;
581 case UNOP_TRUNC: opcode_name = "UNOP_TRUNC"; break;
582 case OP_BOOL: opcode_name = "OP_BOOL"; break;
583 case OP_M2_STRING: opcode_name = "OP_M2_STRING"; break;
584 case STRUCTOP_STRUCT: opcode_name = "STRUCTOP_STRUCT"; break;
585 case STRUCTOP_PTR: opcode_name = "STRUCTOP_PTR"; break;
586 case OP_THIS: opcode_name = "OP_THIS"; break;
587 case OP_SCOPE: opcode_name = "OP_SCOPE"; break;
588 case OP_TYPE: opcode_name = "OP_TYPE"; break;
590 fprintf_filtered (stream, "%20s ", opcode_name);
591 fprintf_filtered (stream,
592 #if defined (LONG_LONG)
597 exp -> elts[elt].longconst);
599 for (eltscan = (char *) &exp->elts[elt],
600 eltsize = sizeof (union exp_element) ;
604 fprintf_filtered (stream, "%c",
605 isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
607 fprintf_filtered (stream, "\n");
611 #endif /* DEBUG_EXPRESSIONS */