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