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