]> git.sur5r.net Git - cc65/blob - src/cc65/asmline.c
Working on the backend
[cc65] / src / cc65 / asmline.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 asmline.h                                 */
4 /*                                                                           */
5 /*                     Internal assembler line structure                     */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
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 <stdio.h>
37 #include <string.h>
38
39 /* common */
40 #include "xmalloc.h"
41 #include "xsprintf.h"
42
43 /* cc65 */
44 #include "error.h"
45 #include "asmline.h"
46
47
48
49 /*****************************************************************************/
50 /*                                   Data                                    */
51 /*****************************************************************************/
52
53
54
55 /* Number used to index lines */
56 static unsigned long LineIndex = 0;
57
58 /* The line list */
59 Line*  FirstLine = 0;           /* Pointer to first line */
60 Line*  LastLine  = 0;           /* Pointer to last line */
61
62
63
64 /*****************************************************************************/
65 /*                                   Code                                    */
66 /*****************************************************************************/
67
68
69
70 static Line* NewLine (const char* Format, va_list ap)
71 /* Interal routine to create a new line from the given text */
72 {
73     char        Buf [8192];
74     unsigned    Len;
75     Line*       L;
76
77
78     /* Make a string from the given format and arguments */
79     xvsprintf (Buf, sizeof (Buf), Format, ap);
80
81     /* Get the length of the line */
82     Len = strlen (Buf);
83
84     /* Allocate memory */
85     L = (Line*) xmalloc (sizeof (Line) + Len);
86
87     /* Partially initialize the struct (the remaining fields are initialized
88      * by the caller).
89      */
90     L->Flags    = 0;
91     L->Size     = 0;
92     L->Len      = Len;
93     memcpy (L->Line, Buf, Len+1);
94
95     /* Return the new line */
96     return L;
97 }
98
99
100
101 Line* NewCodeLine (const char* Format, va_list ap)
102 /* Create a new code line and return it */
103 {
104     /* Create a new line struct */
105     Line* L = NewLine (Format, ap);
106
107     /* Initialize struct fields */
108     L->Index    = LineIndex++;
109
110     /* Insert the line into the list */
111     if (FirstLine == 0) {
112         /* The list is empty */
113         L->Next = L->Prev = 0;
114         FirstLine = LastLine = L;
115     } else {
116         /* There are entries in the list, add the new one at the end */
117         LastLine->Next = L;
118         L->Prev = LastLine;
119         L->Next = 0;
120         LastLine = L;
121     }
122
123     /* Return the new line */
124     return L;
125 }
126
127
128
129 Line* NewCodeLineAfter (Line* LineBefore, const char* Format, va_list ap)
130 /* Create a new line, insert it after L and return it. */
131 {
132     /* Create a new line struct */
133     Line* L = NewLine (Format, ap);
134
135     /* Initialize struct fields. We use the same index for the inserted line
136      * as for its predecessor, since we cannot create new numbers on the
137      * fly and the index is only used to determine sort order.
138      */
139     L->Index    = LineBefore->Index;
140
141     /* Insert the line after its predecessor */
142     L->Next = LineBefore->Next;
143     LineBefore->Next = L;
144     L->Prev = LineBefore;
145     if (L->Next) {
146         L->Next->Prev = L;
147     } else {
148         /* This is the last line */
149         LastLine = L;
150     }
151
152     /* Return the new line */
153     return L;
154 }
155
156
157
158 void FreeCodeLine (Line* L)
159 /* Remove a line from the list and free it */
160 {
161     /* Unlink the line */
162     if (L->Prev == 0) {
163         /* No line before this one */
164         FirstLine = L->Next;
165     } else {
166         L->Prev->Next = L->Next;
167     }
168     if (L->Next == 0) {
169         /* No line after this one */
170         LastLine = L->Prev;
171     } else {
172         L->Next->Prev = L->Prev;
173     }
174
175     /* Free the struct */
176     xfree (L);
177 }
178
179
180