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 #include "dbuf_string.h"
15 /* A 'token' is like !blah or %24f and is under the programmers
17 #define MAX_TOKEN_LEN 64
22 FileBaseName (char *fileFullName)
24 char *p = fileFullName;
32 if ((*fileFullName == '/') || (*fileFullName == '\\') || (*fileFullName == ':'))
43 dbuf_tvprintf (struct dbuf_s *dbuf, const char *format, va_list ap)
45 // Under Linux PPC va_list is a structure instead of a primitive type,
46 // and doesnt like being passed around. This version turns everything
51 // %[CIFN] - special formats with no argument (ie list isnt touched)
52 // All of the system formats
54 // This is acheived by expanding the tokens and zero arg formats into
55 // one big format string, which is passed to the native printf.
57 struct dbuf_s tmpDBuf;
60 char token[MAX_TOKEN_LEN];
61 const char *sz = format;
63 dbuf_init(&tmpDBuf, INITIAL_INLINEASM);
65 /* First pass: expand all of the macros */
70 /* Start of a token. Search until the first
71 [non alpha, *] and call it a token. */
75 while (isalpha ((unsigned char)*sz) || *sz == '*')
80 /* Now find the token in the token list */
81 if ((t = shash_find (_h, token)))
83 dbuf_append_str(&tmpDBuf, t);
87 fprintf (stderr, "Cant find token \"%s\"\n", token);
93 dbuf_append_char(&tmpDBuf, *sz++);
97 /* Second pass: Expand any macros that we own */
98 noTokens = dbuf_c_str(&tmpDBuf);
101 /* recycle tmpDBuf */
102 dbuf_init(&tmpDBuf, INITIAL_INLINEASM);
108 // See if its one that we handle.
113 // Code segment name.
114 dbuf_append_str(&tmpDBuf, CODE_NAME);
120 dbuf_append_str(&tmpDBuf, fullSrcFileName);
125 // Current function name.
126 dbuf_append_str(&tmpDBuf, currFunc->rname);
132 dbuf_printf(&tmpDBuf, "%u", ++count);
137 // Not one of ours. Copy until the end.
138 dbuf_append_char(&tmpDBuf, '%');
139 while (!isalpha ((unsigned char)*sz))
140 dbuf_append_char(&tmpDBuf, *sz++);
142 dbuf_append_char(&tmpDBuf, *sz++);
147 dbuf_append_char(&tmpDBuf, *sz++);
153 sz = dbuf_c_str(&tmpDBuf);
155 dbuf_vprintf(dbuf, sz, ap);
157 dbuf_destroy(&tmpDBuf);
161 dbuf_tprintf (struct dbuf_s *dbuf, const char *szFormat,...)
164 va_start (ap, szFormat);
165 dbuf_tvprintf (dbuf, szFormat, ap);
170 tsprintf (char *buffer, size_t len, const char *szFormat,...)
176 dbuf_init(&dbuf, INITIAL_INLINEASM);
178 va_start (ap, szFormat);
179 dbuf_tvprintf (&dbuf, szFormat, ap);
182 copyLen = min(len - 1, dbuf_get_length(&dbuf));
183 memcpy(buffer, dbuf_get_buf(&dbuf), copyLen);
184 buffer[copyLen] = '\0';
189 tfprintf (FILE *fp, const char *szFormat,...)
195 dbuf_init(&dbuf, INITIAL_INLINEASM);
197 va_start (ap, szFormat);
198 dbuf_tvprintf (&dbuf, szFormat, ap);
201 len = dbuf_get_length(&dbuf);
202 fwrite(dbuf_get_buf(&dbuf), 1, len, fp);
207 asm_addTree (const ASM_MAPPINGS * pMappings)
209 const ASM_MAPPING *pMap;
211 /* Traverse down first */
212 if (pMappings->pParent)
213 asm_addTree (pMappings->pParent);
214 pMap = pMappings->pMappings;
215 while (pMap->szKey && pMap->szValue) {
216 shash_add (&_h, pMap->szKey, pMap->szValue);
221 /*-----------------------------------------------------------------*/
222 /* printILine - return the readable i-code for this ic */
223 /*-----------------------------------------------------------------*/
225 printILine (iCode *ic)
228 struct dbuf_s tmpBuf;
229 iCodeTable *icTab=getTableEntry(ic->op);
231 dbuf_init(&tmpBuf, 1024);
233 if (INLINEASM == ic->op)
234 dbuf_append (&tmpBuf, "inline", (sizeof "inline") - 1);
236 /* stuff the temporary file with the readable icode */
237 icTab->iCodePrint(&tmpBuf, ic, icTab->printName);
240 /* null terminate the buffer */
242 verbalICode = dbuf_detach(&tmpBuf);
244 /* kill the trailing NL */
245 if ('\n' == verbalICode[strlen(verbalICode) - 1])
246 verbalICode[strlen(verbalICode) - 1] = '\0';
248 /* and throw it up */
252 /*-----------------------------------------------------------------*/
253 /* printCLine - return the c-code for this lineno */
254 /*-----------------------------------------------------------------*/
257 printCLine (char *srcFile, int lineno)
259 static FILE *inFile=NULL;
260 static char inLineString[1024];
261 static int inLineNo=0;
262 static char lastSrcFile[PATH_MAX];
263 char *ilsP=inLineString;
266 if (strcmp (lastSrcFile, srcFile) != 0) {
270 strncpyz (lastSrcFile, srcFile, PATH_MAX);
273 if (!inFile && !(inFile = fopen(srcFile, "r"))) {
274 /* can't open the file:
275 don't panic, just return the error message */
276 SDCCsnprintf(inLineString, sizeof(inLineString), "ERROR: %s", strerror(errno));
279 if (lineno<inLineNo) {
280 fseek (inFile, 0, SEEK_SET);
284 while (fgets (inLineString, 1024, inFile)) {
286 if (inLineNo==lineno) {
287 // remove the trailing NL
288 inLineString[strlen(inLineString)-1]='\0';
294 while (isspace ((unsigned char)*ilsP))
300 static const ASM_MAPPING _asxxxx_mapping[] =
302 {"labeldef", "%s::"},
303 {"slabeldef", "%s:"},
304 {"tlabeldef", "%05d$:"},
309 {"area", ".area %s"},
310 {"areacode", ".area %s"},
311 {"areadata", ".area %s"},
312 {"areahome", ".area %s"},
313 {"ascii", ".ascii \"%s\""},
319 {"constbyte", "0x%02X"},
320 {"constword", "0x%04X"},
321 {"immedword", "#0x%04X"},
322 {"immedbyte", "#0x%02X"},
323 {"hashedstr", "#%s"},
324 {"lsbimmeds", "#<%s"},
325 {"msbimmeds", "#>%s"},
326 {"module", ".module %s"},
327 {"global", ".globl %s"},
330 "; ---------------------------------\n"
332 "; ---------------------------------"
334 {"functionlabeldef", "%s:"},
335 {"bankimmeds", "0 ; PENDING: bank support"},
336 {"los","(%s & 0xFF)"},
338 {"hihis","(%s >> 16)"},
339 {"hihihis","(%s >> 24)"},
340 {"lod","(%d & 0xFF)"},
342 {"hihid","(%d >> 16)"},
343 {"hihihid","(%d >> 24)"},
344 {"lol","(%05d$ & 0xFF)"},
345 {"hil","(%05d$ >> 8)"},
346 {"hihil","(%05d$ >> 16)"},
347 {"hihihil","(%05d$ >> 24)"},
349 {"org", ".org 0x%04X"},
353 static const ASM_MAPPING _gas_mapping[] =
355 {"labeldef", "%s::"},
356 {"slabeldef", "%s:"},
357 {"tlabeldef", "%05d$:"},
362 {"area", ".section %s"},
363 {"areacode", ".section %s"},
364 {"areadata", ".section %s"},
365 {"areahome", ".section %s"},
366 {"ascii", ".ascii \"%s\""},
372 {"constbyte", "0x%02X"},
373 {"constword", "0x%04X"},
374 {"immedword", "#0x%04X"},
375 {"immedbyte", "#0x%02X"},
376 {"hashedstr", "#%s"},
377 {"lsbimmeds", "#<%s"},
378 {"msbimmeds", "#>%s"},
379 {"module", ".file \"%s.c\""},
380 {"global", ".globl %s"},
381 {"extern", ".globl %s"},
384 "; ---------------------------------\n"
386 "; ---------------------------------"
388 {"functionlabeldef", "%s:"},
389 {"bankimmeds", "0 ; PENDING: bank support"},
393 static const ASM_MAPPING _a390_mapping[] =
396 {"slabeldef", "%s:"},
397 {"tlabeldef", "L%05d:"},
402 {"area", "; SECTION NOT SUPPORTED"},
403 {"areacode", "; SECTION NOT SUPPORTED"},
404 {"areadata", "; SECTION NOT SUPPORTED"},
405 {"areahome", "; SECTION NOT SUPPORTED"},
406 {"ascii", "db \"%s\""},
407 {"ds", "; STORAGE NOT SUPPORTED"},
409 {"dbs", "db \"%s\""},
412 {"constbyte", "0%02xh"},
413 {"constword", "0%04xh"},
414 {"immedword", "#0%04Xh"},
415 {"immedbyte", "#0%02Xh"},
416 {"hashedstr", "#%s"},
417 {"lsbimmeds", "#<%s"},
418 {"msbimmeds", "#>%s"},
419 {"module", "; .file \"%s.c\""},
420 {"global", "; .globl %s"},
423 "; ---------------------------------\n"
425 "; ---------------------------------"
427 {"functionlabeldef", "%s:"},
428 {"bankimmeds", "0 ; PENDING: bank support"},
429 {"los","(%s & 0FFh)"},
430 {"his","((%s / 256) & 0FFh)"},
431 {"hihis","((%s / 65536) & 0FFh)"},
432 {"hihihis","((%s / 16777216) & 0FFh)"},
433 {"lod","(%d & 0FFh)"},
434 {"hid","((%d / 256) & 0FFh)"},
435 {"hihid","((%d / 65536) & 0FFh)"},
436 {"hihihid","((%d / 16777216) & 0FFh)"},
437 {"lol","(L%05d & 0FFh)"},
438 {"hil","((L%05d / 256) & 0FFh)"},
439 {"hihil","((L%05d / 65536) & 0FFh)"},
440 {"hihihil","((L%09d / 16777216) & 0FFh)"},
442 {"org", ".org 0x%04X"},
446 static const ASM_MAPPING _xa_asm_mapping[] =
449 {"slabeldef", "%s:"},
450 {"tlabeldef", "L%05d:"},
455 {"area", ".area %s"},
456 {"areacode", ".area %s"},
457 {"areadata", ".area %s"},
458 {"areahome", ".area %s"},
459 {"ascii", ".db \"%s\""},
462 {"dbs", ".db \"%s\""},
465 {"constbyte", "0x%02x"},
466 {"constword", "0x%04x"},
467 {"immedword", "0x%04x"},
468 {"immedbyte", "0x%02x"},
469 {"hashedstr", "#%s"},
470 {"lsbimmeds", "#<%s"},
471 {"msbimmeds", "#>%s"},
472 {"module", "; .module %s"},
473 {"global", ".globl %s"},
476 "; ---------------------------------\n"
478 "; ---------------------------------"
480 {"functionlabeldef", "%s:"},
481 {"bankimmeds", "0 ; PENDING: bank support"},
482 {"los","(%s & 0FFh)"},
483 {"his","((%s / 256) & 0FFh)"},
484 {"hihis","((%s / 65536) & 0FFh)"},
485 {"hihihis","((%s / 16777216) & 0FFh)"},
486 {"lod","(%d & 0FFh)"},
487 {"hid","((%d / 256) & 0FFh)"},
488 {"hihid","((%d / 65536) & 0FFh)"},
489 {"hihihid","((%d / 16777216) & 0FFh)"},
490 {"lol","(L%05d & 0FFh)"},
491 {"hil","((L%05d / 256) & 0FFh)"},
492 {"hihil","((L%05d / 65536) & 0FFh)"},
493 {"hihihil","((L%09d / 16777216) & 0FFh)"},
498 const ASM_MAPPINGS asm_asxxxx_mapping =
504 const ASM_MAPPINGS asm_gas_mapping =
510 const ASM_MAPPINGS asm_a390_mapping =
516 const ASM_MAPPINGS asm_xa_asm_mapping =