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 /* A 'token' is like !blah or %24f and is under the programmers
14 #define MAX_TOKEN_LEN 64
19 FileBaseName (char *fileFullName)
21 char *p = fileFullName;
29 if ((*fileFullName == '/') || (*fileFullName == '\\') || (*fileFullName == ':'))
40 _findMapping (const char *szKey)
42 return shash_find (_h, szKey);
45 // Append a string onto another, and update the pointer to the end of
48 _appendAt (char *at, char *onto, const char *sz)
50 wassert (at && onto && sz);
52 return at + strlen (sz);
56 tvsprintf (char *buffer, const char *format, va_list ap)
58 // Under Linux PPC va_list is a structure instead of a primitive type,
59 // and doesnt like being passed around. This version turns everything
64 // %[CIFN] - special formats with no argument (ie list isnt touched)
65 // All of the system formats
67 // This is acheived by expanding the tokens and zero arg formats into
68 // one big format string, which is passed to the native printf.
70 char noTokens[INITIAL_INLINEASM];
71 char newFormat[INITIAL_INLINEASM];
72 char *pInto = noTokens;
74 char token[MAX_TOKEN_LEN];
75 const char *sz = format;
77 // NULL terminate it to let strlen work.
80 /* First pass: expand all of the macros */
85 /* Start of a token. Search until the first
86 [non alpha, *] and call it a token. */
90 while (isalpha (*sz) || *sz == '*')
95 /* Now find the token in the token list */
96 if ((t = _findMapping (token)))
98 pInto = _appendAt (pInto, noTokens, t);
102 fprintf (stderr, "Cant find token \"%s\"\n", token);
114 /* Second pass: Expand any macros that we own */
122 // See if its one that we handle.
127 // Code segment name.
128 pInto = _appendAt (pInto, newFormat, CODE_NAME);
133 pInto = _appendAt (pInto, newFormat, fullSrcFileName);
137 // Current function name.
138 pInto = _appendAt (pInto, newFormat, currFunc->rname);
145 sprintf (id, "%u", ++count);
146 pInto = _appendAt (pInto, newFormat, id);
151 // Not one of ours. Copy until the end.
153 while (!isalpha (*sz))
168 // Now do the actual printing
169 vsprintf (buffer, newFormat, ap);
173 tfprintf (FILE * fp, const char *szFormat,...)
176 char buffer[INITIAL_INLINEASM];
178 va_start (ap, szFormat);
179 tvsprintf (buffer, szFormat, ap);
184 tsprintf (char *buffer, const char *szFormat,...)
187 va_start (ap, szFormat);
188 tvsprintf (buffer, szFormat, ap);
192 asm_addTree (const ASM_MAPPINGS * pMappings)
194 const ASM_MAPPING *pMap;
196 /* Traverse down first */
197 if (pMappings->pParent)
198 asm_addTree (pMappings->pParent);
199 pMap = pMappings->pMappings;
200 while (pMap->szKey && pMap->szValue) {
201 shash_add (&_h, pMap->szKey, pMap->szValue);
206 /*-----------------------------------------------------------------*/
207 /* printCLine - try to find the c-code for this lineno */
208 /*-----------------------------------------------------------------*/
209 static FILE *inFile=NULL;
210 static char inLineString[1024];
211 static int inLineNo=0;
212 static char lastSrcFile[PATH_MAX];
215 char *printCLine (char *srcFile, int lineno) {
216 char *ilsP=inLineString;
219 if (strcmp (lastSrcFile, srcFile) != 0) {
225 inFile=fopen(srcFile, "r");
227 perror ("printCLine");
230 strcpy (lastSrcFile, srcFile);
232 if (lineno<inLineNo) {
233 fseek (inFile, 0, SEEK_SET);
237 while (fgets (inLineString, 1024, inFile)) {
239 if (inLineNo==lineno) {
240 // remove the trailing NL
241 inLineString[strlen(inLineString)-1]='\0';
245 while (isspace ((int)*ilsP))
251 static const ASM_MAPPING _asxxxx_mapping[] =
253 {"labeldef", "%s::"},
254 {"slabeldef", "%s:"},
255 {"tlabeldef", "%05d$:"},
260 {"area", ".area %s"},
261 {"areacode", ".area %s"},
262 {"areadata", ".area %s"},
263 {"areahome", ".area %s"},
264 {"ascii", ".ascii \"%s\""},
270 {"constbyte", "0x%02X"},
271 {"constword", "0x%04X"},
272 {"immedword", "#0x%04X"},
273 {"immedbyte", "#0x%02X"},
274 {"hashedstr", "#%s"},
275 {"lsbimmeds", "#<%s"},
276 {"msbimmeds", "#>%s"},
277 {"module", ".module %s"},
278 {"global", ".globl %s"},
281 "; ---------------------------------\n"
283 "; ---------------------------------"
285 {"functionlabeldef", "%s:"},
286 {"bankimmeds", "0 ; PENDING: bank support"},
287 {"los","(%s & 0xFF)"},
289 {"hihis","(%s >> 16)"},
290 {"hihihis","(%s >> 24)"},
291 {"lod","(%d & 0xFF)"},
293 {"hihid","(%d >> 16)"},
294 {"hihihid","(%d >> 24)"},
295 {"lol","(%05d$ & 0xFF)"},
296 {"hil","(%05d$ >> 8)"},
297 {"hihil","(%05d$ >> 16)"},
298 {"hihihil","(%05d$ >> 24)"},
303 static const ASM_MAPPING _gas_mapping[] =
305 {"labeldef", "%s::"},
306 {"slabeldef", "%s:"},
307 {"tlabeldef", "%05d$:"},
312 {"area", ".section %s"},
313 {"areacode", ".section %s"},
314 {"areadata", ".section %s"},
315 {"areahome", ".section %s"},
316 {"ascii", ".ascii \"%s\""},
322 {"constbyte", "0x%02X"},
323 {"constword", "0x%04X"},
324 {"immedword", "#0x%04X"},
325 {"immedbyte", "#0x%02X"},
326 {"hashedstr", "#%s"},
327 {"lsbimmeds", "#<%s"},
328 {"msbimmeds", "#>%s"},
329 {"module", ".file \"%s.c\""},
330 {"global", ".globl %s"},
331 {"extern", ".globl %s"},
334 "; ---------------------------------\n"
336 "; ---------------------------------"
338 {"functionlabeldef", "%s:"},
339 {"bankimmeds", "0 ; PENDING: bank support"},
343 static const ASM_MAPPING _a390_mapping[] =
346 {"slabeldef", "%s:"},
347 {"tlabeldef", "L%05d:"},
352 {"area", "; SECTION NOT SUPPORTED"},
353 {"areacode", "; SECTION NOT SUPPORTED"},
354 {"areadata", "; SECTION NOT SUPPORTED"},
355 {"areahome", "; SECTION NOT SUPPORTED"},
356 {"ascii", "db \"%s\""},
357 {"ds", "; STORAGE NOT SUPPORTED"},
359 {"dbs", "db \"%s\""},
362 {"constbyte", "0%02xh"},
363 {"constword", "0%04xh"},
364 {"immedword", "#0%04Xh"},
365 {"immedbyte", "#0%02Xh"},
366 {"hashedstr", "#%s"},
367 {"lsbimmeds", "#<%s"},
368 {"msbimmeds", "#>%s"},
369 {"module", "; .file \"%s.c\""},
370 {"global", "; .globl %s"},
373 "; ---------------------------------\n"
375 "; ---------------------------------"
377 {"functionlabeldef", "%s:"},
378 {"bankimmeds", "0 ; PENDING: bank support"},
379 {"los","(%s & 0FFh)"},
380 {"his","((%s / 256) & 0FFh)"},
381 {"hihis","((%s / 65536) & 0FFh)"},
382 {"hihihis","((%s / 16777216) & 0FFh)"},
383 {"lod","(%d & 0FFh)"},
384 {"hid","((%d / 256) & 0FFh)"},
385 {"hihid","((%d / 65536) & 0FFh)"},
386 {"hihihid","((%d / 16777216) & 0FFh)"},
387 {"lol","(L%05d & 0FFh)"},
388 {"hil","((L%05d / 256) & 0FFh)"},
389 {"hihil","((L%05d / 65536) & 0FFh)"},
390 {"hihihil","((L%09d / 16777216) & 0FFh)"},
395 static const ASM_MAPPING _xa_asm_mapping[] =
398 {"slabeldef", "%s:"},
399 {"tlabeldef", "L%05d:"},
404 {"area", ".area %s"},
405 {"areacode", ".area %s"},
406 {"areadata", ".area %s"},
407 {"areahome", ".area %s"},
408 {"ascii", ".db \"%s\""},
411 {"dbs", ".db \"%s\""},
414 {"constbyte", "0x%02x"},
415 {"constword", "0x%04x"},
416 {"immedword", "0x%04x"},
417 {"immedbyte", "0x%02x"},
418 {"hashedstr", "#%s"},
419 {"lsbimmeds", "#<%s"},
420 {"msbimmeds", "#>%s"},
421 {"module", "; .module %s"},
422 {"global", ".globl %s"},
425 "; ---------------------------------\n"
427 "; ---------------------------------"
429 {"functionlabeldef", "%s:"},
430 {"bankimmeds", "0 ; PENDING: bank support"},
431 {"los","(%s & 0FFh)"},
432 {"his","((%s / 256) & 0FFh)"},
433 {"hihis","((%s / 65536) & 0FFh)"},
434 {"hihihis","((%s / 16777216) & 0FFh)"},
435 {"lod","(%d & 0FFh)"},
436 {"hid","((%d / 256) & 0FFh)"},
437 {"hihid","((%d / 65536) & 0FFh)"},
438 {"hihihid","((%d / 16777216) & 0FFh)"},
439 {"lol","(L%05d & 0FFh)"},
440 {"hil","((L%05d / 256) & 0FFh)"},
441 {"hihil","((L%05d / 65536) & 0FFh)"},
442 {"hihihil","((L%09d / 16777216) & 0FFh)"},
447 const ASM_MAPPINGS asm_asxxxx_mapping =
453 const ASM_MAPPINGS asm_gas_mapping =
459 const ASM_MAPPINGS asm_a390_mapping =
465 const ASM_MAPPINGS asm_xa_asm_mapping =