OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [gdb-7.2/] [gdb-7.2-or32-1.0rc1/] [gdb/] [python/] [py-value.c] - Blame information for rev 341

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 330 jeremybenn
/* Python interface to values.
2
 
3
   Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
4
 
5
   This file is part of GDB.
6
 
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
 
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
 
17
   You should have received a copy of the GNU General Public License
18
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
 
20
#include "defs.h"
21
#include "gdb_assert.h"
22
#include "charset.h"
23
#include "value.h"
24
#include "exceptions.h"
25
#include "language.h"
26
#include "dfp.h"
27
#include "valprint.h"
28
 
29
#ifdef HAVE_PYTHON
30
 
31
#include "python-internal.h"
32
 
33
/* Even though Python scalar types directly map to host types, we use
34
   target types here to remain consistent with the the values system in
35
   GDB (which uses target arithmetic).  */
36
 
37
/* Python's integer type corresponds to C's long type.  */
38
#define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
39
 
40
/* Python's float type corresponds to C's double type.  */
41
#define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
42
 
43
/* Python's long type corresponds to C's long long type.  */
44
#define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
45
 
46
/* Python's long type corresponds to C's long long type.  Unsigned version.  */
47
#define builtin_type_upylong builtin_type \
48
  (python_gdbarch)->builtin_unsigned_long_long
49
 
50
#define builtin_type_pybool \
51
  language_bool_type (python_language, python_gdbarch)
52
 
53
#define builtin_type_pychar \
54
  language_string_char_type (python_language, python_gdbarch)
55
 
56
typedef struct value_object {
57
  PyObject_HEAD
58
  struct value_object *next;
59
  struct value_object *prev;
60
  struct value *value;
61
  PyObject *address;
62
  PyObject *type;
63
} value_object;
64
 
65
/* List of all values which are currently exposed to Python. It is
66
   maintained so that when an objfile is discarded, preserve_values
67
   can copy the values' types if needed.  */
68
/* This variable is unnecessarily initialized to NULL in order to
69
   work around a linker bug on MacOS.  */
70
static value_object *values_in_python = NULL;
71
 
72
/* Called by the Python interpreter when deallocating a value object.  */
73
static void
74
valpy_dealloc (PyObject *obj)
75
{
76
  value_object *self = (value_object *) obj;
77
 
78
  /* Remove SELF from the global list.  */
79
  if (self->prev)
80
    self->prev->next = self->next;
81
  else
82
    {
83
      gdb_assert (values_in_python == self);
84
      values_in_python = self->next;
85
    }
86
  if (self->next)
87
    self->next->prev = self->prev;
88
 
89
  value_free (self->value);
90
 
91
  if (self->address)
92
    /* Use braces to appease gcc warning.  *sigh*  */
93
    {
94
      Py_DECREF (self->address);
95
    }
96
 
97
  if (self->type)
98
    {
99
      Py_DECREF (self->type);
100
    }
101
 
102
  self->ob_type->tp_free (self);
103
}
104
 
105
/* Helper to push a Value object on the global list.  */
106
static void
107
note_value (value_object *value_obj)
108
{
109
  value_obj->next = values_in_python;
110
  if (value_obj->next)
111
    value_obj->next->prev = value_obj;
112
  value_obj->prev = NULL;
113
  values_in_python = value_obj;
114
}
115
 
116
/* Called when a new gdb.Value object needs to be allocated.  */
117
static PyObject *
118
valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
119
{
120
  struct value *value = NULL;   /* Initialize to appease gcc warning.  */
121
  value_object *value_obj;
122
 
123
  if (PyTuple_Size (args) != 1)
124
    {
125
      PyErr_SetString (PyExc_TypeError, _("Value object creation takes only "
126
                                          "1 argument"));
127
      return NULL;
128
    }
129
 
130
  value_obj = (value_object *) subtype->tp_alloc (subtype, 1);
131
  if (value_obj == NULL)
132
    {
133
      PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to "
134
                                            "create Value object."));
135
      return NULL;
136
    }
137
 
138
  value = convert_value_from_python (PyTuple_GetItem (args, 0));
139
  if (value == NULL)
140
    {
141
      subtype->tp_free (value_obj);
142
      return NULL;
143
    }
144
 
145
  value_obj->value = value;
146
  value_incref (value);
147
  value_obj->address = NULL;
148
  value_obj->type = NULL;
149
  note_value (value_obj);
150
 
151
  return (PyObject *) value_obj;
152
}
153
 
154
/* Iterate over all the Value objects, calling preserve_one_value on
155
   each.  */
156
void
157
preserve_python_values (struct objfile *objfile, htab_t copied_types)
158
{
159
  value_object *iter;
160
 
161
  for (iter = values_in_python; iter; iter = iter->next)
162
    preserve_one_value (iter->value, objfile, copied_types);
163
}
164
 
