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) {
226 inFile=fopen(srcFile, "r");
228 perror ("printCLine");
231 strcpy (lastSrcFile, srcFile);
233 if (lineno<inLineNo) {
234 fseek (inFile, 0, SEEK_SET);
238 while (fgets (inLineString, 1024, inFile)) {
240 if (inLineNo==lineno) {
241 // remove the trailing NL
242 inLineString[strlen(inLineString)-1]='\0';
246 while (isspace ((int)*ilsP))
252 static const ASM_MAPPING _asxxxx_mapping[] =
254 {"labeldef", "%s::"},
255 {"slabeldef", "%s:"},
256 {"tlabeldef", "%05d$:"},
261 {"area", ".area %s"},
262 {"areacode", ".area %s"},
263 {"areadata", ".area %s"},
264 {"areahome", ".area %s"},
265 {"ascii", ".ascii \"%s\""},
271 {"constbyte", "0x%02X"},
272 {"constword", "0x%04X"},
273 {"immedword", "#0x%04X"},
274 {"immedbyte", "#0x%02X"},
275 {"hashedstr", "#%s"},
276 {"lsbimmeds", "#<%s"},
277 {"msbimmeds", "#>%s"},
278 {"module", ".module %s"},
279 {"global", ".globl %s"},
282 "; ---------------------------------\n"
284 "; ---------------------------------"
286 {"functionlabeldef", "%s:"},
287 {"bankimmeds", "0 ; PENDING: bank support"},
288 {"los","(%s & 0xFF)"},
290 {"hihis","(%s >> 16)"},
291 {"hihihis","(%s >> 24)"},
292 {"lod","(%d & 0xFF)"},
294 {"hihid","(%d >> 16)"},
295 {"hihihid","(%d >> 24)"},
296 {"lol","(%05d$ & 0xFF)"},
297 {"hil","(%05d$ >> 8)"},
298 {"hihil","(%05d$ >> 16)"},
299 {"hihihil","(%05d$ >> 24)"},
304 static const ASM_MAPPING _gas_mapping[] =
306 {"labeldef", "%s::"},
307 {"slabeldef", "%s:"},
308 {"tlabeldef", "%05d$:"},
313 {"area", ".section %s"},
314 {"areacode", ".section %s"},
315 {"areadata", ".section %s"},
316 {"areahome", ".section %s"},
317 {"ascii", ".ascii \"%s\""},
323 {"constbyte", "0x%02X"},
324 {"constword", "0x%04X"},
325 {"immedword", "#0x%04X"},
326 {"immedbyte", "#0x%02X"},
327 {"hashedstr", "#%s"},
328 {"lsbimmeds", "#<%s"},
329 {"msbimmeds", "#>%s"},
330 {"module", ".file \"%s.c\""},
331 {"global", ".globl %s"},
332 {"extern", ".globl %s"},
335 "; ---------------------------------\n"
337 "; ---------------------------------"
339 {"functionlabeldef", "%s:"},
340 {"bankimmeds", "0 ; PENDING: bank support"},
344 static const ASM_MAPPING _a390_mapping[] =
347 {"slabeldef", "%s:"},
348 {"tlabeldef", "L%05d:"},
353 {"area", "; SECTION NOT SUPPORTED"},
354 {"areacode", "; SECTION NOT SUPPORTED"},
355 {"areadata", "; SECTION NOT SUPPORTED"},
356 {"areahome", "; SECTION NOT SUPPORTED"},
357 {"ascii", "db \"%s\""},
358 {"ds", "; STORAGE NOT SUPPORTED"},
360 {"dbs", "db \"%s\""},
363 {"constbyte", "0%02xh"},
364 {"constword", "0%04xh"},
365 {"immedword", "#0%04Xh"},
366 {"immedbyte", "#0%02Xh"},
367 {"hashedstr", "#%s"},
368 {"lsbimmeds", "#<%s"},
369 {"msbimmeds", "#>%s"},
370 {"module", "; .file \"%s.c\""},
371 {"global", "; .globl %s"},
374 "; ---------------------------------\n"
376 "; ---------------------------------"
378 {"functionlabeldef", "%s:"},
379 {"bankimmeds", "0 ; PENDING: bank support"},
380 {"los","(%s & 0FFh)"},
381 {"his","((%s / 256) & 0FFh)"},
382 {"hihis","((%s / 65536) & 0FFh)"},
383 {"hihihis","((%s / 16777216) & 0FFh)"},
384 {"lod","(%d & 0FFh)"},
385 {"hid","((%d / 256) & 0FFh)"},
386 {"hihid","((%d / 65536) & 0FFh)"},
387 {"hihihid","((%d / 16777216) & 0FFh)"},
388 {"lol","(L%05d & 0FFh)"},
389 {"hil","((L%05d / 256) & 0FFh)"},
390 {"hihil","((L%05d / 65536) & 0FFh)"},
391 {"hihihil","((L%09d / 16777216) & 0FFh)"},
396 static const ASM_MAPPING _xa_asm_mapping[] =
399 {"slabeldef", "%s:"},
400 {"tlabeldef", "L%05d:"},
405 {"area", ".area %s"},
406 {"areacode", ".area %s"},
407 {"areadata", ".area %s"},
408 {"areahome", ".area %s"},
409 {"ascii", ".db \"%s\""},
412 {"dbs", ".db \"%s\""},
415 {"constbyte", "0x%02x"},
416 {"constword", "0x%04x"},
417 {"immedword", "0x%04x"},
418 {"immedbyte", "0x%02x"},
419 {"hashedstr", "#%s"},
420 {"lsbimmeds", "#<%s"},
421 {"msbimmeds", "#>%s"},
422 {"module", "; .module %s"},
423 {"global", ".globl %s"},
426 "; ---------------------------------\n"
428 "; ---------------------------------"
430 {"functionlabeldef", "%s:"},
431 {"bankimmeds", "0 ; PENDING: bank support"},
432 {"los","(%s & 0FFh)"},
433 {"his","((%s / 256) & 0FFh)"},
434 {"hihis","((%s / 65536) & 0FFh)"},
435 {"hihihis","((%s / 16777216) & 0FFh)"},
436 {"lod","(%d & 0FFh)"},
437 {"hid","((%d / 256) & 0FFh)"},
438 {"hihid","((%d / 65536) & 0FFh)"},
439 {"hihihid","((%d / 16777216) & 0FFh)"},
440 {"lol","(L%05d & 0FFh)"},
441 {"hil","((L%05d / 256) & 0FFh)"},
442 {"hihil","((L%05d / 65536) & 0FFh)"},
443 {"hihihil","((L%09d / 16777216) & 0FFh)"},
448 const ASM_MAPPINGS asm_asxxxx_mapping =
454 const ASM_MAPPINGS asm_gas_mapping =
460 const ASM_MAPPINGS asm_a390_mapping =
466 const ASM_MAPPINGS asm_xa_asm_mapping =