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