X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fasm.c;h=6fc273a5f773ee8b79fe9f881450978cc522da37;hb=1d1257b95e7011f5d9621b43aafd45bd87fbc34a;hp=d4860c284ab40a8ac4253866a9c12d31b1366db2;hpb=948f2c7c5179a6aeff9286cb4fbdf9ebc55e16b8;p=fw%2Fsdcc diff --git a/src/asm.c b/src/asm.c index d4860c28..6fc273a5 100644 --- a/src/asm.c +++ b/src/asm.c @@ -9,18 +9,9 @@ #include "common.h" #include "asm.h" -#ifdef _WIN32 - // for O_BINARY in _pipe() -# include -# include -#else - // for pipe and close -# include -#endif - /* A 'token' is like !blah or %24f and is under the programmers control. */ -#define MAX_TOKEN_LEN 64 +#define MAX_TOKEN_LEN 64 static hTab *_h; @@ -36,10 +27,10 @@ FileBaseName (char *fileFullName) while (*fileFullName) { if ((*fileFullName == '/') || (*fileFullName == '\\') || (*fileFullName == ':')) - { - p = fileFullName; - p++; - } + { + p = fileFullName; + p++; + } fileFullName++; } return p; @@ -62,7 +53,7 @@ _appendAt (char *at, char *onto, const char *sz, size_t *max) return at + strlen (at); } -void +void tvsprintf (char *buffer, size_t len, const char *format, va_list ap) { // Under Linux PPC va_list is a structure instead of a primitive type, @@ -76,14 +67,14 @@ tvsprintf (char *buffer, size_t len, const char *format, va_list ap) // This is acheived by expanding the tokens and zero arg formats into // one big format string, which is passed to the native printf. - static int count; - char noTokens[INITIAL_INLINEASM]; - char newFormat[INITIAL_INLINEASM]; - char *pInto = noTokens; - size_t pIntoLen = sizeof(noTokens); - char *p; - char token[MAX_TOKEN_LEN]; - const char *sz = format; + static int count; + char noTokens[INITIAL_INLINEASM]; + char newFormat[INITIAL_INLINEASM]; + char *pInto = noTokens; + size_t pIntoLen = sizeof(noTokens); + char *p; + char token[MAX_TOKEN_LEN]; + const char *sz = format; // NULL terminate it to let strlen work. *pInto = '\0'; @@ -92,42 +83,42 @@ tvsprintf (char *buffer, size_t len, const char *format, va_list ap) while (pIntoLen && *sz) { if (*sz == '!') - { - /* Start of a token. Search until the first - [non alpha, *] and call it a token. */ - const char *t; - p = token; - sz++; - while (isalpha (*sz) || *sz == '*') - { - *p++ = *sz++; - } - *p = '\0'; - /* Now find the token in the token list */ - if ((t = _findMapping (token))) - { - pInto = _appendAt (pInto, noTokens, t, &pIntoLen); - } - else - { - fprintf (stderr, "Cant find token \"%s\"\n", token); - wassert (0); - } - } + { + /* Start of a token. Search until the first + [non alpha, *] and call it a token. */ + const char *t; + p = token; + sz++; + while (isalpha ((unsigned char)*sz) || *sz == '*') + { + *p++ = *sz++; + } + *p = '\0'; + /* Now find the token in the token list */ + if ((t = _findMapping (token))) + { + pInto = _appendAt (pInto, noTokens, t, &pIntoLen); + } + else + { + fprintf (stderr, "Cant find token \"%s\"\n", token); + wassert (0); + } + } else { *pInto++ = *sz++; - pIntoLen--; + pIntoLen--; } } if (!pIntoLen) { fprintf(stderr, - "Internal error: tvsprintf overflowed on pass one.\n"); + "Internal error: tvsprintf overflowed on pass one.\n"); // Might as well go on... } - + *pInto = '\0'; /* Second pass: Expand any macros that we own */ @@ -138,89 +129,89 @@ tvsprintf (char *buffer, size_t len, const char *format, va_list ap) while (pIntoLen && *sz) { if (*sz == '%') - { - // See if its one that we handle. - sz++; - switch (*sz) - { - case 'C': - // Code segment name. - pInto = _appendAt (pInto, newFormat, CODE_NAME, &pIntoLen); + { + // See if its one that we handle. + sz++; + switch (*sz) + { + case 'C': + // Code segment name. + pInto = _appendAt (pInto, newFormat, CODE_NAME, &pIntoLen); sz++; - break; - case 'F': - // Source file name. - pInto = _appendAt (pInto, newFormat, fullSrcFileName, &pIntoLen); + break; + case 'F': + // Source file name. + pInto = _appendAt (pInto, newFormat, fullSrcFileName, &pIntoLen); sz++; - break; + break; case 'N': // Current function name. pInto = _appendAt (pInto, newFormat, currFunc->rname, &pIntoLen); sz++; break; - case 'I': - { - // Unique ID. - char id[20]; - SNPRINTF (id, sizeof(id), "%u", ++count); - pInto = _appendAt (pInto, newFormat, id, &pIntoLen); + case 'I': + { + // Unique ID. + char id[20]; + SNPRINTF (id, sizeof(id), "%u", ++count); + pInto = _appendAt (pInto, newFormat, id, &pIntoLen); sz++; - break; - } - default: - // Not one of ours. Copy until the end. - *pInto++ = '%'; - pIntoLen--; - while (pIntoLen && !isalpha (*sz)) - { - *pInto++ = *sz++; - pIntoLen--; - } - if (pIntoLen) - { - *pInto++ = *sz++; - pIntoLen--; - } - } - } + break; + } + default: + // Not one of ours. Copy until the end. + *pInto++ = '%'; + pIntoLen--; + while (pIntoLen && !isalpha ((unsigned char)*sz)) + { + *pInto++ = *sz++; + pIntoLen--; + } + if (pIntoLen) + { + *pInto++ = *sz++; + pIntoLen--; + } + } + } else - { - *pInto++ = *sz++; - pIntoLen--; - } + { + *pInto++ = *sz++; + pIntoLen--; + } } if (!pIntoLen) { fprintf(stderr, - "Internal error: tvsprintf overflowed on pass two.\n"); + "Internal error: tvsprintf overflowed on pass two.\n"); // Might as well go on... - } - + } + *pInto = '\0'; // Now do the actual printing #if defined(HAVE_VSNPRINTF) { - int wrlen; - wrlen = vsnprintf (buffer, len, newFormat, ap); - - if (wrlen < 0 || wrlen >= len) - { - fprintf(stderr, "Internal error: tvsprintf truncated.\n"); - } + int wrlen; + wrlen = vsnprintf (buffer, len, newFormat, ap); + + if (wrlen < 0 || (size_t)wrlen >= len) + { + fprintf(stderr, "Internal error: tvsprintf truncated.\n"); + } } - -#else + +#else vsprintf (buffer, newFormat, ap); if (strlen(buffer) >= len) { - fprintf(stderr, "Internal error: tvsprintf overflowed.\n"); + fprintf(stderr, "Internal error: tvsprintf overflowed.\n"); } -#endif +#endif } -void +void tfprintf (FILE * fp, const char *szFormat,...) { va_list ap; @@ -232,7 +223,7 @@ tfprintf (FILE * fp, const char *szFormat,...) fputs (buffer, fp); } -void +void tsprintf (char *buffer, size_t len, const char *szFormat,...) { va_list ap; @@ -241,7 +232,7 @@ tsprintf (char *buffer, size_t len, const char *szFormat,...) va_end(ap); } -void +void asm_addTree (const ASM_MAPPINGS * pMappings) { const ASM_MAPPING *pMap; @@ -259,55 +250,59 @@ asm_addTree (const ASM_MAPPINGS * pMappings) /*-----------------------------------------------------------------*/ /* printILine - return the readable i-code for this ic */ /* */ -/* iCodePrint wants a file stream so we need a pipe to fool it */ +/* iCodePrint wants a file stream so we need a temporary file to */ +/* fool it */ /*-----------------------------------------------------------------*/ -static char verbalICode[1024]; - -char *printILine (iCode *ic) { - int filedes[2]; - FILE *pipeStream; +char * +printILine (iCode *ic) +{ + static char verbalICode[1024]; + FILE *tmpFile; iCodeTable *icTab=getTableEntry(ic->op); - int res; -#ifdef _WIN32 - res = _pipe(filedes, 256, O_BINARY); -#else - res = pipe(filedes); -#endif - assert(res != -1); // forget it - if (res == -1) - return ""; // return empty line if pipe creation failed - - // stuff the pipe with the readable icode - pipeStream=fdopen(filedes[1],"w"); - if (ic->op != INLINEASM) - icTab->iCodePrint(pipeStream, ic, icTab->printName); - else - fprintf(pipeStream, "inline\n"); - // it really needs an extra push - fflush(pipeStream); - // now swallow it - pipeStream=fdopen(filedes[0],"r"); - fgets(verbalICode, sizeof(verbalICode), pipeStream); - // clean up the mess, we'll return here for all icodes!! - assert(!close (filedes[0])); - assert(!close (filedes[1])); - // kill the trailing NL - verbalICode[strlen(verbalICode)-1]='\0'; - // and throw it up + if (INLINEASM == ic->op) + return "inline"; + + tmpFile = tempfile(); + assert(NULL != tmpFile); + if (NULL == tmpFile) + return ""; /* return empty line if temporary file creation failed */ + + addSetHead(&tmpfileSet, tmpFile); + + /* stuff the temporary file with the readable icode */ + icTab->iCodePrint(tmpFile, ic, icTab->printName); + + /* now swallow it */ + rewind(tmpFile); + fgets(verbalICode, sizeof(verbalICode), tmpFile); + + /* clean up the mess, we'll return here for all icodes!! + * Actually the temporary file is only closed. It will be + * removed by rm_tmpfiles(). + */ + assert(!fclose(tmpFile)); + deleteSetItem(&tmpfileSet, tmpFile); + + /* kill the trailing NL */ + if ('\n' == verbalICode[strlen(verbalICode) - 1]) + verbalICode[strlen(verbalICode) - 1] = '\0'; + + /* and throw it up */ return verbalICode; } /*-----------------------------------------------------------------*/ /* printCLine - return the c-code for this lineno */ /*-----------------------------------------------------------------*/ -static FILE *inFile=NULL; -static char inLineString[1024]; -static int inLineNo=0; -static char lastSrcFile[PATH_MAX]; -int rewinds=0; - -char *printCLine (char *srcFile, int lineno) { +/* int rewinds=0; */ +char * +printCLine (char *srcFile, int lineno) +{ + static FILE *inFile=NULL; + static char inLineString[1024]; + static int inLineNo=0; + static char lastSrcFile[PATH_MAX]; char *ilsP=inLineString; if (inFile) { @@ -328,7 +323,7 @@ char *printCLine (char *srcFile, int lineno) { if (lineno> 8)"}, {"hihis","(%s >> 16)"}, @@ -432,7 +427,7 @@ static const ASM_MAPPING _gas_mapping[] = "; ---------------------------------" }, {"functionlabeldef", "%s:"}, - {"bankimmeds", "0 ; PENDING: bank support"}, + {"bankimmeds", "0 ; PENDING: bank support"}, {NULL, NULL} }; @@ -471,7 +466,7 @@ static const ASM_MAPPING _a390_mapping[] = "; ---------------------------------" }, {"functionlabeldef", "%s:"}, - {"bankimmeds", "0 ; PENDING: bank support"}, + {"bankimmeds", "0 ; PENDING: bank support"}, {"los","(%s & 0FFh)"}, {"his","((%s / 256) & 0FFh)"}, {"hihis","((%s / 65536) & 0FFh)"}, @@ -523,7 +518,7 @@ static const ASM_MAPPING _xa_asm_mapping[] = "; ---------------------------------" }, {"functionlabeldef", "%s:"}, - {"bankimmeds", "0 ; PENDING: bank support"}, + {"bankimmeds", "0 ; PENDING: bank support"}, {"los","(%s & 0FFh)"}, {"his","((%s / 256) & 0FFh)"}, {"hihis","((%s / 65536) & 0FFh)"},