165
/* Given a value of a pointer type, apply the C unary * operator to it.  */
166
static PyObject *
167
valpy_dereference (PyObject *self, PyObject *args)
168
{
169
  struct value *res_val = NULL;   /* Initialize to appease gcc warning.  */
170
  volatile struct gdb_exception except;
171
 
172
  TRY_CATCH (except, RETURN_MASK_ALL)
173
    {
174
      res_val = value_ind (((value_object *) self)->value);
175
    }
176
  GDB_PY_HANDLE_EXCEPTION (except);
177
 
178
  return value_to_value_object (res_val);
179
}
180
 
181
/* Return "&value".  */
182
static PyObject *
183
valpy_get_address (PyObject *self, void *closure)
184
{
185
  struct value *res_val = NULL;   /* Initialize to appease gcc warning.  */
186
  value_object *val_obj = (value_object *) self;
187
  volatile struct gdb_exception except;
188
 
189
  if (!val_obj->address)
190
    {
191
      TRY_CATCH (except, RETURN_MASK_ALL)
192
        {
193
          res_val = value_addr (val_obj->value);
194
        }
195
      if (except.reason < 0)
196
        {
197
          val_obj->address = Py_None;
198
          Py_INCREF (Py_None);
199
        }
200
      else
201
        val_obj->address = value_to_value_object (res_val);
202
    }
203
 
204
  Py_INCREF (val_obj->address);
205
 
206
  return val_obj->address;
207
}
208
 
209
/* Return type of the value.  */
210
static PyObject *
211
valpy_get_type (PyObject *self, void *closure)
212
{
213
  value_object *obj = (value_object *) self;
214
 
215
  if (!obj->type)
216
    {
217
      obj->type = type_to_type_object (value_type (obj->value));
218
      if (!obj->type)
219
        {
220
          obj->type = Py_None;
221
          Py_INCREF (obj->type);
222
        }
223
    }
224
  Py_INCREF (obj->type);
225
  return obj->type;
226
}
227
 
228
/* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
229
   string.  Return a PyObject representing a lazy_string_object type.
230
   A lazy string is a pointer to a string with an optional encoding and
231
   length.  If ENCODING is not given, encoding is set to None.  If an
232
   ENCODING is provided the encoding parameter is set to ENCODING, but
233
   the string is not encoded.  If LENGTH is provided then the length
234
   parameter is set to LENGTH, otherwise length will be set to -1 (first
235
   null of appropriate with).  */
236
static PyObject *
237
valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
238
{
239
  int length = -1;
240
  struct value *value = ((value_object *) self)->value;
241
  const char *user_encoding = NULL;
242
  static char *keywords[] = { "encoding", "length", NULL };
243
  PyObject *str_obj;
244
 
245
  if (!PyArg_ParseTupleAndKeywords (args, kw, "|si", keywords,
246
                                    &user_encoding, &length))
247
    return NULL;
248
 
249
  if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
250
    value = value_ind (value);
251
 
252
  str_obj = gdbpy_create_lazy_string_object (value_address (value), length,
253
                                             user_encoding, value_type (value));
254
 
255
  return (PyObject *) str_obj;
256
}
257
 
258
/* Implementation of gdb.Value.string ([encoding] [, errors]
259
   [, length]) -> string.  Return Unicode string with value contents.
260
   If ENCODING is not given, the string is assumed to be encoded in
261
   the target's charset.  If LENGTH is provided, only fetch string to
262
   the length provided.  */
263
 
264
static PyObject *
265
valpy_string (PyObject *self, PyObject *args, PyObject *kw)
266
{
267
  int length = -1;
268
  gdb_byte *buffer;
269
  struct value *value = ((value_object *) self)->value;
270
  volatile struct gdb_exception except;
271
  PyObject *unicode;
272
  const char *encoding = NULL;
273
  const char *errors = NULL;
274
  const char *user_encoding = NULL;
275
  const char *la_encoding = NULL;
276
  struct type *char_type;
277
  static char *keywords[] = { "encoding", "errors", "length", NULL };
278
 
279
  if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
280
                                    &user_encoding, &errors, &length))
281
    return NULL;
282
 
283
  TRY_CATCH (except, RETURN_MASK_ALL)
284
    {
285
      LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
286
    }
287
  GDB_PY_HANDLE_EXCEPTION (except);
288
 
289
  encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
290
  unicode = PyUnicode_Decode (buffer, length * TYPE_LENGTH (char_type),
291
                              encoding, errors);
292
  xfree (buffer);
293
 
294
  return unicode;
295
}
296
 
297
/* Cast a value to a given type.  */
298
static PyObject *
299
valpy_cast (PyObject *self, PyObject *args)
300
{
301
  PyObject *type_obj;
302
  struct type *type;
303
  struct value *res_val = NULL;   /* Initialize to appease gcc warning.  */
304
  volatile struct gdb_exception except;
305
 
306
  if (! PyArg_ParseTuple (args, "O", &type_obj))
307
    return NULL;
308
 
309
  type = type_object_to_type (type_obj);
310
  if (! type)
311
    {
312
      PyErr_SetString (PyExc_RuntimeError,
313
                       _("Argument must be a type."));
314
      return NULL;
315
    }
316
 
317
  TRY_CATCH (except, RETURN_MASK_ALL)
318
    {
319
      res_val = value_cast (type, ((value_object *) self)->value);
320
    }
321
  GDB_PY_HANDLE_EXCEPTION (except);
322
 
323
  return value_to_value_object (res_val);
324
}
325
 
