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