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