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