326
static Py_ssize_t
327
valpy_length (PyObject *self)
328
{
329
  /* We don't support getting the number of elements in a struct / class.  */
330
  PyErr_SetString (PyExc_NotImplementedError,
331
                   _("Invalid operation on gdb.Value."));
332
  return -1;
333
}
334
 
335
/* Given string name of an element inside structure, return its value
336
   object.  */
337
static PyObject *
338
valpy_getitem (PyObject *self, PyObject *key)
339
{
340
  value_object *self_value = (value_object *) self;
341
  char *field = NULL;
342
  struct value *res_val = NULL;
343
  volatile struct gdb_exception except;
344
 
345
  if (gdbpy_is_string (key))
346
    {
347
      field = python_string_to_host_string (key);
348
      if (field == NULL)
349
        return NULL;
350
    }
351
 
352
  TRY_CATCH (except, RETURN_MASK_ALL)
353
    {
354
      struct value *tmp = self_value->value;
355
 
356
      if (field)
357
        res_val = value_struct_elt (&tmp, NULL, field, 0, NULL);
358
      else
359
        {
360
          /* Assume we are attempting an array access, and let the
361
             value code throw an exception if the index has an invalid
362
             type.  */
363
          struct value *idx = convert_value_from_python (key);
364
 
365
          if (idx != NULL)
366
            {
367
              /* Check the value's type is something that can be accessed via
368
                 a subscript.  */
369
              struct type *type;
370
 
371
              tmp = coerce_ref (tmp);
372
              type = check_typedef (value_type (tmp));
373
              if (TYPE_CODE (type) != TYPE_CODE_ARRAY
374
                  && TYPE_CODE (type) != TYPE_CODE_PTR)
375
                  error( _("Cannot subscript requested type."));
376
              else
377
                res_val = value_subscript (tmp, value_as_long (idx));
378
            }
379
        }
380
    }
381
 
382
  xfree (field);
383
  GDB_PY_HANDLE_EXCEPTION (except);
384
 
385
  return res_val ? value_to_value_object (res_val) : NULL;
386
}
387
 
388
static int
389
valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
390
{
391
  PyErr_Format (PyExc_NotImplementedError,
392
                _("Setting of struct elements is not currently supported."));
393
  return -1;
394
}
395
 
396
/* Called by the Python interpreter to obtain string representation
397
   of the object.  */
398
static PyObject *
399
valpy_str (PyObject *self)
400
{
401
  char *s = NULL;
402
  struct ui_file *stb;
403
  struct cleanup *old_chain;
404
  PyObject *result;
405
  struct value_print_options opts;
406
  volatile struct gdb_exception except;
407
 
408
  get_user_print_options (&opts);
409
  opts.deref_ref = 0;
410
 
411
  stb = mem_fileopen ();
412
  old_chain = make_cleanup_ui_file_delete (stb);
413
 
414
  TRY_CATCH (except, RETURN_MASK_ALL)
415
    {
416
      common_val_print (((value_object *) self)->value, stb, 0,
417
                        &opts, python_language);
418
      s = ui_file_xstrdup (stb, NULL);
419
    }
420
  GDB_PY_HANDLE_EXCEPTION (except);
421
 
422
  do_cleanups (old_chain);
423
 
424
  result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL);
425
  xfree (s);
426
 
427
  return result;
428
}
429
 
430
/* Implements gdb.Value.is_optimized_out.  */
431
static PyObject *
432
valpy_get_is_optimized_out (PyObject *self, void *closure)
433
{
434
  struct value *value = ((value_object *) self)->value;
435
 
436
  if (value_optimized_out (value))
437
    Py_RETURN_TRUE;
438
 
439
  Py_RETURN_FALSE;
440
}
441
 
442
/* Calculate and return the address of the PyObject as the value of
443
   the builtin __hash__ call.  */
444
static long
445
valpy_hash (PyObject *self)
446
{
447
  return (long) (intptr_t) self;
448
}
449
 
450
enum valpy_opcode
451
{
452
  VALPY_ADD,
453
  VALPY_SUB,
454
  VALPY_MUL,
455
  VALPY_DIV,
456
  VALPY_REM,
457
  VALPY_POW,
458
  VALPY_LSH,
459
  VALPY_RSH,
460
  VALPY_BITAND,
461
  VALPY_BITOR,
462
  VALPY_BITXOR
463
};
464
 
465
/* If TYPE is a reference, return the target; otherwise return TYPE.  */
466
#define STRIP_REFERENCE(TYPE) \
467
  ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
468
 
469
/* Returns a value object which is the result of applying the operation
470
   specified by OPCODE to the given arguments.  */
