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