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 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
58 /* Pointer to the current segment list. Output goes here. */
61 /* Actual names for the segments */
62 static char* SegmentNames[SEG_COUNT];
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
69 static Collection SegmentStack = STATIC_COLLECTION_INITIALIZER;
73 /*****************************************************************************/
75 /*****************************************************************************/
79 void InitSegNames (void)
80 /* Initialize the segment names */
82 SegmentNames [SEG_BSS] = xstrdup ("BSS");
83 SegmentNames [SEG_CODE] = xstrdup ("CODE");
84 SegmentNames [SEG_DATA] = xstrdup ("DATA");
85 SegmentNames [SEG_RODATA] = xstrdup ("RODATA");
90 void NewSegName (segment_t Seg, const char* Name)
91 /* Set a new name for a segment */
93 /* Free the old name and set a new one */
94 xfree (SegmentNames [Seg]);
95 SegmentNames [Seg] = xstrdup (Name);
100 int ValidSegName (const char* Name)
101 /* Return true if the given segment name is valid, return false otherwise */
103 /* Must start with '_' or a letter */
104 if ((*Name != '_' && !IsAlpha(*Name)) || strlen(Name) > 80) {
108 /* Can have letters, digits or the underline */
110 if (*Name != '_' && !IsAlNum(*Name)) {
121 static Segments* NewSegments (SymEntry* Func)
122 /* Initialize a Segments structure (set all fields to NULL) */
124 /* Allocate memory */
125 Segments* S = xmalloc (sizeof (Segments));
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;
134 /* Return the new struct */
140 Segments* PushSegments (SymEntry* Func)
141 /* Make the new segment list current but remember the old one */
143 /* Push the current pointer onto the stack */
144 CollAppend (&SegmentStack, CS);
146 /* Create a new Segments structure */
147 CS = NewSegments (Func);
149 /* Return the new struct */
155 void PopSegments (void)
156 /* Pop the old segment list (make it current) */
158 /* Must have something on the stack */
159 PRECONDITION (CollCount (&SegmentStack) > 0);
161 /* Pop the last segment and set it as current */
162 CS = CollPop (&SegmentStack);
167 void UseDataSeg (segment_t DSeg)
168 /* For the current segment list, use the data segment DSeg */
170 /* Check the input */
171 PRECONDITION (CS && DSeg != SEG_CODE);
173 /* Set the new segment to use */
179 struct DataSeg* GetDataSeg (void)
180 /* Return the current data segment */
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");
193 void AddCodeLine (const char* Format, ...)
194 /* Add a line of code to the current code segment */
197 va_start (ap, Format);
199 AddCodeEntry (CS->Code, Format, ap);
205 void AddDataLine (const char* Format, ...)
206 /* Add a line of data to the current data segment */
209 va_start (ap, Format);
211 AddDataEntry (GetDataSeg(), Format, ap);
217 static void PrintFunctionHeader (const SymEntry* Entry, FILE* F)
219 /* Print a comment with the function signature */
221 "; ---------------------------------------------------------------\n"
223 PrintFuncSig (F, Entry->Name, Entry->Type);
226 "; ---------------------------------------------------------------\n"
232 void OutputSegments (const Segments* S, FILE* F)
233 /* Output the given segments to the file */
235 /* If the code segment is associated with a function, print a function header */
237 PrintFunctionHeader (S->Code->Func, F);
240 /* Output the three data segments */
241 OutputDataSeg (S->Data, F);
242 OutputDataSeg (S->ROData, F);
243 OutputDataSeg (S->BSS, F);
245 /* Output the code segment */
246 OutputCodeSeg (S->Code, F);