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 /*****************************************************************************/
57 /*****************************************************************************/
59 /*****************************************************************************/
63 /* Pointer to current code segment */
68 /*****************************************************************************/
69 /* Functions for parsing instructions */
70 /*****************************************************************************/
74 static CodeLabel* NewCodeSegLabel (CodeSeg* S, const char* Name, unsigned Hash)
75 /* Create a new label and insert it into the label hash table */
77 /* Not found - create a new one */
78 CodeLabel* L = NewCodeLabel (Name, Hash);
80 /* Enter the label into the hash table */
81 L->Next = S->LabelHash[L->Hash];
82 S->LabelHash[L->Hash] = L;
84 /* Return the new label */
90 static const char* SkipSpace (const char* S)
91 /* Skip white space and return an updated pointer */
93 while (IsSpace (*S)) {
101 static const char* ReadToken (const char* L, const char* Term,
102 char* Buf, unsigned BufSize)
103 /* Read the next token into Buf, return the updated line pointer. The
104 * token is terminated by one of the characters given in term.
107 /* Read/copy the token */
109 unsigned ParenCount = 0;
110 while (*L && (ParenCount > 0 || strchr (Term, *L) == 0)) {
116 } else if (*L == '(') {
122 /* Terminate the buffer contents */
125 /* Return the updated line pointer */
131 static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
132 /* Parse an instruction nnd generate a code entry from it. If the line contains
133 * errors, output an error message and return NULL.
134 * For simplicity, we don't accept the broad range of input a "real" assembler
135 * does. The instruction and the argument are expected to be separated by
136 * white space, for example.
141 am_t AM = 0; /* Initialize to keep gcc silent */
148 L = ReadToken (L, " \t", Mnemo, sizeof (Mnemo));
150 /* Try to find the opcode description for the mnemonic */
151 OPC = FindOpcode (Mnemo);
153 /* If we didn't find the opcode, print an error and bail out */
155 Error ("ASM code error: %s is not a valid mnemonic", Mnemo);
159 /* Skip separator white space */
162 /* Get the addressing mode */
173 StrCopy (Expr, sizeof (Expr), L+1);
179 L = ReadToken (L+1, ",)", Expr, sizeof (Expr));
181 /* Check for errors */
183 Error ("ASM code error: syntax error");
187 /* Check the different indirect modes */
189 /* Expect zp x indirect */
191 if (toupper (*L) != 'X') {
192 Error ("ASM code error: `X' expected");
197 Error ("ASM code error: `)' expected");
202 Error ("ASM code error: syntax error");
206 } else if (*L == ')') {
207 /* zp indirect or zp indirect, y */
211 if (toupper (*L) != 'Y') {
212 Error ("ASM code error: `Y' expected");
217 Error ("ASM code error: syntax error");
221 } else if (*L == '\0') {
224 Error ("ASM code error: syntax error");
240 /* Absolute, maybe indexed */
241 L = ReadToken (L, ",", Expr, sizeof (Expr));
243 /* Assume absolute */
245 } else if (*L == ',') {
249 Error ("ASM code error: syntax error");
256 } else if (Reg == 'Y') {
259 Error ("ASM code error: syntax error");
263 Error ("ASM code error: syntax error");
272 /* If the instruction is a branch, check for the label and generate it
273 * if it does not exist.
276 if ((OPC->Info & CI_MASK_BRA) == CI_BRA) {
280 /* ### Check for local labels here */
281 CHECK (AM == AM_ABS);
283 Hash = HashStr (Expr) % CS_LABEL_HASH_SIZE;
284 Label = FindCodeLabel (S, Expr, Hash);
286 /* Generate a new label */
287 Label = NewCodeSegLabel (S, Expr, Hash);
291 /* We do now have the addressing mode in AM. Allocate a new CodeEntry
292 * structure and initialize it.
294 E = NewCodeEntry (OPC, AM, Label);
295 if (Expr[0] != '\0') {
296 /* We have an additional expression */
297 E->Arg.Expr = xstrdup (Expr);
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 */
345 /* Free the entries */
346 Count = CollCount (&S->Entries);
347 for (I = 0; I < Count; ++I) {
348 FreeCodeEntry (CollAt (&S->Entries, I));
351 /* Free the collections */
352 DoneCollection (&S->Entries);
353 DoneCollection (&S->Labels);
355 /* Free all labels */
356 for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
357 CodeLabel* L = S->LabelHash[I];
365 /* Free the struct */
371 void PushCodeSeg (CodeSeg* S)
372 /* Push the given code segment onto the stack */
380 CodeSeg* PopCodeSeg (void)
381 /* Remove the current code segment from the stack and return it */
383 /* Remember the current code segment */
386 /* Cannot pop on empty stack */
387 PRECONDITION (S != 0);
392 /* Return the popped code segment */
398 void AddCodeSegLine (CodeSeg* S, const char* Format, ...)
399 /* Add a line to the given code segment */
405 /* Format the line */
408 va_start (ap, Format);
409 xvsprintf (Buf, sizeof (Buf), Format, ap);
412 /* Skip whitespace */
415 /* Check which type of instruction we have */
416 E = 0; /* Assume no insn created */
420 /* Empty line, just ignore it */
424 /* Comment or hint, ignore it for now */
428 /* Control instruction */
429 ReadToken (L, " \t", Token, sizeof (Token));
430 Error ("ASM code error: Pseudo instruction `%s' not supported", Token);
434 E = ParseInsn (S, L);
438 /* If we have a code entry, transfer the labels and insert it */
441 /* Transfer the labels if we have any */
443 unsigned LabelCount = CollCount (&S->Labels);
444 for (I = 0; I < LabelCount; ++I) {
446 CodeLabel* L = CollAt (&S->Labels, I);
447 /* Mark it as defined */
449 /* Move it to the code entry */
450 CollAppend (&E->Labels, L);
453 /* Delete the transfered labels */
454 CollDeleteAll (&S->Labels);
456 /* Add the entry to the list of code entries in this segment */
457 CollAppend (&S->Entries, E);
464 CodeLabel* AddCodeLabel (CodeSeg* S, const char* Name)
465 /* Add a code label for the next instruction to follow */
467 /* Calculate the hash from the name */
468 unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
470 /* Try to find the code label if it does already exist */
471 CodeLabel* L = FindCodeLabel (S, Name, Hash);
473 /* Did we find it? */
475 /* We found it - be sure it does not already have an owner */
476 CHECK (L->Owner == 0);
478 /* Not found - create a new one */
479 L = NewCodeSegLabel (S, Name, Hash);
482 /* We do now have a valid label. Remember it for later */
483 CollAppend (&S->Labels, L);
485 /* Return the label */
491 void AddExtCodeLabel (CodeSeg* S, const char* Name)
492 /* Add an external code label for the next instruction to follow */
494 /* Add the code label */
495 CodeLabel* L = AddCodeLabel (S, Name);
497 /* Mark it as external label */
503 void AddLocCodeLabel (CodeSeg* S, const char* Name)
504 /* Add a local code label for the next instruction to follow */
506 /* Add the code label */
507 AddCodeLabel (S, Name);
512 void AddCodeSegHint (CodeSeg* S, unsigned Hint)
513 /* Add a hint for the preceeding instruction */
517 /* Get the number of entries in this segment */
518 unsigned EntryCount = CollCount (&S->Entries);
520 /* Must have at least one entry */
521 CHECK (EntryCount > 0);
523 /* Get the last entry */
524 E = CollAt (&S->Entries, EntryCount-1);
532 void DelCodeSegAfter (CodeSeg* S, unsigned Last)
533 /* Delete all entries including the given one */
535 /* Get the number of entries in this segment */
536 unsigned Count = CollCount (&S->Entries);
538 /* Remove all entries after the given one */
539 while (Last < Count) {
541 /* Get the next entry */
542 CodeEntry* E = CollAt (&S->Entries, Count-1);
544 /* We have to transfer all labels to the code segment label pool */
545 unsigned LabelCount = CollCount (&E->Labels);
546 while (LabelCount--) {
547 CodeLabel* L = CollAt (&E->Labels, LabelCount);
549 CollAppend (&S->Labels, L);
551 CollDeleteAll (&E->Labels);
553 /* Remove the code entry */
554 FreeCodeEntry (CollAt (&S->Entries, Count-1));
555 CollDelete (&S->Entries, Count-1);
562 void OutputCodeSeg (FILE* F, const CodeSeg* S)
563 /* Output the code segment data to a file */
567 /* Get the number of entries in this segment */
568 unsigned Count = CollCount (&S->Entries);
570 /* Output the segment directive */
571 fprintf (F, ".segment\t\"%s\"\n\n", S->SegName);
573 /* If this is a segment for a function, enter a function */
574 if (S->FuncName[0] != '\0') {
575 fprintf (F, ".proc\t_%s\n\n", S->FuncName);
578 /* Output all entries */
579 for (I = 0; I < Count; ++I) {
580 OutputCodeEntry (F, CollConstAt (&S->Entries, I));
583 /* If this is a segment for a function, leave the function */
584 if (S->FuncName[0] != '\0') {
585 fprintf (F, "\n.endproc\n\n");
591 CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
592 /* Find the label with the given name. Return the label or NULL if not found */
594 /* Get the first hash chain entry */
595 CodeLabel* L = S->LabelHash[Hash];
597 /* Search the list */
599 if (strcmp (Name, L->Name) == 0) {
610 void MergeCodeLabels (CodeSeg* S)
611 /* Merge code labels. That means: For each instruction, remove all labels but
612 * one and adjust the code entries accordingly.
617 /* Walk over all code entries */
618 unsigned EntryCount = CollCount (&S->Entries);
619 for (I = 0; I < EntryCount; ++I) {
624 /* Get a pointer to the next entry */
625 CodeEntry* E = CollAt (&S->Entries, I);
627 /* If this entry has zero labels, continue with the next one */
628 unsigned LabelCount = CollCount (&E->Labels);
629 if (LabelCount == 0) {
633 /* We have at least one label. Use the first one as reference label.
634 * We don't have a notification for global labels for now, and using
635 * the first one will also keep the global function labels, since these
636 * are inserted at position 0.
638 RefLab = CollAt (&E->Labels, 0);
640 /* Walk through the remaining labels and change references to these
641 * labels to a reference to the one and only label. Delete the labels
642 * that are no longer used. To increase performance, walk backwards
645 for (J = LabelCount-1; J >= 1; --J) {
649 /* Get the next label */
650 CodeLabel* L = CollAt (&E->Labels, J);
652 /* Walk through all instructions referencing this label */
653 unsigned RefCount = CollCount (&L->JumpFrom);
654 for (K = 0; K < RefCount; ++K) {
656 /* Get the next instrcuction that references this label */
657 CodeEntry* E = CollAt (&L->JumpFrom, K);
659 /* Change the reference */
660 CHECK (E->JumpTo == L);
662 CollAppend (&RefLab->JumpFrom, E);
666 /* If the label is not an external label, we may remove the
670 if ((L->Flags & LF_EXT) == 0) {
672 CollDelete (&E->Labels, J);
677 /* The reference label is the only remaining label. If it is not an
678 * external label, check if there are any references to this label,
679 * and delete it if this is not the case.
682 if ((RefLab->Flags & LF_EXT) == 0 && CollCount (&RefLab->JumpFrom) == 0) {
683 /* Delete the label */
684 FreeCodeLabel (RefLab);
685 /* Remove it from the list */
686 CollDelete (&E->Labels, 0);
694 unsigned GetCodeSegEntries (const CodeSeg* S)
695 /* Return the number of entries for the given code segment */
697 return CollCount (&S->Entries);