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 /* Pointer to the global segment list */
67 /* Actual names for the segments */
68 static char* SegmentNames[SEG_COUNT];
70 /* We're using a collection for the stack instead of a linked list. Since
71 * functions may not be nested (at least in the current implementation), the
72 * maximum stack depth is 2, so there is not really a need for a better
75 static Collection SegmentStack = STATIC_COLLECTION_INITIALIZER;
79 /*****************************************************************************/
81 /*****************************************************************************/
85 void InitSegNames (void)
86 /* Initialize the segment names */
88 SegmentNames [SEG_BSS] = xstrdup ("BSS");
89 SegmentNames [SEG_CODE] = xstrdup ("CODE");
90 SegmentNames [SEG_DATA] = xstrdup ("DATA");
91 SegmentNames [SEG_RODATA] = xstrdup ("RODATA");
96 void NewSegName (segment_t Seg, const char* Name)
97 /* Set a new name for a segment */
99 /* Free the old name and set a new one */
100 xfree (SegmentNames [Seg]);
101 SegmentNames [Seg] = xstrdup (Name);
106 static Segments* NewSegments (SymEntry* Func)
107 /* Initialize a Segments structure (set all fields to NULL) */
109 /* Allocate memory */
110 Segments* S = xmalloc (sizeof (Segments));
112 /* Initialize the fields */
113 S->Text = NewTextSeg (Func);
114 S->Code = NewCodeSeg (SegmentNames[SEG_CODE], Func);
115 S->Data = NewDataSeg (SegmentNames[SEG_DATA], Func);
116 S->ROData = NewDataSeg (SegmentNames[SEG_RODATA], Func);
117 S->BSS = NewDataSeg (SegmentNames[SEG_BSS], Func);
118 S->CurDSeg = SEG_DATA;
120 /* Return the new struct */
126 Segments* PushSegments (SymEntry* Func)
127 /* Make the new segment list current but remember the old one */
129 /* Push the current pointer onto the stack */
130 CollAppend (&SegmentStack, CS);
132 /* Create a new Segments structure */
133 CS = NewSegments (Func);
135 /* Return the new struct */
141 void PopSegments (void)
142 /* Pop the old segment list (make it current) */
144 /* Must have something on the stack */
145 PRECONDITION (CollCount (&SegmentStack) > 0);
147 /* Pop the last segment and set it as current */
148 CS = CollPop (&SegmentStack);
153 void UseDataSeg (segment_t DSeg)
154 /* For the current segment list, use the data segment DSeg */
156 /* Check the input */
157 PRECONDITION (CS && DSeg != SEG_CODE);
159 /* Set the new segment to use */
165 struct DataSeg* GetDataSeg (void)
166 /* Return the current data segment */
168 PRECONDITION (CS != 0);
169 switch (CS->CurDSeg) {
170 case SEG_BSS: return CS->BSS;
171 case SEG_DATA: return CS->Data;
172 case SEG_RODATA: return CS->ROData;
174 FAIL ("Invalid data segment");
181 void AddTextLine (const char* Format, ...)
182 /* Add a line of code to the current text segment */
185 va_start (ap, Format);
187 TS_AddVLine (CS->Text, Format, ap);
193 void AddCodeLine (const char* Format, ...)
194 /* Add a line of code to the current code segment */
197 va_start (ap, Format);
199 CS_AddVLine (CS->Code, CurTok.LI, Format, ap);
205 void AddCode (opc_t OPC, am_t AM, const char* Arg, struct CodeLabel* JumpTo)
206 /* Add a code entry to the current code segment */
209 CS_AddEntry (CS->Code, NewCodeEntry (OPC, AM, Arg, JumpTo, CurTok.LI));
214 void AddDataLine (const char* Format, ...)
215 /* Add a line of data to the current data segment */
218 va_start (ap, Format);
220 DS_AddVLine (GetDataSeg(), Format, ap);
226 void OutputSegments (const Segments* S, FILE* F)
227 /* Output the given segments to the file */
229 /* Output the function prologue if the segments came from a function */
230 CS_OutputPrologue (S->Code, F);
232 /* Output the text segment */
233 TS_Output (S->Text, F);
235 /* Output the three data segments */
236 DS_Output (S->Data, F);
237 DS_Output (S->ROData, F);
238 DS_Output (S->BSS, F);
240 /* Output the code segment */
241 CS_Output (S->Code, F);
243 /* Output the code segment epiloque */
244 CS_OutputEpilogue (S->Code, F);