OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [gdb/] [ax-gdb.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.