* reloc.c: Add BFD_RELOC_RX_OP_NEG.
[deliverable/binutils-gdb.git] / gdb / expprint.c
CommitLineData
c906108c 1/* Print in infix form a struct expression.
1bac305b 2
197e01b6 3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
7b6bb8da 4 1998, 1999, 2000, 2003, 2007, 2008, 2009, 2010, 2011
4c38e0a4 5 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
23#include "symtab.h"
24#include "gdbtypes.h"
25#include "expression.h"
26#include "value.h"
27#include "language.h"
28#include "parser-defs.h"
eb8bc282 29#include "user-regs.h" /* For user_reg_map_regnum_to_name. */
82eeeb94
AF
30#include "target.h"
31#include "gdb_string.h"
fe898f56 32#include "block.h"
9e35dae4 33#include "objfiles.h"
fcd776e5 34#include "gdb_assert.h"
79a45b7d 35#include "valprint.h"
c906108c
SS
36
37#ifdef HAVE_CTYPE_H
38#include <ctype.h>
39#endif
40
c906108c 41void
fba45db2 42print_expression (struct expression *exp, struct ui_file *stream)
c906108c
SS
43{
44 int pc = 0;
d7f9d729 45
c906108c
SS
46 print_subexp (exp, &pc, stream, PREC_NULL);
47}
48
49/* Print the subexpression of EXP that starts in position POS, on STREAM.
50 PREC is the precedence of the surrounding operator;
51 if the precedence of the main operator of this subexpression is less,
52 parentheses are needed here. */
53
5f9769d1 54void
f86f5ca3 55print_subexp (struct expression *exp, int *pos,
fba45db2 56 struct ui_file *stream, enum precedence prec)
5f9769d1
PH
57{
58 exp->language_defn->la_exp_desc->print_subexp (exp, pos, stream, prec);
59}
60
61/* Standard implementation of print_subexp for use in language_defn
62 vectors. */
63void
64print_subexp_standard (struct expression *exp, int *pos,
65 struct ui_file *stream, enum precedence prec)
c906108c 66{
f86f5ca3
PH
67 unsigned tem;
68 const struct op_print *op_print_tab;
69 int pc;
c906108c 70 unsigned nargs;
f86f5ca3 71 char *op_str;
c906108c
SS
72 int assign_modify = 0;
73 enum exp_opcode opcode;
74 enum precedence myprec = PREC_NULL;
75 /* Set to 1 for a right-associative operator. */
76 int assoc = 0;
3d6d86c6 77 struct value *val;
c906108c
SS
78 char *tempstr = NULL;
79
80 op_print_tab = exp->language_defn->la_op_print_tab;
81 pc = (*pos)++;
82 opcode = exp->elts[pc].opcode;
83 switch (opcode)
84 {
c5aa993b 85 /* Common ops */
c906108c
SS
86
87 case OP_SCOPE:
88 myprec = PREC_PREFIX;
89 assoc = 0;
90 fputs_filtered (type_name_no_tag (exp->elts[pc + 1].type), stream);
91 fputs_filtered ("::", stream);
92 nargs = longest_to_int (exp->elts[pc + 2].longconst);
93 (*pos) += 4 + BYTES_TO_EXP_ELEM (nargs + 1);
94 fputs_filtered (&exp->elts[pc + 3].string, stream);
95 return;
96
97 case OP_LONG:
79a45b7d
TT
98 {
99 struct value_print_options opts;
d7f9d729 100
79a45b7d
TT
101 get_raw_print_options (&opts);
102 (*pos) += 3;
103 value_print (value_from_longest (exp->elts[pc + 1].type,
104 exp->elts[pc + 2].longconst),
105 stream, &opts);
106 }
c906108c
SS
107 return;
108
109 case OP_DOUBLE:
79a45b7d
TT
110 {
111 struct value_print_options opts;
d7f9d729 112
79a45b7d
TT
113 get_raw_print_options (&opts);
114 (*pos) += 3;
115 value_print (value_from_double (exp->elts[pc + 1].type,
116 exp->elts[pc + 2].doubleconst),
117 stream, &opts);
118 }
c906108c
SS
119 return;
120
121 case OP_VAR_VALUE:
122 {
123 struct block *b;
d7f9d729 124
c906108c
SS
125 (*pos) += 3;
126 b = exp->elts[pc + 1].block;
127 if (b != NULL
128 && BLOCK_FUNCTION (b) != NULL
de5ad195 129 && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)) != NULL)
c906108c 130 {
de5ad195 131 fputs_filtered (SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)), stream);
c906108c
SS
132 fputs_filtered ("::", stream);
133 }
de5ad195 134 fputs_filtered (SYMBOL_PRINT_NAME (exp->elts[pc + 2].symbol), stream);
c906108c
SS
135 }
136 return;
137
138 case OP_LAST:
139 (*pos) += 2;
140 fprintf_filtered (stream, "$%d",
141 longest_to_int (exp->elts[pc + 1].longconst));
142 return;
143
144 case OP_REGISTER:
e36180d7 145 {
67f3407f 146 const char *name = &exp->elts[pc + 2].string;
d7f9d729 147
67f3407f 148 (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
eb8bc282 149 fprintf_filtered (stream, "$%s", name);
e36180d7
AC
150 return;
151 }
c906108c
SS
152
153 case OP_BOOL:
154 (*pos) += 2;
155 fprintf_filtered (stream, "%s",
156 longest_to_int (exp->elts[pc + 1].longconst)
157 ? "TRUE" : "FALSE");
158 return;
159
160 case OP_INTERNALVAR:
161 (*pos) += 2;
162 fprintf_filtered (stream, "$%s",
c5aa993b 163 internalvar_name (exp->elts[pc + 1].internalvar));
c906108c
SS
164 return;
165
166 case OP_FUNCALL:
167 (*pos) += 2;
168 nargs = longest_to_int (exp->elts[pc + 1].longconst);
169 print_subexp (exp, pos, stream, PREC_SUFFIX);
170 fputs_filtered (" (", stream);
171 for (tem = 0; tem < nargs; tem++)
172 {
173 if (tem != 0)
174 fputs_filtered (", ", stream);
175 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
176 }
177 fputs_filtered (")", stream);
178 return;
179
180 case OP_NAME:
c5aa993b 181 nargs = longest_to_int (exp->elts[pc + 1].longconst);
c906108c
SS
182 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
183 fputs_filtered (&exp->elts[pc + 2].string, stream);
184 return;
185
186 case OP_STRING:
79a45b7d
TT
187 {
188 struct value_print_options opts;
d7f9d729 189
79a45b7d
TT
190 nargs = longest_to_int (exp->elts[pc + 1].longconst);
191 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
192 /* LA_PRINT_STRING will print using the current repeat count threshold.
193 If necessary, we can temporarily set it to zero, or pass it as an
194 additional parameter to LA_PRINT_STRING. -fnf */
195 get_user_print_options (&opts);
6c7a06a3 196 LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
be759fcf 197 &exp->elts[pc + 2].string, nargs, NULL, 0, &opts);
79a45b7d 198 }
c906108c
SS
199 return;
200
201 case OP_BITSTRING:
c5aa993b 202 nargs = longest_to_int (exp->elts[pc + 1].longconst);
c906108c
SS
203 (*pos)
204 += 3 + BYTES_TO_EXP_ELEM ((nargs + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT);
205 fprintf_unfiltered (stream, "B'<unimplemented>'");
206 return;
207
82eeeb94 208 case OP_OBJC_NSSTRING: /* Objective-C Foundation Class NSString constant. */
79a45b7d
TT
209 {
210 struct value_print_options opts;
d7f9d729 211
79a45b7d
TT
212 nargs = longest_to_int (exp->elts[pc + 1].longconst);
213 (*pos) += 3 + BYTES_TO_EXP_ELEM (nargs + 1);
214 fputs_filtered ("@\"", stream);
215 get_user_print_options (&opts);
6c7a06a3 216 LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
be759fcf 217 &exp->elts[pc + 2].string, nargs, NULL, 0, &opts);
79a45b7d
TT
218 fputs_filtered ("\"", stream);
219 }
82eeeb94
AF
220 return;
221
222 case OP_OBJC_MSGCALL:
223 { /* Objective C message (method) call. */
224 char *selector;
d7f9d729 225
82eeeb94
AF
226 (*pos) += 3;
227 nargs = longest_to_int (exp->elts[pc + 2].longconst);
228 fprintf_unfiltered (stream, "[");
229 print_subexp (exp, pos, stream, PREC_SUFFIX);
230 if (0 == target_read_string (exp->elts[pc + 1].longconst,
231 &selector, 1024, NULL))
232 {
8a3fe4f8 233 error (_("bad selector"));
82eeeb94
AF
234 return;
235 }
236 if (nargs)
237 {
238 char *s, *nextS;
d7f9d729 239
82eeeb94
AF
240 s = alloca (strlen (selector) + 1);
241 strcpy (s, selector);
242 for (tem = 0; tem < nargs; tem++)
243 {
244 nextS = strchr (s, ':');
fcd776e5 245 gdb_assert (nextS); /* Make sure we found ':'. */
82eeeb94
AF
246 *nextS = '\0';
247 fprintf_unfiltered (stream, " %s: ", s);
248 s = nextS + 1;
249 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
250 }
251 }
252 else
253 {
254 fprintf_unfiltered (stream, " %s", selector);
255 }
256 fprintf_unfiltered (stream, "]");
257 /* "selector" was malloc'd by target_read_string. Free it. */
4ef3f3be 258 xfree (selector);
82eeeb94
AF
259 return;
260 }
261
c906108c
SS
262 case OP_ARRAY:
263 (*pos) += 3;
264 nargs = longest_to_int (exp->elts[pc + 2].longconst);
265 nargs -= longest_to_int (exp->elts[pc + 1].longconst);
266 nargs++;
267 tem = 0;
268 if (exp->elts[pc + 4].opcode == OP_LONG
b806fb9a
UW
269 && exp->elts[pc + 5].type
270 == builtin_type (exp->gdbarch)->builtin_char
c906108c
SS
271 && exp->language_defn->la_language == language_c)
272 {
273 /* Attempt to print C character arrays using string syntax.
274 Walk through the args, picking up one character from each
275 of the OP_LONG expression elements. If any array element
276 does not match our expection of what we should find for
277 a simple string, revert back to array printing. Note that
278 the last expression element is an explicit null terminator
279 byte, which doesn't get printed. */
280 tempstr = alloca (nargs);
281 pc += 4;
282 while (tem < nargs)
283 {
284 if (exp->elts[pc].opcode != OP_LONG
b806fb9a
UW
285 || exp->elts[pc + 1].type
286 != builtin_type (exp->gdbarch)->builtin_char)
c906108c
SS
287 {
288 /* Not a simple array of char, use regular array printing. */
289 tem = 0;
290 break;
291 }
292 else
293 {
294 tempstr[tem++] =
295 longest_to_int (exp->elts[pc + 2].longconst);
296 pc += 4;
297 }
298 }
299 }
300 if (tem > 0)
301 {
79a45b7d 302 struct value_print_options opts;
d7f9d729 303
79a45b7d 304 get_user_print_options (&opts);
6c7a06a3 305 LA_PRINT_STRING (stream, builtin_type (exp->gdbarch)->builtin_char,
be759fcf 306 tempstr, nargs - 1, NULL, 0, &opts);
c906108c
SS
307 (*pos) = pc;
308 }
309 else
310 {
db034ac5 311 fputs_filtered (" {", stream);
c906108c
SS
312 for (tem = 0; tem < nargs; tem++)
313 {
314 if (tem != 0)
315 {
316 fputs_filtered (", ", stream);
317 }
318 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
319 }
db034ac5 320 fputs_filtered ("}", stream);
c906108c
SS
321 }
322 return;
323
324 case OP_LABELED:
325 tem = longest_to_int (exp->elts[pc + 1].longconst);
326 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1b831c93 327 /* Gcc support both these syntaxes. Unsure which is preferred. */
c906108c 328#if 1
1b831c93
AC
329 fputs_filtered (&exp->elts[pc + 2].string, stream);
330 fputs_filtered (": ", stream);
c906108c 331#else
1b831c93
AC
332 fputs_filtered (".", stream);
333 fputs_filtered (&exp->elts[pc + 2].string, stream);
334 fputs_filtered ("=", stream);
c906108c 335#endif
c906108c
SS
336 print_subexp (exp, pos, stream, PREC_SUFFIX);
337 return;
338
339 case TERNOP_COND:
340 if ((int) prec > (int) PREC_COMMA)
341 fputs_filtered ("(", stream);
342 /* Print the subexpressions, forcing parentheses
c5aa993b
JM
343 around any binary operations within them.
344 This is more parentheses than are strictly necessary,
345 but it looks clearer. */
c906108c
SS
346 print_subexp (exp, pos, stream, PREC_HYPER);
347 fputs_filtered (" ? ", stream);
348 print_subexp (exp, pos, stream, PREC_HYPER);
349 fputs_filtered (" : ", stream);
350 print_subexp (exp, pos, stream, PREC_HYPER);
351 if ((int) prec > (int) PREC_COMMA)
352 fputs_filtered (")", stream);
353 return;
354
355 case TERNOP_SLICE:
356 case TERNOP_SLICE_COUNT:
357 print_subexp (exp, pos, stream, PREC_SUFFIX);
358 fputs_filtered ("(", stream);
359 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
360 fputs_filtered (opcode == TERNOP_SLICE ? " : " : " UP ", stream);
361 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
362 fputs_filtered (")", stream);
363 return;
364
365 case STRUCTOP_STRUCT:
366 tem = longest_to_int (exp->elts[pc + 1].longconst);
367 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
368 print_subexp (exp, pos, stream, PREC_SUFFIX);
369 fputs_filtered (".", stream);
370 fputs_filtered (&exp->elts[pc + 2].string, stream);
371 return;
372
c5aa993b 373 /* Will not occur for Modula-2 */
c906108c
SS
374 case STRUCTOP_PTR:
375 tem = longest_to_int (exp->elts[pc + 1].longconst);
376 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
377 print_subexp (exp, pos, stream, PREC_SUFFIX);
378 fputs_filtered ("->", stream);
379 fputs_filtered (&exp->elts[pc + 2].string, stream);
380 return;
381
0534816d
DJ
382 case STRUCTOP_MEMBER:
383 print_subexp (exp, pos, stream, PREC_SUFFIX);
384 fputs_filtered (".*", stream);
385 print_subexp (exp, pos, stream, PREC_SUFFIX);
386 return;
387
388 case STRUCTOP_MPTR:
389 print_subexp (exp, pos, stream, PREC_SUFFIX);
390 fputs_filtered ("->*", stream);
391 print_subexp (exp, pos, stream, PREC_SUFFIX);
392 return;
393
c906108c
SS
394 case BINOP_SUBSCRIPT:
395 print_subexp (exp, pos, stream, PREC_SUFFIX);
396 fputs_filtered ("[", stream);
397 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
398 fputs_filtered ("]", stream);
399 return;
400
401 case UNOP_POSTINCREMENT:
402 print_subexp (exp, pos, stream, PREC_SUFFIX);
403 fputs_filtered ("++", stream);
404 return;
405
406 case UNOP_POSTDECREMENT:
407 print_subexp (exp, pos, stream, PREC_SUFFIX);
408 fputs_filtered ("--", stream);
409 return;
410
411 case UNOP_CAST:
412 (*pos) += 2;
413 if ((int) prec > (int) PREC_PREFIX)
c5aa993b 414 fputs_filtered ("(", stream);
c906108c
SS
415 fputs_filtered ("(", stream);
416 type_print (exp->elts[pc + 1].type, "", stream, 0);
417 fputs_filtered (") ", stream);
418 print_subexp (exp, pos, stream, PREC_PREFIX);
419 if ((int) prec > (int) PREC_PREFIX)
c5aa993b 420 fputs_filtered (")", stream);
c906108c
SS
421 return;
422
4e8f195d
TT
423 case UNOP_DYNAMIC_CAST:
424 case UNOP_REINTERPRET_CAST:
425 fputs_filtered (opcode == UNOP_DYNAMIC_CAST ? "dynamic_cast"
426 : "reinterpret_cast", stream);
427 fputs_filtered ("<", stream);
428 (*pos) += 2;
429 type_print (exp->elts[pc + 1].type, "", stream, 0);
430 fputs_filtered ("> (", stream);
431 print_subexp (exp, pos, stream, PREC_PREFIX);
432 fputs_filtered (")", stream);
433 return;
434
c906108c
SS
435 case UNOP_MEMVAL:
436 (*pos) += 2;
437 if ((int) prec > (int) PREC_PREFIX)
c5aa993b 438 fputs_filtered ("(", stream);
905e0470
PM
439 if (TYPE_CODE (exp->elts[pc + 1].type) == TYPE_CODE_FUNC
440 && exp->elts[pc + 3].opcode == OP_LONG)
c5aa993b 441 {
79a45b7d
TT
442 struct value_print_options opts;
443
c5aa993b
JM
444 /* We have a minimal symbol fn, probably. It's encoded
445 as a UNOP_MEMVAL (function-type) of an OP_LONG (int, address).
446 Swallow the OP_LONG (including both its opcodes); ignore
447 its type; print the value in the type of the MEMVAL. */
448 (*pos) += 4;
449 val = value_at_lazy (exp->elts[pc + 1].type,
00a4c844 450 (CORE_ADDR) exp->elts[pc + 5].longconst);
79a45b7d
TT
451 get_raw_print_options (&opts);
452 value_print (val, stream, &opts);
c5aa993b
JM
453 }
454 else
455 {
456 fputs_filtered ("{", stream);
457 type_print (exp->elts[pc + 1].type, "", stream, 0);
458 fputs_filtered ("} ", stream);
459 print_subexp (exp, pos, stream, PREC_PREFIX);
460 }
c906108c 461 if ((int) prec > (int) PREC_PREFIX)
c5aa993b 462 fputs_filtered (")", stream);
c906108c
SS
463 return;
464
9e35dae4
DJ
465 case UNOP_MEMVAL_TLS:
466 (*pos) += 3;
467 if ((int) prec > (int) PREC_PREFIX)
468 fputs_filtered ("(", stream);
469 fputs_filtered ("{", stream);
470 type_print (exp->elts[pc + 2].type, "", stream, 0);
471 fputs_filtered ("} ", stream);
472 print_subexp (exp, pos, stream, PREC_PREFIX);
473 if ((int) prec > (int) PREC_PREFIX)
474 fputs_filtered (")", stream);
475 return;
476
c906108c
SS
477 case BINOP_ASSIGN_MODIFY:
478 opcode = exp->elts[pc + 1].opcode;
479 (*pos) += 2;
480 myprec = PREC_ASSIGN;
481 assoc = 1;
482 assign_modify = 1;
483 op_str = "???";
484 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
485 if (op_print_tab[tem].opcode == opcode)
486 {
487 op_str = op_print_tab[tem].string;
488 break;
489 }
490 if (op_print_tab[tem].opcode != opcode)
491 /* Not found; don't try to keep going because we don't know how
492 to interpret further elements. */
8a3fe4f8 493 error (_("Invalid expression"));
c906108c
SS
494 break;
495
c5aa993b 496 /* C++ ops */
c906108c
SS
497
498 case OP_THIS:
499 ++(*pos);
500 fputs_filtered ("this", stream);
501 return;
502
82eeeb94
AF
503 /* Objective-C ops */
504
505 case OP_OBJC_SELF:
506 ++(*pos);
507 fputs_filtered ("self", stream); /* The ObjC equivalent of "this". */
508 return;
509
c5aa993b 510 /* Modula-2 ops */
c906108c
SS
511
512 case MULTI_SUBSCRIPT:
513 (*pos) += 2;
514 nargs = longest_to_int (exp->elts[pc + 1].longconst);
515 print_subexp (exp, pos, stream, PREC_SUFFIX);
516 fprintf_unfiltered (stream, " [");
517 for (tem = 0; tem < nargs; tem++)
518 {
519 if (tem != 0)
520 fprintf_unfiltered (stream, ", ");
521 print_subexp (exp, pos, stream, PREC_ABOVE_COMMA);
522 }
523 fprintf_unfiltered (stream, "]");
524 return;
525
526 case BINOP_VAL:
c5aa993b
JM
527 (*pos) += 2;
528 fprintf_unfiltered (stream, "VAL(");
529 type_print (exp->elts[pc + 1].type, "", stream, 0);
530 fprintf_unfiltered (stream, ",");
531 print_subexp (exp, pos, stream, PREC_PREFIX);
532 fprintf_unfiltered (stream, ")");
c906108c 533 return;
c5aa993b 534
c5aa993b 535 /* Default ops */
c906108c
SS
536
537 default:
538 op_str = "???";
539 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
540 if (op_print_tab[tem].opcode == opcode)
541 {
542 op_str = op_print_tab[tem].string;
543 myprec = op_print_tab[tem].precedence;
544 assoc = op_print_tab[tem].right_assoc;
545 break;
546 }
547 if (op_print_tab[tem].opcode != opcode)
548 /* Not found; don't try to keep going because we don't know how
549 to interpret further elements. For example, this happens
550 if opcode is OP_TYPE. */
8a3fe4f8 551 error (_("Invalid expression"));
c5aa993b 552 }
c906108c
SS
553
554 /* Note that PREC_BUILTIN will always emit parentheses. */
555 if ((int) myprec < (int) prec)
556 fputs_filtered ("(", stream);
557 if ((int) opcode > (int) BINOP_END)
558 {
559 if (assoc)
560 {
561 /* Unary postfix operator. */
562 print_subexp (exp, pos, stream, PREC_SUFFIX);
563 fputs_filtered (op_str, stream);
564 }
565 else
566 {
567 /* Unary prefix operator. */
568 fputs_filtered (op_str, stream);
569 if (myprec == PREC_BUILTIN_FUNCTION)
570 fputs_filtered ("(", stream);
571 print_subexp (exp, pos, stream, PREC_PREFIX);
572 if (myprec == PREC_BUILTIN_FUNCTION)
573 fputs_filtered (")", stream);
574 }
575 }
576 else
577 {
578 /* Binary operator. */
579 /* Print left operand.
c5aa993b
JM
580 If operator is right-associative,
581 increment precedence for this operand. */
c906108c
SS
582 print_subexp (exp, pos, stream,
583 (enum precedence) ((int) myprec + assoc));
584 /* Print the operator itself. */
585 if (assign_modify)
586 fprintf_filtered (stream, " %s= ", op_str);
587 else if (op_str[0] == ',')
588 fprintf_filtered (stream, "%s ", op_str);
589 else
590 fprintf_filtered (stream, " %s ", op_str);
591 /* Print right operand.
c5aa993b
JM
592 If operator is left-associative,
593 increment precedence for this operand. */
c906108c
SS
594 print_subexp (exp, pos, stream,
595 (enum precedence) ((int) myprec + !assoc));
596 }
597
598 if ((int) myprec < (int) prec)
599 fputs_filtered (")", stream);
600}
601
602/* Return the operator corresponding to opcode OP as
603 a string. NULL indicates that the opcode was not found in the
604 current language table. */
605char *
fba45db2 606op_string (enum exp_opcode op)
c906108c
SS
607{
608 int tem;
f86f5ca3 609 const struct op_print *op_print_tab;
c906108c
SS
610
611 op_print_tab = current_language->la_op_print_tab;
612 for (tem = 0; op_print_tab[tem].opcode != OP_NULL; tem++)
613 if (op_print_tab[tem].opcode == op)
614 return op_print_tab[tem].string;
615 return NULL;
616}
617
c906108c
SS
618/* Support for dumping the raw data from expressions in a human readable
619 form. */
620
5f9769d1 621static char *op_name (struct expression *, enum exp_opcode);
24daaebc 622static int dump_subexp_body (struct expression *exp, struct ui_file *, int);
c906108c 623
5f9769d1
PH
624/* Name for OPCODE, when it appears in expression EXP. */
625
c906108c 626static char *
5f9769d1
PH
627op_name (struct expression *exp, enum exp_opcode opcode)
628{
629 return exp->language_defn->la_exp_desc->op_name (opcode);
630}
631
632/* Default name for the standard operator OPCODE (i.e., one defined in
633 the definition of enum exp_opcode). */
634
635char *
636op_name_standard (enum exp_opcode opcode)
c906108c
SS
637{
638 switch (opcode)
639 {
640 default:
641 {
642 static char buf[30];
643
644 sprintf (buf, "<unknown %d>", opcode);
645 return buf;
646 }
c5aa993b
JM
647 case OP_NULL:
648 return "OP_NULL";
649 case BINOP_ADD:
650 return "BINOP_ADD";
651 case BINOP_SUB:
652 return "BINOP_SUB";
653 case BINOP_MUL:
654 return "BINOP_MUL";
655 case BINOP_DIV:
656 return "BINOP_DIV";
657 case BINOP_REM:
658 return "BINOP_REM";
659 case BINOP_MOD:
660 return "BINOP_MOD";
661 case BINOP_LSH:
662 return "BINOP_LSH";
663 case BINOP_RSH:
664 return "BINOP_RSH";
665 case BINOP_LOGICAL_AND:
666 return "BINOP_LOGICAL_AND";
667 case BINOP_LOGICAL_OR:
668 return "BINOP_LOGICAL_OR";
669 case BINOP_BITWISE_AND:
670 return "BINOP_BITWISE_AND";
671 case BINOP_BITWISE_IOR:
672 return "BINOP_BITWISE_IOR";
673 case BINOP_BITWISE_XOR:
674 return "BINOP_BITWISE_XOR";
675 case BINOP_EQUAL:
676 return "BINOP_EQUAL";
677 case BINOP_NOTEQUAL:
678 return "BINOP_NOTEQUAL";
679 case BINOP_LESS:
680 return "BINOP_LESS";
681 case BINOP_GTR:
682 return "BINOP_GTR";
683 case BINOP_LEQ:
684 return "BINOP_LEQ";
685 case BINOP_GEQ:
686 return "BINOP_GEQ";
687 case BINOP_REPEAT:
688 return "BINOP_REPEAT";
689 case BINOP_ASSIGN:
690 return "BINOP_ASSIGN";
691 case BINOP_COMMA:
692 return "BINOP_COMMA";
693 case BINOP_SUBSCRIPT:
694 return "BINOP_SUBSCRIPT";
695 case MULTI_SUBSCRIPT:
696 return "MULTI_SUBSCRIPT";
697 case BINOP_EXP:
698 return "BINOP_EXP";
699 case BINOP_MIN:
700 return "BINOP_MIN";
701 case BINOP_MAX:
702 return "BINOP_MAX";
c5aa993b
JM
703 case STRUCTOP_MEMBER:
704 return "STRUCTOP_MEMBER";
705 case STRUCTOP_MPTR:
706 return "STRUCTOP_MPTR";
707 case BINOP_INTDIV:
708 return "BINOP_INTDIV";
709 case BINOP_ASSIGN_MODIFY:
710 return "BINOP_ASSIGN_MODIFY";
711 case BINOP_VAL:
712 return "BINOP_VAL";
c5aa993b
JM
713 case BINOP_CONCAT:
714 return "BINOP_CONCAT";
715 case BINOP_RANGE:
716 return "BINOP_RANGE";
717 case BINOP_END:
718 return "BINOP_END";
719 case TERNOP_COND:
720 return "TERNOP_COND";
721 case TERNOP_SLICE:
722 return "TERNOP_SLICE";
723 case TERNOP_SLICE_COUNT:
724 return "TERNOP_SLICE_COUNT";
725 case OP_LONG:
726 return "OP_LONG";
727 case OP_DOUBLE:
728 return "OP_DOUBLE";
729 case OP_VAR_VALUE:
730 return "OP_VAR_VALUE";
731 case OP_LAST:
732 return "OP_LAST";
733 case OP_REGISTER:
734 return "OP_REGISTER";
735 case OP_INTERNALVAR:
736 return "OP_INTERNALVAR";
737 case OP_FUNCALL:
738 return "OP_FUNCALL";
739 case OP_STRING:
740 return "OP_STRING";
741 case OP_BITSTRING:
742 return "OP_BITSTRING";
743 case OP_ARRAY:
744 return "OP_ARRAY";
745 case UNOP_CAST:
746 return "UNOP_CAST";
4e8f195d
TT
747 case UNOP_DYNAMIC_CAST:
748 return "UNOP_DYNAMIC_CAST";
749 case UNOP_REINTERPRET_CAST:
750 return "UNOP_REINTERPRET_CAST";
c5aa993b
JM
751 case UNOP_MEMVAL:
752 return "UNOP_MEMVAL";
9e35dae4
DJ
753 case UNOP_MEMVAL_TLS:
754 return "UNOP_MEMVAL_TLS";
c5aa993b
JM
755 case UNOP_NEG:
756 return "UNOP_NEG";
757 case UNOP_LOGICAL_NOT:
758 return "UNOP_LOGICAL_NOT";
759 case UNOP_COMPLEMENT:
760 return "UNOP_COMPLEMENT";
761 case UNOP_IND:
762 return "UNOP_IND";
763 case UNOP_ADDR:
764 return "UNOP_ADDR";
765 case UNOP_PREINCREMENT:
766 return "UNOP_PREINCREMENT";
767 case UNOP_POSTINCREMENT:
768 return "UNOP_POSTINCREMENT";
769 case UNOP_PREDECREMENT:
770 return "UNOP_PREDECREMENT";
771 case UNOP_POSTDECREMENT:
772 return "UNOP_POSTDECREMENT";
773 case UNOP_SIZEOF:
774 return "UNOP_SIZEOF";
c5aa993b
JM
775 case UNOP_PLUS:
776 return "UNOP_PLUS";
777 case UNOP_CAP:
778 return "UNOP_CAP";
779 case UNOP_CHR:
780 return "UNOP_CHR";
781 case UNOP_ORD:
782 return "UNOP_ORD";
783 case UNOP_ABS:
784 return "UNOP_ABS";
785 case UNOP_FLOAT:
786 return "UNOP_FLOAT";
787 case UNOP_HIGH:
788 return "UNOP_HIGH";
789 case UNOP_MAX:
790 return "UNOP_MAX";
791 case UNOP_MIN:
792 return "UNOP_MIN";
793 case UNOP_ODD:
794 return "UNOP_ODD";
795 case UNOP_TRUNC:
796 return "UNOP_TRUNC";
797 case OP_BOOL:
798 return "OP_BOOL";
799 case OP_M2_STRING:
800 return "OP_M2_STRING";
801 case STRUCTOP_STRUCT:
802 return "STRUCTOP_STRUCT";
803 case STRUCTOP_PTR:
804 return "STRUCTOP_PTR";
805 case OP_THIS:
806 return "OP_THIS";
82eeeb94
AF
807 case OP_OBJC_SELF:
808 return "OP_OBJC_SELF";
c5aa993b
JM
809 case OP_SCOPE:
810 return "OP_SCOPE";
811 case OP_TYPE:
812 return "OP_TYPE";
813 case OP_LABELED:
814 return "OP_LABELED";
7322dca9
SW
815 case OP_ADL_FUNC:
816 return "OP_ADL_FUNC";
c906108c
SS
817 }
818}
819
a411cd0e
DE
820/* Print a raw dump of expression EXP to STREAM.
821 NOTE, if non-NULL, is printed as extra explanatory text. */
822
c906108c 823void
24daaebc
PH
824dump_raw_expression (struct expression *exp, struct ui_file *stream,
825 char *note)
c906108c
SS
826{
827 int elt;
828 char *opcode_name;
829 char *eltscan;
830 int eltsize;
831
832 fprintf_filtered (stream, "Dump of expression @ ");
d4f3574e 833 gdb_print_host_address (exp, stream);
a411cd0e
DE
834 if (note)
835 fprintf_filtered (stream, ", %s:", note);
836 fprintf_filtered (stream, "\n\tLanguage %s, %d elements, %ld bytes each.\n",
c5aa993b 837 exp->language_defn->la_name, exp->nelts,
9d271fd8 838 (long) sizeof (union exp_element));
c906108c
SS
839 fprintf_filtered (stream, "\t%5s %20s %16s %s\n", "Index", "Opcode",
840 "Hex Value", "String Value");
c5aa993b 841 for (elt = 0; elt < exp->nelts; elt++)
c906108c
SS
842 {
843 fprintf_filtered (stream, "\t%5d ", elt);
5f9769d1 844 opcode_name = op_name (exp, exp->elts[elt].opcode);
c906108c
SS
845
846 fprintf_filtered (stream, "%20s ", opcode_name);
c5aa993b 847 print_longest (stream, 'd', 0, exp->elts[elt].longconst);
c906108c
SS
848 fprintf_filtered (stream, " ");
849
850 for (eltscan = (char *) &exp->elts[elt],
c5aa993b 851 eltsize = sizeof (union exp_element);
c906108c
SS
852 eltsize-- > 0;
853 eltscan++)
854 {
855 fprintf_filtered (stream, "%c",
856 isprint (*eltscan) ? (*eltscan & 0xFF) : '.');
857 }
858 fprintf_filtered (stream, "\n");
859 }
860}
861
24daaebc
PH
862/* Dump the subexpression of prefix expression EXP whose operator is at
863 position ELT onto STREAM. Returns the position of the next
864 subexpression in EXP. */
c906108c 865
24daaebc 866int
fba45db2 867dump_subexp (struct expression *exp, struct ui_file *stream, int elt)
c906108c
SS
868{
869 static int indent = 0;
870 int i;
871
872 fprintf_filtered (stream, "\n");
873 fprintf_filtered (stream, "\t%5d ", elt);
874
875 for (i = 1; i <= indent; i++)
876 fprintf_filtered (stream, " ");
877 indent += 2;
878
5f9769d1 879 fprintf_filtered (stream, "%-20s ", op_name (exp, exp->elts[elt].opcode));
c906108c 880
24daaebc
PH
881 elt = dump_subexp_body (exp, stream, elt);
882
883 indent -= 2;
884
885 return elt;
886}
887
888/* Dump the operands of prefix expression EXP whose opcode is at
889 position ELT onto STREAM. Returns the position of the next
890 subexpression in EXP. */
891
892static int
893dump_subexp_body (struct expression *exp, struct ui_file *stream, int elt)
5f9769d1
PH
894{
895 return exp->language_defn->la_exp_desc->dump_subexp_body (exp, stream, elt);
896}
897
898/* Default value for subexp_body in exp_descriptor vector. */
899
900int
901dump_subexp_body_standard (struct expression *exp,
902 struct ui_file *stream, int elt)
24daaebc
PH
903{
904 int opcode = exp->elts[elt++].opcode;
905
906 switch (opcode)
c906108c
SS
907 {
908 case TERNOP_COND:
909 case TERNOP_SLICE:
910 case TERNOP_SLICE_COUNT:
911 elt = dump_subexp (exp, stream, elt);
912 case BINOP_ADD:
913 case BINOP_SUB:
914 case BINOP_MUL:
915 case BINOP_DIV:
916 case BINOP_REM:
917 case BINOP_MOD:
918 case BINOP_LSH:
919 case BINOP_RSH:
920 case BINOP_LOGICAL_AND:
921 case BINOP_LOGICAL_OR:
922 case BINOP_BITWISE_AND:
923 case BINOP_BITWISE_IOR:
924 case BINOP_BITWISE_XOR:
925 case BINOP_EQUAL:
926 case BINOP_NOTEQUAL:
927 case BINOP_LESS:
928 case BINOP_GTR:
929 case BINOP_LEQ:
930 case BINOP_GEQ:
931 case BINOP_REPEAT:
932 case BINOP_ASSIGN:
933 case BINOP_COMMA:
934 case BINOP_SUBSCRIPT:
935 case BINOP_EXP:
936 case BINOP_MIN:
937 case BINOP_MAX:
c906108c
SS
938 case BINOP_INTDIV:
939 case BINOP_ASSIGN_MODIFY:
940 case BINOP_VAL:
c906108c
SS
941 case BINOP_CONCAT:
942 case BINOP_IN:
943 case BINOP_RANGE:
944 case BINOP_END:
0534816d
DJ
945 case STRUCTOP_MEMBER:
946 case STRUCTOP_MPTR:
c906108c
SS
947 elt = dump_subexp (exp, stream, elt);
948 case UNOP_NEG:
949 case UNOP_LOGICAL_NOT:
950 case UNOP_COMPLEMENT:
951 case UNOP_IND:
952 case UNOP_ADDR:
953 case UNOP_PREINCREMENT:
954 case UNOP_POSTINCREMENT:
955 case UNOP_PREDECREMENT:
956 case UNOP_POSTDECREMENT:
957 case UNOP_SIZEOF:
958 case UNOP_PLUS:
959 case UNOP_CAP:
960 case UNOP_CHR:
961 case UNOP_ORD:
962 case UNOP_ABS:
963 case UNOP_FLOAT:
964 case UNOP_HIGH:
965 case UNOP_MAX:
966 case UNOP_MIN:
967 case UNOP_ODD:
968 case UNOP_TRUNC:
c906108c
SS
969 elt = dump_subexp (exp, stream, elt);
970 break;
971 case OP_LONG:
d4f3574e
SS
972 fprintf_filtered (stream, "Type @");
973 gdb_print_host_address (exp->elts[elt].type, stream);
974 fprintf_filtered (stream, " (");
c906108c
SS
975 type_print (exp->elts[elt].type, NULL, stream, 0);
976 fprintf_filtered (stream, "), value %ld (0x%lx)",
c5aa993b
JM
977 (long) exp->elts[elt + 1].longconst,
978 (long) exp->elts[elt + 1].longconst);
c906108c
SS
979 elt += 3;
980 break;
981 case OP_DOUBLE:
d4f3574e
SS
982 fprintf_filtered (stream, "Type @");
983 gdb_print_host_address (exp->elts[elt].type, stream);
984 fprintf_filtered (stream, " (");
c906108c
SS
985 type_print (exp->elts[elt].type, NULL, stream, 0);
986 fprintf_filtered (stream, "), value %g",
c5aa993b 987 (double) exp->elts[elt + 1].doubleconst);
c906108c
SS
988 elt += 3;
989 break;
990 case OP_VAR_VALUE:
d4f3574e
SS
991 fprintf_filtered (stream, "Block @");
992 gdb_print_host_address (exp->elts[elt].block, stream);
993 fprintf_filtered (stream, ", symbol @");
994 gdb_print_host_address (exp->elts[elt + 1].symbol, stream);
995 fprintf_filtered (stream, " (%s)",
3567439c 996 SYMBOL_PRINT_NAME (exp->elts[elt + 1].symbol));
c906108c
SS
997 elt += 3;
998 break;
999 case OP_LAST:
1000 fprintf_filtered (stream, "History element %ld",
c5aa993b 1001 (long) exp->elts[elt].longconst);
c906108c
SS
1002 elt += 2;
1003 break;
1004 case OP_REGISTER:
67f3407f
DJ
1005 fprintf_filtered (stream, "Register $%s", &exp->elts[elt + 1].string);
1006 elt += 3 + BYTES_TO_EXP_ELEM (exp->elts[elt].longconst + 1);
c906108c
SS
1007 break;
1008 case OP_INTERNALVAR:
d4f3574e
SS
1009 fprintf_filtered (stream, "Internal var @");
1010 gdb_print_host_address (exp->elts[elt].internalvar, stream);
1011 fprintf_filtered (stream, " (%s)",
4fa62494 1012 internalvar_name (exp->elts[elt].internalvar));
c906108c
SS
1013 elt += 2;
1014 break;
1015 case OP_FUNCALL:
1016 {
24daaebc 1017 int i, nargs;
c906108c
SS
1018
1019 nargs = longest_to_int (exp->elts[elt].longconst);
1020
1021 fprintf_filtered (stream, "Number of args: %d", nargs);
1022 elt += 2;
1023
1024 for (i = 1; i <= nargs + 1; i++)
1025 elt = dump_subexp (exp, stream, elt);
1026 }
1027 break;
1028 case OP_ARRAY:
1029 {
1030 int lower, upper;
1031 int i;
1032
1033 lower = longest_to_int (exp->elts[elt].longconst);
1034 upper = longest_to_int (exp->elts[elt + 1].longconst);
1035
1036 fprintf_filtered (stream, "Bounds [%d:%d]", lower, upper);
1037 elt += 3;
1038
1039 for (i = 1; i <= upper - lower + 1; i++)
1040 elt = dump_subexp (exp, stream, elt);
1041 }
1042 break;
1043 case UNOP_MEMVAL:
1044 case UNOP_CAST:
4e8f195d
TT
1045 case UNOP_DYNAMIC_CAST:
1046 case UNOP_REINTERPRET_CAST:
d4f3574e
SS
1047 fprintf_filtered (stream, "Type @");
1048 gdb_print_host_address (exp->elts[elt].type, stream);
1049 fprintf_filtered (stream, " (");
c906108c
SS
1050 type_print (exp->elts[elt].type, NULL, stream, 0);
1051 fprintf_filtered (stream, ")");
1052 elt = dump_subexp (exp, stream, elt + 2);
1053 break;
9e35dae4
DJ
1054 case UNOP_MEMVAL_TLS:
1055 fprintf_filtered (stream, "TLS type @");
1056 gdb_print_host_address (exp->elts[elt + 1].type, stream);
1057 fprintf_filtered (stream, " (__thread /* \"%s\" */ ",
1058 (exp->elts[elt].objfile == NULL ? "(null)"
1059 : exp->elts[elt].objfile->name));
1060 type_print (exp->elts[elt + 1].type, NULL, stream, 0);
1061 fprintf_filtered (stream, ")");
1062 elt = dump_subexp (exp, stream, elt + 3);
1063 break;
c906108c 1064 case OP_TYPE:
d4f3574e
SS
1065 fprintf_filtered (stream, "Type @");
1066 gdb_print_host_address (exp->elts[elt].type, stream);
1067 fprintf_filtered (stream, " (");
c906108c
SS
1068 type_print (exp->elts[elt].type, NULL, stream, 0);
1069 fprintf_filtered (stream, ")");
1070 elt += 2;
1071 break;
1072 case STRUCTOP_STRUCT:
1073 case STRUCTOP_PTR:
1074 {
1075 char *elem_name;
1076 int len;
1077
1078 len = longest_to_int (exp->elts[elt].longconst);
1079 elem_name = &exp->elts[elt + 1].string;
1080
1081 fprintf_filtered (stream, "Element name: `%.*s'", len, elem_name);
1082 elt = dump_subexp (exp, stream, elt + 3 + BYTES_TO_EXP_ELEM (len + 1));
1083 }
1084 break;
1085 case OP_SCOPE:
1086 {
1087 char *elem_name;
1088 int len;
1089
d4f3574e
SS
1090 fprintf_filtered (stream, "Type @");
1091 gdb_print_host_address (exp->elts[elt].type, stream);
1092 fprintf_filtered (stream, " (");
c906108c
SS
1093 type_print (exp->elts[elt].type, NULL, stream, 0);
1094 fprintf_filtered (stream, ") ");
1095
1096 len = longest_to_int (exp->elts[elt + 1].longconst);
1097 elem_name = &exp->elts[elt + 2].string;
1098
1099 fprintf_filtered (stream, "Field name: `%.*s'", len, elem_name);
1100 elt += 4 + BYTES_TO_EXP_ELEM (len + 1);
1101 }
1102 break;
1103 default:
1104 case OP_NULL:
c906108c
SS
1105 case MULTI_SUBSCRIPT:
1106 case OP_F77_UNDETERMINED_ARGLIST:
1107 case OP_COMPLEX:
1108 case OP_STRING:
1109 case OP_BITSTRING:
1110 case OP_BOOL:
1111 case OP_M2_STRING:
1112 case OP_THIS:
1113 case OP_LABELED:
1114 case OP_NAME:
c906108c
SS
1115 fprintf_filtered (stream, "Unknown format");
1116 }
1117
c906108c
SS
1118 return elt;
1119}
1120
1121void
24daaebc 1122dump_prefix_expression (struct expression *exp, struct ui_file *stream)
c906108c
SS
1123{
1124 int elt;
1125
1126 fprintf_filtered (stream, "Dump of expression @ ");
d4f3574e 1127 gdb_print_host_address (exp, stream);
24daaebc 1128 fputs_filtered (", after conversion to prefix form:\nExpression: `", stream);
c906108c
SS
1129 if (exp->elts[0].opcode != OP_TYPE)
1130 print_expression (exp, stream);
1131 else
1132 fputs_filtered ("Type printing not yet supported....", stream);
9d271fd8 1133 fprintf_filtered (stream, "'\n\tLanguage %s, %d elements, %ld bytes each.\n",
c5aa993b 1134 exp->language_defn->la_name, exp->nelts,
9d271fd8 1135 (long) sizeof (union exp_element));
c906108c
SS
1136 fputs_filtered ("\n", stream);
1137
c5aa993b 1138 for (elt = 0; elt < exp->nelts;)
c906108c
SS
1139 elt = dump_subexp (exp, stream, elt);
1140 fputs_filtered ("\n", stream);
1141}
This page took 0.886387 seconds and 4 git commands to generate.