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)"},
350 static const ASM_MAPPING _gas_mapping[] =
352 {"labeldef", "%s::"},
353 {"slabeldef", "%s:"},
354 {"tlabeldef", "%05d$:"},
359 {"area", ".section %s"},
360 {"areacode", ".section %s"},
361 {"areadata", ".section %s"},
362 {"areahome", ".section %s"},
363 {"ascii", ".ascii \"%s\""},
369 {"constbyte", "0x%02X"},
370 {"constword", "0x%04X"},
371 {"immedword", "#0x%04X"},
372 {"immedbyte", "#0x%02X"},
373 {"hashedstr", "#%s"},
374 {"lsbimmeds", "#<%s"},
375 {"msbimmeds", "#>%s"},
376 {"module", ".file \"%s.c\""},
377 {"global", ".globl %s"},
380 "; ---------------------------------\n"
382 "; ---------------------------------"
384 {"functionlabeldef", "%s:"},
385 {"bankimmeds", "0 ; PENDING: bank support"},
389 static const ASM_MAPPING _a390_mapping[] =
392 {"slabeldef", "%s:"},
393 {"tlabeldef", "L%05d:"},
398 {"area", "; SECTION NOT SUPPORTED"},
399 {"areacode", "; SECTION NOT SUPPORTED"},
400 {"areadata", "; SECTION NOT SUPPORTED"},
401 {"areahome", "; SECTION NOT SUPPORTED"},
402 {"ascii", "db \"%s\""},
403 {"ds", "; STORAGE NOT SUPPORTED"},
405 {"dbs", "db \"%s\""},
408 {"constbyte", "0%02xh"},
409 {"constword", "0%04xh"},
410 {"immedword", "#0%04Xh"},
411 {"immedbyte", "#0%02Xh"},
412 {"hashedstr", "#%s"},
413 {"lsbimmeds", "#<%s"},
414 {"msbimmeds", "#>%s"},
415 {"module", "; .file \"%s.c\""},
416 {"global", "; .globl %s"},
419 "; ---------------------------------\n"
421 "; ---------------------------------"
423 {"functionlabeldef", "%s:"},
424 {"bankimmeds", "0 ; PENDING: bank support"},
425 {"los","(%s & 0FFh)"},
426 {"his","((%s / 256) & 0FFh)"},
427 {"hihis","((%s / 65536) & 0FFh)"},
428 {"hihihis","((%s / 16777216) & 0FFh)"},
429 {"lod","(%d & 0FFh)"},
430 {"hid","((%d / 256) & 0FFh)"},
431 {"hihid","((%d / 65536) & 0FFh)"},
432 {"hihihid","((%d / 16777216) & 0FFh)"},
433 {"lol","(L%05d & 0FFh)"},
434 {"hil","((L%05d / 256) & 0FFh)"},
435 {"hihil","((L%05d / 65536) & 0FFh)"},
436 {"hihihil","((L%09d / 16777216) & 0FFh)"},
441 const ASM_MAPPINGS asm_asxxxx_mapping =
447 const ASM_MAPPINGS asm_gas_mapping =
453 const ASM_MAPPINGS asm_a390_mapping =