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