]> git.sur5r.net Git - cc65/blob - src/cc65/codeseg.c
Some improvements using the new SB_Printf for string buffers
[cc65] / src / cc65 / codeseg.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 codeseg.c                                 */
4 /*                                                                           */
5 /*                          Code segment structure                           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2004 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
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 "debugflag.h"
43 #include "global.h"
44 #include "hashstr.h"
45 #include "strbuf.h"
46 #include "strutil.h"
47 #include "xmalloc.h"
48
49 /* cc65 */
50 #include "asmlabel.h"
51 #include "codeent.h"
52 #include "codeinfo.h"
53 #include "codeseg.h"
54 #include "datatype.h"
55 #include "error.h"
56 #include "global.h"
57 #include "ident.h"
58 #include "symentry.h"
59
60
61
62 /*****************************************************************************/
63 /*                             Helper functions                              */
64 /*****************************************************************************/
65
66
67
68 static void CS_PrintFunctionHeader (const CodeSeg* S, FILE* F)
69 /* Print a comment with the function signature to the given file */
70 {
71     /* Get the associated function */
72     const SymEntry* Func = S->Func;
73
74     /* If this is a global code segment, do nothing */
75     if (Func) {
76         fprintf (F,
77                  "; ---------------------------------------------------------------\n"
78                  "; ");
79         PrintFuncSig (F, Func->Name, Func->Type);
80         fprintf (F,
81                  "\n"
82                  "; ---------------------------------------------------------------\n"
83                  "\n");
84     }
85 }
86
87
88
89 static void CS_MoveLabelsToEntry (CodeSeg* S, CodeEntry* E)
90 /* Move all labels from the label pool to the given entry and remove them
91  * from the pool.
92  */
93 {
94     /* Transfer the labels if we have any */
95     unsigned I;
96     unsigned LabelCount = CollCount (&S->Labels);
97     for (I = 0; I < LabelCount; ++I) {
98
99         /* Get the label */
100         CodeLabel* L = CollAt (&S->Labels, I);
101
102         /* Attach it to the entry */
103         CE_AttachLabel (E, L);
104     }
105
106     /* Delete the transfered labels */
107     CollDeleteAll (&S->Labels);
108 }
109
110
111
112 static void CS_MoveLabelsToPool (CodeSeg* S, CodeEntry* E)
113 /* Move the labels of the code entry E to the label pool of the code segment */
114 {
115     unsigned LabelCount = CE_GetLabelCount (E);
116     while (LabelCount--) {
117         CodeLabel* L = CE_GetLabel (E, LabelCount);
118         L->Owner = 0;
119         CollAppend (&S->Labels, L);
120     }
121     CollDeleteAll (&E->Labels);
122 }
123
124
125
126 static CodeLabel* CS_FindLabel (CodeSeg* S, const char* Name, unsigned Hash)
127 /* Find the label with the given name. Return the label or NULL if not found */
128 {
129     /* Get the first hash chain entry */
130     CodeLabel* L = S->LabelHash[Hash];
131
132     /* Search the list */
133     while (L) {
134         if (strcmp (Name, L->Name) == 0) {
135             /* Found */
136             break;
137         }
138         L = L->Next;
139     }
140     return L;
141 }
142
143
144
145 static CodeLabel* CS_NewCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
146 /* Create a new label and insert it into the label hash table */
147 {
148     /* Create a new label */
149     CodeLabel* L = NewCodeLabel (Name, Hash);
150
151     /* Enter the label into the hash table */
152     L->Next = S->LabelHash[L->Hash];
153     S->LabelHash[L->Hash] = L;
154
155     /* Return the new label */
156     return L;
157 }
158
159
160
161 static void CS_RemoveLabelFromHash (CodeSeg* S, CodeLabel* L)
162 /* Remove the given code label from the hash list */
163 {
164     /* Get the first entry in the hash chain */
165     CodeLabel* List = S->LabelHash[L->Hash];
166     CHECK (List != 0);
167
168     /* First, remove the label from the hash chain */
169     if (List == L) {
170         /* First entry in hash chain */
171         S->LabelHash[L->Hash] = L->Next;
172     } else {
173         /* Must search through the chain */
174         while (List->Next != L) {
175             /* If we've reached the end of the chain, something is *really* wrong */
176             CHECK (List->Next != 0);
177             /* Next entry */
178             List = List->Next;
179         }
180         /* The next entry is the one, we have been searching for */
181         List->Next = L->Next;
182     }
183 }
184
185
186
187 /*****************************************************************************/
188 /*                    Functions for parsing instructions                     */
189 /*****************************************************************************/
190
191
192
193 static const char* SkipSpace (const char* S)
194 /* Skip white space and return an updated pointer */
195 {
196     while (IsSpace (*S)) {
197         ++S;
198     }
199     return S;
200 }
201
202
203
204 static const char* ReadToken (const char* L, const char* Term,
205                               char* Buf, unsigned BufSize)
206 /* Read the next token into Buf, return the updated line pointer. The
207  * token is terminated by one of the characters given in term.
208  */
209 {
210     /* Read/copy the token */
211     unsigned I = 0;
212     unsigned ParenCount = 0;
213     while (*L && (ParenCount > 0 || strchr (Term, *L) == 0)) {
214         if (I < BufSize-1) {
215             Buf[I] = *L;
216         } else if (I == BufSize-1) {
217             /* Cannot store this character, this is an input error (maybe
218              * identifier too long or similar).
219              */
220             Error ("ASM code error: syntax error");
221         }
222         ++I;
223         if (*L == ')') {
224             --ParenCount;
225         } else if (*L == '(') {
226             ++ParenCount;
227         }
228         ++L;
229     }
230
231     /* Terminate the buffer contents */
232     Buf[I] = '\0';
233
234     /* Return the updated line pointer */
235     return L;
236 }
237
238
239
240 static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
241 /* Parse an instruction nnd generate a code entry from it. If the line contains
242  * errors, output an error message and return NULL.
243  * For simplicity, we don't accept the broad range of input a "real" assembler
244  * does. The instruction and the argument are expected to be separated by
245  * white space, for example.
246  */
247 {
248     char                Mnemo[IDENTSIZE+10];
249     const OPCDesc*      OPC;
250     am_t                AM = 0;         /* Initialize to keep gcc silent */
251     char                Arg[IDENTSIZE+10];
252     char                Reg;
253     CodeEntry*          E;
254     CodeLabel*          Label;
255
256     /* Read the first token and skip white space after it */
257     L = SkipSpace (ReadToken (L, " \t:", Mnemo, sizeof (Mnemo)));
258
259     /* Check if we have a label */
260     if (*L == ':') {
261
262         /* Skip the colon and following white space */
263         L = SkipSpace (L+1);
264
265         /* Add the label */
266         CS_AddLabel (S, Mnemo);
267
268         /* If we have reached end of line, bail out, otherwise a mnemonic
269          * may follow.
270          */
271         if (*L == '\0') {
272             return 0;
273         }
274
275         L = SkipSpace (ReadToken (L, " \t", Mnemo, sizeof (Mnemo)));
276     }
277
278     /* Try to find the opcode description for the mnemonic */
279     OPC = FindOP65 (Mnemo);
280
281     /* If we didn't find the opcode, print an error and bail out */
282     if (OPC == 0) {
283         Error ("ASM code error: %s is not a valid mnemonic", Mnemo);
284         return 0;
285     }
286
287     /* Get the addressing mode */
288     Arg[0] = '\0';
289     switch (*L) {
290
291         case '\0':
292             /* Implicit or accu */
293             if (OPC->Info & OF_NOIMP) {
294                 AM = AM65_ACC;
295             } else {
296                 AM = AM65_IMP;
297             }
298             break;
299
300         case '#':
301             /* Immidiate */
302             StrCopy (Arg, sizeof (Arg), L+1);
303             AM = AM65_IMM;
304             break;
305
306         case '(':
307             /* Indirect */
308             L = ReadToken (L+1, ",)", Arg, sizeof (Arg));
309
310             /* Check for errors */
311             if (*L == '\0') {
312                 Error ("ASM code error: syntax error");
313                 return 0;
314             }
315
316             /* Check the different indirect modes */
317             if (*L == ',') {
318                 /* Expect zp x indirect */
319                 L = SkipSpace (L+1);
320                 if (toupper (*L) != 'X') {
321                     Error ("ASM code error: `X' expected");
322                     return 0;
323                 }
324                 L = SkipSpace (L+1);
325                 if (*L != ')') {
326                     Error ("ASM code error: `)' expected");
327                     return 0;
328                 }
329                 L = SkipSpace (L+1);
330                 if (*L != '\0') {
331                     Error ("ASM code error: syntax error");
332                     return 0;
333                 }
334                 AM = AM65_ZPX_IND;
335             } else if (*L == ')') {
336                 /* zp indirect or zp indirect, y */
337                 L = SkipSpace (L+1);
338                 if (*L == ',') {
339                     L = SkipSpace (L+1);
340                     if (toupper (*L) != 'Y') {
341                         Error ("ASM code error: `Y' expected");
342                         return 0;
343                     }
344                     L = SkipSpace (L+1);
345                     if (*L != '\0') {
346                         Error ("ASM code error: syntax error");
347                         return 0;
348                     }
349                     AM = AM65_ZP_INDY;
350                 } else if (*L == '\0') {
351                     AM = AM65_ZP_IND;
352                 } else {
353                     Error ("ASM code error: syntax error");
354                     return 0;
355                 }
356             }
357             break;
358
359         case 'a':
360         case 'A':
361             /* Accumulator? */
362             if (L[1] == '\0') {
363                 AM = AM65_ACC;
364                 break;
365             }
366             /* FALLTHROUGH */
367
368         default:
369             /* Absolute, maybe indexed */
370             L = ReadToken (L, ",", Arg, sizeof (Arg));
371             if (*L == '\0') {
372                 /* Absolute, zeropage or branch */
373                 if ((OPC->Info & OF_BRA) != 0) {
374                     /* Branch */
375                     AM = AM65_BRA;
376                 } else if (GetZPInfo(Arg) != 0) {
377                     AM = AM65_ZP;
378                 } else {
379                     AM = AM65_ABS;
380                 }
381             } else if (*L == ',') {
382                 /* Indexed */
383                 L = SkipSpace (L+1);
384                 if (*L == '\0') {
385                     Error ("ASM code error: syntax error");
386                     return 0;
387                 } else {
388                     Reg = toupper (*L);
389                     L = SkipSpace (L+1);
390                     if (Reg == 'X') {
391                         if (GetZPInfo(Arg) != 0) {
392                             AM = AM65_ZPX;
393                         } else {
394                             AM = AM65_ABSX;
395                         }
396                     } else if (Reg == 'Y') {
397                         AM = AM65_ABSY;
398                     } else {
399                         Error ("ASM code error: syntax error");
400                         return 0;
401                     }
402                     if (*L != '\0') {
403                         Error ("ASM code error: syntax error");
404                         return 0;
405                     }
406                 }
407             }
408             break;
409
410     }
411
412     /* If the instruction is a branch, check for the label and generate it
413      * if it does not exist. This may lead to unused labels (if the label
414      * is actually an external one) which are removed by the CS_MergeLabels
415      * function later.
416      */
417     Label = 0;
418     if (AM == AM65_BRA) {
419
420         /* Generate the hash over the label, then search for the label */
421         unsigned Hash = HashStr (Arg) % CS_LABEL_HASH_SIZE;
422         Label = CS_FindLabel (S, Arg, Hash);
423
424         /* If we don't have the label, it's a forward ref - create it */
425         if (Label == 0) {
426             /* Generate a new label */
427             Label = CS_NewCodeLabel (S, Arg, Hash);
428         }
429     }
430
431     /* We do now have the addressing mode in AM. Allocate a new CodeEntry
432      * structure and initialize it.
433      */
434     E = NewCodeEntry (OPC->OPC, AM, Arg, Label, LI);
435
436     /* Return the new code entry */
437     return E;
438 }
439
440
441
442 /*****************************************************************************/
443 /*                                   Code                                    */
444 /*****************************************************************************/
445
446
447
448 CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func)
449 /* Create a new code segment, initialize and return it */
450 {
451     unsigned I;
452     const type* RetType;
453
454     /* Allocate memory */
455     CodeSeg* S = xmalloc (sizeof (CodeSeg));
456
457     /* Initialize the fields */
458     S->SegName  = xstrdup (SegName);
459     S->Func     = Func;
460     InitCollection (&S->Entries);
461     InitCollection (&S->Labels);
462     for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
463         S->LabelHash[I] = 0;
464     }
465
466     /* If we have a function given, get the return type of the function.
467      * Assume ANY return type besides void will use the A and X registers.
468      */
469     if (S->Func && !IsTypeVoid ((RetType = GetFuncReturn (Func->Type)))) {
470         if (SizeOf (RetType) == SizeOf (type_long)) {
471             S->ExitRegs = REG_EAX;
472         } else {
473             S->ExitRegs = REG_AX;
474         }
475     } else {
476         S->ExitRegs = REG_NONE;
477     }
478
479     /* Copy the global optimization settings */
480     S->Optimize       = (unsigned char) IS_Get (&Optimize);
481     S->CodeSizeFactor = (unsigned) IS_Get (&CodeSizeFactor);
482
483     /* Return the new struct */
484     return S;
485 }
486
487
488
489 void CS_AddEntry (CodeSeg* S, struct CodeEntry* E)
490 /* Add an entry to the given code segment */
491 {
492     /* Transfer the labels if we have any */
493     CS_MoveLabelsToEntry (S, E);
494
495     /* Add the entry to the list of code entries in this segment */
496     CollAppend (&S->Entries, E);
497 }
498
499
500
501 void CS_AddVLine (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap)
502 /* Add a line to the given code segment */
503 {
504     const char* L;
505     CodeEntry*  E;
506     char        Token[IDENTSIZE+10];
507
508     /* Format the line */
509     StrBuf Buf = STATIC_STRBUF_INITIALIZER;
510     SB_VPrintf (&Buf, Format, ap);
511
512     /* Skip whitespace */
513     L = SkipSpace (SB_GetConstBuf (&Buf));
514
515     /* Check which type of instruction we have */
516     E = 0;      /* Assume no insn created */
517     switch (*L) {
518
519         case '\0':
520             /* Empty line, just ignore it */
521             break;
522
523         case ';':
524             /* Comment or hint, ignore it for now */
525             break;
526
527         case '.':
528             /* Control instruction */
529             ReadToken (L, " \t", Token, sizeof (Token));
530             Error ("ASM code error: Pseudo instruction `%s' not supported", Token);
531             break;
532
533         default:
534             E = ParseInsn (S, LI, L);
535             break;
536     }
537
538     /* If we have a code entry, transfer the labels and insert it */
539     if (E) {
540         CS_AddEntry (S, E);
541     }
542
543     /* Cleanup the string buffer */
544     DoneStrBuf (&Buf);
545 }
546
547
548
549 void CS_AddLine (CodeSeg* S, LineInfo* LI, const char* Format, ...)
550 /* Add a line to the given code segment */
551 {
552     va_list ap;
553     va_start (ap, Format);
554     CS_AddVLine (S, LI, Format, ap);
555     va_end (ap);
556 }
557
558
559
560 void CS_InsertEntry (CodeSeg* S, struct CodeEntry* E, unsigned Index)
561 /* Insert the code entry at the index given. Following code entries will be
562  * moved to slots with higher indices.
563  */
564 {
565     /* Insert the entry into the collection */
566     CollInsert (&S->Entries, E, Index);
567 }
568
569
570
571 void CS_DelEntry (CodeSeg* S, unsigned Index)
572 /* Delete an entry from the code segment. This includes moving any associated
573  * labels, removing references to labels and even removing the referenced labels
574  * if the reference count drops to zero.
575  * Note: Labels are moved forward if possible, that is, they are moved to the
576  * next insn (not the preceeding one).
577  */
578 {
579     /* Get the code entry for the given index */
580     CodeEntry* E = CS_GetEntry (S, Index);
581
582     /* If the entry has a labels, we have to move this label to the next insn.
583      * If there is no next insn, move the label into the code segement label
584      * pool. The operation is further complicated by the fact that the next
585      * insn may already have a label. In that case change all reference to
586      * this label and delete the label instead of moving it.
587      */
588     unsigned Count = CE_GetLabelCount (E);
589     if (Count > 0) {
590
591         /* The instruction has labels attached. Check if there is a next
592          * instruction.
593          */
594         if (Index == CS_GetEntryCount (S)-1) {
595
596             /* No next instruction, move to the codeseg label pool */
597             CS_MoveLabelsToPool (S, E);
598
599         } else {
600
601             /* There is a next insn, get it */
602             CodeEntry* N = CS_GetEntry (S, Index+1);
603
604             /* Move labels to the next entry */
605             CS_MoveLabels (S, E, N);
606
607         }
608     }
609
610     /* If this insn references a label, remove the reference. And, if the
611      * the reference count for this label drops to zero, remove this label.
612      */
613     if (E->JumpTo) {
614         /* Remove the reference */
615         CS_RemoveLabelRef (S, E);
616     }
617
618     /* Delete the pointer to the insn */
619     CollDelete (&S->Entries, Index);
620
621     /* Delete the instruction itself */
622     FreeCodeEntry (E);
623 }
624
625
626
627 void CS_DelEntries (CodeSeg* S, unsigned Start, unsigned Count)
628 /* Delete a range of code entries. This includes removing references to labels,
629  * labels attached to the entries and so on.
630  */
631 {
632     /* Start deleting the entries from the rear, because this involves less
633      * memory moving.
634      */
635     while (Count--) {
636         CS_DelEntry (S, Start + Count);
637     }
638 }
639
640
641
642 void CS_MoveEntries (CodeSeg* S, unsigned Start, unsigned Count, unsigned NewPos)
643 /* Move a range of entries from one position to another. Start is the index
644  * of the first entry to move, Count is the number of entries and NewPos is
645  * the index of the target entry. The entry with the index Start will later
646  * have the index NewPos. All entries with indices NewPos and above are
647  * moved to higher indices. If the code block is moved to the end of the
648  * current code, and if pending labels exist, these labels will get attached
649  * to the first instruction of the moved block (the first one after the
650  * current code end)
651  */
652 {
653     /* If NewPos is at the end of the code segment, move any labels from the
654      * label pool to the first instruction of the moved range.
655      */
656     if (NewPos == CS_GetEntryCount (S)) {
657         CS_MoveLabelsToEntry (S, CS_GetEntry (S, Start));
658     }
659
660     /* Move the code block to the destination */
661     CollMoveMultiple (&S->Entries, Start, Count, NewPos);
662 }
663
664
665
666 struct CodeEntry* CS_GetPrevEntry (CodeSeg* S, unsigned Index)
667 /* Get the code entry preceeding the one with the index Index. If there is no
668  * preceeding code entry, return NULL.
669  */
670 {
671     if (Index == 0) {
672         /* This is the first entry */
673         return 0;
674     } else {
675         /* Previous entry available */
676         return CollAtUnchecked (&S->Entries, Index-1);
677     }
678 }
679
680
681
682 struct CodeEntry* CS_GetNextEntry (CodeSeg* S, unsigned Index)
683 /* Get the code entry following the one with the index Index. If there is no
684  * following code entry, return NULL.
685  */
686 {
687     if (Index >= CollCount (&S->Entries)-1) {
688         /* This is the last entry */
689         return 0;
690     } else {
691         /* Code entries left */
692         return CollAtUnchecked (&S->Entries, Index+1);
693     }
694 }
695
696
697
698 int CS_GetEntries (CodeSeg* S, struct CodeEntry** List,
699                    unsigned Start, unsigned Count)
700 /* Get Count code entries into List starting at index start. Return true if
701  * we got the lines, return false if not enough lines were available.
702  */
703 {
704     /* Check if enough entries are available */
705     if (Start + Count > CollCount (&S->Entries)) {
706         return 0;
707     }
708
709     /* Copy the entries */
710     while (Count--) {
711         *List++ = CollAtUnchecked (&S->Entries, Start++);
712     }
713
714     /* We have the entries */
715     return 1;
716 }
717
718
719
720 unsigned CS_GetEntryIndex (CodeSeg* S, struct CodeEntry* E)
721 /* Return the index of a code entry */
722 {
723     int Index = CollIndex (&S->Entries, E);
724     CHECK (Index >= 0);
725     return Index;
726 }
727
728
729
730 int CS_RangeHasLabel (CodeSeg* S, unsigned Start, unsigned Count)
731 /* Return true if any of the code entries in the given range has a label
732  * attached. If the code segment does not span the given range, check the
733  * possible span instead.
734  */
735 {
736     unsigned EntryCount = CS_GetEntryCount(S);
737
738     /* Adjust count. We expect at least Start to be valid. */
739     CHECK (Start < EntryCount);
740     if (Start + Count > EntryCount) {
741         Count = EntryCount - Start;
742     }
743
744     /* Check each entry. Since we have validated the index above, we may
745      * use the unchecked access function in the loop which is faster.
746      */
747     while (Count--) {
748         const CodeEntry* E = CollAtUnchecked (&S->Entries, Start++);
749         if (CE_HasLabel (E)) {
750             return 1;
751         }
752     }
753
754     /* No label in the complete range */
755     return 0;
756 }
757
758
759
760 CodeLabel* CS_AddLabel (CodeSeg* S, const char* Name)
761 /* Add a code label for the next instruction to follow */
762 {
763     /* Calculate the hash from the name */
764     unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
765
766     /* Try to find the code label if it does already exist */
767     CodeLabel* L = CS_FindLabel (S, Name, Hash);
768
769     /* Did we find it? */
770     if (L) {
771         /* We found it - be sure it does not already have an owner */
772         if (L->Owner) {
773             Error ("ASM label `%s' is already defined", Name);
774             return L;
775         }
776     } else {
777         /* Not found - create a new one */
778         L = CS_NewCodeLabel (S, Name, Hash);
779     }
780
781     /* Safety. This call is quite costly, but safety is better */
782     if (CollIndex (&S->Labels, L) >= 0) {
783         Error ("ASM label `%s' is already defined", Name);
784         return L;
785     }
786
787     /* We do now have a valid label. Remember it for later */
788     CollAppend (&S->Labels, L);
789
790     /* Return the label */
791     return L;
792 }
793
794
795
796 CodeLabel* CS_GenLabel (CodeSeg* S, struct CodeEntry* E)
797 /* If the code entry E does already have a label, return it. Otherwise
798  * create a new label, attach it to E and return it.
799  */
800 {
801     CodeLabel* L;
802
803     if (CE_HasLabel (E)) {
804
805         /* Get the label from this entry */
806         L = CE_GetLabel (E, 0);
807
808     } else {
809
810         /* Get a new name */
811         const char* Name = LocalLabelName (GetLocalLabel ());
812
813         /* Generate the hash over the name */
814         unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
815
816         /* Create a new label */
817         L = CS_NewCodeLabel (S, Name, Hash);
818
819         /* Attach this label to the code entry */
820         CE_AttachLabel (E, L);
821
822     }
823
824     /* Return the label */
825     return L;
826 }
827
828
829
830 void CS_DelLabel (CodeSeg* S, CodeLabel* L)
831 /* Remove references from this label and delete it. */
832 {
833     unsigned Count, I;
834
835     /* First, remove the label from the hash chain */
836     CS_RemoveLabelFromHash (S, L);
837
838     /* Remove references from insns jumping to this label */
839     Count = CollCount (&L->JumpFrom);
840     for (I = 0; I < Count; ++I) {
841         /* Get the insn referencing this label */
842         CodeEntry* E = CollAt (&L->JumpFrom, I);
843         /* Remove the reference */
844         CE_ClearJumpTo (E);
845     }
846     CollDeleteAll (&L->JumpFrom);
847
848     /* Remove the reference to the owning instruction if it has one. The
849      * function may be called for a label without an owner when deleting
850      * unfinished parts of the code. This is unfortunate since it allows
851      * errors to slip through.
852      */
853     if (L->Owner) {
854         CollDeleteItem (&L->Owner->Labels, L);
855     }
856
857     /* All references removed, delete the label itself */
858     FreeCodeLabel (L);
859 }
860
861
862
863 void CS_MergeLabels (CodeSeg* S)
864 /* Merge code labels. That means: For each instruction, remove all labels but
865  * one and adjust references accordingly.
866  */
867 {
868     unsigned I;
869     unsigned J;
870
871     /* First, remove all labels from the label symbol table that don't have an
872      * owner (this means that they are actually external labels but we didn't
873      * know that previously since they may have also been forward references).
874      */
875     for (I = 0; I < CS_LABEL_HASH_SIZE; ++I) {
876
877         /* Get the first label in this hash chain */
878         CodeLabel** L = &S->LabelHash[I];
879         while (*L) {
880             if ((*L)->Owner == 0) {
881
882                 /* The label does not have an owner, remove it from the chain */
883                 CodeLabel* X = *L;
884                 *L = X->Next;
885
886                 /* Cleanup any entries jumping to this label */
887                 for (J = 0; J < CL_GetRefCount (X); ++J) {
888                     /* Get the entry referencing this label */
889                     CodeEntry* E = CL_GetRef (X, J);
890                     /* And remove the reference. Do NOT call CE_ClearJumpTo
891                      * here, because this will also clear the label name,
892                      * which is not what we want.
893                      */
894                     E->JumpTo = 0;
895                 }
896
897                 /* Print some debugging output */
898                 if (Debug) {
899                     printf ("Removing unused global label `%s'", X->Name);
900                 }
901
902                 /* And free the label */
903                 FreeCodeLabel (X);
904             } else {
905                 /* Label is owned, point to next code label pointer */
906                 L = &((*L)->Next);
907             }
908         }
909     }
910
911     /* Walk over all code entries */
912     for (I = 0; I < CS_GetEntryCount (S); ++I) {
913
914         CodeLabel* RefLab;
915         unsigned   J;
916
917         /* Get a pointer to the next entry */
918         CodeEntry* E = CS_GetEntry (S, I);
919
920         /* If this entry has zero labels, continue with the next one */
921         unsigned LabelCount = CE_GetLabelCount (E);
922         if (LabelCount == 0) {
923             continue;
924         }
925
926         /* We have at least one label. Use the first one as reference label. */
927         RefLab = CE_GetLabel (E, 0);
928
929         /* Walk through the remaining labels and change references to these
930          * labels to a reference to the one and only label. Delete the labels
931          * that are no longer used. To increase performance, walk backwards
932          * through the list.
933          */
934         for (J = LabelCount-1; J >= 1; --J) {
935
936             /* Get the next label */
937             CodeLabel* L = CE_GetLabel (E, J);
938
939             /* Move all references from this label to the reference label */
940             CL_MoveRefs (L, RefLab);
941
942             /* Remove the label completely. */
943             CS_DelLabel (S, L);
944         }
945
946         /* The reference label is the only remaining label. Check if there
947          * are any references to this label, and delete it if this is not
948          * the case.
949          */
950         if (CollCount (&RefLab->JumpFrom) == 0) {
951             /* Delete the label */
952             CS_DelLabel (S, RefLab);
953         }
954     }
955 }
956
957
958
959 void CS_MoveLabels (CodeSeg* S, struct CodeEntry* Old, struct CodeEntry* New)
960 /* Move all labels from Old to New. The routine will move the labels itself
961  * if New does not have any labels, and move references if there is at least
962  * a label for new. If references are moved, the old label is deleted
963  * afterwards.
964  */
965 {
966     /* Get the number of labels to move */
967     unsigned OldLabelCount = CE_GetLabelCount (Old);
968
969     /* Does the new entry have itself a label? */
970     if (CE_HasLabel (New)) {
971
972         /* The new entry does already have a label - move references */
973         CodeLabel* NewLabel = CE_GetLabel (New, 0);
974         while (OldLabelCount--) {
975
976             /* Get the next label */
977             CodeLabel* OldLabel = CE_GetLabel (Old, OldLabelCount);
978
979             /* Move references */
980             CL_MoveRefs (OldLabel, NewLabel);
981
982             /* Delete the label */
983             CS_DelLabel (S, OldLabel);
984
985         }
986
987     } else {
988
989         /* The new entry does not have a label, just move them */
990         while (OldLabelCount--) {
991
992             /* Move the label to the new entry */
993             CE_MoveLabel (CE_GetLabel (Old, OldLabelCount), New);
994
995         }
996
997     }
998 }
999
1000
1001
1002 void CS_RemoveLabelRef (CodeSeg* S, struct CodeEntry* E)
1003 /* Remove the reference between E and the label it jumps to. The reference
1004  * will be removed on both sides and E->JumpTo will be 0 after that. If
1005  * the reference was the only one for the label, the label will get
1006  * deleted.
1007  */
1008 {
1009     /* Get a pointer to the label and make sure it exists */
1010     CodeLabel* L = E->JumpTo;
1011     CHECK (L != 0);
1012
1013     /* Delete the entry from the label */
1014     CollDeleteItem (&L->JumpFrom, E);
1015
1016     /* The entry jumps no longer to L */
1017     CE_ClearJumpTo (E);
1018
1019     /* If there are no more references, delete the label */
1020     if (CollCount (&L->JumpFrom) == 0) {
1021         CS_DelLabel (S, L);
1022     }
1023 }
1024
1025
1026
1027 void CS_MoveLabelRef (CodeSeg* S, struct CodeEntry* E, CodeLabel* L)
1028 /* Change the reference of E to L instead of the current one. If this
1029  * was the only reference to the old label, the old label will get
1030  * deleted.
1031  */
1032 {
1033     /* Get the old label */
1034     CodeLabel* OldLabel = E->JumpTo;
1035
1036     /* Be sure that code entry references a label */
1037     PRECONDITION (OldLabel != 0);
1038
1039     /* Remove the reference to our label */
1040     CS_RemoveLabelRef (S, E);
1041
1042     /* Use the new label */
1043     CL_AddRef (L, E);
1044 }
1045
1046
1047
1048 void CS_DelCodeAfter (CodeSeg* S, unsigned Last)
1049 /* Delete all entries including the given one */
1050 {
1051     /* Get the number of entries in this segment */
1052     unsigned Count = CS_GetEntryCount (S);
1053
1054     /* First pass: Delete all references to labels. If the reference count
1055      * for a label drops to zero, delete it.
1056      */
1057     unsigned C = Count;
1058     while (Last < C--) {
1059
1060         /* Get the next entry */
1061         CodeEntry* E = CS_GetEntry (S, C);
1062
1063         /* Check if this entry has a label reference */
1064         if (E->JumpTo) {
1065             /* If the label is a label in the label pool and this is the last
1066              * reference to the label, remove the label from the pool.
1067              */
1068             CodeLabel* L = E->JumpTo;
1069             int Index = CollIndex (&S->Labels, L);
1070             if (Index >= 0 && CollCount (&L->JumpFrom) == 1) {
1071                 /* Delete it from the pool */
1072                 CollDelete (&S->Labels, Index);
1073             }
1074
1075             /* Remove the reference to the label */
1076             CS_RemoveLabelRef (S, E);
1077         }
1078
1079     }
1080
1081     /* Second pass: Delete the instructions. If a label attached to an
1082      * instruction still has references, it must be references from outside
1083      * the deleted area. Don't delete the label in this case, just make it
1084      * ownerless and move it to the label pool.
1085      */
1086     C = Count;
1087     while (Last < C--) {
1088
1089         /* Get the next entry */
1090         CodeEntry* E = CS_GetEntry (S, C);
1091
1092         /* Check if this entry has a label attached */
1093         if (CE_HasLabel (E)) {
1094             /* Move the labels to the pool and clear the owner pointer */
1095             CS_MoveLabelsToPool (S, E);
1096         }
1097
1098         /* Delete the pointer to the entry */
1099         CollDelete (&S->Entries, C);
1100
1101         /* Delete the entry itself */
1102         FreeCodeEntry (E);
1103     }
1104 }
1105
1106
1107
1108 void CS_ResetMarks (CodeSeg* S, unsigned First, unsigned Last)
1109 /* Remove all user marks from the entries in the given range */
1110 {
1111     while (First <= Last) {
1112         CE_ResetMark (CS_GetEntry (S, First++));
1113     }
1114 }
1115
1116
1117
1118 int CS_IsBasicBlock (CodeSeg* S, unsigned First, unsigned Last)
1119 /* Check if the given code segment range is a basic block. That is, check if
1120  * First is the only entrance and Last is the only exit. This means that no
1121  * jump/branch inside the block may jump to an insn below First or after(!)
1122  * Last, and that no insn may jump into this block from the outside.
1123  */
1124 {
1125     unsigned I;
1126
1127     /* Don't accept invalid ranges */
1128     CHECK (First <= Last);
1129
1130     /* First pass: Walk over the range and remove all marks from the entries */
1131     CS_ResetMarks (S, First, Last);
1132
1133     /* Second pass: Walk over the range checking all labels. Note: There may be
1134      * label on the first insn which is ok.
1135      */
1136     I = First + 1;
1137     while (I <= Last) {
1138
1139         /* Get the next entry */
1140         CodeEntry* E = CS_GetEntry (S, I);
1141
1142         /* Check if this entry has one or more labels, if so, check which
1143          * entries jump to this label.
1144          */
1145         unsigned LabelCount = CE_GetLabelCount (E);
1146         unsigned LabelIndex;
1147         for (LabelIndex = 0; LabelIndex < LabelCount; ++LabelIndex) {
1148
1149             /* Get this label */
1150             CodeLabel* L = CE_GetLabel (E, LabelIndex);
1151
1152             /* Walk over all entries that jump to this label. Check for each
1153              * of the entries if it is out of the range.
1154              */
1155             unsigned RefCount = CL_GetRefCount (L);
1156             unsigned RefIndex;
1157             for (RefIndex = 0; RefIndex < RefCount; ++RefIndex) {
1158
1159                 /* Get the code entry that jumps here */
1160                 CodeEntry* Ref = CL_GetRef (L, RefIndex);
1161
1162                 /* Walk over out complete range and check if we find the
1163                  * refering entry. This is cheaper than using CS_GetEntryIndex,
1164                  * because CS_GetEntryIndex will search the complete code
1165                  * segment and not just our range.
1166                  */
1167                 unsigned J;
1168                 for (J = First; J <= Last; ++J) {
1169                     if (Ref == CS_GetEntry (S, J)) {
1170                         break;
1171                     }
1172                 }
1173                 if (J > Last) {
1174                     /* We did not find the entry. This means that the jump to
1175                      * out code segment entry E came from outside the range,
1176                      * which in turn means that the given range is not a basic
1177                      * block.
1178                      */
1179                     CS_ResetMarks (S, First, Last);
1180                     return 0;
1181                 }
1182
1183                 /* If we come here, we found the entry. Mark it, so we know
1184                  * that the branch to the label is in range.
1185                  */
1186                 CE_SetMark (Ref);
1187             }
1188         }
1189
1190         /* Next entry */
1191         ++I;
1192     }
1193
1194     /* Third pass: Walk again over the range and check all branches. If we
1195      * find a branch that is not marked, its target is not inside the range
1196      * (since we checked all the labels in the range before).
1197      */
1198     I = First;
1199     while (I <= Last) {
1200
1201         /* Get the next entry */
1202         CodeEntry* E = CS_GetEntry (S, I);
1203
1204         /* Check if this is a branch and if so, if it has a mark */
1205         if (E->Info & (OF_UBRA | OF_CBRA)) {
1206             if (!CE_HasMark (E)) {
1207                 /* No mark means not a basic block. Before bailing out, be sure
1208                  * to remove the marks from the remaining entries.
1209                  */
1210                 CS_ResetMarks (S, I+1, Last);
1211                 return 0;
1212             }
1213
1214             /* Remove the mark */
1215             CE_ResetMark (E);
1216         }
1217
1218         /* Next entry */
1219         ++I;
1220     }
1221
1222     /* Done - this is a basic block */
1223     return 1;
1224 }
1225
1226
1227
1228 void CS_OutputPrologue (const CodeSeg* S, FILE* F)
1229 /* If the given code segment is a code segment for a function, output the
1230  * assembler prologue into the file. That is: Output a comment header, switch
1231  * to the correct segment and enter the local function scope. If the code
1232  * segment is global, do nothing.
1233  */
1234 {
1235     /* Get the function associated with the code segment */
1236     SymEntry* Func = S->Func;
1237
1238     /* If the code segment is associated with a function, print a function
1239      * header and enter a local scope. Be sure to switch to the correct
1240      * segment before outputing the function label.
1241      */
1242     if (Func) {
1243         /* Get the function descriptor */
1244         const FuncDesc* D = GetFuncDesc (Func->Type);
1245         CS_PrintFunctionHeader (S, F);
1246         fprintf (F, ".segment\t\"%s\"\n\n.proc\t_%s", S->SegName, Func->Name);
1247         if (D->Flags & FD_NEAR) {
1248             fputs (": near", F);
1249         } else if (D->Flags & FD_FAR) {
1250             fputs (": far", F);
1251         }
1252         fputs ("\n\n", F);
1253     }
1254
1255 }
1256
1257
1258
1259 void CS_OutputEpilogue (const CodeSeg* S, FILE* F)
1260 /* If the given code segment is a code segment for a function, output the
1261  * assembler epilogue into the file. That is: Close the local function scope.
1262  */
1263 {
1264     if (S->Func) {
1265         fputs ("\n.endproc\n\n", F);
1266     }
1267 }
1268
1269
1270
1271 void CS_Output (CodeSeg* S, FILE* F)
1272 /* Output the code segment data to a file */
1273 {
1274     unsigned I;
1275     const LineInfo* LI;
1276
1277     /* Get the number of entries in this segment */
1278     unsigned Count = CS_GetEntryCount (S);
1279
1280     /* If the code segment is empty, bail out here */
1281     if (Count == 0) {
1282         return;
1283     }
1284
1285     /* Generate register info */
1286     CS_GenRegInfo (S);
1287
1288     /* Output the segment directive */
1289     fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
1290
1291     /* Output all entries, prepended by the line information if it has changed */
1292     LI = 0;
1293     for (I = 0; I < Count; ++I) {
1294         /* Get the next entry */
1295         const CodeEntry* E = CollConstAt (&S->Entries, I);
1296         /* Check if the line info has changed. If so, output the source line
1297          * if the option is enabled and output debug line info if the debug
1298          * option is enabled.
1299          */
1300         if (E->LI != LI) {
1301             /* Line info has changed, remember the new line info */
1302             LI = E->LI;
1303
1304             /* Add the source line as a comment. Beware: When line continuation
1305              * was used, the line may contain newlines.
1306              */
1307             if (AddSource) {
1308                 const char* L = LI->Line;
1309                 fputs (";\n; ", F);
1310                 while (*L) {
1311                     if (*L == '\n') {
1312                         fputs ("\n; ", F);
1313                     } else {
1314                         fputc (*L, F);
1315                     }
1316                     ++L;
1317                 }
1318                 fputs ("\n;\n", F);
1319             }
1320
1321             /* Add line debug info */
1322             if (DebugInfo) {
1323                 fprintf (F, "\t.dbg\tline, \"%s\", %u\n",
1324                          GetInputName (LI), GetInputLine (LI));
1325             }
1326         }
1327         /* Output the code */
1328         CE_Output (E, F);
1329     }
1330
1331     /* If debug info is enabled, terminate the last line number information */
1332     if (DebugInfo) {
1333         fputs ("\t.dbg\tline\n", F);
1334     }
1335
1336     /* Free register info */
1337     CS_FreeRegInfo (S);
1338 }
1339
1340
1341
1342 void CS_FreeRegInfo (CodeSeg* S)
1343 /* Free register infos for all instructions */
1344 {
1345     unsigned I;
1346     for (I = 0; I < CS_GetEntryCount (S); ++I) {
1347         CE_FreeRegInfo (CS_GetEntry(S, I));
1348     }
1349 }
1350
1351
1352
1353 void CS_GenRegInfo (CodeSeg* S)
1354 /* Generate register infos for all instructions */
1355 {
1356     unsigned I;
1357     RegContents Regs;           /* Initial register contents */
1358     RegContents* CurrentRegs;   /* Current register contents */
1359     int WasJump;                /* True if last insn was a jump */
1360     int Done;                   /* All runs done flag */
1361
1362     /* Be sure to delete all register infos */
1363     CS_FreeRegInfo (S);
1364
1365     /* We may need two runs to get back references right */
1366     do {
1367
1368         /* Assume we're done after this run */
1369         Done = 1;
1370
1371         /* On entry, the register contents are unknown */
1372         RC_Invalidate (&Regs);
1373         CurrentRegs = &Regs;
1374
1375         /* Walk over all insns and note just the changes from one insn to the
1376          * next one.
1377          */
1378         WasJump = 0;
1379         for (I = 0; I < CS_GetEntryCount (S); ++I) {
1380
1381             CodeEntry* P;
1382
1383             /* Get the next instruction */
1384             CodeEntry* E = CollAtUnchecked (&S->Entries, I);
1385
1386             /* If the instruction has a label, we need some special handling */
1387             unsigned LabelCount = CE_GetLabelCount (E);
1388             if (LabelCount > 0) {
1389
1390                 /* Loop over all entry points that jump here. If these entry
1391                  * points already have register info, check if all values are
1392                  * known and identical. If all values are identical, and the
1393                  * preceeding instruction was not an unconditional branch, check
1394                  * if the register value on exit of the preceeding instruction
1395                  * is also identical. If all these values are identical, the
1396                  * value of a register is known, otherwise it is unknown.
1397                  */
1398                 CodeLabel* Label = CE_GetLabel (E, 0);
1399                 unsigned Entry;
1400                 if (WasJump) {
1401                     /* Preceeding insn was an unconditional branch */
1402                     CodeEntry* J = CL_GetRef(Label, 0);
1403                     if (J->RI) {
1404                         Regs = J->RI->Out2;
1405                     } else {
1406                         RC_Invalidate (&Regs);
1407                     }
1408                     Entry = 1;
1409                 } else {
1410                     Regs = *CurrentRegs;
1411                     Entry = 0;
1412                 }
1413
1414                 while (Entry < CL_GetRefCount (Label)) {
1415                     /* Get this entry */
1416                     CodeEntry* J = CL_GetRef (Label, Entry);
1417                     if (J->RI == 0) {
1418                         /* No register info for this entry. This means that the
1419                          * instruction that jumps here is at higher addresses and
1420                          * the jump is a backward jump. We need a second run to
1421                          * get the register info right in this case. Until then,
1422                          * assume unknown register contents.
1423                          */
1424                         Done = 0;
1425                         RC_Invalidate (&Regs);
1426                         break;
1427                     }
1428                     if (J->RI->Out2.RegA != Regs.RegA) {
1429                         Regs.RegA = UNKNOWN_REGVAL;
1430                     }
1431                     if (J->RI->Out2.RegX != Regs.RegX) {
1432                         Regs.RegX = UNKNOWN_REGVAL;
1433                     }
1434                     if (J->RI->Out2.RegY != Regs.RegY) {
1435                         Regs.RegY = UNKNOWN_REGVAL;
1436                     }
1437                     if (J->RI->Out2.SRegLo != Regs.SRegLo) {
1438                         Regs.SRegLo = UNKNOWN_REGVAL;
1439                     }
1440                     if (J->RI->Out2.SRegHi != Regs.SRegHi) {
1441                         Regs.SRegHi = UNKNOWN_REGVAL;
1442                     }
1443                     if (J->RI->Out2.Tmp1 != Regs.Tmp1) {
1444                         Regs.Tmp1 = UNKNOWN_REGVAL;
1445                     }
1446                     ++Entry;
1447                 }
1448
1449                 /* Use this register info */
1450                 CurrentRegs = &Regs;
1451
1452             }
1453
1454             /* Generate register info for this instruction */
1455             CE_GenRegInfo (E, CurrentRegs);
1456
1457             /* Remember for the next insn if this insn was an uncondition branch */
1458             WasJump = (E->Info & OF_UBRA) != 0;
1459
1460             /* Output registers for this insn are input for the next */
1461             CurrentRegs = &E->RI->Out;
1462
1463             /* If this insn is a branch on zero flag, we may have more info on
1464              * register contents for one of both flow directions, but only if
1465              * there is a previous instruction.
1466              */
1467             if ((E->Info & OF_ZBRA) != 0 && (P = CS_GetPrevEntry (S, I)) != 0) {
1468
1469                 /* Get the branch condition */
1470                 bc_t BC = GetBranchCond (E->OPC);
1471
1472                 /* Check the previous instruction */
1473                 switch (P->OPC) {
1474
1475                     case OP65_ADC:
1476                     case OP65_AND:
1477                     case OP65_DEA:
1478                     case OP65_EOR:
1479                     case OP65_INA:
1480                     case OP65_LDA:
1481                     case OP65_ORA:
1482                     case OP65_PLA:
1483                     case OP65_SBC:
1484                         /* A is zero in one execution flow direction */
1485                         if (BC == BC_EQ) {
1486                             E->RI->Out2.RegA = 0;
1487                         } else {
1488                             E->RI->Out.RegA = 0;
1489                         }
1490                         break;
1491
1492                     case OP65_CMP:
1493                         /* If this is an immidiate compare, the A register has
1494                          * the value of the compare later.
1495                          */
1496                         if (CE_KnownImm (P)) {
1497                             if (BC == BC_EQ) {
1498                                 E->RI->Out2.RegA = (unsigned char)P->Num;
1499                             } else {
1500                                 E->RI->Out.RegA = (unsigned char)P->Num;
1501                             }
1502                         }
1503                         break;
1504
1505                     case OP65_CPX:
1506                         /* If this is an immidiate compare, the X register has
1507                          * the value of the compare later.
1508                          */
1509                         if (CE_KnownImm (P)) {
1510                             if (BC == BC_EQ) {
1511                                 E->RI->Out2.RegX = (unsigned char)P->Num;
1512                             } else {
1513                                 E->RI->Out.RegX = (unsigned char)P->Num;
1514                             }
1515                         }
1516                         break;
1517
1518                     case OP65_CPY:
1519                         /* If this is an immidiate compare, the Y register has
1520                          * the value of the compare later.
1521                          */
1522                         if (CE_KnownImm (P)) {
1523                             if (BC == BC_EQ) {
1524                                 E->RI->Out2.RegY = (unsigned char)P->Num;
1525                             } else {
1526                                 E->RI->Out.RegY = (unsigned char)P->Num;
1527                             }
1528                         }
1529                         break;
1530
1531                     case OP65_DEX:
1532                     case OP65_INX:
1533                     case OP65_LDX:
1534                     case OP65_PLX:
1535                         /* X is zero in one execution flow direction */
1536                         if (BC == BC_EQ) {
1537                             E->RI->Out2.RegX = 0;
1538                         } else {
1539                             E->RI->Out.RegX = 0;
1540                         }
1541                         break;
1542
1543                     case OP65_DEY:
1544                     case OP65_INY:
1545                     case OP65_LDY:
1546                     case OP65_PLY:
1547                         /* X is zero in one execution flow direction */
1548                         if (BC == BC_EQ) {
1549                             E->RI->Out2.RegY = 0;
1550                         } else {
1551                             E->RI->Out.RegY = 0;
1552                         }
1553                         break;
1554
1555                     case OP65_TAX:
1556                     case OP65_TXA:
1557                         /* If the branch is a beq, both A and X are zero at the
1558                          * branch target, otherwise they are zero at the next
1559                          * insn.
1560                          */
1561                         if (BC == BC_EQ) {
1562                             E->RI->Out2.RegA = E->RI->Out2.RegX = 0;
1563                         } else {
1564                             E->RI->Out.RegA = E->RI->Out.RegX = 0;
1565                         }
1566                         break;
1567
1568                     case OP65_TAY:
1569                     case OP65_TYA:
1570                         /* If the branch is a beq, both A and Y are zero at the
1571                          * branch target, otherwise they are zero at the next
1572                          * insn.
1573                          */
1574                         if (BC == BC_EQ) {
1575                             E->RI->Out2.RegA = E->RI->Out2.RegY = 0;
1576                         } else {
1577                             E->RI->Out.RegA = E->RI->Out.RegY = 0;
1578                         }
1579                         break;
1580
1581                     default:
1582                         break;
1583
1584                 }
1585             }
1586         }
1587     } while (!Done);
1588
1589 }
1590
1591
1592