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