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