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