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