471
static PyObject *
472
valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
473
{
474
  struct value *res_val = NULL;   /* Initialize to appease gcc warning.  */
475
  volatile struct gdb_exception except;
476
 
477
  TRY_CATCH (except, RETURN_MASK_ALL)
478
    {
479
      struct value *arg1, *arg2;
480
 
481
      /* If the gdb.Value object is the second operand, then it will be passed
482
         to us as the OTHER argument, and SELF will be an entirely different
483
         kind of object, altogether.  Because of this, we can't assume self is
484
         a gdb.Value object and need to convert it from python as well.  */
485
      arg1 = convert_value_from_python (self);
486
      if (arg1 == NULL)
487
        break;
488
 
489
      arg2 = convert_value_from_python (other);
490
      if (arg2 == NULL)
491
        break;
492
 
493
      switch (opcode)
494
        {
495
        case VALPY_ADD:
496
          {
497
            struct type *ltype = value_type (arg1);
498
            struct type *rtype = value_type (arg2);
499
 
500
            CHECK_TYPEDEF (ltype);
501
            ltype = STRIP_REFERENCE (ltype);
502
            CHECK_TYPEDEF (rtype);
503
            rtype = STRIP_REFERENCE (rtype);
504
 
505
            if (TYPE_CODE (ltype) == TYPE_CODE_PTR
506
                && is_integral_type (rtype))
507
              res_val = value_ptradd (arg1, value_as_long (arg2));
508
            else if (TYPE_CODE (rtype) == TYPE_CODE_PTR
509
                     && is_integral_type (ltype))
510
              res_val = value_ptradd (arg2, value_as_long (arg1));
511
            else
512
              res_val = value_binop (arg1, arg2, BINOP_ADD);
513
          }
514
          break;
515
        case VALPY_SUB:
516
          {
517
            struct type *ltype = value_type (arg1);
518
            struct type *rtype = value_type (arg2);
519
 
520
            CHECK_TYPEDEF (ltype);
521
            ltype = STRIP_REFERENCE (ltype);
522
            CHECK_TYPEDEF (rtype);
523
            rtype = STRIP_REFERENCE (rtype);
524
 
525
            if (TYPE_CODE (ltype) == TYPE_CODE_PTR
526
                && TYPE_CODE (rtype) == TYPE_CODE_PTR)
527
              /* A ptrdiff_t for the target would be preferable here.  */
528
              res_val = value_from_longest (builtin_type_pyint,
529
                                            value_ptrdiff (arg1, arg2));
530
            else if (TYPE_CODE (ltype) == TYPE_CODE_PTR
531
                     && is_integral_type (rtype))
532
              res_val = value_ptradd (arg1, - value_as_long (arg2));
533
            else
534
              res_val = value_binop (arg1, arg2, BINOP_SUB);
535
          }
536
          break;
537
        case VALPY_MUL:
538
          res_val = value_binop (arg1, arg2, BINOP_MUL);
539
          break;
540
        case VALPY_DIV:
541
          res_val = value_binop (arg1, arg2, BINOP_DIV);
542
          break;
543
        case VALPY_REM:
544
          res_val = value_binop (arg1, arg2, BINOP_REM);
545
          break;
546
        case VALPY_POW:
547
          res_val = value_binop (arg1, arg2, BINOP_EXP);
548
          break;
549
        case VALPY_LSH:
550
          res_val = value_binop (arg1, arg2, BINOP_LSH);
551
          break;
552
        case VALPY_RSH:
553
          res_val = value_binop (arg1, arg2, BINOP_RSH);
554
          break;
555
        case VALPY_BITAND:
556
          res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND);
557
          break;
558
        case VALPY_BITOR:
559
          res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR);
560
          break;
561
        case VALPY_BITXOR:
562
          res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR);
563
          break;
564
        }
565
    }
566
  GDB_PY_HANDLE_EXCEPTION (except);
567
 
568
  return res_val ? value_to_value_object (res_val) : NULL;
569
}
570
 
571
static PyObject *
572
valpy_add (PyObject *self, PyObject *other)
573
{
574
  return valpy_binop (VALPY_ADD, self, other);
575
}
576
 
577
static PyObject *
578
valpy_subtract (PyObject *self, PyObject *other)
579
{
580
  return valpy_binop (VALPY_SUB, self, other);
581
}
582
 
583
static PyObject *
584
valpy_multiply (PyObject *self, PyObject *other)
585
{
586
  return valpy_binop (VALPY_MUL, self, other);
587
}
588
 
589
static PyObject *
590
valpy_divide (PyObject *self, PyObject *other)
591
{
592
  return valpy_binop (VALPY_DIV, self, other);
593
}
594
 
595
static PyObject *
596
valpy_remainder (PyObject *self, PyObject *other)
597
{
598
  return valpy_binop (VALPY_REM, self, other);
599
}
600
 
601
static PyObject *
602
valpy_power (PyObject *self, PyObject *other, PyObject *unused)
603
{
604
  /* We don't support the ternary form of pow.  I don't know how to express
605
     that, so let's just throw NotImplementedError to at least do something
606
     about it.  */
607
  if (unused != Py_None)
608
    {
609
      PyErr_SetString (PyExc_NotImplementedError,
610
                       "Invalid operation on gdb.Value.");
611
      return NULL;
612
    }
613
 
614
  return valpy_binop (VALPY_POW, self, other);
615
}
616
 
