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 __MINGW32__
13 // for O_BINARY in _pipe()
15 #elif !defined(__BORLANDC__) && !defined(_MSC_VER)
20 /* A 'token' is like !blah or %24f and is under the programmers
22 #define MAX_TOKEN_LEN 64
27 FileBaseName (char *fileFullName)
29 char *p = fileFullName;
37 if ((*fileFullName == '/') || (*fileFullName == '\\') || (*fileFullName == ':'))
48 _findMapping (const char *szKey)
50 return shash_find (_h, szKey);
53 // Append a string onto another, and update the pointer to the end of
56 _appendAt (char *at, char *onto, const char *sz)
58 wassert (at && onto && sz);
60 return at + strlen (sz);
64 tvsprintf (char *buffer, const char *format, va_list ap)
66 // Under Linux PPC va_list is a structure instead of a primitive type,
67 // and doesnt like being passed around. This version turns everything
72 // %[CIFN] - special formats with no argument (ie list isnt touched)
73 // All of the system formats
75 // This is acheived by expanding the tokens and zero arg formats into
76 // one big format string, which is passed to the native printf.
78 char noTokens[INITIAL_INLINEASM];
79 char newFormat[INITIAL_INLINEASM];
80 char *pInto = noTokens;
82 char token[MAX_TOKEN_LEN];
83 const char *sz = format;
85 // NULL terminate it to let strlen work.
88 /* First pass: expand all of the macros */
93 /* Start of a token. Search until the first
94 [non alpha, *] and call it a token. */
98 while (isalpha (*sz) || *sz == '*')
103 /* Now find the token in the token list */
104 if ((t = _findMapping (token)))
106 pInto = _appendAt (pInto, noTokens, t);
110 fprintf (stderr, "Cant find token \"%s\"\n", token);
122 /* Second pass: Expand any macros that we own */
130 // See if its one that we handle.
135 // Code segment name.
136 pInto = _appendAt (pInto, newFormat, CODE_NAME);
141 pInto = _appendAt (pInto, newFormat, fullSrcFileName);
145 // Current function name.
146 pInto = _appendAt (pInto, newFormat, currFunc->rname);
153 sprintf (id, "%u", ++count);
154 pInto = _appendAt (pInto, newFormat, id);
159 // Not one of ours. Copy until the end.
161 while (!isalpha (*sz))
176 // Now do the actual printing
177 vsprintf (buffer, newFormat, ap);
181 tfprintf (FILE * fp, const char *szFormat,...)
184 char buffer[INITIAL_INLINEASM];
186 va_start (ap, szFormat);
187 tvsprintf (buffer, szFormat, ap);
192 tsprintf (char *buffer, const char *szFormat,...)
195 va_start (ap, szFormat);
196 tvsprintf (buffer, szFormat, ap);
200 asm_addTree (const ASM_MAPPINGS * pMappings)
202 const ASM_MAPPING *pMap;
204 /* Traverse down first */
205 if (pMappings->pParent)
206 asm_addTree (pMappings->pParent);
207 pMap = pMappings->pMappings;
208 while (pMap->szKey && pMap->szValue) {
209 shash_add (&_h, pMap->szKey, pMap->szValue);
214 /*-----------------------------------------------------------------*/
215 /* printILine - return the readable i-code for this ic */
217 /* iCodePrint wants a file stream so we need a pipe to fool it */
218 /*-----------------------------------------------------------------*/
219 static char verbalICode[1024];
221 char *printILine (iCode *ic) {
224 iCodeTable *icTab=getTableEntry(ic->op);
226 #if defined __MINGW32__
227 assert(_pipe(filedes, 256, O_BINARY)!=-1); // forget it
229 assert(pipe(filedes)!=-1); // forget it
232 // stuff the pipe with the readable icode
233 pipeStream=fdopen(filedes[1],"w");
234 icTab->iCodePrint(pipeStream, ic, icTab->printName);
235 // it really needs an extra push
238 pipeStream=fdopen(filedes[0],"r");
239 fgets(verbalICode, sizeof(verbalICode), pipeStream);
240 // clean up the mess, we'll return here for all icodes!!
241 assert(!close (filedes[0]));
242 assert(!close (filedes[1]));
243 // kill the trailing NL
244 verbalICode[strlen(verbalICode)-1]='\0';
249 /*-----------------------------------------------------------------*/
250 /* printCLine - return the c-code for this lineno */
251 /*-----------------------------------------------------------------*/
252 static FILE *inFile=NULL;
253 static char inLineString[1024];
254 static int inLineNo=0;
255 static char lastSrcFile[PATH_MAX];
258 char *printCLine (char *srcFile, int lineno) {
259 char *ilsP=inLineString;
262 if (strcmp (lastSrcFile, srcFile) != 0) {
269 inFile=fopen(srcFile, "r");
271 perror ("printCLine");
274 strcpy (lastSrcFile, srcFile);
276 if (lineno<inLineNo) {
277 fseek (inFile, 0, SEEK_SET);
281 while (fgets (inLineString, 1024, inFile)) {
283 if (inLineNo==lineno) {
284 // remove the trailing NL
285 inLineString[strlen(inLineString)-1]='\0';
289 while (isspace ((int)*ilsP))
295 static const ASM_MAPPING _asxxxx_mapping[] =
297 {"labeldef", "%s::"},
298 {"slabeldef", "%s:"},
299 {"tlabeldef", "%05d$:"},
304 {"area", ".area %s"},
305 {"areacode", ".area %s"},
306 {"areadata", ".area %s"},
307 {"areahome", ".area %s"},
308 {"ascii", ".ascii \"%s\""},
314 {"constbyte", "0x%02X"},
315 {"constword", "0x%04X"},
316 {"immedword", "#0x%04X"},
317 {"immedbyte", "#0x%02X"},
318 {"hashedstr", "#%s"},
319 {"lsbimmeds", "#<%s"},
320 {"msbimmeds", "#>%s"},
321 {"module", ".module %s"},
322 {"global", ".globl %s"},
325 "; ---------------------------------\n"
327 "; ---------------------------------"
329 {"functionlabeldef", "%s:"},
330 {"bankimmeds", "0 ; PENDING: bank support"},
331 {"los","(%s & 0xFF)"},
333 {"hihis","(%s >> 16)"},
334 {"hihihis","(%s >> 24)"},
335 {"lod","(%d & 0xFF)"},
337 {"hihid","(%d >> 16)"},
338 {"hihihid","(%d >> 24)"},
339 {"lol","(%05d$ & 0xFF)"},
340 {"hil","(%05d$ >> 8)"},
341 {"hihil","(%05d$ >> 16)"},
342 {"hihihil","(%05d$ >> 24)"},
347 static const ASM_MAPPING _gas_mapping[] =
349 {"labeldef", "%s::"},
350 {"slabeldef", "%s:"},
351 {"tlabeldef", "%05d$:"},
356 {"area", ".section %s"},
357 {"areacode", ".section %s"},
358 {"areadata", ".section %s"},
359 {"areahome", ".section %s"},
360 {"ascii", ".ascii \"%s\""},
366 {"constbyte", "0x%02X"},
367 {"constword", "0x%04X"},
368 {"immedword", "#0x%04X"},
369 {"immedbyte", "#0x%02X"},
370 {"hashedstr", "#%s"},
371 {"lsbimmeds", "#<%s"},
372 {"msbimmeds", "#>%s"},
373 {"module", ".file \"%s.c\""},
374 {"global", ".globl %s"},
375 {"extern", ".globl %s"},
378 "; ---------------------------------\n"
380 "; ---------------------------------"
382 {"functionlabeldef", "%s:"},
383 {"bankimmeds", "0 ; PENDING: bank support"},
387 static const ASM_MAPPING _a390_mapping[] =
390 {"slabeldef", "%s:"},
391 {"tlabeldef", "L%05d:"},
396 {"area", "; SECTION NOT SUPPORTED"},
397 {"areacode", "; SECTION NOT SUPPORTED"},
398 {"areadata", "; SECTION NOT SUPPORTED"},
399 {"areahome", "; SECTION NOT SUPPORTED"},
400 {"ascii", "db \"%s\""},
401 {"ds", "; STORAGE NOT SUPPORTED"},
403 {"dbs", "db \"%s\""},
406 {"constbyte", "0%02xh"},
407 {"constword", "0%04xh"},
408 {"immedword", "#0%04Xh"},
409 {"immedbyte", "#0%02Xh"},
410 {"hashedstr", "#%s"},
411 {"lsbimmeds", "#<%s"},
412 {"msbimmeds", "#>%s"},
413 {"module", "; .file \"%s.c\""},
414 {"global", "; .globl %s"},
417 "; ---------------------------------\n"
419 "; ---------------------------------"
421 {"functionlabeldef", "%s:"},
422 {"bankimmeds", "0 ; PENDING: bank support"},
423 {"los","(%s & 0FFh)"},
424 {"his","((%s / 256) & 0FFh)"},
425 {"hihis","((%s / 65536) & 0FFh)"},
426 {"hihihis","((%s / 16777216) & 0FFh)"},
427 {"lod","(%d & 0FFh)"},
428 {"hid","((%d / 256) & 0FFh)"},
429 {"hihid","((%d / 65536) & 0FFh)"},
430 {"hihihid","((%d / 16777216) & 0FFh)"},
431 {"lol","(L%05d & 0FFh)"},
432 {"hil","((L%05d / 256) & 0FFh)"},
433 {"hihil","((L%05d / 65536) & 0FFh)"},
434 {"hihihil","((L%09d / 16777216) & 0FFh)"},
439 static const ASM_MAPPING _xa_asm_mapping[] =
442 {"slabeldef", "%s:"},
443 {"tlabeldef", "L%05d:"},
448 {"area", ".area %s"},
449 {"areacode", ".area %s"},
450 {"areadata", ".area %s"},
451 {"areahome", ".area %s"},
452 {"ascii", ".db \"%s\""},
455 {"dbs", ".db \"%s\""},
458 {"constbyte", "0x%02x"},
459 {"constword", "0x%04x"},
460 {"immedword", "0x%04x"},
461 {"immedbyte", "0x%02x"},
462 {"hashedstr", "#%s"},
463 {"lsbimmeds", "#<%s"},
464 {"msbimmeds", "#>%s"},
465 {"module", "; .module %s"},
466 {"global", ".globl %s"},
469 "; ---------------------------------\n"
471 "; ---------------------------------"
473 {"functionlabeldef", "%s:"},
474 {"bankimmeds", "0 ; PENDING: bank support"},
475 {"los","(%s & 0FFh)"},
476 {"his","((%s / 256) & 0FFh)"},
477 {"hihis","((%s / 65536) & 0FFh)"},
478 {"hihihis","((%s / 16777216) & 0FFh)"},
479 {"lod","(%d & 0FFh)"},
480 {"hid","((%d / 256) & 0FFh)"},
481 {"hihid","((%d / 65536) & 0FFh)"},
482 {"hihihid","((%d / 16777216) & 0FFh)"},
483 {"lol","(L%05d & 0FFh)"},
484 {"hil","((L%05d / 256) & 0FFh)"},
485 {"hihil","((L%05d / 65536) & 0FFh)"},
486 {"hihihil","((L%09d / 16777216) & 0FFh)"},
491 const ASM_MAPPINGS asm_asxxxx_mapping =
497 const ASM_MAPPINGS asm_gas_mapping =
503 const ASM_MAPPINGS asm_a390_mapping =
509 const ASM_MAPPINGS asm_xa_asm_mapping =