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