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