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