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.
11 #include "dbuf_string.h"
13 /* A 'token' is like !blah or %24f and is under the programmers
15 #define MAX_TOKEN_LEN 64
20 FileBaseName (char *fileFullName)
22 char *p = fileFullName;
30 if ((*fileFullName == '/') || (*fileFullName == '\\') || (*fileFullName == ':'))
41 dbuf_tvprintf (struct dbuf_s *dbuf, const char *format, va_list ap)
43 // Under Linux PPC va_list is a structure instead of a primitive type,
44 // and doesnt like being passed around. This version turns everything
49 // %[CIFN] - special formats with no argument (ie list isnt touched)
50 // All of the system formats
52 // This is acheived by expanding the tokens and zero arg formats into
53 // one big format string, which is passed to the native printf.
55 struct dbuf_s tmpDBuf;
58 char token[MAX_TOKEN_LEN];
59 const char *sz = format;
61 dbuf_init(&tmpDBuf, INITIAL_INLINEASM);
63 /* First pass: expand all of the macros */
68 /* Start of a token. Search until the first
69 [non alpha, *] and call it a token. */
73 while (isalpha ((unsigned char)*sz) || *sz == '*')
78 /* Now find the token in the token list */
79 if ((t = shash_find (_h, token)))
81 dbuf_append_str(&tmpDBuf, t);
85 fprintf (stderr, "Cant find token \"%s\"\n", token);
91 dbuf_append_char(&tmpDBuf, *sz++);
95 /* Second pass: Expand any macros that we own */
96 noTokens = dbuf_c_str(&tmpDBuf);
100 dbuf_init(&tmpDBuf, INITIAL_INLINEASM);
106 // See if its one that we handle.
111 // Code segment name.
112 dbuf_append_str(&tmpDBuf, CODE_NAME);
118 dbuf_append_str(&tmpDBuf, fullSrcFileName);
123 // Current function name.
124 dbuf_append_str(&tmpDBuf, currFunc->rname);
130 dbuf_printf(&tmpDBuf, "%u", ++count);
135 // Not one of ours. Copy until the end.
136 dbuf_append_char(&tmpDBuf, '%');
137 while (!isalpha ((unsigned char)*sz))
138 dbuf_append_char(&tmpDBuf, *sz++);
140 dbuf_append_char(&tmpDBuf, *sz++);
145 dbuf_append_char(&tmpDBuf, *sz++);
151 sz = dbuf_c_str(&tmpDBuf);
153 dbuf_vprintf(dbuf, sz, ap);
155 dbuf_destroy(&tmpDBuf);
159 dbuf_tprintf (struct dbuf_s *dbuf, const char *szFormat,...)
162 va_start (ap, szFormat);
163 dbuf_tvprintf (dbuf, szFormat, ap);
168 tsprintf (char *buffer, size_t len, const char *szFormat,...)
174 dbuf_init(&dbuf, INITIAL_INLINEASM);
176 va_start (ap, szFormat);
177 dbuf_tvprintf (&dbuf, szFormat, ap);
180 copyLen = min(len - 1, dbuf_get_length(&dbuf));
181 memcpy(buffer, dbuf_get_buf(&dbuf), copyLen);
182 buffer[copyLen] = '\0';
187 tfprintf (FILE *fp, const char *szFormat,...)
193 dbuf_init(&dbuf, INITIAL_INLINEASM);
195 va_start (ap, szFormat);
196 dbuf_tvprintf (&dbuf, szFormat, ap);
199 len = dbuf_get_length(&dbuf);
200 fwrite(dbuf_get_buf(&dbuf), 1, len, fp);
205 asm_addTree (const ASM_MAPPINGS * pMappings)
207 const ASM_MAPPING *pMap;
209 /* Traverse down first */
210 if (pMappings->pParent)
211 asm_addTree (pMappings->pParent);
212 pMap = pMappings->pMappings;
213 while (pMap->szKey && pMap->szValue) {
214 shash_add (&_h, pMap->szKey, pMap->szValue);
219 /*-----------------------------------------------------------------*/
220 /* printILine - return the readable i-code for this ic */
221 /*-----------------------------------------------------------------*/
223 printILine (iCode *ic)
226 struct dbuf_s tmpBuf;
227 iCodeTable *icTab=getTableEntry(ic->op);
229 dbuf_init(&tmpBuf, 1024);
231 if (INLINEASM == ic->op)
232 dbuf_append (&tmpBuf, "inline", (sizeof "inline") - 1);
234 /* stuff the temporary file with the readable icode */
235 icTab->iCodePrint(&tmpBuf, ic, icTab->printName);
238 /* null terminate the buffer */
240 verbalICode = dbuf_detach(&tmpBuf);
242 /* kill the trailing NL */
243 if ('\n' == verbalICode[strlen(verbalICode) - 1])
244 verbalICode[strlen(verbalICode) - 1] = '\0';
246 /* and throw it up */
250 /*-----------------------------------------------------------------*/
251 /* printCLine - return the c-code for this lineno */
252 /*-----------------------------------------------------------------*/
255 printCLine (char *srcFile, int lineno)
257 static FILE *inFile=NULL;
258 static char inLineString[1024];
259 static int inLineNo=0;
260 static char lastSrcFile[PATH_MAX];
261 char *ilsP=inLineString;
264 if (strcmp (lastSrcFile, srcFile) != 0) {
271 inFile=fopen(srcFile, "r");
273 perror ("printCLine");
276 strncpyz (lastSrcFile, srcFile, PATH_MAX);
278 if (lineno<inLineNo) {
279 fseek (inFile, 0, SEEK_SET);
283 while (fgets (inLineString, 1024, inFile)) {
285 if (inLineNo==lineno) {
286 // remove the trailing NL
287 inLineString[strlen(inLineString)-1]='\0';
291 while (isspace ((unsigned char)*ilsP))
297 static const ASM_MAPPING _asxxxx_mapping[] =
299 {"labeldef", "%s::"},
300 {"slabeldef", "%s:"},
301 {"tlabeldef", "%05d$:"},
306 {"area", ".area %s"},
307 {"areacode", ".area %s"},
308 {"areadata", ".area %s"},
309 {"areahome", ".area %s"},
310 {"ascii", ".ascii \"%s\""},
316 {"constbyte", "0x%02X"},
317 {"constword", "0x%04X"},
318 {"immedword", "#0x%04X"},
319 {"immedbyte", "#0x%02X"},
320 {"hashedstr", "#%s"},
321 {"lsbimmeds", "#<%s"},
322 {"msbimmeds", "#>%s"},
323 {"module", ".module %s"},
324 {"global", ".globl %s"},
327 "; ---------------------------------\n"
329 "; ---------------------------------"
331 {"functionlabeldef", "%s:"},
332 {"bankimmeds", "0 ; PENDING: bank support"},
333 {"los","(%s & 0xFF)"},
335 {"hihis","(%s >> 16)"},
336 {"hihihis","(%s >> 24)"},
337 {"lod","(%d & 0xFF)"},
339 {"hihid","(%d >> 16)"},
340 {"hihihid","(%d >> 24)"},
341 {"lol","(%05d$ & 0xFF)"},
342 {"hil","(%05d$ >> 8)"},
343 {"hihil","(%05d$ >> 16)"},
344 {"hihihil","(%05d$ >> 24)"},
346 {"org", ".org 0x%04X"},
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"},
378 {"extern", ".globl %s"},
381 "; ---------------------------------\n"
383 "; ---------------------------------"
385 {"functionlabeldef", "%s:"},
386 {"bankimmeds", "0 ; PENDING: bank support"},
390 static const ASM_MAPPING _a390_mapping[] =
393 {"slabeldef", "%s:"},
394 {"tlabeldef", "L%05d:"},
399 {"area", "; SECTION NOT SUPPORTED"},
400 {"areacode", "; SECTION NOT SUPPORTED"},
401 {"areadata", "; SECTION NOT SUPPORTED"},
402 {"areahome", "; SECTION NOT SUPPORTED"},
403 {"ascii", "db \"%s\""},
404 {"ds", "; STORAGE NOT SUPPORTED"},
406 {"dbs", "db \"%s\""},
409 {"constbyte", "0%02xh"},
410 {"constword", "0%04xh"},
411 {"immedword", "#0%04Xh"},
412 {"immedbyte", "#0%02Xh"},
413 {"hashedstr", "#%s"},
414 {"lsbimmeds", "#<%s"},
415 {"msbimmeds", "#>%s"},
416 {"module", "; .file \"%s.c\""},
417 {"global", "; .globl %s"},
420 "; ---------------------------------\n"
422 "; ---------------------------------"
424 {"functionlabeldef", "%s:"},
425 {"bankimmeds", "0 ; PENDING: bank support"},
426 {"los","(%s & 0FFh)"},
427 {"his","((%s / 256) & 0FFh)"},
428 {"hihis","((%s / 65536) & 0FFh)"},
429 {"hihihis","((%s / 16777216) & 0FFh)"},
430 {"lod","(%d & 0FFh)"},
431 {"hid","((%d / 256) & 0FFh)"},
432 {"hihid","((%d / 65536) & 0FFh)"},
433 {"hihihid","((%d / 16777216) & 0FFh)"},
434 {"lol","(L%05d & 0FFh)"},
435 {"hil","((L%05d / 256) & 0FFh)"},
436 {"hihil","((L%05d / 65536) & 0FFh)"},
437 {"hihihil","((L%09d / 16777216) & 0FFh)"},
439 {"org", ".org 0x%04X"},
443 static const ASM_MAPPING _xa_asm_mapping[] =
446 {"slabeldef", "%s:"},
447 {"tlabeldef", "L%05d:"},
452 {"area", ".area %s"},
453 {"areacode", ".area %s"},
454 {"areadata", ".area %s"},
455 {"areahome", ".area %s"},
456 {"ascii", ".db \"%s\""},
459 {"dbs", ".db \"%s\""},
462 {"constbyte", "0x%02x"},
463 {"constword", "0x%04x"},
464 {"immedword", "0x%04x"},
465 {"immedbyte", "0x%02x"},
466 {"hashedstr", "#%s"},
467 {"lsbimmeds", "#<%s"},
468 {"msbimmeds", "#>%s"},
469 {"module", "; .module %s"},
470 {"global", ".globl %s"},
473 "; ---------------------------------\n"
475 "; ---------------------------------"
477 {"functionlabeldef", "%s:"},
478 {"bankimmeds", "0 ; PENDING: bank support"},
479 {"los","(%s & 0FFh)"},
480 {"his","((%s / 256) & 0FFh)"},
481 {"hihis","((%s / 65536) & 0FFh)"},
482 {"hihihis","((%s / 16777216) & 0FFh)"},
483 {"lod","(%d & 0FFh)"},
484 {"hid","((%d / 256) & 0FFh)"},
485 {"hihid","((%d / 65536) & 0FFh)"},
486 {"hihihid","((%d / 16777216) & 0FFh)"},
487 {"lol","(L%05d & 0FFh)"},
488 {"hil","((L%05d / 256) & 0FFh)"},
489 {"hihil","((L%05d / 65536) & 0FFh)"},
490 {"hihihil","((L%09d / 16777216) & 0FFh)"},
495 const ASM_MAPPINGS asm_asxxxx_mapping =
501 const ASM_MAPPINGS asm_gas_mapping =
507 const ASM_MAPPINGS asm_a390_mapping =
513 const ASM_MAPPINGS asm_xa_asm_mapping =