Initial revision
[deliverable/binutils-gdb.git] / gas / frags.c
1 /* frags.c - manage frags -
2 Copyright (C) 1987, 1990, 1991 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
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)
9 any later version.
10
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.
15
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. */
19
20 /* static const char rcsid[] = "$Id$"; */
21
22 #include "as.h"
23 #include "subsegs.h"
24 #include "obstack.h"
25
26 struct obstack frags; /* All, and only, frags live here. */
27
28 fragS zero_address_frag = {
29 0, /* fr_address */
30 NULL, /* fr_next */
31 0, /* fr_fix */
32 0, /* fr_var */
33 0, /* fr_symbol */
34 0, /* fr_offset */
35 NULL, /* fr_opcode */
36 rs_fill, /* fr_type */
37 0, /* fr_subtype */
38 0, /* fr_pcrel_adjust */
39 0, /* fr_bsr */
40 0 /* fr_literal [0] */
41 };
42
43 fragS bss_address_frag = {
44 0, /* fr_address. Gets filled in to make up
45 sy_value-s. */
46 NULL, /* fr_next */
47 0, /* fr_fix */
48 0, /* fr_var */
49 0, /* fr_symbol */
50 0, /* fr_offset */
51 NULL, /* fr_opcode */
52 rs_fill, /* fr_type */
53 0, /* fr_subtype */
54 0, /* fr_pcrel_adjust */
55 0, /* fr_bsr */
56 0 /* fr_literal [0] */
57 };
58 \f
59 /*
60 * frag_grow()
61 *
62 * Internal.
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.
67 */
68 static void frag_grow(nchars)
69 unsigned int nchars;
70 {
71 if (obstack_room (&frags) < nchars) {
72 unsigned int n,oldn;
73 long oldc;
74
75 frag_wane(frag_now);
76 frag_new(0);
77 oldn=(unsigned)-1;
78 oldc=frags.chunk_size;
79 frags.chunk_size=2*nchars;
80 while((n=obstack_room(&frags))<nchars && n<oldn) {
81 frag_wane(frag_now);
82 frag_new(0);
83 oldn=n;
84 }
85 frags.chunk_size=oldc;
86 }
87 if (obstack_room (&frags) < nchars)
88 as_fatal("Can't extend frag %d. chars", nchars);
89 } /* frag_grow() */
90 \f
91 /*
92 * frag_new()
93 *
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.
104 *
105 * Make a new frag, initialising some components. Link new frag at end
106 * of frchain_now.
107 */
108 void frag_new(old_frags_var_max_size)
109 int old_frags_var_max_size; /* Number of chars (already allocated on
110 obstack frags) */
111 /* in variable_length part of frag. */
112 {
113 register fragS * former_last_fragP;
114 /* char *throw_away_pointer; JF unused */
115 register frchainS * frchP;
116 long tmp; /* JF */
117
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. */
121
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. */
126 frchP = frchain_now;
127 know (frchP);
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 */
141
142 /* Just in case we don't get zero'd bytes */
143 bzero(frag_now, SIZEOF_STRUCT_FRAG);
144
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;
156 } /* frag_new() */
157 \f
158 /*
159 * frag_more()
160 *
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.
163 *
164 * Return the address of the 1st char to write into. Advance
165 * frag_now_growth past the new chars.
166 */
167
168 char *frag_more (nchars)
169 int nchars;
170 {
171 register char *retval;
172
173 frag_grow (nchars);
174 retval = obstack_next_free (&frags);
175 obstack_blank_fast (&frags, nchars);
176 return (retval);
177 } /* frag_more() */
178 \f
179 /*
180 * frag_var()
181 *
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.
184 *
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
187 * to write into.
188 */
189
190 char *frag_var(type, max_chars, var, subtype, symbol, offset, opcode)
191 relax_stateT type;
192 int max_chars;
193 int var;
194 relax_substateT subtype;
195 symbolS *symbol;
196 long offset;
197 char *opcode;
198 {
199 register char *retval;
200
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);
214 return (retval);
215 } /* frag_var() */
216 \f
217 /*
218 * frag_variant()
219 *
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.
224 */
225
226 char *frag_variant(type, max_chars, var, subtype, symbol, offset, opcode, pcrel_adjust,bsr)
227 relax_stateT type;
228 int max_chars;
229 int var;
230 relax_substateT subtype;
231 symbolS *symbol;
232 long offset;
233 char *opcode;
234 int pcrel_adjust;
235 char bsr;
236 {
237 register char *retval;
238
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);
251 return (retval);
252 } /* frag_variant() */
253 \f
254 /*
255 * frag_wane()
256 *
257 * Reduce the variable end of a frag to a harmless state.
258 */
259 void frag_wane(fragP)
260 register fragS * fragP;
261 {
262 fragP->fr_type = rs_fill;
263 fragP->fr_offset = 0;
264 fragP->fr_var = 0;
265 }
266 \f
267 /*
268 * frag_align()
269 *
270 * Make a frag for ".align foo,bar". Call is "frag_align (foo,bar);".
271 * Foo & bar are absolute integers.
272 *
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.
275 */
276
277 void frag_align(alignment, fill_character)
278 int alignment;
279 int fill_character;
280 {
281 *(frag_var (rs_align, 1, 1, (relax_substateT)0, (symbolS *)0,
282 (long)alignment, (char *)0)) = fill_character;
283 } /* frag_align() */
284
285 /* end: frags.c */
This page took 0.064195 seconds and 5 git commands to generate.