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