* gdb.ada/Makefile.in (EXECUTABLES): Bring up to date.
[deliverable/binutils-gdb.git] / gdb / ax-gdb.c
1 /* GDB-specific functions for operating on agent expressions.
2
3 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2007, 2008
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
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"
33 #include "gdb_string.h"
34 #include "block.h"
35 #include "regcache.h"
36 #include "user-regs.h"
37
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.
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
52 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
53 \f
54
55
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 */
62
63 static struct value *const_var_ref (struct symbol *var);
64 static struct value *const_expr (union exp_element **pc);
65 static struct value *maybe_const_expr (union exp_element **pc);
66
67 static void gen_traced_pop (struct agent_expr *, struct axs_value *);
68
69 static void gen_sign_extend (struct agent_expr *, struct type *);
70 static void gen_extend (struct agent_expr *, struct type *);
71 static void gen_fetch (struct agent_expr *, struct type *);
72 static void gen_left_shift (struct agent_expr *, int);
73
74
75 static void gen_frame_args_address (struct agent_expr *);
76 static void gen_frame_locals_address (struct agent_expr *);
77 static void gen_offset (struct agent_expr *ax, int offset);
78 static void gen_sym_offset (struct agent_expr *, struct symbol *);
79 static void gen_var_ref (struct agent_expr *ax,
80 struct axs_value *value, struct symbol *var);
81
82
83 static void gen_int_literal (struct agent_expr *ax,
84 struct axs_value *value,
85 LONGEST k, struct type *type);
86
87
88 static void require_rvalue (struct agent_expr *ax, struct axs_value *value);
89 static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value);
90 static int type_wider_than (struct type *type1, struct type *type2);
91 static struct type *max_type (struct type *type1, struct type *type2);
92 static void gen_conversion (struct agent_expr *ax,
93 struct type *from, struct type *to);
94 static int is_nontrivial_conversion (struct type *from, struct type *to);
95 static void gen_usual_arithmetic (struct agent_expr *ax,
96 struct axs_value *value1,
97 struct axs_value *value2);
98 static void gen_integral_promotions (struct agent_expr *ax,
99 struct axs_value *value);
100 static void gen_cast (struct agent_expr *ax,
101 struct axs_value *value, struct type *type);
102 static void gen_scale (struct agent_expr *ax,
103 enum agent_op op, struct type *type);
104 static void gen_add (struct agent_expr *ax,
105 struct axs_value *value,
106 struct axs_value *value1,
107 struct axs_value *value2, char *name);
108 static void gen_sub (struct agent_expr *ax,
109 struct axs_value *value,
110 struct axs_value *value1, struct axs_value *value2);
111 static 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);
117 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value);
118 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
119 static void gen_deref (struct agent_expr *, struct axs_value *);
120 static void gen_address_of (struct agent_expr *, struct axs_value *);
121 static int find_field (struct type *type, char *name);
122 static void gen_bitfield_ref (struct agent_expr *ax,
123 struct axs_value *value,
124 struct type *type, int start, int end);
125 static void gen_struct_ref (struct agent_expr *ax,
126 struct axs_value *value,
127 char *field,
128 char *operator_name, char *operand_name);
129 static void gen_repeat (union exp_element **pc,
130 struct agent_expr *ax, struct axs_value *value);
131 static void gen_sizeof (union exp_element **pc,
132 struct agent_expr *ax, struct axs_value *value);
133 static void gen_expr (union exp_element **pc,
134 struct agent_expr *ax, struct axs_value *value);
135
136 static void agent_command (char *exp, int from_tty);
137 \f
138
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
159 static struct value *
160 const_var_ref (struct symbol *var)
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:
170 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
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. */
182 static struct value *
183 const_expr (union exp_element **pc)
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
205 /* We could add more operators in here. */
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. */
223 static struct value *
224 maybe_const_expr (union exp_element **pc)
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;
232
233 return v;
234 }
235 \f
236
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,
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.
249
250 - We assume the stack elements are infinitely wide, and that we
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!)
256
257 - We don't support side effects. Thus, we don't have to worry about
258 GCC's generalized lvalues, function calls, etc.
259
260 - We don't support floating point. Many places where we switch on
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.
264
265 - We assume all subexpressions have a static, unchanging type. If
266 we tried to support convenience variables, this would be a
267 problem.
268
269 - All values on the stack should always be fully zero- or
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.) */
279 \f
280
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. */
304 static 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. */
309 static void
310 gen_traced_pop (struct agent_expr *ax, struct axs_value *value)
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
317 produce them. So just dispose of this value. */
318 ax_simple (ax, aop_pop);
319 break;
320
321 case axs_lvalue_memory:
322 {
323 int length = TYPE_LENGTH (check_typedef (value->type));
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 }
333 break;
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 }
347 \f
348
349
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. */
354 static void
355 gen_sign_extend (struct agent_expr *ax, struct type *type)
356 {
357 /* Do we need to sign-extend this? */
358 if (!TYPE_UNSIGNED (type))
359 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
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. */
366 static void
367 gen_extend (struct agent_expr *ax, struct type *type)
368 {
369 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
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. */
378 static void
379 gen_fetch (struct agent_expr *ax, struct type *type)
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
387 switch (TYPE_CODE (type))
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? */
395 switch (TYPE_LENGTH (type))
396 {
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;
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:
415 internal_error (__FILE__, __LINE__,
416 _("gen_fetch: strange size"));
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
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. */
427 internal_error (__FILE__, __LINE__,
428 _("gen_fetch: bad type code"));
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. */
436 static void
437 gen_left_shift (struct agent_expr *ax, int distance)
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 }
450 \f
451
452
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. */
457 static void
458 gen_frame_args_address (struct agent_expr *ax)
459 {
460 int frame_reg;
461 LONGEST frame_offset;
462
463 gdbarch_virtual_frame_pointer (current_gdbarch,
464 ax->scope, &frame_reg, &frame_offset);
465 ax_reg (ax, frame_reg);
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. */
472 static void
473 gen_frame_locals_address (struct agent_expr *ax)
474 {
475 int frame_reg;
476 LONGEST frame_offset;
477
478 gdbarch_virtual_frame_pointer (current_gdbarch,
479 ax->scope, &frame_reg, &frame_offset);
480 ax_reg (ax, frame_reg);
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. */
490 static void
491 gen_offset (struct agent_expr *ax, int offset)
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. */
512 static void
513 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
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
522 static void
523 gen_var_ref (struct agent_expr *ax, struct axs_value *value, struct symbol *var)
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:
542 internal_error (__FILE__, __LINE__,
543 _("gen_var_ref: LOC_CONST_BYTES symbols are not supported"));
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 */
568 gen_frame_locals_address (ax);
569 gen_sym_offset (ax, var);
570 value->kind = axs_lvalue_memory;
571 break;
572
573 case LOC_TYPEDEF:
574 error (_("Cannot compute value of typedef `%s'."),
575 SYMBOL_PRINT_NAME (var));
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:
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
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. */
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 {
602 struct minimal_symbol *msym
603 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
604 if (!msym)
605 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
606
607 /* Push the address of the variable. */
608 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
609 value->kind = axs_lvalue_memory;
610 }
611 break;
612
613 case LOC_COMPUTED:
614 /* FIXME: cagney/2004-01-26: It should be possible to
615 unconditionally call the SYMBOL_OPS method when available.
616 Unfortunately DWARF 2 stores the frame-base (instead of the
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);
620 break;
621
622 case LOC_OPTIMIZED_OUT:
623 error (_("The variable `%s' has been optimized out."),
624 SYMBOL_PRINT_NAME (var));
625 break;
626
627 default:
628 error (_("Cannot find value of botched symbol `%s'."),
629 SYMBOL_PRINT_NAME (var));
630 break;
631 }
632 }
633 \f
634
635
636 /* Generating bytecode from GDB expressions: literals */
637
638 static void
639 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
640 struct type *type)
641 {
642 ax_const_l (ax, k);
643 value->kind = axs_rvalue;
644 value->type = check_typedef (type);
645 }
646 \f
647
648
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. */
654 static void
655 require_rvalue (struct agent_expr *ax, struct axs_value *value)
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
672 When we add floating-point support, this is going to have to
673 change. What about SPARC register pairs, for example? */
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
699 static void
700 gen_usual_unary (struct agent_expr *ax, struct axs_value *value)
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. */
707 switch (TYPE_CODE (value->type))
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
716 are no longer an lvalue. */
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 }
725 break;
726
727 /* Don't try to convert structures and unions to rvalues. Let the
728 consumer signal an error. */
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. */
746 static int
747 type_wider_than (struct type *type1, struct type *type2)
748 {
749 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
750 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
751 && TYPE_UNSIGNED (type1)
752 && !TYPE_UNSIGNED (type2)));
753 }
754
755
756 /* Return the "wider" of the two types TYPE1 and TYPE2. */
757 static struct type *
758 max_type (struct type *type1, struct type *type2)
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. */
765 static void
766 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
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. */
795 static int
796 is_nontrivial_conversion (struct type *from, struct type *to)
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. */
819 static void
820 gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1,
821 struct axs_value *value2)
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
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. */
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
848 value1->type = value2->type = check_typedef (target);
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. */
856 static void
857 gen_integral_promotions (struct agent_expr *ax, struct axs_value *value)
858 {
859 if (!type_wider_than (value->type, builtin_type_int))
860 {
861 gen_conversion (ax, value->type, builtin_type_int);
862 value->type = builtin_type_int;
863 }
864 else if (!type_wider_than (value->type, builtin_type_unsigned_int))
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. */
873 static void
874 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
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
882 switch (TYPE_CODE (type))
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:
893 error (_("Invalid type cast: intended type must be scalar."));
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
899 way for us to know what GCC actually does with a cast like
900 this? */
901 break;
902
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
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. */
912 break;
913
914 default:
915 error (_("Casts to requested type are not yet implemented."));
916 }
917
918 value->type = type;
919 }
920 \f
921
922
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. */
927 static void
928 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
929 {
930 struct type *element = TYPE_TARGET_TYPE (type);
931
932 if (TYPE_LENGTH (element) != 1)
933 {
934 ax_const_l (ax, TYPE_LENGTH (element));
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. */
945 static void
946 gen_add (struct agent_expr *ax, struct axs_value *value,
947 struct axs_value *value1, struct axs_value *value2, char *name)
948 {
949 /* Is it INT+PTR? */
950 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
951 && TYPE_CODE (value2->type) == TYPE_CODE_PTR)
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);
957 gen_extend (ax, value2->type); /* Catch overflow. */
958 value->type = value2->type;
959 }
960
961 /* Is it PTR+INT? */
962 else if (TYPE_CODE (value1->type) == TYPE_CODE_PTR
963 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
964 {
965 gen_scale (ax, aop_mul, value1->type);
966 ax_simple (ax, aop_add);
967 gen_extend (ax, value1->type); /* Catch overflow. */
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. */
973 else if (TYPE_CODE (value1->type) == TYPE_CODE_INT
974 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
975 {
976 ax_simple (ax, aop_add);
977 gen_extend (ax, value1->type); /* Catch overflow. */
978 value->type = value1->type;
979 }
980
981 else
982 error (_("Invalid combination of types in %s."), name);
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. */
992 static void
993 gen_sub (struct agent_expr *ax, struct axs_value *value,
994 struct axs_value *value1, struct axs_value *value2)
995 {
996 if (TYPE_CODE (value1->type) == TYPE_CODE_PTR)
997 {
998 /* Is it PTR - INT? */
999 if (TYPE_CODE (value2->type) == TYPE_CODE_INT)
1000 {
1001 gen_scale (ax, aop_mul, value1->type);
1002 ax_simple (ax, aop_sub);
1003 gen_extend (ax, value1->type); /* Catch overflow. */
1004 value->type = value1->type;
1005 }
1006
1007 /* Is it PTR - PTR? Strictly speaking, the types ought to
1008 match, but this is what the normal GDB expression evaluator
1009 tests for. */
1010 else if (TYPE_CODE (value2->type) == TYPE_CODE_PTR
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);
1016 value->type = builtin_type_long; /* FIXME --- should be ptrdiff_t */
1017 }
1018 else
1019 error (_("\
1020 First argument of `-' is a pointer, but second argument is neither\n\
1021 an integer nor a pointer of the same type."));
1022 }
1023
1024 /* Must be number + number. */
1025 else if (TYPE_CODE (value1->type) == TYPE_CODE_INT
1026 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
1027 {
1028 ax_simple (ax, aop_sub);
1029 gen_extend (ax, value1->type); /* Catch overflow. */
1030 value->type = value1->type;
1031 }
1032
1033 else
1034 error (_("Invalid combination of types in subtraction."));
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 */
1045 static void
1046 gen_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)
1049 {
1050 /* We only handle INT op INT. */
1051 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1052 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
1053 error (_("Invalid combination of types in %s."), name);
1054
1055 ax_simple (ax,
1056 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1057 if (may_carry)
1058 gen_extend (ax, value1->type); /* catch overflow */
1059 value->type = value1->type;
1060 value->kind = axs_rvalue;
1061 }
1062
1063
1064 static void
1065 gen_logical_not (struct agent_expr *ax, struct axs_value *value)
1066 {
1067 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1068 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1069 error (_("Invalid type of operand to `!'."));
1070
1071 gen_usual_unary (ax, value);
1072 ax_simple (ax, aop_log_not);
1073 value->type = builtin_type_int;
1074 }
1075
1076
1077 static void
1078 gen_complement (struct agent_expr *ax, struct axs_value *value)
1079 {
1080 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1081 error (_("Invalid type of operand to `~'."));
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 }
1088 \f
1089
1090
1091 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1092
1093 /* Dereference the value on the top of the stack. */
1094 static void
1095 gen_deref (struct agent_expr *ax, struct axs_value *value)
1096 {
1097 /* The caller should check the type, because several operators use
1098 this, and we don't know what error message to generate. */
1099 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1100 internal_error (__FILE__, __LINE__,
1101 _("gen_deref: expected a pointer"));
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));
1109 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1110 ? axs_rvalue : axs_lvalue_memory);
1111 }
1112
1113
1114 /* Produce the address of the lvalue on the top of the stack. */
1115 static void
1116 gen_address_of (struct agent_expr *ax, struct axs_value *value)
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. */
1121 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
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:
1129 error (_("Operand of `&' is an rvalue, which has no address."));
1130
1131 case axs_lvalue_register:
1132 error (_("Operand of `&' is in a register, and has no address."));
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. */
1147 static int
1148 find_field (struct type *type, char *name)
1149 {
1150 int i;
1151
1152 CHECK_TYPEDEF (type);
1153
1154 /* Make sure this isn't C++. */
1155 if (TYPE_N_BASECLASSES (type) != 0)
1156 internal_error (__FILE__, __LINE__,
1157 _("find_field: derived classes supported"));
1158
1159 for (i = 0; i < TYPE_NFIELDS (type); i++)
1160 {
1161 char *this_name = TYPE_FIELD_NAME (type, i);
1162
1163 if (this_name)
1164 {
1165 if (strcmp (name, this_name) == 0)
1166 return i;
1167
1168 if (this_name[0] == '\0')
1169 internal_error (__FILE__, __LINE__,
1170 _("find_field: anonymous unions not supported"));
1171 }
1172 }
1173
1174 error (_("Couldn't find member named `%s' in struct/union `%s'"),
1175 name, TYPE_TAG_NAME (type));
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. */
1185 static void
1186 gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
1187 struct type *type, int start, int end)
1188 {
1189 /* Note that ops[i] fetches 8 << i bits. */
1190 static enum agent_op ops[]
1191 =
1192 {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
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:
1203 addr
1204 For the first fetch, we duplicate the address
1205 addr addr
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:
1209 addr frag1
1210 then we swap, and repeat the process:
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
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;
1224 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1225 / TARGET_CHAR_BIT)
1226 * TARGET_CHAR_BIT);
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))
1243 internal_error (__FILE__, __LINE__,
1244 _("gen_bitfield_ref: bitfield too wide"));
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
1256 more fragments, then the address. */
1257
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
1264 if (!last_frag)
1265 ax_simple (ax, aop_dup); /* keep a copy of the address */
1266
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]);
1278
1279 /* Shift the bits we have to their proper position.
1280 gen_left_shift will generate right shifts when the operand
1281 is negative.
1282
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
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
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. */
1313 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
1314 gen_left_shift (ax, end - (offset + op_size));
1315 else
1316 gen_left_shift (ax, offset - start);
1317
1318 if (!last_frag)
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. */
1346 static void
1347 gen_struct_ref (struct agent_expr *ax, struct axs_value *value, char *field,
1348 char *operator_name, char *operand_name)
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. */
1356 while (TYPE_CODE (value->type) == TYPE_CODE_PTR)
1357 {
1358 gen_usual_unary (ax, value);
1359 gen_deref (ax, value);
1360 }
1361 type = check_typedef (value->type);
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)
1366 error (_("The left operand of `%s' is not a %s."),
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)
1372 error (_("Structure does not live in memory."));
1373
1374 i = find_field (type, field);
1375
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. */
1402 static void
1403 gen_repeat (union exp_element **pc, struct agent_expr *ax,
1404 struct axs_value *value)
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)
1411 error (_("Left operand of `@' must be an object in memory."));
1412
1413 /* Evaluate the length; it had better be a constant. */
1414 {
1415 struct value *v = const_expr (pc);
1416 int length;
1417
1418 if (!v)
1419 error (_("Right operand of `@' must be a constant, in agent expressions."));
1420 if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
1421 error (_("Right operand of `@' must be an integer."));
1422 length = value_as_long (v);
1423 if (length <= 0)
1424 error (_("Right operand of `@' must be positive."));
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
1430 done with it. */
1431 struct type *range
1432 = create_range_type (0, builtin_type_int, 0, length - 1);
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. */
1445 static void
1446 gen_sizeof (union exp_element **pc, struct agent_expr *ax,
1447 struct axs_value *value)
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;
1459
1460 ax_const_l (ax, TYPE_LENGTH (value->type));
1461 value->kind = axs_rvalue;
1462 value->type = builtin_type_int;
1463 }
1464 \f
1465
1466 /* Generating bytecode from GDB expressions: general recursive thingy */
1467
1468 /* XXX: i18n */
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. */
1471 static void
1472 gen_expr (union exp_element **pc, struct agent_expr *ax,
1473 struct axs_value *value)
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);
1482
1483 if (v)
1484 {
1485 ax_const_l (ax, value_as_long (v));
1486 value->kind = axs_rvalue;
1487 value->type = check_typedef (value_type (v));
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)
1534 error (_("Invalid combination of types in array subscripting."));
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
1554 that we actually handle in the inner case statement. */
1555 internal_error (__FILE__, __LINE__,
1556 _("gen_expr: op case sets don't match"));
1557 }
1558 break;
1559
1560 /* Note that we need to be a little subtle about generating code
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. */
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,
1570 in which case we want to emit code to trace it if it's an
1571 lvalue. */
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;
1576
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 }
1584 break;
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 {
1593 const char *name = &(*pc)[2].string;
1594 int reg;
1595 (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
1596 reg = user_reg_map_name_to_regnum (current_gdbarch,
1597 name, strlen (name));
1598 if (reg == -1)
1599 internal_error (__FILE__, __LINE__,
1600 _("Register $%s not available"), name);
1601 if (reg >= gdbarch_num_regs (current_gdbarch))
1602 error (_("'%s' is a pseudo-register; "
1603 "GDB cannot yet trace pseudoregister contents."),
1604 name);
1605 value->kind = axs_lvalue_register;
1606 value->u.reg = reg;
1607 value->type = register_type (current_gdbarch, reg);
1608 }
1609 break;
1610
1611 case OP_INTERNALVAR:
1612 error (_("GDB agent expressions cannot use convenience variables."));
1613
1614 /* Weirdo operator: see comments for gen_repeat for details. */
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 }
1628 break;
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. */
1641 internal_error (__FILE__, __LINE__,
1642 _("gen_expr: OP_MEMVAL operand isn't an rvalue???"));
1643 value->type = type;
1644 value->kind = axs_lvalue_memory;
1645 }
1646 break;
1647
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
1655 case UNOP_NEG:
1656 (*pc)++;
1657 /* -FOO is equivalent to 0 - FOO. */
1658 gen_int_literal (ax, &value1, (LONGEST) 0, builtin_type_int);
1659 gen_usual_unary (ax, &value1); /* shouldn't do much */
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)
1683 error (_("Argument of unary `*' is not a pointer."));
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
1696 of the other unary operator functions. This is because we
1697 have to throw away the code we generate. */
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
1716 shouldn't mention it, and we shouldn't be here. */
1717 internal_error (__FILE__, __LINE__,
1718 _("gen_expr: unhandled struct case"));
1719 }
1720 break;
1721
1722 case OP_TYPE:
1723 error (_("Attempt to use a type name as an expression."));
1724
1725 default:
1726 error (_("Unsupported operator in expression."));
1727 }
1728 }
1729 \f
1730
1731
1732 /* Generating bytecode from GDB expressions: driver */
1733
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. */
1739 struct agent_expr *
1740 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
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
1747 old_chain = make_cleanup_free_agent_expr (ax);
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 }
1765
1766 static void
1767 agent_command (char *exp, int from_tty)
1768 {
1769 struct cleanup *old_chain = 0;
1770 struct expression *expr;
1771 struct agent_expr *agent;
1772 struct frame_info *fi = get_current_frame (); /* need current scope */
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)
1779 error (_("GDB can't do agent expression translation with overlays."));
1780
1781 if (exp == 0)
1782 error_no_arg (_("expression to translate"));
1783
1784 expr = parse_expression (exp);
1785 old_chain = make_cleanup (free_current_contents, &expr);
1786 agent = gen_trace_for_expr (get_frame_pc (fi), expr);
1787 make_cleanup_free_agent_expr (agent);
1788 ax_print (gdb_stdout, agent);
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. */
1792
1793 do_cleanups (old_chain);
1794 dont_repeat ();
1795 }
1796 \f
1797
1798 /* Initialization code. */
1799
1800 void _initialize_ax_gdb (void);
1801 void
1802 _initialize_ax_gdb (void)
1803 {
1804 add_cmd ("agent", class_maintenance, agent_command,
1805 _("Translate an expression into remote agent bytecode."),
1806 &maintenancelist);
1807 }
This page took 0.066279 seconds and 4 git commands to generate.