varasm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /* Output variables, constants and external declarations, for 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 handles generation of all the assembler code
  18. *except* the instructions of a function.
  19. This includes declarations of variables and their initial values.
  20. We also output the assembler code for constants stored in memory
  21. and are responsible for combining constants with the same value. */
  22. #include <stdio.h>
  23. #include <stab.h>
  24. #include "config.h"
  25. #include "tree.h"
  26. #include "c-tree.h"
  27. #include "rtl.h"
  28. #include "obstack.h"
  29. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  30. extern FILE *asm_out_file;
  31. extern struct obstack *current_obstack;
  32. extern struct obstack permanent_obstack;
  33. #define obstack_chunk_alloc xmalloc
  34. extern int xmalloc ();
  35. /* Number for making the label on the next
  36. constant that is stored in memory. */
  37. int const_labelno;
  38. /* Number for making the label on the next
  39. static variable internal to a function. */
  40. int var_labelno;
  41. extern FILE *asm_out_file;
  42. static char *compare_constant_1 ();
  43. static void record_constant_1 ();
  44. /* Output a string of literal assembler code
  45. for an `asm' keyword used between functions. */
  46. void
  47. assemble_asm (string)
  48. tree string;
  49. {
  50. fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
  51. }
  52. /* Output assembler code to make an external function name available.
  53. DECL is a FUNCTION_DECL. */
  54. void
  55. assemble_function (decl)
  56. tree decl;
  57. {
  58. if (TREE_PUBLIC (decl))
  59. fprintf (asm_out_file, ".globl _%s\n",
  60. IDENTIFIER_POINTER (DECL_NAME (decl)));
  61. }
  62. /* Create the rtx to represent a function in calls to it.
  63. DECL is a FUNCTION_DECL node which describes which function.
  64. The rtl is stored in DECL. */
  65. void
  66. make_function_rtl (decl)
  67. tree decl;
  68. {
  69. DECL_RTL (decl)
  70. = gen_rtx (MEM, DECL_MODE (decl),
  71. gen_rtx (SYMBOL_REF, Pmode,
  72. IDENTIFIER_POINTER (DECL_NAME (decl))));
  73. }
  74. /* Return size in bytes of an object of type TYPE.
  75. This size must be a constant. */
  76. static int
  77. static_size_in_bytes (type)
  78. register tree type;
  79. {
  80. register int size;
  81. if (! TREE_LITERAL (TYPE_SIZE (type)))
  82. abort ();
  83. size = TREE_INT_CST_LOW (TYPE_SIZE (type)) * TYPE_SIZE_UNIT (type);
  84. return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  85. }
  86. /* Assemble everything that is needed for a variable declaration DECL.
  87. The variable must be in static storage or else external.
  88. TOP_LEVEL is nonzero if this variable has file scope. */
  89. assemble_variable (decl, top_level)
  90. tree decl;
  91. int top_level;
  92. {
  93. register char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
  94. register int i;
  95. /* Handle variables declared as undefined structure types. */
  96. if (TREE_TYPE (decl) == error_mark_node)
  97. {
  98. DECL_RTL (decl) = gen_rtx (MEM, BLKmode, const0_rtx);
  99. return;
  100. }
  101. /* Can't use just the variable's own name for a variable
  102. whose scope is less than the whole file.
  103. Concatenate a distinguishing number. */
  104. if (!top_level && !TREE_EXTERNAL (decl))
  105. {
  106. int labelno = var_labelno++;
  107. char *label;
  108. label = (char *) alloca (strlen (name) + 10);
  109. sprintf (label, "%s$%d", name, labelno);
  110. name = obstack_copy0 (current_obstack, label, strlen (label));
  111. }
  112. DECL_RTL (decl) = gen_rtx (MEM, DECL_MODE (decl),
  113. gen_rtx (SYMBOL_REF, Pmode, name));
  114. /* No need to say anything for externals,
  115. since assembler considers all undefined symbols external. */
  116. if (TREE_EXTERNAL (decl))
  117. return;
  118. if (DECL_INITIAL (decl) == 0)
  119. {
  120. if (! TREE_LITERAL (DECL_SIZE (decl)))
  121. abort ();
  122. if (TREE_PUBLIC (decl))
  123. fprintf (asm_out_file, ".comm _%s,%d\n", name,
  124. TREE_INT_CST_LOW (DECL_SIZE (decl)) * DECL_SIZE_UNIT (decl)
  125. / BITS_PER_UNIT);
  126. else
  127. fprintf (asm_out_file, ".lcomm _%s,%d\n", name,
  128. TREE_INT_CST_LOW (DECL_SIZE (decl)) * DECL_SIZE_UNIT (decl)
  129. / BITS_PER_UNIT);
  130. return;
  131. }
  132. if (TREE_PUBLIC (decl))
  133. fprintf (asm_out_file, ".globl _%s\n", name);
  134. output_addressed_constants (DECL_INITIAL (decl));
  135. if (TREE_READONLY (decl) && ! TREE_VOLATILE (decl))
  136. fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
  137. else
  138. fprintf (asm_out_file, "%s\n", DATA_SECTION_ASM_OP);
  139. for (i = 0; DECL_ALIGN (decl) >= BITS_PER_UNIT << (i + 1); i++);
  140. ASM_OUTPUT_ALIGN (asm_out_file, i);
  141. fprintf (asm_out_file, "_%s:\n", name);
  142. output_constant (DECL_INITIAL (decl), static_size_in_bytes (TREE_TYPE (decl)));
  143. }
  144. /* Here we combine duplicate floating constants to make
  145. CONST_DOUBLE rtx's, and force those out to memory when necessary. */
  146. /* Chain of all CONST_DOUBLE rtx's constructed for the current function.
  147. They are chained through the third operand slot. */
  148. extern rtx real_constant_chain;
  149. /* Return a CONST_DOUBLE rtx for a value specified by EXP,
  150. which must be a REAL_CST tree node. Make only one CONST_DOUBLE
  151. for each distinct value. */
  152. rtx
  153. immed_real_const (exp)
  154. tree exp;
  155. {
  156. register rtx r;
  157. union {double d; int i[2];} u;
  158. register int i0, i1;
  159. register enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
  160. /* Get the desired `double' value as two ints
  161. since that is how they are stored in a CONST_DOUBLE. */
  162. u.d = TREE_REAL_CST (exp);
  163. i0 = u.i[0];
  164. i1 = u.i[1];
  165. /* Search the chain for an existing CONST_DOUBLE with the right value.
  166. If one is found, return it. */
  167. for (r = real_constant_chain; r; r = XEXP (r, 3))
  168. if (XINT (r, 0) == i0 && XINT (r, 1) == i1
  169. && GET_MODE (r) == mode)
  170. return r;
  171. /* No; make a new one and add it to the chain. */
  172. r = gen_rtx (CONST_DOUBLE, mode, i0, i1, 0);
  173. XEXP (r, 3) = real_constant_chain;
  174. real_constant_chain = r;
  175. /* Associate exp and the rtx with each other. */
  176. TREE_CST_RTL (exp) = r;
  177. XEXP (r, 4) = (rtx) exp;
  178. /* Store const0_rtx in slot 2 just so most things won't barf.
  179. Actual use of slot 2 is only through force_const_double_mem. */
  180. XEXP (r, 2) = const0_rtx;
  181. return r;
  182. }
  183. /* Given a CONST_DOUBLE, cause a constant in memory to be created
  184. (unless we already have one for the same value)
  185. and return a MEM rtx to refer to it. */
  186. rtx
  187. force_const_double_mem (r)
  188. rtx r;
  189. {
  190. if (XEXP (r, 2) == const0_rtx)
  191. {
  192. output_constant_def (XEXP (r, 4));
  193. XEXP (r, 2) = TREE_CST_RTL ((tree) XEXP (r, 4));
  194. }
  195. return XEXP (r, 2);
  196. }
  197. /* Given an expression EXP with a constant value,
  198. reduce it to the sum of an assembler symbol and an integer.
  199. Store them both in the structure *VALUE.
  200. Abort if EXP does not reduce. */
  201. struct addr_const
  202. {
  203. rtx base;
  204. int offset;
  205. };
  206. static void
  207. decode_addr_const (exp, value)
  208. tree exp;
  209. struct addr_const *value;
  210. {
  211. register tree target = TREE_OPERAND (exp, 0);
  212. register int offset = 0;
  213. register rtx x;
  214. while (TREE_CODE (target) == COMPONENT_REF)
  215. {
  216. offset += DECL_OFFSET (TREE_OPERAND (target, 1)) / BITS_PER_UNIT;
  217. target = TREE_OPERAND (target, 0);
  218. }
  219. if (TREE_CODE (target) == VAR_DECL
  220. || TREE_CODE (target) == FUNCTION_DECL)
  221. x = DECL_RTL (target);
  222. else if (TREE_LITERAL (target))
  223. x = TREE_CST_RTL (target);
  224. else
  225. abort ();
  226. if (GET_CODE (x) != MEM)
  227. abort ();
  228. x = XEXP (x, 0);
  229. value->base = x;
  230. value->offset = offset;
  231. }
  232. /* Uniquize all constants that appear in memory.
  233. Each constant in memory thus far output is recorded
  234. in `hash_table' with a `struct constant_descriptor'
  235. that contains a polish representation of the value of
  236. the constant.
  237. We cannot store the trees in the hash table
  238. because the trees may be temporary. */
  239. struct constant_descriptor
  240. {
  241. struct constant_descriptor *next;
  242. int labelno;
  243. char contents[1];
  244. };
  245. #define HASHBITS 30
  246. #define MAX_HASH_TABLE 1007
  247. static struct constant_descriptor *hash_table[MAX_HASH_TABLE];
  248. /* Compute a hash code for a constant expression. */
  249. int
  250. const_hash (exp)
  251. tree exp;
  252. {
  253. register char *p;
  254. register int len, hi, i;
  255. register enum tree_code code = TREE_CODE (exp);
  256. if (code == INTEGER_CST)
  257. {
  258. p = (char *) &TREE_INT_CST_LOW (exp);
  259. len = 2 * sizeof TREE_INT_CST_LOW (exp);
  260. }
  261. else if (code == REAL_CST)
  262. {
  263. p = (char *) &TREE_REAL_CST (exp);
  264. len = sizeof TREE_REAL_CST (exp);
  265. }
  266. else if (code == STRING_CST)
  267. p = TREE_STRING_POINTER (exp), len = TREE_STRING_LENGTH (exp);
  268. else if (code == COMPLEX_CST)
  269. return const_hash (TREE_REALPART (exp)) * 5
  270. + const_hash (TREE_IMAGPART (exp));
  271. else if (code == CONSTRUCTOR)
  272. {
  273. register tree link;
  274. hi = 5;
  275. for (link = TREE_OPERAND (exp, 0); link; link = TREE_CHAIN (link))
  276. hi = (hi * 603 + const_hash (TREE_VALUE (link))) % MAX_HASH_TABLE;
  277. return hi;
  278. }
  279. else if (code == ADDR_EXPR)
  280. {
  281. struct addr_const value;
  282. decode_addr_const (exp, &value);
  283. p = (char *) &value;
  284. len = sizeof value;
  285. }
  286. else if (code == PLUS_EXPR || code == MINUS_EXPR)
  287. return const_hash (TREE_OPERAND (exp, 0)) * 5
  288. + const_hash (TREE_OPERAND (exp, 1));
  289. /* Compute hashing function */
  290. hi = len;
  291. for (i = 0; i < len; i++)
  292. hi = ((hi * 613) + (unsigned)(p[i]));
  293. hi &= (1 << HASHBITS) - 1;
  294. hi %= MAX_HASH_TABLE;
  295. return hi;
  296. }
  297. /* Compare a constant expression EXP with a constant-descriptor DESC.
  298. Return 1 if DESC describes a constant with the same value as EXP. */
  299. static int
  300. compare_constant (exp, desc)
  301. tree exp;
  302. struct constant_descriptor *desc;
  303. {
  304. return 0 != compare_constant_1 (exp, desc->contents);
  305. }
  306. /* Compare constant expression EXP with a substring P of a constant descriptor.
  307. If they match, return a pointer to the end of the substring matched.
  308. If they do not match, return 0.
  309. Since descriptors are written in polish prefix notation,
  310. this function can be used recursively to test one operand of EXP
  311. against a subdescriptor, and if it succeeds it returns the
  312. address of the subdescriptor for the next operand. */
  313. static char *
  314. compare_constant_1 (exp, p)
  315. tree exp;
  316. char *p;
  317. {
  318. register char *strp;
  319. register int len;
  320. register enum tree_code code = TREE_CODE (exp);
  321. if (code != (enum tree_code) *p++)
  322. return 0;
  323. if (code == INTEGER_CST)
  324. {
  325. strp = (char *) &TREE_INT_CST_LOW (exp);
  326. len = 2 * sizeof TREE_INT_CST_LOW (exp);
  327. }
  328. else if (code == REAL_CST)
  329. {
  330. /* Real constants are the same only if the same width of type. */
  331. if (*p++ != TYPE_PRECISION (TREE_TYPE (exp)))
  332. return 0;
  333. strp = (char *) &TREE_REAL_CST (exp);
  334. len = sizeof TREE_REAL_CST (exp);
  335. }
  336. else if (code == STRING_CST)
  337. {
  338. strp = TREE_STRING_POINTER (exp);
  339. len = TREE_STRING_LENGTH (exp);
  340. if (bcmp (&TREE_STRING_LENGTH (exp), p,
  341. sizeof TREE_STRING_LENGTH (exp)))
  342. return 0;
  343. p += sizeof TREE_STRING_LENGTH (exp);
  344. }
  345. else if (code == COMPLEX_CST)
  346. {
  347. p = compare_constant_1 (TREE_REALPART (exp), p);
  348. if (p == 0) return 0;
  349. p = compare_constant_1 (TREE_IMAGPART (exp), p);
  350. return p;
  351. }
  352. else if (code == CONSTRUCTOR)
  353. {
  354. register tree link;
  355. for (link = TREE_OPERAND (exp, 0); link; link = TREE_CHAIN (link))
  356. if ((p = compare_constant_1 (TREE_VALUE (link), p)) == 0)
  357. return 0;
  358. return p;
  359. }
  360. else if (code == ADDR_EXPR)
  361. {
  362. struct addr_const value;
  363. decode_addr_const (exp, &value);
  364. strp = (char *) &value;
  365. len = sizeof value;
  366. }
  367. else if (code == PLUS_EXPR || code == MINUS_EXPR)
  368. {
  369. if (*p++ != (char) code)
  370. return 0;
  371. p = compare_constant_1 (TREE_OPERAND (exp, 0), p);
  372. if (p == 0) return 0;
  373. p = compare_constant_1 (TREE_OPERAND (exp, 1), p);
  374. return p;
  375. }
  376. /* Compare constant contents. */
  377. while (--len >= 0)
  378. if (*p++ != *strp++)
  379. return 0;
  380. return p;
  381. }
  382. /* Construct a constant descriptor for the expression EXP.
  383. It is up to the caller to enter the descriptor in the hash table. */
  384. static struct constant_descriptor *
  385. record_constant (exp)
  386. tree exp;
  387. {
  388. struct constant_descriptor *ptr = 0;
  389. int buf;
  390. obstack_grow (&permanent_obstack, &ptr, sizeof ptr);
  391. obstack_grow (&permanent_obstack, &buf, sizeof buf);
  392. record_constant_1 (exp);
  393. return (struct constant_descriptor *) obstack_finish (&permanent_obstack);
  394. }
  395. /* Add a description of constant expression EXP
  396. to the object growing in `permanent_obstack'.
  397. No need to return its address; the caller will get that
  398. from the obstack when the object is complete. */
  399. static void
  400. record_constant_1 (exp)
  401. tree exp;
  402. {
  403. register char *strp;
  404. register int len;
  405. register enum tree_code code = TREE_CODE (exp);
  406. obstack_1grow (&permanent_obstack, (unsigned char) code);
  407. if (code == INTEGER_CST)
  408. {
  409. strp = (char *) &TREE_INT_CST_LOW (exp);
  410. len = 2 * sizeof TREE_INT_CST_LOW (exp);
  411. }
  412. else if (code == REAL_CST)
  413. {
  414. obstack_1grow (&permanent_obstack, TYPE_PRECISION (TREE_TYPE (exp)));
  415. strp = (char *) &TREE_REAL_CST (exp);
  416. len = sizeof TREE_REAL_CST (exp);
  417. }
  418. else if (code == STRING_CST)
  419. {
  420. strp = TREE_STRING_POINTER (exp);
  421. len = TREE_STRING_LENGTH (exp);
  422. obstack_grow (&permanent_obstack, (char *) &TREE_STRING_LENGTH (exp),
  423. sizeof TREE_STRING_LENGTH (exp));
  424. }
  425. else if (code == COMPLEX_CST)
  426. {
  427. record_constant_1 (TREE_REALPART (exp));
  428. record_constant_1 (TREE_IMAGPART (exp));
  429. return;
  430. }
  431. else if (code == CONSTRUCTOR)
  432. {
  433. register tree link;
  434. int length = list_length (TREE_OPERAND (exp, 0));
  435. obstack_grow (&permanent_obstack, (char *) length, sizeof length);
  436. for (link = TREE_OPERAND (exp, 0); link; link = TREE_CHAIN (link))
  437. record_constant_1 (TREE_VALUE (link));
  438. return;
  439. }
  440. else if (code == ADDR_EXPR)
  441. {
  442. struct addr_const value;
  443. decode_addr_const (exp, &value);
  444. strp = (char *) &value;
  445. len = sizeof value;
  446. }
  447. else if (code == PLUS_EXPR || code == MINUS_EXPR)
  448. {
  449. obstack_1grow (&permanent_obstack, (char) code);
  450. record_constant_1 (TREE_OPERAND (exp, 0));
  451. record_constant_1 (TREE_OPERAND (exp, 1));
  452. return;
  453. }
  454. /* Record constant contents. */
  455. obstack_grow (&permanent_obstack, strp, len);
  456. }
  457. /* Return the constant-label-number for constant value EXP.
  458. If no constant equal to EXP has yet been output,
  459. define a new label and output assembler code for it.
  460. The hash_table records which constants already have label numbers. */
  461. int
  462. get_or_assign_labelno (exp)
  463. tree exp;
  464. {
  465. register int hash, i;
  466. register struct constant_descriptor *desc;
  467. /* Make sure any other constants whose addresses appear in EXP
  468. are assigned label numbers. */
  469. output_addressed_constants (exp);
  470. /* Compute hash code of EXP. Search the descriptors for that hash code
  471. to see if any of them describes EXP. If yes, the descriptor records
  472. the label number already assigned. */
  473. hash = const_hash (exp) % MAX_HASH_TABLE;
  474. for (desc = hash_table[hash]; desc; desc = desc->next)
  475. if (compare_constant (exp, desc))
  476. return desc->labelno;
  477. /* No constant equal to EXP is known to have been output.
  478. Make a constant descriptor to enter EXP in the hash table.
  479. Assign the label number and record it in the descriptor for
  480. future calls to this function to find. */
  481. desc = record_constant (exp);
  482. desc->next = hash_table[hash];
  483. hash_table[hash] = desc;
  484. desc->labelno = const_labelno++;
  485. /* Now output assembler code to define that label
  486. and follow it with the data of EXP. */
  487. /* First switch to text segment. */
  488. fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
  489. /* Align the location counter as required by EXP's data type. */
  490. for (i = 0; TYPE_ALIGN (TREE_TYPE (exp)) >= BITS_PER_UNIT << (i + 1); i++);
  491. ASM_OUTPUT_ALIGN (asm_out_file, i);
  492. /* Output the label itself. */
  493. fprintf (asm_out_file, "LC%d:\n", desc->labelno);
  494. /* Output the value of EXP. */
  495. output_constant (exp,
  496. (TREE_CODE (exp) == STRING_CST
  497. ? TREE_STRING_LENGTH (exp)
  498. : static_size_in_bytes (TREE_TYPE (exp))));
  499. return desc->labelno;
  500. }
  501. /* Return an rtx representing a reference to constant data in memory
  502. for the constant expression EXP.
  503. If assembler code for such a constant has already been output,
  504. return an rtx to refer to it.
  505. Otherwise, output such a constant in memory and generate
  506. an rtx for it. The TREE_CST_RTL of EXP is set up to point to that rtx. */
  507. rtx
  508. output_constant_def (exp)
  509. tree exp;
  510. {
  511. register rtx def;
  512. register int labelno;
  513. char *labelstr;
  514. char label[10];
  515. if (TREE_CST_RTL (exp))
  516. return TREE_CST_RTL (exp);
  517. labelno = get_or_assign_labelno (exp);
  518. sprintf (label, "*LC%d", labelno);
  519. labelstr = obstack_copy0 (current_obstack, label, strlen (label));
  520. def = gen_rtx (SYMBOL_REF, Pmode, labelstr);
  521. TREE_CST_RTL (exp)
  522. = gen_rtx (MEM, TYPE_MODE (TREE_TYPE (exp)), def);
  523. return def;
  524. }
  525. /* Find all the constants whose addresses are referenced inside of EXP,
  526. and make sure assembler code with a label has been output for each one. */
  527. output_addressed_constants (exp)
  528. tree exp;
  529. {
  530. if (TREE_CODE (exp) == ADDR_EXPR)
  531. {
  532. register tree constant = TREE_OPERAND (exp, 0);
  533. while (TREE_CODE (constant) == COMPONENT_REF)
  534. {
  535. constant = TREE_OPERAND (constant, 0);
  536. }
  537. if (TREE_LITERAL (constant))
  538. /* No need to do anything here
  539. for addresses of variables or functions. */
  540. output_constant_def (constant);
  541. }
  542. else if (TREE_CODE (exp) == PLUS_EXPR
  543. || TREE_CODE (exp) == MINUS_EXPR)
  544. {
  545. output_addressed_constants (TREE_OPERAND (exp, 0));
  546. output_addressed_constants (TREE_OPERAND (exp, 1));
  547. }
  548. else if (TREE_CODE (exp) == NOP_EXPR)
  549. {
  550. output_addressed_constants (TREE_OPERAND (exp, 0));
  551. }
  552. else if (TREE_CODE (exp) == CONSTRUCTOR)
  553. {
  554. register tree link;
  555. for (link = TREE_OPERAND (exp, 0); link; link = TREE_CHAIN (link))
  556. output_addressed_constants (TREE_VALUE (link));
  557. }
  558. else if (! TREE_LITERAL (exp))
  559. abort ();
  560. }
  561. /* Output assembler code for constant EXP to FILE, with no label.
  562. This includes the pseudo-op such as ".int" or ".byte", and a newline.
  563. Assumes output_addressed_constants has been done on EXP already.
  564. Generate exactly SIZE bytes of assembler data, padding at the end
  565. with zeros if necessary. SIZE must always be specified.
  566. SIZE is important for structure constructors,
  567. since trailing members may have been omitted from the constructor.
  568. It is also important for initialization of arrays from string constants
  569. since the full length of the string constant might not be wanted.
  570. It is also needed for initialization of unions, where the initializer's
  571. type is just one member, and that may not be as long as the union.
  572. There a case in which we would fail to output exactly SIZE bytes:
  573. for a structure constructor that wants to produce more than SIZE bytes.
  574. But such constructors will never be generated for any possible input. */
  575. output_constant (exp, size)
  576. register tree exp;
  577. register int size;
  578. {
  579. register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
  580. if (size == 0)
  581. return;
  582. switch (code)
  583. {
  584. case INTEGER_TYPE:
  585. case ENUMERAL_TYPE:
  586. case POINTER_TYPE:
  587. if (--size == 0)
  588. fprintf (asm_out_file, "\t%s", ASM_CHAR_OP);
  589. else if (--size < 2)
  590. fprintf (asm_out_file, "\t%s", ASM_SHORT_OP);
  591. else
  592. {
  593. fprintf (asm_out_file, "\t%s", ASM_INT_OP);
  594. size -= 2;
  595. }
  596. output_arith_constant (exp);
  597. fprintf (asm_out_file, "\n");
  598. break;
  599. case REAL_TYPE:
  600. if (TREE_CODE (exp) != REAL_CST)
  601. yyerror ("initializer for floating value is not a floating constant");
  602. if (size < 4)
  603. break;
  604. else if (size < 8)
  605. {
  606. ASM_OUTPUT_FLOAT (asm_out_file, TREE_REAL_CST (exp));
  607. size -= 4;
  608. }
  609. else
  610. {
  611. ASM_OUTPUT_DOUBLE (asm_out_file, TREE_REAL_CST (exp));
  612. size -= 8;
  613. }
  614. break;
  615. case COMPLEX_TYPE:
  616. output_constant (TREE_REALPART (exp), size / 2);
  617. output_constant (TREE_IMAGPART (exp), size / 2);
  618. size -= (size / 2) * 2;
  619. break;
  620. case ARRAY_TYPE:
  621. if (TREE_CODE (exp) == CONSTRUCTOR)
  622. {
  623. output_constructor (exp, size);
  624. return;
  625. }
  626. else if (TREE_CODE (exp) == STRING_CST)
  627. {
  628. register int i;
  629. register unsigned char *p
  630. = (unsigned char *) TREE_STRING_POINTER (exp);
  631. int excess = 0;
  632. if (size > TREE_STRING_LENGTH (exp))
  633. {
  634. excess = size - TREE_STRING_LENGTH (exp);
  635. size = TREE_STRING_LENGTH (exp);
  636. }
  637. if (size > 0 && p[size - 1] == 0)
  638. {
  639. fprintf (asm_out_file, "\t.asciz \"");
  640. size--;
  641. }
  642. else
  643. fprintf (asm_out_file, "\t.ascii \"");
  644. for (i = 0; i < size; i++)
  645. {
  646. register int c = p[i];
  647. if (c == '\"' || c == '\\')
  648. putc ('\\', asm_out_file);
  649. if (c >= ' ' && c < 0177)
  650. putc (c, asm_out_file);
  651. else
  652. fprintf (asm_out_file, "\\%03o", c);
  653. }
  654. fprintf (asm_out_file, "\"\n");
  655. size = excess;
  656. }
  657. else
  658. abort ();
  659. break;
  660. case RECORD_TYPE:
  661. if (TREE_CODE (exp) == CONSTRUCTOR)
  662. output_constructor (exp, size);
  663. else
  664. abort ();
  665. return;
  666. }
  667. if (size > 0)
  668. ASM_OUTPUT_SKIP (asm_out_file, size);
  669. }
  670. /* Subroutine of output_constant, used for CONSTRUCTORs
  671. (aggregate constants).
  672. Generate at least SIZE bytes, padding if necessary. */
  673. output_constructor (exp, size)
  674. tree exp;
  675. int size;
  676. {
  677. register tree link, field = 0, elt;
  678. register int byte;
  679. int total_bytes = 0;
  680. int byte_offset = -1;
  681. if (TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
  682. field = TYPE_FIELDS (TREE_TYPE (exp));
  683. /* As LINK goes through the elements of the constant,
  684. FIELD goes through the structure fields, if the constant is a structure.
  685. But the constant could also be an array. Then FIELD is zero. */
  686. for (link = TREE_OPERAND (exp, 0);
  687. link;
  688. link = TREE_CHAIN (link),
  689. field = field ? TREE_CHAIN (field) : 0)
  690. {
  691. if (field == 0
  692. || (DECL_MODE (field) != BImode))
  693. {
  694. register int fieldsize;
  695. /* An element that is not a bit-field.
  696. Output any buffered-up bit-fields preceding it. */
  697. if (byte_offset >= 0)
  698. {
  699. fprintf (asm_out_file, "\t%s0x%x\n", ASM_CHAR_OP, byte);
  700. total_bytes++;
  701. byte_offset = -1;
  702. }
  703. /* Align to this element's alignment,
  704. if it isn't aligned properly by its predecessors. */
  705. if (field && (total_bytes * BITS_PER_UNIT) % DECL_ALIGN (field) != 0)
  706. {
  707. int byte_align = DECL_ALIGN (field) / BITS_PER_UNIT;
  708. int to_byte = (((total_bytes + byte_align - 1) / byte_align)
  709. * byte_align);
  710. ASM_OUTPUT_SKIP (asm_out_file, to_byte - total_bytes);
  711. total_bytes = to_byte;
  712. }
  713. /* Output the element's initial value. */
  714. fieldsize = static_size_in_bytes (field ? TREE_TYPE (field)
  715. : TREE_TYPE (TREE_TYPE (exp)));
  716. output_constant (TREE_VALUE (link), fieldsize);
  717. /* Count its size. */
  718. total_bytes += fieldsize;
  719. }
  720. else if (TREE_CODE (TREE_VALUE (link)) != INTEGER_CST)
  721. yyerror ("invalid initial value for %s field",
  722. IDENTIFIER_POINTER (DECL_NAME (field)));
  723. else
  724. {
  725. /* Element that is a bit-field. */
  726. int next_offset = DECL_OFFSET (field);
  727. int end_offset
  728. = (next_offset
  729. + (TREE_INT_CST_LOW (DECL_SIZE (field))
  730. * DECL_SIZE_UNIT (field)));
  731. /* We must split the element into pieces that fall within
  732. separate bytes, and combine each byte with previous or
  733. following bit-fields. */
  734. /* next_offset is the offset n fbits from the begining of
  735. the structure to the next bit of this element to be processed.
  736. end_offset is the offset of the first bit past the end of
  737. this element. */
  738. while (next_offset < end_offset)
  739. {
  740. int this_time;
  741. int next_byte = next_offset / BITS_PER_UNIT;
  742. int next_bit = next_offset % BITS_PER_UNIT;
  743. if (byte_offset < 0)
  744. {
  745. byte_offset = next_byte;
  746. byte = 0;
  747. }
  748. else
  749. while (next_byte != byte_offset)
  750. {
  751. fprintf (asm_out_file, "\t%s0x%x\n", ASM_CHAR_OP, byte);
  752. byte_offset++;
  753. total_bytes++;
  754. byte = 0;
  755. }
  756. /* Number of bits we can process at once
  757. (all part of the same byte). */
  758. this_time = MIN (end_offset - next_offset,
  759. BITS_PER_UNIT - next_bit);
  760. #ifdef BYTES_BIG_ENDIAN
  761. /* On big-endian machine, take the most significant bits
  762. first (of the bits that are significant)
  763. and put them into bytes from the most significant end. */
  764. byte |= (((TREE_INT_CST_LOW (TREE_VALUE (link))
  765. >> (end_offset - next_offset - this_time))
  766. & ((1 << this_time) - 1))
  767. << (BITS_PER_UNIT - this_time - next_bit));
  768. #else
  769. /* On little-endian machines,
  770. take first the least significant bits of the value
  771. and pack them starting at the least significant
  772. bits of the bytes. */
  773. byte |= ((TREE_INT_CST_LOW (TREE_VALUE (link))
  774. >> (next_offset - DECL_OFFSET (field)))
  775. & ((1 << this_time) - 1)) << next_bit;
  776. #endif
  777. next_offset += this_time;
  778. }
  779. }
  780. }
  781. if (byte_offset >= 0)
  782. {
  783. fprintf (asm_out_file, "\t%s0x%x\n", ASM_CHAR_OP, byte);
  784. byte_offset = -1;
  785. total_bytes++;
  786. }
  787. if (total_bytes < size)
  788. ASM_OUTPUT_SKIP (asm_out_file, size - total_bytes);
  789. }
  790. /* Output an expression in assembler language
  791. to represent the value of EXP. EXP may contain integer constants,
  792. addresses (assembler labels), and addition and subtraction.
  793. Anything else causes a compiler error message, because the
  794. label is probably relocatable so the assembler and linker could
  795. not handle it. */
  796. output_arith_constant (exp)
  797. tree exp;
  798. {
  799. register int size;
  800. switch (TREE_CODE (exp))
  801. {
  802. case NOP_EXPR:
  803. case CONVERT_EXPR:
  804. output_arith_constant (TREE_OPERAND (exp, 0));
  805. break;
  806. case INTEGER_CST:
  807. fprintf (asm_out_file, "%d", TREE_INT_CST_LOW (exp));
  808. break;
  809. case REAL_CST:
  810. case COMPLEX_CST:
  811. case STRING_CST:
  812. abort ();
  813. break;
  814. case ADDR_EXPR:
  815. {
  816. struct addr_const value;
  817. decode_addr_const (exp, &value);
  818. output_addr_const (asm_out_file, value.base);
  819. if (value.offset)
  820. fprintf (asm_out_file, "+%d", value.offset);
  821. }
  822. break;
  823. case PLUS_EXPR:
  824. case MINUS_EXPR:
  825. fprintf (asm_out_file, ASM_OPEN_PAREN);
  826. output_arith_constant (TREE_OPERAND (exp, 0));
  827. if (TREE_CODE (exp) == PLUS_EXPR)
  828. fprintf (asm_out_file, "+");
  829. else
  830. fprintf (asm_out_file, "-");
  831. output_arith_constant (TREE_OPERAND (exp, 1));
  832. fprintf (asm_out_file, ASM_CLOSE_PAREN);
  833. break;
  834. default:
  835. yyerror ("Arithmetic too complicated on address in static initializer");
  836. }
  837. }