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 /*****************************************************************************/
58 /*****************************************************************************/
59 /* Helper functions */
60 /*****************************************************************************/
64 static void MoveLabelsToPool (CodeSeg* S, CodeEntry* E)
65 /* Move the labels of the code entry E to the label pool of the code segment */
67 unsigned LabelCount = GetCodeLabelCount (E);
68 while (LabelCount--) {
69 CodeLabel* L = GetCodeLabel (E, LabelCount);
72 CollAppend (&S->Labels, L);
74 CollDeleteAll (&E->Labels);
79 static CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
80 /* Find the label with the given name. Return the label or NULL if not found */
82 /* Get the first hash chain entry */
83 CodeLabel* L = S->LabelHash[Hash];
87 if (strcmp (Name, L->Name) == 0) {
98 static CodeLabel* NewCodeSegLabel (CodeSeg* S, const char* Name, unsigned Hash)
99 /* Create a new label and insert it into the label hash table */
101 /* Not found - create a new one */
102 CodeLabel* L = NewCodeLabel (Name, Hash);
104 /* Enter the label into the hash table */
105 L->Next = S->LabelHash[L->Hash];
106 S->LabelHash[L->Hash] = L;
108 /* Return the new label */
114 static void RemoveLabelFromHash (CodeSeg* S, CodeLabel* L)
115 /* Remove the given code label from the hash list */
117 /* Get the first entry in the hash chain */
118 CodeLabel* List = S->LabelHash[L->Hash];
121 /* First, remove the label from the hash chain */
123 /* First entry in hash chain */
124 S->LabelHash[L->Hash] = L->Next;
126 /* Must search through the chain */
127 while (List->Next != L) {
128 /* If we've reached the end of the chain, something is *really* wrong */
129 CHECK (List->Next != 0);
133 /* The next entry is the one, we have been searching for */
134 List->Next = L->Next;
140 /*****************************************************************************/
141 /* Functions for parsing instructions */
142 /*****************************************************************************/
146 static const char* SkipSpace (const char* S)
147 /* Skip white space and return an updated pointer */
149 while (IsSpace (*S)) {
157 static const char* ReadToken (const char* L, const char* Term,
158 char* Buf, unsigned BufSize)
159 /* Read the next token into Buf, return the updated line pointer. The
160 * token is terminated by one of the characters given in term.
163 /* Read/copy the token */
165 unsigned ParenCount = 0;
166 while (*L && (ParenCount > 0 || strchr (Term, *L) == 0)) {
172 } else if (*L == '(') {
178 /* Terminate the buffer contents */
181 /* Return the updated line pointer */
187 static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
188 /* Parse an instruction nnd generate a code entry from it. If the line contains
189 * errors, output an error message and return NULL.
190 * For simplicity, we don't accept the broad range of input a "real" assembler
191 * does. The instruction and the argument are expected to be separated by
192 * white space, for example.
197 am_t AM = 0; /* Initialize to keep gcc silent */
204 L = ReadToken (L, " \t", Mnemo, sizeof (Mnemo));
206 /* Try to find the opcode description for the mnemonic */
207 OPC = FindOpcode (Mnemo);
209 /* If we didn't find the opcode, print an error and bail out */
211 Error ("ASM code error: %s is not a valid mnemonic", Mnemo);
215 /* Skip separator white space */
218 /* Get the addressing mode */
229 StrCopy (Arg, sizeof (Arg), L+1);
235 L = ReadToken (L+1, ",)", Arg, sizeof (Arg));
237 /* Check for errors */
239 Error ("ASM code error: syntax error");
243 /* Check the different indirect modes */
245 /* Expect zp x indirect */
247 if (toupper (*L) != 'X') {
248 Error ("ASM code error: `X' expected");
253 Error ("ASM code error: `)' expected");
258 Error ("ASM code error: syntax error");
262 } else if (*L == ')') {
263 /* zp indirect or zp indirect, y */
267 if (toupper (*L) != 'Y') {
268 Error ("ASM code error: `Y' expected");
273 Error ("ASM code error: syntax error");
277 } else if (*L == '\0') {
280 Error ("ASM code error: syntax error");
296 /* Absolute, maybe indexed */
297 L = ReadToken (L, ",", Arg, sizeof (Arg));
299 /* Assume absolute */
301 } else if (*L == ',') {
305 Error ("ASM code error: syntax error");
312 } else if (Reg == 'Y') {
315 Error ("ASM code error: syntax error");
319 Error ("ASM code error: syntax error");
328 /* If the instruction is a branch, check for the label and generate it
329 * if it does not exist. Ignore anything but local labels here.
332 if ((OPC->Info & OF_BRA) != 0 && Arg[0] == 'L') {
336 /* Addressing mode must be alsobute or something is really wrong */
337 CHECK (AM == AM_ABS);
339 /* Addressing mode is a branch/jump */
342 /* Generate the hash over the label, then search for the label */
343 Hash = HashStr (Arg) % CS_LABEL_HASH_SIZE;
344 Label = FindCodeLabel (S, Arg, Hash);
346 /* If we don't have the label, it's a forward ref - create it */
348 /* Generate a new label */
349 Label = NewCodeSegLabel (S, Arg, Hash);
353 /* We do now have the addressing mode in AM. Allocate a new CodeEntry
354 * structure and initialize it.
356 E = NewCodeEntry (OPC->OPC, AM, Arg, Label);
358 /* Return the new code entry */
364 /*****************************************************************************/
366 /*****************************************************************************/
370 CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func)
371 /* Create a new code segment, initialize and return it */
375 /* Allocate memory */
376 CodeSeg* S = xmalloc (sizeof (CodeSeg));
378 /* Initialize the fields */
379 S->SegName = xstrdup (SegName);
381 InitCollection (&S->Entries);
382 InitCollection (&S->Labels);
383 for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
387 /* If we have a function given, get the return type of the function.
388 * Assume ANY return type besides void will use the A and X registers.
390 if (S->Func && !IsTypeVoid (GetFuncReturn (Func->Type))) {
391 S->ExitRegs = REG_AX;
393 S->ExitRegs = REG_NONE;
396 /* Return the new struct */
402 void AddCodeEntry (CodeSeg* S, const char* Format, va_list ap)
403 /* Add a line to the given code segment */
409 /* Format the line */
411 xvsprintf (Buf, sizeof (Buf), Format, ap);
413 /* Skip whitespace */
416 /* Check which type of instruction we have */
417 E = 0; /* Assume no insn created */
421 /* Empty line, just ignore it */
425 /* Comment or hint, ignore it for now */
429 /* Control instruction */
430 ReadToken (L, " \t", Token, sizeof (Token));
431 Error ("ASM code error: Pseudo instruction `%s' not supported", Token);
435 E = ParseInsn (S, L);
439 /* If we have a code entry, transfer the labels and insert it */
442 /* Transfer the labels if we have any */
444 unsigned LabelCount = CollCount (&S->Labels);
445 for (I = 0; I < LabelCount; ++I) {
448 CodeLabel* L = CollAt (&S->Labels, I);
450 /* Attach it to the entry */
451 AttachCodeLabel (E, L);
454 /* Delete the transfered labels */
455 CollDeleteAll (&S->Labels);
457 /* Add the entry to the list of code entries in this segment */
458 CollAppend (&S->Entries, E);
465 void InsertCodeEntry (CodeSeg* S, struct CodeEntry* E, unsigned Index)
466 /* Insert the code entry at the index given. Following code entries will be
467 * moved to slots with higher indices.
470 /* Insert the entry into the collection */
471 CollInsert (&S->Entries, E, Index);
476 void DelCodeEntry (CodeSeg* S, unsigned Index)
477 /* Delete an entry from the code segment. This includes moving any associated
478 * labels, removing references to labels and even removing the referenced labels
479 * if the reference count drops to zero.
482 /* Get the code entry for the given index */
483 CodeEntry* E = GetCodeEntry (S, Index);
485 /* If the entry has a labels, we have to move this label to the next insn.
486 * If there is no next insn, move the label into the code segement label
487 * pool. The operation is further complicated by the fact that the next
488 * insn may already have a label. In that case change all reference to
489 * this label and delete the label instead of moving it.
491 unsigned Count = GetCodeLabelCount (E);
494 /* The instruction has labels attached. Check if there is a next
497 if (Index == GetCodeEntryCount (S)-1) {
499 /* No next instruction, move to the codeseg label pool */
500 MoveLabelsToPool (S, E);
504 /* There is a next insn, get it */
505 CodeEntry* N = GetCodeEntry (S, Index+1);
507 /* Move labels to the next entry */
508 MoveCodeLabels (S, E, N);
513 /* If this insn references a label, remove the reference. And, if the
514 * the reference count for this label drops to zero, remove this label.
517 /* Remove the reference */
518 RemoveCodeLabelRef (S, E);
521 /* Delete the pointer to the insn */
522 CollDelete (&S->Entries, Index);
524 /* Delete the instruction itself */
530 void DelCodeEntries (CodeSeg* S, unsigned Start, unsigned Count)
531 /* Delete a range of code entries. This includes removing references to labels,
532 * labels attached to the entries and so on.
535 /* Start deleting the entries from the rear, because this involves less
539 DelCodeEntry (S, Start + Count);
545 struct CodeEntry* GetNextCodeEntry (CodeSeg* S, unsigned Index)
546 /* Get the code entry following the one with the index Index. If there is no
547 * following code entry, return NULL.
550 if (Index >= CollCount (&S->Entries)-1) {
551 /* This is the last entry */
554 /* Code entries left */
555 return CollAtUnchecked (&S->Entries, Index+1);
561 int GetCodeEntries (CodeSeg* S, struct CodeEntry** List,
562 unsigned Start, unsigned Count)
563 /* Get Count code entries into List starting at index start. Return true if
564 * we got the lines, return false if not enough lines were available.
567 /* Check if enough entries are available */
568 if (Start + Count > CollCount (&S->Entries)) {
572 /* Copy the entries */
574 *List++ = CollAtUnchecked (&S->Entries, Start++);
577 /* We have the entries */
583 unsigned GetCodeEntryIndex (CodeSeg* S, struct CodeEntry* E)
584 /* Return the index of a code entry */
586 int Index = CollIndex (&S->Entries, E);
593 void AddCodeLabel (CodeSeg* S, const char* Name)
594 /* Add a code label for the next instruction to follow */
596 /* Calculate the hash from the name */
597 unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
599 /* Try to find the code label if it does already exist */
600 CodeLabel* L = FindCodeLabel (S, Name, Hash);
602 /* Did we find it? */
604 /* We found it - be sure it does not already have an owner */
605 CHECK (L->Owner == 0);
607 /* Not found - create a new one */
608 L = NewCodeSegLabel (S, Name, Hash);
611 /* Safety. This call is quite costly, but safety is better */
612 if (CollIndex (&S->Labels, L) >= 0) {
613 Internal ("AddCodeLabel: Label `%s' already defined", Name);
616 /* We do now have a valid label. Remember it for later */
617 CollAppend (&S->Labels, L);
622 CodeLabel* GenCodeLabel (CodeSeg* S, struct CodeEntry* E)
623 /* If the code entry E does already have a label, return it. Otherwise
624 * create a new label, attach it to E and return it.
629 if (CodeEntryHasLabel (E)) {
631 /* Get the label from this entry */
632 L = GetCodeLabel (E, 0);
637 const char* Name = LocalLabelName (GetLocalLabel ());
639 /* Generate the hash over the name */
640 unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
642 /* Create a new label */
643 L = NewCodeSegLabel (S, Name, Hash);
645 /* Attach this label to the code entry */
646 AttachCodeLabel (E, L);
650 /* Return the label */
656 void DelCodeLabel (CodeSeg* S, CodeLabel* L)
657 /* Remove references from this label and delete it. */
661 /* First, remove the label from the hash chain */
662 RemoveLabelFromHash (S, L);
664 /* Remove references from insns jumping to this label */
665 Count = CollCount (&L->JumpFrom);
666 for (I = 0; I < Count; ++I) {
667 /* Get the insn referencing this label */
668 CodeEntry* E = CollAt (&L->JumpFrom, I);
669 /* Remove the reference */
672 CollDeleteAll (&L->JumpFrom);
674 /* Remove the reference to the owning instruction if it has one. The
675 * function may be called for a label without an owner when deleting
676 * unfinished parts of the code. This is unfortunate since it allows
677 * errors to slip through.
680 CollDeleteItem (&L->Owner->Labels, L);
683 /* All references removed, delete the label itself */
689 void MergeCodeLabels (CodeSeg* S)
690 /* Merge code labels. That means: For each instruction, remove all labels but
691 * one and adjust references accordingly.
696 /* Walk over all code entries */
697 unsigned EntryCount = GetCodeEntryCount (S);
698 for (I = 0; I < EntryCount; ++I) {
703 /* Get a pointer to the next entry */
704 CodeEntry* E = GetCodeEntry (S, I);
706 /* If this entry has zero labels, continue with the next one */
707 unsigned LabelCount = GetCodeLabelCount (E);
708 if (LabelCount == 0) {
712 /* We have at least one label. Use the first one as reference label. */
713 RefLab = GetCodeLabel (E, 0);
715 /* Walk through the remaining labels and change references to these
716 * labels to a reference to the one and only label. Delete the labels
717 * that are no longer used. To increase performance, walk backwards
720 for (J = LabelCount-1; J >= 1; --J) {
722 /* Get the next label */
723 CodeLabel* L = GetCodeLabel (E, J);
725 /* Move all references from this label to the reference label */
726 MoveLabelRefs (L, RefLab);
728 /* Remove the label completely. */
732 /* The reference label is the only remaining label. Check if there
733 * are any references to this label, and delete it if this is not
736 if (CollCount (&RefLab->JumpFrom) == 0) {
737 /* Delete the label */
738 DelCodeLabel (S, RefLab);
745 void MoveCodeLabels (CodeSeg* S, struct CodeEntry* Old, struct CodeEntry* New)
746 /* Move all labels from Old to New. The routine will move the labels itself
747 * if New does not have any labels, and move references if there is at least
748 * a label for new. If references are moved, the old label is deleted
752 /* Get the number of labels to move */
753 unsigned OldLabelCount = GetCodeLabelCount (Old);
755 /* Does the new entry have itself a label? */
756 if (CodeEntryHasLabel (New)) {
758 /* The new entry does already have a label - move references */
759 CodeLabel* NewLabel = GetCodeLabel (New, 0);
760 while (OldLabelCount--) {
762 /* Get the next label */
763 CodeLabel* OldLabel = GetCodeLabel (Old, OldLabelCount);
765 /* Move references */
766 MoveLabelRefs (OldLabel, NewLabel);
768 /* Delete the label */
769 DelCodeLabel (S, OldLabel);
775 /* The new entry does not have a label, just move them */
776 while (OldLabelCount--) {
778 /* Move the label to the new entry */
779 MoveCodeLabel (GetCodeLabel (Old, OldLabelCount), New);
788 void RemoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E)
789 /* Remove the reference between E and the label it jumps to. The reference
790 * will be removed on both sides and E->JumpTo will be 0 after that. If
791 * the reference was the only one for the label, the label will get
795 /* Get a pointer to the label and make sure it exists */
796 CodeLabel* L = E->JumpTo;
799 /* Delete the entry from the label */
800 CollDeleteItem (&L->JumpFrom, E);
802 /* The entry jumps no longer to L */
805 /* If there are no more references, delete the label */
806 if (CollCount (&L->JumpFrom) == 0) {
813 void MoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E, CodeLabel* L)
814 /* Change the reference of E to L instead of the current one. If this
815 * was the only reference to the old label, the old label will get
819 /* Get the old label */
820 CodeLabel* OldLabel = E->JumpTo;
822 /* Be sure that code entry references a label */
823 PRECONDITION (OldLabel != 0);
825 /* Remove the reference to our label */
826 RemoveCodeLabelRef (S, E);
828 /* Use the new label */
834 void AddCodeSegHint (CodeSeg* S, unsigned Hint)
835 /* Add a hint for the preceeding instruction */
839 /* Get the number of entries in this segment */
840 unsigned EntryCount = GetCodeEntryCount (S);
842 /* Must have at least one entry */
843 CHECK (EntryCount > 0);
845 /* Get the last entry */
846 E = GetCodeEntry (S, EntryCount-1);
854 void DelCodeSegAfter (CodeSeg* S, unsigned Last)
855 /* Delete all entries including the given one */
857 /* Get the number of entries in this segment */
858 unsigned Count = GetCodeEntryCount (S);
860 /* First pass: Delete all references to labels. If the reference count
861 * for a label drops to zero, delete it.
866 /* Get the next entry */
867 CodeEntry* E = GetCodeEntry (S, C);
869 /* Check if this entry has a label reference */
871 /* Remove the reference to the label */
872 RemoveCodeLabelRef (S, E);
877 /* Second pass: Delete the instructions. If a label attached to an
878 * instruction still has references, it must be references from outside
879 * the deleted area. Don't delete the label in this case, just make it
880 * ownerless and move it to the label pool.
885 /* Get the next entry */
886 CodeEntry* E = GetCodeEntry (S, C);
888 /* Check if this entry has a label attached */
889 if (CodeEntryHasLabel (E)) {
890 /* Move the labels to the pool and clear the owner pointer */
891 MoveLabelsToPool (S, E);
894 /* Delete the pointer to the entry */
895 CollDelete (&S->Entries, C);
897 /* Delete the entry itself */
904 void OutputCodeSeg (const CodeSeg* S, FILE* F)
905 /* Output the code segment data to a file */
909 /* Get the number of entries in this segment */
910 unsigned Count = GetCodeEntryCount (S);
912 /* If the code segment is empty, bail out here */
917 /* Output the segment directive */
918 fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
920 /* If this is a segment for a function, enter a function */
922 fprintf (F, ".proc\t_%s\n\n", S->Func->Name);
925 /* Output all entries */
926 for (I = 0; I < Count; ++I) {
930 OutputCodeEntry (CollConstAt (&S->Entries, I), F);
932 /* Print usage info */
933 Use = GetRegInfo ((CodeSeg*) S, I+1);
936 (Use & REG_A)? 'A' : '_',
937 (Use & REG_X)? 'X' : '_',
938 (Use & REG_Y)? 'Y' : '_');
941 /* If this is a segment for a function, leave the function */
943 fprintf (F, "\n.endproc\n\n");