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);
45 // Append a string onto another, and update the pointer to the end of
48 _appendAt (char *at, char *onto, const char *sz, size_t *max)
50 wassert (at && onto && sz);
51 strncpyz (at, sz, *max);
53 return at + strlen (at);
57 tvsprintf (char *buffer, size_t len, const char *format, va_list ap)
59 // Under Linux PPC va_list is a structure instead of a primitive type,
60 // and doesnt like being passed around. This version turns everything
65 // %[CIFN] - special formats with no argument (ie list isnt touched)
66 // All of the system formats
68 // This is acheived by expanding the tokens and zero arg formats into
69 // one big format string, which is passed to the native printf.
71 char noTokens[INITIAL_INLINEASM];
72 char newFormat[INITIAL_INLINEASM];
73 char *pInto = noTokens;
74 size_t pIntoLen = sizeof(noTokens);
76 char token[MAX_TOKEN_LEN];
77 const char *sz = format;
79 // NULL terminate it to let strlen work.
82 /* First pass: expand all of the macros */
83 while (pIntoLen && *sz)
87 /* Start of a token. Search until the first
88 [non alpha, *] and call it a token. */
92 while (isalpha ((unsigned char)*sz) || *sz == '*')
97 /* Now find the token in the token list */
98 if ((t = _findMapping (token)))
100 pInto = _appendAt (pInto, noTokens, t, &pIntoLen);
104 fprintf (stderr, "Cant find token \"%s\"\n", token);
118 "Internal error: tvsprintf overflowed on pass one.\n");
119 // Might as well go on...
124 /* Second pass: Expand any macros that we own */
127 pIntoLen = sizeof(newFormat);
129 while (pIntoLen && *sz)
133 // See if its one that we handle.
138 // Code segment name.
139 pInto = _appendAt (pInto, newFormat, CODE_NAME, &pIntoLen);
144 pInto = _appendAt (pInto, newFormat, fullSrcFileName, &pIntoLen);
148 // Current function name.
149 pInto = _appendAt (pInto, newFormat, currFunc->rname, &pIntoLen);
156 SNPRINTF (id, sizeof(id), "%u", ++count);
157 pInto = _appendAt (pInto, newFormat, id, &pIntoLen);
162 // Not one of ours. Copy until the end.
165 while (pIntoLen && !isalpha ((unsigned char)*sz))
187 "Internal error: tvsprintf overflowed on pass two.\n");
188 // Might as well go on...
193 // Now do the actual printing
194 #if defined(HAVE_VSNPRINTF)
197 wrlen = vsnprintf (buffer, len, newFormat, ap);
199 if (wrlen < 0 || (size_t)wrlen >= len)
201 fprintf(stderr, "Internal error: tvsprintf truncated.\n");
206 vsprintf (buffer, newFormat, ap);
207 if (strlen(buffer) >= len)
209 fprintf(stderr, "Internal error: tvsprintf overflowed.\n");
215 tfprintf (FILE * fp, const char *szFormat,...)
218 char buffer[INITIAL_INLINEASM];
220 va_start (ap, szFormat);
221 tvsprintf (buffer, INITIAL_INLINEASM, szFormat, ap);
227 tsprintf (char *buffer, size_t len, const char *szFormat,...)
230 va_start (ap, szFormat);
231 tvsprintf (buffer, len, szFormat, ap);
236 asm_addTree (const ASM_MAPPINGS * pMappings)
238 const ASM_MAPPING *pMap;
240 /* Traverse down first */
241 if (pMappings->pParent)
242 asm_addTree (pMappings->pParent);
243 pMap = pMappings->pMappings;
244 while (pMap->szKey && pMap->szValue) {
245 shash_add (&_h, pMap->szKey, pMap->szValue);
250 /*-----------------------------------------------------------------*/
251 /* printILine - return the readable i-code for this ic */
253 /* iCodePrint wants a file stream so we need a temporary file to */
255 /*-----------------------------------------------------------------*/
257 printILine (iCode *ic)
259 static char verbalICode[1024];
261 iCodeTable *icTab=getTableEntry(ic->op);
264 if (INLINEASM == ic->op)
267 tmpFile = tempfile();
268 assert(NULL != tmpFile);
270 return ""; /* return empty line if temporary file creation failed */
272 addSetHead(&tmpfileSet, tmpFile);
274 /* stuff the temporary file with the readable icode */
275 icTab->iCodePrint(tmpFile, ic, icTab->printName);
279 fgets(verbalICode, sizeof(verbalICode), tmpFile);
281 /* clean up the mess, we'll return here for all icodes!!
282 * Actually the temporary file is only closed. It will be
283 * removed by rm_tmpfiles().
285 ret = fclose(tmpFile);
287 deleteSetItem(&tmpfileSet, tmpFile);
289 /* kill the trailing NL */
290 if ('\n' == verbalICode[strlen(verbalICode) - 1])
291 verbalICode[strlen(verbalICode) - 1] = '\0';
293 /* and throw it up */
297 /*-----------------------------------------------------------------*/
298 /* printCLine - return the c-code for this lineno */
299 /*-----------------------------------------------------------------*/
302 printCLine (char *srcFile, int lineno)
304 static FILE *inFile=NULL;
305 static char inLineString[1024];
306 static int inLineNo=0;
307 static char lastSrcFile[PATH_MAX];
308 char *ilsP=inLineString;
311 if (strcmp (lastSrcFile, srcFile) != 0) {
318 inFile=fopen(srcFile, "r");
320 perror ("printCLine");
323 strncpyz (lastSrcFile, srcFile, PATH_MAX);
325 if (lineno<inLineNo) {
326 fseek (inFile, 0, SEEK_SET);
330 while (fgets (inLineString, 1024, inFile)) {
332 if (inLineNo==lineno) {
333 // remove the trailing NL
334 inLineString[strlen(inLineString)-1]='\0';
338 while (isspace ((unsigned char)*ilsP))
344 static const ASM_MAPPING _asxxxx_mapping[] =
346 {"labeldef", "%s::"},
347 {"slabeldef", "%s:"},
348 {"tlabeldef", "%05d$:"},
353 {"area", ".area %s"},
354 {"areacode", ".area %s"},
355 {"areadata", ".area %s"},
356 {"areahome", ".area %s"},
357 {"ascii", ".ascii \"%s\""},
363 {"constbyte", "0x%02X"},
364 {"constword", "0x%04X"},
365 {"immedword", "#0x%04X"},
366 {"immedbyte", "#0x%02X"},
367 {"hashedstr", "#%s"},
368 {"lsbimmeds", "#<%s"},
369 {"msbimmeds", "#>%s"},
370 {"module", ".module %s"},
371 {"global", ".globl %s"},
374 "; ---------------------------------\n"
376 "; ---------------------------------"
378 {"functionlabeldef", "%s:"},
379 {"bankimmeds", "0 ; PENDING: bank support"},
380 {"los","(%s & 0xFF)"},
382 {"hihis","(%s >> 16)"},
383 {"hihihis","(%s >> 24)"},
384 {"lod","(%d & 0xFF)"},
386 {"hihid","(%d >> 16)"},
387 {"hihihid","(%d >> 24)"},
388 {"lol","(%05d$ & 0xFF)"},
389 {"hil","(%05d$ >> 8)"},
390 {"hihil","(%05d$ >> 16)"},
391 {"hihihil","(%05d$ >> 24)"},
393 {"org", ".org %04X"},
397 static const ASM_MAPPING _gas_mapping[] =
399 {"labeldef", "%s::"},
400 {"slabeldef", "%s:"},
401 {"tlabeldef", "%05d$:"},
406 {"area", ".section %s"},
407 {"areacode", ".section %s"},
408 {"areadata", ".section %s"},
409 {"areahome", ".section %s"},
410 {"ascii", ".ascii \"%s\""},
416 {"constbyte", "0x%02X"},
417 {"constword", "0x%04X"},
418 {"immedword", "#0x%04X"},
419 {"immedbyte", "#0x%02X"},
420 {"hashedstr", "#%s"},
421 {"lsbimmeds", "#<%s"},
422 {"msbimmeds", "#>%s"},
423 {"module", ".file \"%s.c\""},
424 {"global", ".globl %s"},
425 {"extern", ".globl %s"},
428 "; ---------------------------------\n"
430 "; ---------------------------------"
432 {"functionlabeldef", "%s:"},
433 {"bankimmeds", "0 ; PENDING: bank support"},
437 static const ASM_MAPPING _a390_mapping[] =
440 {"slabeldef", "%s:"},
441 {"tlabeldef", "L%05d:"},
446 {"area", "; SECTION NOT SUPPORTED"},
447 {"areacode", "; SECTION NOT SUPPORTED"},
448 {"areadata", "; SECTION NOT SUPPORTED"},
449 {"areahome", "; SECTION NOT SUPPORTED"},
450 {"ascii", "db \"%s\""},
451 {"ds", "; STORAGE NOT SUPPORTED"},
453 {"dbs", "db \"%s\""},
456 {"constbyte", "0%02xh"},
457 {"constword", "0%04xh"},
458 {"immedword", "#0%04Xh"},
459 {"immedbyte", "#0%02Xh"},
460 {"hashedstr", "#%s"},
461 {"lsbimmeds", "#<%s"},
462 {"msbimmeds", "#>%s"},
463 {"module", "; .file \"%s.c\""},
464 {"global", "; .globl %s"},
467 "; ---------------------------------\n"
469 "; ---------------------------------"
471 {"functionlabeldef", "%s:"},
472 {"bankimmeds", "0 ; PENDING: bank support"},
473 {"los","(%s & 0FFh)"},
474 {"his","((%s / 256) & 0FFh)"},
475 {"hihis","((%s / 65536) & 0FFh)"},
476 {"hihihis","((%s / 16777216) & 0FFh)"},
477 {"lod","(%d & 0FFh)"},
478 {"hid","((%d / 256) & 0FFh)"},
479 {"hihid","((%d / 65536) & 0FFh)"},
480 {"hihihid","((%d / 16777216) & 0FFh)"},
481 {"lol","(L%05d & 0FFh)"},
482 {"hil","((L%05d / 256) & 0FFh)"},
483 {"hihil","((L%05d / 65536) & 0FFh)"},
484 {"hihihil","((L%09d / 16777216) & 0FFh)"},
489 static const ASM_MAPPING _xa_asm_mapping[] =
492 {"slabeldef", "%s:"},
493 {"tlabeldef", "L%05d:"},
498 {"area", ".area %s"},
499 {"areacode", ".area %s"},
500 {"areadata", ".area %s"},
501 {"areahome", ".area %s"},
502 {"ascii", ".db \"%s\""},
505 {"dbs", ".db \"%s\""},
508 {"constbyte", "0x%02x"},
509 {"constword", "0x%04x"},
510 {"immedword", "0x%04x"},
511 {"immedbyte", "0x%02x"},
512 {"hashedstr", "#%s"},
513 {"lsbimmeds", "#<%s"},
514 {"msbimmeds", "#>%s"},
515 {"module", "; .module %s"},
516 {"global", ".globl %s"},
519 "; ---------------------------------\n"
521 "; ---------------------------------"
523 {"functionlabeldef", "%s:"},
524 {"bankimmeds", "0 ; PENDING: bank support"},
525 {"los","(%s & 0FFh)"},
526 {"his","((%s / 256) & 0FFh)"},
527 {"hihis","((%s / 65536) & 0FFh)"},
528 {"hihihis","((%s / 16777216) & 0FFh)"},
529 {"lod","(%d & 0FFh)"},
530 {"hid","((%d / 256) & 0FFh)"},
531 {"hihid","((%d / 65536) & 0FFh)"},
532 {"hihihid","((%d / 16777216) & 0FFh)"},
533 {"lol","(L%05d & 0FFh)"},
534 {"hil","((L%05d / 256) & 0FFh)"},
535 {"hihil","((L%05d / 65536) & 0FFh)"},
536 {"hihihil","((L%09d / 16777216) & 0FFh)"},
541 const ASM_MAPPINGS asm_asxxxx_mapping =
547 const ASM_MAPPINGS asm_gas_mapping =
553 const ASM_MAPPINGS asm_a390_mapping =
559 const ASM_MAPPINGS asm_xa_asm_mapping =