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;
29 if ((*fileFullName == '/') || (*fileFullName == '\\') || (*fileFullName == ':'))
40 _findMapping (const char *szKey)
42 return shash_find (_h, szKey);
47 _iprintf (char *pInto, const char *sz, va_list * pap)
49 char format[MAX_TOKEN_LEN];
59 /* See if it's a special emitter */
63 /* Name of the code segment */
65 strcpy (pInto, CODE_NAME);
66 pInto = pStart + strlen (pStart);
70 strcpy (pInto, srcFileName);
71 pInto = pStart + strlen (pStart);
75 sprintf (pInto, "%u", ++count);
76 pInto = pStart + strlen (pStart);
81 /* Scan out the arg and pass it on to sprintf */
88 vsprintf (pInto, format, *pap);
89 /* PENDING: Assume that the arg length was an int */
90 (void) va_arg (*pap, int);
93 pInto = pStart + strlen (pStart);
104 tvsprintf (char *buffer, const char *sz, va_list ap)
106 char *pInto = buffer;
108 char token[MAX_TOKEN_LEN];
116 /* Start of a token. Search until the first
117 [non alplha, *] and call it a token. */
121 while (isalpha (*sz) || *sz == '*')
126 /* Now find the token in the token list */
127 if ((t = _findMapping (token)))
129 printf ("tvsprintf: found token \"%s\" to \"%s\"\n", token, t);
130 _iprintf (pInto, t, &ap);
131 pInto = buffer + strlen (buffer);
135 fprintf (stderr, "Cant find token \"%s\"\n", token);
143 while (!isalpha (*sz))
149 vsprintf (pInto, token, ap);
150 pInto = buffer + strlen (buffer);
151 (void) va_arg (ap, int);
161 // Append a string onto another, and update the pointer to the end of
164 _appendAt (char *at, char *onto, const char *sz)
166 wassert (at && onto && sz);
168 return at + strlen (sz);
172 tvsprintf (char *buffer, const char *format, va_list ap)
174 // Under Linux PPC va_list is a structure instead of a primitive type,
175 // and doesnt like being passed around. This version turns everything
176 // into one function.
180 // %[CIF] - special formats with no argument (ie list isnt touched)
181 // All of the system formats
183 // This is acheived by expanding the tokens and zero arg formats into
184 // one big format string, which is passed to the native printf.
186 char newformat[INITIAL_INLINEASM];
187 char *pInto = newformat;
189 char token[MAX_TOKEN_LEN];
190 const char *sz = format;
192 // NULL terminate it to let strlen work.
199 /* Start of a token. Search until the first
200 [non alpha, *] and call it a token. */
204 while (isalpha (*sz) || *sz == '*')
209 /* Now find the token in the token list */
210 if ((t = _findMapping (token)))
212 pInto = _appendAt (pInto, newformat, t);
216 fprintf (stderr, "Cant find token \"%s\"\n", token);
222 // See if its one that we handle.
227 // Code segment name.
228 pInto = _appendAt (pInto, newformat, CODE_NAME);
232 pInto = _appendAt (pInto, newformat, srcFileName);
238 sprintf (id, "%u", ++count);
239 pInto = _appendAt (pInto, newformat, id);
243 // Not one of ours. Copy until the end.
245 while (!isalpha (*sz))
259 // Now do the actual printing
260 vsprintf (buffer, newformat, ap);
265 tfprintf (FILE * fp, const char *szFormat,...)
268 char buffer[INITIAL_INLINEASM];
270 va_start (ap, szFormat);
271 tvsprintf (buffer, szFormat, ap);
276 tsprintf (char *buffer, const char *szFormat,...)
279 va_start (ap, szFormat);
280 tvsprintf (buffer, szFormat, ap);
284 asm_addTree (const ASM_MAPPINGS * pMappings)
286 const ASM_MAPPING *pMap;
288 /* Traverse down first */
289 if (pMappings->pParent)
290 asm_addTree (pMappings->pParent);
291 pMap = pMappings->pMappings;
292 while (pMap->szKey && pMap->szValue) {
293 shash_add (&_h, pMap->szKey, pMap->szValue);
298 static const ASM_MAPPING _asxxxx_mapping[] =
300 {"labeldef", "%s::"},
301 {"slabeldef", "%s:"},
302 {"tlabeldef", "%05d$:"},
307 {"area", ".area %s"},
308 {"areacode", ".area %s"},
309 {"areadata", ".area %s"},
310 {"areahome", ".area %s"},
311 {"ascii", ".ascii \"%s\""},
317 {"constbyte", "0x%02X"},
318 {"constword", "0x%04X"},
319 {"immedword", "#0x%04X"},
320 {"immedbyte", "#0x%02X"},
321 {"hashedstr", "#%s"},
322 {"lsbimmeds", "#<%s"},
323 {"msbimmeds", "#>%s"},
324 {"module", ".module %s"},
325 {"global", ".globl %s"},
328 "; ---------------------------------\n"
330 "; ---------------------------------"
332 {"functionlabeldef", "%s:"},
333 {"bankimmeds", "0 ; PENDING: bank support"},
334 {"los","(%s & 0xFF)"},
336 {"hihis","(%s >> 16)"},
337 {"hihihis","(%s >> 24)"},
338 {"lod","(%d & 0xFF)"},
340 {"hihid","(%d >> 16)"},
341 {"hihihid","(%d >> 24)"},
342 {"lol","(%05d$ & 0xFF)"},
343 {"hil","(%05d$ >> 8)"},
344 {"hihil","(%05d$ >> 16)"},
345 {"hihihil","(%05d$ >> 24)"},
349 static const ASM_MAPPING _gas_mapping[] =
351 {"labeldef", "%s::"},
352 {"slabeldef", "%s:"},
353 {"tlabeldef", "%05d$:"},
358 {"area", ".section %s"},
359 {"areacode", ".section %s"},
360 {"areadata", ".section %s"},
361 {"areahome", ".section %s"},
362 {"ascii", ".ascii \"%s\""},
368 {"constbyte", "0x%02X"},
369 {"constword", "0x%04X"},
370 {"immedword", "#0x%04X"},
371 {"immedbyte", "#0x%02X"},
372 {"hashedstr", "#%s"},
373 {"lsbimmeds", "#<%s"},
374 {"msbimmeds", "#>%s"},
375 {"module", ".file \"%s.c\""},
376 {"global", ".globl %s"},
379 "; ---------------------------------\n"
381 "; ---------------------------------"
383 {"functionlabeldef", "%s:"},
384 {"bankimmeds", "0 ; PENDING: bank support"},
388 const ASM_MAPPINGS asm_asxxxx_mapping =
394 const ASM_MAPPINGS asm_gas_mapping =