2 Provides output functions that modify the output string
3 based on the input tokens and the assembler token mapping
6 Note that the functions below only handle digit format modifiers.
7 eg %02X is ok, but %lu and %.4u will fail.
12 /* A 'token' is like !blah or %24f and is under the programmers
14 #define MAX_TOKEN_LEN 64
19 FileBaseName (char *fileFullName)
21 char *p = fileFullName;
25 if ((*fileFullName == '/') || (*fileFullName == '\\') || (*fileFullName == ':'))
36 _findMapping (const char *szKey)
38 return shash_find (_h, szKey);
43 _iprintf (char *pInto, const char *sz, va_list * pap)
45 char format[MAX_TOKEN_LEN];
55 /* See if it's a special emitter */
59 /* Name of the code segment */
61 strcpy (pInto, CODE_NAME);
62 pInto = pStart + strlen (pStart);
66 strcpy (pInto, srcFileName);
67 pInto = pStart + strlen (pStart);
71 sprintf (pInto, "%u", ++count);
72 pInto = pStart + strlen (pStart);
77 /* Scan out the arg and pass it on to sprintf */
84 vsprintf (pInto, format, *pap);
85 /* PENDING: Assume that the arg length was an int */
86 (void) va_arg (*pap, int);
89 pInto = pStart + strlen (pStart);
100 tvsprintf (char *buffer, const char *sz, va_list ap)
102 char *pInto = buffer;
104 char token[MAX_TOKEN_LEN];
112 /* Start of a token. Search until the first
113 [non alplha, *] and call it a token. */
117 while (isalpha (*sz) || *sz == '*')
122 /* Now find the token in the token list */
123 if ((t = _findMapping (token)))
125 printf ("tvsprintf: found token \"%s\" to \"%s\"\n", token, t);
126 _iprintf (pInto, t, &ap);
127 pInto = buffer + strlen (buffer);
131 fprintf (stderr, "Cant find token \"%s\"\n", token);
139 while (!isalpha (*sz))
145 vsprintf (pInto, token, ap);
146 pInto = buffer + strlen (buffer);
147 (void) va_arg (ap, int);
157 // Append a string onto another, and update the pointer to the end of
160 _appendAt (char *at, char *onto, const char *sz)
162 wassert (at && onto && sz);
164 return at + strlen (sz);
168 tvsprintf (char *buffer, const char *format, va_list ap)
170 // Under Linux PPC va_list is a structure instead of a primitive type,
171 // and doesnt like being passed around. This version turns everything
172 // into one function.
176 // %[CIF] - special formats with no argument (ie list isnt touched)
177 // All of the system formats
179 // This is acheived by expanding the tokens and zero arg formats into
180 // one big format string, which is passed to the native printf.
182 char newformat[MAX_INLINEASM];
183 char *pInto = newformat;
185 char token[MAX_TOKEN_LEN];
186 const char *sz = format;
188 // NULL terminate it to let strlen work.
195 /* Start of a token. Search until the first
196 [non alpha, *] and call it a token. */
200 while (isalpha (*sz) || *sz == '*')
205 /* Now find the token in the token list */
206 if ((t = _findMapping (token)))
208 pInto = _appendAt (pInto, newformat, t);
212 fprintf (stderr, "Cant find token \"%s\"\n", token);
218 // See if its one that we handle.
223 // Code segment name.
224 pInto = _appendAt (pInto, newformat, CODE_NAME);
228 pInto = _appendAt (pInto, newformat, srcFileName);
234 sprintf (id, "%u", ++count);
235 pInto = _appendAt (pInto, newformat, id);
239 // Not one of ours. Copy until the end.
241 while (!isalpha (*sz))
255 // Now do the actual printing
256 vsprintf (buffer, newformat, ap);
261 tfprintf (FILE * fp, const char *szFormat,...)
264 char buffer[MAX_INLINEASM];
266 va_start (ap, szFormat);
267 tvsprintf (buffer, szFormat, ap);
272 tsprintf (char *buffer, const char *szFormat,...)
275 va_start (ap, szFormat);
276 tvsprintf (buffer, szFormat, ap);
280 asm_addTree (const ASM_MAPPINGS * pMappings)
282 const ASM_MAPPING *pMap;
284 /* Traverse down first */
285 if (pMappings->pParent)
286 asm_addTree (pMappings->pParent);
287 pMap = pMappings->pMappings;
288 while (pMap->szKey && pMap->szValue) {
289 shash_add (&_h, pMap->szKey, pMap->szValue);
294 static const ASM_MAPPING _asxxxx_mapping[] =
296 {"labeldef", "%s::"},
297 {"slabeldef", "%s:"},
298 {"tlabeldef", "%05d$:"},
303 {"area", ".area %s"},
304 {"areacode", ".area %s"},
305 {"areadata", ".area %s"},
306 {"areahome", ".area %s"},
307 {"ascii", ".ascii \"%s\""},
313 {"constbyte", "0x%02X"},
314 {"constword", "0x%04X"},
315 {"immedword", "#0x%04X"},
316 {"immedbyte", "#0x%02X"},
317 {"hashedstr", "#%s"},
318 {"lsbimmeds", "#<%s"},
319 {"msbimmeds", "#>%s"},
320 {"module", ".module %s"},
321 {"global", ".globl %s"},
324 "; ---------------------------------\n"
326 "; ---------------------------------"
328 {"functionlabeldef", "%s:"},
329 {"bankimmeds", "0 ; PENDING: bank support"},
333 static const ASM_MAPPING _gas_mapping[] =
335 {"labeldef", "%s::"},
336 {"slabeldef", "%s:"},
337 {"tlabeldef", "%05d$:"},
342 {"area", ".section %s"},
343 {"areacode", ".section %s"},
344 {"areadata", ".section %s"},
345 {"areahome", ".section %s"},
346 {"ascii", ".ascii \"%s\""},
352 {"constbyte", "0x%02X"},
353 {"constword", "0x%04X"},
354 {"immedword", "#0x%04X"},
355 {"immedbyte", "#0x%02X"},
356 {"hashedstr", "#%s"},
357 {"lsbimmeds", "#<%s"},
358 {"msbimmeds", "#>%s"},
359 {"module", ".file \"%s.c\""},
360 {"global", ".globl %s"},
363 "; ---------------------------------\n"
365 "; ---------------------------------"
367 {"functionlabeldef", "%s:"},
368 {"bankimmeds", "0 ; PENDING: bank support"},
372 const ASM_MAPPINGS asm_asxxxx_mapping =
378 const ASM_MAPPINGS asm_gas_mapping =