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 static CodeLabel* NewCodeSegLabel (CodeSeg* S, const char* Name, unsigned Hash)
62 /* Create a new label and insert it into the label hash table */
64 /* Not found - create a new one */
65 CodeLabel* L = NewCodeLabel (Name, Hash);
67 /* Enter the label into the hash table */
68 L->Next = S->LabelHash[L->Hash];
69 S->LabelHash[L->Hash] = L;
71 /* Return the new label */
77 static const char* SkipSpace (const char* S)
78 /* Skip white space and return an updated pointer */
80 while (IsSpace (*S)) {
88 static const char* ReadToken (const char* L, const char* Term,
89 char* Buf, unsigned BufSize)
90 /* Read the next token into Buf, return the updated line pointer. The
91 * token is terminated by one of the characters given in term.
94 /* Read/copy the token */
96 unsigned ParenCount = 0;
97 while (*L && (ParenCount > 0 || strchr (Term, *L) == 0)) {
103 } else if (*L == '(') {
109 /* Terminate the buffer contents */
112 /* Return the updated line pointer */
118 static CodeEntry* ParseInsn (CodeSeg* S, const char* L)
119 /* Parse an instruction nnd generate a code entry from it. If the line contains
120 * errors, output an error message and return NULL.
121 * For simplicity, we don't accept the broad range of input a "real" assembler
122 * does. The instruction and the argument are expected to be separated by
123 * white space, for example.
128 am_t AM = 0; /* Initialize to keep gcc silent */
135 L = ReadToken (L, " \t", Mnemo, sizeof (Mnemo));
137 /* Try to find the opcode description for the mnemonic */
138 OPC = FindOpcode (Mnemo);
140 /* If we didn't find the opcode, print an error and bail out */
142 Error ("ASM code error: %s is not a valid mnemonic", Mnemo);
146 /* Skip separator white space */
149 /* Get the addressing mode */
160 StrCopy (Expr, sizeof (Expr), L+1);
166 L = ReadToken (L+1, ",)", Expr, sizeof (Expr));
168 /* Check for errors */
170 Error ("ASM code error: syntax error");
174 /* Check the different indirect modes */
176 /* Expect zp x indirect */
178 if (toupper (*L) != 'X') {
179 Error ("ASM code error: `X' expected");
184 Error ("ASM code error: `)' expected");
189 Error ("ASM code error: syntax error");
193 } else if (*L == ')') {
194 /* zp indirect or zp indirect, y */
198 if (toupper (*L) != 'Y') {
199 Error ("ASM code error: `Y' expected");
204 Error ("ASM code error: syntax error");
208 } else if (*L == '\0') {
211 Error ("ASM code error: syntax error");
227 /* Absolute, maybe indexed */
228 L = ReadToken (L, ",", Expr, sizeof (Expr));
230 /* Assume absolute */
232 } else if (*L == ',') {
236 Error ("ASM code error: syntax error");
243 } else if (Reg == 'Y') {
246 Error ("ASM code error: syntax error");
250 Error ("ASM code error: syntax error");
259 /* If the instruction is a branch, check for the label and generate it
260 * if it does not exist.
263 if ((OPC->Info & CI_MASK_BRA) == CI_BRA) {
267 /* ### Check for local labels here */
268 CHECK (AM == AM_ABS);
270 Hash = HashStr (Expr) % CS_LABEL_HASH_SIZE;
271 Label = FindCodeLabel (S, Expr, Hash);
273 /* Generate a new label */
274 Label = NewCodeSegLabel (S, Expr, Hash);
278 /* We do now have the addressing mode in AM. Allocate a new CodeEntry
279 * structure and initialize it.
281 E = NewCodeEntry (OPC, AM, Label);
282 if (Expr[0] != '\0') {
283 /* We have an additional expression */
284 E->Arg.Expr = xstrdup (Expr);
287 /* Return the new code entry */
293 CodeSeg* NewCodeSeg (const char* Name)
294 /* Create a new code segment, initialize and return it */
298 /* Allocate memory */
299 CodeSeg* S = xmalloc (sizeof (CodeSeg));
301 /* Initialize the fields */
302 S->Name = xstrdup (Name);
303 InitCollection (&S->Entries);
304 InitCollection (&S->Labels);
305 for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
309 /* Return the new struct */
315 void FreeCodeSeg (CodeSeg* S)
316 /* Free a code segment including all code entries */
323 /* Free the entries */
324 Count = CollCount (&S->Entries);
325 for (I = 0; I < Count; ++I) {
326 FreeCodeEntry (CollAt (&S->Entries, I));
329 /* Free the collections */
330 DoneCollection (&S->Entries);
331 DoneCollection (&S->Labels);
333 /* Free all labels */
334 for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
335 CodeLabel* L = S->LabelHash[I];
343 /* Free the struct */
349 void AddCodeSegLine (CodeSeg* S, const char* Format, ...)
350 /* Add a line to the given code segment */
356 /* Format the line */
359 va_start (ap, Format);
360 xvsprintf (Buf, sizeof (Buf), Format, ap);
363 /* Skip whitespace */
366 /* Check which type of instruction we have */
367 E = 0; /* Assume no insn created */
371 /* Empty line, just ignore it */
375 /* Comment or hint, ignore it for now */
379 /* Control instruction */
380 ReadToken (L, " \t", Token, sizeof (Token));
381 Error ("ASM code error: Pseudo instruction `%s' not supported", Token);
385 E = ParseInsn (S, L);
389 /* If we have a code entry, transfer the labels and insert it */
392 /* Transfer the labels if we have any */
393 unsigned LabelCount = CollCount (&S->Labels);
395 for (I = 0; I < LabelCount; ++I) {
396 CollAppend (&E->Labels, CollAt (&S->Labels, I));
398 CollDeleteAll (&S->Labels);
400 /* Add the entry to the list of code entries in this segment */
401 CollAppend (&S->Entries, E);
408 void AddCodeSegLabel (CodeSeg* S, const char* Name)
409 /* Add a label for the next instruction to follow */
411 /* Calculate the hash from the name */
412 unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
414 /* Try to find the code label if it does already exist */
415 CodeLabel* L = FindCodeLabel (S, Name, Hash);
417 /* Did we find it? */
419 /* We found it - be sure it does not already have an owner */
420 CHECK (L->Owner == 0);
422 /* Not found - create a new one */
423 L = NewCodeSegLabel (S, Name, Hash);
426 /* We do now have a valid label. Remember it for later */
427 CollAppend (&S->Labels, L);
432 void AddCodeSegHint (CodeSeg* S, unsigned Hint)
433 /* Add a hint for the preceeding instruction */
437 /* Get the number of entries in this segment */
438 unsigned EntryCount = CollCount (&S->Entries);
440 /* Must have at least one entry */
441 CHECK (EntryCount > 0);
443 /* Get the last entry */
444 E = CollAt (&S->Entries, EntryCount-1);
452 void DelCodeSegAfter (CodeSeg* S, unsigned Last)
453 /* Delete all entries after the given one */
457 /* Get the number of entries in this segment */
458 unsigned Count = CollCount (&S->Entries);
460 /* ### We need some more cleanup here wrt labels */
462 /* Remove all entries after the given one */
463 for (I = Count-1; I > Last; --I) {
464 FreeCodeEntry (CollAt (&S->Entries, I));
465 CollDelete (&S->Entries, I);
468 /* Delete all waiting labels */
469 CollDeleteAll (&S->Labels);
474 void OutputCodeSeg (FILE* F, const CodeSeg* S)
475 /* Output the code segment data to a file */
479 /* Get the number of entries in this segment */
480 unsigned Count = CollCount (&S->Entries);
482 /* Output the segment directive */
483 fprintf (F, ".segment\t\"%s\"\n", S->Name);
485 /* Output all entries */
486 for (I = 0; I < Count; ++I) {
487 OutputCodeEntry (F, CollConstAt (&S->Entries, I));
493 CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
494 /* Find the label with the given name. Return the label or NULL if not found */
496 /* Get the first hash chain entry */
497 CodeLabel* L = S->LabelHash[Hash];
499 /* Search the list */
501 if (strcmp (Name, L->Name) == 0) {
512 void MergeCodeLabels (CodeSeg* S)
513 /* Merge code labels. That means: For each instruction, remove all labels but
514 * one and adjust the code entries accordingly.
519 /* Walk over all code entries */
520 unsigned EntryCount = CollCount (&S->Entries);
521 for (I = 0; I < EntryCount; ++I) {
526 /* Get a pointer to the next entry */
527 CodeEntry* E = CollAt (&S->Entries, I);
529 /* If this entry has zero labels, continue with the next one */
530 unsigned LabelCount = CollCount (&E->Labels);
531 if (LabelCount == 0) {
535 /* We have at least one label. Use the first one as reference label.
536 * We don't have a notification for global labels for now, and using
537 * the first one will also keep the global function labels, since these
538 * are inserted at position 0.
540 RefLab = CollAt (&E->Labels, 0);
542 /* Walk through the remaining labels and change references to these
543 * labels to a reference to the one and only label. Delete the labels
544 * that are no longer used. To increase performance, walk backwards
547 for (J = LabelCount-1; J >= 1; --J) {
551 /* Get the next label */
552 CodeLabel* L = CollAt (&E->Labels, J);
554 /* Walk through all instructions referencing this label */
555 unsigned RefCount = CollCount (&L->JumpFrom);
556 for (K = 0; K < RefCount; ++K) {
558 /* Get the next instrcuction that references this label */
559 CodeEntry* E = CollAt (&L->JumpFrom, K);
561 /* Change the reference */
562 CHECK (E->JumpTo == L);
564 CollAppend (&RefLab->JumpFrom, E);
568 /* Delete the label */
571 /* Remove it from the list */
572 CollDelete (&E->Labels, J);
576 /* The reference label is the only remaining label. Check if there
577 * are any references to this label, and delete it if this is not
580 if (CollCount (&RefLab->JumpFrom) == 0) {
581 /* Delete the label */
582 FreeCodeLabel (RefLab);
583 /* Remove it from the list */
584 CollDelete (&E->Labels, 0);
591 unsigned GetCodeSegEntries (const CodeSeg* S)
592 /* Return the number of entries for the given code segment */
594 return CollCount (&S->Entries);