1 /*****************************************************************************/
5 /* Code segment structure */
9 /* (C) 2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
55 /*****************************************************************************/
57 /*****************************************************************************/
61 /* Pointer to current code segment */
66 /*****************************************************************************/
67 /* Functions for parsing instructions */
68 /*****************************************************************************/
72 static CodeLabel* NewCodeSegLabel (CodeSeg* S, const char* Name, unsigned Hash)
73 /* Create a new label and insert it into the label hash table */
75 /* Not found - create a new one */
76 CodeLabel* L = NewCodeLabel (Name, Hash);
78 /* Enter the label into the hash table */
79 L->Next = S->LabelHash[L->Hash];
80 S->LabelHash[L->Hash] = L;
82 /* Return the new label */
88 static const char* SkipSpace (const char* S)
89 /* Skip white space and return an updated pointer */
91 while (IsSpace (*S)) {
99 static const char* ReadToken (const char* L, const char* Term,
100 char* Buf, unsigned BufSize)
101 /* Read the next token into Buf, return the updated line pointer. The
102 * token is terminated by one of the characters given in term.
105 /* Read/copy the token */
107 unsigned ParenCount = 0;
108 while (*L && (ParenCount > 0 || strchr (Term, *L) == 0)) {
114 } else if (*L == '(') {
120 /* Terminate the buffer contents */
123 /* Return the updated line pointer */
129 static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
130 /* Parse an instruction nnd generate a code entry from it. If the line contains
131 * errors, output an error message and return NULL.
132 * For simplicity, we don't accept the broad range of input a "real" assembler
133 * does. The instruction and the argument are expected to be separated by
134 * white space, for example.
139 am_t AM = 0; /* Initialize to keep gcc silent */
146 L = ReadToken (L, " \t", Mnemo, sizeof (Mnemo));
148 /* Try to find the opcode description for the mnemonic */
149 OPC = FindOpcode (Mnemo);
151 /* If we didn't find the opcode, print an error and bail out */
153 Error ("ASM code error: %s is not a valid mnemonic", Mnemo);
157 /* Skip separator white space */
160 /* Get the addressing mode */
171 StrCopy (Arg, sizeof (Arg), L+1);
177 L = ReadToken (L+1, ",)", Arg, sizeof (Arg));
179 /* Check for errors */
181 Error ("ASM code error: syntax error");
185 /* Check the different indirect modes */
187 /* Expect zp x indirect */
189 if (toupper (*L) != 'X') {
190 Error ("ASM code error: `X' expected");
195 Error ("ASM code error: `)' expected");
200 Error ("ASM code error: syntax error");
204 } else if (*L == ')') {
205 /* zp indirect or zp indirect, y */
209 if (toupper (*L) != 'Y') {
210 Error ("ASM code error: `Y' expected");
215 Error ("ASM code error: syntax error");
219 } else if (*L == '\0') {
222 Error ("ASM code error: syntax error");
238 /* Absolute, maybe indexed */
239 L = ReadToken (L, ",", Arg, sizeof (Arg));
241 /* Assume absolute */
243 } else if (*L == ',') {
247 Error ("ASM code error: syntax error");
254 } else if (Reg == 'Y') {
257 Error ("ASM code error: syntax error");
261 Error ("ASM code error: syntax error");
270 /* If the instruction is a branch, check for the label and generate it
271 * if it does not exist. Ignore anything but local labels here.
274 if ((OPC->Info & CI_MASK_BRA) == CI_BRA && Arg[0] == 'L') {
278 /* Addressing mode must be alsobute or something is really wrong */
279 CHECK (AM == AM_ABS);
281 /* Addressing mode is a branch/jump */
284 /* Generate the hash over the label, then search for the label */
285 Hash = HashStr (Arg) % CS_LABEL_HASH_SIZE;
286 Label = FindCodeLabel (S, Arg, Hash);
288 /* If we don't have the label, it's a forward ref - create it */
290 /* Generate a new label */
291 Label = NewCodeSegLabel (S, Arg, Hash);
295 /* We do now have the addressing mode in AM. Allocate a new CodeEntry
296 * structure and initialize it.
298 E = NewCodeEntry (OPC, AM, Arg, Label);
300 /* Return the new code entry */
306 /*****************************************************************************/
308 /*****************************************************************************/
312 CodeSeg* NewCodeSeg (const char* SegName, const char* FuncName)
313 /* Create a new code segment, initialize and return it */
317 /* Allocate memory */
318 CodeSeg* S = xmalloc (sizeof (CodeSeg));
320 /* Initialize the fields */
322 S->SegName = xstrdup (SegName);
323 S->FuncName = xstrdup (FuncName);
324 InitCollection (&S->Entries);
325 InitCollection (&S->Labels);
326 for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
330 /* Return the new struct */
336 void FreeCodeSeg (CodeSeg* S)
337 /* Free a code segment including all code entries */
339 FAIL ("Not implemented");
344 void PushCodeSeg (CodeSeg* S)
345 /* Push the given code segment onto the stack */
353 CodeSeg* PopCodeSeg (void)
354 /* Remove the current code segment from the stack and return it */
356 /* Remember the current code segment */
359 /* Cannot pop on empty stack */
360 PRECONDITION (S != 0);
365 /* Return the popped code segment */
371 void AddCodeSegLine (CodeSeg* S, const char* Format, ...)
372 /* Add a line to the given code segment */
378 /* Format the line */
381 va_start (ap, Format);
382 xvsprintf (Buf, sizeof (Buf), Format, ap);
385 /* Skip whitespace */
388 /* Check which type of instruction we have */
389 E = 0; /* Assume no insn created */
393 /* Empty line, just ignore it */
397 /* Comment or hint, ignore it for now */
401 /* Control instruction */
402 ReadToken (L, " \t", Token, sizeof (Token));
403 Error ("ASM code error: Pseudo instruction `%s' not supported", Token);
407 E = ParseInsn (S, L);
411 /* If we have a code entry, transfer the labels and insert it */
414 /* Transfer the labels if we have any */
416 unsigned LabelCount = CollCount (&S->Labels);
417 for (I = 0; I < LabelCount; ++I) {
419 CodeLabel* L = CollAt (&S->Labels, I);
420 /* Mark it as defined */
422 /* Move it to the code entry */
423 CollAppend (&E->Labels, L);
424 /* Tell the label about it's owner */
428 /* Delete the transfered labels */
429 CollDeleteAll (&S->Labels);
431 /* Add the entry to the list of code entries in this segment */
432 CollAppend (&S->Entries, E);
439 void DelCodeSegLine (CodeSeg* S, unsigned Index)
440 /* Delete an entry from the code segment. This includes deleting any associated
441 * labels, removing references to labels and even removing the referenced labels
442 * if the reference count drops to zero.
445 /* Get the code entry for the given index */
446 CodeEntry* E = CollAt (&S->Entries, Index);
448 /* Remove any labels associated with this entry */
450 while ((Count = CollCount (&E->Labels)) > 0) {
451 DelCodeLabel (S, CollAt (&E->Labels, Count-1));
454 /* If this insn references a label, remove the reference. And, if the
455 * the reference count for this label drops to zero, remove this label.
459 /* Remove the reference */
460 if (RemoveLabelRef (E->JumpTo, E) == 0) {
461 /* No references remaining, remove the label */
462 DelCodeLabel (S, E->JumpTo);
465 /* Reset the label pointer to avoid problems later */
469 /* Delete the pointer to the insn */
470 CollDelete (&S->Entries, Index);
472 /* Delete the instruction itself */
478 void AddCodeLabel (CodeSeg* S, const char* Name)
479 /* Add a code label for the next instruction to follow */
481 /* Calculate the hash from the name */
482 unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
484 /* Try to find the code label if it does already exist */
485 CodeLabel* L = FindCodeLabel (S, Name, Hash);
487 /* Did we find it? */
489 /* We found it - be sure it does not already have an owner */
490 CHECK (L->Owner == 0);
492 /* Not found - create a new one */
493 L = NewCodeSegLabel (S, Name, Hash);
496 /* We do now have a valid label. Remember it for later */
497 CollAppend (&S->Labels, L);
502 void DelCodeLabel (CodeSeg* S, CodeLabel* L)
503 /* Remove references from this label and delete it. */
507 /* Get the first entry in the hash chain */
508 CodeLabel* List = S->LabelHash[L->Hash];
510 /* First, remove the label from the hash chain */
512 /* First entry in hash chain */
513 S->LabelHash[L->Hash] = L->Next;
515 /* Must search through the chain */
516 while (List->Next != L) {
517 /* If we've reached the end of the chain, something is *really* wrong */
518 CHECK (List->Next != 0);
522 /* The next entry is the one, we have been searching for */
523 List->Next = L->Next;
526 /* Remove references from insns jumping to this label */
527 Count = CollCount (&L->JumpFrom);
528 for (I = 0; I < Count; ++I) {
529 /* Get the insn referencing this label */
530 CodeEntry* E = CollAt (&L->JumpFrom, I);
531 /* Remove the reference */
534 CollDeleteAll (&L->JumpFrom);
536 /* Remove the reference to the owning instruction */
537 CollDeleteItem (&L->Owner->Labels, L);
539 /* All references removed, delete the label itself */
545 void AddCodeSegHint (CodeSeg* S, unsigned Hint)
546 /* Add a hint for the preceeding instruction */
550 /* Get the number of entries in this segment */
551 unsigned EntryCount = CollCount (&S->Entries);
553 /* Must have at least one entry */
554 CHECK (EntryCount > 0);
556 /* Get the last entry */
557 E = CollAt (&S->Entries, EntryCount-1);
565 void DelCodeSegAfter (CodeSeg* S, unsigned Last)
566 /* Delete all entries including the given one */
568 /* Get the number of entries in this segment */
569 unsigned Count = CollCount (&S->Entries);
571 /* Remove all entries after the given one */
572 while (Last < Count) {
574 /* Get the next entry */
575 CodeEntry* E = CollAt (&S->Entries, Count-1);
577 /* We have to transfer all labels to the code segment label pool */
578 unsigned LabelCount = CollCount (&E->Labels);
579 while (LabelCount--) {
580 CodeLabel* L = CollAt (&E->Labels, LabelCount);
582 CollAppend (&S->Labels, L);
584 CollDeleteAll (&E->Labels);
586 /* Remove the code entry */
587 FreeCodeEntry (CollAt (&S->Entries, Count-1));
588 CollDelete (&S->Entries, Count-1);
595 void OutputCodeSeg (FILE* F, const CodeSeg* S)
596 /* Output the code segment data to a file */
600 /* Get the number of entries in this segment */
601 unsigned Count = CollCount (&S->Entries);
603 fprintf (F, "; Labels: ");
604 for (I = 0; I < CS_LABEL_HASH_SIZE; ++I) {
605 const CodeLabel* L = S->LabelHash[I];
607 fprintf (F, "%s ", L->Name);
613 /* Output the segment directive */
614 fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
616 /* If this is a segment for a function, enter a function */
617 if (S->FuncName[0] != '\0') {
618 fprintf (F, ".proc\t_%s\n\n", S->FuncName);
621 /* Output all entries */
622 for (I = 0; I < Count; ++I) {
623 OutputCodeEntry (F, CollConstAt (&S->Entries, I));
626 /* If this is a segment for a function, leave the function */
627 if (S->FuncName[0] != '\0') {
628 fprintf (F, "\n.endproc\n\n");
634 CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
635 /* Find the label with the given name. Return the label or NULL if not found */
637 /* Get the first hash chain entry */
638 CodeLabel* L = S->LabelHash[Hash];
640 /* Search the list */
642 if (strcmp (Name, L->Name) == 0) {
653 void MergeCodeLabels (CodeSeg* S)
654 /* Merge code labels. That means: For each instruction, remove all labels but
655 * one and adjust the code entries accordingly.
660 /* Walk over all code entries */
661 unsigned EntryCount = CollCount (&S->Entries);
662 for (I = 0; I < EntryCount; ++I) {
667 /* Get a pointer to the next entry */
668 CodeEntry* E = CollAt (&S->Entries, I);
670 /* If this entry has zero labels, continue with the next one */
671 unsigned LabelCount = CollCount (&E->Labels);
672 if (LabelCount == 0) {
676 /* We have at least one label. Use the first one as reference label. */
677 RefLab = CollAt (&E->Labels, 0);
679 /* Walk through the remaining labels and change references to these
680 * labels to a reference to the one and only label. Delete the labels
681 * that are no longer used. To increase performance, walk backwards
684 for (J = LabelCount-1; J >= 1; --J) {
688 /* Get the next label */
689 CodeLabel* L = CollAt (&E->Labels, J);
691 /* Walk through all instructions referencing this label */
692 unsigned RefCount = CollCount (&L->JumpFrom);
693 for (K = 0; K < RefCount; ++K) {
695 /* Get the next instruction that references this label */
696 CodeEntry* E = CollAt (&L->JumpFrom, K);
698 /* Change the reference */
699 CHECK (E->JumpTo == L);
700 AddLabelRef (RefLab, E);
704 /* There are no more instructions jumping to this label now */
705 CollDeleteAll (&L->JumpFrom);
707 /* Remove the label completely. */
711 /* The reference label is the only remaining label. Check if there
712 * are any references to this label, and delete it if this is not
715 if (CollCount (&RefLab->JumpFrom) == 0) {
716 /* Delete the label */
717 DelCodeLabel (S, RefLab);
724 unsigned GetCodeSegEntries (const CodeSeg* S)
725 /* Return the number of entries for the given code segment */
727 return CollCount (&S->Entries);