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