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