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 MoveLabelsToPool (CodeSeg* S, CodeEntry* E)
66 /* Move the labels of the code entry E to the label pool of the code segment */
68 unsigned LabelCount = GetCodeLabelCount (E);
69 while (LabelCount--) {
70 CodeLabel* L = GetCodeLabel (E, LabelCount);
73 CollAppend (&S->Labels, L);
75 CollDeleteAll (&E->Labels);
80 static CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
81 /* Find the label with the given name. Return the label or NULL if not found */
83 /* Get the first hash chain entry */
84 CodeLabel* L = S->LabelHash[Hash];
88 if (strcmp (Name, L->Name) == 0) {
99 static CodeLabel* NewCodeSegLabel (CodeSeg* S, const char* Name, unsigned Hash)
100 /* Create a new label and insert it into the label hash table */
102 /* Not found - create a new one */
103 CodeLabel* L = NewCodeLabel (Name, Hash);
105 /* Enter the label into the hash table */
106 L->Next = S->LabelHash[L->Hash];
107 S->LabelHash[L->Hash] = L;
109 /* Return the new label */
115 static void RemoveLabelFromHash (CodeSeg* S, CodeLabel* L)
116 /* Remove the given code label from the hash list */
118 /* Get the first entry in the hash chain */
119 CodeLabel* List = S->LabelHash[L->Hash];
122 /* First, remove the label from the hash chain */
124 /* First entry in hash chain */
125 S->LabelHash[L->Hash] = L->Next;
127 /* Must search through the chain */
128 while (List->Next != L) {
129 /* If we've reached the end of the chain, something is *really* wrong */
130 CHECK (List->Next != 0);
134 /* The next entry is the one, we have been searching for */
135 List->Next = L->Next;
141 /*****************************************************************************/
142 /* Functions for parsing instructions */
143 /*****************************************************************************/
147 static const char* SkipSpace (const char* S)
148 /* Skip white space and return an updated pointer */
150 while (IsSpace (*S)) {
158 static const char* ReadToken (const char* L, const char* Term,
159 char* Buf, unsigned BufSize)
160 /* Read the next token into Buf, return the updated line pointer. The
161 * token is terminated by one of the characters given in term.
164 /* Read/copy the token */
166 unsigned ParenCount = 0;
167 while (*L && (ParenCount > 0 || strchr (Term, *L) == 0)) {
173 } else if (*L == '(') {
179 /* Terminate the buffer contents */
182 /* Return the updated line pointer */
188 static CodeEntry* ParseInsn (CodeSeg* S, LineInfo* LI, const char* L)
189 /* Parse an instruction nnd generate a code entry from it. If the line contains
190 * errors, output an error message and return NULL.
191 * For simplicity, we don't accept the broad range of input a "real" assembler
192 * does. The instruction and the argument are expected to be separated by
193 * white space, for example.
198 am_t AM = 0; /* Initialize to keep gcc silent */
205 L = ReadToken (L, " \t", Mnemo, sizeof (Mnemo));
207 /* Try to find the opcode description for the mnemonic */
208 OPC = FindOpcode (Mnemo);
210 /* If we didn't find the opcode, print an error and bail out */
212 Error ("ASM code error: %s is not a valid mnemonic", Mnemo);
216 /* Skip separator white space */
219 /* Get the addressing mode */
230 StrCopy (Arg, sizeof (Arg), L+1);
236 L = ReadToken (L+1, ",)", Arg, sizeof (Arg));
238 /* Check for errors */
240 Error ("ASM code error: syntax error");
244 /* Check the different indirect modes */
246 /* Expect zp x indirect */
248 if (toupper (*L) != 'X') {
249 Error ("ASM code error: `X' expected");
254 Error ("ASM code error: `)' expected");
259 Error ("ASM code error: syntax error");
263 } else if (*L == ')') {
264 /* zp indirect or zp indirect, y */
268 if (toupper (*L) != 'Y') {
269 Error ("ASM code error: `Y' expected");
274 Error ("ASM code error: syntax error");
278 } else if (*L == '\0') {
281 Error ("ASM code error: syntax error");
297 /* Absolute, maybe indexed */
298 L = ReadToken (L, ",", Arg, sizeof (Arg));
300 /* Absolute, zeropage or branch */
301 if ((OPC->Info & OF_BRA) != 0) {
304 } else if (IsZPName (Arg)) {
309 } else if (*L == ',') {
313 Error ("ASM code error: syntax error");
319 if (IsZPName (Arg)) {
324 } else if (Reg == 'Y') {
327 Error ("ASM code error: syntax error");
331 Error ("ASM code error: syntax error");
340 /* If the instruction is a branch, check for the label and generate it
341 * if it does not exist. Ignore anything but local labels here.
344 if (AM == AM_BRA && Arg[0] == 'L') {
346 /* Generate the hash over the label, then search for the label */
347 unsigned Hash = HashStr (Arg) % CS_LABEL_HASH_SIZE;
348 Label = FindCodeLabel (S, Arg, Hash);
350 /* If we don't have the label, it's a forward ref - create it */
352 /* Generate a new label */
353 Label = NewCodeSegLabel (S, Arg, Hash);
357 /* We do now have the addressing mode in AM. Allocate a new CodeEntry
358 * structure and initialize it.
360 E = NewCodeEntry (OPC->OPC, AM, Arg, Label, LI);
362 /* Return the new code entry */
368 /*****************************************************************************/
370 /*****************************************************************************/
374 CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func)
375 /* Create a new code segment, initialize and return it */
379 /* Allocate memory */
380 CodeSeg* S = xmalloc (sizeof (CodeSeg));
382 /* Initialize the fields */
383 S->SegName = xstrdup (SegName);
385 InitCollection (&S->Entries);
386 InitCollection (&S->Labels);
387 for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
391 /* If we have a function given, get the return type of the function.
392 * Assume ANY return type besides void will use the A and X registers.
394 if (S->Func && !IsTypeVoid (GetFuncReturn (Func->Type))) {
395 S->ExitRegs = REG_AX;
397 S->ExitRegs = REG_NONE;
400 /* Return the new struct */
406 void AddCodeEntry (CodeSeg* S, LineInfo* LI, const char* Format, va_list ap)
407 /* Add a line to the given code segment */
413 /* Format the line */
415 xvsprintf (Buf, sizeof (Buf), Format, ap);
417 /* Skip whitespace */
420 /* Check which type of instruction we have */
421 E = 0; /* Assume no insn created */
425 /* Empty line, just ignore it */
429 /* Comment or hint, ignore it for now */
433 /* Control instruction */
434 ReadToken (L, " \t", Token, sizeof (Token));
435 Error ("ASM code error: Pseudo instruction `%s' not supported", Token);
439 E = ParseInsn (S, LI, L);
443 /* If we have a code entry, transfer the labels and insert it */
446 /* Transfer the labels if we have any */
448 unsigned LabelCount = CollCount (&S->Labels);
449 for (I = 0; I < LabelCount; ++I) {
452 CodeLabel* L = CollAt (&S->Labels, I);
454 /* Attach it to the entry */
455 AttachCodeLabel (E, L);
458 /* Delete the transfered labels */
459 CollDeleteAll (&S->Labels);
461 /* Add the entry to the list of code entries in this segment */
462 CollAppend (&S->Entries, E);
469 void InsertCodeEntry (CodeSeg* S, struct CodeEntry* E, unsigned Index)
470 /* Insert the code entry at the index given. Following code entries will be
471 * moved to slots with higher indices.
474 /* Insert the entry into the collection */
475 CollInsert (&S->Entries, E, Index);
480 void DelCodeEntry (CodeSeg* S, unsigned Index)
481 /* Delete an entry from the code segment. This includes moving any associated
482 * labels, removing references to labels and even removing the referenced labels
483 * if the reference count drops to zero.
486 /* Get the code entry for the given index */
487 CodeEntry* E = GetCodeEntry (S, Index);
489 /* If the entry has a labels, we have to move this label to the next insn.
490 * If there is no next insn, move the label into the code segement label
491 * pool. The operation is further complicated by the fact that the next
492 * insn may already have a label. In that case change all reference to
493 * this label and delete the label instead of moving it.
495 unsigned Count = GetCodeLabelCount (E);
498 /* The instruction has labels attached. Check if there is a next
501 if (Index == GetCodeEntryCount (S)-1) {
503 /* No next instruction, move to the codeseg label pool */
504 MoveLabelsToPool (S, E);
508 /* There is a next insn, get it */
509 CodeEntry* N = GetCodeEntry (S, Index+1);
511 /* Move labels to the next entry */
512 MoveCodeLabels (S, E, N);
517 /* If this insn references a label, remove the reference. And, if the
518 * the reference count for this label drops to zero, remove this label.
521 /* Remove the reference */
522 RemoveCodeLabelRef (S, E);
525 /* Delete the pointer to the insn */
526 CollDelete (&S->Entries, Index);
528 /* Delete the instruction itself */
534 void DelCodeEntries (CodeSeg* S, unsigned Start, unsigned Count)
535 /* Delete a range of code entries. This includes removing references to labels,
536 * labels attached to the entries and so on.
539 /* Start deleting the entries from the rear, because this involves less
543 DelCodeEntry (S, Start + Count);
549 void MoveCodeEntry (CodeSeg* S, unsigned OldPos, unsigned NewPos)
550 /* Move an entry from one position to another. OldPos is the current position
551 * of the entry, NewPos is the new position of the entry.
554 /* Get the code entry and remove it from the collection */
555 CodeEntry* E = GetCodeEntry (S, OldPos);
556 CollDelete (&S->Entries, OldPos);
558 /* Correct NewPos if needed */
559 if (NewPos >= OldPos) {
560 /* Position has changed with removal */
564 /* Now insert it at the new position */
565 CollInsert (&S->Entries, E, NewPos);
570 struct CodeEntry* GetNextCodeEntry (CodeSeg* S, unsigned Index)
571 /* Get the code entry following the one with the index Index. If there is no
572 * following code entry, return NULL.
575 if (Index >= CollCount (&S->Entries)-1) {
576 /* This is the last entry */
579 /* Code entries left */
580 return CollAtUnchecked (&S->Entries, Index+1);
586 int GetCodeEntries (CodeSeg* S, struct CodeEntry** List,
587 unsigned Start, unsigned Count)
588 /* Get Count code entries into List starting at index start. Return true if
589 * we got the lines, return false if not enough lines were available.
592 /* Check if enough entries are available */
593 if (Start + Count > CollCount (&S->Entries)) {
597 /* Copy the entries */
599 *List++ = CollAtUnchecked (&S->Entries, Start++);
602 /* We have the entries */
608 unsigned GetCodeEntryIndex (CodeSeg* S, struct CodeEntry* E)
609 /* Return the index of a code entry */
611 int Index = CollIndex (&S->Entries, E);
618 void AddCodeLabel (CodeSeg* S, const char* Name)
619 /* Add a code label for the next instruction to follow */
621 /* Calculate the hash from the name */
622 unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
624 /* Try to find the code label if it does already exist */
625 CodeLabel* L = FindCodeLabel (S, Name, Hash);
627 /* Did we find it? */
629 /* We found it - be sure it does not already have an owner */
630 CHECK (L->Owner == 0);
632 /* Not found - create a new one */
633 L = NewCodeSegLabel (S, Name, Hash);
636 /* Safety. This call is quite costly, but safety is better */
637 if (CollIndex (&S->Labels, L) >= 0) {
638 Internal ("AddCodeLabel: Label `%s' already defined", Name);
641 /* We do now have a valid label. Remember it for later */
642 CollAppend (&S->Labels, L);
647 CodeLabel* GenCodeLabel (CodeSeg* S, struct CodeEntry* E)
648 /* If the code entry E does already have a label, return it. Otherwise
649 * create a new label, attach it to E and return it.
654 if (CodeEntryHasLabel (E)) {
656 /* Get the label from this entry */
657 L = GetCodeLabel (E, 0);
662 const char* Name = LocalLabelName (GetLocalLabel ());
664 /* Generate the hash over the name */
665 unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
667 /* Create a new label */
668 L = NewCodeSegLabel (S, Name, Hash);
670 /* Attach this label to the code entry */
671 AttachCodeLabel (E, L);
675 /* Return the label */
681 void DelCodeLabel (CodeSeg* S, CodeLabel* L)
682 /* Remove references from this label and delete it. */
686 /* First, remove the label from the hash chain */
687 RemoveLabelFromHash (S, L);
689 /* Remove references from insns jumping to this label */
690 Count = CollCount (&L->JumpFrom);
691 for (I = 0; I < Count; ++I) {
692 /* Get the insn referencing this label */
693 CodeEntry* E = CollAt (&L->JumpFrom, I);
694 /* Remove the reference */
697 CollDeleteAll (&L->JumpFrom);
699 /* Remove the reference to the owning instruction if it has one. The
700 * function may be called for a label without an owner when deleting
701 * unfinished parts of the code. This is unfortunate since it allows
702 * errors to slip through.
705 CollDeleteItem (&L->Owner->Labels, L);
708 /* All references removed, delete the label itself */
714 void MergeCodeLabels (CodeSeg* S)
715 /* Merge code labels. That means: For each instruction, remove all labels but
716 * one and adjust references accordingly.
721 /* Walk over all code entries */
722 unsigned EntryCount = GetCodeEntryCount (S);
723 for (I = 0; I < EntryCount; ++I) {
728 /* Get a pointer to the next entry */
729 CodeEntry* E = GetCodeEntry (S, I);
731 /* If this entry has zero labels, continue with the next one */
732 unsigned LabelCount = GetCodeLabelCount (E);
733 if (LabelCount == 0) {
737 /* We have at least one label. Use the first one as reference label. */
738 RefLab = GetCodeLabel (E, 0);
740 /* Walk through the remaining labels and change references to these
741 * labels to a reference to the one and only label. Delete the labels
742 * that are no longer used. To increase performance, walk backwards
745 for (J = LabelCount-1; J >= 1; --J) {
747 /* Get the next label */
748 CodeLabel* L = GetCodeLabel (E, J);
750 /* Move all references from this label to the reference label */
751 MoveLabelRefs (L, RefLab);
753 /* Remove the label completely. */
757 /* The reference label is the only remaining label. Check if there
758 * are any references to this label, and delete it if this is not
761 if (CollCount (&RefLab->JumpFrom) == 0) {
762 /* Delete the label */
763 DelCodeLabel (S, RefLab);
770 void MoveCodeLabels (CodeSeg* S, struct CodeEntry* Old, struct CodeEntry* New)
771 /* Move all labels from Old to New. The routine will move the labels itself
772 * if New does not have any labels, and move references if there is at least
773 * a label for new. If references are moved, the old label is deleted
777 /* Get the number of labels to move */
778 unsigned OldLabelCount = GetCodeLabelCount (Old);
780 /* Does the new entry have itself a label? */
781 if (CodeEntryHasLabel (New)) {
783 /* The new entry does already have a label - move references */
784 CodeLabel* NewLabel = GetCodeLabel (New, 0);
785 while (OldLabelCount--) {
787 /* Get the next label */
788 CodeLabel* OldLabel = GetCodeLabel (Old, OldLabelCount);
790 /* Move references */
791 MoveLabelRefs (OldLabel, NewLabel);
793 /* Delete the label */
794 DelCodeLabel (S, OldLabel);
800 /* The new entry does not have a label, just move them */
801 while (OldLabelCount--) {
803 /* Move the label to the new entry */
804 MoveCodeLabel (GetCodeLabel (Old, OldLabelCount), New);
813 void RemoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E)
814 /* Remove the reference between E and the label it jumps to. The reference
815 * will be removed on both sides and E->JumpTo will be 0 after that. If
816 * the reference was the only one for the label, the label will get
820 /* Get a pointer to the label and make sure it exists */
821 CodeLabel* L = E->JumpTo;
824 /* Delete the entry from the label */
825 CollDeleteItem (&L->JumpFrom, E);
827 /* The entry jumps no longer to L */
830 /* If there are no more references, delete the label */
831 if (CollCount (&L->JumpFrom) == 0) {
838 void MoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E, CodeLabel* L)
839 /* Change the reference of E to L instead of the current one. If this
840 * was the only reference to the old label, the old label will get
844 /* Get the old label */
845 CodeLabel* OldLabel = E->JumpTo;
847 /* Be sure that code entry references a label */
848 PRECONDITION (OldLabel != 0);
850 /* Remove the reference to our label */
851 RemoveCodeLabelRef (S, E);
853 /* Use the new label */
859 void DelCodeSegAfter (CodeSeg* S, unsigned Last)
860 /* Delete all entries including the given one */
862 /* Get the number of entries in this segment */
863 unsigned Count = GetCodeEntryCount (S);
865 /* First pass: Delete all references to labels. If the reference count
866 * for a label drops to zero, delete it.
871 /* Get the next entry */
872 CodeEntry* E = GetCodeEntry (S, C);
874 /* Check if this entry has a label reference */
876 /* If the label is a label in the label pool and this is the last
877 * reference to the label, remove the label from the pool.
879 CodeLabel* L = E->JumpTo;
880 int Index = CollIndex (&S->Labels, L);
881 if (Index >= 0 && CollCount (&L->JumpFrom) == 1) {
882 /* Delete it from the pool */
883 CollDelete (&S->Labels, Index);
886 /* Remove the reference to the label */
887 RemoveCodeLabelRef (S, E);
892 /* Second pass: Delete the instructions. If a label attached to an
893 * instruction still has references, it must be references from outside
894 * the deleted area. Don't delete the label in this case, just make it
895 * ownerless and move it to the label pool.
900 /* Get the next entry */
901 CodeEntry* E = GetCodeEntry (S, C);
903 /* Check if this entry has a label attached */
904 if (CodeEntryHasLabel (E)) {
905 /* Move the labels to the pool and clear the owner pointer */
906 MoveLabelsToPool (S, E);
909 /* Delete the pointer to the entry */
910 CollDelete (&S->Entries, C);
912 /* Delete the entry itself */
919 void OutputCodeSeg (const CodeSeg* S, FILE* F)
920 /* Output the code segment data to a file */
925 /* Get the number of entries in this segment */
926 unsigned Count = GetCodeEntryCount (S);
928 /* If the code segment is empty, bail out here */
933 /* Output the segment directive */
934 fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
936 /* If this is a segment for a function, enter a function */
938 fprintf (F, ".proc\t_%s\n\n", S->Func->Name);
941 /* Output all entries, prepended by the line information if it has changed */
943 for (I = 0; I < Count; ++I) {
944 /* Get the next entry */
945 const CodeEntry* E = CollConstAt (&S->Entries, I);
946 /* Check if the line info has changed. If so, output the source line
947 * if the option is enabled and output debug line info if the debug
951 /* Line info has changed, remember the new line info */
954 /* Add the source line as a comment */
956 fprintf (F, ";\n; %s\n;\n", LI->Line);
959 /* Add line debug info */
961 fprintf (F, "\t.dbg\tline, \"%s\", %u\n",
962 GetInputName (LI), GetInputLine (LI));
965 /* Output the code */
966 OutputCodeEntry (E, F);
969 /* If debug info is enabled, terminate the last line number information */
971 fprintf (F, "\t.dbg\tline\n");
974 /* If this is a segment for a function, leave the function */
976 fprintf (F, "\n.endproc\n\n");