]> git.sur5r.net Git - cc65/blob - src/cc65/segments.c
Allow push/pop arguments for segment name #pragmas
[cc65] / src / cc65 / segments.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                segments.c                                 */
4 /*                                                                           */
5 /*                   Lightweight segment management stuff                    */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdarg.h>
37 #include <string.h>
38
39 /* common */
40 #include "chartype.h"
41 #include "check.h"
42 #include "coll.h"
43 #include "scanner.h"
44 #include "segnames.h"
45 #include "strstack.h"
46 #include "xmalloc.h"
47
48 /* cc65 */
49 #include "codeent.h"
50 #include "codeseg.h"
51 #include "dataseg.h"
52 #include "error.h"
53 #include "textseg.h"
54 #include "segments.h"
55
56
57
58 /*****************************************************************************/
59 /*                                   Data                                    */
60 /*****************************************************************************/
61
62
63
64 /* Pointer to the current segment list. Output goes here. */
65 Segments* CS = 0;
66
67 /* Pointer to the global segment list */
68 Segments* GS = 0;
69
70 /* Actual names for the segments */
71 static StrStack SegmentNames[SEG_COUNT];
72
73 /* We're using a collection for the stack instead of a linked list. Since
74  * functions may not be nested (at least in the current implementation), the
75  * maximum stack depth is 2, so there is not really a need for a better
76  * implementation.
77  */
78 static Collection SegmentStack = STATIC_COLLECTION_INITIALIZER;
79
80
81
82 /*****************************************************************************/
83 /*                                   Code                                    */
84 /*****************************************************************************/
85
86
87
88 void InitSegNames (void)
89 /* Initialize the segment names */
90 {
91     SS_Push (&SegmentNames[SEG_BSS], SEGNAME_BSS);
92     SS_Push (&SegmentNames[SEG_CODE], SEGNAME_CODE);
93     SS_Push (&SegmentNames[SEG_DATA], SEGNAME_DATA);
94     SS_Push (&SegmentNames[SEG_RODATA], SEGNAME_RODATA);
95 }
96
97
98
99 void SetSegName (segment_t Seg, const char* Name)
100 /* Set a new name for a segment */
101 {
102     SS_Set (&SegmentNames[Seg], Name);
103 }
104
105
106
107 void PushSegName (segment_t Seg, const char* Name)
108 /* Push the current segment name and set a new name for a segment */
109 {
110     if (SS_IsFull (&SegmentNames[Seg])) {
111         Error ("Segment name stack overflow");
112     } else {
113         SS_Push (&SegmentNames[Seg], Name);
114     }
115 }
116
117
118
119 void PopSegName (segment_t Seg)
120 /* Restore a segment name from the segment name stack */
121 {
122     if (SS_GetCount (&SegmentNames[Seg]) < 2) {
123         Error ("Segment name stack is empty");
124     } else {
125         SS_Drop (&SegmentNames[Seg]);
126     }
127 }
128
129
130
131 const char* GetSegName (segment_t Seg)
132 /* Get the name of the given segment */
133 {
134     return SS_Get (&SegmentNames[Seg]);
135 }
136
137
138
139 static Segments* NewSegments (SymEntry* Func)
140 /* Initialize a Segments structure (set all fields to NULL) */
141 {
142     /* Allocate memory */
143     Segments* S = xmalloc (sizeof (Segments));
144
145     /* Initialize the fields */
146     S->Text     = NewTextSeg (Func);
147     S->Code     = NewCodeSeg (GetSegName (SEG_CODE), Func);
148     S->Data     = NewDataSeg (GetSegName (SEG_DATA), Func);
149     S->ROData   = NewDataSeg (GetSegName (SEG_RODATA), Func);
150     S->BSS      = NewDataSeg (GetSegName (SEG_BSS), Func);
151     S->CurDSeg  = SEG_DATA;
152
153     /* Return the new struct */
154     return S;
155 }
156
157
158
159 Segments* PushSegments (SymEntry* Func)
160 /* Make the new segment list current but remember the old one */
161 {
162     /* Push the current pointer onto the stack */
163     CollAppend (&SegmentStack, CS);
164
165     /* Create a new Segments structure */
166     CS = NewSegments (Func);
167
168     /* Return the new struct */
169     return CS;
170 }
171
172
173
174 void PopSegments (void)
175 /* Pop the old segment list (make it current) */
176 {
177     /* Must have something on the stack */
178     PRECONDITION (CollCount (&SegmentStack) > 0);
179
180     /* Pop the last segment and set it as current */
181     CS = CollPop (&SegmentStack);
182 }
183
184
185
186 void UseDataSeg (segment_t DSeg)
187 /* For the current segment list, use the data segment DSeg */
188 {
189     /* Check the input */
190     PRECONDITION (CS && DSeg != SEG_CODE);
191
192     /* Set the new segment to use */
193     CS->CurDSeg = DSeg;
194 }
195
196
197
198 struct DataSeg* GetDataSeg (void)
199 /* Return the current data segment */
200 {
201     PRECONDITION (CS != 0);
202     switch (CS->CurDSeg) {
203         case SEG_BSS:     return CS->BSS;
204         case SEG_DATA:    return CS->Data;
205         case SEG_RODATA:  return CS->ROData;
206         default:
207             FAIL ("Invalid data segment");
208             return 0;
209     }
210 }
211
212
213
214 void AddTextLine (const char* Format, ...)
215 /* Add a line of code to the current text segment */
216 {
217     va_list ap;
218     va_start (ap, Format);
219     CHECK (CS != 0);
220     TS_AddVLine (CS->Text, Format, ap);
221     va_end (ap);
222 }
223
224
225
226 void AddCodeLine (const char* Format, ...)
227 /* Add a line of code to the current code segment */
228 {
229     va_list ap;
230     va_start (ap, Format);
231     CHECK (CS != 0);
232     CS_AddVLine (CS->Code, CurTok.LI, Format, ap);
233     va_end (ap);
234 }
235
236
237
238 void AddCode (opc_t OPC, am_t AM, const char* Arg, struct CodeLabel* JumpTo)
239 /* Add a code entry to the current code segment */
240 {
241     CHECK (CS != 0);
242     CS_AddEntry (CS->Code, NewCodeEntry (OPC, AM, Arg, JumpTo, CurTok.LI));
243 }
244
245
246
247 void AddDataLine (const char* Format, ...)
248 /* Add a line of data to the current data segment */
249 {
250     va_list ap;
251     va_start (ap, Format);
252     CHECK (CS != 0);
253     DS_AddVLine (GetDataSeg(), Format, ap);
254     va_end (ap);
255 }
256
257
258
259 int HaveGlobalCode (void)
260 /* Return true if the global code segment contains entries (which is an error) */
261 {
262     return (CS_GetEntryCount (GS->Code) > 0);
263 }
264
265
266
267 void RemoveGlobalCode (void)
268 /* Remove all code from the global code segment. Used for error recovery. */
269 {
270     CS_DelEntries (GS->Code, 0, CS_GetEntryCount (GS->Code));
271 }
272
273
274
275 void OutputSegments (const Segments* S, FILE* F)
276 /* Output the given segments to the file */
277 {
278     /* Output the function prologue if the segments came from a function */
279     CS_OutputPrologue (S->Code, F);
280
281     /* Output the text segment */
282     TS_Output (S->Text, F);
283
284     /* Output the three data segments */
285     DS_Output (S->Data, F);
286     DS_Output (S->ROData, F);
287     DS_Output (S->BSS, F);
288
289     /* Output the code segment */
290     CS_Output (S->Code, F);
291
292     /* Output the code segment epiloque */
293     CS_OutputEpilogue (S->Code, F);
294 }
295
296
297