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