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.
13 // for O_BINARY in _pipe()
21 /* A 'token' is like !blah or %24f and is under the programmers
23 #define MAX_TOKEN_LEN 64
28 FileBaseName (char *fileFullName)
30 char *p = fileFullName;
38 if ((*fileFullName == '/') || (*fileFullName == '\\') || (*fileFullName == ':'))
49 _findMapping (const char *szKey)
51 return shash_find (_h, szKey);
54 // Append a string onto another, and update the pointer to the end of
57 _appendAt (char *at, char *onto, const char *sz, size_t *max)
59 wassert (at && onto && sz);
60 strncpyz (at, sz, *max);
62 return at + strlen (at);
66 tvsprintf (char *buffer, size_t len, const char *format, va_list ap)
68 // Under Linux PPC va_list is a structure instead of a primitive type,
69 // and doesnt like being passed around. This version turns everything
74 // %[CIFN] - special formats with no argument (ie list isnt touched)
75 // All of the system formats
77 // This is acheived by expanding the tokens and zero arg formats into
78 // one big format string, which is passed to the native printf.
80 char noTokens[INITIAL_INLINEASM];
81 char newFormat[INITIAL_INLINEASM];
82 char *pInto = noTokens;
83 size_t pIntoLen = sizeof(noTokens);
85 char token[MAX_TOKEN_LEN];
86 const char *sz = format;
88 // NULL terminate it to let strlen work.
91 /* First pass: expand all of the macros */
92 while (pIntoLen && *sz)
96 /* Start of a token. Search until the first
97 [non alpha, *] and call it a token. */
101 while (isalpha (*sz) || *sz == '*')
106 /* Now find the token in the token list */
107 if ((t = _findMapping (token)))
109 pInto = _appendAt (pInto, noTokens, t, &pIntoLen);
113 fprintf (stderr, "Cant find token \"%s\"\n", token);
127 "Internal error: tvsprintf overflowed on pass one.\n");
128 // Might as well go on...
133 /* Second pass: Expand any macros that we own */
136 pIntoLen = sizeof(newFormat);
138 while (pIntoLen && *sz)
142 // See if its one that we handle.
147 // Code segment name.
148 pInto = _appendAt (pInto, newFormat, CODE_NAME, &pIntoLen);
153 pInto = _appendAt (pInto, newFormat, fullSrcFileName, &pIntoLen);
157 // Current function name.
158 pInto = _appendAt (pInto, newFormat, currFunc->rname, &pIntoLen);
165 SNPRINTF (id, sizeof(id), "%u", ++count);
166 pInto = _appendAt (pInto, newFormat, id, &pIntoLen);
171 // Not one of ours. Copy until the end.
174 while (pIntoLen && !isalpha (*sz))
196 "Internal error: tvsprintf overflowed on pass two.\n");
197 // Might as well go on...
202 // Now do the actual printing
203 #if defined(HAVE_VSNPRINTF)
206 wrlen = vsnprintf (buffer, len, newFormat, ap);
208 if (wrlen < 0 || wrlen >= len)
210 fprintf(stderr, "Internal error: tvsprintf truncated.\n");
215 vsprintf (buffer, newFormat, ap);
216 if (strlen(buffer) >= len)
218 fprintf(stderr, "Internal error: tvsprintf overflowed.\n");
224 tfprintf (FILE * fp, const char *szFormat,...)
227 char buffer[INITIAL_INLINEASM];
229 va_start (ap, szFormat);
230 tvsprintf (buffer, INITIAL_INLINEASM, szFormat, ap);
236 tsprintf (char *buffer, size_t len, const char *szFormat,...)
239 va_start (ap, szFormat);
240 tvsprintf (buffer, len, szFormat, ap);
245 asm_addTree (const ASM_MAPPINGS * pMappings)
247 const ASM_MAPPING *pMap;
249 /* Traverse down first */
250 if (pMappings->pParent)
251 asm_addTree (pMappings->pParent);
252 pMap = pMappings->pMappings;
253 while (pMap->szKey && pMap->szValue) {
254 shash_add (&_h, pMap->szKey, pMap->szValue);
259 /*-----------------------------------------------------------------*/
260 /* printILine - return the readable i-code for this ic */
262 /* iCodePrint wants a file stream so we need a pipe to fool it */
263 /*-----------------------------------------------------------------*/
264 static char verbalICode[1024];
266 char *printILine (iCode *ic) {
269 iCodeTable *icTab=getTableEntry(ic->op);
273 res = _pipe(filedes, 256, O_BINARY);
277 assert(res != -1); // forget it
279 return ""; // return empty line if pipe creation failed
281 // stuff the pipe with the readable icode
282 pipeStream=fdopen(filedes[1],"w");
283 icTab->iCodePrint(pipeStream, ic, icTab->printName);
284 // it really needs an extra push
287 pipeStream=fdopen(filedes[0],"r");
288 fgets(verbalICode, sizeof(verbalICode), pipeStream);
289 // clean up the mess, we'll return here for all icodes!!
290 assert(!close (filedes[0]));
291 assert(!close (filedes[1]));
292 // kill the trailing NL
293 verbalICode[strlen(verbalICode)-1]='\0';
298 /*-----------------------------------------------------------------*/
299 /* printCLine - return the c-code for this lineno */
300 /*-----------------------------------------------------------------*/
301 static FILE *inFile=NULL;
302 static char inLineString[1024];
303 static int inLineNo=0;
304 static char lastSrcFile[PATH_MAX];
307 char *printCLine (char *srcFile, int lineno) {
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 ((int)*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)"},
396 static const ASM_MAPPING _gas_mapping[] =
398 {"labeldef", "%s::"},
399 {"slabeldef", "%s:"},
400 {"tlabeldef", "%05d$:"},
405 {"area", ".section %s"},
406 {"areacode", ".section %s"},
407 {"areadata", ".section %s"},
408 {"areahome", ".section %s"},
409 {"ascii", ".ascii \"%s\""},
415 {"constbyte", "0x%02X"},
416 {"constword", "0x%04X"},
417 {"immedword", "#0x%04X"},
418 {"immedbyte", "#0x%02X"},
419 {"hashedstr", "#%s"},
420 {"lsbimmeds", "#<%s"},
421 {"msbimmeds", "#>%s"},
422 {"module", ".file \"%s.c\""},
423 {"global", ".globl %s"},
424 {"extern", ".globl %s"},
427 "; ---------------------------------\n"
429 "; ---------------------------------"
431 {"functionlabeldef", "%s:"},
432 {"bankimmeds", "0 ; PENDING: bank support"},
436 static const ASM_MAPPING _a390_mapping[] =
439 {"slabeldef", "%s:"},
440 {"tlabeldef", "L%05d:"},
445 {"area", "; SECTION NOT SUPPORTED"},
446 {"areacode", "; SECTION NOT SUPPORTED"},
447 {"areadata", "; SECTION NOT SUPPORTED"},
448 {"areahome", "; SECTION NOT SUPPORTED"},
449 {"ascii", "db \"%s\""},
450 {"ds", "; STORAGE NOT SUPPORTED"},
452 {"dbs", "db \"%s\""},
455 {"constbyte", "0%02xh"},
456 {"constword", "0%04xh"},
457 {"immedword", "#0%04Xh"},
458 {"immedbyte", "#0%02Xh"},
459 {"hashedstr", "#%s"},
460 {"lsbimmeds", "#<%s"},
461 {"msbimmeds", "#>%s"},
462 {"module", "; .file \"%s.c\""},
463 {"global", "; .globl %s"},
466 "; ---------------------------------\n"
468 "; ---------------------------------"
470 {"functionlabeldef", "%s:"},
471 {"bankimmeds", "0 ; PENDING: bank support"},
472 {"los","(%s & 0FFh)"},
473 {"his","((%s / 256) & 0FFh)"},
474 {"hihis","((%s / 65536) & 0FFh)"},
475 {"hihihis","((%s / 16777216) & 0FFh)"},
476 {"lod","(%d & 0FFh)"},
477 {"hid","((%d / 256) & 0FFh)"},
478 {"hihid","((%d / 65536) & 0FFh)"},
479 {"hihihid","((%d / 16777216) & 0FFh)"},
480 {"lol","(L%05d & 0FFh)"},
481 {"hil","((L%05d / 256) & 0FFh)"},
482 {"hihil","((L%05d / 65536) & 0FFh)"},
483 {"hihihil","((L%09d / 16777216) & 0FFh)"},
488 static const ASM_MAPPING _xa_asm_mapping[] =
491 {"slabeldef", "%s:"},
492 {"tlabeldef", "L%05d:"},
497 {"area", ".area %s"},
498 {"areacode", ".area %s"},
499 {"areadata", ".area %s"},
500 {"areahome", ".area %s"},
501 {"ascii", ".db \"%s\""},
504 {"dbs", ".db \"%s\""},
507 {"constbyte", "0x%02x"},
508 {"constword", "0x%04x"},
509 {"immedword", "0x%04x"},
510 {"immedbyte", "0x%02x"},
511 {"hashedstr", "#%s"},
512 {"lsbimmeds", "#<%s"},
513 {"msbimmeds", "#>%s"},
514 {"module", "; .module %s"},
515 {"global", ".globl %s"},
518 "; ---------------------------------\n"
520 "; ---------------------------------"
522 {"functionlabeldef", "%s:"},
523 {"bankimmeds", "0 ; PENDING: bank support"},
524 {"los","(%s & 0FFh)"},
525 {"his","((%s / 256) & 0FFh)"},
526 {"hihis","((%s / 65536) & 0FFh)"},
527 {"hihihis","((%s / 16777216) & 0FFh)"},
528 {"lod","(%d & 0FFh)"},
529 {"hid","((%d / 256) & 0FFh)"},
530 {"hihid","((%d / 65536) & 0FFh)"},
531 {"hihihid","((%d / 16777216) & 0FFh)"},
532 {"lol","(L%05d & 0FFh)"},
533 {"hil","((L%05d / 256) & 0FFh)"},
534 {"hihil","((L%05d / 65536) & 0FFh)"},
535 {"hihihil","((L%09d / 16777216) & 0FFh)"},
540 const ASM_MAPPINGS asm_asxxxx_mapping =
546 const ASM_MAPPINGS asm_gas_mapping =
552 const ASM_MAPPINGS asm_a390_mapping =
558 const ASM_MAPPINGS asm_xa_asm_mapping =