* mips-tdep.c (mips32_scan_prologue): Reset frame_offset to zero
[deliverable/binutils-gdb.git] / gdb / ax-gdb.c
CommitLineData
1bac305b
AC
1/* GDB-specific functions for operating on agent expressions.
2
28e7fd62 3 Copyright (C) 1998-2013 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
c906108c
SS
20#include "defs.h"
21#include "symtab.h"
22#include "symfile.h"
23#include "gdbtypes.h"
b97aedf3 24#include "language.h"
c906108c
SS
25#include "value.h"
26#include "expression.h"
27#include "command.h"
28#include "gdbcmd.h"
29#include "frame.h"
30#include "target.h"
31#include "ax.h"
32#include "ax-gdb.h"
309367d4 33#include "gdb_string.h"
fe898f56 34#include "block.h"
7b83296f 35#include "regcache.h"
029a67e4 36#include "user-regs.h"
f7c79c41 37#include "language.h"
6c228b9c 38#include "dictionary.h"
00bf0b85 39#include "breakpoint.h"
f61e138d 40#include "tracepoint.h"
b6e7192f 41#include "cp-support.h"
6710bf39 42#include "arch-utils.h"
d3ce09f5 43#include "cli/cli-utils.h"
34b536a8 44#include "linespec.h"
c906108c 45
3065dfb6
SS
46#include "valprint.h"
47#include "c-lang.h"
48
d3ce09f5
SS
49#include "format.h"
50
6426a772
JM
51/* To make sense of this file, you should read doc/agentexpr.texi.
52 Then look at the types and enums in ax-gdb.h. For the code itself,
53 look at gen_expr, towards the bottom; that's the main function that
54 looks at the GDB expressions and calls everything else to generate
55 code.
c906108c
SS
56
57 I'm beginning to wonder whether it wouldn't be nicer to internally
58 generate trees, with types, and then spit out the bytecode in
59 linear form afterwards; we could generate fewer `swap', `ext', and
60 `zero_ext' bytecodes that way; it would make good constant folding
61 easier, too. But at the moment, I think we should be willing to
62 pay for the simplicity of this code with less-than-optimal bytecode
63 strings.
64
c5aa993b
JM
65 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
66\f
c906108c
SS
67
68
0e2de366 69/* Prototypes for local functions. */
c906108c
SS
70
71/* There's a standard order to the arguments of these functions:
72 union exp_element ** --- pointer into expression
73 struct agent_expr * --- agent expression buffer to generate code into
74 struct axs_value * --- describes value left on top of stack */
c5aa993b 75
a14ed312
KB
76static struct value *const_var_ref (struct symbol *var);
77static struct value *const_expr (union exp_element **pc);
78static struct value *maybe_const_expr (union exp_element **pc);
79
3e43a32a
MS
80static void gen_traced_pop (struct gdbarch *, struct agent_expr *,
81 struct axs_value *);
a14ed312
KB
82
83static void gen_sign_extend (struct agent_expr *, struct type *);
84static void gen_extend (struct agent_expr *, struct type *);
85static void gen_fetch (struct agent_expr *, struct type *);
86static void gen_left_shift (struct agent_expr *, int);
87
88
f7c79c41
UW
89static void gen_frame_args_address (struct gdbarch *, struct agent_expr *);
90static void gen_frame_locals_address (struct gdbarch *, struct agent_expr *);
a14ed312
KB
91static void gen_offset (struct agent_expr *ax, int offset);
92static void gen_sym_offset (struct agent_expr *, struct symbol *);
f7c79c41 93static void gen_var_ref (struct gdbarch *, struct agent_expr *ax,
a14ed312
KB
94 struct axs_value *value, struct symbol *var);
95
96
97static void gen_int_literal (struct agent_expr *ax,
98 struct axs_value *value,
99 LONGEST k, struct type *type);
100
f7c79c41
UW
101static void gen_usual_unary (struct expression *exp, struct agent_expr *ax,
102 struct axs_value *value);
a14ed312
KB
103static int type_wider_than (struct type *type1, struct type *type2);
104static struct type *max_type (struct type *type1, struct type *type2);
105static void gen_conversion (struct agent_expr *ax,
106 struct type *from, struct type *to);
107static int is_nontrivial_conversion (struct type *from, struct type *to);
f7c79c41
UW
108static void gen_usual_arithmetic (struct expression *exp,
109 struct agent_expr *ax,
a14ed312
KB
110 struct axs_value *value1,
111 struct axs_value *value2);
f7c79c41
UW
112static void gen_integral_promotions (struct expression *exp,
113 struct agent_expr *ax,
a14ed312
KB
114 struct axs_value *value);
115static void gen_cast (struct agent_expr *ax,
116 struct axs_value *value, struct type *type);
117static void gen_scale (struct agent_expr *ax,
118 enum agent_op op, struct type *type);
f7c79c41
UW
119static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
120 struct axs_value *value1, struct axs_value *value2);
121static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
122 struct axs_value *value1, struct axs_value *value2);
123static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
124 struct axs_value *value1, struct axs_value *value2,
125 struct type *result_type);
a14ed312
KB
126static void gen_binop (struct agent_expr *ax,
127 struct axs_value *value,
128 struct axs_value *value1,
129 struct axs_value *value2,
130 enum agent_op op,
131 enum agent_op op_unsigned, int may_carry, char *name);
f7c79c41
UW
132static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
133 struct type *result_type);
a14ed312
KB
134static void gen_complement (struct agent_expr *ax, struct axs_value *value);
135static void gen_deref (struct agent_expr *, struct axs_value *);
136static void gen_address_of (struct agent_expr *, struct axs_value *);
505e835d 137static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
a14ed312
KB
138 struct axs_value *value,
139 struct type *type, int start, int end);
b6e7192f
SS
140static void gen_primitive_field (struct expression *exp,
141 struct agent_expr *ax,
142 struct axs_value *value,
143 int offset, int fieldno, struct type *type);
144static int gen_struct_ref_recursive (struct expression *exp,
145 struct agent_expr *ax,
146 struct axs_value *value,
147 char *field, int offset,
148 struct type *type);
505e835d 149static void gen_struct_ref (struct expression *exp, struct agent_expr *ax,
a14ed312
KB
150 struct axs_value *value,
151 char *field,
152 char *operator_name, char *operand_name);
400c6af0 153static void gen_static_field (struct gdbarch *gdbarch,
b6e7192f
SS
154 struct agent_expr *ax, struct axs_value *value,
155 struct type *type, int fieldno);
f7c79c41 156static void gen_repeat (struct expression *exp, union exp_element **pc,
a14ed312 157 struct agent_expr *ax, struct axs_value *value);
f7c79c41
UW
158static void gen_sizeof (struct expression *exp, union exp_element **pc,
159 struct agent_expr *ax, struct axs_value *value,
160 struct type *size_type);
f61e138d
SS
161static void gen_expr_binop_rest (struct expression *exp,
162 enum exp_opcode op, union exp_element **pc,
163 struct agent_expr *ax,
164 struct axs_value *value,
165 struct axs_value *value1,
166 struct axs_value *value2);
c5aa993b 167
a14ed312 168static void agent_command (char *exp, int from_tty);
c906108c 169\f
c5aa993b 170
c906108c
SS
171/* Detecting constant expressions. */
172
173/* If the variable reference at *PC is a constant, return its value.
174 Otherwise, return zero.
175
176 Hey, Wally! How can a variable reference be a constant?
177
178 Well, Beav, this function really handles the OP_VAR_VALUE operator,
179 not specifically variable references. GDB uses OP_VAR_VALUE to
180 refer to any kind of symbolic reference: function names, enum
181 elements, and goto labels are all handled through the OP_VAR_VALUE
182 operator, even though they're constants. It makes sense given the
183 situation.
184
185 Gee, Wally, don'cha wonder sometimes if data representations that
186 subvert commonly accepted definitions of terms in favor of heavily
187 context-specific interpretations are really just a tool of the
188 programming hegemony to preserve their power and exclude the
189 proletariat? */
190
191static struct value *
fba45db2 192const_var_ref (struct symbol *var)
c906108c
SS
193{
194 struct type *type = SYMBOL_TYPE (var);
195
196 switch (SYMBOL_CLASS (var))
197 {
198 case LOC_CONST:
199 return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
200
201 case LOC_LABEL:
4478b372 202 return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
c906108c
SS
203
204 default:
205 return 0;
206 }
207}
208
209
210/* If the expression starting at *PC has a constant value, return it.
211 Otherwise, return zero. If we return a value, then *PC will be
212 advanced to the end of it. If we return zero, *PC could be
213 anywhere. */
214static struct value *
fba45db2 215const_expr (union exp_element **pc)
c906108c
SS
216{
217 enum exp_opcode op = (*pc)->opcode;
218 struct value *v1;
219
220 switch (op)
221 {
222 case OP_LONG:
223 {
224 struct type *type = (*pc)[1].type;
225 LONGEST k = (*pc)[2].longconst;
5b4ee69b 226
c906108c
SS
227 (*pc) += 4;
228 return value_from_longest (type, k);
229 }
230
231 case OP_VAR_VALUE:
232 {
233 struct value *v = const_var_ref ((*pc)[2].symbol);
5b4ee69b 234
c906108c
SS
235 (*pc) += 4;
236 return v;
237 }
238
c5aa993b 239 /* We could add more operators in here. */
c906108c
SS
240
241 case UNOP_NEG:
242 (*pc)++;
243 v1 = const_expr (pc);
244 if (v1)
245 return value_neg (v1);
246 else
247 return 0;
248
249 default:
250 return 0;
251 }
252}
253
254
255/* Like const_expr, but guarantee also that *PC is undisturbed if the
256 expression is not constant. */
257static struct value *
fba45db2 258maybe_const_expr (union exp_element **pc)
c906108c
SS
259{
260 union exp_element *tentative_pc = *pc;
261 struct value *v = const_expr (&tentative_pc);
262
263 /* If we got a value, then update the real PC. */
264 if (v)
265 *pc = tentative_pc;
c5aa993b 266
c906108c
SS
267 return v;
268}
c906108c 269\f
c5aa993b 270
c906108c
SS
271/* Generating bytecode from GDB expressions: general assumptions */
272
273/* Here are a few general assumptions made throughout the code; if you
274 want to make a change that contradicts one of these, then you'd
275 better scan things pretty thoroughly.
276
277 - We assume that all values occupy one stack element. For example,
c5aa993b
JM
278 sometimes we'll swap to get at the left argument to a binary
279 operator. If we decide that void values should occupy no stack
280 elements, or that synthetic arrays (whose size is determined at
281 run time, created by the `@' operator) should occupy two stack
282 elements (address and length), then this will cause trouble.
c906108c
SS
283
284 - We assume the stack elements are infinitely wide, and that we
c5aa993b
JM
285 don't have to worry what happens if the user requests an
286 operation that is wider than the actual interpreter's stack.
287 That is, it's up to the interpreter to handle directly all the
288 integer widths the user has access to. (Woe betide the language
289 with bignums!)
c906108c
SS
290
291 - We don't support side effects. Thus, we don't have to worry about
c5aa993b 292 GCC's generalized lvalues, function calls, etc.
c906108c
SS
293
294 - We don't support floating point. Many places where we switch on
c5aa993b
JM
295 some type don't bother to include cases for floating point; there
296 may be even more subtle ways this assumption exists. For
297 example, the arguments to % must be integers.
c906108c
SS
298
299 - We assume all subexpressions have a static, unchanging type. If
c5aa993b
JM
300 we tried to support convenience variables, this would be a
301 problem.
c906108c
SS
302
303 - All values on the stack should always be fully zero- or
c5aa993b
JM
304 sign-extended.
305
306 (I wasn't sure whether to choose this or its opposite --- that
307 only addresses are assumed extended --- but it turns out that
308 neither convention completely eliminates spurious extend
309 operations (if everything is always extended, then you have to
310 extend after add, because it could overflow; if nothing is
311 extended, then you end up producing extends whenever you change
312 sizes), and this is simpler.) */
c906108c 313\f
c5aa993b 314
c906108c
SS
315/* Generating bytecode from GDB expressions: the `trace' kludge */
316
317/* The compiler in this file is a general-purpose mechanism for
318 translating GDB expressions into bytecode. One ought to be able to
319 find a million and one uses for it.
320
321 However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake
322 of expediency. Let he who is without sin cast the first stone.
323
324 For the data tracing facility, we need to insert `trace' bytecodes
325 before each data fetch; this records all the memory that the
326 expression touches in the course of evaluation, so that memory will
327 be available when the user later tries to evaluate the expression
328 in GDB.
329
330 This should be done (I think) in a post-processing pass, that walks
331 an arbitrary agent expression and inserts `trace' operations at the
332 appropriate points. But it's much faster to just hack them
333 directly into the code. And since we're in a crunch, that's what
334 I've done.
335
336 Setting the flag trace_kludge to non-zero enables the code that
337 emits the trace bytecodes at the appropriate points. */
08922a10 338int trace_kludge;
c906108c 339
3065dfb6
SS
340/* Inspired by trace_kludge, this indicates that pointers to chars
341 should get an added tracenz bytecode to record nonzero bytes, up to
342 a length that is the value of trace_string_kludge. */
343int trace_string_kludge;
344
400c6af0
SS
345/* Scan for all static fields in the given class, including any base
346 classes, and generate tracing bytecodes for each. */
347
348static void
349gen_trace_static_fields (struct gdbarch *gdbarch,
350 struct agent_expr *ax,
351 struct type *type)
352{
353 int i, nbases = TYPE_N_BASECLASSES (type);
354 struct axs_value value;
355
356 CHECK_TYPEDEF (type);
357
358 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
359 {
360 if (field_is_static (&TYPE_FIELD (type, i)))
361 {
362 gen_static_field (gdbarch, ax, &value, type, i);
363 if (value.optimized_out)
364 continue;
365 switch (value.kind)
366 {
367 case axs_lvalue_memory:
368 {
744a8059
SP
369 /* Initialize the TYPE_LENGTH if it is a typedef. */
370 check_typedef (value.type);
371 ax_const_l (ax, TYPE_LENGTH (value.type));
400c6af0
SS
372 ax_simple (ax, aop_trace);
373 }
374 break;
375
376 case axs_lvalue_register:
35c9c7ba
SS
377 /* We don't actually need the register's value to be pushed,
378 just note that we need it to be collected. */
379 ax_reg_mask (ax, value.u.reg);
400c6af0
SS
380
381 default:
382 break;
383 }
384 }
385 }
386
387 /* Now scan through base classes recursively. */
388 for (i = 0; i < nbases; i++)
389 {
390 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
391
392 gen_trace_static_fields (gdbarch, ax, basetype);
393 }
394}
395
c906108c
SS
396/* Trace the lvalue on the stack, if it needs it. In either case, pop
397 the value. Useful on the left side of a comma, and at the end of
398 an expression being used for tracing. */
399static void
400c6af0
SS
400gen_traced_pop (struct gdbarch *gdbarch,
401 struct agent_expr *ax, struct axs_value *value)
c906108c 402{
3065dfb6
SS
403 int string_trace = 0;
404 if (trace_string_kludge
405 && TYPE_CODE (value->type) == TYPE_CODE_PTR
406 && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
407 's'))
408 string_trace = 1;
409
c906108c
SS
410 if (trace_kludge)
411 switch (value->kind)
412 {
413 case axs_rvalue:
3065dfb6
SS
414 if (string_trace)
415 {
416 ax_const_l (ax, trace_string_kludge);
417 ax_simple (ax, aop_tracenz);
418 }
419 else
420 /* We don't trace rvalues, just the lvalues necessary to
421 produce them. So just dispose of this value. */
422 ax_simple (ax, aop_pop);
c906108c
SS
423 break;
424
425 case axs_lvalue_memory:
426 {
3065dfb6
SS
427 if (string_trace)
428 ax_simple (ax, aop_dup);
429
744a8059
SP
430 /* Initialize the TYPE_LENGTH if it is a typedef. */
431 check_typedef (value->type);
432
c906108c
SS
433 /* There's no point in trying to use a trace_quick bytecode
434 here, since "trace_quick SIZE pop" is three bytes, whereas
435 "const8 SIZE trace" is also three bytes, does the same
436 thing, and the simplest code which generates that will also
437 work correctly for objects with large sizes. */
744a8059 438 ax_const_l (ax, TYPE_LENGTH (value->type));
c906108c 439 ax_simple (ax, aop_trace);
3065dfb6
SS
440
441 if (string_trace)
442 {
443 ax_simple (ax, aop_ref32);
444 ax_const_l (ax, trace_string_kludge);
445 ax_simple (ax, aop_tracenz);
446 }
c906108c 447 }
c5aa993b 448 break;
c906108c
SS
449
450 case axs_lvalue_register:
35c9c7ba
SS
451 /* We don't actually need the register's value to be on the
452 stack, and the target will get heartburn if the register is
453 larger than will fit in a stack, so just mark it for
454 collection and be done with it. */
455 ax_reg_mask (ax, value->u.reg);
3065dfb6
SS
456
457 /* But if the register points to a string, assume the value
458 will fit on the stack and push it anyway. */
459 if (string_trace)
460 {
461 ax_reg (ax, value->u.reg);
462 ax_const_l (ax, trace_string_kludge);
463 ax_simple (ax, aop_tracenz);
464 }
c906108c
SS
465 break;
466 }
467 else
468 /* If we're not tracing, just pop the value. */
469 ax_simple (ax, aop_pop);
400c6af0
SS
470
471 /* To trace C++ classes with static fields stored elsewhere. */
472 if (trace_kludge
473 && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
474 || TYPE_CODE (value->type) == TYPE_CODE_UNION))
475 gen_trace_static_fields (gdbarch, ax, value->type);
c906108c 476}
c5aa993b 477\f
c906108c
SS
478
479
c906108c
SS
480/* Generating bytecode from GDB expressions: helper functions */
481
482/* Assume that the lower bits of the top of the stack is a value of
483 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
484static void
fba45db2 485gen_sign_extend (struct agent_expr *ax, struct type *type)
c906108c
SS
486{
487 /* Do we need to sign-extend this? */
c5aa993b 488 if (!TYPE_UNSIGNED (type))
0004e5a2 489 ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
c906108c
SS
490}
491
492
493/* Assume the lower bits of the top of the stack hold a value of type
494 TYPE, and the upper bits are garbage. Sign-extend or truncate as
495 needed. */
496static void
fba45db2 497gen_extend (struct agent_expr *ax, struct type *type)
c906108c 498{
0004e5a2 499 int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
5b4ee69b 500
c906108c
SS
501 /* I just had to. */
502 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
503}
504
505
506/* Assume that the top of the stack contains a value of type "pointer
507 to TYPE"; generate code to fetch its value. Note that TYPE is the
508 target type, not the pointer type. */
509static void
fba45db2 510gen_fetch (struct agent_expr *ax, struct type *type)
c906108c
SS
511{
512 if (trace_kludge)
513 {
514 /* Record the area of memory we're about to fetch. */
515 ax_trace_quick (ax, TYPE_LENGTH (type));
516 }
517
af381b8c
JB
518 if (TYPE_CODE (type) == TYPE_CODE_RANGE)
519 type = TYPE_TARGET_TYPE (type);
520
0004e5a2 521 switch (TYPE_CODE (type))
c906108c
SS
522 {
523 case TYPE_CODE_PTR:
b97aedf3 524 case TYPE_CODE_REF:
c906108c
SS
525 case TYPE_CODE_ENUM:
526 case TYPE_CODE_INT:
527 case TYPE_CODE_CHAR:
3b11a015 528 case TYPE_CODE_BOOL:
c906108c
SS
529 /* It's a scalar value, so we know how to dereference it. How
530 many bytes long is it? */
0004e5a2 531 switch (TYPE_LENGTH (type))
c906108c 532 {
c5aa993b
JM
533 case 8 / TARGET_CHAR_BIT:
534 ax_simple (ax, aop_ref8);
535 break;
536 case 16 / TARGET_CHAR_BIT:
537 ax_simple (ax, aop_ref16);
538 break;
539 case 32 / TARGET_CHAR_BIT:
540 ax_simple (ax, aop_ref32);
541 break;
542 case 64 / TARGET_CHAR_BIT:
543 ax_simple (ax, aop_ref64);
544 break;
c906108c
SS
545
546 /* Either our caller shouldn't have asked us to dereference
547 that pointer (other code's fault), or we're not
548 implementing something we should be (this code's fault).
549 In any case, it's a bug the user shouldn't see. */
550 default:
8e65ff28 551 internal_error (__FILE__, __LINE__,
3d263c1d 552 _("gen_fetch: strange size"));
c906108c
SS
553 }
554
555 gen_sign_extend (ax, type);
556 break;
557
558 default:
52323be9
LM
559 /* Our caller requested us to dereference a pointer from an unsupported
560 type. Error out and give callers a chance to handle the failure
561 gracefully. */
562 error (_("gen_fetch: Unsupported type code `%s'."),
563 TYPE_NAME (type));
c906108c
SS
564 }
565}
566
567
568/* Generate code to left shift the top of the stack by DISTANCE bits, or
569 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
570 unsigned (logical) right shifts. */
571static void
fba45db2 572gen_left_shift (struct agent_expr *ax, int distance)
c906108c
SS
573{
574 if (distance > 0)
575 {
576 ax_const_l (ax, distance);
577 ax_simple (ax, aop_lsh);
578 }
579 else if (distance < 0)
580 {
581 ax_const_l (ax, -distance);
582 ax_simple (ax, aop_rsh_unsigned);
583 }
584}
c5aa993b 585\f
c906108c
SS
586
587
c906108c
SS
588/* Generating bytecode from GDB expressions: symbol references */
589
590/* Generate code to push the base address of the argument portion of
591 the top stack frame. */
592static void
f7c79c41 593gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
c906108c 594{
39d4ef09
AC
595 int frame_reg;
596 LONGEST frame_offset;
c906108c 597
f7c79c41 598 gdbarch_virtual_frame_pointer (gdbarch,
c7bb205c 599 ax->scope, &frame_reg, &frame_offset);
c5aa993b 600 ax_reg (ax, frame_reg);
c906108c
SS
601 gen_offset (ax, frame_offset);
602}
603
604
605/* Generate code to push the base address of the locals portion of the
606 top stack frame. */
607static void
f7c79c41 608gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
c906108c 609{
39d4ef09
AC
610 int frame_reg;
611 LONGEST frame_offset;
c906108c 612
f7c79c41 613 gdbarch_virtual_frame_pointer (gdbarch,
c7bb205c 614 ax->scope, &frame_reg, &frame_offset);
c5aa993b 615 ax_reg (ax, frame_reg);
c906108c
SS
616 gen_offset (ax, frame_offset);
617}
618
619
620/* Generate code to add OFFSET to the top of the stack. Try to
621 generate short and readable code. We use this for getting to
622 variables on the stack, and structure members. If we were
623 programming in ML, it would be clearer why these are the same
624 thing. */
625static void
fba45db2 626gen_offset (struct agent_expr *ax, int offset)
c906108c
SS
627{
628 /* It would suffice to simply push the offset and add it, but this
629 makes it easier to read positive and negative offsets in the
630 bytecode. */
631 if (offset > 0)
632 {
633 ax_const_l (ax, offset);
634 ax_simple (ax, aop_add);
635 }
636 else if (offset < 0)
637 {
638 ax_const_l (ax, -offset);
639 ax_simple (ax, aop_sub);
640 }
641}
642
643
644/* In many cases, a symbol's value is the offset from some other
645 address (stack frame, base register, etc.) Generate code to add
646 VAR's value to the top of the stack. */
647static void
fba45db2 648gen_sym_offset (struct agent_expr *ax, struct symbol *var)
c906108c
SS
649{
650 gen_offset (ax, SYMBOL_VALUE (var));
651}
652
653
654/* Generate code for a variable reference to AX. The variable is the
655 symbol VAR. Set VALUE to describe the result. */
656
657static void
f7c79c41
UW
658gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
659 struct axs_value *value, struct symbol *var)
c906108c 660{
0e2de366 661 /* Dereference any typedefs. */
c906108c 662 value->type = check_typedef (SYMBOL_TYPE (var));
400c6af0 663 value->optimized_out = 0;
c906108c
SS
664
665 /* I'm imitating the code in read_var_value. */
666 switch (SYMBOL_CLASS (var))
667 {
668 case LOC_CONST: /* A constant, like an enum value. */
669 ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
670 value->kind = axs_rvalue;
671 break;
672
673 case LOC_LABEL: /* A goto label, being used as a value. */
674 ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
675 value->kind = axs_rvalue;
676 break;
677
678 case LOC_CONST_BYTES:
8e65ff28 679 internal_error (__FILE__, __LINE__,
3e43a32a
MS
680 _("gen_var_ref: LOC_CONST_BYTES "
681 "symbols are not supported"));
c906108c
SS
682
683 /* Variable at a fixed location in memory. Easy. */
684 case LOC_STATIC:
685 /* Push the address of the variable. */
686 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
687 value->kind = axs_lvalue_memory;
688 break;
689
690 case LOC_ARG: /* var lives in argument area of frame */
f7c79c41 691 gen_frame_args_address (gdbarch, ax);
c906108c
SS
692 gen_sym_offset (ax, var);
693 value->kind = axs_lvalue_memory;
694 break;
695
696 case LOC_REF_ARG: /* As above, but the frame slot really
697 holds the address of the variable. */
f7c79c41 698 gen_frame_args_address (gdbarch, ax);
c906108c
SS
699 gen_sym_offset (ax, var);
700 /* Don't assume any particular pointer size. */
f7c79c41 701 gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
c906108c
SS
702 value->kind = axs_lvalue_memory;
703 break;
704
705 case LOC_LOCAL: /* var lives in locals area of frame */
f7c79c41 706 gen_frame_locals_address (gdbarch, ax);
c906108c
SS
707 gen_sym_offset (ax, var);
708 value->kind = axs_lvalue_memory;
709 break;
710
c906108c 711 case LOC_TYPEDEF:
3d263c1d 712 error (_("Cannot compute value of typedef `%s'."),
de5ad195 713 SYMBOL_PRINT_NAME (var));
c906108c
SS
714 break;
715
716 case LOC_BLOCK:
717 ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
718 value->kind = axs_rvalue;
719 break;
720
721 case LOC_REGISTER:
c906108c
SS
722 /* Don't generate any code at all; in the process of treating
723 this as an lvalue or rvalue, the caller will generate the
724 right code. */
725 value->kind = axs_lvalue_register;
768a979c 726 value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
c906108c
SS
727 break;
728
729 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
2a2d4dc3
AS
730 register, not on the stack. Simpler than LOC_REGISTER
731 because it's just like any other case where the thing
732 has a real address. */
c906108c 733 case LOC_REGPARM_ADDR:
768a979c 734 ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
c906108c
SS
735 value->kind = axs_lvalue_memory;
736 break;
737
738 case LOC_UNRESOLVED:
739 {
c5aa993b 740 struct minimal_symbol *msym
3567439c 741 = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
5b4ee69b 742
c5aa993b 743 if (!msym)
3d263c1d 744 error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
c5aa993b 745
c906108c
SS
746 /* Push the address of the variable. */
747 ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym));
748 value->kind = axs_lvalue_memory;
749 }
c5aa993b 750 break;
c906108c 751
a55cc764 752 case LOC_COMPUTED:
a67af2b9 753 /* FIXME: cagney/2004-01-26: It should be possible to
768a979c 754 unconditionally call the SYMBOL_COMPUTED_OPS method when available.
d3efc286 755 Unfortunately DWARF 2 stores the frame-base (instead of the
a67af2b9
AC
756 function) location in a function's symbol. Oops! For the
757 moment enable this when/where applicable. */
505e835d 758 SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
a55cc764
DJ
759 break;
760
c906108c 761 case LOC_OPTIMIZED_OUT:
400c6af0
SS
762 /* Flag this, but don't say anything; leave it up to callers to
763 warn the user. */
764 value->optimized_out = 1;
c906108c
SS
765 break;
766
767 default:
3d263c1d 768 error (_("Cannot find value of botched symbol `%s'."),
de5ad195 769 SYMBOL_PRINT_NAME (var));
c906108c
SS
770 break;
771 }
772}
c5aa993b 773\f
c906108c
SS
774
775
c906108c
SS
776/* Generating bytecode from GDB expressions: literals */
777
778static void
fba45db2
KB
779gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
780 struct type *type)
c906108c
SS
781{
782 ax_const_l (ax, k);
783 value->kind = axs_rvalue;
648027cc 784 value->type = check_typedef (type);
c906108c 785}
c5aa993b 786\f
c906108c
SS
787
788
c906108c
SS
789/* Generating bytecode from GDB expressions: unary conversions, casts */
790
791/* Take what's on the top of the stack (as described by VALUE), and
792 try to make an rvalue out of it. Signal an error if we can't do
793 that. */
55aa24fb 794void
fba45db2 795require_rvalue (struct agent_expr *ax, struct axs_value *value)
c906108c 796{
3a96536b
SS
797 /* Only deal with scalars, structs and such may be too large
798 to fit in a stack entry. */
799 value->type = check_typedef (value->type);
800 if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
801 || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
802 || TYPE_CODE (value->type) == TYPE_CODE_UNION
803 || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
1c40aa62 804 error (_("Value not scalar: cannot be an rvalue."));
3a96536b 805
c906108c
SS
806 switch (value->kind)
807 {
808 case axs_rvalue:
809 /* It's already an rvalue. */
810 break;
811
812 case axs_lvalue_memory:
813 /* The top of stack is the address of the object. Dereference. */
814 gen_fetch (ax, value->type);
815 break;
816
817 case axs_lvalue_register:
818 /* There's nothing on the stack, but value->u.reg is the
819 register number containing the value.
820
c5aa993b
JM
821 When we add floating-point support, this is going to have to
822 change. What about SPARC register pairs, for example? */
c906108c
SS
823 ax_reg (ax, value->u.reg);
824 gen_extend (ax, value->type);
825 break;
826 }
827
828 value->kind = axs_rvalue;
829}
830
831
832/* Assume the top of the stack is described by VALUE, and perform the
833 usual unary conversions. This is motivated by ANSI 6.2.2, but of
834 course GDB expressions are not ANSI; they're the mishmash union of
835 a bunch of languages. Rah.
836
837 NOTE! This function promises to produce an rvalue only when the
838 incoming value is of an appropriate type. In other words, the
839 consumer of the value this function produces may assume the value
840 is an rvalue only after checking its type.
841
842 The immediate issue is that if the user tries to use a structure or
843 union as an operand of, say, the `+' operator, we don't want to try
844 to convert that structure to an rvalue; require_rvalue will bomb on
845 structs and unions. Rather, we want to simply pass the struct
846 lvalue through unchanged, and let `+' raise an error. */
847
848static void
f7c79c41
UW
849gen_usual_unary (struct expression *exp, struct agent_expr *ax,
850 struct axs_value *value)
c906108c
SS
851{
852 /* We don't have to generate any code for the usual integral
853 conversions, since values are always represented as full-width on
854 the stack. Should we tweak the type? */
855
856 /* Some types require special handling. */
0004e5a2 857 switch (TYPE_CODE (value->type))
c906108c
SS
858 {
859 /* Functions get converted to a pointer to the function. */
860 case TYPE_CODE_FUNC:
861 value->type = lookup_pointer_type (value->type);
862 value->kind = axs_rvalue; /* Should always be true, but just in case. */
863 break;
864
865 /* Arrays get converted to a pointer to their first element, and
c5aa993b 866 are no longer an lvalue. */
c906108c
SS
867 case TYPE_CODE_ARRAY:
868 {
869 struct type *elements = TYPE_TARGET_TYPE (value->type);
5b4ee69b 870
c906108c
SS
871 value->type = lookup_pointer_type (elements);
872 value->kind = axs_rvalue;
873 /* We don't need to generate any code; the address of the array
874 is also the address of its first element. */
875 }
c5aa993b 876 break;
c906108c 877
c5aa993b
JM
878 /* Don't try to convert structures and unions to rvalues. Let the
879 consumer signal an error. */
c906108c
SS
880 case TYPE_CODE_STRUCT:
881 case TYPE_CODE_UNION:
882 return;
c906108c
SS
883 }
884
885 /* If the value is an lvalue, dereference it. */
886 require_rvalue (ax, value);
887}
888
889
890/* Return non-zero iff the type TYPE1 is considered "wider" than the
891 type TYPE2, according to the rules described in gen_usual_arithmetic. */
892static int
fba45db2 893type_wider_than (struct type *type1, struct type *type2)
c906108c
SS
894{
895 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
896 || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
897 && TYPE_UNSIGNED (type1)
c5aa993b 898 && !TYPE_UNSIGNED (type2)));
c906108c
SS
899}
900
901
902/* Return the "wider" of the two types TYPE1 and TYPE2. */
903static struct type *
fba45db2 904max_type (struct type *type1, struct type *type2)
c906108c
SS
905{
906 return type_wider_than (type1, type2) ? type1 : type2;
907}
908
909
910/* Generate code to convert a scalar value of type FROM to type TO. */
911static void
fba45db2 912gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
c906108c
SS
913{
914 /* Perhaps there is a more graceful way to state these rules. */
915
916 /* If we're converting to a narrower type, then we need to clear out
917 the upper bits. */
918 if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
919 gen_extend (ax, from);
920
921 /* If the two values have equal width, but different signednesses,
922 then we need to extend. */
923 else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
924 {
925 if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
926 gen_extend (ax, to);
927 }
928
929 /* If we're converting to a wider type, and becoming unsigned, then
930 we need to zero out any possible sign bits. */
931 else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
932 {
933 if (TYPE_UNSIGNED (to))
934 gen_extend (ax, to);
935 }
936}
937
938
939/* Return non-zero iff the type FROM will require any bytecodes to be
940 emitted to be converted to the type TO. */
941static int
fba45db2 942is_nontrivial_conversion (struct type *from, struct type *to)
c906108c 943{
35c9c7ba 944 struct agent_expr *ax = new_agent_expr (NULL, 0);
c906108c
SS
945 int nontrivial;
946
947 /* Actually generate the code, and see if anything came out. At the
948 moment, it would be trivial to replicate the code in
949 gen_conversion here, but in the future, when we're supporting
950 floating point and the like, it may not be. Doing things this
951 way allows this function to be independent of the logic in
952 gen_conversion. */
953 gen_conversion (ax, from, to);
954 nontrivial = ax->len > 0;
955 free_agent_expr (ax);
956 return nontrivial;
957}
958
959
960/* Generate code to perform the "usual arithmetic conversions" (ANSI C
961 6.2.1.5) for the two operands of an arithmetic operator. This
962 effectively finds a "least upper bound" type for the two arguments,
963 and promotes each argument to that type. *VALUE1 and *VALUE2
964 describe the values as they are passed in, and as they are left. */
965static void
f7c79c41
UW
966gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
967 struct axs_value *value1, struct axs_value *value2)
c906108c
SS
968{
969 /* Do the usual binary conversions. */
970 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
971 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
972 {
973 /* The ANSI integral promotions seem to work this way: Order the
c5aa993b
JM
974 integer types by size, and then by signedness: an n-bit
975 unsigned type is considered "wider" than an n-bit signed
976 type. Promote to the "wider" of the two types, and always
977 promote at least to int. */
f7c79c41 978 struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
c906108c
SS
979 max_type (value1->type, value2->type));
980
981 /* Deal with value2, on the top of the stack. */
982 gen_conversion (ax, value2->type, target);
983
984 /* Deal with value1, not on the top of the stack. Don't
985 generate the `swap' instructions if we're not actually going
986 to do anything. */
987 if (is_nontrivial_conversion (value1->type, target))
988 {
989 ax_simple (ax, aop_swap);
990 gen_conversion (ax, value1->type, target);
991 ax_simple (ax, aop_swap);
992 }
993
648027cc 994 value1->type = value2->type = check_typedef (target);
c906108c
SS
995 }
996}
997
998
999/* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
1000 the value on the top of the stack, as described by VALUE. Assume
1001 the value has integral type. */
1002static void
f7c79c41
UW
1003gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
1004 struct axs_value *value)
c906108c 1005{
f7c79c41
UW
1006 const struct builtin_type *builtin = builtin_type (exp->gdbarch);
1007
1008 if (!type_wider_than (value->type, builtin->builtin_int))
c906108c 1009 {
f7c79c41
UW
1010 gen_conversion (ax, value->type, builtin->builtin_int);
1011 value->type = builtin->builtin_int;
c906108c 1012 }
f7c79c41 1013 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
c906108c 1014 {
f7c79c41
UW
1015 gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
1016 value->type = builtin->builtin_unsigned_int;
c906108c
SS
1017 }
1018}
1019
1020
1021/* Generate code for a cast to TYPE. */
1022static void
fba45db2 1023gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
c906108c
SS
1024{
1025 /* GCC does allow casts to yield lvalues, so this should be fixed
1026 before merging these changes into the trunk. */
1027 require_rvalue (ax, value);
0e2de366 1028 /* Dereference typedefs. */
c906108c
SS
1029 type = check_typedef (type);
1030
0004e5a2 1031 switch (TYPE_CODE (type))
c906108c
SS
1032 {
1033 case TYPE_CODE_PTR:
b97aedf3 1034 case TYPE_CODE_REF:
c906108c
SS
1035 /* It's implementation-defined, and I'll bet this is what GCC
1036 does. */
1037 break;
1038
1039 case TYPE_CODE_ARRAY:
1040 case TYPE_CODE_STRUCT:
1041 case TYPE_CODE_UNION:
1042 case TYPE_CODE_FUNC:
3d263c1d 1043 error (_("Invalid type cast: intended type must be scalar."));
c906108c
SS
1044
1045 case TYPE_CODE_ENUM:
3b11a015 1046 case TYPE_CODE_BOOL:
c906108c
SS
1047 /* We don't have to worry about the size of the value, because
1048 all our integral values are fully sign-extended, and when
1049 casting pointers we can do anything we like. Is there any
74b35824
JB
1050 way for us to know what GCC actually does with a cast like
1051 this? */
c906108c 1052 break;
c5aa993b 1053
c906108c
SS
1054 case TYPE_CODE_INT:
1055 gen_conversion (ax, value->type, type);
1056 break;
1057
1058 case TYPE_CODE_VOID:
1059 /* We could pop the value, and rely on everyone else to check
c5aa993b
JM
1060 the type and notice that this value doesn't occupy a stack
1061 slot. But for now, leave the value on the stack, and
1062 preserve the "value == stack element" assumption. */
c906108c
SS
1063 break;
1064
1065 default:
3d263c1d 1066 error (_("Casts to requested type are not yet implemented."));
c906108c
SS
1067 }
1068
1069 value->type = type;
1070}
c5aa993b 1071\f
c906108c
SS
1072
1073
c906108c
SS
1074/* Generating bytecode from GDB expressions: arithmetic */
1075
1076/* Scale the integer on the top of the stack by the size of the target
1077 of the pointer type TYPE. */
1078static void
fba45db2 1079gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
c906108c
SS
1080{
1081 struct type *element = TYPE_TARGET_TYPE (type);
1082
0004e5a2 1083 if (TYPE_LENGTH (element) != 1)
c906108c 1084 {
0004e5a2 1085 ax_const_l (ax, TYPE_LENGTH (element));
c906108c
SS
1086 ax_simple (ax, op);
1087 }
1088}
1089
1090
f7c79c41 1091/* Generate code for pointer arithmetic PTR + INT. */
c906108c 1092static void
f7c79c41
UW
1093gen_ptradd (struct agent_expr *ax, struct axs_value *value,
1094 struct axs_value *value1, struct axs_value *value2)
c906108c 1095{
b97aedf3 1096 gdb_assert (pointer_type (value1->type));
f7c79c41 1097 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
c906108c 1098
f7c79c41
UW
1099 gen_scale (ax, aop_mul, value1->type);
1100 ax_simple (ax, aop_add);
1101 gen_extend (ax, value1->type); /* Catch overflow. */
1102 value->type = value1->type;
1103 value->kind = axs_rvalue;
1104}
c906108c 1105
c906108c 1106
f7c79c41
UW
1107/* Generate code for pointer arithmetic PTR - INT. */
1108static void
1109gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
1110 struct axs_value *value1, struct axs_value *value2)
1111{
b97aedf3 1112 gdb_assert (pointer_type (value1->type));
f7c79c41 1113 gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
c906108c 1114
f7c79c41
UW
1115 gen_scale (ax, aop_mul, value1->type);
1116 ax_simple (ax, aop_sub);
1117 gen_extend (ax, value1->type); /* Catch overflow. */
1118 value->type = value1->type;
c906108c
SS
1119 value->kind = axs_rvalue;
1120}
1121
1122
f7c79c41 1123/* Generate code for pointer arithmetic PTR - PTR. */
c906108c 1124static void
f7c79c41
UW
1125gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
1126 struct axs_value *value1, struct axs_value *value2,
1127 struct type *result_type)
c906108c 1128{
b97aedf3
SS
1129 gdb_assert (pointer_type (value1->type));
1130 gdb_assert (pointer_type (value2->type));
c906108c 1131
f7c79c41
UW
1132 if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
1133 != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
ac74f770
MS
1134 error (_("\
1135First argument of `-' is a pointer, but second argument is neither\n\
1136an integer nor a pointer of the same type."));
c906108c 1137
f7c79c41
UW
1138 ax_simple (ax, aop_sub);
1139 gen_scale (ax, aop_div_unsigned, value1->type);
1140 value->type = result_type;
c906108c
SS
1141 value->kind = axs_rvalue;
1142}
1143
3b11a015
SS
1144static void
1145gen_equal (struct agent_expr *ax, struct axs_value *value,
1146 struct axs_value *value1, struct axs_value *value2,
1147 struct type *result_type)
1148{
1149 if (pointer_type (value1->type) || pointer_type (value2->type))
1150 ax_simple (ax, aop_equal);
1151 else
1152 gen_binop (ax, value, value1, value2,
1153 aop_equal, aop_equal, 0, "equal");
1154 value->type = result_type;
1155 value->kind = axs_rvalue;
1156}
1157
1158static void
1159gen_less (struct agent_expr *ax, struct axs_value *value,
1160 struct axs_value *value1, struct axs_value *value2,
1161 struct type *result_type)
1162{
1163 if (pointer_type (value1->type) || pointer_type (value2->type))
1164 ax_simple (ax, aop_less_unsigned);
1165 else
1166 gen_binop (ax, value, value1, value2,
1167 aop_less_signed, aop_less_unsigned, 0, "less than");
1168 value->type = result_type;
1169 value->kind = axs_rvalue;
1170}
f7c79c41 1171
c906108c
SS
1172/* Generate code for a binary operator that doesn't do pointer magic.
1173 We set VALUE to describe the result value; we assume VALUE1 and
1174 VALUE2 describe the two operands, and that they've undergone the
1175 usual binary conversions. MAY_CARRY should be non-zero iff the
1176 result needs to be extended. NAME is the English name of the
1177 operator, used in error messages */
1178static void
fba45db2 1179gen_binop (struct agent_expr *ax, struct axs_value *value,
3e43a32a
MS
1180 struct axs_value *value1, struct axs_value *value2,
1181 enum agent_op op, enum agent_op op_unsigned,
1182 int may_carry, char *name)
c906108c
SS
1183{
1184 /* We only handle INT op INT. */
0004e5a2
DJ
1185 if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
1186 || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
3d263c1d 1187 error (_("Invalid combination of types in %s."), name);
c5aa993b 1188
c906108c
SS
1189 ax_simple (ax,
1190 TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
1191 if (may_carry)
c5aa993b 1192 gen_extend (ax, value1->type); /* catch overflow */
c906108c
SS
1193 value->type = value1->type;
1194 value->kind = axs_rvalue;
1195}
1196
1197
1198static void
f7c79c41
UW
1199gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1200 struct type *result_type)
c906108c
SS
1201{
1202 if (TYPE_CODE (value->type) != TYPE_CODE_INT
1203 && TYPE_CODE (value->type) != TYPE_CODE_PTR)
3d263c1d 1204 error (_("Invalid type of operand to `!'."));
c906108c 1205
c906108c 1206 ax_simple (ax, aop_log_not);
f7c79c41 1207 value->type = result_type;
c906108c
SS
1208}
1209
1210
1211static void
fba45db2 1212gen_complement (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1213{
1214 if (TYPE_CODE (value->type) != TYPE_CODE_INT)
3d263c1d 1215 error (_("Invalid type of operand to `~'."));
c906108c 1216
c906108c
SS
1217 ax_simple (ax, aop_bit_not);
1218 gen_extend (ax, value->type);
1219}
c5aa993b 1220\f
c906108c
SS
1221
1222
c906108c
SS
1223/* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1224
1225/* Dereference the value on the top of the stack. */
1226static void
fba45db2 1227gen_deref (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1228{
1229 /* The caller should check the type, because several operators use
1230 this, and we don't know what error message to generate. */
b97aedf3 1231 if (!pointer_type (value->type))
8e65ff28 1232 internal_error (__FILE__, __LINE__,
3d263c1d 1233 _("gen_deref: expected a pointer"));
c906108c
SS
1234
1235 /* We've got an rvalue now, which is a pointer. We want to yield an
1236 lvalue, whose address is exactly that pointer. So we don't
1237 actually emit any code; we just change the type from "Pointer to
1238 T" to "T", and mark the value as an lvalue in memory. Leave it
1239 to the consumer to actually dereference it. */
1240 value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
b1028c8e
PA
1241 if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
1242 error (_("Attempt to dereference a generic pointer."));
0004e5a2 1243 value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
c906108c
SS
1244 ? axs_rvalue : axs_lvalue_memory);
1245}
1246
1247
1248/* Produce the address of the lvalue on the top of the stack. */
1249static void
fba45db2 1250gen_address_of (struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1251{
1252 /* Special case for taking the address of a function. The ANSI
1253 standard describes this as a special case, too, so this
1254 arrangement is not without motivation. */
0004e5a2 1255 if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
c906108c
SS
1256 /* The value's already an rvalue on the stack, so we just need to
1257 change the type. */
1258 value->type = lookup_pointer_type (value->type);
1259 else
1260 switch (value->kind)
1261 {
1262 case axs_rvalue:
3d263c1d 1263 error (_("Operand of `&' is an rvalue, which has no address."));
c906108c
SS
1264
1265 case axs_lvalue_register:
3d263c1d 1266 error (_("Operand of `&' is in a register, and has no address."));
c906108c
SS
1267
1268 case axs_lvalue_memory:
1269 value->kind = axs_rvalue;
1270 value->type = lookup_pointer_type (value->type);
1271 break;
1272 }
1273}
1274
c906108c
SS
1275/* Generate code to push the value of a bitfield of a structure whose
1276 address is on the top of the stack. START and END give the
1277 starting and one-past-ending *bit* numbers of the field within the
1278 structure. */
1279static void
505e835d
UW
1280gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
1281 struct axs_value *value, struct type *type,
1282 int start, int end)
c906108c
SS
1283{
1284 /* Note that ops[i] fetches 8 << i bits. */
1285 static enum agent_op ops[]
5b4ee69b 1286 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
c906108c
SS
1287 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1288
1289 /* We don't want to touch any byte that the bitfield doesn't
1290 actually occupy; we shouldn't make any accesses we're not
1291 explicitly permitted to. We rely here on the fact that the
1292 bytecode `ref' operators work on unaligned addresses.
1293
1294 It takes some fancy footwork to get the stack to work the way
1295 we'd like. Say we're retrieving a bitfield that requires three
1296 fetches. Initially, the stack just contains the address:
c5aa993b 1297 addr
c906108c 1298 For the first fetch, we duplicate the address
c5aa993b 1299 addr addr
c906108c
SS
1300 then add the byte offset, do the fetch, and shift and mask as
1301 needed, yielding a fragment of the value, properly aligned for
1302 the final bitwise or:
c5aa993b 1303 addr frag1
c906108c 1304 then we swap, and repeat the process:
c5aa993b
JM
1305 frag1 addr --- address on top
1306 frag1 addr addr --- duplicate it
1307 frag1 addr frag2 --- get second fragment
1308 frag1 frag2 addr --- swap again
1309 frag1 frag2 frag3 --- get third fragment
c906108c
SS
1310 Notice that, since the third fragment is the last one, we don't
1311 bother duplicating the address this time. Now we have all the
1312 fragments on the stack, and we can simply `or' them together,
1313 yielding the final value of the bitfield. */
1314
1315 /* The first and one-after-last bits in the field, but rounded down
1316 and up to byte boundaries. */
1317 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
c5aa993b
JM
1318 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1319 / TARGET_CHAR_BIT)
1320 * TARGET_CHAR_BIT);
c906108c
SS
1321
1322 /* current bit offset within the structure */
1323 int offset;
1324
1325 /* The index in ops of the opcode we're considering. */
1326 int op;
1327
1328 /* The number of fragments we generated in the process. Probably
1329 equal to the number of `one' bits in bytesize, but who cares? */
1330 int fragment_count;
1331
0e2de366 1332 /* Dereference any typedefs. */
c906108c
SS
1333 type = check_typedef (type);
1334
1335 /* Can we fetch the number of bits requested at all? */
1336 if ((end - start) > ((1 << num_ops) * 8))
8e65ff28 1337 internal_error (__FILE__, __LINE__,
3d263c1d 1338 _("gen_bitfield_ref: bitfield too wide"));
c906108c
SS
1339
1340 /* Note that we know here that we only need to try each opcode once.
1341 That may not be true on machines with weird byte sizes. */
1342 offset = bound_start;
1343 fragment_count = 0;
1344 for (op = num_ops - 1; op >= 0; op--)
1345 {
1346 /* number of bits that ops[op] would fetch */
1347 int op_size = 8 << op;
1348
1349 /* The stack at this point, from bottom to top, contains zero or
c5aa993b
JM
1350 more fragments, then the address. */
1351
c906108c
SS
1352 /* Does this fetch fit within the bitfield? */
1353 if (offset + op_size <= bound_end)
1354 {
1355 /* Is this the last fragment? */
1356 int last_frag = (offset + op_size == bound_end);
1357
c5aa993b
JM
1358 if (!last_frag)
1359 ax_simple (ax, aop_dup); /* keep a copy of the address */
1360
c906108c
SS
1361 /* Add the offset. */
1362 gen_offset (ax, offset / TARGET_CHAR_BIT);
1363
1364 if (trace_kludge)
1365 {
1366 /* Record the area of memory we're about to fetch. */
1367 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1368 }
1369
1370 /* Perform the fetch. */
1371 ax_simple (ax, ops[op]);
c5aa993b
JM
1372
1373 /* Shift the bits we have to their proper position.
c906108c
SS
1374 gen_left_shift will generate right shifts when the operand
1375 is negative.
1376
c5aa993b
JM
1377 A big-endian field diagram to ponder:
1378 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1379 +------++------++------++------++------++------++------++------+
1380 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1381 ^ ^ ^ ^
1382 bit number 16 32 48 53
c906108c
SS
1383 These are bit numbers as supplied by GDB. Note that the
1384 bit numbers run from right to left once you've fetched the
1385 value!
1386
c5aa993b
JM
1387 A little-endian field diagram to ponder:
1388 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1389 +------++------++------++------++------++------++------++------+
1390 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1391 ^ ^ ^ ^ ^
1392 bit number 48 32 16 4 0
1393
1394 In both cases, the most significant end is on the left
1395 (i.e. normal numeric writing order), which means that you
1396 don't go crazy thinking about `left' and `right' shifts.
1397
1398 We don't have to worry about masking yet:
1399 - If they contain garbage off the least significant end, then we
1400 must be looking at the low end of the field, and the right
1401 shift will wipe them out.
1402 - If they contain garbage off the most significant end, then we
1403 must be looking at the most significant end of the word, and
1404 the sign/zero extension will wipe them out.
1405 - If we're in the interior of the word, then there is no garbage
1406 on either end, because the ref operators zero-extend. */
505e835d 1407 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
c906108c 1408 gen_left_shift (ax, end - (offset + op_size));
c5aa993b 1409 else
c906108c
SS
1410 gen_left_shift (ax, offset - start);
1411
c5aa993b 1412 if (!last_frag)
c906108c
SS
1413 /* Bring the copy of the address up to the top. */
1414 ax_simple (ax, aop_swap);
1415
1416 offset += op_size;
1417 fragment_count++;
1418 }
1419 }
1420
1421 /* Generate enough bitwise `or' operations to combine all the
1422 fragments we left on the stack. */
1423 while (fragment_count-- > 1)
1424 ax_simple (ax, aop_bit_or);
1425
1426 /* Sign- or zero-extend the value as appropriate. */
1427 ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
1428
1429 /* This is *not* an lvalue. Ugh. */
1430 value->kind = axs_rvalue;
1431 value->type = type;
1432}
1433
b6e7192f
SS
1434/* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1435 is an accumulated offset (in bytes), will be nonzero for objects
1436 embedded in other objects, like C++ base classes. Behavior should
1437 generally follow value_primitive_field. */
1438
1439static void
1440gen_primitive_field (struct expression *exp,
1441 struct agent_expr *ax, struct axs_value *value,
1442 int offset, int fieldno, struct type *type)
1443{
1444 /* Is this a bitfield? */
1445 if (TYPE_FIELD_PACKED (type, fieldno))
1446 gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
1447 (offset * TARGET_CHAR_BIT
1448 + TYPE_FIELD_BITPOS (type, fieldno)),
1449 (offset * TARGET_CHAR_BIT
1450 + TYPE_FIELD_BITPOS (type, fieldno)
1451 + TYPE_FIELD_BITSIZE (type, fieldno)));
1452 else
1453 {
1454 gen_offset (ax, offset
1455 + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
1456 value->kind = axs_lvalue_memory;
1457 value->type = TYPE_FIELD_TYPE (type, fieldno);
1458 }
1459}
1460
1461/* Search for the given field in either the given type or one of its
1462 base classes. Return 1 if found, 0 if not. */
1463
1464static int
1465gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
1466 struct axs_value *value,
1467 char *field, int offset, struct type *type)
1468{
1469 int i, rslt;
1470 int nbases = TYPE_N_BASECLASSES (type);
1471
1472 CHECK_TYPEDEF (type);
1473
1474 for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
1475 {
0d5cff50 1476 const char *this_name = TYPE_FIELD_NAME (type, i);
b6e7192f
SS
1477
1478 if (this_name)
1479 {
1480 if (strcmp (field, this_name) == 0)
1481 {
1482 /* Note that bytecodes for the struct's base (aka
1483 "this") will have been generated already, which will
1484 be unnecessary but not harmful if the static field is
1485 being handled as a global. */
1486 if (field_is_static (&TYPE_FIELD (type, i)))
1487 {
400c6af0
SS
1488 gen_static_field (exp->gdbarch, ax, value, type, i);
1489 if (value->optimized_out)
3e43a32a
MS
1490 error (_("static field `%s' has been "
1491 "optimized out, cannot use"),
400c6af0 1492 field);
b6e7192f
SS
1493 return 1;
1494 }
1495
1496 gen_primitive_field (exp, ax, value, offset, i, type);
1497 return 1;
1498 }
1499#if 0 /* is this right? */
1500 if (this_name[0] == '\0')
1501 internal_error (__FILE__, __LINE__,
1502 _("find_field: anonymous unions not supported"));
1503#endif
1504 }
1505 }
1506
1507 /* Now scan through base classes recursively. */
1508 for (i = 0; i < nbases; i++)
1509 {
1510 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1511
1512 rslt = gen_struct_ref_recursive (exp, ax, value, field,
3e43a32a
MS
1513 offset + TYPE_BASECLASS_BITPOS (type, i)
1514 / TARGET_CHAR_BIT,
b6e7192f
SS
1515 basetype);
1516 if (rslt)
1517 return 1;
1518 }
1519
1520 /* Not found anywhere, flag so caller can complain. */
1521 return 0;
1522}
c906108c
SS
1523
1524/* Generate code to reference the member named FIELD of a structure or
1525 union. The top of the stack, as described by VALUE, should have
1526 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1527 the operator being compiled, and OPERAND_NAME is the kind of thing
1528 it operates on; we use them in error messages. */
1529static void
505e835d
UW
1530gen_struct_ref (struct expression *exp, struct agent_expr *ax,
1531 struct axs_value *value, char *field,
fba45db2 1532 char *operator_name, char *operand_name)
c906108c
SS
1533{
1534 struct type *type;
b6e7192f 1535 int found;
c906108c
SS
1536
1537 /* Follow pointers until we reach a non-pointer. These aren't the C
1538 semantics, but they're what the normal GDB evaluator does, so we
1539 should at least be consistent. */
b97aedf3 1540 while (pointer_type (value->type))
c906108c 1541 {
f7c79c41 1542 require_rvalue (ax, value);
c906108c
SS
1543 gen_deref (ax, value);
1544 }
e8860ec2 1545 type = check_typedef (value->type);
c906108c
SS
1546
1547 /* This must yield a structure or a union. */
1548 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1549 && TYPE_CODE (type) != TYPE_CODE_UNION)
3d263c1d 1550 error (_("The left operand of `%s' is not a %s."),
c906108c
SS
1551 operator_name, operand_name);
1552
1553 /* And it must be in memory; we don't deal with structure rvalues,
1554 or structures living in registers. */
1555 if (value->kind != axs_lvalue_memory)
3d263c1d 1556 error (_("Structure does not live in memory."));
c906108c 1557
b6e7192f
SS
1558 /* Search through fields and base classes recursively. */
1559 found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);
1560
1561 if (!found)
1562 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1563 field, TYPE_TAG_NAME (type));
1564}
c5aa993b 1565
b6e7192f
SS
1566static int
1567gen_namespace_elt (struct expression *exp,
1568 struct agent_expr *ax, struct axs_value *value,
1569 const struct type *curtype, char *name);
1570static int
1571gen_maybe_namespace_elt (struct expression *exp,
1572 struct agent_expr *ax, struct axs_value *value,
1573 const struct type *curtype, char *name);
1574
1575static void
400c6af0 1576gen_static_field (struct gdbarch *gdbarch,
b6e7192f
SS
1577 struct agent_expr *ax, struct axs_value *value,
1578 struct type *type, int fieldno)
1579{
1580 if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
c906108c 1581 {
b6e7192f 1582 ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
c906108c 1583 value->kind = axs_lvalue_memory;
b6e7192f 1584 value->type = TYPE_FIELD_TYPE (type, fieldno);
400c6af0 1585 value->optimized_out = 0;
b6e7192f
SS
1586 }
1587 else
1588 {
ff355380 1589 const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
b6e7192f 1590 struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
b6e7192f 1591
400c6af0
SS
1592 if (sym)
1593 {
1594 gen_var_ref (gdbarch, ax, value, sym);
1595
1596 /* Don't error if the value was optimized out, we may be
1597 scanning all static fields and just want to pass over this
1598 and continue with the rest. */
1599 }
1600 else
1601 {
1602 /* Silently assume this was optimized out; class printing
1603 will let the user know why the data is missing. */
1604 value->optimized_out = 1;
1605 }
b6e7192f
SS
1606 }
1607}
1608
1609static int
1610gen_struct_elt_for_reference (struct expression *exp,
1611 struct agent_expr *ax, struct axs_value *value,
1612 struct type *type, char *fieldname)
1613{
1614 struct type *t = type;
1615 int i;
b6e7192f
SS
1616
1617 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1618 && TYPE_CODE (t) != TYPE_CODE_UNION)
1619 internal_error (__FILE__, __LINE__,
1620 _("non-aggregate type to gen_struct_elt_for_reference"));
1621
1622 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1623 {
0d5cff50 1624 const char *t_field_name = TYPE_FIELD_NAME (t, i);
b6e7192f
SS
1625
1626 if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1627 {
1628 if (field_is_static (&TYPE_FIELD (t, i)))
1629 {
400c6af0
SS
1630 gen_static_field (exp->gdbarch, ax, value, t, i);
1631 if (value->optimized_out)
3e43a32a
MS
1632 error (_("static field `%s' has been "
1633 "optimized out, cannot use"),
400c6af0 1634 fieldname);
b6e7192f
SS
1635 return 1;
1636 }
1637 if (TYPE_FIELD_PACKED (t, i))
1638 error (_("pointers to bitfield members not allowed"));
1639
1640 /* FIXME we need a way to do "want_address" equivalent */
1641
1642 error (_("Cannot reference non-static field \"%s\""), fieldname);
1643 }
c906108c 1644 }
b6e7192f
SS
1645
1646 /* FIXME add other scoped-reference cases here */
1647
1648 /* Do a last-ditch lookup. */
1649 return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
c906108c
SS
1650}
1651
b6e7192f
SS
1652/* C++: Return the member NAME of the namespace given by the type
1653 CURTYPE. */
1654
1655static int
1656gen_namespace_elt (struct expression *exp,
1657 struct agent_expr *ax, struct axs_value *value,
1658 const struct type *curtype, char *name)
1659{
1660 int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);
1661
1662 if (!found)
1663 error (_("No symbol \"%s\" in namespace \"%s\"."),
1664 name, TYPE_TAG_NAME (curtype));
1665
1666 return found;
1667}
1668
1669/* A helper function used by value_namespace_elt and
1670 value_struct_elt_for_reference. It looks up NAME inside the
1671 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1672 is a class and NAME refers to a type in CURTYPE itself (as opposed
1673 to, say, some base class of CURTYPE). */
1674
1675static int
1676gen_maybe_namespace_elt (struct expression *exp,
1677 struct agent_expr *ax, struct axs_value *value,
1678 const struct type *curtype, char *name)
1679{
1680 const char *namespace_name = TYPE_TAG_NAME (curtype);
1681 struct symbol *sym;
1682
1683 sym = cp_lookup_symbol_namespace (namespace_name, name,
1684 block_for_pc (ax->scope),
ac0cd78b 1685 VAR_DOMAIN);
b6e7192f
SS
1686
1687 if (sym == NULL)
1688 return 0;
1689
1690 gen_var_ref (exp->gdbarch, ax, value, sym);
1691
400c6af0
SS
1692 if (value->optimized_out)
1693 error (_("`%s' has been optimized out, cannot use"),
1694 SYMBOL_PRINT_NAME (sym));
1695
b6e7192f
SS
1696 return 1;
1697}
1698
1699
1700static int
1701gen_aggregate_elt_ref (struct expression *exp,
1702 struct agent_expr *ax, struct axs_value *value,
1703 struct type *type, char *field,
1704 char *operator_name, char *operand_name)
1705{
1706 switch (TYPE_CODE (type))
1707 {
1708 case TYPE_CODE_STRUCT:
1709 case TYPE_CODE_UNION:
1710 return gen_struct_elt_for_reference (exp, ax, value, type, field);
1711 break;
1712 case TYPE_CODE_NAMESPACE:
1713 return gen_namespace_elt (exp, ax, value, type, field);
1714 break;
1715 default:
1716 internal_error (__FILE__, __LINE__,
1717 _("non-aggregate type in gen_aggregate_elt_ref"));
1718 }
1719
1720 return 0;
1721}
c906108c 1722
0e2de366 1723/* Generate code for GDB's magical `repeat' operator.
c906108c
SS
1724 LVALUE @ INT creates an array INT elements long, and whose elements
1725 have the same type as LVALUE, located in memory so that LVALUE is
1726 its first element. For example, argv[0]@argc gives you the array
1727 of command-line arguments.
1728
1729 Unfortunately, because we have to know the types before we actually
1730 have a value for the expression, we can't implement this perfectly
1731 without changing the type system, having values that occupy two
1732 stack slots, doing weird things with sizeof, etc. So we require
1733 the right operand to be a constant expression. */
1734static void
f7c79c41
UW
1735gen_repeat (struct expression *exp, union exp_element **pc,
1736 struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1737{
1738 struct axs_value value1;
5b4ee69b 1739
c906108c
SS
1740 /* We don't want to turn this into an rvalue, so no conversions
1741 here. */
f7c79c41 1742 gen_expr (exp, pc, ax, &value1);
c906108c 1743 if (value1.kind != axs_lvalue_memory)
3d263c1d 1744 error (_("Left operand of `@' must be an object in memory."));
c906108c
SS
1745
1746 /* Evaluate the length; it had better be a constant. */
1747 {
1748 struct value *v = const_expr (pc);
1749 int length;
1750
c5aa993b 1751 if (!v)
3e43a32a
MS
1752 error (_("Right operand of `@' must be a "
1753 "constant, in agent expressions."));
04624583 1754 if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
3d263c1d 1755 error (_("Right operand of `@' must be an integer."));
c906108c
SS
1756 length = value_as_long (v);
1757 if (length <= 0)
3d263c1d 1758 error (_("Right operand of `@' must be positive."));
c906108c
SS
1759
1760 /* The top of the stack is already the address of the object, so
1761 all we need to do is frob the type of the lvalue. */
1762 {
1763 /* FIXME-type-allocation: need a way to free this type when we are
c5aa993b 1764 done with it. */
e3506a9f
UW
1765 struct type *array
1766 = lookup_array_range_type (value1.type, 0, length - 1);
c906108c
SS
1767
1768 value->kind = axs_lvalue_memory;
1769 value->type = array;
1770 }
1771 }
1772}
1773
1774
1775/* Emit code for the `sizeof' operator.
1776 *PC should point at the start of the operand expression; we advance it
1777 to the first instruction after the operand. */
1778static void
f7c79c41
UW
1779gen_sizeof (struct expression *exp, union exp_element **pc,
1780 struct agent_expr *ax, struct axs_value *value,
1781 struct type *size_type)
c906108c
SS
1782{
1783 /* We don't care about the value of the operand expression; we only
1784 care about its type. However, in the current arrangement, the
1785 only way to find an expression's type is to generate code for it.
1786 So we generate code for the operand, and then throw it away,
1787 replacing it with code that simply pushes its size. */
1788 int start = ax->len;
5b4ee69b 1789
f7c79c41 1790 gen_expr (exp, pc, ax, value);
c906108c
SS
1791
1792 /* Throw away the code we just generated. */
1793 ax->len = start;
c5aa993b 1794
c906108c
SS
1795 ax_const_l (ax, TYPE_LENGTH (value->type));
1796 value->kind = axs_rvalue;
f7c79c41 1797 value->type = size_type;
c906108c 1798}
c906108c 1799\f
c5aa993b 1800
c906108c
SS
1801/* Generating bytecode from GDB expressions: general recursive thingy */
1802
3d263c1d 1803/* XXX: i18n */
c906108c
SS
1804/* A gen_expr function written by a Gen-X'er guy.
1805 Append code for the subexpression of EXPR starting at *POS_P to AX. */
55aa24fb 1806void
f7c79c41
UW
1807gen_expr (struct expression *exp, union exp_element **pc,
1808 struct agent_expr *ax, struct axs_value *value)
c906108c
SS
1809{
1810 /* Used to hold the descriptions of operand expressions. */
09d559e4 1811 struct axs_value value1, value2, value3;
f61e138d 1812 enum exp_opcode op = (*pc)[0].opcode, op2;
09d559e4 1813 int if1, go1, if2, go2, end;
3b11a015 1814 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
c906108c
SS
1815
1816 /* If we're looking at a constant expression, just push its value. */
1817 {
1818 struct value *v = maybe_const_expr (pc);
c5aa993b 1819
c906108c
SS
1820 if (v)
1821 {
1822 ax_const_l (ax, value_as_long (v));
1823 value->kind = axs_rvalue;
df407dfe 1824 value->type = check_typedef (value_type (v));
c906108c
SS
1825 return;
1826 }
1827 }
1828
1829 /* Otherwise, go ahead and generate code for it. */
1830 switch (op)
1831 {
1832 /* Binary arithmetic operators. */
1833 case BINOP_ADD:
1834 case BINOP_SUB:
1835 case BINOP_MUL:
1836 case BINOP_DIV:
1837 case BINOP_REM:
948103cf
SS
1838 case BINOP_LSH:
1839 case BINOP_RSH:
c906108c
SS
1840 case BINOP_SUBSCRIPT:
1841 case BINOP_BITWISE_AND:
1842 case BINOP_BITWISE_IOR:
1843 case BINOP_BITWISE_XOR:
782b2b07
SS
1844 case BINOP_EQUAL:
1845 case BINOP_NOTEQUAL:
1846 case BINOP_LESS:
1847 case BINOP_GTR:
1848 case BINOP_LEQ:
1849 case BINOP_GEQ:
c906108c 1850 (*pc)++;
f7c79c41
UW
1851 gen_expr (exp, pc, ax, &value1);
1852 gen_usual_unary (exp, ax, &value1);
f61e138d
SS
1853 gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
1854 break;
1855
09d559e4
SS
1856 case BINOP_LOGICAL_AND:
1857 (*pc)++;
1858 /* Generate the obvious sequence of tests and jumps. */
1859 gen_expr (exp, pc, ax, &value1);
1860 gen_usual_unary (exp, ax, &value1);
1861 if1 = ax_goto (ax, aop_if_goto);
1862 go1 = ax_goto (ax, aop_goto);
1863 ax_label (ax, if1, ax->len);
1864 gen_expr (exp, pc, ax, &value2);
1865 gen_usual_unary (exp, ax, &value2);
1866 if2 = ax_goto (ax, aop_if_goto);
1867 go2 = ax_goto (ax, aop_goto);
1868 ax_label (ax, if2, ax->len);
1869 ax_const_l (ax, 1);
1870 end = ax_goto (ax, aop_goto);
1871 ax_label (ax, go1, ax->len);
1872 ax_label (ax, go2, ax->len);
1873 ax_const_l (ax, 0);
1874 ax_label (ax, end, ax->len);
1875 value->kind = axs_rvalue;
3b11a015 1876 value->type = int_type;
09d559e4
SS
1877 break;
1878
1879 case BINOP_LOGICAL_OR:
1880 (*pc)++;
1881 /* Generate the obvious sequence of tests and jumps. */
1882 gen_expr (exp, pc, ax, &value1);
1883 gen_usual_unary (exp, ax, &value1);
1884 if1 = ax_goto (ax, aop_if_goto);
1885 gen_expr (exp, pc, ax, &value2);
1886 gen_usual_unary (exp, ax, &value2);
1887 if2 = ax_goto (ax, aop_if_goto);
1888 ax_const_l (ax, 0);
1889 end = ax_goto (ax, aop_goto);
1890 ax_label (ax, if1, ax->len);
1891 ax_label (ax, if2, ax->len);
1892 ax_const_l (ax, 1);
1893 ax_label (ax, end, ax->len);
1894 value->kind = axs_rvalue;
3b11a015 1895 value->type = int_type;
09d559e4
SS
1896 break;
1897
1898 case TERNOP_COND:
1899 (*pc)++;
1900 gen_expr (exp, pc, ax, &value1);
1901 gen_usual_unary (exp, ax, &value1);
1902 /* For (A ? B : C), it's easiest to generate subexpression
1903 bytecodes in order, but if_goto jumps on true, so we invert
1904 the sense of A. Then we can do B by dropping through, and
1905 jump to do C. */
3b11a015 1906 gen_logical_not (ax, &value1, int_type);
09d559e4
SS
1907 if1 = ax_goto (ax, aop_if_goto);
1908 gen_expr (exp, pc, ax, &value2);
1909 gen_usual_unary (exp, ax, &value2);
1910 end = ax_goto (ax, aop_goto);
1911 ax_label (ax, if1, ax->len);
1912 gen_expr (exp, pc, ax, &value3);
1913 gen_usual_unary (exp, ax, &value3);
1914 ax_label (ax, end, ax->len);
1915 /* This is arbitary - what if B and C are incompatible types? */
1916 value->type = value2.type;
1917 value->kind = value2.kind;
1918 break;
1919
f61e138d
SS
1920 case BINOP_ASSIGN:
1921 (*pc)++;
1922 if ((*pc)[0].opcode == OP_INTERNALVAR)
c906108c 1923 {
f61e138d
SS
1924 char *name = internalvar_name ((*pc)[1].internalvar);
1925 struct trace_state_variable *tsv;
5b4ee69b 1926
f61e138d
SS
1927 (*pc) += 3;
1928 gen_expr (exp, pc, ax, value);
1929 tsv = find_trace_state_variable (name);
1930 if (tsv)
f7c79c41 1931 {
f61e138d
SS
1932 ax_tsv (ax, aop_setv, tsv->number);
1933 if (trace_kludge)
1934 ax_tsv (ax, aop_tracev, tsv->number);
f7c79c41 1935 }
f7c79c41 1936 else
3e43a32a
MS
1937 error (_("$%s is not a trace state variable, "
1938 "may not assign to it"), name);
f61e138d
SS
1939 }
1940 else
1941 error (_("May only assign to trace state variables"));
1942 break;
782b2b07 1943
f61e138d
SS
1944 case BINOP_ASSIGN_MODIFY:
1945 (*pc)++;
1946 op2 = (*pc)[0].opcode;
1947 (*pc)++;
1948 (*pc)++;
1949 if ((*pc)[0].opcode == OP_INTERNALVAR)
1950 {
1951 char *name = internalvar_name ((*pc)[1].internalvar);
1952 struct trace_state_variable *tsv;
5b4ee69b 1953
f61e138d
SS
1954 (*pc) += 3;
1955 tsv = find_trace_state_variable (name);
1956 if (tsv)
1957 {
1958 /* The tsv will be the left half of the binary operation. */
1959 ax_tsv (ax, aop_getv, tsv->number);
1960 if (trace_kludge)
1961 ax_tsv (ax, aop_tracev, tsv->number);
1962 /* Trace state variables are always 64-bit integers. */
1963 value1.kind = axs_rvalue;
1964 value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
1965 /* Now do right half of expression. */
1966 gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
1967 /* We have a result of the binary op, set the tsv. */
1968 ax_tsv (ax, aop_setv, tsv->number);
1969 if (trace_kludge)
1970 ax_tsv (ax, aop_tracev, tsv->number);
1971 }
1972 else
3e43a32a
MS
1973 error (_("$%s is not a trace state variable, "
1974 "may not assign to it"), name);
c906108c 1975 }
f61e138d
SS
1976 else
1977 error (_("May only assign to trace state variables"));
c906108c
SS
1978 break;
1979
1980 /* Note that we need to be a little subtle about generating code
c5aa993b
JM
1981 for comma. In C, we can do some optimizations here because
1982 we know the left operand is only being evaluated for effect.
1983 However, if the tracing kludge is in effect, then we always
1984 need to evaluate the left hand side fully, so that all the
1985 variables it mentions get traced. */
c906108c
SS
1986 case BINOP_COMMA:
1987 (*pc)++;
f7c79c41 1988 gen_expr (exp, pc, ax, &value1);
c906108c 1989 /* Don't just dispose of the left operand. We might be tracing,
c5aa993b
JM
1990 in which case we want to emit code to trace it if it's an
1991 lvalue. */
400c6af0 1992 gen_traced_pop (exp->gdbarch, ax, &value1);
f7c79c41 1993 gen_expr (exp, pc, ax, value);
c906108c
SS
1994 /* It's the consumer's responsibility to trace the right operand. */
1995 break;
c5aa993b 1996
c906108c
SS
1997 case OP_LONG: /* some integer constant */
1998 {
1999 struct type *type = (*pc)[1].type;
2000 LONGEST k = (*pc)[2].longconst;
5b4ee69b 2001
c906108c
SS
2002 (*pc) += 4;
2003 gen_int_literal (ax, value, k, type);
2004 }
c5aa993b 2005 break;
c906108c
SS
2006
2007 case OP_VAR_VALUE:
f7c79c41 2008 gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);
400c6af0
SS
2009
2010 if (value->optimized_out)
2011 error (_("`%s' has been optimized out, cannot use"),
2012 SYMBOL_PRINT_NAME ((*pc)[2].symbol));
2013
c906108c
SS
2014 (*pc) += 4;
2015 break;
2016
2017 case OP_REGISTER:
2018 {
67f3407f
DJ
2019 const char *name = &(*pc)[2].string;
2020 int reg;
5b4ee69b 2021
67f3407f 2022 (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
f7c79c41 2023 reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
67f3407f
DJ
2024 if (reg == -1)
2025 internal_error (__FILE__, __LINE__,
2026 _("Register $%s not available"), name);
6ab12e0f
PA
2027 /* No support for tracing user registers yet. */
2028 if (reg >= gdbarch_num_regs (exp->gdbarch)
2029 + gdbarch_num_pseudo_regs (exp->gdbarch))
abc1f4cd
HZ
2030 error (_("'%s' is a user-register; "
2031 "GDB cannot yet trace user-register contents."),
6ab12e0f 2032 name);
c906108c
SS
2033 value->kind = axs_lvalue_register;
2034 value->u.reg = reg;
f7c79c41 2035 value->type = register_type (exp->gdbarch, reg);
c906108c 2036 }
c5aa993b 2037 break;
c906108c
SS
2038
2039 case OP_INTERNALVAR:
f61e138d 2040 {
22d2b532
SDJ
2041 struct internalvar *var = (*pc)[1].internalvar;
2042 const char *name = internalvar_name (var);
f61e138d 2043 struct trace_state_variable *tsv;
5b4ee69b 2044
f61e138d
SS
2045 (*pc) += 3;
2046 tsv = find_trace_state_variable (name);
2047 if (tsv)
2048 {
2049 ax_tsv (ax, aop_getv, tsv->number);
2050 if (trace_kludge)
2051 ax_tsv (ax, aop_tracev, tsv->number);
2052 /* Trace state variables are always 64-bit integers. */
2053 value->kind = axs_rvalue;
2054 value->type = builtin_type (exp->gdbarch)->builtin_long_long;
2055 }
22d2b532 2056 else if (! compile_internalvar_to_ax (var, ax, value))
3e43a32a
MS
2057 error (_("$%s is not a trace state variable; GDB agent "
2058 "expressions cannot use convenience variables."), name);
f61e138d
SS
2059 }
2060 break;
c906108c 2061
c5aa993b 2062 /* Weirdo operator: see comments for gen_repeat for details. */
c906108c
SS
2063 case BINOP_REPEAT:
2064 /* Note that gen_repeat handles its own argument evaluation. */
2065 (*pc)++;
f7c79c41 2066 gen_repeat (exp, pc, ax, value);
c906108c
SS
2067 break;
2068
2069 case UNOP_CAST:
2070 {
2071 struct type *type = (*pc)[1].type;
5b4ee69b 2072
c906108c 2073 (*pc) += 3;
f7c79c41 2074 gen_expr (exp, pc, ax, value);
c906108c
SS
2075 gen_cast (ax, value, type);
2076 }
c5aa993b 2077 break;
c906108c 2078
9eaf6705
TT
2079 case UNOP_CAST_TYPE:
2080 {
2081 int offset;
2082 struct value *val;
2083 struct type *type;
2084
2085 ++*pc;
2086 offset = *pc - exp->elts;
2087 val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2088 type = value_type (val);
2089 *pc = &exp->elts[offset];
2090
2091 gen_expr (exp, pc, ax, value);
2092 gen_cast (ax, value, type);
2093 }
2094 break;
2095
c906108c
SS
2096 case UNOP_MEMVAL:
2097 {
2098 struct type *type = check_typedef ((*pc)[1].type);
5b4ee69b 2099
c906108c 2100 (*pc) += 3;
f7c79c41 2101 gen_expr (exp, pc, ax, value);
a0c78a73
PA
2102
2103 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2104 already have the right value on the stack. For
2105 axs_lvalue_register, we must convert. */
2106 if (value->kind == axs_lvalue_register)
2107 require_rvalue (ax, value);
2108
c906108c
SS
2109 value->type = type;
2110 value->kind = axs_lvalue_memory;
2111 }
c5aa993b 2112 break;
c906108c 2113
9eaf6705
TT
2114 case UNOP_MEMVAL_TYPE:
2115 {
2116 int offset;
2117 struct value *val;
2118 struct type *type;
2119
2120 ++*pc;
2121 offset = *pc - exp->elts;
2122 val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
2123 type = value_type (val);
2124 *pc = &exp->elts[offset];
2125
2126 gen_expr (exp, pc, ax, value);
2127
2128 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
2129 already have the right value on the stack. For
2130 axs_lvalue_register, we must convert. */
2131 if (value->kind == axs_lvalue_register)
2132 require_rvalue (ax, value);
2133
2134 value->type = type;
2135 value->kind = axs_lvalue_memory;
2136 }
2137 break;
2138
36e9969c
NS
2139 case UNOP_PLUS:
2140 (*pc)++;
0e2de366 2141 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
f7c79c41
UW
2142 gen_expr (exp, pc, ax, value);
2143 gen_usual_unary (exp, ax, value);
36e9969c
NS
2144 break;
2145
c906108c
SS
2146 case UNOP_NEG:
2147 (*pc)++;
2148 /* -FOO is equivalent to 0 - FOO. */
22601c15
UW
2149 gen_int_literal (ax, &value1, 0,
2150 builtin_type (exp->gdbarch)->builtin_int);
f7c79c41
UW
2151 gen_usual_unary (exp, ax, &value1); /* shouldn't do much */
2152 gen_expr (exp, pc, ax, &value2);
2153 gen_usual_unary (exp, ax, &value2);
2154 gen_usual_arithmetic (exp, ax, &value1, &value2);
2155 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
c906108c
SS
2156 break;
2157
2158 case UNOP_LOGICAL_NOT:
2159 (*pc)++;
f7c79c41
UW
2160 gen_expr (exp, pc, ax, value);
2161 gen_usual_unary (exp, ax, value);
3b11a015 2162 gen_logical_not (ax, value, int_type);
c906108c
SS
2163 break;
2164
2165 case UNOP_COMPLEMENT:
2166 (*pc)++;
f7c79c41
UW
2167 gen_expr (exp, pc, ax, value);
2168 gen_usual_unary (exp, ax, value);
2169 gen_integral_promotions (exp, ax, value);
c906108c
SS
2170 gen_complement (ax, value);
2171 break;
2172
2173 case UNOP_IND:
2174 (*pc)++;
f7c79c41
UW
2175 gen_expr (exp, pc, ax, value);
2176 gen_usual_unary (exp, ax, value);
b97aedf3 2177 if (!pointer_type (value->type))
3d263c1d 2178 error (_("Argument of unary `*' is not a pointer."));
c906108c
SS
2179 gen_deref (ax, value);
2180 break;
2181
2182 case UNOP_ADDR:
2183 (*pc)++;
f7c79c41 2184 gen_expr (exp, pc, ax, value);
c906108c
SS
2185 gen_address_of (ax, value);
2186 break;
2187
2188 case UNOP_SIZEOF:
2189 (*pc)++;
2190 /* Notice that gen_sizeof handles its own operand, unlike most
c5aa993b
JM
2191 of the other unary operator functions. This is because we
2192 have to throw away the code we generate. */
f7c79c41
UW
2193 gen_sizeof (exp, pc, ax, value,
2194 builtin_type (exp->gdbarch)->builtin_int);
c906108c
SS
2195 break;
2196
2197 case STRUCTOP_STRUCT:
2198 case STRUCTOP_PTR:
2199 {
2200 int length = (*pc)[1].longconst;
2201 char *name = &(*pc)[2].string;
2202
2203 (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
f7c79c41 2204 gen_expr (exp, pc, ax, value);
c906108c 2205 if (op == STRUCTOP_STRUCT)
505e835d 2206 gen_struct_ref (exp, ax, value, name, ".", "structure or union");
c906108c 2207 else if (op == STRUCTOP_PTR)
505e835d 2208 gen_struct_ref (exp, ax, value, name, "->",
c906108c
SS
2209 "pointer to a structure or union");
2210 else
2211 /* If this `if' chain doesn't handle it, then the case list
c5aa993b 2212 shouldn't mention it, and we shouldn't be here. */
8e65ff28 2213 internal_error (__FILE__, __LINE__,
3d263c1d 2214 _("gen_expr: unhandled struct case"));
c906108c 2215 }
c5aa993b 2216 break;
c906108c 2217
6c228b9c
SS
2218 case OP_THIS:
2219 {
66a17cb6 2220 struct symbol *sym, *func;
6c228b9c 2221 struct block *b;
66a17cb6 2222 const struct language_defn *lang;
6c228b9c 2223
66a17cb6
TT
2224 b = block_for_pc (ax->scope);
2225 func = block_linkage_function (b);
2226 lang = language_def (SYMBOL_LANGUAGE (func));
6c228b9c 2227
66a17cb6 2228 sym = lookup_language_this (lang, b);
6c228b9c 2229 if (!sym)
66a17cb6 2230 error (_("no `%s' found"), lang->la_name_of_this);
6c228b9c
SS
2231
2232 gen_var_ref (exp->gdbarch, ax, value, sym);
400c6af0
SS
2233
2234 if (value->optimized_out)
2235 error (_("`%s' has been optimized out, cannot use"),
2236 SYMBOL_PRINT_NAME (sym));
2237
6c228b9c
SS
2238 (*pc) += 2;
2239 }
2240 break;
2241
b6e7192f
SS
2242 case OP_SCOPE:
2243 {
2244 struct type *type = (*pc)[1].type;
2245 int length = longest_to_int ((*pc)[2].longconst);
2246 char *name = &(*pc)[3].string;
2247 int found;
2248
2249 found = gen_aggregate_elt_ref (exp, ax, value, type, name,
2250 "?", "??");
2251 if (!found)
2252 error (_("There is no field named %s"), name);
2253 (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
2254 }
2255 break;
2256
c906108c 2257 case OP_TYPE:
608b4967
TT
2258 case OP_TYPEOF:
2259 case OP_DECLTYPE:
3d263c1d 2260 error (_("Attempt to use a type name as an expression."));
c906108c
SS
2261
2262 default:
b6e7192f 2263 error (_("Unsupported operator %s (%d) in expression."),
bd0b9f9e 2264 op_name (exp, op), op);
c906108c
SS
2265 }
2266}
f61e138d
SS
2267
2268/* This handles the middle-to-right-side of code generation for binary
2269 expressions, which is shared between regular binary operations and
2270 assign-modify (+= and friends) expressions. */
2271
2272static void
2273gen_expr_binop_rest (struct expression *exp,
2274 enum exp_opcode op, union exp_element **pc,
2275 struct agent_expr *ax, struct axs_value *value,
2276 struct axs_value *value1, struct axs_value *value2)
2277{
3b11a015
SS
2278 struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
2279
f61e138d
SS
2280 gen_expr (exp, pc, ax, value2);
2281 gen_usual_unary (exp, ax, value2);
2282 gen_usual_arithmetic (exp, ax, value1, value2);
2283 switch (op)
2284 {
2285 case BINOP_ADD:
2286 if (TYPE_CODE (value1->type) == TYPE_CODE_INT
b97aedf3 2287 && pointer_type (value2->type))
f61e138d
SS
2288 {
2289 /* Swap the values and proceed normally. */
2290 ax_simple (ax, aop_swap);
2291 gen_ptradd (ax, value, value2, value1);
2292 }
b97aedf3 2293 else if (pointer_type (value1->type)
f61e138d
SS
2294 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2295 gen_ptradd (ax, value, value1, value2);
2296 else
2297 gen_binop (ax, value, value1, value2,
2298 aop_add, aop_add, 1, "addition");
2299 break;
2300 case BINOP_SUB:
b97aedf3 2301 if (pointer_type (value1->type)
f61e138d
SS
2302 && TYPE_CODE (value2->type) == TYPE_CODE_INT)
2303 gen_ptrsub (ax,value, value1, value2);
b97aedf3
SS
2304 else if (pointer_type (value1->type)
2305 && pointer_type (value2->type))
f61e138d
SS
2306 /* FIXME --- result type should be ptrdiff_t */
2307 gen_ptrdiff (ax, value, value1, value2,
2308 builtin_type (exp->gdbarch)->builtin_long);
2309 else
2310 gen_binop (ax, value, value1, value2,
2311 aop_sub, aop_sub, 1, "subtraction");
2312 break;
2313 case BINOP_MUL:
2314 gen_binop (ax, value, value1, value2,
2315 aop_mul, aop_mul, 1, "multiplication");
2316 break;
2317 case BINOP_DIV:
2318 gen_binop (ax, value, value1, value2,
2319 aop_div_signed, aop_div_unsigned, 1, "division");
2320 break;
2321 case BINOP_REM:
2322 gen_binop (ax, value, value1, value2,
2323 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2324 break;
948103cf
SS
2325 case BINOP_LSH:
2326 gen_binop (ax, value, value1, value2,
2327 aop_lsh, aop_lsh, 1, "left shift");
2328 break;
2329 case BINOP_RSH:
2330 gen_binop (ax, value, value1, value2,
2331 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2332 break;
f61e138d 2333 case BINOP_SUBSCRIPT:
be636754
PA
2334 {
2335 struct type *type;
2336
2337 if (binop_types_user_defined_p (op, value1->type, value2->type))
2338 {
3e43a32a
MS
2339 error (_("cannot subscript requested type: "
2340 "cannot call user defined functions"));
be636754
PA
2341 }
2342 else
2343 {
2344 /* If the user attempts to subscript something that is not
2345 an array or pointer type (like a plain int variable for
2346 example), then report this as an error. */
2347 type = check_typedef (value1->type);
2348 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2349 && TYPE_CODE (type) != TYPE_CODE_PTR)
2350 {
2351 if (TYPE_NAME (type))
2352 error (_("cannot subscript something of type `%s'"),
2353 TYPE_NAME (type));
2354 else
2355 error (_("cannot subscript requested type"));
2356 }
2357 }
2358
5d5b640e 2359 if (!is_integral_type (value2->type))
3e43a32a
MS
2360 error (_("Argument to arithmetic operation "
2361 "not a number or boolean."));
5d5b640e 2362
be636754
PA
2363 gen_ptradd (ax, value, value1, value2);
2364 gen_deref (ax, value);
2365 break;
2366 }
f61e138d
SS
2367 case BINOP_BITWISE_AND:
2368 gen_binop (ax, value, value1, value2,
2369 aop_bit_and, aop_bit_and, 0, "bitwise and");
2370 break;
2371
2372 case BINOP_BITWISE_IOR:
2373 gen_binop (ax, value, value1, value2,
2374 aop_bit_or, aop_bit_or, 0, "bitwise or");
2375 break;
2376
2377 case BINOP_BITWISE_XOR:
2378 gen_binop (ax, value, value1, value2,
2379 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2380 break;
2381
2382 case BINOP_EQUAL:
3b11a015 2383 gen_equal (ax, value, value1, value2, int_type);
f61e138d
SS
2384 break;
2385
2386 case BINOP_NOTEQUAL:
3b11a015
SS
2387 gen_equal (ax, value, value1, value2, int_type);
2388 gen_logical_not (ax, value, int_type);
f61e138d
SS
2389 break;
2390
2391 case BINOP_LESS:
3b11a015 2392 gen_less (ax, value, value1, value2, int_type);
f61e138d
SS
2393 break;
2394
2395 case BINOP_GTR:
2396 ax_simple (ax, aop_swap);
3b11a015 2397 gen_less (ax, value, value1, value2, int_type);
f61e138d
SS
2398 break;
2399
2400 case BINOP_LEQ:
2401 ax_simple (ax, aop_swap);
3b11a015
SS
2402 gen_less (ax, value, value1, value2, int_type);
2403 gen_logical_not (ax, value, int_type);
f61e138d
SS
2404 break;
2405
2406 case BINOP_GEQ:
3b11a015
SS
2407 gen_less (ax, value, value1, value2, int_type);
2408 gen_logical_not (ax, value, int_type);
f61e138d
SS
2409 break;
2410
2411 default:
2412 /* We should only list operators in the outer case statement
2413 that we actually handle in the inner case statement. */
2414 internal_error (__FILE__, __LINE__,
2415 _("gen_expr: op case sets don't match"));
2416 }
2417}
c906108c 2418\f
c5aa993b 2419
0936ad1d
SS
2420/* Given a single variable and a scope, generate bytecodes to trace
2421 its value. This is for use in situations where we have only a
2422 variable's name, and no parsed expression; for instance, when the
2423 name comes from a list of local variables of a function. */
2424
2425struct agent_expr *
400c6af0
SS
2426gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2427 struct symbol *var)
0936ad1d
SS
2428{
2429 struct cleanup *old_chain = 0;
35c9c7ba 2430 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
0936ad1d
SS
2431 struct axs_value value;
2432
2433 old_chain = make_cleanup_free_agent_expr (ax);
2434
2435 trace_kludge = 1;
400c6af0
SS
2436 gen_var_ref (gdbarch, ax, &value, var);
2437
2438 /* If there is no actual variable to trace, flag it by returning
2439 an empty agent expression. */
2440 if (value.optimized_out)
2441 {
2442 do_cleanups (old_chain);
2443 return NULL;
2444 }
0936ad1d
SS
2445
2446 /* Make sure we record the final object, and get rid of it. */
400c6af0 2447 gen_traced_pop (gdbarch, ax, &value);
0936ad1d
SS
2448
2449 /* Oh, and terminate. */
2450 ax_simple (ax, aop_end);
2451
2452 /* We have successfully built the agent expr, so cancel the cleanup
2453 request. If we add more cleanups that we always want done, this
2454 will have to get more complicated. */
2455 discard_cleanups (old_chain);
2456 return ax;
2457}
c5aa993b 2458
c906108c
SS
2459/* Generating bytecode from GDB expressions: driver */
2460
c906108c
SS
2461/* Given a GDB expression EXPR, return bytecode to trace its value.
2462 The result will use the `trace' and `trace_quick' bytecodes to
2463 record the value of all memory touched by the expression. The
2464 caller can then use the ax_reqs function to discover which
2465 registers it relies upon. */
2466struct agent_expr *
fba45db2 2467gen_trace_for_expr (CORE_ADDR scope, struct expression *expr)
c906108c
SS
2468{
2469 struct cleanup *old_chain = 0;
35c9c7ba 2470 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
c906108c
SS
2471 union exp_element *pc;
2472 struct axs_value value;
2473
f23d52e0 2474 old_chain = make_cleanup_free_agent_expr (ax);
c906108c
SS
2475
2476 pc = expr->elts;
2477 trace_kludge = 1;
35c9c7ba 2478 value.optimized_out = 0;
f7c79c41 2479 gen_expr (expr, &pc, ax, &value);
c906108c
SS
2480
2481 /* Make sure we record the final object, and get rid of it. */
400c6af0 2482 gen_traced_pop (expr->gdbarch, ax, &value);
c906108c
SS
2483
2484 /* Oh, and terminate. */
2485 ax_simple (ax, aop_end);
2486
2487 /* We have successfully built the agent expr, so cancel the cleanup
2488 request. If we add more cleanups that we always want done, this
2489 will have to get more complicated. */
2490 discard_cleanups (old_chain);
2491 return ax;
2492}
c906108c 2493
782b2b07
SS
2494/* Given a GDB expression EXPR, return a bytecode sequence that will
2495 evaluate and return a result. The bytecodes will do a direct
2496 evaluation, using the current data on the target, rather than
2497 recording blocks of memory and registers for later use, as
2498 gen_trace_for_expr does. The generated bytecode sequence leaves
2499 the result of expression evaluation on the top of the stack. */
2500
2501struct agent_expr *
2502gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2503{
2504 struct cleanup *old_chain = 0;
35c9c7ba 2505 struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
782b2b07
SS
2506 union exp_element *pc;
2507 struct axs_value value;
2508
2509 old_chain = make_cleanup_free_agent_expr (ax);
2510
2511 pc = expr->elts;
2512 trace_kludge = 0;
35c9c7ba 2513 value.optimized_out = 0;
782b2b07
SS
2514 gen_expr (expr, &pc, ax, &value);
2515
35c9c7ba
SS
2516 require_rvalue (ax, &value);
2517
782b2b07
SS
2518 /* Oh, and terminate. */
2519 ax_simple (ax, aop_end);
2520
2521 /* We have successfully built the agent expr, so cancel the cleanup
2522 request. If we add more cleanups that we always want done, this
2523 will have to get more complicated. */
2524 discard_cleanups (old_chain);
2525 return ax;
2526}
2527
6710bf39
SS
2528struct agent_expr *
2529gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch)
2530{
2531 struct cleanup *old_chain = 0;
2532 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2533 struct axs_value value;
2534
2535 old_chain = make_cleanup_free_agent_expr (ax);
2536
2537 trace_kludge = 1;
2538
2539 gdbarch_gen_return_address (gdbarch, ax, &value, scope);
2540
2541 /* Make sure we record the final object, and get rid of it. */
2542 gen_traced_pop (gdbarch, ax, &value);
2543
2544 /* Oh, and terminate. */
2545 ax_simple (ax, aop_end);
2546
2547 /* We have successfully built the agent expr, so cancel the cleanup
2548 request. If we add more cleanups that we always want done, this
2549 will have to get more complicated. */
2550 discard_cleanups (old_chain);
2551 return ax;
2552}
2553
d3ce09f5
SS
2554/* Given a collection of printf-style arguments, generate code to
2555 evaluate the arguments and pass everything to a special
2556 bytecode. */
2557
2558struct agent_expr *
2559gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
2560 CORE_ADDR function, LONGEST channel,
2561 char *format, int fmtlen,
2562 struct format_piece *frags,
2563 int nargs, struct expression **exprs)
2564{
d3ce09f5
SS
2565 struct cleanup *old_chain = 0;
2566 struct agent_expr *ax = new_agent_expr (gdbarch, scope);
2567 union exp_element *pc;
2568 struct axs_value value;
0e43993a 2569 int tem;
d3ce09f5
SS
2570
2571 old_chain = make_cleanup_free_agent_expr (ax);
2572
2573 /* Evaluate and push the args on the stack in reverse order,
2574 for simplicity of collecting them on the target side. */
2575 for (tem = nargs - 1; tem >= 0; --tem)
2576 {
2577 pc = exprs[tem]->elts;
2578 /* We're computing values, not doing side effects. */
2579 trace_kludge = 0;
2580 value.optimized_out = 0;
2581 gen_expr (exprs[tem], &pc, ax, &value);
2582 require_rvalue (ax, &value);
2583 }
2584
2585 /* Push function and channel. */
2586 ax_const_l (ax, channel);
2587 ax_const_l (ax, function);
2588
2589 /* Issue the printf bytecode proper. */
2590 ax_simple (ax, aop_printf);
2591 ax_simple (ax, nargs);
2592 ax_string (ax, format, fmtlen);
2593
2594 /* And terminate. */
2595 ax_simple (ax, aop_end);
2596
2597 /* We have successfully built the agent expr, so cancel the cleanup
2598 request. If we add more cleanups that we always want done, this
2599 will have to get more complicated. */
2600 discard_cleanups (old_chain);
2601
2602 return ax;
2603}
2604
c906108c 2605static void
34b536a8 2606agent_eval_command_one (char *exp, int eval, CORE_ADDR pc)
c906108c
SS
2607{
2608 struct cleanup *old_chain = 0;
2609 struct expression *expr;
2610 struct agent_expr *agent;
c906108c 2611
34b536a8
HZ
2612 if (!eval)
2613 {
2614 trace_string_kludge = 0;
2615 if (*exp == '/')
2616 exp = decode_agent_options (exp);
2617 }
3065dfb6 2618
34b536a8 2619 if (!eval && strcmp (exp, "$_ret") == 0)
6710bf39 2620 {
34b536a8 2621 agent = gen_trace_for_return_address (pc, get_current_arch ());
6710bf39
SS
2622 old_chain = make_cleanup_free_agent_expr (agent);
2623 }
2624 else
2625 {
34b536a8 2626 expr = parse_exp_1 (&exp, pc, block_for_pc (pc), 0);
6710bf39 2627 old_chain = make_cleanup (free_current_contents, &expr);
34b536a8
HZ
2628 if (eval)
2629 agent = gen_eval_for_expr (pc, expr);
2630 else
2631 agent = gen_trace_for_expr (pc, expr);
6710bf39
SS
2632 make_cleanup_free_agent_expr (agent);
2633 }
2634
35c9c7ba 2635 ax_reqs (agent);
c906108c 2636 ax_print (gdb_stdout, agent);
085dd6e6
JM
2637
2638 /* It would be nice to call ax_reqs here to gather some general info
2639 about the expression, and then print out the result. */
c906108c
SS
2640
2641 do_cleanups (old_chain);
2642 dont_repeat ();
2643}
782b2b07 2644
782b2b07 2645static void
34b536a8 2646agent_command_1 (char *exp, int eval)
782b2b07 2647{
782b2b07
SS
2648 /* We don't deal with overlay debugging at the moment. We need to
2649 think more carefully about this. If you copy this code into
2650 another command, change the error message; the user shouldn't
2651 have to know anything about agent expressions. */
2652 if (overlay_debugging)
2653 error (_("GDB can't do agent expression translation with overlays."));
2654
2655 if (exp == 0)
2656 error_no_arg (_("expression to translate"));
2657
34b536a8
HZ
2658 if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
2659 {
2660 struct linespec_result canonical;
2661 int ix;
2662 struct linespec_sals *iter;
2663 struct cleanup *old_chain;
2664
2665 exp = skip_spaces (exp);
2666 init_linespec_result (&canonical);
2667 decode_line_full (&exp, DECODE_LINE_FUNFIRSTLINE,
2668 (struct symtab *) NULL, 0, &canonical,
2669 NULL, NULL);
2670 old_chain = make_cleanup_destroy_linespec_result (&canonical);
2671 exp = skip_spaces (exp);
2672 if (exp[0] == ',')
2673 {
2674 exp++;
2675 exp = skip_spaces (exp);
2676 }
2677 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
2678 {
2679 int i;
782b2b07 2680
34b536a8
HZ
2681 for (i = 0; i < iter->sals.nelts; i++)
2682 agent_eval_command_one (exp, eval, iter->sals.sals[i].pc);
2683 }
2684 do_cleanups (old_chain);
2685 }
2686 else
2687 agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
782b2b07 2688
782b2b07
SS
2689 dont_repeat ();
2690}
34b536a8
HZ
2691
2692static void
2693agent_command (char *exp, int from_tty)
2694{
2695 agent_command_1 (exp, 0);
2696}
2697
2698/* Parse the given expression, compile it into an agent expression
2699 that does direct evaluation, and display the resulting
2700 expression. */
2701
2702static void
2703agent_eval_command (char *exp, int from_tty)
2704{
2705 agent_command_1 (exp, 1);
2706}
2707
d3ce09f5
SS
2708/* Parse the given expression, compile it into an agent expression
2709 that does a printf, and display the resulting expression. */
2710
2711static void
2712maint_agent_printf_command (char *exp, int from_tty)
2713{
2714 struct cleanup *old_chain = 0;
2715 struct expression *expr;
2716 struct expression *argvec[100];
2717 struct agent_expr *agent;
2718 struct frame_info *fi = get_current_frame (); /* need current scope */
2719 char *cmdrest;
2720 char *format_start, *format_end;
2721 struct format_piece *fpieces;
2722 int nargs;
2723
2724 /* We don't deal with overlay debugging at the moment. We need to
2725 think more carefully about this. If you copy this code into
2726 another command, change the error message; the user shouldn't
2727 have to know anything about agent expressions. */
2728 if (overlay_debugging)
2729 error (_("GDB can't do agent expression translation with overlays."));
2730
2731 if (exp == 0)
2732 error_no_arg (_("expression to translate"));
2733
2734 cmdrest = exp;
2735
2736 cmdrest = skip_spaces (cmdrest);
2737
2738 if (*cmdrest++ != '"')
2739 error (_("Must start with a format string."));
2740
2741 format_start = cmdrest;
2742
2743 fpieces = parse_format_string (&cmdrest);
2744
2745 old_chain = make_cleanup (free_format_pieces_cleanup, &fpieces);
2746
2747 format_end = cmdrest;
2748
2749 if (*cmdrest++ != '"')
2750 error (_("Bad format string, non-terminated '\"'."));
2751
2752 cmdrest = skip_spaces (cmdrest);
2753
2754 if (*cmdrest != ',' && *cmdrest != 0)
2755 error (_("Invalid argument syntax"));
2756
2757 if (*cmdrest == ',')
2758 cmdrest++;
2759 cmdrest = skip_spaces (cmdrest);
2760
2761 nargs = 0;
2762 while (*cmdrest != '\0')
2763 {
2764 char *cmd1;
2765
2766 cmd1 = cmdrest;
2767 expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
2768 argvec[nargs] = expr;
2769 ++nargs;
2770 cmdrest = cmd1;
2771 if (*cmdrest == ',')
2772 ++cmdrest;
2773 /* else complain? */
2774 }
2775
2776
2777 agent = gen_printf (get_frame_pc (fi), get_current_arch (), 0, 0,
2778 format_start, format_end - format_start,
2779 fpieces, nargs, argvec);
2780 make_cleanup_free_agent_expr (agent);
2781 ax_reqs (agent);
2782 ax_print (gdb_stdout, agent);
2783
2784 /* It would be nice to call ax_reqs here to gather some general info
2785 about the expression, and then print out the result. */
2786
2787 do_cleanups (old_chain);
2788 dont_repeat ();
2789}
c906108c 2790\f
c5aa993b 2791
c906108c
SS
2792/* Initialization code. */
2793
a14ed312 2794void _initialize_ax_gdb (void);
c906108c 2795void
fba45db2 2796_initialize_ax_gdb (void)
c906108c 2797{
c906108c 2798 add_cmd ("agent", class_maintenance, agent_command,
34b536a8
HZ
2799 _("\
2800Translate an expression into remote agent bytecode for tracing.\n\
2801Usage: maint agent [-at location,] EXPRESSION\n\
2802If -at is given, generate remote agent bytecode for this location.\n\
2803If not, generate remote agent bytecode for current frame pc address."),
782b2b07
SS
2804 &maintenancelist);
2805
2806 add_cmd ("agent-eval", class_maintenance, agent_eval_command,
34b536a8
HZ
2807 _("\
2808Translate an expression into remote agent bytecode for evaluation.\n\
2809Usage: maint agent-eval [-at location,] EXPRESSION\n\
2810If -at is given, generate remote agent bytecode for this location.\n\
2811If not, generate remote agent bytecode for current frame pc address."),
c906108c 2812 &maintenancelist);
d3ce09f5
SS
2813
2814 add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
2815 _("Translate an expression into remote "
2816 "agent bytecode for evaluation and display the bytecodes."),
2817 &maintenancelist);
c906108c 2818}
This page took 0.916146 seconds and 4 git commands to generate.