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