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