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 static const char *_findMapping(const char *szKey)
20 return shash_find(_h, szKey);
24 static void _iprintf(char *pInto, const char *sz, va_list *pap)
26 char format[MAX_TOKEN_LEN];
33 /* See if it's a special emitter */
37 /* Name of the code segment */
39 strcpy(pInto, CODE_NAME);
40 pInto = pStart + strlen(pStart);
44 strcpy(pInto, srcFileName);
45 pInto = pStart + strlen(pStart);
49 sprintf(pInto, "%u", ++count);
50 pInto = pStart + strlen(pStart);
55 /* Scan out the arg and pass it on to sprintf */
62 vsprintf(pInto, format, *pap);
63 /* PENDING: Assume that the arg length was an int */
64 (void)va_arg(*pap, int);
67 pInto = pStart + strlen(pStart);
76 void tvsprintf(char *buffer, const char *sz, va_list ap)
80 char token[MAX_TOKEN_LEN];
86 /* Start of a token. Search until the first
87 [non alplha, *] and call it a token. */
91 while (isalpha(*sz) || *sz == '*') {
95 /* Now find the token in the token list */
96 if ((t = _findMapping(token))) {
97 printf("tvsprintf: found token \"%s\" to \"%s\"\n", token, t);
98 _iprintf(pInto, t, &ap);
99 pInto = buffer + strlen(buffer);
102 fprintf(stderr, "Cant find token \"%s\"\n", token);
106 else if (*sz == '%') {
109 while (!isalpha(*sz)) {
114 vsprintf(pInto, token, ap);
115 pInto = buffer + strlen(buffer);
116 (void)va_arg(ap, int);
125 // Append a string onto another, and update the pointer to the end of
127 static char *_appendAt(char *at, char *onto, const char *sz)
129 wassert(at && onto && sz);
131 return at + strlen(sz);
134 void tvsprintf(char *buffer, const char *format, va_list ap)
136 // Under Linux PPC va_list is a structure instead of a primitive type,
137 // and doesnt like being passed around. This version turns everything
138 // into one function.
142 // %[CIF] - special formats with no argument (ie list isnt touched)
143 // All of the system formats
145 // This is acheived by expanding the tokens and zero arg formats into
146 // one big format string, which is passed to the native printf.
148 char newformat[MAX_INLINEASM];
149 char *pInto = newformat;
151 char token[MAX_TOKEN_LEN];
152 const char *sz = format;
154 // NULL terminate it to let strlen work.
159 /* Start of a token. Search until the first
160 [non alpha, *] and call it a token. */
164 while (isalpha(*sz) || *sz == '*') {
168 /* Now find the token in the token list */
169 if ((t = _findMapping(token))) {
170 pInto = _appendAt(pInto, newformat, t);
173 fprintf(stderr, "Cant find token \"%s\"\n", token);
177 else if (*sz == '%') {
178 // See if its one that we handle.
182 // Code segment name.
183 pInto = _appendAt(pInto, newformat, CODE_NAME);
187 pInto = _appendAt(pInto, newformat, srcFileName);
192 sprintf(id, "%u", ++count);
193 pInto = _appendAt(pInto, newformat, id);
197 // Not one of ours. Copy until the end.
199 while (!isalpha(*sz)) {
211 // Now do the actual printing
212 vsprintf(buffer, newformat, ap);
216 void tfprintf(FILE *fp, const char *szFormat, ...)
219 char buffer[MAX_INLINEASM];
221 va_start(ap, szFormat);
222 tvsprintf(buffer, szFormat, ap);
226 void tsprintf(char *buffer, const char *szFormat, ...)
229 va_start(ap, szFormat);
230 tvsprintf(buffer, szFormat, ap);
233 void asm_addTree(const ASM_MAPPINGS *pMappings)
235 const ASM_MAPPING *pMap;
236 /* Traverse down first */
237 if (pMappings->pParent)
238 asm_addTree(pMappings->pParent);
239 pMap = pMappings->pMappings;
240 while (pMap->szKey && pMap->szValue) {
241 shash_add(&_h, pMap->szKey, pMap->szValue);
246 static const ASM_MAPPING _asxxxx_mapping[] = {
247 { "labeldef", "%s::" },
248 { "slabeldef", "%s:" },
249 { "tlabeldef", "%05d$:" },
250 { "tlabel", "%05d$" },
254 { "area", ".area %s" },
255 { "areacode", ".area %s" },
256 { "areadata", ".area %s" },
257 { "areahome", ".area %s" },
258 { "ascii", ".ascii \"%s\"" },
264 { "constbyte", "0x%02X" },
265 { "constword", "0x%04X" },
266 { "immedword", "#0x%04X" },
267 { "immedbyte", "#0x%02X" },
268 { "hashedstr", "#%s" },
269 { "lsbimmeds", "#<%s" },
270 { "msbimmeds", "#>%s" },
271 { "module", ".module %s" },
272 { "global", ".globl %s" },
273 { "fileprelude", "" },
275 "; ---------------------------------\n"
277 "; ---------------------------------"
279 { "functionlabeldef", "%s:" },
280 { "bankimmeds", "0 ; PENDING: bank support" },
284 const ASM_MAPPINGS asm_asxxxx_mapping = {