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