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, srcFileName);
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;
214 char *printCLine (char *srcFile, int lineno) {
215 char *ilsP=inLineString;
217 inFile=fopen(srcFile, "r");
219 perror ("printCLine");
223 if (lineno<inLineNo) {
224 fseek (inFile, 0, SEEK_SET);
228 while (fgets (inLineString, 1024, inFile)) {
230 if (inLineNo==lineno) {
231 // remove the trailing NL
232 inLineString[strlen(inLineString)-1]='\0';
236 while (isspace ((int)*ilsP))
242 static const ASM_MAPPING _asxxxx_mapping[] =
244 {"labeldef", "%s::"},
245 {"slabeldef", "%s:"},
246 {"tlabeldef", "%05d$:"},
251 {"area", ".area %s"},
252 {"areacode", ".area %s"},
253 {"areadata", ".area %s"},
254 {"areahome", ".area %s"},
255 {"ascii", ".ascii \"%s\""},
261 {"constbyte", "0x%02X"},
262 {"constword", "0x%04X"},
263 {"immedword", "#0x%04X"},
264 {"immedbyte", "#0x%02X"},
265 {"hashedstr", "#%s"},
266 {"lsbimmeds", "#<%s"},
267 {"msbimmeds", "#>%s"},
268 {"module", ".module %s"},
269 {"global", ".globl %s"},
272 "; ---------------------------------\n"
274 "; ---------------------------------"
276 {"functionlabeldef", "%s:"},
277 {"bankimmeds", "0 ; PENDING: bank support"},
278 {"los","(%s & 0xFF)"},
280 {"hihis","(%s >> 16)"},
281 {"hihihis","(%s >> 24)"},
282 {"lod","(%d & 0xFF)"},
284 {"hihid","(%d >> 16)"},
285 {"hihihid","(%d >> 24)"},
286 {"lol","(%05d$ & 0xFF)"},
287 {"hil","(%05d$ >> 8)"},
288 {"hihil","(%05d$ >> 16)"},
289 {"hihihil","(%05d$ >> 24)"},
294 static const ASM_MAPPING _gas_mapping[] =
296 {"labeldef", "%s::"},
297 {"slabeldef", "%s:"},
298 {"tlabeldef", "%05d$:"},
303 {"area", ".section %s"},
304 {"areacode", ".section %s"},
305 {"areadata", ".section %s"},
306 {"areahome", ".section %s"},
307 {"ascii", ".ascii \"%s\""},
313 {"constbyte", "0x%02X"},
314 {"constword", "0x%04X"},
315 {"immedword", "#0x%04X"},
316 {"immedbyte", "#0x%02X"},
317 {"hashedstr", "#%s"},
318 {"lsbimmeds", "#<%s"},
319 {"msbimmeds", "#>%s"},
320 {"module", ".file \"%s.c\""},
321 {"global", ".globl %s"},
322 {"extern", ".globl %s"},
325 "; ---------------------------------\n"
327 "; ---------------------------------"
329 {"functionlabeldef", "%s:"},
330 {"bankimmeds", "0 ; PENDING: bank support"},
334 static const ASM_MAPPING _a390_mapping[] =
337 {"slabeldef", "%s:"},
338 {"tlabeldef", "L%05d:"},
343 {"area", "; SECTION NOT SUPPORTED"},
344 {"areacode", "; SECTION NOT SUPPORTED"},
345 {"areadata", "; SECTION NOT SUPPORTED"},
346 {"areahome", "; SECTION NOT SUPPORTED"},
347 {"ascii", "db \"%s\""},
348 {"ds", "; STORAGE NOT SUPPORTED"},
350 {"dbs", "db \"%s\""},
353 {"constbyte", "0%02xh"},
354 {"constword", "0%04xh"},
355 {"immedword", "#0%04Xh"},
356 {"immedbyte", "#0%02Xh"},
357 {"hashedstr", "#%s"},
358 {"lsbimmeds", "#<%s"},
359 {"msbimmeds", "#>%s"},
360 {"module", "; .file \"%s.c\""},
361 {"global", "; .globl %s"},
364 "; ---------------------------------\n"
366 "; ---------------------------------"
368 {"functionlabeldef", "%s:"},
369 {"bankimmeds", "0 ; PENDING: bank support"},
370 {"los","(%s & 0FFh)"},
371 {"his","((%s / 256) & 0FFh)"},
372 {"hihis","((%s / 65536) & 0FFh)"},
373 {"hihihis","((%s / 16777216) & 0FFh)"},
374 {"lod","(%d & 0FFh)"},
375 {"hid","((%d / 256) & 0FFh)"},
376 {"hihid","((%d / 65536) & 0FFh)"},
377 {"hihihid","((%d / 16777216) & 0FFh)"},
378 {"lol","(L%05d & 0FFh)"},
379 {"hil","((L%05d / 256) & 0FFh)"},
380 {"hihil","((L%05d / 65536) & 0FFh)"},
381 {"hihihil","((L%09d / 16777216) & 0FFh)"},
386 static const ASM_MAPPING _xa_asm_mapping[] =
389 {"slabeldef", "%s:"},
390 {"tlabeldef", "L%05d:"},
395 {"area", ".area %s"},
396 {"areacode", ".area %s"},
397 {"areadata", ".area %s"},
398 {"areahome", ".area %s"},
399 {"ascii", ".db \"%s\""},
402 {"dbs", ".db \"%s\""},
405 {"constbyte", "0x%02x"},
406 {"constword", "0x%04x"},
407 {"immedword", "0x%04x"},
408 {"immedbyte", "0x%02x"},
409 {"hashedstr", "#%s"},
410 {"lsbimmeds", "#<%s"},
411 {"msbimmeds", "#>%s"},
412 {"module", "; .module %s"},
413 {"global", ".globl %s"},
416 "; ---------------------------------\n"
418 "; ---------------------------------"
420 {"functionlabeldef", "%s:"},
421 {"bankimmeds", "0 ; PENDING: bank support"},
422 {"los","(%s & 0FFh)"},
423 {"his","((%s / 256) & 0FFh)"},
424 {"hihis","((%s / 65536) & 0FFh)"},
425 {"hihihis","((%s / 16777216) & 0FFh)"},
426 {"lod","(%d & 0FFh)"},
427 {"hid","((%d / 256) & 0FFh)"},
428 {"hihid","((%d / 65536) & 0FFh)"},
429 {"hihihid","((%d / 16777216) & 0FFh)"},
430 {"lol","(L%05d & 0FFh)"},
431 {"hil","((L%05d / 256) & 0FFh)"},
432 {"hihil","((L%05d / 65536) & 0FFh)"},
433 {"hihihil","((L%09d / 16777216) & 0FFh)"},
438 const ASM_MAPPINGS asm_asxxxx_mapping =
444 const ASM_MAPPINGS asm_gas_mapping =
450 const ASM_MAPPINGS asm_a390_mapping =
456 const ASM_MAPPINGS asm_xa_asm_mapping =