expread.y 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /* Parse C expressions for GDB.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. /* Parse a C expression from text in a string,
  18. and return the result as a struct expression pointer.
  19. That structure contains arithmetic operations in reverse polish,
  20. with constants represented by operations that are followed by special data.
  21. See expression.h for the details of the format.
  22. What is important here is that it can be built up sequentially
  23. during the process of parsing; the lower levels of the tree always
  24. come first in the result. */
  25. %{
  26. #include "defs.h"
  27. #include "param.h"
  28. #include "symtab.h"
  29. #include "frame.h"
  30. #include "expression.h"
  31. #include <stdio.h>
  32. static struct expression *expout;
  33. static int expout_size;
  34. static int expout_ptr;
  35. static int yylex ();
  36. static yyerror ();
  37. static void write_exp_elt ();
  38. static void write_exp_string ();
  39. static void start_arglist ();
  40. static int end_arglist ();
  41. static void free_funcalls ();
  42. static char *copy_name ();
  43. /* If this is nonzero, this block is used as the lexical context
  44. for symbol names. */
  45. static struct block *expression_context_block;
  46. /* Number of arguments seen so far in innermost function call. */
  47. static int arglist_len;
  48. /* Data structure for saving values of arglist_len
  49. for function calls whose arguments contain other function calls. */
  50. struct funcall
  51. {
  52. struct funcall *next;
  53. int arglist_len;
  54. };
  55. struct funcall *funcall_chain;
  56. /* This kind of datum is used to represent the name
  57. of a symbol token. */
  58. struct stoken
  59. {
  60. char *ptr;
  61. int length;
  62. };
  63. %}
  64. /* Although the yacc "value" of an expression is not used,
  65. since the result is stored in the structure being created,
  66. other node types do have values. */
  67. %union
  68. {
  69. long lval;
  70. double dval;
  71. struct symbol *sym;
  72. struct type *tval;
  73. struct stoken sval;
  74. int voidval;
  75. struct block *bval;
  76. enum exp_opcode opcode;
  77. struct internalvar *ivar;
  78. }
  79. %type <voidval> exp exp1 start variable
  80. %type <tval> type typebase
  81. %type <bval> block
  82. %token <lval> INT CHAR
  83. %token <dval> FLOAT
  84. /* Both NAME and TYPENAME tokens represent symbols in the input,
  85. and both convey their data as strings.
  86. But a TYPENAME is a string that happens to be defined as a typedef
  87. or builtin type name (such as int or char)
  88. and a NAME is any other symbol.
  89. Contexts where this distinction is not important can use the
  90. nonterminal "name", which matches either NAME or TYPENAME. */
  91. %token <sval> NAME TYPENAME STRING
  92. %type <sval> name
  93. %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
  94. %token <lval> LAST REGNAME
  95. %token <ivar> VARIABLE
  96. %token <opcode> ASSIGN_MODIFY
  97. %left ','
  98. %left ABOVE_COMMA
  99. %right '=' ASSIGN_MODIFY
  100. %left OR
  101. %left AND
  102. %left '|'
  103. %left '^'
  104. %left '&'
  105. %left EQUAL NOTEQUAL
  106. %left '<' '>' LEQ GEQ
  107. %left LSH RSH
  108. %left '+' '-'
  109. %left '*' '/' '%'
  110. %left '@'
  111. %right UNARY INCREMENT DECREMENT
  112. %right ARROW '.' '['
  113. %left COLONCOLON
  114. %%
  115. start : exp1
  116. ;
  117. /* Expressions, including the comma operator. */
  118. exp1 : exp
  119. | exp1 ',' exp
  120. { write_exp_elt (BINOP_COMMA); }
  121. ;
  122. /* Expressions, not including the comma operator. */
  123. exp : '*' exp %prec UNARY
  124. { write_exp_elt (UNOP_IND); }
  125. exp : '&' exp %prec UNARY
  126. { write_exp_elt (UNOP_ADDR); }
  127. exp : '-' exp %prec UNARY
  128. { write_exp_elt (UNOP_NEG); }
  129. ;
  130. exp : '!' exp %prec UNARY
  131. { write_exp_elt (UNOP_ZEROP); }
  132. ;
  133. exp : '~' exp %prec UNARY
  134. { write_exp_elt (UNOP_LOGNOT); }
  135. ;
  136. exp : INCREMENT exp %prec UNARY
  137. { write_exp_elt (UNOP_PREINCREMENT); }
  138. ;
  139. exp : DECREMENT exp %prec UNARY
  140. { write_exp_elt (UNOP_PREDECREMENT); }
  141. ;
  142. exp : exp INCREMENT %prec UNARY
  143. { write_exp_elt (UNOP_POSTINCREMENT); }
  144. ;
  145. exp : exp DECREMENT %prec UNARY
  146. { write_exp_elt (UNOP_POSTDECREMENT); }
  147. ;
  148. exp : SIZEOF exp %prec UNARY
  149. { write_exp_elt (UNOP_SIZEOF); }
  150. ;
  151. exp : exp ARROW name
  152. { write_exp_elt (STRUCTOP_PTR);
  153. write_exp_string ($3);
  154. write_exp_elt (STRUCTOP_PTR); }
  155. ;
  156. exp : exp '.' name
  157. { write_exp_elt (STRUCTOP_STRUCT);
  158. write_exp_string ($3);
  159. write_exp_elt (STRUCTOP_STRUCT); }
  160. ;
  161. exp : exp '[' exp1 ']'
  162. { write_exp_elt (BINOP_SUBSCRIPT); }
  163. ;
  164. exp : exp '('
  165. /* This is to save the value of arglist_len
  166. being accumulated by an outer function call. */
  167. { start_arglist (); }
  168. arglist ')'
  169. { write_exp_elt (OP_FUNCALL);
  170. write_exp_elt (end_arglist ());
  171. write_exp_elt (OP_FUNCALL); }
  172. ;
  173. arglist :
  174. ;
  175. arglist : exp
  176. { arglist_len = 1; }
  177. ;
  178. arglist : arglist ',' exp %prec ABOVE_COMMA
  179. { arglist_len++; }
  180. ;
  181. exp : '{' type '}' exp %prec UNARY
  182. { write_exp_elt (UNOP_MEMVAL);
  183. write_exp_elt ($2);
  184. write_exp_elt (UNOP_MEMVAL); }
  185. ;
  186. exp : '(' type ')' exp %prec UNARY
  187. { write_exp_elt (UNOP_CAST);
  188. write_exp_elt ($2);
  189. write_exp_elt (UNOP_CAST); }
  190. ;
  191. exp : '(' exp1 ')'
  192. { }
  193. ;
  194. /* Binary operators in order of decreasing precedence. */
  195. exp : exp '@' exp
  196. { write_exp_elt (BINOP_REPEAT); }
  197. ;
  198. exp : exp '*' exp
  199. { write_exp_elt (BINOP_MUL); }
  200. ;
  201. exp : exp '/' exp
  202. { write_exp_elt (BINOP_DIV); }
  203. ;
  204. exp : exp '%' exp
  205. { write_exp_elt (BINOP_REM); }
  206. ;
  207. exp : exp '+' exp
  208. { write_exp_elt (BINOP_ADD); }
  209. ;
  210. exp : exp '-' exp
  211. { write_exp_elt (BINOP_SUB); }
  212. ;
  213. exp : exp LSH exp
  214. { write_exp_elt (BINOP_LSH); }
  215. ;
  216. exp : exp RSH exp
  217. { write_exp_elt (BINOP_RSH); }
  218. ;
  219. exp : exp EQUAL exp
  220. { write_exp_elt (BINOP_EQUAL); }
  221. ;
  222. exp : exp NOTEQUAL exp
  223. { write_exp_elt (BINOP_NOTEQUAL); }
  224. ;
  225. exp : exp LEQ exp
  226. { write_exp_elt (BINOP_LEQ); }
  227. ;
  228. exp : exp GEQ exp
  229. { write_exp_elt (BINOP_GEQ); }
  230. ;
  231. exp : exp '<' exp
  232. { write_exp_elt (BINOP_LESS); }
  233. ;
  234. exp : exp '>' exp
  235. { write_exp_elt (BINOP_GTR); }
  236. ;
  237. exp : exp '&' exp
  238. { write_exp_elt (BINOP_LOGAND); }
  239. ;
  240. exp : exp '^' exp
  241. { write_exp_elt (BINOP_LOGXOR); }
  242. ;
  243. exp : exp '|' exp
  244. { write_exp_elt (BINOP_LOGIOR); }
  245. ;
  246. exp : exp AND exp
  247. { write_exp_elt (BINOP_AND); }
  248. ;
  249. exp : exp OR exp
  250. { write_exp_elt (BINOP_OR); }
  251. ;
  252. exp : exp '?' exp ':' exp
  253. { write_exp_elt (TERNOP_COND); }
  254. ;
  255. exp : exp '=' exp
  256. { write_exp_elt (BINOP_ASSIGN); }
  257. ;
  258. exp : exp ASSIGN_MODIFY exp
  259. { write_exp_elt (BINOP_ASSIGN_MODIFY);
  260. write_exp_elt ($2);
  261. write_exp_elt (BINOP_ASSIGN_MODIFY); }
  262. ;
  263. exp : INT
  264. { write_exp_elt (OP_LONG);
  265. write_exp_elt (builtin_type_long);
  266. write_exp_elt ($1);
  267. write_exp_elt (OP_LONG); }
  268. ;
  269. exp : CHAR
  270. { write_exp_elt (OP_LONG);
  271. write_exp_elt (builtin_type_char);
  272. write_exp_elt ($1);
  273. write_exp_elt (OP_LONG); }
  274. ;
  275. exp : FLOAT
  276. { write_exp_elt (OP_DOUBLE);
  277. write_exp_elt (builtin_type_double);
  278. write_exp_elt ($1);
  279. write_exp_elt (OP_DOUBLE); }
  280. ;
  281. exp : variable
  282. ;
  283. exp : LAST
  284. { write_exp_elt (OP_LAST);
  285. write_exp_elt ($1);
  286. write_exp_elt (OP_LAST); }
  287. ;
  288. exp : REGNAME
  289. { write_exp_elt (OP_REGISTER);
  290. write_exp_elt ($1);
  291. write_exp_elt (OP_REGISTER); }
  292. ;
  293. exp : VARIABLE
  294. { write_exp_elt (OP_INTERNALVAR);
  295. write_exp_elt ($1);
  296. write_exp_elt (OP_INTERNALVAR); }
  297. ;
  298. exp : SIZEOF '(' type ')'
  299. { write_exp_elt (OP_LONG);
  300. write_exp_elt (builtin_type_int);
  301. write_exp_elt ((long) TYPE_LENGTH ($3));
  302. write_exp_elt (OP_LONG); }
  303. ;
  304. exp : STRING
  305. { write_exp_elt (OP_STRING);
  306. write_exp_string ($1);
  307. write_exp_elt (OP_STRING); }
  308. ;
  309. block : name
  310. { struct symtab *tem = lookup_symtab (copy_name ($1));
  311. struct symbol *sym;
  312. if (tem)
  313. $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), 1);
  314. else
  315. {
  316. sym = lookup_symbol (copy_name ($1),
  317. expression_context_block,
  318. VAR_NAMESPACE);
  319. if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  320. $$ = SYMBOL_BLOCK_VALUE (sym);
  321. else
  322. error ("No file or function \"%s\".",
  323. copy_name ($1));
  324. }}
  325. ;
  326. block : block COLONCOLON name
  327. { struct symbol *tem
  328. = lookup_symbol ($3, copy_name ($1), VAR_NAMESPACE);
  329. if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
  330. error ("No function \"%s\" in specified context.",
  331. copy_name ($1));
  332. $$ = SYMBOL_BLOCK_VALUE (tem); }
  333. ;
  334. variable: block COLONCOLON name
  335. { struct symbol *sym;
  336. sym = lookup_symbol ($3, copy_name ($1), VAR_NAMESPACE);
  337. if (sym == 0)
  338. error ("No symbol \"%s\" in specified context.",
  339. copy_name ($3));
  340. write_exp_elt (OP_VAR_VALUE);
  341. write_exp_elt (sym);
  342. write_exp_elt (OP_VAR_VALUE); }
  343. ;
  344. variable: NAME
  345. { struct symbol *sym;
  346. sym = lookup_symbol (copy_name ($1),
  347. expression_context_block,
  348. VAR_NAMESPACE);
  349. if (sym)
  350. {
  351. write_exp_elt (OP_VAR_VALUE);
  352. write_exp_elt (sym);
  353. write_exp_elt (OP_VAR_VALUE);
  354. }
  355. else
  356. {
  357. register char *arg = copy_name ($1);
  358. register int i;
  359. for (i = 0; i < misc_function_count; i++)
  360. if (!strcmp (misc_function_vector[i].name, arg))
  361. break;
  362. if (i < misc_function_count)
  363. {
  364. write_exp_elt (OP_LONG);
  365. write_exp_elt (builtin_type_int);
  366. write_exp_elt (misc_function_vector[i].address);
  367. write_exp_elt (OP_LONG);
  368. write_exp_elt (UNOP_MEMVAL);
  369. write_exp_elt (builtin_type_char);
  370. write_exp_elt (UNOP_MEMVAL);
  371. }
  372. else
  373. if (symtab_list == 0)
  374. error ("No symbol table is loaded. Use the \"symbol-file\" command.");
  375. else
  376. error ("No symbol \"%s\" in current context.",
  377. copy_name ($1));
  378. }
  379. }
  380. ;
  381. type : typebase
  382. | type '*'
  383. { $$ = lookup_pointer_type ($1); }
  384. ;
  385. typebase
  386. : TYPENAME
  387. { $$ = lookup_typename (copy_name ($1),
  388. expression_context_block, 0); }
  389. | STRUCT name
  390. { $$ = lookup_struct (copy_name ($2),
  391. expression_context_block); }
  392. | UNION name
  393. { $$ = lookup_union (copy_name ($2),
  394. expression_context_block); }
  395. | ENUM name
  396. { $$ = lookup_enum (copy_name ($2),
  397. expression_context_block); }
  398. | UNSIGNED name
  399. { $$ = lookup_unsigned_typename (copy_name ($2)); }
  400. ;
  401. name : NAME
  402. | TYPENAME
  403. ;
  404. %%
  405. /* Begin counting arguments for a function call,
  406. saving the data about any containing call. */
  407. static void
  408. start_arglist ()
  409. {
  410. register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
  411. new->next = funcall_chain;
  412. new->arglist_len = arglist_len;
  413. arglist_len = 0;
  414. funcall_chain = new;
  415. }
  416. /* Return the number of arguments in a function call just terminated,
  417. and restore the data for the containing function call. */
  418. static int
  419. end_arglist ()
  420. {
  421. register int val = arglist_len;
  422. register struct funcall *call = funcall_chain;
  423. funcall_chain = call->next;
  424. arglist_len = call->arglist_len;
  425. free (call);
  426. return val;
  427. }
  428. /* Free everything in the funcall chain.
  429. Used when there is an error inside parsing. */
  430. static void
  431. free_funcalls ()
  432. {
  433. register struct funcall *call, *next;
  434. for (call = funcall_chain; call; call = next)
  435. {
  436. next = call->next;
  437. free (call);
  438. }
  439. }
  440. /* This page contains the functions for adding data to the struct expression
  441. being constructed. */
  442. /* Add one element to the end of the expression. */
  443. static void
  444. write_exp_elt (expelt)
  445. union exp_element expelt;
  446. {
  447. if (expout_ptr >= expout_size)
  448. {
  449. expout_size *= 2;
  450. expout = (struct expression *) xrealloc (expout,
  451. sizeof (struct expression)
  452. + expout_size * sizeof (union exp_element));
  453. }
  454. expout->elts[expout_ptr++] = expelt;
  455. }
  456. /* Add a string constant to the end of the expression.
  457. Follow it by its length in bytes, as a separate exp_element. */
  458. static void
  459. write_exp_string (str)
  460. struct stoken str;
  461. {
  462. register int len = str.length;
  463. register int lenelt
  464. = (len + sizeof (union exp_element)) / sizeof (union exp_element);
  465. expout_ptr += lenelt;
  466. if (expout_ptr >= expout_size)
  467. {
  468. expout_size = max (expout_size * 2, expout_ptr + 10);
  469. expout = (struct expression *) xrealloc (expout,
  470. sizeof (struct expression)
  471. + expout_size * sizeof (union exp_element));
  472. }
  473. bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
  474. ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
  475. write_exp_elt (len);
  476. }
  477. /* During parsing of a C expression, the pointer to the next character
  478. is in this variable. */
  479. static char *lexptr;
  480. /* Tokens that refer to names do so with explicit pointer and length,
  481. so they can share the storage that lexptr is parsing.
  482. When it is necessary to pass a name to a function that expects
  483. a null-terminated string, the substring is copied out
  484. into a block of storage that namecopy points to.
  485. namecopy is allocated once, guaranteed big enough, for each parsing. */
  486. static char *namecopy;
  487. /* Take care of parsing a number (anything that starts with a digit).
  488. Set yylval and return the token type; update lexptr.
  489. LEN is the number of characters in it. */
  490. /*** Needs some error checking for the float case ***/
  491. static int
  492. parse_number (olen)
  493. int olen;
  494. {
  495. register char *p = lexptr;
  496. register long n = 0;
  497. register int c;
  498. register int base = 10;
  499. register len = olen;
  500. char *err_copy;
  501. extern double atof ();
  502. for (c = 0; c < len; c++)
  503. if (p[c] == '.')
  504. {
  505. /* It's a float since it contains a point. */
  506. yylval.dval = atof (p);
  507. lexptr += len;
  508. return FLOAT;
  509. }
  510. if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2)))
  511. {
  512. p += 2;
  513. base = 16;
  514. len -= 2;
  515. }
  516. else if (*p == '0')
  517. base = 8;
  518. while (len-- > 0)
  519. {
  520. c = *p++;
  521. n *= base;
  522. if (c >= '0' && c <= '9')
  523. n += c - '0';
  524. else
  525. {
  526. if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
  527. if (base == 16 && c >= 'a' && c <= 'f')
  528. n += c - 'a' + 10;
  529. else if (len == 0 && c == 'l')
  530. ;
  531. else
  532. {
  533. err_copy = (char *) alloca (olen + 1);
  534. bcopy (lexptr, err_copy, olen);
  535. err_copy[olen] = 0;
  536. error ("Invalid number \"%s\".", err_copy);
  537. }
  538. }
  539. }
  540. lexptr = p;
  541. yylval.lval = n;
  542. return INT;
  543. }
  544. struct token
  545. {
  546. char *operator;
  547. int token;
  548. enum exp_opcode opcode;
  549. };
  550. static struct token tokentab3[] =
  551. {
  552. {">>=", ASSIGN_MODIFY, BINOP_RSH},
  553. {"<<=", ASSIGN_MODIFY, BINOP_LSH}
  554. };
  555. static struct token tokentab2[] =
  556. {
  557. {"+=", ASSIGN_MODIFY, BINOP_ADD},
  558. {"-=", ASSIGN_MODIFY, BINOP_SUB},
  559. {"*=", ASSIGN_MODIFY, BINOP_MUL},
  560. {"/=", ASSIGN_MODIFY, BINOP_DIV},
  561. {"%=", ASSIGN_MODIFY, BINOP_REM},
  562. {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
  563. {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
  564. {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
  565. {"++", INCREMENT, BINOP_END},
  566. {"--", DECREMENT, BINOP_END},
  567. {"->", ARROW, BINOP_END},
  568. {"&&", AND, BINOP_END},
  569. {"||", OR, BINOP_END},
  570. {"::", COLONCOLON, BINOP_END},
  571. {"<<", LSH, BINOP_END},
  572. {">>", RSH, BINOP_END},
  573. {"==", EQUAL, BINOP_END},
  574. {"!=", NOTEQUAL, BINOP_END},
  575. {"<=", LEQ, BINOP_END},
  576. {">=", GEQ, BINOP_END}
  577. };
  578. /* Read one token, getting characters through lexptr. */
  579. static int
  580. yylex ()
  581. {
  582. register int c;
  583. register int namelen;
  584. register int i;
  585. register char *tokstart;
  586. retry:
  587. tokstart = lexptr;
  588. /* See if it is a special token of length 3. */
  589. for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
  590. if (!strncmp (tokstart, tokentab3[i].operator, 3))
  591. {
  592. lexptr += 3;
  593. yylval.opcode = tokentab3[i].opcode;
  594. return tokentab3[i].token;
  595. }
  596. /* See if it is a special token of length 2. */
  597. for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
  598. if (!strncmp (tokstart, tokentab2[i].operator, 2))
  599. {
  600. lexptr += 2;
  601. yylval.opcode = tokentab2[i].opcode;
  602. return tokentab2[i].token;
  603. }
  604. switch (c = *tokstart)
  605. {
  606. case 0:
  607. return 0;
  608. case ' ':
  609. case '\t':
  610. case '\n':
  611. lexptr++;
  612. goto retry;
  613. case '\'':
  614. lexptr++;
  615. c = *lexptr++;
  616. if (c == '\\')
  617. c = parse_escape (&lexptr);
  618. yylval.lval = c;
  619. c = *lexptr++;
  620. if (c != '\'')
  621. error ("Invalid character constant.");
  622. return CHAR;
  623. case '+':
  624. case '-':
  625. case '*':
  626. case '/':
  627. case '%':
  628. case '|':
  629. case '&':
  630. case '^':
  631. case '~':
  632. case '!':
  633. case '@':
  634. case '<':
  635. case '>':
  636. case '(':
  637. case ')':
  638. case '[':
  639. case ']':
  640. case '.':
  641. case '?':
  642. case ':':
  643. case '=':
  644. case '{':
  645. case '}':
  646. case ',':
  647. lexptr++;
  648. return c;
  649. case '"':
  650. for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
  651. if (c == '\\')
  652. {
  653. c = tokstart[++namelen];
  654. if (c >= '0' && c <= '9')
  655. {
  656. c = tokstart[++namelen];
  657. if (c >= '0' && c <= '9')
  658. c = tokstart[++namelen];
  659. }
  660. }
  661. yylval.sval.ptr = tokstart + 1;
  662. yylval.sval.length = namelen - 1;
  663. lexptr += namelen + 1;
  664. return STRING;
  665. }
  666. if (c >= '0' && c <= '9')
  667. {
  668. /* It's a number */
  669. for (namelen = 0;
  670. c = tokstart[namelen],
  671. (c == '_' || c == '$' || c == '.' || (c >= '0' && c <= '9')
  672. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  673. namelen++)
  674. ;
  675. return parse_number (namelen);
  676. }
  677. if (!(c == '_' || c == '$'
  678. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  679. error ("Invalid token in expression.");
  680. /* It is a name. See how long it is. */
  681. for (namelen = 0;
  682. c = tokstart[namelen],
  683. (c == '_' || c == '$' || (c >= '0' && c <= '9')
  684. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  685. namelen++)
  686. ;
  687. /* The token "if" terminates the expression and is NOT
  688. removed from the input stream. */
  689. if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  690. {
  691. return 0;
  692. }
  693. lexptr += namelen;
  694. /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
  695. and $$digits (equivalent to $<-digits> if you could type that).
  696. Make token type LAST, and put the number (the digits) in yylval. */
  697. if (*tokstart == '$')
  698. {
  699. register int negate = 0;
  700. c = 1;
  701. /* Double dollar means negate the number and add -1 as well.
  702. Thus $$ alone means -1. */
  703. if (namelen >= 2 && tokstart[1] == '$')
  704. {
  705. negate = 1;
  706. c = 2;
  707. }
  708. if (c == namelen)
  709. {
  710. /* Just dollars (one or two) */
  711. yylval.lval = - negate;
  712. return LAST;
  713. }
  714. /* Is the rest of the token digits? */
  715. for (; c < namelen; c++)
  716. if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
  717. break;
  718. if (c == namelen)
  719. {
  720. yylval.lval = atoi (tokstart + 1 + negate);
  721. if (negate)
  722. yylval.lval = - yylval.lval;
  723. return LAST;
  724. }
  725. }
  726. /* Handle tokens that refer to machine registers:
  727. $ followed by a register name. */
  728. if (*tokstart == '$')
  729. for (c = 0; c < NUM_REGS; c++)
  730. if (namelen - 1 == strlen (reg_names[c])
  731. && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
  732. {
  733. yylval.lval = c;
  734. return REGNAME;
  735. }
  736. if (namelen == 6 && !strncmp (tokstart, "struct", 6))
  737. {
  738. return STRUCT;
  739. }
  740. if (namelen == 5 && !strncmp (tokstart, "union", 5))
  741. {
  742. return UNION;
  743. }
  744. if (namelen == 4 && !strncmp (tokstart, "enum", 4))
  745. {
  746. return ENUM;
  747. }
  748. if (namelen == 6 && !strncmp (tokstart, "sizeof", 6))
  749. {
  750. return SIZEOF;
  751. }
  752. if (namelen == 8 && !strncmp (tokstart, "unsigned", 6))
  753. {
  754. return UNSIGNED;
  755. }
  756. yylval.sval.ptr = tokstart;
  757. yylval.sval.length = namelen;
  758. /* Any other names starting in $ are debugger internal variables. */
  759. if (*tokstart == '$')
  760. {
  761. yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1);
  762. return VARIABLE;
  763. }
  764. /* Use token-type TYPENAME for symbols that happen to be defined
  765. currently as names of types; NAME for other symbols.
  766. The caller is not constrained to care about the distinction. */
  767. if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
  768. return TYPENAME;
  769. return NAME;
  770. }
  771. static
  772. yyerror ()
  773. {
  774. error ("Invalid syntax in expression.");
  775. }
  776. /* Return a null-terminated temporary copy of the name
  777. of a string token. */
  778. static char *
  779. copy_name (token)
  780. struct stoken token;
  781. {
  782. bcopy (token.ptr, namecopy, token.length);
  783. namecopy[token.length] = 0;
  784. return namecopy;
  785. }
  786. /* Reverse an expression from suffix form (in which it is constructed)
  787. to prefix form (in which we can conveniently print or execute it). */
  788. static void prefixify_subexp ();
  789. static void
  790. prefixify_expression (expr)
  791. register struct expression *expr;
  792. {
  793. register int len = sizeof (struct expression) +
  794. expr->nelts * sizeof (union exp_element);
  795. register struct expression *temp
  796. = (struct expression *) alloca (len);
  797. register int inpos = expr->nelts, outpos = 0;
  798. /* Copy the original expression into temp. */
  799. bcopy (expr, temp, len);
  800. prefixify_subexp (temp, expr, inpos, outpos);
  801. }
  802. /* Return the number of exp_elements in the subexpression of EXPR
  803. whose last exp_element is at index ENDPOS - 1 in EXPR. */
  804. static int
  805. length_of_subexp (expr, endpos)
  806. register struct expression *expr;
  807. register int endpos;
  808. {
  809. register int oplen = 1;
  810. register int args = 0;
  811. register int i;
  812. i = (int) expr->elts[endpos - 1].opcode;
  813. switch (i)
  814. {
  815. case OP_LONG:
  816. case OP_DOUBLE:
  817. oplen = 4;
  818. break;
  819. case OP_VAR_VALUE:
  820. case OP_LAST:
  821. case OP_REGISTER:
  822. case OP_INTERNALVAR:
  823. oplen = 3;
  824. break;
  825. case OP_FUNCALL:
  826. oplen = 3;
  827. args = 1 + expr->elts[endpos - 2].longconst;
  828. break;
  829. case UNOP_CAST:
  830. case UNOP_MEMVAL:
  831. oplen = 3;
  832. args = 1;
  833. break;
  834. case STRUCTOP_STRUCT:
  835. case STRUCTOP_PTR:
  836. args = 1;
  837. case OP_STRING:
  838. oplen = 3 + ((expr->elts[endpos - 2].longconst
  839. + sizeof (union exp_element))
  840. / sizeof (union exp_element));
  841. break;
  842. case TERNOP_COND:
  843. args = 3;
  844. break;
  845. case BINOP_ASSIGN_MODIFY:
  846. oplen = 3;
  847. args = 2;
  848. break;
  849. default:
  850. args = 1 + (i < (int) BINOP_END);
  851. }
  852. while (args > 0)
  853. {
  854. oplen += length_of_subexp (expr, endpos - oplen);
  855. args--;
  856. }
  857. return oplen;
  858. }
  859. /* Copy the subexpression ending just before index INEND in INEXPR
  860. into OUTEXPR, starting at index OUTBEG.
  861. In the process, convert it from suffix to prefix form. */
  862. static void
  863. prefixify_subexp (inexpr, outexpr, inend, outbeg)
  864. register struct expression *inexpr;
  865. struct expression *outexpr;
  866. register int inend;
  867. int outbeg;
  868. {
  869. register int oplen = 1;
  870. register int args = 0;
  871. register int i;
  872. int *arglens;
  873. enum exp_opcode opcode;
  874. /* Compute how long the last operation is (in OPLEN),
  875. and also how many preceding subexpressions serve as
  876. arguments for it (in ARGS). */
  877. opcode = inexpr->elts[inend - 1].opcode;
  878. switch (opcode)
  879. {
  880. case OP_LONG:
  881. case OP_DOUBLE:
  882. oplen = 4;
  883. break;
  884. case OP_VAR_VALUE:
  885. case OP_LAST:
  886. case OP_REGISTER:
  887. case OP_INTERNALVAR:
  888. oplen = 3;
  889. break;
  890. case OP_FUNCALL:
  891. oplen = 3;
  892. args = 1 + inexpr->elts[inend - 2].longconst;
  893. break;
  894. case UNOP_CAST:
  895. case UNOP_MEMVAL:
  896. oplen = 3;
  897. args = 1;
  898. break;
  899. case STRUCTOP_STRUCT:
  900. case STRUCTOP_PTR:
  901. args = 1;
  902. case OP_STRING:
  903. oplen = 3 + ((inexpr->elts[inend - 2].longconst
  904. + sizeof (union exp_element))
  905. / sizeof (union exp_element));
  906. break;
  907. case TERNOP_COND:
  908. args = 3;
  909. break;
  910. case BINOP_ASSIGN_MODIFY:
  911. oplen = 3;
  912. args = 2;
  913. break;
  914. default:
  915. args = 1 + ((int) opcode < (int) BINOP_END);
  916. }
  917. /* Copy the final operator itself, from the end of the input
  918. to the beginning of the output. */
  919. inend -= oplen;
  920. bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
  921. oplen * sizeof (union exp_element));
  922. outbeg += oplen;
  923. /* Find the lengths of the arg subexpressions. */
  924. arglens = (int *) alloca (args * sizeof (int));
  925. for (i = args - 1; i >= 0; i--)
  926. {
  927. oplen = length_of_subexp (inexpr, inend);
  928. arglens[i] = oplen;
  929. inend -= oplen;
  930. }
  931. /* Now copy each subexpression, preserving the order of
  932. the subexpressions, but prefixifying each one.
  933. In this loop, inend starts at the beginning of
  934. the expression this level is working on
  935. and marches forward over the arguments.
  936. outbeg does similarly in the output. */
  937. for (i = 0; i < args; i++)
  938. {
  939. oplen = arglens[i];
  940. inend += oplen;
  941. prefixify_subexp (inexpr, outexpr, inend, outbeg);
  942. outbeg += oplen;
  943. }
  944. }
  945. /* This page contains the two entry points to this file. */
  946. /* Read a C expression from the string *STRINGPTR points to,
  947. parse it, and return a pointer to a struct expression that we malloc.
  948. Use block BLOCK as the lexical context for variable names;
  949. if BLOCK is zero, use the block of the selected stack frame.
  950. Meanwhile, advance *STRINGPTR to point after the expression,
  951. at the first nonwhite character that is not part of the expression
  952. (possibly a null character). */
  953. struct expression *
  954. parse_c_1 (stringptr, block)
  955. char **stringptr;
  956. struct block *block;
  957. {
  958. struct cleanup *old_chain;
  959. lexptr = *stringptr;
  960. if (lexptr == 0 || *lexptr == 0)
  961. error_no_arg ("expression to compute");
  962. old_chain = make_cleanup (free_funcalls, 0);
  963. funcall_chain = 0;
  964. expression_context_block = block ? block : get_selected_block ();
  965. namecopy = (char *) alloca (strlen (lexptr) + 1);
  966. expout_size = 10;
  967. expout_ptr = 0;
  968. expout = (struct expression *) xmalloc (sizeof (struct expression)
  969. + expout_size * sizeof (union exp_element));
  970. make_cleanup (free_current_contents, &expout);
  971. if (yyparse ())
  972. yyerror ();
  973. discard_cleanups (old_chain);
  974. expout->nelts = expout_ptr;
  975. expout = (struct expression *)
  976. xrealloc (expout,
  977. sizeof (struct expression)
  978. + expout_ptr * sizeof (union exp_element));
  979. prefixify_expression (expout);
  980. *stringptr = lexptr;
  981. return expout;
  982. }
  983. /* Parse STRING as an expression, and complain if this fails
  984. to use up all of the contents of STRING. */
  985. struct expression *
  986. parse_c_expression (string)
  987. char *string;
  988. {
  989. register struct expression *exp;
  990. exp = parse_c_1 (&string, 0);
  991. if (*string)
  992. error ("Junk after end of expression.");
  993. return exp;
  994. }