tree.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. /* Language-indepednent node constructors for parse phase of GNU compiler.
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. /* This file contains the low level primitives for operating on tree nodes,
  18. including allocation, list operations, interning of identifiers,
  19. construction of data type nodes and statement nodes,
  20. and construction of type conversion nodes. It also contains
  21. tables index by tree code that describe how to take apart
  22. nodes of that code.
  23. It is intended to be language-independent, but occasionally
  24. calls language-dependent routines defined (for C) in typecheck.c.
  25. The low-level allocation routines oballoc and permalloc
  26. are used also for allocating many other kinds of objects
  27. by all passes of the compiler. */
  28. #include "config.h"
  29. #include <stdio.h>
  30. #include "tree.h"
  31. #include "obstack.h"
  32. #define obstack_chunk_alloc xmalloc
  33. #define obstack_chunk_free free
  34. extern int xmalloc ();
  35. extern void free ();
  36. /* Tree nodes of permanent duration are allocated in this obstack.
  37. They are the identifier nodes, and everything outside of
  38. the bodies and parameters of function definitions. */
  39. struct obstack permanent_obstack;
  40. /* The contents of the current function definition are allocated
  41. in this obstack, and all are freed at the end of the function. */
  42. struct obstack temporary_obstack;
  43. /* This points at either permanent_obstack or temporary_obstack. */
  44. struct obstack *current_obstack;
  45. /* Table indexed by tree code giving a string containing a character
  46. classifying the tree code. Possibilities are
  47. t, d, s, c, r and e. See tree.def for details. */
  48. #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
  49. char *tree_code_type[] = {
  50. #include "tree.def"
  51. };
  52. #undef DEFTREECODE
  53. /* Table indexed by tree code giving number of expression
  54. operands beyond the fixed part of the node structure.
  55. Not used for types or decls. */
  56. #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
  57. int tree_code_length[] = {
  58. #include "tree.def"
  59. };
  60. #undef DEFTREECODE
  61. /* Counter for assigning unique ids to all tree nodes. */
  62. int tree_node_counter = 0;
  63. /* Hash table for uniquizing IDENTIFIER_NODEs by name. */
  64. #define MAX_HASH_TABLE 1008
  65. static tree hash_table[MAX_HASH_TABLE]; /* id hash buckets */
  66. /* Init data for node creation, at the beginning of compilation. */
  67. void
  68. init_tree ()
  69. {
  70. obstack_init (&permanent_obstack);
  71. current_obstack = &permanent_obstack;
  72. tree_node_counter = 1;
  73. bzero (hash_table, sizeof hash_table);
  74. }
  75. /* Start allocating on the temporary (per function) obstack.
  76. This is done in start_function before parsing the function body. */
  77. temporary_allocation ()
  78. {
  79. /* Set up the obstack: */
  80. obstack_init (&temporary_obstack);
  81. current_obstack = &temporary_obstack;
  82. }
  83. /* Go back to allocating on the permanent obstack
  84. and free everything in the temporary obstack.
  85. This is done in finish_function after fully compiling a function. */
  86. permanent_allocation ()
  87. {
  88. /* Free up previous temporary obstack data */
  89. obstack_free (&temporary_obstack, NULL);
  90. current_obstack = &permanent_obstack;
  91. }
  92. /* Allocate SIZE bytes in the current obstack
  93. and return a pointer to them.
  94. In practice the current obstack is always the temporary one. */
  95. char *
  96. oballoc (size)
  97. int size;
  98. {
  99. return (char *) obstack_alloc (current_obstack, size);
  100. }
  101. /* Free the object PTR in the current obstack
  102. as well as everything allocated since PTR.
  103. In practice the current obstack is always the temporary one. */
  104. void
  105. obfree (ptr)
  106. char *ptr;
  107. {
  108. obstack_free (current_obstack, ptr);
  109. }
  110. /* Allocate SIZE bytes in the permanent obstack
  111. and return a pointer to them. */
  112. char *
  113. permalloc (size)
  114. long size;
  115. {
  116. char *object;
  117. return (char *) obstack_alloc (&permanent_obstack, size);
  118. }
  119. /* Return a newly allocated node of code CODE.
  120. Initialize the node's unique id and its TREE_PERMANENT flag.
  121. For decl and type nodes, some other fields are initialized.
  122. The rest of the node is initialized to zero.
  123. Achoo! I got a code in the node. */
  124. tree
  125. make_node (code)
  126. enum tree_code code;
  127. {
  128. register tree t;
  129. register int type = *tree_code_type[(int) code];
  130. register int length;
  131. register struct obstack *obstack;
  132. register int i;
  133. switch (type)
  134. {
  135. case 'd': /* A decl node */
  136. length = sizeof (struct tree_decl);
  137. break;
  138. case 't': /* a type node */
  139. length = sizeof (struct tree_type);
  140. break;
  141. case 's': /* a stmt node */
  142. length = sizeof (struct tree_shared)
  143. + 2 * sizeof (int)
  144. + tree_code_length[(int) code] * sizeof (char *);
  145. break;
  146. default: /* an expression or constant. */
  147. length = sizeof (struct tree_shared)
  148. + tree_code_length[(int) code] * sizeof (char *);
  149. }
  150. obstack = (code != IDENTIFIER_NODE) ? current_obstack : &permanent_obstack;
  151. t = (tree) obstack_alloc (obstack, length);
  152. TREE_UID (t) = tree_node_counter++;
  153. TREE_TYPE (t) = 0;
  154. TREE_CHAIN (t) = 0;
  155. for (i = (length / sizeof (int)) - 1;
  156. i >= sizeof (struct tree_shared) / sizeof (int) - 1;
  157. i--)
  158. ((int *) t)[i] = 0;
  159. TREE_SET_CODE (t, code);
  160. if (obstack == &permanent_obstack)
  161. TREE_PERMANENT (t) = 1;
  162. if (type == 'd')
  163. {
  164. extern int lineno;
  165. DECL_ALIGN (t) = 1;
  166. DECL_SIZE_UNIT (t) = 1;
  167. DECL_VOFFSET_UNIT (t) = 1;
  168. DECL_SOURCE_LINE (t) = lineno;
  169. DECL_SOURCE_FILE (t) = input_filename;
  170. }
  171. if (type == 't')
  172. {
  173. TYPE_ALIGN (t) = 1;
  174. TYPE_SIZE_UNIT (t) = 1;
  175. TYPE_SEP_UNIT (t) = 1;
  176. TYPE_MAIN_VARIANT (t) = t;
  177. }
  178. if (type == 'c')
  179. {
  180. TREE_LITERAL (t) = 1;
  181. }
  182. return t;
  183. }
  184. /* Return a new node with the same contents as NODE
  185. except that its TREE_CHAIN is zero and it has a fresh uid. */
  186. tree
  187. copy_node (node)
  188. tree node;
  189. {
  190. register tree t;
  191. register enum tree_code code = TREE_CODE (node);
  192. register int length;
  193. register int i;
  194. switch (*tree_code_type[(int) code])
  195. {
  196. case 'd': /* A decl node */
  197. length = sizeof (struct tree_decl);
  198. break;
  199. case 't': /* a type node */
  200. length = sizeof (struct tree_type);
  201. break;
  202. case 's':
  203. length = sizeof (struct tree_shared)
  204. + 2 * sizeof (int)
  205. + tree_code_length[(int) code] * sizeof (char *);
  206. break;
  207. default: /* a statement, expression or constant. */
  208. length = sizeof (struct tree_shared)
  209. + tree_code_length[(int) code] * sizeof (char *);
  210. }
  211. t = (tree) obstack_alloc (current_obstack, length);
  212. for (i = (length / sizeof (int)) - 1;
  213. i >= 0;
  214. i--)
  215. ((int *) t)[i] = ((int *) node)[i];
  216. TREE_UID (t) = tree_node_counter++;
  217. TREE_CHAIN (t) = 0;
  218. TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
  219. return t;
  220. }
  221. #define HASHBITS 30
  222. /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
  223. If an identifier with that name has previously been referred to,
  224. the same node is returned this time. */
  225. tree
  226. get_identifier (text)
  227. register char *text;
  228. {
  229. register int hi;
  230. register int i;
  231. register tree idp;
  232. register int len;
  233. /* Compute length of text in len. */
  234. for (len = 0; text[len]; len++);
  235. /* Compute hash code */
  236. hi = len;
  237. for (i = 0; i < len; i++)
  238. hi = ((hi * 613) + (unsigned)(text[i]));
  239. hi &= (1 << HASHBITS) - 1;
  240. hi %= MAX_HASH_TABLE;
  241. /* Search table for identifier */
  242. for (idp = hash_table[hi]; idp!=NULL; idp = TREE_CHAIN (idp))
  243. if (IDENTIFIER_LENGTH (idp) == len &&
  244. !strcmp (IDENTIFIER_POINTER (idp), text))
  245. return idp; /* <-- return if found */
  246. /* Not found, create one, add to chain */
  247. idp = make_node (IDENTIFIER_NODE);
  248. IDENTIFIER_LENGTH (idp) = len;
  249. IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
  250. TREE_CHAIN (idp) = hash_table[hi];
  251. hash_table[hi] = idp;
  252. return idp; /* <-- return if created */
  253. }
  254. /* Return a newly constructed INTEGER_CST node whose constant value
  255. is specified by the two ints LOW and HI.
  256. The TREE_TYPE is not initialized. */
  257. tree
  258. build_int_2 (low, hi)
  259. int low, hi;
  260. {
  261. register tree t = make_node (INTEGER_CST);
  262. TREE_INT_CST_LOW (t) = low;
  263. TREE_INT_CST_HIGH (t) = hi;
  264. TREE_TYPE (t) = integer_type_node;
  265. return t;
  266. }
  267. /* Return a REAL_CST node containing a value atoi (STR) * 10**EX.
  268. The TREE_TYPE is not initialized. */
  269. tree
  270. build_real_from_string (str, ex)
  271. char *str;
  272. int ex;
  273. {
  274. double r;
  275. int i;
  276. tree t;
  277. t = make_node (REAL_CST);
  278. /* ??? This conversion code is not good.
  279. Use the new atof once it is done. */
  280. sscanf (str, "%lf", &r);
  281. if (0 < ex)
  282. for (i = 0; i < ex; i++)
  283. r = r * 10;
  284. else
  285. for (i = 0; i < -ex; i++)
  286. r = r / 10;
  287. TREE_REAL_CST (t) = r;
  288. return (t);
  289. }
  290. /* Return a newly constructed REAL_CST node whose value is D.
  291. The TREE_TYPE is not initialized. */
  292. tree
  293. build_real (d)
  294. double d;
  295. {
  296. tree v;
  297. v = make_node (REAL_CST);
  298. TREE_REAL_CST (v) = d;
  299. return v;
  300. }
  301. /* Return a newly constructed REAL_CST node whose value
  302. is the integer value of the INTEGER_CST node I.
  303. The TREE_TYPE is not initialized. */
  304. tree
  305. build_real_from_int_cst (i)
  306. tree i;
  307. {
  308. tree v;
  309. v = make_node (REAL_CST);
  310. TREE_REAL_CST (v)
  311. = (double) TREE_INT_CST_LOW (i)
  312. + ((double) (1 << (HOST_BITS_PER_INT / 2))
  313. * (double) (1 << (HOST_BITS_PER_INT / 2))
  314. * (double) TREE_INT_CST_HIGH (i));
  315. return v;
  316. }
  317. /* Return a newly constructed STRING_CST node whose value is
  318. the LEN characters at STR.
  319. The TREE_TYPE is not initialized. */
  320. tree
  321. build_string (len, str)
  322. int len;
  323. char *str;
  324. {
  325. register tree s = make_node (STRING_CST);
  326. TREE_STRING_LENGTH (s) = len;
  327. TREE_STRING_POINTER (s) = obstack_copy0 (current_obstack, str, len);
  328. return s;
  329. }
  330. /* Return a newly constructed COMPLEX_CST node whose value is
  331. specified by the real and imaginary parts REAL and IMAG.
  332. Both REAL and IMAG should be constant nodes.
  333. The TREE_TYPE is not initialized. */
  334. tree
  335. build_complex (real, imag)
  336. tree real, imag;
  337. {
  338. register tree t = make_node (COMPLEX_CST);
  339. TREE_REALPART (t) = real;
  340. TREE_IMAGPART (t) = imag;
  341. return t;
  342. }
  343. /* Return nonzero if the type FTYPE is unsigned (all possible values >= 0).
  344. Nonscalar types are considered unsigned; real types considered signed. */
  345. int
  346. type_unsigned_p (ftype)
  347. tree ftype;
  348. {
  349. register tree type = ftype;
  350. register tree t;
  351. if (TREE_CODE (ftype) == POINTER_TYPE)
  352. return 1;
  353. if (TREE_CODE (ftype) == REAL_TYPE)
  354. return 0;
  355. if (TREE_CODE (ftype) != INTEGER_TYPE
  356. && TREE_CODE (ftype) != ENUMERAL_TYPE)
  357. return 1;
  358. while (1)
  359. {
  360. t = TYPE_MIN_VALUE (type);
  361. if (TREE_CODE (t) == INTEGER_CST)
  362. return TREE_INT_CST_HIGH (t) >= 0;
  363. type = TREE_TYPE (type);
  364. if (type == 0)
  365. return 0;
  366. }
  367. }
  368. /* Return 1 if EXPR is the integer constant zero. */
  369. int
  370. integer_zerop (expr)
  371. tree expr;
  372. {
  373. return TREE_CODE (expr) == INTEGER_CST
  374. && TREE_INT_CST_LOW (expr) == 0
  375. && TREE_INT_CST_HIGH (expr) == 0;
  376. }
  377. /* Return 1 if EXPR is the integer constant one. */
  378. int
  379. integer_onep (expr)
  380. tree expr;
  381. {
  382. return TREE_CODE (expr) == INTEGER_CST
  383. && TREE_INT_CST_LOW (expr) == 1
  384. && TREE_INT_CST_HIGH (expr) == 0;
  385. }
  386. /* Return 1 if EXPR is an integer containing all 1's
  387. in as much precision as it contains. */
  388. int
  389. integer_all_onesp (expr)
  390. tree expr;
  391. {
  392. register int prec;
  393. register int uns;
  394. if (TREE_CODE (expr) != INTEGER_CST)
  395. return 0;
  396. uns = type_unsigned_p (TREE_TYPE (expr));
  397. if (!uns)
  398. return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
  399. prec = TYPE_PRECISION (TREE_TYPE (expr));
  400. if (prec >= HOST_BITS_PER_INT)
  401. return TREE_INT_CST_LOW (expr) == -1
  402. && TREE_INT_CST_HIGH (expr) == (1 << (prec - HOST_BITS_PER_INT)) - 1;
  403. else
  404. return TREE_INT_CST_LOW (expr) == (1 << prec) - 1;
  405. }
  406. /* Return the length of a chain of nodes chained through TREE_CHAIN.
  407. We expect a null pointer to mark the end of the chain.
  408. This is the Lisp primitive `length'. */
  409. int
  410. list_length (t)
  411. tree t;
  412. {
  413. register tree tail;
  414. register int len = 0;
  415. for (tail = t; tail; tail = TREE_CHAIN (tail))
  416. len++;
  417. return len;
  418. }
  419. /* Concatenate two chains of nodes (chained through TREE_CHAIN)
  420. by modifying the last node in chain 1 to point to chain 2.
  421. This is the Lisp primitive `nconc'. */
  422. tree
  423. chainon (op1, op2)
  424. tree op1, op2;
  425. {
  426. tree t;
  427. if (op1)
  428. {
  429. for (t = op1; TREE_CHAIN (t); t = TREE_CHAIN (t))
  430. if (t == op2) abort (); /* Circularity being created */
  431. TREE_CHAIN (t) = op2;
  432. return op1;
  433. }
  434. else return op2;
  435. }
  436. /* Return a newly created TREE_LIST node whose
  437. purpose and value fields are PARM and VALUE. */
  438. tree
  439. build_tree_list (parm, value)
  440. tree parm, value;
  441. {
  442. register tree t = make_node (TREE_LIST);
  443. TREE_PURPOSE (t) = parm;
  444. TREE_VALUE (t) = value;
  445. return t;
  446. }
  447. /* Return a newly created TREE_LIST node whose
  448. purpose and value fields are PARM and VALUE
  449. and whose TREE_CHAIN is CHAIN. */
  450. tree
  451. tree_cons (purpose, value, chain)
  452. tree purpose, value, chain;
  453. {
  454. register tree node = make_node (TREE_LIST);
  455. TREE_CHAIN (node) = chain;
  456. TREE_PURPOSE (node) = purpose;
  457. TREE_VALUE (node) = value;
  458. return node;
  459. }
  460. /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
  461. tree
  462. tree_last (chain)
  463. register tree chain;
  464. {
  465. register tree next;
  466. if (chain)
  467. while (next = TREE_CHAIN (chain))
  468. chain = next;
  469. return chain;
  470. }
  471. /* Reverse the order of elements in the chain T,
  472. and return the new head of the chain (old last element). */
  473. tree
  474. nreverse (t)
  475. tree t;
  476. {
  477. register tree prev = 0, decl, next;
  478. for (decl = t; decl; decl = next)
  479. {
  480. next = TREE_CHAIN (decl);
  481. TREE_CHAIN (decl) = prev;
  482. prev = decl;
  483. }
  484. return prev;
  485. }
  486. /* Return the size nominally occupied by an object of type TYPE
  487. when it resides in memory. The value is measured in units of bytes,
  488. and its data type is that normally used for type sizes
  489. (which is the first type created by make_signed_type or
  490. make_unsigned_type). */
  491. tree
  492. size_in_bytes (type)
  493. tree type;
  494. {
  495. if (type == error_mark_node)
  496. return integer_zero_node;
  497. return convert_units (TYPE_SIZE (type), TYPE_SIZE_UNIT (type),
  498. BITS_PER_UNIT);
  499. }
  500. /* Return nonzero if arg is static -- a reference to an object in
  501. static storage. This is not the same as the C meaning of `static'. */
  502. int
  503. staticp (arg)
  504. tree arg;
  505. {
  506. register enum tree_code code = TREE_CODE (arg);
  507. if ((code == VAR_DECL || code == FUNCTION_DECL)
  508. && (TREE_STATIC (arg) || TREE_EXTERNAL (arg)))
  509. return 1;
  510. if (code == COMPONENT_REF)
  511. return staticp (TREE_OPERAND (arg, 0));
  512. return 0;
  513. }
  514. /* Verify that an expression, REF, is a reference to data that makes sense
  515. to modify or take the address of
  516. (i.e., for processing the argument to unary & or the left arg to =).
  517. Error if REF is some other kind of expression.
  518. We can safely ignore the difference between "makes sense to modify"
  519. and "makes sense to take the address of", because attempting to
  520. take the address of a variable will force it into memory anyway. */
  521. int
  522. lvalue_or_else (ref)
  523. tree ref;
  524. {
  525. register enum tree_code code = TREE_CODE (ref);
  526. if (code == COMPONENT_REF)
  527. return lvalue_or_else (TREE_OPERAND (ref, 0));
  528. else if (code == INDIRECT_REF || code == ARRAY_REF || code == VAR_DECL
  529. || code == FUNCTION_DECL || code == PARM_DECL || code == RESULT_DECL
  530. || code == ERROR_MARK)
  531. return 1;
  532. yyerror ("invalid lvalue (not a reference to data in memory)");
  533. return 0;
  534. }
  535. /* This should be applied to any node which may be used in more than one place,
  536. but must be evaluated only once. Normally, the code generator would
  537. reevaluate the node each time; this forces it to compute it once and save
  538. the result. This is done by encapsulating the node in a SAVE_EXPR. */
  539. tree
  540. save_expr (expr)
  541. tree expr;
  542. {
  543. register tree t = fold (expr);
  544. /* If the tree evaluates to a constant, then we don't what to hide that
  545. fact (i.e. this allows further folding, and direct checks for constants).
  546. Since it is no problem to reevaluate literals, we just return the
  547. literal node. */
  548. if (TREE_LITERAL (t) || TREE_READONLY (t) || TREE_CODE (t) == SAVE_EXPR)
  549. return t;
  550. t = build2 (SAVE_EXPR, t, NULL);
  551. TREE_TYPE (t) = TREE_TYPE (expr);
  552. TREE_VOLATILE (t) = TREE_VOLATILE (expr);
  553. return t;
  554. }
  555. /* Stabilize a reference so that we can use it any number of times
  556. without causing its operands to be evaluated more than once.
  557. Returns the stabilized reference. */
  558. tree
  559. stabilize_reference (ref)
  560. tree ref;
  561. {
  562. register tree result;
  563. register enum tree_code code = TREE_CODE (ref);
  564. if (code == INDIRECT_REF)
  565. {
  566. result = build1 (INDIRECT_REF, save_expr (TREE_OPERAND (ref, 0)));
  567. }
  568. else if (code == COMPONENT_REF)
  569. {
  570. result = build2 (COMPONENT_REF,
  571. stabilize_reference (TREE_OPERAND (ref, 0)),
  572. TREE_OPERAND (ref, 1));
  573. }
  574. else if (code == ARRAY_REF)
  575. {
  576. result = build2 (ARRAY_REF, save_expr (TREE_OPERAND (ref, 0)),
  577. save_expr (TREE_OPERAND (ref, 1)));
  578. }
  579. else if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
  580. {
  581. result = ref;
  582. }
  583. else
  584. {
  585. if (code != ERROR_MARK)
  586. yyerror ("invalid lvalue (not a reference to data in memory)");
  587. return error_mark_node;
  588. }
  589. TREE_TYPE (result) = TREE_TYPE (ref);
  590. TREE_VOLATILE (result) = TREE_VOLATILE (ref);
  591. return result;
  592. }
  593. /* Low-level constructors for expressions. */
  594. /* Return a newly created expression-node of code SC and operand ARG1.
  595. Used for codes that want one operand.
  596. The TREE_TYPE is not initialized. */
  597. tree
  598. build1 (sc, arg1)
  599. enum tree_code sc;
  600. tree arg1;
  601. {
  602. register tree t = make_node (sc);
  603. TREE_OPERAND (t, 0) = arg1;
  604. return t;
  605. }
  606. /* Return a newly created expression-node of code SC
  607. and operands ARG1 and ARG2.
  608. Used for codes that want two operands.
  609. The TREE_TYPE is not initialized. */
  610. tree
  611. build2 (sc, arg1, arg2)
  612. enum tree_code sc;
  613. tree arg1, arg2;
  614. {
  615. register tree t = make_node (sc);
  616. TREE_OPERAND (t, 0) = arg1;
  617. TREE_OPERAND (t, 1) = arg2;
  618. return t;
  619. }
  620. /* Return a newly created expression-node of code SC
  621. and operands ARG1, ARG2 and ARG3.
  622. Used for codes that want three operands.
  623. The TREE_TYPE is not initialized. */
  624. tree
  625. build3 (sc, arg1, arg2, arg3)
  626. int sc;
  627. tree arg1, arg2, arg3;
  628. {
  629. register tree t = make_node (sc);
  630. TREE_OPERAND (t, 0) = arg1;
  631. TREE_OPERAND (t, 1) = arg2;
  632. TREE_OPERAND (t, 2) = arg3;
  633. return t;
  634. }
  635. /* Low-level constructors for statements.
  636. These constructors all expect source file name and line number
  637. as arguments, as well as enough arguments to fill in the data
  638. in the statement node. */
  639. tree
  640. build_goto (filename, line, label)
  641. char *filename;
  642. int line;
  643. tree label;
  644. {
  645. register tree t = make_node (GOTO_STMT);
  646. STMT_SOURCE_FILE (t) = filename;
  647. STMT_SOURCE_LINE (t) = line;
  648. STMT_BODY (t) = label;
  649. return t;
  650. }
  651. tree
  652. build_return (filename, line, arg)
  653. char *filename;
  654. int line;
  655. tree arg;
  656. {
  657. register tree t = make_node (RETURN_STMT);
  658. STMT_SOURCE_FILE (t) = filename;
  659. STMT_SOURCE_LINE (t) = line;
  660. STMT_BODY (t) = arg;
  661. return t;
  662. }
  663. tree
  664. build_expr_stmt (filename, line, expr)
  665. char *filename;
  666. int line;
  667. tree expr;
  668. {
  669. register tree t = make_node (EXPR_STMT);
  670. STMT_SOURCE_FILE (t) = filename;
  671. STMT_SOURCE_LINE (t) = line;
  672. STMT_BODY (t) = expr;
  673. return t;
  674. }
  675. tree
  676. build_if (filename, line, cond, thenclause, elseclause)
  677. char *filename;
  678. int line;
  679. tree cond, thenclause, elseclause;
  680. {
  681. register tree t = make_node (IF_STMT);
  682. STMT_SOURCE_FILE (t) = filename;
  683. STMT_SOURCE_LINE (t) = line;
  684. STMT_COND (t) = cond;
  685. STMT_THEN (t) = thenclause;
  686. STMT_ELSE (t) = elseclause;
  687. return t;
  688. }
  689. tree
  690. build_exit (filename, line, cond)
  691. char *filename;
  692. int line;
  693. tree cond;
  694. {
  695. register tree t = make_node (EXIT_STMT);
  696. STMT_SOURCE_FILE (t) = filename;
  697. STMT_SOURCE_LINE (t) = line;
  698. STMT_BODY (t) = cond;
  699. return t;
  700. }
  701. tree
  702. build_asm_stmt (filename, line, asmcode)
  703. char *filename;
  704. int line;
  705. tree asmcode;
  706. {
  707. register tree t = make_node (ASM_STMT);
  708. STMT_SOURCE_FILE (t) = filename;
  709. STMT_SOURCE_LINE (t) = line;
  710. STMT_BODY (t) = asmcode;
  711. return t;
  712. }
  713. tree
  714. build_case (filename, line, object, cases)
  715. char *filename;
  716. int line;
  717. tree object, cases;
  718. {
  719. register tree t = make_node (CASE_STMT);
  720. STMT_SOURCE_FILE (t) = filename;
  721. STMT_SOURCE_LINE (t) = line;
  722. STMT_CASE_INDEX (t) = object;
  723. STMT_CASE_LIST (t) = cases;
  724. return t;
  725. }
  726. tree
  727. build_let (filename, line, vars, body, supercontext, tags)
  728. char *filename;
  729. int line;
  730. tree vars, body, supercontext, tags;
  731. {
  732. register tree t = make_node (LET_STMT);
  733. STMT_SOURCE_FILE (t) = filename;
  734. STMT_SOURCE_LINE (t) = line;
  735. STMT_VARS (t) = vars;
  736. STMT_BODY (t) = body;
  737. STMT_SUPERCONTEXT (t) = supercontext;
  738. STMT_BIND_SIZE (t) = 0;
  739. STMT_TYPE_TAGS (t) = tags;
  740. return t;
  741. }
  742. tree
  743. build_loop (filename, line, body)
  744. char *filename;
  745. int line;
  746. tree body;
  747. {
  748. register tree t = make_node (LOOP_STMT);
  749. STMT_SOURCE_FILE (t) = filename;
  750. STMT_SOURCE_LINE (t) = line;
  751. STMT_BODY (t) = body;
  752. return t;
  753. }
  754. tree
  755. build_compound (filename, line, body)
  756. char *filename;
  757. int line;
  758. tree body;
  759. {
  760. register tree t = make_node (COMPOUND_STMT);
  761. STMT_SOURCE_FILE (t) = filename;
  762. STMT_SOURCE_LINE (t) = line;
  763. STMT_BODY (t) = body;
  764. return t;
  765. }
  766. /* Return a type like TYPE except that its TREE_READONLY is CONSTP
  767. and its TREE_VOLATILE is VOLATILEP.
  768. Such variant types already made are recorded so that duplicates
  769. are not made.
  770. A variant types should never be used as the type of an expression.
  771. Always copy the variant information into the TREE_READONLY
  772. and TREE_VOLATILE of the expression, and then give the expression
  773. as its type the "main variant", the variant whose TREE_READONLY
  774. and TREE_VOLATILE are zero. Use TYPE_MAIN_VARIANT to find the
  775. main variant. */
  776. tree
  777. build_type_variant (type, constp, volatilep)
  778. tree type;
  779. int constp, volatilep;
  780. {
  781. register tree t, m = TYPE_MAIN_VARIANT (type);
  782. register struct obstack *ambient_obstack = current_obstack;
  783. /* Treat any nonzero argument as 1. */
  784. constp = !!constp;
  785. volatilep = !!volatilep;
  786. /* First search the chain variants for one that is what we want. */
  787. for (t = m; t; t = TYPE_NEXT_VARIANT (t))
  788. if (constp == TREE_READONLY (t)
  789. && volatilep == TREE_VOLATILE (t))
  790. return t;
  791. /* We need a new one. */
  792. current_obstack = TREE_PERMANENT (type) ? &permanent_obstack : &temporary_obstack;
  793. t = copy_node (type);
  794. TREE_READONLY (t) = constp;
  795. TREE_VOLATILE (t) = volatilep;
  796. TYPE_POINTER_TO (t) = 0;
  797. /* Add this type to the chain of variants of TYPE. */
  798. TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
  799. TYPE_NEXT_VARIANT (m) = t;
  800. current_obstack = ambient_obstack;
  801. return t;
  802. }
  803. /* Constructors for pointer, array and function types.
  804. (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
  805. constructed by language-dependent code, not here.) */
  806. tree
  807. build_pointer_type (to_type)
  808. tree to_type;
  809. {
  810. register tree t = TYPE_POINTER_TO (to_type);
  811. register struct obstack *ambient_obstack = current_obstack;
  812. /* First, if we already have a type for pointers to TO_TYPE, use it. */
  813. if (t)
  814. return t;
  815. /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
  816. current_obstack = (TREE_PERMANENT (to_type)
  817. ? &permanent_obstack
  818. : &temporary_obstack);
  819. t = make_node (POINTER_TYPE);
  820. TREE_TYPE (t) = to_type;
  821. /* Record this type as the pointer to TO_TYPE. */
  822. TYPE_POINTER_TO (to_type) = t;
  823. /* If this type is permanent but we are really inside a function,
  824. lay it out now, so that the size, etc. are permanent too. */
  825. if (current_obstack != ambient_obstack)
  826. layout_type (t);
  827. current_obstack = ambient_obstack;
  828. return t;
  829. }
  830. tree
  831. build_array_type (elt_type, index_type)
  832. tree elt_type, index_type;
  833. {
  834. register tree t = make_node (ARRAY_TYPE);
  835. if (TREE_CODE (elt_type) == FUNCTION_TYPE)
  836. {
  837. yyerror ("arrays of functions are not meaningful");
  838. elt_type = integer_type_node;
  839. }
  840. TREE_TYPE (t) = elt_type;
  841. TYPE_DOMAIN (t) = index_type;
  842. /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
  843. build_pointer_type (elt_type);
  844. return t;
  845. }
  846. /* Build a function type, which is a FUNCTION_TYPE node.
  847. The TREE_TYPE of this node is the type of the value returned.
  848. If no value is returned, the TREE_TYPE may be 0 or it
  849. may be a node for a void type.
  850. ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
  851. are data type nodes for the arguments of the function. */
  852. tree
  853. build_function_type (value_type, arg_types)
  854. tree value_type, arg_types;
  855. {
  856. register tree t;
  857. if (TREE_CODE (value_type) == FUNCTION_DECL
  858. || TREE_CODE (value_type) == ARRAY_TYPE)
  859. {
  860. yyerror ("function return type cannot be function or array");
  861. value_type = integer_type_node;
  862. }
  863. t = make_node (FUNCTION_TYPE);
  864. TREE_TYPE (t) = value_type;
  865. TYPE_ARG_TYPES (t) = arg_types;
  866. return t;
  867. }
  868. /* Return OP, stripped of any conversions to wider types as much as is safe.
  869. Converting the value back to OP's type makes a value equivalent to OP.
  870. If FOR_TYPE is nonzero, we return a value which, if converted to
  871. type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
  872. OP must have integer, real or enumeral type. Pointers are not allowed!
  873. There are some cases where the obvious value we could return
  874. would regenerate to OP if converted to OP's type,
  875. but would not extend like OP to wider types.
  876. If FOR_TYPE indicates such extension is contemplated, we eschew such values.
  877. For example, if OP is (unsigned short)(signed char)-1,
  878. we avoid returning (signed char)-1 if FOR_TYPE is int,
  879. even though extending that to an unsigned short would regenerate OP,
  880. since the result of extending (signed char)-1 to (int)
  881. is different from (int) OP. */
  882. tree
  883. get_unwidened (op, for_type)
  884. register tree op;
  885. tree for_type;
  886. {
  887. /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
  888. /* TYPE_PRECISION is safe in place of type_precision since
  889. pointer types are not allowed. */
  890. register tree type = TREE_TYPE (op);
  891. register int final_prec = TYPE_PRECISION (for_type != 0 ? for_type : type);
  892. register int uns
  893. = (for_type != 0 && for_type != type
  894. && final_prec > TYPE_PRECISION (type)
  895. && type_unsigned_p (type));
  896. register tree win = op;
  897. while (TREE_CODE (op) == NOP_EXPR)
  898. {
  899. register int bitschange
  900. = TYPE_PRECISION (TREE_TYPE (op))
  901. - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
  902. /* Truncations are many-one so cannot be removed.
  903. Unless we are later going to truncate down even farther. */
  904. if (bitschange < 0
  905. && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
  906. break;
  907. /* See what's inside this conversion. If we decide to strip it,
  908. we will set WIN. */
  909. op = TREE_OPERAND (op, 0);
  910. /* If we have not stripped any zero-extensions (uns is 0),
  911. we can strip any kind of extension.
  912. If we have previously stripped a zero-extension,
  913. only zero-extensions can safely be stripped.
  914. Any extension can be stripped if the bits it would produce
  915. are all going to be discarded later by truncating to FOR_TYPE. */
  916. if (bitschange > 0)
  917. {
  918. if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
  919. win = op;
  920. /* type_unsigned_p says whether this is a zero-extension.
  921. Let's avoid computing it if it does not affect WIN
  922. and if UNS will not be needed again. */
  923. if ((uns || TREE_CODE (op) == NOP_EXPR)
  924. && type_unsigned_p (TREE_TYPE (op)))
  925. {
  926. uns = 1;
  927. win = op;
  928. }
  929. }
  930. }
  931. if (TREE_CODE (op) == COMPONENT_REF)
  932. {
  933. int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
  934. * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
  935. type = type_for_size (innerprec, type_unsigned_p (TREE_TYPE (op)));
  936. /* We can get this structure field in the narrowest type it fits in
  937. but the resulting extension to its nominal type (a fullword type)
  938. must fit the same conditions as for other extensions. */
  939. if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
  940. && (! uns || final_prec <= innerprec
  941. || type_unsigned_p (TREE_TYPE (op))))
  942. {
  943. if (type != 0)
  944. {
  945. win = build2 (COMPONENT_REF, TREE_OPERAND (op, 0),
  946. TREE_OPERAND (op, 1));
  947. TREE_TYPE (win) = type;
  948. }
  949. }
  950. }
  951. return win;
  952. }
  953. /* Return OP or a simpler expression for a narrower value
  954. which can be sign-extended or zero-extended to give back OP.
  955. Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
  956. or 0 if the value should be sign-extended. */
  957. tree
  958. get_narrower (op, unsignedp_ptr)
  959. register tree op;
  960. int *unsignedp_ptr;
  961. {
  962. register int uns = 0;
  963. int first = 1;
  964. register tree win = op;
  965. while (TREE_CODE (op) == NOP_EXPR)
  966. {
  967. register int bitschange
  968. = TYPE_PRECISION (TREE_TYPE (op))
  969. - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
  970. /* Truncations are many-one so cannot be removed. */
  971. if (bitschange < 0)
  972. break;
  973. /* See what's inside this conversion. If we decide to strip it,
  974. we will set WIN. */
  975. op = TREE_OPERAND (op, 0);
  976. if (bitschange > 0)
  977. {
  978. /* An extension: the outermost one can be stripped,
  979. but remember whether it is zero or sign extension. */
  980. if (first)
  981. uns = type_unsigned_p (TREE_TYPE (op));
  982. /* Otherwise, if a sign extension has been stripped,
  983. only sign extensions can now be stripped;
  984. if a zero extension has been stripped, only zero-extensions. */
  985. else if (uns != type_unsigned_p (TREE_TYPE (op)))
  986. break;
  987. first = 0;
  988. }
  989. /* A change in nominal type can always be stripped. */
  990. win = op;
  991. }
  992. if (TREE_CODE (op) == COMPONENT_REF)
  993. {
  994. int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
  995. * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
  996. tree type = type_for_size (innerprec, type_unsigned_p (TREE_TYPE (op)));
  997. /* We can get this structure field in a narrower type that fits it,
  998. but the resulting extension to its nominal type (a fullword type)
  999. must satisfy the same conditions as for other extensions. */
  1000. if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
  1001. && (first || uns == type_unsigned_p (TREE_TYPE (op)))
  1002. && type != 0)
  1003. {
  1004. win = build2 (COMPONENT_REF, TREE_OPERAND (op, 0),
  1005. TREE_OPERAND (op, 1));
  1006. TREE_TYPE (win) = type;
  1007. }
  1008. }
  1009. *unsignedp_ptr = uns;
  1010. return win;
  1011. }
  1012. /* Return the precision of a type, for arithmetic purposes.
  1013. Supports all types on which arithmetic is possible
  1014. (including pointer types).
  1015. It's not clear yet what will be right for complex types. */
  1016. int
  1017. type_precision (type)
  1018. register tree type;
  1019. {
  1020. return ((TREE_CODE (type) == INTEGER_TYPE
  1021. || TREE_CODE (type) == ENUMERAL_TYPE
  1022. || TREE_CODE (type) == REAL_TYPE)
  1023. ? TYPE_PRECISION (type) : BITS_PER_WORD);
  1024. }
  1025. /* Nonzero if integer constant C has a value that is permissible
  1026. for type TYPE (an INTEGER_TYPE). */
  1027. int
  1028. int_fits_type_p (c, type)
  1029. tree c, type;
  1030. {
  1031. if (type_unsigned_p (type))
  1032. return (!INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
  1033. && !INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type)));
  1034. else
  1035. return (!INT_CST_LT (TYPE_MAX_VALUE (type), c)
  1036. && !INT_CST_LT (c, TYPE_MIN_VALUE (type)));
  1037. }
  1038. /* Subroutines of `convert'. */
  1039. /* Change of width--truncation and extension of integers or reals--
  1040. is represented with NOP_EXPR. Proper functioning of many things
  1041. assumes that no other conversions can be NOP_EXPRs.
  1042. Conversion between integer and pointer is represented with CONVERT_EXPR.
  1043. Converting integer to real uses FLOAT_EXPR
  1044. and real to integer uses FIX_TRUNC_EXPR. */
  1045. /* Generate an expression for a conversion using expression code CODE.
  1046. It will convert EXPR to type TYPE. */
  1047. static tree
  1048. build_convert (code, type, expr)
  1049. enum tree_code code;
  1050. tree type, expr;
  1051. {
  1052. register tree tem = build1 (code, expr);
  1053. TREE_TYPE (tem) = type;
  1054. TREE_VOLATILE (tem) = TREE_VOLATILE (expr);
  1055. return tem;
  1056. }
  1057. static tree
  1058. convert_to_pointer (type, expr)
  1059. tree type, expr;
  1060. {
  1061. register tree intype = TREE_TYPE (expr);
  1062. register enum tree_code form = TREE_CODE (intype);
  1063. if (integer_zerop (expr))
  1064. {
  1065. if (type == TREE_TYPE (null_pointer_node))
  1066. return null_pointer_node;
  1067. expr = build_int_2 (0, 0);
  1068. TREE_TYPE (expr) = type;
  1069. return expr;
  1070. }
  1071. if (form == POINTER_TYPE)
  1072. return build_convert (NOP_EXPR, type, expr);
  1073. if (intype == integer_type_node)
  1074. return build_convert (CONVERT_EXPR, type, expr);
  1075. if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  1076. return convert_to_pointer (type, convert (integer_type_node, expr));
  1077. yyerror ("cannot convert to a pointer type");
  1078. return null_pointer_node;
  1079. }
  1080. /* The result of this is always supposed to be a newly created tree node
  1081. not in use in any existing structure. */
  1082. static tree
  1083. convert_to_integer (type, expr)
  1084. tree type, expr;
  1085. {
  1086. register tree intype = TREE_TYPE (expr);
  1087. register enum tree_code form = TREE_CODE (intype);
  1088. extern tree build_binary_op_nodefault ();
  1089. extern tree build_unary_op ();
  1090. if (form == POINTER_TYPE)
  1091. {
  1092. if (integer_zerop (expr))
  1093. expr = integer_zero_node;
  1094. else
  1095. expr = fold (build_convert (CONVERT_EXPR, integer_type_node, expr));
  1096. intype = TREE_TYPE (expr);
  1097. form = TREE_CODE (intype);
  1098. }
  1099. if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  1100. {
  1101. register int outprec = TYPE_PRECISION (type);
  1102. register int inprec = TYPE_PRECISION (intype);
  1103. register enum tree_code ex_form = TREE_CODE (expr);
  1104. if (outprec >= inprec)
  1105. return build_convert (NOP_EXPR, type, expr);
  1106. /* Here detect when we can distribute the truncation down past some arithmetic.
  1107. For example, if adding two longs and converting to an int,
  1108. we can equally well convert both to ints and then add.
  1109. For the operations handled here, such truncation distribution
  1110. is always safe.
  1111. It is desirable in these cases:
  1112. 1) when truncating down to full-word from a larger size
  1113. 2) when truncating takes no work.
  1114. 3) when at least one operand of the arithmetic has been extended
  1115. (as by C's default conversions). In this case we need two conversions
  1116. if we do the arithmetic as already requested, so we might as well
  1117. truncate both and then combine. Perhaps that way we need only one.
  1118. Note that in general we cannot do the arithmetic in a type
  1119. shorter than the desired result of conversion, even if the operands
  1120. are both extended from a shorter type, because they might overflow
  1121. if combined in that type. The exceptions to this--the times when
  1122. two narrow values can be combined in their narrow type even to
  1123. make a wider result--are handled by "shorten" in build_binary_op. */
  1124. switch (ex_form)
  1125. {
  1126. case RSHIFT_EXPR:
  1127. /* We can pass truncation down through right shifting
  1128. when the shift count is a negative constant. */
  1129. if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  1130. || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) > 0)
  1131. break;
  1132. goto trunc1;
  1133. case LSHIFT_EXPR:
  1134. /* We can pass truncation down through left shifting
  1135. when the shift count is a positive constant. */
  1136. if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  1137. || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) < 0)
  1138. break;
  1139. /* In this case, shifting is like multiplication. */
  1140. case PLUS_EXPR:
  1141. case MINUS_EXPR:
  1142. case MULT_EXPR:
  1143. case MAX_EXPR:
  1144. case MIN_EXPR:
  1145. case BIT_AND_EXPR:
  1146. case BIT_IOR_EXPR:
  1147. case BIT_XOR_EXPR:
  1148. case BIT_ANDTC_EXPR:
  1149. trunc1:
  1150. {
  1151. tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  1152. tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  1153. if (outprec >= BITS_PER_WORD
  1154. || TRULY_NOOP_TRUNCATION (outprec, inprec)
  1155. || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
  1156. || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
  1157. {
  1158. /* Do the arithmetic in type TYPEX,
  1159. then convert result to TYPE. */
  1160. register tree typex = type;
  1161. /* Can't do arithmetic in enumeral types
  1162. so use an integer type that will hold the values. */
  1163. if (TREE_CODE (typex) == ENUMERAL_TYPE)
  1164. typex = type_for_size (TYPE_PRECISION (typex));
  1165. /* But now perhaps TYPEX is as wide as INPREC.
  1166. In that case, do nothing special here.
  1167. (Otherwise would recurse infinitely in convert. */
  1168. if (TYPE_PRECISION (typex) != inprec)
  1169. {
  1170. /* Don't do unsigned arithmetic where signed was wanted,
  1171. or vice versa. */
  1172. typex = (type_unsigned_p (TREE_TYPE (expr))
  1173. ? unsigned_type (typex) : signed_type (typex));
  1174. return convert (type,
  1175. build_binary_op_nodefault (ex_form,
  1176. convert (typex, arg0),
  1177. convert (typex, arg1)));
  1178. }
  1179. }
  1180. }
  1181. break;
  1182. case EQ_EXPR:
  1183. case NE_EXPR:
  1184. case GT_EXPR:
  1185. case GE_EXPR:
  1186. case LT_EXPR:
  1187. case LE_EXPR:
  1188. case TRUTH_AND_EXPR:
  1189. case TRUTH_OR_EXPR:
  1190. case TRUTH_NOT_EXPR:
  1191. /* If we want result of comparison converted to a byte,
  1192. we can just regard it as a byte, since it is 0 or 1. */
  1193. TREE_TYPE (expr) = type;
  1194. return expr;
  1195. case NEGATE_EXPR:
  1196. case BIT_NOT_EXPR:
  1197. case ABS_EXPR:
  1198. {
  1199. register tree typex = type;
  1200. /* Can't do arithmetic in enumeral types
  1201. so use an integer type that will hold the values. */
  1202. if (TREE_CODE (typex) == ENUMERAL_TYPE)
  1203. typex = type_for_size (TYPE_PRECISION (typex));
  1204. /* But now perhaps TYPEX is as wide as INPREC.
  1205. In that case, do nothing special here.
  1206. (Otherwise would recurse infinitely in convert. */
  1207. if (TYPE_PRECISION (typex) != inprec)
  1208. {
  1209. /* Don't do unsigned arithmetic where signed was wanted,
  1210. or vice versa. */
  1211. typex = (type_unsigned_p (TREE_TYPE (expr))
  1212. ? unsigned_type (typex) : signed_type (typex));
  1213. return convert (type,
  1214. build_unary_op (ex_form,
  1215. convert (typex, TREE_OPERAND (expr, 0)),
  1216. 1));
  1217. }
  1218. }
  1219. case NOP_EXPR:
  1220. /* If truncating after truncating, might as well do all at once.
  1221. If truncating after extending, we may get rid of wasted work. */
  1222. return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
  1223. }
  1224. return build_convert (NOP_EXPR, type, expr);
  1225. }
  1226. if (form == REAL_TYPE)
  1227. return build_convert (FIX_TRUNC_EXPR, type, expr);
  1228. yyerror ("aggregate value used where an integer was expected");
  1229. {
  1230. register tree tem = build_int_2 (0, 0);
  1231. TREE_TYPE (tem) = type;
  1232. return tem;
  1233. }
  1234. }
  1235. static tree
  1236. convert_to_real (type, expr)
  1237. tree type, expr;
  1238. {
  1239. register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
  1240. if (form == REAL_TYPE)
  1241. return build_convert (NOP_EXPR, type, expr);
  1242. if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  1243. return build_convert (FLOAT_EXPR, type, expr);
  1244. if (form == POINTER_TYPE)
  1245. yyerror ("pointer value used where a float was expected");
  1246. else
  1247. yyerror ("aggregate value used where a float was expected");
  1248. {
  1249. register tree tem = make_node (REAL_CST);
  1250. TREE_TYPE (tem) = type;
  1251. TREE_REAL_CST (tem) = 0;
  1252. return tem;
  1253. }
  1254. }
  1255. /* Create an expression whose value is that of EXPR,
  1256. converted to type TYPE. The TREE_TYPE of the value
  1257. is always TYPE. This function implements all reasonable
  1258. conversions; callers should filter out those that are
  1259. not permitted by the language being compiled. */
  1260. tree
  1261. convert (type, expr)
  1262. tree type, expr;
  1263. {
  1264. register tree e = expr;
  1265. register enum tree_code code = TREE_CODE (type);
  1266. if (type == TREE_TYPE (expr) || TREE_CODE (expr) == ERROR_MARK)
  1267. return expr;
  1268. if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
  1269. {
  1270. yyerror ("void value not ignored as it ought to be");
  1271. return error_mark_node;
  1272. }
  1273. if (code == VOID_TYPE)
  1274. return build_convert (CONVERT_EXPR, type, e);
  1275. #if 0
  1276. /* This is incorrect. A truncation can't be stripped this way.
  1277. Extensions will be stripped by the use of get_unwidened. */
  1278. if (TREE_CODE (expr) == NOP_EXPR)
  1279. return convert (type, TREE_OPERAND (expr, 0));
  1280. #endif
  1281. if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
  1282. return fold (convert_to_integer (type, e));
  1283. if (code == POINTER_TYPE)
  1284. return fold (convert_to_pointer (type, e));
  1285. if (code == REAL_TYPE)
  1286. return fold (convert_to_real (type, e));
  1287. yyerror ("conversion to non-scalar type requested");
  1288. return error_mark_node;
  1289. }