* subsegs.c (subsegs_begin): create bss0_frchainP in the same was
[external/binutils.git] / gas / subsegs.c
1 /* subsegs.c - subsegments -
2    Copyright (C) 1987, 1990, 1991, 1992 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 /*
21  * Segments & sub-segments.
22  */
23
24 #include "as.h"
25
26 #include "subsegs.h"
27 #include "obstack.h"
28
29 #ifdef MANY_SEGMENTS
30 segment_info_type segment_info[SEG_MAXIMUM_ORDINAL];
31
32 frchainS*       frchain_root,
33     *   frchain_now;
34
35 #else
36 frchainS*       frchain_root,
37     *   frchain_now,    /* Commented in "subsegs.h". */
38     *   data0_frchainP,
39  * bss0_frchainP;
40
41 #endif
42 char * const /* in: segT   out: char* */
43     seg_name[] = {
44             "absolute",
45 #ifdef MANY_SEGMENTS
46             "e0","e1","e2","e3","e4","e5","e6","e7","e8","e9",
47 #else
48             "text",
49             "data",
50             "bss",
51 #endif
52             "unknown",
53             "absent",
54             "pass1",
55             "ASSEMBLER-INTERNAL-LOGIC-ERROR!",
56             "bignum/flonum",
57             "difference",
58             "debug",
59             "transfert vector preload",
60             "transfert vector postload",
61             "register",
62             "",
63     }; /* Used by error reporters, dumpers etc. */
64
65 \f
66 void
67     subsegs_begin()
68 {
69         /* Check table(s) seg_name[], seg_N_TYPE[] is in correct order */
70 #ifdef MANY_SEGMENTS
71 #else
72         know( SEG_ABSOLUTE      ==  0 );
73         know( SEG_TEXT          ==  1 );
74         know( SEG_DATA          ==  2 );
75         know( SEG_BSS           ==  3 );
76         know( SEG_UNKNOWN       ==  4 );
77         know( SEG_ABSENT        ==  5 );
78         know( SEG_PASS1 ==  6 );
79         know( SEG_GOOF  ==  7 );
80         know( SEG_BIG           ==  8 );
81         know( SEG_DIFFERENCE    ==  9 );
82         know( SEG_DEBUG == 10 );
83         know( SEG_NTV           == 11 );
84         know( SEG_PTV           == 12 );
85         know( SEG_REGISTER      == 13 );
86         know( SEG_MAXIMUM_ORDINAL == SEG_REGISTER );
87         /*  know( segment_name (SEG_MAXIMUM_ORDINAL + 1) [0] == 0 );*/
88 #endif
89         
90         obstack_begin( &frags, 5000);
91         frchain_root = NULL;
92         frchain_now  = NULL;            /* Warn new_subseg() that we are booting. */
93         /* Fake up 1st frag. */
94         /* It won't be used=> is ok if obstack... */
95         /* pads the end of it for alignment. */
96         frag_now=(fragS *)obstack_alloc(&frags,SIZEOF_STRUCT_FRAG);
97         memset(frag_now,  SIZEOF_STRUCT_FRAG, 0); 
98         /* This 1st frag will not be in any frchain. */
99         /* We simply give subseg_new somewhere to scribble. */
100         now_subseg = 42;                /* Lie for 1st call to subseg_new. */
101 #ifdef MANY_SEGMENTS
102         {
103                 int i;
104                 for (i = SEG_E0; i < SEG_UNKNOWN; i++) {
105                         subseg_new(i, 0);
106                         segment_info[i].frchainP = frchain_now;
107                 }
108         }
109 #else
110         subseg_new (SEG_DATA, 0);       /* .data 0 */
111         data0_frchainP = frchain_now;
112
113         subseg_new (SEG_BSS, 0);
114         bss0_frchainP = frchain_now;
115
116 #endif
117         
118 }
119 \f
120 /*
121  *                      subseg_change()
122  *
123  * Change the subsegment we are in, BUT DO NOT MAKE A NEW FRAG for the
124  * subsegment. If we are already in the correct subsegment, change nothing.
125  * This is used eg as a worker for subseg_new [which does make a new frag_now]
126  * and for changing segments after we have read the source. We construct eg
127  * fixSs even after the source file is read, so we do have to keep the
128  * segment context correct.
129  */
130 void
131     subseg_change (seg, subseg)
132 register segT   seg;
133 register int    subseg;
134 {
135         now_seg  = seg;
136         now_subseg = subseg;
137 #ifdef MANY_SEGMENTS
138         seg_fix_rootP = & segment_info[seg].fix_root;
139         seg_fix_tailP = & segment_info[seg].fix_tail;
140 #else
141         if (seg == SEG_DATA)
142             {
143                     seg_fix_rootP = & data_fix_root;
144                     seg_fix_tailP = & data_fix_tail;
145             }
146         else if (seg == SEG_TEXT)
147             {
148                     seg_fix_rootP = & text_fix_root;
149                     seg_fix_tailP = & text_fix_tail;
150             }
151         else {
152                     know (seg == SEG_BSS);
153                     seg_fix_rootP = & bss_fix_root;
154                     seg_fix_tailP = & bss_fix_tail;
155             }
156 #endif
157 }
158 \f
159 /*
160  *                      subseg_new()
161  *
162  * If you attempt to change to the current subsegment, nothing happens.
163  *
164  * In:  segT, subsegT code for new subsegment.
165  *      frag_now -> incomplete frag for current subsegment.
166  *      If frag_now==NULL, then there is no old, incomplete frag, so
167  *      the old frag is not closed off.
168  *
169  * Out: now_subseg, now_seg updated.
170  *      Frchain_now points to the (possibly new) struct frchain for this
171  *      sub-segment.
172  *      Frchain_root updated if needed.
173  */
174
175 void
176     subseg_new (seg, subseg)    /* begin assembly for a new sub-segment */
177 register segT   seg; /* SEG_DATA or SEG_TEXT */
178 register subsegT        subseg;
179 {
180         long tmp;               /* JF for obstack alignment hacking */
181 #ifndef MANY_SEGMENTS
182         know(seg == SEG_DATA || seg == SEG_TEXT || seg == SEG_BSS);
183 #endif
184 #ifdef OBJ_AOUT
185 /* If -R specifed, always put stuff into the data section */
186         if (flagseen['R']) 
187         {
188           if (seg == SEG_DATA) 
189           {
190             subseg += 1000;
191             seg = SEG_TEXT;
192           }
193         }
194 #endif
195         if (seg != now_seg || subseg != now_subseg)
196             {                           /* we just changed sub-segments */
197                     register    frchainS *      frcP;   /* crawl frchain chain */
198                     register    frchainS**      lastPP; /* address of last pointer */
199                     frchainS *  newP;   /* address of new frchain */
200                     register fragS *            former_last_fragP;
201                     register fragS *            new_fragP;
202                     
203                     if (frag_now)               /* If not bootstrapping. */
204                         {
205                                 frag_now -> fr_fix = obstack_next_free(& frags) - frag_now -> fr_literal;
206                                 frag_wane(frag_now);    /* Close off any frag in old subseg. */
207                         }
208                     /*
209                      * It would be nice to keep an obstack for each subsegment, if we swap
210                      * subsegments a lot. Hence we would have much fewer frag_wanes().
211                      */
212                     {
213                             
214                             obstack_finish( &frags);
215                             /*
216                              * If we don't do the above, the next object we put on obstack frags
217                              * will appear to start at the fr_literal of the current frag.
218                              * Also, above ensures that the next object will begin on a
219                              * address that is aligned correctly for the engine that runs
220                              * this program.
221                              */
222                     }
223                     subseg_change (seg, (int)subseg);
224                     /*
225                      * Attempt to find or make a frchain for that sub seg.
226                      * Crawl along chain of frchainSs, begins @ frchain_root.
227                      * If we need to make a frchainS, link it into correct
228                      * position of chain rooted in frchain_root.
229                      */
230                     for (frcP = * (lastPP = & frchain_root);
231                          frcP
232                          && (int)(frcP -> frch_seg) <= (int)seg;
233                          frcP = * ( lastPP = & frcP -> frch_next)
234                          )
235                         {
236                                 if (   (int)(frcP -> frch_seg) == (int)seg
237                                     && frcP -> frch_subseg >= subseg)
238                                     {
239                                             break;
240                                     }
241                         }
242                     /*
243                      * frcP:            Address of the 1st frchainS in correct segment with
244                      *          frch_subseg >= subseg.
245                      *          We want to either use this frchainS, or we want
246                      *          to insert a new frchainS just before it.
247                      *
248                      *          If frcP==NULL, then we are at the end of the chain
249                      *          of frchainS-s. A NULL frcP means we fell off the end
250                      *          of the chain looking for a
251                      *          frch_subseg >= subseg, so we
252                      *          must make a new frchainS.
253                      *
254                      *          If we ever maintain a pointer to
255                      *          the last frchainS in the chain, we change that pointer
256                      *          ONLY when frcP==NULL.
257                      *
258                      * lastPP:  Address of the pointer with value frcP;
259                      *          Never NULL.
260                      *          May point to frchain_root.
261                      *
262                      */
263                     if (   ! frcP
264                         || (   (int)(frcP -> frch_seg) > (int)seg
265                             || frcP->frch_subseg > subseg)) /* Kinky logic only works with 2 segments. */
266                         {
267                                 /*
268                                  * This should be the only code that creates a frchainS.
269                                  */
270                                 newP=(frchainS *)obstack_alloc(&frags,sizeof(frchainS));
271                                 memset(newP, sizeof(frchainS), 0); 
272                                 /* This begines on a good boundary */
273                                 /* because a obstack_done() preceeded  it. */
274                                 /* It implies an obstack_done(), so we */
275                                 /* expect the next object allocated to */
276                                 /* begin on a correct boundary. */
277                                 *lastPP = newP;
278                                 newP -> frch_next = frcP; /* perhaps NULL */
279                                 (frcP = newP) -> frch_subseg            = subseg;
280                                 newP  -> frch_seg               = seg;
281                                 newP  -> frch_last              = NULL;
282                         }
283                     /*
284                      * Here with frcP ->ing to the frchainS for subseg.
285                      */
286                     frchain_now = frcP;
287                     /*
288                      * Make a fresh frag for the subsegment.
289                      */
290                     /* We expect this to happen on a correct */
291                     /* boundary since it was proceeded by a */
292                     /* obstack_done(). */
293                     tmp=obstack_alignment_mask(&frags); /* JF disable alignment */
294                     obstack_alignment_mask(&frags)=0;
295                     frag_now=(fragS *)obstack_alloc(&frags,SIZEOF_STRUCT_FRAG);
296                     obstack_alignment_mask(&frags)=tmp;
297                     /* know( frags . obstack_c_next_free == frag_now -> fr_literal ); */
298                     /* But we want any more chars to come */
299                     /* immediately after the structure we just made. */
300                     new_fragP = frag_now;
301                     new_fragP -> fr_next = NULL;
302                     /*
303                      * Append new frag to current frchain.
304                      */
305                     former_last_fragP = frcP -> frch_last;
306                     if (former_last_fragP)
307                         {
308                                 know( former_last_fragP -> fr_next == NULL );
309                                 know( frchain_now -> frch_root );
310                                 former_last_fragP -> fr_next = new_fragP;
311                         }
312                     else
313                         {
314                                 frcP -> frch_root = new_fragP;
315                         }
316                     frcP -> frch_last = new_fragP;
317             }                           /* if (changing subsegments) */
318 }                               /* subseg_new() */
319
320 /*
321  * Local Variables:
322  * comment-column: 0
323  * fill-column: 131
324  * End:
325  */
326
327 /* end of subsegs.c */