1 /* Chill language support routines for GDB, the GNU debugger.
2 Copyright 1992, 1993, 1994, 1995, 1996, 2000
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "expression.h"
27 #include "parser-defs.h"
32 extern void _initialize_chill_language (void);
35 evaluate_subexp_chill (struct type
*, struct expression
*, int *,
38 static value_ptr
value_chill_max_min (enum exp_opcode
, value_ptr
);
40 static value_ptr
value_chill_card (value_ptr
);
42 static value_ptr
value_chill_length (value_ptr
);
44 static struct type
*chill_create_fundamental_type (struct objfile
*, int);
46 static void chill_printstr (struct ui_file
* stream
, char *string
,
47 unsigned int length
, int width
,
50 static void chill_printchar (int, struct ui_file
*);
52 /* For now, Chill uses a simple mangling algorithm whereby you simply
53 discard everything after the occurance of two successive CPLUS_MARKER
54 characters to derive the demangled form. */
57 chill_demangle (const char *mangled
)
59 const char *joiner
= NULL
;
61 const char *cp
= mangled
;
65 if (is_cplus_marker (*cp
))
72 if (joiner
!= NULL
&& *(joiner
+ 1) == *joiner
)
74 demangled
= savestring (mangled
, joiner
- mangled
);
84 chill_printchar (register int c
, struct ui_file
*stream
)
86 c
&= 0xFF; /* Avoid sign bit follies */
88 if (PRINT_LITERAL_FORM (c
))
90 if (c
== '\'' || c
== '^')
91 fprintf_filtered (stream
, "'%c%c'", c
, c
);
93 fprintf_filtered (stream
, "'%c'", c
);
97 fprintf_filtered (stream
, "'^(%u)'", (unsigned int) c
);
101 /* Print the character string STRING, printing at most LENGTH characters.
102 Printing stops early if the number hits print_max; repeat counts
103 are printed as appropriate. Print ellipses at the end if we
104 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
105 Note that gdb maintains the length of strings without counting the
106 terminating null byte, while chill strings are typically written with
107 an explicit null byte. So we always assume an implied null byte
108 until gdb is able to maintain non-null terminated strings as well
109 as null terminated strings (FIXME).
113 chill_printstr (struct ui_file
*stream
, char *string
, unsigned int length
,
114 int width
, int force_ellipses
)
116 register unsigned int i
;
117 unsigned int things_printed
= 0;
118 int in_literal_form
= 0;
119 int in_control_form
= 0;
120 int need_slashslash
= 0;
125 fputs_filtered ("\"\"", stream
);
129 for (i
= 0; i
< length
&& things_printed
< print_max
; ++i
)
131 /* Position of the character we are examining
132 to see whether it is repeated. */
134 /* Number of repetitions we have detected so far. */
141 fputs_filtered ("//", stream
);
147 while (rep1
< length
&& string
[rep1
] == string
[i
])
154 if (reps
> repeat_count_threshold
)
156 if (in_control_form
|| in_literal_form
)
159 fputs_filtered (")", stream
);
160 fputs_filtered ("\"//", stream
);
161 in_control_form
= in_literal_form
= 0;
163 chill_printchar (c
, stream
);
164 fprintf_filtered (stream
, "<repeats %u times>", reps
);
166 things_printed
+= repeat_count_threshold
;
171 if (!in_literal_form
&& !in_control_form
)
172 fputs_filtered ("\"", stream
);
173 if (PRINT_LITERAL_FORM (c
))
175 if (!in_literal_form
)
179 fputs_filtered (")", stream
);
184 fprintf_filtered (stream
, "%c", c
);
185 if (c
== '"' || c
== '^')
186 /* duplicate this one as must be done at input */
187 fprintf_filtered (stream
, "%c", c
);
191 if (!in_control_form
)
197 fputs_filtered ("^(", stream
);
201 fprintf_filtered (stream
, ",");
203 fprintf_filtered (stream
, "%u", (unsigned int) c
);
209 /* Terminate the quotes if necessary. */
212 fputs_filtered (")", stream
);
214 if (in_literal_form
|| in_control_form
)
216 fputs_filtered ("\"", stream
);
218 if (force_ellipses
|| (i
< length
))
220 fputs_filtered ("...", stream
);
225 chill_create_fundamental_type (struct objfile
*objfile
, int typeid)
227 register struct type
*type
= NULL
;
232 /* FIXME: For now, if we are asked to produce a type not in this
233 language, create the equivalent of a C integer type with the
234 name "<?type?>". When all the dust settles from the type
235 reconstruction work, this should probably become an error. */
236 type
= init_type (TYPE_CODE_INT
, 2, 0, "<?type?>", objfile
);
237 warning ("internal error: no chill fundamental type %d", typeid);
240 /* FIXME: Currently the GNU Chill compiler emits some DWARF entries for
241 typedefs, unrelated to anything directly in the code being compiled,
242 that have some FT_VOID types. Just fake it for now. */
243 type
= init_type (TYPE_CODE_VOID
, 0, 0, "<?VOID?>", objfile
);
246 type
= init_type (TYPE_CODE_BOOL
, 1, TYPE_FLAG_UNSIGNED
, "BOOL", objfile
);
249 type
= init_type (TYPE_CODE_CHAR
, 1, TYPE_FLAG_UNSIGNED
, "CHAR", objfile
);
252 type
= init_type (TYPE_CODE_INT
, 1, 0, "BYTE", objfile
);
254 case FT_UNSIGNED_CHAR
:
255 type
= init_type (TYPE_CODE_INT
, 1, TYPE_FLAG_UNSIGNED
, "UBYTE", objfile
);
257 case FT_SHORT
: /* Chill ints are 2 bytes */
258 type
= init_type (TYPE_CODE_INT
, 2, 0, "INT", objfile
);
260 case FT_UNSIGNED_SHORT
: /* Chill ints are 2 bytes */
261 type
= init_type (TYPE_CODE_INT
, 2, TYPE_FLAG_UNSIGNED
, "UINT", objfile
);
263 case FT_INTEGER
: /* FIXME? */
264 case FT_SIGNED_INTEGER
: /* FIXME? */
265 case FT_LONG
: /* Chill longs are 4 bytes */
266 case FT_SIGNED_LONG
: /* Chill longs are 4 bytes */
267 type
= init_type (TYPE_CODE_INT
, 4, 0, "LONG", objfile
);
269 case FT_UNSIGNED_INTEGER
: /* FIXME? */
270 case FT_UNSIGNED_LONG
: /* Chill longs are 4 bytes */
271 type
= init_type (TYPE_CODE_INT
, 4, TYPE_FLAG_UNSIGNED
, "ULONG", objfile
);
274 type
= init_type (TYPE_CODE_FLT
, 4, 0, "REAL", objfile
);
276 case FT_DBL_PREC_FLOAT
:
277 type
= init_type (TYPE_CODE_FLT
, 8, 0, "LONG_REAL", objfile
);
284 /* Table of operators and their precedences for printing expressions. */
286 static const struct op_print chill_op_print_tab
[] =
288 {"AND", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
289 {"OR", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
290 {"NOT", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
291 {"MOD", BINOP_MOD
, PREC_MUL
, 0},
292 {"REM", BINOP_REM
, PREC_MUL
, 0},
293 {"SIZE", UNOP_SIZEOF
, PREC_BUILTIN_FUNCTION
, 0},
294 {"LOWER", UNOP_LOWER
, PREC_BUILTIN_FUNCTION
, 0},
295 {"UPPER", UNOP_UPPER
, PREC_BUILTIN_FUNCTION
, 0},
296 {"CARD", UNOP_CARD
, PREC_BUILTIN_FUNCTION
, 0},
297 {"MAX", UNOP_CHMAX
, PREC_BUILTIN_FUNCTION
, 0},
298 {"MIN", UNOP_CHMIN
, PREC_BUILTIN_FUNCTION
, 0},
299 {":=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
300 {"=", BINOP_EQUAL
, PREC_EQUAL
, 0},
301 {"/=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
302 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
303 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
304 {">", BINOP_GTR
, PREC_ORDER
, 0},
305 {"<", BINOP_LESS
, PREC_ORDER
, 0},
306 {"+", BINOP_ADD
, PREC_ADD
, 0},
307 {"-", BINOP_SUB
, PREC_ADD
, 0},
308 {"*", BINOP_MUL
, PREC_MUL
, 0},
309 {"/", BINOP_DIV
, PREC_MUL
, 0},
310 {"//", BINOP_CONCAT
, PREC_PREFIX
, 0}, /* FIXME: precedence? */
311 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
312 {"->", UNOP_IND
, PREC_SUFFIX
, 1},
313 {"->", UNOP_ADDR
, PREC_PREFIX
, 0},
314 {":", BINOP_RANGE
, PREC_ASSIGN
, 0},
318 /* The built-in types of Chill. */
320 struct type
*builtin_type_chill_bool
;
321 struct type
*builtin_type_chill_char
;
322 struct type
*builtin_type_chill_long
;
323 struct type
*builtin_type_chill_ulong
;
324 struct type
*builtin_type_chill_real
;
326 struct type
**CONST_PTR (chill_builtin_types
[]) =
328 &builtin_type_chill_bool
,
329 &builtin_type_chill_char
,
330 &builtin_type_chill_long
,
331 &builtin_type_chill_ulong
,
332 &builtin_type_chill_real
,
336 /* Calculate LOWER or UPPER of TYPE.
337 Returns the result as an integer.
338 *RESULT_TYPE is the appropriate type for the result. */
341 type_lower_upper (enum exp_opcode op
, /* Either UNOP_LOWER or UNOP_UPPER */
342 struct type
*type
, struct type
**result_type
)
346 CHECK_TYPEDEF (type
);
347 switch (TYPE_CODE (type
))
349 case TYPE_CODE_STRUCT
:
350 *result_type
= builtin_type_int
;
351 if (chill_varying_type (type
))
352 return type_lower_upper (op
, TYPE_FIELD_TYPE (type
, 1), result_type
);
354 case TYPE_CODE_ARRAY
:
355 case TYPE_CODE_BITSTRING
:
356 case TYPE_CODE_STRING
:
357 type
= TYPE_FIELD_TYPE (type
, 0); /* Get index type */
359 /* ... fall through ... */
360 case TYPE_CODE_RANGE
:
361 *result_type
= TYPE_TARGET_TYPE (type
);
362 return op
== UNOP_LOWER
? TYPE_LOW_BOUND (type
) : TYPE_HIGH_BOUND (type
);
368 if (get_discrete_bounds (type
, &low
, &high
) >= 0)
371 return op
== UNOP_LOWER
? low
: high
;
374 case TYPE_CODE_UNDEF
:
376 case TYPE_CODE_UNION
:
381 case TYPE_CODE_ERROR
:
382 case TYPE_CODE_MEMBER
:
383 case TYPE_CODE_METHOD
:
385 case TYPE_CODE_COMPLEX
:
389 error ("unknown mode for LOWER/UPPER builtin");
393 value_chill_length (value_ptr val
)
396 struct type
*type
= VALUE_TYPE (val
);
398 CHECK_TYPEDEF (type
);
399 switch (TYPE_CODE (type
))
401 case TYPE_CODE_ARRAY
:
402 case TYPE_CODE_BITSTRING
:
403 case TYPE_CODE_STRING
:
404 tmp
= type_lower_upper (UNOP_UPPER
, type
, &ttype
)
405 - type_lower_upper (UNOP_LOWER
, type
, &ttype
) + 1;
407 case TYPE_CODE_STRUCT
:
408 if (chill_varying_type (type
))
410 tmp
= unpack_long (TYPE_FIELD_TYPE (type
, 0), VALUE_CONTENTS (val
));
413 /* ... else fall through ... */
415 error ("bad argument to LENGTH builtin");
417 return value_from_longest (builtin_type_int
, tmp
);
421 value_chill_card (value_ptr val
)
424 struct type
*type
= VALUE_TYPE (val
);
425 CHECK_TYPEDEF (type
);
427 if (TYPE_CODE (type
) == TYPE_CODE_SET
)
429 struct type
*range_type
= TYPE_INDEX_TYPE (type
);
430 LONGEST lower_bound
, upper_bound
;
433 get_discrete_bounds (range_type
, &lower_bound
, &upper_bound
);
434 for (i
= lower_bound
; i
<= upper_bound
; i
++)
435 if (value_bit_index (type
, VALUE_CONTENTS (val
), i
) > 0)
439 error ("bad argument to CARD builtin");
441 return value_from_longest (builtin_type_int
, tmp
);
445 value_chill_max_min (enum exp_opcode op
, value_ptr val
)
448 struct type
*type
= VALUE_TYPE (val
);
449 struct type
*elttype
;
450 CHECK_TYPEDEF (type
);
452 if (TYPE_CODE (type
) == TYPE_CODE_SET
)
454 LONGEST lower_bound
, upper_bound
;
457 elttype
= TYPE_INDEX_TYPE (type
);
458 CHECK_TYPEDEF (elttype
);
459 get_discrete_bounds (elttype
, &lower_bound
, &upper_bound
);
461 if (op
== UNOP_CHMAX
)
463 for (i
= upper_bound
; i
>= lower_bound
; i
--)
465 if (value_bit_index (type
, VALUE_CONTENTS (val
), i
) > 0)
475 for (i
= lower_bound
; i
<= upper_bound
; i
++)
477 if (value_bit_index (type
, VALUE_CONTENTS (val
), i
) > 0)
486 error ("%s for empty powerset", op
== UNOP_CHMAX
? "MAX" : "MIN");
489 error ("bad argument to %s builtin", op
== UNOP_CHMAX
? "MAX" : "MIN");
491 return value_from_longest (TYPE_CODE (elttype
) == TYPE_CODE_RANGE
492 ? TYPE_TARGET_TYPE (elttype
)
498 evaluate_subexp_chill (struct type
*expect_type
,
499 register struct expression
*exp
, register int *pos
,
507 enum exp_opcode op
= exp
->elts
[*pos
].opcode
;
510 case MULTI_SUBSCRIPT
:
511 if (noside
== EVAL_SKIP
)
514 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
515 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
516 type
= check_typedef (VALUE_TYPE (arg1
));
518 if (nargs
== 1 && TYPE_CODE (type
) == TYPE_CODE_INT
)
520 /* Looks like string repetition. */
521 value_ptr string
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
522 return value_concat (arg1
, string
);
525 switch (TYPE_CODE (type
))
528 type
= check_typedef (TYPE_TARGET_TYPE (type
));
529 if (!type
|| TYPE_CODE (type
) != TYPE_CODE_FUNC
)
530 error ("reference value used as function");
531 /* ... fall through ... */
533 /* It's a function call. */
534 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
537 /* Allocate arg vector, including space for the function to be
538 called in argvec[0] and a terminating NULL */
539 argvec
= (value_ptr
*) alloca (sizeof (value_ptr
) * (nargs
+ 2));
542 for (; tem
<= nargs
&& tem
<= TYPE_NFIELDS (type
); tem
++)
545 = evaluate_subexp_chill (TYPE_FIELD_TYPE (type
, tem
- 1),
548 for (; tem
<= nargs
; tem
++)
549 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
550 argvec
[tem
] = 0; /* signal end of arglist */
552 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
559 value_ptr index
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
560 arg1
= value_subscript (arg1
, index
);
567 if (noside
== EVAL_SKIP
)
569 (*exp
->language_defn
->evaluate_exp
) (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
572 arg1
= (*exp
->language_defn
->evaluate_exp
) (NULL_TYPE
, exp
, pos
,
573 EVAL_AVOID_SIDE_EFFECTS
);
574 tem
= type_lower_upper (op
, VALUE_TYPE (arg1
), &type
);
575 return value_from_longest (type
, tem
);
579 arg1
= (*exp
->language_defn
->evaluate_exp
) (NULL_TYPE
, exp
, pos
, noside
);
580 return value_chill_length (arg1
);
584 arg1
= (*exp
->language_defn
->evaluate_exp
) (NULL_TYPE
, exp
, pos
, noside
);
585 return value_chill_card (arg1
);
590 arg1
= (*exp
->language_defn
->evaluate_exp
) (NULL_TYPE
, exp
, pos
, noside
);
591 return value_chill_max_min (op
, arg1
);
594 error ("',' operator used in invalid context");
600 return evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
602 return value_from_longest (builtin_type_long
, (LONGEST
) 1);
605 const struct language_defn chill_language_defn
=
613 chill_parse
, /* parser */
614 chill_error
, /* parser error function */
615 evaluate_subexp_chill
,
616 chill_printchar
, /* print a character constant */
617 chill_printstr
, /* function to print a string constant */
618 NULL
, /* Function to print a single char */
619 chill_create_fundamental_type
, /* Create fundamental type in this language */
620 chill_print_type
, /* Print a type using appropriate syntax */
621 chill_val_print
, /* Print a value using appropriate syntax */
622 chill_value_print
, /* Print a top-levl value */
623 {"", "B'", "", ""}, /* Binary format info */
624 {"O'%lo", "O'", "o", ""}, /* Octal format info */
625 {"D'%ld", "D'", "d", ""}, /* Decimal format info */
626 {"H'%lx", "H'", "x", ""}, /* Hex format info */
627 chill_op_print_tab
, /* expression operators for printing */
628 0, /* arrays are first-class (not c-style) */
629 0, /* String lower bound */
630 &builtin_type_chill_char
, /* Type of string elements */
634 /* Initialization for Chill */
637 _initialize_chill_language (void)
639 builtin_type_chill_bool
=
640 init_type (TYPE_CODE_BOOL
, TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
642 "BOOL", (struct objfile
*) NULL
);
643 builtin_type_chill_char
=
644 init_type (TYPE_CODE_CHAR
, TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
646 "CHAR", (struct objfile
*) NULL
);
647 builtin_type_chill_long
=
648 init_type (TYPE_CODE_INT
, TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
650 "LONG", (struct objfile
*) NULL
);
651 builtin_type_chill_ulong
=
652 init_type (TYPE_CODE_INT
, TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
654 "ULONG", (struct objfile
*) NULL
);
655 builtin_type_chill_real
=
656 init_type (TYPE_CODE_FLT
, TARGET_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
658 "LONG_REAL", (struct objfile
*) NULL
);
660 add_language (&chill_language_defn
);