Commit | Line | Data |
---|---|---|
04497f0b NW |
1 | /*- |
2 | * This code is derived from software copyrighted by the Free Software | |
3 | * Foundation. | |
4 | * | |
5 | * Modified 1991 by Donn Seeley at UUNET Technologies, Inc. | |
6 | * Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory. | |
7 | */ | |
8 | ||
9 | /* Parse C expressions for GDB. | |
10 | Copyright (C) 1986, 1989 Free Software Foundation, Inc. | |
11 | ||
12 | This file is part of GDB. | |
13 | ||
14 | GDB is free software; you can redistribute it and/or modify | |
15 | it under the terms of the GNU General Public License as published by | |
16 | the Free Software Foundation; either version 1, or (at your option) | |
17 | any later version. | |
18 | ||
19 | GDB is distributed in the hope that it will be useful, | |
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | GNU General Public License for more details. | |
23 | ||
24 | You should have received a copy of the GNU General Public License | |
25 | along with GDB; see the file COPYING. If not, write to | |
26 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
27 | \f | |
28 | /* Parse a C expression from text in a string, | |
29 | and return the result as a struct expression pointer. | |
30 | That structure contains arithmetic operations in reverse polish, | |
31 | with constants represented by operations that are followed by special data. | |
32 | See expression.h for the details of the format. | |
33 | What is important here is that it can be built up sequentially | |
34 | during the process of parsing; the lower levels of the tree always | |
35 | come first in the result. */ | |
36 | ||
37 | %{ | |
38 | #ifndef lint | |
39 | static char sccsid[] = "@(#)expread.y 6.3 (Berkeley) 5/8/91"; | |
40 | #endif /* not lint */ | |
41 | ||
42 | #include <stdio.h> | |
43 | #include "defs.h" | |
44 | #include "param.h" | |
45 | #include "symtab.h" | |
46 | #include "frame.h" | |
47 | #include "expression.h" | |
48 | ||
49 | #include <a.out.h> | |
50 | ||
51 | static struct expression *expout; | |
52 | static int expout_size; | |
53 | static int expout_ptr; | |
54 | ||
55 | static int yylex (); | |
56 | static void yyerror (); | |
57 | static void write_exp_elt (); | |
58 | static void write_exp_elt_opcode (); | |
59 | static void write_exp_elt_sym (); | |
60 | static void write_exp_elt_longcst (); | |
61 | static void write_exp_elt_dblcst (); | |
62 | static void write_exp_elt_type (); | |
63 | static void write_exp_elt_intern (); | |
64 | static void write_exp_string (); | |
65 | static void start_arglist (); | |
66 | static int end_arglist (); | |
67 | static void free_funcalls (); | |
68 | static char *copy_name (); | |
69 | ||
70 | /* If this is nonzero, this block is used as the lexical context | |
71 | for symbol names. */ | |
72 | ||
73 | static struct block *expression_context_block; | |
74 | ||
75 | /* The innermost context required by the stack and register variables | |
76 | we've encountered so far. */ | |
77 | struct block *innermost_block; | |
78 | ||
79 | /* The block in which the most recently discovered symbol was found. */ | |
80 | struct block *block_found; | |
81 | ||
82 | /* Number of arguments seen so far in innermost function call. */ | |
83 | static int arglist_len; | |
84 | ||
85 | /* Data structure for saving values of arglist_len | |
86 | for function calls whose arguments contain other function calls. */ | |
87 | ||
88 | struct funcall | |
89 | { | |
90 | struct funcall *next; | |
91 | int arglist_len; | |
92 | }; | |
93 | ||
94 | struct funcall *funcall_chain; | |
95 | ||
96 | /* This kind of datum is used to represent the name | |
97 | of a symbol token. */ | |
98 | ||
99 | struct stoken | |
100 | { | |
101 | char *ptr; | |
102 | int length; | |
103 | }; | |
104 | ||
105 | /* For parsing of complicated types. | |
106 | An array should be preceded in the list by the size of the array. */ | |
107 | enum type_pieces | |
108 | {tp_end = -1, tp_pointer, tp_reference, tp_array, tp_function}; | |
109 | static enum type_pieces *type_stack; | |
110 | static int type_stack_depth, type_stack_size; | |
111 | ||
112 | static void push_type (); | |
113 | static enum type_pieces pop_type (); | |
114 | ||
115 | /* Allow debugging of parsing. */ | |
116 | #define YYDEBUG 1 | |
117 | %} | |
118 | ||
119 | /* Although the yacc "value" of an expression is not used, | |
120 | since the result is stored in the structure being created, | |
121 | other node types do have values. */ | |
122 | ||
123 | %union | |
124 | { | |
125 | LONGEST lval; | |
126 | unsigned LONGEST ulval; | |
127 | double dval; | |
128 | struct symbol *sym; | |
129 | struct type *tval; | |
130 | struct stoken sval; | |
131 | int voidval; | |
132 | struct block *bval; | |
133 | enum exp_opcode opcode; | |
134 | struct internalvar *ivar; | |
135 | ||
136 | struct type **tvec; | |
137 | int *ivec; | |
138 | } | |
139 | ||
140 | %type <voidval> exp exp1 start variable | |
141 | %type <tval> type typebase | |
142 | %type <tvec> nonempty_typelist | |
143 | %type <bval> block | |
144 | ||
145 | /* Fancy type parsing. */ | |
146 | %type <voidval> func_mod direct_abs_decl abs_decl | |
147 | %type <tval> ptype | |
148 | %type <lval> array_mod | |
149 | ||
150 | %token <lval> INT CHAR | |
151 | %token <ulval> UINT | |
152 | %token <dval> FLOAT | |
153 | ||
154 | /* Both NAME and TYPENAME tokens represent symbols in the input, | |
155 | and both convey their data as strings. | |
156 | But a TYPENAME is a string that happens to be defined as a typedef | |
157 | or builtin type name (such as int or char) | |
158 | and a NAME is any other symbol. | |
159 | ||
160 | Contexts where this distinction is not important can use the | |
161 | nonterminal "name", which matches either NAME or TYPENAME. */ | |
162 | ||
163 | %token <sval> NAME TYPENAME BLOCKNAME STRING | |
164 | %type <sval> name name_not_typename typename | |
165 | ||
166 | %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON | |
167 | ||
168 | /* Special type cases, put in to allow the parser to distinguish different | |
169 | legal basetypes. */ | |
170 | %token SIGNED LONG SHORT INT_KEYWORD | |
171 | ||
172 | %token <lval> LAST REGNAME | |
173 | ||
174 | %token <ivar> VARIABLE | |
175 | ||
176 | %token <opcode> ASSIGN_MODIFY | |
177 | ||
178 | /* C++ */ | |
179 | %token THIS | |
180 | ||
181 | %left ',' | |
182 | %left ABOVE_COMMA | |
183 | %right '=' ASSIGN_MODIFY | |
184 | %right '?' | |
185 | %left OR | |
186 | %left AND | |
187 | %left '|' | |
188 | %left '^' | |
189 | %left '&' | |
190 | %left EQUAL NOTEQUAL | |
191 | %left '<' '>' LEQ GEQ | |
192 | %left LSH RSH | |
193 | %left '@' | |
194 | %left '+' '-' | |
195 | %left '*' '/' '%' | |
196 | %right UNARY INCREMENT DECREMENT | |
197 | %right ARROW '.' '[' '(' | |
198 | %left COLONCOLON | |
199 | \f | |
200 | %% | |
201 | ||
202 | start : exp1 | |
203 | ; | |
204 | ||
205 | /* Expressions, including the comma operator. */ | |
206 | exp1 : exp | |
207 | | exp1 ',' exp | |
208 | { write_exp_elt_opcode (BINOP_COMMA); } | |
209 | ; | |
210 | ||
211 | /* Expressions, not including the comma operator. */ | |
212 | exp : '*' exp %prec UNARY | |
213 | { write_exp_elt_opcode (UNOP_IND); } | |
214 | ||
215 | exp : '&' exp %prec UNARY | |
216 | { write_exp_elt_opcode (UNOP_ADDR); } | |
217 | ||
218 | exp : '-' exp %prec UNARY | |
219 | { write_exp_elt_opcode (UNOP_NEG); } | |
220 | ; | |
221 | ||
222 | exp : '!' exp %prec UNARY | |
223 | { write_exp_elt_opcode (UNOP_ZEROP); } | |
224 | ; | |
225 | ||
226 | exp : '~' exp %prec UNARY | |
227 | { write_exp_elt_opcode (UNOP_LOGNOT); } | |
228 | ; | |
229 | ||
230 | exp : INCREMENT exp %prec UNARY | |
231 | { write_exp_elt_opcode (UNOP_PREINCREMENT); } | |
232 | ; | |
233 | ||
234 | exp : DECREMENT exp %prec UNARY | |
235 | { write_exp_elt_opcode (UNOP_PREDECREMENT); } | |
236 | ; | |
237 | ||
238 | exp : exp INCREMENT %prec UNARY | |
239 | { write_exp_elt_opcode (UNOP_POSTINCREMENT); } | |
240 | ; | |
241 | ||
242 | exp : exp DECREMENT %prec UNARY | |
243 | { write_exp_elt_opcode (UNOP_POSTDECREMENT); } | |
244 | ; | |
245 | ||
246 | exp : SIZEOF exp %prec UNARY | |
247 | { write_exp_elt_opcode (UNOP_SIZEOF); } | |
248 | ; | |
249 | ||
250 | exp : exp ARROW name | |
251 | { write_exp_elt_opcode (STRUCTOP_PTR); | |
252 | write_exp_string ($3); | |
253 | write_exp_elt_opcode (STRUCTOP_PTR); } | |
254 | ; | |
255 | ||
256 | exp : exp ARROW '*' exp | |
257 | { write_exp_elt_opcode (STRUCTOP_MPTR); } | |
258 | ; | |
259 | ||
260 | exp : exp '.' name | |
261 | { write_exp_elt_opcode (STRUCTOP_STRUCT); | |
262 | write_exp_string ($3); | |
263 | write_exp_elt_opcode (STRUCTOP_STRUCT); } | |
264 | ; | |
265 | ||
266 | exp : exp '.' '*' exp | |
267 | { write_exp_elt_opcode (STRUCTOP_MEMBER); } | |
268 | ; | |
269 | ||
270 | exp : exp '[' exp1 ']' | |
271 | { write_exp_elt_opcode (BINOP_SUBSCRIPT); } | |
272 | ; | |
273 | ||
274 | exp : exp '(' | |
275 | /* This is to save the value of arglist_len | |
276 | being accumulated by an outer function call. */ | |
277 | { start_arglist (); } | |
278 | arglist ')' %prec ARROW | |
279 | { write_exp_elt_opcode (OP_FUNCALL); | |
280 | write_exp_elt_longcst ((LONGEST) end_arglist ()); | |
281 | write_exp_elt_opcode (OP_FUNCALL); } | |
282 | ; | |
283 | ||
284 | arglist : | |
285 | ; | |
286 | ||
287 | arglist : exp | |
288 | { arglist_len = 1; } | |
289 | ; | |
290 | ||
291 | arglist : arglist ',' exp %prec ABOVE_COMMA | |
292 | { arglist_len++; } | |
293 | ; | |
294 | ||
295 | exp : '{' type '}' exp %prec UNARY | |
296 | { write_exp_elt_opcode (UNOP_MEMVAL); | |
297 | write_exp_elt_type ($2); | |
298 | write_exp_elt_opcode (UNOP_MEMVAL); } | |
299 | ; | |
300 | ||
301 | exp : '(' type ')' exp %prec UNARY | |
302 | { write_exp_elt_opcode (UNOP_CAST); | |
303 | write_exp_elt_type ($2); | |
304 | write_exp_elt_opcode (UNOP_CAST); } | |
305 | ; | |
306 | ||
307 | exp : '(' exp1 ')' | |
308 | { } | |
309 | ; | |
310 | ||
311 | /* Binary operators in order of decreasing precedence. */ | |
312 | ||
313 | exp : exp '@' exp | |
314 | { write_exp_elt_opcode (BINOP_REPEAT); } | |
315 | ; | |
316 | ||
317 | exp : exp '*' exp | |
318 | { write_exp_elt_opcode (BINOP_MUL); } | |
319 | ; | |
320 | ||
321 | exp : exp '/' exp | |
322 | { write_exp_elt_opcode (BINOP_DIV); } | |
323 | ; | |
324 | ||
325 | exp : exp '%' exp | |
326 | { write_exp_elt_opcode (BINOP_REM); } | |
327 | ; | |
328 | ||
329 | exp : exp '+' exp | |
330 | { write_exp_elt_opcode (BINOP_ADD); } | |
331 | ; | |
332 | ||
333 | exp : exp '-' exp | |
334 | { write_exp_elt_opcode (BINOP_SUB); } | |
335 | ; | |
336 | ||
337 | exp : exp LSH exp | |
338 | { write_exp_elt_opcode (BINOP_LSH); } | |
339 | ; | |
340 | ||
341 | exp : exp RSH exp | |
342 | { write_exp_elt_opcode (BINOP_RSH); } | |
343 | ; | |
344 | ||
345 | exp : exp EQUAL exp | |
346 | { write_exp_elt_opcode (BINOP_EQUAL); } | |
347 | ; | |
348 | ||
349 | exp : exp NOTEQUAL exp | |
350 | { write_exp_elt_opcode (BINOP_NOTEQUAL); } | |
351 | ; | |
352 | ||
353 | exp : exp LEQ exp | |
354 | { write_exp_elt_opcode (BINOP_LEQ); } | |
355 | ; | |
356 | ||
357 | exp : exp GEQ exp | |
358 | { write_exp_elt_opcode (BINOP_GEQ); } | |
359 | ; | |
360 | ||
361 | exp : exp '<' exp | |
362 | { write_exp_elt_opcode (BINOP_LESS); } | |
363 | ; | |
364 | ||
365 | exp : exp '>' exp | |
366 | { write_exp_elt_opcode (BINOP_GTR); } | |
367 | ; | |
368 | ||
369 | exp : exp '&' exp | |
370 | { write_exp_elt_opcode (BINOP_LOGAND); } | |
371 | ; | |
372 | ||
373 | exp : exp '^' exp | |
374 | { write_exp_elt_opcode (BINOP_LOGXOR); } | |
375 | ; | |
376 | ||
377 | exp : exp '|' exp | |
378 | { write_exp_elt_opcode (BINOP_LOGIOR); } | |
379 | ; | |
380 | ||
381 | exp : exp AND exp | |
382 | { write_exp_elt_opcode (BINOP_AND); } | |
383 | ; | |
384 | ||
385 | exp : exp OR exp | |
386 | { write_exp_elt_opcode (BINOP_OR); } | |
387 | ; | |
388 | ||
389 | exp : exp '?' exp ':' exp %prec '?' | |
390 | { write_exp_elt_opcode (TERNOP_COND); } | |
391 | ; | |
392 | ||
393 | exp : exp '=' exp | |
394 | { write_exp_elt_opcode (BINOP_ASSIGN); } | |
395 | ; | |
396 | ||
397 | exp : exp ASSIGN_MODIFY exp | |
398 | { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); | |
399 | write_exp_elt_opcode ($2); | |
400 | write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); } | |
401 | ; | |
402 | ||
403 | exp : INT | |
404 | { write_exp_elt_opcode (OP_LONG); | |
405 | if ($1 == (int) $1 || $1 == (unsigned int) $1) | |
406 | write_exp_elt_type (builtin_type_int); | |
407 | else | |
408 | write_exp_elt_type (BUILTIN_TYPE_LONGEST); | |
409 | write_exp_elt_longcst ((LONGEST) $1); | |
410 | write_exp_elt_opcode (OP_LONG); } | |
411 | ; | |
412 | ||
413 | exp : UINT | |
414 | { | |
415 | write_exp_elt_opcode (OP_LONG); | |
416 | if ($1 == (unsigned int) $1) | |
417 | write_exp_elt_type (builtin_type_unsigned_int); | |
418 | else | |
419 | write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST); | |
420 | write_exp_elt_longcst ((LONGEST) $1); | |
421 | write_exp_elt_opcode (OP_LONG); | |
422 | } | |
423 | ; | |
424 | ||
425 | exp : CHAR | |
426 | { write_exp_elt_opcode (OP_LONG); | |
427 | write_exp_elt_type (builtin_type_char); | |
428 | write_exp_elt_longcst ((LONGEST) $1); | |
429 | write_exp_elt_opcode (OP_LONG); } | |
430 | ; | |
431 | ||
432 | exp : FLOAT | |
433 | { write_exp_elt_opcode (OP_DOUBLE); | |
434 | write_exp_elt_type (builtin_type_double); | |
435 | write_exp_elt_dblcst ($1); | |
436 | write_exp_elt_opcode (OP_DOUBLE); } | |
437 | ; | |
438 | ||
439 | exp : variable | |
440 | ; | |
441 | ||
442 | exp : LAST | |
443 | { write_exp_elt_opcode (OP_LAST); | |
444 | write_exp_elt_longcst ((LONGEST) $1); | |
445 | write_exp_elt_opcode (OP_LAST); } | |
446 | ; | |
447 | ||
448 | exp : REGNAME | |
449 | { write_exp_elt_opcode (OP_REGISTER); | |
450 | write_exp_elt_longcst ((LONGEST) $1); | |
451 | write_exp_elt_opcode (OP_REGISTER); } | |
452 | ; | |
453 | ||
454 | exp : VARIABLE | |
455 | { write_exp_elt_opcode (OP_INTERNALVAR); | |
456 | write_exp_elt_intern ($1); | |
457 | write_exp_elt_opcode (OP_INTERNALVAR); } | |
458 | ; | |
459 | ||
460 | exp : SIZEOF '(' type ')' %prec UNARY | |
461 | { write_exp_elt_opcode (OP_LONG); | |
462 | write_exp_elt_type (builtin_type_int); | |
463 | write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3)); | |
464 | write_exp_elt_opcode (OP_LONG); } | |
465 | ; | |
466 | ||
467 | exp : STRING | |
468 | { write_exp_elt_opcode (OP_STRING); | |
469 | write_exp_string ($1); | |
470 | write_exp_elt_opcode (OP_STRING); } | |
471 | ; | |
472 | ||
473 | /* C++. */ | |
474 | exp : THIS | |
475 | { write_exp_elt_opcode (OP_THIS); | |
476 | write_exp_elt_opcode (OP_THIS); } | |
477 | ; | |
478 | ||
479 | /* end of C++. */ | |
480 | ||
481 | block : BLOCKNAME | |
482 | { | |
483 | struct symtab *tem = lookup_symtab (copy_name ($1)); | |
484 | struct symbol *sym; | |
485 | ||
486 | if (tem) | |
487 | $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), 1); | |
488 | else | |
489 | { | |
490 | sym = lookup_symbol (copy_name ($1), | |
491 | expression_context_block, | |
492 | VAR_NAMESPACE, 0); | |
493 | if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK) | |
494 | $$ = SYMBOL_BLOCK_VALUE (sym); | |
495 | else | |
496 | error ("No file or function \"%s\".", | |
497 | copy_name ($1)); | |
498 | } | |
499 | } | |
500 | ; | |
501 | ||
502 | block : block COLONCOLON name | |
503 | { struct symbol *tem | |
504 | = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE, 0); | |
505 | if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK) | |
506 | error ("No function \"%s\" in specified context.", | |
507 | copy_name ($3)); | |
508 | $$ = SYMBOL_BLOCK_VALUE (tem); } | |
509 | ; | |
510 | ||
511 | variable: block COLONCOLON name | |
512 | { struct symbol *sym; | |
513 | sym = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE, 0); | |
514 | if (sym == 0) | |
515 | error ("No symbol \"%s\" in specified context.", | |
516 | copy_name ($3)); | |
517 | write_exp_elt_opcode (OP_VAR_VALUE); | |
518 | write_exp_elt_sym (sym); | |
519 | write_exp_elt_opcode (OP_VAR_VALUE); } | |
520 | ; | |
521 | ||
522 | variable: typebase COLONCOLON name | |
523 | { | |
524 | struct type *type = $1; | |
525 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT | |
526 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
527 | error ("`%s' is not defined as an aggregate type.", | |
528 | TYPE_NAME (type)); | |
529 | ||
530 | write_exp_elt_opcode (OP_SCOPE); | |
531 | write_exp_elt_type (type); | |
532 | write_exp_string ($3); | |
533 | write_exp_elt_opcode (OP_SCOPE); | |
534 | } | |
535 | | COLONCOLON name | |
536 | { | |
537 | char *name = copy_name ($2); | |
538 | struct symbol *sym; | |
539 | int i; | |
540 | ||
541 | sym = lookup_symbol (name, 0, VAR_NAMESPACE, 0); | |
542 | if (sym) | |
543 | { | |
544 | write_exp_elt_opcode (OP_VAR_VALUE); | |
545 | write_exp_elt_sym (sym); | |
546 | write_exp_elt_opcode (OP_VAR_VALUE); | |
547 | break; | |
548 | } | |
549 | for (i = 0; i < misc_function_count; i++) | |
550 | if (!strcmp (misc_function_vector[i].name, name)) | |
551 | break; | |
552 | ||
553 | if (i < misc_function_count) | |
554 | { | |
555 | enum misc_function_type mft = | |
556 | (enum misc_function_type) | |
557 | misc_function_vector[i].type; | |
558 | ||
559 | write_exp_elt_opcode (OP_LONG); | |
560 | write_exp_elt_type (builtin_type_int); | |
561 | write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address); | |
562 | write_exp_elt_opcode (OP_LONG); | |
563 | write_exp_elt_opcode (UNOP_MEMVAL); | |
564 | if (mft == mf_data || mft == mf_bss) | |
565 | write_exp_elt_type (builtin_type_int); | |
566 | else if (mft == mf_text) | |
567 | write_exp_elt_type (lookup_function_type (builtin_type_int)); | |
568 | else | |
569 | write_exp_elt_type (builtin_type_char); | |
570 | write_exp_elt_opcode (UNOP_MEMVAL); | |
571 | } | |
572 | else | |
573 | if (symtab_list == 0 | |
574 | && partial_symtab_list == 0) | |
575 | error ("No symbol table is loaded. Use the \"symbol-file\" command."); | |
576 | else | |
577 | error ("No symbol \"%s\" in current context.", name); | |
578 | } | |
579 | ; | |
580 | ||
581 | variable: name_not_typename | |
582 | { struct symbol *sym; | |
583 | int is_a_field_of_this; | |
584 | ||
585 | sym = lookup_symbol (copy_name ($1), | |
586 | expression_context_block, | |
587 | VAR_NAMESPACE, | |
588 | &is_a_field_of_this); | |
589 | if (sym) | |
590 | { | |
591 | switch (sym->class) | |
592 | { | |
593 | case LOC_REGISTER: | |
594 | case LOC_ARG: | |
595 | case LOC_LOCAL: | |
596 | if (innermost_block == 0 || | |
597 | contained_in (block_found, | |
598 | innermost_block)) | |
599 | innermost_block = block_found; | |
600 | } | |
601 | write_exp_elt_opcode (OP_VAR_VALUE); | |
602 | write_exp_elt_sym (sym); | |
603 | write_exp_elt_opcode (OP_VAR_VALUE); | |
604 | } | |
605 | else if (is_a_field_of_this) | |
606 | { | |
607 | /* C++: it hangs off of `this'. Must | |
608 | not inadvertently convert from a method call | |
609 | to data ref. */ | |
610 | if (innermost_block == 0 || | |
611 | contained_in (block_found, innermost_block)) | |
612 | innermost_block = block_found; | |
613 | write_exp_elt_opcode (OP_THIS); | |
614 | write_exp_elt_opcode (OP_THIS); | |
615 | write_exp_elt_opcode (STRUCTOP_PTR); | |
616 | write_exp_string ($1); | |
617 | write_exp_elt_opcode (STRUCTOP_PTR); | |
618 | } | |
619 | else | |
620 | { | |
621 | register int i; | |
622 | register char *arg = copy_name ($1); | |
623 | ||
624 | for (i = 0; i < misc_function_count; i++) | |
625 | if (!strcmp (misc_function_vector[i].name, arg)) | |
626 | break; | |
627 | ||
628 | if (i < misc_function_count) | |
629 | { | |
630 | enum misc_function_type mft = | |
631 | (enum misc_function_type) | |
632 | misc_function_vector[i].type; | |
633 | ||
634 | write_exp_elt_opcode (OP_LONG); | |
635 | write_exp_elt_type (builtin_type_int); | |
636 | write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address); | |
637 | write_exp_elt_opcode (OP_LONG); | |
638 | write_exp_elt_opcode (UNOP_MEMVAL); | |
639 | if (mft == mf_data || mft == mf_bss) | |
640 | write_exp_elt_type (builtin_type_int); | |
641 | else if (mft == mf_text) | |
642 | write_exp_elt_type (lookup_function_type (builtin_type_int)); | |
643 | else | |
644 | write_exp_elt_type (builtin_type_char); | |
645 | write_exp_elt_opcode (UNOP_MEMVAL); | |
646 | } | |
647 | else if (symtab_list == 0 | |
648 | && partial_symtab_list == 0) | |
649 | error ("No symbol table is loaded. Use the \"symbol-file\" command."); | |
650 | else | |
651 | error ("No symbol \"%s\" in current context.", | |
652 | copy_name ($1)); | |
653 | } | |
654 | } | |
655 | ; | |
656 | ||
657 | ||
658 | ptype : typebase | |
659 | | typebase abs_decl | |
660 | { | |
661 | /* This is where the interesting stuff happens. */ | |
662 | int done = 0; | |
663 | int array_size; | |
664 | struct type *follow_type = $1; | |
665 | ||
666 | while (!done) | |
667 | switch (pop_type ()) | |
668 | { | |
669 | case tp_end: | |
670 | done = 1; | |
671 | break; | |
672 | case tp_pointer: | |
673 | follow_type = lookup_pointer_type (follow_type); | |
674 | break; | |
675 | case tp_reference: | |
676 | follow_type = lookup_reference_type (follow_type); | |
677 | break; | |
678 | case tp_array: | |
679 | array_size = (int) pop_type (); | |
680 | if (array_size != -1) | |
681 | follow_type = create_array_type (follow_type, | |
682 | array_size); | |
683 | else | |
684 | follow_type = lookup_pointer_type (follow_type); | |
685 | break; | |
686 | case tp_function: | |
687 | follow_type = lookup_function_type (follow_type); | |
688 | break; | |
689 | } | |
690 | $$ = follow_type; | |
691 | } | |
692 | ; | |
693 | ||
694 | abs_decl: '*' | |
695 | { push_type (tp_pointer); $$ = 0; } | |
696 | | '*' abs_decl | |
697 | { push_type (tp_pointer); $$ = $2; } | |
698 | | direct_abs_decl | |
699 | ; | |
700 | ||
701 | direct_abs_decl: '(' abs_decl ')' | |
702 | { $$ = $2; } | |
703 | | direct_abs_decl array_mod | |
704 | { | |
705 | push_type ((enum type_pieces) $2); | |
706 | push_type (tp_array); | |
707 | } | |
708 | | array_mod | |
709 | { | |
710 | push_type ((enum type_pieces) $1); | |
711 | push_type (tp_array); | |
712 | $$ = 0; | |
713 | } | |
714 | | direct_abs_decl func_mod | |
715 | { push_type (tp_function); } | |
716 | | func_mod | |
717 | { push_type (tp_function); } | |
718 | ; | |
719 | ||
720 | array_mod: '[' ']' | |
721 | { $$ = -1; } | |
722 | | '[' INT ']' | |
723 | { $$ = $2; } | |
724 | ; | |
725 | ||
726 | func_mod: '(' ')' | |
727 | { $$ = 0; } | |
728 | ; | |
729 | ||
730 | type : ptype | |
731 | | typebase COLONCOLON '*' | |
732 | { $$ = lookup_member_type (builtin_type_int, $1); } | |
733 | | type '(' typebase COLONCOLON '*' ')' | |
734 | { $$ = lookup_member_type ($1, $3); } | |
735 | | type '(' typebase COLONCOLON '*' ')' '(' ')' | |
736 | { $$ = lookup_member_type | |
737 | (lookup_function_type ($1), $3); } | |
738 | | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')' | |
739 | { $$ = lookup_member_type | |
740 | (lookup_function_type ($1), $3); | |
741 | free ($8); } | |
742 | ; | |
743 | ||
744 | typebase | |
745 | : TYPENAME | |
746 | { $$ = lookup_typename (copy_name ($1), | |
747 | expression_context_block, 0); } | |
748 | | INT_KEYWORD | |
749 | { $$ = builtin_type_int; } | |
750 | | LONG | |
751 | { $$ = builtin_type_long; } | |
752 | | SHORT | |
753 | { $$ = builtin_type_short; } | |
754 | | LONG INT_KEYWORD | |
755 | { $$ = builtin_type_long; } | |
756 | | UNSIGNED LONG INT_KEYWORD | |
757 | { $$ = builtin_type_unsigned_long; } | |
758 | | SHORT INT_KEYWORD | |
759 | { $$ = builtin_type_short; } | |
760 | | UNSIGNED SHORT INT_KEYWORD | |
761 | { $$ = builtin_type_unsigned_short; } | |
762 | | STRUCT name | |
763 | { $$ = lookup_struct (copy_name ($2), | |
764 | expression_context_block); } | |
765 | | UNION name | |
766 | { $$ = lookup_union (copy_name ($2), | |
767 | expression_context_block); } | |
768 | | ENUM name | |
769 | { $$ = lookup_enum (copy_name ($2), | |
770 | expression_context_block); } | |
771 | | UNSIGNED typename | |
772 | { $$ = lookup_unsigned_typename (copy_name ($2)); } | |
773 | | UNSIGNED | |
774 | { $$ = builtin_type_unsigned_int; } | |
775 | | SIGNED typename | |
776 | { $$ = lookup_typename (copy_name ($2), | |
777 | expression_context_block, 0); } | |
778 | | SIGNED | |
779 | { $$ = builtin_type_int; } | |
780 | ; | |
781 | ||
782 | typename: TYPENAME | |
783 | | INT_KEYWORD | |
784 | { | |
785 | $$.ptr = "int"; | |
786 | $$.length = 3; | |
787 | } | |
788 | | LONG | |
789 | { | |
790 | $$.ptr = "long"; | |
791 | $$.length = 4; | |
792 | } | |
793 | | SHORT | |
794 | { | |
795 | $$.ptr = "short"; | |
796 | $$.length = 5; | |
797 | } | |
798 | ; | |
799 | ||
800 | nonempty_typelist | |
801 | : type | |
802 | { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2); | |
803 | $$[0] = (struct type *)0; | |
804 | $$[1] = $1; | |
805 | } | |
806 | | nonempty_typelist ',' type | |
807 | { int len = sizeof (struct type *) * ++($<ivec>1[0]); | |
808 | $$ = (struct type **)xrealloc ($1, len); | |
809 | $$[$<ivec>$[0]] = $3; | |
810 | } | |
811 | ; | |
812 | ||
813 | name : NAME | |
814 | | BLOCKNAME | |
815 | | TYPENAME | |
816 | ; | |
817 | ||
818 | name_not_typename : NAME | |
819 | | BLOCKNAME | |
820 | ; | |
821 | ||
822 | %% | |
823 | \f | |
824 | /* Begin counting arguments for a function call, | |
825 | saving the data about any containing call. */ | |
826 | ||
827 | static void | |
828 | start_arglist () | |
829 | { | |
830 | register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall)); | |
831 | ||
832 | new->next = funcall_chain; | |
833 | new->arglist_len = arglist_len; | |
834 | arglist_len = 0; | |
835 | funcall_chain = new; | |
836 | } | |
837 | ||
838 | /* Return the number of arguments in a function call just terminated, | |
839 | and restore the data for the containing function call. */ | |
840 | ||
841 | static int | |
842 | end_arglist () | |
843 | { | |
844 | register int val = arglist_len; | |
845 | register struct funcall *call = funcall_chain; | |
846 | funcall_chain = call->next; | |
847 | arglist_len = call->arglist_len; | |
848 | free (call); | |
849 | return val; | |
850 | } | |
851 | ||
852 | /* Free everything in the funcall chain. | |
853 | Used when there is an error inside parsing. */ | |
854 | ||
855 | static void | |
856 | free_funcalls () | |
857 | { | |
858 | register struct funcall *call, *next; | |
859 | ||
860 | for (call = funcall_chain; call; call = next) | |
861 | { | |
862 | next = call->next; | |
863 | free (call); | |
864 | } | |
865 | } | |
866 | \f | |
867 | /* This page contains the functions for adding data to the struct expression | |
868 | being constructed. */ | |
869 | ||
870 | /* Add one element to the end of the expression. */ | |
871 | ||
872 | /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into | |
873 | a register through here */ | |
874 | ||
875 | static void | |
876 | write_exp_elt (expelt) | |
877 | union exp_element expelt; | |
878 | { | |
879 | if (expout_ptr >= expout_size) | |
880 | { | |
881 | expout_size *= 2; | |
882 | expout = (struct expression *) xrealloc (expout, | |
883 | sizeof (struct expression) | |
884 | + expout_size * sizeof (union exp_element)); | |
885 | } | |
886 | expout->elts[expout_ptr++] = expelt; | |
887 | } | |
888 | ||
889 | static void | |
890 | write_exp_elt_opcode (expelt) | |
891 | enum exp_opcode expelt; | |
892 | { | |
893 | union exp_element tmp; | |
894 | ||
895 | tmp.opcode = expelt; | |
896 | ||
897 | write_exp_elt (tmp); | |
898 | } | |
899 | ||
900 | static void | |
901 | write_exp_elt_sym (expelt) | |
902 | struct symbol *expelt; | |
903 | { | |
904 | union exp_element tmp; | |
905 | ||
906 | tmp.symbol = expelt; | |
907 | ||
908 | write_exp_elt (tmp); | |
909 | } | |
910 | ||
911 | static void | |
912 | write_exp_elt_longcst (expelt) | |
913 | LONGEST expelt; | |
914 | { | |
915 | union exp_element tmp; | |
916 | ||
917 | tmp.longconst = expelt; | |
918 | ||
919 | write_exp_elt (tmp); | |
920 | } | |
921 | ||
922 | static void | |
923 | write_exp_elt_dblcst (expelt) | |
924 | double expelt; | |
925 | { | |
926 | union exp_element tmp; | |
927 | ||
928 | tmp.doubleconst = expelt; | |
929 | ||
930 | write_exp_elt (tmp); | |
931 | } | |
932 | ||
933 | static void | |
934 | write_exp_elt_type (expelt) | |
935 | struct type *expelt; | |
936 | { | |
937 | union exp_element tmp; | |
938 | ||
939 | tmp.type = expelt; | |
940 | ||
941 | write_exp_elt (tmp); | |
942 | } | |
943 | ||
944 | static void | |
945 | write_exp_elt_intern (expelt) | |
946 | struct internalvar *expelt; | |
947 | { | |
948 | union exp_element tmp; | |
949 | ||
950 | tmp.internalvar = expelt; | |
951 | ||
952 | write_exp_elt (tmp); | |
953 | } | |
954 | ||
955 | /* Add a string constant to the end of the expression. | |
956 | Follow it by its length in bytes, as a separate exp_element. */ | |
957 | ||
958 | static void | |
959 | write_exp_string (str) | |
960 | struct stoken str; | |
961 | { | |
962 | register int len = str.length; | |
963 | register int lenelt | |
964 | = (len + sizeof (union exp_element)) / sizeof (union exp_element); | |
965 | ||
966 | expout_ptr += lenelt; | |
967 | ||
968 | if (expout_ptr >= expout_size) | |
969 | { | |
970 | expout_size = max (expout_size * 2, expout_ptr + 10); | |
971 | expout = (struct expression *) | |
972 | xrealloc (expout, (sizeof (struct expression) | |
973 | + (expout_size * sizeof (union exp_element)))); | |
974 | } | |
975 | bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len); | |
976 | ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0; | |
977 | write_exp_elt_longcst ((LONGEST) len); | |
978 | } | |
979 | \f | |
980 | /* During parsing of a C expression, the pointer to the next character | |
981 | is in this variable. */ | |
982 | ||
983 | static char *lexptr; | |
984 | ||
985 | /* Tokens that refer to names do so with explicit pointer and length, | |
986 | so they can share the storage that lexptr is parsing. | |
987 | ||
988 | When it is necessary to pass a name to a function that expects | |
989 | a null-terminated string, the substring is copied out | |
990 | into a block of storage that namecopy points to. | |
991 | ||
992 | namecopy is allocated once, guaranteed big enough, for each parsing. */ | |
993 | ||
994 | static char *namecopy; | |
995 | ||
996 | /* Current depth in parentheses within the expression. */ | |
997 | ||
998 | static int paren_depth; | |
999 | ||
1000 | /* Nonzero means stop parsing on first comma (if not within parentheses). */ | |
1001 | ||
1002 | static int comma_terminates; | |
1003 | ||
1004 | /* Take care of parsing a number (anything that starts with a digit). | |
1005 | Set yylval and return the token type; update lexptr. | |
1006 | LEN is the number of characters in it. */ | |
1007 | ||
1008 | /*** Needs some error checking for the float case ***/ | |
1009 | ||
1010 | static int | |
1011 | parse_number (olen) | |
1012 | int olen; | |
1013 | { | |
1014 | register char *p = lexptr; | |
1015 | register LONGEST n = 0; | |
1016 | register int c; | |
1017 | register int base = 10; | |
1018 | register int len = olen; | |
1019 | char *err_copy; | |
1020 | int unsigned_p = 0; | |
1021 | ||
1022 | extern double atof (); | |
1023 | ||
1024 | for (c = 0; c < len; c++) | |
1025 | if (p[c] == '.') | |
1026 | { | |
1027 | /* It's a float since it contains a point. */ | |
1028 | yylval.dval = atof (p); | |
1029 | lexptr += len; | |
1030 | return FLOAT; | |
1031 | } | |
1032 | ||
1033 | if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) | |
1034 | { | |
1035 | p += 2; | |
1036 | base = 16; | |
1037 | len -= 2; | |
1038 | } | |
1039 | else if (*p == '0') | |
1040 | base = 8; | |
1041 | ||
1042 | while (len-- > 0) | |
1043 | { | |
1044 | c = *p++; | |
1045 | if (c >= 'A' && c <= 'Z') c += 'a' - 'A'; | |
1046 | if (c != 'l' && c != 'u') | |
1047 | n *= base; | |
1048 | if (c >= '0' && c <= '9') | |
1049 | n += c - '0'; | |
1050 | else | |
1051 | { | |
1052 | if (base == 16 && c >= 'a' && c <= 'f') | |
1053 | n += c - 'a' + 10; | |
1054 | else if (len == 0 && c == 'l') | |
1055 | ; | |
1056 | else if (len == 0 && c == 'u') | |
1057 | unsigned_p = 1; | |
1058 | else if (base == 10 && len != 0 && (c == 'e' || c == 'E')) | |
1059 | { | |
1060 | /* Scientific notation, where we are unlucky enough not | |
1061 | to have a '.' in the string. */ | |
1062 | yylval.dval = atof (lexptr); | |
1063 | lexptr += olen; | |
1064 | return FLOAT; | |
1065 | } | |
1066 | else | |
1067 | { | |
1068 | err_copy = (char *) alloca (olen + 1); | |
1069 | bcopy (lexptr, err_copy, olen); | |
1070 | err_copy[olen] = 0; | |
1071 | error ("Invalid number \"%s\".", err_copy); | |
1072 | } | |
1073 | } | |
1074 | } | |
1075 | ||
1076 | lexptr = p; | |
1077 | if (unsigned_p) | |
1078 | { | |
1079 | yylval.ulval = n; | |
1080 | return UINT; | |
1081 | } | |
1082 | else | |
1083 | { | |
1084 | yylval.lval = n; | |
1085 | return INT; | |
1086 | } | |
1087 | } | |
1088 | ||
1089 | struct token | |
1090 | { | |
1091 | char *operator; | |
1092 | int token; | |
1093 | enum exp_opcode opcode; | |
1094 | }; | |
1095 | ||
1096 | static struct token tokentab3[] = | |
1097 | { | |
1098 | {">>=", ASSIGN_MODIFY, BINOP_RSH}, | |
1099 | {"<<=", ASSIGN_MODIFY, BINOP_LSH} | |
1100 | }; | |
1101 | ||
1102 | static struct token tokentab2[] = | |
1103 | { | |
1104 | {"+=", ASSIGN_MODIFY, BINOP_ADD}, | |
1105 | {"-=", ASSIGN_MODIFY, BINOP_SUB}, | |
1106 | {"*=", ASSIGN_MODIFY, BINOP_MUL}, | |
1107 | {"/=", ASSIGN_MODIFY, BINOP_DIV}, | |
1108 | {"%=", ASSIGN_MODIFY, BINOP_REM}, | |
1109 | {"|=", ASSIGN_MODIFY, BINOP_LOGIOR}, | |
1110 | {"&=", ASSIGN_MODIFY, BINOP_LOGAND}, | |
1111 | {"^=", ASSIGN_MODIFY, BINOP_LOGXOR}, | |
1112 | {"++", INCREMENT, BINOP_END}, | |
1113 | {"--", DECREMENT, BINOP_END}, | |
1114 | {"->", ARROW, BINOP_END}, | |
1115 | {"&&", AND, BINOP_END}, | |
1116 | {"||", OR, BINOP_END}, | |
1117 | {"::", COLONCOLON, BINOP_END}, | |
1118 | {"<<", LSH, BINOP_END}, | |
1119 | {">>", RSH, BINOP_END}, | |
1120 | {"==", EQUAL, BINOP_END}, | |
1121 | {"!=", NOTEQUAL, BINOP_END}, | |
1122 | {"<=", LEQ, BINOP_END}, | |
1123 | {">=", GEQ, BINOP_END} | |
1124 | }; | |
1125 | ||
1126 | /* assign machine-independent names to certain registers | |
1127 | * (unless overridden by the REGISTER_NAMES table) | |
1128 | */ | |
1129 | struct std_regs { | |
1130 | char *name; | |
1131 | int regnum; | |
1132 | } std_regs[] = { | |
1133 | #ifdef PC_REGNUM | |
1134 | { "pc", PC_REGNUM }, | |
1135 | #endif | |
1136 | #ifdef FP_REGNUM | |
1137 | { "fp", FP_REGNUM }, | |
1138 | #endif | |
1139 | #ifdef SP_REGNUM | |
1140 | { "sp", SP_REGNUM }, | |
1141 | #endif | |
1142 | #ifdef PS_REGNUM | |
1143 | { "ps", PS_REGNUM }, | |
1144 | #endif | |
1145 | }; | |
1146 | ||
1147 | #define NUM_STD_REGS (sizeof std_regs / sizeof std_regs[0]) | |
1148 | ||
1149 | /* Read one token, getting characters through lexptr. */ | |
1150 | ||
1151 | static int | |
1152 | yylex () | |
1153 | { | |
1154 | register int c; | |
1155 | register int namelen; | |
1156 | register int i; | |
1157 | register char *tokstart; | |
1158 | ||
1159 | retry: | |
1160 | ||
1161 | tokstart = lexptr; | |
1162 | /* See if it is a special token of length 3. */ | |
1163 | for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++) | |
1164 | if (!strncmp (tokstart, tokentab3[i].operator, 3)) | |
1165 | { | |
1166 | lexptr += 3; | |
1167 | yylval.opcode = tokentab3[i].opcode; | |
1168 | return tokentab3[i].token; | |
1169 | } | |
1170 | ||
1171 | /* See if it is a special token of length 2. */ | |
1172 | for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++) | |
1173 | if (!strncmp (tokstart, tokentab2[i].operator, 2)) | |
1174 | { | |
1175 | lexptr += 2; | |
1176 | yylval.opcode = tokentab2[i].opcode; | |
1177 | return tokentab2[i].token; | |
1178 | } | |
1179 | ||
1180 | switch (c = *tokstart) | |
1181 | { | |
1182 | case 0: | |
1183 | return 0; | |
1184 | ||
1185 | case ' ': | |
1186 | case '\t': | |
1187 | case '\n': | |
1188 | lexptr++; | |
1189 | goto retry; | |
1190 | ||
1191 | case '\'': | |
1192 | lexptr++; | |
1193 | c = *lexptr++; | |
1194 | if (c == '\\') | |
1195 | c = parse_escape (&lexptr); | |
1196 | yylval.lval = c; | |
1197 | c = *lexptr++; | |
1198 | if (c != '\'') | |
1199 | error ("Invalid character constant."); | |
1200 | return CHAR; | |
1201 | ||
1202 | case '(': | |
1203 | paren_depth++; | |
1204 | lexptr++; | |
1205 | return c; | |
1206 | ||
1207 | case ')': | |
1208 | if (paren_depth == 0) | |
1209 | return 0; | |
1210 | paren_depth--; | |
1211 | lexptr++; | |
1212 | return c; | |
1213 | ||
1214 | case ',': | |
1215 | if (comma_terminates && paren_depth == 0) | |
1216 | return 0; | |
1217 | lexptr++; | |
1218 | return c; | |
1219 | ||
1220 | case '.': | |
1221 | /* Might be a floating point number. */ | |
1222 | if (lexptr[1] >= '0' && lexptr[1] <= '9') | |
1223 | break; /* Falls into number code. */ | |
1224 | ||
1225 | case '+': | |
1226 | case '-': | |
1227 | case '*': | |
1228 | case '/': | |
1229 | case '%': | |
1230 | case '|': | |
1231 | case '&': | |
1232 | case '^': | |
1233 | case '~': | |
1234 | case '!': | |
1235 | case '@': | |
1236 | case '<': | |
1237 | case '>': | |
1238 | case '[': | |
1239 | case ']': | |
1240 | case '?': | |
1241 | case ':': | |
1242 | case '=': | |
1243 | case '{': | |
1244 | case '}': | |
1245 | lexptr++; | |
1246 | return c; | |
1247 | ||
1248 | case '"': | |
1249 | for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++) | |
1250 | if (c == '\\') | |
1251 | { | |
1252 | c = tokstart[++namelen]; | |
1253 | if (c >= '0' && c <= '9') | |
1254 | { | |
1255 | c = tokstart[++namelen]; | |
1256 | if (c >= '0' && c <= '9') | |
1257 | c = tokstart[++namelen]; | |
1258 | } | |
1259 | } | |
1260 | yylval.sval.ptr = tokstart + 1; | |
1261 | yylval.sval.length = namelen - 1; | |
1262 | lexptr += namelen + 1; | |
1263 | return STRING; | |
1264 | } | |
1265 | ||
1266 | /* Is it a number? */ | |
1267 | /* Note: We have already dealt with the case of the token '.'. | |
1268 | See case '.' above. */ | |
1269 | if ((c >= '0' && c <= '9') || c == '.') | |
1270 | { | |
1271 | /* It's a number. */ | |
1272 | int got_dot = 0, got_e = 0; | |
1273 | register char *p = tokstart; | |
1274 | int hex = c == '0' && (p[1] == 'x' || p[1] == 'X'); | |
1275 | if (hex) | |
1276 | p += 2; | |
1277 | for (;; ++p) | |
1278 | { | |
1279 | if (!hex && !got_e && (*p == 'e' || *p == 'E')) | |
1280 | got_dot = got_e = 1; | |
1281 | else if (!hex && !got_dot && *p == '.') | |
1282 | got_dot = 1; | |
1283 | else if (got_e && (p[-1] == 'e' || p[-1] == 'E') | |
1284 | && (*p == '-' || *p == '+')) | |
1285 | /* This is the sign of the exponent, not the end of the | |
1286 | number. */ | |
1287 | continue; | |
1288 | else if (!got_dot && !got_e && (*p=='l'||*p=='L')){ | |
1289 | ++p; break; | |
1290 | } | |
1291 | else if (!got_dot && !got_e && !hex && (*p=='u'||*p=='U')){ | |
1292 | ++p; break; | |
1293 | } | |
1294 | else if (*p < '0' || *p > '9' | |
1295 | && (!hex || ((*p < 'a' || *p > 'f') | |
1296 | && (*p < 'A' || *p > 'F')))) | |
1297 | break; | |
1298 | } | |
1299 | return parse_number (p - tokstart); | |
1300 | } | |
1301 | ||
1302 | if (!(c == '_' || c == '$' | |
1303 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) | |
1304 | /* We must have come across a bad character (e.g. ';'). */ | |
1305 | error ("Invalid character '%c' in expression.", c); | |
1306 | ||
1307 | /* It's a name. See how long it is. */ | |
1308 | namelen = 0; | |
1309 | for (c = tokstart[namelen]; | |
1310 | (c == '_' || c == '$' || (c >= '0' && c <= '9') | |
1311 | || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); | |
1312 | c = tokstart[++namelen]) | |
1313 | ; | |
1314 | ||
1315 | /* The token "if" terminates the expression and is NOT | |
1316 | removed from the input stream. */ | |
1317 | if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f') | |
1318 | { | |
1319 | return 0; | |
1320 | } | |
1321 | ||
1322 | lexptr += namelen; | |
1323 | ||
1324 | /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1) | |
1325 | and $$digits (equivalent to $<-digits> if you could type that). | |
1326 | Make token type LAST, and put the number (the digits) in yylval. */ | |
1327 | ||
1328 | if (*tokstart == '$') | |
1329 | { | |
1330 | register int negate = 0; | |
1331 | c = 1; | |
1332 | /* Double dollar means negate the number and add -1 as well. | |
1333 | Thus $$ alone means -1. */ | |
1334 | if (namelen >= 2 && tokstart[1] == '$') | |
1335 | { | |
1336 | negate = 1; | |
1337 | c = 2; | |
1338 | } | |
1339 | if (c == namelen) | |
1340 | { | |
1341 | /* Just dollars (one or two) */ | |
1342 | yylval.lval = - negate; | |
1343 | return LAST; | |
1344 | } | |
1345 | /* Is the rest of the token digits? */ | |
1346 | for (; c < namelen; c++) | |
1347 | if (!(tokstart[c] >= '0' && tokstart[c] <= '9')) | |
1348 | break; | |
1349 | if (c == namelen) | |
1350 | { | |
1351 | yylval.lval = atoi (tokstart + 1 + negate); | |
1352 | if (negate) | |
1353 | yylval.lval = - yylval.lval; | |
1354 | return LAST; | |
1355 | } | |
1356 | } | |
1357 | ||
1358 | /* Handle tokens that refer to machine registers: | |
1359 | $ followed by a register name. */ | |
1360 | ||
1361 | if (*tokstart == '$') { | |
1362 | for (c = 0; c < NUM_REGS; c++) | |
1363 | if (namelen - 1 == strlen (reg_names[c]) | |
1364 | && !strncmp (tokstart + 1, reg_names[c], namelen - 1)) | |
1365 | { | |
1366 | yylval.lval = c; | |
1367 | return REGNAME; | |
1368 | } | |
1369 | for (c = 0; c < NUM_STD_REGS; c++) | |
1370 | if (namelen - 1 == strlen (std_regs[c].name) | |
1371 | && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1)) | |
1372 | { | |
1373 | yylval.lval = std_regs[c].regnum; | |
1374 | return REGNAME; | |
1375 | } | |
1376 | } | |
1377 | /* Catch specific keywords. Should be done with a data structure. */ | |
1378 | switch (namelen) | |
1379 | { | |
1380 | case 8: | |
1381 | if (!strncmp (tokstart, "unsigned", 8)) | |
1382 | return UNSIGNED; | |
1383 | break; | |
1384 | case 6: | |
1385 | if (!strncmp (tokstart, "struct", 6)) | |
1386 | return STRUCT; | |
1387 | if (!strncmp (tokstart, "signed", 6)) | |
1388 | return SIGNED; | |
1389 | if (!strncmp (tokstart, "sizeof", 6)) | |
1390 | return SIZEOF; | |
1391 | break; | |
1392 | case 5: | |
1393 | if (!strncmp (tokstart, "union", 5)) | |
1394 | return UNION; | |
1395 | if (!strncmp (tokstart, "short", 5)) | |
1396 | return SHORT; | |
1397 | break; | |
1398 | case 4: | |
1399 | if (!strncmp (tokstart, "enum", 4)) | |
1400 | return ENUM; | |
1401 | if (!strncmp (tokstart, "long", 4)) | |
1402 | return LONG; | |
1403 | if (!strncmp (tokstart, "this", 4) | |
1404 | && lookup_symbol ("$this", expression_context_block, | |
1405 | VAR_NAMESPACE, 0)) | |
1406 | return THIS; | |
1407 | break; | |
1408 | case 3: | |
1409 | if (!strncmp (tokstart, "int", 3)) | |
1410 | return INT_KEYWORD; | |
1411 | break; | |
1412 | default: | |
1413 | break; | |
1414 | } | |
1415 | ||
1416 | yylval.sval.ptr = tokstart; | |
1417 | yylval.sval.length = namelen; | |
1418 | ||
1419 | /* Any other names starting in $ are debugger internal variables. */ | |
1420 | ||
1421 | if (*tokstart == '$') | |
1422 | { | |
1423 | yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1); | |
1424 | return VARIABLE; | |
1425 | } | |
1426 | ||
1427 | /* Use token-type BLOCKNAME for symbols that happen to be defined as | |
1428 | functions or symtabs. If this is not so, then ... | |
1429 | Use token-type TYPENAME for symbols that happen to be defined | |
1430 | currently as names of types; NAME for other symbols. | |
1431 | The caller is not constrained to care about the distinction. */ | |
1432 | { | |
1433 | char *tmp = copy_name (yylval.sval); | |
1434 | struct symbol *sym; | |
1435 | ||
1436 | if (lookup_partial_symtab (tmp)) | |
1437 | return BLOCKNAME; | |
1438 | sym = lookup_symbol (tmp, expression_context_block, | |
1439 | VAR_NAMESPACE, 0); | |
1440 | if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK) | |
1441 | return BLOCKNAME; | |
1442 | if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1)) | |
1443 | return TYPENAME; | |
1444 | return NAME; | |
1445 | } | |
1446 | } | |
1447 | ||
1448 | static void | |
1449 | yyerror () | |
1450 | { | |
1451 | error ("Invalid syntax in expression."); | |
1452 | } | |
1453 | ||
1454 | /* Return a null-terminated temporary copy of the name | |
1455 | of a string token. */ | |
1456 | ||
1457 | static char * | |
1458 | copy_name (token) | |
1459 | struct stoken token; | |
1460 | { | |
1461 | bcopy (token.ptr, namecopy, token.length); | |
1462 | namecopy[token.length] = 0; | |
1463 | return namecopy; | |
1464 | } | |
1465 | \f | |
1466 | /* Reverse an expression from suffix form (in which it is constructed) | |
1467 | to prefix form (in which we can conveniently print or execute it). */ | |
1468 | ||
1469 | static void prefixify_subexp (); | |
1470 | ||
1471 | static void | |
1472 | prefixify_expression (expr) | |
1473 | register struct expression *expr; | |
1474 | { | |
1475 | register int len = sizeof (struct expression) + | |
1476 | expr->nelts * sizeof (union exp_element); | |
1477 | register struct expression *temp; | |
1478 | register int inpos = expr->nelts, outpos = 0; | |
1479 | ||
1480 | temp = (struct expression *) alloca (len); | |
1481 | ||
1482 | /* Copy the original expression into temp. */ | |
1483 | bcopy (expr, temp, len); | |
1484 | ||
1485 | prefixify_subexp (temp, expr, inpos, outpos); | |
1486 | } | |
1487 | ||
1488 | /* Return the number of exp_elements in the subexpression of EXPR | |
1489 | whose last exp_element is at index ENDPOS - 1 in EXPR. */ | |
1490 | ||
1491 | static int | |
1492 | length_of_subexp (expr, endpos) | |
1493 | register struct expression *expr; | |
1494 | register int endpos; | |
1495 | { | |
1496 | register int oplen = 1; | |
1497 | register int args = 0; | |
1498 | register int i; | |
1499 | ||
1500 | if (endpos < 0) | |
1501 | error ("?error in length_of_subexp"); | |
1502 | ||
1503 | i = (int) expr->elts[endpos - 1].opcode; | |
1504 | ||
1505 | switch (i) | |
1506 | { | |
1507 | /* C++ */ | |
1508 | case OP_SCOPE: | |
1509 | oplen = 4 + ((expr->elts[endpos - 2].longconst | |
1510 | + sizeof (union exp_element)) | |
1511 | / sizeof (union exp_element)); | |
1512 | break; | |
1513 | ||
1514 | case OP_LONG: | |
1515 | case OP_DOUBLE: | |
1516 | oplen = 4; | |
1517 | break; | |
1518 | ||
1519 | case OP_VAR_VALUE: | |
1520 | case OP_LAST: | |
1521 | case OP_REGISTER: | |
1522 | case OP_INTERNALVAR: | |
1523 | oplen = 3; | |
1524 | break; | |
1525 | ||
1526 | case OP_FUNCALL: | |
1527 | oplen = 3; | |
1528 | args = 1 + expr->elts[endpos - 2].longconst; | |
1529 | break; | |
1530 | ||
1531 | case UNOP_CAST: | |
1532 | case UNOP_MEMVAL: | |
1533 | oplen = 3; | |
1534 | args = 1; | |
1535 | break; | |
1536 | ||
1537 | case STRUCTOP_STRUCT: | |
1538 | case STRUCTOP_PTR: | |
1539 | args = 1; | |
1540 | case OP_STRING: | |
1541 | oplen = 3 + ((expr->elts[endpos - 2].longconst | |
1542 | + sizeof (union exp_element)) | |
1543 | / sizeof (union exp_element)); | |
1544 | break; | |
1545 | ||
1546 | case TERNOP_COND: | |
1547 | args = 3; | |
1548 | break; | |
1549 | ||
1550 | case BINOP_ASSIGN_MODIFY: | |
1551 | oplen = 3; | |
1552 | args = 2; | |
1553 | break; | |
1554 | ||
1555 | /* C++ */ | |
1556 | case OP_THIS: | |
1557 | oplen = 2; | |
1558 | break; | |
1559 | ||
1560 | default: | |
1561 | args = 1 + (i < (int) BINOP_END); | |
1562 | } | |
1563 | ||
1564 | while (args > 0) | |
1565 | { | |
1566 | oplen += length_of_subexp (expr, endpos - oplen); | |
1567 | args--; | |
1568 | } | |
1569 | ||
1570 | return oplen; | |
1571 | } | |
1572 | ||
1573 | /* Copy the subexpression ending just before index INEND in INEXPR | |
1574 | into OUTEXPR, starting at index OUTBEG. | |
1575 | In the process, convert it from suffix to prefix form. */ | |
1576 | ||
1577 | static void | |
1578 | prefixify_subexp (inexpr, outexpr, inend, outbeg) | |
1579 | register struct expression *inexpr; | |
1580 | struct expression *outexpr; | |
1581 | register int inend; | |
1582 | int outbeg; | |
1583 | { | |
1584 | register int oplen = 1; | |
1585 | register int args = 0; | |
1586 | register int i; | |
1587 | int *arglens; | |
1588 | enum exp_opcode opcode; | |
1589 | ||
1590 | /* Compute how long the last operation is (in OPLEN), | |
1591 | and also how many preceding subexpressions serve as | |
1592 | arguments for it (in ARGS). */ | |
1593 | ||
1594 | opcode = inexpr->elts[inend - 1].opcode; | |
1595 | switch (opcode) | |
1596 | { | |
1597 | /* C++ */ | |
1598 | case OP_SCOPE: | |
1599 | oplen = 4 + ((inexpr->elts[inend - 2].longconst | |
1600 | + sizeof (union exp_element)) | |
1601 | / sizeof (union exp_element)); | |
1602 | break; | |
1603 | ||
1604 | case OP_LONG: | |
1605 | case OP_DOUBLE: | |
1606 | oplen = 4; | |
1607 | break; | |
1608 | ||
1609 | case OP_VAR_VALUE: | |
1610 | case OP_LAST: | |
1611 | case OP_REGISTER: | |
1612 | case OP_INTERNALVAR: | |
1613 | oplen = 3; | |
1614 | break; | |
1615 | ||
1616 | case OP_FUNCALL: | |
1617 | oplen = 3; | |
1618 | args = 1 + inexpr->elts[inend - 2].longconst; | |
1619 | break; | |
1620 | ||
1621 | case UNOP_CAST: | |
1622 | case UNOP_MEMVAL: | |
1623 | oplen = 3; | |
1624 | args = 1; | |
1625 | break; | |
1626 | ||
1627 | case STRUCTOP_STRUCT: | |
1628 | case STRUCTOP_PTR: | |
1629 | args = 1; | |
1630 | case OP_STRING: | |
1631 | oplen = 3 + ((inexpr->elts[inend - 2].longconst | |
1632 | + sizeof (union exp_element)) | |
1633 | / sizeof (union exp_element)); | |
1634 | ||
1635 | break; | |
1636 | ||
1637 | case TERNOP_COND: | |
1638 | args = 3; | |
1639 | break; | |
1640 | ||
1641 | case BINOP_ASSIGN_MODIFY: | |
1642 | oplen = 3; | |
1643 | args = 2; | |
1644 | break; | |
1645 | ||
1646 | /* C++ */ | |
1647 | case OP_THIS: | |
1648 | oplen = 2; | |
1649 | break; | |
1650 | ||
1651 | default: | |
1652 | args = 1 + ((int) opcode < (int) BINOP_END); | |
1653 | } | |
1654 | ||
1655 | /* Copy the final operator itself, from the end of the input | |
1656 | to the beginning of the output. */ | |
1657 | inend -= oplen; | |
1658 | bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg], | |
1659 | oplen * sizeof (union exp_element)); | |
1660 | outbeg += oplen; | |
1661 | ||
1662 | /* Find the lengths of the arg subexpressions. */ | |
1663 | arglens = (int *) alloca (args * sizeof (int)); | |
1664 | for (i = args - 1; i >= 0; i--) | |
1665 | { | |
1666 | oplen = length_of_subexp (inexpr, inend); | |
1667 | arglens[i] = oplen; | |
1668 | inend -= oplen; | |
1669 | } | |
1670 | ||
1671 | /* Now copy each subexpression, preserving the order of | |
1672 | the subexpressions, but prefixifying each one. | |
1673 | In this loop, inend starts at the beginning of | |
1674 | the expression this level is working on | |
1675 | and marches forward over the arguments. | |
1676 | outbeg does similarly in the output. */ | |
1677 | for (i = 0; i < args; i++) | |
1678 | { | |
1679 | oplen = arglens[i]; | |
1680 | inend += oplen; | |
1681 | prefixify_subexp (inexpr, outexpr, inend, outbeg); | |
1682 | outbeg += oplen; | |
1683 | } | |
1684 | } | |
1685 | \f | |
1686 | /* This page contains the two entry points to this file. */ | |
1687 | ||
1688 | /* Read a C expression from the string *STRINGPTR points to, | |
1689 | parse it, and return a pointer to a struct expression that we malloc. | |
1690 | Use block BLOCK as the lexical context for variable names; | |
1691 | if BLOCK is zero, use the block of the selected stack frame. | |
1692 | Meanwhile, advance *STRINGPTR to point after the expression, | |
1693 | at the first nonwhite character that is not part of the expression | |
1694 | (possibly a null character). | |
1695 | ||
1696 | If COMMA is nonzero, stop if a comma is reached. */ | |
1697 | ||
1698 | struct expression * | |
1699 | parse_c_1 (stringptr, block, comma) | |
1700 | char **stringptr; | |
1701 | struct block *block; | |
1702 | { | |
1703 | struct cleanup *old_chain; | |
1704 | ||
1705 | lexptr = *stringptr; | |
1706 | ||
1707 | paren_depth = 0; | |
1708 | type_stack_depth = 0; | |
1709 | ||
1710 | comma_terminates = comma; | |
1711 | ||
1712 | if (lexptr == 0 || *lexptr == 0) | |
1713 | error_no_arg ("expression to compute"); | |
1714 | ||
1715 | old_chain = make_cleanup (free_funcalls, 0); | |
1716 | funcall_chain = 0; | |
1717 | ||
1718 | expression_context_block = block ? block : get_selected_block (); | |
1719 | ||
1720 | namecopy = (char *) alloca (strlen (lexptr) + 1); | |
1721 | expout_size = 10; | |
1722 | expout_ptr = 0; | |
1723 | expout = (struct expression *) | |
1724 | xmalloc (sizeof (struct expression) | |
1725 | + expout_size * sizeof (union exp_element)); | |
1726 | make_cleanup (free_current_contents, &expout); | |
1727 | if (yyparse ()) | |
1728 | yyerror (); | |
1729 | discard_cleanups (old_chain); | |
1730 | expout->nelts = expout_ptr; | |
1731 | expout = (struct expression *) | |
1732 | xrealloc (expout, | |
1733 | sizeof (struct expression) | |
1734 | + expout_ptr * sizeof (union exp_element)); | |
1735 | prefixify_expression (expout); | |
1736 | *stringptr = lexptr; | |
1737 | return expout; | |
1738 | } | |
1739 | ||
1740 | /* Parse STRING as an expression, and complain if this fails | |
1741 | to use up all of the contents of STRING. */ | |
1742 | ||
1743 | struct expression * | |
1744 | parse_c_expression (string) | |
1745 | char *string; | |
1746 | { | |
1747 | register struct expression *exp; | |
1748 | exp = parse_c_1 (&string, 0, 0); | |
1749 | if (*string) | |
1750 | error ("Junk after end of expression."); | |
1751 | return exp; | |
1752 | } | |
1753 | ||
1754 | static void | |
1755 | push_type (tp) | |
1756 | enum type_pieces tp; | |
1757 | { | |
1758 | if (type_stack_depth == type_stack_size) | |
1759 | { | |
1760 | type_stack_size *= 2; | |
1761 | type_stack = (enum type_pieces *) | |
1762 | xrealloc (type_stack, type_stack_size * sizeof (enum type_pieces)); | |
1763 | } | |
1764 | type_stack[type_stack_depth++] = tp; | |
1765 | } | |
1766 | ||
1767 | static enum type_pieces | |
1768 | pop_type () | |
1769 | { | |
1770 | if (type_stack_depth) | |
1771 | return type_stack[--type_stack_depth]; | |
1772 | return tp_end; | |
1773 | } | |
1774 | ||
1775 | void | |
1776 | _initialize_expread () | |
1777 | { | |
1778 | type_stack_size = 80; | |
1779 | type_stack_depth = 0; | |
1780 | type_stack = (enum type_pieces *) | |
1781 | xmalloc (type_stack_size * sizeof (enum type_pieces)); | |
1782 | } |