1 /*****************************************************************************/
5 /* Code segment structure */
9 /* (C) 2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
59 /*****************************************************************************/
60 /* Helper functions */
61 /*****************************************************************************/
65 static void CS_MoveLabelsToEntry (CodeSeg* S, CodeEntry* E)
66 /* Move all labels from the label pool to the given entry and remove them
70 /* Transfer the labels if we have any */
72 unsigned LabelCount = CollCount (&S->Labels);
73 for (I = 0; I < LabelCount; ++I) {
76 CodeLabel* L = CollAt (&S->Labels, I);
78 /* Attach it to the entry */
79 CE_AttachLabel (E, L);
82 /* Delete the transfered labels */
83 CollDeleteAll (&S->Labels);
88 static void CS_MoveLabelsToPool (CodeSeg* S, CodeEntry* E)
89 /* Move the labels of the code entry E to the label pool of the code segment */
91 unsigned LabelCount = CE_GetLabelCount (E);
92 while (LabelCount--) {
93 CodeLabel* L = CE_GetLabel (E, LabelCount);
95 CollAppend (&S->Labels, L);
97 CollDeleteAll (&E->Labels);
102 static CodeLabel* CS_FindLabel (CodeSeg* S, const char* Name, unsigned Hash)
103 /* Find the label with the given name. Return the label or NULL if not found */
105 /* Get the first hash chain entry */
106 CodeLabel* L = S->LabelHash[Hash];
108 /* Search the list */
110 if (strcmp (Name, L->Name) == 0) {
121 static CodeLabel* CS_NewCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
122 /* Create a new label and insert it into the label hash table */
124 /* Create a new label */
125 CodeLabel* L = NewCodeLabel (Name, Hash);
127 /* Enter the label into the hash table */
128 L->Next = S->LabelHash[L->Hash];
129 S->LabelHash[L->Hash] = L;
131 /* Return the new label */
137 static void CS_RemoveLabelFromHash (CodeSeg* S, CodeLabel* L)
138 /* Remove the given code label from the hash list */
140 /* Get the first entry in the hash chain */
141 CodeLabel* List = S->LabelHash[L->Hash];
144 /* First, remove the label from the hash chain */
146 /* First entry in hash chain */
147 S->LabelHash[L->Hash] = L->Next;
149 /* Must search through the chain */
150 while (List->Next != L) {
151 /* If we've reached the end of the chain, something is *really* wrong */
152 CHECK (List->Next != 0);
156 /* The next entry is the one, we have been searching for */
157 List->Next = L->Next;
163 static CodeLabel* CS_AddLabelInternal (CodeSeg* S, const char* Name,
164 void (*ErrorFunc) (const char*, ...))
165 /* Add a code label for the next instruction to follow */
167 /* Calculate the hash from the name */
168 unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
170 /* Try to find the code label if it does already exist */
171 CodeLabel* L = CS_FindLabel (S, Name, Hash);
173 /* Did we find it? */
175 /* We found it - be sure it does not already have an owner */
177 ErrorFunc ("ASM label `%s' is already defined", Name);
180 /* Not found - create a new one */
181 L = CS_NewCodeLabel (S, Name, Hash);
184 /* Safety. This call is quite costly, but safety is better */
185 if (CollIndex (&S->Labels, L) >= 0) {
186 ErrorFunc ("ASM label `%s' is already defined", Name);
189 /* We do now have a valid label. Remember it for later */
190 CollAppend (&S->Labels, L);
192 /* Return the label */
198 /*****************************************************************************/
199 /* Functions for parsing instructions */
200 /*****************************************************************************/
204 static const char* SkipSpace (const char* S)
205 /* Skip white space and return an updated pointer */
207 while (IsSpace (*S)) {
215 static const char* ReadToken (const char* L, const char* Term,
216 char* Buf, unsigned BufSize)
217 /* Read the next token into Buf, return the updated line pointer. The
218 * token is terminated by one of the characters given in term.
221 /* Read/copy the token */
223 unsigned ParenCount = 0;
224 while (*L && (ParenCount > 0 || strchr (Term, *L) == 0)) {
227 } else if (I == BufSize-1) {
228 /* Cannot store this character, this is an input error (maybe
229 * identifier too long or similar).
231 Error ("ASM code error: syntax error");
236 } else if (*L == '(') {
242 /* Terminate the buffer contents */
245 /* Return the updated line pointer */
251 static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
252 /* Parse an instruction nnd generate a code entry from it. If the line contains
253 * errors, output an error message and return NULL.
254 * For simplicity, we don't accept the broad range of input a "real" assembler
255 * does. The instruction and the argument are expected to be separated by
256 * white space, for example.
261 am_t AM = 0; /* Initialize to keep gcc silent */
267 /* Read the first token and skip white space after it */
268 L = SkipSpace (ReadToken (L, " \t:", Mnemo, sizeof (Mnemo)));
270 /* Check if we have a label */
273 /* Skip the colon and following white space */
277 CS_AddLabelInternal (S, Mnemo, Error);
279 /* If we have reached end of line, bail out, otherwise a mnemonic
286 L = SkipSpace (ReadToken (L, " \t", Mnemo, sizeof (Mnemo)));
289 /* Try to find the opcode description for the mnemonic */
290 OPC = FindOP65 (Mnemo);
292 /* If we didn't find the opcode, print an error and bail out */
294 Error ("ASM code error: %s is not a valid mnemonic", Mnemo);
298 /* Get the addressing mode */
309 StrCopy (Arg, sizeof (Arg), L+1);
315 L = ReadToken (L+1, ",)", Arg, sizeof (Arg));
317 /* Check for errors */
319 Error ("ASM code error: syntax error");
323 /* Check the different indirect modes */
325 /* Expect zp x indirect */
327 if (toupper (*L) != 'X') {
328 Error ("ASM code error: `X' expected");
333 Error ("ASM code error: `)' expected");
338 Error ("ASM code error: syntax error");
342 } else if (*L == ')') {
343 /* zp indirect or zp indirect, y */
347 if (toupper (*L) != 'Y') {
348 Error ("ASM code error: `Y' expected");
353 Error ("ASM code error: syntax error");
357 } else if (*L == '\0') {
360 Error ("ASM code error: syntax error");
376 /* Absolute, maybe indexed */
377 L = ReadToken (L, ",", Arg, sizeof (Arg));
379 /* Absolute, zeropage or branch */
380 if ((OPC->Info & OF_BRA) != 0) {
383 } else if (GetZPInfo(Arg) != 0) {
388 } else if (*L == ',') {
392 Error ("ASM code error: syntax error");
398 if (GetZPInfo(Arg) != 0) {
403 } else if (Reg == 'Y') {
406 Error ("ASM code error: syntax error");
410 Error ("ASM code error: syntax error");
419 /* If the instruction is a branch, check for the label and generate it
420 * if it does not exist. This may lead to unused labels (if the label
421 * is actually an external one) which are removed by the CS_MergeLabels
425 if (AM == AM65_BRA) {
427 /* Generate the hash over the label, then search for the label */
428 unsigned Hash = HashStr (Arg) % CS_LABEL_HASH_SIZE;
429 Label = CS_FindLabel (S, Arg, Hash);
431 /* If we don't have the label, it's a forward ref - create it */
433 /* Generate a new label */
434 Label = CS_NewCodeLabel (S, Arg, Hash);
438 /* We do now have the addressing mode in AM. Allocate a new CodeEntry
439 * structure and initialize it.
441 E = NewCodeEntry (OPC->OPC, AM, Arg, Label, LI);
443 /* Return the new code entry */
449 /*****************************************************************************/
451 /*****************************************************************************/
455 CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func)
456 /* Create a new code segment, initialize and return it */
461 /* Allocate memory */
462 CodeSeg* S = xmalloc (sizeof (CodeSeg));
464 /* Initialize the fields */
465 S->SegName = xstrdup (SegName);
467 InitCollection (&S->Entries);
468 InitCollection (&S->Labels);
469 for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
473 /* If we have a function given, get the return type of the function.
474 * Assume ANY return type besides void will use the A and X registers.
476 if (S->Func && !IsTypeVoid ((RetType = GetFuncReturn (Func->Type)))) {
477 if (SizeOf (RetType) == SizeOf (type_long)) {
478 S->ExitRegs = REG_EAX;
480 S->ExitRegs = REG_AX;
483 S->ExitRegs = REG_NONE;
486 /* Return the new struct */
492 void CS_AddEntry (CodeSeg* S, struct CodeEntry* E)
493 /* Add an entry to the given code segment */
495 /* Transfer the labels if we have any */
496 CS_MoveLabelsToEntry (S, E);
498 /* Add the entry to the list of code entries in this segment */
499 CollAppend (&S->Entries, E);
504 void CS_AddVLine (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap)
505 /* Add a line to the given code segment */
511 /* Format the line */
513 xvsprintf (Buf, sizeof (Buf), Format, ap);
515 /* Skip whitespace */
518 /* Check which type of instruction we have */
519 E = 0; /* Assume no insn created */
523 /* Empty line, just ignore it */
527 /* Comment or hint, ignore it for now */
531 /* Control instruction */
532 ReadToken (L, " \t", Token, sizeof (Token));
533 Error ("ASM code error: Pseudo instruction `%s' not supported", Token);
537 E = ParseInsn (S, LI, L);
541 /* If we have a code entry, transfer the labels and insert it */
549 void CS_AddLine (CodeSeg* S, LineInfo* LI, const char* Format, ...)
550 /* Add a line to the given code segment */
553 va_start (ap, Format);
554 CS_AddVLine (S, LI, Format, ap);
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.
565 /* Insert the entry into the collection */
566 CollInsert (&S->Entries, E, Index);
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.
577 /* Get the code entry for the given index */
578 CodeEntry* E = CS_GetEntry (S, Index);
580 /* If the entry has a labels, we have to move this label to the next insn.
581 * If there is no next insn, move the label into the code segement label
582 * pool. The operation is further complicated by the fact that the next
583 * insn may already have a label. In that case change all reference to
584 * this label and delete the label instead of moving it.
586 unsigned Count = CE_GetLabelCount (E);
589 /* The instruction has labels attached. Check if there is a next
592 if (Index == CS_GetEntryCount (S)-1) {
594 /* No next instruction, move to the codeseg label pool */
595 CS_MoveLabelsToPool (S, E);
599 /* There is a next insn, get it */
600 CodeEntry* N = CS_GetEntry (S, Index+1);
602 /* Move labels to the next entry */
603 CS_MoveLabels (S, E, N);
608 /* If this insn references a label, remove the reference. And, if the
609 * the reference count for this label drops to zero, remove this label.
612 /* Remove the reference */
613 CS_RemoveLabelRef (S, E);
616 /* Delete the pointer to the insn */
617 CollDelete (&S->Entries, Index);
619 /* Delete the instruction itself */
625 void CS_DelEntries (CodeSeg* S, unsigned Start, unsigned Count)
626 /* Delete a range of code entries. This includes removing references to labels,
627 * labels attached to the entries and so on.
630 /* Start deleting the entries from the rear, because this involves less
634 CS_DelEntry (S, Start + Count);
640 void CS_MoveEntries (CodeSeg* S, unsigned Start, unsigned Count, unsigned NewPos)
641 /* Move a range of entries from one position to another. Start is the index
642 * of the first entry to move, Count is the number of entries and NewPos is
643 * the index of the target entry. The entry with the index Start will later
644 * have the index NewPos. All entries with indices NewPos and above are
645 * moved to higher indices. If the code block is moved to the end of the
646 * current code, and if pending labels exist, these labels will get attached
647 * to the first instruction of the moved block (the first one after the
651 /* If NewPos is at the end of the code segment, move any labels from the
652 * label pool to the first instruction of the moved range.
654 if (NewPos == CS_GetEntryCount (S)) {
655 CS_MoveLabelsToEntry (S, CS_GetEntry (S, Start));
658 /* Move the code block to the destination */
659 CollMoveMultiple (&S->Entries, Start, Count, NewPos);
664 struct CodeEntry* CS_GetPrevEntry (CodeSeg* S, unsigned Index)
665 /* Get the code entry preceeding the one with the index Index. If there is no
666 * preceeding code entry, return NULL.
670 /* This is the first entry */
673 /* Previous entry available */
674 return CollAtUnchecked (&S->Entries, Index-1);
680 struct CodeEntry* CS_GetNextEntry (CodeSeg* S, unsigned Index)
681 /* Get the code entry following the one with the index Index. If there is no
682 * following code entry, return NULL.
685 if (Index >= CollCount (&S->Entries)-1) {
686 /* This is the last entry */
689 /* Code entries left */
690 return CollAtUnchecked (&S->Entries, Index+1);
696 int CS_GetEntries (CodeSeg* S, struct CodeEntry** List,
697 unsigned Start, unsigned Count)
698 /* Get Count code entries into List starting at index start. Return true if
699 * we got the lines, return false if not enough lines were available.
702 /* Check if enough entries are available */
703 if (Start + Count > CollCount (&S->Entries)) {
707 /* Copy the entries */
709 *List++ = CollAtUnchecked (&S->Entries, Start++);
712 /* We have the entries */
718 unsigned CS_GetEntryIndex (CodeSeg* S, struct CodeEntry* E)
719 /* Return the index of a code entry */
721 int Index = CollIndex (&S->Entries, E);
728 CodeLabel* CS_AddLabel (CodeSeg* S, const char* Name)
729 /* Add a code label for the next instruction to follow */
731 return CS_AddLabelInternal (S, Name, Internal);
736 CodeLabel* CS_GenLabel (CodeSeg* S, struct CodeEntry* E)
737 /* If the code entry E does already have a label, return it. Otherwise
738 * create a new label, attach it to E and return it.
743 if (CE_HasLabel (E)) {
745 /* Get the label from this entry */
746 L = CE_GetLabel (E, 0);
751 const char* Name = LocalLabelName (GetLocalLabel ());
753 /* Generate the hash over the name */
754 unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
756 /* Create a new label */
757 L = CS_NewCodeLabel (S, Name, Hash);
759 /* Attach this label to the code entry */
760 CE_AttachLabel (E, L);
764 /* Return the label */
770 void CS_DelLabel (CodeSeg* S, CodeLabel* L)
771 /* Remove references from this label and delete it. */
775 /* First, remove the label from the hash chain */
776 CS_RemoveLabelFromHash (S, L);
778 /* Remove references from insns jumping to this label */
779 Count = CollCount (&L->JumpFrom);
780 for (I = 0; I < Count; ++I) {
781 /* Get the insn referencing this label */
782 CodeEntry* E = CollAt (&L->JumpFrom, I);
783 /* Remove the reference */
786 CollDeleteAll (&L->JumpFrom);
788 /* Remove the reference to the owning instruction if it has one. The
789 * function may be called for a label without an owner when deleting
790 * unfinished parts of the code. This is unfortunate since it allows
791 * errors to slip through.
794 CollDeleteItem (&L->Owner->Labels, L);
797 /* All references removed, delete the label itself */
803 void CS_MergeLabels (CodeSeg* S)
804 /* Merge code labels. That means: For each instruction, remove all labels but
805 * one and adjust references accordingly.
810 /* First, remove all labels from the label symbol table that don't have an
811 * owner (this means that they are actually external labels but we didn't
812 * know that previously since they may have also been forward references).
814 for (I = 0; I < CS_LABEL_HASH_SIZE; ++I) {
816 /* Get the first label in this hash chain */
817 CodeLabel** L = &S->LabelHash[I];
819 if ((*L)->Owner == 0) {
820 /* The label does not have an owner, remove it from the chain */
824 printf ("Removing unused global label `%s'", X->Name);
828 /* Label is owned, point to next code label pointer */
834 /* Walk over all code entries */
835 for (I = 0; I < CS_GetEntryCount (S); ++I) {
840 /* Get a pointer to the next entry */
841 CodeEntry* E = CS_GetEntry (S, I);
843 /* If this entry has zero labels, continue with the next one */
844 unsigned LabelCount = CE_GetLabelCount (E);
845 if (LabelCount == 0) {
849 /* We have at least one label. Use the first one as reference label. */
850 RefLab = CE_GetLabel (E, 0);
852 /* Walk through the remaining labels and change references to these
853 * labels to a reference to the one and only label. Delete the labels
854 * that are no longer used. To increase performance, walk backwards
857 for (J = LabelCount-1; J >= 1; --J) {
859 /* Get the next label */
860 CodeLabel* L = CE_GetLabel (E, J);
862 /* Move all references from this label to the reference label */
863 CL_MoveRefs (L, RefLab);
865 /* Remove the label completely. */
869 /* The reference label is the only remaining label. Check if there
870 * are any references to this label, and delete it if this is not
873 if (CollCount (&RefLab->JumpFrom) == 0) {
874 /* Delete the label */
875 CS_DelLabel (S, RefLab);
882 void CS_MoveLabels (CodeSeg* S, struct CodeEntry* Old, struct CodeEntry* New)
883 /* Move all labels from Old to New. The routine will move the labels itself
884 * if New does not have any labels, and move references if there is at least
885 * a label for new. If references are moved, the old label is deleted
889 /* Get the number of labels to move */
890 unsigned OldLabelCount = CE_GetLabelCount (Old);
892 /* Does the new entry have itself a label? */
893 if (CE_HasLabel (New)) {
895 /* The new entry does already have a label - move references */
896 CodeLabel* NewLabel = CE_GetLabel (New, 0);
897 while (OldLabelCount--) {
899 /* Get the next label */
900 CodeLabel* OldLabel = CE_GetLabel (Old, OldLabelCount);
902 /* Move references */
903 CL_MoveRefs (OldLabel, NewLabel);
905 /* Delete the label */
906 CS_DelLabel (S, OldLabel);
912 /* The new entry does not have a label, just move them */
913 while (OldLabelCount--) {
915 /* Move the label to the new entry */
916 CE_MoveLabel (CE_GetLabel (Old, OldLabelCount), New);
925 void CS_RemoveLabelRef (CodeSeg* S, struct CodeEntry* E)
926 /* Remove the reference between E and the label it jumps to. The reference
927 * will be removed on both sides and E->JumpTo will be 0 after that. If
928 * the reference was the only one for the label, the label will get
932 /* Get a pointer to the label and make sure it exists */
933 CodeLabel* L = E->JumpTo;
936 /* Delete the entry from the label */
937 CollDeleteItem (&L->JumpFrom, E);
939 /* The entry jumps no longer to L */
942 /* If there are no more references, delete the label */
943 if (CollCount (&L->JumpFrom) == 0) {
950 void CS_MoveLabelRef (CodeSeg* S, struct CodeEntry* E, CodeLabel* L)
951 /* Change the reference of E to L instead of the current one. If this
952 * was the only reference to the old label, the old label will get
956 /* Get the old label */
957 CodeLabel* OldLabel = E->JumpTo;
959 /* Be sure that code entry references a label */
960 PRECONDITION (OldLabel != 0);
962 /* Remove the reference to our label */
963 CS_RemoveLabelRef (S, E);
965 /* Use the new label */
971 void CS_DelCodeAfter (CodeSeg* S, unsigned Last)
972 /* Delete all entries including the given one */
974 /* Get the number of entries in this segment */
975 unsigned Count = CS_GetEntryCount (S);
977 /* First pass: Delete all references to labels. If the reference count
978 * for a label drops to zero, delete it.
983 /* Get the next entry */
984 CodeEntry* E = CS_GetEntry (S, C);
986 /* Check if this entry has a label reference */
988 /* If the label is a label in the label pool and this is the last
989 * reference to the label, remove the label from the pool.
991 CodeLabel* L = E->JumpTo;
992 int Index = CollIndex (&S->Labels, L);
993 if (Index >= 0 && CollCount (&L->JumpFrom) == 1) {
994 /* Delete it from the pool */
995 CollDelete (&S->Labels, Index);
998 /* Remove the reference to the label */
999 CS_RemoveLabelRef (S, E);
1004 /* Second pass: Delete the instructions. If a label attached to an
1005 * instruction still has references, it must be references from outside
1006 * the deleted area. Don't delete the label in this case, just make it
1007 * ownerless and move it to the label pool.
1010 while (Last < C--) {
1012 /* Get the next entry */
1013 CodeEntry* E = CS_GetEntry (S, C);
1015 /* Check if this entry has a label attached */
1016 if (CE_HasLabel (E)) {
1017 /* Move the labels to the pool and clear the owner pointer */
1018 CS_MoveLabelsToPool (S, E);
1021 /* Delete the pointer to the entry */
1022 CollDelete (&S->Entries, C);
1024 /* Delete the entry itself */
1031 void CS_Output (const CodeSeg* S, FILE* F)
1032 /* Output the code segment data to a file */
1037 /* Get the number of entries in this segment */
1038 unsigned Count = CS_GetEntryCount (S);
1040 /* If the code segment is empty, bail out here */
1045 /* Output the segment directive */
1046 fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
1048 /* If this is a segment for a function, enter a function */
1050 fprintf (F, ".proc\t_%s\n\n", S->Func->Name);
1053 /* Output all entries, prepended by the line information if it has changed */
1055 for (I = 0; I < Count; ++I) {
1056 /* Get the next entry */
1057 const CodeEntry* E = CollConstAt (&S->Entries, I);
1058 /* Check if the line info has changed. If so, output the source line
1059 * if the option is enabled and output debug line info if the debug
1060 * option is enabled.
1063 /* Line info has changed, remember the new line info */
1066 /* Add the source line as a comment */
1068 fprintf (F, ";\n; %s\n;\n", LI->Line);
1071 /* Add line debug info */
1073 fprintf (F, "\t.dbg\tline, \"%s\", %u\n",
1074 GetInputName (LI), GetInputLine (LI));
1077 /* Output the code */
1081 /* If debug info is enabled, terminate the last line number information */
1083 fprintf (F, "\t.dbg\tline\n");
1086 /* If this is a segment for a function, leave the function */
1088 fprintf (F, "\n.endproc\n\n");
1094 void CS_FreeRegInfo (CodeSeg* S)
1095 /* Free register infos for all instructions */
1098 for (I = 0; I < CS_GetEntryCount (S); ++I) {
1099 CE_FreeRegInfo (CS_GetEntry(S, I));
1105 void CS_GenRegInfo (CodeSeg* S)
1106 /* Generate register infos for all instructions */
1109 RegContents Regs; /* Initial register contents */
1110 RegContents* CurrentRegs; /* Current register contents */
1111 int WasJump; /* True if last insn was a jump */
1113 /* Be sure to delete all register infos */
1116 /* On entry, the register contents are unknown */
1117 RC_Invalidate (&Regs);
1118 CurrentRegs = &Regs;
1120 /* First pass. Walk over all insns and note just the changes from one
1121 * insn to the next one.
1124 for (I = 0; I < CS_GetEntryCount (S); ++I) {
1128 /* Get the next instruction */
1129 CodeEntry* E = CollAtUnchecked (&S->Entries, I);
1131 /* If the instruction has a label, we need some special handling */
1132 unsigned LabelCount = CE_GetLabelCount (E);
1133 if (LabelCount > 0) {
1135 /* Loop over all entry points that jump here. If these entry
1136 * points already have register info, check if all values are
1137 * known and identical. If all values are identical, and the
1138 * preceeding instruction was not an unconditional branch, check
1139 * if the register value on exit of the preceeding instruction
1140 * is also identical. If all these values are identical, the
1141 * value of a register is known, otherwise it is unknown.
1143 CodeLabel* Label = CE_GetLabel (E, 0);
1146 /* Preceeding insn was an unconditional branch */
1147 CodeEntry* J = CL_GetRef(Label, 0);
1151 RC_Invalidate (&Regs);
1155 Regs = *CurrentRegs;
1159 while (Entry < CL_GetRefCount (Label)) {
1160 /* Get this entry */
1161 CodeEntry* J = CL_GetRef (Label, Entry);
1163 /* No register info for this entry, bail out */
1164 RC_Invalidate (&Regs);
1167 if (J->RI->Out2.RegA != Regs.RegA) {
1170 if (J->RI->Out2.RegX != Regs.RegX) {
1173 if (J->RI->Out2.RegY != Regs.RegY) {
1176 if (J->RI->Out2.SRegLo != Regs.SRegLo) {
1179 if (J->RI->Out2.SRegHi != Regs.SRegHi) {
1185 /* Use this register info */
1186 CurrentRegs = &Regs;
1190 /* Generate register info for this instruction */
1191 CE_GenRegInfo (E, CurrentRegs);
1193 /* Remember for the next insn if this insn was an uncondition branch */
1194 WasJump = (E->Info & OF_UBRA) != 0;
1196 /* Output registers for this insn are input for the next */
1197 CurrentRegs = &E->RI->Out;
1199 /* If this insn is a branch on zero flag, we may have more info on
1200 * register contents for one of both flow directions, but only if
1201 * there is a previous instruction.
1203 if ((E->Info & OF_ZBRA) != 0 && (P = CS_GetPrevEntry (S, I)) != 0) {
1205 /* Get the branch condition */
1206 bc_t BC = GetBranchCond (E->OPC);
1208 /* Check the previous instruction */
1220 /* A is zero in one execution flow direction */
1222 E->RI->Out2.RegA = 0;
1224 E->RI->Out.RegA = 0;
1229 /* If this is an immidiate compare, the A register has
1230 * the value of the compare later.
1232 if (CE_KnownImm (P)) {
1234 E->RI->Out2.RegA = (unsigned char)P->Num;
1236 E->RI->Out.RegA = (unsigned char)P->Num;
1242 /* If this is an immidiate compare, the X register has
1243 * the value of the compare later.
1245 if (CE_KnownImm (P)) {
1247 E->RI->Out2.RegX = (unsigned char)P->Num;
1249 E->RI->Out.RegX = (unsigned char)P->Num;
1255 /* If this is an immidiate compare, the Y register has
1256 * the value of the compare later.
1258 if (CE_KnownImm (P)) {
1260 E->RI->Out2.RegY = (unsigned char)P->Num;
1262 E->RI->Out.RegY = (unsigned char)P->Num;
1271 /* X is zero in one execution flow direction */
1273 E->RI->Out2.RegX = 0;
1275 E->RI->Out.RegX = 0;
1283 /* X is zero in one execution flow direction */
1285 E->RI->Out2.RegY = 0;
1287 E->RI->Out.RegY = 0;
1293 /* If the branch is a beq, both A and X are zero at the
1294 * branch target, otherwise they are zero at the next
1298 E->RI->Out2.RegA = E->RI->Out2.RegX = 0;
1300 E->RI->Out.RegA = E->RI->Out.RegX = 0;
1306 /* If the branch is a beq, both A and Y are zero at the
1307 * branch target, otherwise they are zero at the next
1311 E->RI->Out2.RegA = E->RI->Out2.RegY = 0;
1313 E->RI->Out.RegA = E->RI->Out.RegY = 0;