import gdb-19990422 snapshot
[binutils-gdb.git] / gdb / ax-gdb.c
1 /* GDB-specific functions for operating on agent expressions
2 Copyright 1998 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "symfile.h"
23 #include "gdbtypes.h"
24 #include "value.h"
25 #include "expression.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "frame.h"
29 #include "target.h"
30 #include "ax.h"
31 #include "ax-gdb.h"
32
33 /* Probably the best way to read this file is to start with the types
34 and enums in ax-gdb.h, and then look at gen_expr, towards the
35 bottom; that's the main function that looks at the GDB expressions
36 and calls everything else to generate code.
37
38 I'm beginning to wonder whether it wouldn't be nicer to internally
39 generate trees, with types, and then spit out the bytecode in
40 linear form afterwards; we could generate fewer `swap', `ext', and
41 `zero_ext' bytecodes that way; it would make good constant folding
42 easier, too. But at the moment, I think we should be willing to
43 pay for the simplicity of this code with less-than-optimal bytecode
44 strings.
45
46 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
47
48
49 \f
50 /* Prototypes for local functions. */
51
52 /* There's a standard order to the arguments of these functions:
53 union exp_element ** --- pointer into expression
54 struct agent_expr * --- agent expression buffer to generate code into
55 struct axs_value * --- describes value left on top of stack */
56
57 static struct value *const_var_ref PARAMS ((struct symbol *var));
58 static struct value *const_expr PARAMS ((union exp_element **pc));
59 static struct value *maybe_const_expr PARAMS ((union exp_element **pc));
60
61 static void gen_traced_pop PARAMS ((struct agent_expr *, struct axs_value *));
62
63 static void gen_sign_extend PARAMS ((struct agent_expr *, struct type *));
64 static void gen_extend PARAMS ((struct agent_expr *, struct type *));
65 static void gen_fetch PARAMS ((struct agent_expr *, struct type *));
66 static void gen_left_shift PARAMS ((struct agent_expr *, int));
67
68
69 static void gen_frame_args_address PARAMS ((struct agent_expr *));
70 static void gen_frame_locals_address PARAMS ((struct agent_expr *));
71 static void gen_offset PARAMS ((struct agent_expr *ax, int offset));
72 static void gen_sym_offset PARAMS ((struct agent_expr *, struct symbol *));
73 static void gen_var_ref PARAMS ((struct agent_expr *ax,
74 struct axs_value *value,
75 struct symbol *var));
76
77
78 static void gen_int_literal PARAMS ((struct agent_expr *ax,
79 struct axs_value *value,
80 LONGEST k, struct type *type));
81
82
83 static void require_rvalue PARAMS ((struct agent_expr *ax,
84 struct axs_value *value));
85 static void gen_usual_unary PARAMS ((struct agent_expr *ax,
86 struct axs_value *value));
87 static int type_wider_than PARAMS ((struct type *type1,
88 struct type *type2));
89 static struct type *max_type PARAMS ((struct type *type1,
90 struct type *type2));
91 static void gen_conversion PARAMS ((struct agent_expr *ax,
92 struct type *from,
93 struct type *to));
94 static int is_nontrivial_conversion PARAMS ((struct type *from,
95 struct type *to));
96 static void gen_usual_arithmetic PARAMS ((struct agent_expr *ax,
97 struct axs_value *value1,
98 struct axs_value *value2));
99 static void gen_integral_promotions PARAMS ((struct agent_expr *ax,
100 struct axs_value *value));
101 static void gen_cast PARAMS ((struct agent_expr *ax,
102 struct axs_value *value,
103 struct type *type));
104 static void gen_scale PARAMS ((struct agent_expr *ax,
105 enum agent_op op,
106 struct type *type));
107 static void gen_add PARAMS ((struct agent_expr *ax,
108 struct axs_value *value,
109 struct axs_value *value1,
110 struct axs_value *value2,
111 char *name));
112 static void gen_sub PARAMS ((struct agent_expr *ax,
113 struct axs_value *value,
114 struct axs_value *value1,
115 struct axs_value *value2));
116 static void gen_binop PARAMS ((struct agent_expr *ax,
117 struct axs_value *value,
118 struct axs_value *value1,
119 struct axs_value *value2,
120 enum agent_op op,
121 enum agent_op op_unsigned,
122 int may_carry,
123 char *name));
124 static void gen_logical_not PARAMS ((struct agent_expr *ax,
125 struct axs_value *value));
126 static void gen_complement PARAMS ((struct agent_expr *ax,
127 struct axs_value *value));
128 static void gen_deref PARAMS ((struct agent_expr *, struct axs_value *));
129 static void gen_address_of PARAMS ((struct agent_expr *, struct axs_value *));
130 static int find_field PARAMS ((struct type *type, char *name));
131 static void gen_bitfield_ref PARAMS ((struct agent_expr *ax,
132 struct axs_value *value,
133 struct type *type,
134 int start, int end));
135 static void gen_struct_ref PARAMS ((struct agent_expr *ax,
136 struct axs_value *value,
137 char *field,
138 char *operator_name,
139 char *operand_name));
140 static void gen_repeat PARAMS ((union exp_element **pc,
141 struct agent_expr *ax,
142 struct axs_value *value));
143 static void gen_sizeof PARAMS ((union exp_element **pc,
144 struct agent_expr *ax,
145 struct axs_value *value));
146 static void gen_expr PARAMS ((union exp_element **pc,
147 struct agent_expr *ax,
148 struct axs_value *value));
149
150 static void print_axs_value PARAMS ((GDB_FILE *f, struct axs_value *value));
151 static void agent_command PARAMS ((char *exp, int from_tty));
152
153 \f
154 /* Detecting constant expressions. */
155
156 /* If the variable reference at *PC is a constant, return its value.
157 Otherwise, return zero.
158
159 Hey, Wally! How can a variable reference be a constant?
160
161 Well, Beav, this function really handles the OP_VAR_VALUE operator,
162 not specifically variable references. GDB uses OP_VAR_VALUE to
163 refer to any kind of symbolic reference: function names, enum
164 elements, and goto labels are all handled through the OP_VAR_VALUE
165 operator, even though they're constants. It makes sense given the
166 situation.
167
168 Gee, Wally, don'cha wonder sometimes if data representations that
169 subvert commonly accepted definitions of terms in favor of heavily
170 context-specific interpretations are really just a tool of the
171 programming hegemony to preserve their power and exclude the
172 proletariat? */
173
174 static struct value *
175 const_var_ref (var)
176 struct symbol *var;
177 {
178 struct type *type = SYMBOL_TYPE (var);
179
180 switch (SYMBOL_CLASS (var))
181 {
182 case LOC_CONST:
183 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
184
185 case LOC_LABEL:
186 return value_from_longest (type, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
187
188 default:
189 return 0;
190 }
191 }
192
193
194 /* If the expression starting at *PC has a constant value, return it.
195 Otherwise, return zero. If we return a value, then *PC will be
196 advanced to the end of it. If we return zero, *PC could be
197 anywhere. */
198 static struct value *
199 const_expr (pc)
200 union exp_element **pc;
201 {
202 enum exp_opcode op = (*pc)->opcode;
203 struct value *v1;
204
205 switch (op)
206 {
207 case OP_LONG:
208 {
209 struct type *type = (*pc)[1].type;
210 LONGEST k = (*pc)[2].longconst;
211 (*pc) += 4;
212 return value_from_longest (type, k);
213 }
214
215 case OP_VAR_VALUE:
216 {
217 struct value *v = const_var_ref ((*pc)[2].symbol);
218 (*pc) += 4;
219 return v;
220 }
221
222 /* We could add more operators in here. */
223
224 case UNOP_NEG:
225 (*pc)++;
226 v1 = const_expr (pc);
227 if (v1)
228 return value_neg (v1);
229 else
230 return 0;
231
232 default:
233 return 0;
234 }
235 }
236
237
238 /* Like const_expr, but guarantee also that *PC is undisturbed if the
239 expression is not constant. */
240 static struct value *
241 maybe_const_expr (pc)
242 union exp_element **pc;
243 {
244 union exp_element *tentative_pc = *pc;
245 struct value *v = const_expr (&tentative_pc);
246
247 /* If we got a value, then update the real PC. */
248 if (v)
249 *pc = tentative_pc;
250
251 return v;
252 }
253
254 \f
255 /* Generating bytecode from GDB expressions: general assumptions */
256
257 /* Here are a few general assumptions made throughout the code; if you
258 want to make a change that contradicts one of these, then you'd
259 better scan things pretty thoroughly.
260
261 - We assume that all values occupy one stack element. For example,
262 sometimes we'll swap to get at the left argument to a binary
263 operator. If we decide that void values should occupy no stack
264 elements, or that synthetic arrays (whose size is determined at
265 run time, created by the `@' operator) should occupy two stack
266 elements (address and length), then this will cause trouble.
267
268 - We assume the stack elements are infinitely wide, and that we
269 don't have to worry what happens if the user requests an
270 operation that is wider than the actual interpreter's stack.
271 That is, it's up to the interpreter to handle directly all the
272 integer widths the user has access to. (Woe betide the language
273 with bignums!)
274
275 - We don't support side effects. Thus, we don't have to worry about
276 GCC's generalized lvalues, function calls, etc.
277
278 - We don't support floating point. Many places where we switch on
279 some type don't bother to include cases for floating point; there
280 may be even more subtle ways this assumption exists. For
281 example, the arguments to % must be integers.
282
283 - We assume all subexpressions have a static, unchanging type. If
284 we tried to support convenience variables, this would be a
285 problem.
286
287 - All values on the stack should always be fully zero- or
288 sign-extended.
289
290 (I wasn't sure whether to choose this or its opposite --- that
291 only addresses are assumed extended --- but it turns out that
292 neither convention completely eliminates spurious extend
293 operations (if everything is always extended, then you have to
294 extend after add, because it could overflow; if nothing is
295 extended, then you end up producing extends whenever you change
296 sizes), and this is simpler.) */
297
298 \f
299 /* Generating bytecode from GDB expressions: the `trace' kludge */
300
301 /* The compiler in this file is a general-purpose mechanism for
302 translating GDB expressions into bytecode. One ought to be able to
303 find a million and one uses for it.
304
305 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
306 of expediency. Let he who is without sin cast the first stone.
307
308 For the data tracing facility, we need to insert `trace' bytecodes
309 before each data fetch; this records all the memory that the
310 expression touches in the course of evaluation, so that memory will
311 be available when the user later tries to evaluate the expression
312 in GDB.
313
314 This should be done (I think) in a post-processing pass, that walks
315 an arbitrary agent expression and inserts `trace' operations at the
316 appropriate points. But it's much faster to just hack them
317 directly into the code. And since we're in a crunch, that's what
318 I've done.
319
320 Setting the flag trace_kludge to non-zero enables the code that
321 emits the trace bytecodes at the appropriate points. */
322 static int trace_kludge;
323
324 /* Trace the lvalue on the stack, if it needs it. In either case, pop
325 the value. Useful on the left side of a comma, and at the end of
326 an expression being used for tracing. */
327 static void
328 gen_traced_pop (ax, value)
329 struct agent_expr *ax;
330 struct axs_value *value;
331 {
332 if (trace_kludge)
333 switch (value->kind)
334 {
335 case axs_rvalue:
336 /* We don't trace rvalues, just the lvalues necessary to
337 produce them. So just dispose of this value. */
338 ax_simple (ax, aop_pop);
339 break;
340
341 case axs_lvalue_memory:
342 {
343 int length = TYPE_LENGTH (value->type);
344
345 /* There's no point in trying to use a trace_quick bytecode
346 here, since "trace_quick SIZE pop" is three bytes, whereas
347 "const8 SIZE trace" is also three bytes, does the same
348 thing, and the simplest code which generates that will also
349 work correctly for objects with large sizes. */
350 ax_const_l (ax, length);
351 ax_simple (ax, aop_trace);
352 }
353 break;
354
355 case axs_lvalue_register:
356 /* We need to mention the register somewhere in the bytecode,
357 so ax_reqs will pick it up and add it to the mask of
358 registers used. */
359 ax_reg (ax, value->u.reg);
360 ax_simple (ax, aop_pop);
361 break;
362 }
363 else
364 /* If we're not tracing, just pop the value. */
365 ax_simple (ax, aop_pop);
366 }
367
368
369 \f
370 /* Generating bytecode from GDB expressions: helper functions */
371
372 /* Assume that the lower bits of the top of the stack is a value of
373 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
374 static void
375 gen_sign_extend (ax, type)
376 struct agent_expr *ax;
377 struct type *type;
378 {
379 /* Do we need to sign-extend this? */
380 if (! TYPE_UNSIGNED (type))
381 ax_ext (ax, type->length * TARGET_CHAR_BIT);
382 }
383
384
385 /* Assume the lower bits of the top of the stack hold a value of type
386 TYPE, and the upper bits are garbage. Sign-extend or truncate as
387 needed. */
388 static void
389 gen_extend (ax, type)
390 struct agent_expr *ax;
391 struct type *type;
392 {
393 int bits = type->length * TARGET_CHAR_BIT;
394 /* I just had to. */
395 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
396 }
397
398
399 /* Assume that the top of the stack contains a value of type "pointer
400 to TYPE"; generate code to fetch its value. Note that TYPE is the
401 target type, not the pointer type. */
402 static void
403 gen_fetch (ax, type)
404 struct agent_expr *ax;
405 struct type *type;
406 {
407 if (trace_kludge)
408 {
409 /* Record the area of memory we're about to fetch. */
410 ax_trace_quick (ax, TYPE_LENGTH (type));
411 }
412
413 switch (type->code)
414 {
415 case TYPE_CODE_PTR:
416 case TYPE_CODE_ENUM:
417 case TYPE_CODE_INT:
418 case TYPE_CODE_CHAR:
419 /* It's a scalar value, so we know how to dereference it. How
420 many bytes long is it? */
421 switch (type->length)
422 {
423 case 8 / TARGET_CHAR_BIT: ax_simple (ax, aop_ref8 ); break;
424 case 16 / TARGET_CHAR_BIT: ax_simple (ax, aop_ref16); break;
425 case 32 / TARGET_CHAR_BIT: ax_simple (ax, aop_ref32); break;
426 case 64 / TARGET_CHAR_BIT: ax_simple (ax, aop_ref64); break;
427
428 /* Either our caller shouldn't have asked us to dereference
429 that pointer (other code's fault), or we're not
430 implementing something we should be (this code's fault).
431 In any case, it's a bug the user shouldn't see. */
432 default:
433 error ("GDB bug: ax-gdb.c (gen_fetch): strange size");
434 }
435
436 gen_sign_extend (ax, type);
437 break;
438
439 default:
440 /* Either our caller shouldn't have asked us to dereference that
441 pointer (other code's fault), or we're not implementing
442 something we should be (this code's fault). In any case,
443 it's a bug the user shouldn't see. */
444 error ("GDB bug: ax-gdb.c (gen_fetch): bad type code");
445 }
446 }
447
448
449 /* Generate code to left shift the top of the stack by DISTANCE bits, or
450 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
451 unsigned (logical) right shifts. */
452 static void
453 gen_left_shift (ax, distance)
454 struct agent_expr *ax;
455 int distance;
456 {
457 if (distance > 0)
458 {
459 ax_const_l (ax, distance);
460 ax_simple (ax, aop_lsh);
461 }
462 else if (distance < 0)
463 {
464 ax_const_l (ax, -distance);
465 ax_simple (ax, aop_rsh_unsigned);
466 }
467 }
468
469
470 \f
471 /* Generating bytecode from GDB expressions: symbol references */
472
473 /* Generate code to push the base address of the argument portion of
474 the top stack frame. */
475 static void
476 gen_frame_args_address (ax)
477 struct agent_expr *ax;
478 {
479 long frame_reg, frame_offset;
480
481 TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
482 ax_reg (ax, frame_reg);
483 gen_offset (ax, frame_offset);
484 }
485
486
487 /* Generate code to push the base address of the locals portion of the
488 top stack frame. */
489 static void
490 gen_frame_locals_address (ax)
491 struct agent_expr *ax;
492 {
493 long frame_reg, frame_offset;
494
495 TARGET_VIRTUAL_FRAME_POINTER (ax->scope, &frame_reg, &frame_offset);
496 ax_reg (ax, frame_reg);
497 gen_offset (ax, frame_offset);
498 }
499
500
501 /* Generate code to add OFFSET to the top of the stack. Try to
502 generate short and readable code. We use this for getting to
503 variables on the stack, and structure members. If we were
504 programming in ML, it would be clearer why these are the same
505 thing. */
506 static void
507 gen_offset (ax, offset)
508 struct agent_expr *ax;
509 int offset;
510 {
511 /* It would suffice to simply push the offset and add it, but this
512 makes it easier to read positive and negative offsets in the
513 bytecode. */
514 if (offset > 0)
515 {
516 ax_const_l (ax, offset);
517 ax_simple (ax, aop_add);
518 }
519 else if (offset < 0)
520 {
521 ax_const_l (ax, -offset);
522 ax_simple (ax, aop_sub);
523 }
524 }
525
526
527 /* In many cases, a symbol's value is the offset from some other
528 address (stack frame, base register, etc.) Generate code to add
529 VAR's value to the top of the stack. */
530 static void
531 gen_sym_offset (ax, var)
532 struct agent_expr *ax;
533 struct symbol *var;
534 {
535 gen_offset (ax, SYMBOL_VALUE (var));
536 }
537
538
539 /* Generate code for a variable reference to AX. The variable is the
540 symbol VAR. Set VALUE to describe the result. */
541
542 static void
543 gen_var_ref (ax, value, var)
544 struct agent_expr *ax;
545 struct axs_value *value;
546 struct symbol *var;
547 {
548 /* Dereference any typedefs. */
549 value->type = check_typedef (SYMBOL_TYPE (var));
550
551 /* I'm imitating the code in read_var_value. */
552 switch (SYMBOL_CLASS (var))
553 {
554 case LOC_CONST: /* A constant, like an enum value. */
555 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
556 value->kind = axs_rvalue;
557 break;
558
559 case LOC_LABEL: /* A goto label, being used as a value. */
560 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
561 value->kind = axs_rvalue;
562 break;
563
564 case LOC_CONST_BYTES:
565 error ("GDB bug: ax-gdb.c (gen_var_ref): LOC_CONST_BYTES symbols are not supported");
566
567 /* Variable at a fixed location in memory. Easy. */
568 case LOC_STATIC:
569 /* Push the address of the variable. */
570 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
571 value->kind = axs_lvalue_memory;
572 break;
573
574 case LOC_ARG: /* var lives in argument area of frame */
575 gen_frame_args_address (ax);
576 gen_sym_offset (ax, var);
577 value->kind = axs_lvalue_memory;
578 break;
579
580 case LOC_REF_ARG: /* As above, but the frame slot really
581 holds the address of the variable. */
582 gen_frame_args_address (ax);
583 gen_sym_offset (ax, var);
584 /* Don't assume any particular pointer size. */
585 gen_fetch (ax, lookup_pointer_type (builtin_type_void));
586 value->kind = axs_lvalue_memory;
587 break;
588
589 case LOC_LOCAL: /* var lives in locals area of frame */
590 case LOC_LOCAL_ARG:
591 gen_frame_locals_address (ax);
592 gen_sym_offset (ax, var);
593 value->kind = axs_lvalue_memory;
594 break;
595
596 case LOC_BASEREG: /* relative to some base register */
597 case LOC_BASEREG_ARG:
598 ax_reg (ax, SYMBOL_BASEREG (var));
599 gen_sym_offset (ax, var);
600 value->kind = axs_lvalue_memory;
601 break;
602
603 case LOC_TYPEDEF:
604 error ("Cannot compute value of typedef `%s'.",
605 SYMBOL_SOURCE_NAME (var));
606 break;
607
608 case LOC_BLOCK:
609 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
610 value->kind = axs_rvalue;
611 break;
612
613 case LOC_REGISTER:
614 case LOC_REGPARM:
615 /* Don't generate any code at all; in the process of treating
616 this as an lvalue or rvalue, the caller will generate the
617 right code. */
618 value->kind = axs_lvalue_register;
619 value->u.reg = SYMBOL_VALUE (var);
620 break;
621
622 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
623 register, not on the stack. Simpler than LOC_REGISTER and
624 LOC_REGPARM, because it's just like any other case where the
625 thing has a real address. */
626 case LOC_REGPARM_ADDR:
627 ax_reg (ax, SYMBOL_VALUE (var));
628 value->kind = axs_lvalue_memory;
629 break;
630
631 case LOC_UNRESOLVED:
632 {
633 struct minimal_symbol *msym
634 = lookup_minimal_symbol (SYMBOL_NAME (var), NULL, NULL);
635 if (! msym)
636 error ("Couldn't resolve symbol `%s'.", SYMBOL_SOURCE_NAME (var));
637
638 /* Push the address of the variable. */
639 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
640 value->kind = axs_lvalue_memory;
641 }
642 break;
643
644 case LOC_OPTIMIZED_OUT:
645 error ("The variable `%s' has been optimized out.",
646 SYMBOL_SOURCE_NAME (var));
647 break;
648
649 default:
650 error ("Cannot find value of botched symbol `%s'.",
651 SYMBOL_SOURCE_NAME (var));
652 break;
653 }
654 }
655
656
657 \f
658 /* Generating bytecode from GDB expressions: literals */
659
660 static void
661 gen_int_literal (ax, value, k, type)
662 struct agent_expr *ax;
663 struct axs_value *value;
664 LONGEST k;
665 struct type *type;
666 {
667 ax_const_l (ax, k);
668 value->kind = axs_rvalue;
669 value->type = type;
670 }
671
672
673 \f
674 /* Generating bytecode from GDB expressions: unary conversions, casts */
675
676 /* Take what's on the top of the stack (as described by VALUE), and
677 try to make an rvalue out of it. Signal an error if we can't do
678 that. */
679 static void
680 require_rvalue (ax, value)
681 struct agent_expr *ax;
682 struct axs_value *value;
683 {
684 switch (value->kind)
685 {
686 case axs_rvalue:
687 /* It's already an rvalue. */
688 break;
689
690 case axs_lvalue_memory:
691 /* The top of stack is the address of the object. Dereference. */
692 gen_fetch (ax, value->type);
693 break;
694
695 case axs_lvalue_register:
696 /* There's nothing on the stack, but value->u.reg is the
697 register number containing the value.
698
699 When we add floating-point support, this is going to have to
700 change. What about SPARC register pairs, for example? */
701 ax_reg (ax, value->u.reg);
702 gen_extend (ax, value->type);
703 break;
704 }
705
706 value->kind = axs_rvalue;
707 }
708
709
710 /* Assume the top of the stack is described by VALUE, and perform the
711 usual unary conversions. This is motivated by ANSI 6.2.2, but of
712 course GDB expressions are not ANSI; they're the mishmash union of
713 a bunch of languages. Rah.
714
715 NOTE! This function promises to produce an rvalue only when the
716 incoming value is of an appropriate type. In other words, the
717 consumer of the value this function produces may assume the value
718 is an rvalue only after checking its type.
719
720 The immediate issue is that if the user tries to use a structure or
721 union as an operand of, say, the `+' operator, we don't want to try
722 to convert that structure to an rvalue; require_rvalue will bomb on
723 structs and unions. Rather, we want to simply pass the struct
724 lvalue through unchanged, and let `+' raise an error. */
725
726 static void
727 gen_usual_unary (ax, value)
728 struct agent_expr *ax;
729 struct axs_value *value;
730 {
731 /* We don't have to generate any code for the usual integral
732 conversions, since values are always represented as full-width on
733 the stack. Should we tweak the type? */
734
735 /* Some types require special handling. */
736 switch (value->type->code)
737 {
738 /* Functions get converted to a pointer to the function. */
739 case TYPE_CODE_FUNC:
740 value->type = lookup_pointer_type (value->type);
741 value->kind = axs_rvalue; /* Should always be true, but just in case. */
742 break;
743
744 /* Arrays get converted to a pointer to their first element, and
745 are no longer an lvalue. */
746 case TYPE_CODE_ARRAY:
747 {
748 struct type *elements = TYPE_TARGET_TYPE (value->type);
749 value->type = lookup_pointer_type (elements);
750 value->kind = axs_rvalue;
751 /* We don't need to generate any code; the address of the array
752 is also the address of its first element. */
753 }
754 break;
755
756 /* Don't try to convert structures and unions to rvalues. Let the
757 consumer signal an error. */
758 case TYPE_CODE_STRUCT:
759 case TYPE_CODE_UNION:
760 return;
761
762 /* If the value is an enum, call it an integer. */
763 case TYPE_CODE_ENUM:
764 value->type = builtin_type_int;
765 break;
766 }
767
768 /* If the value is an lvalue, dereference it. */
769 require_rvalue (ax, value);
770 }
771
772
773 /* Return non-zero iff the type TYPE1 is considered "wider" than the
774 type TYPE2, according to the rules described in gen_usual_arithmetic. */
775 static int
776 type_wider_than (type1, type2)
777 struct type *type1, *type2;
778 {
779 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
780 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
781 && TYPE_UNSIGNED (type1)
782 && ! TYPE_UNSIGNED (type2)));
783 }
784
785
786 /* Return the "wider" of the two types TYPE1 and TYPE2. */
787 static struct type *
788 max_type (type1, type2)
789 struct type *type1, *type2;
790 {
791 return type_wider_than (type1, type2) ? type1 : type2;
792 }
793
794
795 /* Generate code to convert a scalar value of type FROM to type TO. */
796 static void
797 gen_conversion (ax, from, to)
798 struct agent_expr *ax;
799 struct type *from, *to;
800 {
801 /* Perhaps there is a more graceful way to state these rules. */
802
803 /* If we're converting to a narrower type, then we need to clear out
804 the upper bits. */
805 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
806 gen_extend (ax, from);
807
808 /* If the two values have equal width, but different signednesses,
809 then we need to extend. */
810 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
811 {
812 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
813 gen_extend (ax, to);
814 }
815
816 /* If we're converting to a wider type, and becoming unsigned, then
817 we need to zero out any possible sign bits. */
818 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
819 {
820 if (TYPE_UNSIGNED (to))
821 gen_extend (ax, to);
822 }
823 }
824
825
826 /* Return non-zero iff the type FROM will require any bytecodes to be
827 emitted to be converted to the type TO. */
828 static int
829 is_nontrivial_conversion (from, to)
830 struct type *from, *to;
831 {
832 struct agent_expr *ax = new_agent_expr (0);
833 int nontrivial;
834
835 /* Actually generate the code, and see if anything came out. At the
836 moment, it would be trivial to replicate the code in
837 gen_conversion here, but in the future, when we're supporting
838 floating point and the like, it may not be. Doing things this
839 way allows this function to be independent of the logic in
840 gen_conversion. */
841 gen_conversion (ax, from, to);
842 nontrivial = ax->len > 0;
843 free_agent_expr (ax);
844 return nontrivial;
845 }
846
847
848 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
849 6.2.1.5) for the two operands of an arithmetic operator. This
850 effectively finds a "least upper bound" type for the two arguments,
851 and promotes each argument to that type. *VALUE1 and *VALUE2
852 describe the values as they are passed in, and as they are left. */
853 static void
854 gen_usual_arithmetic (ax, value1, value2)
855 struct agent_expr *ax;
856 struct axs_value *value1, *value2;
857 {
858 /* Do the usual binary conversions. */
859 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
860 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
861 {
862 /* The ANSI integral promotions seem to work this way: Order the
863 integer types by size, and then by signedness: an n-bit
864 unsigned type is considered "wider" than an n-bit signed
865 type. Promote to the "wider" of the two types, and always
866 promote at least to int. */
867 struct type *target = max_type (builtin_type_int,
868 max_type (value1->type, value2->type));
869
870 /* Deal with value2, on the top of the stack. */
871 gen_conversion (ax, value2->type, target);
872
873 /* Deal with value1, not on the top of the stack. Don't
874 generate the `swap' instructions if we're not actually going
875 to do anything. */
876 if (is_nontrivial_conversion (value1->type, target))
877 {
878 ax_simple (ax, aop_swap);
879 gen_conversion (ax, value1->type, target);
880 ax_simple (ax, aop_swap);
881 }
882
883 value1->type = value2->type = target;
884 }
885 }
886
887
888 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
889 the value on the top of the stack, as described by VALUE. Assume
890 the value has integral type. */
891 static void
892 gen_integral_promotions (ax, value)
893 struct agent_expr *ax;
894 struct axs_value *value;
895 {
896 if (! type_wider_than (value->type, builtin_type_int))
897 {
898 gen_conversion (ax, value->type, builtin_type_int);
899 value->type = builtin_type_int;
900 }
901 else if (! type_wider_than (value->type, builtin_type_unsigned_int))
902 {
903 gen_conversion (ax, value->type, builtin_type_unsigned_int);
904 value->type = builtin_type_unsigned_int;
905 }
906 }
907
908
909 /* Generate code for a cast to TYPE. */
910 static void
911 gen_cast (ax, value, type)
912 struct agent_expr *ax;
913 struct axs_value *value;
914 struct type *type;
915 {
916 /* GCC does allow casts to yield lvalues, so this should be fixed
917 before merging these changes into the trunk. */
918 require_rvalue (ax, value);
919 /* Dereference typedefs. */
920 type = check_typedef (type);
921
922 switch (type->code)
923 {
924 case TYPE_CODE_PTR:
925 /* It's implementation-defined, and I'll bet this is what GCC
926 does. */
927 break;
928
929 case TYPE_CODE_ARRAY:
930 case TYPE_CODE_STRUCT:
931 case TYPE_CODE_UNION:
932 case TYPE_CODE_FUNC:
933 error ("Illegal type cast: intended type must be scalar.");
934
935 case TYPE_CODE_ENUM:
936 /* We don't have to worry about the size of the value, because
937 all our integral values are fully sign-extended, and when
938 casting pointers we can do anything we like. Is there any
939 way for us to actually know what GCC actually does with a
940 cast like this? */
941 value->type = type;
942 break;
943
944 case TYPE_CODE_INT:
945 gen_conversion (ax, value->type, type);
946 break;
947
948 case TYPE_CODE_VOID:
949 /* We could pop the value, and rely on everyone else to check
950 the type and notice that this value doesn't occupy a stack
951 slot. But for now, leave the value on the stack, and
952 preserve the "value == stack element" assumption. */
953 break;
954
955 default:
956 error ("Casts to requested type are not yet implemented.");
957 }
958
959 value->type = type;
960 }
961
962
963 \f
964 /* Generating bytecode from GDB expressions: arithmetic */
965
966 /* Scale the integer on the top of the stack by the size of the target
967 of the pointer type TYPE. */
968 static void
969 gen_scale (ax, op, type)
970 struct agent_expr *ax;
971 enum agent_op op;
972 struct type *type;
973 {
974 struct type *element = TYPE_TARGET_TYPE (type);
975
976 if (element->length != 1)
977 {
978 ax_const_l (ax, element->length);
979 ax_simple (ax, op);
980 }
981 }
982
983
984 /* Generate code for an addition; non-trivial because we deal with
985 pointer arithmetic. We set VALUE to describe the result value; we
986 assume VALUE1 and VALUE2 describe the two operands, and that
987 they've undergone the usual binary conversions. Used by both
988 BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */
989 static void
990 gen_add (ax, value, value1, value2, name)
991 struct agent_expr *ax;
992 struct axs_value *value, *value1, *value2;
993 char *name;
994 {
995 /* Is it INT+PTR? */
996 if (value1->type->code == TYPE_CODE_INT
997 && value2->type->code == TYPE_CODE_PTR)
998 {
999 /* Swap the values and proceed normally. */
1000 ax_simple (ax, aop_swap);
1001 gen_scale (ax, aop_mul, value2->type);
1002 ax_simple (ax, aop_add);
1003 gen_extend (ax, value2->type); /* Catch overflow. */
1004 value->type = value2->type;
1005 }
1006
1007 /* Is it PTR+INT? */
1008 else if (value1->type->code == TYPE_CODE_PTR
1009 && value2->type->code == TYPE_CODE_INT)
1010 {
1011 gen_scale (ax, aop_mul, value1->type);
1012 ax_simple (ax, aop_add);
1013 gen_extend (ax, value1->type); /* Catch overflow. */
1014 value->type = value1->type;
1015 }
1016
1017 /* Must be number + number; the usual binary conversions will have
1018 brought them both to the same width. */
1019 else if (value1->type->code == TYPE_CODE_INT
1020 && value2->type->code == TYPE_CODE_INT)
1021 {
1022 ax_simple (ax, aop_add);
1023 gen_extend (ax, value1->type); /* Catch overflow. */
1024 value->type = value1->type;
1025 }
1026
1027 else
1028 error ("Illegal combination of types in %s.", name);
1029
1030 value->kind = axs_rvalue;
1031 }
1032
1033
1034 /* Generate code for an addition; non-trivial because we have to deal
1035 with pointer arithmetic. We set VALUE to describe the result
1036 value; we assume VALUE1 and VALUE2 describe the two operands, and
1037 that they've undergone the usual binary conversions. */
1038 static void
1039 gen_sub (ax, value, value1, value2)
1040 struct agent_expr *ax;
1041 struct axs_value *value, *value1, *value2;
1042 {
1043 struct type *element;
1044
1045 if (value1->type->code == TYPE_CODE_PTR)
1046 {
1047 /* Is it PTR - INT? */
1048 if (value2->type->code == TYPE_CODE_INT)
1049 {
1050 gen_scale (ax, aop_mul, value1->type);
1051 ax_simple (ax, aop_sub);
1052 gen_extend (ax, value1->type); /* Catch overflow. */
1053 value->type = value1->type;
1054 }
1055
1056 /* Is it PTR - PTR? Strictly speaking, the types ought to
1057 match, but this is what the normal GDB expression evaluator
1058 tests for. */
1059 else if (value2->type->code == TYPE_CODE_PTR
1060 && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1061 == TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type))))
1062 {
1063 ax_simple (ax, aop_sub);
1064 gen_scale (ax, aop_div_unsigned, value1->type);
1065 value->type = builtin_type_long; /* FIXME --- should be ptrdiff_t */
1066 }
1067 else
1068 error ("\
1069 First argument of `-' is a pointer, but second argument is neither\n\
1070 an integer nor a pointer of the same type.");
1071 }
1072
1073 /* Must be number + number. */
1074 else if (value1->type->code == TYPE_CODE_INT
1075 && value2->type->code == TYPE_CODE_INT)
1076 {
1077 ax_simple (ax, aop_sub);
1078 gen_extend (ax, value1->type); /* Catch overflow. */
1079 value->type = value1->type;
1080 }
1081
1082 else
1083 error ("Illegal combination of types in subtraction.");
1084
1085 value->kind = axs_rvalue;
1086 }
1087
1088 /* Generate code for a binary operator that doesn't do pointer magic.
1089 We set VALUE to describe the result value; we assume VALUE1 and
1090 VALUE2 describe the two operands, and that they've undergone the
1091 usual binary conversions. MAY_CARRY should be non-zero iff the
1092 result needs to be extended. NAME is the English name of the
1093 operator, used in error messages */
1094 static void
1095 gen_binop (ax, value, value1, value2, op, op_unsigned, may_carry, name)
1096 struct agent_expr *ax;
1097 struct axs_value *value, *value1, *value2;
1098 enum agent_op op, op_unsigned;
1099 int may_carry;
1100 char *name;
1101 {
1102 /* We only handle INT op INT. */
1103 if ((value1->type->code != TYPE_CODE_INT)
1104 || (value2->type->code != TYPE_CODE_INT))
1105 error ("Illegal combination of types in %s.", name);
1106
1107 ax_simple (ax,
1108 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1109 if (may_carry)
1110 gen_extend (ax, value1->type); /* catch overflow */
1111 value->type = value1->type;
1112 value->kind = axs_rvalue;
1113 }
1114
1115
1116 static void
1117 gen_logical_not (ax, value)
1118 struct agent_expr *ax;
1119 struct axs_value *value;
1120 {
1121 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1122 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1123 error ("Illegal type of operand to `!'.");
1124
1125 gen_usual_unary (ax, value);
1126 ax_simple (ax, aop_log_not);
1127 value->type = builtin_type_int;
1128 }
1129
1130
1131 static void
1132 gen_complement (ax, value)
1133 struct agent_expr *ax;
1134 struct axs_value *value;
1135 {
1136 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1137 error ("Illegal type of operand to `~'.");
1138
1139 gen_usual_unary (ax, value);
1140 gen_integral_promotions (ax, value);
1141 ax_simple (ax, aop_bit_not);
1142 gen_extend (ax, value->type);
1143 }
1144
1145
1146 \f
1147 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1148
1149 /* Dereference the value on the top of the stack. */
1150 static void
1151 gen_deref (ax, value)
1152 struct agent_expr *ax;
1153 struct axs_value *value;
1154 {
1155 /* The caller should check the type, because several operators use
1156 this, and we don't know what error message to generate. */
1157 if (value->type->code != TYPE_CODE_PTR)
1158 error ("GDB bug: ax-gdb.c (gen_deref): expected a pointer");
1159
1160 /* We've got an rvalue now, which is a pointer. We want to yield an
1161 lvalue, whose address is exactly that pointer. So we don't
1162 actually emit any code; we just change the type from "Pointer to
1163 T" to "T", and mark the value as an lvalue in memory. Leave it
1164 to the consumer to actually dereference it. */
1165 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1166 value->kind = ((value->type->code == TYPE_CODE_FUNC)
1167 ? axs_rvalue : axs_lvalue_memory);
1168 }
1169
1170
1171 /* Produce the address of the lvalue on the top of the stack. */
1172 static void
1173 gen_address_of (ax, value)
1174 struct agent_expr *ax;
1175 struct axs_value *value;
1176 {
1177 /* Special case for taking the address of a function. The ANSI
1178 standard describes this as a special case, too, so this
1179 arrangement is not without motivation. */
1180 if (value->type->code == TYPE_CODE_FUNC)
1181 /* The value's already an rvalue on the stack, so we just need to
1182 change the type. */
1183 value->type = lookup_pointer_type (value->type);
1184 else
1185 switch (value->kind)
1186 {
1187 case axs_rvalue:
1188 error ("Operand of `&' is an rvalue, which has no address.");
1189
1190 case axs_lvalue_register:
1191 error ("Operand of `&' is in a register, and has no address.");
1192
1193 case axs_lvalue_memory:
1194 value->kind = axs_rvalue;
1195 value->type = lookup_pointer_type (value->type);
1196 break;
1197 }
1198 }
1199
1200
1201 /* A lot of this stuff will have to change to support C++. But we're
1202 not going to deal with that at the moment. */
1203
1204 /* Find the field in the structure type TYPE named NAME, and return
1205 its index in TYPE's field array. */
1206 static int
1207 find_field (type, name)
1208 struct type *type;
1209 char *name;
1210 {
1211 int i;
1212
1213 CHECK_TYPEDEF (type);
1214
1215 /* Make sure this isn't C++. */
1216 if (TYPE_N_BASECLASSES (type) != 0)
1217 error ("GDB bug: ax-gdb.c (find_field): derived classes supported");
1218
1219 for (i = 0; i < TYPE_NFIELDS (type); i++)
1220 {
1221 char *this_name = TYPE_FIELD_NAME (type, i);
1222
1223 if (this_name && STREQ (name, this_name))
1224 return i;
1225
1226 if (this_name[0] == '\0')
1227 error ("GDB bug: ax-gdb.c (find_field): anonymous unions not supported");
1228 }
1229
1230 error ("Couldn't find member named `%s' in struct/union `%s'",
1231 name, type->tag_name);
1232
1233 return 0;
1234 }
1235
1236
1237 /* Generate code to push the value of a bitfield of a structure whose
1238 address is on the top of the stack. START and END give the
1239 starting and one-past-ending *bit* numbers of the field within the
1240 structure. */
1241 static void
1242 gen_bitfield_ref (ax, value, type, start, end)
1243 struct agent_expr *ax;
1244 struct axs_value *value;
1245 struct type *type;
1246 int start, end;
1247 {
1248 /* Note that ops[i] fetches 8 << i bits. */
1249 static enum agent_op ops[]
1250 = { aop_ref8, aop_ref16, aop_ref32, aop_ref64 };
1251 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1252
1253 /* We don't want to touch any byte that the bitfield doesn't
1254 actually occupy; we shouldn't make any accesses we're not
1255 explicitly permitted to. We rely here on the fact that the
1256 bytecode `ref' operators work on unaligned addresses.
1257
1258 It takes some fancy footwork to get the stack to work the way
1259 we'd like. Say we're retrieving a bitfield that requires three
1260 fetches. Initially, the stack just contains the address:
1261 addr
1262 For the first fetch, we duplicate the address
1263 addr addr
1264 then add the byte offset, do the fetch, and shift and mask as
1265 needed, yielding a fragment of the value, properly aligned for
1266 the final bitwise or:
1267 addr frag1
1268 then we swap, and repeat the process:
1269 frag1 addr --- address on top
1270 frag1 addr addr --- duplicate it
1271 frag1 addr frag2 --- get second fragment
1272 frag1 frag2 addr --- swap again
1273 frag1 frag2 frag3 --- get third fragment
1274 Notice that, since the third fragment is the last one, we don't
1275 bother duplicating the address this time. Now we have all the
1276 fragments on the stack, and we can simply `or' them together,
1277 yielding the final value of the bitfield. */
1278
1279 /* The first and one-after-last bits in the field, but rounded down
1280 and up to byte boundaries. */
1281 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1282 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1283 / TARGET_CHAR_BIT)
1284 * TARGET_CHAR_BIT);
1285
1286 /* current bit offset within the structure */
1287 int offset;
1288
1289 /* The index in ops of the opcode we're considering. */
1290 int op;
1291
1292 /* The number of fragments we generated in the process. Probably
1293 equal to the number of `one' bits in bytesize, but who cares? */
1294 int fragment_count;
1295
1296 /* Dereference any typedefs. */
1297 type = check_typedef (type);
1298
1299 /* Can we fetch the number of bits requested at all? */
1300 if ((end - start) > ((1 << num_ops) * 8))
1301 error ("GDB bug: ax-gdb.c (gen_bitfield_ref): bitfield too wide");
1302
1303 /* Note that we know here that we only need to try each opcode once.
1304 That may not be true on machines with weird byte sizes. */
1305 offset = bound_start;
1306 fragment_count = 0;
1307 for (op = num_ops - 1; op >= 0; op--)
1308 {
1309 /* number of bits that ops[op] would fetch */
1310 int op_size = 8 << op;
1311
1312 /* The stack at this point, from bottom to top, contains zero or
1313 more fragments, then the address. */
1314
1315 /* Does this fetch fit within the bitfield? */
1316 if (offset + op_size <= bound_end)
1317 {
1318 /* Is this the last fragment? */
1319 int last_frag = (offset + op_size == bound_end);
1320
1321 if (! last_frag)
1322 ax_simple (ax, aop_dup); /* keep a copy of the address */
1323
1324 /* Add the offset. */
1325 gen_offset (ax, offset / TARGET_CHAR_BIT);
1326
1327 if (trace_kludge)
1328 {
1329 /* Record the area of memory we're about to fetch. */
1330 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1331 }
1332
1333 /* Perform the fetch. */
1334 ax_simple (ax, ops[op]);
1335
1336 /* Shift the bits we have to their proper position.
1337 gen_left_shift will generate right shifts when the operand
1338 is negative.
1339
1340 A big-endian field diagram to ponder:
1341 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1342 +------++------++------++------++------++------++------++------+
1343 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1344 ^ ^ ^ ^
1345 bit number 16 32 48 53
1346 These are bit numbers as supplied by GDB. Note that the
1347 bit numbers run from right to left once you've fetched the
1348 value!
1349
1350 A little-endian field diagram to ponder:
1351 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1352 +------++------++------++------++------++------++------++------+
1353 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1354 ^ ^ ^ ^ ^
1355 bit number 48 32 16 4 0
1356
1357 In both cases, the most significant end is on the left
1358 (i.e. normal numeric writing order), which means that you
1359 don't go crazy thinking about `left' and `right' shifts.
1360
1361 We don't have to worry about masking yet:
1362 - If they contain garbage off the least significant end, then we
1363 must be looking at the low end of the field, and the right
1364 shift will wipe them out.
1365 - If they contain garbage off the most significant end, then we
1366 must be looking at the most significant end of the word, and
1367 the sign/zero extension will wipe them out.
1368 - If we're in the interior of the word, then there is no garbage
1369 on either end, because the ref operators zero-extend. */
1370 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1371 gen_left_shift (ax, end - (offset + op_size));
1372 else
1373 gen_left_shift (ax, offset - start);
1374
1375 if (! last_frag)
1376 /* Bring the copy of the address up to the top. */
1377 ax_simple (ax, aop_swap);
1378
1379 offset += op_size;
1380 fragment_count++;
1381 }
1382 }
1383
1384 /* Generate enough bitwise `or' operations to combine all the
1385 fragments we left on the stack. */
1386 while (fragment_count-- > 1)
1387 ax_simple (ax, aop_bit_or);
1388
1389 /* Sign- or zero-extend the value as appropriate. */
1390 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1391
1392 /* This is *not* an lvalue. Ugh. */
1393 value->kind = axs_rvalue;
1394 value->type = type;
1395 }
1396
1397
1398 /* Generate code to reference the member named FIELD of a structure or
1399 union. The top of the stack, as described by VALUE, should have
1400 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1401 the operator being compiled, and OPERAND_NAME is the kind of thing
1402 it operates on; we use them in error messages. */
1403 static void
1404 gen_struct_ref (ax, value, field, operator_name, operand_name)
1405 struct agent_expr *ax;
1406 struct axs_value *value;
1407 char *field;
1408 char *operator_name;
1409 char *operand_name;
1410 {
1411 struct type *type;
1412 int i;
1413
1414 /* Follow pointers until we reach a non-pointer. These aren't the C
1415 semantics, but they're what the normal GDB evaluator does, so we
1416 should at least be consistent. */
1417 while (value->type->code == TYPE_CODE_PTR)
1418 {
1419 gen_usual_unary (ax, value);
1420 gen_deref (ax, value);
1421 }
1422 type = value->type;
1423
1424 /* This must yield a structure or a union. */
1425 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1426 && TYPE_CODE (type) != TYPE_CODE_UNION)
1427 error ("The left operand of `%s' is not a %s.",
1428 operator_name, operand_name);
1429
1430 /* And it must be in memory; we don't deal with structure rvalues,
1431 or structures living in registers. */
1432 if (value->kind != axs_lvalue_memory)
1433 error ("Structure does not live in memory.");
1434
1435 i = find_field (type, field);
1436
1437 /* Is this a bitfield? */
1438 if (TYPE_FIELD_PACKED (type, i))
1439 gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, i),
1440 TYPE_FIELD_BITPOS (type, i),
1441 (TYPE_FIELD_BITPOS (type, i)
1442 + TYPE_FIELD_BITSIZE (type, i)));
1443 else
1444 {
1445 gen_offset (ax, TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT);
1446 value->kind = axs_lvalue_memory;
1447 value->type = TYPE_FIELD_TYPE (type, i);
1448 }
1449 }
1450
1451
1452 /* Generate code for GDB's magical `repeat' operator.
1453 LVALUE @ INT creates an array INT elements long, and whose elements
1454 have the same type as LVALUE, located in memory so that LVALUE is
1455 its first element. For example, argv[0]@argc gives you the array
1456 of command-line arguments.
1457
1458 Unfortunately, because we have to know the types before we actually
1459 have a value for the expression, we can't implement this perfectly
1460 without changing the type system, having values that occupy two
1461 stack slots, doing weird things with sizeof, etc. So we require
1462 the right operand to be a constant expression. */
1463 static void
1464 gen_repeat (pc, ax, value)
1465 union exp_element **pc;
1466 struct agent_expr *ax;
1467 struct axs_value *value;
1468 {
1469 struct axs_value value1;
1470 /* We don't want to turn this into an rvalue, so no conversions
1471 here. */
1472 gen_expr (pc, ax, &value1);
1473 if (value1.kind != axs_lvalue_memory)
1474 error ("Left operand of `@' must be an object in memory.");
1475
1476 /* Evaluate the length; it had better be a constant. */
1477 {
1478 struct value *v = const_expr (pc);
1479 int length;
1480
1481 if (! v)
1482 error ("Right operand of `@' must be a constant, in agent expressions.");
1483 if (v->type->code != TYPE_CODE_INT)
1484 error ("Right operand of `@' must be an integer.");
1485 length = value_as_long (v);
1486 if (length <= 0)
1487 error ("Right operand of `@' must be positive.");
1488
1489 /* The top of the stack is already the address of the object, so
1490 all we need to do is frob the type of the lvalue. */
1491 {
1492 /* FIXME-type-allocation: need a way to free this type when we are
1493 done with it. */
1494 struct type *range
1495 = create_range_type (0, builtin_type_int, 0, length - 1);
1496 struct type *array = create_array_type (0, value1.type, range);
1497
1498 value->kind = axs_lvalue_memory;
1499 value->type = array;
1500 }
1501 }
1502 }
1503
1504
1505 /* Emit code for the `sizeof' operator.
1506 *PC should point at the start of the operand expression; we advance it
1507 to the first instruction after the operand. */
1508 static void
1509 gen_sizeof (pc, ax, value)
1510 union exp_element **pc;
1511 struct agent_expr *ax;
1512 struct axs_value *value;
1513 {
1514 /* We don't care about the value of the operand expression; we only
1515 care about its type. However, in the current arrangement, the
1516 only way to find an expression's type is to generate code for it.
1517 So we generate code for the operand, and then throw it away,
1518 replacing it with code that simply pushes its size. */
1519 int start = ax->len;
1520 gen_expr (pc, ax, value);
1521
1522 /* Throw away the code we just generated. */
1523 ax->len = start;
1524
1525 ax_const_l (ax, TYPE_LENGTH (value->type));
1526 value->kind = axs_rvalue;
1527 value->type = builtin_type_int;
1528 }
1529
1530 \f
1531 /* Generating bytecode from GDB expressions: general recursive thingy */
1532
1533 /* A gen_expr function written by a Gen-X'er guy.
1534 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1535 static void
1536 gen_expr (pc, ax, value)
1537 union exp_element **pc;
1538 struct agent_expr *ax;
1539 struct axs_value *value;
1540 {
1541 /* Used to hold the descriptions of operand expressions. */
1542 struct axs_value value1, value2;
1543 enum exp_opcode op = (*pc)[0].opcode;
1544
1545 /* If we're looking at a constant expression, just push its value. */
1546 {
1547 struct value *v = maybe_const_expr (pc);
1548
1549 if (v)
1550 {
1551 ax_const_l (ax, value_as_long (v));
1552 value->kind = axs_rvalue;
1553 value->type = check_typedef (VALUE_TYPE (v));
1554 return;
1555 }
1556 }
1557
1558 /* Otherwise, go ahead and generate code for it. */
1559 switch (op)
1560 {
1561 /* Binary arithmetic operators. */
1562 case BINOP_ADD:
1563 case BINOP_SUB:
1564 case BINOP_MUL:
1565 case BINOP_DIV:
1566 case BINOP_REM:
1567 case BINOP_SUBSCRIPT:
1568 case BINOP_BITWISE_AND:
1569 case BINOP_BITWISE_IOR:
1570 case BINOP_BITWISE_XOR:
1571 (*pc)++;
1572 gen_expr (pc, ax, &value1);
1573 gen_usual_unary (ax, &value1);
1574 gen_expr (pc, ax, &value2);
1575 gen_usual_unary (ax, &value2);
1576 gen_usual_arithmetic (ax, &value1, &value2);
1577 switch (op)
1578 {
1579 case BINOP_ADD:
1580 gen_add (ax, value, &value1, &value2, "addition");
1581 break;
1582 case BINOP_SUB:
1583 gen_sub (ax, value, &value1, &value2);
1584 break;
1585 case BINOP_MUL:
1586 gen_binop (ax, value, &value1, &value2,
1587 aop_mul, aop_mul, 1, "multiplication");
1588 break;
1589 case BINOP_DIV:
1590 gen_binop (ax, value, &value1, &value2,
1591 aop_div_signed, aop_div_unsigned, 1, "division");
1592 break;
1593 case BINOP_REM:
1594 gen_binop (ax, value, &value1, &value2,
1595 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
1596 break;
1597 case BINOP_SUBSCRIPT:
1598 gen_add (ax, value, &value1, &value2, "array subscripting");
1599 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1600 error ("Illegal combination of types in array subscripting.");
1601 gen_deref (ax, value);
1602 break;
1603 case BINOP_BITWISE_AND:
1604 gen_binop (ax, value, &value1, &value2,
1605 aop_bit_and, aop_bit_and, 0, "bitwise and");
1606 break;
1607
1608 case BINOP_BITWISE_IOR:
1609 gen_binop (ax, value, &value1, &value2,
1610 aop_bit_or, aop_bit_or, 0, "bitwise or");
1611 break;
1612
1613 case BINOP_BITWISE_XOR:
1614 gen_binop (ax, value, &value1, &value2,
1615 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
1616 break;
1617
1618 default:
1619 /* We should only list operators in the outer case statement
1620 that we actually handle in the inner case statement. */
1621 error ("GDB bug: ax-gdb.c (gen_expr): op case sets don't match");
1622 }
1623 break;
1624
1625 /* Note that we need to be a little subtle about generating code
1626 for comma. In C, we can do some optimizations here because
1627 we know the left operand is only being evaluated for effect.
1628 However, if the tracing kludge is in effect, then we always
1629 need to evaluate the left hand side fully, so that all the
1630 variables it mentions get traced. */
1631 case BINOP_COMMA:
1632 (*pc)++;
1633 gen_expr (pc, ax, &value1);
1634 /* Don't just dispose of the left operand. We might be tracing,
1635 in which case we want to emit code to trace it if it's an
1636 lvalue. */
1637 gen_traced_pop (ax, &value1);
1638 gen_expr (pc, ax, value);
1639 /* It's the consumer's responsibility to trace the right operand. */
1640 break;
1641
1642 case OP_LONG: /* some integer constant */
1643 {
1644 struct type *type = (*pc)[1].type;
1645 LONGEST k = (*pc)[2].longconst;
1646 (*pc) += 4;
1647 gen_int_literal (ax, value, k, type);
1648 }
1649 break;
1650
1651 case OP_VAR_VALUE:
1652 gen_var_ref (ax, value, (*pc)[2].symbol);
1653 (*pc) += 4;
1654 break;
1655
1656 case OP_REGISTER:
1657 {
1658 int reg = (int) (*pc)[1].longconst;
1659 (*pc) += 3;
1660 value->kind = axs_lvalue_register;
1661 value->u.reg = reg;
1662 value->type = REGISTER_VIRTUAL_TYPE (reg);
1663 }
1664 break;
1665
1666 case OP_INTERNALVAR:
1667 error ("GDB agent expressions cannot use convenience variables.");
1668
1669 /* Weirdo operator: see comments for gen_repeat for details. */
1670 case BINOP_REPEAT:
1671 /* Note that gen_repeat handles its own argument evaluation. */
1672 (*pc)++;
1673 gen_repeat (pc, ax, value);
1674 break;
1675
1676 case UNOP_CAST:
1677 {
1678 struct type *type = (*pc)[1].type;
1679 (*pc) += 3;
1680 gen_expr (pc, ax, value);
1681 gen_cast (ax, value, type);
1682 }
1683 break;
1684
1685 case UNOP_MEMVAL:
1686 {
1687 struct type *type = check_typedef ((*pc)[1].type);
1688 (*pc) += 3;
1689 gen_expr (pc, ax, value);
1690 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
1691 it's just a hack for dealing with minsyms; you take some
1692 integer constant, pretend it's the address of an lvalue of
1693 the given type, and dereference it. */
1694 if (value->kind != axs_rvalue)
1695 /* This would be weird. */
1696 error ("GDB bug: ax-gdb.c (gen_expr): OP_MEMVAL operand isn't an rvalue???");
1697 value->type = type;
1698 value->kind = axs_lvalue_memory;
1699 }
1700 break;
1701
1702 case UNOP_NEG:
1703 (*pc)++;
1704 /* -FOO is equivalent to 0 - FOO. */
1705 gen_int_literal (ax, &value1, (LONGEST) 0, builtin_type_int);
1706 gen_usual_unary (ax, &value1); /* shouldn't do much */
1707 gen_expr (pc, ax, &value2);
1708 gen_usual_unary (ax, &value2);
1709 gen_usual_arithmetic (ax, &value1, &value2);
1710 gen_sub (ax, value, &value1, &value2);
1711 break;
1712
1713 case UNOP_LOGICAL_NOT:
1714 (*pc)++;
1715 gen_expr (pc, ax, value);
1716 gen_logical_not (ax, value);
1717 break;
1718
1719 case UNOP_COMPLEMENT:
1720 (*pc)++;
1721 gen_expr (pc, ax, value);
1722 gen_complement (ax, value);
1723 break;
1724
1725 case UNOP_IND:
1726 (*pc)++;
1727 gen_expr (pc, ax, value);
1728 gen_usual_unary (ax, value);
1729 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1730 error ("Argument of unary `*' is not a pointer.");
1731 gen_deref (ax, value);
1732 break;
1733
1734 case UNOP_ADDR:
1735 (*pc)++;
1736 gen_expr (pc, ax, value);
1737 gen_address_of (ax, value);
1738 break;
1739
1740 case UNOP_SIZEOF:
1741 (*pc)++;
1742 /* Notice that gen_sizeof handles its own operand, unlike most
1743 of the other unary operator functions. This is because we
1744 have to throw away the code we generate. */
1745 gen_sizeof (pc, ax, value);
1746 break;
1747
1748 case STRUCTOP_STRUCT:
1749 case STRUCTOP_PTR:
1750 {
1751 int length = (*pc)[1].longconst;
1752 char *name = &(*pc)[2].string;
1753
1754 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
1755 gen_expr (pc, ax, value);
1756 if (op == STRUCTOP_STRUCT)
1757 gen_struct_ref (ax, value, name, ".", "structure or union");
1758 else if (op == STRUCTOP_PTR)
1759 gen_struct_ref (ax, value, name, "->",
1760 "pointer to a structure or union");
1761 else
1762 /* If this `if' chain doesn't handle it, then the case list
1763 shouldn't mention it, and we shouldn't be here. */
1764 error ("GDB bug: ax-gdb.c (gen_expr): unhandled struct case");
1765 }
1766 break;
1767
1768 case OP_TYPE:
1769 error ("Attempt to use a type name as an expression.");
1770
1771 default:
1772 error ("Unsupported operator in expression.");
1773 }
1774 }
1775
1776
1777 \f
1778 #if 0 /* not used */
1779 /* Generating bytecode from GDB expressions: driver */
1780
1781 /* Given a GDB expression EXPR, produce a string of agent bytecode
1782 which computes its value. Return the agent expression, and set
1783 *VALUE to describe its type, and whether it's an lvalue or rvalue. */
1784 struct agent_expr *
1785 expr_to_agent (expr, value)
1786 struct expression *expr;
1787 struct axs_value *value;
1788 {
1789 struct cleanup *old_chain = 0;
1790 struct agent_expr *ax = new_agent_expr ();
1791 union exp_element *pc;
1792
1793 old_chain = make_cleanup ((make_cleanup_func) free_agent_expr, ax);
1794
1795 pc = expr->elts;
1796 trace_kludge = 0;
1797 gen_expr (&pc, ax, value);
1798
1799 /* We have successfully built the agent expr, so cancel the cleanup
1800 request. If we add more cleanups that we always want done, this
1801 will have to get more complicated. */
1802 discard_cleanups (old_chain);
1803 return ax;
1804 }
1805
1806
1807 /* Given a GDB expression EXPR denoting an lvalue in memory, produce a
1808 string of agent bytecode which will leave its address and size on
1809 the top of stack. Return the agent expression.
1810
1811 Not sure this function is useful at all. */
1812 struct agent_expr *
1813 expr_to_address_and_size (expr)
1814 struct expression *expr;
1815 {
1816 struct axs_value value;
1817 struct agent_expr *ax = expr_to_agent (expr, &value);
1818
1819 /* Complain if the result is not a memory lvalue. */
1820 if (value.kind != axs_lvalue_memory)
1821 {
1822 free_agent_expr (ax);
1823 error ("Expression does not denote an object in memory.");
1824 }
1825
1826 /* Push the object's size on the stack. */
1827 ax_const_l (ax, TYPE_LENGTH (value.type));
1828
1829 return ax;
1830 }
1831 #endif /* 0 */
1832
1833 /* Given a GDB expression EXPR, return bytecode to trace its value.
1834 The result will use the `trace' and `trace_quick' bytecodes to
1835 record the value of all memory touched by the expression. The
1836 caller can then use the ax_reqs function to discover which
1837 registers it relies upon. */
1838 struct agent_expr *
1839 gen_trace_for_expr (scope, expr)
1840 CORE_ADDR scope;
1841 struct expression *expr;
1842 {
1843 struct cleanup *old_chain = 0;
1844 struct agent_expr *ax = new_agent_expr (scope);
1845 union exp_element *pc;
1846 struct axs_value value;
1847
1848 old_chain = make_cleanup ((make_cleanup_func) free_agent_expr, ax);
1849
1850 pc = expr->elts;
1851 trace_kludge = 1;
1852 gen_expr (&pc, ax, &value);
1853
1854 /* Make sure we record the final object, and get rid of it. */
1855 gen_traced_pop (ax, &value);
1856
1857 /* Oh, and terminate. */
1858 ax_simple (ax, aop_end);
1859
1860 /* We have successfully built the agent expr, so cancel the cleanup
1861 request. If we add more cleanups that we always want done, this
1862 will have to get more complicated. */
1863 discard_cleanups (old_chain);
1864 return ax;
1865 }
1866
1867
1868 \f
1869 /* The "agent" command, for testing: compile and disassemble an expression. */
1870
1871 static void
1872 print_axs_value (f, value)
1873 GDB_FILE *f;
1874 struct axs_value *value;
1875 {
1876 switch (value->kind)
1877 {
1878 case axs_rvalue:
1879 fputs_filtered ("rvalue", f);
1880 break;
1881
1882 case axs_lvalue_memory:
1883 fputs_filtered ("memory lvalue", f);
1884 break;
1885
1886 case axs_lvalue_register:
1887 fprintf_filtered (f, "register %d lvalue", value->u.reg);
1888 break;
1889 }
1890
1891 fputs_filtered (" : ", f);
1892 type_print (value->type, "", f, -1);
1893 }
1894
1895
1896 static void
1897 agent_command (exp, from_tty)
1898 char *exp;
1899 int from_tty;
1900 {
1901 struct cleanup *old_chain = 0;
1902 struct expression *expr;
1903 struct agent_expr *agent;
1904 struct agent_reqs reqs;
1905 struct frame_info *fi = get_current_frame (); /* need current scope */
1906
1907 /* We don't deal with overlay debugging at the moment. We need to
1908 think more carefully about this. If you copy this code into
1909 another command, change the error message; the user shouldn't
1910 have to know anything about agent expressions. */
1911 if (overlay_debugging)
1912 error ("GDB can't do agent expression translation with overlays.");
1913
1914 if (exp == 0)
1915 error_no_arg ("expression to translate");
1916
1917 expr = parse_expression (exp);
1918 old_chain = make_cleanup ((make_cleanup_func) free_current_contents, &expr);
1919 agent = gen_trace_for_expr (fi->pc, expr);
1920 make_cleanup ((make_cleanup_func) free_agent_expr, agent);
1921 ax_print (gdb_stdout, agent);
1922 ax_reqs (agent, &reqs);
1923
1924 do_cleanups (old_chain);
1925 dont_repeat ();
1926 }
1927
1928 \f
1929 /* Initialization code. */
1930
1931 void _initialize_ax_gdb PARAMS ((void));
1932 void
1933 _initialize_ax_gdb ()
1934 {
1935 struct cmd_list_element *c;
1936
1937 add_cmd ("agent", class_maintenance, agent_command,
1938 "Translate an expression into remote agent bytecode.",
1939 &maintenancelist);
1940 }