]> git.sur5r.net Git - cc65/blob - src/cc65/segments.c
Working on the backend
[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 "xmalloc.h"
45
46 /* cc65 */
47 #include "codeseg.h"
48 #include "dataseg.h"
49 #include "textseg.h"
50 #include "segments.h"
51
52
53
54 /*****************************************************************************/
55 /*                                   Data                                    */
56 /*****************************************************************************/
57
58
59
60 /* Pointer to the current segment list. Output goes here. */
61 Segments* CS = 0;
62
63 /* Actual names for the segments */
64 static char* SegmentNames[SEG_COUNT];
65
66 /* We're using a collection for the stack instead of a linked list. Since
67  * functions may not be nested (at least in the current implementation), the
68  * maximum stack depth is 2, so there is not really a need for a better
69  * implementation.
70  */
71 static Collection SegmentStack = STATIC_COLLECTION_INITIALIZER;
72
73
74
75 /*****************************************************************************/
76 /*                                   Code                                    */
77 /*****************************************************************************/
78
79
80
81 void InitSegNames (void)
82 /* Initialize the segment names */
83 {
84     SegmentNames [SEG_BSS]      = xstrdup ("BSS");
85     SegmentNames [SEG_CODE]     = xstrdup ("CODE");
86     SegmentNames [SEG_DATA]     = xstrdup ("DATA");
87     SegmentNames [SEG_RODATA]   = xstrdup ("RODATA");
88 }
89
90
91
92 void NewSegName (segment_t Seg, const char* Name)
93 /* Set a new name for a segment */
94 {
95     /* Free the old name and set a new one */
96     xfree (SegmentNames [Seg]);
97     SegmentNames [Seg] = xstrdup (Name);
98 }
99
100
101
102 int ValidSegName (const char* Name)
103 /* Return true if the given segment name is valid, return false otherwise */
104 {
105     /* Must start with '_' or a letter */
106     if ((*Name != '_' && !IsAlpha(*Name)) || strlen(Name) > 80) {
107         return 0;
108     }
109
110     /* Can have letters, digits or the underline */
111     while (*++Name) {
112         if (*Name != '_' && !IsAlNum(*Name)) {
113             return 0;
114         }
115     }
116
117     /* Name is ok */
118     return 1;
119 }
120
121
122
123 static Segments* NewSegments (SymEntry* Func)
124 /* Initialize a Segments structure (set all fields to NULL) */
125 {
126     /* Allocate memory */
127     Segments* S = xmalloc (sizeof (Segments));
128
129     /* Initialize the fields */
130     S->Text     = NewTextSeg (Func);
131     S->Code     = NewCodeSeg (SegmentNames[SEG_CODE], Func);
132     S->Data     = NewDataSeg (SegmentNames[SEG_DATA], Func);
133     S->ROData   = NewDataSeg (SegmentNames[SEG_RODATA], Func);
134     S->BSS      = NewDataSeg (SegmentNames[SEG_BSS], Func);
135     S->CurDSeg  = SEG_DATA;
136
137     /* Return the new struct */
138     return S;
139 }
140
141
142
143 Segments* PushSegments (SymEntry* Func)
144 /* Make the new segment list current but remember the old one */
145 {
146     /* Push the current pointer onto the stack */
147     CollAppend (&SegmentStack, CS);
148
149     /* Create a new Segments structure */
150     CS = NewSegments (Func);
151
152     /* Return the new struct */
153     return CS;
154 }
155
156
157
158 void PopSegments (void)
159 /* Pop the old segment list (make it current) */
160 {
161     /* Must have something on the stack */
162     PRECONDITION (CollCount (&SegmentStack) > 0);
163
164     /* Pop the last segment and set it as current */
165     CS = CollPop (&SegmentStack);
166 }
167
168
169
170 void UseDataSeg (segment_t DSeg)
171 /* For the current segment list, use the data segment DSeg */
172 {
173     /* Check the input */
174     PRECONDITION (CS && DSeg != SEG_CODE);
175
176     /* Set the new segment to use */
177     CS->CurDSeg = DSeg;
178 }
179
180
181
182 struct DataSeg* GetDataSeg (void)
183 /* Return the current data segment */
184 {
185     PRECONDITION (CS != 0);
186     switch (CS->CurDSeg) {
187         case SEG_BSS:     return CS->BSS;
188         case SEG_DATA:    return CS->Data;
189         case SEG_RODATA:  return CS->ROData;
190         default:          
191             FAIL ("Invalid data segment");
192             return 0;
193     }
194 }
195
196
197
198 void AddTextLine (const char* Format, ...)
199 /* Add a line of code to the current text segment */
200 {
201     va_list ap;
202     va_start (ap, Format);
203     CHECK (CS != 0);
204     AddTextEntry (CS->Text, Format, ap);
205     va_end (ap);
206 }
207
208
209
210 void AddCodeLine (const char* Format, ...)
211 /* Add a line of code to the current code segment */
212 {
213     va_list ap;
214     va_start (ap, Format);
215     CHECK (CS != 0);
216     AddCodeEntry (CS->Code, CurTok.LI, Format, ap);
217     va_end (ap);
218 }
219
220
221
222 void AddDataLine (const char* Format, ...)
223 /* Add a line of data to the current data segment */
224 {
225     va_list ap;
226     va_start (ap, Format);
227     CHECK (CS != 0);
228     AddDataEntry (GetDataSeg(), Format, ap);
229     va_end (ap);
230 }
231
232
233
234 static void PrintFunctionHeader (const SymEntry* Entry, FILE* F)
235 {
236     /* Print a comment with the function signature */
237     fprintf (F,
238              "; ---------------------------------------------------------------\n"
239              "; ");
240     PrintFuncSig (F, Entry->Name, Entry->Type);
241     fprintf (F,
242              "\n"
243              "; ---------------------------------------------------------------\n"
244              "\n");
245 }
246
247
248
249 void OutputSegments (const Segments* S, FILE* F)
250 /* Output the given segments to the file */
251 {
252     /* If the code segment is associated with a function, print a function header */
253     if (S->Code->Func) {
254         PrintFunctionHeader (S->Code->Func, F);
255     }
256
257     /* Output the text segment */
258     OutputTextSeg (S->Text, F);
259
260     /* Output the three data segments */
261     OutputDataSeg (S->Data, F);
262     OutputDataSeg (S->ROData, F);
263     OutputDataSeg (S->BSS, F);
264
265     /* Output the code segment */
266     OutputCodeSeg (S->Code, F);
267 }
268
269
270