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