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