tree.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  1. /* Language-indepednent node constructors for parse phase of GNU compiler.
  2. Copyright (C) 1987, 1988 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. #include "varargs.h"
  33. #define obstack_chunk_alloc xmalloc
  34. #define obstack_chunk_free free
  35. extern int xmalloc ();
  36. extern void free ();
  37. /* Tree nodes of permanent duration are allocated in this obstack.
  38. They are the identifier nodes, and everything outside of
  39. the bodies and parameters of function definitions. */
  40. struct obstack permanent_obstack;
  41. /* The initial RTL, and all ..._TYPE nodes, in a function
  42. are allocated in this obstack. Usually they are freed at the
  43. end of the function, but if the function is inline they are saved. */
  44. struct obstack maybepermanent_obstack;
  45. /* The contents of the current function definition are allocated
  46. in this obstack, and all are freed at the end of the function. */
  47. struct obstack temporary_obstack;
  48. /* The tree nodes of an expression are allocated
  49. in this obstack, and all are freed at the end of the expression. */
  50. struct obstack momentary_obstack;
  51. /* This points at either permanent_obstack or maybepermanent_obstack. */
  52. struct obstack *saveable_obstack;
  53. /* This is same as saveable_obstack during parse and expansion phase;
  54. it points to temporary_obstack during optimization.
  55. This is the obstack to be used for creating rtl objects. */
  56. struct obstack *rtl_obstack;
  57. /* This points at either permanent_obstack or temporary_obstack. */
  58. struct obstack *current_obstack;
  59. /* This points at either permanent_obstack or temporary_obstack
  60. or momentary_obstack. */
  61. struct obstack *expression_obstack;
  62. /* Addresses of first objects in some obstacks.
  63. This is for freeing their entire contents. */
  64. char *maybepermanent_firstobj;
  65. char *temporary_firstobj;
  66. char *momentary_firstobj;
  67. /* Stack of places to restore the momentary obstack back to. */
  68. struct momentary_level
  69. {
  70. /* Pointer back to previous such level. */
  71. struct momentary_level *prev;
  72. /* First object allocated within this level. */
  73. char *base;
  74. /* Value of expression_obstack saved at entry to this level. */
  75. struct obstack *obstack;
  76. };
  77. struct momentary_level *momentary_stack;
  78. /* Table indexed by tree code giving a string containing a character
  79. classifying the tree code. Possibilities are
  80. t, d, s, c, r and e. See tree.def for details. */
  81. #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
  82. char *tree_code_type[] = {
  83. #include "tree.def"
  84. };
  85. #undef DEFTREECODE
  86. /* Table indexed by tree code giving number of expression
  87. operands beyond the fixed part of the node structure.
  88. Not used for types or decls. */
  89. #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
  90. int tree_code_length[] = {
  91. #include "tree.def"
  92. };
  93. #undef DEFTREECODE
  94. /* Counter for assigning unique ids to all tree nodes. */
  95. int tree_node_counter = 0;
  96. /* Hash table for uniquizing IDENTIFIER_NODEs by name. */
  97. #define MAX_HASH_TABLE 1009
  98. static tree hash_table[MAX_HASH_TABLE]; /* id hash buckets */
  99. /* Init data for node creation, at the beginning of compilation. */
  100. void
  101. init_tree ()
  102. {
  103. obstack_init (&permanent_obstack);
  104. obstack_init (&temporary_obstack);
  105. temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
  106. obstack_init (&momentary_obstack);
  107. momentary_firstobj = (char *) obstack_alloc (&momentary_obstack, 0);
  108. obstack_init (&maybepermanent_obstack);
  109. maybepermanent_firstobj
  110. = (char *) obstack_alloc (&maybepermanent_obstack, 0);
  111. current_obstack = &permanent_obstack;
  112. expression_obstack = &permanent_obstack;
  113. rtl_obstack = saveable_obstack = &permanent_obstack;
  114. tree_node_counter = 1;
  115. bzero (hash_table, sizeof hash_table);
  116. }
  117. /* Start allocating on the temporary (per function) obstack.
  118. This is done in start_function before parsing the function body,
  119. and before each initialization at top level, and to go back
  120. to temporary allocation after doing end_temporary_allocation. */
  121. void
  122. temporary_allocation ()
  123. {
  124. current_obstack = &temporary_obstack;
  125. expression_obstack = &temporary_obstack;
  126. rtl_obstack = saveable_obstack = &maybepermanent_obstack;
  127. momentary_stack = 0;
  128. }
  129. /* Start allocating on the permanent obstack but don't
  130. free the temporary data. After calling this, call
  131. `permanent_allocation' to fully resume permanent allocation status. */
  132. void
  133. end_temporary_allocation ()
  134. {
  135. current_obstack = &permanent_obstack;
  136. expression_obstack = &permanent_obstack;
  137. rtl_obstack = saveable_obstack = &permanent_obstack;
  138. }
  139. /* Go back to allocating on the permanent obstack
  140. and free everything in the temporary obstack.
  141. This is done in finish_function after fully compiling a function. */
  142. void
  143. permanent_allocation ()
  144. {
  145. /* Free up previous temporary obstack data */
  146. obstack_free (&temporary_obstack, temporary_firstobj);
  147. obstack_free (&momentary_obstack, momentary_firstobj);
  148. obstack_free (&maybepermanent_obstack, maybepermanent_firstobj);
  149. current_obstack = &permanent_obstack;
  150. expression_obstack = &permanent_obstack;
  151. rtl_obstack = saveable_obstack = &permanent_obstack;
  152. }
  153. /* Save permanently everything on the maybepermanent_obstack. */
  154. void
  155. preserve_data ()
  156. {
  157. maybepermanent_firstobj
  158. = (char *) obstack_alloc (&maybepermanent_obstack, 0);
  159. }
  160. /* Allocate SIZE bytes in the current obstack
  161. and return a pointer to them.
  162. In practice the current obstack is always the temporary one. */
  163. char *
  164. oballoc (size)
  165. int size;
  166. {
  167. return (char *) obstack_alloc (current_obstack, size);
  168. }
  169. /* Free the object PTR in the current obstack
  170. as well as everything allocated since PTR.
  171. In practice the current obstack is always the temporary one. */
  172. void
  173. obfree (ptr)
  174. char *ptr;
  175. {
  176. obstack_free (current_obstack, ptr);
  177. }
  178. /* Allocate SIZE bytes in the permanent obstack
  179. and return a pointer to them. */
  180. char *
  181. permalloc (size)
  182. long size;
  183. {
  184. return (char *) obstack_alloc (&permanent_obstack, size);
  185. }
  186. /* Start a level of momentary allocation.
  187. In C, each compound statement has its own level
  188. and that level is freed at the end of each statement.
  189. All expression nodes are allocated in the momentary allocation level. */
  190. void
  191. push_momentary ()
  192. {
  193. struct momentary_level *tem
  194. = (struct momentary_level *) obstack_alloc (&momentary_obstack,
  195. sizeof (struct momentary_level));
  196. tem->prev = momentary_stack;
  197. tem->base = (char *) obstack_base (&momentary_obstack);
  198. tem->obstack = expression_obstack;
  199. momentary_stack = tem;
  200. expression_obstack = &momentary_obstack;
  201. }
  202. /* Free all the storage in the current momentary-allocation level.
  203. In C, this happens at the end of each statement. */
  204. void
  205. clear_momentary ()
  206. {
  207. obstack_free (&momentary_obstack, momentary_stack->base);
  208. }
  209. /* Discard a level of momentary allocation.
  210. In C, this happens at the end of each compound statement.
  211. Restore the status of expression node allocation
  212. that was in effect before this level was created. */
  213. void
  214. pop_momentary ()
  215. {
  216. struct momentary_level *tem = momentary_stack;
  217. momentary_stack = tem->prev;
  218. obstack_free (&momentary_obstack, tem);
  219. expression_obstack = tem->obstack;
  220. }
  221. /* Call when starting to parse a declaration:
  222. make expressions in the declaration last the length of the function.
  223. Returns an argument that should be passed to resume_momentary later. */
  224. int
  225. suspend_momentary ()
  226. {
  227. register int tem = expression_obstack == &momentary_obstack;
  228. expression_obstack = current_obstack;
  229. return tem;
  230. }
  231. /* Call when finished parsing a declaration:
  232. restore the treatment of node-allocation that was
  233. in effect before the suspension.
  234. YES should be the value previously returned by suspend_momentary. */
  235. void
  236. resume_momentary (yes)
  237. int yes;
  238. {
  239. if (yes)
  240. expression_obstack = &momentary_obstack;
  241. }
  242. /* Return a newly allocated node of code CODE.
  243. Initialize the node's unique id and its TREE_PERMANENT flag.
  244. For decl and type nodes, some other fields are initialized.
  245. The rest of the node is initialized to zero.
  246. Achoo! I got a code in the node. */
  247. tree
  248. make_node (code)
  249. enum tree_code code;
  250. {
  251. register tree t;
  252. register int type = *tree_code_type[(int) code];
  253. register int length;
  254. register struct obstack *obstack = current_obstack;
  255. register int i;
  256. switch (type)
  257. {
  258. case 'd': /* A decl node */
  259. length = sizeof (struct tree_decl);
  260. /* All decls in an inline function need to be saved. */
  261. if (obstack != &permanent_obstack)
  262. obstack = saveable_obstack;
  263. break;
  264. case 't': /* a type node */
  265. length = sizeof (struct tree_type);
  266. /* All data types are put where we can preserve them if nec. */
  267. if (obstack != &permanent_obstack)
  268. obstack = saveable_obstack;
  269. break;
  270. case 's': /* a stmt node */
  271. length = sizeof (struct tree_common)
  272. + 2 * sizeof (int)
  273. + tree_code_length[(int) code] * sizeof (char *);
  274. /* All stmts are put where we can preserve them if nec. */
  275. if (obstack != &permanent_obstack)
  276. obstack = saveable_obstack;
  277. break;
  278. case 'r': /* a reference */
  279. case 'e': /* an expression */
  280. obstack = expression_obstack;
  281. length = sizeof (struct tree_exp)
  282. + (tree_code_length[(int) code] - 1) * sizeof (char *);
  283. break;
  284. case 'c': /* a constant */
  285. obstack = expression_obstack;
  286. /* We can't use tree_code_length for this, since the number of words
  287. is machine-dependent due to varying alignment of `double'. */
  288. if (code == REAL_CST)
  289. {
  290. length = sizeof (struct tree_real_cst);
  291. break;
  292. }
  293. case 'x': /* something random, like an identifier. */
  294. length = sizeof (struct tree_common)
  295. + tree_code_length[(int) code] * sizeof (char *);
  296. /* Identifier nodes are always permanent since they are
  297. unique in a compiler run. */
  298. if (code == IDENTIFIER_NODE) obstack = &permanent_obstack;
  299. }
  300. t = (tree) obstack_alloc (obstack, length);
  301. TREE_UID (t) = tree_node_counter++;
  302. TREE_TYPE (t) = 0;
  303. TREE_CHAIN (t) = 0;
  304. for (i = (length / sizeof (int)) - 1;
  305. i >= sizeof (struct tree_common) / sizeof (int) - 1;
  306. i--)
  307. ((int *) t)[i] = 0;
  308. TREE_SET_CODE (t, code);
  309. if (obstack == &permanent_obstack)
  310. TREE_PERMANENT (t) = 1;
  311. if (type == 'd')
  312. {
  313. extern int lineno;
  314. DECL_ALIGN (t) = 1;
  315. DECL_SIZE_UNIT (t) = 1;
  316. DECL_VOFFSET_UNIT (t) = 1;
  317. DECL_SOURCE_LINE (t) = lineno;
  318. DECL_SOURCE_FILE (t) = input_filename;
  319. }
  320. if (type == 't')
  321. {
  322. TYPE_ALIGN (t) = 1;
  323. TYPE_SIZE_UNIT (t) = 1;
  324. TYPE_MAIN_VARIANT (t) = t;
  325. }
  326. if (type == 'c')
  327. {
  328. TREE_LITERAL (t) = 1;
  329. }
  330. return t;
  331. }
  332. /* Return a new node with the same contents as NODE
  333. except that its TREE_CHAIN is zero and it has a fresh uid. */
  334. tree
  335. copy_node (node)
  336. tree node;
  337. {
  338. register tree t;
  339. register enum tree_code code = TREE_CODE (node);
  340. register int length;
  341. register int i;
  342. switch (*tree_code_type[(int) code])
  343. {
  344. case 'd': /* A decl node */
  345. length = sizeof (struct tree_decl);
  346. break;
  347. case 't': /* a type node */
  348. length = sizeof (struct tree_type);
  349. break;
  350. case 's':
  351. length = sizeof (struct tree_common)
  352. + 2 * sizeof (int)
  353. + tree_code_length[(int) code] * sizeof (char *);
  354. break;
  355. case 'r': /* a reference */
  356. case 'e': /* a expression */
  357. length = sizeof (struct tree_exp)
  358. + (tree_code_length[(int) code] - 1) * sizeof (char *);
  359. break;
  360. case 'c': /* a constant */
  361. /* We can't use tree_code_length for this, since the number of words
  362. is machine-dependent due to varying alignment of `double'. */
  363. if (code == REAL_CST)
  364. {
  365. length = sizeof (struct tree_real_cst);
  366. break;
  367. }
  368. case 'x': /* something random, like an identifier. */
  369. length = sizeof (struct tree_common)
  370. + tree_code_length[(int) code] * sizeof (char *);
  371. }
  372. t = (tree) obstack_alloc (current_obstack, length);
  373. for (i = (length / sizeof (int)) - 1;
  374. i >= 0;
  375. i--)
  376. ((int *) t)[i] = ((int *) node)[i];
  377. TREE_UID (t) = tree_node_counter++;
  378. TREE_CHAIN (t) = 0;
  379. TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
  380. return t;
  381. }
  382. #define HASHBITS 30
  383. /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
  384. If an identifier with that name has previously been referred to,
  385. the same node is returned this time. */
  386. tree
  387. get_identifier (text)
  388. register char *text;
  389. {
  390. register int hi;
  391. register int i;
  392. register tree idp;
  393. register int len;
  394. /* Compute length of text in len. */
  395. for (len = 0; text[len]; len++);
  396. /* Compute hash code */
  397. hi = len;
  398. for (i = 0; i < len; i++)
  399. hi = ((hi * 613) + (unsigned)(text[i]));
  400. hi &= (1 << HASHBITS) - 1;
  401. hi %= MAX_HASH_TABLE;
  402. /* Search table for identifier */
  403. for (idp = hash_table[hi]; idp!=NULL; idp = TREE_CHAIN (idp))
  404. if (IDENTIFIER_LENGTH (idp) == len &&
  405. !strcmp (IDENTIFIER_POINTER (idp), text))
  406. return idp; /* <-- return if found */
  407. /* Not found, create one, add to chain */
  408. idp = make_node (IDENTIFIER_NODE);
  409. IDENTIFIER_LENGTH (idp) = len;
  410. IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
  411. TREE_CHAIN (idp) = hash_table[hi];
  412. hash_table[hi] = idp;
  413. return idp; /* <-- return if created */
  414. }
  415. /* Return a newly constructed INTEGER_CST node whose constant value
  416. is specified by the two ints LOW and HI.
  417. The TREE_TYPE is set to `int'. */
  418. tree
  419. build_int_2 (low, hi)
  420. int low, hi;
  421. {
  422. register tree t = make_node (INTEGER_CST);
  423. TREE_INT_CST_LOW (t) = low;
  424. TREE_INT_CST_HIGH (t) = hi;
  425. TREE_TYPE (t) = integer_type_node;
  426. return t;
  427. }
  428. /* Return a newly constructed REAL_CST node whose value is D.
  429. The TREE_TYPE is not initialized. */
  430. tree
  431. build_real (d)
  432. double d;
  433. {
  434. tree v;
  435. v = make_node (REAL_CST);
  436. TREE_REAL_CST (v) = d;
  437. return v;
  438. }
  439. /* Return a newly constructed REAL_CST node whose value
  440. is the integer value of the INTEGER_CST node I.
  441. The TREE_TYPE is not initialized. */
  442. tree
  443. build_real_from_int_cst (i)
  444. tree i;
  445. {
  446. tree v;
  447. double d;
  448. v = make_node (REAL_CST);
  449. if (TREE_INT_CST_HIGH (i) < 0)
  450. {
  451. d = (double) (~ TREE_INT_CST_HIGH (i));
  452. d *= ((double) (1 << (HOST_BITS_PER_INT / 2))
  453. * (double) (1 << (HOST_BITS_PER_INT / 2)));
  454. d += (double) (unsigned) (~ TREE_INT_CST_LOW (i));
  455. d = (- d - 1.0);
  456. }
  457. else
  458. {
  459. d = (double) TREE_INT_CST_HIGH (i);
  460. d *= ((double) (1 << (HOST_BITS_PER_INT / 2))
  461. * (double) (1 << (HOST_BITS_PER_INT / 2)));
  462. d += (double) (unsigned) TREE_INT_CST_LOW (i);
  463. }
  464. TREE_REAL_CST (v) = d;
  465. return v;
  466. }
  467. /* Return a newly constructed STRING_CST node whose value is
  468. the LEN characters at STR.
  469. The TREE_TYPE is not initialized. */
  470. tree
  471. build_string (len, str)
  472. int len;
  473. char *str;
  474. {
  475. register tree s = make_node (STRING_CST);
  476. TREE_STRING_LENGTH (s) = len;
  477. TREE_STRING_POINTER (s) = obstack_copy0 (saveable_obstack, str, len);
  478. return s;
  479. }
  480. /* Return a newly constructed COMPLEX_CST node whose value is
  481. specified by the real and imaginary parts REAL and IMAG.
  482. Both REAL and IMAG should be constant nodes.
  483. The TREE_TYPE is not initialized. */
  484. tree
  485. build_complex (real, imag)
  486. tree real, imag;
  487. {
  488. register tree t = make_node (COMPLEX_CST);
  489. TREE_REALPART (t) = real;
  490. TREE_IMAGPART (t) = imag;
  491. return t;
  492. }
  493. /* Return 1 if EXPR is the integer constant zero. */
  494. int
  495. integer_zerop (expr)
  496. tree expr;
  497. {
  498. return (TREE_CODE (expr) == INTEGER_CST
  499. && TREE_INT_CST_LOW (expr) == 0
  500. && TREE_INT_CST_HIGH (expr) == 0);
  501. }
  502. /* Return 1 if EXPR is the integer constant one. */
  503. int
  504. integer_onep (expr)
  505. tree expr;
  506. {
  507. return (TREE_CODE (expr) == INTEGER_CST
  508. && TREE_INT_CST_LOW (expr) == 1
  509. && TREE_INT_CST_HIGH (expr) == 0);
  510. }
  511. /* Return 1 if EXPR is an integer containing all 1's
  512. in as much precision as it contains. */
  513. int
  514. integer_all_onesp (expr)
  515. tree expr;
  516. {
  517. register int prec;
  518. register int uns;
  519. if (TREE_CODE (expr) != INTEGER_CST)
  520. return 0;
  521. uns = TREE_UNSIGNED (TREE_TYPE (expr));
  522. if (!uns)
  523. return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
  524. prec = TYPE_PRECISION (TREE_TYPE (expr));
  525. if (prec >= HOST_BITS_PER_INT)
  526. return TREE_INT_CST_LOW (expr) == -1
  527. && TREE_INT_CST_HIGH (expr) == (1 << (prec - HOST_BITS_PER_INT)) - 1;
  528. else
  529. return TREE_INT_CST_LOW (expr) == (1 << prec) - 1;
  530. }
  531. /* Return the length of a chain of nodes chained through TREE_CHAIN.
  532. We expect a null pointer to mark the end of the chain.
  533. This is the Lisp primitive `length'. */
  534. int
  535. list_length (t)
  536. tree t;
  537. {
  538. register tree tail;
  539. register int len = 0;
  540. for (tail = t; tail; tail = TREE_CHAIN (tail))
  541. len++;
  542. return len;
  543. }
  544. /* Concatenate two chains of nodes (chained through TREE_CHAIN)
  545. by modifying the last node in chain 1 to point to chain 2.
  546. This is the Lisp primitive `nconc'. */
  547. tree
  548. chainon (op1, op2)
  549. tree op1, op2;
  550. {
  551. tree t;
  552. if (op1)
  553. {
  554. for (t = op1; TREE_CHAIN (t); t = TREE_CHAIN (t))
  555. if (t == op2) abort (); /* Circularity being created */
  556. TREE_CHAIN (t) = op2;
  557. return op1;
  558. }
  559. else return op2;
  560. }
  561. /* Return a newly created TREE_LIST node whose
  562. purpose and value fields are PARM and VALUE. */
  563. tree
  564. build_tree_list (parm, value)
  565. tree parm, value;
  566. {
  567. register tree t = make_node (TREE_LIST);
  568. TREE_PURPOSE (t) = parm;
  569. TREE_VALUE (t) = value;
  570. return t;
  571. }
  572. /* Return a newly created TREE_LIST node whose
  573. purpose and value fields are PARM and VALUE
  574. and whose TREE_CHAIN is CHAIN. */
  575. tree
  576. tree_cons (purpose, value, chain)
  577. tree purpose, value, chain;
  578. {
  579. register tree node = make_node (TREE_LIST);
  580. TREE_CHAIN (node) = chain;
  581. TREE_PURPOSE (node) = purpose;
  582. TREE_VALUE (node) = value;
  583. return node;
  584. }
  585. /* Same as `tree_cons' but make a permanent object. */
  586. tree
  587. perm_tree_cons (purpose, value, chain)
  588. tree purpose, value, chain;
  589. {
  590. register tree node;
  591. register struct obstack *ambient_obstack = current_obstack;
  592. current_obstack = &permanent_obstack;
  593. node = make_node (TREE_LIST);
  594. TREE_CHAIN (node) = chain;
  595. TREE_PURPOSE (node) = purpose;
  596. TREE_VALUE (node) = value;
  597. current_obstack = ambient_obstack;
  598. return node;
  599. }
  600. /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
  601. tree
  602. tree_last (chain)
  603. register tree chain;
  604. {
  605. register tree next;
  606. if (chain)
  607. while (next = TREE_CHAIN (chain))
  608. chain = next;
  609. return chain;
  610. }
  611. /* Reverse the order of elements in the chain T,
  612. and return the new head of the chain (old last element). */
  613. tree
  614. nreverse (t)
  615. tree t;
  616. {
  617. register tree prev = 0, decl, next;
  618. for (decl = t; decl; decl = next)
  619. {
  620. next = TREE_CHAIN (decl);
  621. TREE_CHAIN (decl) = prev;
  622. prev = decl;
  623. }
  624. return prev;
  625. }
  626. /* Return the size nominally occupied by an object of type TYPE
  627. when it resides in memory. The value is measured in units of bytes,
  628. and its data type is that normally used for type sizes
  629. (which is the first type created by make_signed_type or
  630. make_unsigned_type). */
  631. tree
  632. size_in_bytes (type)
  633. tree type;
  634. {
  635. if (type == error_mark_node)
  636. return integer_zero_node;
  637. if (TYPE_SIZE (type) == 0)
  638. {
  639. incomplete_type_error (0, type);
  640. return integer_zero_node;
  641. }
  642. return convert_units (TYPE_SIZE (type), TYPE_SIZE_UNIT (type),
  643. BITS_PER_UNIT);
  644. }
  645. /* Return the size of TYPE (in bytes) as an integer,
  646. or return -1 if the size can vary. */
  647. int
  648. int_size_in_bytes (type)
  649. tree type;
  650. {
  651. int size;
  652. if (type == error_mark_node)
  653. return 0;
  654. if (TYPE_SIZE (type) == 0)
  655. return -1;
  656. if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
  657. return -1;
  658. size = TREE_INT_CST_LOW (TYPE_SIZE (type)) * TYPE_SIZE_UNIT (type);
  659. return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  660. }
  661. /* Return nonzero if arg is static -- a reference to an object in
  662. static storage. This is not the same as the C meaning of `static'. */
  663. int
  664. staticp (arg)
  665. tree arg;
  666. {
  667. register enum tree_code code = TREE_CODE (arg);
  668. if ((code == VAR_DECL || code == FUNCTION_DECL || code == CONSTRUCTOR)
  669. && (TREE_STATIC (arg) || TREE_EXTERNAL (arg)))
  670. return 1;
  671. if (code == STRING_CST)
  672. return 1;
  673. if (code == COMPONENT_REF)
  674. return staticp (TREE_OPERAND (arg, 0));
  675. if (code == ARRAY_REF)
  676. {
  677. if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
  678. && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
  679. return staticp (TREE_OPERAND (arg, 0));
  680. }
  681. return 0;
  682. }
  683. /* Return nonzero if REF is an lvalue valid for this language.
  684. Lvalues can be assigned, unless they have TREE_READONLY.
  685. Lvalues can have their address taken, unless they have TREE_REGDECL. */
  686. int
  687. lvalue_p (ref)
  688. tree ref;
  689. {
  690. register enum tree_code code = TREE_CODE (ref);
  691. if (language_lvalue_valid (ref))
  692. switch (code)
  693. {
  694. case COMPONENT_REF:
  695. return lvalue_p (TREE_OPERAND (ref, 0));
  696. case STRING_CST:
  697. return 1;
  698. case INDIRECT_REF:
  699. case ARRAY_REF:
  700. case VAR_DECL:
  701. case PARM_DECL:
  702. case RESULT_DECL:
  703. case ERROR_MARK:
  704. if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE)
  705. return 1;
  706. }
  707. return 0;
  708. }
  709. /* Return nonzero if REF is an lvalue valid for this language;
  710. otherwise, print an error message and return zero. */
  711. int
  712. lvalue_or_else (ref, string)
  713. tree ref;
  714. char *string;
  715. {
  716. int win = lvalue_p (ref);
  717. if (! win)
  718. error ("invalid lvalue in %s", string);
  719. return win;
  720. }
  721. /* This should be applied to any node which may be used in more than one place,
  722. but must be evaluated only once. Normally, the code generator would
  723. reevaluate the node each time; this forces it to compute it once and save
  724. the result. This is done by encapsulating the node in a SAVE_EXPR. */
  725. tree
  726. save_expr (expr)
  727. tree expr;
  728. {
  729. register tree t = fold (expr);
  730. /* If the tree evaluates to a constant, then we don't want to hide that
  731. fact (i.e. this allows further folding, and direct checks for constants).
  732. Since it is no problem to reevaluate literals, we just return the
  733. literal node. */
  734. if (TREE_LITERAL (t) || TREE_READONLY (t) || TREE_CODE (t) == SAVE_EXPR)
  735. return t;
  736. return build (SAVE_EXPR, TREE_TYPE (expr), t, NULL);
  737. }
  738. /* Stabilize a reference so that we can use it any number of times
  739. without causing its operands to be evaluated more than once.
  740. Returns the stabilized reference.
  741. Also allows conversion expressions whose operands are references.
  742. Any other kind of expression is returned unchanged. */
  743. tree
  744. stabilize_reference (ref)
  745. tree ref;
  746. {
  747. register tree result;
  748. register enum tree_code code = TREE_CODE (ref);
  749. switch (code)
  750. {
  751. case VAR_DECL:
  752. case PARM_DECL:
  753. case RESULT_DECL:
  754. result = ref;
  755. break;
  756. case NOP_EXPR:
  757. case CONVERT_EXPR:
  758. case FLOAT_EXPR:
  759. case FIX_TRUNC_EXPR:
  760. case FIX_FLOOR_EXPR:
  761. case FIX_ROUND_EXPR:
  762. case FIX_CEIL_EXPR:
  763. result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
  764. break;
  765. case INDIRECT_REF:
  766. result = build_nt (INDIRECT_REF, save_expr (TREE_OPERAND (ref, 0)));
  767. break;
  768. case COMPONENT_REF:
  769. result = build_nt (COMPONENT_REF,
  770. stabilize_reference (TREE_OPERAND (ref, 0)),
  771. TREE_OPERAND (ref, 1));
  772. break;
  773. case ARRAY_REF:
  774. result = build_nt (ARRAY_REF, stabilize_reference (TREE_OPERAND (ref, 0)),
  775. save_expr (TREE_OPERAND (ref, 1)));
  776. break;
  777. /* If arg isn't a kind of lvalue we recognize, make no change.
  778. Caller should recognize the error for an invalid lvalue. */
  779. default:
  780. return ref;
  781. case ERROR_MARK:
  782. return error_mark_node;
  783. }
  784. TREE_TYPE (result) = TREE_TYPE (ref);
  785. TREE_READONLY (result) = TREE_READONLY (ref);
  786. TREE_VOLATILE (result) = TREE_VOLATILE (ref);
  787. TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
  788. return result;
  789. }
  790. /* Low-level constructors for expressions. */
  791. /* Build an expression of code CODE, data type TYPE,
  792. and operands as specified by the arguments ARG1 and following arguments.
  793. Expressions and reference nodes can be created this way.
  794. Constants, decls, types and misc nodes cannot be. */
  795. tree
  796. build (va_alist)
  797. va_dcl
  798. {
  799. register va_list p;
  800. enum tree_code code;
  801. register tree t;
  802. register int length;
  803. register int i;
  804. va_start (p);
  805. code = va_arg (p, enum tree_code);
  806. t = make_node (code);
  807. length = tree_code_length[(int) code];
  808. TREE_TYPE (t) = va_arg (p, tree);
  809. if (length == 2)
  810. {
  811. /* This is equivalent to the loop below, but faster. */
  812. register tree arg0 = va_arg (p, tree);
  813. register tree arg1 = va_arg (p, tree);
  814. TREE_OPERAND (t, 0) = arg0;
  815. TREE_OPERAND (t, 1) = arg1;
  816. TREE_VOLATILE (t)
  817. = (arg0 && TREE_VOLATILE (arg0)) || (arg1 && TREE_VOLATILE (arg1));
  818. }
  819. else
  820. {
  821. for (i = 0; i < length; i++)
  822. {
  823. register tree operand = va_arg (p, tree);
  824. TREE_OPERAND (t, i) = operand;
  825. if (operand && TREE_VOLATILE (operand))
  826. TREE_VOLATILE (t) = 1;
  827. }
  828. }
  829. va_end (p);
  830. return t;
  831. }
  832. /* Similar except don't specify the TREE_TYPE
  833. and leave the TREE_VOLATILE as 0.
  834. It is permissible for arguments to be null,
  835. or even garbage if their values do not matter. */
  836. tree
  837. build_nt (va_alist)
  838. va_dcl
  839. {
  840. register va_list p;
  841. register enum tree_code code;
  842. register tree t;
  843. register int length;
  844. register int i;
  845. va_start (p);
  846. code = va_arg (p, enum tree_code);
  847. t = make_node (code);
  848. length = tree_code_length[(int) code];
  849. for (i = 0; i < length; i++)
  850. TREE_OPERAND (t, i) = va_arg (p, tree);
  851. va_end (p);
  852. return t;
  853. }
  854. /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
  855. We do NOT enter this node in any sort of symbol table.
  856. layout_decl is used to set up the decl's storage layout.
  857. Other slots are initialized to 0 or null pointers. */
  858. tree
  859. build_decl (code, name, type)
  860. enum tree_code code;
  861. tree name, type;
  862. {
  863. register tree t;
  864. t = make_node (code);
  865. /* if (type == error_mark_node)
  866. type = integer_type_node; */
  867. /* That is not done, deliberately, so that having error_mark_node
  868. as the type can suppress useless errors in the use of this variable. */
  869. DECL_NAME (t) = name;
  870. TREE_TYPE (t) = type;
  871. DECL_ARGUMENTS (t) = NULL_TREE;
  872. DECL_INITIAL (t) = NULL_TREE;
  873. if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
  874. layout_decl (t, 0);
  875. else if (code == FUNCTION_DECL)
  876. DECL_MODE (t) = FUNCTION_MODE;
  877. return t;
  878. }
  879. /* Low-level constructors for statements.
  880. These constructors all expect source file name and line number
  881. as arguments, as well as enough arguments to fill in the data
  882. in the statement node. */
  883. tree
  884. build_goto (filename, line, label)
  885. char *filename;
  886. int line;
  887. tree label;
  888. {
  889. register tree t = make_node (GOTO_STMT);
  890. STMT_SOURCE_FILE (t) = filename;
  891. STMT_SOURCE_LINE (t) = line;
  892. STMT_BODY (t) = label;
  893. return t;
  894. }
  895. tree
  896. build_return (filename, line, arg)
  897. char *filename;
  898. int line;
  899. tree arg;
  900. {
  901. register tree t = make_node (RETURN_STMT);
  902. STMT_SOURCE_FILE (t) = filename;
  903. STMT_SOURCE_LINE (t) = line;
  904. STMT_BODY (t) = arg;
  905. return t;
  906. }
  907. tree
  908. build_expr_stmt (filename, line, expr)
  909. char *filename;
  910. int line;
  911. tree expr;
  912. {
  913. register tree t = make_node (EXPR_STMT);
  914. STMT_SOURCE_FILE (t) = filename;
  915. STMT_SOURCE_LINE (t) = line;
  916. STMT_BODY (t) = expr;
  917. return t;
  918. }
  919. tree
  920. build_if (filename, line, cond, thenclause, elseclause)
  921. char *filename;
  922. int line;
  923. tree cond, thenclause, elseclause;
  924. {
  925. register tree t = make_node (IF_STMT);
  926. STMT_SOURCE_FILE (t) = filename;
  927. STMT_SOURCE_LINE (t) = line;
  928. STMT_COND (t) = cond;
  929. STMT_THEN (t) = thenclause;
  930. STMT_ELSE (t) = elseclause;
  931. return t;
  932. }
  933. tree
  934. build_exit (filename, line, cond)
  935. char *filename;
  936. int line;
  937. tree cond;
  938. {
  939. register tree t = make_node (EXIT_STMT);
  940. STMT_SOURCE_FILE (t) = filename;
  941. STMT_SOURCE_LINE (t) = line;
  942. STMT_BODY (t) = cond;
  943. return t;
  944. }
  945. tree
  946. build_asm_stmt (filename, line, asmcode)
  947. char *filename;
  948. int line;
  949. tree asmcode;
  950. {
  951. register tree t = make_node (ASM_STMT);
  952. STMT_SOURCE_FILE (t) = filename;
  953. STMT_SOURCE_LINE (t) = line;
  954. STMT_BODY (t) = asmcode;
  955. return t;
  956. }
  957. tree
  958. build_case (filename, line, object, cases)
  959. char *filename;
  960. int line;
  961. tree object, cases;
  962. {
  963. register tree t = make_node (CASE_STMT);
  964. STMT_SOURCE_FILE (t) = filename;
  965. STMT_SOURCE_LINE (t) = line;
  966. STMT_CASE_INDEX (t) = object;
  967. STMT_CASE_LIST (t) = cases;
  968. return t;
  969. }
  970. tree
  971. build_let (filename, line, vars, body, supercontext, tags)
  972. char *filename;
  973. int line;
  974. tree vars, body, supercontext, tags;
  975. {
  976. register tree t = make_node (LET_STMT);
  977. STMT_SOURCE_FILE (t) = filename;
  978. STMT_SOURCE_LINE (t) = line;
  979. STMT_VARS (t) = vars;
  980. STMT_BODY (t) = body;
  981. STMT_SUPERCONTEXT (t) = supercontext;
  982. STMT_BIND_SIZE (t) = 0;
  983. STMT_TYPE_TAGS (t) = tags;
  984. return t;
  985. }
  986. tree
  987. build_loop (filename, line, body)
  988. char *filename;
  989. int line;
  990. tree body;
  991. {
  992. register tree t = make_node (LOOP_STMT);
  993. STMT_SOURCE_FILE (t) = filename;
  994. STMT_SOURCE_LINE (t) = line;
  995. STMT_BODY (t) = body;
  996. return t;
  997. }
  998. tree
  999. build_compound (filename, line, body)
  1000. char *filename;
  1001. int line;
  1002. tree body;
  1003. {
  1004. register tree t = make_node (COMPOUND_STMT);
  1005. STMT_SOURCE_FILE (t) = filename;
  1006. STMT_SOURCE_LINE (t) = line;
  1007. STMT_BODY (t) = body;
  1008. return t;
  1009. }
  1010. /* Return a type like TYPE except that its TREE_READONLY is CONSTP
  1011. and its TREE_VOLATILE is VOLATILEP.
  1012. Such variant types already made are recorded so that duplicates
  1013. are not made.
  1014. A variant types should never be used as the type of an expression.
  1015. Always copy the variant information into the TREE_READONLY
  1016. and TREE_VOLATILE of the expression, and then give the expression
  1017. as its type the "main variant", the variant whose TREE_READONLY
  1018. and TREE_VOLATILE are zero. Use TYPE_MAIN_VARIANT to find the
  1019. main variant. */
  1020. tree
  1021. build_type_variant (type, constp, volatilep)
  1022. tree type;
  1023. int constp, volatilep;
  1024. {
  1025. register tree t, m = TYPE_MAIN_VARIANT (type);
  1026. register struct obstack *ambient_obstack = current_obstack;
  1027. /* Treat any nonzero argument as 1. */
  1028. constp = !!constp;
  1029. volatilep = !!volatilep;
  1030. /* First search the chain variants for one that is what we want. */
  1031. for (t = m; t; t = TYPE_NEXT_VARIANT (t))
  1032. if (constp == TREE_READONLY (t)
  1033. && volatilep == TREE_VOLATILE (t))
  1034. return t;
  1035. /* We need a new one. */
  1036. current_obstack
  1037. = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
  1038. t = copy_node (type);
  1039. TREE_READONLY (t) = constp;
  1040. TREE_VOLATILE (t) = volatilep;
  1041. TYPE_POINTER_TO (t) = 0;
  1042. /* Add this type to the chain of variants of TYPE. */
  1043. TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
  1044. TYPE_NEXT_VARIANT (m) = t;
  1045. current_obstack = ambient_obstack;
  1046. return t;
  1047. }
  1048. /* Hashing of types so that we don't make duplicates.
  1049. The entry point is `type_hash_canon'. */
  1050. /* Each hash table slot is a bucket containing a chain
  1051. of these structures. */
  1052. struct type_hash
  1053. {
  1054. struct type_hash *next; /* Next structure in the bucket. */
  1055. int hashcode; /* Hash code of this type. */
  1056. tree type; /* The type recorded here. */
  1057. };
  1058. /* Now here is the hash table. When recording a type, it is added
  1059. to the slot whose index is the hash code mod the table size.
  1060. Note that the hash table is used for several kinds of types
  1061. (function types, array types and array index range types, for now).
  1062. While all these live in the same table, they are completely independent,
  1063. and the hash code is computed differently for each of these. */
  1064. #define TYPE_HASH_SIZE 29
  1065. struct type_hash *type_hash_table[TYPE_HASH_SIZE];
  1066. /* Here is how primitive or already-canonicalized types' hash
  1067. codes are made. */
  1068. #define TYPE_HASH(TYPE) TREE_UID (TYPE)
  1069. /* Compute a hash code for a list of types (chain of TREE_LIST nodes
  1070. with types in the TREE_VALUE slots), by adding the hash codes
  1071. of the individual types. */
  1072. int
  1073. type_hash_list (list)
  1074. tree list;
  1075. {
  1076. register int hashcode;
  1077. register tree tail;
  1078. for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
  1079. hashcode += TYPE_HASH (TREE_VALUE (tail));
  1080. return hashcode;
  1081. }
  1082. /* Look in the type hash table for a type isomorphic to TYPE.
  1083. If one is found, return it. Otherwise return 0. */
  1084. tree
  1085. type_hash_lookup (hashcode, type)
  1086. int hashcode;
  1087. tree type;
  1088. {
  1089. register struct type_hash *h;
  1090. for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
  1091. if (h->hashcode == hashcode
  1092. && TREE_CODE (h->type) == TREE_CODE (type)
  1093. && TREE_TYPE (h->type) == TREE_TYPE (type)
  1094. && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
  1095. || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
  1096. TYPE_MAX_VALUE (type)))
  1097. && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
  1098. || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
  1099. TYPE_MIN_VALUE (type)))
  1100. && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
  1101. || (TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
  1102. && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
  1103. && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
  1104. return h->type;
  1105. return 0;
  1106. }
  1107. /* Add an entry to the type-hash-table
  1108. for a type TYPE whose hash code is HASHCODE. */
  1109. void
  1110. type_hash_add (hashcode, type)
  1111. int hashcode;
  1112. tree type;
  1113. {
  1114. register struct type_hash *h;
  1115. h = (struct type_hash *) oballoc (sizeof (struct type_hash));
  1116. h->hashcode = hashcode;
  1117. h->type = type;
  1118. h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
  1119. type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
  1120. }
  1121. /* Given TYPE, and HASHCODE its hash code, return the canonical
  1122. object for an identical type if one already exists.
  1123. Otherwise, return TYPE, and record it as the canonical object
  1124. if it is a permanent object.
  1125. To use this function, first create a type of the sort you want.
  1126. Then compute its hash code from the fields of the type that
  1127. make it different from other similar types.
  1128. Then call this function and use the value.
  1129. This function frees the type you pass in if it is a duplicate. */
  1130. /* Set to 1 to debug without canonicalization. Never set by program. */
  1131. int debug_no_type_hash = 0;
  1132. tree
  1133. type_hash_canon (hashcode, type)
  1134. int hashcode;
  1135. tree type;
  1136. {
  1137. tree t1;
  1138. if (debug_no_type_hash)
  1139. return type;
  1140. t1 = type_hash_lookup (hashcode, type);
  1141. if (t1 != 0)
  1142. {
  1143. struct obstack *o
  1144. = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
  1145. obstack_free (o, type);
  1146. return t1;
  1147. }
  1148. /* If this is a new type, record it for later reuse. */
  1149. if (current_obstack == &permanent_obstack)
  1150. type_hash_add (hashcode, type);
  1151. return type;
  1152. }
  1153. /* Given two lists of types
  1154. (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
  1155. return 1 if the lists contain the same types in the same order. */
  1156. int
  1157. type_list_equal (l1, l2)
  1158. tree l1, l2;
  1159. {
  1160. register tree t1, t2;
  1161. for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
  1162. if (TREE_VALUE (t1) != TREE_VALUE (t2))
  1163. return 0;
  1164. return t1 == t2;
  1165. }
  1166. /* Nonzero if integer constants T1 and T2
  1167. represent the same constant value. */
  1168. int
  1169. tree_int_cst_equal (t1, t2)
  1170. tree t1, t2;
  1171. {
  1172. if (t1 == t2)
  1173. return 1;
  1174. if (t1 == 0 || t2 == 0)
  1175. return 0;
  1176. if (TREE_CODE (t1) == INTEGER_CST
  1177. && TREE_CODE (t2) == INTEGER_CST
  1178. && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
  1179. && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
  1180. return 1;
  1181. return 0;
  1182. }
  1183. /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
  1184. The precise way of comparison depends on their data type. */
  1185. int
  1186. tree_int_cst_lt (t1, t2)
  1187. tree t1, t2;
  1188. {
  1189. if (t1 == t2)
  1190. return 0;
  1191. if (!TREE_UNSIGNED (TREE_TYPE (t1)))
  1192. return INT_CST_LT (t1, t2);
  1193. return INT_CST_LT_UNSIGNED (t1, t2);
  1194. }
  1195. /* Constructors for pointer, array and function types.
  1196. (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
  1197. constructed by language-dependent code, not here.) */
  1198. /* Construct, lay out and return the type of pointers to TO_TYPE.
  1199. If such a type has already been constructed, reuse it. */
  1200. tree
  1201. build_pointer_type (to_type)
  1202. tree to_type;
  1203. {
  1204. register tree t = TYPE_POINTER_TO (to_type);
  1205. register struct obstack *ambient_obstack = current_obstack;
  1206. /* First, if we already have a type for pointers to TO_TYPE, use it. */
  1207. if (t)
  1208. return t;
  1209. /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
  1210. current_obstack = (TREE_PERMANENT (to_type)
  1211. ? &permanent_obstack
  1212. : saveable_obstack);
  1213. t = make_node (POINTER_TYPE);
  1214. TREE_TYPE (t) = to_type;
  1215. /* Record this type as the pointer to TO_TYPE. */
  1216. TYPE_POINTER_TO (to_type) = t;
  1217. /* Lay out the type. This function has many callers that are concerned
  1218. with expression-construction, and this simplifies them all.
  1219. Also, it guarantees the TYPE_SIZE is permanent if the type is. */
  1220. layout_type (t);
  1221. current_obstack = ambient_obstack;
  1222. return t;
  1223. }
  1224. /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
  1225. and number of elements specified by the range of values of INDEX_TYPE.
  1226. If such a type has already been constructed, reuse it. */
  1227. tree
  1228. build_array_type (elt_type, index_type)
  1229. tree elt_type, index_type;
  1230. {
  1231. register tree t = make_node (ARRAY_TYPE);
  1232. int hashcode;
  1233. if (TREE_CODE (elt_type) == FUNCTION_TYPE)
  1234. {
  1235. error ("arrays of functions are not meaningful");
  1236. elt_type = integer_type_node;
  1237. }
  1238. TREE_TYPE (t) = elt_type;
  1239. TYPE_DOMAIN (t) = index_type;
  1240. /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
  1241. build_pointer_type (elt_type);
  1242. if (index_type == 0)
  1243. return t;
  1244. hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
  1245. t = type_hash_canon (hashcode, t);
  1246. if (TYPE_SIZE (t) == 0)
  1247. layout_type (t);
  1248. return t;
  1249. }
  1250. /* Construct, lay out and return
  1251. the type of functions returning type VALUE_TYPE
  1252. given arguments of types ARG_TYPES.
  1253. ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
  1254. are data type nodes for the arguments of the function.
  1255. If such a type has already been constructed, reuse it. */
  1256. tree
  1257. build_function_type (value_type, arg_types)
  1258. tree value_type, arg_types;
  1259. {
  1260. register tree t;
  1261. int hashcode;
  1262. if (TREE_CODE (value_type) == FUNCTION_TYPE
  1263. || TREE_CODE (value_type) == ARRAY_TYPE)
  1264. {
  1265. error ("function return type cannot be function or array");
  1266. value_type = integer_type_node;
  1267. }
  1268. /* Make a node of the sort we want. */
  1269. t = make_node (FUNCTION_TYPE);
  1270. TREE_TYPE (t) = value_type;
  1271. TYPE_ARG_TYPES (t) = arg_types;
  1272. /* If we already have such a type, use the old one and free this one. */
  1273. hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
  1274. t = type_hash_canon (hashcode, t);
  1275. if (TYPE_SIZE (t) == 0)
  1276. layout_type (t);
  1277. return t;
  1278. }
  1279. /* Return OP, stripped of any conversions to wider types as much as is safe.
  1280. Converting the value back to OP's type makes a value equivalent to OP.
  1281. If FOR_TYPE is nonzero, we return a value which, if converted to
  1282. type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
  1283. If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
  1284. narrowest type that can hold the value, even if they don't exactly fit.
  1285. Otherwise, bit-field references are changed to a narrower type
  1286. only if they can be fetched directly from memory in that type.
  1287. OP must have integer, real or enumeral type. Pointers are not allowed!
  1288. There are some cases where the obvious value we could return
  1289. would regenerate to OP if converted to OP's type,
  1290. but would not extend like OP to wider types.
  1291. If FOR_TYPE indicates such extension is contemplated, we eschew such values.
  1292. For example, if OP is (unsigned short)(signed char)-1,
  1293. we avoid returning (signed char)-1 if FOR_TYPE is int,
  1294. even though extending that to an unsigned short would regenerate OP,
  1295. since the result of extending (signed char)-1 to (int)
  1296. is different from (int) OP. */
  1297. tree
  1298. get_unwidened (op, for_type)
  1299. register tree op;
  1300. tree for_type;
  1301. {
  1302. /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
  1303. /* TYPE_PRECISION is safe in place of type_precision since
  1304. pointer types are not allowed. */
  1305. register tree type = TREE_TYPE (op);
  1306. register int final_prec = TYPE_PRECISION (for_type != 0 ? for_type : type);
  1307. register int uns
  1308. = (for_type != 0 && for_type != type
  1309. && final_prec > TYPE_PRECISION (type)
  1310. && TREE_UNSIGNED (type));
  1311. register tree win = op;
  1312. while (TREE_CODE (op) == NOP_EXPR)
  1313. {
  1314. register int bitschange
  1315. = TYPE_PRECISION (TREE_TYPE (op))
  1316. - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
  1317. /* Truncations are many-one so cannot be removed.
  1318. Unless we are later going to truncate down even farther. */
  1319. if (bitschange < 0
  1320. && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
  1321. break;
  1322. /* See what's inside this conversion. If we decide to strip it,
  1323. we will set WIN. */
  1324. op = TREE_OPERAND (op, 0);
  1325. /* If we have not stripped any zero-extensions (uns is 0),
  1326. we can strip any kind of extension.
  1327. If we have previously stripped a zero-extension,
  1328. only zero-extensions can safely be stripped.
  1329. Any extension can be stripped if the bits it would produce
  1330. are all going to be discarded later by truncating to FOR_TYPE. */
  1331. if (bitschange > 0)
  1332. {
  1333. if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
  1334. win = op;
  1335. /* TREE_UNSIGNED says whether this is a zero-extension.
  1336. Let's avoid computing it if it does not affect WIN
  1337. and if UNS will not be needed again. */
  1338. if ((uns || TREE_CODE (op) == NOP_EXPR)
  1339. && TREE_UNSIGNED (TREE_TYPE (op)))
  1340. {
  1341. uns = 1;
  1342. win = op;
  1343. }
  1344. }
  1345. }
  1346. if (TREE_CODE (op) == COMPONENT_REF
  1347. /* Since type_for_size always gives an integer type. */
  1348. && TREE_CODE (type) != REAL_TYPE)
  1349. {
  1350. int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
  1351. * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
  1352. type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
  1353. /* We can get this structure field in the narrowest type it fits in.
  1354. If FOR_TYPE is 0, do this only for a field that matches the
  1355. narrower type exactly and is aligned for it (i.e. mode isn't BI).
  1356. The resulting extension to its nominal type (a fullword type)
  1357. must fit the same conditions as for other extensions. */
  1358. if (innerprec <= TYPE_PRECISION (TREE_TYPE (op))
  1359. && (for_type || DECL_MODE (TREE_OPERAND (op, 1)) != BImode)
  1360. && (! uns || final_prec <= innerprec
  1361. || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
  1362. && type != 0)
  1363. {
  1364. win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
  1365. TREE_OPERAND (op, 1));
  1366. TREE_VOLATILE (win) = TREE_VOLATILE (op);
  1367. TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
  1368. }
  1369. }
  1370. return win;
  1371. }
  1372. /* Return OP or a simpler expression for a narrower value
  1373. which can be sign-extended or zero-extended to give back OP.
  1374. Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
  1375. or 0 if the value should be sign-extended. */
  1376. tree
  1377. get_narrower (op, unsignedp_ptr)
  1378. register tree op;
  1379. int *unsignedp_ptr;
  1380. {
  1381. register int uns = 0;
  1382. int first = 1;
  1383. register tree win = op;
  1384. while (TREE_CODE (op) == NOP_EXPR)
  1385. {
  1386. register int bitschange
  1387. = TYPE_PRECISION (TREE_TYPE (op))
  1388. - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
  1389. /* Truncations are many-one so cannot be removed. */
  1390. if (bitschange < 0)
  1391. break;
  1392. /* See what's inside this conversion. If we decide to strip it,
  1393. we will set WIN. */
  1394. op = TREE_OPERAND (op, 0);
  1395. if (bitschange > 0)
  1396. {
  1397. /* An extension: the outermost one can be stripped,
  1398. but remember whether it is zero or sign extension. */
  1399. if (first)
  1400. uns = TREE_UNSIGNED (TREE_TYPE (op));
  1401. /* Otherwise, if a sign extension has been stripped,
  1402. only sign extensions can now be stripped;
  1403. if a zero extension has been stripped, only zero-extensions. */
  1404. else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
  1405. break;
  1406. first = 0;
  1407. }
  1408. /* A change in nominal type can always be stripped. */
  1409. win = op;
  1410. }
  1411. if (TREE_CODE (op) == COMPONENT_REF
  1412. /* Since type_for_size always gives an integer type. */
  1413. && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
  1414. {
  1415. int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
  1416. * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
  1417. tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
  1418. /* We can get this structure field in a narrower type that fits it,
  1419. but the resulting extension to its nominal type (a fullword type)
  1420. must satisfy the same conditions as for other extensions.
  1421. Do this only for fields that are aligned (not BImode),
  1422. because when bit-field insns will be used there is no
  1423. advantage in doing this. */
  1424. if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
  1425. && DECL_MODE (TREE_OPERAND (op, 1)) != BImode
  1426. && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
  1427. && type != 0)
  1428. {
  1429. if (first)
  1430. uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
  1431. win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
  1432. TREE_OPERAND (op, 1));
  1433. TREE_VOLATILE (win) = TREE_VOLATILE (op);
  1434. TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
  1435. }
  1436. }
  1437. *unsignedp_ptr = uns;
  1438. return win;
  1439. }
  1440. /* Return the precision of a type, for arithmetic purposes.
  1441. Supports all types on which arithmetic is possible
  1442. (including pointer types).
  1443. It's not clear yet what will be right for complex types. */
  1444. int
  1445. type_precision (type)
  1446. register tree type;
  1447. {
  1448. return ((TREE_CODE (type) == INTEGER_TYPE
  1449. || TREE_CODE (type) == ENUMERAL_TYPE
  1450. || TREE_CODE (type) == REAL_TYPE)
  1451. ? TYPE_PRECISION (type) : POINTER_SIZE);
  1452. }
  1453. /* Nonzero if integer constant C has a value that is permissible
  1454. for type TYPE (an INTEGER_TYPE). */
  1455. int
  1456. int_fits_type_p (c, type)
  1457. tree c, type;
  1458. {
  1459. if (TREE_UNSIGNED (type))
  1460. return (!INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
  1461. && !INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type)));
  1462. else
  1463. return (!INT_CST_LT (TYPE_MAX_VALUE (type), c)
  1464. && !INT_CST_LT (c, TYPE_MIN_VALUE (type)));
  1465. }
  1466. /* Subroutines of `convert'. */
  1467. /* Change of width--truncation and extension of integers or reals--
  1468. is represented with NOP_EXPR. Proper functioning of many things
  1469. assumes that no other conversions can be NOP_EXPRs.
  1470. Conversion between integer and pointer is represented with CONVERT_EXPR.
  1471. Converting integer to real uses FLOAT_EXPR
  1472. and real to integer uses FIX_TRUNC_EXPR. */
  1473. static tree
  1474. convert_to_pointer (type, expr)
  1475. tree type, expr;
  1476. {
  1477. register tree intype = TREE_TYPE (expr);
  1478. register enum tree_code form = TREE_CODE (intype);
  1479. if (integer_zerop (expr))
  1480. {
  1481. if (type == TREE_TYPE (null_pointer_node))
  1482. return null_pointer_node;
  1483. expr = build_int_2 (0, 0);
  1484. TREE_TYPE (expr) = type;
  1485. return expr;
  1486. }
  1487. if (form == POINTER_TYPE)
  1488. return build (NOP_EXPR, type, expr);
  1489. if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  1490. {
  1491. if (type_precision (intype) == POINTER_SIZE)
  1492. return build (CONVERT_EXPR, type, expr);
  1493. return convert_to_pointer (type,
  1494. convert (type_for_size (POINTER_SIZE, 0),
  1495. expr));
  1496. }
  1497. error ("cannot convert to a pointer type");
  1498. return null_pointer_node;
  1499. }
  1500. /* The result of this is always supposed to be a newly created tree node
  1501. not in use in any existing structure. */
  1502. static tree
  1503. convert_to_integer (type, expr)
  1504. tree type, expr;
  1505. {
  1506. register tree intype = TREE_TYPE (expr);
  1507. register enum tree_code form = TREE_CODE (intype);
  1508. extern tree build_binary_op_nodefault ();
  1509. extern tree build_unary_op ();
  1510. if (form == POINTER_TYPE)
  1511. {
  1512. if (integer_zerop (expr))
  1513. expr = integer_zero_node;
  1514. else
  1515. expr = fold (build (CONVERT_EXPR,
  1516. type_for_size (POINTER_SIZE, 0), expr));
  1517. intype = TREE_TYPE (expr);
  1518. form = TREE_CODE (intype);
  1519. if (intype == type)
  1520. return expr;
  1521. }
  1522. if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  1523. {
  1524. register int outprec = TYPE_PRECISION (type);
  1525. register int inprec = TYPE_PRECISION (intype);
  1526. register enum tree_code ex_form = TREE_CODE (expr);
  1527. if (outprec >= inprec)
  1528. return build (NOP_EXPR, type, expr);
  1529. /* Here detect when we can distribute the truncation down past some arithmetic.
  1530. For example, if adding two longs and converting to an int,
  1531. we can equally well convert both to ints and then add.
  1532. For the operations handled here, such truncation distribution
  1533. is always safe.
  1534. It is desirable in these cases:
  1535. 1) when truncating down to full-word from a larger size
  1536. 2) when truncating takes no work.
  1537. 3) when at least one operand of the arithmetic has been extended
  1538. (as by C's default conversions). In this case we need two conversions
  1539. if we do the arithmetic as already requested, so we might as well
  1540. truncate both and then combine. Perhaps that way we need only one.
  1541. Note that in general we cannot do the arithmetic in a type
  1542. shorter than the desired result of conversion, even if the operands
  1543. are both extended from a shorter type, because they might overflow
  1544. if combined in that type. The exceptions to this--the times when
  1545. two narrow values can be combined in their narrow type even to
  1546. make a wider result--are handled by "shorten" in build_binary_op. */
  1547. switch (ex_form)
  1548. {
  1549. case RSHIFT_EXPR:
  1550. /* We can pass truncation down through right shifting
  1551. when the shift count is a negative constant. */
  1552. if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  1553. || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) > 0)
  1554. break;
  1555. goto trunc1;
  1556. case LSHIFT_EXPR:
  1557. /* We can pass truncation down through left shifting
  1558. when the shift count is a positive constant. */
  1559. if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  1560. || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) < 0)
  1561. break;
  1562. /* In this case, shifting is like multiplication. */
  1563. case PLUS_EXPR:
  1564. case MINUS_EXPR:
  1565. case MULT_EXPR:
  1566. case MAX_EXPR:
  1567. case MIN_EXPR:
  1568. case BIT_AND_EXPR:
  1569. case BIT_IOR_EXPR:
  1570. case BIT_XOR_EXPR:
  1571. case BIT_ANDTC_EXPR:
  1572. trunc1:
  1573. {
  1574. tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  1575. tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  1576. if (outprec >= BITS_PER_WORD
  1577. || TRULY_NOOP_TRUNCATION (outprec, inprec)
  1578. || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
  1579. || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
  1580. {
  1581. /* Do the arithmetic in type TYPEX,
  1582. then convert result to TYPE. */
  1583. register tree typex = type;
  1584. /* Can't do arithmetic in enumeral types
  1585. so use an integer type that will hold the values. */
  1586. if (TREE_CODE (typex) == ENUMERAL_TYPE)
  1587. typex = type_for_size (TYPE_PRECISION (typex),
  1588. TREE_UNSIGNED (typex));
  1589. /* But now perhaps TYPEX is as wide as INPREC.
  1590. In that case, do nothing special here.
  1591. (Otherwise would recurse infinitely in convert. */
  1592. if (TYPE_PRECISION (typex) != inprec)
  1593. {
  1594. /* Don't do unsigned arithmetic where signed was wanted,
  1595. or vice versa. */
  1596. typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  1597. ? unsigned_type (typex) : signed_type (typex));
  1598. return convert (type,
  1599. build_binary_op_nodefault (ex_form,
  1600. convert (typex, arg0),
  1601. convert (typex, arg1)));
  1602. }
  1603. }
  1604. }
  1605. break;
  1606. case EQ_EXPR:
  1607. case NE_EXPR:
  1608. case GT_EXPR:
  1609. case GE_EXPR:
  1610. case LT_EXPR:
  1611. case LE_EXPR:
  1612. case TRUTH_AND_EXPR:
  1613. case TRUTH_OR_EXPR:
  1614. case TRUTH_NOT_EXPR:
  1615. /* If we want result of comparison converted to a byte,
  1616. we can just regard it as a byte, since it is 0 or 1. */
  1617. TREE_TYPE (expr) = type;
  1618. return expr;
  1619. case NEGATE_EXPR:
  1620. case BIT_NOT_EXPR:
  1621. case ABS_EXPR:
  1622. {
  1623. register tree typex = type;
  1624. /* Can't do arithmetic in enumeral types
  1625. so use an integer type that will hold the values. */
  1626. if (TREE_CODE (typex) == ENUMERAL_TYPE)
  1627. typex = type_for_size (TYPE_PRECISION (typex),
  1628. TREE_UNSIGNED (typex));
  1629. /* But now perhaps TYPEX is as wide as INPREC.
  1630. In that case, do nothing special here.
  1631. (Otherwise would recurse infinitely in convert. */
  1632. if (TYPE_PRECISION (typex) != inprec)
  1633. {
  1634. /* Don't do unsigned arithmetic where signed was wanted,
  1635. or vice versa. */
  1636. typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  1637. ? unsigned_type (typex) : signed_type (typex));
  1638. return convert (type,
  1639. build_unary_op (ex_form,
  1640. convert (typex, TREE_OPERAND (expr, 0)),
  1641. 1));
  1642. }
  1643. }
  1644. case NOP_EXPR:
  1645. /* If truncating after truncating, might as well do all at once.
  1646. If truncating after extending, we may get rid of wasted work. */
  1647. return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
  1648. }
  1649. return build (NOP_EXPR, type, expr);
  1650. }
  1651. if (form == REAL_TYPE)
  1652. return build (FIX_TRUNC_EXPR, type, expr);
  1653. error ("aggregate value used where an integer was expected");
  1654. {
  1655. register tree tem = build_int_2 (0, 0);
  1656. TREE_TYPE (tem) = type;
  1657. return tem;
  1658. }
  1659. }
  1660. static tree
  1661. convert_to_real (type, expr)
  1662. tree type, expr;
  1663. {
  1664. register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
  1665. extern int flag_float_store;
  1666. if (form == REAL_TYPE)
  1667. return build (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
  1668. type, expr);
  1669. if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  1670. return build (FLOAT_EXPR, type, expr);
  1671. if (form == POINTER_TYPE)
  1672. error ("pointer value used where a float was expected");
  1673. else
  1674. error ("aggregate value used where a float was expected");
  1675. {
  1676. register tree tem = make_node (REAL_CST);
  1677. TREE_TYPE (tem) = type;
  1678. TREE_REAL_CST (tem) = 0;
  1679. return tem;
  1680. }
  1681. }
  1682. /* Create an expression whose value is that of EXPR,
  1683. converted to type TYPE. The TREE_TYPE of the value
  1684. is always TYPE. This function implements all reasonable
  1685. conversions; callers should filter out those that are
  1686. not permitted by the language being compiled. */
  1687. tree
  1688. convert (type, expr)
  1689. tree type, expr;
  1690. {
  1691. register tree e = expr;
  1692. register enum tree_code code = TREE_CODE (type);
  1693. if (type == TREE_TYPE (expr) || TREE_CODE (expr) == ERROR_MARK)
  1694. return expr;
  1695. if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
  1696. return error_mark_node;
  1697. if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
  1698. {
  1699. error ("void value not ignored as it ought to be");
  1700. return error_mark_node;
  1701. }
  1702. if (code == VOID_TYPE)
  1703. return build (CONVERT_EXPR, type, e);
  1704. #if 0
  1705. /* This is incorrect. A truncation can't be stripped this way.
  1706. Extensions will be stripped by the use of get_unwidened. */
  1707. if (TREE_CODE (expr) == NOP_EXPR)
  1708. return convert (type, TREE_OPERAND (expr, 0));
  1709. #endif
  1710. if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
  1711. return fold (convert_to_integer (type, e));
  1712. if (code == POINTER_TYPE)
  1713. return fold (convert_to_pointer (type, e));
  1714. if (code == REAL_TYPE)
  1715. return fold (convert_to_real (type, e));
  1716. error ("conversion to non-scalar type requested");
  1717. return error_mark_node;
  1718. }