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