cc057f89a02a4ca57c6b2d466f11921ec713e5a3
[deliverable/binutils-gdb.git] / gdb / ax-gdb.c
1 /* GDB-specific functions for operating on agent expressions.
2
3 Copyright (C) 1998-2001, 2003, 2007-2012 Free Software Foundation,
4 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 "language.h"
26 #include "value.h"
27 #include "expression.h"
28 #include "command.h"
29 #include "gdbcmd.h"
30 #include "frame.h"
31 #include "target.h"
32 #include "ax.h"
33 #include "ax-gdb.h"
34 #include "gdb_string.h"
35 #include "block.h"
36 #include "regcache.h"
37 #include "user-regs.h"
38 #include "language.h"
39 #include "dictionary.h"
40 #include "breakpoint.h"
41 #include "tracepoint.h"
42 #include "cp-support.h"
43 #include "arch-utils.h"
44
45 #include "valprint.h"
46 #include "c-lang.h"
47
48 /* To make sense of this file, you should read doc/agentexpr.texi.
49 Then look at the types and enums in ax-gdb.h. For the code itself,
50 look at gen_expr, towards the bottom; that's the main function that
51 looks at the GDB expressions and calls everything else to generate
52 code.
53
54 I'm beginning to wonder whether it wouldn't be nicer to internally
55 generate trees, with types, and then spit out the bytecode in
56 linear form afterwards; we could generate fewer `swap', `ext', and
57 `zero_ext' bytecodes that way; it would make good constant folding
58 easier, too. But at the moment, I think we should be willing to
59 pay for the simplicity of this code with less-than-optimal bytecode
60 strings.
61
62 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
63 \f
64
65
66 /* Prototypes for local functions. */
67
68 /* There's a standard order to the arguments of these functions:
69 union exp_element ** --- pointer into expression
70 struct agent_expr * --- agent expression buffer to generate code into
71 struct axs_value * --- describes value left on top of stack */
72
73 static struct value *const_var_ref (struct symbol *var);
74 static struct value *const_expr (union exp_element **pc);
75 static struct value *maybe_const_expr (union exp_element **pc);
76
77 static void gen_traced_pop (struct gdbarch *, struct agent_expr *,
78 struct axs_value *);
79
80 static void gen_sign_extend (struct agent_expr *, struct type *);
81 static void gen_extend (struct agent_expr *, struct type *);
82 static void gen_fetch (struct agent_expr *, struct type *);
83 static void gen_left_shift (struct agent_expr *, int);
84
85
86 static void gen_frame_args_address (struct gdbarch *, struct agent_expr *);
87 static void gen_frame_locals_address (struct gdbarch *, struct agent_expr *);
88 static void gen_offset (struct agent_expr *ax, int offset);
89 static void gen_sym_offset (struct agent_expr *, struct symbol *);
90 static void gen_var_ref (struct gdbarch *, struct agent_expr *ax,
91 struct axs_value *value, struct symbol *var);
92
93
94 static void gen_int_literal (struct agent_expr *ax,
95 struct axs_value *value,
96 LONGEST k, struct type *type);
97
98
99 static void require_rvalue (struct agent_expr *ax, struct axs_value *value);
100 static void gen_usual_unary (struct expression *exp, struct agent_expr *ax,
101 struct axs_value *value);
102 static int type_wider_than (struct type *type1, struct type *type2);
103 static struct type *max_type (struct type *type1, struct type *type2);
104 static void gen_conversion (struct agent_expr *ax,
105 struct type *from, struct type *to);
106 static int is_nontrivial_conversion (struct type *from, struct type *to);
107 static void gen_usual_arithmetic (struct expression *exp,
108 struct agent_expr *ax,
109 struct axs_value *value1,
110 struct axs_value *value2);
111 static void gen_integral_promotions (struct expression *exp,
112 struct agent_expr *ax,
113 struct axs_value *value);
114 static void gen_cast (struct agent_expr *ax,
115 struct axs_value *value, struct type *type);
116 static void gen_scale (struct agent_expr *ax,
117 enum agent_op op, struct type *type);
118 static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
119 struct axs_value *value1, struct axs_value *value2);
120 static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
121 struct axs_value *value1, struct axs_value *value2);
122 static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
123 struct axs_value *value1, struct axs_value *value2,
124 struct type *result_type);
125 static void gen_binop (struct agent_expr *ax,
126 struct axs_value *value,
127 struct axs_value *value1,
128 struct axs_value *value2,
129 enum agent_op op,
130 enum agent_op op_unsigned, int may_carry, char *name);
131 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
132 struct type *result_type);
133 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
134 static void gen_deref (struct agent_expr *, struct axs_value *);
135 static void gen_address_of (struct agent_expr *, struct axs_value *);
136 static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
137 struct axs_value *value,
138 struct type *type, int start, int end);
139 static void gen_primitive_field (struct expression *exp,
140 struct agent_expr *ax,
141 struct axs_value *value,
142 int offset, int fieldno, struct type *type);
143 static int gen_struct_ref_recursive (struct expression *exp,
144 struct agent_expr *ax,
145 struct axs_value *value,
146 char *field, int offset,
147 struct type *type);
148 static void gen_struct_ref (struct expression *exp, struct agent_expr *ax,
149 struct axs_value *value,
150 char *field,
151 char *operator_name, char *operand_name);
152 static void gen_static_field (struct gdbarch *gdbarch,
153 struct agent_expr *ax, struct axs_value *value,
154 struct type *type, int fieldno);
155 static void gen_repeat (struct expression *exp, union exp_element **pc,
156 struct agent_expr *ax, struct axs_value *value);
157 static void gen_sizeof (struct expression *exp, union exp_element **pc,
158 struct agent_expr *ax, struct axs_value *value,
159 struct type *size_type);
160 static void gen_expr (struct expression *exp, union exp_element **pc,
161 struct agent_expr *ax, struct axs_value *value);
162 static void gen_expr_binop_rest (struct expression *exp,
163 enum exp_opcode op, union exp_element **pc,
164 struct agent_expr *ax,
165 struct axs_value *value,
166 struct axs_value *value1,
167 struct axs_value *value2);
168
169 static void agent_command (char *exp, int from_tty);
170 \f
171
172 /* Detecting constant expressions. */
173
174 /* If the variable reference at *PC is a constant, return its value.
175 Otherwise, return zero.
176
177 Hey, Wally! How can a variable reference be a constant?
178
179 Well, Beav, this function really handles the OP_VAR_VALUE operator,
180 not specifically variable references. GDB uses OP_VAR_VALUE to
181 refer to any kind of symbolic reference: function names, enum
182 elements, and goto labels are all handled through the OP_VAR_VALUE
183 operator, even though they're constants. It makes sense given the
184 situation.
185
186 Gee, Wally, don'cha wonder sometimes if data representations that
187 subvert commonly accepted definitions of terms in favor of heavily
188 context-specific interpretations are really just a tool of the
189 programming hegemony to preserve their power and exclude the
190 proletariat? */
191
192 static struct value *
193 const_var_ref (struct symbol *var)
194 {
195 struct type *type = SYMBOL_TYPE (var);
196
197 switch (SYMBOL_CLASS (var))
198 {
199 case LOC_CONST:
200 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
201
202 case LOC_LABEL:
203 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
204
205 default:
206 return 0;
207 }
208 }
209
210
211 /* If the expression starting at *PC has a constant value, return it.
212 Otherwise, return zero. If we return a value, then *PC will be
213 advanced to the end of it. If we return zero, *PC could be
214 anywhere. */
215 static struct value *
216 const_expr (union exp_element **pc)
217 {
218 enum exp_opcode op = (*pc)->opcode;
219 struct value *v1;
220
221 switch (op)
222 {
223 case OP_LONG:
224 {
225 struct type *type = (*pc)[1].type;
226 LONGEST k = (*pc)[2].longconst;
227
228 (*pc) += 4;
229 return value_from_longest (type, k);
230 }
231
232 case OP_VAR_VALUE:
233 {
234 struct value *v = const_var_ref ((*pc)[2].symbol);
235
236 (*pc) += 4;
237 return v;
238 }
239
240 /* We could add more operators in here. */
241
242 case UNOP_NEG:
243 (*pc)++;
244 v1 = const_expr (pc);
245 if (v1)
246 return value_neg (v1);
247 else
248 return 0;
249
250 default:
251 return 0;
252 }
253 }
254
255
256 /* Like const_expr, but guarantee also that *PC is undisturbed if the
257 expression is not constant. */
258 static struct value *
259 maybe_const_expr (union exp_element **pc)
260 {
261 union exp_element *tentative_pc = *pc;
262 struct value *v = const_expr (&tentative_pc);
263
264 /* If we got a value, then update the real PC. */
265 if (v)
266 *pc = tentative_pc;
267
268 return v;
269 }
270 \f
271
272 /* Generating bytecode from GDB expressions: general assumptions */
273
274 /* Here are a few general assumptions made throughout the code; if you
275 want to make a change that contradicts one of these, then you'd
276 better scan things pretty thoroughly.
277
278 - We assume that all values occupy one stack element. For example,
279 sometimes we'll swap to get at the left argument to a binary
280 operator. If we decide that void values should occupy no stack
281 elements, or that synthetic arrays (whose size is determined at
282 run time, created by the `@' operator) should occupy two stack
283 elements (address and length), then this will cause trouble.
284
285 - We assume the stack elements are infinitely wide, and that we
286 don't have to worry what happens if the user requests an
287 operation that is wider than the actual interpreter's stack.
288 That is, it's up to the interpreter to handle directly all the
289 integer widths the user has access to. (Woe betide the language
290 with bignums!)
291
292 - We don't support side effects. Thus, we don't have to worry about
293 GCC's generalized lvalues, function calls, etc.
294
295 - We don't support floating point. Many places where we switch on
296 some type don't bother to include cases for floating point; there
297 may be even more subtle ways this assumption exists. For
298 example, the arguments to % must be integers.
299
300 - We assume all subexpressions have a static, unchanging type. If
301 we tried to support convenience variables, this would be a
302 problem.
303
304 - All values on the stack should always be fully zero- or
305 sign-extended.
306
307 (I wasn't sure whether to choose this or its opposite --- that
308 only addresses are assumed extended --- but it turns out that
309 neither convention completely eliminates spurious extend
310 operations (if everything is always extended, then you have to
311 extend after add, because it could overflow; if nothing is
312 extended, then you end up producing extends whenever you change
313 sizes), and this is simpler.) */
314 \f
315
316 /* Generating bytecode from GDB expressions: the `trace' kludge */
317
318 /* The compiler in this file is a general-purpose mechanism for
319 translating GDB expressions into bytecode. One ought to be able to
320 find a million and one uses for it.
321
322 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
323 of expediency. Let he who is without sin cast the first stone.
324
325 For the data tracing facility, we need to insert `trace' bytecodes
326 before each data fetch; this records all the memory that the
327 expression touches in the course of evaluation, so that memory will
328 be available when the user later tries to evaluate the expression
329 in GDB.
330
331 This should be done (I think) in a post-processing pass, that walks
332 an arbitrary agent expression and inserts `trace' operations at the
333 appropriate points. But it's much faster to just hack them
334 directly into the code. And since we're in a crunch, that's what
335 I've done.
336
337 Setting the flag trace_kludge to non-zero enables the code that
338 emits the trace bytecodes at the appropriate points. */
339 int trace_kludge;
340
341 /* Inspired by trace_kludge, this indicates that pointers to chars
342 should get an added tracenz bytecode to record nonzero bytes, up to
343 a length that is the value of trace_string_kludge. */
344 int trace_string_kludge;
345
346 /* Scan for all static fields in the given class, including any base
347 classes, and generate tracing bytecodes for each. */
348
349 static void
350 gen_trace_static_fields (struct gdbarch *gdbarch,
351 struct agent_expr *ax,
352 struct type *type)
353 {
354 int i, nbases = TYPE_N_BASECLASSES (type);
355 struct axs_value value;
356
357 CHECK_TYPEDEF (type);
358
359 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
360 {
361 if (field_is_static (&TYPE_FIELD (type, i)))
362 {
363 gen_static_field (gdbarch, ax, &value, type, i);
364 if (value.optimized_out)
365 continue;
366 switch (value.kind)
367 {
368 case axs_lvalue_memory:
369 {
370 int length = TYPE_LENGTH (check_typedef (value.type));
371
372 ax_const_l (ax, length);
373 ax_simple (ax, aop_trace);
374 }
375 break;
376
377 case axs_lvalue_register:
378 /* We don't actually need the register's value to be pushed,
379 just note that we need it to be collected. */
380 ax_reg_mask (ax, value.u.reg);
381
382 default:
383 break;
384 }
385 }
386 }
387
388 /* Now scan through base classes recursively. */
389 for (i = 0; i < nbases; i++)
390 {
391 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
392
393 gen_trace_static_fields (gdbarch, ax, basetype);
394 }
395 }
396
397 /* Trace the lvalue on the stack, if it needs it. In either case, pop
398 the value. Useful on the left side of a comma, and at the end of
399 an expression being used for tracing. */
400 static void
401 gen_traced_pop (struct gdbarch *gdbarch,
402 struct agent_expr *ax, struct axs_value *value)
403 {
404 int string_trace = 0;
405 if (trace_string_kludge
406 && TYPE_CODE (value->type) == TYPE_CODE_PTR
407 && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
408 's'))
409 string_trace = 1;
410
411 if (trace_kludge)
412 switch (value->kind)
413 {
414 case axs_rvalue:
415 if (string_trace)
416 {
417 ax_const_l (ax, trace_string_kludge);
418 ax_simple (ax, aop_tracenz);
419 }
420 else
421 /* We don't trace rvalues, just the lvalues necessary to
422 produce them. So just dispose of this value. */
423 ax_simple (ax, aop_pop);
424 break;
425
426 case axs_lvalue_memory:
427 {
428 int length = TYPE_LENGTH (check_typedef (value->type));
429
430 if (string_trace)
431 ax_simple (ax, aop_dup);
432
433 /* There's no point in trying to use a trace_quick bytecode
434 here, since "trace_quick SIZE pop" is three bytes, whereas
435 "const8 SIZE trace" is also three bytes, does the same
436 thing, and the simplest code which generates that will also
437 work correctly for objects with large sizes. */
438 ax_const_l (ax, length);
439 ax_simple (ax, aop_trace);
440
441 if (string_trace)
442 {
443 ax_simple (ax, aop_ref32);
444 ax_const_l (ax, trace_string_kludge);
445 ax_simple (ax, aop_tracenz);
446 }
447 }
448 break;
449
450 case axs_lvalue_register:
451 /* We don't actually need the register's value to be on the
452 stack, and the target will get heartburn if the register is
453 larger than will fit in a stack, so just mark it for
454 collection and be done with it. */
455 ax_reg_mask (ax, value->u.reg);
456
457 /* But if the register points to a string, assume the value
458 will fit on the stack and push it anyway. */
459 if (string_trace)
460 {
461 ax_reg (ax, value->u.reg);
462 ax_const_l (ax, trace_string_kludge);
463 ax_simple (ax, aop_tracenz);
464 }
465 break;
466 }
467 else
468 /* If we're not tracing, just pop the value. */
469 ax_simple (ax, aop_pop);
470
471 /* To trace C++ classes with static fields stored elsewhere. */
472 if (trace_kludge
473 && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
474 || TYPE_CODE (value->type) == TYPE_CODE_UNION))
475 gen_trace_static_fields (gdbarch, ax, value->type);
476 }
477 \f
478
479
480 /* Generating bytecode from GDB expressions: helper functions */
481
482 /* Assume that the lower bits of the top of the stack is a value of
483 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
484 static void
485 gen_sign_extend (struct agent_expr *ax, struct type *type)
486 {
487 /* Do we need to sign-extend this? */
488 if (!TYPE_UNSIGNED (type))
489 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
490 }
491
492
493 /* Assume the lower bits of the top of the stack hold a value of type
494 TYPE, and the upper bits are garbage. Sign-extend or truncate as
495 needed. */
496 static void
497 gen_extend (struct agent_expr *ax, struct type *type)
498 {
499 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
500
501 /* I just had to. */
502 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
503 }
504
505
506 /* Assume that the top of the stack contains a value of type "pointer
507 to TYPE"; generate code to fetch its value. Note that TYPE is the
508 target type, not the pointer type. */
509 static void
510 gen_fetch (struct agent_expr *ax, struct type *type)
511 {
512 if (trace_kludge)
513 {
514 /* Record the area of memory we're about to fetch. */
515 ax_trace_quick (ax, TYPE_LENGTH (type));
516 }
517
518 switch (TYPE_CODE (type))
519 {
520 case TYPE_CODE_PTR:
521 case TYPE_CODE_REF:
522 case TYPE_CODE_ENUM:
523 case TYPE_CODE_INT:
524 case TYPE_CODE_CHAR:
525 case TYPE_CODE_BOOL:
526 /* It's a scalar value, so we know how to dereference it. How
527 many bytes long is it? */
528 switch (TYPE_LENGTH (type))
529 {
530 case 8 / TARGET_CHAR_BIT:
531 ax_simple (ax, aop_ref8);
532 break;
533 case 16 / TARGET_CHAR_BIT:
534 ax_simple (ax, aop_ref16);
535 break;
536 case 32 / TARGET_CHAR_BIT:
537 ax_simple (ax, aop_ref32);
538 break;
539 case 64 / TARGET_CHAR_BIT:
540 ax_simple (ax, aop_ref64);
541 break;
542
543 /* Either our caller shouldn't have asked us to dereference
544 that pointer (other code's fault), or we're not
545 implementing something we should be (this code's fault).
546 In any case, it's a bug the user shouldn't see. */
547 default:
548 internal_error (__FILE__, __LINE__,
549 _("gen_fetch: strange size"));
550 }
551
552 gen_sign_extend (ax, type);
553 break;
554
555 default:
556 /* Our caller requested us to dereference a pointer from an unsupported
557 type. Error out and give callers a chance to handle the failure
558 gracefully. */
559 error (_("gen_fetch: Unsupported type code `%s'."),
560 TYPE_NAME (type));
561 }
562 }
563
564
565 /* Generate code to left shift the top of the stack by DISTANCE bits, or
566 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
567 unsigned (logical) right shifts. */
568 static void
569 gen_left_shift (struct agent_expr *ax, int distance)
570 {
571 if (distance > 0)
572 {
573 ax_const_l (ax, distance);
574 ax_simple (ax, aop_lsh);
575 }
576 else if (distance < 0)
577 {
578 ax_const_l (ax, -distance);
579 ax_simple (ax, aop_rsh_unsigned);
580 }
581 }
582 \f
583
584
585 /* Generating bytecode from GDB expressions: symbol references */
586
587 /* Generate code to push the base address of the argument portion of
588 the top stack frame. */
589 static void
590 gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
591 {
592 int frame_reg;
593 LONGEST frame_offset;
594
595 gdbarch_virtual_frame_pointer (gdbarch,
596 ax->scope, &frame_reg, &frame_offset);
597 ax_reg (ax, frame_reg);
598 gen_offset (ax, frame_offset);
599 }
600
601
602 /* Generate code to push the base address of the locals portion of the
603 top stack frame. */
604 static void
605 gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
606 {
607 int frame_reg;
608 LONGEST frame_offset;
609
610 gdbarch_virtual_frame_pointer (gdbarch,
611 ax->scope, &frame_reg, &frame_offset);
612 ax_reg (ax, frame_reg);
613 gen_offset (ax, frame_offset);
614 }
615
616
617 /* Generate code to add OFFSET to the top of the stack. Try to
618 generate short and readable code. We use this for getting to
619 variables on the stack, and structure members. If we were
620 programming in ML, it would be clearer why these are the same
621 thing. */
622 static void
623 gen_offset (struct agent_expr *ax, int offset)
624 {
625 /* It would suffice to simply push the offset and add it, but this
626 makes it easier to read positive and negative offsets in the
627 bytecode. */
628 if (offset > 0)
629 {
630 ax_const_l (ax, offset);
631 ax_simple (ax, aop_add);
632 }
633 else if (offset < 0)
634 {
635 ax_const_l (ax, -offset);
636 ax_simple (ax, aop_sub);
637 }
638 }
639
640
641 /* In many cases, a symbol's value is the offset from some other
642 address (stack frame, base register, etc.) Generate code to add
643 VAR's value to the top of the stack. */
644 static void
645 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
646 {
647 gen_offset (ax, SYMBOL_VALUE (var));
648 }
649
650
651 /* Generate code for a variable reference to AX. The variable is the
652 symbol VAR. Set VALUE to describe the result. */
653
654 static void
655 gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
656 struct axs_value *value, struct symbol *var)
657 {
658 /* Dereference any typedefs. */
659 value->type = check_typedef (SYMBOL_TYPE (var));
660 value->optimized_out = 0;
661
662 /* I'm imitating the code in read_var_value. */
663 switch (SYMBOL_CLASS (var))
664 {
665 case LOC_CONST: /* A constant, like an enum value. */
666 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
667 value->kind = axs_rvalue;
668 break;
669
670 case LOC_LABEL: /* A goto label, being used as a value. */
671 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
672 value->kind = axs_rvalue;
673 break;
674
675 case LOC_CONST_BYTES:
676 internal_error (__FILE__, __LINE__,
677 _("gen_var_ref: LOC_CONST_BYTES "
678 "symbols are not supported"));
679
680 /* Variable at a fixed location in memory. Easy. */
681 case LOC_STATIC:
682 /* Push the address of the variable. */
683 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
684 value->kind = axs_lvalue_memory;
685 break;
686
687 case LOC_ARG: /* var lives in argument area of frame */
688 gen_frame_args_address (gdbarch, ax);
689 gen_sym_offset (ax, var);
690 value->kind = axs_lvalue_memory;
691 break;
692
693 case LOC_REF_ARG: /* As above, but the frame slot really
694 holds the address of the variable. */
695 gen_frame_args_address (gdbarch, ax);
696 gen_sym_offset (ax, var);
697 /* Don't assume any particular pointer size. */
698 gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
699 value->kind = axs_lvalue_memory;
700 break;
701
702 case LOC_LOCAL: /* var lives in locals area of frame */
703 gen_frame_locals_address (gdbarch, ax);
704 gen_sym_offset (ax, var);
705 value->kind = axs_lvalue_memory;
706 break;
707
708 case LOC_TYPEDEF:
709 error (_("Cannot compute value of typedef `%s'."),
710 SYMBOL_PRINT_NAME (var));
711 break;
712
713 case LOC_BLOCK:
714 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
715 value->kind = axs_rvalue;
716 break;
717
718 case LOC_REGISTER:
719 /* Don't generate any code at all; in the process of treating
720 this as an lvalue or rvalue, the caller will generate the
721 right code. */
722 value->kind = axs_lvalue_register;
723 value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
724 break;
725
726 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
727 register, not on the stack. Simpler than LOC_REGISTER
728 because it's just like any other case where the thing
729 has a real address. */
730 case LOC_REGPARM_ADDR:
731 ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
732 value->kind = axs_lvalue_memory;
733 break;
734
735 case LOC_UNRESOLVED:
736 {
737 struct minimal_symbol *msym
738 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
739
740 if (!msym)
741 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
742
743 /* Push the address of the variable. */
744 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
745 value->kind = axs_lvalue_memory;
746 }
747 break;
748
749 case LOC_COMPUTED:
750 /* FIXME: cagney/2004-01-26: It should be possible to
751 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
752 Unfortunately DWARF 2 stores the frame-base (instead of the
753 function) location in a function's symbol. Oops! For the
754 moment enable this when/where applicable. */
755 SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
756 break;
757
758 case LOC_OPTIMIZED_OUT:
759 /* Flag this, but don't say anything; leave it up to callers to
760 warn the user. */
761 value->optimized_out = 1;
762 break;
763
764 default:
765 error (_("Cannot find value of botched symbol `%s'."),
766 SYMBOL_PRINT_NAME (var));
767 break;
768 }
769 }
770 \f
771
772
773 /* Generating bytecode from GDB expressions: literals */
774
775 static void
776 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
777 struct type *type)
778 {
779 ax_const_l (ax, k);
780 value->kind = axs_rvalue;
781 value->type = check_typedef (type);
782 }
783 \f
784
785
786 /* Generating bytecode from GDB expressions: unary conversions, casts */
787
788 /* Take what's on the top of the stack (as described by VALUE), and
789 try to make an rvalue out of it. Signal an error if we can't do
790 that. */
791 static void
792 require_rvalue (struct agent_expr *ax, struct axs_value *value)
793 {
794 /* Only deal with scalars, structs and such may be too large
795 to fit in a stack entry. */
796 value->type = check_typedef (value->type);
797 if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
798 || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
799 || TYPE_CODE (value->type) == TYPE_CODE_UNION
800 || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
801 error (_("Value not scalar: cannot be an rvalue."));
802
803 switch (value->kind)
804 {
805 case axs_rvalue:
806 /* It's already an rvalue. */
807 break;
808
809 case axs_lvalue_memory:
810 /* The top of stack is the address of the object. Dereference. */
811 gen_fetch (ax, value->type);
812 break;
813
814 case axs_lvalue_register:
815 /* There's nothing on the stack, but value->u.reg is the
816 register number containing the value.
817
818 When we add floating-point support, this is going to have to
819 change. What about SPARC register pairs, for example? */
820 ax_reg (ax, value->u.reg);
821 gen_extend (ax, value->type);
822 break;
823 }
824
825 value->kind = axs_rvalue;
826 }
827
828
829 /* Assume the top of the stack is described by VALUE, and perform the
830 usual unary conversions. This is motivated by ANSI 6.2.2, but of
831 course GDB expressions are not ANSI; they're the mishmash union of
832 a bunch of languages. Rah.
833
834 NOTE! This function promises to produce an rvalue only when the
835 incoming value is of an appropriate type. In other words, the
836 consumer of the value this function produces may assume the value
837 is an rvalue only after checking its type.
838
839 The immediate issue is that if the user tries to use a structure or
840 union as an operand of, say, the `+' operator, we don't want to try
841 to convert that structure to an rvalue; require_rvalue will bomb on
842 structs and unions. Rather, we want to simply pass the struct
843 lvalue through unchanged, and let `+' raise an error. */
844
845 static void
846 gen_usual_unary (struct expression *exp, struct agent_expr *ax,
847 struct axs_value *value)
848 {
849 /* We don't have to generate any code for the usual integral
850 conversions, since values are always represented as full-width on
851 the stack. Should we tweak the type? */
852
853 /* Some types require special handling. */
854 switch (TYPE_CODE (value->type))
855 {
856 /* Functions get converted to a pointer to the function. */
857 case TYPE_CODE_FUNC:
858 value->type = lookup_pointer_type (value->type);
859 value->kind = axs_rvalue; /* Should always be true, but just in case. */
860 break;
861
862 /* Arrays get converted to a pointer to their first element, and
863 are no longer an lvalue. */
864 case TYPE_CODE_ARRAY:
865 {
866 struct type *elements = TYPE_TARGET_TYPE (value->type);
867
868 value->type = lookup_pointer_type (elements);
869 value->kind = axs_rvalue;
870 /* We don't need to generate any code; the address of the array
871 is also the address of its first element. */
872 }
873 break;
874
875 /* Don't try to convert structures and unions to rvalues. Let the
876 consumer signal an error. */
877 case TYPE_CODE_STRUCT:
878 case TYPE_CODE_UNION:
879 return;
880 }
881
882 /* If the value is an lvalue, dereference it. */
883 require_rvalue (ax, value);
884 }
885
886
887 /* Return non-zero iff the type TYPE1 is considered "wider" than the
888 type TYPE2, according to the rules described in gen_usual_arithmetic. */
889 static int
890 type_wider_than (struct type *type1, struct type *type2)
891 {
892 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
893 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
894 && TYPE_UNSIGNED (type1)
895 && !TYPE_UNSIGNED (type2)));
896 }
897
898
899 /* Return the "wider" of the two types TYPE1 and TYPE2. */
900 static struct type *
901 max_type (struct type *type1, struct type *type2)
902 {
903 return type_wider_than (type1, type2) ? type1 : type2;
904 }
905
906
907 /* Generate code to convert a scalar value of type FROM to type TO. */
908 static void
909 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
910 {
911 /* Perhaps there is a more graceful way to state these rules. */
912
913 /* If we're converting to a narrower type, then we need to clear out
914 the upper bits. */
915 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
916 gen_extend (ax, from);
917
918 /* If the two values have equal width, but different signednesses,
919 then we need to extend. */
920 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
921 {
922 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
923 gen_extend (ax, to);
924 }
925
926 /* If we're converting to a wider type, and becoming unsigned, then
927 we need to zero out any possible sign bits. */
928 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
929 {
930 if (TYPE_UNSIGNED (to))
931 gen_extend (ax, to);
932 }
933 }
934
935
936 /* Return non-zero iff the type FROM will require any bytecodes to be
937 emitted to be converted to the type TO. */
938 static int
939 is_nontrivial_conversion (struct type *from, struct type *to)
940 {
941 struct agent_expr *ax = new_agent_expr (NULL, 0);
942 int nontrivial;
943
944 /* Actually generate the code, and see if anything came out. At the
945 moment, it would be trivial to replicate the code in
946 gen_conversion here, but in the future, when we're supporting
947 floating point and the like, it may not be. Doing things this
948 way allows this function to be independent of the logic in
949 gen_conversion. */
950 gen_conversion (ax, from, to);
951 nontrivial = ax->len > 0;
952 free_agent_expr (ax);
953 return nontrivial;
954 }
955
956
957 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
958 6.2.1.5) for the two operands of an arithmetic operator. This
959 effectively finds a "least upper bound" type for the two arguments,
960 and promotes each argument to that type. *VALUE1 and *VALUE2
961 describe the values as they are passed in, and as they are left. */
962 static void
963 gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
964 struct axs_value *value1, struct axs_value *value2)
965 {
966 /* Do the usual binary conversions. */
967 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
968 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
969 {
970 /* The ANSI integral promotions seem to work this way: Order the
971 integer types by size, and then by signedness: an n-bit
972 unsigned type is considered "wider" than an n-bit signed
973 type. Promote to the "wider" of the two types, and always
974 promote at least to int. */
975 struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
976 max_type (value1->type, value2->type));
977
978 /* Deal with value2, on the top of the stack. */
979 gen_conversion (ax, value2->type, target);
980
981 /* Deal with value1, not on the top of the stack. Don't
982 generate the `swap' instructions if we're not actually going
983 to do anything. */
984 if (is_nontrivial_conversion (value1->type, target))
985 {
986 ax_simple (ax, aop_swap);
987 gen_conversion (ax, value1->type, target);
988 ax_simple (ax, aop_swap);
989 }
990
991 value1->type = value2->type = check_typedef (target);
992 }
993 }
994
995
996 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
997 the value on the top of the stack, as described by VALUE. Assume
998 the value has integral type. */
999 static void
1000 gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
1001 struct axs_value *value)
1002 {
1003 const struct builtin_type *builtin = builtin_type (exp->gdbarch);
1004
1005 if (!type_wider_than (value->type, builtin->builtin_int))
1006 {
1007 gen_conversion (ax, value->type, builtin->builtin_int);
1008 value->type = builtin->builtin_int;
1009 }
1010 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
1011 {
1012 gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
1013 value->type = builtin->builtin_unsigned_int;
1014 }
1015 }
1016
1017
1018 /* Generate code for a cast to TYPE. */
1019 static void
1020 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
1021 {
1022 /* GCC does allow casts to yield lvalues, so this should be fixed
1023 before merging these changes into the trunk. */
1024 require_rvalue (ax, value);
1025 /* Dereference typedefs. */
1026 type = check_typedef (type);
1027
1028 switch (TYPE_CODE (type))
1029 {
1030 case TYPE_CODE_PTR:
1031 case TYPE_CODE_REF:
1032 /* It's implementation-defined, and I'll bet this is what GCC
1033 does. */
1034 break;
1035
1036 case TYPE_CODE_ARRAY:
1037 case TYPE_CODE_STRUCT:
1038 case TYPE_CODE_UNION:
1039 case TYPE_CODE_FUNC:
1040 error (_("Invalid type cast: intended type must be scalar."));
1041
1042 case TYPE_CODE_ENUM:
1043 case TYPE_CODE_BOOL:
1044 /* We don't have to worry about the size of the value, because
1045 all our integral values are fully sign-extended, and when
1046 casting pointers we can do anything we like. Is there any
1047 way for us to know what GCC actually does with a cast like
1048 this? */
1049 break;
1050
1051 case TYPE_CODE_INT:
1052 gen_conversion (ax, value->type, type);
1053 break;
1054
1055 case TYPE_CODE_VOID:
1056 /* We could pop the value, and rely on everyone else to check
1057 the type and notice that this value doesn't occupy a stack
1058 slot. But for now, leave the value on the stack, and
1059 preserve the "value == stack element" assumption. */
1060 break;
1061
1062 default:
1063 error (_("Casts to requested type are not yet implemented."));
1064 }
1065
1066 value->type = type;
1067 }
1068 \f
1069
1070
1071 /* Generating bytecode from GDB expressions: arithmetic */
1072
1073 /* Scale the integer on the top of the stack by the size of the target
1074 of the pointer type TYPE. */
1075 static void
1076 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
1077 {
1078 struct type *element = TYPE_TARGET_TYPE (type);
1079
1080 if (TYPE_LENGTH (element) != 1)
1081 {
1082 ax_const_l (ax, TYPE_LENGTH (element));
1083 ax_simple (ax, op);
1084 }
1085 }
1086
1087
1088 /* Generate code for pointer arithmetic PTR + INT. */
1089 static void
1090 gen_ptradd (struct agent_expr *ax, struct axs_value *value,
1091 struct axs_value *value1, struct axs_value *value2)
1092 {
1093 gdb_assert (pointer_type (value1->type));
1094 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1095
1096 gen_scale (ax, aop_mul, value1->type);
1097 ax_simple (ax, aop_add);
1098 gen_extend (ax, value1->type); /* Catch overflow. */
1099 value->type = value1->type;
1100 value->kind = axs_rvalue;
1101 }
1102
1103
1104 /* Generate code for pointer arithmetic PTR - INT. */
1105 static void
1106 gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
1107 struct axs_value *value1, struct axs_value *value2)
1108 {
1109 gdb_assert (pointer_type (value1->type));
1110 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1111
1112 gen_scale (ax, aop_mul, value1->type);
1113 ax_simple (ax, aop_sub);
1114 gen_extend (ax, value1->type); /* Catch overflow. */
1115 value->type = value1->type;
1116 value->kind = axs_rvalue;
1117 }
1118
1119
1120 /* Generate code for pointer arithmetic PTR - PTR. */
1121 static void
1122 gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
1123 struct axs_value *value1, struct axs_value *value2,
1124 struct type *result_type)
1125 {
1126 gdb_assert (pointer_type (value1->type));
1127 gdb_assert (pointer_type (value2->type));
1128
1129 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1130 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
1131 error (_("\
1132 First argument of `-' is a pointer, but second argument is neither\n\
1133 an integer nor a pointer of the same type."));
1134
1135 ax_simple (ax, aop_sub);
1136 gen_scale (ax, aop_div_unsigned, value1->type);
1137 value->type = result_type;
1138 value->kind = axs_rvalue;
1139 }
1140
1141 static void
1142 gen_equal (struct agent_expr *ax, struct axs_value *value,
1143 struct axs_value *value1, struct axs_value *value2,
1144 struct type *result_type)
1145 {
1146 if (pointer_type (value1->type) || pointer_type (value2->type))
1147 ax_simple (ax, aop_equal);
1148 else
1149 gen_binop (ax, value, value1, value2,
1150 aop_equal, aop_equal, 0, "equal");
1151 value->type = result_type;
1152 value->kind = axs_rvalue;
1153 }
1154
1155 static void
1156 gen_less (struct agent_expr *ax, struct axs_value *value,
1157 struct axs_value *value1, struct axs_value *value2,
1158 struct type *result_type)
1159 {
1160 if (pointer_type (value1->type) || pointer_type (value2->type))
1161 ax_simple (ax, aop_less_unsigned);
1162 else
1163 gen_binop (ax, value, value1, value2,
1164 aop_less_signed, aop_less_unsigned, 0, "less than");
1165 value->type = result_type;
1166 value->kind = axs_rvalue;
1167 }
1168
1169 /* Generate code for a binary operator that doesn't do pointer magic.
1170 We set VALUE to describe the result value; we assume VALUE1 and
1171 VALUE2 describe the two operands, and that they've undergone the
1172 usual binary conversions. MAY_CARRY should be non-zero iff the
1173 result needs to be extended. NAME is the English name of the
1174 operator, used in error messages */
1175 static void
1176 gen_binop (struct agent_expr *ax, struct axs_value *value,
1177 struct axs_value *value1, struct axs_value *value2,
1178 enum agent_op op, enum agent_op op_unsigned,
1179 int may_carry, char *name)
1180 {
1181 /* We only handle INT op INT. */
1182 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1183 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1184 error (_("Invalid combination of types in %s."), name);
1185
1186 ax_simple (ax,
1187 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1188 if (may_carry)
1189 gen_extend (ax, value1->type); /* catch overflow */
1190 value->type = value1->type;
1191 value->kind = axs_rvalue;
1192 }
1193
1194
1195 static void
1196 gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1197 struct type *result_type)
1198 {
1199 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1200 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1201 error (_("Invalid type of operand to `!'."));
1202
1203 ax_simple (ax, aop_log_not);
1204 value->type = result_type;
1205 }
1206
1207
1208 static void
1209 gen_complement (struct agent_expr *ax, struct axs_value *value)
1210 {
1211 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1212 error (_("Invalid type of operand to `~'."));
1213
1214 ax_simple (ax, aop_bit_not);
1215 gen_extend (ax, value->type);
1216 }
1217 \f
1218
1219
1220 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1221
1222 /* Dereference the value on the top of the stack. */
1223 static void
1224 gen_deref (struct agent_expr *ax, struct axs_value *value)
1225 {
1226 /* The caller should check the type, because several operators use
1227 this, and we don't know what error message to generate. */
1228 if (!pointer_type (value->type))
1229 internal_error (__FILE__, __LINE__,
1230 _("gen_deref: expected a pointer"));
1231
1232 /* We've got an rvalue now, which is a pointer. We want to yield an
1233 lvalue, whose address is exactly that pointer. So we don't
1234 actually emit any code; we just change the type from "Pointer to
1235 T" to "T", and mark the value as an lvalue in memory. Leave it
1236 to the consumer to actually dereference it. */
1237 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1238 if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
1239 error (_("Attempt to dereference a generic pointer."));
1240 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1241 ? axs_rvalue : axs_lvalue_memory);
1242 }
1243
1244
1245 /* Produce the address of the lvalue on the top of the stack. */
1246 static void
1247 gen_address_of (struct agent_expr *ax, struct axs_value *value)
1248 {
1249 /* Special case for taking the address of a function. The ANSI
1250 standard describes this as a special case, too, so this
1251 arrangement is not without motivation. */
1252 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1253 /* The value's already an rvalue on the stack, so we just need to
1254 change the type. */
1255 value->type = lookup_pointer_type (value->type);
1256 else
1257 switch (value->kind)
1258 {
1259 case axs_rvalue:
1260 error (_("Operand of `&' is an rvalue, which has no address."));
1261
1262 case axs_lvalue_register:
1263 error (_("Operand of `&' is in a register, and has no address."));
1264
1265 case axs_lvalue_memory:
1266 value->kind = axs_rvalue;
1267 value->type = lookup_pointer_type (value->type);
1268 break;
1269 }
1270 }
1271
1272 /* Generate code to push the value of a bitfield of a structure whose
1273 address is on the top of the stack. START and END give the
1274 starting and one-past-ending *bit* numbers of the field within the
1275 structure. */
1276 static void
1277 gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
1278 struct axs_value *value, struct type *type,
1279 int start, int end)
1280 {
1281 /* Note that ops[i] fetches 8 << i bits. */
1282 static enum agent_op ops[]
1283 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1284 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1285
1286 /* We don't want to touch any byte that the bitfield doesn't
1287 actually occupy; we shouldn't make any accesses we're not
1288 explicitly permitted to. We rely here on the fact that the
1289 bytecode `ref' operators work on unaligned addresses.
1290
1291 It takes some fancy footwork to get the stack to work the way
1292 we'd like. Say we're retrieving a bitfield that requires three
1293 fetches. Initially, the stack just contains the address:
1294 addr
1295 For the first fetch, we duplicate the address
1296 addr addr
1297 then add the byte offset, do the fetch, and shift and mask as
1298 needed, yielding a fragment of the value, properly aligned for
1299 the final bitwise or:
1300 addr frag1
1301 then we swap, and repeat the process:
1302 frag1 addr --- address on top
1303 frag1 addr addr --- duplicate it
1304 frag1 addr frag2 --- get second fragment
1305 frag1 frag2 addr --- swap again
1306 frag1 frag2 frag3 --- get third fragment
1307 Notice that, since the third fragment is the last one, we don't
1308 bother duplicating the address this time. Now we have all the
1309 fragments on the stack, and we can simply `or' them together,
1310 yielding the final value of the bitfield. */
1311
1312 /* The first and one-after-last bits in the field, but rounded down
1313 and up to byte boundaries. */
1314 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1315 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1316 / TARGET_CHAR_BIT)
1317 * TARGET_CHAR_BIT);
1318
1319 /* current bit offset within the structure */
1320 int offset;
1321
1322 /* The index in ops of the opcode we're considering. */
1323 int op;
1324
1325 /* The number of fragments we generated in the process. Probably
1326 equal to the number of `one' bits in bytesize, but who cares? */
1327 int fragment_count;
1328
1329 /* Dereference any typedefs. */
1330 type = check_typedef (type);
1331
1332 /* Can we fetch the number of bits requested at all? */
1333 if ((end - start) > ((1 << num_ops) * 8))
1334 internal_error (__FILE__, __LINE__,
1335 _("gen_bitfield_ref: bitfield too wide"));
1336
1337 /* Note that we know here that we only need to try each opcode once.
1338 That may not be true on machines with weird byte sizes. */
1339 offset = bound_start;
1340 fragment_count = 0;
1341 for (op = num_ops - 1; op >= 0; op--)
1342 {
1343 /* number of bits that ops[op] would fetch */
1344 int op_size = 8 << op;
1345
1346 /* The stack at this point, from bottom to top, contains zero or
1347 more fragments, then the address. */
1348
1349 /* Does this fetch fit within the bitfield? */
1350 if (offset + op_size <= bound_end)
1351 {
1352 /* Is this the last fragment? */
1353 int last_frag = (offset + op_size == bound_end);
1354
1355 if (!last_frag)
1356 ax_simple (ax, aop_dup); /* keep a copy of the address */
1357
1358 /* Add the offset. */
1359 gen_offset (ax, offset / TARGET_CHAR_BIT);
1360
1361 if (trace_kludge)
1362 {
1363 /* Record the area of memory we're about to fetch. */
1364 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1365 }
1366
1367 /* Perform the fetch. */
1368 ax_simple (ax, ops[op]);
1369
1370 /* Shift the bits we have to their proper position.
1371 gen_left_shift will generate right shifts when the operand
1372 is negative.
1373
1374 A big-endian field diagram to ponder:
1375 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1376 +------++------++------++------++------++------++------++------+
1377 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1378 ^ ^ ^ ^
1379 bit number 16 32 48 53
1380 These are bit numbers as supplied by GDB. Note that the
1381 bit numbers run from right to left once you've fetched the
1382 value!
1383
1384 A little-endian field diagram to ponder:
1385 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1386 +------++------++------++------++------++------++------++------+
1387 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1388 ^ ^ ^ ^ ^
1389 bit number 48 32 16 4 0
1390
1391 In both cases, the most significant end is on the left
1392 (i.e. normal numeric writing order), which means that you
1393 don't go crazy thinking about `left' and `right' shifts.
1394
1395 We don't have to worry about masking yet:
1396 - If they contain garbage off the least significant end, then we
1397 must be looking at the low end of the field, and the right
1398 shift will wipe them out.
1399 - If they contain garbage off the most significant end, then we
1400 must be looking at the most significant end of the word, and
1401 the sign/zero extension will wipe them out.
1402 - If we're in the interior of the word, then there is no garbage
1403 on either end, because the ref operators zero-extend. */
1404 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
1405 gen_left_shift (ax, end - (offset + op_size));
1406 else
1407 gen_left_shift (ax, offset - start);
1408
1409 if (!last_frag)
1410 /* Bring the copy of the address up to the top. */
1411 ax_simple (ax, aop_swap);
1412
1413 offset += op_size;
1414 fragment_count++;
1415 }
1416 }
1417
1418 /* Generate enough bitwise `or' operations to combine all the
1419 fragments we left on the stack. */
1420 while (fragment_count-- > 1)
1421 ax_simple (ax, aop_bit_or);
1422
1423 /* Sign- or zero-extend the value as appropriate. */
1424 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1425
1426 /* This is *not* an lvalue. Ugh. */
1427 value->kind = axs_rvalue;
1428 value->type = type;
1429 }
1430
1431 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1432 is an accumulated offset (in bytes), will be nonzero for objects
1433 embedded in other objects, like C++ base classes. Behavior should
1434 generally follow value_primitive_field. */
1435
1436 static void
1437 gen_primitive_field (struct expression *exp,
1438 struct agent_expr *ax, struct axs_value *value,
1439 int offset, int fieldno, struct type *type)
1440 {
1441 /* Is this a bitfield? */
1442 if (TYPE_FIELD_PACKED (type, fieldno))
1443 gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
1444 (offset * TARGET_CHAR_BIT
1445 + TYPE_FIELD_BITPOS (type, fieldno)),
1446 (offset * TARGET_CHAR_BIT
1447 + TYPE_FIELD_BITPOS (type, fieldno)
1448 + TYPE_FIELD_BITSIZE (type, fieldno)));
1449 else
1450 {
1451 gen_offset (ax, offset
1452 + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1453 value->kind = axs_lvalue_memory;
1454 value->type = TYPE_FIELD_TYPE (type, fieldno);
1455 }
1456 }
1457
1458 /* Search for the given field in either the given type or one of its
1459 base classes. Return 1 if found, 0 if not. */
1460
1461 static int
1462 gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
1463 struct axs_value *value,
1464 char *field, int offset, struct type *type)
1465 {
1466 int i, rslt;
1467 int nbases = TYPE_N_BASECLASSES (type);
1468
1469 CHECK_TYPEDEF (type);
1470
1471 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1472 {
1473 const char *this_name = TYPE_FIELD_NAME (type, i);
1474
1475 if (this_name)
1476 {
1477 if (strcmp (field, this_name) == 0)
1478 {
1479 /* Note that bytecodes for the struct's base (aka
1480 "this") will have been generated already, which will
1481 be unnecessary but not harmful if the static field is
1482 being handled as a global. */
1483 if (field_is_static (&TYPE_FIELD (type, i)))
1484 {
1485 gen_static_field (exp->gdbarch, ax, value, type, i);
1486 if (value->optimized_out)
1487 error (_("static field `%s' has been "
1488 "optimized out, cannot use"),
1489 field);
1490 return 1;
1491 }
1492
1493 gen_primitive_field (exp, ax, value, offset, i, type);
1494 return 1;
1495 }
1496 #if 0 /* is this right? */
1497 if (this_name[0] == '\0')
1498 internal_error (__FILE__, __LINE__,
1499 _("find_field: anonymous unions not supported"));
1500 #endif
1501 }
1502 }
1503
1504 /* Now scan through base classes recursively. */
1505 for (i = 0; i < nbases; i++)
1506 {
1507 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1508
1509 rslt = gen_struct_ref_recursive (exp, ax, value, field,
1510 offset + TYPE_BASECLASS_BITPOS (type, i)
1511 / TARGET_CHAR_BIT,
1512 basetype);
1513 if (rslt)
1514 return 1;
1515 }
1516
1517 /* Not found anywhere, flag so caller can complain. */
1518 return 0;
1519 }
1520
1521 /* Generate code to reference the member named FIELD of a structure or
1522 union. The top of the stack, as described by VALUE, should have
1523 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1524 the operator being compiled, and OPERAND_NAME is the kind of thing
1525 it operates on; we use them in error messages. */
1526 static void
1527 gen_struct_ref (struct expression *exp, struct agent_expr *ax,
1528 struct axs_value *value, char *field,
1529 char *operator_name, char *operand_name)
1530 {
1531 struct type *type;
1532 int found;
1533
1534 /* Follow pointers until we reach a non-pointer. These aren't the C
1535 semantics, but they're what the normal GDB evaluator does, so we
1536 should at least be consistent. */
1537 while (pointer_type (value->type))
1538 {
1539 require_rvalue (ax, value);
1540 gen_deref (ax, value);
1541 }
1542 type = check_typedef (value->type);
1543
1544 /* This must yield a structure or a union. */
1545 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1546 && TYPE_CODE (type) != TYPE_CODE_UNION)
1547 error (_("The left operand of `%s' is not a %s."),
1548 operator_name, operand_name);
1549
1550 /* And it must be in memory; we don't deal with structure rvalues,
1551 or structures living in registers. */
1552 if (value->kind != axs_lvalue_memory)
1553 error (_("Structure does not live in memory."));
1554
1555 /* Search through fields and base classes recursively. */
1556 found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);
1557
1558 if (!found)
1559 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1560 field, TYPE_TAG_NAME (type));
1561 }
1562
1563 static int
1564 gen_namespace_elt (struct expression *exp,
1565 struct agent_expr *ax, struct axs_value *value,
1566 const struct type *curtype, char *name);
1567 static int
1568 gen_maybe_namespace_elt (struct expression *exp,
1569 struct agent_expr *ax, struct axs_value *value,
1570 const struct type *curtype, char *name);
1571
1572 static void
1573 gen_static_field (struct gdbarch *gdbarch,
1574 struct agent_expr *ax, struct axs_value *value,
1575 struct type *type, int fieldno)
1576 {
1577 if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
1578 {
1579 ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1580 value->kind = axs_lvalue_memory;
1581 value->type = TYPE_FIELD_TYPE (type, fieldno);
1582 value->optimized_out = 0;
1583 }
1584 else
1585 {
1586 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1587 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
1588
1589 if (sym)
1590 {
1591 gen_var_ref (gdbarch, ax, value, sym);
1592
1593 /* Don't error if the value was optimized out, we may be
1594 scanning all static fields and just want to pass over this
1595 and continue with the rest. */
1596 }
1597 else
1598 {
1599 /* Silently assume this was optimized out; class printing
1600 will let the user know why the data is missing. */
1601 value->optimized_out = 1;
1602 }
1603 }
1604 }
1605
1606 static int
1607 gen_struct_elt_for_reference (struct expression *exp,
1608 struct agent_expr *ax, struct axs_value *value,
1609 struct type *type, char *fieldname)
1610 {
1611 struct type *t = type;
1612 int i;
1613
1614 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1615 && TYPE_CODE (t) != TYPE_CODE_UNION)
1616 internal_error (__FILE__, __LINE__,
1617 _("non-aggregate type to gen_struct_elt_for_reference"));
1618
1619 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1620 {
1621 const char *t_field_name = TYPE_FIELD_NAME (t, i);
1622
1623 if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1624 {
1625 if (field_is_static (&TYPE_FIELD (t, i)))
1626 {
1627 gen_static_field (exp->gdbarch, ax, value, t, i);
1628 if (value->optimized_out)
1629 error (_("static field `%s' has been "
1630 "optimized out, cannot use"),
1631 fieldname);
1632 return 1;
1633 }
1634 if (TYPE_FIELD_PACKED (t, i))
1635 error (_("pointers to bitfield members not allowed"));
1636
1637 /* FIXME we need a way to do "want_address" equivalent */
1638
1639 error (_("Cannot reference non-static field \"%s\""), fieldname);
1640 }
1641 }
1642
1643 /* FIXME add other scoped-reference cases here */
1644
1645 /* Do a last-ditch lookup. */
1646 return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
1647 }
1648
1649 /* C++: Return the member NAME of the namespace given by the type
1650 CURTYPE. */
1651
1652 static int
1653 gen_namespace_elt (struct expression *exp,
1654 struct agent_expr *ax, struct axs_value *value,
1655 const struct type *curtype, char *name)
1656 {
1657 int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);
1658
1659 if (!found)
1660 error (_("No symbol \"%s\" in namespace \"%s\"."),
1661 name, TYPE_TAG_NAME (curtype));
1662
1663 return found;
1664 }
1665
1666 /* A helper function used by value_namespace_elt and
1667 value_struct_elt_for_reference. It looks up NAME inside the
1668 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1669 is a class and NAME refers to a type in CURTYPE itself (as opposed
1670 to, say, some base class of CURTYPE). */
1671
1672 static int
1673 gen_maybe_namespace_elt (struct expression *exp,
1674 struct agent_expr *ax, struct axs_value *value,
1675 const struct type *curtype, char *name)
1676 {
1677 const char *namespace_name = TYPE_TAG_NAME (curtype);
1678 struct symbol *sym;
1679
1680 sym = cp_lookup_symbol_namespace (namespace_name, name,
1681 block_for_pc (ax->scope),
1682 VAR_DOMAIN);
1683
1684 if (sym == NULL)
1685 return 0;
1686
1687 gen_var_ref (exp->gdbarch, ax, value, sym);
1688
1689 if (value->optimized_out)
1690 error (_("`%s' has been optimized out, cannot use"),
1691 SYMBOL_PRINT_NAME (sym));
1692
1693 return 1;
1694 }
1695
1696
1697 static int
1698 gen_aggregate_elt_ref (struct expression *exp,
1699 struct agent_expr *ax, struct axs_value *value,
1700 struct type *type, char *field,
1701 char *operator_name, char *operand_name)
1702 {
1703 switch (TYPE_CODE (type))
1704 {
1705 case TYPE_CODE_STRUCT:
1706 case TYPE_CODE_UNION:
1707 return gen_struct_elt_for_reference (exp, ax, value, type, field);
1708 break;
1709 case TYPE_CODE_NAMESPACE:
1710 return gen_namespace_elt (exp, ax, value, type, field);
1711 break;
1712 default:
1713 internal_error (__FILE__, __LINE__,
1714 _("non-aggregate type in gen_aggregate_elt_ref"));
1715 }
1716
1717 return 0;
1718 }
1719
1720 /* Generate code for GDB's magical `repeat' operator.
1721 LVALUE @ INT creates an array INT elements long, and whose elements
1722 have the same type as LVALUE, located in memory so that LVALUE is
1723 its first element. For example, argv[0]@argc gives you the array
1724 of command-line arguments.
1725
1726 Unfortunately, because we have to know the types before we actually
1727 have a value for the expression, we can't implement this perfectly
1728 without changing the type system, having values that occupy two
1729 stack slots, doing weird things with sizeof, etc. So we require
1730 the right operand to be a constant expression. */
1731 static void
1732 gen_repeat (struct expression *exp, union exp_element **pc,
1733 struct agent_expr *ax, struct axs_value *value)
1734 {
1735 struct axs_value value1;
1736
1737 /* We don't want to turn this into an rvalue, so no conversions
1738 here. */
1739 gen_expr (exp, pc, ax, &value1);
1740 if (value1.kind != axs_lvalue_memory)
1741 error (_("Left operand of `@' must be an object in memory."));
1742
1743 /* Evaluate the length; it had better be a constant. */
1744 {
1745 struct value *v = const_expr (pc);
1746 int length;
1747
1748 if (!v)
1749 error (_("Right operand of `@' must be a "
1750 "constant, in agent expressions."));
1751 if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1752 error (_("Right operand of `@' must be an integer."));
1753 length = value_as_long (v);
1754 if (length <= 0)
1755 error (_("Right operand of `@' must be positive."));
1756
1757 /* The top of the stack is already the address of the object, so
1758 all we need to do is frob the type of the lvalue. */
1759 {
1760 /* FIXME-type-allocation: need a way to free this type when we are
1761 done with it. */
1762 struct type *array
1763 = lookup_array_range_type (value1.type, 0, length - 1);
1764
1765 value->kind = axs_lvalue_memory;
1766 value->type = array;
1767 }
1768 }
1769 }
1770
1771
1772 /* Emit code for the `sizeof' operator.
1773 *PC should point at the start of the operand expression; we advance it
1774 to the first instruction after the operand. */
1775 static void
1776 gen_sizeof (struct expression *exp, union exp_element **pc,
1777 struct agent_expr *ax, struct axs_value *value,
1778 struct type *size_type)
1779 {
1780 /* We don't care about the value of the operand expression; we only
1781 care about its type. However, in the current arrangement, the
1782 only way to find an expression's type is to generate code for it.
1783 So we generate code for the operand, and then throw it away,
1784 replacing it with code that simply pushes its size. */
1785 int start = ax->len;
1786
1787 gen_expr (exp, pc, ax, value);
1788
1789 /* Throw away the code we just generated. */
1790 ax->len = start;
1791
1792 ax_const_l (ax, TYPE_LENGTH (value->type));
1793 value->kind = axs_rvalue;
1794 value->type = size_type;
1795 }
1796 \f
1797
1798 /* Generating bytecode from GDB expressions: general recursive thingy */
1799
1800 /* XXX: i18n */
1801 /* A gen_expr function written by a Gen-X'er guy.
1802 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1803 static void
1804 gen_expr (struct expression *exp, union exp_element **pc,
1805 struct agent_expr *ax, struct axs_value *value)
1806 {
1807 /* Used to hold the descriptions of operand expressions. */
1808 struct axs_value value1, value2, value3;
1809 enum exp_opcode op = (*pc)[0].opcode, op2;
1810 int if1, go1, if2, go2, end;
1811 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
1812
1813 /* If we're looking at a constant expression, just push its value. */
1814 {
1815 struct value *v = maybe_const_expr (pc);
1816
1817 if (v)
1818 {
1819 ax_const_l (ax, value_as_long (v));
1820 value->kind = axs_rvalue;
1821 value->type = check_typedef (value_type (v));
1822 return;
1823 }
1824 }
1825
1826 /* Otherwise, go ahead and generate code for it. */
1827 switch (op)
1828 {
1829 /* Binary arithmetic operators. */
1830 case BINOP_ADD:
1831 case BINOP_SUB:
1832 case BINOP_MUL:
1833 case BINOP_DIV:
1834 case BINOP_REM:
1835 case BINOP_LSH:
1836 case BINOP_RSH:
1837 case BINOP_SUBSCRIPT:
1838 case BINOP_BITWISE_AND:
1839 case BINOP_BITWISE_IOR:
1840 case BINOP_BITWISE_XOR:
1841 case BINOP_EQUAL:
1842 case BINOP_NOTEQUAL:
1843 case BINOP_LESS:
1844 case BINOP_GTR:
1845 case BINOP_LEQ:
1846 case BINOP_GEQ:
1847 (*pc)++;
1848 gen_expr (exp, pc, ax, &value1);
1849 gen_usual_unary (exp, ax, &value1);
1850 gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1851 break;
1852
1853 case BINOP_LOGICAL_AND:
1854 (*pc)++;
1855 /* Generate the obvious sequence of tests and jumps. */
1856 gen_expr (exp, pc, ax, &value1);
1857 gen_usual_unary (exp, ax, &value1);
1858 if1 = ax_goto (ax, aop_if_goto);
1859 go1 = ax_goto (ax, aop_goto);
1860 ax_label (ax, if1, ax->len);
1861 gen_expr (exp, pc, ax, &value2);
1862 gen_usual_unary (exp, ax, &value2);
1863 if2 = ax_goto (ax, aop_if_goto);
1864 go2 = ax_goto (ax, aop_goto);
1865 ax_label (ax, if2, ax->len);
1866 ax_const_l (ax, 1);
1867 end = ax_goto (ax, aop_goto);
1868 ax_label (ax, go1, ax->len);
1869 ax_label (ax, go2, ax->len);
1870 ax_const_l (ax, 0);
1871 ax_label (ax, end, ax->len);
1872 value->kind = axs_rvalue;
1873 value->type = int_type;
1874 break;
1875
1876 case BINOP_LOGICAL_OR:
1877 (*pc)++;
1878 /* Generate the obvious sequence of tests and jumps. */
1879 gen_expr (exp, pc, ax, &value1);
1880 gen_usual_unary (exp, ax, &value1);
1881 if1 = ax_goto (ax, aop_if_goto);
1882 gen_expr (exp, pc, ax, &value2);
1883 gen_usual_unary (exp, ax, &value2);
1884 if2 = ax_goto (ax, aop_if_goto);
1885 ax_const_l (ax, 0);
1886 end = ax_goto (ax, aop_goto);
1887 ax_label (ax, if1, ax->len);
1888 ax_label (ax, if2, ax->len);
1889 ax_const_l (ax, 1);
1890 ax_label (ax, end, ax->len);
1891 value->kind = axs_rvalue;
1892 value->type = int_type;
1893 break;
1894
1895 case TERNOP_COND:
1896 (*pc)++;
1897 gen_expr (exp, pc, ax, &value1);
1898 gen_usual_unary (exp, ax, &value1);
1899 /* For (A ? B : C), it's easiest to generate subexpression
1900 bytecodes in order, but if_goto jumps on true, so we invert
1901 the sense of A. Then we can do B by dropping through, and
1902 jump to do C. */
1903 gen_logical_not (ax, &value1, int_type);
1904 if1 = ax_goto (ax, aop_if_goto);
1905 gen_expr (exp, pc, ax, &value2);
1906 gen_usual_unary (exp, ax, &value2);
1907 end = ax_goto (ax, aop_goto);
1908 ax_label (ax, if1, ax->len);
1909 gen_expr (exp, pc, ax, &value3);
1910 gen_usual_unary (exp, ax, &value3);
1911 ax_label (ax, end, ax->len);
1912 /* This is arbitary - what if B and C are incompatible types? */
1913 value->type = value2.type;
1914 value->kind = value2.kind;
1915 break;
1916
1917 case BINOP_ASSIGN:
1918 (*pc)++;
1919 if ((*pc)[0].opcode == OP_INTERNALVAR)
1920 {
1921 char *name = internalvar_name ((*pc)[1].internalvar);
1922 struct trace_state_variable *tsv;
1923
1924 (*pc) += 3;
1925 gen_expr (exp, pc, ax, value);
1926 tsv = find_trace_state_variable (name);
1927 if (tsv)
1928 {
1929 ax_tsv (ax, aop_setv, tsv->number);
1930 if (trace_kludge)
1931 ax_tsv (ax, aop_tracev, tsv->number);
1932 }
1933 else
1934 error (_("$%s is not a trace state variable, "
1935 "may not assign to it"), name);
1936 }
1937 else
1938 error (_("May only assign to trace state variables"));
1939 break;
1940
1941 case BINOP_ASSIGN_MODIFY:
1942 (*pc)++;
1943 op2 = (*pc)[0].opcode;
1944 (*pc)++;
1945 (*pc)++;
1946 if ((*pc)[0].opcode == OP_INTERNALVAR)
1947 {
1948 char *name = internalvar_name ((*pc)[1].internalvar);
1949 struct trace_state_variable *tsv;
1950
1951 (*pc) += 3;
1952 tsv = find_trace_state_variable (name);
1953 if (tsv)
1954 {
1955 /* The tsv will be the left half of the binary operation. */
1956 ax_tsv (ax, aop_getv, tsv->number);
1957 if (trace_kludge)
1958 ax_tsv (ax, aop_tracev, tsv->number);
1959 /* Trace state variables are always 64-bit integers. */
1960 value1.kind = axs_rvalue;
1961 value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
1962 /* Now do right half of expression. */
1963 gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1964 /* We have a result of the binary op, set the tsv. */
1965 ax_tsv (ax, aop_setv, tsv->number);
1966 if (trace_kludge)
1967 ax_tsv (ax, aop_tracev, tsv->number);
1968 }
1969 else
1970 error (_("$%s is not a trace state variable, "
1971 "may not assign to it"), name);
1972 }
1973 else
1974 error (_("May only assign to trace state variables"));
1975 break;
1976
1977 /* Note that we need to be a little subtle about generating code
1978 for comma. In C, we can do some optimizations here because
1979 we know the left operand is only being evaluated for effect.
1980 However, if the tracing kludge is in effect, then we always
1981 need to evaluate the left hand side fully, so that all the
1982 variables it mentions get traced. */
1983 case BINOP_COMMA:
1984 (*pc)++;
1985 gen_expr (exp, pc, ax, &value1);
1986 /* Don't just dispose of the left operand. We might be tracing,
1987 in which case we want to emit code to trace it if it's an
1988 lvalue. */
1989 gen_traced_pop (exp->gdbarch, ax, &value1);
1990 gen_expr (exp, pc, ax, value);
1991 /* It's the consumer's responsibility to trace the right operand. */
1992 break;
1993
1994 case OP_LONG: /* some integer constant */
1995 {
1996 struct type *type = (*pc)[1].type;
1997 LONGEST k = (*pc)[2].longconst;
1998
1999 (*pc) += 4;
2000 gen_int_literal (ax, value, k, type);
2001 }
2002 break;
2003
2004 case OP_VAR_VALUE:
2005 gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);
2006
2007 if (value->optimized_out)
2008 error (_("`%s' has been optimized out, cannot use"),
2009 SYMBOL_PRINT_NAME ((*pc)[2].symbol));
2010
2011 (*pc) += 4;
2012 break;
2013
2014 case OP_REGISTER:
2015 {
2016 const char *name = &(*pc)[2].string;
2017 int reg;
2018
2019 (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
2020 reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
2021 if (reg == -1)
2022 internal_error (__FILE__, __LINE__,
2023 _("Register $%s not available"), name);
2024 /* No support for tracing user registers yet. */
2025 if (reg >= gdbarch_num_regs (exp->gdbarch)
2026 + gdbarch_num_pseudo_regs (exp->gdbarch))
2027 error (_("'%s' is a user-register; "
2028 "GDB cannot yet trace user-register contents."),
2029 name);
2030 value->kind = axs_lvalue_register;
2031 value->u.reg = reg;
2032 value->type = register_type (exp->gdbarch, reg);
2033 }
2034 break;
2035
2036 case OP_INTERNALVAR:
2037 {
2038 const char *name = internalvar_name ((*pc)[1].internalvar);
2039 struct trace_state_variable *tsv;
2040
2041 (*pc) += 3;
2042 tsv = find_trace_state_variable (name);
2043 if (tsv)
2044 {
2045 ax_tsv (ax, aop_getv, tsv->number);
2046 if (trace_kludge)
2047 ax_tsv (ax, aop_tracev, tsv->number);
2048 /* Trace state variables are always 64-bit integers. */
2049 value->kind = axs_rvalue;
2050 value->type = builtin_type (exp->gdbarch)->builtin_long_long;
2051 }
2052 else
2053 error (_("$%s is not a trace state variable; GDB agent "
2054 "expressions cannot use convenience variables."), name);
2055 }
2056 break;
2057
2058 /* Weirdo operator: see comments for gen_repeat for details. */
2059 case BINOP_REPEAT:
2060 /* Note that gen_repeat handles its own argument evaluation. */
2061 (*pc)++;
2062 gen_repeat (exp, pc, ax, value);
2063 break;
2064
2065 case UNOP_CAST:
2066 {
2067 struct type *type = (*pc)[1].type;
2068
2069 (*pc) += 3;
2070 gen_expr (exp, pc, ax, value);
2071 gen_cast (ax, value, type);
2072 }
2073 break;
2074
2075 case UNOP_MEMVAL:
2076 {
2077 struct type *type = check_typedef ((*pc)[1].type);
2078
2079 (*pc) += 3;
2080 gen_expr (exp, pc, ax, value);
2081
2082 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2083 already have the right value on the stack. For
2084 axs_lvalue_register, we must convert. */
2085 if (value->kind == axs_lvalue_register)
2086 require_rvalue (ax, value);
2087
2088 value->type = type;
2089 value->kind = axs_lvalue_memory;
2090 }
2091 break;
2092
2093 case UNOP_PLUS:
2094 (*pc)++;
2095 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2096 gen_expr (exp, pc, ax, value);
2097 gen_usual_unary (exp, ax, value);
2098 break;
2099
2100 case UNOP_NEG:
2101 (*pc)++;
2102 /* -FOO is equivalent to 0 - FOO. */
2103 gen_int_literal (ax, &value1, 0,
2104 builtin_type (exp->gdbarch)->builtin_int);
2105 gen_usual_unary (exp, ax, &value1); /* shouldn't do much */
2106 gen_expr (exp, pc, ax, &value2);
2107 gen_usual_unary (exp, ax, &value2);
2108 gen_usual_arithmetic (exp, ax, &value1, &value2);
2109 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2110 break;
2111
2112 case UNOP_LOGICAL_NOT:
2113 (*pc)++;
2114 gen_expr (exp, pc, ax, value);
2115 gen_usual_unary (exp, ax, value);
2116 gen_logical_not (ax, value, int_type);
2117 break;
2118
2119 case UNOP_COMPLEMENT:
2120 (*pc)++;
2121 gen_expr (exp, pc, ax, value);
2122 gen_usual_unary (exp, ax, value);
2123 gen_integral_promotions (exp, ax, value);
2124 gen_complement (ax, value);
2125 break;
2126
2127 case UNOP_IND:
2128 (*pc)++;
2129 gen_expr (exp, pc, ax, value);
2130 gen_usual_unary (exp, ax, value);
2131 if (!pointer_type (value->type))
2132 error (_("Argument of unary `*' is not a pointer."));
2133 gen_deref (ax, value);
2134 break;
2135
2136 case UNOP_ADDR:
2137 (*pc)++;
2138 gen_expr (exp, pc, ax, value);
2139 gen_address_of (ax, value);
2140 break;
2141
2142 case UNOP_SIZEOF:
2143 (*pc)++;
2144 /* Notice that gen_sizeof handles its own operand, unlike most
2145 of the other unary operator functions. This is because we
2146 have to throw away the code we generate. */
2147 gen_sizeof (exp, pc, ax, value,
2148 builtin_type (exp->gdbarch)->builtin_int);
2149 break;
2150
2151 case STRUCTOP_STRUCT:
2152 case STRUCTOP_PTR:
2153 {
2154 int length = (*pc)[1].longconst;
2155 char *name = &(*pc)[2].string;
2156
2157 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
2158 gen_expr (exp, pc, ax, value);
2159 if (op == STRUCTOP_STRUCT)
2160 gen_struct_ref (exp, ax, value, name, ".", "structure or union");
2161 else if (op == STRUCTOP_PTR)
2162 gen_struct_ref (exp, ax, value, name, "->",
2163 "pointer to a structure or union");
2164 else
2165 /* If this `if' chain doesn't handle it, then the case list
2166 shouldn't mention it, and we shouldn't be here. */
2167 internal_error (__FILE__, __LINE__,
2168 _("gen_expr: unhandled struct case"));
2169 }
2170 break;
2171
2172 case OP_THIS:
2173 {
2174 char *this_name;
2175 struct symbol *sym, *func;
2176 struct block *b;
2177 const struct language_defn *lang;
2178
2179 b = block_for_pc (ax->scope);
2180 func = block_linkage_function (b);
2181 lang = language_def (SYMBOL_LANGUAGE (func));
2182
2183 sym = lookup_language_this (lang, b);
2184 if (!sym)
2185 error (_("no `%s' found"), lang->la_name_of_this);
2186
2187 gen_var_ref (exp->gdbarch, ax, value, sym);
2188
2189 if (value->optimized_out)
2190 error (_("`%s' has been optimized out, cannot use"),
2191 SYMBOL_PRINT_NAME (sym));
2192
2193 (*pc) += 2;
2194 }
2195 break;
2196
2197 case OP_SCOPE:
2198 {
2199 struct type *type = (*pc)[1].type;
2200 int length = longest_to_int ((*pc)[2].longconst);
2201 char *name = &(*pc)[3].string;
2202 int found;
2203
2204 found = gen_aggregate_elt_ref (exp, ax, value, type, name,
2205 "?", "??");
2206 if (!found)
2207 error (_("There is no field named %s"), name);
2208 (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2209 }
2210 break;
2211
2212 case OP_TYPE:
2213 error (_("Attempt to use a type name as an expression."));
2214
2215 default:
2216 error (_("Unsupported operator %s (%d) in expression."),
2217 op_string (op), op);
2218 }
2219 }
2220
2221 /* This handles the middle-to-right-side of code generation for binary
2222 expressions, which is shared between regular binary operations and
2223 assign-modify (+= and friends) expressions. */
2224
2225 static void
2226 gen_expr_binop_rest (struct expression *exp,
2227 enum exp_opcode op, union exp_element **pc,
2228 struct agent_expr *ax, struct axs_value *value,
2229 struct axs_value *value1, struct axs_value *value2)
2230 {
2231 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
2232
2233 gen_expr (exp, pc, ax, value2);
2234 gen_usual_unary (exp, ax, value2);
2235 gen_usual_arithmetic (exp, ax, value1, value2);
2236 switch (op)
2237 {
2238 case BINOP_ADD:
2239 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
2240 && pointer_type (value2->type))
2241 {
2242 /* Swap the values and proceed normally. */
2243 ax_simple (ax, aop_swap);
2244 gen_ptradd (ax, value, value2, value1);
2245 }
2246 else if (pointer_type (value1->type)
2247 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2248 gen_ptradd (ax, value, value1, value2);
2249 else
2250 gen_binop (ax, value, value1, value2,
2251 aop_add, aop_add, 1, "addition");
2252 break;
2253 case BINOP_SUB:
2254 if (pointer_type (value1->type)
2255 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2256 gen_ptrsub (ax,value, value1, value2);
2257 else if (pointer_type (value1->type)
2258 && pointer_type (value2->type))
2259 /* FIXME --- result type should be ptrdiff_t */
2260 gen_ptrdiff (ax, value, value1, value2,
2261 builtin_type (exp->gdbarch)->builtin_long);
2262 else
2263 gen_binop (ax, value, value1, value2,
2264 aop_sub, aop_sub, 1, "subtraction");
2265 break;
2266 case BINOP_MUL:
2267 gen_binop (ax, value, value1, value2,
2268 aop_mul, aop_mul, 1, "multiplication");
2269 break;
2270 case BINOP_DIV:
2271 gen_binop (ax, value, value1, value2,
2272 aop_div_signed, aop_div_unsigned, 1, "division");
2273 break;
2274 case BINOP_REM:
2275 gen_binop (ax, value, value1, value2,
2276 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2277 break;
2278 case BINOP_LSH:
2279 gen_binop (ax, value, value1, value2,
2280 aop_lsh, aop_lsh, 1, "left shift");
2281 break;
2282 case BINOP_RSH:
2283 gen_binop (ax, value, value1, value2,
2284 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2285 break;
2286 case BINOP_SUBSCRIPT:
2287 {
2288 struct type *type;
2289
2290 if (binop_types_user_defined_p (op, value1->type, value2->type))
2291 {
2292 error (_("cannot subscript requested type: "
2293 "cannot call user defined functions"));
2294 }
2295 else
2296 {
2297 /* If the user attempts to subscript something that is not
2298 an array or pointer type (like a plain int variable for
2299 example), then report this as an error. */
2300 type = check_typedef (value1->type);
2301 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2302 && TYPE_CODE (type) != TYPE_CODE_PTR)
2303 {
2304 if (TYPE_NAME (type))
2305 error (_("cannot subscript something of type `%s'"),
2306 TYPE_NAME (type));
2307 else
2308 error (_("cannot subscript requested type"));
2309 }
2310 }
2311
2312 if (!is_integral_type (value2->type))
2313 error (_("Argument to arithmetic operation "
2314 "not a number or boolean."));
2315
2316 gen_ptradd (ax, value, value1, value2);
2317 gen_deref (ax, value);
2318 break;
2319 }
2320 case BINOP_BITWISE_AND:
2321 gen_binop (ax, value, value1, value2,
2322 aop_bit_and, aop_bit_and, 0, "bitwise and");
2323 break;
2324
2325 case BINOP_BITWISE_IOR:
2326 gen_binop (ax, value, value1, value2,
2327 aop_bit_or, aop_bit_or, 0, "bitwise or");
2328 break;
2329
2330 case BINOP_BITWISE_XOR:
2331 gen_binop (ax, value, value1, value2,
2332 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2333 break;
2334
2335 case BINOP_EQUAL:
2336 gen_equal (ax, value, value1, value2, int_type);
2337 break;
2338
2339 case BINOP_NOTEQUAL:
2340 gen_equal (ax, value, value1, value2, int_type);
2341 gen_logical_not (ax, value, int_type);
2342 break;
2343
2344 case BINOP_LESS:
2345 gen_less (ax, value, value1, value2, int_type);
2346 break;
2347
2348 case BINOP_GTR:
2349 ax_simple (ax, aop_swap);
2350 gen_less (ax, value, value1, value2, int_type);
2351 break;
2352
2353 case BINOP_LEQ:
2354 ax_simple (ax, aop_swap);
2355 gen_less (ax, value, value1, value2, int_type);
2356 gen_logical_not (ax, value, int_type);
2357 break;
2358
2359 case BINOP_GEQ:
2360 gen_less (ax, value, value1, value2, int_type);
2361 gen_logical_not (ax, value, int_type);
2362 break;
2363
2364 default:
2365 /* We should only list operators in the outer case statement
2366 that we actually handle in the inner case statement. */
2367 internal_error (__FILE__, __LINE__,
2368 _("gen_expr: op case sets don't match"));
2369 }
2370 }
2371 \f
2372
2373 /* Given a single variable and a scope, generate bytecodes to trace
2374 its value. This is for use in situations where we have only a
2375 variable's name, and no parsed expression; for instance, when the
2376 name comes from a list of local variables of a function. */
2377
2378 struct agent_expr *
2379 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2380 struct symbol *var)
2381 {
2382 struct cleanup *old_chain = 0;
2383 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2384 struct axs_value value;
2385
2386 old_chain = make_cleanup_free_agent_expr (ax);
2387
2388 trace_kludge = 1;
2389 gen_var_ref (gdbarch, ax, &value, var);
2390
2391 /* If there is no actual variable to trace, flag it by returning
2392 an empty agent expression. */
2393 if (value.optimized_out)
2394 {
2395 do_cleanups (old_chain);
2396 return NULL;
2397 }
2398
2399 /* Make sure we record the final object, and get rid of it. */
2400 gen_traced_pop (gdbarch, ax, &value);
2401
2402 /* Oh, and terminate. */
2403 ax_simple (ax, aop_end);
2404
2405 /* We have successfully built the agent expr, so cancel the cleanup
2406 request. If we add more cleanups that we always want done, this
2407 will have to get more complicated. */
2408 discard_cleanups (old_chain);
2409 return ax;
2410 }
2411
2412 /* Generating bytecode from GDB expressions: driver */
2413
2414 /* Given a GDB expression EXPR, return bytecode to trace its value.
2415 The result will use the `trace' and `trace_quick' bytecodes to
2416 record the value of all memory touched by the expression. The
2417 caller can then use the ax_reqs function to discover which
2418 registers it relies upon. */
2419 struct agent_expr *
2420 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
2421 {
2422 struct cleanup *old_chain = 0;
2423 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2424 union exp_element *pc;
2425 struct axs_value value;
2426
2427 old_chain = make_cleanup_free_agent_expr (ax);
2428
2429 pc = expr->elts;
2430 trace_kludge = 1;
2431 value.optimized_out = 0;
2432 gen_expr (expr, &pc, ax, &value);
2433
2434 /* Make sure we record the final object, and get rid of it. */
2435 gen_traced_pop (expr->gdbarch, ax, &value);
2436
2437 /* Oh, and terminate. */
2438 ax_simple (ax, aop_end);
2439
2440 /* We have successfully built the agent expr, so cancel the cleanup
2441 request. If we add more cleanups that we always want done, this
2442 will have to get more complicated. */
2443 discard_cleanups (old_chain);
2444 return ax;
2445 }
2446
2447 /* Given a GDB expression EXPR, return a bytecode sequence that will
2448 evaluate and return a result. The bytecodes will do a direct
2449 evaluation, using the current data on the target, rather than
2450 recording blocks of memory and registers for later use, as
2451 gen_trace_for_expr does. The generated bytecode sequence leaves
2452 the result of expression evaluation on the top of the stack. */
2453
2454 struct agent_expr *
2455 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2456 {
2457 struct cleanup *old_chain = 0;
2458 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2459 union exp_element *pc;
2460 struct axs_value value;
2461
2462 old_chain = make_cleanup_free_agent_expr (ax);
2463
2464 pc = expr->elts;
2465 trace_kludge = 0;
2466 value.optimized_out = 0;
2467 gen_expr (expr, &pc, ax, &value);
2468
2469 require_rvalue (ax, &value);
2470
2471 /* Oh, and terminate. */
2472 ax_simple (ax, aop_end);
2473
2474 /* We have successfully built the agent expr, so cancel the cleanup
2475 request. If we add more cleanups that we always want done, this
2476 will have to get more complicated. */
2477 discard_cleanups (old_chain);
2478 return ax;
2479 }
2480
2481 struct agent_expr *
2482 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch)
2483 {
2484 struct cleanup *old_chain = 0;
2485 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2486 struct axs_value value;
2487
2488 old_chain = make_cleanup_free_agent_expr (ax);
2489
2490 trace_kludge = 1;
2491
2492 gdbarch_gen_return_address (gdbarch, ax, &value, scope);
2493
2494 /* Make sure we record the final object, and get rid of it. */
2495 gen_traced_pop (gdbarch, ax, &value);
2496
2497 /* Oh, and terminate. */
2498 ax_simple (ax, aop_end);
2499
2500 /* We have successfully built the agent expr, so cancel the cleanup
2501 request. If we add more cleanups that we always want done, this
2502 will have to get more complicated. */
2503 discard_cleanups (old_chain);
2504 return ax;
2505 }
2506
2507 static void
2508 agent_command (char *exp, int from_tty)
2509 {
2510 struct cleanup *old_chain = 0;
2511 struct expression *expr;
2512 struct agent_expr *agent;
2513 struct frame_info *fi = get_current_frame (); /* need current scope */
2514
2515 /* We don't deal with overlay debugging at the moment. We need to
2516 think more carefully about this. If you copy this code into
2517 another command, change the error message; the user shouldn't
2518 have to know anything about agent expressions. */
2519 if (overlay_debugging)
2520 error (_("GDB can't do agent expression translation with overlays."));
2521
2522 if (exp == 0)
2523 error_no_arg (_("expression to translate"));
2524
2525 trace_string_kludge = 0;
2526 if (*exp == '/')
2527 exp = decode_agent_options (exp);
2528
2529 /* Recognize the return address collection directive specially. Note
2530 that it is not really an expression of any sort. */
2531 if (strcmp (exp, "$_ret") == 0)
2532 {
2533 agent = gen_trace_for_return_address (get_frame_pc (fi),
2534 get_current_arch ());
2535 old_chain = make_cleanup_free_agent_expr (agent);
2536 }
2537 else
2538 {
2539 expr = parse_expression (exp);
2540 old_chain = make_cleanup (free_current_contents, &expr);
2541 agent = gen_trace_for_expr (get_frame_pc (fi), expr);
2542 make_cleanup_free_agent_expr (agent);
2543 }
2544
2545 ax_reqs (agent);
2546 ax_print (gdb_stdout, agent);
2547
2548 /* It would be nice to call ax_reqs here to gather some general info
2549 about the expression, and then print out the result. */
2550
2551 do_cleanups (old_chain);
2552 dont_repeat ();
2553 }
2554
2555 /* Parse the given expression, compile it into an agent expression
2556 that does direct evaluation, and display the resulting
2557 expression. */
2558
2559 static void
2560 agent_eval_command (char *exp, int from_tty)
2561 {
2562 struct cleanup *old_chain = 0;
2563 struct expression *expr;
2564 struct agent_expr *agent;
2565 struct frame_info *fi = get_current_frame (); /* need current scope */
2566
2567 /* We don't deal with overlay debugging at the moment. We need to
2568 think more carefully about this. If you copy this code into
2569 another command, change the error message; the user shouldn't
2570 have to know anything about agent expressions. */
2571 if (overlay_debugging)
2572 error (_("GDB can't do agent expression translation with overlays."));
2573
2574 if (exp == 0)
2575 error_no_arg (_("expression to translate"));
2576
2577 expr = parse_expression (exp);
2578 old_chain = make_cleanup (free_current_contents, &expr);
2579 agent = gen_eval_for_expr (get_frame_pc (fi), expr);
2580 make_cleanup_free_agent_expr (agent);
2581 ax_reqs (agent);
2582 ax_print (gdb_stdout, agent);
2583
2584 /* It would be nice to call ax_reqs here to gather some general info
2585 about the expression, and then print out the result. */
2586
2587 do_cleanups (old_chain);
2588 dont_repeat ();
2589 }
2590 \f
2591
2592 /* Initialization code. */
2593
2594 void _initialize_ax_gdb (void);
2595 void
2596 _initialize_ax_gdb (void)
2597 {
2598 add_cmd ("agent", class_maintenance, agent_command,
2599 _("Translate an expression into "
2600 "remote agent bytecode for tracing."),
2601 &maintenancelist);
2602
2603 add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2604 _("Translate an expression into remote "
2605 "agent bytecode for evaluation."),
2606 &maintenancelist);
2607 }
This page took 0.121203 seconds and 4 git commands to generate.