]> git.sur5r.net Git - cc65/blob - src/cc65/codeseg.c
Working on the new 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@cc65.org                                                 */
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 /* cc65 */
48 #include "asmlabel.h"
49 #include "codeent.h"
50 #include "codeinfo.h"
51 #include "error.h"
52 #include "codeseg.h"
53
54
55
56 /*****************************************************************************/
57 /*                             Helper functions                              */
58 /*****************************************************************************/
59
60
61
62 static void MoveLabelsToPool (CodeSeg* S, CodeEntry* E)
63 /* Move the labels of the code entry E to the label pool of the code segment */
64 {
65     unsigned LabelCount = GetCodeLabelCount (E);
66     while (LabelCount--) {
67         CodeLabel* L = GetCodeLabel (E, LabelCount);
68         L->Flags &= ~LF_DEF;
69         L->Owner = 0;
70         CollAppend (&S->Labels, L);
71     }
72     CollDeleteAll (&E->Labels);
73 }
74
75
76
77 static CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
78 /* Find the label with the given name. Return the label or NULL if not found */
79 {
80     /* Get the first hash chain entry */
81     CodeLabel* L = S->LabelHash[Hash];
82
83     /* Search the list */
84     while (L) {
85         if (strcmp (Name, L->Name) == 0) {
86             /* Found */
87             break;
88         }
89         L = L->Next;
90     }
91     return L;
92 }
93
94
95
96 static CodeLabel* NewCodeSegLabel (CodeSeg* S, const char* Name, unsigned Hash)
97 /* Create a new label and insert it into the label hash table */
98 {
99     /* Not found - create a new one */
100     CodeLabel* L = NewCodeLabel (Name, Hash);
101
102     /* Enter the label into the hash table */
103     L->Next = S->LabelHash[L->Hash];
104     S->LabelHash[L->Hash] = L;
105
106     /* Return the new label */
107     return L;
108 }
109
110
111
112 static void RemoveLabelFromHash (CodeSeg* S, CodeLabel* L)
113 /* Remove the given code label from the hash list */
114 {
115     /* Get the first entry in the hash chain */
116     CodeLabel* List = S->LabelHash[L->Hash];
117     CHECK (List != 0);
118
119     /* First, remove the label from the hash chain */
120     if (List == L) {
121         /* First entry in hash chain */
122         S->LabelHash[L->Hash] = L->Next;
123     } else {
124         /* Must search through the chain */
125         while (List->Next != L) {
126             /* If we've reached the end of the chain, something is *really* wrong */
127             CHECK (List->Next != 0);
128             /* Next entry */
129             List = List->Next;
130         }
131         /* The next entry is the one, we have been searching for */
132         List->Next = L->Next;
133     }
134 }
135
136
137
138 /*****************************************************************************/
139 /*                    Functions for parsing instructions                     */
140 /*****************************************************************************/
141
142
143
144 static const char* SkipSpace (const char* S)
145 /* Skip white space and return an updated pointer */
146 {
147     while (IsSpace (*S)) {
148         ++S;
149     }
150     return S;
151 }
152
153
154
155 static const char* ReadToken (const char* L, const char* Term,
156                               char* Buf, unsigned BufSize)
157 /* Read the next token into Buf, return the updated line pointer. The
158  * token is terminated by one of the characters given in term.
159  */
160 {
161     /* Read/copy the token */
162     unsigned I = 0;
163     unsigned ParenCount = 0;
164     while (*L && (ParenCount > 0 || strchr (Term, *L) == 0)) {
165         if (I < BufSize-1) {
166             Buf[I++] = *L;
167         }
168         if (*L == ')') {
169             --ParenCount;
170         } else if (*L == '(') {
171             ++ParenCount;
172         }
173         ++L;
174     }
175
176     /* Terminate the buffer contents */
177     Buf[I] = '\0';
178
179     /* Return the updated line pointer */
180     return L;
181 }
182
183
184
185 static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
186 /* Parse an instruction nnd generate a code entry from it. If the line contains
187  * errors, output an error message and return NULL.
188  * For simplicity, we don't accept the broad range of input a "real" assembler
189  * does. The instruction and the argument are expected to be separated by
190  * white space, for example.
191  */
192 {
193     char                Mnemo[16];
194     const OPCDesc*      OPC;
195     am_t                AM = 0;         /* Initialize to keep gcc silent */
196     char                Arg[64];
197     char                Reg;
198     CodeEntry*          E;
199     CodeLabel*          Label;
200
201     /* Mnemonic */
202     L = ReadToken (L, " \t", Mnemo, sizeof (Mnemo));
203
204     /* Try to find the opcode description for the mnemonic */
205     OPC = FindOpcode (Mnemo);
206
207     /* If we didn't find the opcode, print an error and bail out */
208     if (OPC == 0) {
209         Error ("ASM code error: %s is not a valid mnemonic", Mnemo);
210         return 0;
211     }
212
213     /* Skip separator white space */
214     L = SkipSpace (L);
215
216     /* Get the addressing mode */
217     Arg[0] = '\0';
218     switch (*L) {
219
220         case '\0':
221             /* Implicit */
222             AM = AM_IMP;
223             break;
224
225         case '#':
226             /* Immidiate */
227             StrCopy (Arg, sizeof (Arg), L+1);
228             AM = AM_IMM;
229             break;
230
231         case '(':
232             /* Indirect */
233             L = ReadToken (L+1, ",)", Arg, sizeof (Arg));
234
235             /* Check for errors */
236             if (*L == '\0') {
237                 Error ("ASM code error: syntax error");
238                 return 0;
239             }
240
241             /* Check the different indirect modes */
242             if (*L == ',') {
243                 /* Expect zp x indirect */
244                 L = SkipSpace (L+1);
245                 if (toupper (*L) != 'X') {
246                     Error ("ASM code error: `X' expected");
247                     return 0;
248                 }
249                 L = SkipSpace (L+1);
250                 if (*L != ')') {
251                     Error ("ASM code error: `)' expected");
252                     return 0;
253                 }
254                 L = SkipSpace (L+1);
255                 if (*L != '\0') {
256                     Error ("ASM code error: syntax error");
257                     return 0;
258                 }
259                 AM = AM_ZPX_IND;
260             } else if (*L == ')') {
261                 /* zp indirect or zp indirect, y */
262                 L = SkipSpace (L+1);
263                 if (*L == ',') {
264                     L = SkipSpace (L+1);
265                     if (toupper (*L) != 'Y') {
266                         Error ("ASM code error: `Y' expected");
267                         return 0;
268                     }
269                     L = SkipSpace (L+1);
270                     if (*L != '\0') {
271                         Error ("ASM code error: syntax error");
272                         return 0;
273                     }
274                     AM = AM_ZP_INDY;
275                 } else if (*L == '\0') {
276                     AM = AM_ZP_IND;
277                 } else {
278                     Error ("ASM code error: syntax error");
279                     return 0;
280                 }
281             }
282             break;
283
284         case 'a':
285         case 'A':
286             /* Accumulator? */
287             if (L[1] == '\0') {
288                 AM = AM_ACC;
289                 break;
290             }
291             /* FALLTHROUGH */
292
293         default:
294             /* Absolute, maybe indexed */
295             L = ReadToken (L, ",", Arg, sizeof (Arg));
296             if (*L == '\0') {
297                 /* Assume absolute */
298                 AM = AM_ABS;
299             } else if (*L == ',') {
300                 /* Indexed */
301                 L = SkipSpace (L+1);
302                 if (*L == '\0') {
303                     Error ("ASM code error: syntax error");
304                     return 0;
305                 } else {
306                     Reg = toupper (*L);
307                     L = SkipSpace (L+1);
308                     if (Reg == 'X') {
309                         AM = AM_ABSX;
310                     } else if (Reg == 'Y') {
311                         AM = AM_ABSY;
312                     } else {
313                         Error ("ASM code error: syntax error");
314                         return 0;
315                     }
316                     if (*L != '\0') {
317                         Error ("ASM code error: syntax error");
318                         return 0;
319                     }
320                 }
321             }
322             break;
323
324     }
325
326     /* If the instruction is a branch, check for the label and generate it
327      * if it does not exist. Ignore anything but local labels here.
328      */
329     Label = 0;
330     if ((OPC->Info & OF_BRA) != 0 && Arg[0] == 'L') {
331
332         unsigned Hash;
333
334         /* Addressing mode must be alsobute or something is really wrong */
335         CHECK (AM == AM_ABS);
336
337         /* Addressing mode is a branch/jump */
338         AM = AM_BRA;
339
340         /* Generate the hash over the label, then search for the label */
341         Hash = HashStr (Arg) % CS_LABEL_HASH_SIZE;
342         Label = FindCodeLabel (S, Arg, Hash);
343
344         /* If we don't have the label, it's a forward ref - create it */
345         if (Label == 0) {
346             /* Generate a new label */
347             Label = NewCodeSegLabel (S, Arg, Hash);
348         }
349     }
350
351     /* We do now have the addressing mode in AM. Allocate a new CodeEntry
352      * structure and initialize it.
353      */
354     E = NewCodeEntry (OPC, AM, Arg, Label);
355
356     /* Return the new code entry */
357     return E;
358 }
359
360
361
362 /*****************************************************************************/
363 /*                                   Code                                    */
364 /*****************************************************************************/
365
366
367
368 CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func)
369 /* Create a new code segment, initialize and return it */
370 {
371     unsigned I;
372
373     /* Allocate memory */
374     CodeSeg* S = xmalloc (sizeof (CodeSeg));
375
376     /* Initialize the fields */
377     S->SegName  = xstrdup (SegName);
378     S->Func     = Func;
379     InitCollection (&S->Entries);
380     InitCollection (&S->Labels);
381     for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
382         S->LabelHash[I] = 0;
383     }
384
385     /* Return the new struct */
386     return S;
387 }
388
389
390
391 void AddCodeEntry (CodeSeg* S, const char* Format, va_list ap)
392 /* Add a line to the given code segment */
393 {
394     const char* L;
395     CodeEntry*  E;
396     char        Token[64];
397
398     /* Format the line */
399     char Buf [256];
400     xvsprintf (Buf, sizeof (Buf), Format, ap);
401
402     /* Skip whitespace */
403     L = SkipSpace (Buf);
404
405     /* Check which type of instruction we have */
406     E = 0;      /* Assume no insn created */
407     switch (*L) {
408
409         case '\0':
410             /* Empty line, just ignore it */
411             break;
412
413         case ';':
414             /* Comment or hint, ignore it for now */
415             break;
416
417         case '.':
418             /* Control instruction */
419             ReadToken (L, " \t", Token, sizeof (Token));
420             Error ("ASM code error: Pseudo instruction `%s' not supported", Token);
421             break;
422
423         default:
424             E = ParseInsn (S, L);
425             break;
426     }
427
428     /* If we have a code entry, transfer the labels and insert it */
429     if (E) {
430
431         /* Transfer the labels if we have any */
432         unsigned I;
433         unsigned LabelCount = CollCount (&S->Labels);
434         for (I = 0; I < LabelCount; ++I) {
435
436             /* Get the label */
437             CodeLabel* L = CollAt (&S->Labels, I);
438
439             /* Attach it to the entry */
440             AttachCodeLabel (E, L);
441         }
442
443         /* Delete the transfered labels */
444         CollDeleteAll (&S->Labels);
445
446         /* Add the entry to the list of code entries in this segment */
447         CollAppend (&S->Entries, E);
448
449     }
450 }
451
452
453
454 void InsertCodeEntry (CodeSeg* S, struct CodeEntry* E, unsigned Index)
455 /* Insert the code entry at the index given. Following code entries will be
456  * moved to slots with higher indices.
457  */
458 {
459     /* Insert the entry into the collection */
460     CollInsert (&S->Entries, E, Index);
461 }
462
463
464
465 void DelCodeEntry (CodeSeg* S, unsigned Index)
466 /* Delete an entry from the code segment. This includes moving any associated
467  * labels, removing references to labels and even removing the referenced labels
468  * if the reference count drops to zero.
469  */
470 {
471     /* Get the code entry for the given index */
472     CodeEntry* E = GetCodeEntry (S, Index);
473
474     /* If the entry has a labels, we have to move this label to the next insn.
475      * If there is no next insn, move the label into the code segement label
476      * pool. The operation is further complicated by the fact that the next
477      * insn may already have a label. In that case change all reference to
478      * this label and delete the label instead of moving it.
479      */
480     unsigned Count = GetCodeLabelCount (E);
481     if (Count > 0) {
482
483         /* The instruction has labels attached. Check if there is a next
484          * instruction.
485          */
486         if (Index == GetCodeEntryCount (S)-1) {
487
488             /* No next instruction, move to the codeseg label pool */
489             MoveLabelsToPool (S, E);
490
491         } else {
492
493             /* There is a next insn, get it */
494             CodeEntry* N = GetCodeEntry (S, Index+1);
495
496             /* Move labels to the next entry */
497             MoveCodeLabels (S, E, N);
498
499         }
500     }
501
502     /* If this insn references a label, remove the reference. And, if the
503      * the reference count for this label drops to zero, remove this label.
504      */
505     if (E->JumpTo) {
506         /* Remove the reference */
507         RemoveCodeLabelRef (S, E);
508     }
509
510     /* Delete the pointer to the insn */
511     CollDelete (&S->Entries, Index);
512
513     /* Delete the instruction itself */
514     FreeCodeEntry (E);
515 }
516
517
518
519 struct CodeEntry* GetCodeEntry (CodeSeg* S, unsigned Index)
520 /* Get an entry from the given code segment */
521 {
522     return CollAt (&S->Entries, Index);
523 }
524
525
526
527 struct CodeEntry* GetNextCodeEntry (CodeSeg* S, unsigned Index)
528 /* Get the code entry following the one with the index Index. If there is no
529  * following code entry, return NULL.
530  */
531 {
532     if (Index >= CollCount (&S->Entries)-1) {
533         /* This is the last entry */
534         return 0;
535     } else {
536         /* Code entries left */
537         return CollAt (&S->Entries, Index+1);
538     }
539 }
540
541
542
543 unsigned GetCodeEntryIndex (CodeSeg* S, struct CodeEntry* E)
544 /* Return the index of a code entry */
545 {
546     int Index = CollIndex (&S->Entries, E);
547     CHECK (Index >= 0);
548     return Index;
549 }
550
551
552
553 void AddCodeLabel (CodeSeg* S, const char* Name)
554 /* Add a code label for the next instruction to follow */
555 {
556     /* Calculate the hash from the name */
557     unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
558
559     /* Try to find the code label if it does already exist */
560     CodeLabel* L = FindCodeLabel (S, Name, Hash);
561
562     /* Did we find it? */
563     if (L) {
564         /* We found it - be sure it does not already have an owner */
565         CHECK (L->Owner == 0);
566     } else {
567         /* Not found - create a new one */
568         L = NewCodeSegLabel (S, Name, Hash);
569     }
570
571     /* Safety. This call is quite costly, but safety is better */
572     if (CollIndex (&S->Labels, L) >= 0) {
573         Internal ("AddCodeLabel: Label `%s' already defined", Name);
574     }
575
576     /* We do now have a valid label. Remember it for later */
577     CollAppend (&S->Labels, L);
578 }
579
580
581
582 CodeLabel* GenCodeLabel (CodeSeg* S, struct CodeEntry* E)
583 /* If the code entry E does already have a label, return it. Otherwise
584  * create a new label, attach it to E and return it.
585  */
586 {
587     CodeLabel* L;
588
589     if (CodeEntryHasLabel (E)) {
590
591         /* Get the label from this entry */
592         L = GetCodeLabel (E, 0);
593
594     } else {
595
596         /* Get a new name */
597         const char* Name = LocalLabelName (GetLocalLabel ());
598
599         /* Generate the hash over the name */
600         unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
601
602         /* Create a new label */
603         L = NewCodeSegLabel (S, Name, Hash);
604
605         /* Attach this label to the code entry */
606         AttachCodeLabel (E, L);
607
608     }
609
610     /* Return the label */
611     return L;
612 }
613
614
615
616 void DelCodeLabel (CodeSeg* S, CodeLabel* L)
617 /* Remove references from this label and delete it. */
618 {
619     unsigned Count, I;
620
621     /* First, remove the label from the hash chain */
622     RemoveLabelFromHash (S, L);
623
624     /* Remove references from insns jumping to this label */
625     Count = CollCount (&L->JumpFrom);
626     for (I = 0; I < Count; ++I) {
627         /* Get the insn referencing this label */
628         CodeEntry* E = CollAt (&L->JumpFrom, I);
629         /* Remove the reference */
630         E->JumpTo = 0;
631     }
632     CollDeleteAll (&L->JumpFrom);
633
634     /* Remove the reference to the owning instruction if it has one. The
635      * function may be called for a label without an owner when deleting
636      * unfinished parts of the code. This is unfortunate since it allows
637      * errors to slip through.
638      */
639     if (L->Owner) {
640         CollDeleteItem (&L->Owner->Labels, L);
641     }
642
643     /* All references removed, delete the label itself */
644     FreeCodeLabel (L);
645 }
646
647
648
649 void MergeCodeLabels (CodeSeg* S)
650 /* Merge code labels. That means: For each instruction, remove all labels but
651  * one and adjust references accordingly.
652  */
653 {
654     unsigned I;
655
656     /* Walk over all code entries */
657     unsigned EntryCount = GetCodeEntryCount (S);
658     for (I = 0; I < EntryCount; ++I) {
659
660         CodeLabel* RefLab;
661         unsigned   J;
662
663         /* Get a pointer to the next entry */
664         CodeEntry* E = GetCodeEntry (S, I);
665
666         /* If this entry has zero labels, continue with the next one */
667         unsigned LabelCount = GetCodeLabelCount (E);
668         if (LabelCount == 0) {
669             continue;
670         }
671
672         /* We have at least one label. Use the first one as reference label. */
673         RefLab = GetCodeLabel (E, 0);
674
675         /* Walk through the remaining labels and change references to these
676          * labels to a reference to the one and only label. Delete the labels
677          * that are no longer used. To increase performance, walk backwards
678          * through the list.
679          */
680         for (J = LabelCount-1; J >= 1; --J) {
681
682             /* Get the next label */
683             CodeLabel* L = GetCodeLabel (E, J);
684
685             /* Move all references from this label to the reference label */
686             MoveLabelRefs (L, RefLab);
687
688             /* Remove the label completely. */
689             DelCodeLabel (S, L);
690         }
691
692         /* The reference label is the only remaining label. Check if there
693          * are any references to this label, and delete it if this is not
694          * the case.
695          */
696         if (CollCount (&RefLab->JumpFrom) == 0) {
697             /* Delete the label */
698             DelCodeLabel (S, RefLab);
699         }
700     }
701 }
702
703
704
705 void MoveCodeLabels (CodeSeg* S, struct CodeEntry* Old, struct CodeEntry* New)
706 /* Move all labels from Old to New. The routine will move the labels itself
707  * if New does not have any labels, and move references if there is at least
708  * a label for new. If references are moved, the old label is deleted
709  * afterwards.
710  */
711 {
712     /* Get the number of labels to move */
713     unsigned OldLabelCount = GetCodeLabelCount (Old);
714
715     /* Does the new entry have itself a label? */
716     if (CodeEntryHasLabel (New)) {
717
718         /* The new entry does already have a label - move references */
719         CodeLabel* NewLabel = GetCodeLabel (New, 0);
720         while (OldLabelCount--) {
721
722             /* Get the next label */
723             CodeLabel* OldLabel = GetCodeLabel (Old, OldLabelCount);
724
725             /* Move references */
726             MoveLabelRefs (OldLabel, NewLabel);
727
728             /* Delete the label */
729             DelCodeLabel (S, OldLabel);
730
731         }
732
733     } else {
734
735         /* The new entry does not have a label, just move them */
736         while (OldLabelCount--) {
737
738             /* Move the label to the new entry */
739             MoveCodeLabel (GetCodeLabel (Old, OldLabelCount), New);
740
741         }
742
743     }
744 }
745
746
747
748 void RemoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E)
749 /* Remove the reference between E and the label it jumps to. The reference
750  * will be removed on both sides and E->JumpTo will be 0 after that. If
751  * the reference was the only one for the label, the label will get
752  * deleted.
753  */
754 {
755     /* Get a pointer to the label and make sure it exists */
756     CodeLabel* L = E->JumpTo;
757     CHECK (L != 0);
758
759     /* Delete the entry from the label */
760     CollDeleteItem (&L->JumpFrom, E);
761
762     /* The entry jumps no longer to L */
763     E->JumpTo = 0;
764
765     /* If there are no more references, delete the label */
766     if (CollCount (&L->JumpFrom) == 0) {
767         DelCodeLabel (S, L);
768     }
769 }
770
771
772
773 void MoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E, CodeLabel* L)
774 /* Change the reference of E to L instead of the current one. If this
775  * was the only reference to the old label, the old label will get
776  * deleted.
777  */
778 {
779     /* Get the old label */
780     CodeLabel* OldLabel = E->JumpTo;
781
782     /* Be sure that code entry references a label */
783     PRECONDITION (OldLabel != 0);
784
785     /* Remove the reference to our label */
786     RemoveCodeLabelRef (S, E);
787
788     /* Use the new label */
789     AddLabelRef (L, E);
790 }
791
792
793
794 void AddCodeSegHint (CodeSeg* S, unsigned Hint)
795 /* Add a hint for the preceeding instruction */
796 {
797     CodeEntry* E;
798
799     /* Get the number of entries in this segment */
800     unsigned EntryCount = GetCodeEntryCount (S);
801
802     /* Must have at least one entry */
803     CHECK (EntryCount > 0);
804
805     /* Get the last entry */
806     E = GetCodeEntry (S, EntryCount-1);
807
808     /* Add the hint */
809     E->Hints |= Hint;
810 }
811
812
813
814 void DelCodeSegAfter (CodeSeg* S, unsigned Last)
815 /* Delete all entries including the given one */
816 {
817     /* Get the number of entries in this segment */
818     unsigned Count = GetCodeEntryCount (S);
819
820     /* First pass: Delete all references to labels. If the reference count
821      * for a label drops to zero, delete it.
822      */
823     unsigned C = Count;
824     while (Last < C--) {
825
826         /* Get the next entry */
827         CodeEntry* E = GetCodeEntry (S, C);
828
829         /* Check if this entry has a label reference */
830         if (E->JumpTo) {
831             /* Remove the reference to the label */
832             RemoveCodeLabelRef (S, E);
833         }
834
835     }
836
837     /* Second pass: Delete the instructions. If a label attached to an
838      * instruction still has references, it must be references from outside
839      * the deleted area. Don't delete the label in this case, just make it
840      * ownerless and move it to the label pool.
841      */
842     C = Count;
843     while (Last < C--) {
844
845         /* Get the next entry */
846         CodeEntry* E = GetCodeEntry (S, C);
847
848         /* Check if this entry has a label attached */
849         if (CodeEntryHasLabel (E)) {
850             /* Move the labels to the pool and clear the owner pointer */
851             MoveLabelsToPool (S, E);
852         }
853
854         /* Delete the pointer to the entry */
855         CollDelete (&S->Entries, C);
856
857         /* Delete the entry itself */
858         FreeCodeEntry (E);
859     }
860 }
861
862
863
864 void OutputCodeSeg (const CodeSeg* S, FILE* F)
865 /* Output the code segment data to a file */
866 {
867     unsigned I;
868
869     /* Get the number of entries in this segment */
870     unsigned Count = GetCodeEntryCount (S);
871
872     /* If the code segment is empty, bail out here */
873     if (Count == 0) {
874         return;
875     }
876
877     /* Output the segment directive */
878     fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
879
880     /* If this is a segment for a function, enter a function */
881     if (S->Func) {
882         fprintf (F, ".proc\t_%s\n\n", S->Func->Name);
883     }
884
885     /* Output all entries */
886     for (I = 0; I < Count; ++I) {
887
888         unsigned char Use;
889
890         OutputCodeEntry (CollConstAt (&S->Entries, I), F);
891
892         /* Print usage info */
893         Use = GetRegInfo ((CodeSeg*) S, I+1);
894         fprintf (F,
895                  "  Use: %c%c%c\n",
896                  (Use & REG_A)? 'A' : '_',
897                  (Use & REG_X)? 'X' : '_',
898                  (Use & REG_Y)? 'Y' : '_');
899     }
900
901     /* If this is a segment for a function, leave the function */
902     if (S->Func) {
903         fprintf (F, "\n.endproc\n\n");
904     }
905 }
906
907
908
909 unsigned GetCodeEntryCount (const CodeSeg* S)
910 /* Return the number of entries for the given code segment */
911 {
912     return CollCount (&S->Entries);
913 }
914
915
916
917