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 /*****************************************************************************/
56 /*****************************************************************************/
57 /* Helper functions */
58 /*****************************************************************************/
62 static void MoveLabelsToPool (CodeSeg* S, CodeEntry* E)
63 /* Move the labels of the code entry E to the label pool of the code segment */
65 unsigned LabelCount = GetCodeLabelCount (E);
66 while (LabelCount--) {
67 CodeLabel* L = GetCodeLabel (E, LabelCount);
70 CollAppend (&S->Labels, L);
72 CollDeleteAll (&E->Labels);
77 static CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
78 /* Find the label with the given name. Return the label or NULL if not found */
80 /* Get the first hash chain entry */
81 CodeLabel* L = S->LabelHash[Hash];
85 if (strcmp (Name, L->Name) == 0) {
96 /*****************************************************************************/
97 /* Functions for parsing instructions */
98 /*****************************************************************************/
102 static CodeLabel* NewCodeSegLabel (CodeSeg* S, const char* Name, unsigned Hash)
103 /* Create a new label and insert it into the label hash table */
105 /* Not found - create a new one */
106 CodeLabel* L = NewCodeLabel (Name, Hash);
108 /* Enter the label into the hash table */
109 L->Next = S->LabelHash[L->Hash];
110 S->LabelHash[L->Hash] = L;
112 /* Return the new label */
118 static const char* SkipSpace (const char* S)
119 /* Skip white space and return an updated pointer */
121 while (IsSpace (*S)) {
129 static const char* ReadToken (const char* L, const char* Term,
130 char* Buf, unsigned BufSize)
131 /* Read the next token into Buf, return the updated line pointer. The
132 * token is terminated by one of the characters given in term.
135 /* Read/copy the token */
137 unsigned ParenCount = 0;
138 while (*L && (ParenCount > 0 || strchr (Term, *L) == 0)) {
144 } else if (*L == '(') {
150 /* Terminate the buffer contents */
153 /* Return the updated line pointer */
159 static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
160 /* Parse an instruction nnd generate a code entry from it. If the line contains
161 * errors, output an error message and return NULL.
162 * For simplicity, we don't accept the broad range of input a "real" assembler
163 * does. The instruction and the argument are expected to be separated by
164 * white space, for example.
169 am_t AM = 0; /* Initialize to keep gcc silent */
176 L = ReadToken (L, " \t", Mnemo, sizeof (Mnemo));
178 /* Try to find the opcode description for the mnemonic */
179 OPC = FindOpcode (Mnemo);
181 /* If we didn't find the opcode, print an error and bail out */
183 Error ("ASM code error: %s is not a valid mnemonic", Mnemo);
187 /* Skip separator white space */
190 /* Get the addressing mode */
201 StrCopy (Arg, sizeof (Arg), L+1);
207 L = ReadToken (L+1, ",)", Arg, sizeof (Arg));
209 /* Check for errors */
211 Error ("ASM code error: syntax error");
215 /* Check the different indirect modes */
217 /* Expect zp x indirect */
219 if (toupper (*L) != 'X') {
220 Error ("ASM code error: `X' expected");
225 Error ("ASM code error: `)' expected");
230 Error ("ASM code error: syntax error");
234 } else if (*L == ')') {
235 /* zp indirect or zp indirect, y */
239 if (toupper (*L) != 'Y') {
240 Error ("ASM code error: `Y' expected");
245 Error ("ASM code error: syntax error");
249 } else if (*L == '\0') {
252 Error ("ASM code error: syntax error");
268 /* Absolute, maybe indexed */
269 L = ReadToken (L, ",", Arg, sizeof (Arg));
271 /* Assume absolute */
273 } else if (*L == ',') {
277 Error ("ASM code error: syntax error");
284 } else if (Reg == 'Y') {
287 Error ("ASM code error: syntax error");
291 Error ("ASM code error: syntax error");
300 /* If the instruction is a branch, check for the label and generate it
301 * if it does not exist. Ignore anything but local labels here.
304 if ((OPC->Info & OF_BRA) != 0 && Arg[0] == 'L') {
308 /* Addressing mode must be alsobute or something is really wrong */
309 CHECK (AM == AM_ABS);
311 /* Addressing mode is a branch/jump */
314 /* Generate the hash over the label, then search for the label */
315 Hash = HashStr (Arg) % CS_LABEL_HASH_SIZE;
316 Label = FindCodeLabel (S, Arg, Hash);
318 /* If we don't have the label, it's a forward ref - create it */
320 /* Generate a new label */
321 Label = NewCodeSegLabel (S, Arg, Hash);
325 /* We do now have the addressing mode in AM. Allocate a new CodeEntry
326 * structure and initialize it.
328 E = NewCodeEntry (OPC, AM, Arg, Label);
330 /* Return the new code entry */
336 /*****************************************************************************/
338 /*****************************************************************************/
342 CodeSeg* NewCodeSeg (const char* SegName, SymEntry* Func)
343 /* Create a new code segment, initialize and return it */
347 /* Allocate memory */
348 CodeSeg* S = xmalloc (sizeof (CodeSeg));
350 /* Initialize the fields */
351 S->SegName = xstrdup (SegName);
353 InitCollection (&S->Entries);
354 InitCollection (&S->Labels);
355 for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
359 /* Return the new struct */
365 void AddCodeEntry (CodeSeg* S, const char* Format, va_list ap)
366 /* Add a line to the given code segment */
372 /* Format the line */
374 xvsprintf (Buf, sizeof (Buf), Format, ap);
376 /* Skip whitespace */
379 /* Check which type of instruction we have */
380 E = 0; /* Assume no insn created */
384 /* Empty line, just ignore it */
388 /* Comment or hint, ignore it for now */
392 /* Control instruction */
393 ReadToken (L, " \t", Token, sizeof (Token));
394 Error ("ASM code error: Pseudo instruction `%s' not supported", Token);
398 E = ParseInsn (S, L);
402 /* If we have a code entry, transfer the labels and insert it */
405 /* Transfer the labels if we have any */
407 unsigned LabelCount = CollCount (&S->Labels);
408 for (I = 0; I < LabelCount; ++I) {
411 CodeLabel* L = CollAt (&S->Labels, I);
413 /* Attach it to the entry */
414 AttachCodeLabel (E, L);
417 /* Delete the transfered labels */
418 CollDeleteAll (&S->Labels);
420 /* Add the entry to the list of code entries in this segment */
421 CollAppend (&S->Entries, E);
428 void DelCodeEntry (CodeSeg* S, unsigned Index)
429 /* Delete an entry from the code segment. This includes moving any associated
430 * labels, removing references to labels and even removing the referenced labels
431 * if the reference count drops to zero.
434 /* Get the code entry for the given index */
435 CodeEntry* E = GetCodeEntry (S, Index);
437 /* If the entry has a labels, we have to move this label to the next insn.
438 * If there is no next insn, move the label into the code segement label
439 * pool. The operation is further complicated by the fact that the next
440 * insn may already have a label. In that case change all reference to
441 * this label and delete the label instead of moving it.
443 unsigned Count = GetCodeLabelCount (E);
446 /* The instruction has labels attached. Check if there is a next
449 if (Index == GetCodeEntryCount (S)-1) {
451 /* No next instruction, move to the codeseg label pool */
452 MoveLabelsToPool (S, E);
456 /* There is a next insn, get it */
457 CodeEntry* N = GetCodeEntry (S, Index+1);
459 /* Move labels to the next entry */
460 MoveCodeLabels (S, E, N);
465 /* If this insn references a label, remove the reference. And, if the
466 * the reference count for this label drops to zero, remove this label.
469 /* Remove the reference */
470 RemoveCodeLabelRef (S, E);
473 /* Delete the pointer to the insn */
474 CollDelete (&S->Entries, Index);
476 /* Delete the instruction itself */
482 struct CodeEntry* GetCodeEntry (CodeSeg* S, unsigned Index)
483 /* Get an entry from the given code segment */
485 return CollAt (&S->Entries, Index);
490 unsigned GetCodeEntryIndex (CodeSeg* S, struct CodeEntry* E)
491 /* Return the index of a code entry */
493 int Index = CollIndex (&S->Entries, E);
500 void AddCodeLabel (CodeSeg* S, const char* Name)
501 /* Add a code label for the next instruction to follow */
503 /* Calculate the hash from the name */
504 unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
506 /* Try to find the code label if it does already exist */
507 CodeLabel* L = FindCodeLabel (S, Name, Hash);
509 /* Did we find it? */
511 /* We found it - be sure it does not already have an owner */
512 CHECK (L->Owner == 0);
514 /* Not found - create a new one */
515 L = NewCodeSegLabel (S, Name, Hash);
518 /* We do now have a valid label. Remember it for later */
519 CollAppend (&S->Labels, L);
524 CodeLabel* GenCodeLabel (CodeSeg* S, struct CodeEntry* E)
525 /* If the code entry E does already have a label, return it. Otherwise
526 * create a new label, attach it to E and return it.
531 if (CodeEntryHasLabel (E)) {
533 /* Get the label from this entry */
534 L = GetCodeLabel (E, 0);
539 const char* Name = LocalLabelName (GetLocalLabel ());
541 /* Generate the hash over the name */
542 unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
544 /* Create a new label */
545 L = NewCodeSegLabel (S, Name, Hash);
547 /* Attach this label to the code entry */
548 AttachCodeLabel (E, L);
552 /* Return the label */
558 void DelCodeLabel (CodeSeg* S, CodeLabel* L)
559 /* Remove references from this label and delete it. */
563 /* Get the first entry in the hash chain */
564 CodeLabel* List = S->LabelHash[L->Hash];
566 /* First, remove the label from the hash chain */
568 /* First entry in hash chain */
569 S->LabelHash[L->Hash] = L->Next;
571 /* Must search through the chain */
572 while (List->Next != L) {
573 /* If we've reached the end of the chain, something is *really* wrong */
574 CHECK (List->Next != 0);
578 /* The next entry is the one, we have been searching for */
579 List->Next = L->Next;
582 /* Remove references from insns jumping to this label */
583 Count = CollCount (&L->JumpFrom);
584 for (I = 0; I < Count; ++I) {
585 /* Get the insn referencing this label */
586 CodeEntry* E = CollAt (&L->JumpFrom, I);
587 /* Remove the reference */
590 CollDeleteAll (&L->JumpFrom);
592 /* Remove the reference to the owning instruction */
593 CollDeleteItem (&L->Owner->Labels, L);
595 /* All references removed, delete the label itself */
601 void MergeCodeLabels (CodeSeg* S)
602 /* Merge code labels. That means: For each instruction, remove all labels but
603 * one and adjust references accordingly.
608 /* Walk over all code entries */
609 unsigned EntryCount = GetCodeEntryCount (S);
610 for (I = 0; I < EntryCount; ++I) {
615 /* Get a pointer to the next entry */
616 CodeEntry* E = GetCodeEntry (S, I);
618 /* If this entry has zero labels, continue with the next one */
619 unsigned LabelCount = GetCodeLabelCount (E);
620 if (LabelCount == 0) {
624 /* We have at least one label. Use the first one as reference label. */
625 RefLab = GetCodeLabel (E, 0);
627 /* Walk through the remaining labels and change references to these
628 * labels to a reference to the one and only label. Delete the labels
629 * that are no longer used. To increase performance, walk backwards
632 for (J = LabelCount-1; J >= 1; --J) {
634 /* Get the next label */
635 CodeLabel* L = GetCodeLabel (E, J);
637 /* Move all references from this label to the reference label */
638 MoveLabelRefs (L, RefLab);
640 /* Remove the label completely. */
644 /* The reference label is the only remaining label. Check if there
645 * are any references to this label, and delete it if this is not
648 if (CollCount (&RefLab->JumpFrom) == 0) {
649 /* Delete the label */
650 DelCodeLabel (S, RefLab);
657 void MoveCodeLabels (CodeSeg* S, struct CodeEntry* Old, struct CodeEntry* New)
658 /* Move all labels from Old to New. The routine will move the labels itself
659 * if New does not have any labels, and move references if there is at least
660 * a label for new. If references are moved, the old label is deleted
664 /* Get the number of labels to move */
665 unsigned OldLabelCount = GetCodeLabelCount (Old);
667 /* Does the new entry have itself a label? */
668 if (CodeEntryHasLabel (New)) {
670 /* The new entry does already have a label - move references */
671 CodeLabel* NewLabel = GetCodeLabel (New, 0);
672 while (OldLabelCount--) {
674 /* Get the next label */
675 CodeLabel* OldLabel = GetCodeLabel (Old, OldLabelCount);
677 /* Move references */
678 MoveLabelRefs (OldLabel, NewLabel);
680 /* Delete the label */
681 DelCodeLabel (S, OldLabel);
687 /* The new entry does not have a label, just move them */
688 while (OldLabelCount--) {
690 /* Move the label to the new entry */
691 MoveCodeLabel (GetCodeLabel (Old, OldLabelCount), New);
700 void RemoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E)
701 /* Remove the reference between E and the label it jumps to. The reference
702 * will be removed on both sides and E->JumpTo will be 0 after that. If
703 * the reference was the only one for the label, the label will get
707 /* Get a pointer to the label and make sure it exists */
708 CodeLabel* L = E->JumpTo;
711 /* Delete the entry from the label */
712 CollDeleteItem (&L->JumpFrom, E);
714 /* The entry jumps no longer to L */
717 /* If there are no more references, delete the label */
718 if (CollCount (&L->JumpFrom) == 0) {
725 void MoveCodeLabelRef (CodeSeg* S, struct CodeEntry* E, CodeLabel* L)
726 /* Change the reference of E to L instead of the current one. If this
727 * was the only reference to the old label, the old label will get
731 /* Get the old label */
732 CodeLabel* OldLabel = E->JumpTo;
734 /* Be sure that code entry references a label */
735 PRECONDITION (OldLabel != 0);
737 /* Remove the reference to our label */
738 RemoveCodeLabelRef (S, E);
740 /* Use the new label */
746 void AddCodeSegHint (CodeSeg* S, unsigned Hint)
747 /* Add a hint for the preceeding instruction */
751 /* Get the number of entries in this segment */
752 unsigned EntryCount = GetCodeEntryCount (S);
754 /* Must have at least one entry */
755 CHECK (EntryCount > 0);
757 /* Get the last entry */
758 E = GetCodeEntry (S, EntryCount-1);
766 void DelCodeSegAfter (CodeSeg* S, unsigned Last)
767 /* Delete all entries including the given one */
769 /* Get the number of entries in this segment */
770 unsigned Count = GetCodeEntryCount (S);
772 /* Remove all entries after the given one */
773 while (Last < Count) {
775 /* Get the next entry */
776 CodeEntry* E = GetCodeEntry (S, Count-1);
778 /* We have to transfer all labels to the code segment label pool */
779 MoveLabelsToPool (S, E);
781 /* Remove the code entry */
783 CollDelete (&S->Entries, Count-1);
790 void OutputCodeSeg (const CodeSeg* S, FILE* F)
791 /* Output the code segment data to a file */
795 /* Get the number of entries in this segment */
796 unsigned Count = GetCodeEntryCount (S);
798 /* If the code segment is empty, bail out here */
803 /* Output the segment directive */
804 fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
806 /* If this is a segment for a function, enter a function */
808 fprintf (F, ".proc\t_%s\n\n", S->Func->Name);
811 /* Output all entries */
812 for (I = 0; I < Count; ++I) {
813 OutputCodeEntry (CollConstAt (&S->Entries, I), F);
816 /* If this is a segment for a function, leave the function */
818 fprintf (F, "\n.endproc\n\n");
824 unsigned GetCodeEntryCount (const CodeSeg* S)
825 /* Return the number of entries for the given code segment */
827 return CollCount (&S->Entries);