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