617
static PyObject *
618
valpy_negative (PyObject *self)
619
{
620
  struct value *val = NULL;
621
  volatile struct gdb_exception except;
622
 
623
  TRY_CATCH (except, RETURN_MASK_ALL)
624
    {
625
      val = value_neg (((value_object *) self)->value);
626
    }
627
  GDB_PY_HANDLE_EXCEPTION (except);
628
 
629
  return value_to_value_object (val);
630
}
631
 
632
static PyObject *
633
valpy_positive (PyObject *self)
634
{
635
  return value_to_value_object (((value_object *) self)->value);
636
}
637
 
638
static PyObject *
639
valpy_absolute (PyObject *self)
640
{
641
  struct value *value = ((value_object *) self)->value;
642
 
643
  if (value_less (value, value_zero (value_type (value), not_lval)))
644
    return valpy_negative (self);
645
  else
646
    return valpy_positive (self);
647
}
648
 
649
/* Implements boolean evaluation of gdb.Value.  */
650
static int
651
valpy_nonzero (PyObject *self)
652
{
653
  value_object *self_value = (value_object *) self;
654
  struct type *type;
655
 
656
  type = check_typedef (value_type (self_value->value));
657
 
658
  if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
659
    return !!value_as_long (self_value->value);
660
  else if (TYPE_CODE (type) == TYPE_CODE_FLT)
661
    return value_as_double (self_value->value) != 0;
662
  else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
663
    return !decimal_is_zero (value_contents (self_value->value),
664
                             TYPE_LENGTH (type),
665
                             gdbarch_byte_order (get_type_arch (type)));
666
  else
667
    {
668
      PyErr_SetString (PyExc_TypeError, _("Attempted truth testing on invalid "
669
                                          "gdb.Value type."));
670
      return 0;
671
    }
672
}
673
 
674
/* Implements ~ for value objects.  */
675
static PyObject *
676
valpy_invert (PyObject *self)
677
{
678
  struct value *val = NULL;
679
  volatile struct gdb_exception except;
680
 
681
  TRY_CATCH (except, RETURN_MASK_ALL)
682
    {
683
      val = value_complement (((value_object *) self)->value);
684
    }
685
  GDB_PY_HANDLE_EXCEPTION (except);
686
 
687
  return value_to_value_object (val);
688
}
689
 
690
/* Implements left shift for value objects.  */
691
static PyObject *
692
valpy_lsh (PyObject *self, PyObject *other)
693
{
694
  return valpy_binop (VALPY_LSH, self, other);
695
}
696
 
697
/* Implements right shift for value objects.  */
698
static PyObject *
699
valpy_rsh (PyObject *self, PyObject *other)
700
{
701
  return valpy_binop (VALPY_RSH, self, other);
702
}
703
 
704
/* Implements bitwise and for value objects.  */
705
static PyObject *
706
valpy_and (PyObject *self, PyObject *other)
707
{
708
  return valpy_binop (VALPY_BITAND, self, other);
709
}
710
 
711
/* Implements bitwise or for value objects.  */
712
static PyObject *
713
valpy_or (PyObject *self, PyObject *other)
714
{
715
  return valpy_binop (VALPY_BITOR, self, other);
716
}
717
 
718
/* Implements bitwise xor for value objects.  */
719
static PyObject *
720
valpy_xor (PyObject *self, PyObject *other)
721
{
722
  return valpy_binop (VALPY_BITXOR, self, other);
723
}
724
 
725
/* Implements comparison operations for value objects.  */
726
static PyObject *
727
valpy_richcompare (PyObject *self, PyObject *other, int op)
728
{
729
  int result = 0;
730
  struct value *value_other;
731
  volatile struct gdb_exception except;
732
 
733
  if (other == Py_None)
734
    /* Comparing with None is special.  From what I can tell, in Python
735
       None is smaller than anything else.  */
736
    switch (op) {
737
      case Py_LT:
738
      case Py_LE:
739
      case Py_EQ:
740
        Py_RETURN_FALSE;
741
      case Py_NE:
742
      case Py_GT:
743
      case Py_GE:
744
        Py_RETURN_TRUE;
745
      default:
746
        /* Can't happen.  */
747
        PyErr_SetString (PyExc_NotImplementedError,
748
                         _("Invalid operation on gdb.Value."));
749
        return NULL;
750
    }
751
 
752
  TRY_CATCH (except, RETURN_MASK_ALL)
753
    {
754
      value_other = convert_value_from_python (other);
755
      if (value_other == NULL)
756
        {
757
          result = -1;
758
          break;
759
        }
760
 
761
      switch (op) {
762
        case Py_LT:
763
          result = value_less (((value_object *) self)->value, value_other);
764
          break;
765
        case Py_LE:
766
          result = value_less (((value_object *) self)->value, value_other)
767
            || value_equal (((value_object *) self)->value, value_other);
768
          break;
769
        case Py_EQ:
770
          result = value_equal (((value_object *) self)->value, value_other);
771
          break;
772
        case Py_NE:
773
          result = !value_equal (((value_object *) self)->value, value_other);
774
          break;
775
        case Py_GT:
776
          result = value_less (value_other, ((value_object *) self)->value);
777
          break;
778
        case Py_GE:
779
          result = value_less (value_other, ((value_object *) self)->value)
780
            || value_equal (((value_object *) self)->value, value_other);
781
          break;
782
        default:
783
          /* Can't happen.  */
784
          PyErr_SetString (PyExc_NotImplementedError,
785
                           _("Invalid operation on gdb.Value."));
786
          result = -1;
787
          break;
788
      }
789
    }
790
  GDB_PY_HANDLE_EXCEPTION (except);
791
 
792
  /* In this case, the Python exception has already been set.  */
793
  if (result < 0)
794
    return NULL;
795
 
796
  if (result == 1)
797
    Py_RETURN_TRUE;
798
 
799
  Py_RETURN_FALSE;
800
}
801
 
