* sparcnbsd-tdep.c (GDB_OSABI_NETBSD_CORE): Define, based on the
[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"
7b83296f 37#include "regcache.h"
c906108c 38
6426a772
JM
39/* To make sense of this file, you should read doc/agentexpr.texi.
40 Then look at the types and enums in ax-gdb.h. For the code itself,
41 look at gen_expr, towards the bottom; that's the main function that
42 looks at the GDB expressions and calls everything else to generate
43 code.
c906108c
SS
44
45 I'm beginning to wonder whether it wouldn't be nicer to internally
46 generate trees, with types, and then spit out the bytecode in
47 linear form afterwards; we could generate fewer `swap', `ext', and
48 `zero_ext' bytecodes that way; it would make good constant folding
49 easier, too. But at the moment, I think we should be willing to
50 pay for the simplicity of this code with less-than-optimal bytecode
51 strings.
52
c5aa993b
JM
53 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
54\f
c906108c
SS
55
56
c906108c
SS
57/* Prototypes for local functions. */
58
59/* There's a standard order to the arguments of these functions:
60 union exp_element ** --- pointer into expression
61 struct agent_expr * --- agent expression buffer to generate code into
62 struct axs_value * --- describes value left on top of stack */
c5aa993b 63
a14ed312
KB
64static struct value *const_var_ref (struct symbol *var);
65static struct value *const_expr (union exp_element **pc);
66static struct value *maybe_const_expr (union exp_element **pc);
67
68static void gen_traced_pop (struct agent_expr *, struct axs_value *);
69
70static void gen_sign_extend (struct agent_expr *, struct type *);
71static void gen_extend (struct agent_expr *, struct type *);
72static void gen_fetch (struct agent_expr *, struct type *);
73static void gen_left_shift (struct agent_expr *, int);
74
75
76static void gen_frame_args_address (struct agent_expr *);
77static void gen_frame_locals_address (struct agent_expr *);
78static void gen_offset (struct agent_expr *ax, int offset);
79static void gen_sym_offset (struct agent_expr *, struct symbol *);
80static void gen_var_ref (struct agent_expr *ax,
81 struct axs_value *value, struct symbol *var);
82
83
84static void gen_int_literal (struct agent_expr *ax,
85 struct axs_value *value,
86 LONGEST k, struct type *type);
87
88
89static void require_rvalue (struct agent_expr *ax, struct axs_value *value);
90static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value);
91static int type_wider_than (struct type *type1, struct type *type2);
92static struct type *max_type (struct type *type1, struct type *type2);
93static void gen_conversion (struct agent_expr *ax,
94 struct type *from, struct type *to);
95static int is_nontrivial_conversion (struct type *from, struct type *to);
96static void gen_usual_arithmetic (struct agent_expr *ax,
97 struct axs_value *value1,
98 struct axs_value *value2);
99static void gen_integral_promotions (struct agent_expr *ax,
100 struct axs_value *value);
101static void gen_cast (struct agent_expr *ax,
102 struct axs_value *value, struct type *type);
103static void gen_scale (struct agent_expr *ax,
104 enum agent_op op, struct type *type);
105static void gen_add (struct agent_expr *ax,
106 struct axs_value *value,
107 struct axs_value *value1,
108 struct axs_value *value2, char *name);
109static void gen_sub (struct agent_expr *ax,
110 struct axs_value *value,
111 struct axs_value *value1, struct axs_value *value2);
112static void gen_binop (struct agent_expr *ax,
113 struct axs_value *value,
114 struct axs_value *value1,
115 struct axs_value *value2,
116 enum agent_op op,
117 enum agent_op op_unsigned, int may_carry, char *name);
118static void gen_logical_not (struct agent_expr *ax, struct axs_value *value);
119static void gen_complement (struct agent_expr *ax, struct axs_value *value);
120static void gen_deref (struct agent_expr *, struct axs_value *);
121static void gen_address_of (struct agent_expr *, struct axs_value *);
122static int find_field (struct type *type, char *name);
123static void gen_bitfield_ref (struct agent_expr *ax,
124 struct axs_value *value,
125 struct type *type, int start, int end);
126static void gen_struct_ref (struct agent_expr *ax,
127 struct axs_value *value,
128 char *field,
129 char *operator_name, char *operand_name);
130static void gen_repeat (union exp_element **pc,
131 struct agent_expr *ax, struct axs_value *value);
132static void gen_sizeof (union exp_element **pc,
133 struct agent_expr *ax, struct axs_value *value);
134static void gen_expr (union exp_element **pc,
135 struct agent_expr *ax, struct axs_value *value);
c5aa993b 136
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 620
a55cc764
DJ
621 case LOC_COMPUTED:
622 case LOC_COMPUTED_ARG:
623 (*SYMBOL_LOCATION_FUNCS (var)->tracepoint_var_ref) (var, ax, value);
624 break;
625
c906108c
SS
626 case LOC_OPTIMIZED_OUT:
627 error ("The variable `%s' has been optimized out.",
de5ad195 628 SYMBOL_PRINT_NAME (var));
c906108c
SS
629 break;
630
631 default:
632 error ("Cannot find value of botched symbol `%s'.",
de5ad195 633 SYMBOL_PRINT_NAME (var));
c906108c
SS
634 break;
635 }
636}
c5aa993b 637\f
c906108c
SS
638
639
c906108c
SS
640/* Generating bytecode from GDB expressions: literals */
641
642static void
fba45db2
KB
643gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
644 struct type *type)
c906108c
SS
645{
646 ax_const_l (ax, k);
647 value->kind = axs_rvalue;
648 value->type = type;
649}
c5aa993b 650\f
c906108c
SS
651
652
c906108c
SS
653/* Generating bytecode from GDB expressions: unary conversions, casts */
654
655/* Take what's on the top of the stack (as described by VALUE), and
656 try to make an rvalue out of it. Signal an error if we can't do
657 that. */
658static void
fba45db2 659require_rvalue (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
660{
661 switch (value->kind)
662 {
663 case axs_rvalue:
664 /* It's already an rvalue. */
665 break;
666
667 case axs_lvalue_memory:
668 /* The top of stack is the address of the object. Dereference. */
669 gen_fetch (ax, value->type);
670 break;
671
672 case axs_lvalue_register:
673 /* There's nothing on the stack, but value->u.reg is the
674 register number containing the value.
675
c5aa993b
JM
676 When we add floating-point support, this is going to have to
677 change. What about SPARC register pairs, for example? */
c906108c
SS
678 ax_reg (ax, value->u.reg);
679 gen_extend (ax, value->type);
680 break;
681 }
682
683 value->kind = axs_rvalue;
684}
685
686
687/* Assume the top of the stack is described by VALUE, and perform the
688 usual unary conversions. This is motivated by ANSI 6.2.2, but of
689 course GDB expressions are not ANSI; they're the mishmash union of
690 a bunch of languages. Rah.
691
692 NOTE! This function promises to produce an rvalue only when the
693 incoming value is of an appropriate type. In other words, the
694 consumer of the value this function produces may assume the value
695 is an rvalue only after checking its type.
696
697 The immediate issue is that if the user tries to use a structure or
698 union as an operand of, say, the `+' operator, we don't want to try
699 to convert that structure to an rvalue; require_rvalue will bomb on
700 structs and unions. Rather, we want to simply pass the struct
701 lvalue through unchanged, and let `+' raise an error. */
702
703static void
fba45db2 704gen_usual_unary (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
705{
706 /* We don't have to generate any code for the usual integral
707 conversions, since values are always represented as full-width on
708 the stack. Should we tweak the type? */
709
710 /* Some types require special handling. */
0004e5a2 711 switch (TYPE_CODE (value->type))
c906108c
SS
712 {
713 /* Functions get converted to a pointer to the function. */
714 case TYPE_CODE_FUNC:
715 value->type = lookup_pointer_type (value->type);
716 value->kind = axs_rvalue; /* Should always be true, but just in case. */
717 break;
718
719 /* Arrays get converted to a pointer to their first element, and
c5aa993b 720 are no longer an lvalue. */
c906108c
SS
721 case TYPE_CODE_ARRAY:
722 {
723 struct type *elements = TYPE_TARGET_TYPE (value->type);
724 value->type = lookup_pointer_type (elements);
725 value->kind = axs_rvalue;
726 /* We don't need to generate any code; the address of the array
727 is also the address of its first element. */
728 }
c5aa993b 729 break;
c906108c 730
c5aa993b
JM
731 /* Don't try to convert structures and unions to rvalues. Let the
732 consumer signal an error. */
c906108c
SS
733 case TYPE_CODE_STRUCT:
734 case TYPE_CODE_UNION:
735 return;
736
737 /* If the value is an enum, call it an integer. */
738 case TYPE_CODE_ENUM:
739 value->type = builtin_type_int;
740 break;
741 }
742
743 /* If the value is an lvalue, dereference it. */
744 require_rvalue (ax, value);
745}
746
747
748/* Return non-zero iff the type TYPE1 is considered "wider" than the
749 type TYPE2, according to the rules described in gen_usual_arithmetic. */
750static int
fba45db2 751type_wider_than (struct type *type1, struct type *type2)
c906108c
SS
752{
753 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
754 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
755 && TYPE_UNSIGNED (type1)
c5aa993b 756 && !TYPE_UNSIGNED (type2)));
c906108c
SS
757}
758
759
760/* Return the "wider" of the two types TYPE1 and TYPE2. */
761static struct type *
fba45db2 762max_type (struct type *type1, struct type *type2)
c906108c
SS
763{
764 return type_wider_than (type1, type2) ? type1 : type2;
765}
766
767
768/* Generate code to convert a scalar value of type FROM to type TO. */
769static void
fba45db2 770gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
c906108c
SS
771{
772 /* Perhaps there is a more graceful way to state these rules. */
773
774 /* If we're converting to a narrower type, then we need to clear out
775 the upper bits. */
776 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
777 gen_extend (ax, from);
778
779 /* If the two values have equal width, but different signednesses,
780 then we need to extend. */
781 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
782 {
783 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
784 gen_extend (ax, to);
785 }
786
787 /* If we're converting to a wider type, and becoming unsigned, then
788 we need to zero out any possible sign bits. */
789 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
790 {
791 if (TYPE_UNSIGNED (to))
792 gen_extend (ax, to);
793 }
794}
795
796
797/* Return non-zero iff the type FROM will require any bytecodes to be
798 emitted to be converted to the type TO. */
799static int
fba45db2 800is_nontrivial_conversion (struct type *from, struct type *to)
c906108c
SS
801{
802 struct agent_expr *ax = new_agent_expr (0);
803 int nontrivial;
804
805 /* Actually generate the code, and see if anything came out. At the
806 moment, it would be trivial to replicate the code in
807 gen_conversion here, but in the future, when we're supporting
808 floating point and the like, it may not be. Doing things this
809 way allows this function to be independent of the logic in
810 gen_conversion. */
811 gen_conversion (ax, from, to);
812 nontrivial = ax->len > 0;
813 free_agent_expr (ax);
814 return nontrivial;
815}
816
817
818/* Generate code to perform the "usual arithmetic conversions" (ANSI C
819 6.2.1.5) for the two operands of an arithmetic operator. This
820 effectively finds a "least upper bound" type for the two arguments,
821 and promotes each argument to that type. *VALUE1 and *VALUE2
822 describe the values as they are passed in, and as they are left. */
823static void
fba45db2
KB
824gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1,
825 struct axs_value *value2)
c906108c
SS
826{
827 /* Do the usual binary conversions. */
828 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
829 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
830 {
831 /* The ANSI integral promotions seem to work this way: Order the
c5aa993b
JM
832 integer types by size, and then by signedness: an n-bit
833 unsigned type is considered "wider" than an n-bit signed
834 type. Promote to the "wider" of the two types, and always
835 promote at least to int. */
c906108c
SS
836 struct type *target = max_type (builtin_type_int,
837 max_type (value1->type, value2->type));
838
839 /* Deal with value2, on the top of the stack. */
840 gen_conversion (ax, value2->type, target);
841
842 /* Deal with value1, not on the top of the stack. Don't
843 generate the `swap' instructions if we're not actually going
844 to do anything. */
845 if (is_nontrivial_conversion (value1->type, target))
846 {
847 ax_simple (ax, aop_swap);
848 gen_conversion (ax, value1->type, target);
849 ax_simple (ax, aop_swap);
850 }
851
852 value1->type = value2->type = target;
853 }
854}
855
856
857/* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
858 the value on the top of the stack, as described by VALUE. Assume
859 the value has integral type. */
860static void
fba45db2 861gen_integral_promotions (struct agent_expr *ax, struct axs_value *value)
c906108c 862{
c5aa993b 863 if (!type_wider_than (value->type, builtin_type_int))
c906108c
SS
864 {
865 gen_conversion (ax, value->type, builtin_type_int);
866 value->type = builtin_type_int;
867 }
c5aa993b 868 else if (!type_wider_than (value->type, builtin_type_unsigned_int))
c906108c
SS
869 {
870 gen_conversion (ax, value->type, builtin_type_unsigned_int);
871 value->type = builtin_type_unsigned_int;
872 }
873}
874
875
876/* Generate code for a cast to TYPE. */
877static void
fba45db2 878gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
c906108c
SS
879{
880 /* GCC does allow casts to yield lvalues, so this should be fixed
881 before merging these changes into the trunk. */
882 require_rvalue (ax, value);
883 /* Dereference typedefs. */
884 type = check_typedef (type);
885
0004e5a2 886 switch (TYPE_CODE (type))
c906108c
SS
887 {
888 case TYPE_CODE_PTR:
889 /* It's implementation-defined, and I'll bet this is what GCC
890 does. */
891 break;
892
893 case TYPE_CODE_ARRAY:
894 case TYPE_CODE_STRUCT:
895 case TYPE_CODE_UNION:
896 case TYPE_CODE_FUNC:
897 error ("Illegal type cast: intended type must be scalar.");
898
899 case TYPE_CODE_ENUM:
900 /* We don't have to worry about the size of the value, because
901 all our integral values are fully sign-extended, and when
902 casting pointers we can do anything we like. Is there any
903 way for us to actually know what GCC actually does with a
904 cast like this? */
905 value->type = type;
906 break;
c5aa993b 907
c906108c
SS
908 case TYPE_CODE_INT:
909 gen_conversion (ax, value->type, type);
910 break;
911
912 case TYPE_CODE_VOID:
913 /* We could pop the value, and rely on everyone else to check
c5aa993b
JM
914 the type and notice that this value doesn't occupy a stack
915 slot. But for now, leave the value on the stack, and
916 preserve the "value == stack element" assumption. */
c906108c
SS
917 break;
918
919 default:
920 error ("Casts to requested type are not yet implemented.");
921 }
922
923 value->type = type;
924}
c5aa993b 925\f
c906108c
SS
926
927
c906108c
SS
928/* Generating bytecode from GDB expressions: arithmetic */
929
930/* Scale the integer on the top of the stack by the size of the target
931 of the pointer type TYPE. */
932static void
fba45db2 933gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
c906108c
SS
934{
935 struct type *element = TYPE_TARGET_TYPE (type);
936
0004e5a2 937 if (TYPE_LENGTH (element) != 1)
c906108c 938 {
0004e5a2 939 ax_const_l (ax, TYPE_LENGTH (element));
c906108c
SS
940 ax_simple (ax, op);
941 }
942}
943
944
945/* Generate code for an addition; non-trivial because we deal with
946 pointer arithmetic. We set VALUE to describe the result value; we
947 assume VALUE1 and VALUE2 describe the two operands, and that
948 they've undergone the usual binary conversions. Used by both
949 BINOP_ADD and BINOP_SUBSCRIPT. NAME is used in error messages. */
950static void
fba45db2
KB
951gen_add (struct agent_expr *ax, struct axs_value *value,
952 struct axs_value *value1, struct axs_value *value2, char *name)
c906108c
SS
953{
954 /* Is it INT+PTR? */
0004e5a2
DJ
955 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
956 && TYPE_CODE (value2->type) == TYPE_CODE_PTR)
c906108c
SS
957 {
958 /* Swap the values and proceed normally. */
959 ax_simple (ax, aop_swap);
960 gen_scale (ax, aop_mul, value2->type);
961 ax_simple (ax, aop_add);
c5aa993b 962 gen_extend (ax, value2->type); /* Catch overflow. */
c906108c
SS
963 value->type = value2->type;
964 }
965
966 /* Is it PTR+INT? */
0004e5a2
DJ
967 else if (TYPE_CODE (value1->type) == TYPE_CODE_PTR
968 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
c906108c
SS
969 {
970 gen_scale (ax, aop_mul, value1->type);
971 ax_simple (ax, aop_add);
c5aa993b 972 gen_extend (ax, value1->type); /* Catch overflow. */
c906108c
SS
973 value->type = value1->type;
974 }
975
976 /* Must be number + number; the usual binary conversions will have
977 brought them both to the same width. */
0004e5a2
DJ
978 else if (TYPE_CODE (value1->type) == TYPE_CODE_INT
979 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
c906108c
SS
980 {
981 ax_simple (ax, aop_add);
c5aa993b 982 gen_extend (ax, value1->type); /* Catch overflow. */
c906108c
SS
983 value->type = value1->type;
984 }
985
986 else
987 error ("Illegal combination of types in %s.", name);
988
989 value->kind = axs_rvalue;
990}
991
992
993/* Generate code for an addition; non-trivial because we have to deal
994 with pointer arithmetic. We set VALUE to describe the result
995 value; we assume VALUE1 and VALUE2 describe the two operands, and
996 that they've undergone the usual binary conversions. */
997static void
fba45db2
KB
998gen_sub (struct agent_expr *ax, struct axs_value *value,
999 struct axs_value *value1, struct axs_value *value2)
c906108c 1000{
0004e5a2 1001 if (TYPE_CODE (value1->type) == TYPE_CODE_PTR)
c906108c
SS
1002 {
1003 /* Is it PTR - INT? */
0004e5a2 1004 if (TYPE_CODE (value2->type) == TYPE_CODE_INT)
c906108c
SS
1005 {
1006 gen_scale (ax, aop_mul, value1->type);
1007 ax_simple (ax, aop_sub);
c5aa993b 1008 gen_extend (ax, value1->type); /* Catch overflow. */
c906108c
SS
1009 value->type = value1->type;
1010 }
1011
1012 /* Is it PTR - PTR? Strictly speaking, the types ought to
c5aa993b
JM
1013 match, but this is what the normal GDB expression evaluator
1014 tests for. */
0004e5a2 1015 else if (TYPE_CODE (value2->type) == TYPE_CODE_PTR
c906108c
SS
1016 && (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1017 == TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type))))
1018 {
1019 ax_simple (ax, aop_sub);
1020 gen_scale (ax, aop_div_unsigned, value1->type);
c5aa993b 1021 value->type = builtin_type_long; /* FIXME --- should be ptrdiff_t */
c906108c
SS
1022 }
1023 else
1024 error ("\
1025First argument of `-' is a pointer, but second argument is neither\n\
1026an integer nor a pointer of the same type.");
1027 }
1028
1029 /* Must be number + number. */
0004e5a2
DJ
1030 else if (TYPE_CODE (value1->type) == TYPE_CODE_INT
1031 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
c906108c
SS
1032 {
1033 ax_simple (ax, aop_sub);
c5aa993b 1034 gen_extend (ax, value1->type); /* Catch overflow. */
c906108c
SS
1035 value->type = value1->type;
1036 }
c5aa993b 1037
c906108c
SS
1038 else
1039 error ("Illegal combination of types in subtraction.");
1040
1041 value->kind = axs_rvalue;
1042}
1043
1044/* Generate code for a binary operator that doesn't do pointer magic.
1045 We set VALUE to describe the result value; we assume VALUE1 and
1046 VALUE2 describe the two operands, and that they've undergone the
1047 usual binary conversions. MAY_CARRY should be non-zero iff the
1048 result needs to be extended. NAME is the English name of the
1049 operator, used in error messages */
1050static void
fba45db2
KB
1051gen_binop (struct agent_expr *ax, struct axs_value *value,
1052 struct axs_value *value1, struct axs_value *value2, enum agent_op op,
1053 enum agent_op op_unsigned, int may_carry, char *name)
c906108c
SS
1054{
1055 /* We only handle INT op INT. */
0004e5a2
DJ
1056 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1057 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
c906108c 1058 error ("Illegal combination of types in %s.", name);
c5aa993b 1059
c906108c
SS
1060 ax_simple (ax,
1061 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1062 if (may_carry)
c5aa993b 1063 gen_extend (ax, value1->type); /* catch overflow */
c906108c
SS
1064 value->type = value1->type;
1065 value->kind = axs_rvalue;
1066}
1067
1068
1069static void
fba45db2 1070gen_logical_not (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1071{
1072 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1073 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
1074 error ("Illegal type of operand to `!'.");
1075
1076 gen_usual_unary (ax, value);
1077 ax_simple (ax, aop_log_not);
1078 value->type = builtin_type_int;
1079}
1080
1081
1082static void
fba45db2 1083gen_complement (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1084{
1085 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
1086 error ("Illegal type of operand to `~'.");
1087
1088 gen_usual_unary (ax, value);
1089 gen_integral_promotions (ax, value);
1090 ax_simple (ax, aop_bit_not);
1091 gen_extend (ax, value->type);
1092}
c5aa993b 1093\f
c906108c
SS
1094
1095
c906108c
SS
1096/* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1097
1098/* Dereference the value on the top of the stack. */
1099static void
fba45db2 1100gen_deref (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1101{
1102 /* The caller should check the type, because several operators use
1103 this, and we don't know what error message to generate. */
0004e5a2 1104 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
8e65ff28
AC
1105 internal_error (__FILE__, __LINE__,
1106 "gen_deref: expected a pointer");
c906108c
SS
1107
1108 /* We've got an rvalue now, which is a pointer. We want to yield an
1109 lvalue, whose address is exactly that pointer. So we don't
1110 actually emit any code; we just change the type from "Pointer to
1111 T" to "T", and mark the value as an lvalue in memory. Leave it
1112 to the consumer to actually dereference it. */
1113 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
0004e5a2 1114 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
c906108c
SS
1115 ? axs_rvalue : axs_lvalue_memory);
1116}
1117
1118
1119/* Produce the address of the lvalue on the top of the stack. */
1120static void
fba45db2 1121gen_address_of (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1122{
1123 /* Special case for taking the address of a function. The ANSI
1124 standard describes this as a special case, too, so this
1125 arrangement is not without motivation. */
0004e5a2 1126 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
c906108c
SS
1127 /* The value's already an rvalue on the stack, so we just need to
1128 change the type. */
1129 value->type = lookup_pointer_type (value->type);
1130 else
1131 switch (value->kind)
1132 {
1133 case axs_rvalue:
1134 error ("Operand of `&' is an rvalue, which has no address.");
1135
1136 case axs_lvalue_register:
1137 error ("Operand of `&' is in a register, and has no address.");
1138
1139 case axs_lvalue_memory:
1140 value->kind = axs_rvalue;
1141 value->type = lookup_pointer_type (value->type);
1142 break;
1143 }
1144}
1145
1146
1147/* A lot of this stuff will have to change to support C++. But we're
1148 not going to deal with that at the moment. */
1149
1150/* Find the field in the structure type TYPE named NAME, and return
1151 its index in TYPE's field array. */
1152static int
fba45db2 1153find_field (struct type *type, char *name)
c906108c
SS
1154{
1155 int i;
1156
1157 CHECK_TYPEDEF (type);
1158
1159 /* Make sure this isn't C++. */
1160 if (TYPE_N_BASECLASSES (type) != 0)
8e65ff28
AC
1161 internal_error (__FILE__, __LINE__,
1162 "find_field: derived classes supported");
c906108c
SS
1163
1164 for (i = 0; i < TYPE_NFIELDS (type); i++)
1165 {
1166 char *this_name = TYPE_FIELD_NAME (type, i);
1167
bde58177 1168 if (this_name && strcmp (name, this_name) == 0)
c906108c
SS
1169 return i;
1170
1171 if (this_name[0] == '\0')
8e65ff28
AC
1172 internal_error (__FILE__, __LINE__,
1173 "find_field: anonymous unions not supported");
c906108c
SS
1174 }
1175
1176 error ("Couldn't find member named `%s' in struct/union `%s'",
7495dfdb 1177 name, TYPE_TAG_NAME (type));
c906108c
SS
1178
1179 return 0;
1180}
1181
1182
1183/* Generate code to push the value of a bitfield of a structure whose
1184 address is on the top of the stack. START and END give the
1185 starting and one-past-ending *bit* numbers of the field within the
1186 structure. */
1187static void
fba45db2
KB
1188gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
1189 struct type *type, int start, int end)
c906108c
SS
1190{
1191 /* Note that ops[i] fetches 8 << i bits. */
1192 static enum agent_op ops[]
c5aa993b
JM
1193 =
1194 {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
c906108c
SS
1195 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1196
1197 /* We don't want to touch any byte that the bitfield doesn't
1198 actually occupy; we shouldn't make any accesses we're not
1199 explicitly permitted to. We rely here on the fact that the
1200 bytecode `ref' operators work on unaligned addresses.
1201
1202 It takes some fancy footwork to get the stack to work the way
1203 we'd like. Say we're retrieving a bitfield that requires three
1204 fetches. Initially, the stack just contains the address:
c5aa993b 1205 addr
c906108c 1206 For the first fetch, we duplicate the address
c5aa993b 1207 addr addr
c906108c
SS
1208 then add the byte offset, do the fetch, and shift and mask as
1209 needed, yielding a fragment of the value, properly aligned for
1210 the final bitwise or:
c5aa993b 1211 addr frag1
c906108c 1212 then we swap, and repeat the process:
c5aa993b
JM
1213 frag1 addr --- address on top
1214 frag1 addr addr --- duplicate it
1215 frag1 addr frag2 --- get second fragment
1216 frag1 frag2 addr --- swap again
1217 frag1 frag2 frag3 --- get third fragment
c906108c
SS
1218 Notice that, since the third fragment is the last one, we don't
1219 bother duplicating the address this time. Now we have all the
1220 fragments on the stack, and we can simply `or' them together,
1221 yielding the final value of the bitfield. */
1222
1223 /* The first and one-after-last bits in the field, but rounded down
1224 and up to byte boundaries. */
1225 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
c5aa993b
JM
1226 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1227 / TARGET_CHAR_BIT)
1228 * TARGET_CHAR_BIT);
c906108c
SS
1229
1230 /* current bit offset within the structure */
1231 int offset;
1232
1233 /* The index in ops of the opcode we're considering. */
1234 int op;
1235
1236 /* The number of fragments we generated in the process. Probably
1237 equal to the number of `one' bits in bytesize, but who cares? */
1238 int fragment_count;
1239
1240 /* Dereference any typedefs. */
1241 type = check_typedef (type);
1242
1243 /* Can we fetch the number of bits requested at all? */
1244 if ((end - start) > ((1 << num_ops) * 8))
8e65ff28
AC
1245 internal_error (__FILE__, __LINE__,
1246 "gen_bitfield_ref: bitfield too wide");
c906108c
SS
1247
1248 /* Note that we know here that we only need to try each opcode once.
1249 That may not be true on machines with weird byte sizes. */
1250 offset = bound_start;
1251 fragment_count = 0;
1252 for (op = num_ops - 1; op >= 0; op--)
1253 {
1254 /* number of bits that ops[op] would fetch */
1255 int op_size = 8 << op;
1256
1257 /* The stack at this point, from bottom to top, contains zero or
c5aa993b
JM
1258 more fragments, then the address. */
1259
c906108c
SS
1260 /* Does this fetch fit within the bitfield? */
1261 if (offset + op_size <= bound_end)
1262 {
1263 /* Is this the last fragment? */
1264 int last_frag = (offset + op_size == bound_end);
1265
c5aa993b
JM
1266 if (!last_frag)
1267 ax_simple (ax, aop_dup); /* keep a copy of the address */
1268
c906108c
SS
1269 /* Add the offset. */
1270 gen_offset (ax, offset / TARGET_CHAR_BIT);
1271
1272 if (trace_kludge)
1273 {
1274 /* Record the area of memory we're about to fetch. */
1275 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1276 }
1277
1278 /* Perform the fetch. */
1279 ax_simple (ax, ops[op]);
c5aa993b
JM
1280
1281 /* Shift the bits we have to their proper position.
c906108c
SS
1282 gen_left_shift will generate right shifts when the operand
1283 is negative.
1284
c5aa993b
JM
1285 A big-endian field diagram to ponder:
1286 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1287 +------++------++------++------++------++------++------++------+
1288 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1289 ^ ^ ^ ^
1290 bit number 16 32 48 53
c906108c
SS
1291 These are bit numbers as supplied by GDB. Note that the
1292 bit numbers run from right to left once you've fetched the
1293 value!
1294
c5aa993b
JM
1295 A little-endian field diagram to ponder:
1296 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1297 +------++------++------++------++------++------++------++------+
1298 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1299 ^ ^ ^ ^ ^
1300 bit number 48 32 16 4 0
1301
1302 In both cases, the most significant end is on the left
1303 (i.e. normal numeric writing order), which means that you
1304 don't go crazy thinking about `left' and `right' shifts.
1305
1306 We don't have to worry about masking yet:
1307 - If they contain garbage off the least significant end, then we
1308 must be looking at the low end of the field, and the right
1309 shift will wipe them out.
1310 - If they contain garbage off the most significant end, then we
1311 must be looking at the most significant end of the word, and
1312 the sign/zero extension will wipe them out.
1313 - If we're in the interior of the word, then there is no garbage
1314 on either end, because the ref operators zero-extend. */
d7449b42 1315 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
c906108c 1316 gen_left_shift (ax, end - (offset + op_size));
c5aa993b 1317 else
c906108c
SS
1318 gen_left_shift (ax, offset - start);
1319
c5aa993b 1320 if (!last_frag)
c906108c
SS
1321 /* Bring the copy of the address up to the top. */
1322 ax_simple (ax, aop_swap);
1323
1324 offset += op_size;
1325 fragment_count++;
1326 }
1327 }
1328
1329 /* Generate enough bitwise `or' operations to combine all the
1330 fragments we left on the stack. */
1331 while (fragment_count-- > 1)
1332 ax_simple (ax, aop_bit_or);
1333
1334 /* Sign- or zero-extend the value as appropriate. */
1335 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1336
1337 /* This is *not* an lvalue. Ugh. */
1338 value->kind = axs_rvalue;
1339 value->type = type;
1340}
1341
1342
1343/* Generate code to reference the member named FIELD of a structure or
1344 union. The top of the stack, as described by VALUE, should have
1345 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1346 the operator being compiled, and OPERAND_NAME is the kind of thing
1347 it operates on; we use them in error messages. */
1348static void
fba45db2
KB
1349gen_struct_ref (struct agent_expr *ax, struct axs_value *value, char *field,
1350 char *operator_name, char *operand_name)
c906108c
SS
1351{
1352 struct type *type;
1353 int i;
1354
1355 /* Follow pointers until we reach a non-pointer. These aren't the C
1356 semantics, but they're what the normal GDB evaluator does, so we
1357 should at least be consistent. */
0004e5a2 1358 while (TYPE_CODE (value->type) == TYPE_CODE_PTR)
c906108c
SS
1359 {
1360 gen_usual_unary (ax, value);
1361 gen_deref (ax, value);
1362 }
e8860ec2 1363 type = check_typedef (value->type);
c906108c
SS
1364
1365 /* This must yield a structure or a union. */
1366 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1367 && TYPE_CODE (type) != TYPE_CODE_UNION)
1368 error ("The left operand of `%s' is not a %s.",
1369 operator_name, operand_name);
1370
1371 /* And it must be in memory; we don't deal with structure rvalues,
1372 or structures living in registers. */
1373 if (value->kind != axs_lvalue_memory)
1374 error ("Structure does not live in memory.");
1375
1376 i = find_field (type, field);
c5aa993b 1377
c906108c
SS
1378 /* Is this a bitfield? */
1379 if (TYPE_FIELD_PACKED (type, i))
1380 gen_bitfield_ref (ax, value, TYPE_FIELD_TYPE (type, i),
1381 TYPE_FIELD_BITPOS (type, i),
1382 (TYPE_FIELD_BITPOS (type, i)
1383 + TYPE_FIELD_BITSIZE (type, i)));
1384 else
1385 {
1386 gen_offset (ax, TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT);
1387 value->kind = axs_lvalue_memory;
1388 value->type = TYPE_FIELD_TYPE (type, i);
1389 }
1390}
1391
1392
1393/* Generate code for GDB's magical `repeat' operator.
1394 LVALUE @ INT creates an array INT elements long, and whose elements
1395 have the same type as LVALUE, located in memory so that LVALUE is
1396 its first element. For example, argv[0]@argc gives you the array
1397 of command-line arguments.
1398
1399 Unfortunately, because we have to know the types before we actually
1400 have a value for the expression, we can't implement this perfectly
1401 without changing the type system, having values that occupy two
1402 stack slots, doing weird things with sizeof, etc. So we require
1403 the right operand to be a constant expression. */
1404static void
fba45db2
KB
1405gen_repeat (union exp_element **pc, struct agent_expr *ax,
1406 struct axs_value *value)
c906108c
SS
1407{
1408 struct axs_value value1;
1409 /* We don't want to turn this into an rvalue, so no conversions
1410 here. */
1411 gen_expr (pc, ax, &value1);
1412 if (value1.kind != axs_lvalue_memory)
1413 error ("Left operand of `@' must be an object in memory.");
1414
1415 /* Evaluate the length; it had better be a constant. */
1416 {
1417 struct value *v = const_expr (pc);
1418 int length;
1419
c5aa993b 1420 if (!v)
c906108c 1421 error ("Right operand of `@' must be a constant, in agent expressions.");
0004e5a2 1422 if (TYPE_CODE (v->type) != TYPE_CODE_INT)
c906108c
SS
1423 error ("Right operand of `@' must be an integer.");
1424 length = value_as_long (v);
1425 if (length <= 0)
1426 error ("Right operand of `@' must be positive.");
1427
1428 /* The top of the stack is already the address of the object, so
1429 all we need to do is frob the type of the lvalue. */
1430 {
1431 /* FIXME-type-allocation: need a way to free this type when we are
c5aa993b 1432 done with it. */
c906108c 1433 struct type *range
c5aa993b 1434 = create_range_type (0, builtin_type_int, 0, length - 1);
c906108c
SS
1435 struct type *array = create_array_type (0, value1.type, range);
1436
1437 value->kind = axs_lvalue_memory;
1438 value->type = array;
1439 }
1440 }
1441}
1442
1443
1444/* Emit code for the `sizeof' operator.
1445 *PC should point at the start of the operand expression; we advance it
1446 to the first instruction after the operand. */
1447static void
fba45db2
KB
1448gen_sizeof (union exp_element **pc, struct agent_expr *ax,
1449 struct axs_value *value)
c906108c
SS
1450{
1451 /* We don't care about the value of the operand expression; we only
1452 care about its type. However, in the current arrangement, the
1453 only way to find an expression's type is to generate code for it.
1454 So we generate code for the operand, and then throw it away,
1455 replacing it with code that simply pushes its size. */
1456 int start = ax->len;
1457 gen_expr (pc, ax, value);
1458
1459 /* Throw away the code we just generated. */
1460 ax->len = start;
c5aa993b 1461
c906108c
SS
1462 ax_const_l (ax, TYPE_LENGTH (value->type));
1463 value->kind = axs_rvalue;
1464 value->type = builtin_type_int;
1465}
c906108c 1466\f
c5aa993b 1467
c906108c
SS
1468/* Generating bytecode from GDB expressions: general recursive thingy */
1469
1470/* A gen_expr function written by a Gen-X'er guy.
1471 Append code for the subexpression of EXPR starting at *POS_P to AX. */
1472static void
fba45db2
KB
1473gen_expr (union exp_element **pc, struct agent_expr *ax,
1474 struct axs_value *value)
c906108c
SS
1475{
1476 /* Used to hold the descriptions of operand expressions. */
1477 struct axs_value value1, value2;
1478 enum exp_opcode op = (*pc)[0].opcode;
1479
1480 /* If we're looking at a constant expression, just push its value. */
1481 {
1482 struct value *v = maybe_const_expr (pc);
c5aa993b 1483
c906108c
SS
1484 if (v)
1485 {
1486 ax_const_l (ax, value_as_long (v));
1487 value->kind = axs_rvalue;
1488 value->type = check_typedef (VALUE_TYPE (v));
1489 return;
1490 }
1491 }
1492
1493 /* Otherwise, go ahead and generate code for it. */
1494 switch (op)
1495 {
1496 /* Binary arithmetic operators. */
1497 case BINOP_ADD:
1498 case BINOP_SUB:
1499 case BINOP_MUL:
1500 case BINOP_DIV:
1501 case BINOP_REM:
1502 case BINOP_SUBSCRIPT:
1503 case BINOP_BITWISE_AND:
1504 case BINOP_BITWISE_IOR:
1505 case BINOP_BITWISE_XOR:
1506 (*pc)++;
1507 gen_expr (pc, ax, &value1);
1508 gen_usual_unary (ax, &value1);
1509 gen_expr (pc, ax, &value2);
1510 gen_usual_unary (ax, &value2);
1511 gen_usual_arithmetic (ax, &value1, &value2);
1512 switch (op)
1513 {
1514 case BINOP_ADD:
1515 gen_add (ax, value, &value1, &value2, "addition");
1516 break;
1517 case BINOP_SUB:
1518 gen_sub (ax, value, &value1, &value2);
1519 break;
1520 case BINOP_MUL:
1521 gen_binop (ax, value, &value1, &value2,
1522 aop_mul, aop_mul, 1, "multiplication");
1523 break;
1524 case BINOP_DIV:
1525 gen_binop (ax, value, &value1, &value2,
1526 aop_div_signed, aop_div_unsigned, 1, "division");
1527 break;
1528 case BINOP_REM:
1529 gen_binop (ax, value, &value1, &value2,
1530 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
1531 break;
1532 case BINOP_SUBSCRIPT:
1533 gen_add (ax, value, &value1, &value2, "array subscripting");
1534 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1535 error ("Illegal combination of types in array subscripting.");
1536 gen_deref (ax, value);
1537 break;
1538 case BINOP_BITWISE_AND:
1539 gen_binop (ax, value, &value1, &value2,
1540 aop_bit_and, aop_bit_and, 0, "bitwise and");
1541 break;
1542
1543 case BINOP_BITWISE_IOR:
1544 gen_binop (ax, value, &value1, &value2,
1545 aop_bit_or, aop_bit_or, 0, "bitwise or");
1546 break;
1547
1548 case BINOP_BITWISE_XOR:
1549 gen_binop (ax, value, &value1, &value2,
1550 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
1551 break;
1552
1553 default:
1554 /* We should only list operators in the outer case statement
c5aa993b 1555 that we actually handle in the inner case statement. */
8e65ff28
AC
1556 internal_error (__FILE__, __LINE__,
1557 "gen_expr: op case sets don't match");
c906108c
SS
1558 }
1559 break;
1560
1561 /* Note that we need to be a little subtle about generating code
c5aa993b
JM
1562 for comma. In C, we can do some optimizations here because
1563 we know the left operand is only being evaluated for effect.
1564 However, if the tracing kludge is in effect, then we always
1565 need to evaluate the left hand side fully, so that all the
1566 variables it mentions get traced. */
c906108c
SS
1567 case BINOP_COMMA:
1568 (*pc)++;
1569 gen_expr (pc, ax, &value1);
1570 /* Don't just dispose of the left operand. We might be tracing,
c5aa993b
JM
1571 in which case we want to emit code to trace it if it's an
1572 lvalue. */
c906108c
SS
1573 gen_traced_pop (ax, &value1);
1574 gen_expr (pc, ax, value);
1575 /* It's the consumer's responsibility to trace the right operand. */
1576 break;
c5aa993b 1577
c906108c
SS
1578 case OP_LONG: /* some integer constant */
1579 {
1580 struct type *type = (*pc)[1].type;
1581 LONGEST k = (*pc)[2].longconst;
1582 (*pc) += 4;
1583 gen_int_literal (ax, value, k, type);
1584 }
c5aa993b 1585 break;
c906108c
SS
1586
1587 case OP_VAR_VALUE:
1588 gen_var_ref (ax, value, (*pc)[2].symbol);
1589 (*pc) += 4;
1590 break;
1591
1592 case OP_REGISTER:
1593 {
1594 int reg = (int) (*pc)[1].longconst;
1595 (*pc) += 3;
1596 value->kind = axs_lvalue_register;
1597 value->u.reg = reg;
7b83296f 1598 value->type = register_type (current_gdbarch, reg);
c906108c 1599 }
c5aa993b 1600 break;
c906108c
SS
1601
1602 case OP_INTERNALVAR:
1603 error ("GDB agent expressions cannot use convenience variables.");
1604
c5aa993b 1605 /* Weirdo operator: see comments for gen_repeat for details. */
c906108c
SS
1606 case BINOP_REPEAT:
1607 /* Note that gen_repeat handles its own argument evaluation. */
1608 (*pc)++;
1609 gen_repeat (pc, ax, value);
1610 break;
1611
1612 case UNOP_CAST:
1613 {
1614 struct type *type = (*pc)[1].type;
1615 (*pc) += 3;
1616 gen_expr (pc, ax, value);
1617 gen_cast (ax, value, type);
1618 }
c5aa993b 1619 break;
c906108c
SS
1620
1621 case UNOP_MEMVAL:
1622 {
1623 struct type *type = check_typedef ((*pc)[1].type);
1624 (*pc) += 3;
1625 gen_expr (pc, ax, value);
1626 /* I'm not sure I understand UNOP_MEMVAL entirely. I think
1627 it's just a hack for dealing with minsyms; you take some
1628 integer constant, pretend it's the address of an lvalue of
1629 the given type, and dereference it. */
1630 if (value->kind != axs_rvalue)
1631 /* This would be weird. */
8e65ff28
AC
1632 internal_error (__FILE__, __LINE__,
1633 "gen_expr: OP_MEMVAL operand isn't an rvalue???");
c906108c
SS
1634 value->type = type;
1635 value->kind = axs_lvalue_memory;
1636 }
c5aa993b 1637 break;
c906108c
SS
1638
1639 case UNOP_NEG:
1640 (*pc)++;
1641 /* -FOO is equivalent to 0 - FOO. */
1642 gen_int_literal (ax, &value1, (LONGEST) 0, builtin_type_int);
c5aa993b 1643 gen_usual_unary (ax, &value1); /* shouldn't do much */
c906108c
SS
1644 gen_expr (pc, ax, &value2);
1645 gen_usual_unary (ax, &value2);
1646 gen_usual_arithmetic (ax, &value1, &value2);
1647 gen_sub (ax, value, &value1, &value2);
1648 break;
1649
1650 case UNOP_LOGICAL_NOT:
1651 (*pc)++;
1652 gen_expr (pc, ax, value);
1653 gen_logical_not (ax, value);
1654 break;
1655
1656 case UNOP_COMPLEMENT:
1657 (*pc)++;
1658 gen_expr (pc, ax, value);
1659 gen_complement (ax, value);
1660 break;
1661
1662 case UNOP_IND:
1663 (*pc)++;
1664 gen_expr (pc, ax, value);
1665 gen_usual_unary (ax, value);
1666 if (TYPE_CODE (value->type) != TYPE_CODE_PTR)
1667 error ("Argument of unary `*' is not a pointer.");
1668 gen_deref (ax, value);
1669 break;
1670
1671 case UNOP_ADDR:
1672 (*pc)++;
1673 gen_expr (pc, ax, value);
1674 gen_address_of (ax, value);
1675 break;
1676
1677 case UNOP_SIZEOF:
1678 (*pc)++;
1679 /* Notice that gen_sizeof handles its own operand, unlike most
c5aa993b
JM
1680 of the other unary operator functions. This is because we
1681 have to throw away the code we generate. */
c906108c
SS
1682 gen_sizeof (pc, ax, value);
1683 break;
1684
1685 case STRUCTOP_STRUCT:
1686 case STRUCTOP_PTR:
1687 {
1688 int length = (*pc)[1].longconst;
1689 char *name = &(*pc)[2].string;
1690
1691 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
1692 gen_expr (pc, ax, value);
1693 if (op == STRUCTOP_STRUCT)
1694 gen_struct_ref (ax, value, name, ".", "structure or union");
1695 else if (op == STRUCTOP_PTR)
1696 gen_struct_ref (ax, value, name, "->",
1697 "pointer to a structure or union");
1698 else
1699 /* If this `if' chain doesn't handle it, then the case list
c5aa993b 1700 shouldn't mention it, and we shouldn't be here. */
8e65ff28
AC
1701 internal_error (__FILE__, __LINE__,
1702 "gen_expr: unhandled struct case");
c906108c 1703 }
c5aa993b 1704 break;
c906108c
SS
1705
1706 case OP_TYPE:
1707 error ("Attempt to use a type name as an expression.");
1708
1709 default:
1710 error ("Unsupported operator in expression.");
1711 }
1712}
c906108c 1713\f
c5aa993b
JM
1714
1715
c906108c
SS
1716/* Generating bytecode from GDB expressions: driver */
1717
1718/* Given a GDB expression EXPR, produce a string of agent bytecode
1719 which computes its value. Return the agent expression, and set
1720 *VALUE to describe its type, and whether it's an lvalue or rvalue. */
1721struct agent_expr *
fba45db2 1722expr_to_agent (struct expression *expr, struct axs_value *value)
c906108c
SS
1723{
1724 struct cleanup *old_chain = 0;
6426a772 1725 struct agent_expr *ax = new_agent_expr (0);
c906108c
SS
1726 union exp_element *pc;
1727
f23d52e0 1728 old_chain = make_cleanup_free_agent_expr (ax);
c906108c
SS
1729
1730 pc = expr->elts;
1731 trace_kludge = 0;
1732 gen_expr (&pc, ax, value);
1733
1734 /* We have successfully built the agent expr, so cancel the cleanup
1735 request. If we add more cleanups that we always want done, this
1736 will have to get more complicated. */
1737 discard_cleanups (old_chain);
1738 return ax;
1739}
1740
1741
6426a772 1742#if 0 /* not used */
c906108c
SS
1743/* Given a GDB expression EXPR denoting an lvalue in memory, produce a
1744 string of agent bytecode which will leave its address and size on
1745 the top of stack. Return the agent expression.
1746
1747 Not sure this function is useful at all. */
1748struct agent_expr *
fba45db2 1749expr_to_address_and_size (struct expression *expr)
c906108c
SS
1750{
1751 struct axs_value value;
1752 struct agent_expr *ax = expr_to_agent (expr, &value);
1753
1754 /* Complain if the result is not a memory lvalue. */
1755 if (value.kind != axs_lvalue_memory)
1756 {
1757 free_agent_expr (ax);
1758 error ("Expression does not denote an object in memory.");
1759 }
1760
1761 /* Push the object's size on the stack. */
1762 ax_const_l (ax, TYPE_LENGTH (value.type));
1763
1764 return ax;
1765}
6426a772 1766#endif
c906108c
SS
1767
1768/* Given a GDB expression EXPR, return bytecode to trace its value.
1769 The result will use the `trace' and `trace_quick' bytecodes to
1770 record the value of all memory touched by the expression. The
1771 caller can then use the ax_reqs function to discover which
1772 registers it relies upon. */
1773struct agent_expr *
fba45db2 1774gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
c906108c
SS
1775{
1776 struct cleanup *old_chain = 0;
1777 struct agent_expr *ax = new_agent_expr (scope);
1778 union exp_element *pc;
1779 struct axs_value value;
1780
f23d52e0 1781 old_chain = make_cleanup_free_agent_expr (ax);
c906108c
SS
1782
1783 pc = expr->elts;
1784 trace_kludge = 1;
1785 gen_expr (&pc, ax, &value);
1786
1787 /* Make sure we record the final object, and get rid of it. */
1788 gen_traced_pop (ax, &value);
1789
1790 /* Oh, and terminate. */
1791 ax_simple (ax, aop_end);
1792
1793 /* We have successfully built the agent expr, so cancel the cleanup
1794 request. If we add more cleanups that we always want done, this
1795 will have to get more complicated. */
1796 discard_cleanups (old_chain);
1797 return ax;
1798}
c906108c
SS
1799
1800static void
fba45db2 1801agent_command (char *exp, int from_tty)
c906108c
SS
1802{
1803 struct cleanup *old_chain = 0;
1804 struct expression *expr;
1805 struct agent_expr *agent;
6426a772 1806 struct frame_info *fi = get_current_frame (); /* need current scope */
c906108c
SS
1807
1808 /* We don't deal with overlay debugging at the moment. We need to
1809 think more carefully about this. If you copy this code into
1810 another command, change the error message; the user shouldn't
1811 have to know anything about agent expressions. */
1812 if (overlay_debugging)
1813 error ("GDB can't do agent expression translation with overlays.");
1814
1815 if (exp == 0)
1816 error_no_arg ("expression to translate");
c5aa993b 1817
c906108c 1818 expr = parse_expression (exp);
c13c43fd 1819 old_chain = make_cleanup (free_current_contents, &expr);
bdd78e62 1820 agent = gen_trace_for_expr (get_frame_pc (fi), expr);
f23d52e0 1821 make_cleanup_free_agent_expr (agent);
c906108c 1822 ax_print (gdb_stdout, agent);
085dd6e6
JM
1823
1824 /* It would be nice to call ax_reqs here to gather some general info
1825 about the expression, and then print out the result. */
c906108c
SS
1826
1827 do_cleanups (old_chain);
1828 dont_repeat ();
1829}
c906108c 1830\f
c5aa993b 1831
c906108c
SS
1832/* Initialization code. */
1833
a14ed312 1834void _initialize_ax_gdb (void);
c906108c 1835void
fba45db2 1836_initialize_ax_gdb (void)
c906108c 1837{
c906108c
SS
1838 add_cmd ("agent", class_maintenance, agent_command,
1839 "Translate an expression into remote agent bytecode.",
1840 &maintenancelist);
1841}
This page took 0.331483 seconds and 4 git commands to generate.