Commit | Line | Data |
---|---|---|
1bac305b AC |
1 | /* GDB-specific functions for operating on agent expressions. |
2 | ||
4c38e0a4 | 3 | Copyright (C) 1998, 1999, 2000, 2001, 2003, 2007, 2008, 2009, 2010 |
6aba47ca | 4 | Free Software Foundation, 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" |
c906108c | 42 | |
6426a772 JM |
43 | /* To make sense of this file, you should read doc/agentexpr.texi. |
44 | Then look at the types and enums in ax-gdb.h. For the code itself, | |
45 | look at gen_expr, towards the bottom; that's the main function that | |
46 | looks at the GDB expressions and calls everything else to generate | |
47 | code. | |
c906108c SS |
48 | |
49 | I'm beginning to wonder whether it wouldn't be nicer to internally | |
50 | generate trees, with types, and then spit out the bytecode in | |
51 | linear form afterwards; we could generate fewer `swap', `ext', and | |
52 | `zero_ext' bytecodes that way; it would make good constant folding | |
53 | easier, too. But at the moment, I think we should be willing to | |
54 | pay for the simplicity of this code with less-than-optimal bytecode | |
55 | strings. | |
56 | ||
c5aa993b JM |
57 | Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */ |
58 | \f | |
c906108c SS |
59 | |
60 | ||
c906108c SS |
61 | /* Prototypes for local functions. */ |
62 | ||
63 | /* There's a standard order to the arguments of these functions: | |
64 | union exp_element ** --- pointer into expression | |
65 | struct agent_expr * --- agent expression buffer to generate code into | |
66 | struct axs_value * --- describes value left on top of stack */ | |
c5aa993b | 67 | |
a14ed312 KB |
68 | static struct value *const_var_ref (struct symbol *var); |
69 | static struct value *const_expr (union exp_element **pc); | |
70 | static struct value *maybe_const_expr (union exp_element **pc); | |
71 | ||
72 | static void gen_traced_pop (struct agent_expr *, struct axs_value *); | |
73 | ||
74 | static void gen_sign_extend (struct agent_expr *, struct type *); | |
75 | static void gen_extend (struct agent_expr *, struct type *); | |
76 | static void gen_fetch (struct agent_expr *, struct type *); | |
77 | static void gen_left_shift (struct agent_expr *, int); | |
78 | ||
79 | ||
f7c79c41 UW |
80 | static void gen_frame_args_address (struct gdbarch *, struct agent_expr *); |
81 | static void gen_frame_locals_address (struct gdbarch *, struct agent_expr *); | |
a14ed312 KB |
82 | static void gen_offset (struct agent_expr *ax, int offset); |
83 | static void gen_sym_offset (struct agent_expr *, struct symbol *); | |
f7c79c41 | 84 | static void gen_var_ref (struct gdbarch *, struct agent_expr *ax, |
a14ed312 KB |
85 | struct axs_value *value, struct symbol *var); |
86 | ||
87 | ||
88 | static void gen_int_literal (struct agent_expr *ax, | |
89 | struct axs_value *value, | |
90 | LONGEST k, struct type *type); | |
91 | ||
92 | ||
93 | static void require_rvalue (struct agent_expr *ax, struct axs_value *value); | |
f7c79c41 UW |
94 | static void gen_usual_unary (struct expression *exp, struct agent_expr *ax, |
95 | struct axs_value *value); | |
a14ed312 KB |
96 | static int type_wider_than (struct type *type1, struct type *type2); |
97 | static struct type *max_type (struct type *type1, struct type *type2); | |
98 | static void gen_conversion (struct agent_expr *ax, | |
99 | struct type *from, struct type *to); | |
100 | static int is_nontrivial_conversion (struct type *from, struct type *to); | |
f7c79c41 UW |
101 | static void gen_usual_arithmetic (struct expression *exp, |
102 | struct agent_expr *ax, | |
a14ed312 KB |
103 | struct axs_value *value1, |
104 | struct axs_value *value2); | |
f7c79c41 UW |
105 | static void gen_integral_promotions (struct expression *exp, |
106 | struct agent_expr *ax, | |
a14ed312 KB |
107 | struct axs_value *value); |
108 | static void gen_cast (struct agent_expr *ax, | |
109 | struct axs_value *value, struct type *type); | |
110 | static void gen_scale (struct agent_expr *ax, | |
111 | enum agent_op op, struct type *type); | |
f7c79c41 UW |
112 | static void gen_ptradd (struct agent_expr *ax, struct axs_value *value, |
113 | struct axs_value *value1, struct axs_value *value2); | |
114 | static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value, | |
115 | struct axs_value *value1, struct axs_value *value2); | |
116 | static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value, | |
117 | struct axs_value *value1, struct axs_value *value2, | |
118 | struct type *result_type); | |
a14ed312 KB |
119 | static void gen_binop (struct agent_expr *ax, |
120 | struct axs_value *value, | |
121 | struct axs_value *value1, | |
122 | struct axs_value *value2, | |
123 | enum agent_op op, | |
124 | enum agent_op op_unsigned, int may_carry, char *name); | |
f7c79c41 UW |
125 | static void gen_logical_not (struct agent_expr *ax, struct axs_value *value, |
126 | struct type *result_type); | |
a14ed312 KB |
127 | static void gen_complement (struct agent_expr *ax, struct axs_value *value); |
128 | static void gen_deref (struct agent_expr *, struct axs_value *); | |
129 | static void gen_address_of (struct agent_expr *, struct axs_value *); | |
130 | static int find_field (struct type *type, char *name); | |
505e835d | 131 | static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax, |
a14ed312 KB |
132 | struct axs_value *value, |
133 | struct type *type, int start, int end); | |
505e835d | 134 | static void gen_struct_ref (struct expression *exp, struct agent_expr *ax, |
a14ed312 KB |
135 | struct axs_value *value, |
136 | char *field, | |
137 | char *operator_name, char *operand_name); | |
f7c79c41 | 138 | static void gen_repeat (struct expression *exp, union exp_element **pc, |
a14ed312 | 139 | struct agent_expr *ax, struct axs_value *value); |
f7c79c41 UW |
140 | static void gen_sizeof (struct expression *exp, union exp_element **pc, |
141 | struct agent_expr *ax, struct axs_value *value, | |
142 | struct type *size_type); | |
143 | static void gen_expr (struct expression *exp, union exp_element **pc, | |
a14ed312 | 144 | struct agent_expr *ax, struct axs_value *value); |
f61e138d SS |
145 | static void gen_expr_binop_rest (struct expression *exp, |
146 | enum exp_opcode op, union exp_element **pc, | |
147 | struct agent_expr *ax, | |
148 | struct axs_value *value, | |
149 | struct axs_value *value1, | |
150 | struct axs_value *value2); | |
c5aa993b | 151 | |
a14ed312 | 152 | static void agent_command (char *exp, int from_tty); |
c906108c | 153 | \f |
c5aa993b | 154 | |
c906108c SS |
155 | /* Detecting constant expressions. */ |
156 | ||
157 | /* If the variable reference at *PC is a constant, return its value. | |
158 | Otherwise, return zero. | |
159 | ||
160 | Hey, Wally! How can a variable reference be a constant? | |
161 | ||
162 | Well, Beav, this function really handles the OP_VAR_VALUE operator, | |
163 | not specifically variable references. GDB uses OP_VAR_VALUE to | |
164 | refer to any kind of symbolic reference: function names, enum | |
165 | elements, and goto labels are all handled through the OP_VAR_VALUE | |
166 | operator, even though they're constants. It makes sense given the | |
167 | situation. | |
168 | ||
169 | Gee, Wally, don'cha wonder sometimes if data representations that | |
170 | subvert commonly accepted definitions of terms in favor of heavily | |
171 | context-specific interpretations are really just a tool of the | |
172 | programming hegemony to preserve their power and exclude the | |
173 | proletariat? */ | |
174 | ||
175 | static struct value * | |
fba45db2 | 176 | const_var_ref (struct symbol *var) |
c906108c SS |
177 | { |
178 | struct type *type = SYMBOL_TYPE (var); | |
179 | ||
180 | switch (SYMBOL_CLASS (var)) | |
181 | { | |
182 | case LOC_CONST: | |
183 | return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var)); | |
184 | ||
185 | case LOC_LABEL: | |
4478b372 | 186 | return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var)); |
c906108c SS |
187 | |
188 | default: | |
189 | return 0; | |
190 | } | |
191 | } | |
192 | ||
193 | ||
194 | /* If the expression starting at *PC has a constant value, return it. | |
195 | Otherwise, return zero. If we return a value, then *PC will be | |
196 | advanced to the end of it. If we return zero, *PC could be | |
197 | anywhere. */ | |
198 | static struct value * | |
fba45db2 | 199 | const_expr (union exp_element **pc) |
c906108c SS |
200 | { |
201 | enum exp_opcode op = (*pc)->opcode; | |
202 | struct value *v1; | |
203 | ||
204 | switch (op) | |
205 | { | |
206 | case OP_LONG: | |
207 | { | |
208 | struct type *type = (*pc)[1].type; | |
209 | LONGEST k = (*pc)[2].longconst; | |
210 | (*pc) += 4; | |
211 | return value_from_longest (type, k); | |
212 | } | |
213 | ||
214 | case OP_VAR_VALUE: | |
215 | { | |
216 | struct value *v = const_var_ref ((*pc)[2].symbol); | |
217 | (*pc) += 4; | |
218 | return v; | |
219 | } | |
220 | ||
c5aa993b | 221 | /* We could add more operators in here. */ |
c906108c SS |
222 | |
223 | case UNOP_NEG: | |
224 | (*pc)++; | |
225 | v1 = const_expr (pc); | |
226 | if (v1) | |
227 | return value_neg (v1); | |
228 | else | |
229 | return 0; | |
230 | ||
231 | default: | |
232 | return 0; | |
233 | } | |
234 | } | |
235 | ||
236 | ||
237 | /* Like const_expr, but guarantee also that *PC is undisturbed if the | |
238 | expression is not constant. */ | |
239 | static struct value * | |
fba45db2 | 240 | maybe_const_expr (union exp_element **pc) |
c906108c SS |
241 | { |
242 | union exp_element *tentative_pc = *pc; | |
243 | struct value *v = const_expr (&tentative_pc); | |
244 | ||
245 | /* If we got a value, then update the real PC. */ | |
246 | if (v) | |
247 | *pc = tentative_pc; | |
c5aa993b | 248 | |
c906108c SS |
249 | return v; |
250 | } | |
c906108c | 251 | \f |
c5aa993b | 252 | |
c906108c SS |
253 | /* Generating bytecode from GDB expressions: general assumptions */ |
254 | ||
255 | /* Here are a few general assumptions made throughout the code; if you | |
256 | want to make a change that contradicts one of these, then you'd | |
257 | better scan things pretty thoroughly. | |
258 | ||
259 | - We assume that all values occupy one stack element. For example, | |
c5aa993b JM |
260 | sometimes we'll swap to get at the left argument to a binary |
261 | operator. If we decide that void values should occupy no stack | |
262 | elements, or that synthetic arrays (whose size is determined at | |
263 | run time, created by the `@' operator) should occupy two stack | |
264 | elements (address and length), then this will cause trouble. | |
c906108c SS |
265 | |
266 | - We assume the stack elements are infinitely wide, and that we | |
c5aa993b JM |
267 | don't have to worry what happens if the user requests an |
268 | operation that is wider than the actual interpreter's stack. | |
269 | That is, it's up to the interpreter to handle directly all the | |
270 | integer widths the user has access to. (Woe betide the language | |
271 | with bignums!) | |
c906108c SS |
272 | |
273 | - We don't support side effects. Thus, we don't have to worry about | |
c5aa993b | 274 | GCC's generalized lvalues, function calls, etc. |
c906108c SS |
275 | |
276 | - We don't support floating point. Many places where we switch on | |
c5aa993b JM |
277 | some type don't bother to include cases for floating point; there |
278 | may be even more subtle ways this assumption exists. For | |
279 | example, the arguments to % must be integers. | |
c906108c SS |
280 | |
281 | - We assume all subexpressions have a static, unchanging type. If | |
c5aa993b JM |
282 | we tried to support convenience variables, this would be a |
283 | problem. | |
c906108c SS |
284 | |
285 | - All values on the stack should always be fully zero- or | |
c5aa993b JM |
286 | sign-extended. |
287 | ||
288 | (I wasn't sure whether to choose this or its opposite --- that | |
289 | only addresses are assumed extended --- but it turns out that | |
290 | neither convention completely eliminates spurious extend | |
291 | operations (if everything is always extended, then you have to | |
292 | extend after add, because it could overflow; if nothing is | |
293 | extended, then you end up producing extends whenever you change | |
294 | sizes), and this is simpler.) */ | |
c906108c | 295 | \f |
c5aa993b | 296 | |
c906108c SS |
297 | /* Generating bytecode from GDB expressions: the `trace' kludge */ |
298 | ||
299 | /* The compiler in this file is a general-purpose mechanism for | |
300 | translating GDB expressions into bytecode. One ought to be able to | |
301 | find a million and one uses for it. | |
302 | ||
303 | However, at the moment it is HOPELESSLY BRAIN-DAMAGED for the sake | |
304 | of expediency. Let he who is without sin cast the first stone. | |
305 | ||
306 | For the data tracing facility, we need to insert `trace' bytecodes | |
307 | before each data fetch; this records all the memory that the | |
308 | expression touches in the course of evaluation, so that memory will | |
309 | be available when the user later tries to evaluate the expression | |
310 | in GDB. | |
311 | ||
312 | This should be done (I think) in a post-processing pass, that walks | |
313 | an arbitrary agent expression and inserts `trace' operations at the | |
314 | appropriate points. But it's much faster to just hack them | |
315 | directly into the code. And since we're in a crunch, that's what | |
316 | I've done. | |
317 | ||
318 | Setting the flag trace_kludge to non-zero enables the code that | |
319 | emits the trace bytecodes at the appropriate points. */ | |
320 | static int trace_kludge; | |
321 | ||
322 | /* Trace the lvalue on the stack, if it needs it. In either case, pop | |
323 | the value. Useful on the left side of a comma, and at the end of | |
324 | an expression being used for tracing. */ | |
325 | static void | |
fba45db2 | 326 | gen_traced_pop (struct agent_expr *ax, struct axs_value *value) |
c906108c SS |
327 | { |
328 | if (trace_kludge) | |
329 | switch (value->kind) | |
330 | { | |
331 | case axs_rvalue: | |
332 | /* We don't trace rvalues, just the lvalues necessary to | |
c5aa993b | 333 | produce them. So just dispose of this value. */ |
c906108c SS |
334 | ax_simple (ax, aop_pop); |
335 | break; | |
336 | ||
337 | case axs_lvalue_memory: | |
338 | { | |
648027cc | 339 | int length = TYPE_LENGTH (check_typedef (value->type)); |
c906108c SS |
340 | |
341 | /* There's no point in trying to use a trace_quick bytecode | |
342 | here, since "trace_quick SIZE pop" is three bytes, whereas | |
343 | "const8 SIZE trace" is also three bytes, does the same | |
344 | thing, and the simplest code which generates that will also | |
345 | work correctly for objects with large sizes. */ | |
346 | ax_const_l (ax, length); | |
347 | ax_simple (ax, aop_trace); | |
348 | } | |
c5aa993b | 349 | break; |
c906108c SS |
350 | |
351 | case axs_lvalue_register: | |
352 | /* We need to mention the register somewhere in the bytecode, | |
353 | so ax_reqs will pick it up and add it to the mask of | |
354 | registers used. */ | |
355 | ax_reg (ax, value->u.reg); | |
356 | ax_simple (ax, aop_pop); | |
357 | break; | |
358 | } | |
359 | else | |
360 | /* If we're not tracing, just pop the value. */ | |
361 | ax_simple (ax, aop_pop); | |
362 | } | |
c5aa993b | 363 | \f |
c906108c SS |
364 | |
365 | ||
c906108c SS |
366 | /* Generating bytecode from GDB expressions: helper functions */ |
367 | ||
368 | /* Assume that the lower bits of the top of the stack is a value of | |
369 | type TYPE, and the upper bits are zero. Sign-extend if necessary. */ | |
370 | static void | |
fba45db2 | 371 | gen_sign_extend (struct agent_expr *ax, struct type *type) |
c906108c SS |
372 | { |
373 | /* Do we need to sign-extend this? */ | |
c5aa993b | 374 | if (!TYPE_UNSIGNED (type)) |
0004e5a2 | 375 | ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT); |
c906108c SS |
376 | } |
377 | ||
378 | ||
379 | /* Assume the lower bits of the top of the stack hold a value of type | |
380 | TYPE, and the upper bits are garbage. Sign-extend or truncate as | |
381 | needed. */ | |
382 | static void | |
fba45db2 | 383 | gen_extend (struct agent_expr *ax, struct type *type) |
c906108c | 384 | { |
0004e5a2 | 385 | int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT; |
c906108c SS |
386 | /* I just had to. */ |
387 | ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits)); | |
388 | } | |
389 | ||
390 | ||
391 | /* Assume that the top of the stack contains a value of type "pointer | |
392 | to TYPE"; generate code to fetch its value. Note that TYPE is the | |
393 | target type, not the pointer type. */ | |
394 | static void | |
fba45db2 | 395 | gen_fetch (struct agent_expr *ax, struct type *type) |
c906108c SS |
396 | { |
397 | if (trace_kludge) | |
398 | { | |
399 | /* Record the area of memory we're about to fetch. */ | |
400 | ax_trace_quick (ax, TYPE_LENGTH (type)); | |
401 | } | |
402 | ||
0004e5a2 | 403 | switch (TYPE_CODE (type)) |
c906108c SS |
404 | { |
405 | case TYPE_CODE_PTR: | |
b97aedf3 | 406 | case TYPE_CODE_REF: |
c906108c SS |
407 | case TYPE_CODE_ENUM: |
408 | case TYPE_CODE_INT: | |
409 | case TYPE_CODE_CHAR: | |
410 | /* It's a scalar value, so we know how to dereference it. How | |
411 | many bytes long is it? */ | |
0004e5a2 | 412 | switch (TYPE_LENGTH (type)) |
c906108c | 413 | { |
c5aa993b JM |
414 | case 8 / TARGET_CHAR_BIT: |
415 | ax_simple (ax, aop_ref8); | |
416 | break; | |
417 | case 16 / TARGET_CHAR_BIT: | |
418 | ax_simple (ax, aop_ref16); | |
419 | break; | |
420 | case 32 / TARGET_CHAR_BIT: | |
421 | ax_simple (ax, aop_ref32); | |
422 | break; | |
423 | case 64 / TARGET_CHAR_BIT: | |
424 | ax_simple (ax, aop_ref64); | |
425 | break; | |
c906108c SS |
426 | |
427 | /* Either our caller shouldn't have asked us to dereference | |
428 | that pointer (other code's fault), or we're not | |
429 | implementing something we should be (this code's fault). | |
430 | In any case, it's a bug the user shouldn't see. */ | |
431 | default: | |
8e65ff28 | 432 | internal_error (__FILE__, __LINE__, |
3d263c1d | 433 | _("gen_fetch: strange size")); |
c906108c SS |
434 | } |
435 | ||
436 | gen_sign_extend (ax, type); | |
437 | break; | |
438 | ||
439 | default: | |
440 | /* Either our caller shouldn't have asked us to dereference that | |
c5aa993b JM |
441 | pointer (other code's fault), or we're not implementing |
442 | something we should be (this code's fault). In any case, | |
443 | it's a bug the user shouldn't see. */ | |
8e65ff28 | 444 | internal_error (__FILE__, __LINE__, |
3d263c1d | 445 | _("gen_fetch: bad type code")); |
c906108c SS |
446 | } |
447 | } | |
448 | ||
449 | ||
450 | /* Generate code to left shift the top of the stack by DISTANCE bits, or | |
451 | right shift it by -DISTANCE bits if DISTANCE < 0. This generates | |
452 | unsigned (logical) right shifts. */ | |
453 | static void | |
fba45db2 | 454 | gen_left_shift (struct agent_expr *ax, int distance) |
c906108c SS |
455 | { |
456 | if (distance > 0) | |
457 | { | |
458 | ax_const_l (ax, distance); | |
459 | ax_simple (ax, aop_lsh); | |
460 | } | |
461 | else if (distance < 0) | |
462 | { | |
463 | ax_const_l (ax, -distance); | |
464 | ax_simple (ax, aop_rsh_unsigned); | |
465 | } | |
466 | } | |
c5aa993b | 467 | \f |
c906108c SS |
468 | |
469 | ||
c906108c SS |
470 | /* Generating bytecode from GDB expressions: symbol references */ |
471 | ||
472 | /* Generate code to push the base address of the argument portion of | |
473 | the top stack frame. */ | |
474 | static void | |
f7c79c41 | 475 | gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax) |
c906108c | 476 | { |
39d4ef09 AC |
477 | int frame_reg; |
478 | LONGEST frame_offset; | |
c906108c | 479 | |
f7c79c41 | 480 | gdbarch_virtual_frame_pointer (gdbarch, |
c7bb205c | 481 | ax->scope, &frame_reg, &frame_offset); |
c5aa993b | 482 | ax_reg (ax, frame_reg); |
c906108c SS |
483 | gen_offset (ax, frame_offset); |
484 | } | |
485 | ||
486 | ||
487 | /* Generate code to push the base address of the locals portion of the | |
488 | top stack frame. */ | |
489 | static void | |
f7c79c41 | 490 | gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax) |
c906108c | 491 | { |
39d4ef09 AC |
492 | int frame_reg; |
493 | LONGEST frame_offset; | |
c906108c | 494 | |
f7c79c41 | 495 | gdbarch_virtual_frame_pointer (gdbarch, |
c7bb205c | 496 | ax->scope, &frame_reg, &frame_offset); |
c5aa993b | 497 | ax_reg (ax, frame_reg); |
c906108c SS |
498 | gen_offset (ax, frame_offset); |
499 | } | |
500 | ||
501 | ||
502 | /* Generate code to add OFFSET to the top of the stack. Try to | |
503 | generate short and readable code. We use this for getting to | |
504 | variables on the stack, and structure members. If we were | |
505 | programming in ML, it would be clearer why these are the same | |
506 | thing. */ | |
507 | static void | |
fba45db2 | 508 | gen_offset (struct agent_expr *ax, int offset) |
c906108c SS |
509 | { |
510 | /* It would suffice to simply push the offset and add it, but this | |
511 | makes it easier to read positive and negative offsets in the | |
512 | bytecode. */ | |
513 | if (offset > 0) | |
514 | { | |
515 | ax_const_l (ax, offset); | |
516 | ax_simple (ax, aop_add); | |
517 | } | |
518 | else if (offset < 0) | |
519 | { | |
520 | ax_const_l (ax, -offset); | |
521 | ax_simple (ax, aop_sub); | |
522 | } | |
523 | } | |
524 | ||
525 | ||
526 | /* In many cases, a symbol's value is the offset from some other | |
527 | address (stack frame, base register, etc.) Generate code to add | |
528 | VAR's value to the top of the stack. */ | |
529 | static void | |
fba45db2 | 530 | gen_sym_offset (struct agent_expr *ax, struct symbol *var) |
c906108c SS |
531 | { |
532 | gen_offset (ax, SYMBOL_VALUE (var)); | |
533 | } | |
534 | ||
535 | ||
536 | /* Generate code for a variable reference to AX. The variable is the | |
537 | symbol VAR. Set VALUE to describe the result. */ | |
538 | ||
539 | static void | |
f7c79c41 UW |
540 | gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax, |
541 | struct axs_value *value, struct symbol *var) | |
c906108c SS |
542 | { |
543 | /* Dereference any typedefs. */ | |
544 | value->type = check_typedef (SYMBOL_TYPE (var)); | |
545 | ||
546 | /* I'm imitating the code in read_var_value. */ | |
547 | switch (SYMBOL_CLASS (var)) | |
548 | { | |
549 | case LOC_CONST: /* A constant, like an enum value. */ | |
550 | ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var)); | |
551 | value->kind = axs_rvalue; | |
552 | break; | |
553 | ||
554 | case LOC_LABEL: /* A goto label, being used as a value. */ | |
555 | ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var)); | |
556 | value->kind = axs_rvalue; | |
557 | break; | |
558 | ||
559 | case LOC_CONST_BYTES: | |
8e65ff28 | 560 | internal_error (__FILE__, __LINE__, |
3d263c1d | 561 | _("gen_var_ref: LOC_CONST_BYTES symbols are not supported")); |
c906108c SS |
562 | |
563 | /* Variable at a fixed location in memory. Easy. */ | |
564 | case LOC_STATIC: | |
565 | /* Push the address of the variable. */ | |
566 | ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var)); | |
567 | value->kind = axs_lvalue_memory; | |
568 | break; | |
569 | ||
570 | case LOC_ARG: /* var lives in argument area of frame */ | |
f7c79c41 | 571 | gen_frame_args_address (gdbarch, ax); |
c906108c SS |
572 | gen_sym_offset (ax, var); |
573 | value->kind = axs_lvalue_memory; | |
574 | break; | |
575 | ||
576 | case LOC_REF_ARG: /* As above, but the frame slot really | |
577 | holds the address of the variable. */ | |
f7c79c41 | 578 | gen_frame_args_address (gdbarch, ax); |
c906108c SS |
579 | gen_sym_offset (ax, var); |
580 | /* Don't assume any particular pointer size. */ | |
f7c79c41 | 581 | gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr); |
c906108c SS |
582 | value->kind = axs_lvalue_memory; |
583 | break; | |
584 | ||
585 | case LOC_LOCAL: /* var lives in locals area of frame */ | |
f7c79c41 | 586 | gen_frame_locals_address (gdbarch, ax); |
c906108c SS |
587 | gen_sym_offset (ax, var); |
588 | value->kind = axs_lvalue_memory; | |
589 | break; | |
590 | ||
c906108c | 591 | case LOC_TYPEDEF: |
3d263c1d | 592 | error (_("Cannot compute value of typedef `%s'."), |
de5ad195 | 593 | SYMBOL_PRINT_NAME (var)); |
c906108c SS |
594 | break; |
595 | ||
596 | case LOC_BLOCK: | |
597 | ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var))); | |
598 | value->kind = axs_rvalue; | |
599 | break; | |
600 | ||
601 | case LOC_REGISTER: | |
c906108c SS |
602 | /* Don't generate any code at all; in the process of treating |
603 | this as an lvalue or rvalue, the caller will generate the | |
604 | right code. */ | |
605 | value->kind = axs_lvalue_register; | |
768a979c | 606 | value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch); |
c906108c SS |
607 | break; |
608 | ||
609 | /* A lot like LOC_REF_ARG, but the pointer lives directly in a | |
2a2d4dc3 AS |
610 | register, not on the stack. Simpler than LOC_REGISTER |
611 | because it's just like any other case where the thing | |
612 | has a real address. */ | |
c906108c | 613 | case LOC_REGPARM_ADDR: |
768a979c | 614 | ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch)); |
c906108c SS |
615 | value->kind = axs_lvalue_memory; |
616 | break; | |
617 | ||
618 | case LOC_UNRESOLVED: | |
619 | { | |
c5aa993b | 620 | struct minimal_symbol *msym |
3567439c | 621 | = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL); |
c5aa993b | 622 | if (!msym) |
3d263c1d | 623 | error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var)); |
c5aa993b | 624 | |
c906108c SS |
625 | /* Push the address of the variable. */ |
626 | ax_const_l (ax, SYMBOL_VALUE_ADDRESS (msym)); | |
627 | value->kind = axs_lvalue_memory; | |
628 | } | |
c5aa993b | 629 | break; |
c906108c | 630 | |
a55cc764 | 631 | case LOC_COMPUTED: |
a67af2b9 | 632 | /* FIXME: cagney/2004-01-26: It should be possible to |
768a979c | 633 | unconditionally call the SYMBOL_COMPUTED_OPS method when available. |
d3efc286 | 634 | Unfortunately DWARF 2 stores the frame-base (instead of the |
a67af2b9 AC |
635 | function) location in a function's symbol. Oops! For the |
636 | moment enable this when/where applicable. */ | |
505e835d | 637 | SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value); |
a55cc764 DJ |
638 | break; |
639 | ||
c906108c | 640 | case LOC_OPTIMIZED_OUT: |
3d263c1d | 641 | error (_("The variable `%s' has been optimized out."), |
de5ad195 | 642 | SYMBOL_PRINT_NAME (var)); |
c906108c SS |
643 | break; |
644 | ||
645 | default: | |
3d263c1d | 646 | error (_("Cannot find value of botched symbol `%s'."), |
de5ad195 | 647 | SYMBOL_PRINT_NAME (var)); |
c906108c SS |
648 | break; |
649 | } | |
650 | } | |
c5aa993b | 651 | \f |
c906108c SS |
652 | |
653 | ||
c906108c SS |
654 | /* Generating bytecode from GDB expressions: literals */ |
655 | ||
656 | static void | |
fba45db2 KB |
657 | gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k, |
658 | struct type *type) | |
c906108c SS |
659 | { |
660 | ax_const_l (ax, k); | |
661 | value->kind = axs_rvalue; | |
648027cc | 662 | value->type = check_typedef (type); |
c906108c | 663 | } |
c5aa993b | 664 | \f |
c906108c SS |
665 | |
666 | ||
c906108c SS |
667 | /* Generating bytecode from GDB expressions: unary conversions, casts */ |
668 | ||
669 | /* Take what's on the top of the stack (as described by VALUE), and | |
670 | try to make an rvalue out of it. Signal an error if we can't do | |
671 | that. */ | |
672 | static void | |
fba45db2 | 673 | require_rvalue (struct agent_expr *ax, struct axs_value *value) |
c906108c SS |
674 | { |
675 | switch (value->kind) | |
676 | { | |
677 | case axs_rvalue: | |
678 | /* It's already an rvalue. */ | |
679 | break; | |
680 | ||
681 | case axs_lvalue_memory: | |
682 | /* The top of stack is the address of the object. Dereference. */ | |
683 | gen_fetch (ax, value->type); | |
684 | break; | |
685 | ||
686 | case axs_lvalue_register: | |
687 | /* There's nothing on the stack, but value->u.reg is the | |
688 | register number containing the value. | |
689 | ||
c5aa993b JM |
690 | When we add floating-point support, this is going to have to |
691 | change. What about SPARC register pairs, for example? */ | |
c906108c SS |
692 | ax_reg (ax, value->u.reg); |
693 | gen_extend (ax, value->type); | |
694 | break; | |
695 | } | |
696 | ||
697 | value->kind = axs_rvalue; | |
698 | } | |
699 | ||
700 | ||
701 | /* Assume the top of the stack is described by VALUE, and perform the | |
702 | usual unary conversions. This is motivated by ANSI 6.2.2, but of | |
703 | course GDB expressions are not ANSI; they're the mishmash union of | |
704 | a bunch of languages. Rah. | |
705 | ||
706 | NOTE! This function promises to produce an rvalue only when the | |
707 | incoming value is of an appropriate type. In other words, the | |
708 | consumer of the value this function produces may assume the value | |
709 | is an rvalue only after checking its type. | |
710 | ||
711 | The immediate issue is that if the user tries to use a structure or | |
712 | union as an operand of, say, the `+' operator, we don't want to try | |
713 | to convert that structure to an rvalue; require_rvalue will bomb on | |
714 | structs and unions. Rather, we want to simply pass the struct | |
715 | lvalue through unchanged, and let `+' raise an error. */ | |
716 | ||
717 | static void | |
f7c79c41 UW |
718 | gen_usual_unary (struct expression *exp, struct agent_expr *ax, |
719 | struct axs_value *value) | |
c906108c SS |
720 | { |
721 | /* We don't have to generate any code for the usual integral | |
722 | conversions, since values are always represented as full-width on | |
723 | the stack. Should we tweak the type? */ | |
724 | ||
725 | /* Some types require special handling. */ | |
0004e5a2 | 726 | switch (TYPE_CODE (value->type)) |
c906108c SS |
727 | { |
728 | /* Functions get converted to a pointer to the function. */ | |
729 | case TYPE_CODE_FUNC: | |
730 | value->type = lookup_pointer_type (value->type); | |
731 | value->kind = axs_rvalue; /* Should always be true, but just in case. */ | |
732 | break; | |
733 | ||
734 | /* Arrays get converted to a pointer to their first element, and | |
c5aa993b | 735 | are no longer an lvalue. */ |
c906108c SS |
736 | case TYPE_CODE_ARRAY: |
737 | { | |
738 | struct type *elements = TYPE_TARGET_TYPE (value->type); | |
739 | value->type = lookup_pointer_type (elements); | |
740 | value->kind = axs_rvalue; | |
741 | /* We don't need to generate any code; the address of the array | |
742 | is also the address of its first element. */ | |
743 | } | |
c5aa993b | 744 | break; |
c906108c | 745 | |
c5aa993b JM |
746 | /* Don't try to convert structures and unions to rvalues. Let the |
747 | consumer signal an error. */ | |
c906108c SS |
748 | case TYPE_CODE_STRUCT: |
749 | case TYPE_CODE_UNION: | |
750 | return; | |
751 | ||
752 | /* If the value is an enum, call it an integer. */ | |
753 | case TYPE_CODE_ENUM: | |
f7c79c41 | 754 | value->type = builtin_type (exp->gdbarch)->builtin_int; |
c906108c SS |
755 | break; |
756 | } | |
757 | ||
758 | /* If the value is an lvalue, dereference it. */ | |
759 | require_rvalue (ax, value); | |
760 | } | |
761 | ||
762 | ||
763 | /* Return non-zero iff the type TYPE1 is considered "wider" than the | |
764 | type TYPE2, according to the rules described in gen_usual_arithmetic. */ | |
765 | static int | |
fba45db2 | 766 | type_wider_than (struct type *type1, struct type *type2) |
c906108c SS |
767 | { |
768 | return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2) | |
769 | || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2) | |
770 | && TYPE_UNSIGNED (type1) | |
c5aa993b | 771 | && !TYPE_UNSIGNED (type2))); |
c906108c SS |
772 | } |
773 | ||
774 | ||
775 | /* Return the "wider" of the two types TYPE1 and TYPE2. */ | |
776 | static struct type * | |
fba45db2 | 777 | max_type (struct type *type1, struct type *type2) |
c906108c SS |
778 | { |
779 | return type_wider_than (type1, type2) ? type1 : type2; | |
780 | } | |
781 | ||
782 | ||
783 | /* Generate code to convert a scalar value of type FROM to type TO. */ | |
784 | static void | |
fba45db2 | 785 | gen_conversion (struct agent_expr *ax, struct type *from, struct type *to) |
c906108c SS |
786 | { |
787 | /* Perhaps there is a more graceful way to state these rules. */ | |
788 | ||
789 | /* If we're converting to a narrower type, then we need to clear out | |
790 | the upper bits. */ | |
791 | if (TYPE_LENGTH (to) < TYPE_LENGTH (from)) | |
792 | gen_extend (ax, from); | |
793 | ||
794 | /* If the two values have equal width, but different signednesses, | |
795 | then we need to extend. */ | |
796 | else if (TYPE_LENGTH (to) == TYPE_LENGTH (from)) | |
797 | { | |
798 | if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to)) | |
799 | gen_extend (ax, to); | |
800 | } | |
801 | ||
802 | /* If we're converting to a wider type, and becoming unsigned, then | |
803 | we need to zero out any possible sign bits. */ | |
804 | else if (TYPE_LENGTH (to) > TYPE_LENGTH (from)) | |
805 | { | |
806 | if (TYPE_UNSIGNED (to)) | |
807 | gen_extend (ax, to); | |
808 | } | |
809 | } | |
810 | ||
811 | ||
812 | /* Return non-zero iff the type FROM will require any bytecodes to be | |
813 | emitted to be converted to the type TO. */ | |
814 | static int | |
fba45db2 | 815 | is_nontrivial_conversion (struct type *from, struct type *to) |
c906108c SS |
816 | { |
817 | struct agent_expr *ax = new_agent_expr (0); | |
818 | int nontrivial; | |
819 | ||
820 | /* Actually generate the code, and see if anything came out. At the | |
821 | moment, it would be trivial to replicate the code in | |
822 | gen_conversion here, but in the future, when we're supporting | |
823 | floating point and the like, it may not be. Doing things this | |
824 | way allows this function to be independent of the logic in | |
825 | gen_conversion. */ | |
826 | gen_conversion (ax, from, to); | |
827 | nontrivial = ax->len > 0; | |
828 | free_agent_expr (ax); | |
829 | return nontrivial; | |
830 | } | |
831 | ||
832 | ||
833 | /* Generate code to perform the "usual arithmetic conversions" (ANSI C | |
834 | 6.2.1.5) for the two operands of an arithmetic operator. This | |
835 | effectively finds a "least upper bound" type for the two arguments, | |
836 | and promotes each argument to that type. *VALUE1 and *VALUE2 | |
837 | describe the values as they are passed in, and as they are left. */ | |
838 | static void | |
f7c79c41 UW |
839 | gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax, |
840 | struct axs_value *value1, struct axs_value *value2) | |
c906108c SS |
841 | { |
842 | /* Do the usual binary conversions. */ | |
843 | if (TYPE_CODE (value1->type) == TYPE_CODE_INT | |
844 | && TYPE_CODE (value2->type) == TYPE_CODE_INT) | |
845 | { | |
846 | /* The ANSI integral promotions seem to work this way: Order the | |
c5aa993b JM |
847 | integer types by size, and then by signedness: an n-bit |
848 | unsigned type is considered "wider" than an n-bit signed | |
849 | type. Promote to the "wider" of the two types, and always | |
850 | promote at least to int. */ | |
f7c79c41 | 851 | struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int, |
c906108c SS |
852 | max_type (value1->type, value2->type)); |
853 | ||
854 | /* Deal with value2, on the top of the stack. */ | |
855 | gen_conversion (ax, value2->type, target); | |
856 | ||
857 | /* Deal with value1, not on the top of the stack. Don't | |
858 | generate the `swap' instructions if we're not actually going | |
859 | to do anything. */ | |
860 | if (is_nontrivial_conversion (value1->type, target)) | |
861 | { | |
862 | ax_simple (ax, aop_swap); | |
863 | gen_conversion (ax, value1->type, target); | |
864 | ax_simple (ax, aop_swap); | |
865 | } | |
866 | ||
648027cc | 867 | value1->type = value2->type = check_typedef (target); |
c906108c SS |
868 | } |
869 | } | |
870 | ||
871 | ||
872 | /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on | |
873 | the value on the top of the stack, as described by VALUE. Assume | |
874 | the value has integral type. */ | |
875 | static void | |
f7c79c41 UW |
876 | gen_integral_promotions (struct expression *exp, struct agent_expr *ax, |
877 | struct axs_value *value) | |
c906108c | 878 | { |
f7c79c41 UW |
879 | const struct builtin_type *builtin = builtin_type (exp->gdbarch); |
880 | ||
881 | if (!type_wider_than (value->type, builtin->builtin_int)) | |
c906108c | 882 | { |
f7c79c41 UW |
883 | gen_conversion (ax, value->type, builtin->builtin_int); |
884 | value->type = builtin->builtin_int; | |
c906108c | 885 | } |
f7c79c41 | 886 | else if (!type_wider_than (value->type, builtin->builtin_unsigned_int)) |
c906108c | 887 | { |
f7c79c41 UW |
888 | gen_conversion (ax, value->type, builtin->builtin_unsigned_int); |
889 | value->type = builtin->builtin_unsigned_int; | |
c906108c SS |
890 | } |
891 | } | |
892 | ||
893 | ||
894 | /* Generate code for a cast to TYPE. */ | |
895 | static void | |
fba45db2 | 896 | gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type) |
c906108c SS |
897 | { |
898 | /* GCC does allow casts to yield lvalues, so this should be fixed | |
899 | before merging these changes into the trunk. */ | |
900 | require_rvalue (ax, value); | |
901 | /* Dereference typedefs. */ | |
902 | type = check_typedef (type); | |
903 | ||
0004e5a2 | 904 | switch (TYPE_CODE (type)) |
c906108c SS |
905 | { |
906 | case TYPE_CODE_PTR: | |
b97aedf3 | 907 | case TYPE_CODE_REF: |
c906108c SS |
908 | /* It's implementation-defined, and I'll bet this is what GCC |
909 | does. */ | |
910 | break; | |
911 | ||
912 | case TYPE_CODE_ARRAY: | |
913 | case TYPE_CODE_STRUCT: | |
914 | case TYPE_CODE_UNION: | |
915 | case TYPE_CODE_FUNC: | |
3d263c1d | 916 | error (_("Invalid type cast: intended type must be scalar.")); |
c906108c SS |
917 | |
918 | case TYPE_CODE_ENUM: | |
919 | /* We don't have to worry about the size of the value, because | |
920 | all our integral values are fully sign-extended, and when | |
921 | casting pointers we can do anything we like. Is there any | |
74b35824 JB |
922 | way for us to know what GCC actually does with a cast like |
923 | this? */ | |
c906108c | 924 | break; |
c5aa993b | 925 | |
c906108c SS |
926 | case TYPE_CODE_INT: |
927 | gen_conversion (ax, value->type, type); | |
928 | break; | |
929 | ||
930 | case TYPE_CODE_VOID: | |
931 | /* We could pop the value, and rely on everyone else to check | |
c5aa993b JM |
932 | the type and notice that this value doesn't occupy a stack |
933 | slot. But for now, leave the value on the stack, and | |
934 | preserve the "value == stack element" assumption. */ | |
c906108c SS |
935 | break; |
936 | ||
937 | default: | |
3d263c1d | 938 | error (_("Casts to requested type are not yet implemented.")); |
c906108c SS |
939 | } |
940 | ||
941 | value->type = type; | |
942 | } | |
c5aa993b | 943 | \f |
c906108c SS |
944 | |
945 | ||
c906108c SS |
946 | /* Generating bytecode from GDB expressions: arithmetic */ |
947 | ||
948 | /* Scale the integer on the top of the stack by the size of the target | |
949 | of the pointer type TYPE. */ | |
950 | static void | |
fba45db2 | 951 | gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type) |
c906108c SS |
952 | { |
953 | struct type *element = TYPE_TARGET_TYPE (type); | |
954 | ||
0004e5a2 | 955 | if (TYPE_LENGTH (element) != 1) |
c906108c | 956 | { |
0004e5a2 | 957 | ax_const_l (ax, TYPE_LENGTH (element)); |
c906108c SS |
958 | ax_simple (ax, op); |
959 | } | |
960 | } | |
961 | ||
962 | ||
f7c79c41 | 963 | /* Generate code for pointer arithmetic PTR + INT. */ |
c906108c | 964 | static void |
f7c79c41 UW |
965 | gen_ptradd (struct agent_expr *ax, struct axs_value *value, |
966 | struct axs_value *value1, struct axs_value *value2) | |
c906108c | 967 | { |
b97aedf3 | 968 | gdb_assert (pointer_type (value1->type)); |
f7c79c41 | 969 | gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT); |
c906108c | 970 | |
f7c79c41 UW |
971 | gen_scale (ax, aop_mul, value1->type); |
972 | ax_simple (ax, aop_add); | |
973 | gen_extend (ax, value1->type); /* Catch overflow. */ | |
974 | value->type = value1->type; | |
975 | value->kind = axs_rvalue; | |
976 | } | |
c906108c | 977 | |
c906108c | 978 | |
f7c79c41 UW |
979 | /* Generate code for pointer arithmetic PTR - INT. */ |
980 | static void | |
981 | gen_ptrsub (struct agent_expr *ax, struct axs_value *value, | |
982 | struct axs_value *value1, struct axs_value *value2) | |
983 | { | |
b97aedf3 | 984 | gdb_assert (pointer_type (value1->type)); |
f7c79c41 | 985 | gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT); |
c906108c | 986 | |
f7c79c41 UW |
987 | gen_scale (ax, aop_mul, value1->type); |
988 | ax_simple (ax, aop_sub); | |
989 | gen_extend (ax, value1->type); /* Catch overflow. */ | |
990 | value->type = value1->type; | |
c906108c SS |
991 | value->kind = axs_rvalue; |
992 | } | |
993 | ||
994 | ||
f7c79c41 | 995 | /* Generate code for pointer arithmetic PTR - PTR. */ |
c906108c | 996 | static void |
f7c79c41 UW |
997 | gen_ptrdiff (struct agent_expr *ax, struct axs_value *value, |
998 | struct axs_value *value1, struct axs_value *value2, | |
999 | struct type *result_type) | |
c906108c | 1000 | { |
b97aedf3 SS |
1001 | gdb_assert (pointer_type (value1->type)); |
1002 | gdb_assert (pointer_type (value2->type)); | |
c906108c | 1003 | |
f7c79c41 UW |
1004 | if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type)) |
1005 | != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type))) | |
1006 | error (_("\ | |
c906108c | 1007 | First argument of `-' is a pointer, but second argument is neither\n\ |
3d263c1d | 1008 | an integer nor a pointer of the same type.")); |
c906108c | 1009 | |
f7c79c41 UW |
1010 | ax_simple (ax, aop_sub); |
1011 | gen_scale (ax, aop_div_unsigned, value1->type); | |
1012 | value->type = result_type; | |
c906108c SS |
1013 | value->kind = axs_rvalue; |
1014 | } | |
1015 | ||
f7c79c41 | 1016 | |
c906108c SS |
1017 | /* Generate code for a binary operator that doesn't do pointer magic. |
1018 | We set VALUE to describe the result value; we assume VALUE1 and | |
1019 | VALUE2 describe the two operands, and that they've undergone the | |
1020 | usual binary conversions. MAY_CARRY should be non-zero iff the | |
1021 | result needs to be extended. NAME is the English name of the | |
1022 | operator, used in error messages */ | |
1023 | static void | |
fba45db2 KB |
1024 | gen_binop (struct agent_expr *ax, struct axs_value *value, |
1025 | struct axs_value *value1, struct axs_value *value2, enum agent_op op, | |
1026 | enum agent_op op_unsigned, int may_carry, char *name) | |
c906108c SS |
1027 | { |
1028 | /* We only handle INT op INT. */ | |
0004e5a2 DJ |
1029 | if ((TYPE_CODE (value1->type) != TYPE_CODE_INT) |
1030 | || (TYPE_CODE (value2->type) != TYPE_CODE_INT)) | |
3d263c1d | 1031 | error (_("Invalid combination of types in %s."), name); |
c5aa993b | 1032 | |
c906108c SS |
1033 | ax_simple (ax, |
1034 | TYPE_UNSIGNED (value1->type) ? op_unsigned : op); | |
1035 | if (may_carry) | |
c5aa993b | 1036 | gen_extend (ax, value1->type); /* catch overflow */ |
c906108c SS |
1037 | value->type = value1->type; |
1038 | value->kind = axs_rvalue; | |
1039 | } | |
1040 | ||
1041 | ||
1042 | static void | |
f7c79c41 UW |
1043 | gen_logical_not (struct agent_expr *ax, struct axs_value *value, |
1044 | struct type *result_type) | |
c906108c SS |
1045 | { |
1046 | if (TYPE_CODE (value->type) != TYPE_CODE_INT | |
1047 | && TYPE_CODE (value->type) != TYPE_CODE_PTR) | |
3d263c1d | 1048 | error (_("Invalid type of operand to `!'.")); |
c906108c | 1049 | |
c906108c | 1050 | ax_simple (ax, aop_log_not); |
f7c79c41 | 1051 | value->type = result_type; |
c906108c SS |
1052 | } |
1053 | ||
1054 | ||
1055 | static void | |
fba45db2 | 1056 | gen_complement (struct agent_expr *ax, struct axs_value *value) |
c906108c SS |
1057 | { |
1058 | if (TYPE_CODE (value->type) != TYPE_CODE_INT) | |
3d263c1d | 1059 | error (_("Invalid type of operand to `~'.")); |
c906108c | 1060 | |
c906108c SS |
1061 | ax_simple (ax, aop_bit_not); |
1062 | gen_extend (ax, value->type); | |
1063 | } | |
c5aa993b | 1064 | \f |
c906108c SS |
1065 | |
1066 | ||
c906108c SS |
1067 | /* Generating bytecode from GDB expressions: * & . -> @ sizeof */ |
1068 | ||
1069 | /* Dereference the value on the top of the stack. */ | |
1070 | static void | |
fba45db2 | 1071 | gen_deref (struct agent_expr *ax, struct axs_value *value) |
c906108c SS |
1072 | { |
1073 | /* The caller should check the type, because several operators use | |
1074 | this, and we don't know what error message to generate. */ | |
b97aedf3 | 1075 | if (!pointer_type (value->type)) |
8e65ff28 | 1076 | internal_error (__FILE__, __LINE__, |
3d263c1d | 1077 | _("gen_deref: expected a pointer")); |
c906108c SS |
1078 | |
1079 | /* We've got an rvalue now, which is a pointer. We want to yield an | |
1080 | lvalue, whose address is exactly that pointer. So we don't | |
1081 | actually emit any code; we just change the type from "Pointer to | |
1082 | T" to "T", and mark the value as an lvalue in memory. Leave it | |
1083 | to the consumer to actually dereference it. */ | |
1084 | value->type = check_typedef (TYPE_TARGET_TYPE (value->type)); | |
0004e5a2 | 1085 | value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC) |
c906108c SS |
1086 | ? axs_rvalue : axs_lvalue_memory); |
1087 | } | |
1088 | ||
1089 | ||
1090 | /* Produce the address of the lvalue on the top of the stack. */ | |
1091 | static void | |
fba45db2 | 1092 | gen_address_of (struct agent_expr *ax, struct axs_value *value) |
c906108c SS |
1093 | { |
1094 | /* Special case for taking the address of a function. The ANSI | |
1095 | standard describes this as a special case, too, so this | |
1096 | arrangement is not without motivation. */ | |
0004e5a2 | 1097 | if (TYPE_CODE (value->type) == TYPE_CODE_FUNC) |
c906108c SS |
1098 | /* The value's already an rvalue on the stack, so we just need to |
1099 | change the type. */ | |
1100 | value->type = lookup_pointer_type (value->type); | |
1101 | else | |
1102 | switch (value->kind) | |
1103 | { | |
1104 | case axs_rvalue: | |
3d263c1d | 1105 | error (_("Operand of `&' is an rvalue, which has no address.")); |
c906108c SS |
1106 | |
1107 | case axs_lvalue_register: | |
3d263c1d | 1108 | error (_("Operand of `&' is in a register, and has no address.")); |
c906108c SS |
1109 | |
1110 | case axs_lvalue_memory: | |
1111 | value->kind = axs_rvalue; | |
1112 | value->type = lookup_pointer_type (value->type); | |
1113 | break; | |
1114 | } | |
1115 | } | |
1116 | ||
1117 | ||
1118 | /* A lot of this stuff will have to change to support C++. But we're | |
1119 | not going to deal with that at the moment. */ | |
1120 | ||
1121 | /* Find the field in the structure type TYPE named NAME, and return | |
1122 | its index in TYPE's field array. */ | |
1123 | static int | |
fba45db2 | 1124 | find_field (struct type *type, char *name) |
c906108c SS |
1125 | { |
1126 | int i; | |
1127 | ||
1128 | CHECK_TYPEDEF (type); | |
1129 | ||
1130 | /* Make sure this isn't C++. */ | |
1131 | if (TYPE_N_BASECLASSES (type) != 0) | |
8e65ff28 | 1132 | internal_error (__FILE__, __LINE__, |
3d263c1d | 1133 | _("find_field: derived classes supported")); |
c906108c SS |
1134 | |
1135 | for (i = 0; i < TYPE_NFIELDS (type); i++) | |
1136 | { | |
1137 | char *this_name = TYPE_FIELD_NAME (type, i); | |
1138 | ||
747f3d18 MS |
1139 | if (this_name) |
1140 | { | |
1141 | if (strcmp (name, this_name) == 0) | |
1142 | return i; | |
c906108c | 1143 | |
747f3d18 MS |
1144 | if (this_name[0] == '\0') |
1145 | internal_error (__FILE__, __LINE__, | |
1146 | _("find_field: anonymous unions not supported")); | |
1147 | } | |
c906108c SS |
1148 | } |
1149 | ||
3d263c1d | 1150 | error (_("Couldn't find member named `%s' in struct/union `%s'"), |
7495dfdb | 1151 | name, TYPE_TAG_NAME (type)); |
c906108c SS |
1152 | |
1153 | return 0; | |
1154 | } | |
1155 | ||
1156 | ||
1157 | /* Generate code to push the value of a bitfield of a structure whose | |
1158 | address is on the top of the stack. START and END give the | |
1159 | starting and one-past-ending *bit* numbers of the field within the | |
1160 | structure. */ | |
1161 | static void | |
505e835d UW |
1162 | gen_bitfield_ref (struct expression *exp, struct agent_expr *ax, |
1163 | struct axs_value *value, struct type *type, | |
1164 | int start, int end) | |
c906108c SS |
1165 | { |
1166 | /* Note that ops[i] fetches 8 << i bits. */ | |
1167 | static enum agent_op ops[] | |
c5aa993b JM |
1168 | = |
1169 | {aop_ref8, aop_ref16, aop_ref32, aop_ref64}; | |
c906108c SS |
1170 | static int num_ops = (sizeof (ops) / sizeof (ops[0])); |
1171 | ||
1172 | /* We don't want to touch any byte that the bitfield doesn't | |
1173 | actually occupy; we shouldn't make any accesses we're not | |
1174 | explicitly permitted to. We rely here on the fact that the | |
1175 | bytecode `ref' operators work on unaligned addresses. | |
1176 | ||
1177 | It takes some fancy footwork to get the stack to work the way | |
1178 | we'd like. Say we're retrieving a bitfield that requires three | |
1179 | fetches. Initially, the stack just contains the address: | |
c5aa993b | 1180 | addr |
c906108c | 1181 | For the first fetch, we duplicate the address |
c5aa993b | 1182 | addr addr |
c906108c SS |
1183 | then add the byte offset, do the fetch, and shift and mask as |
1184 | needed, yielding a fragment of the value, properly aligned for | |
1185 | the final bitwise or: | |
c5aa993b | 1186 | addr frag1 |
c906108c | 1187 | then we swap, and repeat the process: |
c5aa993b JM |
1188 | frag1 addr --- address on top |
1189 | frag1 addr addr --- duplicate it | |
1190 | frag1 addr frag2 --- get second fragment | |
1191 | frag1 frag2 addr --- swap again | |
1192 | frag1 frag2 frag3 --- get third fragment | |
c906108c SS |
1193 | Notice that, since the third fragment is the last one, we don't |
1194 | bother duplicating the address this time. Now we have all the | |
1195 | fragments on the stack, and we can simply `or' them together, | |
1196 | yielding the final value of the bitfield. */ | |
1197 | ||
1198 | /* The first and one-after-last bits in the field, but rounded down | |
1199 | and up to byte boundaries. */ | |
1200 | int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT; | |
c5aa993b JM |
1201 | int bound_end = (((end + TARGET_CHAR_BIT - 1) |
1202 | / TARGET_CHAR_BIT) | |
1203 | * TARGET_CHAR_BIT); | |
c906108c SS |
1204 | |
1205 | /* current bit offset within the structure */ | |
1206 | int offset; | |
1207 | ||
1208 | /* The index in ops of the opcode we're considering. */ | |
1209 | int op; | |
1210 | ||
1211 | /* The number of fragments we generated in the process. Probably | |
1212 | equal to the number of `one' bits in bytesize, but who cares? */ | |
1213 | int fragment_count; | |
1214 | ||
1215 | /* Dereference any typedefs. */ | |
1216 | type = check_typedef (type); | |
1217 | ||
1218 | /* Can we fetch the number of bits requested at all? */ | |
1219 | if ((end - start) > ((1 << num_ops) * 8)) | |
8e65ff28 | 1220 | internal_error (__FILE__, __LINE__, |
3d263c1d | 1221 | _("gen_bitfield_ref: bitfield too wide")); |
c906108c SS |
1222 | |
1223 | /* Note that we know here that we only need to try each opcode once. | |
1224 | That may not be true on machines with weird byte sizes. */ | |
1225 | offset = bound_start; | |
1226 | fragment_count = 0; | |
1227 | for (op = num_ops - 1; op >= 0; op--) | |
1228 | { | |
1229 | /* number of bits that ops[op] would fetch */ | |
1230 | int op_size = 8 << op; | |
1231 | ||
1232 | /* The stack at this point, from bottom to top, contains zero or | |
c5aa993b JM |
1233 | more fragments, then the address. */ |
1234 | ||
c906108c SS |
1235 | /* Does this fetch fit within the bitfield? */ |
1236 | if (offset + op_size <= bound_end) | |
1237 | { | |
1238 | /* Is this the last fragment? */ | |
1239 | int last_frag = (offset + op_size == bound_end); | |
1240 | ||
c5aa993b JM |
1241 | if (!last_frag) |
1242 | ax_simple (ax, aop_dup); /* keep a copy of the address */ | |
1243 | ||
c906108c SS |
1244 | /* Add the offset. */ |
1245 | gen_offset (ax, offset / TARGET_CHAR_BIT); | |
1246 | ||
1247 | if (trace_kludge) | |
1248 | { | |
1249 | /* Record the area of memory we're about to fetch. */ | |
1250 | ax_trace_quick (ax, op_size / TARGET_CHAR_BIT); | |
1251 | } | |
1252 | ||
1253 | /* Perform the fetch. */ | |
1254 | ax_simple (ax, ops[op]); | |
c5aa993b JM |
1255 | |
1256 | /* Shift the bits we have to their proper position. | |
c906108c SS |
1257 | gen_left_shift will generate right shifts when the operand |
1258 | is negative. | |
1259 | ||
c5aa993b JM |
1260 | A big-endian field diagram to ponder: |
1261 | byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7 | |
1262 | +------++------++------++------++------++------++------++------+ | |
1263 | xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx | |
1264 | ^ ^ ^ ^ | |
1265 | bit number 16 32 48 53 | |
c906108c SS |
1266 | These are bit numbers as supplied by GDB. Note that the |
1267 | bit numbers run from right to left once you've fetched the | |
1268 | value! | |
1269 | ||
c5aa993b JM |
1270 | A little-endian field diagram to ponder: |
1271 | byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0 | |
1272 | +------++------++------++------++------++------++------++------+ | |
1273 | xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx | |
1274 | ^ ^ ^ ^ ^ | |
1275 | bit number 48 32 16 4 0 | |
1276 | ||
1277 | In both cases, the most significant end is on the left | |
1278 | (i.e. normal numeric writing order), which means that you | |
1279 | don't go crazy thinking about `left' and `right' shifts. | |
1280 | ||
1281 | We don't have to worry about masking yet: | |
1282 | - If they contain garbage off the least significant end, then we | |
1283 | must be looking at the low end of the field, and the right | |
1284 | shift will wipe them out. | |
1285 | - If they contain garbage off the most significant end, then we | |
1286 | must be looking at the most significant end of the word, and | |
1287 | the sign/zero extension will wipe them out. | |
1288 | - If we're in the interior of the word, then there is no garbage | |
1289 | on either end, because the ref operators zero-extend. */ | |
505e835d | 1290 | if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG) |
c906108c | 1291 | gen_left_shift (ax, end - (offset + op_size)); |
c5aa993b | 1292 | else |
c906108c SS |
1293 | gen_left_shift (ax, offset - start); |
1294 | ||
c5aa993b | 1295 | if (!last_frag) |
c906108c SS |
1296 | /* Bring the copy of the address up to the top. */ |
1297 | ax_simple (ax, aop_swap); | |
1298 | ||
1299 | offset += op_size; | |
1300 | fragment_count++; | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | /* Generate enough bitwise `or' operations to combine all the | |
1305 | fragments we left on the stack. */ | |
1306 | while (fragment_count-- > 1) | |
1307 | ax_simple (ax, aop_bit_or); | |
1308 | ||
1309 | /* Sign- or zero-extend the value as appropriate. */ | |
1310 | ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start)); | |
1311 | ||
1312 | /* This is *not* an lvalue. Ugh. */ | |
1313 | value->kind = axs_rvalue; | |
1314 | value->type = type; | |
1315 | } | |
1316 | ||
1317 | ||
1318 | /* Generate code to reference the member named FIELD of a structure or | |
1319 | union. The top of the stack, as described by VALUE, should have | |
1320 | type (pointer to a)* struct/union. OPERATOR_NAME is the name of | |
1321 | the operator being compiled, and OPERAND_NAME is the kind of thing | |
1322 | it operates on; we use them in error messages. */ | |
1323 | static void | |
505e835d UW |
1324 | gen_struct_ref (struct expression *exp, struct agent_expr *ax, |
1325 | struct axs_value *value, char *field, | |
fba45db2 | 1326 | char *operator_name, char *operand_name) |
c906108c SS |
1327 | { |
1328 | struct type *type; | |
1329 | int i; | |
1330 | ||
1331 | /* Follow pointers until we reach a non-pointer. These aren't the C | |
1332 | semantics, but they're what the normal GDB evaluator does, so we | |
1333 | should at least be consistent. */ | |
b97aedf3 | 1334 | while (pointer_type (value->type)) |
c906108c | 1335 | { |
f7c79c41 | 1336 | require_rvalue (ax, value); |
c906108c SS |
1337 | gen_deref (ax, value); |
1338 | } | |
e8860ec2 | 1339 | type = check_typedef (value->type); |
c906108c SS |
1340 | |
1341 | /* This must yield a structure or a union. */ | |
1342 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT | |
1343 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
3d263c1d | 1344 | error (_("The left operand of `%s' is not a %s."), |
c906108c SS |
1345 | operator_name, operand_name); |
1346 | ||
1347 | /* And it must be in memory; we don't deal with structure rvalues, | |
1348 | or structures living in registers. */ | |
1349 | if (value->kind != axs_lvalue_memory) | |
3d263c1d | 1350 | error (_("Structure does not live in memory.")); |
c906108c SS |
1351 | |
1352 | i = find_field (type, field); | |
c5aa993b | 1353 | |
c906108c SS |
1354 | /* Is this a bitfield? */ |
1355 | if (TYPE_FIELD_PACKED (type, i)) | |
505e835d | 1356 | gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, i), |
c906108c SS |
1357 | TYPE_FIELD_BITPOS (type, i), |
1358 | (TYPE_FIELD_BITPOS (type, i) | |
1359 | + TYPE_FIELD_BITSIZE (type, i))); | |
1360 | else | |
1361 | { | |
1362 | gen_offset (ax, TYPE_FIELD_BITPOS (type, i) / TARGET_CHAR_BIT); | |
1363 | value->kind = axs_lvalue_memory; | |
1364 | value->type = TYPE_FIELD_TYPE (type, i); | |
1365 | } | |
1366 | } | |
1367 | ||
1368 | ||
1369 | /* Generate code for GDB's magical `repeat' operator. | |
1370 | LVALUE @ INT creates an array INT elements long, and whose elements | |
1371 | have the same type as LVALUE, located in memory so that LVALUE is | |
1372 | its first element. For example, argv[0]@argc gives you the array | |
1373 | of command-line arguments. | |
1374 | ||
1375 | Unfortunately, because we have to know the types before we actually | |
1376 | have a value for the expression, we can't implement this perfectly | |
1377 | without changing the type system, having values that occupy two | |
1378 | stack slots, doing weird things with sizeof, etc. So we require | |
1379 | the right operand to be a constant expression. */ | |
1380 | static void | |
f7c79c41 UW |
1381 | gen_repeat (struct expression *exp, union exp_element **pc, |
1382 | struct agent_expr *ax, struct axs_value *value) | |
c906108c SS |
1383 | { |
1384 | struct axs_value value1; | |
1385 | /* We don't want to turn this into an rvalue, so no conversions | |
1386 | here. */ | |
f7c79c41 | 1387 | gen_expr (exp, pc, ax, &value1); |
c906108c | 1388 | if (value1.kind != axs_lvalue_memory) |
3d263c1d | 1389 | error (_("Left operand of `@' must be an object in memory.")); |
c906108c SS |
1390 | |
1391 | /* Evaluate the length; it had better be a constant. */ | |
1392 | { | |
1393 | struct value *v = const_expr (pc); | |
1394 | int length; | |
1395 | ||
c5aa993b | 1396 | if (!v) |
3d263c1d | 1397 | error (_("Right operand of `@' must be a constant, in agent expressions.")); |
04624583 | 1398 | if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT) |
3d263c1d | 1399 | error (_("Right operand of `@' must be an integer.")); |
c906108c SS |
1400 | length = value_as_long (v); |
1401 | if (length <= 0) | |
3d263c1d | 1402 | error (_("Right operand of `@' must be positive.")); |
c906108c SS |
1403 | |
1404 | /* The top of the stack is already the address of the object, so | |
1405 | all we need to do is frob the type of the lvalue. */ | |
1406 | { | |
1407 | /* FIXME-type-allocation: need a way to free this type when we are | |
c5aa993b | 1408 | done with it. */ |
e3506a9f UW |
1409 | struct type *array |
1410 | = lookup_array_range_type (value1.type, 0, length - 1); | |
c906108c SS |
1411 | |
1412 | value->kind = axs_lvalue_memory; | |
1413 | value->type = array; | |
1414 | } | |
1415 | } | |
1416 | } | |
1417 | ||
1418 | ||
1419 | /* Emit code for the `sizeof' operator. | |
1420 | *PC should point at the start of the operand expression; we advance it | |
1421 | to the first instruction after the operand. */ | |
1422 | static void | |
f7c79c41 UW |
1423 | gen_sizeof (struct expression *exp, union exp_element **pc, |
1424 | struct agent_expr *ax, struct axs_value *value, | |
1425 | struct type *size_type) | |
c906108c SS |
1426 | { |
1427 | /* We don't care about the value of the operand expression; we only | |
1428 | care about its type. However, in the current arrangement, the | |
1429 | only way to find an expression's type is to generate code for it. | |
1430 | So we generate code for the operand, and then throw it away, | |
1431 | replacing it with code that simply pushes its size. */ | |
1432 | int start = ax->len; | |
f7c79c41 | 1433 | gen_expr (exp, pc, ax, value); |
c906108c SS |
1434 | |
1435 | /* Throw away the code we just generated. */ | |
1436 | ax->len = start; | |
c5aa993b | 1437 | |
c906108c SS |
1438 | ax_const_l (ax, TYPE_LENGTH (value->type)); |
1439 | value->kind = axs_rvalue; | |
f7c79c41 | 1440 | value->type = size_type; |
c906108c | 1441 | } |
c906108c | 1442 | \f |
c5aa993b | 1443 | |
c906108c SS |
1444 | /* Generating bytecode from GDB expressions: general recursive thingy */ |
1445 | ||
3d263c1d | 1446 | /* XXX: i18n */ |
c906108c SS |
1447 | /* A gen_expr function written by a Gen-X'er guy. |
1448 | Append code for the subexpression of EXPR starting at *POS_P to AX. */ | |
1449 | static void | |
f7c79c41 UW |
1450 | gen_expr (struct expression *exp, union exp_element **pc, |
1451 | struct agent_expr *ax, struct axs_value *value) | |
c906108c SS |
1452 | { |
1453 | /* Used to hold the descriptions of operand expressions. */ | |
09d559e4 | 1454 | struct axs_value value1, value2, value3; |
f61e138d | 1455 | enum exp_opcode op = (*pc)[0].opcode, op2; |
09d559e4 | 1456 | int if1, go1, if2, go2, end; |
c906108c SS |
1457 | |
1458 | /* If we're looking at a constant expression, just push its value. */ | |
1459 | { | |
1460 | struct value *v = maybe_const_expr (pc); | |
c5aa993b | 1461 | |
c906108c SS |
1462 | if (v) |
1463 | { | |
1464 | ax_const_l (ax, value_as_long (v)); | |
1465 | value->kind = axs_rvalue; | |
df407dfe | 1466 | value->type = check_typedef (value_type (v)); |
c906108c SS |
1467 | return; |
1468 | } | |
1469 | } | |
1470 | ||
1471 | /* Otherwise, go ahead and generate code for it. */ | |
1472 | switch (op) | |
1473 | { | |
1474 | /* Binary arithmetic operators. */ | |
1475 | case BINOP_ADD: | |
1476 | case BINOP_SUB: | |
1477 | case BINOP_MUL: | |
1478 | case BINOP_DIV: | |
1479 | case BINOP_REM: | |
1480 | case BINOP_SUBSCRIPT: | |
1481 | case BINOP_BITWISE_AND: | |
1482 | case BINOP_BITWISE_IOR: | |
1483 | case BINOP_BITWISE_XOR: | |
782b2b07 SS |
1484 | case BINOP_EQUAL: |
1485 | case BINOP_NOTEQUAL: | |
1486 | case BINOP_LESS: | |
1487 | case BINOP_GTR: | |
1488 | case BINOP_LEQ: | |
1489 | case BINOP_GEQ: | |
c906108c | 1490 | (*pc)++; |
f7c79c41 UW |
1491 | gen_expr (exp, pc, ax, &value1); |
1492 | gen_usual_unary (exp, ax, &value1); | |
f61e138d SS |
1493 | gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2); |
1494 | break; | |
1495 | ||
09d559e4 SS |
1496 | case BINOP_LOGICAL_AND: |
1497 | (*pc)++; | |
1498 | /* Generate the obvious sequence of tests and jumps. */ | |
1499 | gen_expr (exp, pc, ax, &value1); | |
1500 | gen_usual_unary (exp, ax, &value1); | |
1501 | if1 = ax_goto (ax, aop_if_goto); | |
1502 | go1 = ax_goto (ax, aop_goto); | |
1503 | ax_label (ax, if1, ax->len); | |
1504 | gen_expr (exp, pc, ax, &value2); | |
1505 | gen_usual_unary (exp, ax, &value2); | |
1506 | if2 = ax_goto (ax, aop_if_goto); | |
1507 | go2 = ax_goto (ax, aop_goto); | |
1508 | ax_label (ax, if2, ax->len); | |
1509 | ax_const_l (ax, 1); | |
1510 | end = ax_goto (ax, aop_goto); | |
1511 | ax_label (ax, go1, ax->len); | |
1512 | ax_label (ax, go2, ax->len); | |
1513 | ax_const_l (ax, 0); | |
1514 | ax_label (ax, end, ax->len); | |
1515 | value->kind = axs_rvalue; | |
1516 | value->type = language_bool_type (exp->language_defn, exp->gdbarch); | |
1517 | break; | |
1518 | ||
1519 | case BINOP_LOGICAL_OR: | |
1520 | (*pc)++; | |
1521 | /* Generate the obvious sequence of tests and jumps. */ | |
1522 | gen_expr (exp, pc, ax, &value1); | |
1523 | gen_usual_unary (exp, ax, &value1); | |
1524 | if1 = ax_goto (ax, aop_if_goto); | |
1525 | gen_expr (exp, pc, ax, &value2); | |
1526 | gen_usual_unary (exp, ax, &value2); | |
1527 | if2 = ax_goto (ax, aop_if_goto); | |
1528 | ax_const_l (ax, 0); | |
1529 | end = ax_goto (ax, aop_goto); | |
1530 | ax_label (ax, if1, ax->len); | |
1531 | ax_label (ax, if2, ax->len); | |
1532 | ax_const_l (ax, 1); | |
1533 | ax_label (ax, end, ax->len); | |
1534 | value->kind = axs_rvalue; | |
1535 | value->type = language_bool_type (exp->language_defn, exp->gdbarch); | |
1536 | break; | |
1537 | ||
1538 | case TERNOP_COND: | |
1539 | (*pc)++; | |
1540 | gen_expr (exp, pc, ax, &value1); | |
1541 | gen_usual_unary (exp, ax, &value1); | |
1542 | /* For (A ? B : C), it's easiest to generate subexpression | |
1543 | bytecodes in order, but if_goto jumps on true, so we invert | |
1544 | the sense of A. Then we can do B by dropping through, and | |
1545 | jump to do C. */ | |
1546 | gen_logical_not (ax, &value1, | |
1547 | language_bool_type (exp->language_defn, exp->gdbarch)); | |
1548 | if1 = ax_goto (ax, aop_if_goto); | |
1549 | gen_expr (exp, pc, ax, &value2); | |
1550 | gen_usual_unary (exp, ax, &value2); | |
1551 | end = ax_goto (ax, aop_goto); | |
1552 | ax_label (ax, if1, ax->len); | |
1553 | gen_expr (exp, pc, ax, &value3); | |
1554 | gen_usual_unary (exp, ax, &value3); | |
1555 | ax_label (ax, end, ax->len); | |
1556 | /* This is arbitary - what if B and C are incompatible types? */ | |
1557 | value->type = value2.type; | |
1558 | value->kind = value2.kind; | |
1559 | break; | |
1560 | ||
f61e138d SS |
1561 | case BINOP_ASSIGN: |
1562 | (*pc)++; | |
1563 | if ((*pc)[0].opcode == OP_INTERNALVAR) | |
c906108c | 1564 | { |
f61e138d SS |
1565 | char *name = internalvar_name ((*pc)[1].internalvar); |
1566 | struct trace_state_variable *tsv; | |
1567 | (*pc) += 3; | |
1568 | gen_expr (exp, pc, ax, value); | |
1569 | tsv = find_trace_state_variable (name); | |
1570 | if (tsv) | |
f7c79c41 | 1571 | { |
f61e138d SS |
1572 | ax_tsv (ax, aop_setv, tsv->number); |
1573 | if (trace_kludge) | |
1574 | ax_tsv (ax, aop_tracev, tsv->number); | |
f7c79c41 | 1575 | } |
f7c79c41 | 1576 | else |
f61e138d SS |
1577 | error (_("$%s is not a trace state variable, may not assign to it"), name); |
1578 | } | |
1579 | else | |
1580 | error (_("May only assign to trace state variables")); | |
1581 | break; | |
782b2b07 | 1582 | |
f61e138d SS |
1583 | case BINOP_ASSIGN_MODIFY: |
1584 | (*pc)++; | |
1585 | op2 = (*pc)[0].opcode; | |
1586 | (*pc)++; | |
1587 | (*pc)++; | |
1588 | if ((*pc)[0].opcode == OP_INTERNALVAR) | |
1589 | { | |
1590 | char *name = internalvar_name ((*pc)[1].internalvar); | |
1591 | struct trace_state_variable *tsv; | |
1592 | (*pc) += 3; | |
1593 | tsv = find_trace_state_variable (name); | |
1594 | if (tsv) | |
1595 | { | |
1596 | /* The tsv will be the left half of the binary operation. */ | |
1597 | ax_tsv (ax, aop_getv, tsv->number); | |
1598 | if (trace_kludge) | |
1599 | ax_tsv (ax, aop_tracev, tsv->number); | |
1600 | /* Trace state variables are always 64-bit integers. */ | |
1601 | value1.kind = axs_rvalue; | |
1602 | value1.type = builtin_type (exp->gdbarch)->builtin_long_long; | |
1603 | /* Now do right half of expression. */ | |
1604 | gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2); | |
1605 | /* We have a result of the binary op, set the tsv. */ | |
1606 | ax_tsv (ax, aop_setv, tsv->number); | |
1607 | if (trace_kludge) | |
1608 | ax_tsv (ax, aop_tracev, tsv->number); | |
1609 | } | |
1610 | else | |
1611 | error (_("$%s is not a trace state variable, may not assign to it"), name); | |
c906108c | 1612 | } |
f61e138d SS |
1613 | else |
1614 | error (_("May only assign to trace state variables")); | |
c906108c SS |
1615 | break; |
1616 | ||
1617 | /* Note that we need to be a little subtle about generating code | |
c5aa993b JM |
1618 | for comma. In C, we can do some optimizations here because |
1619 | we know the left operand is only being evaluated for effect. | |
1620 | However, if the tracing kludge is in effect, then we always | |
1621 | need to evaluate the left hand side fully, so that all the | |
1622 | variables it mentions get traced. */ | |
c906108c SS |
1623 | case BINOP_COMMA: |
1624 | (*pc)++; | |
f7c79c41 | 1625 | gen_expr (exp, pc, ax, &value1); |
c906108c | 1626 | /* Don't just dispose of the left operand. We might be tracing, |
c5aa993b JM |
1627 | in which case we want to emit code to trace it if it's an |
1628 | lvalue. */ | |
c906108c | 1629 | gen_traced_pop (ax, &value1); |
f7c79c41 | 1630 | gen_expr (exp, pc, ax, value); |
c906108c SS |
1631 | /* It's the consumer's responsibility to trace the right operand. */ |
1632 | break; | |
c5aa993b | 1633 | |
c906108c SS |
1634 | case OP_LONG: /* some integer constant */ |
1635 | { | |
1636 | struct type *type = (*pc)[1].type; | |
1637 | LONGEST k = (*pc)[2].longconst; | |
1638 | (*pc) += 4; | |
1639 | gen_int_literal (ax, value, k, type); | |
1640 | } | |
c5aa993b | 1641 | break; |
c906108c SS |
1642 | |
1643 | case OP_VAR_VALUE: | |
f7c79c41 | 1644 | gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol); |
c906108c SS |
1645 | (*pc) += 4; |
1646 | break; | |
1647 | ||
1648 | case OP_REGISTER: | |
1649 | { | |
67f3407f DJ |
1650 | const char *name = &(*pc)[2].string; |
1651 | int reg; | |
1652 | (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1); | |
f7c79c41 | 1653 | reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name)); |
67f3407f DJ |
1654 | if (reg == -1) |
1655 | internal_error (__FILE__, __LINE__, | |
1656 | _("Register $%s not available"), name); | |
f7c79c41 | 1657 | if (reg >= gdbarch_num_regs (exp->gdbarch)) |
02e4669d JB |
1658 | error (_("'%s' is a pseudo-register; " |
1659 | "GDB cannot yet trace pseudoregister contents."), | |
1660 | name); | |
c906108c SS |
1661 | value->kind = axs_lvalue_register; |
1662 | value->u.reg = reg; | |
f7c79c41 | 1663 | value->type = register_type (exp->gdbarch, reg); |
c906108c | 1664 | } |
c5aa993b | 1665 | break; |
c906108c SS |
1666 | |
1667 | case OP_INTERNALVAR: | |
f61e138d SS |
1668 | { |
1669 | const char *name = internalvar_name ((*pc)[1].internalvar); | |
1670 | struct trace_state_variable *tsv; | |
1671 | (*pc) += 3; | |
1672 | tsv = find_trace_state_variable (name); | |
1673 | if (tsv) | |
1674 | { | |
1675 | ax_tsv (ax, aop_getv, tsv->number); | |
1676 | if (trace_kludge) | |
1677 | ax_tsv (ax, aop_tracev, tsv->number); | |
1678 | /* Trace state variables are always 64-bit integers. */ | |
1679 | value->kind = axs_rvalue; | |
1680 | value->type = builtin_type (exp->gdbarch)->builtin_long_long; | |
1681 | } | |
1682 | else | |
1683 | error (_("$%s is not a trace state variable; GDB agent expressions cannot use convenience variables."), name); | |
1684 | } | |
1685 | break; | |
c906108c | 1686 | |
c5aa993b | 1687 | /* Weirdo operator: see comments for gen_repeat for details. */ |
c906108c SS |
1688 | case BINOP_REPEAT: |
1689 | /* Note that gen_repeat handles its own argument evaluation. */ | |
1690 | (*pc)++; | |
f7c79c41 | 1691 | gen_repeat (exp, pc, ax, value); |
c906108c SS |
1692 | break; |
1693 | ||
1694 | case UNOP_CAST: | |
1695 | { | |
1696 | struct type *type = (*pc)[1].type; | |
1697 | (*pc) += 3; | |
f7c79c41 | 1698 | gen_expr (exp, pc, ax, value); |
c906108c SS |
1699 | gen_cast (ax, value, type); |
1700 | } | |
c5aa993b | 1701 | break; |
c906108c SS |
1702 | |
1703 | case UNOP_MEMVAL: | |
1704 | { | |
1705 | struct type *type = check_typedef ((*pc)[1].type); | |
1706 | (*pc) += 3; | |
f7c79c41 | 1707 | gen_expr (exp, pc, ax, value); |
c906108c SS |
1708 | /* I'm not sure I understand UNOP_MEMVAL entirely. I think |
1709 | it's just a hack for dealing with minsyms; you take some | |
1710 | integer constant, pretend it's the address of an lvalue of | |
1711 | the given type, and dereference it. */ | |
1712 | if (value->kind != axs_rvalue) | |
1713 | /* This would be weird. */ | |
8e65ff28 | 1714 | internal_error (__FILE__, __LINE__, |
3d263c1d | 1715 | _("gen_expr: OP_MEMVAL operand isn't an rvalue???")); |
c906108c SS |
1716 | value->type = type; |
1717 | value->kind = axs_lvalue_memory; | |
1718 | } | |
c5aa993b | 1719 | break; |
c906108c | 1720 | |
36e9969c NS |
1721 | case UNOP_PLUS: |
1722 | (*pc)++; | |
1723 | /* + FOO is equivalent to 0 + FOO, which can be optimized. */ | |
f7c79c41 UW |
1724 | gen_expr (exp, pc, ax, value); |
1725 | gen_usual_unary (exp, ax, value); | |
36e9969c NS |
1726 | break; |
1727 | ||
c906108c SS |
1728 | case UNOP_NEG: |
1729 | (*pc)++; | |
1730 | /* -FOO is equivalent to 0 - FOO. */ | |
22601c15 UW |
1731 | gen_int_literal (ax, &value1, 0, |
1732 | builtin_type (exp->gdbarch)->builtin_int); | |
f7c79c41 UW |
1733 | gen_usual_unary (exp, ax, &value1); /* shouldn't do much */ |
1734 | gen_expr (exp, pc, ax, &value2); | |
1735 | gen_usual_unary (exp, ax, &value2); | |
1736 | gen_usual_arithmetic (exp, ax, &value1, &value2); | |
1737 | gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation"); | |
c906108c SS |
1738 | break; |
1739 | ||
1740 | case UNOP_LOGICAL_NOT: | |
1741 | (*pc)++; | |
f7c79c41 UW |
1742 | gen_expr (exp, pc, ax, value); |
1743 | gen_usual_unary (exp, ax, value); | |
1744 | gen_logical_not (ax, value, | |
1745 | language_bool_type (exp->language_defn, exp->gdbarch)); | |
c906108c SS |
1746 | break; |
1747 | ||
1748 | case UNOP_COMPLEMENT: | |
1749 | (*pc)++; | |
f7c79c41 UW |
1750 | gen_expr (exp, pc, ax, value); |
1751 | gen_usual_unary (exp, ax, value); | |
1752 | gen_integral_promotions (exp, ax, value); | |
c906108c SS |
1753 | gen_complement (ax, value); |
1754 | break; | |
1755 | ||
1756 | case UNOP_IND: | |
1757 | (*pc)++; | |
f7c79c41 UW |
1758 | gen_expr (exp, pc, ax, value); |
1759 | gen_usual_unary (exp, ax, value); | |
b97aedf3 | 1760 | if (!pointer_type (value->type)) |
3d263c1d | 1761 | error (_("Argument of unary `*' is not a pointer.")); |
c906108c SS |
1762 | gen_deref (ax, value); |
1763 | break; | |
1764 | ||
1765 | case UNOP_ADDR: | |
1766 | (*pc)++; | |
f7c79c41 | 1767 | gen_expr (exp, pc, ax, value); |
c906108c SS |
1768 | gen_address_of (ax, value); |
1769 | break; | |
1770 | ||
1771 | case UNOP_SIZEOF: | |
1772 | (*pc)++; | |
1773 | /* Notice that gen_sizeof handles its own operand, unlike most | |
c5aa993b JM |
1774 | of the other unary operator functions. This is because we |
1775 | have to throw away the code we generate. */ | |
f7c79c41 UW |
1776 | gen_sizeof (exp, pc, ax, value, |
1777 | builtin_type (exp->gdbarch)->builtin_int); | |
c906108c SS |
1778 | break; |
1779 | ||
1780 | case STRUCTOP_STRUCT: | |
1781 | case STRUCTOP_PTR: | |
1782 | { | |
1783 | int length = (*pc)[1].longconst; | |
1784 | char *name = &(*pc)[2].string; | |
1785 | ||
1786 | (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1); | |
f7c79c41 | 1787 | gen_expr (exp, pc, ax, value); |
c906108c | 1788 | if (op == STRUCTOP_STRUCT) |
505e835d | 1789 | gen_struct_ref (exp, ax, value, name, ".", "structure or union"); |
c906108c | 1790 | else if (op == STRUCTOP_PTR) |
505e835d | 1791 | gen_struct_ref (exp, ax, value, name, "->", |
c906108c SS |
1792 | "pointer to a structure or union"); |
1793 | else | |
1794 | /* If this `if' chain doesn't handle it, then the case list | |
c5aa993b | 1795 | shouldn't mention it, and we shouldn't be here. */ |
8e65ff28 | 1796 | internal_error (__FILE__, __LINE__, |
3d263c1d | 1797 | _("gen_expr: unhandled struct case")); |
c906108c | 1798 | } |
c5aa993b | 1799 | break; |
c906108c | 1800 | |
6c228b9c SS |
1801 | case OP_THIS: |
1802 | { | |
6a0fc12f | 1803 | char *this_name; |
6c228b9c SS |
1804 | struct symbol *func, *sym; |
1805 | struct block *b; | |
1806 | ||
6a0fc12f PA |
1807 | func = block_linkage_function (block_for_pc (ax->scope)); |
1808 | this_name = language_def (SYMBOL_LANGUAGE (func))->la_name_of_this; | |
6c228b9c | 1809 | b = SYMBOL_BLOCK_VALUE (func); |
6c228b9c SS |
1810 | |
1811 | /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER | |
1812 | symbol instead of the LOC_ARG one (if both exist). */ | |
6a0fc12f | 1813 | sym = lookup_block_symbol (b, this_name, NULL, VAR_DOMAIN); |
6c228b9c | 1814 | if (!sym) |
6a0fc12f | 1815 | error (_("no `%s' found"), this_name); |
6c228b9c SS |
1816 | |
1817 | gen_var_ref (exp->gdbarch, ax, value, sym); | |
1818 | (*pc) += 2; | |
1819 | } | |
1820 | break; | |
1821 | ||
c906108c | 1822 | case OP_TYPE: |
3d263c1d | 1823 | error (_("Attempt to use a type name as an expression.")); |
c906108c SS |
1824 | |
1825 | default: | |
3d263c1d | 1826 | error (_("Unsupported operator in expression.")); |
c906108c SS |
1827 | } |
1828 | } | |
f61e138d SS |
1829 | |
1830 | /* This handles the middle-to-right-side of code generation for binary | |
1831 | expressions, which is shared between regular binary operations and | |
1832 | assign-modify (+= and friends) expressions. */ | |
1833 | ||
1834 | static void | |
1835 | gen_expr_binop_rest (struct expression *exp, | |
1836 | enum exp_opcode op, union exp_element **pc, | |
1837 | struct agent_expr *ax, struct axs_value *value, | |
1838 | struct axs_value *value1, struct axs_value *value2) | |
1839 | { | |
1840 | gen_expr (exp, pc, ax, value2); | |
1841 | gen_usual_unary (exp, ax, value2); | |
1842 | gen_usual_arithmetic (exp, ax, value1, value2); | |
1843 | switch (op) | |
1844 | { | |
1845 | case BINOP_ADD: | |
1846 | if (TYPE_CODE (value1->type) == TYPE_CODE_INT | |
b97aedf3 | 1847 | && pointer_type (value2->type)) |
f61e138d SS |
1848 | { |
1849 | /* Swap the values and proceed normally. */ | |
1850 | ax_simple (ax, aop_swap); | |
1851 | gen_ptradd (ax, value, value2, value1); | |
1852 | } | |
b97aedf3 | 1853 | else if (pointer_type (value1->type) |
f61e138d SS |
1854 | && TYPE_CODE (value2->type) == TYPE_CODE_INT) |
1855 | gen_ptradd (ax, value, value1, value2); | |
1856 | else | |
1857 | gen_binop (ax, value, value1, value2, | |
1858 | aop_add, aop_add, 1, "addition"); | |
1859 | break; | |
1860 | case BINOP_SUB: | |
b97aedf3 | 1861 | if (pointer_type (value1->type) |
f61e138d SS |
1862 | && TYPE_CODE (value2->type) == TYPE_CODE_INT) |
1863 | gen_ptrsub (ax,value, value1, value2); | |
b97aedf3 SS |
1864 | else if (pointer_type (value1->type) |
1865 | && pointer_type (value2->type)) | |
f61e138d SS |
1866 | /* FIXME --- result type should be ptrdiff_t */ |
1867 | gen_ptrdiff (ax, value, value1, value2, | |
1868 | builtin_type (exp->gdbarch)->builtin_long); | |
1869 | else | |
1870 | gen_binop (ax, value, value1, value2, | |
1871 | aop_sub, aop_sub, 1, "subtraction"); | |
1872 | break; | |
1873 | case BINOP_MUL: | |
1874 | gen_binop (ax, value, value1, value2, | |
1875 | aop_mul, aop_mul, 1, "multiplication"); | |
1876 | break; | |
1877 | case BINOP_DIV: | |
1878 | gen_binop (ax, value, value1, value2, | |
1879 | aop_div_signed, aop_div_unsigned, 1, "division"); | |
1880 | break; | |
1881 | case BINOP_REM: | |
1882 | gen_binop (ax, value, value1, value2, | |
1883 | aop_rem_signed, aop_rem_unsigned, 1, "remainder"); | |
1884 | break; | |
1885 | case BINOP_SUBSCRIPT: | |
1886 | gen_ptradd (ax, value, value1, value2); | |
b97aedf3 | 1887 | if (!pointer_type (value->type)) |
f61e138d SS |
1888 | error (_("Invalid combination of types in array subscripting.")); |
1889 | gen_deref (ax, value); | |
1890 | break; | |
1891 | case BINOP_BITWISE_AND: | |
1892 | gen_binop (ax, value, value1, value2, | |
1893 | aop_bit_and, aop_bit_and, 0, "bitwise and"); | |
1894 | break; | |
1895 | ||
1896 | case BINOP_BITWISE_IOR: | |
1897 | gen_binop (ax, value, value1, value2, | |
1898 | aop_bit_or, aop_bit_or, 0, "bitwise or"); | |
1899 | break; | |
1900 | ||
1901 | case BINOP_BITWISE_XOR: | |
1902 | gen_binop (ax, value, value1, value2, | |
1903 | aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or"); | |
1904 | break; | |
1905 | ||
1906 | case BINOP_EQUAL: | |
1907 | gen_binop (ax, value, value1, value2, | |
1908 | aop_equal, aop_equal, 0, "equal"); | |
1909 | break; | |
1910 | ||
1911 | case BINOP_NOTEQUAL: | |
1912 | gen_binop (ax, value, value1, value2, | |
1913 | aop_equal, aop_equal, 0, "equal"); | |
1914 | gen_logical_not (ax, value, | |
1915 | language_bool_type (exp->language_defn, | |
1916 | exp->gdbarch)); | |
1917 | break; | |
1918 | ||
1919 | case BINOP_LESS: | |
1920 | gen_binop (ax, value, value1, value2, | |
1921 | aop_less_signed, aop_less_unsigned, 0, "less than"); | |
1922 | break; | |
1923 | ||
1924 | case BINOP_GTR: | |
1925 | ax_simple (ax, aop_swap); | |
1926 | gen_binop (ax, value, value1, value2, | |
1927 | aop_less_signed, aop_less_unsigned, 0, "less than"); | |
1928 | break; | |
1929 | ||
1930 | case BINOP_LEQ: | |
1931 | ax_simple (ax, aop_swap); | |
1932 | gen_binop (ax, value, value1, value2, | |
1933 | aop_less_signed, aop_less_unsigned, 0, "less than"); | |
1934 | gen_logical_not (ax, value, | |
1935 | language_bool_type (exp->language_defn, | |
1936 | exp->gdbarch)); | |
1937 | break; | |
1938 | ||
1939 | case BINOP_GEQ: | |
1940 | gen_binop (ax, value, value1, value2, | |
1941 | aop_less_signed, aop_less_unsigned, 0, "less than"); | |
1942 | gen_logical_not (ax, value, | |
1943 | language_bool_type (exp->language_defn, | |
1944 | exp->gdbarch)); | |
1945 | break; | |
1946 | ||
1947 | default: | |
1948 | /* We should only list operators in the outer case statement | |
1949 | that we actually handle in the inner case statement. */ | |
1950 | internal_error (__FILE__, __LINE__, | |
1951 | _("gen_expr: op case sets don't match")); | |
1952 | } | |
1953 | } | |
c906108c | 1954 | \f |
c5aa993b | 1955 | |
0936ad1d SS |
1956 | /* Given a single variable and a scope, generate bytecodes to trace |
1957 | its value. This is for use in situations where we have only a | |
1958 | variable's name, and no parsed expression; for instance, when the | |
1959 | name comes from a list of local variables of a function. */ | |
1960 | ||
1961 | struct agent_expr * | |
1962 | gen_trace_for_var (CORE_ADDR scope, struct symbol *var) | |
1963 | { | |
1964 | struct cleanup *old_chain = 0; | |
1965 | struct agent_expr *ax = new_agent_expr (scope); | |
1966 | struct axs_value value; | |
1967 | ||
1968 | old_chain = make_cleanup_free_agent_expr (ax); | |
1969 | ||
1970 | trace_kludge = 1; | |
1971 | gen_var_ref (NULL, ax, &value, var); | |
1972 | ||
1973 | /* Make sure we record the final object, and get rid of it. */ | |
1974 | gen_traced_pop (ax, &value); | |
1975 | ||
1976 | /* Oh, and terminate. */ | |
1977 | ax_simple (ax, aop_end); | |
1978 | ||
1979 | /* We have successfully built the agent expr, so cancel the cleanup | |
1980 | request. If we add more cleanups that we always want done, this | |
1981 | will have to get more complicated. */ | |
1982 | discard_cleanups (old_chain); | |
1983 | return ax; | |
1984 | } | |
c5aa993b | 1985 | |
c906108c SS |
1986 | /* Generating bytecode from GDB expressions: driver */ |
1987 | ||
c906108c SS |
1988 | /* Given a GDB expression EXPR, return bytecode to trace its value. |
1989 | The result will use the `trace' and `trace_quick' bytecodes to | |
1990 | record the value of all memory touched by the expression. The | |
1991 | caller can then use the ax_reqs function to discover which | |
1992 | registers it relies upon. */ | |
1993 | struct agent_expr * | |
fba45db2 | 1994 | gen_trace_for_expr (CORE_ADDR scope, struct expression *expr) |
c906108c SS |
1995 | { |
1996 | struct cleanup *old_chain = 0; | |
1997 | struct agent_expr *ax = new_agent_expr (scope); | |
1998 | union exp_element *pc; | |
1999 | struct axs_value value; | |
2000 | ||
f23d52e0 | 2001 | old_chain = make_cleanup_free_agent_expr (ax); |
c906108c SS |
2002 | |
2003 | pc = expr->elts; | |
2004 | trace_kludge = 1; | |
f7c79c41 | 2005 | gen_expr (expr, &pc, ax, &value); |
c906108c SS |
2006 | |
2007 | /* Make sure we record the final object, and get rid of it. */ | |
2008 | gen_traced_pop (ax, &value); | |
2009 | ||
2010 | /* Oh, and terminate. */ | |
2011 | ax_simple (ax, aop_end); | |
2012 | ||
2013 | /* We have successfully built the agent expr, so cancel the cleanup | |
2014 | request. If we add more cleanups that we always want done, this | |
2015 | will have to get more complicated. */ | |
2016 | discard_cleanups (old_chain); | |
2017 | return ax; | |
2018 | } | |
c906108c | 2019 | |
782b2b07 SS |
2020 | /* Given a GDB expression EXPR, return a bytecode sequence that will |
2021 | evaluate and return a result. The bytecodes will do a direct | |
2022 | evaluation, using the current data on the target, rather than | |
2023 | recording blocks of memory and registers for later use, as | |
2024 | gen_trace_for_expr does. The generated bytecode sequence leaves | |
2025 | the result of expression evaluation on the top of the stack. */ | |
2026 | ||
2027 | struct agent_expr * | |
2028 | gen_eval_for_expr (CORE_ADDR scope, struct expression *expr) | |
2029 | { | |
2030 | struct cleanup *old_chain = 0; | |
2031 | struct agent_expr *ax = new_agent_expr (scope); | |
2032 | union exp_element *pc; | |
2033 | struct axs_value value; | |
2034 | ||
2035 | old_chain = make_cleanup_free_agent_expr (ax); | |
2036 | ||
2037 | pc = expr->elts; | |
2038 | trace_kludge = 0; | |
2039 | gen_expr (expr, &pc, ax, &value); | |
2040 | ||
2041 | /* Oh, and terminate. */ | |
2042 | ax_simple (ax, aop_end); | |
2043 | ||
2044 | /* We have successfully built the agent expr, so cancel the cleanup | |
2045 | request. If we add more cleanups that we always want done, this | |
2046 | will have to get more complicated. */ | |
2047 | discard_cleanups (old_chain); | |
2048 | return ax; | |
2049 | } | |
2050 | ||
c906108c | 2051 | static void |
fba45db2 | 2052 | agent_command (char *exp, int from_tty) |
c906108c SS |
2053 | { |
2054 | struct cleanup *old_chain = 0; | |
2055 | struct expression *expr; | |
2056 | struct agent_expr *agent; | |
6426a772 | 2057 | struct frame_info *fi = get_current_frame (); /* need current scope */ |
c906108c SS |
2058 | |
2059 | /* We don't deal with overlay debugging at the moment. We need to | |
2060 | think more carefully about this. If you copy this code into | |
2061 | another command, change the error message; the user shouldn't | |
2062 | have to know anything about agent expressions. */ | |
2063 | if (overlay_debugging) | |
3d263c1d | 2064 | error (_("GDB can't do agent expression translation with overlays.")); |
c906108c SS |
2065 | |
2066 | if (exp == 0) | |
3d263c1d | 2067 | error_no_arg (_("expression to translate")); |
c5aa993b | 2068 | |
c906108c | 2069 | expr = parse_expression (exp); |
c13c43fd | 2070 | old_chain = make_cleanup (free_current_contents, &expr); |
bdd78e62 | 2071 | agent = gen_trace_for_expr (get_frame_pc (fi), expr); |
f23d52e0 | 2072 | make_cleanup_free_agent_expr (agent); |
c906108c | 2073 | ax_print (gdb_stdout, agent); |
085dd6e6 JM |
2074 | |
2075 | /* It would be nice to call ax_reqs here to gather some general info | |
2076 | about the expression, and then print out the result. */ | |
c906108c SS |
2077 | |
2078 | do_cleanups (old_chain); | |
2079 | dont_repeat (); | |
2080 | } | |
782b2b07 SS |
2081 | |
2082 | /* Parse the given expression, compile it into an agent expression | |
2083 | that does direct evaluation, and display the resulting | |
2084 | expression. */ | |
2085 | ||
2086 | static void | |
2087 | agent_eval_command (char *exp, int from_tty) | |
2088 | { | |
2089 | struct cleanup *old_chain = 0; | |
2090 | struct expression *expr; | |
2091 | struct agent_expr *agent; | |
2092 | struct frame_info *fi = get_current_frame (); /* need current scope */ | |
2093 | ||
2094 | /* We don't deal with overlay debugging at the moment. We need to | |
2095 | think more carefully about this. If you copy this code into | |
2096 | another command, change the error message; the user shouldn't | |
2097 | have to know anything about agent expressions. */ | |
2098 | if (overlay_debugging) | |
2099 | error (_("GDB can't do agent expression translation with overlays.")); | |
2100 | ||
2101 | if (exp == 0) | |
2102 | error_no_arg (_("expression to translate")); | |
2103 | ||
2104 | expr = parse_expression (exp); | |
2105 | old_chain = make_cleanup (free_current_contents, &expr); | |
2106 | agent = gen_eval_for_expr (get_frame_pc (fi), expr); | |
2107 | make_cleanup_free_agent_expr (agent); | |
2108 | ax_print (gdb_stdout, agent); | |
2109 | ||
2110 | /* It would be nice to call ax_reqs here to gather some general info | |
2111 | about the expression, and then print out the result. */ | |
2112 | ||
2113 | do_cleanups (old_chain); | |
2114 | dont_repeat (); | |
2115 | } | |
c906108c | 2116 | \f |
c5aa993b | 2117 | |
c906108c SS |
2118 | /* Initialization code. */ |
2119 | ||
a14ed312 | 2120 | void _initialize_ax_gdb (void); |
c906108c | 2121 | void |
fba45db2 | 2122 | _initialize_ax_gdb (void) |
c906108c | 2123 | { |
c906108c | 2124 | add_cmd ("agent", class_maintenance, agent_command, |
782b2b07 SS |
2125 | _("Translate an expression into remote agent bytecode for tracing."), |
2126 | &maintenancelist); | |
2127 | ||
2128 | add_cmd ("agent-eval", class_maintenance, agent_eval_command, | |
2129 | _("Translate an expression into remote agent bytecode for evaluation."), | |
c906108c SS |
2130 | &maintenancelist); |
2131 | } |