802
/* Helper function to determine if a type is "int-like".  */
803
static int
804
is_intlike (struct type *type, int ptr_ok)
805
{
806
  CHECK_TYPEDEF (type);
807
  return (TYPE_CODE (type) == TYPE_CODE_INT
808
          || TYPE_CODE (type) == TYPE_CODE_ENUM
809
          || TYPE_CODE (type) == TYPE_CODE_BOOL
810
          || TYPE_CODE (type) == TYPE_CODE_CHAR
811
          || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
812
}
813
 
814
/* Implements conversion to int.  */
815
static PyObject *
816
valpy_int (PyObject *self)
817
{
818
  struct value *value = ((value_object *) self)->value;
819
  struct type *type = value_type (value);
820
  LONGEST l = 0;
821
  volatile struct gdb_exception except;
822
 
823
  CHECK_TYPEDEF (type);
824
  if (!is_intlike (type, 0))
825
    {
826
      PyErr_SetString (PyExc_RuntimeError,
827
                       _("Cannot convert value to int."));
828
      return NULL;
829
    }
830
 
831
  TRY_CATCH (except, RETURN_MASK_ALL)
832
    {
833
      l = value_as_long (value);
834
    }
835
  GDB_PY_HANDLE_EXCEPTION (except);
836
 
837
#ifdef HAVE_LONG_LONG           /* Defined by Python.  */
838
  /* If we have 'long long', and the value overflows a 'long', use a
839
     Python Long; otherwise use a Python Int.  */
840
  if (sizeof (l) > sizeof (long) && (l > PyInt_GetMax ()
841
                                     || l < (- (LONGEST) PyInt_GetMax ()) - 1))
842
    return PyLong_FromLongLong (l);
843
#endif
844
  return PyInt_FromLong (l);
845
}
846
 
847
/* Implements conversion to long.  */
848
static PyObject *
849
valpy_long (PyObject *self)
850
{
851
  struct value *value = ((value_object *) self)->value;
852
  struct type *type = value_type (value);
853
  LONGEST l = 0;
854
  volatile struct gdb_exception except;
855
 
856
  if (!is_intlike (type, 1))
857
    {
858
      PyErr_SetString (PyExc_RuntimeError,
859
                       _("Cannot convert value to long."));
860
      return NULL;
861
    }
862
 
863
  TRY_CATCH (except, RETURN_MASK_ALL)
864
    {
865
      l = value_as_long (value);
866
    }
867
  GDB_PY_HANDLE_EXCEPTION (except);
868
 
869
#ifdef HAVE_LONG_LONG           /* Defined by Python.  */
870
  return PyLong_FromLongLong (l);
871
#else
872
  return PyLong_FromLong (l);
873
#endif
874
}
875
 
876
/* Implements conversion to float.  */
877
static PyObject *
878
valpy_float (PyObject *self)
879
{
880
  struct value *value = ((value_object *) self)->value;
881
  struct type *type = value_type (value);
882
  double d = 0;
883
  volatile struct gdb_exception except;
884
 
885
  CHECK_TYPEDEF (type);
886
  if (TYPE_CODE (type) != TYPE_CODE_FLT)
887
    {
888
      PyErr_SetString (PyExc_RuntimeError,
889
                       _("Cannot convert value to float."));
890
      return NULL;
891
    }
892
 
893
  TRY_CATCH (except, RETURN_MASK_ALL)
894
    {
895
      d = value_as_double (value);
896
    }
897
  GDB_PY_HANDLE_EXCEPTION (except);
898
 
899
  return PyFloat_FromDouble (d);
900
}
901
 
902
/* Returns an object for a value which is released from the all_values chain,
903
   so its lifetime is not bound to the execution of a command.  */
904
PyObject *
905
value_to_value_object (struct value *val)
906
{
907
  value_object *val_obj;
908
 
909
  val_obj = PyObject_New (value_object, &value_object_type);
910
  if (val_obj != NULL)
911
    {
912
      val_obj->value = val;
913
      value_incref (val);
914
      val_obj->address = NULL;
915
      val_obj->type = NULL;
916
      note_value (val_obj);
917
    }
918
 
919
  return (PyObject *) val_obj;
920
}
921
 
922
/* Returns a borrowed reference to the struct value corresponding to
923
   the given value object.  */
924
struct value *
925
value_object_to_value (PyObject *self)
926
{
927
  value_object *real;
928
 
929
  if (! PyObject_TypeCheck (self, &value_object_type))
930
    return NULL;
931
  real = (value_object *) self;
932
  return real->value;
933
}
934
 
935
/* Try to convert a Python value to a gdb value.  If the value cannot
936
   be converted, set a Python exception and return NULL.  Returns a
937
   reference to a new value on the all_values chain.  */
938
 
939
struct value *
940
convert_value_from_python (PyObject *obj)
941
{
942
  struct value *value = NULL; /* -Wall */
943
  struct cleanup *old;
944
  volatile struct gdb_exception except;
945
  int cmp;
946
 
947
  gdb_assert (obj != NULL);
948
 
949
  TRY_CATCH (except, RETURN_MASK_ALL)
950
    {
951
      if (PyBool_Check (obj))
952
        {
953
          cmp = PyObject_IsTrue (obj);
954
          if (cmp >= 0)
955
            value = value_from_longest (builtin_type_pybool, cmp);
956
        }
957
      else if (PyInt_Check (obj))
958
        {
959
          long l = PyInt_AsLong (obj);
960
 
961
          if (! PyErr_Occurred ())
962
            value = value_from_longest (builtin_type_pyint, l);
963
        }
964
      else if (PyLong_Check (obj))
965
        {
966
          LONGEST l = PyLong_AsLongLong (obj);
967
 
968
          if (PyErr_Occurred ())
969
            {
970
              /* If the error was an overflow, we can try converting to
971
                 ULONGEST instead.  */
972
              if (PyErr_ExceptionMatches (PyExc_OverflowError))
973
                {
974
                  PyObject *etype, *evalue, *etraceback, *zero;
975
 
976
                  PyErr_Fetch (&etype, &evalue, &etraceback);
977
                  zero = PyInt_FromLong (0);
978
 
979
                  /* Check whether obj is positive.  */
980
                  if (PyObject_RichCompareBool (obj, zero, Py_GT) > 0)
981
                    {
982
                      ULONGEST ul;
983
 
984
                      ul = PyLong_AsUnsignedLongLong (obj);
985
                      if (! PyErr_Occurred ())
986
                        value = value_from_ulongest (builtin_type_upylong, ul);
987
                    }
988
                  else
989
                    /* There's nothing we can do.  */
990
                    PyErr_Restore (etype, evalue, etraceback);
991
 
992
                  Py_DECREF (zero);
993
                }
994
            }
995
          else
996
            value = value_from_longest (builtin_type_pylong, l);
997
        }
998
      else if (PyFloat_Check (obj))
999
        {
1000
          double d = PyFloat_AsDouble (obj);
1001
 
1002
          if (! PyErr_Occurred ())
1003
            value = value_from_double (builtin_type_pyfloat, d);
1004
        }
1005
      else if (gdbpy_is_string (obj))
1006
        {
1007
          char *s;
1008
 
1009
          s = python_string_to_target_string (obj);
1010
          if (s != NULL)
1011
            {
1012
              old = make_cleanup (xfree, s);
1013
              value = value_cstring (s, strlen (s), builtin_type_pychar);
1014
              do_cleanups (old);
1015
            }
1016
        }
1017
      else if (PyObject_TypeCheck (obj, &value_object_type))
1018
        value = value_copy (((value_object *) obj)->value);
1019
      else if (gdbpy_is_lazy_string (obj))
1020
        {
1021
          PyObject *result;
1022
          PyObject *function = PyString_FromString ("value");
1023
 
1024
          result = PyObject_CallMethodObjArgs (obj, function,  NULL);
1025
          value = value_copy (((value_object *) result)->value);
1026
        }
1027
      else
1028
        PyErr_Format (PyExc_TypeError, _("Could not convert Python object: %s."),
1029
                      PyString_AsString (PyObject_Str (obj)));
1030
    }
1031
  if (except.reason < 0)
1032
    {
1033
      PyErr_Format (except.reason == RETURN_QUIT
1034
                    ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
1035
                    "%s", except.message);
1036
      return NULL;
1037
    }
1038
 
1039
  return value;
1040
}
1041
 
1042
/* Returns value object in the ARGth position in GDB's history.  */
1043
PyObject *
1044
gdbpy_history (PyObject *self, PyObject *args)
1045
{
1046
  int i;
1047
  struct value *res_val = NULL;   /* Initialize to appease gcc warning.  */
1048
  volatile struct gdb_exception except;
1049
 
1050
  if (!PyArg_ParseTuple (args, "i", &i))
1051
    return NULL;
1052
 
1053
  TRY_CATCH (except, RETURN_MASK_ALL)
1054
    {
1055
      res_val = access_value_history (i);
1056
    }
1057
  GDB_PY_HANDLE_EXCEPTION (except);
1058
 
1059
  return value_to_value_object (res_val);
1060
}
1061
 
1062
/* Returns 1 in OBJ is a gdb.Value object, 0 otherwise.  */
1063
 
1064
int
1065
gdbpy_is_value_object (PyObject *obj)
1066
{
1067
  return PyObject_TypeCheck (obj, &value_object_type);
1068
}
1069
 
1070
void
1071
gdbpy_initialize_values (void)
1072
{
1073
  if (PyType_Ready (&value_object_type) < 0)
1074
    return;
1075
 
1076
  Py_INCREF (&value_object_type);
1077
  PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type);
1078
 
1079
  values_in_python = NULL;
1080
}
1081
 
