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