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 /*****************************************************************************/
54 /*****************************************************************************/
56 /*****************************************************************************/
60 static const char* SkipSpace (const char* S)
61 /* Skip white space and return an updated pointer */
63 while (IsSpace (*S)) {
71 static const char* ReadToken (const char* L, const char* Term,
72 char* Buf, unsigned BufSize)
73 /* Read the next token into Buf, return the updated line pointer. The
74 * token is terminated by one of the characters given in term.
77 /* Read/copy the token */
79 unsigned ParenCount = 0;
80 while (*L && (ParenCount > 0 || strchr (Term, *L) == 0)) {
86 } else if (*L == '(') {
92 /* Terminate the buffer contents */
95 /* Return the updated line pointer */
101 static CodeEntry* ParseInsn (const char* L)
102 /* Parse an instruction nnd generate a code entry from it. If the line contains
103 * errors, output an error message and return NULL.
104 * For simplicity, we don't accept the broad range of input a "real" assembler
105 * does. The instruction and the argument are expected to be separated by
106 * white space, for example.
111 am_t AM = 0; /* Initialize to keep gcc silent */
117 L = ReadToken (L, " \t", Mnemo, sizeof (Mnemo));
119 /* Try to find the opcode description for the mnemonic */
120 OPC = FindOpcode (Mnemo);
122 /* If we didn't find the opcode, print an error and bail out */
124 Error ("ASM code error: %s is not a valid mnemonic", Mnemo);
128 /* Skip separator white space */
131 /* Get the addressing mode */
137 StrCopy (Expr, sizeof (Expr), L+1);
143 L = ReadToken (L+1, ",)", Expr, sizeof (Expr));
145 /* Check for errors */
147 Error ("ASM code error: syntax error");
151 /* Check the different indirect modes */
153 /* Expect zp x indirect */
155 if (toupper (*L) != 'X') {
156 Error ("ASM code error: `X' expected");
161 Error ("ASM code error: `)' expected");
166 Error ("ASM code error: syntax error");
170 } else if (*L == ')') {
171 /* zp indirect or zp indirect, y */
175 if (toupper (*L) != 'Y') {
176 Error ("ASM code error: `Y' expected");
181 Error ("ASM code error: syntax error");
185 } else if (*L == '\0') {
188 Error ("ASM code error: syntax error");
204 /* Absolute, maybe indexed */
205 L = ReadToken (L, ",", Expr, sizeof (Expr));
207 /* Assume absolute */
209 } else if (*L == ',') {
213 Error ("ASM code error: syntax error");
220 } else if (Reg == 'Y') {
223 Error ("ASM code error: syntax error");
227 Error ("ASM code error: syntax error");
236 /* We do now have the addressing mode in AM. Allocate a new CodeEntry
237 * structure and initialize it.
239 E = NewCodeEntry (OPC, AM);
240 if (Expr[0] != '\0') {
241 /* We have an additional expression */
242 E->Arg.Expr = xstrdup (Expr);
245 /* Return the new code entry */
251 CodeSeg* NewCodeSeg (const char* Name)
252 /* Create a new code segment, initialize and return it */
256 /* Allocate memory */
257 CodeSeg* S = xmalloc (sizeof (CodeSeg));
259 /* Initialize the fields */
260 S->Name = xstrdup (Name);
261 InitCollection (&S->Entries);
262 InitCollection (&S->Labels);
263 for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
267 /* Return the new struct */
273 void FreeCodeSeg (CodeSeg* S)
274 /* Free a code segment including all code entries */
281 /* Free the entries */
282 Count = CollCount (&S->Entries);
283 for (I = 0; I < Count; ++I) {
284 FreeCodeEntry (CollAt (&S->Entries, I));
287 /* Free the collections */
288 DoneCollection (&S->Entries);
289 DoneCollection (&S->Labels);
291 /* Free all labels */
292 for (I = 0; I < sizeof(S->LabelHash) / sizeof(S->LabelHash[0]); ++I) {
293 CodeLabel* L = S->LabelHash[I];
301 /* Free the struct */
307 void AddCodeSegLine (CodeSeg* S, const char* Format, ...)
308 /* Add a line to the given code segment */
313 /* Format the line */
316 va_start (ap, Format);
317 xvsprintf (Buf, sizeof (Buf), Format, ap);
320 /* Skip whitespace */
323 /* Check which type of instruction we have */
324 E = 0; /* Assume no insn created */
328 /* Empty line, just ignore it */
332 /* Comment or hint, ignore it for now */
336 /* Control instruction */
337 Error ("ASM code error: Pseudo instructions not supported");
345 /* If we have a code entry, transfer the labels and insert it */
348 /* Transfer the labels if we have any */
349 unsigned LabelCount = CollCount (&S->Labels);
351 for (I = 0; I < LabelCount; ++I) {
352 CollAppend (&E->Labels, CollAt (&S->Labels, I));
354 CollDeleteAll (&S->Labels);
356 /* Add the entry to the list of code entries in this segment */
357 CollAppend (&S->Entries, E);
364 void AddCodeSegLabel (CodeSeg* S, const char* Name)
365 /* Add a label for the next instruction to follow */
367 /* Calculate the hash from the name */
368 unsigned Hash = HashStr (Name) % CS_LABEL_HASH_SIZE;
370 /* Try to find the code label if it does already exist */
371 CodeLabel* L = FindCodeLabel (S, Name, Hash);
373 /* Did we find it? */
375 /* We found it - be sure it does not already have an owner */
376 CHECK (L->Owner == 0);
378 /* Not found - create a new one */
379 L = NewCodeLabel (Name, Hash);
381 /* Enter the label into the hash table */
382 L->Next = S->LabelHash[L->Hash];
383 S->LabelHash[L->Hash] = L;
386 /* We do now have a valid label. Remember it for later */
387 CollAppend (&S->Labels, L);
392 void OutputCodeSeg (FILE* F, const CodeSeg* S)
393 /* Output the code segment data to a file */
397 /* Get the number of entries in this segment */
398 unsigned Count = CollCount (&S->Entries);
400 /* Output all entries */
401 for (I = 0; I < Count; ++I) {
402 OutputCodeEntry (F, CollConstAt (&S->Entries, I));
408 CodeLabel* FindCodeLabel (CodeSeg* S, const char* Name, unsigned Hash)
409 /* Find the label with the given name. Return the label or NULL if not found */
411 /* Get the first hash chain entry */
412 CodeLabel* L = S->LabelHash[Hash];
414 /* Search the list */
416 if (strcmp (Name, L->Name) == 0) {
427 void MergeCodeLabels (CodeSeg* S)
428 /* Merge code labels. That means: For each instruction, remove all labels but
429 * one and adjust the code entries accordingly.
434 /* Walk over all code entries */
435 unsigned EntryCount = CollCount (&S->Entries);
436 for (I = 0; I < EntryCount; ++I) {
441 /* Get a pointer to the next entry */
442 CodeEntry* E = CollAt (&S->Entries, I);
444 /* If this entry has one or zero labels, continue with the next one */
445 unsigned LabelCount = CollCount (&E->Labels);
446 if (LabelCount <= 1) {
450 /* We have more than one label. Use the first one as reference label */
451 RefLab = CollAt (&E->Labels, 0);
453 /* Walk through the remaining labels and change references to these
454 * labels to a reference to the one and only label. Delete the labels
455 * that are no longer used. To increase performance, walk backwards
458 for (J = LabelCount-1; J >= 1; --J) {
462 /* Get the next label */
463 CodeLabel* L = CollAt (&E->Labels, J);
465 /* Walk through all instructions referencing this label */
466 unsigned RefCount = CollCount (&L->JumpFrom);
467 for (K = 0; K < RefCount; ++K) {
469 /* Get the next instrcuction that references this label */
470 CodeEntry* E = CollAt (&L->JumpFrom, K);
472 /* Change the reference */
473 CHECK (E->JumpTo == L);
475 CollAppend (&RefLab->JumpFrom, E);
479 /* Delete the label */
482 /* Remove it from the list */
483 CollDelete (&E->Labels, J);