1 /* Chill language support routines for GDB, the GNU debugger.
2 Copyright 1992, 1995, 1996, 2000 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
25 #include "expression.h"
26 #include "parser-defs.h"
31 extern void _initialize_chill_language (void);
34 evaluate_subexp_chill (struct type
*, struct expression
*, int *,
37 static value_ptr
value_chill_max_min (enum exp_opcode
, value_ptr
);
39 static value_ptr
value_chill_card (value_ptr
);
41 static value_ptr
value_chill_length (value_ptr
);
43 static struct type
*chill_create_fundamental_type (struct objfile
*, int);
45 static void chill_printstr (struct ui_file
* stream
, char *string
,
46 unsigned int length
, int width
,
49 static void chill_printchar (int, struct ui_file
*);
51 /* For now, Chill uses a simple mangling algorithm whereby you simply
52 discard everything after the occurance of two successive CPLUS_MARKER
53 characters to derive the demangled form. */
56 chill_demangle (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 (c
, stream
)
86 struct ui_file
*stream
;
88 c
&= 0xFF; /* Avoid sign bit follies */
90 if (PRINT_LITERAL_FORM (c
))
92 if (c
== '\'' || c
== '^')
93 fprintf_filtered (stream
, "'%c%c'", c
, c
);
95 fprintf_filtered (stream
, "'%c'", c
);
99 fprintf_filtered (stream
, "'^(%u)'", (unsigned int) c
);
103 /* Print the character string STRING, printing at most LENGTH characters.
104 Printing stops early if the number hits print_max; repeat counts
105 are printed as appropriate. Print ellipses at the end if we
106 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
107 Note that gdb maintains the length of strings without counting the
108 terminating null byte, while chill strings are typically written with
109 an explicit null byte. So we always assume an implied null byte
110 until gdb is able to maintain non-null terminated strings as well
111 as null terminated strings (FIXME).
115 chill_printstr (stream
, string
, length
, width
, force_ellipses
)
116 struct ui_file
*stream
;
122 register unsigned int i
;
123 unsigned int things_printed
= 0;
124 int in_literal_form
= 0;
125 int in_control_form
= 0;
126 int need_slashslash
= 0;
131 fputs_filtered ("\"\"", stream
);
135 for (i
= 0; i
< length
&& things_printed
< print_max
; ++i
)
137 /* Position of the character we are examining
138 to see whether it is repeated. */
140 /* Number of repetitions we have detected so far. */
147 fputs_filtered ("//", stream
);
153 while (rep1
< length
&& string
[rep1
] == string
[i
])
160 if (reps
> repeat_count_threshold
)
162 if (in_control_form
|| in_literal_form
)
165 fputs_filtered (")", stream
);
166 fputs_filtered ("\"//", stream
);
167 in_control_form
= in_literal_form
= 0;
169 chill_printchar (c
, stream
);
170 fprintf_filtered (stream
, "<repeats %u times>", reps
);
172 things_printed
+= repeat_count_threshold
;
177 if (!in_literal_form
&& !in_control_form
)
178 fputs_filtered ("\"", stream
);
179 if (PRINT_LITERAL_FORM (c
))
181 if (!in_literal_form
)
185 fputs_filtered (")", stream
);
190 fprintf_filtered (stream
, "%c", c
);
191 if (c
== '"' || c
== '^')
192 /* duplicate this one as must be done at input */
193 fprintf_filtered (stream
, "%c", c
);
197 if (!in_control_form
)
203 fputs_filtered ("^(", stream
);
207 fprintf_filtered (stream
, ",");
209 fprintf_filtered (stream
, "%u", (unsigned int) c
);
215 /* Terminate the quotes if necessary. */
218 fputs_filtered (")", stream
);
220 if (in_literal_form
|| in_control_form
)
222 fputs_filtered ("\"", stream
);
224 if (force_ellipses
|| (i
< length
))
226 fputs_filtered ("...", stream
);
231 chill_create_fundamental_type (objfile
, typeid)
232 struct objfile
*objfile
;
235 register struct type
*type
= NULL
;
240 /* FIXME: For now, if we are asked to produce a type not in this
241 language, create the equivalent of a C integer type with the
242 name "<?type?>". When all the dust settles from the type
243 reconstruction work, this should probably become an error. */
244 type
= init_type (TYPE_CODE_INT
, 2, 0, "<?type?>", objfile
);
245 warning ("internal error: no chill fundamental type %d", typeid);
248 /* FIXME: Currently the GNU Chill compiler emits some DWARF entries for
249 typedefs, unrelated to anything directly in the code being compiled,
250 that have some FT_VOID types. Just fake it for now. */
251 type
= init_type (TYPE_CODE_VOID
, 0, 0, "<?VOID?>", objfile
);
254 type
= init_type (TYPE_CODE_BOOL
, 1, TYPE_FLAG_UNSIGNED
, "BOOL", objfile
);
257 type
= init_type (TYPE_CODE_CHAR
, 1, TYPE_FLAG_UNSIGNED
, "CHAR", objfile
);
260 type
= init_type (TYPE_CODE_INT
, 1, 0, "BYTE", objfile
);
262 case FT_UNSIGNED_CHAR
:
263 type
= init_type (TYPE_CODE_INT
, 1, TYPE_FLAG_UNSIGNED
, "UBYTE", objfile
);
265 case FT_SHORT
: /* Chill ints are 2 bytes */
266 type
= init_type (TYPE_CODE_INT
, 2, 0, "INT", objfile
);
268 case FT_UNSIGNED_SHORT
: /* Chill ints are 2 bytes */
269 type
= init_type (TYPE_CODE_INT
, 2, TYPE_FLAG_UNSIGNED
, "UINT", objfile
);
271 case FT_INTEGER
: /* FIXME? */
272 case FT_SIGNED_INTEGER
: /* FIXME? */
273 case FT_LONG
: /* Chill longs are 4 bytes */
274 case FT_SIGNED_LONG
: /* Chill longs are 4 bytes */
275 type
= init_type (TYPE_CODE_INT
, 4, 0, "LONG", objfile
);
277 case FT_UNSIGNED_INTEGER
: /* FIXME? */
278 case FT_UNSIGNED_LONG
: /* Chill longs are 4 bytes */
279 type
= init_type (TYPE_CODE_INT
, 4, TYPE_FLAG_UNSIGNED
, "ULONG", objfile
);
282 type
= init_type (TYPE_CODE_FLT
, 4, 0, "REAL", objfile
);
284 case FT_DBL_PREC_FLOAT
:
285 type
= init_type (TYPE_CODE_FLT
, 8, 0, "LONG_REAL", objfile
);
292 /* Table of operators and their precedences for printing expressions. */
294 static const struct op_print chill_op_print_tab
[] =
296 {"AND", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
297 {"OR", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
298 {"NOT", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
299 {"MOD", BINOP_MOD
, PREC_MUL
, 0},
300 {"REM", BINOP_REM
, PREC_MUL
, 0},
301 {"SIZE", UNOP_SIZEOF
, PREC_BUILTIN_FUNCTION
, 0},
302 {"LOWER", UNOP_LOWER
, PREC_BUILTIN_FUNCTION
, 0},
303 {"UPPER", UNOP_UPPER
, PREC_BUILTIN_FUNCTION
, 0},
304 {"CARD", UNOP_CARD
, PREC_BUILTIN_FUNCTION
, 0},
305 {"MAX", UNOP_CHMAX
, PREC_BUILTIN_FUNCTION
, 0},
306 {"MIN", UNOP_CHMIN
, PREC_BUILTIN_FUNCTION
, 0},
307 {":=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
308 {"=", BINOP_EQUAL
, PREC_EQUAL
, 0},
309 {"/=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
310 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
311 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
312 {">", BINOP_GTR
, PREC_ORDER
, 0},
313 {"<", BINOP_LESS
, PREC_ORDER
, 0},
314 {"+", BINOP_ADD
, PREC_ADD
, 0},
315 {"-", BINOP_SUB
, PREC_ADD
, 0},
316 {"*", BINOP_MUL
, PREC_MUL
, 0},
317 {"/", BINOP_DIV
, PREC_MUL
, 0},
318 {"//", BINOP_CONCAT
, PREC_PREFIX
, 0}, /* FIXME: precedence? */
319 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
320 {"->", UNOP_IND
, PREC_SUFFIX
, 1},
321 {"->", UNOP_ADDR
, PREC_PREFIX
, 0},
322 {":", BINOP_RANGE
, PREC_ASSIGN
, 0},
326 /* The built-in types of Chill. */
328 struct type
*builtin_type_chill_bool
;
329 struct type
*builtin_type_chill_char
;
330 struct type
*builtin_type_chill_long
;
331 struct type
*builtin_type_chill_ulong
;
332 struct type
*builtin_type_chill_real
;
334 struct type
**CONST_PTR (chill_builtin_types
[]) =
336 &builtin_type_chill_bool
,
337 &builtin_type_chill_char
,
338 &builtin_type_chill_long
,
339 &builtin_type_chill_ulong
,
340 &builtin_type_chill_real
,
344 /* Calculate LOWER or UPPER of TYPE.
345 Returns the result as an integer.
346 *RESULT_TYPE is the appropriate type for the result. */
349 type_lower_upper (op
, type
, result_type
)
350 enum exp_opcode op
; /* Either UNOP_LOWER or UNOP_UPPER */
352 struct type
**result_type
;
356 CHECK_TYPEDEF (type
);
357 switch (TYPE_CODE (type
))
359 case TYPE_CODE_STRUCT
:
360 *result_type
= builtin_type_int
;
361 if (chill_varying_type (type
))
362 return type_lower_upper (op
, TYPE_FIELD_TYPE (type
, 1), result_type
);
364 case TYPE_CODE_ARRAY
:
365 case TYPE_CODE_BITSTRING
:
366 case TYPE_CODE_STRING
:
367 type
= TYPE_FIELD_TYPE (type
, 0); /* Get index type */
369 /* ... fall through ... */
370 case TYPE_CODE_RANGE
:
371 *result_type
= TYPE_TARGET_TYPE (type
);
372 return op
== UNOP_LOWER
? TYPE_LOW_BOUND (type
) : TYPE_HIGH_BOUND (type
);
378 if (get_discrete_bounds (type
, &low
, &high
) >= 0)
381 return op
== UNOP_LOWER
? low
: high
;
384 case TYPE_CODE_UNDEF
:
386 case TYPE_CODE_UNION
:
391 case TYPE_CODE_ERROR
:
392 case TYPE_CODE_MEMBER
:
393 case TYPE_CODE_METHOD
:
395 case TYPE_CODE_COMPLEX
:
399 error ("unknown mode for LOWER/UPPER builtin");
403 value_chill_length (val
)
407 struct type
*type
= VALUE_TYPE (val
);
409 CHECK_TYPEDEF (type
);
410 switch (TYPE_CODE (type
))
412 case TYPE_CODE_ARRAY
:
413 case TYPE_CODE_BITSTRING
:
414 case TYPE_CODE_STRING
:
415 tmp
= type_lower_upper (UNOP_UPPER
, type
, &ttype
)
416 - type_lower_upper (UNOP_LOWER
, type
, &ttype
) + 1;
418 case TYPE_CODE_STRUCT
:
419 if (chill_varying_type (type
))
421 tmp
= unpack_long (TYPE_FIELD_TYPE (type
, 0), VALUE_CONTENTS (val
));
424 /* ... else fall through ... */
426 error ("bad argument to LENGTH builtin");
428 return value_from_longest (builtin_type_int
, tmp
);
432 value_chill_card (val
)
436 struct type
*type
= VALUE_TYPE (val
);
437 CHECK_TYPEDEF (type
);
439 if (TYPE_CODE (type
) == TYPE_CODE_SET
)
441 struct type
*range_type
= TYPE_INDEX_TYPE (type
);
442 LONGEST lower_bound
, upper_bound
;
445 get_discrete_bounds (range_type
, &lower_bound
, &upper_bound
);
446 for (i
= lower_bound
; i
<= upper_bound
; i
++)
447 if (value_bit_index (type
, VALUE_CONTENTS (val
), i
) > 0)
451 error ("bad argument to CARD builtin");
453 return value_from_longest (builtin_type_int
, tmp
);
457 value_chill_max_min (op
, val
)
462 struct type
*type
= VALUE_TYPE (val
);
463 struct type
*elttype
;
464 CHECK_TYPEDEF (type
);
466 if (TYPE_CODE (type
) == TYPE_CODE_SET
)
468 LONGEST lower_bound
, upper_bound
;
471 elttype
= TYPE_INDEX_TYPE (type
);
472 CHECK_TYPEDEF (elttype
);
473 get_discrete_bounds (elttype
, &lower_bound
, &upper_bound
);
475 if (op
== UNOP_CHMAX
)
477 for (i
= upper_bound
; i
>= lower_bound
; i
--)
479 if (value_bit_index (type
, VALUE_CONTENTS (val
), i
) > 0)
489 for (i
= lower_bound
; i
<= upper_bound
; i
++)
491 if (value_bit_index (type
, VALUE_CONTENTS (val
), i
) > 0)
500 error ("%s for empty powerset", op
== UNOP_CHMAX
? "MAX" : "MIN");
503 error ("bad argument to %s builtin", op
== UNOP_CHMAX
? "MAX" : "MIN");
505 return value_from_longest (TYPE_CODE (elttype
) == TYPE_CODE_RANGE
506 ? TYPE_TARGET_TYPE (elttype
)
512 evaluate_subexp_chill (expect_type
, exp
, pos
, noside
)
513 struct type
*expect_type
;
514 register struct expression
*exp
;
523 enum exp_opcode op
= exp
->elts
[*pos
].opcode
;
526 case MULTI_SUBSCRIPT
:
527 if (noside
== EVAL_SKIP
)
530 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
531 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
532 type
= check_typedef (VALUE_TYPE (arg1
));
534 if (nargs
== 1 && TYPE_CODE (type
) == TYPE_CODE_INT
)
536 /* Looks like string repetition. */
537 value_ptr string
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
538 return value_concat (arg1
, string
);
541 switch (TYPE_CODE (type
))
544 type
= check_typedef (TYPE_TARGET_TYPE (type
));
545 if (!type
|| TYPE_CODE (type
) != TYPE_CODE_FUNC
)
546 error ("reference value used as function");
547 /* ... fall through ... */
549 /* It's a function call. */
550 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
553 /* Allocate arg vector, including space for the function to be
554 called in argvec[0] and a terminating NULL */
555 argvec
= (value_ptr
*) alloca (sizeof (value_ptr
) * (nargs
+ 2));
558 for (; tem
<= nargs
&& tem
<= TYPE_NFIELDS (type
); tem
++)
561 = evaluate_subexp_chill (TYPE_FIELD_TYPE (type
, tem
- 1),
564 for (; tem
<= nargs
; tem
++)
565 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
566 argvec
[tem
] = 0; /* signal end of arglist */
568 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
575 value_ptr index
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
576 arg1
= value_subscript (arg1
, index
);
583 if (noside
== EVAL_SKIP
)
585 (*exp
->language_defn
->evaluate_exp
) (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
588 arg1
= (*exp
->language_defn
->evaluate_exp
) (NULL_TYPE
, exp
, pos
,
589 EVAL_AVOID_SIDE_EFFECTS
);
590 tem
= type_lower_upper (op
, VALUE_TYPE (arg1
), &type
);
591 return value_from_longest (type
, tem
);
595 arg1
= (*exp
->language_defn
->evaluate_exp
) (NULL_TYPE
, exp
, pos
, noside
);
596 return value_chill_length (arg1
);
600 arg1
= (*exp
->language_defn
->evaluate_exp
) (NULL_TYPE
, exp
, pos
, noside
);
601 return value_chill_card (arg1
);
606 arg1
= (*exp
->language_defn
->evaluate_exp
) (NULL_TYPE
, exp
, pos
, noside
);
607 return value_chill_max_min (op
, arg1
);
610 error ("',' operator used in invalid context");
616 return evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
618 return value_from_longest (builtin_type_long
, (LONGEST
) 1);
621 const struct language_defn chill_language_defn
=
628 chill_parse
, /* parser */
629 chill_error
, /* parser error function */
630 evaluate_subexp_chill
,
631 chill_printchar
, /* print a character constant */
632 chill_printstr
, /* function to print a string constant */
633 NULL
, /* Function to print a single char */
634 chill_create_fundamental_type
, /* Create fundamental type in this language */
635 chill_print_type
, /* Print a type using appropriate syntax */
636 chill_val_print
, /* Print a value using appropriate syntax */
637 chill_value_print
, /* Print a top-levl value */
638 {"", "B'", "", ""}, /* Binary format info */
639 {"O'%lo", "O'", "o", ""}, /* Octal format info */
640 {"D'%ld", "D'", "d", ""}, /* Decimal format info */
641 {"H'%lx", "H'", "x", ""}, /* Hex format info */
642 chill_op_print_tab
, /* expression operators for printing */
643 0, /* arrays are first-class (not c-style) */
644 0, /* String lower bound */
645 &builtin_type_chill_char
, /* Type of string elements */
649 /* Initialization for Chill */
652 _initialize_chill_language ()
654 builtin_type_chill_bool
=
655 init_type (TYPE_CODE_BOOL
, TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
657 "BOOL", (struct objfile
*) NULL
);
658 builtin_type_chill_char
=
659 init_type (TYPE_CODE_CHAR
, TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
661 "CHAR", (struct objfile
*) NULL
);
662 builtin_type_chill_long
=
663 init_type (TYPE_CODE_INT
, TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
665 "LONG", (struct objfile
*) NULL
);
666 builtin_type_chill_ulong
=
667 init_type (TYPE_CODE_INT
, TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
669 "ULONG", (struct objfile
*) NULL
);
670 builtin_type_chill_real
=
671 init_type (TYPE_CODE_FLT
, TARGET_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
673 "LONG_REAL", (struct objfile
*) NULL
);
675 add_language (&chill_language_defn
);