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