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