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 #if !defined(__BORLANDC__) && !defined(_MSC_VER)
17 /* A 'token' is like !blah or %24f and is under the programmers
19 #define MAX_TOKEN_LEN 64
24 FileBaseName (char *fileFullName)
26 char *p = fileFullName;
34 if ((*fileFullName == '/') || (*fileFullName == '\\') || (*fileFullName == ':'))
45 _findMapping (const char *szKey)
47 return shash_find (_h, szKey);
50 // Append a string onto another, and update the pointer to the end of
53 _appendAt (char *at, char *onto, const char *sz)
55 wassert (at && onto && sz);
57 return at + strlen (sz);
61 tvsprintf (char *buffer, const char *format, va_list ap)
63 // Under Linux PPC va_list is a structure instead of a primitive type,
64 // and doesnt like being passed around. This version turns everything
69 // %[CIFN] - special formats with no argument (ie list isnt touched)
70 // All of the system formats
72 // This is acheived by expanding the tokens and zero arg formats into
73 // one big format string, which is passed to the native printf.
75 char noTokens[INITIAL_INLINEASM];
76 char newFormat[INITIAL_INLINEASM];
77 char *pInto = noTokens;
79 char token[MAX_TOKEN_LEN];
80 const char *sz = format;
82 // NULL terminate it to let strlen work.
85 /* First pass: expand all of the macros */
90 /* Start of a token. Search until the first
91 [non alpha, *] and call it a token. */
95 while (isalpha (*sz) || *sz == '*')
100 /* Now find the token in the token list */
101 if ((t = _findMapping (token)))
103 pInto = _appendAt (pInto, noTokens, t);
107 fprintf (stderr, "Cant find token \"%s\"\n", token);
119 /* Second pass: Expand any macros that we own */
127 // See if its one that we handle.
132 // Code segment name.
133 pInto = _appendAt (pInto, newFormat, CODE_NAME);
138 pInto = _appendAt (pInto, newFormat, fullSrcFileName);
142 // Current function name.
143 pInto = _appendAt (pInto, newFormat, currFunc->rname);
150 sprintf (id, "%u", ++count);
151 pInto = _appendAt (pInto, newFormat, id);
156 // Not one of ours. Copy until the end.
158 while (!isalpha (*sz))
173 // Now do the actual printing
174 vsprintf (buffer, newFormat, ap);
178 tfprintf (FILE * fp, const char *szFormat,...)
181 char buffer[INITIAL_INLINEASM];
183 va_start (ap, szFormat);
184 tvsprintf (buffer, szFormat, ap);
189 tsprintf (char *buffer, const char *szFormat,...)
192 va_start (ap, szFormat);
193 tvsprintf (buffer, szFormat, ap);
197 asm_addTree (const ASM_MAPPINGS * pMappings)
199 const ASM_MAPPING *pMap;
201 /* Traverse down first */
202 if (pMappings->pParent)
203 asm_addTree (pMappings->pParent);
204 pMap = pMappings->pMappings;
205 while (pMap->szKey && pMap->szValue) {
206 shash_add (&_h, pMap->szKey, pMap->szValue);
211 /*-----------------------------------------------------------------*/
212 /* printILine - return the readable i-code for this ic */
214 /* iCodePrint wants a file stream so we need a pipe to fool it */
215 /*-----------------------------------------------------------------*/
216 static char verbalICode[1024];
218 char *printILine (iCode *ic) {
221 iCodeTable *icTab=getTableEntry(ic->op);
223 assert(pipe(filedes)!=-1); // forget it
225 // stuff the pipe with the readable icode
226 pipeStream=fdopen(filedes[1],"w");
227 icTab->iCodePrint(pipeStream, ic, icTab->printName);
228 // it really needs an extra push
231 pipeStream=fdopen(filedes[0],"r");
232 fgets(verbalICode, sizeof(verbalICode), pipeStream);
233 // clean up the mess, we'll return here for all icodes!!
234 assert(!close (filedes[0]));
235 assert(!close (filedes[1]));
236 // kill the trailing NL
237 verbalICode[strlen(verbalICode)-1]='\0';
242 /*-----------------------------------------------------------------*/
243 /* printCLine - return the c-code for this lineno */
244 /*-----------------------------------------------------------------*/
245 static FILE *inFile=NULL;
246 static char inLineString[1024];
247 static int inLineNo=0;
248 static char lastSrcFile[PATH_MAX];
251 char *printCLine (char *srcFile, int lineno) {
252 char *ilsP=inLineString;
255 if (strcmp (lastSrcFile, srcFile) != 0) {
262 inFile=fopen(srcFile, "r");
264 perror ("printCLine");
267 strcpy (lastSrcFile, srcFile);
269 if (lineno<inLineNo) {
270 fseek (inFile, 0, SEEK_SET);
274 while (fgets (inLineString, 1024, inFile)) {
276 if (inLineNo==lineno) {
277 // remove the trailing NL
278 inLineString[strlen(inLineString)-1]='\0';
282 while (isspace ((int)*ilsP))
288 static const ASM_MAPPING _asxxxx_mapping[] =
290 {"labeldef", "%s::"},
291 {"slabeldef", "%s:"},
292 {"tlabeldef", "%05d$:"},
297 {"area", ".area %s"},
298 {"areacode", ".area %s"},
299 {"areadata", ".area %s"},
300 {"areahome", ".area %s"},
301 {"ascii", ".ascii \"%s\""},
307 {"constbyte", "0x%02X"},
308 {"constword", "0x%04X"},
309 {"immedword", "#0x%04X"},
310 {"immedbyte", "#0x%02X"},
311 {"hashedstr", "#%s"},
312 {"lsbimmeds", "#<%s"},
313 {"msbimmeds", "#>%s"},
314 {"module", ".module %s"},
315 {"global", ".globl %s"},
318 "; ---------------------------------\n"
320 "; ---------------------------------"
322 {"functionlabeldef", "%s:"},
323 {"bankimmeds", "0 ; PENDING: bank support"},
324 {"los","(%s & 0xFF)"},
326 {"hihis","(%s >> 16)"},
327 {"hihihis","(%s >> 24)"},
328 {"lod","(%d & 0xFF)"},
330 {"hihid","(%d >> 16)"},
331 {"hihihid","(%d >> 24)"},
332 {"lol","(%05d$ & 0xFF)"},
333 {"hil","(%05d$ >> 8)"},
334 {"hihil","(%05d$ >> 16)"},
335 {"hihihil","(%05d$ >> 24)"},
340 static const ASM_MAPPING _gas_mapping[] =
342 {"labeldef", "%s::"},
343 {"slabeldef", "%s:"},
344 {"tlabeldef", "%05d$:"},
349 {"area", ".section %s"},
350 {"areacode", ".section %s"},
351 {"areadata", ".section %s"},
352 {"areahome", ".section %s"},
353 {"ascii", ".ascii \"%s\""},
359 {"constbyte", "0x%02X"},
360 {"constword", "0x%04X"},
361 {"immedword", "#0x%04X"},
362 {"immedbyte", "#0x%02X"},
363 {"hashedstr", "#%s"},
364 {"lsbimmeds", "#<%s"},
365 {"msbimmeds", "#>%s"},
366 {"module", ".file \"%s.c\""},
367 {"global", ".globl %s"},
368 {"extern", ".globl %s"},
371 "; ---------------------------------\n"
373 "; ---------------------------------"
375 {"functionlabeldef", "%s:"},
376 {"bankimmeds", "0 ; PENDING: bank support"},
380 static const ASM_MAPPING _a390_mapping[] =
383 {"slabeldef", "%s:"},
384 {"tlabeldef", "L%05d:"},
389 {"area", "; SECTION NOT SUPPORTED"},
390 {"areacode", "; SECTION NOT SUPPORTED"},
391 {"areadata", "; SECTION NOT SUPPORTED"},
392 {"areahome", "; SECTION NOT SUPPORTED"},
393 {"ascii", "db \"%s\""},
394 {"ds", "; STORAGE NOT SUPPORTED"},
396 {"dbs", "db \"%s\""},
399 {"constbyte", "0%02xh"},
400 {"constword", "0%04xh"},
401 {"immedword", "#0%04Xh"},
402 {"immedbyte", "#0%02Xh"},
403 {"hashedstr", "#%s"},
404 {"lsbimmeds", "#<%s"},
405 {"msbimmeds", "#>%s"},
406 {"module", "; .file \"%s.c\""},
407 {"global", "; .globl %s"},
410 "; ---------------------------------\n"
412 "; ---------------------------------"
414 {"functionlabeldef", "%s:"},
415 {"bankimmeds", "0 ; PENDING: bank support"},
416 {"los","(%s & 0FFh)"},
417 {"his","((%s / 256) & 0FFh)"},
418 {"hihis","((%s / 65536) & 0FFh)"},
419 {"hihihis","((%s / 16777216) & 0FFh)"},
420 {"lod","(%d & 0FFh)"},
421 {"hid","((%d / 256) & 0FFh)"},
422 {"hihid","((%d / 65536) & 0FFh)"},
423 {"hihihid","((%d / 16777216) & 0FFh)"},
424 {"lol","(L%05d & 0FFh)"},
425 {"hil","((L%05d / 256) & 0FFh)"},
426 {"hihil","((L%05d / 65536) & 0FFh)"},
427 {"hihihil","((L%09d / 16777216) & 0FFh)"},
432 static const ASM_MAPPING _xa_asm_mapping[] =
435 {"slabeldef", "%s:"},
436 {"tlabeldef", "L%05d:"},
441 {"area", ".area %s"},
442 {"areacode", ".area %s"},
443 {"areadata", ".area %s"},
444 {"areahome", ".area %s"},
445 {"ascii", ".db \"%s\""},
448 {"dbs", ".db \"%s\""},
451 {"constbyte", "0x%02x"},
452 {"constword", "0x%04x"},
453 {"immedword", "0x%04x"},
454 {"immedbyte", "0x%02x"},
455 {"hashedstr", "#%s"},
456 {"lsbimmeds", "#<%s"},
457 {"msbimmeds", "#>%s"},
458 {"module", "; .module %s"},
459 {"global", ".globl %s"},
462 "; ---------------------------------\n"
464 "; ---------------------------------"
466 {"functionlabeldef", "%s:"},
467 {"bankimmeds", "0 ; PENDING: bank support"},
468 {"los","(%s & 0FFh)"},
469 {"his","((%s / 256) & 0FFh)"},
470 {"hihis","((%s / 65536) & 0FFh)"},
471 {"hihihis","((%s / 16777216) & 0FFh)"},
472 {"lod","(%d & 0FFh)"},
473 {"hid","((%d / 256) & 0FFh)"},
474 {"hihid","((%d / 65536) & 0FFh)"},
475 {"hihihid","((%d / 16777216) & 0FFh)"},
476 {"lol","(L%05d & 0FFh)"},
477 {"hil","((L%05d / 256) & 0FFh)"},
478 {"hihil","((L%05d / 65536) & 0FFh)"},
479 {"hihihil","((L%09d / 16777216) & 0FFh)"},
484 const ASM_MAPPINGS asm_asxxxx_mapping =
490 const ASM_MAPPINGS asm_gas_mapping =
496 const ASM_MAPPINGS asm_a390_mapping =
502 const ASM_MAPPINGS asm_xa_asm_mapping =