varasm.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. /* Output variables, constants and external declarations, for 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 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 "rtl.h"
  26. #include "tree.h"
  27. #include "flags.h"
  28. #include "expr.h"
  29. #include "obstack.h"
  30. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  31. /* File in which assembler code is being written. */
  32. extern FILE *asm_out_file;
  33. extern struct obstack *current_obstack;
  34. extern struct obstack *saveable_obstack;
  35. extern struct obstack permanent_obstack;
  36. #define obstack_chunk_alloc xmalloc
  37. extern int xmalloc ();
  38. /* Number for making the label on the next
  39. constant that is stored in memory. */
  40. int const_labelno;
  41. /* Number for making the label on the next
  42. static variable internal to a function. */
  43. int var_labelno;
  44. extern FILE *asm_out_file;
  45. static char *compare_constant_1 ();
  46. static void record_constant_1 ();
  47. void assemble_name ();
  48. void output_addressed_constants ();
  49. void output_constant ();
  50. void output_constructor ();
  51. /* Output a string of literal assembler code
  52. for an `asm' keyword used between functions. */
  53. void
  54. assemble_asm (string)
  55. tree string;
  56. {
  57. app_enable ();
  58. fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
  59. }
  60. /* Output assembler code associated with defining the name of a function
  61. as described by DECL. */
  62. void
  63. assemble_function (decl)
  64. tree decl;
  65. {
  66. rtx x, n;
  67. char *fnname;
  68. /* Get the function's name, as described by its RTL.
  69. This may be different from the DECL_NAME name used in the source file. */
  70. x = DECL_RTL (decl);
  71. if (GET_CODE (x) != MEM)
  72. abort ();
  73. n = XEXP (x, 0);
  74. if (GET_CODE (n) != SYMBOL_REF)
  75. abort ();
  76. fnname = XSTR (n, 0);
  77. /* The following code does not need preprocessing in the assembler. */
  78. app_disable ();
  79. /* Tell assembler to switch to text segment. */
  80. fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
  81. /* Tell assembler to move to target machine's alignment for functions. */
  82. ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT));
  83. /* Make function name accessible from other files, if appropriate. */
  84. if (TREE_PUBLIC (decl))
  85. ASM_GLOBALIZE_LABEL (asm_out_file, fnname);
  86. /* Do any machine/system dependent processing of the function name */
  87. #ifdef ASM_DECLARE_FUNCTION_NAME
  88. ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname);
  89. #else
  90. /* Standard thing is just output label for the function. */
  91. ASM_OUTPUT_LABEL (asm_out_file, fnname);
  92. #endif /* ASM_DECLARE_FUNCTION_NAME */
  93. }
  94. /* Assemble " .int 0\n" or whatever this assembler wants. */
  95. void
  96. assemble_integer_zero ()
  97. {
  98. ASM_OUTPUT_INT (asm_out_file, const0_rtx);
  99. }
  100. /* Create the rtx to represent a function in calls to it.
  101. DECL is a FUNCTION_DECL node which describes which function.
  102. The rtl is stored in DECL. */
  103. void
  104. make_function_rtl (decl)
  105. tree decl;
  106. {
  107. if (DECL_RTL (decl) == 0)
  108. DECL_RTL (decl)
  109. = gen_rtx (MEM, DECL_MODE (decl),
  110. gen_rtx (SYMBOL_REF, Pmode,
  111. IDENTIFIER_POINTER (DECL_NAME (decl))));
  112. }
  113. /* Assemble everything that is needed for a variable or function declaration.
  114. Not used for automatic variables, and not used for function definitions.
  115. Should not be called for variables of incomplete structure type.
  116. ASMSPEC is the user's specification of assembler symbol name to use.
  117. TOP_LEVEL is nonzero if this variable has file scope.
  118. WRITE_SYMBOLS is 2 if writing dbx symbol output.
  119. The dbx data for a file-scope variable is written here.
  120. AT_END is nonzero if this is the special handling, at end of compilation,
  121. to define things that have had only tentative definitions. */
  122. void
  123. assemble_variable (decl, asmspec, top_level, write_symbols, at_end)
  124. tree decl;
  125. tree asmspec;
  126. int top_level;
  127. int write_symbols;
  128. int at_end;
  129. {
  130. register char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
  131. register int i;
  132. if (asmspec != 0)
  133. {
  134. if (TREE_CODE (asmspec) != STRING_CST)
  135. abort ();
  136. name = (char *) oballoc (strlen (TREE_STRING_POINTER (asmspec)) + 2);
  137. name[0] = '*';
  138. strcpy (&name[1], TREE_STRING_POINTER (asmspec));
  139. }
  140. /* For a duplicate declaration, we can be called twice on the
  141. same DECL node. Don't alter the RTL already made
  142. unless the old mode is wrong (which can happen when
  143. the previous rtl was made when the type was incomplete). */
  144. if (DECL_RTL (decl) == 0
  145. || GET_MODE (DECL_RTL (decl)) != DECL_MODE (decl))
  146. {
  147. if (DECL_RTL (decl) && asmspec == 0)
  148. name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
  149. /* Can't use just the variable's own name for a variable
  150. whose scope is less than the whole file.
  151. Concatenate a distinguishing number. */
  152. else if (!top_level && !TREE_EXTERNAL (decl) && asmspec == 0)
  153. {
  154. char *label;
  155. ASM_FORMAT_PRIVATE_NAME (label, name, var_labelno);
  156. name = obstack_copy0 (current_obstack, label, strlen (label));
  157. var_labelno++;
  158. }
  159. DECL_RTL (decl) = gen_rtx (MEM, DECL_MODE (decl),
  160. gen_rtx (SYMBOL_REF, Pmode, name));
  161. if (TREE_VOLATILE (decl))
  162. DECL_RTL (decl)->volatil = 1;
  163. if (TREE_READONLY (decl))
  164. DECL_RTL (decl)->unchanging = 1;
  165. DECL_RTL (decl)->in_struct
  166. = (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
  167. || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
  168. || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE);
  169. }
  170. /* Output no assembler code for a function declaration.
  171. Only definitions of functions output anything. */
  172. if (TREE_CODE (decl) == FUNCTION_DECL)
  173. return;
  174. /* Normally no need to say anything for external references,
  175. since assembler considers all undefined symbols external. */
  176. if (TREE_EXTERNAL (decl))
  177. {
  178. #ifdef ASM_OUTPUT_EXTERNAL
  179. /* Some systems do require some output. */
  180. ASM_OUTPUT_EXTERNAL (asm_out_file, decl, name);
  181. #endif
  182. return;
  183. }
  184. /* Don't output anything when a tentative file-scope definition is seen.
  185. But at end of compilation, do output code for them. */
  186. if (! at_end && DECL_INITIAL (decl) == 0 && top_level)
  187. return;
  188. /* If type was incomplete when the variable was declared,
  189. see if it is complete now. */
  190. if (DECL_SIZE (decl) == 0)
  191. layout_decl (decl);
  192. /* Still incomplete => don't allocate it; treat the tentative defn
  193. (which is what it must have been) as an `extern' reference. */
  194. if (DECL_SIZE (decl) == 0)
  195. {
  196. error_with_file_and_line (DECL_SOURCE_FILE (decl),
  197. DECL_SOURCE_LINE (decl),
  198. "storage size of static var `%s' isn't known",
  199. IDENTIFIER_POINTER (DECL_NAME (decl)));
  200. return;
  201. }
  202. /* The first declaration of a variable that comes through this function
  203. decides whether it is global (in C, has external linkage)
  204. or local (in C, has internal linkage). So do nothing more
  205. if this function has already run. */
  206. if (TREE_ASM_WRITTEN (decl))
  207. return;
  208. TREE_ASM_WRITTEN (decl) = 1;
  209. if (write_symbols == 2)
  210. dbxout_symbol (decl, 0);
  211. else if (write_symbols != 0)
  212. /* Make sure the file is known to GDB even if it has no functions. */
  213. set_current_gdbfile (DECL_SOURCE_FILE (decl));
  214. /* If storage size is erroneously variable, just continue.
  215. Error message was already made. */
  216. if (! TREE_LITERAL (DECL_SIZE (decl)))
  217. return;
  218. app_disable ();
  219. /* Handle uninitialized definitions. */
  220. if (DECL_INITIAL (decl) == 0)
  221. {
  222. int size = (TREE_INT_CST_LOW (DECL_SIZE (decl))
  223. * DECL_SIZE_UNIT (decl)
  224. / BITS_PER_UNIT);
  225. /* Round size up to multiple of BIGGEST_ALIGNMENT bits
  226. so that each uninitialized object starts on such a boundary. */
  227. size = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
  228. / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
  229. * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
  230. if (TREE_PUBLIC (decl))
  231. ASM_OUTPUT_COMMON (asm_out_file, name, size);
  232. else
  233. ASM_OUTPUT_LOCAL (asm_out_file, name, size);
  234. return;
  235. }
  236. /* Handle initialized definitions. */
  237. if (TREE_PUBLIC (decl))
  238. ASM_GLOBALIZE_LABEL (asm_out_file, name);
  239. output_addressed_constants (DECL_INITIAL (decl));
  240. if (TREE_READONLY (decl) && ! TREE_VOLATILE (decl))
  241. fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
  242. else
  243. fprintf (asm_out_file, "%s\n", DATA_SECTION_ASM_OP);
  244. for (i = 0; DECL_ALIGN (decl) >= BITS_PER_UNIT << (i + 1); i++);
  245. ASM_OUTPUT_ALIGN (asm_out_file, i);
  246. ASM_OUTPUT_LABEL (asm_out_file, name);
  247. output_constant (DECL_INITIAL (decl), int_size_in_bytes (TREE_TYPE (decl)));
  248. }
  249. /* Output to FILE a reference to the assembler name of a C-level name NAME.
  250. If NAME starts with a *, the rest of NAME is output verbatim.
  251. Otherwise NAME is transformed in an implementation-defined way
  252. (usually by the addition of an underscore).
  253. Many macros in the tm file are defined to call this function. */
  254. void
  255. assemble_name (file, name)
  256. FILE *file;
  257. char *name;
  258. {
  259. if (name[0] == '*')
  260. fputs (&name[1], file);
  261. else
  262. ASM_OUTPUT_LABELREF (file, name);
  263. }
  264. /* Here we combine duplicate floating constants to make
  265. CONST_DOUBLE rtx's, and force those out to memory when necessary. */
  266. /* Chain of all CONST_DOUBLE rtx's constructed for the current function.
  267. They are chained through the third operand slot. */
  268. extern rtx real_constant_chain;
  269. /* Return a CONST_DOUBLE for a specified `double' value
  270. and machine mode. */
  271. rtx
  272. immed_real_const_1 (d, mode)
  273. double d;
  274. enum machine_mode mode;
  275. {
  276. register rtx r;
  277. union {double d; int i[2];} u;
  278. register int i0, i1;
  279. /* Get the desired `double' value as two ints
  280. since that is how they are stored in a CONST_DOUBLE. */
  281. u.d = d;
  282. i0 = u.i[0];
  283. i1 = u.i[1];
  284. /* Search the chain for an existing CONST_DOUBLE with the right value.
  285. If one is found, return it. */
  286. for (r = real_constant_chain; r; r = XEXP (r, 3))
  287. if (XINT (r, 0) == i0 && XINT (r, 1) == i1
  288. && GET_MODE (r) == mode)
  289. return r;
  290. /* No; make a new one and add it to the chain. */
  291. r = gen_rtx (CONST_DOUBLE, mode, i0, i1, 0);
  292. XEXP (r, 3) = real_constant_chain;
  293. real_constant_chain = r;
  294. /* Store const0_rtx in slot 2 just so most things won't barf.
  295. Actual use of slot 2 is only through force_const_double_mem. */
  296. XEXP (r, 2) = const0_rtx;
  297. return r;
  298. }
  299. /* Return a CONST_DOUBLE rtx for a value specified by EXP,
  300. which must be a REAL_CST tree node. Make only one CONST_DOUBLE
  301. for each distinct value. */
  302. rtx
  303. immed_real_const (exp)
  304. tree exp;
  305. {
  306. register rtx r;
  307. r = immed_real_const_1 (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)));
  308. /* Associate exp and with this rtl value. */
  309. TREE_CST_RTL (exp) = r;
  310. return r;
  311. }
  312. /* Given a CONST_DOUBLE, cause a constant in memory to be created
  313. (unless we already have one for the same value)
  314. and return a MEM rtx to refer to it. */
  315. rtx
  316. force_const_double_mem (r)
  317. rtx r;
  318. {
  319. if (XEXP (r, 2) == const0_rtx)
  320. {
  321. XEXP (r, 2) = force_const_mem (GET_MODE (r), r);
  322. }
  323. /* XEXP (r, 2) is now a MEM with a constant address.
  324. If that is legitimate, return it.
  325. Othewise it will need reloading, so return a copy of it. */
  326. if (memory_address_p (GET_MODE (r), XEXP (XEXP (r, 2), 0)))
  327. return XEXP (r, 2);
  328. return gen_rtx (MEM, GET_MODE (r), XEXP (XEXP (r, 2), 0));
  329. }
  330. /* At the start of a function, forget the memory-constants
  331. previously made for CONST_DOUBLEs. */
  332. static void
  333. clear_const_double_mem ()
  334. {
  335. register rtx r;
  336. for (r = real_constant_chain; r; r = XEXP (r, 3))
  337. XEXP (r, 2) = 0;
  338. }
  339. /* Given an expression EXP with a constant value,
  340. reduce it to the sum of an assembler symbol and an integer.
  341. Store them both in the structure *VALUE.
  342. Abort if EXP does not reduce. */
  343. struct addr_const
  344. {
  345. rtx base;
  346. int offset;
  347. };
  348. static void
  349. decode_addr_const (exp, value)
  350. tree exp;
  351. struct addr_const *value;
  352. {
  353. register tree target = TREE_OPERAND (exp, 0);
  354. register int offset = 0;
  355. register rtx x;
  356. while (1)
  357. {
  358. if (TREE_CODE (target) == COMPONENT_REF)
  359. {
  360. offset += DECL_OFFSET (TREE_OPERAND (target, 1)) / BITS_PER_UNIT;
  361. target = TREE_OPERAND (target, 0);
  362. }
  363. else if (TREE_CODE (target) == ARRAY_REF)
  364. {
  365. if (TREE_CODE (TREE_OPERAND (target, 1)) != INTEGER_CST
  366. || TREE_CODE (TYPE_SIZE (TREE_TYPE (target))) != INTEGER_CST)
  367. abort ();
  368. offset += ((TYPE_SIZE_UNIT (TREE_TYPE (target))
  369. * TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (target)))
  370. * TREE_INT_CST_LOW (TREE_OPERAND (target, 1)))
  371. / BITS_PER_UNIT);
  372. target = TREE_OPERAND (target, 0);
  373. }
  374. else break;
  375. }
  376. if (TREE_CODE (target) == VAR_DECL
  377. || TREE_CODE (target) == FUNCTION_DECL)
  378. x = DECL_RTL (target);
  379. else if (TREE_LITERAL (target))
  380. x = TREE_CST_RTL (target);
  381. else
  382. abort ();
  383. if (GET_CODE (x) != MEM)
  384. abort ();
  385. x = XEXP (x, 0);
  386. value->base = x;
  387. value->offset = offset;
  388. }
  389. /* Uniquize all constants that appear in memory.
  390. Each constant in memory thus far output is recorded
  391. in `const_hash_table' with a `struct constant_descriptor'
  392. that contains a polish representation of the value of
  393. the constant.
  394. We cannot store the trees in the hash table
  395. because the trees may be temporary. */
  396. struct constant_descriptor
  397. {
  398. struct constant_descriptor *next;
  399. char *label;
  400. char contents[1];
  401. };
  402. #define HASHBITS 30
  403. #define MAX_HASH_TABLE 1007
  404. static struct constant_descriptor *const_hash_table[MAX_HASH_TABLE];
  405. /* Compute a hash code for a constant expression. */
  406. int
  407. const_hash (exp)
  408. tree exp;
  409. {
  410. register char *p;
  411. register int len, hi, i;
  412. register enum tree_code code = TREE_CODE (exp);
  413. if (code == INTEGER_CST)
  414. {
  415. p = (char *) &TREE_INT_CST_LOW (exp);
  416. len = 2 * sizeof TREE_INT_CST_LOW (exp);
  417. }
  418. else if (code == REAL_CST)
  419. {
  420. p = (char *) &TREE_REAL_CST (exp);
  421. len = sizeof TREE_REAL_CST (exp);
  422. }
  423. else if (code == STRING_CST)
  424. p = TREE_STRING_POINTER (exp), len = TREE_STRING_LENGTH (exp);
  425. else if (code == COMPLEX_CST)
  426. return const_hash (TREE_REALPART (exp)) * 5
  427. + const_hash (TREE_IMAGPART (exp));
  428. else if (code == CONSTRUCTOR)
  429. {
  430. register tree link;
  431. hi = 5;
  432. for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
  433. hi = (hi * 603 + const_hash (TREE_VALUE (link))) % MAX_HASH_TABLE;
  434. return hi;
  435. }
  436. else if (code == ADDR_EXPR)
  437. {
  438. struct addr_const value;
  439. decode_addr_const (exp, &value);
  440. p = (char *) &value;
  441. len = sizeof value;
  442. }
  443. else if (code == PLUS_EXPR || code == MINUS_EXPR)
  444. return const_hash (TREE_OPERAND (exp, 0)) * 9
  445. + const_hash (TREE_OPERAND (exp, 1));
  446. else if (code == NOP_EXPR || code == CONVERT_EXPR)
  447. return const_hash (TREE_OPERAND (exp, 0)) * 7 + 2;
  448. /* Compute hashing function */
  449. hi = len;
  450. for (i = 0; i < len; i++)
  451. hi = ((hi * 613) + (unsigned)(p[i]));
  452. hi &= (1 << HASHBITS) - 1;
  453. hi %= MAX_HASH_TABLE;
  454. return hi;
  455. }
  456. /* Compare a constant expression EXP with a constant-descriptor DESC.
  457. Return 1 if DESC describes a constant with the same value as EXP. */
  458. static int
  459. compare_constant (exp, desc)
  460. tree exp;
  461. struct constant_descriptor *desc;
  462. {
  463. return 0 != compare_constant_1 (exp, desc->contents);
  464. }
  465. /* Compare constant expression EXP with a substring P of a constant descriptor.
  466. If they match, return a pointer to the end of the substring matched.
  467. If they do not match, return 0.
  468. Since descriptors are written in polish prefix notation,
  469. this function can be used recursively to test one operand of EXP
  470. against a subdescriptor, and if it succeeds it returns the
  471. address of the subdescriptor for the next operand. */
  472. static char *
  473. compare_constant_1 (exp, p)
  474. tree exp;
  475. char *p;
  476. {
  477. register char *strp;
  478. register int len;
  479. register enum tree_code code = TREE_CODE (exp);
  480. if (code != (enum tree_code) *p++)
  481. return 0;
  482. if (code == INTEGER_CST)
  483. {
  484. strp = (char *) &TREE_INT_CST_LOW (exp);
  485. len = 2 * sizeof TREE_INT_CST_LOW (exp);
  486. }
  487. else if (code == REAL_CST)
  488. {
  489. /* Real constants are the same only if the same width of type. */
  490. if (*p++ != TYPE_PRECISION (TREE_TYPE (exp)))
  491. return 0;
  492. strp = (char *) &TREE_REAL_CST (exp);
  493. len = sizeof TREE_REAL_CST (exp);
  494. }
  495. else if (code == STRING_CST)
  496. {
  497. if (flag_writable_strings)
  498. return 0;
  499. strp = TREE_STRING_POINTER (exp);
  500. len = TREE_STRING_LENGTH (exp);
  501. if (bcmp (&TREE_STRING_LENGTH (exp), p,
  502. sizeof TREE_STRING_LENGTH (exp)))
  503. return 0;
  504. p += sizeof TREE_STRING_LENGTH (exp);
  505. }
  506. else if (code == COMPLEX_CST)
  507. {
  508. p = compare_constant_1 (TREE_REALPART (exp), p);
  509. if (p == 0) return 0;
  510. p = compare_constant_1 (TREE_IMAGPART (exp), p);
  511. return p;
  512. }
  513. else if (code == CONSTRUCTOR)
  514. {
  515. register tree link;
  516. int length = list_length (CONSTRUCTOR_ELTS (exp));
  517. if (bcmp (&length, p, sizeof length))
  518. return 0;
  519. p += sizeof length;
  520. for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
  521. if ((p = compare_constant_1 (TREE_VALUE (link), p)) == 0)
  522. return 0;
  523. return p;
  524. }
  525. else if (code == ADDR_EXPR)
  526. {
  527. struct addr_const value;
  528. decode_addr_const (exp, &value);
  529. strp = (char *) &value;
  530. len = sizeof value;
  531. }
  532. else if (code == PLUS_EXPR || code == MINUS_EXPR)
  533. {
  534. if (*p++ != (char) code)
  535. return 0;
  536. p = compare_constant_1 (TREE_OPERAND (exp, 0), p);
  537. if (p == 0) return 0;
  538. p = compare_constant_1 (TREE_OPERAND (exp, 1), p);
  539. return p;
  540. }
  541. else if (code == NOP_EXPR || code == CONVERT_EXPR)
  542. {
  543. if (*p++ != (char) code)
  544. return 0;
  545. p = compare_constant_1 (TREE_OPERAND (exp, 0), p);
  546. return p;
  547. }
  548. /* Compare constant contents. */
  549. while (--len >= 0)
  550. if (*p++ != *strp++)
  551. return 0;
  552. return p;
  553. }
  554. /* Construct a constant descriptor for the expression EXP.
  555. It is up to the caller to enter the descriptor in the hash table. */
  556. static struct constant_descriptor *
  557. record_constant (exp)
  558. tree exp;
  559. {
  560. struct constant_descriptor *ptr = 0;
  561. int buf;
  562. obstack_grow (&permanent_obstack, &ptr, sizeof ptr);
  563. obstack_grow (&permanent_obstack, &buf, sizeof buf);
  564. record_constant_1 (exp);
  565. return (struct constant_descriptor *) obstack_finish (&permanent_obstack);
  566. }
  567. /* Add a description of constant expression EXP
  568. to the object growing in `permanent_obstack'.
  569. No need to return its address; the caller will get that
  570. from the obstack when the object is complete. */
  571. static void
  572. record_constant_1 (exp)
  573. tree exp;
  574. {
  575. register char *strp;
  576. register int len;
  577. register enum tree_code code = TREE_CODE (exp);
  578. obstack_1grow (&permanent_obstack, (unsigned char) code);
  579. if (code == INTEGER_CST)
  580. {
  581. strp = (char *) &TREE_INT_CST_LOW (exp);
  582. len = 2 * sizeof TREE_INT_CST_LOW (exp);
  583. }
  584. else if (code == REAL_CST)
  585. {
  586. obstack_1grow (&permanent_obstack, TYPE_PRECISION (TREE_TYPE (exp)));
  587. strp = (char *) &TREE_REAL_CST (exp);
  588. len = sizeof TREE_REAL_CST (exp);
  589. }
  590. else if (code == STRING_CST)
  591. {
  592. if (flag_writable_strings)
  593. return;
  594. strp = TREE_STRING_POINTER (exp);
  595. len = TREE_STRING_LENGTH (exp);
  596. obstack_grow (&permanent_obstack, (char *) &TREE_STRING_LENGTH (exp),
  597. sizeof TREE_STRING_LENGTH (exp));
  598. }
  599. else if (code == COMPLEX_CST)
  600. {
  601. record_constant_1 (TREE_REALPART (exp));
  602. record_constant_1 (TREE_IMAGPART (exp));
  603. return;
  604. }
  605. else if (code == CONSTRUCTOR)
  606. {
  607. register tree link;
  608. int length = list_length (CONSTRUCTOR_ELTS (exp));
  609. obstack_grow (&permanent_obstack, (char *) &length, sizeof length);
  610. for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
  611. record_constant_1 (TREE_VALUE (link));
  612. return;
  613. }
  614. else if (code == ADDR_EXPR)
  615. {
  616. struct addr_const value;
  617. decode_addr_const (exp, &value);
  618. strp = (char *) &value;
  619. len = sizeof value;
  620. }
  621. else if (code == PLUS_EXPR || code == MINUS_EXPR)
  622. {
  623. obstack_1grow (&permanent_obstack, (char) code);
  624. record_constant_1 (TREE_OPERAND (exp, 0));
  625. record_constant_1 (TREE_OPERAND (exp, 1));
  626. return;
  627. }
  628. else if (code == NOP_EXPR || code == CONVERT_EXPR)
  629. {
  630. obstack_1grow (&permanent_obstack, (char) code);
  631. record_constant_1 (TREE_OPERAND (exp, 0));
  632. return;
  633. }
  634. /* Record constant contents. */
  635. obstack_grow (&permanent_obstack, strp, len);
  636. }
  637. /* Return the constant-label-string for constant value EXP.
  638. If no constant equal to EXP has yet been output,
  639. define a new label and output assembler code for it.
  640. The const_hash_table records which constants already have label strings. */
  641. static char *
  642. get_or_assign_label (exp)
  643. tree exp;
  644. {
  645. register int hash, i;
  646. register struct constant_descriptor *desc;
  647. char label[10];
  648. /* Make sure any other constants whose addresses appear in EXP
  649. are assigned label numbers. */
  650. output_addressed_constants (exp);
  651. /* Compute hash code of EXP. Search the descriptors for that hash code
  652. to see if any of them describes EXP. If yes, the descriptor records
  653. the label number already assigned. */
  654. hash = const_hash (exp) % MAX_HASH_TABLE;
  655. for (desc = const_hash_table[hash]; desc; desc = desc->next)
  656. if (compare_constant (exp, desc))
  657. return desc->label;
  658. /* No constant equal to EXP is known to have been output.
  659. Make a constant descriptor to enter EXP in the hash table.
  660. Assign the label number and record it in the descriptor for
  661. future calls to this function to find. */
  662. desc = record_constant (exp);
  663. desc->next = const_hash_table[hash];
  664. const_hash_table[hash] = desc;
  665. /* Now output assembler code to define that label
  666. and follow it with the data of EXP. */
  667. /* First switch to text segment, except for writable strings. */
  668. if ((TREE_CODE (exp) == STRING_CST) && flag_writable_strings)
  669. fprintf (asm_out_file, "%s\n", DATA_SECTION_ASM_OP);
  670. else
  671. fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
  672. /* Align the location counter as required by EXP's data type. */
  673. for (i = 0; TYPE_ALIGN (TREE_TYPE (exp)) >= BITS_PER_UNIT << (i + 1); i++);
  674. ASM_OUTPUT_ALIGN (asm_out_file, i);
  675. /* Output the label itself. */
  676. ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LC", const_labelno);
  677. /* Output the value of EXP. */
  678. output_constant (exp,
  679. (TREE_CODE (exp) == STRING_CST
  680. ? TREE_STRING_LENGTH (exp)
  681. : int_size_in_bytes (TREE_TYPE (exp))));
  682. /* Create a string containing the label name, in LABEL. */
  683. ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
  684. ++const_labelno;
  685. desc->label
  686. = (char *) obstack_copy0 (&permanent_obstack, label, strlen (label));
  687. return desc->label;
  688. }
  689. /* Return an rtx representing a reference to constant data in memory
  690. for the constant expression EXP.
  691. If assembler code for such a constant has already been output,
  692. return an rtx to refer to it.
  693. Otherwise, output such a constant in memory and generate
  694. an rtx for it. The TREE_CST_RTL of EXP is set up to point to that rtx. */
  695. rtx
  696. output_constant_def (exp)
  697. tree exp;
  698. {
  699. register rtx def;
  700. char label[10];
  701. if (TREE_CST_RTL (exp))
  702. return TREE_CST_RTL (exp);
  703. def = gen_rtx (SYMBOL_REF, Pmode, get_or_assign_label (exp));
  704. TREE_CST_RTL (exp)
  705. = gen_rtx (MEM, TYPE_MODE (TREE_TYPE (exp)), def);
  706. TREE_CST_RTL (exp)->unchanging = 1;
  707. return TREE_CST_RTL (exp);
  708. }
  709. /* Similar hash facility for making memory-constants
  710. from constant rtl-expressions. It is used on RISC machines
  711. where immediate integer arguments and constant addresses are restricted
  712. so that such constants must be stored in memory.
  713. This pool of constants is reinitialized for each function
  714. so each function gets its own constants-pool that comes right before it. */
  715. #define MAX_RTX_HASH_TABLE 61
  716. static struct constant_descriptor *const_rtx_hash_table[MAX_RTX_HASH_TABLE];
  717. void
  718. init_const_rtx_hash_table ()
  719. {
  720. bzero (const_rtx_hash_table, sizeof const_rtx_hash_table);
  721. clear_const_double_mem ();
  722. }
  723. struct rtx_const
  724. {
  725. enum kind { RTX_DOUBLE, RTX_INT } kind : 16;
  726. enum machine_mode mode : 16;
  727. union {
  728. int d[2];
  729. struct addr_const addr;
  730. } un;
  731. };
  732. /* Express an rtx for a constant integer (perhaps symbolic)
  733. as the sum of a symbol or label plus an explicit integer.
  734. They are stored into VALUE. */
  735. static void
  736. decode_rtx_const (x, value)
  737. rtx x;
  738. struct rtx_const *value;
  739. {
  740. value->kind = RTX_INT; /* Most usual kind. */
  741. value->mode = GET_MODE (x);
  742. value->un.addr.base = 0;
  743. value->un.addr.offset = 0;
  744. switch (GET_CODE (x))
  745. {
  746. case CONST_DOUBLE:
  747. value->kind = RTX_DOUBLE;
  748. value->mode = GET_MODE (x);
  749. value->un.d[0] = XINT (x, 0);
  750. value->un.d[1] = XINT (x, 1);
  751. break;
  752. case CONST_INT:
  753. value->un.addr.offset = INTVAL (x);
  754. break;
  755. case SYMBOL_REF:
  756. /* Use the string's address, not the SYMBOL_REF's address,
  757. for the sake of addresses of library routines. */
  758. value->un.addr.base = XEXP (x, 0);
  759. break;
  760. case LABEL_REF:
  761. /* Return the CODE_LABEL, not the LABEL_REF. */
  762. value->un.addr.base = XEXP (x, 0);
  763. break;
  764. case CONST:
  765. x = XEXP (x, 0);
  766. if (GET_CODE (x) == PLUS)
  767. {
  768. value->un.addr.base = XEXP (x, 0);
  769. if (GET_CODE (XEXP (x, 1)) != CONST_INT)
  770. abort ();
  771. value->un.addr.offset = INTVAL (XEXP (x, 1));
  772. }
  773. else if (GET_CODE (x) == MINUS)
  774. {
  775. value->un.addr.base = XEXP (x, 0);
  776. if (GET_CODE (XEXP (x, 1)) != CONST_INT)
  777. abort ();
  778. value->un.addr.offset = - INTVAL (XEXP (x, 1));
  779. }
  780. else
  781. abort ();
  782. break;
  783. default:
  784. abort ();
  785. }
  786. }
  787. /* Compute a hash code for a constant RTL expression. */
  788. int
  789. const_hash_rtx (x)
  790. rtx x;
  791. {
  792. register int hi, i, len;
  793. register char *p;
  794. struct rtx_const value;
  795. decode_rtx_const (x, &value);
  796. /* Compute hashing function */
  797. hi = 0;
  798. for (i = 0; i < sizeof value / sizeof (int); i++)
  799. hi += ((int *) &value)[i];
  800. hi &= (1 << HASHBITS) - 1;
  801. hi %= MAX_RTX_HASH_TABLE;
  802. return hi;
  803. }
  804. /* Compare a constant rtl object X with a constant-descriptor DESC.
  805. Return 1 if DESC describes a constant with the same value as X. */
  806. static int
  807. compare_constant_rtx (x, desc)
  808. rtx x;
  809. struct constant_descriptor *desc;
  810. {
  811. register int *p = (int *) desc->contents;
  812. register int *strp;
  813. register int len;
  814. struct rtx_const value;
  815. decode_rtx_const (x, &value);
  816. strp = (int *) &value;
  817. len = sizeof value / sizeof (int);
  818. /* Compare constant contents. */
  819. while (--len >= 0)
  820. if (*p++ != *strp++)
  821. return 0;
  822. return 1;
  823. }
  824. /* Construct a constant descriptor for the rtl-expression X.
  825. It is up to the caller to enter the descriptor in the hash table. */
  826. static struct constant_descriptor *
  827. record_constant_rtx (x)
  828. rtx x;
  829. {
  830. struct constant_descriptor *ptr = 0;
  831. int buf;
  832. struct rtx_const value;
  833. decode_rtx_const (x, &value);
  834. obstack_grow (saveable_obstack, &ptr, sizeof ptr);
  835. obstack_grow (saveable_obstack, &buf, sizeof buf);
  836. /* Record constant contents. */
  837. obstack_grow (saveable_obstack, &value, sizeof value);
  838. return (struct constant_descriptor *) obstack_finish (saveable_obstack);
  839. }
  840. /* Given a constant rtx X, make (or find) a memory constant for its value
  841. and return a MEM rtx to refer to it in memory. */
  842. rtx
  843. force_const_mem (mode, x)
  844. enum machine_mode mode;
  845. rtx x;
  846. {
  847. register int hash, i;
  848. register struct constant_descriptor *desc;
  849. char label[10];
  850. char *found = 0;
  851. rtx def;
  852. /* Compute hash code of X. Search the descriptors for that hash code
  853. to see if any of them describes X. If yes, the descriptor records
  854. the label number already assigned. */
  855. hash = const_hash_rtx (x);
  856. for (desc = const_rtx_hash_table[hash]; desc; desc = desc->next)
  857. if (compare_constant_rtx (x, desc))
  858. {
  859. found = desc->label;
  860. break;
  861. }
  862. if (found == 0)
  863. {
  864. /* No constant equal to X is known to have been output.
  865. Make a constant descriptor to enter X in the hash table.
  866. Assign the label number and record it in the descriptor for
  867. future calls to this function to find. */
  868. desc = record_constant_rtx (x);
  869. desc->next = const_rtx_hash_table[hash];
  870. const_rtx_hash_table[hash] = desc;
  871. /* Now output assembler code to define that label
  872. and follow it with the data of EXP. */
  873. /* First switch to text segment. */
  874. fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
  875. /* Align the location counter as required by EXP's data type. */
  876. ASM_OUTPUT_ALIGN (asm_out_file, exact_log2 (UNITS_PER_WORD));
  877. /* Output the label itself. */
  878. ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LC", const_labelno);
  879. /* Output the value of EXP. */
  880. if (GET_CODE (x) == CONST_DOUBLE)
  881. {
  882. union {double d; int i[2];} u;
  883. u.i[0] = XINT (x, 0);
  884. u.i[1] = XINT (x, 1);
  885. if (GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (DFmode))
  886. ASM_OUTPUT_DOUBLE (asm_out_file, u.d);
  887. else
  888. ASM_OUTPUT_FLOAT (asm_out_file, u.d);
  889. }
  890. else
  891. ASM_OUTPUT_INT (asm_out_file, x);
  892. /* Create a string containing the label name, in LABEL. */
  893. ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
  894. ++const_labelno;
  895. desc->label = found
  896. = (char *) obstack_copy0 (&permanent_obstack, label, strlen (label));
  897. }
  898. /* We have a symbol name; construct the SYMBOL_REF and the MEM. */
  899. def = gen_rtx (MEM, mode, gen_rtx (SYMBOL_REF, Pmode, desc->label));
  900. def->unchanging = 1;
  901. /* Mark the symbol_ref as belonging to this constants pool. */
  902. XEXP (def, 0)->unchanging = 1;
  903. return def;
  904. }
  905. /* Find all the constants whose addresses are referenced inside of EXP,
  906. and make sure assembler code with a label has been output for each one. */
  907. void
  908. output_addressed_constants (exp)
  909. tree exp;
  910. {
  911. switch (TREE_CODE (exp))
  912. {
  913. case ADDR_EXPR:
  914. {
  915. register tree constant = TREE_OPERAND (exp, 0);
  916. while (TREE_CODE (constant) == COMPONENT_REF)
  917. {
  918. constant = TREE_OPERAND (constant, 0);
  919. }
  920. if (TREE_LITERAL (constant))
  921. /* No need to do anything here
  922. for addresses of variables or functions. */
  923. output_constant_def (constant);
  924. }
  925. break;
  926. case PLUS_EXPR:
  927. case MINUS_EXPR:
  928. output_addressed_constants (TREE_OPERAND (exp, 0));
  929. output_addressed_constants (TREE_OPERAND (exp, 1));
  930. break;
  931. case NOP_EXPR:
  932. case CONVERT_EXPR:
  933. output_addressed_constants (TREE_OPERAND (exp, 0));
  934. break;
  935. case CONSTRUCTOR:
  936. {
  937. register tree link;
  938. for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
  939. output_addressed_constants (TREE_VALUE (link));
  940. }
  941. break;
  942. case ERROR_MARK:
  943. break;
  944. default:
  945. if (! TREE_LITERAL (exp))
  946. abort ();
  947. }
  948. }
  949. /* Output assembler code for constant EXP to FILE, with no label.
  950. This includes the pseudo-op such as ".int" or ".byte", and a newline.
  951. Assumes output_addressed_constants has been done on EXP already.
  952. Generate exactly SIZE bytes of assembler data, padding at the end
  953. with zeros if necessary. SIZE must always be specified.
  954. SIZE is important for structure constructors,
  955. since trailing members may have been omitted from the constructor.
  956. It is also important for initialization of arrays from string constants
  957. since the full length of the string constant might not be wanted.
  958. It is also needed for initialization of unions, where the initializer's
  959. type is just one member, and that may not be as long as the union.
  960. There a case in which we would fail to output exactly SIZE bytes:
  961. for a structure constructor that wants to produce more than SIZE bytes.
  962. But such constructors will never be generated for any possible input. */
  963. void
  964. output_constant (exp, size)
  965. register tree exp;
  966. register int size;
  967. {
  968. register enum tree_code code = TREE_CODE (TREE_TYPE (exp));
  969. rtx x;
  970. if (size == 0)
  971. return;
  972. switch (code)
  973. {
  974. case INTEGER_TYPE:
  975. case ENUMERAL_TYPE:
  976. case POINTER_TYPE:
  977. x = expand_expr (exp, 0, VOIDmode, EXPAND_SUM);
  978. if (size == 1)
  979. {
  980. ASM_OUTPUT_CHAR (asm_out_file, x);
  981. size -= 1;
  982. }
  983. else if (size == 2)
  984. {
  985. ASM_OUTPUT_SHORT (asm_out_file, x);
  986. size -= 2;
  987. }
  988. else if (size == 4)
  989. {
  990. ASM_OUTPUT_INT (asm_out_file, x);
  991. size -= 4;
  992. }
  993. else
  994. abort ();
  995. break;
  996. case REAL_TYPE:
  997. if (TREE_CODE (exp) != REAL_CST)
  998. error ("initializer for floating value is not a floating constant");
  999. if (size < 4)
  1000. break;
  1001. else if (size < 8)
  1002. {
  1003. ASM_OUTPUT_FLOAT (asm_out_file, TREE_REAL_CST (exp));
  1004. size -= 4;
  1005. }
  1006. else
  1007. {
  1008. ASM_OUTPUT_DOUBLE (asm_out_file, TREE_REAL_CST (exp));
  1009. size -= 8;
  1010. }
  1011. break;
  1012. case COMPLEX_TYPE:
  1013. output_constant (TREE_REALPART (exp), size / 2);
  1014. output_constant (TREE_IMAGPART (exp), size / 2);
  1015. size -= (size / 2) * 2;
  1016. break;
  1017. case ARRAY_TYPE:
  1018. if (TREE_CODE (exp) == CONSTRUCTOR)
  1019. {
  1020. output_constructor (exp, size);
  1021. return;
  1022. }
  1023. else if (TREE_CODE (exp) == STRING_CST)
  1024. {
  1025. register int i;
  1026. register unsigned char *p
  1027. = (unsigned char *) TREE_STRING_POINTER (exp);
  1028. int excess = 0;
  1029. if (size > TREE_STRING_LENGTH (exp))
  1030. {
  1031. excess = size - TREE_STRING_LENGTH (exp);
  1032. size = TREE_STRING_LENGTH (exp);
  1033. }
  1034. #ifdef ASM_OUTPUT_ASCII
  1035. ASM_OUTPUT_ASCII (asm_out_file, p, size);
  1036. #else
  1037. fprintf (asm_out_file, "\t.ascii \"");
  1038. for (i = 0; i < size; i++)
  1039. {
  1040. register int c = p[i];
  1041. if (c == '\"' || c == '\\')
  1042. putc ('\\', asm_out_file);
  1043. if (c >= ' ' && c < 0177)
  1044. putc (c, asm_out_file);
  1045. else
  1046. {
  1047. fprintf (asm_out_file, "\\%o", c);
  1048. /* After an octal-escape, if a digit follows,
  1049. terminate one string constant and start another.
  1050. The Vax assembler fails to stop reading the escape
  1051. after three digits, so this is the only way we
  1052. can get it to parse the data properly. */
  1053. if (i < size - 1 && p[i + 1] >= '0' && p[i + 1] <= '9')
  1054. fprintf (asm_out_file, "\"\n\t.ascii \"");
  1055. }
  1056. }
  1057. fprintf (asm_out_file, "\"\n");
  1058. #endif /* no ASM_OUTPUT_ASCII */
  1059. size = excess;
  1060. }
  1061. else
  1062. abort ();
  1063. break;
  1064. case RECORD_TYPE:
  1065. case UNION_TYPE:
  1066. if (TREE_CODE (exp) == CONSTRUCTOR)
  1067. output_constructor (exp, size);
  1068. else
  1069. abort ();
  1070. return;
  1071. }
  1072. if (size > 0)
  1073. ASM_OUTPUT_SKIP (asm_out_file, size);
  1074. }
  1075. /* Subroutine of output_constant, used for CONSTRUCTORs
  1076. (aggregate constants).
  1077. Generate at least SIZE bytes, padding if necessary. */
  1078. void
  1079. output_constructor (exp, size)
  1080. tree exp;
  1081. int size;
  1082. {
  1083. register tree link, field = 0;
  1084. register int byte;
  1085. int total_bytes = 0;
  1086. int byte_offset = -1;
  1087. if (TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
  1088. || TREE_CODE (TREE_TYPE (exp)) == UNION_TYPE)
  1089. field = TYPE_FIELDS (TREE_TYPE (exp));
  1090. /* As LINK goes through the elements of the constant,
  1091. FIELD goes through the structure fields, if the constant is a structure.
  1092. But the constant could also be an array. Then FIELD is zero. */
  1093. for (link = CONSTRUCTOR_ELTS (exp);
  1094. link;
  1095. link = TREE_CHAIN (link),
  1096. field = field ? TREE_CHAIN (field) : 0)
  1097. {
  1098. if (field == 0
  1099. || (DECL_MODE (field) != BImode))
  1100. {
  1101. register int fieldsize;
  1102. /* An element that is not a bit-field.
  1103. Output any buffered-up bit-fields preceding it. */
  1104. if (byte_offset >= 0)
  1105. {
  1106. ASM_OUTPUT_BYTE (asm_out_file, byte);
  1107. total_bytes++;
  1108. byte_offset = -1;
  1109. }
  1110. /* Align to this element's alignment,
  1111. if it isn't aligned properly by its predecessors. */
  1112. if (field && (total_bytes * BITS_PER_UNIT) % DECL_ALIGN (field) != 0)
  1113. {
  1114. int byte_align = DECL_ALIGN (field) / BITS_PER_UNIT;
  1115. int to_byte = (((total_bytes + byte_align - 1) / byte_align)
  1116. * byte_align);
  1117. ASM_OUTPUT_SKIP (asm_out_file, to_byte - total_bytes);
  1118. total_bytes = to_byte;
  1119. }
  1120. /* Output the element's initial value. */
  1121. if (field)
  1122. {
  1123. if (! TREE_LITERAL (DECL_SIZE (field)))
  1124. abort ();
  1125. fieldsize = TREE_INT_CST_LOW (DECL_SIZE (field))
  1126. * DECL_SIZE_UNIT (field);
  1127. fieldsize = (fieldsize + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  1128. }
  1129. else
  1130. fieldsize = int_size_in_bytes (TREE_TYPE (TREE_TYPE (exp)));
  1131. output_constant (TREE_VALUE (link), fieldsize);
  1132. /* Count its size. */
  1133. total_bytes += fieldsize;
  1134. }
  1135. else if (TREE_CODE (TREE_VALUE (link)) != INTEGER_CST)
  1136. error ("invalid initial value for member `%s'",
  1137. IDENTIFIER_POINTER (DECL_NAME (field)));
  1138. else
  1139. {
  1140. /* Element that is a bit-field. */
  1141. int next_offset = DECL_OFFSET (field);
  1142. int end_offset
  1143. = (next_offset
  1144. + (TREE_INT_CST_LOW (DECL_SIZE (field))
  1145. * DECL_SIZE_UNIT (field)));
  1146. /* We must split the element into pieces that fall within
  1147. separate bytes, and combine each byte with previous or
  1148. following bit-fields. */
  1149. /* next_offset is the offset n fbits from the begining of
  1150. the structure to the next bit of this element to be processed.
  1151. end_offset is the offset of the first bit past the end of
  1152. this element. */
  1153. while (next_offset < end_offset)
  1154. {
  1155. int this_time;
  1156. int next_byte = next_offset / BITS_PER_UNIT;
  1157. int next_bit = next_offset % BITS_PER_UNIT;
  1158. if (byte_offset < 0)
  1159. {
  1160. byte_offset = next_byte;
  1161. byte = 0;
  1162. }
  1163. else
  1164. while (next_byte != byte_offset)
  1165. {
  1166. ASM_OUTPUT_BYTE (asm_out_file, byte);
  1167. byte_offset++;
  1168. total_bytes++;
  1169. byte = 0;
  1170. }
  1171. /* Number of bits we can process at once
  1172. (all part of the same byte). */
  1173. this_time = MIN (end_offset - next_offset,
  1174. BITS_PER_UNIT - next_bit);
  1175. #ifdef BYTES_BIG_ENDIAN
  1176. /* On big-endian machine, take the most significant bits
  1177. first (of the bits that are significant)
  1178. and put them into bytes from the most significant end. */
  1179. byte |= (((TREE_INT_CST_LOW (TREE_VALUE (link))
  1180. >> (end_offset - next_offset - this_time))
  1181. & ((1 << this_time) - 1))
  1182. << (BITS_PER_UNIT - this_time - next_bit));
  1183. #else
  1184. /* On little-endian machines,
  1185. take first the least significant bits of the value
  1186. and pack them starting at the least significant
  1187. bits of the bytes. */
  1188. byte |= ((TREE_INT_CST_LOW (TREE_VALUE (link))
  1189. >> (next_offset - DECL_OFFSET (field)))
  1190. & ((1 << this_time) - 1)) << next_bit;
  1191. #endif
  1192. next_offset += this_time;
  1193. }
  1194. }
  1195. }
  1196. if (byte_offset >= 0)
  1197. {
  1198. ASM_OUTPUT_BYTE (asm_out_file, byte);
  1199. byte_offset = -1;
  1200. total_bytes++;
  1201. }
  1202. if (total_bytes < size)
  1203. ASM_OUTPUT_SKIP (asm_out_file, size - total_bytes);
  1204. }