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