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