]> git.sur5r.net Git - cc65/blob - src/cc65/codeseg.c
Working on the backend
[cc65] / src / cc65 / codeseg.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 codeseg.c                                 */
4 /*                                                                           */
5 /*                          Code segment structure                           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001     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 <string.h>
37 #include <ctype.h>
38
39 /* common */
40 #include "chartype.h"
41 #include "check.h"
42 #include "hashstr.h"
43 #include "strutil.h"
44 #include "xmalloc.h"
45 #include "xsprintf.h"
46
47 /* b6502 */
48 #include "codeent.h"
49 #include "error.h"
50 #include "codeseg.h"
51
52
53
54 /*****************************************************************************/
55 /*                                   Code                                    */
56 /*****************************************************************************/
57
58
59
60 static const char* SkipSpace (const char* S)
61 /* Skip white space and return an updated pointer */
62 {
63     while (IsSpace (*S)) {
64         ++S;
65     }
66     return S;
67 }
68
69
70
71 static const char* ReadToken (const char* L, const char* Term,
72                               char* Buf, unsigned BufSize)
73 /* Read the next token into Buf, return the updated line pointer. The
74  * token is terminated by one of the characters given in term.
75  */
76 {
77     /* Read/copy the token */
78     unsigned I = 0;
79     unsigned ParenCount = 0;
80     while (*L && (ParenCount > 0 || strchr (Term, *L) == 0)) {
81         if (I < BufSize-1) {
82             Buf[I++] = *L;
83         }
84         if (*L == ')') {
85             --ParenCount;
86         } else if (*L == '(') {
87             ++ParenCount;
88         }
89         ++L;
90     }
91
92     /* Terminate the buffer contents */
93     Buf[I] = '\0';
94
95     /* Return the updated line pointer */
96     return L;
97 }
98
99
100
101 static CodeEntry* ParseInsn (const char* L)
102 /* Parse an instruction nnd generate a code entry from it. If the line contains
103  * errors, output an error message and return NULL.
104  * For simplicity, we don't accept the broad range of input a "real" assembler
105  * does. The instruction and the argument are expected to be separated by
106  * white space, for example.
107  */
108 {
109     char                Mnemo[16];
110     const OPCDesc*      OPC;
111     am_t                AM = 0;         /* Initialize to keep gcc silent */
112     char                Expr[64];
113     char                Reg;
114     CodeEntry*          E;
115
116     /* Mnemonic */
117     L = ReadToken (L, " \t", Mnemo, sizeof (Mnemo));
118
119     /* Try to find the opcode description for the mnemonic */
120     OPC = FindOpcode (Mnemo);
121
122     /* If we didn't find the opcode, print an error and bail out */
123     if (OPC == 0) {
124         Error ("ASM code error: %s is not a valid mnemonic", Mnemo);
125         return 0;
126     }
127
128     /* Skip separator white space */
129     L = SkipSpace (L);
130
131     /* Get the addressing mode */
132     Expr[0] = '\0';
133     switch (*L) {
134
135         case '#':
136             /* Immidiate */
137             StrCopy (Expr, sizeof (Expr), L+1);
138             AM = AM_IMM;
139             break;
140
141         case '(':
142             /* Indirect */
143             L = ReadToken (L+1, ",)", Expr, sizeof (Expr));
144
145             /* Check for errors */
146             if (*L == '\0') {
147                 Error ("ASM code error: syntax error");
148                 return 0;
149             }
150
151             /* Check the different indirect modes */
152             if (*L == ',') {
153                 /* Expect zp x indirect */
154                 L = SkipSpace (L+1);
155                 if (toupper (*L) != 'X') {
156                     Error ("ASM code error: `X' expected");
157                     return 0;
158                 }
159                 L = SkipSpace (L+1);
160                 if (*L != ')') {
161                     Error ("ASM code error: `)' expected");
162                     return 0;
163                 }
164                 L = SkipSpace (L+1);
165                 if (*L != '\0') {
166                     Error ("ASM code error: syntax error");
167                     return 0;
168                 }
169                 AM = AM_ZPX_IND;
170             } else if (*L == ')') {
171                 /* zp indirect or zp indirect, y */
172                 L = SkipSpace (L+1);
173                 if (*L == ',') {
174                     L = SkipSpace (L+1);
175                     if (toupper (*L) != 'Y') {
176                         Error ("ASM code error: `Y' expected");
177                         return 0;
178                     }
179                     L = SkipSpace (L+1);
180                     if (*L != '\0') {
181                         Error ("ASM code error: syntax error");
182                         return 0;
183                     }
184                     AM = AM_ZP_INDY;
185                 } else if (*L == '\0') {
186                     AM = AM_ZP_IND;
187                 } else {
188                     Error ("ASM code error: syntax error");
189                     return 0;
190                 }
191             }
192             break;
193
194         case 'a':
195         case 'A':
196             /* Accumulator? */
197             if (L[1] == '\0') {
198                 AM = AM_IMP;
199                 break;
200             }
201             /* FALLTHROUGH */
202
203         default:
204             /* Absolute, maybe indexed */
205             L = ReadToken (L, ",", Expr, sizeof (Expr));
206             if (*L == '\0') {
207                 /* Assume absolute */
208                 AM = AM_ABS;
209             } else if (*L == ',') {
210                 /* Indexed */
211                 L = SkipSpace (L+1);
212                 if (*L == '\0') {
213                     Error ("ASM code error: syntax error");
214                     return 0;
215                 } else {
216                     Reg = toupper (*L);
217                     L = SkipSpace (L+1);
218                     if (Reg == 'X') {
219                         AM = AM_ABSX;
220                     } else if (Reg == 'Y') {
221                         AM = AM_ABSY;
222                     } else {
223                         Error ("ASM code error: syntax error");
224                         return 0;
225                     }
226                     if (*L != '\0') {
227                         Error ("ASM code error: syntax error");
228                         return 0;
229                     }
230                 }
231             }
232             break;
233
234     }
235
236     /* We do now have the addressing mode in AM. Allocate a new CodeEntry
237      * structure and initialize it.
238      */
239     E = NewCodeEntry (OPC, AM);
240     if (Expr[0] != '\0') {
241         /* We have an additional expression */
242         E->Arg.Expr = xstrdup (Expr);
243     }
244
245     /* Return the new code entry */
246     return E;
247 }
248
249
250
251 CodeSeg* NewCodeSeg (const char* Name)
252 /* Create a new code segment, initialize and return it */
253 {
254     unsigned I;
255
256     /* Allocate memory */
257     CodeSeg* S = xmalloc (sizeof (CodeSeg));
258
259     /* Initialize the fields */
260     S->Name = xstrdup (Name);
261     InitCollection (&S->Entries);
262     InitCollection (&S->Labels);
263     for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
264         S->LabelHash[I] = 0;
265     }
266
267     /* Return the new struct */
268     return S;
269 }
270
271
272
273 void FreeCodeSeg (CodeSeg* S)
274 /* Free a code segment including all code entries */
275 {
276     unsigned I, Count;
277
278     /* Free the name */
279     xfree (S->Name);
280
281     /* Free the entries */
282     Count = CollCount (&S->Entries);
283     for (I = 0; I < Count; ++I) {
284         FreeCodeEntry (CollAt (&S->Entries, I));
285     }
286
287     /* Free the collections */
288     DoneCollection (&S->Entries);
289     DoneCollection (&S->Labels);
290
291     /* Free all labels */
292     for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
293         CodeLabel* L = S->LabelHash[I];
294         while (L) {
295             CodeLabel* Tmp = L;
296             L = L->Next;
297             FreeCodeLabel (Tmp);
298         }
299     }
300
301     /* Free the struct */
302     xfree (S);
303 }
304
305
306
307 void AddCodeSegLine (CodeSeg* S, const char* Format, ...)
308 /* Add a line to the given code segment */
309 {
310     const char* L;
311     CodeEntry*  E;
312
313     /* Format the line */
314     va_list ap;
315     char Buf [256];
316     va_start (ap, Format);
317     xvsprintf (Buf, sizeof (Buf), Format, ap);
318     va_end (ap);
319
320     /* Skip whitespace */
321     L = SkipSpace (Buf);
322
323     /* Check which type of instruction we have */
324     E = 0;      /* Assume no insn created */
325     switch (*L) {
326
327         case '\0':
328             /* Empty line, just ignore it */
329             break;
330
331         case ';':
332             /* Comment or hint, ignore it for now */
333             break;
334
335         case '.':
336             /* Control instruction */
337             Error ("ASM code error: Pseudo instructions not supported");
338             break;
339
340         default:
341             E = ParseInsn (L);
342             break;
343     }
344
345     /* If we have a code entry, transfer the labels and insert it */
346     if (E) {
347
348         /* Transfer the labels if we have any */
349         unsigned LabelCount = CollCount (&S->Labels);
350         unsigned I;
351         for (I = 0; I < LabelCount; ++I) {
352             CollAppend (&E->Labels, CollAt (&S->Labels, I));
353         }
354         CollDeleteAll (&S->Labels);
355
356         /* Add the entry to the list of code entries in this segment */
357         CollAppend (&S->Entries, E);
358
359     }
360 }
361
362
363
364 void AddCodeSegLabel (CodeSeg* S, const char* Name)
365 /* Add a label for the next instruction to follow */
366 {
367     /* Calculate the hash from the name */
368     unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
369
370     /* Try to find the code label if it does already exist */
371     CodeLabel* L = FindCodeLabel (S, Name, Hash);
372
373     /* Did we find it? */
374     if (L) {
375         /* We found it - be sure it does not already have an owner */
376         CHECK (L->Owner == 0);
377     } else {
378         /* Not found - create a new one */
379         L = NewCodeLabel (Name, Hash);
380
381         /* Enter the label into the hash table */
382         L->Next = S->LabelHash[L->Hash];
383         S->LabelHash[L->Hash] = L;
384     }
385
386     /* We do now have a valid label. Remember it for later */
387     CollAppend (&S->Labels, L);
388 }
389
390
391
392 void OutputCodeSeg (FILE* F, const CodeSeg* S)
393 /* Output the code segment data to a file */
394 {
395     unsigned I;
396
397     /* Get the number of entries in this segment */
398     unsigned Count = CollCount (&S->Entries);
399
400     /* Output all entries */
401     for (I = 0; I < Count; ++I) {
402         OutputCodeEntry (F, CollConstAt (&S->Entries, I));
403     }
404 }
405
406
407
408 CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
409 /* Find the label with the given name. Return the label or NULL if not found */
410 {
411     /* Get the first hash chain entry */
412     CodeLabel* L = S->LabelHash[Hash];
413
414     /* Search the list */
415     while (L) {
416         if (strcmp (Name, L->Name) == 0) {
417             /* Found */
418             break;
419         }
420         L = L->Next;
421     }
422     return L;
423 }
424
425
426
427 void MergeCodeLabels (CodeSeg* S)
428 /* Merge code labels. That means: For each instruction, remove all labels but
429  * one and adjust the code entries accordingly.
430  */
431 {
432     unsigned I;
433
434     /* Walk over all code entries */
435     unsigned EntryCount = CollCount (&S->Entries);
436     for (I = 0; I < EntryCount; ++I) {
437
438         CodeLabel* RefLab;
439         unsigned   J;
440
441         /* Get a pointer to the next entry */
442         CodeEntry* E = CollAt (&S->Entries, I);
443
444         /* If this entry has one or zero labels, continue with the next one */
445         unsigned LabelCount = CollCount (&E->Labels);
446         if (LabelCount <= 1) {
447             continue;
448         }
449
450         /* We have more than one label. Use the first one as reference label */
451         RefLab = CollAt (&E->Labels, 0);
452
453         /* Walk through the remaining labels and change references to these
454          * labels to a reference to the one and only label. Delete the labels
455          * that are no longer used. To increase performance, walk backwards
456          * through the list.
457          */
458         for (J = LabelCount-1; J >= 1; --J) {
459
460             unsigned K;
461
462             /* Get the next label */
463             CodeLabel* L = CollAt (&E->Labels, J);
464
465             /* Walk through all instructions referencing this label */
466             unsigned RefCount = CollCount (&L->JumpFrom);
467             for (K = 0; K < RefCount; ++K) {
468
469                 /* Get the next instrcuction that references this label */
470                 CodeEntry* E = CollAt (&L->JumpFrom, K);
471
472                 /* Change the reference */
473                 CHECK (E->JumpTo == L);
474                 E->JumpTo = RefLab;
475                 CollAppend (&RefLab->JumpFrom, E);
476
477             }
478
479             /* Delete the label */
480             FreeCodeLabel (L);
481
482             /* Remove it from the list */
483             CollDelete (&E->Labels, J);
484
485         }
486     }
487 }
488
489
490