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
18 char * FileBaseName(char * fileFullName)
20 char * p = fileFullName;
22 while (*fileFullName) {
23 if((*fileFullName=='/')||(*fileFullName=='\\')||(*fileFullName==':')) {
32 static const char *_findMapping(const char *szKey)
34 return shash_find(_h, szKey);
38 static void _iprintf(char *pInto, const char *sz, va_list *pap)
40 char format[MAX_TOKEN_LEN];
47 /* See if it's a special emitter */
51 /* Name of the code segment */
53 strcpy(pInto, CODE_NAME);
54 pInto = pStart + strlen(pStart);
58 strcpy(pInto, srcFileName);
59 pInto = pStart + strlen(pStart);
63 sprintf(pInto, "%u", ++count);
64 pInto = pStart + strlen(pStart);
69 /* Scan out the arg and pass it on to sprintf */
76 vsprintf(pInto, format, *pap);
77 /* PENDING: Assume that the arg length was an int */
78 (void)va_arg(*pap, int);
81 pInto = pStart + strlen(pStart);
90 void tvsprintf(char *buffer, const char *sz, va_list ap)
94 char token[MAX_TOKEN_LEN];
100 /* Start of a token. Search until the first
101 [non alplha, *] and call it a token. */
105 while (isalpha(*sz) || *sz == '*') {
109 /* Now find the token in the token list */
110 if ((t = _findMapping(token))) {
111 printf("tvsprintf: found token \"%s\" to \"%s\"\n", token, t);
112 _iprintf(pInto, t, &ap);
113 pInto = buffer + strlen(buffer);
116 fprintf(stderr, "Cant find token \"%s\"\n", token);
120 else if (*sz == '%') {
123 while (!isalpha(*sz)) {
128 vsprintf(pInto, token, ap);
129 pInto = buffer + strlen(buffer);
130 (void)va_arg(ap, int);
139 // Append a string onto another, and update the pointer to the end of
141 static char *_appendAt(char *at, char *onto, const char *sz)
143 wassert(at && onto && sz);
145 return at + strlen(sz);
148 void tvsprintf(char *buffer, const char *format, va_list ap)
150 // Under Linux PPC va_list is a structure instead of a primitive type,
151 // and doesnt like being passed around. This version turns everything
152 // into one function.
156 // %[CIF] - special formats with no argument (ie list isnt touched)
157 // All of the system formats
159 // This is acheived by expanding the tokens and zero arg formats into
160 // one big format string, which is passed to the native printf.
162 char newformat[MAX_INLINEASM];
163 char *pInto = newformat;
165 char token[MAX_TOKEN_LEN];
166 const char *sz = format;
168 // NULL terminate it to let strlen work.
173 /* Start of a token. Search until the first
174 [non alpha, *] and call it a token. */
178 while (isalpha(*sz) || *sz == '*') {
182 /* Now find the token in the token list */
183 if ((t = _findMapping(token))) {
184 pInto = _appendAt(pInto, newformat, t);
187 fprintf(stderr, "Cant find token \"%s\"\n", token);
191 else if (*sz == '%') {
192 // See if its one that we handle.
196 // Code segment name.
197 pInto = _appendAt(pInto, newformat, CODE_NAME);
201 pInto = _appendAt(pInto, newformat, srcFileName);
206 sprintf(id, "%u", ++count);
207 pInto = _appendAt(pInto, newformat, id);
211 // Not one of ours. Copy until the end.
213 while (!isalpha(*sz)) {
225 // Now do the actual printing
226 vsprintf(buffer, newformat, ap);
230 void tfprintf(FILE *fp, const char *szFormat, ...)
233 char buffer[MAX_INLINEASM];
235 va_start(ap, szFormat);
236 tvsprintf(buffer, szFormat, ap);
240 void tsprintf(char *buffer, const char *szFormat, ...)
243 va_start(ap, szFormat);
244 tvsprintf(buffer, szFormat, ap);
247 void asm_addTree(const ASM_MAPPINGS *pMappings)
249 const ASM_MAPPING *pMap;
250 /* Traverse down first */
251 if (pMappings->pParent)
252 asm_addTree(pMappings->pParent);
253 pMap = pMappings->pMappings;
254 while (pMap->szKey && pMap->szValue) {
255 shash_add(&_h, pMap->szKey, pMap->szValue);
260 static const ASM_MAPPING _asxxxx_mapping[] = {
261 { "labeldef", "%s::" },
262 { "slabeldef", "%s:" },
263 { "tlabeldef", "%05d$:" },
264 { "tlabel", "%05d$" },
268 { "area", ".area %s" },
269 { "areacode", ".area %s" },
270 { "areadata", ".area %s" },
271 { "areahome", ".area %s" },
272 { "ascii", ".ascii \"%s\"" },
278 { "constbyte", "0x%02X" },
279 { "constword", "0x%04X" },
280 { "immedword", "#0x%04X" },
281 { "immedbyte", "#0x%02X" },
282 { "hashedstr", "#%s" },
283 { "lsbimmeds", "#<%s" },
284 { "msbimmeds", "#>%s" },
285 { "module", ".module %s" },
286 { "global", ".globl %s" },
287 { "fileprelude", "" },
289 "; ---------------------------------\n"
291 "; ---------------------------------"
293 { "functionlabeldef", "%s:" },
294 { "bankimmeds", "0 ; PENDING: bank support" },
298 const ASM_MAPPINGS asm_asxxxx_mapping = {