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