1 /* frags.c - manage frags -
2 Copyright (C) 1987, 1990, 1991 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* static const char rcsid[] = "$Id$"; */
26 struct obstack frags; /* All, and only, frags live here. */
28 fragS zero_address_frag = {
36 rs_fill, /* fr_type */
38 0, /* fr_pcrel_adjust */
40 0 /* fr_literal [0] */
43 fragS bss_address_frag = {
44 0, /* fr_address. Gets filled in to make up
52 rs_fill, /* fr_type */
54 0, /* fr_pcrel_adjust */
56 0 /* fr_literal [0] */
63 * Try to augment current frag by nchars chars.
64 * If there is no room, close of the current frag with a ".fill 0"
65 * and begin a new frag. Unless the new frag has nchars chars available
66 * do not return. Do not set up any fields of *now_frag.
68 static void frag_grow(nchars)
71 if (obstack_room (&frags) < nchars) {
78 oldc=frags.chunk_size;
79 frags.chunk_size=2*nchars;
80 while((n=obstack_room(&frags))<nchars && n<oldn) {
85 frags.chunk_size=oldc;
87 if (obstack_room (&frags) < nchars)
88 as_fatal("Can't extend frag %d. chars", nchars);
94 * Call this to close off a completed frag, and start up a new (empty)
95 * frag, in the same subsegment as the old frag.
96 * [frchain_now remains the same but frag_now is updated.]
97 * Because this calculates the correct value of fr_fix by
98 * looking at the obstack 'frags', it needs to know how many
99 * characters at the end of the old frag belong to (the maximal)
100 * fr_var: the rest must belong to fr_fix.
101 * It doesn't actually set up the old frag's fr_var: you may have
102 * set fr_var == 1, but allocated 10 chars to the end of the frag:
103 * in this case you pass old_frags_var_max_size == 10.
105 * Make a new frag, initialising some components. Link new frag at end
108 void frag_new(old_frags_var_max_size)
109 int old_frags_var_max_size; /* Number of chars (already allocated on
111 /* in variable_length part of frag. */
113 register fragS * former_last_fragP;
114 /* char *throw_away_pointer; JF unused */
115 register frchainS * frchP;
118 frag_now->fr_fix = (char *) (obstack_next_free (&frags)) -
119 (frag_now->fr_literal) - old_frags_var_max_size;
120 /* Fix up old frag's fr_fix. */
122 obstack_finish (&frags);
123 /* This will align the obstack so the */
124 /* next struct we allocate on it will */
125 /* begin at a correct boundary. */
128 former_last_fragP = frchP->frch_last;
129 know (former_last_fragP);
130 know (former_last_fragP == frag_now);
131 obstack_blank (&frags, SIZEOF_STRUCT_FRAG);
132 /* We expect this will begin at a correct */
133 /* boundary for a struct. */
134 tmp=obstack_alignment_mask(&frags);
135 obstack_alignment_mask(&frags)=0; /* Turn off alignment */
136 /* If we ever hit a machine
137 where strings must be
138 aligned, we Lose Big */
139 frag_now=(fragS *)obstack_finish(&frags);
140 obstack_alignment_mask(&frags)=tmp; /* Restore alignment */
142 /* Just in case we don't get zero'd bytes */
143 bzero(frag_now, SIZEOF_STRUCT_FRAG);
145 /* obstack_unaligned_done (&frags, &frag_now); */
146 /* know (frags.obstack_c_next_free == frag_now->fr_literal); */
147 /* Generally, frag_now->points to an */
148 /* address rounded up to next alignment. */
149 /* However, characters will add to obstack */
150 /* frags IMMEDIATELY after the struct frag, */
151 /* even if they are not starting at an */
152 /* alignment address. */
153 former_last_fragP->fr_next = frag_now;
154 frchP->frch_last = frag_now;
155 frag_now->fr_next = NULL;
161 * Start a new frag unless we have n more chars of room in the current frag.
162 * Close off the old frag with a .fill 0.
164 * Return the address of the 1st char to write into. Advance
165 * frag_now_growth past the new chars.
168 char *frag_more (nchars)
171 register char *retval;
174 retval = obstack_next_free (&frags);
175 obstack_blank_fast (&frags, nchars);
182 * Start a new frag unless we have max_chars more chars of room in the current frag.
183 * Close off the old frag with a .fill 0.
185 * Set up a machine_dependent relaxable frag, then start a new frag.
186 * Return the address of the 1st char of the var part of the old frag
190 char *frag_var(type, max_chars, var, subtype, symbol, offset, opcode)
194 relax_substateT subtype;
199 register char *retval;
201 frag_grow (max_chars);
202 retval = obstack_next_free (&frags);
203 obstack_blank_fast (&frags, max_chars);
204 frag_now->fr_var = var;
205 frag_now->fr_type = type;
206 frag_now->fr_subtype = subtype;
207 frag_now->fr_symbol = symbol;
208 frag_now->fr_offset = offset;
209 frag_now->fr_opcode = opcode;
210 /* default these to zero. */
211 frag_now->fr_pcrel_adjust = 0;
212 frag_now->fr_bsr = 0;
213 frag_new (max_chars);
220 * OVE: This variant of frag_var assumes that space for the tail has been
221 * allocated by caller.
222 * No call to frag_grow is done.
223 * Two new arguments have been added.
226 char *frag_variant(type, max_chars, var, subtype, symbol, offset, opcode, pcrel_adjust,bsr)
230 relax_substateT subtype;
237 register char *retval;
239 /* frag_grow (max_chars); */
240 retval = obstack_next_free (&frags);
241 /* obstack_blank_fast (&frags, max_chars); */ /* OVE: so far the only diff */
242 frag_now->fr_var = var;
243 frag_now->fr_type = type;
244 frag_now->fr_subtype = subtype;
245 frag_now->fr_symbol = symbol;
246 frag_now->fr_offset = offset;
247 frag_now->fr_opcode = opcode;
248 frag_now->fr_pcrel_adjust = pcrel_adjust;
249 frag_now->fr_bsr = bsr;
250 frag_new (max_chars);
252 } /* frag_variant() */
257 * Reduce the variable end of a frag to a harmless state.
259 void frag_wane(fragP)
260 register fragS * fragP;
262 fragP->fr_type = rs_fill;
263 fragP->fr_offset = 0;
270 * Make a frag for ".align foo,bar". Call is "frag_align (foo,bar);".
271 * Foo & bar are absolute integers.
273 * Call to close off the current frag with a ".align", then start a new
274 * (so far empty) frag, in the same subsegment as the last frag.
277 void frag_align(alignment, fill_character)
281 *(frag_var (rs_align, 1, 1, (relax_substateT)0, (symbolS *)0,
282 (long)alignment, (char *)0)) = fill_character;