From 4c527a8dc0a11d9bb33bebfe2fafa42e34b2895b Mon Sep 17 00:00:00 2001 From: borutr Date: Sun, 7 Jan 2007 17:18:12 +0000 Subject: [PATCH] * use dynamic memory buffers instead temporary files * support/Util/dbuf_string.c, support/Util/dbuf_string.h: added * src/Makefile.in, src/SDCC.lex, src/SDCCast.c, src/SDCCast.h, src/SDCCcse.c, src/SDCCglobl.h, src/SDCCglue.c, src/SDCCglue.h, src/SDCCicode.c, src/SDCCicode.h, src/SDCCmain.c, src/SDCCmem.c, src/SDCCmem.h, src/SDCCpeeph.c, src/SDCCpeeph.h, src/SDCCsymt.c, src/SDCCsymt.h, src/SDCCutil.c, src/asm.c, src/asm.h, src/avr/gen.c, src/avr/main.c, src/ds390/gen.c, src/ds390/main.c, src/hc08/gen.c, src/hc08/main.c, src/izt/gen.c, src/mcs51/gen.c, src/mcs51/main.c, src/pic/device.c, src/pic/gen.c, src/pic/glue.c, src/pic/main.c, src/pic/ralloc.c, src/pic16/gen.c, src/pic16/glue.c, src/pic16/main.c, src/pic16/ralloc.c, src/port.h, src/src.dsp, src/xa51/gen.c, src/xa51/main.c, src/z80/gen.c, src/z80/main.c, support/Util/dbuf.c, support/Util/dbuf.h: modified git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@4563 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- ChangeLog | 19 ++ src/Makefile.in | 2 +- src/SDCC.lex | 24 +- src/SDCCast.c | 6 +- src/SDCCast.h | 2 +- src/SDCCcse.c | 6 +- src/SDCCglobl.h | 12 - src/SDCCglue.c | 525 ++++++++++++------------------------- src/SDCCglue.h | 6 +- src/SDCCicode.c | 271 ++++++++++--------- src/SDCCicode.h | 3 +- src/SDCCmain.c | 18 +- src/SDCCmem.c | 57 ++-- src/SDCCmem.h | 6 +- src/SDCCpeeph.c | 18 +- src/SDCCpeeph.h | 4 +- src/SDCCsymt.c | 114 ++++---- src/SDCCsymt.h | 2 + src/SDCCutil.c | 22 +- src/asm.c | 211 ++++++--------- src/asm.h | 5 +- src/avr/gen.c | 6 +- src/avr/main.c | 4 +- src/ds390/gen.c | 36 +-- src/ds390/main.c | 48 ++-- src/hc08/gen.c | 9 +- src/hc08/main.c | 16 +- src/izt/gen.c | 2 +- src/mcs51/gen.c | 35 ++- src/mcs51/main.c | 13 +- src/pic/device.c | 2 - src/pic/gen.c | 6 +- src/pic/glue.c | 207 ++++++++------- src/pic/main.c | 9 +- src/pic/ralloc.c | 2 +- src/pic16/gen.c | 15 +- src/pic16/glue.c | 76 +++--- src/pic16/main.c | 25 +- src/pic16/ralloc.c | 10 +- src/port.h | 3 +- src/src.dsp | 8 + src/xa51/gen.c | 10 +- src/xa51/main.c | 2 +- src/z80/gen.c | 31 ++- src/z80/main.c | 6 +- support/Util/dbuf.c | 50 ++-- support/Util/dbuf.h | 14 +- support/Util/dbuf_string.c | 204 ++++++++++++++ support/Util/dbuf_string.h | 45 ++++ 49 files changed, 1166 insertions(+), 1061 deletions(-) create mode 100644 support/Util/dbuf_string.c create mode 100644 support/Util/dbuf_string.h diff --git a/ChangeLog b/ChangeLog index 35532979..f2415c78 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +2007-01-07 Borut Razem + + * use dynamic memory buffers instead temporary files + * support/Util/dbuf_string.c, support/Util/dbuf_string.h: + added + * src/Makefile.in, src/SDCC.lex, src/SDCCast.c, src/SDCCast.h, + src/SDCCcse.c, src/SDCCglobl.h, src/SDCCglue.c, src/SDCCglue.h, + src/SDCCicode.c, src/SDCCicode.h, src/SDCCmain.c, src/SDCCmem.c, + src/SDCCmem.h, src/SDCCpeeph.c, src/SDCCpeeph.h, src/SDCCsymt.c, + src/SDCCsymt.h, src/SDCCutil.c, src/asm.c, src/asm.h, src/avr/gen.c, + src/avr/main.c, src/ds390/gen.c, src/ds390/main.c, src/hc08/gen.c, + src/hc08/main.c, src/izt/gen.c, src/mcs51/gen.c, src/mcs51/main.c, + src/pic/device.c, src/pic/gen.c, src/pic/glue.c, src/pic/main.c, + src/pic/ralloc.c, src/pic16/gen.c, src/pic16/glue.c, + src/pic16/main.c, src/pic16/ralloc.c, src/port.h, src/src.dsp, + src/xa51/gen.c, src/xa51/main.c, src/z80/gen.c, src/z80/main.c, + support/Util/dbuf.c, support/Util/dbuf.h: + modified + 2007-01-07 Maarten Brock * src/port.h: added TARGET_Z80_LIKE macro diff --git a/src/Makefile.in b/src/Makefile.in index e3436524..7a5f62f4 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -24,7 +24,7 @@ ifdef SDCC_SUB_VERSION CFLAGS += -DSDCC_SUB_VERSION_STR=\"$(SDCC_SUB_VERSION)\" endif -SLIBOBJS = SDCCerr.o NewAlloc.o MySystem.o BuildCmd.o dbuf.o findme.o +SLIBOBJS = SDCCerr.o NewAlloc.o MySystem.o BuildCmd.o dbuf.o dbuf_string.o findme.o OBJECTS = SDCCy.o SDCChasht.o SDCCmain.o \ SDCCsymt.o SDCCopt.o SDCCast.o SDCCmem.o SDCCval.o \ diff --git a/src/SDCC.lex b/src/SDCC.lex index d4a037e7..201b1c92 100644 --- a/src/SDCC.lex +++ b/src/SDCC.lex @@ -35,7 +35,7 @@ IS (u|U|l|L)* #include #include "common.h" #include "newalloc.h" -#include "dbuf.h" +#include "dbuf_string.h" #define TKEYWORD(token) return (isTargetKeyword(yytext) ? token :\ check_type()) @@ -78,7 +78,7 @@ _?"_asm" { count(); if (!options.std_sdcc && yytext[1] != '_') { - dbuf_append(&asmbuff, yytext, strlen(yytext)); + dbuf_append_str(&asmbuff, yytext); } else { @@ -90,10 +90,10 @@ _?"_asm" { } \n { count(); - dbuf_append(&asmbuff, yytext, 1); + dbuf_append_char(&asmbuff, *yytext); } . { - dbuf_append(&asmbuff, yytext, 1); + dbuf_append_char(&asmbuff, *yytext); } "at" { count(); TKEYWORDSDCC(AT); } "__at" { count(); TKEYWORD(AT); } @@ -377,9 +377,9 @@ static char *stringLiteral(void) if (dbuf.alloc == 0) dbuf_init(&dbuf, STR_BUF_CHUNCK_LEN); else - dbuf_set_size(&dbuf, 0); + dbuf_set_length(&dbuf, 0); - dbuf_append(&dbuf, "\"", 1); + dbuf_append_char(&dbuf, '"'); /* put into the buffer till we hit the first \" */ while ((ch = input()) != 0) { @@ -402,7 +402,7 @@ static char *stringLiteral(void) case '\n': /* if new line we have a new line break, which is illegal */ werror(W_NEWLINE_IN_STRING); - dbuf_append(&dbuf, "\n", 1); + dbuf_append_char(&dbuf, '\n'); lineno = ++mylineno; column = 0; break; @@ -411,7 +411,7 @@ static char *stringLiteral(void) /* if this is a quote then we have work to do */ /* find the next non whitespace character */ /* if that is a double quote then carry on */ - dbuf_append(&dbuf, "\"", 1); /* Pass end of this string or substring to evaluator */ + dbuf_append_char(&dbuf, '"'); /* Pass end of this string or substring to evaluator */ while ((ch = input()) && (isspace(ch) || ch == '\\' || ch == '#')) { switch (ch) { case '\\': @@ -437,11 +437,10 @@ static char *stringLiteral(void) const char *line; dbuf_init(&linebuf, STR_BUF_CHUNCK_LEN); - dbuf_append(&linebuf, "#", 1); + dbuf_append_char(&linebuf, '#'); while ((ch = input()) && ch != '\n') { - buf[0] = ch; - dbuf_append(&linebuf, buf, 1); + dbuf_append_char(&linebuf, (char)ch); } if (ch == '\n') { @@ -472,8 +471,7 @@ static char *stringLiteral(void) break; default: - buf[0] = ch; - dbuf_append(&dbuf, buf, 1); /* Put next substring introducer into output string */ + dbuf_append_char(&dbuf, (char)ch); /* Put next substring introducer into output string */ } } diff --git a/src/SDCCast.c b/src/SDCCast.c index def33ed0..76396221 100644 --- a/src/SDCCast.c +++ b/src/SDCCast.c @@ -63,7 +63,7 @@ static ast *backPatchLabels (ast *, symbol *, symbol *); void PA(ast *t); int inInitMode = 0; memmap *GcurMemmap=NULL; /* points to the memmap that's currently active */ -FILE *codeOutFile; +struct dbuf_s *codeOutBuf; int ptt (ast * tree) { @@ -5983,7 +5983,7 @@ createFunction (symbol * name, ast * body) /* create the node & generate intermediate code */ GcurMemmap = code; - codeOutFile = code->oFile; + codeOutBuf = &code->oBuf; piCode = iCodeFromAst (ex); if (fatalError) @@ -5998,7 +5998,7 @@ createFunction (symbol * name, ast * body) if (staticAutos) { GcurMemmap = statsg; - codeOutFile = statsg->oFile; + codeOutBuf = &statsg->oBuf; eBBlockFromiCode (iCodeFromAst (decorateType (resolveSymbols (staticAutos), RESULT_TYPE_NONE))); staticAutos = NULL; } diff --git a/src/SDCCast.h b/src/SDCCast.h index a3fb3d3c..e14ef46f 100644 --- a/src/SDCCast.h +++ b/src/SDCCast.h @@ -173,7 +173,7 @@ ast; /* forward declarations for global variables */ extern ast *staticAutos; -extern FILE *codeOutFile; +extern struct dbuf_s *codeOutBuf; extern struct memmap *GcurMemmap; /* forward definitions for functions */ diff --git a/src/SDCCcse.c b/src/SDCCcse.c index df360ff0..7d86488d 100644 --- a/src/SDCCcse.c +++ b/src/SDCCcse.c @@ -24,6 +24,7 @@ #include "common.h" #include "newalloc.h" +#include "dbuf_string.h" /*-----------------------------------------------------------------*/ @@ -131,6 +132,7 @@ pcseDef (void *item, va_list ap) { cseDef *cdp = item; iCodeTable *icTab; + struct dbuf_s dbuf; (void) ap; @@ -138,7 +140,9 @@ pcseDef (void *item, va_list ap) fprintf (stdout, "**null op**"); printOperand (cdp->sym, stdout); icTab = getTableEntry (cdp->diCode->op); - icTab->iCodePrint (stdout, cdp->diCode, icTab->printName); + dbuf_init (&dbuf, 1024); + icTab->iCodePrint (&dbuf, cdp->diCode, icTab->printName); + dbuf_write_and_destroy (&dbuf, stdout); return 1; } diff --git a/src/SDCCglobl.h b/src/SDCCglobl.h index 044fe654..48c466e4 100644 --- a/src/SDCCglobl.h +++ b/src/SDCCglobl.h @@ -307,18 +307,6 @@ extern set *libDirsSet; /* list of lib search directories */ void setParseWithComma (set **, const char *); -/** Creates a temporary file a'la tmpfile which avoids the bugs - in cygwin wrt c:\tmp. - Scans, in order: TMP, TEMP, TMPDIR, else uses tmpfile(). -*/ -FILE *tempfile (void); - -/** Creates a temporary file name a'la tmpnam which avoids the bugs - in cygwin wrt c:\tmp. - Scans, in order: TMP, TEMP, TMPDIR, else uses tmpfile(). -*/ -char *tempfilename (void); - /** An assert() macro that will go out through sdcc's error system. */ diff --git a/src/SDCCglue.c b/src/SDCCglue.c index 704656eb..fabe9975 100644 --- a/src/SDCCglue.c +++ b/src/SDCCglue.c @@ -28,6 +28,7 @@ #include "newalloc.h" #include #include +#include "dbuf_string.h" #ifdef _WIN32 #include @@ -37,7 +38,7 @@ symbol *interrupts[INTNO_MAX+1]; -void printIval (symbol *, sym_link *, initList *, FILE *); +void printIval (symbol *, sym_link *, initList *, struct dbuf_s *); set *publics = NULL; /* public variables */ set *externs = NULL; /* Variables that are declared as extern */ @@ -99,39 +100,6 @@ DEFSETFUNC (rmTmpFiles) return 0; } -/*-----------------------------------------------------------------*/ -/* rm_tmpfiles - close and remove temporary files and delete sets */ -/*-----------------------------------------------------------------*/ -void -rm_tmpfiles (void) -{ - /* close temporary files */ - applyToSet (pipeSet, closePipes); - /* close temporary files */ - deleteSet (&pipeSet); - - applyToSet (tmpfileSet, closeTmpFiles); - /* remove temporary files */ - applyToSet (tmpfileNameSet, rmTmpFiles); - /* delete temorary file sets */ - deleteSet (&tmpfileSet); - deleteSet (&tmpfileNameSet); -} - -/*-----------------------------------------------------------------*/ -/* copyFile - copies source file to destination file */ -/*-----------------------------------------------------------------*/ -void -copyFile (FILE * dest, FILE * src) -{ - int ch; - - rewind (src); - while (!feof (src)) - if ((ch = fgetc (src)) != EOF) - fputc (ch, dest); -} - char * aopLiteralLong (value * val, int offset, int size) { @@ -208,13 +176,13 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag) { /* PENDING: special case here - should remove */ if (!strcmp (map->sname, CODE_NAME)) - tfprintf (map->oFile, "\t!areacode\n", map->sname); + dbuf_tprintf (&map->oBuf, "\t!areacode\n", map->sname); else if (!strcmp (map->sname, DATA_NAME)) - tfprintf (map->oFile, "\t!areadata\n", map->sname); + dbuf_tprintf (&map->oBuf, "\t!areadata\n", map->sname); else if (!strcmp (map->sname, HOME_NAME)) - tfprintf (map->oFile, "\t!areahome\n", map->sname); + dbuf_tprintf (&map->oBuf, "\t!areahome\n", map->sname); else - tfprintf (map->oFile, "\t!area\n", map->sname); + dbuf_tprintf (&map->oBuf, "\t!area\n", map->sname); } for (sym = setFirstItem (map->syms); sym; @@ -266,16 +234,16 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag) if (!sym->level) /* global */ { if (IS_STATIC (sym->etype)) - fprintf (map->oFile, "F%s$", moduleName); /* scope is file */ + dbuf_printf (&map->oBuf, "F%s$", moduleName); /* scope is file */ else - fprintf (map->oFile, "G$"); /* scope is global */ + dbuf_printf (&map->oBuf, "G$"); /* scope is global */ } else { /* symbol is local */ - fprintf (map->oFile, "L%s$", (sym->localof ? sym->localof->name : "-null-")); + dbuf_printf (&map->oBuf, "L%s$", (sym->localof ? sym->localof->name : "-null-")); } - fprintf (map->oFile, "%s$%d$%d", sym->name, sym->level, sym->block); + dbuf_printf (&map->oBuf, "%s$%d$%d", sym->name, sym->level, sym->block); } /* if it has an initial value then do it only if @@ -300,14 +268,16 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag) if (!SPEC_ABSA (sym->etype)) { - FILE *tmpFile = tempfile (); - addSetHead (&tmpfileSet, tmpFile); + struct dbuf_s tmpBuf; + + dbuf_init(&tmpBuf, 4096); // before allocation we must parse the sym->ival tree // but without actually generating initialization code noAlloc++; resolveIvalSym (sym->ival, sym->type); - printIval (sym, sym->type, sym->ival, tmpFile); + printIval (sym, sym->type, sym->ival, &tmpBuf); noAlloc--; + dbuf_destroy(&tmpBuf); } } else { if (IS_AGGREGATE (sym->type)) { @@ -320,7 +290,7 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag) ival = newNode ('=', newAst_VALUE (symbolVal (sym)), decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_TYPE_NONE)); } - codeOutFile = statsg->oFile; + codeOutBuf = &statsg->oBuf; if (ival) { // set ival's lineno to where the symbol was defined @@ -344,7 +314,7 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag) { char *equ="="; if (options.debug) { - fprintf (map->oFile, " == 0x%04x\n", SPEC_ADDR (sym->etype)); + dbuf_printf (&map->oBuf, " == 0x%04x\n", SPEC_ADDR (sym->etype)); } if (TARGET_IS_XA51) { if (map==sfr) { @@ -353,7 +323,7 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag) equ="bit"; } } - fprintf (map->oFile, "%s\t%s\t0x%04x\n", + dbuf_printf (&map->oBuf, "%s\t%s\t0x%04x\n", sym->rname, equ, SPEC_ADDR (sym->etype)); } @@ -365,17 +335,17 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag) } /* allocate space */ if (options.debug) { - fprintf (map->oFile, "==.\n"); + dbuf_printf (&map->oBuf, "==.\n"); } if (SPEC_ABSA (sym->etype)) { - tfprintf (map->oFile, "\t!org\n", SPEC_ADDR (sym->etype)); + dbuf_tprintf (&map->oBuf, "\t!org\n", SPEC_ADDR (sym->etype)); } if (IS_STATIC (sym->etype) || sym->level) - tfprintf (map->oFile, "!slabeldef\n", sym->rname); + dbuf_tprintf (&map->oBuf, "!slabeldef\n", sym->rname); else - tfprintf (map->oFile, "!labeldef\n", sym->rname); - tfprintf (map->oFile, "\t!ds\n", (unsigned int) size & 0xffff); + dbuf_tprintf (&map->oBuf, "!labeldef\n", sym->rname); + dbuf_tprintf (&map->oBuf, "\t!ds\n", (unsigned int) size & 0xffff); } sym->ival = NULL; } @@ -524,7 +494,7 @@ initPointer (initList * ilist, sym_link *toType) /* printChar - formats and prints a characater string with DB */ /*-----------------------------------------------------------------*/ void -printChar (FILE * ofile, char *s, int plen) +printChar (struct dbuf_s * oBuf, char *s, int plen) { int i; int len = plen; @@ -541,8 +511,8 @@ printChar (FILE * ofile, char *s, int plen) { *p = '\0'; if (p != buf) - tfprintf (ofile, "\t!ascii\n", buf); - tfprintf (ofile, "\t!db !constbyte\n", (unsigned char)*s); + dbuf_tprintf (oBuf, "\t!ascii\n", buf); + dbuf_tprintf (oBuf, "\t!db !constbyte\n", (unsigned char)*s); p = buf; } else @@ -557,7 +527,7 @@ printChar (FILE * ofile, char *s, int plen) if (p != buf) { *p = '\0'; - tfprintf (ofile, "\t!ascii\n", buf); + dbuf_tprintf (oBuf, "\t!ascii\n", buf); p = buf; } @@ -568,7 +538,7 @@ printChar (FILE * ofile, char *s, int plen) } while (pplen < plen) { - tfprintf (ofile, "\t!db !constbyte\n", 0); + dbuf_tprintf (oBuf, "\t!db !constbyte\n", 0); pplen++; } } @@ -608,21 +578,21 @@ pointerTypeToGPByte (const int p_type, const char *iname, const char *oname) /* printPointerType - generates ival for pointer type */ /*-----------------------------------------------------------------*/ void -_printPointerType (FILE * oFile, const char *name) +_printPointerType (struct dbuf_s * oBuf, const char *name) { if (options.model == MODEL_FLAT24) { if (port->little_endian) - fprintf (oFile, "\t.byte %s,(%s >> 8),(%s >> 16)", name, name, name); + dbuf_printf (oBuf, "\t.byte %s,(%s >> 8),(%s >> 16)", name, name, name); else - fprintf (oFile, "\t.byte (%s >> 16),(%s >> 8),%s", name, name, name); + dbuf_printf (oBuf, "\t.byte (%s >> 16),(%s >> 8),%s", name, name, name); } else { if (port->little_endian) - fprintf (oFile, "\t.byte %s,(%s >> 8)", name, name); + dbuf_printf (oBuf, "\t.byte %s,(%s >> 8)", name, name); else - fprintf (oFile, "\t.byte (%s >> 8),%s", name, name); + dbuf_printf (oBuf, "\t.byte (%s >> 8),%s", name, name); } } @@ -630,28 +600,28 @@ _printPointerType (FILE * oFile, const char *name) /* printPointerType - generates ival for pointer type */ /*-----------------------------------------------------------------*/ void -printPointerType (FILE * oFile, const char *name) +printPointerType (struct dbuf_s * oBuf, const char *name) { - _printPointerType (oFile, name); - fprintf (oFile, "\n"); + _printPointerType (oBuf, name); + dbuf_printf (oBuf, "\n"); } /*-----------------------------------------------------------------*/ /* printGPointerType - generates ival for generic pointer type */ /*-----------------------------------------------------------------*/ void -printGPointerType (FILE * oFile, const char *iname, const char *oname, +printGPointerType (struct dbuf_s * oBuf, const char *iname, const char *oname, const unsigned int type) { - _printPointerType (oFile, iname); - fprintf (oFile, ",#0x%02x\n", pointerTypeToGPByte (type, iname, oname)); + _printPointerType (oBuf, iname); + dbuf_printf (oBuf, ",#0x%02x\n", pointerTypeToGPByte (type, iname, oname)); } /*-----------------------------------------------------------------*/ /* printIvalType - generates ival for int/char */ /*-----------------------------------------------------------------*/ void -printIvalType (symbol *sym, sym_link * type, initList * ilist, FILE * oFile) +printIvalType (symbol *sym, sym_link * type, initList * ilist, struct dbuf_s * oBuf) { value *val; @@ -671,32 +641,32 @@ printIvalType (symbol *sym, sym_link * type, initList * ilist, FILE * oFile) switch (getSize (type)) { case 1: if (!val) - tfprintf (oFile, "\t!db !constbyte\n", 0); + dbuf_tprintf (oBuf, "\t!db !constbyte\n", 0); else - tfprintf (oFile, "\t!dbs\n", + dbuf_tprintf (oBuf, "\t!dbs\n", aopLiteral (val, 0)); break; case 2: if (port->use_dw_for_init) - tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2)); + dbuf_tprintf (oBuf, "\t!dws\n", aopLiteralLong (val, 0, 2)); else if (port->little_endian) - fprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1)); + dbuf_printf (oBuf, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1)); else - fprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 1), aopLiteral (val, 0)); + dbuf_printf (oBuf, "\t.byte %s,%s\n", aopLiteral (val, 1), aopLiteral (val, 0)); break; case 4: if (!val) { - tfprintf (oFile, "\t!dw !constword\n", 0); - tfprintf (oFile, "\t!dw !constword\n", 0); + dbuf_tprintf (oBuf, "\t!dw !constword\n", 0); + dbuf_tprintf (oBuf, "\t!dw !constword\n", 0); } else if (port->little_endian) { - fprintf (oFile, "\t.byte %s,%s,%s,%s\n", + dbuf_printf (oBuf, "\t.byte %s,%s,%s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1), aopLiteral (val, 2), aopLiteral (val, 3)); } else { - fprintf (oFile, "\t.byte %s,%s,%s,%s\n", + dbuf_printf (oBuf, "\t.byte %s,%s,%s,%s\n", aopLiteral (val, 3), aopLiteral (val, 2), aopLiteral (val, 1), aopLiteral (val, 0)); } @@ -707,7 +677,7 @@ printIvalType (symbol *sym, sym_link * type, initList * ilist, FILE * oFile) /*-----------------------------------------------------------------*/ /* printIvalBitFields - generate initializer for bitfields */ /*-----------------------------------------------------------------*/ -void printIvalBitFields(symbol **sym, initList **ilist, FILE * oFile) +void printIvalBitFields(symbol **sym, initList **ilist, struct dbuf_s * oBuf) { value *val ; symbol *lsym = *sym; @@ -739,14 +709,14 @@ void printIvalBitFields(symbol **sym, initList **ilist, FILE * oFile) } while (1); switch (size) { case 1: - tfprintf (oFile, "\t!db !constbyte\n",ival); + dbuf_tprintf (oBuf, "\t!db !constbyte\n",ival); break; case 2: - tfprintf (oFile, "\t!dw !constword\n",ival); + dbuf_tprintf (oBuf, "\t!dw !constword\n",ival); break; case 4: /* EEP: why is this db and not dw? */ - tfprintf (oFile, "\t!db !constword,!constword\n", + dbuf_tprintf (oBuf, "\t!db !constword,!constword\n", (ival >> 8) & 0xffff, (ival & 0xffff)); break; } @@ -759,7 +729,7 @@ void printIvalBitFields(symbol **sym, initList **ilist, FILE * oFile) /*-----------------------------------------------------------------*/ void printIvalStruct (symbol * sym, sym_link * type, - initList * ilist, FILE * oFile) + initList * ilist, struct dbuf_s * oBuf) { symbol *sflds; initList *iloop = NULL; @@ -776,14 +746,14 @@ printIvalStruct (symbol * sym, sym_link * type, } if (SPEC_STRUCT (type)->type == UNION) { - printIval (sym, sflds->type, iloop, oFile); + printIval (sym, sflds->type, iloop, oBuf); iloop = iloop->next; } else { for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) { if (IS_BITFIELD(sflds->type)) { - printIvalBitFields(&sflds,&iloop,oFile); + printIvalBitFields(&sflds, &iloop, oBuf); } else { - printIval (sym, sflds->type, iloop, oFile); + printIval (sym, sflds->type, iloop, oBuf); } } } @@ -797,7 +767,7 @@ printIvalStruct (symbol * sym, sym_link * type, /* printIvalChar - generates initital value for character array */ /*-----------------------------------------------------------------*/ int -printIvalChar (symbol * sym, sym_link * type, initList * ilist, FILE * oFile, char *s) +printIvalChar (symbol * sym, sym_link * type, initList * ilist, struct dbuf_s * oBuf, char *s) { value *val; unsigned int size = DCL_ELEM (type); @@ -819,7 +789,7 @@ printIvalChar (symbol * sym, sym_link * type, initList * ilist, FILE * oFile, ch DCL_ELEM (type) = size; } - printChar (oFile, SPEC_CVAL (val->etype).v_char, size); + printChar (oBuf, SPEC_CVAL (val->etype).v_char, size); return 1; } @@ -827,7 +797,7 @@ printIvalChar (symbol * sym, sym_link * type, initList * ilist, FILE * oFile, ch return 0; } else - printChar (oFile, s, strlen (s) + 1); + printChar (oBuf, s, strlen (s) + 1); return 1; } @@ -836,7 +806,7 @@ printIvalChar (symbol * sym, sym_link * type, initList * ilist, FILE * oFile, ch /*-----------------------------------------------------------------*/ void printIvalArray (symbol * sym, sym_link * type, initList * ilist, - FILE * oFile) + struct dbuf_s * oBuf) { value *val; initList *iloop; @@ -858,7 +828,7 @@ printIvalArray (symbol * sym, sym_link * type, initList * ilist, } if (printIvalChar (sym, type, (ilist->type == INIT_DEEP ? ilist->init.deep : ilist), - oFile, SPEC_CVAL (sym->etype).v_char)) + oBuf, SPEC_CVAL (sym->etype).v_char)) return; } /* not the special case */ @@ -872,7 +842,7 @@ printIvalArray (symbol * sym, sym_link * type, initList * ilist, werrorfl (sym->fileDef, sym->lineDef, W_EXCESS_INITIALIZERS, "array", sym->name); break; } - printIval (sym, type->next, iloop, oFile); + printIval (sym, type->next, iloop, oBuf); } } @@ -881,7 +851,7 @@ printIvalArray (symbol * sym, sym_link * type, initList * ilist, if (sizenext); while (size--) { - tfprintf (oFile, "\t!db !constbyte\n", 0); + dbuf_tprintf (oBuf, "\t!db !constbyte\n", 0); } } } else { @@ -900,7 +870,7 @@ printIvalArray (symbol * sym, sym_link * type, initList * ilist, /* printIvalFuncPtr - generate initial value for function pointers */ /*-----------------------------------------------------------------*/ void -printIvalFuncPtr (sym_link * type, initList * ilist, FILE * oFile) +printIvalFuncPtr (sym_link * type, initList * ilist, struct dbuf_s * oBuf) { value *val; int dLvl = 0; @@ -920,14 +890,14 @@ printIvalFuncPtr (sym_link * type, initList * ilist, FILE * oFile) werrorfl (ilist->filename, ilist->lineno, E_INCOMPAT_TYPES); printFromToType (val->type, type); } - printIvalCharPtr (NULL, type, val, oFile); + printIvalCharPtr (NULL, type, val, oBuf); return; } /* check the types */ if ((dLvl = compareType (val->type, type->next)) <= 0) { - tfprintf (oFile, "\t!dw !constword\n", 0); + dbuf_tprintf (oBuf, "\t!dw !constword\n", 0); return; } @@ -936,20 +906,20 @@ printIvalFuncPtr (sym_link * type, initList * ilist, FILE * oFile) { if (port->use_dw_for_init) { - tfprintf (oFile, "\t!dws\n", val->name); + dbuf_tprintf (oBuf, "\t!dws\n", val->name); } else { - printPointerType (oFile, val->name); + printPointerType (oBuf, val->name); } } else if (port->use_dw_for_init) { - tfprintf (oFile, "\t!dws\n", val->sym->rname); + dbuf_tprintf (oBuf, "\t!dws\n", val->sym->rname); } else { - printPointerType (oFile, val->sym->rname); + printPointerType (oBuf, val->sym->rname); } return; @@ -959,7 +929,7 @@ printIvalFuncPtr (sym_link * type, initList * ilist, FILE * oFile) /* printIvalCharPtr - generates initial values for character pointers */ /*-----------------------------------------------------------------*/ int -printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile) +printIvalCharPtr (symbol * sym, sym_link * type, value * val, struct dbuf_s * oBuf) { int size = 0; @@ -973,18 +943,18 @@ printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile) { if (size == 1) /* This appears to be Z80 specific?? */ { - tfprintf (oFile, + dbuf_tprintf (oBuf, "\t!dbs\n", val->name); } else if (size == FPTRSIZE) { if (port->use_dw_for_init) { - tfprintf (oFile, "\t!dws\n", val->name); + dbuf_tprintf (oBuf, "\t!dws\n", val->name); } else { - printPointerType (oFile, val->name); + printPointerType (oBuf, val->name); } } else if (size == GPTRSIZE) @@ -999,7 +969,7 @@ printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile) // this is a literal string type=CPOINTER; } - printGPointerType (oFile, val->name, sym->name, type); + printGPointerType (oBuf, val->name, sym->name, type); } else { @@ -1013,16 +983,16 @@ printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile) switch (size) { case 1: - tfprintf (oFile, "\t!dbs\n", aopLiteral (val, 0)); + dbuf_tprintf (oBuf, "\t!dbs\n", aopLiteral (val, 0)); break; case 2: if (port->use_dw_for_init) - tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, size)); + dbuf_tprintf (oBuf, "\t!dws\n", aopLiteralLong (val, 0, size)); else if (port->little_endian) - tfprintf (oFile, "\t.byte %s,%s\n", + dbuf_tprintf (oBuf, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1)); else - tfprintf (oFile, "\t.byte %s,%s\n", + dbuf_tprintf (oBuf, "\t.byte %s,%s\n", aopLiteral (val, 1), aopLiteral (val, 0)); break; case 3: @@ -1031,12 +1001,12 @@ printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile) werrorfl (sym->fileDef, sym->lineDef, E_LITERAL_GENERIC); } if (port->little_endian) { - fprintf (oFile, "\t.byte %s,%s,%s\n", + dbuf_printf (oBuf, "\t.byte %s,%s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1), aopLiteral (val, 2)); } else { - fprintf (oFile, "\t.byte %s,%s,%s\n", + dbuf_printf (oBuf, "\t.byte %s,%s,%s\n", aopLiteral (val, 2), aopLiteral (val, 1), aopLiteral (val, 0)); @@ -1048,13 +1018,13 @@ printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile) werrorfl (sym->fileDef, sym->lineDef, E_LITERAL_GENERIC); } if (port->little_endian) { - fprintf (oFile, "\t.byte %s,%s,%s,%s\n", + dbuf_printf (oBuf, "\t.byte %s,%s,%s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1), aopLiteral (val, 2), aopLiteral (val, 3)); } else { - fprintf (oFile, "\t.byte %s,%s,%s,%s\n", + dbuf_printf (oBuf, "\t.byte %s,%s,%s,%s\n", aopLiteral (val, 3), aopLiteral (val, 2), aopLiteral (val, 1), @@ -1077,7 +1047,7 @@ printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile) /* printIvalPtr - generates initial value for pointers */ /*-----------------------------------------------------------------*/ void -printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile) +printIvalPtr (symbol * sym, sym_link * type, initList * ilist, struct dbuf_s * oBuf) { value *val; int size; @@ -1089,7 +1059,7 @@ printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile) /* function pointer */ if (IS_FUNC (type->next)) { - printIvalFuncPtr (type, ilist, oFile); + printIvalFuncPtr (type, ilist, oBuf); return; } @@ -1098,7 +1068,7 @@ printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile) /* if character pointer */ if (IS_CHAR (type->next)) - if (printIvalCharPtr (sym, type, val, oFile)) + if (printIvalCharPtr (sym, type, val, oBuf)) return; /* check the type */ @@ -1113,34 +1083,34 @@ printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile) switch (getSize (type)) { case 1: - tfprintf (oFile, "\t!db !constbyte\n", (unsigned int) floatFromVal (val) & 0xff); + dbuf_tprintf (oBuf, "\t!db !constbyte\n", (unsigned int) floatFromVal (val) & 0xff); break; case 2: if (port->use_dw_for_init) - tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2)); + dbuf_tprintf (oBuf, "\t!dws\n", aopLiteralLong (val, 0, 2)); else if (port->little_endian) - tfprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1)); + dbuf_tprintf (oBuf, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1)); else - tfprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 1), aopLiteral (val, 0)); + dbuf_tprintf (oBuf, "\t.byte %s,%s\n", aopLiteral (val, 1), aopLiteral (val, 0)); break; case 3: // how about '390?? - fprintf (oFile, "; generic printIvalPtr\n"); + dbuf_printf (oBuf, "; generic printIvalPtr\n"); if (port->little_endian) { - fprintf (oFile, "\t.byte %s,%s", + dbuf_printf (oBuf, "\t.byte %s,%s", aopLiteral (val, 0), aopLiteral (val, 1)); } else { - fprintf (oFile, "\t.byte %s,%s", + dbuf_printf (oBuf, "\t.byte %s,%s", aopLiteral (val, 1), aopLiteral (val, 0)); } if (IS_GENPTR (val->type)) - fprintf (oFile, ",%s\n", aopLiteral (val, 2)); + dbuf_printf (oBuf, ",%s\n", aopLiteral (val, 2)); else if (IS_PTR (val->type)) - fprintf (oFile, ",#%x\n", pointerTypeToGPByte (DCL_TYPE (val->type), NULL, NULL)); + dbuf_printf (oBuf, ",#%x\n", pointerTypeToGPByte (DCL_TYPE (val->type), NULL, NULL)); else - fprintf (oFile, ",%s\n", aopLiteral (val, 2)); + dbuf_printf (oBuf, ",%s\n", aopLiteral (val, 2)); } return; } @@ -1150,19 +1120,19 @@ printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile) if (size == 1) /* Z80 specific?? */ { - tfprintf (oFile, "\t!dbs\n", val->name); + dbuf_tprintf (oBuf, "\t!dbs\n", val->name); } else if (size == FPTRSIZE) { if (port->use_dw_for_init) { - tfprintf (oFile, "\t!dws\n", val->name); + dbuf_tprintf (oBuf, "\t!dws\n", val->name); } else { - printPointerType (oFile, val->name); + printPointerType (oBuf, val->name); } } else if (size == GPTRSIZE) { - printGPointerType (oFile, val->name, sym->name, + printGPointerType (oBuf, val->name, sym->name, (IS_PTR (val->type) ? DCL_TYPE (val->type) : PTR_TYPE (SPEC_OCLS (val->etype)))); } @@ -1173,21 +1143,21 @@ printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile) /* printIval - generates code for initial value */ /*-----------------------------------------------------------------*/ void -printIval (symbol * sym, sym_link * type, initList * ilist, FILE * oFile) +printIval (symbol * sym, sym_link * type, initList * ilist, struct dbuf_s * oBuf) { sym_link *itype; /* if structure then */ if (IS_STRUCT (type)) { - printIvalStruct (sym, type, ilist, oFile); + printIvalStruct (sym, type, ilist, oBuf); return; } /* if this is an array */ if (IS_ARRAY (type)) { - printIvalArray (sym, type, ilist, oFile); + printIvalArray (sym, type, ilist, oBuf); return; } @@ -1223,14 +1193,14 @@ printIval (symbol * sym, sym_link * type, initList * ilist, FILE * oFile) /* if this is a pointer */ if (IS_PTR (type)) { - printIvalPtr (sym, type, ilist, oFile); + printIvalPtr (sym, type, ilist, oBuf); return; } /* if type is SPECIFIER */ if (IS_SPEC (type)) { - printIvalType (sym, type, ilist, oFile); + printIvalType (sym, type, ilist, oBuf); return; } } @@ -1239,7 +1209,7 @@ printIval (symbol * sym, sym_link * type, initList * ilist, FILE * oFile) /* emitStaticSeg - emitcode for the static segment */ /*-----------------------------------------------------------------*/ void -emitStaticSeg (memmap * map, FILE * out) +emitStaticSeg (memmap * map, struct dbuf_s * oBuf) { symbol *sym; @@ -1266,43 +1236,43 @@ emitStaticSeg (memmap * map, FILE * out) if (!sym->level) { /* global */ if (IS_STATIC (sym->etype)) - fprintf (out, "F%s$", moduleName); /* scope is file */ + dbuf_printf (oBuf, "F%s$", moduleName); /* scope is file */ else - fprintf (out, "G$"); /* scope is global */ + dbuf_printf (oBuf, "G$"); /* scope is global */ } else /* symbol is local */ - fprintf (out, "L%s$", + dbuf_printf (oBuf, "L%s$", (sym->localof ? sym->localof->name : "-null-")); - fprintf (out, "%s$%d$%d", sym->name, sym->level, sym->block); + dbuf_printf (oBuf, "%s$%d$%d", sym->name, sym->level, sym->block); } /* if it has an absolute address and no initializer */ if (SPEC_ABSA (sym->etype) && !sym->ival) { if (options.debug) - fprintf (out, " == 0x%04x\n", SPEC_ADDR (sym->etype)); + dbuf_printf (oBuf, " == 0x%04x\n", SPEC_ADDR (sym->etype)); - fprintf (out, "%s\t=\t0x%04x\n", + dbuf_printf (oBuf, "%s\t=\t0x%04x\n", sym->rname, SPEC_ADDR (sym->etype)); } else { if (options.debug) - fprintf (out, " == .\n"); + dbuf_printf (oBuf, " == .\n"); /* if it has an initial value */ if (sym->ival) { if (SPEC_ABSA (sym->etype)) { - tfprintf (out, "\t!org\n", SPEC_ADDR (sym->etype)); + dbuf_tprintf (oBuf, "\t!org\n", SPEC_ADDR (sym->etype)); } - fprintf (out, "%s:\n", sym->rname); + dbuf_printf (oBuf, "%s:\n", sym->rname); noAlloc++; resolveIvalSym (sym->ival, sym->type); - printIval (sym, sym->type, sym->ival, out); + printIval (sym, sym->type, sym->ival, oBuf); noAlloc--; /* if sym is a simple string and sym->ival is a string, WE don't need it anymore */ @@ -1319,15 +1289,15 @@ emitStaticSeg (memmap * map, FILE * out) if (size==0) { werrorfl (sym->fileDef, sym->lineDef, E_UNKNOWN_SIZE,sym->name); } - fprintf (out, "%s:\n", sym->rname); + dbuf_printf (oBuf, "%s:\n", sym->rname); /* special case for character strings */ if (IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) && SPEC_CVAL (sym->etype).v_char) - printChar (out, + printChar (oBuf, SPEC_CVAL (sym->etype).v_char, size); else - tfprintf (out, "\t!ds\n", (unsigned int) size & 0xffff); + dbuf_tprintf (oBuf, "\t!ds\n", (unsigned int) size & 0xffff); } } } @@ -1361,15 +1331,15 @@ emitMaps (void) emitRegularMap (code, TRUE, FALSE); if (options.const_seg) { - tfprintf (code->oFile, "\t!area\n", options.const_seg); + dbuf_tprintf (&code->oBuf, "\t!area\n", options.const_seg); } - emitStaticSeg (statsg, code->oFile); + emitStaticSeg (statsg, &code->oBuf); if (port->genXINIT) { - tfprintf (code->oFile, "\t!area\n", xinit->sname); - emitStaticSeg (xinit, code->oFile); + dbuf_tprintf (&code->oBuf, "\t!area\n", xinit->sname); + emitStaticSeg (xinit, &code->oBuf); } - tfprintf (code->oFile, "\t!area\n", c_abs->sname); - emitStaticSeg (c_abs, code->oFile); + dbuf_tprintf (&code->oBuf, "\t!area\n", c_abs->sname); + emitStaticSeg (c_abs, &code->oBuf); inInitMode--; } @@ -1380,7 +1350,7 @@ emitMaps (void) void flushStatics (void) { - emitStaticSeg (statsg, codeOutFile); + emitStaticSeg (statsg, codeOutBuf); statsg->syms = NULL; } @@ -1388,7 +1358,7 @@ flushStatics (void) /* createInterruptVect - creates the interrupt vector */ /*-----------------------------------------------------------------*/ void -createInterruptVect (FILE * vFile) +createInterruptVect (struct dbuf_s *vBuf) { mainf = newSymbol ("main", 0); mainf->block = 0; @@ -1410,11 +1380,11 @@ createInterruptVect (FILE * vFile) return; } - tfprintf (vFile, "\t!areacode\n", HOME_NAME); - fprintf (vFile, "__interrupt_vect:\n"); + dbuf_tprintf (vBuf, "\t!areacode\n", HOME_NAME); + dbuf_printf (vBuf, "__interrupt_vect:\n"); - if (!port->genIVT || !(port->genIVT (vFile, interrupts, maxInterrupts))) + if (!port->genIVT || !(port->genIVT (vBuf, interrupts, maxInterrupts))) { /* There's no such thing as a "generic" interrupt table header. */ wassert(0); @@ -1483,12 +1453,12 @@ printExterns (FILE * afile) /* emitOverlay - will emit code for the overlay stuff */ /*-----------------------------------------------------------------*/ static void -emitOverlay (FILE * afile) +emitOverlay (struct dbuf_s * aBuf) { set *ovrset; if (!elementsInSet (ovrSetSets)) - tfprintf (afile, "\t!area\n", port->mem.overlay_name); + dbuf_tprintf (aBuf, "\t!area\n", port->mem.overlay_name); /* for each of the sets in the overlay segment do */ for (ovrset = setFirstItem (ovrSetSets); ovrset; @@ -1500,7 +1470,7 @@ emitOverlay (FILE * afile) if (elementsInSet (ovrset)) { /* output the area informtion */ - fprintf (afile, "\t.area\t%s\n", port->mem.overlay_name); /* MOF */ + dbuf_printf (aBuf, "\t.area\t%s\n", port->mem.overlay_name); /* MOF */ } for (sym = setFirstItem (ovrset); sym; @@ -1537,15 +1507,15 @@ emitOverlay (FILE * afile) if (!sym->level) { /* global */ if (IS_STATIC (sym->etype)) - fprintf (afile, "F%s$", moduleName); /* scope is file */ + dbuf_printf (aBuf, "F%s$", moduleName); /* scope is file */ else - fprintf (afile, "G$"); /* scope is global */ + dbuf_printf (aBuf, "G$"); /* scope is global */ } else /* symbol is local */ - fprintf (afile, "L%s$", + dbuf_printf (aBuf, "L%s$", (sym->localof ? sym->localof->name : "-null-")); - fprintf (afile, "%s$%d$%d", sym->name, sym->level, sym->block); + dbuf_printf (aBuf, "%s$%d$%d", sym->name, sym->level, sym->block); } /* if is has an absolute address then generate @@ -1554,9 +1524,9 @@ emitOverlay (FILE * afile) { if (options.debug) - fprintf (afile, " == 0x%04x\n", SPEC_ADDR (sym->etype)); + dbuf_printf (aBuf, " == 0x%04x\n", SPEC_ADDR (sym->etype)); - fprintf (afile, "%s\t=\t0x%04x\n", + dbuf_printf (aBuf, "%s\t=\t0x%04x\n", sym->rname, SPEC_ADDR (sym->etype)); } @@ -1567,11 +1537,11 @@ emitOverlay (FILE * afile) werrorfl (sym->fileDef, sym->lineDef, E_UNKNOWN_SIZE); } if (options.debug) - fprintf (afile, "==.\n"); + dbuf_printf (aBuf, "==.\n"); /* allocate space */ - tfprintf (afile, "!labeldef\n", sym->rname); - tfprintf (afile, "\t!ds\n", (unsigned int) getSize (sym->type) & 0xffff); + dbuf_tprintf (aBuf, "!labeldef\n", sym->rname); + dbuf_tprintf (aBuf, "\t!ds\n", (unsigned int) getSize (sym->type) & 0xffff); } } @@ -1608,12 +1578,15 @@ spacesToUnderscores (char *dest, const char *src, size_t len) void glue (void) { - FILE *vFile; + struct dbuf_s vBuf; + struct dbuf_s ovrBuf; FILE *asmFile; - FILE *ovrFile = tempfile (); char moduleBuf[PATH_MAX]; int mcs51_like; + dbuf_init(&vBuf, 4096); + dbuf_init(&ovrBuf, 4096); + if(port->general.glue_up_main && (TARGET_IS_MCS51 || TARGET_IS_DS390 || TARGET_IS_XA51 || TARGET_IS_DS400)) { @@ -1624,25 +1597,21 @@ glue (void) mcs51_like=0; } - addSetHead (&tmpfileSet, ovrFile); /* print the global struct definitions */ if (options.debug) cdbStructBlock (0); - vFile = tempfile (); /* PENDING: this isn't the best place but it will do */ if (port->general.glue_up_main) { /* create the interrupt vector table */ - createInterruptVect (vFile); + createInterruptVect (&vBuf); } - addSetHead (&tmpfileSet, vFile); - /* emit code for the all the variables declared */ emitMaps (); /* do the overlay segments */ - emitOverlay (ovrFile); + emitOverlay (&ovrBuf); outputDebugSymbols(); @@ -1719,7 +1688,7 @@ glue (void) fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; special function registers\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, sfr->oFile); + dbuf_write_and_destroy (&sfr->oBuf, asmFile); } if(mcs51_like) @@ -1728,8 +1697,8 @@ glue (void) fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; special function bits\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, sfrbit->oFile); - + dbuf_write_and_destroy (&sfrbit->oBuf, asmFile); + /*JCF: Create the areas for the register banks*/ if(RegBankUsed[0]||RegBankUsed[1]||RegBankUsed[2]||RegBankUsed[3]) { @@ -1767,7 +1736,7 @@ glue (void) fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; %s ram data\n", mcs51_like?"internal":""); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, data->oFile); + dbuf_write_and_destroy (&data->oBuf, asmFile); /* create the overlay segments */ @@ -1775,7 +1744,7 @@ glue (void) fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; overlayable items in %s ram \n", mcs51_like?"internal":""); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, ovrFile); + dbuf_write_and_destroy (&ovrBuf, asmFile); } /* create the stack segment MOF */ @@ -1793,7 +1762,7 @@ glue (void) fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; indirectly addressable internal ram data\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, idata->oFile); + dbuf_write_and_destroy (&idata->oBuf, asmFile); } /* create the absolute idata/data segment */ @@ -1801,8 +1770,8 @@ glue (void) fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; absolute internal ram data\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, d_abs->oFile); - copyFile (asmFile, i_abs->oFile); + dbuf_write_and_destroy (&d_abs->oBuf, asmFile); + dbuf_write_and_destroy (&i_abs->oBuf, asmFile); } /* copy the bit segment */ @@ -1810,7 +1779,7 @@ glue (void) fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; bit data\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, bit->oFile); + dbuf_write_and_destroy (&bit->oBuf, asmFile); } /* copy paged external ram data */ @@ -1819,7 +1788,7 @@ glue (void) fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; paged external ram data\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, pdata->oFile); + dbuf_write_and_destroy (&pdata->oBuf, asmFile); } /* if external stack then reserve space for it */ @@ -1837,14 +1806,14 @@ glue (void) fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; external ram data\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, xdata->oFile); + dbuf_write_and_destroy (&xdata->oBuf, asmFile); } /* copy external initialized ram data */ fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; external initialized ram data\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, xidata->oFile); + dbuf_write_and_destroy (&xidata->oBuf, asmFile); /* If the port wants to generate any extra areas, let it do so. */ if (port->extraAreas.genExtraAreaDeclaration) @@ -1859,7 +1828,7 @@ glue (void) fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; interrupt vector \n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, vFile); + dbuf_write_and_destroy (&vBuf, asmFile); } /* copy global & static initialisations */ @@ -1922,7 +1891,7 @@ glue (void) } } - copyFile (asmFile, statsg->oFile); + dbuf_write_and_destroy (&statsg->oBuf, asmFile); if (port->general.glue_up_main && mainf && IFFUNC_HASBODY(mainf->type)) { @@ -1939,7 +1908,7 @@ glue (void) "; Home\n" "%s", iComments2, iComments2); tfprintf (asmFile, "\t!areahome\n", HOME_NAME); - copyFile (asmFile, home->oFile); + dbuf_write_and_destroy (&home->oBuf, asmFile); if (mainf && IFFUNC_HASBODY(mainf->type)) { @@ -1964,172 +1933,10 @@ glue (void) fprintf (asmFile, "; code\n"); fprintf (asmFile, "%s", iComments2); tfprintf (asmFile, "\t!areacode\n", options.code_seg); - copyFile (asmFile, code->oFile); + dbuf_write_and_destroy (&code->oBuf, asmFile); if (port->genAssemblerEnd) { port->genAssemblerEnd(asmFile); } fclose (asmFile); - - rm_tmpfiles (); -} - - -/** Creates a temporary file with unique file name - Scans, in order: - - TMP, TEMP, TMPDIR env. variables - - if Un*x system: /tmp and /var/tmp - - root directory using mkstemp() if available - - default location using tempnam() -*/ -static int -tempfileandname(char *fname, size_t len) -{ -#define TEMPLATE "sdccXXXXXX" -#define TEMPLATE_LEN ((sizeof TEMPLATE) - 1) - - const char *tmpdir = NULL; - int fd; - - if ((tmpdir = getenv ("TMP")) == NULL) - if ((tmpdir = getenv ("TEMP")) == NULL) - tmpdir = getenv ("TMPDIR"); - -#if defined(_WIN32) - { - static int warning_emitted; - - if (tmpdir == NULL) - { - tmpdir = "c:\\"; - if (!warning_emitted) - { - fprintf (stderr, "TMP not defined in environment, using %s for temporary files\n.", tmpdir); - warning_emitted = 1; - } - } - } -#else - { - /* try with /tmp and /var/tmp on Un*x systems */ - struct stat statbuf; - - if (tmpdir == NULL) { - if (stat("/tmp", &statbuf) != -1) - tmpdir = "/tmp"; - else if (stat("/var/tmp", &statbuf) != -1) - tmpdir = "/var/tmp"; - } - } -#endif - -#ifdef HAVE_MKSTEMP - { - char fnamebuf[PATH_MAX]; - size_t name_len; - - if (fname == NULL || len == 0) { - fname = fnamebuf; - len = sizeof fnamebuf; - } - - if (tmpdir) { - name_len = strlen(tmpdir) + 1 + TEMPLATE_LEN; - - assert(name_len < len); - if (!(name_len < len)) /* in NDEBUG is defined */ - return -1; /* buffer too small, temporary file can not be created */ - - sprintf(fname, "%s" DIR_SEPARATOR_STRING TEMPLATE, tmpdir); - } - else { - name_len = TEMPLATE_LEN; - - assert(name_len < len); - if (!(name_len < len)) /* in NDEBUG is defined */ - return -1; /* buffer too small, temporary file can not be created */ - - strcpy(fname, TEMPLATE); - } - - fd = mkstemp(fname); - } -#else - { - char *name = tempnam(tmpdir, "sdcc"); - - if (name == NULL) { - perror("Can't create temporary file name"); - exit(1); - } - - assert(strlen(name) < len); - if (!(strlen(name) < len)) /* in NDEBUG is defined */ - return -1; /* buffer too small, temporary file can not be created */ - - strcpy(fname, name); -#ifdef _WIN32 - fd = open(name, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE); -#else - fd = open(name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); -#endif - } -#endif - - if (fd == -1) { - perror("Can't create temporary file"); - exit(1); - } - - return fd; -} - - -/** Create a temporary file name -*/ -char * -tempfilename(void) -{ - int fd; - static char fnamebuf[PATH_MAX]; - - if ((fd = tempfileandname(fnamebuf, sizeof fnamebuf)) == -1) { - fprintf(stderr, "Can't create temporary file name!"); - exit(1); - } - - fd = close(fd); - assert(fd != -1); - - return fnamebuf; -} - - -/** Create a temporary file and add it to tmpfileNameSet, - so that it is removed explicitly by rm_tmpfiles() - or implicitly at program extit. -*/ -FILE * -tempfile(void) -{ - int fd; - char *tmp; - FILE *fp; - char fnamebuf[PATH_MAX]; - - if ((fd = tempfileandname(fnamebuf, sizeof fnamebuf)) == -1) { - fprintf(stderr, "Can't create temporary file!"); - exit(1); - } - - tmp = Safe_strdup(fnamebuf); - if (tmp) - addSetHead(&tmpfileNameSet, tmp); - - if ((fp = fdopen(fd, "w+b")) == NULL) { - perror("Can't create temporary file!"); - exit(1); - } - - return fp; } diff --git a/src/SDCCglue.h b/src/SDCCglue.h index 2113213c..627fc941 100644 --- a/src/SDCCglue.h +++ b/src/SDCCglue.h @@ -32,15 +32,11 @@ void glue (void); This is needed in gen.c of z80 port */ char *aopLiteral (value *, int); void flushStatics (void); -int printIvalCharPtr (symbol *, sym_link *, value *, FILE *); +int printIvalCharPtr (symbol *, sym_link *, value *, struct dbuf_s *); extern symbol *interrupts[]; extern set *publics; -extern set *tmpfileSet; -extern set *pipeSet; -extern set *tmpfileNameSet; -void rm_tmpfiles (void); int pointerTypeToGPByte (const int p_type, const char *iname, const char *oname); #endif diff --git a/src/SDCCicode.c b/src/SDCCicode.c index 1d4da0df..a8de6938 100644 --- a/src/SDCCicode.c +++ b/src/SDCCicode.c @@ -25,6 +25,7 @@ #include "common.h" #include "newalloc.h" #include "math.h" +#include "dbuf_string.h" /*-----------------------------------------------------------------*/ /* global variables */ @@ -54,7 +55,7 @@ int isLvaluereq(int lvl); void setOClass (sym_link * ptr, sym_link * spec); static operand *geniCodeCast (sym_link *, operand *, bool); -#define PRINTFUNC(x) void x (FILE *of, iCode *ic, char *s) +#define PRINTFUNC(x) void x (struct dbuf_s *dbuf, iCode *ic, char *s) /* forward definition of ic print functions */ PRINTFUNC (picGetValueAtAddr); PRINTFUNC (picSetValueAtAddr); @@ -133,37 +134,53 @@ iCodeTable codeTable[] = int printOperand (operand * op, FILE * file) { - sym_link *opetype; + struct dbuf_s dbuf; + int ret; int pnl = 0; - if (!op) - return 1; - if (!file) { file = stdout; pnl = 1; } + dbuf_init (&dbuf, 1024); + ret = dbuf_printOperand(op, &dbuf); + dbuf_write_and_destroy (&dbuf, file); + + if (pnl) + putc ('\n', file); + + return ret; +} + +int +dbuf_printOperand (operand * op, struct dbuf_s *dbuf) +{ + sym_link *opetype; + + if (!op) + return 1; + switch (op->type) { case VALUE: opetype = getSpec (operandType (op)); if (IS_FLOAT (opetype)) - fprintf (file, "%g {", SPEC_CVAL (opetype).v_float); + dbuf_printf (dbuf, "%g {", SPEC_CVAL (opetype).v_float); else if (IS_FIXED16X16 (opetype)) - fprintf (file, "%g {", doubleFromFixed16x16(SPEC_CVAL (opetype).v_fixed16x16)); + dbuf_printf (dbuf, "%g {", doubleFromFixed16x16(SPEC_CVAL (opetype).v_fixed16x16)); else - fprintf (file, "0x%x {", (unsigned) floatFromVal (op->operand.valOperand)); - printTypeChain (operandType (op), file); - fprintf (file, "}"); + dbuf_printf (dbuf, "0x%x {", (unsigned) floatFromVal (op->operand.valOperand)); + dbuf_printTypeChain (operandType (op), dbuf); + dbuf_append_char (dbuf, '}'); break; case SYMBOL: #define REGA 1 //#if REGA /* { */ if(REGA && !getenv("PRINT_SHORT_OPERANDS")) { - fprintf (file, "%s [k%d lr%d:%d so:%d]{ ia%d a2p%d re%d rm%d nos%d ru%d dp%d}", /*{ar%d rm%d ru%d p%d a%d u%d i%d au%d k%d ks%d}" , */ + dbuf_printf (dbuf, "%s [k%d lr%d:%d so:%d]{ ia%d a2p%d re%d rm%d nos%d ru%d dp%d}", /*{ar%d rm%d ru%d p%d a%d u%d i%d au%d k%d ks%d}" , */ (OP_SYMBOL (op)->rname[0] ? OP_SYMBOL (op)->rname : OP_SYMBOL (op)->name), op->key, OP_LIVEFROM (op), OP_LIVETO (op), @@ -173,11 +190,11 @@ printOperand (operand * op, FILE * file) OP_SYMBOL(op)->ruonly,OP_SYMBOL(op)->dptr ); { - fprintf (file, "{"); - printTypeChain (operandType (op), file); + dbuf_append_char (dbuf, '{'); + dbuf_printTypeChain (operandType (op), dbuf); if (SPIL_LOC (op) && IS_ITEMP (op)) - fprintf (file, "}{ sir@ %s", SPIL_LOC (op)->rname); - fprintf (file, "}"); + dbuf_printf (dbuf, "}{ sir@ %s", SPIL_LOC (op)->rname); + dbuf_append_char (dbuf, '}'); } @@ -188,42 +205,42 @@ printOperand (operand * op, FILE * file) { if (!OP_SYMBOL (op)->remat) if (OP_SYMBOL (op)->usl.spillLoc) - fprintf (file, "[%s]", (OP_SYMBOL (op)->usl.spillLoc->rname[0] ? + dbuf_printf (dbuf, "[%s]", (OP_SYMBOL (op)->usl.spillLoc->rname[0] ? OP_SYMBOL (op)->usl.spillLoc->rname : OP_SYMBOL (op)->usl.spillLoc->name)); else - fprintf (file, "[err]"); + dbuf_append_str (dbuf, "[err]"); else - fprintf (file, "[remat]"); + dbuf_append_str (dbuf, "[remat]"); } else { int i; - fprintf (file, "["); + dbuf_append_char (dbuf, '['); for (i = 0; i < OP_SYMBOL (op)->nRegs; i++) - fprintf (file, "%s ", port->getRegName (OP_SYMBOL (op)->regs[i])); - fprintf (file, "]"); + dbuf_printf (dbuf, "%s ", port->getRegName (OP_SYMBOL (op)->regs[i])); + dbuf_append_char (dbuf, ']'); } } //#else /* } else { */ } else { /* (getenv("PRINT_SHORT_OPERANDS") != NULL) */ - fprintf (file, "%s ", (OP_SYMBOL (op)->rname[0] ? OP_SYMBOL (op)->rname : OP_SYMBOL (op)->name)); + dbuf_printf (dbuf, "%s ", (OP_SYMBOL (op)->rname[0] ? OP_SYMBOL (op)->rname : OP_SYMBOL (op)->name)); if(getenv("PRINT_SHORT_OPERANDS")[0] < '1') { - fprintf (file, "[lr%d:%d so:%d]", + dbuf_printf (dbuf, "[lr%d:%d so:%d]", OP_LIVEFROM (op), OP_LIVETO (op), OP_SYMBOL (op)->stack); } if(getenv("PRINT_SHORT_OPERANDS")[0] < '2') { - fprintf (file, "{"); - printTypeChain (operandType (op), file); + dbuf_append_char (dbuf, '{'); + dbuf_printTypeChain (operandType (op), dbuf); if (SPIL_LOC (op) && IS_ITEMP (op)) - fprintf (file, "}{ sir@ %s", SPIL_LOC (op)->rname); - fprintf (file, "}"); + dbuf_printf (dbuf, "}{ sir@ %s", SPIL_LOC (op)->rname); + dbuf_append_char (dbuf, '}'); } /* if assigned to registers */ @@ -233,21 +250,21 @@ printOperand (operand * op, FILE * file) { if (!OP_SYMBOL (op)->remat) if (OP_SYMBOL (op)->usl.spillLoc) - fprintf (file, "[%s]", (OP_SYMBOL (op)->usl.spillLoc->rname[0] ? + dbuf_printf (dbuf, "[%s]", (OP_SYMBOL (op)->usl.spillLoc->rname[0] ? OP_SYMBOL (op)->usl.spillLoc->rname : OP_SYMBOL (op)->usl.spillLoc->name)); else - fprintf (file, "[err]"); + dbuf_append_str (dbuf, "[err]"); else - fprintf (file, "[remat]"); + dbuf_append_str (dbuf, "[remat]"); } else { int i; - fprintf (file, "["); + dbuf_append_char (dbuf, '['); for (i = 0; i < OP_SYMBOL (op)->nRegs; i++) - fprintf (file, "%s ", port->getRegName (OP_SYMBOL (op)->regs[i])); - fprintf (file, "]"); + dbuf_printf (dbuf, "%s ", port->getRegName (OP_SYMBOL (op)->regs[i])); + dbuf_append_char (dbuf, ']'); } } //#endif /* } */ @@ -255,14 +272,12 @@ printOperand (operand * op, FILE * file) break; case TYPE: - fprintf (file, "("); - printTypeChain (op->operand.typeOperand, file); - fprintf (file, ")"); + dbuf_append_char (dbuf, '('); + dbuf_printTypeChain (op->operand.typeOperand, dbuf); + dbuf_append_char (dbuf, ')'); break; } - if (pnl) - fprintf (file, "\n"); return 0; } @@ -272,197 +287,195 @@ printOperand (operand * op, FILE * file) /*-----------------------------------------------------------------*/ PRINTFUNC (picGetValueAtAddr) { - fprintf (of, "\t"); - printOperand (IC_RESULT (ic), of); - fprintf (of, " = "); - fprintf (of, "@["); - printOperand (IC_LEFT (ic), of); - fprintf (of, "]"); - - fprintf (of, "\n"); + dbuf_append_char (dbuf, '\t'); + dbuf_printOperand (IC_RESULT (ic), dbuf); + dbuf_append_str (dbuf, " = "); + dbuf_append_str (dbuf, "@["); + dbuf_printOperand (IC_LEFT (ic), dbuf); + dbuf_append_str (dbuf, "]\n"); } PRINTFUNC (picSetValueAtAddr) { - fprintf (of, "\t"); - fprintf (of, "*["); - printOperand (IC_LEFT (ic), of); - fprintf (of, "] = "); - printOperand (IC_RIGHT (ic), of); - fprintf (of, "\n"); + dbuf_append_char (dbuf, '\t'); + dbuf_append_str (dbuf, "*["); + dbuf_printOperand (IC_LEFT (ic), dbuf); + dbuf_append_str (dbuf, "] = "); + dbuf_printOperand (IC_RIGHT (ic), dbuf); + dbuf_append_char (dbuf, '\n'); } PRINTFUNC (picAddrOf) { - fprintf (of, "\t"); - printOperand (IC_RESULT (ic), of); + dbuf_append_char (dbuf, '\t'); + dbuf_printOperand (IC_RESULT (ic), dbuf); if (IS_ITEMP (IC_LEFT (ic))) - fprintf (of, " = "); + dbuf_append_str (dbuf, " = "); else - fprintf (of, " = &["); - printOperand (IC_LEFT (ic), of); + dbuf_append_str (dbuf, " = &["); + dbuf_printOperand (IC_LEFT (ic), dbuf); if (IC_RIGHT (ic)) { if (IS_ITEMP (IC_LEFT (ic))) - fprintf (of, " offsetAdd "); + dbuf_append_str (dbuf, " offsetAdd "); else - fprintf (of, " , "); - printOperand (IC_RIGHT (ic), of); + dbuf_append_str (dbuf, " , "); + dbuf_printOperand (IC_RIGHT (ic), dbuf); } if (IS_ITEMP (IC_LEFT (ic))) - fprintf (of, "\n"); + dbuf_append_char (dbuf, '\n'); else - fprintf (of, "]\n"); + dbuf_append_str (dbuf, "]\n"); } PRINTFUNC (picJumpTable) { symbol *sym; - fprintf (of, "\t"); - fprintf (of, "%s\t", s); - printOperand (IC_JTCOND (ic), of); - fprintf (of, "\n"); + dbuf_append_char (dbuf, '\t'); + dbuf_printf (dbuf, "%s\t", s); + dbuf_printOperand (IC_JTCOND (ic), dbuf); + dbuf_append_char (dbuf, '\n'); for (sym = setFirstItem (IC_JTLABELS (ic)); sym; sym = setNextItem (IC_JTLABELS (ic))) - fprintf (of, "\t\t\t%s\n", sym->name); + dbuf_printf (dbuf, "\t\t\t%s\n", sym->name); } PRINTFUNC (picGeneric) { - fprintf (of, "\t"); - printOperand (IC_RESULT (ic), of); - fprintf (of, " = "); - printOperand (IC_LEFT (ic), of); - fprintf (of, " %s ", s); - printOperand (IC_RIGHT (ic), of); - fprintf (of, "\n"); + dbuf_append_char (dbuf, '\t'); + dbuf_printOperand (IC_RESULT (ic), dbuf); + dbuf_append_str (dbuf, " = "); + dbuf_printOperand (IC_LEFT (ic), dbuf); + dbuf_printf (dbuf, " %s ", s); + dbuf_printOperand (IC_RIGHT (ic), dbuf); + dbuf_append_char (dbuf, '\n'); } PRINTFUNC (picGenericOne) { - fprintf (of, "\t"); + dbuf_append_char (dbuf, '\t'); if (IC_RESULT (ic)) { - printOperand (IC_RESULT (ic), of); - fprintf (of, " = "); + dbuf_printOperand (IC_RESULT (ic), dbuf); + dbuf_append_str (dbuf, " = "); } if (IC_LEFT (ic)) { - fprintf (of, "%s ", s); - printOperand (IC_LEFT (ic), of); + dbuf_printf (dbuf, "%s ", s); + dbuf_printOperand (IC_LEFT (ic), dbuf); } if (!IC_RESULT (ic) && !IC_LEFT (ic)) - fprintf (of, s); + dbuf_append_str (dbuf, s); if (ic->op == SEND || ic->op == RECEIVE) { - fprintf(of,"{argreg = %d}",ic->argreg); + dbuf_printf (dbuf,"{argreg = %d}",ic->argreg); } if (ic->op == IPUSH) { - fprintf(of,"{parmPush = %d}",ic->parmPush); + dbuf_printf (dbuf,"{parmPush = %d}",ic->parmPush); } - fprintf (of, "\n"); + dbuf_append_char (dbuf, '\n'); } PRINTFUNC (picCast) { - fprintf (of, "\t"); - printOperand (IC_RESULT (ic), of); - fprintf (of, " = "); - printOperand (IC_LEFT (ic), of); - printOperand (IC_RIGHT (ic), of); - fprintf (of, "\n"); + dbuf_append_char (dbuf, '\t'); + dbuf_printOperand (IC_RESULT (ic), dbuf); + dbuf_append_str (dbuf, " = "); + dbuf_printOperand (IC_LEFT (ic), dbuf); + dbuf_printOperand (IC_RIGHT (ic), dbuf); + dbuf_append_char (dbuf, '\n'); } PRINTFUNC (picAssign) { - fprintf (of, "\t"); + dbuf_append_char (dbuf, '\t'); if (IC_RESULT (ic)->isaddr && IS_ITEMP (IC_RESULT (ic))) - fprintf (of, "*("); + dbuf_append_str (dbuf, "*("); - printOperand (IC_RESULT (ic), of); + dbuf_printOperand (IC_RESULT (ic), dbuf); if (IC_RESULT (ic)->isaddr && IS_ITEMP (IC_RESULT (ic))) - fprintf (of, ")"); + dbuf_append_char (dbuf, ')'); - fprintf (of, " %s ", s); - printOperand (IC_RIGHT (ic), of); + dbuf_printf (dbuf, " %s ", s); + dbuf_printOperand (IC_RIGHT (ic), dbuf); - fprintf (of, "\n"); + dbuf_append_char (dbuf, '\n'); } PRINTFUNC (picLabel) { - fprintf (of, " %s($%d) :\n", IC_LABEL (ic)->name, IC_LABEL (ic)->key); + dbuf_printf (dbuf, " %s($%d) :\n", IC_LABEL (ic)->name, IC_LABEL (ic)->key); } PRINTFUNC (picGoto) { - fprintf (of, "\t"); - fprintf (of, " goto %s($%d)\n", IC_LABEL (ic)->name, IC_LABEL (ic)->key); + dbuf_append_char (dbuf, '\t'); + dbuf_printf (dbuf, " goto %s($%d)\n", IC_LABEL (ic)->name, IC_LABEL (ic)->key); } PRINTFUNC (picIfx) { - fprintf (of, "\t"); - fprintf (of, "if "); - printOperand (IC_COND (ic), of); + dbuf_append_char (dbuf, '\t'); + dbuf_append_str (dbuf, "if "); + dbuf_printOperand (IC_COND (ic), dbuf); if (!IC_TRUE (ic)) - fprintf (of, " == 0 goto %s($%d)\n", IC_FALSE (ic)->name, IC_FALSE (ic)->key); + dbuf_printf (dbuf, " == 0 goto %s($%d)\n", IC_FALSE (ic)->name, IC_FALSE (ic)->key); else { - fprintf (of, " != 0 goto %s($%d)\n", IC_TRUE (ic)->name, IC_TRUE (ic)->key); + dbuf_printf (dbuf, " != 0 goto %s($%d)\n", IC_TRUE (ic)->name, IC_TRUE (ic)->key); if (IC_FALSE (ic)) - fprintf (of, "\tzzgoto %s\n", IC_FALSE (ic)->name); + dbuf_printf (dbuf, "\tzzgoto %s\n", IC_FALSE (ic)->name); } } PRINTFUNC (picInline) { - fprintf (of, "%s", IC_INLINE (ic)); + dbuf_append_str (dbuf, IC_INLINE (ic)); } PRINTFUNC (picReceive) { - printOperand (IC_RESULT (ic), of); - fprintf (of, " = %s ", s); - printOperand (IC_LEFT (ic), of); - fprintf (of, "\n"); + dbuf_printOperand (IC_RESULT (ic), dbuf); + dbuf_printf (dbuf, " = %s ", s); + dbuf_printOperand (IC_LEFT (ic), dbuf); + dbuf_append_char (dbuf, '\n'); } PRINTFUNC (picDummyRead) { - fprintf (of, "\t"); - fprintf (of, "%s ", s); - printOperand (IC_RIGHT (ic), of); - fprintf (of, "\n"); + dbuf_append_char (dbuf, '\t'); + dbuf_printf (dbuf, "%s ", s); + dbuf_printOperand (IC_RIGHT (ic), dbuf); + dbuf_append_char (dbuf, '\n'); } PRINTFUNC (picCritical) { - fprintf (of, "\t"); + dbuf_append_char (dbuf, '\t'); if (IC_RESULT (ic)) - printOperand (IC_RESULT (ic), of); + dbuf_printOperand (IC_RESULT (ic), dbuf); else - fprintf (of, "(stack)"); - fprintf (of, " = %s ", s); - fprintf (of, "\n"); + dbuf_append_str (dbuf, "(stack)"); + dbuf_printf (dbuf, " = %s ", s); + dbuf_append_char (dbuf, '\n'); } PRINTFUNC (picEndCritical) { - fprintf (of, "\t"); - fprintf (of, "%s = ", s); + dbuf_append_char (dbuf, '\t'); + dbuf_printf (dbuf, "%s = ", s); if (IC_RIGHT (ic)) - printOperand (IC_RIGHT (ic), of); + dbuf_printOperand (IC_RIGHT (ic), dbuf); else - fprintf (of, "(stack)"); - fprintf (of, "\n"); + dbuf_append_str (dbuf, "(stack)"); + dbuf_append_char (dbuf, '\n'); } /*-----------------------------------------------------------------*/ @@ -473,6 +486,7 @@ piCode (void *item, FILE * of) { iCode *ic = item; iCodeTable *icTab; + struct dbuf_s dbuf; if (!of) of = stdout; @@ -481,7 +495,9 @@ piCode (void *item, FILE * of) fprintf (of, "%s(%d:%d:%d:%d:%d)\t", ic->filename, ic->lineno, ic->seq, ic->key, ic->depth, ic->supportRtn); - icTab->iCodePrint (of, ic, icTab->printName); + dbuf_init (&dbuf, 1024); + icTab->iCodePrint (&dbuf, ic, icTab->printName); + dbuf_write_and_destroy (&dbuf, of); return 1; } @@ -497,6 +513,7 @@ printiCChain (iCode * icChain, FILE * of) { iCode *loop; iCodeTable *icTab; + struct dbuf_s dbuf; if (!of) of = stdout; @@ -508,7 +525,9 @@ printiCChain (iCode * icChain, FILE * of) loop->filename, loop->lineno, loop->seq, loop->key, loop->depth, loop->supportRtn); - icTab->iCodePrint (of, loop, icTab->printName); + dbuf_init(&dbuf, 1024); + icTab->iCodePrint (&dbuf, loop, icTab->printName); + dbuf_write_and_destroy (&dbuf, of); } } } diff --git a/src/SDCCicode.h b/src/SDCCicode.h index e4c3c1be..a05c67c4 100644 --- a/src/SDCCicode.h +++ b/src/SDCCicode.h @@ -201,7 +201,7 @@ typedef struct icodeFuncTable { int icode; char *printName; - void (*iCodePrint) (FILE *, iCode *, char *); + void (*iCodePrint) (struct dbuf_s *, iCode *, char *); iCode *(*iCodeCopy) (iCode *); } iCodeTable; @@ -328,6 +328,7 @@ operand *operandFromLink (sym_link *); sym_link *aggrToPtr (sym_link *, bool); int aggrToPtrDclType (sym_link *, bool); int piCode (void *, FILE *); +int dbuf_printOperand (operand *, struct dbuf_s *); int printOperand (operand *, FILE *); void setOperandType (operand *, sym_link *); bool isOperandInFarSpace (operand *); diff --git a/src/SDCCmain.c b/src/SDCCmain.c index 8cb6954b..8b9f8801 100644 --- a/src/SDCCmain.c +++ b/src/SDCCmain.c @@ -539,20 +539,21 @@ setParseWithComma (set **dest, const char *src) --end; ++end; - dbuf_init(&dbuf, 16); - p = src; while (src < end) { + dbuf_init(&dbuf, 16); + while (p < end && ',' != *p) ++p; dbuf_append(&dbuf, src, p - src); - addSet(dest, Safe_strdup(dbuf_c_str(&dbuf))); - dbuf_set_size(&dbuf, 0); + + /* null terminate the buffer */ + dbuf_c_str(&dbuf); + addSet(dest, dbuf_detach(&dbuf)); + src = ++p; } - - dbuf_destroy(&dbuf); } /*-----------------------------------------------------------------*/ @@ -2106,7 +2107,6 @@ preProcess (char **envp) perror ("Preproc file not found"); exit (1); } - addSetHead (&pipeSet, yyin); } return 0; @@ -2336,9 +2336,6 @@ main (int argc, char **argv, char **envp) exit (1); } - /* install atexit handler */ - atexit(rm_tmpfiles); - /* install signal handler; it's only purpose is to call exit() to remove temp files */ if (!getenv("SDCC_LEAVE_SIGNALS")) @@ -2428,7 +2425,6 @@ main (int argc, char **argv, char **envp) if (pclose(yyin)) fatalError = 1; - deleteSetItem(&pipeSet, yyin); if (fatalError) { exit (1); diff --git a/src/SDCCmem.c b/src/SDCCmem.c index cb583388..a128b8b6 100644 --- a/src/SDCCmem.c +++ b/src/SDCCmem.c @@ -3,6 +3,7 @@ /*-----------------------------------------------------------------*/ #include "common.h" +#include "dbuf_string.h" /* memory segments */ memmap *xstack = NULL; /* xternal stack data */ @@ -70,13 +71,10 @@ allocMap (char rspace, /* sfr space */ map->sname = name; map->dbName = dbName; map->ptrType = ptrType; - if (!(map->oFile = tempfile ())) - { - werror (E_TMPFILE_FAILED); - exit (1); - } - addSetHead (&tmpfileSet, map->oFile); map->syms = NULL; + + dbuf_init(&map->oBuf, 4096); + return map; } @@ -1009,7 +1007,7 @@ redoStackOffsets (void) /* printAllocInfoSeg- print the allocation for a given section */ /*-----------------------------------------------------------------*/ static void -printAllocInfoSeg (memmap * map, symbol * func, FILE * of) +printAllocInfoSeg (memmap * map, symbol * func, struct dbuf_s *oBuf) { symbol *sym; @@ -1027,7 +1025,7 @@ printAllocInfoSeg (memmap * map, symbol * func, FILE * of) if (sym->localof != func) continue; - fprintf (of, ";%-25s Allocated ", sym->name); + dbuf_printf (oBuf, ";%-25s Allocated ", sym->name); /* if assigned to registers */ if (!sym->allocreq && sym->reqv) @@ -1037,10 +1035,10 @@ printAllocInfoSeg (memmap * map, symbol * func, FILE * of) sym = OP_SYMBOL (sym->reqv); if (!sym->isspilt || sym->remat) { - fprintf (of, "to registers "); + dbuf_append_str (oBuf, "to registers "); for (i = 0; i < 4 && sym->regs[i]; i++) - fprintf (of, "%s ", port->getRegName (sym->regs[i])); - fprintf (of, "\n"); + dbuf_printf (oBuf, "%s ", port->getRegName (sym->regs[i])); + dbuf_append_char (oBuf, '\n'); continue; } else @@ -1052,12 +1050,12 @@ printAllocInfoSeg (memmap * map, symbol * func, FILE * of) /* if on stack */ if (sym->onStack) { - fprintf (of, "to stack - offset %d\n", sym->stack); + dbuf_printf (oBuf, "to stack - offset %d\n", sym->stack); continue; } /* otherwise give rname */ - fprintf (of, "with name '%s'\n", sym->rname); + dbuf_printf (oBuf, "with name '%s'\n", sym->rname); } } @@ -1132,27 +1130,24 @@ doOverlays (eBBlock ** ebbs, int count) /* printAllocInfo - prints allocation information for a function */ /*-----------------------------------------------------------------*/ void -printAllocInfo (symbol * func, FILE * of) +printAllocInfo (symbol * func, struct dbuf_s * oBuf) { if (!func) return; - if (!of) - of = stdout; - /* must be called after register allocation is complete */ - fprintf (of, ";------------------------------------------------------------\n"); - fprintf (of, ";Allocation info for local variables in function '%s'\n", func->name); - fprintf (of, ";------------------------------------------------------------\n"); - - printAllocInfoSeg (xstack, func, of); - printAllocInfoSeg (istack, func, of); - printAllocInfoSeg (code, func, of); - printAllocInfoSeg (data, func, of); - printAllocInfoSeg (xdata, func, of); - printAllocInfoSeg (idata, func, of); - printAllocInfoSeg (sfr, func, of); - printAllocInfoSeg (sfrbit, func, of); + dbuf_append_str (oBuf, ";------------------------------------------------------------\n"); + dbuf_printf (oBuf, ";Allocation info for local variables in function '%s'\n", func->name); + dbuf_append_str (oBuf, ";------------------------------------------------------------\n"); + + printAllocInfoSeg (xstack, func, oBuf); + printAllocInfoSeg (istack, func, oBuf); + printAllocInfoSeg (code, func, oBuf); + printAllocInfoSeg (data, func, oBuf); + printAllocInfoSeg (xdata, func, oBuf); + printAllocInfoSeg (idata, func, oBuf); + printAllocInfoSeg (sfr, func, oBuf); + printAllocInfoSeg (sfrbit, func, oBuf); { set *ovrset; @@ -1163,10 +1158,10 @@ printAllocInfo (symbol * func, FILE * of) ovrset = setNextItem (ovrSetSets)) { overlay->syms = ovrset; - printAllocInfoSeg (overlay, func, of); + printAllocInfoSeg (overlay, func, oBuf); } overlay->syms = tempOverlaySyms; } - fprintf (of, ";------------------------------------------------------------\n"); + dbuf_append_str (oBuf, ";------------------------------------------------------------\n"); } diff --git a/src/SDCCmem.h b/src/SDCCmem.h index 9b212424..c97e1913 100644 --- a/src/SDCCmem.h +++ b/src/SDCCmem.h @@ -5,6 +5,8 @@ #ifndef SDCCMEM_H #define SDCCMEM_H +#include "dbuf.h" + struct set; struct value; struct eBBlock; @@ -23,7 +25,7 @@ typedef struct memmap unsigned bitsp:1; /* 1 = bit addressable space */ unsigned codesp:1; /* 1 = code space */ unsigned regsp:1; /* 1= sfr space */ - FILE *oFile; /* object file associated */ + struct dbuf_s oBuf; /* object buffer associated */ struct set *syms; /* symbols defined in this segment */ } memmap; @@ -103,7 +105,7 @@ int allocVariables (struct symbol *); void overlay2Set (); void overlay2data (); void redoStackOffsets (); -void printAllocInfo (struct symbol *, FILE *); +void printAllocInfo (struct symbol *, struct dbuf_s *); void doOverlays (struct eBBlock **, int count); void deleteFromSeg(struct symbol *); #endif diff --git a/src/SDCCpeeph.c b/src/SDCCpeeph.c index ef221546..daf96c3a 100644 --- a/src/SDCCpeeph.c +++ b/src/SDCCpeeph.c @@ -24,6 +24,7 @@ -------------------------------------------------------------------------*/ #include "common.h" +#include "dbuf_string.h" #define ISCHARDIGIT(c) isdigit((unsigned char)c) #define ISCHARSPACE(c) isspace((unsigned char)c) @@ -1198,14 +1199,11 @@ callFuncByName (char *fname, /* printLine - prints a line chain into a given file */ /*-----------------------------------------------------------------*/ void -printLine (lineNode * head, FILE * of) +printLine (lineNode * head, struct dbuf_s * oBuf) { iCode *last_ic = NULL; bool debug_iCode_tracking = (getenv("DEBUG_ICODE_TRACKING")!=NULL); - if (!of) - of = stdout; - while (head) { if (head->ic!=last_ic) @@ -1214,23 +1212,23 @@ printLine (lineNode * head, FILE * of) if (debug_iCode_tracking) { if (head->ic) - fprintf (of, "; block = %d, seq = %d\n", + dbuf_printf (oBuf, "; block = %d, seq = %d\n", head->ic->block, head->ic->seq); else - fprintf (of, "; iCode lost\n"); + dbuf_append_str (oBuf, "; iCode lost\n"); } } /* don't indent comments & labels */ if (head->line && (head->isComment || head->isLabel)) { - fprintf (of, "%s\n", head->line); + dbuf_printf (oBuf, "%s\n", head->line); } else { if (head->isInline && *head->line=='#') { // comment out preprocessor directives in inline asm - fprintf (of, ";"); + dbuf_append_char (oBuf, ';'); } - fprintf (of, "\t%s\n", head->line); + dbuf_printf (oBuf, "\t%s\n", head->line); } head = head->next; } @@ -1274,7 +1272,7 @@ newPeepRule (lineNode * match, /* newLineNode - creates a new peep line */ /*-----------------------------------------------------------------*/ lineNode * -newLineNode (char *line) +newLineNode (const char *line) { lineNode *pl; diff --git a/src/SDCCpeeph.h b/src/SDCCpeeph.h index d5e38816..0c1b2f85 100644 --- a/src/SDCCpeeph.h +++ b/src/SDCCpeeph.h @@ -73,8 +73,8 @@ bool isLabelDefinition (const char *line, const char **start, int *len, extern hTab *labelHash; labelHashEntry *getLabelRef (const char *label, lineNode *head); -void printLine (lineNode *, FILE *); -lineNode *newLineNode (char *); +void printLine (lineNode *, struct dbuf_s *); +lineNode *newLineNode (const char *); lineNode *connectLine (lineNode *, lineNode *); void initPeepHole (void); void peepHole (lineNode **); diff --git a/src/SDCCsymt.c b/src/SDCCsymt.c index ddedaf06..d9ca91a1 100644 --- a/src/SDCCsymt.c +++ b/src/SDCCsymt.c @@ -23,6 +23,7 @@ #include "common.h" #include "newalloc.h" +#include "dbuf_string.h" #include "SDCCsymt.h" @@ -2795,10 +2796,8 @@ void PT(sym_link *type) void printTypeChain (sym_link * start, FILE * of) { + struct dbuf_s dbuf; int nlr = 0; - value *args; - sym_link * type, * search; - STORAGE_CLASS scls; if (!of) { @@ -2806,8 +2805,23 @@ printTypeChain (sym_link * start, FILE * of) nlr = 1; } + dbuf_init (&dbuf, 1024); + dbuf_printTypeChain (start, &dbuf); + dbuf_write_and_destroy (&dbuf, of); + + if (nlr) + putc ('\n', of); +} + +void +dbuf_printTypeChain (sym_link * start, struct dbuf_s *dbuf) +{ + value *args; + sym_link * type, * search; + STORAGE_CLASS scls; + if (start==NULL) { - fprintf (of, "void"); + dbuf_append_str (dbuf, "void"); return; } @@ -2827,18 +2841,18 @@ printTypeChain (sym_link * start, FILE * of) if (type==start) { switch (scls) { - case S_DATA: fprintf (of, "data-"); break; - case S_XDATA: fprintf (of, "xdata-"); break; - case S_SFR: fprintf (of, "sfr-"); break; - case S_SBIT: fprintf (of, "sbit-"); break; - case S_CODE: fprintf (of, "code-"); break; - case S_IDATA: fprintf (of, "idata-"); break; - case S_PDATA: fprintf (of, "pdata-"); break; - case S_LITERAL: fprintf (of, "literal-"); break; - case S_STACK: fprintf (of, "stack-"); break; - case S_XSTACK: fprintf (of, "xstack-"); break; - case S_BIT: fprintf (of, "bit-"); break; - case S_EEPROM: fprintf (of, "eeprom-"); break; + case S_DATA: dbuf_append_str (dbuf, "data-"); break; + case S_XDATA: dbuf_append_str (dbuf, "xdata-"); break; + case S_SFR: dbuf_append_str (dbuf, "sfr-"); break; + case S_SBIT: dbuf_append_str (dbuf, "sbit-"); break; + case S_CODE: dbuf_append_str (dbuf, "code-"); break; + case S_IDATA: dbuf_append_str (dbuf, "idata-"); break; + case S_PDATA: dbuf_append_str (dbuf, "pdata-"); break; + case S_LITERAL: dbuf_append_str (dbuf, "literal-"); break; + case S_STACK: dbuf_append_str (dbuf, "stack-"); break; + case S_XSTACK: dbuf_append_str (dbuf, "xstack-"); break; + case S_BIT: dbuf_append_str (dbuf, "bit-"); break; + case S_EEPROM: dbuf_append_str (dbuf, "eeprom-"); break; default: break; } } @@ -2847,57 +2861,57 @@ printTypeChain (sym_link * start, FILE * of) { if (!IS_FUNC(type)) { if (DCL_PTR_VOLATILE (type)) { - fprintf (of, "volatile-"); + dbuf_append_str (dbuf, "volatile-"); } if (DCL_PTR_CONST (type)) { - fprintf (of, "const-"); + dbuf_append_str (dbuf, "const-"); } } switch (DCL_TYPE (type)) { case FUNCTION: - fprintf (of, "function %s %s", + dbuf_printf (dbuf, "function %s %s", (IFFUNC_ISBUILTIN(type) ? "__builtin__" : " "), (IFFUNC_ISJAVANATIVE(type) ? "_JavaNative" : " ")); - fprintf (of, "( "); + dbuf_append_str (dbuf, "( "); for (args = FUNC_ARGS(type); args; args=args->next) { - printTypeChain(args->type, of); + dbuf_printTypeChain(args->type, dbuf); if (args->next) - fprintf(of, ", "); + dbuf_append_str (dbuf, ", "); } - fprintf (of, ") "); + dbuf_append_str (dbuf, ") "); break; case GPOINTER: - fprintf (of, "generic* "); + dbuf_append_str (dbuf, "generic* "); break; case CPOINTER: - fprintf (of, "code* "); + dbuf_append_str (dbuf, "code* "); break; case FPOINTER: - fprintf (of, "xdata* "); + dbuf_append_str (dbuf, "xdata* "); break; case EEPPOINTER: - fprintf (of, "eeprom* "); + dbuf_append_str (dbuf, "eeprom* "); break; case POINTER: - fprintf (of, "near* "); + dbuf_append_str (dbuf, "near* "); break; case IPOINTER: - fprintf (of, "idata* "); + dbuf_append_str (dbuf, "idata* "); break; case PPOINTER: - fprintf (of, "pdata* "); + dbuf_append_str (dbuf, "pdata* "); break; case UPOINTER: - fprintf (of, "unknown* "); + dbuf_append_str (dbuf, "unknown* "); break; case ARRAY: if (DCL_ELEM(type)) { - fprintf (of, "[%d] ", DCL_ELEM(type)); + dbuf_printf (dbuf, "[%d] ", DCL_ELEM(type)); } else { - fprintf (of, "[] "); + dbuf_append_str (dbuf, "[] "); } break; } @@ -2905,57 +2919,57 @@ printTypeChain (sym_link * start, FILE * of) else { if (SPEC_VOLATILE (type)) - fprintf (of, "volatile-"); + dbuf_append_str (dbuf, "volatile-"); if (SPEC_CONST (type)) - fprintf (of, "const-"); + dbuf_append_str (dbuf, "const-"); if (SPEC_USIGN (type)) - fprintf (of, "unsigned-"); + dbuf_append_str (dbuf, "unsigned-"); switch (SPEC_NOUN (type)) { case V_INT: if (IS_LONG (type)) - fprintf (of, "long-"); - fprintf (of, "int"); + dbuf_append_str (dbuf, "long-"); + dbuf_append_str (dbuf, "int"); break; case V_CHAR: - fprintf (of, "char"); + dbuf_append_str (dbuf, "char"); break; case V_VOID: - fprintf (of, "void"); + dbuf_append_str (dbuf, "void"); break; case V_FLOAT: - fprintf (of, "float"); + dbuf_append_str (dbuf, "float"); break; case V_FIXED16X16: - fprintf (of, "fixed16x16"); + dbuf_append_str (dbuf, "fixed16x16"); break; case V_STRUCT: - fprintf (of, "struct %s", SPEC_STRUCT (type)->tag); + dbuf_printf (dbuf, "struct %s", SPEC_STRUCT (type)->tag); break; case V_SBIT: - fprintf (of, "sbit"); + dbuf_append_str (dbuf, "sbit"); break; case V_BIT: - fprintf (of, "bit"); + dbuf_append_str (dbuf, "bit"); break; case V_BITFIELD: - fprintf (of, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type)); + dbuf_printf (dbuf, "bitfield {%d,%d}", SPEC_BSTR (type), SPEC_BLEN (type)); break; case V_DOUBLE: - fprintf (of, "double"); + dbuf_append_str (dbuf, "double"); break; default: - fprintf (of, "unknown type"); + dbuf_append_str (dbuf, "unknown type"); break; } } @@ -2964,10 +2978,8 @@ printTypeChain (sym_link * start, FILE * of) search = search->next; type = search; if (type) - fputc (' ', of); + dbuf_append_char(dbuf, ' '); } - if (nlr) - fprintf (of, "\n"); } /*--------------------------------------------------------------------*/ diff --git a/src/SDCCsymt.h b/src/SDCCsymt.h index be9fcadd..6ad8662b 100644 --- a/src/SDCCsymt.h +++ b/src/SDCCsymt.h @@ -29,6 +29,7 @@ #define SDCC_NAME_MAX 3*SDCC_SYMNAME_MAX // big enough for ___etc #include "SDCChasht.h" #include "SDCCglobl.h" +#include "dbuf.h" #define INTNO_MAX 255 /* maximum allowed interrupt number */ #define INTNO_UNSPEC (INTNO_MAX+1) /* interrupt number unspecified */ @@ -614,6 +615,7 @@ void processFuncPtrArgs (sym_link *); void processFuncArgs (symbol *); int isSymbolEqual (symbol *, symbol *); int powof2 (TYPE_TARGET_ULONG); +void dbuf_printTypeChain (sym_link *, struct dbuf_s *); void printTypeChain (sym_link *, FILE *); void printTypeChainRaw (sym_link *, FILE *); void initCSupport (); diff --git a/src/SDCCutil.c b/src/SDCCutil.c index 34db23da..048f5f54 100644 --- a/src/SDCCutil.c +++ b/src/SDCCutil.c @@ -34,6 +34,7 @@ #include "SDCCmacro.h" #include "SDCCutil.h" #include "newalloc.h" +#include "dbuf_string.h" #ifndef _WIN32 #include "findme.h" #endif @@ -82,13 +83,16 @@ appendStrSet(set *list, const char *pre, const char *post) for (item = setFirstItem(list); item != NULL; item = setNextItem(list)) { dbuf_init(&dbuf, PATH_MAX); + if (pre != NULL) - dbuf_append(&dbuf, pre, strlen(pre)); - dbuf_append(&dbuf, item, strlen(item)); + dbuf_append_str(&dbuf, pre); + dbuf_append_str(&dbuf, item); if (post != NULL) - dbuf_append(&dbuf, post, strlen(post)); - addSet(&new_list, (void *)dbuf_c_str(&dbuf)); - dbuf_detach(&dbuf); + dbuf_append_str(&dbuf, post); + + /* null terminate the buffer */ + dbuf_c_str(&dbuf); + addSet(&new_list, dbuf_detach(&dbuf)); } return new_list; @@ -107,8 +111,8 @@ joinStrSet(set *list) for (s = setFirstItem(list); s != NULL; s = setNextItem(list)) { - dbuf_append(&dbuf, s, strlen(s)); - dbuf_append(&dbuf, " ", 1); + dbuf_append_str(&dbuf, s); + dbuf_append_char(&dbuf, ' '); } s = dbuf_c_str(&dbuf); @@ -339,7 +343,7 @@ init_pragma_token(struct pragma_token_s *token) char * get_pragma_token(const char *s, struct pragma_token_s *token) { - dbuf_set_size(&token->dbuf, 0); + dbuf_set_length(&token->dbuf, 0); /* skip leading spaces */ while ('\n' != *s && isspace(*s)) @@ -366,7 +370,7 @@ get_pragma_token(const char *s, struct pragma_token_s *token) { while ('\0' != *s && !isspace(*s)) { - dbuf_append(&token->dbuf, s, 1); + dbuf_append_char(&token->dbuf, *s); ++s; } diff --git a/src/asm.c b/src/asm.c index 9b75aac1..154f11ff 100644 --- a/src/asm.c +++ b/src/asm.c @@ -8,6 +8,7 @@ */ #include "common.h" #include "asm.h" +#include "dbuf_string.h" /* A 'token' is like !blah or %24f and is under the programmers control. */ @@ -36,25 +37,8 @@ FileBaseName (char *fileFullName) return p; } -static const char * -_findMapping (const char *szKey) -{ - return shash_find (_h, szKey); -} - -// Append a string onto another, and update the pointer to the end of -// the new string. -static char * -_appendAt (char *at, char *onto, const char *sz, size_t *max) -{ - wassert (at && onto && sz); - strncpyz (at, sz, *max); - *max -= strlen (at); - return at + strlen (at); -} - void -tvsprintf (char *buffer, size_t len, const char *format, va_list ap) +dbuf_tvprintf (struct dbuf_s *dbuf, const char *format, va_list ap) { // Under Linux PPC va_list is a structure instead of a primitive type, // and doesnt like being passed around. This version turns everything @@ -67,20 +51,17 @@ 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; - - // NULL terminate it to let strlen work. - *pInto = '\0'; + static int count; + struct dbuf_s tmpDBuf; + const char *noTokens; + char *p; + char token[MAX_TOKEN_LEN]; + const char *sz = format; + + dbuf_init(&tmpDBuf, INITIAL_INLINEASM); /* First pass: expand all of the macros */ - while (pIntoLen && *sz) + while (*sz) { if (*sz == '!') { @@ -95,9 +76,9 @@ tvsprintf (char *buffer, size_t len, const char *format, va_list ap) } *p = '\0'; /* Now find the token in the token list */ - if ((t = _findMapping (token))) + if ((t = shash_find (_h, token))) { - pInto = _appendAt (pInto, noTokens, t, &pIntoLen); + dbuf_append_str(&tmpDBuf, t); } else { @@ -107,26 +88,18 @@ tvsprintf (char *buffer, size_t len, const char *format, va_list ap) } else { - *pInto++ = *sz++; - pIntoLen--; + dbuf_append_char(&tmpDBuf, *sz++); } } - if (!pIntoLen) - { - fprintf(stderr, - "Internal error: tvsprintf overflowed on pass one.\n"); - // Might as well go on... - } - - *pInto = '\0'; - /* Second pass: Expand any macros that we own */ + noTokens = dbuf_c_str(&tmpDBuf); sz = noTokens; - pInto = newFormat; - pIntoLen = sizeof(newFormat); - while (pIntoLen && *sz) + /* recycle tmpDBuf */ + dbuf_init(&tmpDBuf, INITIAL_INLINEASM); + + while (*sz) { if (*sz == '%') { @@ -136,100 +109,96 @@ tvsprintf (char *buffer, size_t len, const char *format, va_list ap) { case 'C': // Code segment name. - pInto = _appendAt (pInto, newFormat, CODE_NAME, &pIntoLen); + dbuf_append_str(&tmpDBuf, CODE_NAME); sz++; break; + case 'F': // Source file name. - pInto = _appendAt (pInto, newFormat, fullSrcFileName, &pIntoLen); + dbuf_append_str(&tmpDBuf, fullSrcFileName); sz++; break; + case 'N': // Current function name. - pInto = _appendAt (pInto, newFormat, currFunc->rname, &pIntoLen); + dbuf_append_str(&tmpDBuf, currFunc->rname); sz++; break; + case 'I': - { - // Unique ID. - char id[20]; - SNPRINTF (id, sizeof(id), "%u", ++count); - pInto = _appendAt (pInto, newFormat, id, &pIntoLen); - sz++; - break; - } + // Unique ID. + dbuf_printf(&tmpDBuf, "%u", ++count); + sz++; + 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--; - } + dbuf_append_char(&tmpDBuf, '%'); + while (!isalpha ((unsigned char)*sz)) + dbuf_append_char(&tmpDBuf, *sz++); + + dbuf_append_char(&tmpDBuf, *sz++); } } else { - *pInto++ = *sz++; - pIntoLen--; + dbuf_append_char(&tmpDBuf, *sz++); } } - if (!pIntoLen) - { - fprintf(stderr, - "Internal error: tvsprintf overflowed on pass two.\n"); - // Might as well go on... - } + dbuf_free(noTokens); - *pInto = '\0'; + sz = dbuf_c_str(&tmpDBuf); - // Now do the actual printing -#if defined(HAVE_VSNPRINTF) - { - int wrlen; - wrlen = vsnprintf (buffer, len, newFormat, ap); + dbuf_vprintf(dbuf, sz, ap); - if (wrlen < 0 || (size_t)wrlen >= len) - { - fprintf(stderr, "Internal error: tvsprintf truncated.\n"); - } - } + dbuf_destroy(&tmpDBuf); +} -#else - vsprintf (buffer, newFormat, ap); - if (strlen(buffer) >= len) - { - fprintf(stderr, "Internal error: tvsprintf overflowed.\n"); - } -#endif +void +dbuf_tprintf (struct dbuf_s *dbuf, const char *szFormat,...) +{ + va_list ap; + va_start (ap, szFormat); + dbuf_tvprintf (dbuf, szFormat, ap); + va_end(ap); } void -tfprintf (FILE * fp, const char *szFormat,...) +tsprintf (char *buffer, size_t len, const char *szFormat,...) { va_list ap; - char buffer[INITIAL_INLINEASM]; + struct dbuf_s dbuf; + size_t copyLen; + + dbuf_init(&dbuf, INITIAL_INLINEASM); va_start (ap, szFormat); - tvsprintf (buffer, INITIAL_INLINEASM, szFormat, ap); + dbuf_tvprintf (&dbuf, szFormat, ap); va_end(ap); - fputs (buffer, fp); + + copyLen = min(len - 1, dbuf_get_length(&dbuf)); + memcpy(buffer, dbuf_get_buf(&dbuf), copyLen); + buffer[copyLen] = '\0'; + dbuf_destroy(&dbuf); } void -tsprintf (char *buffer, size_t len, const char *szFormat,...) +tfprintf (FILE *fp, const char *szFormat,...) { va_list ap; + struct dbuf_s dbuf; + size_t len; + + dbuf_init(&dbuf, INITIAL_INLINEASM); + va_start (ap, szFormat); - tvsprintf (buffer, len, szFormat, ap); + dbuf_tvprintf (&dbuf, szFormat, ap); va_end(ap); + + len = dbuf_get_length(&dbuf); + fwrite(dbuf_get_buf(&dbuf), 1, len, fp); + dbuf_destroy(&dbuf); } void @@ -249,42 +218,26 @@ asm_addTree (const ASM_MAPPINGS * pMappings) /*-----------------------------------------------------------------*/ /* printILine - return the readable i-code for this ic */ -/* */ -/* iCodePrint wants a file stream so we need a temporary file to */ -/* fool it */ /*-----------------------------------------------------------------*/ char * printILine (iCode *ic) { - static char verbalICode[1024]; - FILE *tmpFile; + char *verbalICode; + struct dbuf_s tmpBuf; iCodeTable *icTab=getTableEntry(ic->op); - int ret; - if (INLINEASM == ic->op) - return "inline"; + dbuf_init(&tmpBuf, 1024); - 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); + if (INLINEASM == ic->op) + dbuf_append (&tmpBuf, "inline", (sizeof "inline") - 1); + else { + /* stuff the temporary file with the readable icode */ + icTab->iCodePrint(&tmpBuf, ic, icTab->printName); + } - /* 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(). - */ - ret = fclose(tmpFile); - assert(!ret); - deleteSetItem(&tmpfileSet, tmpFile); + /* null terminate the buffer */ + dbuf_c_str(&tmpBuf); + verbalICode = dbuf_detach(&tmpBuf); /* kill the trailing NL */ if ('\n' == verbalICode[strlen(verbalICode) - 1]) diff --git a/src/asm.h b/src/asm.h index 23263cb9..473af8c1 100644 --- a/src/asm.h +++ b/src/asm.h @@ -1,9 +1,12 @@ #ifndef ASM_PORT_INCLUDE #define ASM_PORT_INCLUDE +#include "dbuf.h" + void tfprintf (FILE * fp, const char *szFormat, ...); void tsprintf (char *buffer, size_t len, const char *szFormat, ...); -void tvsprintf (char *buffer, size_t len, const char *szFormat, va_list ap); +void dbuf_tprintf (struct dbuf_s *dbuf, const char *szFormat, ...); +void dbuf_tvprintf (struct dbuf_s *dbuf, const char *szFormat, va_list ap); typedef struct { diff --git a/src/avr/gen.c b/src/avr/gen.c index 59dbabce..5e78cdd3 100644 --- a/src/avr/gen.c +++ b/src/avr/gen.c @@ -65,7 +65,7 @@ static struct { extern int avr_ptrRegReq; extern int avr_nRegs; -extern FILE *codeOutFile; +extern struct dbuf_s *codeOutBuf; #define RESULTONSTACK(x) \ (IC_RESULT(x) && IC_RESULT(x)->aop && \ IC_RESULT(x)->aop->type == AOP_STK ) @@ -5134,7 +5134,7 @@ genAVRCode (iCode * lic) recvCnt = 0; /* print the allocation information */ if (allocInfo) - printAllocInfo (currFunc, codeOutFile); + printAllocInfo (currFunc, codeOutBuf); /* if debug information required */ if (options.debug && currFunc) { debugFile->writeFunction (currFunc, lic); @@ -5360,6 +5360,6 @@ genAVRCode (iCode * lic) peepHole (&lineHead); /* now do the actual printing */ - printLine (lineHead, codeOutFile); + printLine (lineHead, codeOutBuf); return; } diff --git a/src/avr/main.c b/src/avr/main.c index 982bc5c5..efde4d2b 100644 --- a/src/avr/main.c +++ b/src/avr/main.c @@ -126,9 +126,9 @@ _avr_genAssemblerPreamble (FILE * of) /* Generate interrupt vector table. */ static int -_avr_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts) +_avr_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts) { - return TRUE; + return TRUE; } /* Indicate which extended bit operations this port supports */ diff --git a/src/ds390/gen.c b/src/ds390/gen.c index b82bdbec..0c4517c2 100644 --- a/src/ds390/gen.c +++ b/src/ds390/gen.c @@ -34,6 +34,7 @@ #include #include "SDCCglobl.h" #include "newalloc.h" +#include "dbuf_string.h" #include "common.h" #include "main.h" @@ -205,32 +206,33 @@ static int _lazyDPS = 0; /* if non-zero, we are doing lazy evaluation of /* emitcode - writes the code into a file : for now it is simple */ /*-----------------------------------------------------------------*/ static void -emitcode (char *inst, const char *fmt,...) +emitcode (const char *inst, const char *fmt,...) { va_list ap; - char lb[INITIAL_INLINEASM]; - char *lbp = lb; + struct dbuf_s dbuf; + const char *lbp, *lb; + + dbuf_init (&dbuf, INITIAL_INLINEASM); va_start (ap, fmt); if (inst && *inst) { + dbuf_append_str (&dbuf, inst); + if (fmt && *fmt) { - SNPRINTF (lb, sizeof(lb), "%s\t", inst); - } - else - { - SNPRINTF (lb, sizeof(lb), "%s", inst); - } - - tvsprintf (lb + strlen(lb), sizeof(lb) - strlen(lb), fmt, ap); + dbuf_append_char (&dbuf, '\t'); + dbuf_tvprintf (&dbuf, fmt, ap); + } } else { - tvsprintf (lb, sizeof(lb), fmt, ap); + dbuf_tvprintf (&dbuf, fmt, ap); } + lbp = lb = dbuf_c_str(&dbuf); + while (isspace ((unsigned char)*lbp)) { lbp++; @@ -249,6 +251,8 @@ emitcode (char *inst, const char *fmt,...) lineCurr->aln = ds390newAsmLineNode(_currentDPS); lineCurr->isComment = (*lbp == ';'); va_end (ap); + + dbuf_destroy(&dbuf); } static void @@ -14199,7 +14203,7 @@ gen390Code (iCode * lic) #if 1 /* print the allocation information */ if (allocInfo && currFunc) - printAllocInfo (currFunc, codeOutFile); + printAllocInfo (currFunc, codeOutBuf); #endif /* if debug information required */ if (options.debug && currFunc) @@ -14230,7 +14234,9 @@ gen390Code (iCode * lic) cln = ic->lineno; } if (options.iCodeInAsm) { - emitcode("", ";ic:%d: %s", ic->key, printILine(ic)); + char *iLine = printILine(ic); + emitcode("", ";ic:%d: %s", ic->key, iLine); + dbuf_free(iLine); } /* if the result is marked as spilt and rematerializable or code for @@ -14464,6 +14470,6 @@ gen390Code (iCode * lic) peepHole (&lineHead); /* now do the actual printing */ - printLine (lineHead, codeOutFile); + printLine (lineHead, codeOutBuf); return; } diff --git a/src/ds390/main.c b/src/ds390/main.c index 11f515c3..e4891327 100644 --- a/src/ds390/main.c +++ b/src/ds390/main.c @@ -10,6 +10,7 @@ #include "gen.h" #include "BuildCmd.h" #include "MySystem.h" +#include "dbuf_string.h" #include "../SDCCutil.h" #include "../SDCCglobl.h" static char _defaultRules[] = @@ -233,49 +234,49 @@ _ds390_genAssemblerPreamble (FILE * of) /* Generate interrupt vector table. */ static int -_ds390_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts) +_ds390_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts) { int i; if (options.model != MODEL_FLAT24) { - fprintf (of, "\tljmp\t__sdcc_gsinit_startup\n"); + dbuf_printf (oBuf, "\tljmp\t__sdcc_gsinit_startup\n"); /* now for the other interrupts */ for (i = 0; i < maxInterrupts; i++) { if (interrupts[i]) { - fprintf (of, "\tljmp\t%s\n", interrupts[i]->rname); + dbuf_printf (oBuf, "\tljmp\t%s\n", interrupts[i]->rname); if ( i != maxInterrupts - 1 ) - fprintf (of, "\t.ds\t5\n"); + dbuf_printf (oBuf, "\t.ds\t5\n"); } else { - fprintf (of, "\treti\n"); + dbuf_printf (oBuf, "\treti\n"); if ( i != maxInterrupts - 1 ) - fprintf (of, "\t.ds\t7\n"); + dbuf_printf (oBuf, "\t.ds\t7\n"); } } return TRUE; } - fprintf (of, "\tajmp\t__reset_vect\n"); + dbuf_printf (oBuf, "\tajmp\t__reset_vect\n"); /* now for the other interrupts */ for (i = 0; i < maxInterrupts; i++) { if (interrupts[i]) { - fprintf (of, "\tljmp\t%s\n\t.ds\t4\n", interrupts[i]->rname); + dbuf_printf (oBuf, "\tljmp\t%s\n\t.ds\t4\n", interrupts[i]->rname); } else { - fprintf (of, "\treti\n\t.ds\t7\n"); + dbuf_printf (oBuf, "\treti\n\t.ds\t7\n"); } } - fprintf (of, "__reset_vect:\n\tljmp\t__sdcc_gsinit_startup\n"); + dbuf_printf (oBuf, "__reset_vect:\n\tljmp\t__sdcc_gsinit_startup\n"); return TRUE; } @@ -1011,7 +1012,7 @@ static void _tininative_finaliseOptions (void) options.cc_only =1; } -static int _tininative_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts) +static int _tininative_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts) { return TRUE; } @@ -1267,19 +1268,20 @@ PORT tininative_port = }; static int -_ds400_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts) +_ds400_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts) { - /* We can't generate a static IVT, since the boot rom creates one - * for us in rom_init. - * - * we must patch it as part of the C startup. - */ - fprintf (of, ";\tDS80C400 IVT must be generated at runtime.\n"); - fprintf (of, "\tsjmp\t__sdcc_400boot\n"); - fprintf (of, "\t.ascii\t'TINI'\t; required signature for 400 boot loader.\n"); - fprintf (of, "\t.db\t0\t; selected bank: zero *should* work...\n"); - fprintf (of, "\t__sdcc_400boot:\tljmp\t__sdcc_gsinit_startup\n"); - return TRUE; + /* We can't generate a static IVT, since the boot rom creates one + * for us in rom_init. + * + * we must patch it as part of the C startup. + */ + dbuf_printf (oBuf, ";\tDS80C400 IVT must be generated at runtime.\n"); + dbuf_printf (oBuf, "\tsjmp\t__sdcc_400boot\n"); + dbuf_printf (oBuf, "\t.ascii\t'TINI'\t; required signature for 400 boot loader.\n"); + dbuf_printf (oBuf, "\t.db\t0\t; selected bank: zero *should* work...\n"); + dbuf_printf (oBuf, "\t__sdcc_400boot:\tljmp\t__sdcc_gsinit_startup\n"); + + return TRUE; } diff --git a/src/hc08/gen.c b/src/hc08/gen.c index aa324162..a4911827 100644 --- a/src/hc08/gen.c +++ b/src/hc08/gen.c @@ -83,7 +83,7 @@ static asmop *hc08_aop_pass[4]; extern int hc08_ptrRegReq; extern int hc08_nRegs; -extern FILE *codeOutFile; +extern struct dbuf_s *codeOutBuf; //static void saveRBank (int, iCode *, bool); static bool operandsEqu (operand * op1, operand * op2); static void loadRegFromConst (regs *reg, char *c); @@ -8346,7 +8346,7 @@ genhc08Code (iCode * lic) /* print the allocation information */ if (allocInfo && currFunc) - printAllocInfo (currFunc, codeOutFile); + printAllocInfo (currFunc, codeOutBuf); /* if debug information required */ if (options.debug && currFunc) { @@ -8418,13 +8418,16 @@ genhc08Code (iCode * lic) if (options.iCodeInAsm) { char regsInUse[80]; int i; + char *iLine; for (i=0; i<6; i++) { sprintf (®sInUse[i], "%c", ic->riu & (1<seq, printILine(ic)); + dbuf_free(iLine); } /* if the result is marked as spilt and rematerializable or code for @@ -8697,6 +8700,6 @@ genhc08Code (iCode * lic) peepHole (&lineHead); /* now do the actual printing */ - printLine (lineHead, codeOutFile); + printLine (lineHead, codeOutBuf); return; } diff --git a/src/hc08/main.c b/src/hc08/main.c index cd6eb0c0..da5fd14f 100644 --- a/src/hc08/main.c +++ b/src/hc08/main.c @@ -8,9 +8,9 @@ #include "main.h" #include "ralloc.h" #include "gen.h" +#include "dbuf_string.h" #include "../SDCCutil.h" -void copyFile(FILE *dest, FILE *src); extern char * iComments2; extern DEBUGFILE dwarf2DebugFile; extern int dwarf2FinalizeFile(FILE *); @@ -252,28 +252,28 @@ _hc08_genExtraAreas (FILE * asmFile, bool mainExists) fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; extended address mode data\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, xdata->oFile); + dbuf_write_and_destroy (&xdata->oBuf, asmFile); } /* Generate interrupt vector table. */ static int -_hc08_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts) +_hc08_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts) { int i; - fprintf (of, "\t.area\tCODEIVT (ABS)\n"); - fprintf (of, "\t.org\t0x%04x\n", + dbuf_printf (oBuf, "\t.area\tCODEIVT (ABS)\n"); + dbuf_printf (oBuf, "\t.org\t0x%04x\n", (0xfffe - (maxInterrupts * 2))); for (i=maxInterrupts;i>0;i--) { if (interrupts[i]) - fprintf (of, "\t.dw\t%s\n", interrupts[i]->rname); + dbuf_printf (oBuf, "\t.dw\t%s\n", interrupts[i]->rname); else - fprintf (of, "\t.dw\t0xffff\n"); + dbuf_printf (oBuf, "\t.dw\t0xffff\n"); } - fprintf (of, "\t.dw\t%s", "__sdcc_gs_init_startup\n"); + dbuf_printf (oBuf, "\t.dw\t%s", "__sdcc_gs_init_startup\n"); return TRUE; } diff --git a/src/izt/gen.c b/src/izt/gen.c index 22ed6ead..6579981f 100644 --- a/src/izt/gen.c +++ b/src/izt/gen.c @@ -234,7 +234,7 @@ _printLines (void) { // Currently a holder function. The Z80 needs some special mangling // for bank support. - printLine (_G.lines.head, codeOutFile); + printLine (_G.lines.head, codeOutBuf); } void diff --git a/src/mcs51/gen.c b/src/mcs51/gen.c index ad53c30b..10487d91 100644 --- a/src/mcs51/gen.c +++ b/src/mcs51/gen.c @@ -42,6 +42,7 @@ #include "SDCCpeeph.h" #include "ralloc.h" #include "gen.h" +#include "dbuf_string.h" char *aopLiteral (value * val, int offset); char *aopLiteralLong (value * val, int offset, int size); @@ -117,7 +118,7 @@ static char *rb1regs[] = { "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7" }; -extern FILE *codeOutFile; +extern struct dbuf_s *codeOutBuf; static void saveRBank (int, iCode *, bool); #define RESULTONSTACK(x) \ @@ -149,32 +150,33 @@ static unsigned char SRMask[] = /* emitcode - writes the code into a file : for now it is simple */ /*-----------------------------------------------------------------*/ static void -emitcode (char *inst, const char *fmt,...) +emitcode (const char *inst, const char *fmt,...) { va_list ap; - char lb[INITIAL_INLINEASM]; - char *lbp = lb; + struct dbuf_s dbuf; + const char *lbp, *lb; + + dbuf_init (&dbuf, INITIAL_INLINEASM); va_start (ap, fmt); if (inst && *inst) { + dbuf_append_str (&dbuf, inst); + if (fmt && *fmt) { - SNPRINTF (lb, sizeof(lb), "%s\t", inst); - } - else - { - SNPRINTF (lb, sizeof(lb), "%s", inst); + dbuf_append_char (&dbuf, '\t'); + dbuf_tvprintf (&dbuf, fmt, ap); } - - tvsprintf (lb + strlen(lb), sizeof(lb) - strlen(lb), fmt, ap); } else { - tvsprintf (lb, sizeof(lb), fmt, ap); + dbuf_tvprintf (&dbuf, fmt, ap); } + lbp = lb = dbuf_c_str(&dbuf); + while (isspace ((unsigned char)*lbp)) { lbp++; @@ -192,6 +194,8 @@ emitcode (char *inst, const char *fmt,...) lineCurr->ic = _G.current_iCode; lineCurr->isComment = (*lbp==';'); va_end (ap); + + dbuf_destroy(&dbuf); } static void @@ -11716,7 +11720,7 @@ gen51Code (iCode * lic) /* print the allocation information */ if (allocInfo && currFunc) - printAllocInfo (currFunc, codeOutFile); + printAllocInfo (currFunc, codeOutBuf); /* if debug information required */ if (options.debug && currFunc) { @@ -11755,6 +11759,7 @@ gen51Code (iCode * lic) if (options.iCodeInAsm) { char regsInUse[80]; int i; + char *iLine; #if 0 for (i=0; i<8; i++) { @@ -11771,7 +11776,9 @@ gen51Code (iCode * lic) } #endif } + iLine = printILine(ic); emitcode("", "; [%s] ic:%d: %s", regsInUse, ic->seq, printILine(ic)); + dbuf_free(iLine); } /* if the result is marked as spilt and rematerializable or code for @@ -12010,6 +12017,6 @@ gen51Code (iCode * lic) peepHole (&lineHead); /* now do the actual printing */ - printLine (lineHead, codeOutFile); + printLine (lineHead, codeOutBuf); return; } diff --git a/src/mcs51/main.c b/src/mcs51/main.c index 2307724f..8027833e 100644 --- a/src/mcs51/main.c +++ b/src/mcs51/main.c @@ -8,6 +8,7 @@ #include "main.h" #include "ralloc.h" #include "gen.h" +#include "dbuf_string.h" #include "../SDCCutil.h" static char _defaultRules[] = @@ -175,26 +176,26 @@ _mcs51_genAssemblerPreamble (FILE * of) /* Generate interrupt vector table. */ static int -_mcs51_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts) +_mcs51_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts) { int i; - fprintf (of, "\tljmp\t__sdcc_gsinit_startup\n"); + dbuf_printf (oBuf, "\tljmp\t__sdcc_gsinit_startup\n"); /* now for the other interrupts */ for (i = 0; i < maxInterrupts; i++) { if (interrupts[i]) { - fprintf (of, "\tljmp\t%s\n", interrupts[i]->rname); + dbuf_printf (oBuf, "\tljmp\t%s\n", interrupts[i]->rname); if ( i != maxInterrupts - 1 ) - fprintf (of, "\t.ds\t5\n"); + dbuf_printf (oBuf, "\t.ds\t5\n"); } else { - fprintf (of, "\treti\n"); + dbuf_printf (oBuf, "\treti\n"); if ( i != maxInterrupts - 1 ) - fprintf (of, "\t.ds\t7\n"); + dbuf_printf (oBuf, "\t.ds\t7\n"); } } return TRUE; diff --git a/src/pic/device.c b/src/pic/device.c index ecee306e..fc35b7e1 100644 --- a/src/pic/device.c +++ b/src/pic/device.c @@ -63,8 +63,6 @@ static unsigned int config_word = DEFAULT_CONFIG_WORD; static unsigned int config2_word = DEFAULT_CONFIG2_WORD; static memRange *rangeRAM = NULL; -extern void emitSymbolToFile (FILE *of, const char *name, const char *section_type, int size, int addr, int useEQU, int globalize); - /* parse a value from the configuration file */ static int parse_config_value(char *str) diff --git a/src/pic/gen.c b/src/pic/gen.c index 2fa2fccc..c3328398 100644 --- a/src/pic/gen.c +++ b/src/pic/gen.c @@ -116,7 +116,7 @@ typedef struct resolvedIfx { extern int pic14_ptrRegReq ; extern int pic14_nRegs; -extern FILE *codeOutFile; +extern struct dbuf_s *codeOutBuf; static void saverbank (int, iCode *,bool); static lineNode *lineHead = NULL; @@ -10674,7 +10674,9 @@ void genpic14Code (iCode *lic) } if (options.iCodeInAsm) { + char *iLine = printILine(ic); emitpComment ("[ICODE] %s:%d: %s", ic->filename, ic->lineno, printILine (ic)); + dbuf_free(iLine); } /* if the result is marked as spilt and rematerializable or code for @@ -10884,7 +10886,7 @@ void genpic14Code (iCode *lic) peepHole (&lineHead); } /* now do the actual printing */ - printLine (lineHead,codeOutFile); + printLine (lineHead,codeOutBuf); #ifdef PCODE_DEBUG DFPRINTF((stderr,"printing pBlock\n\n")); diff --git a/src/pic/glue.c b/src/pic/glue.c index 59a31a9f..da91bf8a 100644 --- a/src/pic/glue.c +++ b/src/pic/glue.c @@ -30,6 +30,7 @@ #include "gen.h" #include "main.h" #include "device.h" +#include "dbuf_string.h" #ifdef WORDS_BIGENDIAN @@ -49,9 +50,7 @@ extern unsigned maxInterrupts; extern int maxRegBank; extern symbol *mainf; extern char *VersionString; -extern FILE *codeOutFile; -extern set *tmpfileSet; -extern set *tmpfileNameSet; +extern struct dbuf_s *codeOutBuf; extern char *iComments1; extern char *iComments2; //extern void emitStaticSeg (memmap * map); @@ -61,7 +60,6 @@ extern DEFSETFUNC (closeTmpFiles); extern DEFSETFUNC (rmTmpFiles); extern void AnalyzeBanking (void); -extern void copyFile (FILE * dest, FILE * src); extern void ReuseReg(void); extern void InlinepCode(void); extern void writeUsedRegs(FILE *); @@ -79,7 +77,7 @@ int pic14_hasInterrupt = 0; // Indicates whether to emit interrupt handler or n static set *emitted = NULL; int pic14_stringInSet(const char *str, set **world, int autoAdd); -static void emitSymbolToFile (FILE *of, const char *name, const char *section_type, int size, int addr, int useEQU, int globalize); +static void emitSymbol (struct dbuf_s *oBuf, const char *name, const char *section_type, int size, int addr, int useEQU, int globalize); /*-----------------------------------------------------------------*/ /* aopLiteral - string from a literal value */ @@ -139,7 +137,7 @@ is_valid_identifier( const char *name ) #define IS_DEFINED_HERE(sym) (!IS_EXTERN(sym->etype)) extern int IS_CONFIG_ADDRESS( int addr ); static void -pic14_constructAbsMap (FILE *ofile) +pic14_constructAbsMap (struct dbuf_s *oBuf) { memmap *maps[] = { data, sfr, NULL }; int i; @@ -194,22 +192,22 @@ pic14_constructAbsMap (FILE *ofile) } // for i /* now emit definitions for all absolute symbols */ - fprintf (ofile, "%s", iComments2); - fprintf (ofile, "; absolute symbol definitions\n"); - fprintf (ofile, "%s", iComments2); + dbuf_printf (oBuf, "%s", iComments2); + dbuf_printf (oBuf, "; absolute symbol definitions\n"); + dbuf_printf (oBuf, "%s", iComments2); for (addr=min; addr <= max; addr++) { size = 1; aliases = hTabItemWithKey (ht, addr); if (aliases && elementsInSet(aliases)) { - fprintf (ofile, "udata_abs_%s_%x\tudata_ovr\t0x%04x", + dbuf_printf (oBuf, "udata_abs_%s_%x\tudata_ovr\t0x%04x", moduleName, addr, addr); for (sym = setFirstItem (aliases); sym; sym = setNextItem (aliases)) { /* emit STATUS as well as _STATUS, required for SFRs only */ - fprintf (ofile, "\n%s", sym->name); - fprintf (ofile, "\n%s", sym->rname); + dbuf_printf (oBuf, "\n%s", sym->name); + dbuf_printf (oBuf, "\n%s", sym->rname); if (getSize(sym->type) > size) { size = getSize(sym->type); } @@ -219,7 +217,7 @@ pic14_constructAbsMap (FILE *ofile) pic14_stringInSet(sym->rname, &emitted, 1); } } // for - fprintf (ofile, "\tres\t%d\n", size); + dbuf_printf (oBuf, "\tres\t%d\n", size); } // if } // for i @@ -228,45 +226,45 @@ pic14_constructAbsMap (FILE *ofile) * differently sized sharebanks, since STK12 will be * required by larger devices but only up to STK03 might * be defined using smaller devices. */ - fprintf (ofile, "\n"); + dbuf_printf (oBuf, "\n"); shared = pic14_getSharedStack(&low, &high, &size); if (!pic14_options.isLibrarySource) { pic = pic14_getPIC(); - fprintf (ofile, "\tglobal PSAVE\n"); - fprintf (ofile, "\tglobal SSAVE\n"); - fprintf (ofile, "\tglobal WSAVE\n"); + dbuf_printf (oBuf, "\tglobal PSAVE\n"); + dbuf_printf (oBuf, "\tglobal SSAVE\n"); + dbuf_printf (oBuf, "\tglobal WSAVE\n"); for (i = size - 4; i >= 0; i--) { - fprintf (ofile, "\tglobal STK%02d\n", i); + dbuf_printf (oBuf, "\tglobal STK%02d\n", i); } // for i // 16f84 has no SHAREBANK (in linkerscript) but memory aliased in two // banks, sigh... if (1 || !shared) { // for single banked devices: use normal, "banked" RAM - fprintf (ofile, "sharebank udata_ovr 0x%04X\n", low); + dbuf_printf (oBuf, "sharebank udata_ovr 0x%04X\n", low); } else { // for devices with at least two banks, require a sharebank section - fprintf (ofile, "sharebank udata_shr\n"); + dbuf_printf (oBuf, "sharebank udata_shr\n"); } - fprintf (ofile, "PSAVE\tres 1\n"); - fprintf (ofile, "SSAVE\tres 1\n"); - fprintf (ofile, "WSAVE\tres 1\n"); // WSAVE *must* be in sharebank (IRQ handlers) + dbuf_printf (oBuf, "PSAVE\tres 1\n"); + dbuf_printf (oBuf, "SSAVE\tres 1\n"); + dbuf_printf (oBuf, "WSAVE\tres 1\n"); // WSAVE *must* be in sharebank (IRQ handlers) /* fill rest of sharebank with stack STKxx .. STK00 */ for (i = size - 4; i >= 0; i--) { - fprintf (ofile, "STK%02d\tres 1\n", i); + dbuf_printf (oBuf, "STK%02d\tres 1\n", i); } // for i } else { /* declare STKxx as extern for all files * except the one containing main() */ - fprintf (ofile, "\textern PSAVE\n"); - fprintf (ofile, "\textern SSAVE\n"); - fprintf (ofile, "\textern WSAVE\n"); + dbuf_printf (oBuf, "\textern PSAVE\n"); + dbuf_printf (oBuf, "\textern SSAVE\n"); + dbuf_printf (oBuf, "\textern WSAVE\n"); for (i = size - 4; i >= 0; i--) { char buffer[128]; SNPRINTF(&buffer[0], 127, "STK%02d", i); - fprintf (ofile, "\textern %s\n", &buffer[0]); + dbuf_printf (oBuf, "\textern %s\n", &buffer[0]); pic14_stringInSet(&buffer[0], &emitted, 1); } // for i } @@ -283,7 +281,7 @@ pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag) /* print the area name */ if (addPublics) - fprintf (map->oFile, ";\t.area\t%s\n", map->sname); + dbuf_printf (&map->oBuf, ";\t.area\t%s\n", map->sname); for (sym = setFirstItem (map->syms); sym; sym = setNextItem (map->syms)) { @@ -333,13 +331,13 @@ pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag) { if (!sym->level) /* global */ if (IS_STATIC (sym->etype)) - fprintf (map->oFile, "F%s_", moduleName); /* scope is file */ + dbuf_printf (&map->oBuf, "F%s_", moduleName); /* scope is file */ else - fprintf (map->oFile, "G_"); /* scope is global */ + dbuf_printf (&map->oBuf, "G_"); /* scope is global */ else /* symbol is local */ - fprintf (map->oFile, "L%s_", (sym->localof ? sym->localof->name : "-null-")); - fprintf (map->oFile, "%s_%d_%d", sym->name, sym->level, sym->block); + dbuf_printf (&map->oBuf, "L%s_", (sym->localof ? sym->localof->name : "-null-")); + dbuf_printf (&map->oBuf, "%s_%d_%d", sym->name, sym->level, sym->block); } #endif /* absolute symbols are handled in pic14_constructAbsMap */ @@ -351,9 +349,9 @@ pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag) if (0 && SPEC_ABSA (sym->etype)) { //if (options.debug || sym->level == 0) - //fprintf (map->oFile,"; == 0x%04x\n",SPEC_ADDR (sym->etype)); + //dbuf_printf (&map->oBuf,"; == 0x%04x\n",SPEC_ADDR (sym->etype)); - fprintf (map->oFile, "%s\tEQU\t0x%04x\n", + dbuf_printf (&map->oBuf, "%s\tEQU\t0x%04x\n", sym->rname, SPEC_ADDR (sym->etype)); } @@ -370,7 +368,7 @@ pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag) } else { - emitSymbolToFile (map->oFile, + emitSymbol (&map->oBuf, sym->rname, NULL, getSize (sym->type) & 0xffff, @@ -386,12 +384,12 @@ pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag) if ((size = (unsigned int) getSize (sym->type) & 0xffff) > 1) { for (i = 1; i < size; i++) - fprintf (map->oFile, "\t%s_%d\n", sym->rname, i); + dbuf_printf (&map->oBuf, "\t%s_%d\n", sym->rname, i); } } */ } - //fprintf (map->oFile, "\t.ds\t0x%04x\n", (unsigned int)getSize (sym->type) & 0xffff); + //dbuf_printf (&map->oBuf, "\t.ds\t0x%04x\n", (unsigned int)getSize (sym->type) & 0xffff); } /* if it has a initial value then do it only if @@ -404,7 +402,7 @@ pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag) else ival = newNode ('=', newAst_VALUE(symbolVal (sym)), decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_TYPE_NONE)); - codeOutFile = statsg->oFile; + codeOutBuf = &statsg->oBuf; GcurMemmap = statsg; eBBlockFromiCode (iCodeFromAst (ival)); sym->ival = NULL; @@ -799,7 +797,7 @@ pic14emitStaticSeg (memmap * map) { symbol *sym; - fprintf (map->oFile, ";\t.area\t%s\n", map->sname); + dbuf_printf (&map->oBuf, ";\t.area\t%s\n", map->sname); //fprintf(stderr, "%s\n",__FUNCTION__); @@ -824,15 +822,15 @@ pic14emitStaticSeg (memmap * map) if (!sym->level) { /* global */ if (IS_STATIC (sym->etype)) - fprintf (code->oFile, "F%s_", moduleName); /* scope is file */ + dbuf_printf (&code->oBuf, "F%s_", moduleName); /* scope is file */ else - fprintf (code->oFile, "G_"); /* scope is global */ + dbuf_printf (&code->oBuf, "G_"); /* scope is global */ } else /* symbol is local */ - fprintf (code->oFile, "L%s_", + dbuf_printf (&code->oBuf, "L%s_", (sym->localof ? sym->localof->name : "-null-")); - fprintf (code->oFile, "%s_%d_%d", sym->name, sym->level, sym->block); + dbuf_printf (&code->oBuf, "%s_%d_%d", sym->name, sym->level, sym->block); } @@ -840,26 +838,26 @@ pic14emitStaticSeg (memmap * map) if (SPEC_ABSA (sym->etype)) { if (options.debug || sym->level == 0) - fprintf (code->oFile, " == 0x%04x\n", SPEC_ADDR (sym->etype)); + dbuf_printf (&code->oBuf, " == 0x%04x\n", SPEC_ADDR (sym->etype)); - fprintf (code->oFile, "%s\t=\t0x%04x\n", + dbuf_printf (&code->oBuf, "%s\t=\t0x%04x\n", sym->rname, SPEC_ADDR (sym->etype)); } else { if (options.debug || sym->level == 0) - fprintf (code->oFile, " == .\n"); + dbuf_printf (&code->oBuf, " == .\n"); /* if it has an initial value */ if (sym->ival) { pBlock *pb; - fprintf (code->oFile, "%s:\n", sym->rname); + dbuf_printf (&code->oBuf, "%s:\n", sym->rname); noAlloc++; resolveIvalSym (sym->ival, sym->type); - //printIval (sym, sym->type, sym->ival, code->oFile); + //printIval (sym, sym->type, sym->ival, &code->oBuf); pb = newpCodeChain(NULL, 'P',newpCodeCharP("; Starting pCode block for Ival")); addpBlock(pb); addpCode2pBlock(pb,newpCodeLabel(sym->rname,-1)); @@ -871,7 +869,7 @@ pic14emitStaticSeg (memmap * map) { /* allocate space */ - fprintf (code->oFile, "%s:\n", sym->rname); + dbuf_printf (&code->oBuf, "%s:\n", sym->rname); /* special case for character strings */ if (IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) && SPEC_CVAL (sym->etype).v_char) @@ -880,7 +878,7 @@ pic14emitStaticSeg (memmap * map) SPEC_CVAL (sym->etype).v_char, strlen (SPEC_CVAL (sym->etype).v_char) + 1);*/ else - fprintf (code->oFile, "\t.ds\t0x%04x\n", (unsigned int) getSize (sym->type) & 0xffff); + dbuf_printf (&code->oBuf, "\t.ds\t0x%04x\n", (unsigned int) getSize (sym->type) & 0xffff); } } } @@ -894,7 +892,7 @@ pic14emitStaticSeg (memmap * map) static void pic14emitMaps () { - pic14_constructAbsMap (sfr->oFile); + pic14_constructAbsMap (&sfr->oBuf); /* no special considerations for the following data, idata & bit & xdata */ pic14emitRegularMap (data, TRUE, TRUE); @@ -912,7 +910,7 @@ pic14emitMaps () /* createInterruptVect - creates the interrupt vector */ /*-----------------------------------------------------------------*/ static void -pic14createInterruptVect (FILE * vFile) +pic14createInterruptVect (struct dbuf_s * vBuf) { mainf = newSymbol ("main", 0); mainf->block = 0; @@ -937,13 +935,13 @@ pic14createInterruptVect (FILE * vFile) return; } - fprintf (vFile, "%s", iComments2); - fprintf (vFile, "; reset vector \n"); - fprintf (vFile, "%s", iComments2); - fprintf (vFile, "STARTUP\t%s\n", CODE_NAME); // Lkr file should place section STARTUP at address 0x0 - fprintf (vFile, "\tnop\n"); /* first location for used by incircuit debugger */ - fprintf( vFile, "\tpagesel __sdcc_gsinit_startup\n"); - fprintf (vFile, "\tgoto\t__sdcc_gsinit_startup\n"); + dbuf_printf (vBuf, "%s", iComments2); + dbuf_printf (vBuf, "; reset vector \n"); + dbuf_printf (vBuf, "%s", iComments2); + dbuf_printf (vBuf, "STARTUP\t%s\n", CODE_NAME); // Lkr file should place section STARTUP at address 0x0 + dbuf_printf (vBuf, "\tnop\n"); /* first location for used by incircuit debugger */ + dbuf_printf (vBuf, "\tpagesel __sdcc_gsinit_startup\n"); + dbuf_printf (vBuf, "\tgoto\t__sdcc_gsinit_startup\n"); } @@ -993,11 +991,12 @@ pic14_emitSymbolIfNew(FILE *file, const char *fmt, const char *sym, int checkLoc } /*-------------------------------------------------------------------*/ -/* emitSymbolToFile - write a symbol definition only if it is not */ +/* emitSymbol - write a symbol definition only if it is not */ /* already present */ /*-------------------------------------------------------------------*/ + static void -emitSymbolToFile (FILE *of, const char *name, const char *section_type, int size, int addr, int useEQU, int globalize) +emitSymbol (struct dbuf_s *oBuf, const char *name, const char *section_type, int size, int addr, int useEQU, int globalize) { static unsigned int sec_idx = 0; @@ -1017,7 +1016,7 @@ emitSymbolToFile (FILE *of, const char *name, const char *section_type, int size /* new symbol -- define it */ //fprintf (stderr, "%s: emitting %s (%d)\n", __FUNCTION__, name, size); if (useEQU) { - fprintf (of, "%s\tEQU\t0x%04x\n", name, addr); + dbuf_printf (oBuf, "%s\tEQU\t0x%04x\n", name, addr); } else { /* we place each symbol into a section of its own to allow the linker * to distribute the data into all available memory banks */ @@ -1027,10 +1026,10 @@ emitSymbolToFile (FILE *of, const char *name, const char *section_type, int size /* absolute symbols are handled in pic14_constructAbsMap */ /* do nothing */ } else { - if (globalize) fprintf (of, "\tglobal\t%s\n", name); - fprintf (of, "udata_%s_%u\t%s\n", moduleName, + if (globalize) dbuf_printf (oBuf, "\tglobal\t%s\n", name); + dbuf_printf (oBuf, "udata_%s_%u\t%s\n", moduleName, sec_idx++, section_type); - fprintf (of, "%s\tres\t%d\n", name, size); + dbuf_printf (oBuf, "%s\tres\t%d\n", name, size); } } } @@ -1084,7 +1083,9 @@ pic14printLocals (FILE * afile) set *allregs[6] = { dynAllocRegs, dynStackRegs, dynProcessorRegs, dynDirectRegs, dynDirectBitRegs, dynInternalRegs }; regs *reg; int i; + struct dbuf_s dbuf; + dbuf_init(&dbuf, 1024); /* emit all registers from all possible sets */ for (i = 0; i < 6; i++) { if (allregs[i] == NULL) continue; @@ -1093,18 +1094,19 @@ pic14printLocals (FILE * afile) if (reg->isEmitted) continue; if (reg->wasUsed && !reg->isExtern) { - emitSymbolToFile(afile, reg->name, "udata", reg->size, reg->isFixed ? reg->address : -1, 0, reg->isPublic); + emitSymbol(&dbuf, reg->name, "udata", reg->size, reg->isFixed ? reg->address : -1, 0, reg->isPublic); } reg->isEmitted = 1; } // for } // for + dbuf_write_and_destroy(&dbuf, afile); } /*-----------------------------------------------------------------*/ /* emitOverlay - will emit code for the overlay stuff */ /*-----------------------------------------------------------------*/ static void -pic14emitOverlay (FILE * afile) +pic14emitOverlay (struct dbuf_s * aBuf) { set *ovrset; @@ -1112,7 +1114,7 @@ pic14emitOverlay (FILE * afile) /* the hack below, fixes translates for devices which * only have udata_shr memory */ - fprintf (afile, "%s\t%s\n", + dbuf_printf (aBuf, "%s\t%s\n", (elementsInSet(ovrSetSets)?"":";"), port->mem.overlay_name); @@ -1132,9 +1134,9 @@ pic14emitOverlay (FILE * afile) /* I don't think this applies to us. We are using gpasm. CRF */ - fprintf (afile, ";\t.area _DUMMY\n"); + dbuf_printf (aBuf, ";\t.area _DUMMY\n"); /* output the area informtion */ - fprintf (afile, ";\t.area\t%s\n", port->mem.overlay_name); /* MOF */ + dbuf_printf (aBuf, ";\t.area\t%s\n", port->mem.overlay_name); /* MOF */ } for (sym = setFirstItem (ovrset); sym; @@ -1170,15 +1172,15 @@ pic14emitOverlay (FILE * afile) if (!sym->level) { /* global */ if (IS_STATIC (sym->etype)) - fprintf (afile, "F%s_", moduleName); /* scope is file */ + dbuf_printf (aBuf, "F%s_", moduleName); /* scope is file */ else - fprintf (afile, "G_"); /* scope is global */ + dbuf_printf (aBuf, "G_"); /* scope is global */ } else /* symbol is local */ - fprintf (afile, "L%s_", + dbuf_printf (aBuf, "L%s_", (sym->localof ? sym->localof->name : "-null-")); - fprintf (afile, "%s_%d_%d", sym->name, sym->level, sym->block); + dbuf_printf (aBuf, "%s_%d_%d", sym->name, sym->level, sym->block); } /* if is has an absolute address then generate @@ -1187,20 +1189,20 @@ pic14emitOverlay (FILE * afile) { if (options.debug || sym->level == 0) - fprintf (afile, " == 0x%04x\n", SPEC_ADDR (sym->etype)); + dbuf_printf (aBuf, " == 0x%04x\n", SPEC_ADDR (sym->etype)); - fprintf (afile, "%s\t=\t0x%04x\n", + dbuf_printf (aBuf, "%s\t=\t0x%04x\n", sym->rname, SPEC_ADDR (sym->etype)); } else { if (options.debug || sym->level == 0) - fprintf (afile, "==.\n"); + dbuf_printf (aBuf, "==.\n"); /* allocate space */ - fprintf (afile, "%s:\n", sym->rname); - fprintf (afile, "\t.ds\t0x%04x\n", (unsigned int) getSize (sym->type) & 0xffff); + dbuf_printf (aBuf, "%s:\n", sym->rname); + dbuf_printf (aBuf, "\t.ds\t0x%04x\n", (unsigned int) getSize (sym->type) & 0xffff); } } @@ -1235,11 +1237,13 @@ void picglue () { char udata_name[80]; - FILE *vFile; FILE *asmFile; - FILE *ovrFile = tempfile(); - - addSetHead(&tmpfileSet,ovrFile); + struct dbuf_s ovrBuf; + struct dbuf_s vBuf; + + dbuf_init(&ovrBuf, 4096); + dbuf_init(&vBuf, 4096); + pCodeInitRegisters(); /* check for main() */ @@ -1293,19 +1297,15 @@ picglue () if (options.debug) cdbStructBlock (0); - vFile = tempfile(); - - addSetHead(&tmpfileSet,vFile); - /* emit code for the all the variables declared */ pic14emitMaps (); /* do the overlay segments */ - pic14emitOverlay(ovrFile); + pic14emitOverlay(&ovrBuf); /* PENDING: this isnt the best place but it will do */ if (port->general.glue_up_main) { /* create the interrupt vector table */ - pic14createInterruptVect (vFile); + pic14createInterruptVect (&vBuf); } AnalyzepCode('*'); @@ -1365,7 +1365,7 @@ picglue () fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; special function registers\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, sfr->oFile); + dbuf_write_and_destroy (&sfr->oBuf, asmFile); if (udata_section_name) { @@ -1377,7 +1377,7 @@ picglue () fprintf (asmFile, "; udata\n"); fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "%s\tudata\n", udata_name); - copyFile (asmFile, data->oFile); + dbuf_write_and_destroy(&data->oBuf, asmFile); /* Put all variables into a cblock */ AnalyzeBanking(); @@ -1390,7 +1390,7 @@ picglue () fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; overlayable items in internal ram \n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, ovrFile); + dbuf_write_and_destroy (&ovrBuf, asmFile); #if 0 @@ -1407,8 +1407,8 @@ picglue () fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; indirectly addressable internal ram data\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, idata->oFile); - + dbuf_write_and_destroy (&idata->oBuf, asmFile); + /* if external stack then reserve space of it */ if (mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack ) { fprintf (asmFile, "%s", iComments2); @@ -1422,21 +1422,22 @@ picglue () fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; external ram data\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, xdata->oFile); - + dbuf_write_and_destroy (&xdata->oBuf, asmFile); + #endif /* copy the bit segment */ fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; bit data\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, bit->oFile); + dbuf_write_and_destroy (&bit->oBuf, asmFile); /* copy the interrupt vector table */ - if (mainf && IFFUNC_HASBODY(mainf->type)) { - copyFile (asmFile, vFile); - } - + if (mainf && IFFUNC_HASBODY(mainf->type)) + dbuf_write_and_destroy (&vBuf, asmFile); + else + dbuf_destroy(&vBuf); + /* create interupt ventor handler */ pic14_emitInterruptHandler (asmFile); @@ -1485,6 +1486,4 @@ picglue () fprintf (asmFile,"\tend\n"); fclose (asmFile); - - rm_tmpfiles(); } diff --git a/src/pic/main.c b/src/pic/main.c index 40a130b3..9b8cd24c 100644 --- a/src/pic/main.c +++ b/src/pic/main.c @@ -12,6 +12,7 @@ #include "SDCCmacro.h" #include "MySystem.h" #include "glue.h" +#include "dbuf_string.h" #include //#include "gen.h" @@ -257,7 +258,7 @@ _pic14_genAssemblerPreamble (FILE * of) /* Generate interrupt vector table. */ static int -_pic14_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts) +_pic14_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts) { int i; @@ -267,18 +268,18 @@ _pic14_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts) return FALSE; } - fprintf (of, "\t;ajmp\t__sdcc_gsinit_startup\n"); + dbuf_printf (oBuf, "\t;ajmp\t__sdcc_gsinit_startup\n"); /* now for the other interrupts */ for (i = 0; i < maxInterrupts; i++) { if (interrupts[i]) { - fprintf (of, "\t;ljmp\t%s\n\t.ds\t4\n", interrupts[i]->rname); + dbuf_printf (oBuf, "\t;ljmp\t%s\n\t.ds\t4\n", interrupts[i]->rname); } else { - fprintf (of, "\t;reti\n\t.ds\t7\n"); + dbuf_printf (oBuf, "\t;reti\n\t.ds\t7\n"); } } diff --git a/src/pic/ralloc.c b/src/pic/ralloc.c index d9063a84..06cb9e76 100644 --- a/src/pic/ralloc.c +++ b/src/pic/ralloc.c @@ -172,7 +172,7 @@ static void static char * debugAopGet (char *str, operand * op) { - if (str) + if (str) debugLog (str); printOperand (op, debugF); diff --git a/src/pic16/gen.c b/src/pic16/gen.c index 00ffa038..012f409d 100644 --- a/src/pic16/gen.c +++ b/src/pic16/gen.c @@ -182,7 +182,7 @@ static struct { extern int pic16_ptrRegReq ; extern int pic16_nRegs; -extern FILE *codeOutFile; +extern struct dbuf_s *codeOutBuf; //static void saverbank (int, iCode *,bool); static lineNode *lineHead = NULL; @@ -955,7 +955,9 @@ static asmop *aopForRemat (operand *op, bool result) // x symbol *sym) for (;;) { oldic = ic; -// pic16_emitpcomment("ic: %s\n", printILine(ic)); +// chat *iLine = printILine(ic); +// pic16_emitpcomment("ic: %s\n", iLine); +// dbuf_free(iLine); if (ic->op == '+') { val += (int) operandLitValue(IC_RIGHT(ic)); @@ -13638,11 +13640,12 @@ void genpic16Code (iCode *lic) } if(options.iCodeInAsm) { - char *l; + char *iLine; /* insert here code to print iCode as comment */ - l = Safe_strdup(printILine(ic)); - pic16_emitpcomment("ic:%d: %s", ic->seq, l); + iLine = printILine(ic); + pic16_emitpcomment("ic:%d: %s", ic->seq, iLine); + dbuf_free(iLine); } /* if the result is marked as @@ -13850,7 +13853,7 @@ void genpic16Code (iCode *lic) peepHole (&lineHead); /* now do the actual printing */ - printLine (lineHead, codeOutFile); + printLine (lineHead, codeOutBuf); #ifdef PCODE_DEBUG DFPRINTF((stderr,"printing pBlock\n\n")); diff --git a/src/pic16/glue.c b/src/pic16/glue.c index c11d8502..02447f32 100644 --- a/src/pic16/glue.c +++ b/src/pic16/glue.c @@ -30,11 +30,9 @@ #include "gen.h" #include "device.h" #include "main.h" +#include "dbuf_string.h" #include -#include - - extern symbol *interrupts[256]; void pic16_printIval (symbol * sym, sym_link * type, initList * ilist, char ptype, void *p); @@ -45,9 +43,7 @@ extern unsigned maxInterrupts; extern int maxRegBank; extern symbol *mainf; extern char *VersionString; -extern FILE *codeOutFile; -extern set *tmpfileSet; -extern set *tmpfileNameSet; +extern struct dbuf_s *codeOutBuf; extern char *iComments1; extern char *iComments2; @@ -65,7 +61,6 @@ extern DEFSETFUNC (rmTmpFiles); extern void pic16_AnalyzeBanking (void); extern void pic16_OptimizeJumps (); extern void pic16_OptimizeBanksel (); -extern void copyFile (FILE * dest, FILE * src); extern void pic16_InlinepCode(void); extern void pic16_writeUsedRegs(FILE *); @@ -128,7 +123,7 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag) // fprintf(stderr, "%s:%d map name= %s\n", __FUNCTION__, __LINE__, map->sname); if(addPublics) - fprintf (map->oFile, ";\t.area\t%s\n", map->sname); + dbuf_printf (&map->oBuf, ";\t.area\t%s\n", map->sname); /* print the area name */ for (sym = setFirstItem (map->syms); sym; sym = setNextItem (map->syms)) { @@ -225,7 +220,7 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag) // fprintf (stderr,"; %s == 0x%04x\t\treqv= %p nRegs= %d\n", // sym->name, SPEC_ADDR (sym->etype), sym->reqv, sym->regType); - fprintf (map->oFile, "%s\tEQU\t0x%04x\n", + dbuf_printf (&map->oBuf, "%s\tEQU\t0x%04x\n", sym->rname, SPEC_ADDR (sym->etype)); @@ -313,13 +308,13 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag) if (IS_BITVAR (sym->etype)) { bitvars++; } else { - fprintf (map->oFile, "\t%s\n", sym->rname); + dbuf_printf (map->oBuf, "\t%s\n", sym->rname); if ((size = (unsigned int) getSize (sym->type) & 0xffff) > 1) { for (i = 1; i < size; i++) - fprintf (map->oFile, "\t%s_%d\n", sym->rname, i); + dbuf_printf (map->oBuf, "\t%s_%d\n", sym->rname, i); } } - fprintf (map->oFile, "\t.ds\t0x%04x\n", (unsigned int)getSize (sym->type) & 0xffff); + dbuf_printf (map->oBuf, "\t.ds\t0x%04x\n", (unsigned int)getSize (sym->type) & 0xffff); #endif } @@ -367,7 +362,7 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag) if(ival) { setAstLineno(ival, sym->lineDef); - codeOutFile = statsg->oFile; + codeOutBuf = &statsg->oBuf; GcurMemmap = statsg; eBBlockFromiCode (iCodeFromAst (ival)); sym->ival = NULL; @@ -1363,7 +1358,7 @@ CODESPACE: %d\tCONST: %d\tPTRCONST: %d\tSPEC_CONST: %d\n", __FUNCTION__, /* symbol has absolute address but no initial value */ /* allocate space */ - fprintf (code->oFile, "%s:\n", sym->rname); + dbuf_printf (&code->oBuf, "%s:\n", sym->rname); /* special case for character strings */ if (IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) && @@ -1388,7 +1383,7 @@ CODESPACE: %d\tCONST: %d\tPTRCONST: %d\tSPEC_CONST: %d\n", __FUNCTION__, pBlock *pb; /* symbol doesn't have absolute address but has initial value */ - fprintf (code->oFile, "%s:\n", sym->rname); + dbuf_printf (&code->oBuf, "%s:\n", sym->rname); noAlloc++; resolveIvalSym (sym->ival, sym->type); @@ -1412,7 +1407,7 @@ CODESPACE: %d\tCONST: %d\tPTRCONST: %d\tSPEC_CONST: %d\n", __FUNCTION__, /* symbol doesn't have absolute address and no initial value */ /* allocate space */ // fprintf(stderr, "%s:%d [3] generating init for label: %s\n", __FILE__, __LINE__, sym->rname); - fprintf (code->oFile, "%s:\n", sym->rname); + dbuf_printf (&code->oBuf, "%s:\n", sym->rname); /* special case for character strings */ if (IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) && SPEC_CVAL (sym->etype).v_char) { @@ -1476,7 +1471,7 @@ pic16emitMaps () /* createInterruptVect - creates the interrupt vector */ /*-----------------------------------------------------------------*/ static void -pic16createInterruptVect (FILE * vFile) +pic16createInterruptVect (struct dbuf_s * vBuf) { /* if the main is only a prototype ie. no body then do nothing */ #if 0 @@ -1489,8 +1484,8 @@ pic16createInterruptVect (FILE * vFile) #endif #if 0 if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) { - fprintf (vFile, ";\t.area\t%s\n", CODE_NAME); - fprintf(vFile, ".intvecs\tcode\t0x%06x\n", pic16_options.ivt_loc); + dbuf_printf (vBuf, ";\t.area\t%s\n", CODE_NAME); + dbuf_printf (vBuf, ".intvecs\tcode\t0x%06x\n", pic16_options.ivt_loc); /* this is an overkill since WE are the port, * and we know if we have a genIVT function! */ @@ -1604,12 +1599,12 @@ pic16_printExterns(FILE *afile) /* emitOverlay - will emit code for the overlay stuff */ /*-----------------------------------------------------------------*/ static void -pic16emitOverlay (FILE * afile) +pic16emitOverlay (struct dbuf_s *aBuf) { set *ovrset; if (!elementsInSet (ovrSetSets)) - fprintf (afile, ";\t.area\t%s\n", port->mem.overlay_name); + dbuf_printf (aBuf, ";\t.area\t%s\n", port->mem.overlay_name); /* for each of the sets in the overlay segment do */ for (ovrset = setFirstItem (ovrSetSets); ovrset; @@ -1624,9 +1619,9 @@ pic16emitOverlay (FILE * afile) otherwise the assembler will append each of these declarations into one chunk and will not overlay sad but true */ - fprintf (afile, ";\t.area _DUMMY\n"); + dbuf_printf (aBuf, ";\t.area _DUMMY\n"); /* output the area informtion */ - fprintf (afile, ";\t.area\t%s\n", port->mem.overlay_name); /* MOF */ + dbuf_printf (aBuf, ";\t.area\t%s\n", port->mem.overlay_name); /* MOF */ } for (sym = setFirstItem (ovrset); sym; @@ -1666,20 +1661,20 @@ pic16emitOverlay (FILE * afile) { if (options.debug || sym->level == 0) - fprintf (afile, " == 0x%04x\n", SPEC_ADDR (sym->etype)); + dbuf_printf (aBuf, " == 0x%04x\n", SPEC_ADDR (sym->etype)); - fprintf (afile, "%s\t=\t0x%04x\n", + dbuf_printf (aBuf, "%s\t=\t0x%04x\n", sym->rname, SPEC_ADDR (sym->etype)); } else { if (options.debug || sym->level == 0) - fprintf (afile, "==.\n"); + dbuf_printf (aBuf, "==.\n"); /* allocate space */ - fprintf (afile, "%s:\n", sym->rname); - fprintf (afile, "\t.ds\t0x%04x\n", (unsigned int) getSize (sym->type) & 0xffff); + dbuf_printf (aBuf, "%s:\n", sym->rname); + dbuf_printf (aBuf, "\t.ds\t0x%04x\n", (unsigned int) getSize (sym->type) & 0xffff); } } @@ -1716,16 +1711,18 @@ void emitStatistics(FILE *asmFile) void pic16glue () { - FILE *vFile; FILE *asmFile; - FILE *ovrFile = tempfile(); + struct dbuf_s ovrBuf; + struct dbuf_s vBuf; + + dbuf_init(&ovrBuf, 4096); + dbuf_init(&vBuf, 4096); mainf = newSymbol ("main", 0); mainf->block = 0; mainf = findSymWithLevel(SymbolTab, mainf); - addSetHead(&tmpfileSet,ovrFile); pic16_pCodeInitRegisters(); if(pic16_options.no_crt && mainf && IFFUNC_HASBODY(mainf->type)) { @@ -1766,20 +1763,17 @@ pic16glue () /* print the global struct definitions */ - vFile = tempfile(); /* PENDING: this isnt the best place but it will do */ if (port->general.glue_up_main) { /* create the interrupt vector table */ - pic16createInterruptVect (vFile); + pic16createInterruptVect (&vBuf); } - addSetHead(&tmpfileSet,vFile); - /* emit code for the all the variables declared */ pic16emitMaps (); /* do the overlay segments */ - pic16emitOverlay(ovrFile); + pic16emitOverlay(&ovrBuf); pic16_AnalyzepCode('*'); #if 1 @@ -1872,7 +1866,7 @@ pic16glue () fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; external ram data\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, xdata->oFile); + dbuf_write_and_destroy (&xdata->oBuf, asmFile); #endif #if 0 @@ -1880,7 +1874,7 @@ pic16glue () fprintf (asmFile, "%s", iComments2); fprintf (asmFile, "; bit data\n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, bit->oFile); + dbuf_write_and_destroy (&bit->oBuf, asmFile); #endif /* copy the interrupt vector table */ @@ -1888,7 +1882,7 @@ pic16glue () fprintf (asmFile, "\n%s", iComments2); fprintf (asmFile, "; interrupt vector \n"); fprintf (asmFile, "%s", iComments2); - copyFile (asmFile, vFile); + dbuf_write_and_destroy (&vBuf, asmFile); } /* copy global & static initialisations */ @@ -1908,7 +1902,7 @@ pic16glue () } } -// copyFile (stderr, code->oFile); +// dbuf_write_and_destroy (&code->oBuf, stderr); fprintf(asmFile, "; I code from now on!\n"); pic16_copypCode(asmFile, 'I'); @@ -1942,6 +1936,4 @@ pic16glue () fprintf (asmFile,"\tend\n"); fclose (asmFile); - - rm_tmpfiles(); } diff --git a/src/pic16/main.c b/src/pic16/main.c index a5a81b6f..ad257088 100644 --- a/src/pic16/main.c +++ b/src/pic16/main.c @@ -31,6 +31,7 @@ #include "SDCCutil.h" #include "glue.h" #include "pcode.h" +#include "dbuf_string.h" static char _defaultRules[] = @@ -979,35 +980,35 @@ _pic16_genAssemblerPreamble (FILE * of) /* Generate interrupt vector table. */ static int -_pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts) +_pic16_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts) { #if 1 /* PIC18F family has only two interrupts, the high and the low * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */ if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) { - fprintf(of, "; RESET vector\n"); - fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n"); + dbuf_printf(oBuf, "; RESET vector\n"); + dbuf_printf(oBuf, "\tgoto\t__sdcc_gsinit_startup\n"); } if(!pic16_options.omit_ivt) { - fprintf(of, "\tres 4\n"); + dbuf_printf(oBuf, "\tres 4\n"); - fprintf(of, "; High priority interrupt vector 0x0008\n"); + dbuf_printf(oBuf, "; High priority interrupt vector 0x0008\n"); if(interrupts[1]) { - fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname); - fprintf(of, "\tres\t12\n"); + dbuf_printf(oBuf, "\tgoto\t%s\n", interrupts[1]->rname); + dbuf_printf(oBuf, "\tres\t12\n"); } else { - fprintf(of, "\tretfie\n"); - fprintf(of, "\tres\t14\n"); + dbuf_printf(oBuf, "\tretfie\n"); + dbuf_printf(oBuf, "\tres\t14\n"); } - fprintf(of, "; Low priority interrupt vector 0x0018\n"); + dbuf_printf(oBuf, "; Low priority interrupt vector 0x0018\n"); if(interrupts[2]) { - fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname); + dbuf_printf(oBuf, "\tgoto\t%s\n", interrupts[2]->rname); } else { - fprintf(of, "\tretfie\n"); + dbuf_printf(oBuf, "\tretfie\n"); } } #endif diff --git a/src/pic16/ralloc.c b/src/pic16/ralloc.c index c06b5daa..9a952c35 100644 --- a/src/pic16/ralloc.c +++ b/src/pic16/ralloc.c @@ -3803,6 +3803,7 @@ static void packForPush (iCode * ic, eBBlock * ebp) { iCode *dic; + char *iLine; debugLog ("%s\n", __FUNCTION__); if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic))) @@ -3811,10 +3812,13 @@ packForPush (iCode * ic, eBBlock * ebp) #if 0 { int n1, n2; + char *iLine; n1 = bitVectnBitsOn( OP_DEFS(IC_LEFT(ic))); n2 = bitVectnBitsOn( OP_USES(IC_LEFT(ic))); + iLine = printILine(ic); debugf3("defs: %d\tuses: %d\t%s\n", n1, n2, printILine(ic)); + dbuf_free(iLine); debugf2("IC_LEFT(ic): from %d to %d\n", OP_LIVEFROM(IC_LEFT(ic)), OP_LIVETO(IC_LEFT(ic))); } #endif @@ -3848,7 +3852,9 @@ packForPush (iCode * ic, eBBlock * ebp) and the that the definition is an assignment */ IC_LEFT (ic) = IC_RIGHT (dic); - debugf("remiCodeFromeBBlock: %s\n", printILine(dic)); + iLine = printILine(dic); + debugf("remiCodeFromeBBlock: %s\n", iLine); + dbuf_free(iLine); remiCodeFromeBBlock (ebp, dic); bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key); @@ -3860,7 +3866,7 @@ static void printSymType(char * str, sym_link *sl) if(!pic16_ralloc_debug)return; debugLog (" %s Symbol type: ",str); - printTypeChain( sl, debugF); + printTypeChain (sl, debugF); debugLog ("\n"); } diff --git a/src/port.h b/src/port.h index d3e9a86d..a52bed1b 100644 --- a/src/port.h +++ b/src/port.h @@ -8,6 +8,7 @@ #include "SDCCicode.h" #include "SDCCargs.h" #include "SDCCpeeph.h" +#include "dbuf.h" #include "mcs51/peep.h" #define TARGET_ID_MCS51 1 @@ -296,7 +297,7 @@ typedef struct * it returns zero, default (8051) IVT generation code * will be used. */ - int (*genIVT) (FILE * of, symbol ** intTable, int intCount); + int (*genIVT) (struct dbuf_s *oBuf, symbol ** intTable, int intCount); void (*genXINIT) (FILE * of); diff --git a/src/src.dsp b/src/src.dsp index 78af8830..de62a825 100644 --- a/src/src.dsp +++ b/src/src.dsp @@ -104,6 +104,10 @@ SOURCE=..\support\Util\dbuf.c # End Source File # Begin Source File +SOURCE=..\support\Util\dbuf_string.c +# End Source File +# Begin Source File + SOURCE=..\support\Util\MySystem.c # End Source File # Begin Source File @@ -269,6 +273,10 @@ SOURCE=..\support\Util\dbuf.h # End Source File # Begin Source File +SOURCE=..\support\Util\dbuf_string.h +# End Source File +# Begin Source File + SOURCE=..\support\Util\MySystem.h # End Source File # Begin Source File diff --git a/src/xa51/gen.c b/src/xa51/gen.c index 508d890f..dd6fabb3 100755 --- a/src/xa51/gen.c +++ b/src/xa51/gen.c @@ -66,7 +66,7 @@ _G; extern int xa51_ptrRegReq; extern int xa51_nRegs; -extern FILE *codeOutFile; +extern struct dbuf_s *codeOutBuf; static lineNode *lineHead = NULL; static lineNode *lineCurr = NULL; @@ -814,7 +814,7 @@ static void genFunction (iCode * ic) { emitcode (";", "genFunction %s", sym->rname); /* print the allocation information */ - printAllocInfo (currFunc, codeOutFile); + printAllocInfo (currFunc, codeOutBuf); emitcode ("", "%s:", sym->rname); @@ -1946,7 +1946,9 @@ void genXA51Code (iCode * lic) { cln = ic->lineno; } if (options.iCodeInAsm) { - emitcode("", ";ic:%d: %s", ic->key, printILine(ic)); + char *iLine = printILine(ic); + emitcode("", ";ic:%d: %s", ic->key, iLine); + dbuf_free(iLine); } /* if the result is marked as @@ -2156,6 +2158,6 @@ void genXA51Code (iCode * lic) { peepHole (&lineHead); /* now do the actual printing */ - printLine (lineHead, codeOutFile); + printLine (lineHead, codeOutBuf); return; } diff --git a/src/xa51/main.c b/src/xa51/main.c index 5ec0b74b..990fb755 100755 --- a/src/xa51/main.c +++ b/src/xa51/main.c @@ -123,7 +123,7 @@ _xa51_getRegName (struct regs *reg) /* Generate interrupt vector table. */ static int -_xa51_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts) +_xa51_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts) { return TRUE; } diff --git a/src/z80/gen.c b/src/z80/gen.c index dfa20d92..08cf5b4c 100644 --- a/src/z80/gen.c +++ b/src/z80/gen.c @@ -137,7 +137,7 @@ static char *_fReceive[] = static char **_fReturn; static char **_fTmp; -extern FILE *codeOutFile; +extern struct dbuf_s *codeOutBuf; enum { @@ -388,7 +388,7 @@ _tidyUp (char *buf) } static lineNode * -_newLineNode (char *line) +_newLineNode (const char *line) { lineNode *pl; @@ -401,11 +401,16 @@ _newLineNode (char *line) static void _vemit2 (const char *szFormat, va_list ap) { - char buffer[INITIAL_INLINEASM]; + struct dbuf_s dbuf; + const char *buffer; - tvsprintf (buffer, sizeof(buffer), szFormat, ap); + dbuf_init(&dbuf, INITIAL_INLINEASM); - _tidyUp (buffer); + dbuf_tvprintf (&dbuf, szFormat, ap); + + buffer = dbuf_c_str(&dbuf); + + _tidyUp ((char *)buffer); _G.lines.current = (_G.lines.current ? connectLine (_G.lines.current, _newLineNode (buffer)) : (_G.lines.head = _newLineNode (buffer))); @@ -414,6 +419,8 @@ _vemit2 (const char *szFormat, va_list ap) _G.lines.current->isDebug = _G.lines.isDebug; _G.lines.current->ic = _G.current_iCode; _G.lines.current->isComment = (*buffer == ';'); + + dbuf_destroy(&dbuf); } static void @@ -8109,7 +8116,9 @@ genZ80Code (iCode * lic) } if (options.iCodeInAsm) { - emit2 (";ic:%d: %s", ic->key, printILine(ic)); + char *iLine = printILine(ic); + emit2 (";ic:%d: %s", ic->key, iLine); + dbuf_free(iLine); } /* if the result is marked as spilt and rematerializable or code for @@ -8393,16 +8402,16 @@ genZ80Code (iCode * lic) /* This is unfortunate */ /* now do the actual printing */ { - FILE *fp = codeOutFile; - if (isInHome () && codeOutFile == code->oFile) - codeOutFile = home->oFile; - printLine (_G.lines.head, codeOutFile); + struct dbuf_s *buf = codeOutBuf; + if (isInHome () && codeOutBuf == &code->oBuf) + codeOutBuf = &home->oBuf; + printLine (_G.lines.head, codeOutBuf); if (_G.flushStatics) { flushStatics (); _G.flushStatics = 0; } - codeOutFile = fp; + codeOutBuf = buf; } freeTrace(&_G.lines.trace); diff --git a/src/z80/main.c b/src/z80/main.c index 0a5592a1..83025434 100644 --- a/src/z80/main.c +++ b/src/z80/main.c @@ -27,7 +27,7 @@ #include "MySystem.h" #include "BuildCmd.h" #include "SDCCutil.h" -#include "dbuf.h" +#include "dbuf_string.h" static char _z80_defaultRules[] = { @@ -405,10 +405,10 @@ _setValues(void) for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet)) { buildCmdLine2(path, sizeof path, "-k\"%s" DIR_SEPARATOR_STRING "{port}\" ", s); - dbuf_append(&dbuf, path, strlen(path)); + dbuf_append_str(&dbuf, path); } buildCmdLine2(path, sizeof path, "-l\"{port}.lib\"", s); - dbuf_append(&dbuf, path, strlen(path)); + dbuf_append_str(&dbuf, path); setMainValue ("z80libspec", dbuf_c_str(&dbuf)); dbuf_destroy(&dbuf); diff --git a/support/Util/dbuf.c b/support/Util/dbuf.c index 44762d8c..bf466e30 100644 --- a/support/Util/dbuf.c +++ b/support/Util/dbuf.c @@ -1,8 +1,8 @@ /* dbuf.c - Dynamic buffer implementation - version 1.1.3, December 17th, 2006 + version 1.2.0, January 6th, 2007 - Copyright (c) 2002-2006 Borut Razem + Copyright (c) 2002-2007 Borut Razem This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -33,10 +33,12 @@ /* * Assure that the buffer is large enough to hold - * size bytes; enlarge it if necessary. + * current length + size bytes; enlarge it if necessary. + * + * Intended for internal use. */ -static int dbuf_expand(struct dbuf_s *dbuf, size_t size) +int _dbuf_expand(struct dbuf_s *dbuf, size_t size) { assert(dbuf->alloc != 0); assert(dbuf->buf != NULL); @@ -101,14 +103,14 @@ struct dbuf_s *dbuf_new(size_t size) * Set the buffer size. Buffer size can be only decreased. */ -int dbuf_set_size(struct dbuf_s *dbuf, size_t size) +int dbuf_set_length(struct dbuf_s *dbuf, size_t len) { assert(dbuf != NULL); assert(dbuf->alloc != 0); - assert(size <= dbuf->len); + assert(len <= dbuf->len); - if (size <= dbuf->len) { - dbuf->len = size; + if (len <= dbuf->len) { + dbuf->len = len; return 1; } @@ -120,15 +122,15 @@ int dbuf_set_size(struct dbuf_s *dbuf, size_t size) * Append the buf to the end of the buffer. */ -int dbuf_append(struct dbuf_s *dbuf, const void *buf, size_t size) +int dbuf_append(struct dbuf_s *dbuf, const void *buf, size_t len) { assert(dbuf != NULL); assert(dbuf->alloc != 0); assert(dbuf->buf != NULL); - if (dbuf_expand(dbuf, size) != 0) { - memcpy(&(((char *)dbuf->buf)[dbuf->len]), buf, size); - dbuf->len += size; + if (_dbuf_expand(dbuf, len) != 0) { + memcpy(&(((char *)dbuf->buf)[dbuf->len]), buf, len); + dbuf->len += len; return 1; } @@ -147,10 +149,12 @@ const char *dbuf_c_str(struct dbuf_s *dbuf) assert(dbuf->alloc != 0); assert(dbuf->buf != NULL); - dbuf_expand(dbuf, 1); - ((char *)dbuf->buf)[dbuf->len] = '\0'; + if (_dbuf_expand(dbuf, 1) != 0) { + ((char *)dbuf->buf)[dbuf->len] = '\0'; + return dbuf->buf; + } - return dbuf->buf; + return NULL; } @@ -169,10 +173,10 @@ const void *dbuf_get_buf(struct dbuf_s *dbuf) /* - * Get the buffer size. + * Get the buffer length. */ -size_t dbuf_get_size(struct dbuf_s *dbuf) +size_t dbuf_get_length(struct dbuf_s *dbuf) { assert(dbuf != NULL); assert(dbuf->alloc != 0); @@ -256,3 +260,15 @@ void dbuf_delete(struct dbuf_s *dbuf) dbuf_destroy(dbuf); free(dbuf); } + + +/* + * Free detached buffer. + * + * See dbuf_detach() + */ + +void dbuf_free(const void *buf) +{ + free((void *)buf); +} diff --git a/support/Util/dbuf.h b/support/Util/dbuf.h index 4675aafe..8a9f26ec 100644 --- a/support/Util/dbuf.h +++ b/support/Util/dbuf.h @@ -1,8 +1,8 @@ /* dbuf.h - Dynamic buffer interface - version 1.1.1, April 11th, 2003 + version 1.2.0, January 6th, 2007 - Copyright (c) 2002-2003 Borut Razem + Copyright (c) 2002-2007 Borut Razem This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -32,7 +32,7 @@ struct dbuf_s { size_t alloc; /* size of allocated buffer in bytes */ - size_t len; /* actual size of the buffer in bytes */ + size_t len; /* actual length of the buffer in bytes */ void *buf; /* pointer to the buffer, allocated on heap */ }; @@ -41,17 +41,19 @@ struct dbuf_s { extern "C" { #endif +int _dbuf_expand(struct dbuf_s *dbuf, size_t size); struct dbuf_s *dbuf_new(size_t size); int dbuf_init(struct dbuf_s *dbuf, size_t size); -int dbuf_set_size(struct dbuf_s *dbuf, size_t size); -int dbuf_append(struct dbuf_s *dbuf, const void *buf, size_t size); +int dbuf_set_length(struct dbuf_s *dbuf, size_t size); +int dbuf_append(struct dbuf_s *dbuf, const void *buf, size_t len); const void *dbuf_get_buf(struct dbuf_s *dbuf); -size_t dbuf_get_size(struct dbuf_s *dbuf); +size_t dbuf_get_length(struct dbuf_s *dbuf); const char *dbuf_c_str(struct dbuf_s *dbuf); int dbuf_trim(struct dbuf_s *dbuf); void *dbuf_detach(struct dbuf_s *dbuf); void dbuf_destroy(struct dbuf_s *dbuf); void dbuf_delete(struct dbuf_s *dbuf); +void dbuf_free(const void *buf); #ifdef __cplusplus } diff --git a/support/Util/dbuf_string.c b/support/Util/dbuf_string.c new file mode 100644 index 00000000..a4dae9e3 --- /dev/null +++ b/support/Util/dbuf_string.c @@ -0,0 +1,204 @@ +/* + dbuf_string.c - Append formatted string to the dynamic buffer + version 1.0.0, January 6th, 2007 + + Copyright (c) 2002-2007 Borut Razem + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street - Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + + +#include +#include +#include +#include +#include "dbuf_string.h" + + +/* + * Append string to the end of the buffer. + */ + +int +dbuf_append_str(struct dbuf_s *dbuf, const char *str) +{ + assert(str != NULL); + + return dbuf_append(dbuf, str, strlen(str)); +} + + +/* + * Append single character to the end of the buffer. + */ + +int +dbuf_append_char(struct dbuf_s *dbuf, char chr) +{ + return dbuf_append(dbuf, &chr, 1); +} + +/* + * Calculate length of the resulting formatted string. + * + * Borrowed from vasprintf.c + */ + +static int +calc_result_length (const char *format, va_list args) +{ + const char *p = format; + /* Add one to make sure that it is never zero, which might cause malloc + to return NULL. */ + int total_width = strlen (format) + 1; + va_list ap; + + memcpy (&ap, &args, sizeof (va_list)); + + while (*p != '\0') + { + if (*p++ == '%') + { + while (strchr ("-+ #0", *p)) + ++p; + if (*p == '*') + { + ++p; + total_width += abs (va_arg (ap, int)); + } + else + total_width += strtoul (p, (char **) &p, 10); + if (*p == '.') + { + ++p; + if (*p == '*') + { + ++p; + total_width += abs (va_arg (ap, int)); + } + else + total_width += strtoul (p, (char **) &p, 10); + } + while (strchr ("hlL", *p)) + ++p; + /* Should be big enough for any format specifier except %s and floats. */ + total_width += 30; + switch (*p) + { + case 'd': + case 'i': + case 'o': + case 'u': + case 'x': + case 'X': + case 'c': + (void) va_arg (ap, int); + break; + case 'f': + case 'e': + case 'E': + case 'g': + case 'G': + (void) va_arg (ap, double); + /* Since an ieee double can have an exponent of 307, we'll + make the buffer wide enough to cover the gross case. */ + total_width += 307; + break; + case 's': + total_width += strlen (va_arg (ap, char *)); + break; + case 'p': + case 'n': + (void) va_arg (ap, char *); + break; + } + p++; + } + } + + return total_width; +} + + +/* + * Append the formatted string to the end of the buffer. + */ + +int +dbuf_vprintf (struct dbuf_s *dbuf, const char *format, va_list args) +{ + int size = calc_result_length (format, args); + + assert(dbuf != NULL); + assert(dbuf->alloc != 0); + assert(dbuf->buf != NULL); + + if (_dbuf_expand(dbuf, size) != 0) { + int len = vsprintf(&(((char *)dbuf->buf)[dbuf->len]), format, args); + + if (len >= 0) { + /* if written length is greater then the calculated one, + we have a buffer overrun! */ + assert(len <= size); + dbuf->len += len; + } + return len; + } + + return 0; +} + + +/* + * Append the formatted string to the end of the buffer. + */ + +int +dbuf_printf (struct dbuf_s *dbuf, const char *format, ...) +{ + va_list arg; + int len; + + va_start (arg, format); + len = dbuf_vprintf(dbuf, format, arg); + va_end (arg); + + return len; +} + + +/* + * Write dynamic buffer to the file. + */ + +void +dbuf_write (struct dbuf_s *dbuf, FILE *dest) +{ + fwrite(dbuf_get_buf(dbuf), 1, dbuf_get_length(dbuf), dest); +} + + +/* + * Write dynamic buffer to the file and destroy it. + */ + +void +dbuf_write_and_destroy (struct dbuf_s *dbuf, FILE *dest) +{ + dbuf_write (dbuf, dest); + + dbuf_destroy(dbuf); +} diff --git a/support/Util/dbuf_string.h b/support/Util/dbuf_string.h new file mode 100644 index 00000000..8c4d78bb --- /dev/null +++ b/support/Util/dbuf_string.h @@ -0,0 +1,45 @@ +/* + dbuf_string.h - Append formatted string to the dynamic buffer + version 1.0.0, January 6th, 2007 + + Copyright (c) 2002-2007 Borut Razem + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street - Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + + +#ifndef __DBUF_STRING_H +#define __DBUF_STRING_H + +#include +#include "dbuf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int dbuf_append_str(struct dbuf_s *dbuf, const char *str); +int dbuf_append_char(struct dbuf_s *dbuf, char chr); +int dbuf_vprintf(struct dbuf_s *dbuf, const char *format, va_list args); +int dbuf_printf (struct dbuf_s *dbuf, const char *format, ...); +void dbuf_write (struct dbuf_s *dbuf, FILE *dest); +void dbuf_write_and_destroy (struct dbuf_s *dbuf, FILE *dest); + +#ifdef __cplusplus +} +#endif + +#endif /* __DBUF_STRING_H */ -- 2.30.2