1 /* Print in infix form a struct expression.
2 Copyright (C) 1986, 1989, 1991, 2000 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., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "expression.h"
27 #include "parser-defs.h"
33 /* Prototypes for local functions */
35 static void print_subexp (struct expression *, int *, struct ui_file *,
39 print_expression (exp, stream)
40 struct expression *exp;
41 struct ui_file *stream;
44 print_subexp (exp, &pc, stream, PREC_NULL);
47 /* Print the subexpression of EXP that starts in position POS, on STREAM.
48 PREC is the precedence of the surrounding operator;
49 if the precedence of the main operator of this subexpression is less,
50 parentheses are needed here. */
53 print_subexp (exp, pos, stream, prec)
54 register struct expression *exp;
56 struct ui_file *stream;
59 register unsigned tem;
60 register const struct op_print *op_print_tab;
63 register char *op_str;
64 int assign_modify = 0;
65 enum exp_opcode opcode;
66 enum precedence myprec = PREC_NULL;
67 /* Set to 1 for a right-associative operator. */
72 op_print_tab = exp->language_defn->la_op_print_tab;
74 opcode = exp->elts[pc].opcode;
82 fputs_filtered (type_name_no_tag (exp->elts[pc + 1].type), stream);
83 fputs_filtered ("::", stream);
84 nargs = longest_to_int (exp->elts[pc + 2].longconst);
85 (*pos) += 4 + BYTES_TO_EXP_ELEM (nargs + 1);
86 fputs_filtered (&exp->elts[pc + 3].string, stream);
91 value_print (value_from_longest (exp->elts[pc + 1].type,
92 exp->elts[pc + 2].longconst),
93 stream, 0, Val_no_prettyprint);
98 value_print (value_from_double (exp->elts[pc + 1].type,
99 exp->elts[pc + 2].doubleconst),
100 stream, 0, Val_no_prettyprint);
107 b = exp->elts[pc + 1].block;
109 && BLOCK_FUNCTION (b) != NULL
110 && SYMBOL_SOURCE_NAME (BLOCK_FUNCTION (b)) != NULL)
112 fputs_filtered (SYMBOL_SOURCE_NAME (BLOCK_FUNCTION (b)), stream);
113 fputs_filtered ("::", stream);
115 fputs_filtered (SYMBOL_SOURCE_NAME (exp->elts[pc + 2].symbol), stream);
121 fprintf_filtered (stream, "$%d",
122 longest_to_int (exp->elts[pc + 1].longconst));
127 fprintf_filtered (stream, "$%s",
128 REGISTER_NAME (longest_to_int (exp->elts[pc + 1].longconst)));
133 fprintf_filtered (stream, "%s",
134 longest_to_int (exp->elts[pc + 1].longconst)
140 fprintf_filtered (stream, "$%s",
141 internalvar_name (exp->elts[pc + 1].internalvar));
146 nargs = longest_to_int (exp->elts[pc + 1].longconst);
147 print_subexp (exp, pos, stream, PREC_SUFFIX);
148 fputs_filtered (" (", stream);
149 for (tem = 0; tem < nargs; tem++)
152 fputs_filtered (", ", stream);
153 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
155 fputs_filtered (")", stream);
160 nargs = longest_to_int (exp->elts[pc + 1].longconst);
161 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
162 fputs_filtered (&exp->elts[pc + 2].string, stream);
166 nargs = longest_to_int (exp->elts[pc + 1].longconst);
167 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
168 /* LA_PRINT_STRING will print using the current repeat count threshold.
169 If necessary, we can temporarily set it to zero, or pass it as an
170 additional parameter to LA_PRINT_STRING. -fnf */
171 LA_PRINT_STRING (stream, &exp->elts[pc + 2].string, nargs, 1, 0);
175 nargs = longest_to_int (exp->elts[pc + 1].longconst);
177 += 3 + BYTES_TO_EXP_ELEM ((nargs + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
178 fprintf_unfiltered (stream, "B'<unimplemented>'");
183 nargs = longest_to_int (exp->elts[pc + 2].longconst);
184 nargs -= longest_to_int (exp->elts[pc + 1].longconst);
187 if (exp->elts[pc + 4].opcode == OP_LONG
188 && exp->elts[pc + 5].type == builtin_type_char
189 && exp->language_defn->la_language == language_c)
191 /* Attempt to print C character arrays using string syntax.
192 Walk through the args, picking up one character from each
193 of the OP_LONG expression elements. If any array element
194 does not match our expection of what we should find for
195 a simple string, revert back to array printing. Note that
196 the last expression element is an explicit null terminator
197 byte, which doesn't get printed. */
198 tempstr = alloca (nargs);
202 if (exp->elts[pc].opcode != OP_LONG
203 || exp->elts[pc + 1].type != builtin_type_char)
205 /* Not a simple array of char, use regular array printing. */
212 longest_to_int (exp->elts[pc + 2].longconst);
219 LA_PRINT_STRING (stream, tempstr, nargs - 1, 1, 0);
224 int is_chill = exp->language_defn->la_language == language_chill;
225 fputs_filtered (is_chill ? " [" : " {", stream);
226 for (tem = 0; tem < nargs; tem++)
230 fputs_filtered (", ", stream);
232 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
234 fputs_filtered (is_chill ? "]" : "}", stream);
239 tem = longest_to_int (exp->elts[pc + 1].longconst);
240 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
242 if (exp->language_defn->la_language == language_chill)
244 fputs_filtered (".", stream);
245 fputs_filtered (&exp->elts[pc + 2].string, stream);
246 fputs_filtered (exp->elts[*pos].opcode == OP_LABELED ? ", "
252 /* Gcc support both these syntaxes. Unsure which is preferred. */
254 fputs_filtered (&exp->elts[pc + 2].string, stream);
255 fputs_filtered (": ", stream);
257 fputs_filtered (".", stream);
258 fputs_filtered (&exp->elts[pc + 2].string, stream);
259 fputs_filtered ("=", stream);
262 print_subexp (exp, pos, stream, PREC_SUFFIX);
266 if ((int) prec > (int) PREC_COMMA)
267 fputs_filtered ("(", stream);
268 /* Print the subexpressions, forcing parentheses
269 around any binary operations within them.
270 This is more parentheses than are strictly necessary,
271 but it looks clearer. */
272 print_subexp (exp, pos, stream, PREC_HYPER);
273 fputs_filtered (" ? ", stream);
274 print_subexp (exp, pos, stream, PREC_HYPER);
275 fputs_filtered (" : ", stream);
276 print_subexp (exp, pos, stream, PREC_HYPER);
277 if ((int) prec > (int) PREC_COMMA)
278 fputs_filtered (")", stream);
282 case TERNOP_SLICE_COUNT:
283 print_subexp (exp, pos, stream, PREC_SUFFIX);
284 fputs_filtered ("(", stream);
285 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
286 fputs_filtered (opcode == TERNOP_SLICE ? " : " : " UP ", stream);
287 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
288 fputs_filtered (")", stream);
291 case STRUCTOP_STRUCT:
292 tem = longest_to_int (exp->elts[pc + 1].longconst);
293 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
294 print_subexp (exp, pos, stream, PREC_SUFFIX);
295 fputs_filtered (".", stream);
296 fputs_filtered (&exp->elts[pc + 2].string, stream);
299 /* Will not occur for Modula-2 */
301 tem = longest_to_int (exp->elts[pc + 1].longconst);
302 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
303 print_subexp (exp, pos, stream, PREC_SUFFIX);
304 fputs_filtered ("->", stream);
305 fputs_filtered (&exp->elts[pc + 2].string, stream);
308 case BINOP_SUBSCRIPT:
309 print_subexp (exp, pos, stream, PREC_SUFFIX);
310 fputs_filtered ("[", stream);
311 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
312 fputs_filtered ("]", stream);
315 case UNOP_POSTINCREMENT:
316 print_subexp (exp, pos, stream, PREC_SUFFIX);
317 fputs_filtered ("++", stream);
320 case UNOP_POSTDECREMENT:
321 print_subexp (exp, pos, stream, PREC_SUFFIX);
322 fputs_filtered ("--", stream);
327 if ((int) prec > (int) PREC_PREFIX)
328 fputs_filtered ("(", stream);
329 fputs_filtered ("(", stream);
330 type_print (exp->elts[pc + 1].type, "", stream, 0);
331 fputs_filtered (") ", stream);
332 print_subexp (exp, pos, stream, PREC_PREFIX);
333 if ((int) prec > (int) PREC_PREFIX)
334 fputs_filtered (")", stream);
339 if ((int) prec > (int) PREC_PREFIX)
340 fputs_filtered ("(", stream);
341 if (exp->elts[pc + 1].type->code == TYPE_CODE_FUNC &&
342 exp->elts[pc + 3].opcode == OP_LONG)
344 /* We have a minimal symbol fn, probably. It's encoded
345 as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
346 Swallow the OP_LONG (including both its opcodes); ignore
347 its type; print the value in the type of the MEMVAL. */
349 val = value_at_lazy (exp->elts[pc + 1].type,
350 (CORE_ADDR) exp->elts[pc + 5].longconst,
352 value_print (val, stream, 0, Val_no_prettyprint);
356 fputs_filtered ("{", stream);
357 type_print (exp->elts[pc + 1].type, "", stream, 0);
358 fputs_filtered ("} ", stream);
359 print_subexp (exp, pos, stream, PREC_PREFIX);
361 if ((int) prec > (int) PREC_PREFIX)
362 fputs_filtered (")", stream);
365 case BINOP_ASSIGN_MODIFY:
366 opcode = exp->elts[pc + 1].opcode;
368 myprec = PREC_ASSIGN;
372 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
373 if (op_print_tab[tem].opcode == opcode)
375 op_str = op_print_tab[tem].string;
378 if (op_print_tab[tem].opcode != opcode)
379 /* Not found; don't try to keep going because we don't know how
380 to interpret further elements. */
381 error ("Invalid expression");
388 fputs_filtered ("this", stream);
393 case MULTI_SUBSCRIPT:
395 nargs = longest_to_int (exp->elts[pc + 1].longconst);
396 print_subexp (exp, pos, stream, PREC_SUFFIX);
397 fprintf_unfiltered (stream, " [");
398 for (tem = 0; tem < nargs; tem++)
401 fprintf_unfiltered (stream, ", ");
402 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
404 fprintf_unfiltered (stream, "]");
409 fprintf_unfiltered (stream, "VAL(");
410 type_print (exp->elts[pc + 1].type, "", stream, 0);
411 fprintf_unfiltered (stream, ",");
412 print_subexp (exp, pos, stream, PREC_PREFIX);
413 fprintf_unfiltered (stream, ")");
418 error ("print_subexp: Not implemented.");
424 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
425 if (op_print_tab[tem].opcode == opcode)
427 op_str = op_print_tab[tem].string;
428 myprec = op_print_tab[tem].precedence;
429 assoc = op_print_tab[tem].right_assoc;
432 if (op_print_tab[tem].opcode != opcode)
433 /* Not found; don't try to keep going because we don't know how
434 to interpret further elements. For example, this happens
435 if opcode is OP_TYPE. */
436 error ("Invalid expression");
439 /* Note that PREC_BUILTIN will always emit parentheses. */
440 if ((int) myprec < (int) prec)
441 fputs_filtered ("(", stream);
442 if ((int) opcode > (int) BINOP_END)
446 /* Unary postfix operator. */
447 print_subexp (exp, pos, stream, PREC_SUFFIX);
448 fputs_filtered (op_str, stream);
452 /* Unary prefix operator. */
453 fputs_filtered (op_str, stream);
454 if (myprec == PREC_BUILTIN_FUNCTION)
455 fputs_filtered ("(", stream);
456 print_subexp (exp, pos, stream, PREC_PREFIX);
457 if (myprec == PREC_BUILTIN_FUNCTION)
458 fputs_filtered (")", stream);
463 /* Binary operator. */
464 /* Print left operand.
465 If operator is right-associative,
466 increment precedence for this operand. */
467 print_subexp (exp, pos, stream,
468 (enum precedence) ((int) myprec + assoc));
469 /* Print the operator itself. */
471 fprintf_filtered (stream, " %s= ", op_str);
472 else if (op_str[0] == ',')
473 fprintf_filtered (stream, "%s ", op_str);
475 fprintf_filtered (stream, " %s ", op_str);
476 /* Print right operand.
477 If operator is left-associative,
478 increment precedence for this operand. */
479 print_subexp (exp, pos, stream,
480 (enum precedence) ((int) myprec + !assoc));
483 if ((int) myprec < (int) prec)
484 fputs_filtered (")", stream);
487 /* Return the operator corresponding to opcode OP as
488 a string. NULL indicates that the opcode was not found in the
489 current language table. */
495 register const struct op_print *op_print_tab;
497 op_print_tab = current_language->la_op_print_tab;
498 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
499 if (op_print_tab[tem].opcode == op)
500 return op_print_tab[tem].string;
504 /* Support for dumping the raw data from expressions in a human readable
507 static char *op_name (int opcode);
519 sprintf (buf, "<unknown %d>", opcode);
540 case BINOP_LOGICAL_AND:
541 return "BINOP_LOGICAL_AND";
542 case BINOP_LOGICAL_OR:
543 return "BINOP_LOGICAL_OR";
544 case BINOP_BITWISE_AND:
545 return "BINOP_BITWISE_AND";
546 case BINOP_BITWISE_IOR:
547 return "BINOP_BITWISE_IOR";
548 case BINOP_BITWISE_XOR:
549 return "BINOP_BITWISE_XOR";
551 return "BINOP_EQUAL";
553 return "BINOP_NOTEQUAL";
563 return "BINOP_REPEAT";
565 return "BINOP_ASSIGN";
567 return "BINOP_COMMA";
568 case BINOP_SUBSCRIPT:
569 return "BINOP_SUBSCRIPT";
570 case MULTI_SUBSCRIPT:
571 return "MULTI_SUBSCRIPT";
578 case STRUCTOP_MEMBER:
579 return "STRUCTOP_MEMBER";
581 return "STRUCTOP_MPTR";
583 return "BINOP_INTDIV";
584 case BINOP_ASSIGN_MODIFY:
585 return "BINOP_ASSIGN_MODIFY";
593 return "BINOP_CONCAT";
595 return "BINOP_RANGE";
599 return "TERNOP_COND";
601 return "TERNOP_SLICE";
602 case TERNOP_SLICE_COUNT:
603 return "TERNOP_SLICE_COUNT";
609 return "OP_VAR_VALUE";
613 return "OP_REGISTER";
615 return "OP_INTERNALVAR";
621 return "OP_BITSTRING";
627 return "UNOP_MEMVAL";
630 case UNOP_LOGICAL_NOT:
631 return "UNOP_LOGICAL_NOT";
632 case UNOP_COMPLEMENT:
633 return "UNOP_COMPLEMENT";
638 case UNOP_PREINCREMENT:
639 return "UNOP_PREINCREMENT";
640 case UNOP_POSTINCREMENT:
641 return "UNOP_POSTINCREMENT";
642 case UNOP_PREDECREMENT:
643 return "UNOP_PREDECREMENT";
644 case UNOP_POSTDECREMENT:
645 return "UNOP_POSTDECREMENT";
647 return "UNOP_SIZEOF";
653 return "UNOP_LENGTH";
679 return "OP_M2_STRING";
680 case STRUCTOP_STRUCT:
681 return "STRUCTOP_STRUCT";
683 return "STRUCTOP_PTR";
696 dump_prefix_expression (exp, stream, note)
697 struct expression *exp;
698 struct ui_file *stream;
706 fprintf_filtered (stream, "Dump of expression @ ");
707 gdb_print_host_address (exp, stream);
708 fprintf_filtered (stream, ", %s:\nExpression: `", note);
709 if (exp->elts[0].opcode != OP_TYPE)
710 print_expression (exp, stream);
712 fprintf_filtered (stream, "Type printing not yet supported....");
713 fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
714 exp->language_defn->la_name, exp->nelts,
715 (long) sizeof (union exp_element));
716 fprintf_filtered (stream, "\t%5s %20s %16s %s\n", "Index", "Opcode",
717 "Hex Value", "String Value");
718 for (elt = 0; elt < exp->nelts; elt++)
720 fprintf_filtered (stream, "\t%5d ", elt);
721 opcode_name = op_name (exp->elts[elt].opcode);
723 fprintf_filtered (stream, "%20s ", opcode_name);
724 print_longest (stream, 'd', 0, exp->elts[elt].longconst);
725 fprintf_filtered (stream, " ");
727 for (eltscan = (char *) &exp->elts[elt],
728 eltsize = sizeof (union exp_element);
732 fprintf_filtered (stream, "%c",
733 isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
735 fprintf_filtered (stream, "\n");
739 static int dump_subexp (struct expression *exp, struct ui_file *stream,
743 dump_subexp (exp, stream, elt)
744 struct expression *exp;
745 struct ui_file *stream;
748 static int indent = 0;
751 fprintf_filtered (stream, "\n");
752 fprintf_filtered (stream, "\t%5d ", elt);
754 for (i = 1; i <= indent; i++)
755 fprintf_filtered (stream, " ");
758 fprintf_filtered (stream, "%-20s ", op_name (exp->elts[elt].opcode));
760 switch (exp->elts[elt++].opcode)
764 case TERNOP_SLICE_COUNT:
765 elt = dump_subexp (exp, stream, elt);
774 case BINOP_LOGICAL_AND:
775 case BINOP_LOGICAL_OR:
776 case BINOP_BITWISE_AND:
777 case BINOP_BITWISE_IOR:
778 case BINOP_BITWISE_XOR:
788 case BINOP_SUBSCRIPT:
793 case BINOP_ASSIGN_MODIFY:
801 elt = dump_subexp (exp, stream, elt);
803 case UNOP_LOGICAL_NOT:
804 case UNOP_COMPLEMENT:
807 case UNOP_PREINCREMENT:
808 case UNOP_POSTINCREMENT:
809 case UNOP_PREDECREMENT:
810 case UNOP_POSTDECREMENT:
829 elt = dump_subexp (exp, stream, elt);
832 fprintf_filtered (stream, "Type @");
833 gdb_print_host_address (exp->elts[elt].type, stream);
834 fprintf_filtered (stream, " (");
835 type_print (exp->elts[elt].type, NULL, stream, 0);
836 fprintf_filtered (stream, "), value %ld (0x%lx)",
837 (long) exp->elts[elt + 1].longconst,
838 (long) exp->elts[elt + 1].longconst);
842 fprintf_filtered (stream, "Type @");
843 gdb_print_host_address (exp->elts[elt].type, stream);
844 fprintf_filtered (stream, " (");
845 type_print (exp->elts[elt].type, NULL, stream, 0);
846 fprintf_filtered (stream, "), value %g",
847 (double) exp->elts[elt + 1].doubleconst);
851 fprintf_filtered (stream, "Block @");
852 gdb_print_host_address (exp->elts[elt].block, stream);
853 fprintf_filtered (stream, ", symbol @");
854 gdb_print_host_address (exp->elts[elt + 1].symbol, stream);
855 fprintf_filtered (stream, " (%s)",
856 SYMBOL_NAME (exp->elts[elt + 1].symbol));
860 fprintf_filtered (stream, "History element %ld",
861 (long) exp->elts[elt].longconst);
865 fprintf_filtered (stream, "Register %ld",
866 (long) exp->elts[elt].longconst);
870 fprintf_filtered (stream, "Internal var @");
871 gdb_print_host_address (exp->elts[elt].internalvar, stream);
872 fprintf_filtered (stream, " (%s)",
873 exp->elts[elt].internalvar->name);
880 nargs = longest_to_int (exp->elts[elt].longconst);
882 fprintf_filtered (stream, "Number of args: %d", nargs);
885 for (i = 1; i <= nargs + 1; i++)
886 elt = dump_subexp (exp, stream, elt);
894 lower = longest_to_int (exp->elts[elt].longconst);
895 upper = longest_to_int (exp->elts[elt + 1].longconst);
897 fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper);
900 for (i = 1; i <= upper - lower + 1; i++)
901 elt = dump_subexp (exp, stream, elt);
906 fprintf_filtered (stream, "Type @");
907 gdb_print_host_address (exp->elts[elt].type, stream);
908 fprintf_filtered (stream, " (");
909 type_print (exp->elts[elt].type, NULL, stream, 0);
910 fprintf_filtered (stream, ")");
911 elt = dump_subexp (exp, stream, elt + 2);
914 fprintf_filtered (stream, "Type @");
915 gdb_print_host_address (exp->elts[elt].type, stream);
916 fprintf_filtered (stream, " (");
917 type_print (exp->elts[elt].type, NULL, stream, 0);
918 fprintf_filtered (stream, ")");
921 case STRUCTOP_STRUCT:
927 len = longest_to_int (exp->elts[elt].longconst);
928 elem_name = &exp->elts[elt + 1].string;
930 fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name);
931 elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1));
939 fprintf_filtered (stream, "Type @");
940 gdb_print_host_address (exp->elts[elt].type, stream);
941 fprintf_filtered (stream, " (");
942 type_print (exp->elts[elt].type, NULL, stream, 0);
943 fprintf_filtered (stream, ") ");
945 len = longest_to_int (exp->elts[elt + 1].longconst);
946 elem_name = &exp->elts[elt + 2].string;
948 fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name);
949 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
954 case STRUCTOP_MEMBER:
956 case MULTI_SUBSCRIPT:
957 case OP_F77_UNDETERMINED_ARGLIST:
967 fprintf_filtered (stream, "Unknown format");
976 dump_postfix_expression (exp, stream, note)
977 struct expression *exp;
978 struct ui_file *stream;
983 fprintf_filtered (stream, "Dump of expression @ ");
984 gdb_print_host_address (exp, stream);
985 fprintf_filtered (stream, ", %s:\nExpression: `", note);
986 if (exp->elts[0].opcode != OP_TYPE)
987 print_expression (exp, stream);
989 fputs_filtered ("Type printing not yet supported....", stream);
990 fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
991 exp->language_defn->la_name, exp->nelts,
992 (long) sizeof (union exp_element));
993 fputs_filtered ("\n", stream);
995 for (elt = 0; elt < exp->nelts;)
996 elt = dump_subexp (exp, stream, elt);
997 fputs_filtered ("\n", stream);