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