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