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