1 /*****************************************************************************/
5 /* Lightweight segment management stuff */
9 /* (C) 2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
55 /*****************************************************************************/
57 /*****************************************************************************/
61 /* Pointer to the current segment list. Output goes here. */
64 /* Actual names for the segments */
65 static char* SegmentNames[SEG_COUNT];
67 /* We're using a collection for the stack instead of a linked list. Since
68 * functions may not be nested (at least in the current implementation), the
69 * maximum stack depth is 2, so there is not really a need for a better
72 static Collection SegmentStack = STATIC_COLLECTION_INITIALIZER;
76 /*****************************************************************************/
78 /*****************************************************************************/
82 void InitSegNames (void)
83 /* Initialize the segment names */
85 SegmentNames [SEG_BSS] = xstrdup ("BSS");
86 SegmentNames [SEG_CODE] = xstrdup ("CODE");
87 SegmentNames [SEG_DATA] = xstrdup ("DATA");
88 SegmentNames [SEG_RODATA] = xstrdup ("RODATA");
93 void NewSegName (segment_t Seg, const char* Name)
94 /* Set a new name for a segment */
96 /* Free the old name and set a new one */
97 xfree (SegmentNames [Seg]);
98 SegmentNames [Seg] = xstrdup (Name);
103 int ValidSegName (const char* Name)
104 /* Return true if the given segment name is valid, return false otherwise */
106 /* Must start with '_' or a letter */
107 if ((*Name != '_' && !IsAlpha(*Name)) || strlen(Name) > 80) {
111 /* Can have letters, digits or the underline */
113 if (*Name != '_' && !IsAlNum(*Name)) {
124 static Segments* NewSegments (SymEntry* Func)
125 /* Initialize a Segments structure (set all fields to NULL) */
127 /* Allocate memory */
128 Segments* S = xmalloc (sizeof (Segments));
130 /* Initialize the fields */
131 S->Text = NewTextSeg (Func);
132 S->Code = NewCodeSeg (SegmentNames[SEG_CODE], Func);
133 S->Data = NewDataSeg (SegmentNames[SEG_DATA], Func);
134 S->ROData = NewDataSeg (SegmentNames[SEG_RODATA], Func);
135 S->BSS = NewDataSeg (SegmentNames[SEG_BSS], Func);
136 S->CurDSeg = SEG_DATA;
138 /* Return the new struct */
144 Segments* PushSegments (SymEntry* Func)
145 /* Make the new segment list current but remember the old one */
147 /* Push the current pointer onto the stack */
148 CollAppend (&SegmentStack, CS);
150 /* Create a new Segments structure */
151 CS = NewSegments (Func);
153 /* Return the new struct */
159 void PopSegments (void)
160 /* Pop the old segment list (make it current) */
162 /* Must have something on the stack */
163 PRECONDITION (CollCount (&SegmentStack) > 0);
165 /* Pop the last segment and set it as current */
166 CS = CollPop (&SegmentStack);
171 void UseDataSeg (segment_t DSeg)
172 /* For the current segment list, use the data segment DSeg */
174 /* Check the input */
175 PRECONDITION (CS && DSeg != SEG_CODE);
177 /* Set the new segment to use */
183 struct DataSeg* GetDataSeg (void)
184 /* Return the current data segment */
186 PRECONDITION (CS != 0);
187 switch (CS->CurDSeg) {
188 case SEG_BSS: return CS->BSS;
189 case SEG_DATA: return CS->Data;
190 case SEG_RODATA: return CS->ROData;
192 FAIL ("Invalid data segment");
199 void AddTextLine (const char* Format, ...)
200 /* Add a line of code to the current text segment */
203 va_start (ap, Format);
205 TS_AddLine (CS->Text, Format, ap);
211 void AddCodeLine (const char* Format, ...)
212 /* Add a line of code to the current code segment */
215 va_start (ap, Format);
217 CS_AddLine (CS->Code, CurTok.LI, Format, ap);
223 void AddCode (opc_t OPC, am_t AM, const char* Arg, struct CodeLabel* JumpTo)
224 /* Add a code entry to the current code segment */
227 CS_AddEntry (CS->Code, NewCodeEntry (OPC, AM, Arg, JumpTo, CurTok.LI));
232 void AddDataLine (const char* Format, ...)
233 /* Add a line of data to the current data segment */
236 va_start (ap, Format);
238 DS_AddLine (GetDataSeg(), Format, ap);
244 static void PrintFunctionHeader (const SymEntry* Entry, FILE* F)
246 /* Print a comment with the function signature */
248 "; ---------------------------------------------------------------\n"
250 PrintFuncSig (F, Entry->Name, Entry->Type);
253 "; ---------------------------------------------------------------\n"
259 void OutputSegments (const Segments* S, FILE* F)
260 /* Output the given segments to the file */
262 /* If the code segment is associated with a function, print a function header */
264 PrintFunctionHeader (S->Code->Func, F);
267 /* Output the text segment */
268 TS_Output (S->Text, F);
270 /* Output the three data segments */
271 DS_Output (S->Data, F);
272 DS_Output (S->ROData, F);
273 DS_Output (S->BSS, F);
275 /* Output the code segment */
276 CS_Output (S->Code, F);