1082
 
1083
 
1084
static PyGetSetDef value_object_getset[] = {
1085
  { "address", valpy_get_address, NULL, "The address of the value.",
1086
    NULL },
1087
  { "is_optimized_out", valpy_get_is_optimized_out, NULL,
1088
    "Boolean telling whether the value is optimized out (i.e., not available).",
1089
    NULL },
1090
  { "type", valpy_get_type, NULL, "Type of the value.", NULL },
1091
  {NULL}  /* Sentinel */
1092
};
1093
 
1094
static PyMethodDef value_object_methods[] = {
1095
  { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
1096
  { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
1097
  { "lazy_string", (PyCFunction) valpy_lazy_string, METH_VARARGS | METH_KEYWORDS,
1098
    "lazy_string ([encoding]  [, length]) -> lazy_string\n\
1099
Return a lazy string representation of the value." },
1100
  { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
1101
    "string ([encoding] [, errors] [, length]) -> string\n\
1102
Return Unicode string representation of the value." },
1103
  {NULL}  /* Sentinel */
1104
};
1105
 
1106
static PyNumberMethods value_object_as_number = {
1107
  valpy_add,
1108
  valpy_subtract,
1109
  valpy_multiply,
1110
  valpy_divide,
1111
  valpy_remainder,
1112
  NULL,                       /* nb_divmod */
1113
  valpy_power,                /* nb_power */
1114
  valpy_negative,             /* nb_negative */
1115
  valpy_positive,             /* nb_positive */
1116
  valpy_absolute,             /* nb_absolute */
1117
  valpy_nonzero,              /* nb_nonzero */
1118
  valpy_invert,               /* nb_invert */
1119
  valpy_lsh,                  /* nb_lshift */
1120
  valpy_rsh,                  /* nb_rshift */
1121
  valpy_and,                  /* nb_and */
1122
  valpy_xor,                  /* nb_xor */
1123
  valpy_or,                   /* nb_or */
1124
  NULL,                       /* nb_coerce */
1125
  valpy_int,                  /* nb_int */
1126
  valpy_long,                 /* nb_long */
1127
  valpy_float,                /* nb_float */
1128
  NULL,                       /* nb_oct */
1129
  NULL                        /* nb_hex */
1130
};
1131
 
1132
static PyMappingMethods value_object_as_mapping = {
1133
  valpy_length,
1134
  valpy_getitem,
1135
  valpy_setitem
1136
};
1137
 
1138
PyTypeObject value_object_type = {
1139
  PyObject_HEAD_INIT (NULL)
1140
  0,                               /*ob_size*/
1141
  "gdb.Value",                    /*tp_name*/
1142
  sizeof (value_object),          /*tp_basicsize*/
1143
  0,                               /*tp_itemsize*/
1144
  valpy_dealloc,                  /*tp_dealloc*/
1145
  0,                               /*tp_print*/
1146
  0,                               /*tp_getattr*/
1147
  0,                               /*tp_setattr*/
1148
  0,                               /*tp_compare*/
1149
  0,                               /*tp_repr*/
1150
  &value_object_as_number,        /*tp_as_number*/
1151
  0,                               /*tp_as_sequence*/
1152
  &value_object_as_mapping,       /*tp_as_mapping*/
1153
  valpy_hash,                     /*tp_hash*/
1154
  0,                               /*tp_call*/
1155
  valpy_str,                      /*tp_str*/
1156
  0,                               /*tp_getattro*/
1157
  0,                               /*tp_setattro*/
1158
  0,                               /*tp_as_buffer*/
1159
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES,   /*tp_flags*/
1160
  "GDB value object",             /* tp_doc */
1161
  0,                               /* tp_traverse */
1162
  0,                               /* tp_clear */
1163
  valpy_richcompare,              /* tp_richcompare */
1164
  0,                               /* tp_weaklistoffset */
1165
  0,                               /* tp_iter */
1166
  0,                               /* tp_iternext */
1167
  value_object_methods,           /* tp_methods */
1168
  0,                               /* tp_members */
1169
  value_object_getset,            /* tp_getset */
1170
  0,                               /* tp_base */
1171
  0,                               /* tp_dict */
1172
  0,                               /* tp_descr_get */
1173
  0,                               /* tp_descr_set */
1174
  0,                               /* tp_dictoffset */
1175
  0,                               /* tp_init */
1176
  0,                               /* tp_alloc */
1177
  valpy_new                       /* tp_new */
1178
};
1179
 
1180
#else
1181
 
1182
void
1183
preserve_python_values (struct objfile *objfile, htab_t copied_types)
1184
{
1185
  /* Nothing.  */
1186
}
1187
 
1188
#endif /* HAVE_PYTHON */

powered by: WebSVN 2.1.0

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