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