bd813380fe26339515dedf384ae475264dc86e40
[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 /* If the value is an enum or a bool, call it an integer. */
882 case TYPE_CODE_ENUM:
883 case TYPE_CODE_BOOL:
884 value->type = builtin_type (exp->gdbarch)->builtin_int;
885 break;
886 }
887
888 /* If the value is an lvalue, dereference it. */
889 require_rvalue (ax, value);
890 }
891
892
893 /* Return non-zero iff the type TYPE1 is considered "wider" than the
894 type TYPE2, according to the rules described in gen_usual_arithmetic. */
895 static int
896 type_wider_than (struct type *type1, struct type *type2)
897 {
898 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
899 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
900 && TYPE_UNSIGNED (type1)
901 && !TYPE_UNSIGNED (type2)));
902 }
903
904
905 /* Return the "wider" of the two types TYPE1 and TYPE2. */
906 static struct type *
907 max_type (struct type *type1, struct type *type2)
908 {
909 return type_wider_than (type1, type2) ? type1 : type2;
910 }
911
912
913 /* Generate code to convert a scalar value of type FROM to type TO. */
914 static void
915 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
916 {
917 /* Perhaps there is a more graceful way to state these rules. */
918
919 /* If we're converting to a narrower type, then we need to clear out
920 the upper bits. */
921 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
922 gen_extend (ax, from);
923
924 /* If the two values have equal width, but different signednesses,
925 then we need to extend. */
926 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
927 {
928 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
929 gen_extend (ax, to);
930 }
931
932 /* If we're converting to a wider type, and becoming unsigned, then
933 we need to zero out any possible sign bits. */
934 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
935 {
936 if (TYPE_UNSIGNED (to))
937 gen_extend (ax, to);
938 }
939 }
940
941
942 /* Return non-zero iff the type FROM will require any bytecodes to be
943 emitted to be converted to the type TO. */
944 static int
945 is_nontrivial_conversion (struct type *from, struct type *to)
946 {
947 struct agent_expr *ax = new_agent_expr (NULL, 0);
948 int nontrivial;
949
950 /* Actually generate the code, and see if anything came out. At the
951 moment, it would be trivial to replicate the code in
952 gen_conversion here, but in the future, when we're supporting
953 floating point and the like, it may not be. Doing things this
954 way allows this function to be independent of the logic in
955 gen_conversion. */
956 gen_conversion (ax, from, to);
957 nontrivial = ax->len > 0;
958 free_agent_expr (ax);
959 return nontrivial;
960 }
961
962
963 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
964 6.2.1.5) for the two operands of an arithmetic operator. This
965 effectively finds a "least upper bound" type for the two arguments,
966 and promotes each argument to that type. *VALUE1 and *VALUE2
967 describe the values as they are passed in, and as they are left. */
968 static void
969 gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
970 struct axs_value *value1, struct axs_value *value2)
971 {
972 /* Do the usual binary conversions. */
973 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
974 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
975 {
976 /* The ANSI integral promotions seem to work this way: Order the
977 integer types by size, and then by signedness: an n-bit
978 unsigned type is considered "wider" than an n-bit signed
979 type. Promote to the "wider" of the two types, and always
980 promote at least to int. */
981 struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
982 max_type (value1->type, value2->type));
983
984 /* Deal with value2, on the top of the stack. */
985 gen_conversion (ax, value2->type, target);
986
987 /* Deal with value1, not on the top of the stack. Don't
988 generate the `swap' instructions if we're not actually going
989 to do anything. */
990 if (is_nontrivial_conversion (value1->type, target))
991 {
992 ax_simple (ax, aop_swap);
993 gen_conversion (ax, value1->type, target);
994 ax_simple (ax, aop_swap);
995 }
996
997 value1->type = value2->type = check_typedef (target);
998 }
999 }
1000
1001
1002 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
1003 the value on the top of the stack, as described by VALUE. Assume
1004 the value has integral type. */
1005 static void
1006 gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
1007 struct axs_value *value)
1008 {
1009 const struct builtin_type *builtin = builtin_type (exp->gdbarch);
1010
1011 if (!type_wider_than (value->type, builtin->builtin_int))
1012 {
1013 gen_conversion (ax, value->type, builtin->builtin_int);
1014 value->type = builtin->builtin_int;
1015 }
1016 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
1017 {
1018 gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
1019 value->type = builtin->builtin_unsigned_int;
1020 }
1021 }
1022
1023
1024 /* Generate code for a cast to TYPE. */
1025 static void
1026 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
1027 {
1028 /* GCC does allow casts to yield lvalues, so this should be fixed
1029 before merging these changes into the trunk. */
1030 require_rvalue (ax, value);
1031 /* Dereference typedefs. */
1032 type = check_typedef (type);
1033
1034 switch (TYPE_CODE (type))
1035 {
1036 case TYPE_CODE_PTR:
1037 case TYPE_CODE_REF:
1038 /* It's implementation-defined, and I'll bet this is what GCC
1039 does. */
1040 break;
1041
1042 case TYPE_CODE_ARRAY:
1043 case TYPE_CODE_STRUCT:
1044 case TYPE_CODE_UNION:
1045 case TYPE_CODE_FUNC:
1046 error (_("Invalid type cast: intended type must be scalar."));
1047
1048 case TYPE_CODE_ENUM:
1049 case TYPE_CODE_BOOL:
1050 /* We don't have to worry about the size of the value, because
1051 all our integral values are fully sign-extended, and when
1052 casting pointers we can do anything we like. Is there any
1053 way for us to know what GCC actually does with a cast like
1054 this? */
1055 break;
1056
1057 case TYPE_CODE_INT:
1058 gen_conversion (ax, value->type, type);
1059 break;
1060
1061 case TYPE_CODE_VOID:
1062 /* We could pop the value, and rely on everyone else to check
1063 the type and notice that this value doesn't occupy a stack
1064 slot. But for now, leave the value on the stack, and
1065 preserve the "value == stack element" assumption. */
1066 break;
1067
1068 default:
1069 error (_("Casts to requested type are not yet implemented."));
1070 }
1071
1072 value->type = type;
1073 }
1074 \f
1075
1076
1077 /* Generating bytecode from GDB expressions: arithmetic */
1078
1079 /* Scale the integer on the top of the stack by the size of the target
1080 of the pointer type TYPE. */
1081 static void
1082 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
1083 {
1084 struct type *element = TYPE_TARGET_TYPE (type);
1085
1086 if (TYPE_LENGTH (element) != 1)
1087 {
1088 ax_const_l (ax, TYPE_LENGTH (element));
1089 ax_simple (ax, op);
1090 }
1091 }
1092
1093
1094 /* Generate code for pointer arithmetic PTR + INT. */
1095 static void
1096 gen_ptradd (struct agent_expr *ax, struct axs_value *value,
1097 struct axs_value *value1, struct axs_value *value2)
1098 {
1099 gdb_assert (pointer_type (value1->type));
1100 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1101
1102 gen_scale (ax, aop_mul, value1->type);
1103 ax_simple (ax, aop_add);
1104 gen_extend (ax, value1->type); /* Catch overflow. */
1105 value->type = value1->type;
1106 value->kind = axs_rvalue;
1107 }
1108
1109
1110 /* Generate code for pointer arithmetic PTR - INT. */
1111 static void
1112 gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
1113 struct axs_value *value1, struct axs_value *value2)
1114 {
1115 gdb_assert (pointer_type (value1->type));
1116 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
1117
1118 gen_scale (ax, aop_mul, value1->type);
1119 ax_simple (ax, aop_sub);
1120 gen_extend (ax, value1->type); /* Catch overflow. */
1121 value->type = value1->type;
1122 value->kind = axs_rvalue;
1123 }
1124
1125
1126 /* Generate code for pointer arithmetic PTR - PTR. */
1127 static void
1128 gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
1129 struct axs_value *value1, struct axs_value *value2,
1130 struct type *result_type)
1131 {
1132 gdb_assert (pointer_type (value1->type));
1133 gdb_assert (pointer_type (value2->type));
1134
1135 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1136 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
1137 error (_("\
1138 First argument of `-' is a pointer, but second argument is neither\n\
1139 an integer nor a pointer of the same type."));
1140
1141 ax_simple (ax, aop_sub);
1142 gen_scale (ax, aop_div_unsigned, value1->type);
1143 value->type = result_type;
1144 value->kind = axs_rvalue;
1145 }
1146
1147 static void
1148 gen_equal (struct agent_expr *ax, struct axs_value *value,
1149 struct axs_value *value1, struct axs_value *value2,
1150 struct type *result_type)
1151 {
1152 if (pointer_type (value1->type) || pointer_type (value2->type))
1153 ax_simple (ax, aop_equal);
1154 else
1155 gen_binop (ax, value, value1, value2,
1156 aop_equal, aop_equal, 0, "equal");
1157 value->type = result_type;
1158 value->kind = axs_rvalue;
1159 }
1160
1161 static void
1162 gen_less (struct agent_expr *ax, struct axs_value *value,
1163 struct axs_value *value1, struct axs_value *value2,
1164 struct type *result_type)
1165 {
1166 if (pointer_type (value1->type) || pointer_type (value2->type))
1167 ax_simple (ax, aop_less_unsigned);
1168 else
1169 gen_binop (ax, value, value1, value2,
1170 aop_less_signed, aop_less_unsigned, 0, "less than");
1171 value->type = result_type;
1172 value->kind = axs_rvalue;
1173 }
1174
1175 /* Generate code for a binary operator that doesn't do pointer magic.
1176 We set VALUE to describe the result value; we assume VALUE1 and
1177 VALUE2 describe the two operands, and that they've undergone the
1178 usual binary conversions. MAY_CARRY should be non-zero iff the
1179 result needs to be extended. NAME is the English name of the
1180 operator, used in error messages */
1181 static void
1182 gen_binop (struct agent_expr *ax, struct axs_value *value,
1183 struct axs_value *value1, struct axs_value *value2,
1184 enum agent_op op, enum agent_op op_unsigned,
1185 int may_carry, char *name)
1186 {
1187 /* We only handle INT op INT. */
1188 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1189 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1190 error (_("Invalid combination of types in %s."), name);
1191
1192 ax_simple (ax,
1193 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1194 if (may_carry)
1195 gen_extend (ax, value1->type); /* catch overflow */
1196 value->type = value1->type;
1197 value->kind = axs_rvalue;
1198 }
1199
1200
1201 static void
1202 gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1203 struct type *result_type)
1204 {
1205 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1206 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1207 error (_("Invalid type of operand to `!'."));
1208
1209 ax_simple (ax, aop_log_not);
1210 value->type = result_type;
1211 }
1212
1213
1214 static void
1215 gen_complement (struct agent_expr *ax, struct axs_value *value)
1216 {
1217 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1218 error (_("Invalid type of operand to `~'."));
1219
1220 ax_simple (ax, aop_bit_not);
1221 gen_extend (ax, value->type);
1222 }
1223 \f
1224
1225
1226 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1227
1228 /* Dereference the value on the top of the stack. */
1229 static void
1230 gen_deref (struct agent_expr *ax, struct axs_value *value)
1231 {
1232 /* The caller should check the type, because several operators use
1233 this, and we don't know what error message to generate. */
1234 if (!pointer_type (value->type))
1235 internal_error (__FILE__, __LINE__,
1236 _("gen_deref: expected a pointer"));
1237
1238 /* We've got an rvalue now, which is a pointer. We want to yield an
1239 lvalue, whose address is exactly that pointer. So we don't
1240 actually emit any code; we just change the type from "Pointer to
1241 T" to "T", and mark the value as an lvalue in memory. Leave it
1242 to the consumer to actually dereference it. */
1243 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
1244 if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
1245 error (_("Attempt to dereference a generic pointer."));
1246 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1247 ? axs_rvalue : axs_lvalue_memory);
1248 }
1249
1250
1251 /* Produce the address of the lvalue on the top of the stack. */
1252 static void
1253 gen_address_of (struct agent_expr *ax, struct axs_value *value)
1254 {
1255 /* Special case for taking the address of a function. The ANSI
1256 standard describes this as a special case, too, so this
1257 arrangement is not without motivation. */
1258 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1259 /* The value's already an rvalue on the stack, so we just need to
1260 change the type. */
1261 value->type = lookup_pointer_type (value->type);
1262 else
1263 switch (value->kind)
1264 {
1265 case axs_rvalue:
1266 error (_("Operand of `&' is an rvalue, which has no address."));
1267
1268 case axs_lvalue_register:
1269 error (_("Operand of `&' is in a register, and has no address."));
1270
1271 case axs_lvalue_memory:
1272 value->kind = axs_rvalue;
1273 value->type = lookup_pointer_type (value->type);
1274 break;
1275 }
1276 }
1277
1278 /* Generate code to push the value of a bitfield of a structure whose
1279 address is on the top of the stack. START and END give the
1280 starting and one-past-ending *bit* numbers of the field within the
1281 structure. */
1282 static void
1283 gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
1284 struct axs_value *value, struct type *type,
1285 int start, int end)
1286 {
1287 /* Note that ops[i] fetches 8 << i bits. */
1288 static enum agent_op ops[]
1289 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1290 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1291
1292 /* We don't want to touch any byte that the bitfield doesn't
1293 actually occupy; we shouldn't make any accesses we're not
1294 explicitly permitted to. We rely here on the fact that the
1295 bytecode `ref' operators work on unaligned addresses.
1296
1297 It takes some fancy footwork to get the stack to work the way
1298 we'd like. Say we're retrieving a bitfield that requires three
1299 fetches. Initially, the stack just contains the address:
1300 addr
1301 For the first fetch, we duplicate the address
1302 addr addr
1303 then add the byte offset, do the fetch, and shift and mask as
1304 needed, yielding a fragment of the value, properly aligned for
1305 the final bitwise or:
1306 addr frag1
1307 then we swap, and repeat the process:
1308 frag1 addr --- address on top
1309 frag1 addr addr --- duplicate it
1310 frag1 addr frag2 --- get second fragment
1311 frag1 frag2 addr --- swap again
1312 frag1 frag2 frag3 --- get third fragment
1313 Notice that, since the third fragment is the last one, we don't
1314 bother duplicating the address this time. Now we have all the
1315 fragments on the stack, and we can simply `or' them together,
1316 yielding the final value of the bitfield. */
1317
1318 /* The first and one-after-last bits in the field, but rounded down
1319 and up to byte boundaries. */
1320 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1321 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1322 / TARGET_CHAR_BIT)
1323 * TARGET_CHAR_BIT);
1324
1325 /* current bit offset within the structure */
1326 int offset;
1327
1328 /* The index in ops of the opcode we're considering. */
1329 int op;
1330
1331 /* The number of fragments we generated in the process. Probably
1332 equal to the number of `one' bits in bytesize, but who cares? */
1333 int fragment_count;
1334
1335 /* Dereference any typedefs. */
1336 type = check_typedef (type);
1337
1338 /* Can we fetch the number of bits requested at all? */
1339 if ((end - start) > ((1 << num_ops) * 8))
1340 internal_error (__FILE__, __LINE__,
1341 _("gen_bitfield_ref: bitfield too wide"));
1342
1343 /* Note that we know here that we only need to try each opcode once.
1344 That may not be true on machines with weird byte sizes. */
1345 offset = bound_start;
1346 fragment_count = 0;
1347 for (op = num_ops - 1; op >= 0; op--)
1348 {
1349 /* number of bits that ops[op] would fetch */
1350 int op_size = 8 << op;
1351
1352 /* The stack at this point, from bottom to top, contains zero or
1353 more fragments, then the address. */
1354
1355 /* Does this fetch fit within the bitfield? */
1356 if (offset + op_size <= bound_end)
1357 {
1358 /* Is this the last fragment? */
1359 int last_frag = (offset + op_size == bound_end);
1360
1361 if (!last_frag)
1362 ax_simple (ax, aop_dup); /* keep a copy of the address */
1363
1364 /* Add the offset. */
1365 gen_offset (ax, offset / TARGET_CHAR_BIT);
1366
1367 if (trace_kludge)
1368 {
1369 /* Record the area of memory we're about to fetch. */
1370 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1371 }
1372
1373 /* Perform the fetch. */
1374 ax_simple (ax, ops[op]);
1375
1376 /* Shift the bits we have to their proper position.
1377 gen_left_shift will generate right shifts when the operand
1378 is negative.
1379
1380 A big-endian field diagram to ponder:
1381 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1382 +------++------++------++------++------++------++------++------+
1383 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1384 ^ ^ ^ ^
1385 bit number 16 32 48 53
1386 These are bit numbers as supplied by GDB. Note that the
1387 bit numbers run from right to left once you've fetched the
1388 value!
1389
1390 A little-endian field diagram to ponder:
1391 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1392 +------++------++------++------++------++------++------++------+
1393 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1394 ^ ^ ^ ^ ^
1395 bit number 48 32 16 4 0
1396
1397 In both cases, the most significant end is on the left
1398 (i.e. normal numeric writing order), which means that you
1399 don't go crazy thinking about `left' and `right' shifts.
1400
1401 We don't have to worry about masking yet:
1402 - If they contain garbage off the least significant end, then we
1403 must be looking at the low end of the field, and the right
1404 shift will wipe them out.
1405 - If they contain garbage off the most significant end, then we
1406 must be looking at the most significant end of the word, and
1407 the sign/zero extension will wipe them out.
1408 - If we're in the interior of the word, then there is no garbage
1409 on either end, because the ref operators zero-extend. */
1410 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
1411 gen_left_shift (ax, end - (offset + op_size));
1412 else
1413 gen_left_shift (ax, offset - start);
1414
1415 if (!last_frag)
1416 /* Bring the copy of the address up to the top. */
1417 ax_simple (ax, aop_swap);
1418
1419 offset += op_size;
1420 fragment_count++;
1421 }
1422 }
1423
1424 /* Generate enough bitwise `or' operations to combine all the
1425 fragments we left on the stack. */
1426 while (fragment_count-- > 1)
1427 ax_simple (ax, aop_bit_or);
1428
1429 /* Sign- or zero-extend the value as appropriate. */
1430 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1431
1432 /* This is *not* an lvalue. Ugh. */
1433 value->kind = axs_rvalue;
1434 value->type = type;
1435 }
1436
1437 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1438 is an accumulated offset (in bytes), will be nonzero for objects
1439 embedded in other objects, like C++ base classes. Behavior should
1440 generally follow value_primitive_field. */
1441
1442 static void
1443 gen_primitive_field (struct expression *exp,
1444 struct agent_expr *ax, struct axs_value *value,
1445 int offset, int fieldno, struct type *type)
1446 {
1447 /* Is this a bitfield? */
1448 if (TYPE_FIELD_PACKED (type, fieldno))
1449 gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
1450 (offset * TARGET_CHAR_BIT
1451 + TYPE_FIELD_BITPOS (type, fieldno)),
1452 (offset * TARGET_CHAR_BIT
1453 + TYPE_FIELD_BITPOS (type, fieldno)
1454 + TYPE_FIELD_BITSIZE (type, fieldno)));
1455 else
1456 {
1457 gen_offset (ax, offset
1458 + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1459 value->kind = axs_lvalue_memory;
1460 value->type = TYPE_FIELD_TYPE (type, fieldno);
1461 }
1462 }
1463
1464 /* Search for the given field in either the given type or one of its
1465 base classes. Return 1 if found, 0 if not. */
1466
1467 static int
1468 gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
1469 struct axs_value *value,
1470 char *field, int offset, struct type *type)
1471 {
1472 int i, rslt;
1473 int nbases = TYPE_N_BASECLASSES (type);
1474
1475 CHECK_TYPEDEF (type);
1476
1477 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1478 {
1479 const char *this_name = TYPE_FIELD_NAME (type, i);
1480
1481 if (this_name)
1482 {
1483 if (strcmp (field, this_name) == 0)
1484 {
1485 /* Note that bytecodes for the struct's base (aka
1486 "this") will have been generated already, which will
1487 be unnecessary but not harmful if the static field is
1488 being handled as a global. */
1489 if (field_is_static (&TYPE_FIELD (type, i)))
1490 {
1491 gen_static_field (exp->gdbarch, ax, value, type, i);
1492 if (value->optimized_out)
1493 error (_("static field `%s' has been "
1494 "optimized out, cannot use"),
1495 field);
1496 return 1;
1497 }
1498
1499 gen_primitive_field (exp, ax, value, offset, i, type);
1500 return 1;
1501 }
1502 #if 0 /* is this right? */
1503 if (this_name[0] == '\0')
1504 internal_error (__FILE__, __LINE__,
1505 _("find_field: anonymous unions not supported"));
1506 #endif
1507 }
1508 }
1509
1510 /* Now scan through base classes recursively. */
1511 for (i = 0; i < nbases; i++)
1512 {
1513 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1514
1515 rslt = gen_struct_ref_recursive (exp, ax, value, field,
1516 offset + TYPE_BASECLASS_BITPOS (type, i)
1517 / TARGET_CHAR_BIT,
1518 basetype);
1519 if (rslt)
1520 return 1;
1521 }
1522
1523 /* Not found anywhere, flag so caller can complain. */
1524 return 0;
1525 }
1526
1527 /* Generate code to reference the member named FIELD of a structure or
1528 union. The top of the stack, as described by VALUE, should have
1529 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1530 the operator being compiled, and OPERAND_NAME is the kind of thing
1531 it operates on; we use them in error messages. */
1532 static void
1533 gen_struct_ref (struct expression *exp, struct agent_expr *ax,
1534 struct axs_value *value, char *field,
1535 char *operator_name, char *operand_name)
1536 {
1537 struct type *type;
1538 int found;
1539
1540 /* Follow pointers until we reach a non-pointer. These aren't the C
1541 semantics, but they're what the normal GDB evaluator does, so we
1542 should at least be consistent. */
1543 while (pointer_type (value->type))
1544 {
1545 require_rvalue (ax, value);
1546 gen_deref (ax, value);
1547 }
1548 type = check_typedef (value->type);
1549
1550 /* This must yield a structure or a union. */
1551 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1552 && TYPE_CODE (type) != TYPE_CODE_UNION)
1553 error (_("The left operand of `%s' is not a %s."),
1554 operator_name, operand_name);
1555
1556 /* And it must be in memory; we don't deal with structure rvalues,
1557 or structures living in registers. */
1558 if (value->kind != axs_lvalue_memory)
1559 error (_("Structure does not live in memory."));
1560
1561 /* Search through fields and base classes recursively. */
1562 found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);
1563
1564 if (!found)
1565 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1566 field, TYPE_TAG_NAME (type));
1567 }
1568
1569 static int
1570 gen_namespace_elt (struct expression *exp,
1571 struct agent_expr *ax, struct axs_value *value,
1572 const struct type *curtype, char *name);
1573 static int
1574 gen_maybe_namespace_elt (struct expression *exp,
1575 struct agent_expr *ax, struct axs_value *value,
1576 const struct type *curtype, char *name);
1577
1578 static void
1579 gen_static_field (struct gdbarch *gdbarch,
1580 struct agent_expr *ax, struct axs_value *value,
1581 struct type *type, int fieldno)
1582 {
1583 if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
1584 {
1585 ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
1586 value->kind = axs_lvalue_memory;
1587 value->type = TYPE_FIELD_TYPE (type, fieldno);
1588 value->optimized_out = 0;
1589 }
1590 else
1591 {
1592 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
1593 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
1594
1595 if (sym)
1596 {
1597 gen_var_ref (gdbarch, ax, value, sym);
1598
1599 /* Don't error if the value was optimized out, we may be
1600 scanning all static fields and just want to pass over this
1601 and continue with the rest. */
1602 }
1603 else
1604 {
1605 /* Silently assume this was optimized out; class printing
1606 will let the user know why the data is missing. */
1607 value->optimized_out = 1;
1608 }
1609 }
1610 }
1611
1612 static int
1613 gen_struct_elt_for_reference (struct expression *exp,
1614 struct agent_expr *ax, struct axs_value *value,
1615 struct type *type, char *fieldname)
1616 {
1617 struct type *t = type;
1618 int i;
1619
1620 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1621 && TYPE_CODE (t) != TYPE_CODE_UNION)
1622 internal_error (__FILE__, __LINE__,
1623 _("non-aggregate type to gen_struct_elt_for_reference"));
1624
1625 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1626 {
1627 const char *t_field_name = TYPE_FIELD_NAME (t, i);
1628
1629 if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1630 {
1631 if (field_is_static (&TYPE_FIELD (t, i)))
1632 {
1633 gen_static_field (exp->gdbarch, ax, value, t, i);
1634 if (value->optimized_out)
1635 error (_("static field `%s' has been "
1636 "optimized out, cannot use"),
1637 fieldname);
1638 return 1;
1639 }
1640 if (TYPE_FIELD_PACKED (t, i))
1641 error (_("pointers to bitfield members not allowed"));
1642
1643 /* FIXME we need a way to do "want_address" equivalent */
1644
1645 error (_("Cannot reference non-static field \"%s\""), fieldname);
1646 }
1647 }
1648
1649 /* FIXME add other scoped-reference cases here */
1650
1651 /* Do a last-ditch lookup. */
1652 return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
1653 }
1654
1655 /* C++: Return the member NAME of the namespace given by the type
1656 CURTYPE. */
1657
1658 static int
1659 gen_namespace_elt (struct expression *exp,
1660 struct agent_expr *ax, struct axs_value *value,
1661 const struct type *curtype, char *name)
1662 {
1663 int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);
1664
1665 if (!found)
1666 error (_("No symbol \"%s\" in namespace \"%s\"."),
1667 name, TYPE_TAG_NAME (curtype));
1668
1669 return found;
1670 }
1671
1672 /* A helper function used by value_namespace_elt and
1673 value_struct_elt_for_reference. It looks up NAME inside the
1674 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1675 is a class and NAME refers to a type in CURTYPE itself (as opposed
1676 to, say, some base class of CURTYPE). */
1677
1678 static int
1679 gen_maybe_namespace_elt (struct expression *exp,
1680 struct agent_expr *ax, struct axs_value *value,
1681 const struct type *curtype, char *name)
1682 {
1683 const char *namespace_name = TYPE_TAG_NAME (curtype);
1684 struct symbol *sym;
1685
1686 sym = cp_lookup_symbol_namespace (namespace_name, name,
1687 block_for_pc (ax->scope),
1688 VAR_DOMAIN);
1689
1690 if (sym == NULL)
1691 return 0;
1692
1693 gen_var_ref (exp->gdbarch, ax, value, sym);
1694
1695 if (value->optimized_out)
1696 error (_("`%s' has been optimized out, cannot use"),
1697 SYMBOL_PRINT_NAME (sym));
1698
1699 return 1;
1700 }
1701
1702
1703 static int
1704 gen_aggregate_elt_ref (struct expression *exp,
1705 struct agent_expr *ax, struct axs_value *value,
1706 struct type *type, char *field,
1707 char *operator_name, char *operand_name)
1708 {
1709 switch (TYPE_CODE (type))
1710 {
1711 case TYPE_CODE_STRUCT:
1712 case TYPE_CODE_UNION:
1713 return gen_struct_elt_for_reference (exp, ax, value, type, field);
1714 break;
1715 case TYPE_CODE_NAMESPACE:
1716 return gen_namespace_elt (exp, ax, value, type, field);
1717 break;
1718 default:
1719 internal_error (__FILE__, __LINE__,
1720 _("non-aggregate type in gen_aggregate_elt_ref"));
1721 }
1722
1723 return 0;
1724 }
1725
1726 /* Generate code for GDB's magical `repeat' operator.
1727 LVALUE @ INT creates an array INT elements long, and whose elements
1728 have the same type as LVALUE, located in memory so that LVALUE is
1729 its first element. For example, argv[0]@argc gives you the array
1730 of command-line arguments.
1731
1732 Unfortunately, because we have to know the types before we actually
1733 have a value for the expression, we can't implement this perfectly
1734 without changing the type system, having values that occupy two
1735 stack slots, doing weird things with sizeof, etc. So we require
1736 the right operand to be a constant expression. */
1737 static void
1738 gen_repeat (struct expression *exp, union exp_element **pc,
1739 struct agent_expr *ax, struct axs_value *value)
1740 {
1741 struct axs_value value1;
1742
1743 /* We don't want to turn this into an rvalue, so no conversions
1744 here. */
1745 gen_expr (exp, pc, ax, &value1);
1746 if (value1.kind != axs_lvalue_memory)
1747 error (_("Left operand of `@' must be an object in memory."));
1748
1749 /* Evaluate the length; it had better be a constant. */
1750 {
1751 struct value *v = const_expr (pc);
1752 int length;
1753
1754 if (!v)
1755 error (_("Right operand of `@' must be a "
1756 "constant, in agent expressions."));
1757 if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1758 error (_("Right operand of `@' must be an integer."));
1759 length = value_as_long (v);
1760 if (length <= 0)
1761 error (_("Right operand of `@' must be positive."));
1762
1763 /* The top of the stack is already the address of the object, so
1764 all we need to do is frob the type of the lvalue. */
1765 {
1766 /* FIXME-type-allocation: need a way to free this type when we are
1767 done with it. */
1768 struct type *array
1769 = lookup_array_range_type (value1.type, 0, length - 1);
1770
1771 value->kind = axs_lvalue_memory;
1772 value->type = array;
1773 }
1774 }
1775 }
1776
1777
1778 /* Emit code for the `sizeof' operator.
1779 *PC should point at the start of the operand expression; we advance it
1780 to the first instruction after the operand. */
1781 static void
1782 gen_sizeof (struct expression *exp, union exp_element **pc,
1783 struct agent_expr *ax, struct axs_value *value,
1784 struct type *size_type)
1785 {
1786 /* We don't care about the value of the operand expression; we only
1787 care about its type. However, in the current arrangement, the
1788 only way to find an expression's type is to generate code for it.
1789 So we generate code for the operand, and then throw it away,
1790 replacing it with code that simply pushes its size. */
1791 int start = ax->len;
1792
1793 gen_expr (exp, pc, ax, value);
1794
1795 /* Throw away the code we just generated. */
1796 ax->len = start;
1797
1798 ax_const_l (ax, TYPE_LENGTH (value->type));
1799 value->kind = axs_rvalue;
1800 value->type = size_type;
1801 }
1802 \f
1803
1804 /* Generating bytecode from GDB expressions: general recursive thingy */
1805
1806 /* XXX: i18n */
1807 /* A gen_expr function written by a Gen-X'er guy.
1808 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1809 static void
1810 gen_expr (struct expression *exp, union exp_element **pc,
1811 struct agent_expr *ax, struct axs_value *value)
1812 {
1813 /* Used to hold the descriptions of operand expressions. */
1814 struct axs_value value1, value2, value3;
1815 enum exp_opcode op = (*pc)[0].opcode, op2;
1816 int if1, go1, if2, go2, end;
1817 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
1818
1819 /* If we're looking at a constant expression, just push its value. */
1820 {
1821 struct value *v = maybe_const_expr (pc);
1822
1823 if (v)
1824 {
1825 ax_const_l (ax, value_as_long (v));
1826 value->kind = axs_rvalue;
1827 value->type = check_typedef (value_type (v));
1828 return;
1829 }
1830 }
1831
1832 /* Otherwise, go ahead and generate code for it. */
1833 switch (op)
1834 {
1835 /* Binary arithmetic operators. */
1836 case BINOP_ADD:
1837 case BINOP_SUB:
1838 case BINOP_MUL:
1839 case BINOP_DIV:
1840 case BINOP_REM:
1841 case BINOP_LSH:
1842 case BINOP_RSH:
1843 case BINOP_SUBSCRIPT:
1844 case BINOP_BITWISE_AND:
1845 case BINOP_BITWISE_IOR:
1846 case BINOP_BITWISE_XOR:
1847 case BINOP_EQUAL:
1848 case BINOP_NOTEQUAL:
1849 case BINOP_LESS:
1850 case BINOP_GTR:
1851 case BINOP_LEQ:
1852 case BINOP_GEQ:
1853 (*pc)++;
1854 gen_expr (exp, pc, ax, &value1);
1855 gen_usual_unary (exp, ax, &value1);
1856 gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1857 break;
1858
1859 case BINOP_LOGICAL_AND:
1860 (*pc)++;
1861 /* Generate the obvious sequence of tests and jumps. */
1862 gen_expr (exp, pc, ax, &value1);
1863 gen_usual_unary (exp, ax, &value1);
1864 if1 = ax_goto (ax, aop_if_goto);
1865 go1 = ax_goto (ax, aop_goto);
1866 ax_label (ax, if1, ax->len);
1867 gen_expr (exp, pc, ax, &value2);
1868 gen_usual_unary (exp, ax, &value2);
1869 if2 = ax_goto (ax, aop_if_goto);
1870 go2 = ax_goto (ax, aop_goto);
1871 ax_label (ax, if2, ax->len);
1872 ax_const_l (ax, 1);
1873 end = ax_goto (ax, aop_goto);
1874 ax_label (ax, go1, ax->len);
1875 ax_label (ax, go2, ax->len);
1876 ax_const_l (ax, 0);
1877 ax_label (ax, end, ax->len);
1878 value->kind = axs_rvalue;
1879 value->type = int_type;
1880 break;
1881
1882 case BINOP_LOGICAL_OR:
1883 (*pc)++;
1884 /* Generate the obvious sequence of tests and jumps. */
1885 gen_expr (exp, pc, ax, &value1);
1886 gen_usual_unary (exp, ax, &value1);
1887 if1 = ax_goto (ax, aop_if_goto);
1888 gen_expr (exp, pc, ax, &value2);
1889 gen_usual_unary (exp, ax, &value2);
1890 if2 = ax_goto (ax, aop_if_goto);
1891 ax_const_l (ax, 0);
1892 end = ax_goto (ax, aop_goto);
1893 ax_label (ax, if1, ax->len);
1894 ax_label (ax, if2, ax->len);
1895 ax_const_l (ax, 1);
1896 ax_label (ax, end, ax->len);
1897 value->kind = axs_rvalue;
1898 value->type = int_type;
1899 break;
1900
1901 case TERNOP_COND:
1902 (*pc)++;
1903 gen_expr (exp, pc, ax, &value1);
1904 gen_usual_unary (exp, ax, &value1);
1905 /* For (A ? B : C), it's easiest to generate subexpression
1906 bytecodes in order, but if_goto jumps on true, so we invert
1907 the sense of A. Then we can do B by dropping through, and
1908 jump to do C. */
1909 gen_logical_not (ax, &value1, int_type);
1910 if1 = ax_goto (ax, aop_if_goto);
1911 gen_expr (exp, pc, ax, &value2);
1912 gen_usual_unary (exp, ax, &value2);
1913 end = ax_goto (ax, aop_goto);
1914 ax_label (ax, if1, ax->len);
1915 gen_expr (exp, pc, ax, &value3);
1916 gen_usual_unary (exp, ax, &value3);
1917 ax_label (ax, end, ax->len);
1918 /* This is arbitary - what if B and C are incompatible types? */
1919 value->type = value2.type;
1920 value->kind = value2.kind;
1921 break;
1922
1923 case BINOP_ASSIGN:
1924 (*pc)++;
1925 if ((*pc)[0].opcode == OP_INTERNALVAR)
1926 {
1927 char *name = internalvar_name ((*pc)[1].internalvar);
1928 struct trace_state_variable *tsv;
1929
1930 (*pc) += 3;
1931 gen_expr (exp, pc, ax, value);
1932 tsv = find_trace_state_variable (name);
1933 if (tsv)
1934 {
1935 ax_tsv (ax, aop_setv, tsv->number);
1936 if (trace_kludge)
1937 ax_tsv (ax, aop_tracev, tsv->number);
1938 }
1939 else
1940 error (_("$%s is not a trace state variable, "
1941 "may not assign to it"), name);
1942 }
1943 else
1944 error (_("May only assign to trace state variables"));
1945 break;
1946
1947 case BINOP_ASSIGN_MODIFY:
1948 (*pc)++;
1949 op2 = (*pc)[0].opcode;
1950 (*pc)++;
1951 (*pc)++;
1952 if ((*pc)[0].opcode == OP_INTERNALVAR)
1953 {
1954 char *name = internalvar_name ((*pc)[1].internalvar);
1955 struct trace_state_variable *tsv;
1956
1957 (*pc) += 3;
1958 tsv = find_trace_state_variable (name);
1959 if (tsv)
1960 {
1961 /* The tsv will be the left half of the binary operation. */
1962 ax_tsv (ax, aop_getv, tsv->number);
1963 if (trace_kludge)
1964 ax_tsv (ax, aop_tracev, tsv->number);
1965 /* Trace state variables are always 64-bit integers. */
1966 value1.kind = axs_rvalue;
1967 value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
1968 /* Now do right half of expression. */
1969 gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1970 /* We have a result of the binary op, set the tsv. */
1971 ax_tsv (ax, aop_setv, tsv->number);
1972 if (trace_kludge)
1973 ax_tsv (ax, aop_tracev, tsv->number);
1974 }
1975 else
1976 error (_("$%s is not a trace state variable, "
1977 "may not assign to it"), name);
1978 }
1979 else
1980 error (_("May only assign to trace state variables"));
1981 break;
1982
1983 /* Note that we need to be a little subtle about generating code
1984 for comma. In C, we can do some optimizations here because
1985 we know the left operand is only being evaluated for effect.
1986 However, if the tracing kludge is in effect, then we always
1987 need to evaluate the left hand side fully, so that all the
1988 variables it mentions get traced. */
1989 case BINOP_COMMA:
1990 (*pc)++;
1991 gen_expr (exp, pc, ax, &value1);
1992 /* Don't just dispose of the left operand. We might be tracing,
1993 in which case we want to emit code to trace it if it's an
1994 lvalue. */
1995 gen_traced_pop (exp->gdbarch, ax, &value1);
1996 gen_expr (exp, pc, ax, value);
1997 /* It's the consumer's responsibility to trace the right operand. */
1998 break;
1999
2000 case OP_LONG: /* some integer constant */
2001 {
2002 struct type *type = (*pc)[1].type;
2003 LONGEST k = (*pc)[2].longconst;
2004
2005 (*pc) += 4;
2006 gen_int_literal (ax, value, k, type);
2007 }
2008 break;
2009
2010 case OP_VAR_VALUE:
2011 gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);
2012
2013 if (value->optimized_out)
2014 error (_("`%s' has been optimized out, cannot use"),
2015 SYMBOL_PRINT_NAME ((*pc)[2].symbol));
2016
2017 (*pc) += 4;
2018 break;
2019
2020 case OP_REGISTER:
2021 {
2022 const char *name = &(*pc)[2].string;
2023 int reg;
2024
2025 (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
2026 reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
2027 if (reg == -1)
2028 internal_error (__FILE__, __LINE__,
2029 _("Register $%s not available"), name);
2030 /* No support for tracing user registers yet. */
2031 if (reg >= gdbarch_num_regs (exp->gdbarch)
2032 + gdbarch_num_pseudo_regs (exp->gdbarch))
2033 error (_("'%s' is a user-register; "
2034 "GDB cannot yet trace user-register contents."),
2035 name);
2036 value->kind = axs_lvalue_register;
2037 value->u.reg = reg;
2038 value->type = register_type (exp->gdbarch, reg);
2039 }
2040 break;
2041
2042 case OP_INTERNALVAR:
2043 {
2044 const char *name = internalvar_name ((*pc)[1].internalvar);
2045 struct trace_state_variable *tsv;
2046
2047 (*pc) += 3;
2048 tsv = find_trace_state_variable (name);
2049 if (tsv)
2050 {
2051 ax_tsv (ax, aop_getv, tsv->number);
2052 if (trace_kludge)
2053 ax_tsv (ax, aop_tracev, tsv->number);
2054 /* Trace state variables are always 64-bit integers. */
2055 value->kind = axs_rvalue;
2056 value->type = builtin_type (exp->gdbarch)->builtin_long_long;
2057 }
2058 else
2059 error (_("$%s is not a trace state variable; GDB agent "
2060 "expressions cannot use convenience variables."), name);
2061 }
2062 break;
2063
2064 /* Weirdo operator: see comments for gen_repeat for details. */
2065 case BINOP_REPEAT:
2066 /* Note that gen_repeat handles its own argument evaluation. */
2067 (*pc)++;
2068 gen_repeat (exp, pc, ax, value);
2069 break;
2070
2071 case UNOP_CAST:
2072 {
2073 struct type *type = (*pc)[1].type;
2074
2075 (*pc) += 3;
2076 gen_expr (exp, pc, ax, value);
2077 gen_cast (ax, value, type);
2078 }
2079 break;
2080
2081 case UNOP_MEMVAL:
2082 {
2083 struct type *type = check_typedef ((*pc)[1].type);
2084
2085 (*pc) += 3;
2086 gen_expr (exp, pc, ax, value);
2087
2088 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2089 already have the right value on the stack. For
2090 axs_lvalue_register, we must convert. */
2091 if (value->kind == axs_lvalue_register)
2092 require_rvalue (ax, value);
2093
2094 value->type = type;
2095 value->kind = axs_lvalue_memory;
2096 }
2097 break;
2098
2099 case UNOP_PLUS:
2100 (*pc)++;
2101 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2102 gen_expr (exp, pc, ax, value);
2103 gen_usual_unary (exp, ax, value);
2104 break;
2105
2106 case UNOP_NEG:
2107 (*pc)++;
2108 /* -FOO is equivalent to 0 - FOO. */
2109 gen_int_literal (ax, &value1, 0,
2110 builtin_type (exp->gdbarch)->builtin_int);
2111 gen_usual_unary (exp, ax, &value1); /* shouldn't do much */
2112 gen_expr (exp, pc, ax, &value2);
2113 gen_usual_unary (exp, ax, &value2);
2114 gen_usual_arithmetic (exp, ax, &value1, &value2);
2115 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2116 break;
2117
2118 case UNOP_LOGICAL_NOT:
2119 (*pc)++;
2120 gen_expr (exp, pc, ax, value);
2121 gen_usual_unary (exp, ax, value);
2122 gen_logical_not (ax, value, int_type);
2123 break;
2124
2125 case UNOP_COMPLEMENT:
2126 (*pc)++;
2127 gen_expr (exp, pc, ax, value);
2128 gen_usual_unary (exp, ax, value);
2129 gen_integral_promotions (exp, ax, value);
2130 gen_complement (ax, value);
2131 break;
2132
2133 case UNOP_IND:
2134 (*pc)++;
2135 gen_expr (exp, pc, ax, value);
2136 gen_usual_unary (exp, ax, value);
2137 if (!pointer_type (value->type))
2138 error (_("Argument of unary `*' is not a pointer."));
2139 gen_deref (ax, value);
2140 break;
2141
2142 case UNOP_ADDR:
2143 (*pc)++;
2144 gen_expr (exp, pc, ax, value);
2145 gen_address_of (ax, value);
2146 break;
2147
2148 case UNOP_SIZEOF:
2149 (*pc)++;
2150 /* Notice that gen_sizeof handles its own operand, unlike most
2151 of the other unary operator functions. This is because we
2152 have to throw away the code we generate. */
2153 gen_sizeof (exp, pc, ax, value,
2154 builtin_type (exp->gdbarch)->builtin_int);
2155 break;
2156
2157 case STRUCTOP_STRUCT:
2158 case STRUCTOP_PTR:
2159 {
2160 int length = (*pc)[1].longconst;
2161 char *name = &(*pc)[2].string;
2162
2163 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
2164 gen_expr (exp, pc, ax, value);
2165 if (op == STRUCTOP_STRUCT)
2166 gen_struct_ref (exp, ax, value, name, ".", "structure or union");
2167 else if (op == STRUCTOP_PTR)
2168 gen_struct_ref (exp, ax, value, name, "->",
2169 "pointer to a structure or union");
2170 else
2171 /* If this `if' chain doesn't handle it, then the case list
2172 shouldn't mention it, and we shouldn't be here. */
2173 internal_error (__FILE__, __LINE__,
2174 _("gen_expr: unhandled struct case"));
2175 }
2176 break;
2177
2178 case OP_THIS:
2179 {
2180 char *this_name;
2181 struct symbol *sym, *func;
2182 struct block *b;
2183 const struct language_defn *lang;
2184
2185 b = block_for_pc (ax->scope);
2186 func = block_linkage_function (b);
2187 lang = language_def (SYMBOL_LANGUAGE (func));
2188
2189 sym = lookup_language_this (lang, b);
2190 if (!sym)
2191 error (_("no `%s' found"), lang->la_name_of_this);
2192
2193 gen_var_ref (exp->gdbarch, ax, value, sym);
2194
2195 if (value->optimized_out)
2196 error (_("`%s' has been optimized out, cannot use"),
2197 SYMBOL_PRINT_NAME (sym));
2198
2199 (*pc) += 2;
2200 }
2201 break;
2202
2203 case OP_SCOPE:
2204 {
2205 struct type *type = (*pc)[1].type;
2206 int length = longest_to_int ((*pc)[2].longconst);
2207 char *name = &(*pc)[3].string;
2208 int found;
2209
2210 found = gen_aggregate_elt_ref (exp, ax, value, type, name,
2211 "?", "??");
2212 if (!found)
2213 error (_("There is no field named %s"), name);
2214 (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2215 }
2216 break;
2217
2218 case OP_TYPE:
2219 error (_("Attempt to use a type name as an expression."));
2220
2221 default:
2222 error (_("Unsupported operator %s (%d) in expression."),
2223 op_string (op), op);
2224 }
2225 }
2226
2227 /* This handles the middle-to-right-side of code generation for binary
2228 expressions, which is shared between regular binary operations and
2229 assign-modify (+= and friends) expressions. */
2230
2231 static void
2232 gen_expr_binop_rest (struct expression *exp,
2233 enum exp_opcode op, union exp_element **pc,
2234 struct agent_expr *ax, struct axs_value *value,
2235 struct axs_value *value1, struct axs_value *value2)
2236 {
2237 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
2238
2239 gen_expr (exp, pc, ax, value2);
2240 gen_usual_unary (exp, ax, value2);
2241 gen_usual_arithmetic (exp, ax, value1, value2);
2242 switch (op)
2243 {
2244 case BINOP_ADD:
2245 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
2246 && pointer_type (value2->type))
2247 {
2248 /* Swap the values and proceed normally. */
2249 ax_simple (ax, aop_swap);
2250 gen_ptradd (ax, value, value2, value1);
2251 }
2252 else if (pointer_type (value1->type)
2253 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2254 gen_ptradd (ax, value, value1, value2);
2255 else
2256 gen_binop (ax, value, value1, value2,
2257 aop_add, aop_add, 1, "addition");
2258 break;
2259 case BINOP_SUB:
2260 if (pointer_type (value1->type)
2261 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2262 gen_ptrsub (ax,value, value1, value2);
2263 else if (pointer_type (value1->type)
2264 && pointer_type (value2->type))
2265 /* FIXME --- result type should be ptrdiff_t */
2266 gen_ptrdiff (ax, value, value1, value2,
2267 builtin_type (exp->gdbarch)->builtin_long);
2268 else
2269 gen_binop (ax, value, value1, value2,
2270 aop_sub, aop_sub, 1, "subtraction");
2271 break;
2272 case BINOP_MUL:
2273 gen_binop (ax, value, value1, value2,
2274 aop_mul, aop_mul, 1, "multiplication");
2275 break;
2276 case BINOP_DIV:
2277 gen_binop (ax, value, value1, value2,
2278 aop_div_signed, aop_div_unsigned, 1, "division");
2279 break;
2280 case BINOP_REM:
2281 gen_binop (ax, value, value1, value2,
2282 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2283 break;
2284 case BINOP_LSH:
2285 gen_binop (ax, value, value1, value2,
2286 aop_lsh, aop_lsh, 1, "left shift");
2287 break;
2288 case BINOP_RSH:
2289 gen_binop (ax, value, value1, value2,
2290 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2291 break;
2292 case BINOP_SUBSCRIPT:
2293 {
2294 struct type *type;
2295
2296 if (binop_types_user_defined_p (op, value1->type, value2->type))
2297 {
2298 error (_("cannot subscript requested type: "
2299 "cannot call user defined functions"));
2300 }
2301 else
2302 {
2303 /* If the user attempts to subscript something that is not
2304 an array or pointer type (like a plain int variable for
2305 example), then report this as an error. */
2306 type = check_typedef (value1->type);
2307 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2308 && TYPE_CODE (type) != TYPE_CODE_PTR)
2309 {
2310 if (TYPE_NAME (type))
2311 error (_("cannot subscript something of type `%s'"),
2312 TYPE_NAME (type));
2313 else
2314 error (_("cannot subscript requested type"));
2315 }
2316 }
2317
2318 if (!is_integral_type (value2->type))
2319 error (_("Argument to arithmetic operation "
2320 "not a number or boolean."));
2321
2322 gen_ptradd (ax, value, value1, value2);
2323 gen_deref (ax, value);
2324 break;
2325 }
2326 case BINOP_BITWISE_AND:
2327 gen_binop (ax, value, value1, value2,
2328 aop_bit_and, aop_bit_and, 0, "bitwise and");
2329 break;
2330
2331 case BINOP_BITWISE_IOR:
2332 gen_binop (ax, value, value1, value2,
2333 aop_bit_or, aop_bit_or, 0, "bitwise or");
2334 break;
2335
2336 case BINOP_BITWISE_XOR:
2337 gen_binop (ax, value, value1, value2,
2338 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2339 break;
2340
2341 case BINOP_EQUAL:
2342 gen_equal (ax, value, value1, value2, int_type);
2343 break;
2344
2345 case BINOP_NOTEQUAL:
2346 gen_equal (ax, value, value1, value2, int_type);
2347 gen_logical_not (ax, value, int_type);
2348 break;
2349
2350 case BINOP_LESS:
2351 gen_less (ax, value, value1, value2, int_type);
2352 break;
2353
2354 case BINOP_GTR:
2355 ax_simple (ax, aop_swap);
2356 gen_less (ax, value, value1, value2, int_type);
2357 break;
2358
2359 case BINOP_LEQ:
2360 ax_simple (ax, aop_swap);
2361 gen_less (ax, value, value1, value2, int_type);
2362 gen_logical_not (ax, value, int_type);
2363 break;
2364
2365 case BINOP_GEQ:
2366 gen_less (ax, value, value1, value2, int_type);
2367 gen_logical_not (ax, value, int_type);
2368 break;
2369
2370 default:
2371 /* We should only list operators in the outer case statement
2372 that we actually handle in the inner case statement. */
2373 internal_error (__FILE__, __LINE__,
2374 _("gen_expr: op case sets don't match"));
2375 }
2376 }
2377 \f
2378
2379 /* Given a single variable and a scope, generate bytecodes to trace
2380 its value. This is for use in situations where we have only a
2381 variable's name, and no parsed expression; for instance, when the
2382 name comes from a list of local variables of a function. */
2383
2384 struct agent_expr *
2385 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2386 struct symbol *var)
2387 {
2388 struct cleanup *old_chain = 0;
2389 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2390 struct axs_value value;
2391
2392 old_chain = make_cleanup_free_agent_expr (ax);
2393
2394 trace_kludge = 1;
2395 gen_var_ref (gdbarch, ax, &value, var);
2396
2397 /* If there is no actual variable to trace, flag it by returning
2398 an empty agent expression. */
2399 if (value.optimized_out)
2400 {
2401 do_cleanups (old_chain);
2402 return NULL;
2403 }
2404
2405 /* Make sure we record the final object, and get rid of it. */
2406 gen_traced_pop (gdbarch, ax, &value);
2407
2408 /* Oh, and terminate. */
2409 ax_simple (ax, aop_end);
2410
2411 /* We have successfully built the agent expr, so cancel the cleanup
2412 request. If we add more cleanups that we always want done, this
2413 will have to get more complicated. */
2414 discard_cleanups (old_chain);
2415 return ax;
2416 }
2417
2418 /* Generating bytecode from GDB expressions: driver */
2419
2420 /* Given a GDB expression EXPR, return bytecode to trace its value.
2421 The result will use the `trace' and `trace_quick' bytecodes to
2422 record the value of all memory touched by the expression. The
2423 caller can then use the ax_reqs function to discover which
2424 registers it relies upon. */
2425 struct agent_expr *
2426 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
2427 {
2428 struct cleanup *old_chain = 0;
2429 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2430 union exp_element *pc;
2431 struct axs_value value;
2432
2433 old_chain = make_cleanup_free_agent_expr (ax);
2434
2435 pc = expr->elts;
2436 trace_kludge = 1;
2437 value.optimized_out = 0;
2438 gen_expr (expr, &pc, ax, &value);
2439
2440 /* Make sure we record the final object, and get rid of it. */
2441 gen_traced_pop (expr->gdbarch, ax, &value);
2442
2443 /* Oh, and terminate. */
2444 ax_simple (ax, aop_end);
2445
2446 /* We have successfully built the agent expr, so cancel the cleanup
2447 request. If we add more cleanups that we always want done, this
2448 will have to get more complicated. */
2449 discard_cleanups (old_chain);
2450 return ax;
2451 }
2452
2453 /* Given a GDB expression EXPR, return a bytecode sequence that will
2454 evaluate and return a result. The bytecodes will do a direct
2455 evaluation, using the current data on the target, rather than
2456 recording blocks of memory and registers for later use, as
2457 gen_trace_for_expr does. The generated bytecode sequence leaves
2458 the result of expression evaluation on the top of the stack. */
2459
2460 struct agent_expr *
2461 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2462 {
2463 struct cleanup *old_chain = 0;
2464 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
2465 union exp_element *pc;
2466 struct axs_value value;
2467
2468 old_chain = make_cleanup_free_agent_expr (ax);
2469
2470 pc = expr->elts;
2471 trace_kludge = 0;
2472 value.optimized_out = 0;
2473 gen_expr (expr, &pc, ax, &value);
2474
2475 require_rvalue (ax, &value);
2476
2477 /* Oh, and terminate. */
2478 ax_simple (ax, aop_end);
2479
2480 /* We have successfully built the agent expr, so cancel the cleanup
2481 request. If we add more cleanups that we always want done, this
2482 will have to get more complicated. */
2483 discard_cleanups (old_chain);
2484 return ax;
2485 }
2486
2487 struct agent_expr *
2488 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch)
2489 {
2490 struct cleanup *old_chain = 0;
2491 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2492 struct axs_value value;
2493
2494 old_chain = make_cleanup_free_agent_expr (ax);
2495
2496 trace_kludge = 1;
2497
2498 gdbarch_gen_return_address (gdbarch, ax, &value, scope);
2499
2500 /* Make sure we record the final object, and get rid of it. */
2501 gen_traced_pop (gdbarch, ax, &value);
2502
2503 /* Oh, and terminate. */
2504 ax_simple (ax, aop_end);
2505
2506 /* We have successfully built the agent expr, so cancel the cleanup
2507 request. If we add more cleanups that we always want done, this
2508 will have to get more complicated. */
2509 discard_cleanups (old_chain);
2510 return ax;
2511 }
2512
2513 static void
2514 agent_command (char *exp, int from_tty)
2515 {
2516 struct cleanup *old_chain = 0;
2517 struct expression *expr;
2518 struct agent_expr *agent;
2519 struct frame_info *fi = get_current_frame (); /* need current scope */
2520
2521 /* We don't deal with overlay debugging at the moment. We need to
2522 think more carefully about this. If you copy this code into
2523 another command, change the error message; the user shouldn't
2524 have to know anything about agent expressions. */
2525 if (overlay_debugging)
2526 error (_("GDB can't do agent expression translation with overlays."));
2527
2528 if (exp == 0)
2529 error_no_arg (_("expression to translate"));
2530
2531 trace_string_kludge = 0;
2532 if (*exp == '/')
2533 exp = decode_agent_options (exp);
2534
2535 /* Recognize the return address collection directive specially. Note
2536 that it is not really an expression of any sort. */
2537 if (strcmp (exp, "$_ret") == 0)
2538 {
2539 agent = gen_trace_for_return_address (get_frame_pc (fi),
2540 get_current_arch ());
2541 old_chain = make_cleanup_free_agent_expr (agent);
2542 }
2543 else
2544 {
2545 expr = parse_expression (exp);
2546 old_chain = make_cleanup (free_current_contents, &expr);
2547 agent = gen_trace_for_expr (get_frame_pc (fi), expr);
2548 make_cleanup_free_agent_expr (agent);
2549 }
2550
2551 ax_reqs (agent);
2552 ax_print (gdb_stdout, agent);
2553
2554 /* It would be nice to call ax_reqs here to gather some general info
2555 about the expression, and then print out the result. */
2556
2557 do_cleanups (old_chain);
2558 dont_repeat ();
2559 }
2560
2561 /* Parse the given expression, compile it into an agent expression
2562 that does direct evaluation, and display the resulting
2563 expression. */
2564
2565 static void
2566 agent_eval_command (char *exp, int from_tty)
2567 {
2568 struct cleanup *old_chain = 0;
2569 struct expression *expr;
2570 struct agent_expr *agent;
2571 struct frame_info *fi = get_current_frame (); /* need current scope */
2572
2573 /* We don't deal with overlay debugging at the moment. We need to
2574 think more carefully about this. If you copy this code into
2575 another command, change the error message; the user shouldn't
2576 have to know anything about agent expressions. */
2577 if (overlay_debugging)
2578 error (_("GDB can't do agent expression translation with overlays."));
2579
2580 if (exp == 0)
2581 error_no_arg (_("expression to translate"));
2582
2583 expr = parse_expression (exp);
2584 old_chain = make_cleanup (free_current_contents, &expr);
2585 agent = gen_eval_for_expr (get_frame_pc (fi), expr);
2586 make_cleanup_free_agent_expr (agent);
2587 ax_reqs (agent);
2588 ax_print (gdb_stdout, agent);
2589
2590 /* It would be nice to call ax_reqs here to gather some general info
2591 about the expression, and then print out the result. */
2592
2593 do_cleanups (old_chain);
2594 dont_repeat ();
2595 }
2596 \f
2597
2598 /* Initialization code. */
2599
2600 void _initialize_ax_gdb (void);
2601 void
2602 _initialize_ax_gdb (void)
2603 {
2604 add_cmd ("agent", class_maintenance, agent_command,
2605 _("Translate an expression into "
2606 "remote agent bytecode for tracing."),
2607 &maintenancelist);
2608
2609 add_cmd ("agent-eval", class_maintenance, agent_eval_command,
2610 _("Translate an expression into remote "
2611 "agent bytecode for evaluation."),
2612 &maintenancelist);
2613 }
This page took 0.085699 seconds and 4 git commands to generate.