frags.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* frags.c - manage frags -
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include "as.h"
  16. #include "subsegs.h"
  17. #include "obstack.h"
  18. #include "frags.h"
  19. #include "struc-symbol.h"
  20. struct obstack frags; /* All, and only, frags live here. */
  21. fragS zero_address_frag = {
  22. 0, /* fr_address */
  23. NULL, /* fr_next */
  24. 0, /* fr_fix */
  25. 0, /* fr_var */
  26. 0, /* fr_symbol */
  27. 0, /* fr_offset */
  28. NULL, /* fr_opcode */
  29. rs_fill, /* fr_type */
  30. 0, /* fr_subtype */
  31. 0, /* fr_pcrel_adjust */
  32. 0, /* fr_bsr */
  33. 0 /* fr_literal [0] */
  34. };
  35. fragS bss_address_frag = {
  36. 0, /* fr_address. Gets filled in to make up
  37. sy_value-s. */
  38. NULL, /* fr_next */
  39. 0, /* fr_fix */
  40. 0, /* fr_var */
  41. 0, /* fr_symbol */
  42. 0, /* fr_offset */
  43. NULL, /* fr_opcode */
  44. rs_fill, /* fr_type */
  45. 0, /* fr_subtype */
  46. 0, /* fr_pcrel_adjust */
  47. 0, /* fr_bsr */
  48. 0 /* fr_literal [0] */
  49. };
  50. /*
  51. * frag_grow()
  52. *
  53. * Internal.
  54. * Try to augment current frag by nchars chars.
  55. * If there is no room, close of the current frag with a ".fill 0"
  56. * and begin a new frag. Unless the new frag has nchars chars available
  57. * do not return. Do not set up any fields of *now_frag.
  58. */
  59. static void
  60. frag_grow (nchars)
  61. int nchars;
  62. {
  63. if (obstack_room (&frags) < nchars) {
  64. unsigned int n,oldn;
  65. long oldc;
  66. frag_wane (frag_now);
  67. frag_new (0);
  68. oldn=(unsigned)-1;
  69. oldc=frags.chunk_size;
  70. frags.chunk_size=2*nchars;
  71. while((n=obstack_room(&frags))<nchars && n<oldn) {
  72. frag_wane(frag_now);
  73. frag_new(0);
  74. oldn=n;
  75. }
  76. frags.chunk_size=oldc;
  77. }
  78. if (obstack_room (&frags) < nchars)
  79. as_fatal ("Can't extend frag %d. chars", nchars);
  80. }
  81. /*
  82. * frag_new()
  83. *
  84. * Call this to close off a completed frag, and start up a new (empty)
  85. * frag, in the same subsegment as the old frag.
  86. * [frchain_now remains the same but frag_now is updated.]
  87. * Because this calculates the correct value of fr_fix by
  88. * looking at the obstack 'frags', it needs to know how many
  89. * characters at the end of the old frag belong to (the maximal)
  90. * fr_var: the rest must belong to fr_fix.
  91. * It doesn't actually set up the old frag's fr_var: you may have
  92. * set fr_var == 1, but allocated 10 chars to the end of the frag:
  93. * in this case you pass old_frags_var_max_size == 10.
  94. *
  95. * Make a new frag, initialising some components. Link new frag at end
  96. * of frchain_now.
  97. */
  98. void
  99. frag_new (old_frags_var_max_size)
  100. int old_frags_var_max_size; /* Number of chars (already allocated on
  101. obstack frags) */
  102. /* in variable_length part of frag. */
  103. {
  104. register fragS * former_last_fragP;
  105. /* char *throw_away_pointer; JF unused */
  106. register frchainS * frchP;
  107. long tmp; /* JF */
  108. frag_now->fr_fix = (char *) (obstack_next_free (&frags)) -
  109. (frag_now->fr_literal) - old_frags_var_max_size;
  110. /* Fix up old frag's fr_fix. */
  111. obstack_finish (&frags);
  112. /* This will align the obstack so the */
  113. /* next struct we allocate on it will */
  114. /* begin at a correct boundary. */
  115. frchP = frchain_now;
  116. know (frchP);
  117. former_last_fragP = frchP->frch_last;
  118. know (former_last_fragP);
  119. know (former_last_fragP == frag_now);
  120. obstack_blank (&frags, SIZEOF_STRUCT_FRAG);
  121. /* We expect this will begin at a correct */
  122. /* boundary for a struct. */
  123. tmp=obstack_alignment_mask(&frags);
  124. obstack_alignment_mask(&frags)=0; /* Turn off alignment */
  125. /* If we ever hit a machine
  126. where strings must be
  127. aligned, we Lose Big */
  128. frag_now=(fragS *)obstack_finish(&frags);
  129. obstack_alignment_mask(&frags)=tmp; /* Restore alignment */
  130. /* Just in case we don't get zero'd bytes */
  131. bzero(frag_now, SIZEOF_STRUCT_FRAG);
  132. /* obstack_unaligned_done (&frags, &frag_now); */
  133. /* know (frags.obstack_c_next_free == frag_now->fr_literal); */
  134. /* Generally, frag_now->points to an */
  135. /* address rounded up to next alignment. */
  136. /* However, characters will add to obstack */
  137. /* frags IMMEDIATELY after the struct frag, */
  138. /* even if they are not starting at an */
  139. /* alignment address. */
  140. former_last_fragP->fr_next = frag_now;
  141. frchP->frch_last = frag_now;
  142. frag_now->fr_next = NULL;
  143. } /* frag_new() */
  144. /*
  145. * frag_more()
  146. *
  147. * Start a new frag unless we have n more chars of room in the current frag.
  148. * Close off the old frag with a .fill 0.
  149. *
  150. * Return the address of the 1st char to write into. Advance
  151. * frag_now_growth past the new chars.
  152. */
  153. char *
  154. frag_more (nchars)
  155. int nchars;
  156. {
  157. register char *retval;
  158. frag_grow (nchars);
  159. retval = obstack_next_free (&frags);
  160. obstack_blank_fast (&frags, nchars);
  161. return (retval);
  162. } /* frag_more() */
  163. /*
  164. * frag_var()
  165. *
  166. * Start a new frag unless we have max_chars more chars of room in the current frag.
  167. * Close off the old frag with a .fill 0.
  168. *
  169. * Set up a machine_dependent relaxable frag, then start a new frag.
  170. * Return the address of the 1st char of the var part of the old frag
  171. * to write into.
  172. */
  173. char *
  174. frag_var (type, max_chars, var, subtype, symbol, offset, opcode)
  175. relax_stateT type;
  176. int max_chars;
  177. int var;
  178. relax_substateT subtype;
  179. symbolS * symbol;
  180. long int offset;
  181. char * opcode;
  182. {
  183. register char *retval;
  184. frag_grow (max_chars);
  185. retval = obstack_next_free (&frags);
  186. obstack_blank_fast (&frags, max_chars);
  187. frag_now->fr_var = var;
  188. frag_now->fr_type = type;
  189. frag_now->fr_subtype = subtype;
  190. frag_now->fr_symbol = symbol;
  191. frag_now->fr_offset = offset;
  192. frag_now->fr_opcode = opcode;
  193. /* default these to zero. */
  194. frag_now->fr_pcrel_adjust = 0;
  195. frag_now->fr_bsr = 0;
  196. frag_new (max_chars);
  197. return (retval);
  198. } /* frag_var() */
  199. /*
  200. * frag_variant()
  201. *
  202. * OVE: This variant of frag_var assumes that space for the tail has been
  203. * allocated by caller.
  204. * No call to frag_grow is done.
  205. * Two new arguments have been added.
  206. */
  207. char *
  208. frag_variant (type, max_chars, var, subtype, symbol, offset, opcode, pcrel_adjust,bsr)
  209. relax_stateT type;
  210. int max_chars;
  211. int var;
  212. relax_substateT subtype;
  213. symbolS *symbol;
  214. long int offset;
  215. char *opcode;
  216. char pcrel_adjust;
  217. char bsr;
  218. {
  219. register char *retval;
  220. /* frag_grow (max_chars); */
  221. retval = obstack_next_free (&frags);
  222. /* obstack_blank_fast (&frags, max_chars); */ /* OVE: so far the only diff */
  223. frag_now->fr_var = var;
  224. frag_now->fr_type = type;
  225. frag_now->fr_subtype = subtype;
  226. frag_now->fr_symbol = symbol;
  227. frag_now->fr_offset = offset;
  228. frag_now->fr_opcode = opcode;
  229. frag_now->fr_pcrel_adjust = pcrel_adjust;
  230. frag_now->fr_bsr = bsr;
  231. frag_new (max_chars);
  232. return (retval);
  233. } /* frag_variant() */
  234. /*
  235. * frag_wane()
  236. *
  237. * Reduce the variable end of a frag to a harmless state.
  238. */
  239. void
  240. frag_wane (fragP)
  241. register fragS * fragP;
  242. {
  243. fragP->fr_type = rs_fill;
  244. fragP->fr_offset = 0;
  245. fragP->fr_var = 0;
  246. }
  247. /*
  248. * frag_align()
  249. *
  250. * Make a frag for ".align foo,bar". Call is "frag_align (foo,bar);".
  251. * Foo & bar are absolute integers.
  252. *
  253. * Call to close off the current frag with a ".align", then start a new
  254. * (so far empty) frag, in the same subsegment as the last frag.
  255. */
  256. void
  257. frag_align (alignment, fill_character)
  258. int alignment;
  259. int fill_character;
  260. {
  261. *(frag_var (rs_align, 1, 1, (relax_substateT)0, (symbolS *)0,
  262. (long)alignment, (char *)0)) = fill_character;
  263. }
  264. /* end: frags.c */