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