From a80283a747da0ca5a791dcfc5fe1a6261feecc5c Mon Sep 17 00:00:00 2001 From: "phil@softsynth.com" Date: Thu, 26 Aug 2010 02:07:37 +0000 Subject: [PATCH] Fixed lots of warning and made code compatible with C89 and ANSI with -pedantic. Use fseek and ftell on WIN32 instead of fseeko and ftello. --- build/unix/Makefile | 8 ++++++++ csrc/pf_core.c | 27 +++++++++++++++------------ csrc/pf_guts.h | 12 +++++------- csrc/pf_inner.c | 26 ++++++++++++++------------ csrc/pf_io.h | 10 ++++++++-- csrc/pf_main.c | 5 +++-- csrc/pf_save.c | 28 ++++++++++++++-------------- csrc/pfcompil.c | 12 ++++++------ csrc/pfcustom.c | 2 +- releases.txt | 4 +++- 10 files changed, 77 insertions(+), 57 deletions(-) diff --git a/build/unix/Makefile b/build/unix/Makefile index 8896f7c..162cf5b 100644 --- a/build/unix/Makefile +++ b/build/unix/Makefile @@ -27,11 +27,13 @@ TEMPOBJECTDIR = $(PFORTHDIR)/tempobjects WIDTHOPT= FULL_WARNINGS = \ + -c89 \ -fsigned-char \ -fno-builtin \ -fno-unroll-loops \ -fpeephole \ -fno-keep-inline-functions \ + -pedantic \ -Wcast-qual \ -Wall \ -Wwrite-strings \ @@ -117,6 +119,12 @@ help: @echo " The file 'fth/pfdicdat.h' is generated by pForth. It contains a binary image of the Forth dictionary." @echo " It allows pForth to work as a standalone image that does not need to load a dictionary file." +test: $(PFORTHAPP) + cd $(FTHDIR); ../pforth_standalone -q t_corex.fth + cd $(FTHDIR); ../pforth_standalone -q t_strings.fth + cd $(FTHDIR); ../pforth_standalone -q t_locals.fth + cd $(FTHDIR); ../pforth_standalone -q t_alloc.fth + cd $(FTHDIR); ../pforth_standalone -q t_floats.fth clean: rm -f $(PFOBJS) $(PFEMBOBJS) diff --git a/csrc/pf_core.c b/csrc/pf_core.c index 0564a65..f7bf38f 100644 --- a/csrc/pf_core.c +++ b/csrc/pf_core.c @@ -92,7 +92,6 @@ static void pfInit( void ) gVarEcho = 0; /* Echo input. */ gVarTraceLevel = 0; /* Trace Level for Inner Interpreter. */ gVarTraceFlags = 0; /* Enable various internal debug messages. */ - gVarQuiet = 0; /* Suppress unnecessary messages, OK, etc. */ gVarReturnCode = 0; /* Returned to caller of Forth, eg. UNIX shell. */ gIncludeIndex = 0; @@ -224,32 +223,32 @@ PForthDictionary pfCreateDictionary( cell_t HeaderSize, cell_t CodeSize ) * to (ucell_t) on 16 bit systems. */ #define DIC_ALIGNMENT_SIZE ((ucell_t)(0x10)) -#define DIC_ALIGN(addr) ((uint8_t *)((((ucell_t)(addr)) + DIC_ALIGNMENT_SIZE - 1) & ~(DIC_ALIGNMENT_SIZE - 1))) +#define DIC_ALIGN(addr) ((((ucell_t)(addr)) + DIC_ALIGNMENT_SIZE - 1) & ~(DIC_ALIGNMENT_SIZE - 1)) /* Allocate memory for header. */ if( HeaderSize > 0 ) { - dic->dic_HeaderBaseUnaligned = ( uint8_t * ) pfAllocMem( (ucell_t) HeaderSize + DIC_ALIGNMENT_SIZE ); + dic->dic_HeaderBaseUnaligned = (ucell_t) pfAllocMem( (ucell_t) HeaderSize + DIC_ALIGNMENT_SIZE ); if( !dic->dic_HeaderBaseUnaligned ) goto nomem; /* Align header base. */ dic->dic_HeaderBase = DIC_ALIGN(dic->dic_HeaderBaseUnaligned); - pfSetMemory( dic->dic_HeaderBase, 0xA5, (ucell_t) HeaderSize); + pfSetMemory( (char *) dic->dic_HeaderBase, 0xA5, (ucell_t) HeaderSize); dic->dic_HeaderLimit = dic->dic_HeaderBase + HeaderSize; dic->dic_HeaderPtr = dic->dic_HeaderBase; } else { - dic->dic_HeaderBase = NULL; + dic->dic_HeaderBase = 0; } /* Allocate memory for code. */ - dic->dic_CodeBaseUnaligned = ( uint8_t * ) pfAllocMem( (ucell_t) CodeSize + DIC_ALIGNMENT_SIZE ); + dic->dic_CodeBaseUnaligned = (ucell_t) pfAllocMem( (ucell_t) CodeSize + DIC_ALIGNMENT_SIZE ); if( !dic->dic_CodeBaseUnaligned ) goto nomem; dic->dic_CodeBase = DIC_ALIGN(dic->dic_CodeBaseUnaligned); - pfSetMemory( dic->dic_CodeBase, 0x5A, (ucell_t) CodeSize); + pfSetMemory( (char *) dic->dic_CodeBase, 0x5A, (ucell_t) CodeSize); dic->dic_CodeLimit = dic->dic_CodeBase + CodeSize; - dic->dic_CodePtr.Byte = dic->dic_CodeBase + QUADUP(NUM_PRIMITIVES); + dic->dic_CodePtr.Byte = ((uint8_t *) (dic->dic_CodeBase + QUADUP(NUM_PRIMITIVES))); return (PForthDictionary) dic; nomem: @@ -450,7 +449,7 @@ cell_t pfDoForth( const char *DicFileName, const char *SourceName, cell_t IfInit { pfSetCurrentTask( cftd ); - if( !pfQueryQuiet() ) + if( !gVarQuiet ) { MSG( "PForth V"PFORTH_VERSION ); if( IsHostLittleEndian() ) MSG("-LE"); @@ -508,8 +507,12 @@ cell_t pfDoForth( const char *DicFileName, const char *SourceName, cell_t IfInit } } if( dic == NULL ) goto error2; - EMIT_CR; - + + if( !gVarQuiet ) + { + EMIT_CR; + } + pfDebugMessage("pfDoForth: try AUTO.INIT\n"); Result = pfExecIfDefined("AUTO.INIT"); if( Result != 0 ) @@ -560,7 +563,7 @@ cell_t pfDoForth( const char *DicFileName, const char *SourceName, cell_t IfInit error2: MSG("pfDoForth: Error occured.\n"); pfDeleteTask( cftd ); - // Terminate so we restore normal shell tty mode. + /* Terminate so we restore normal shell tty mode. */ pfTerm(); #ifdef PF_USER_INIT diff --git a/csrc/pf_guts.h b/csrc/pf_guts.h index acb92a6..80df530 100644 --- a/csrc/pf_guts.h +++ b/csrc/pf_guts.h @@ -429,9 +429,7 @@ typedef struct pfDictionary_s ucell_t dic_HeaderPtr; ucell_t dic_HeaderLimit; /* Code segment contains tokenized code and data. */ - ucell_t dic_CodeBaseUnaligned; - ucell_t dic_CodeBase; union { @@ -552,15 +550,15 @@ extern cell_t gIncludeIndex; #define IN_DICS(addr) (IN_CODE_DIC(addr) || IN_NAME_DIC(addr)) /* Address conversion */ -#define ABS_TO_NAMEREL( a ) ((cell_t) (((uint8_t *) a) - NAME_BASE )) -#define ABS_TO_CODEREL( a ) ((cell_t) (((uint8_t *) a) - CODE_BASE )) -#define NAMEREL_TO_ABS( a ) ((char *) (((cell_t) a) + NAME_BASE)) -#define CODEREL_TO_ABS( a ) ((cell_t *) (((cell_t) a) + CODE_BASE)) +#define ABS_TO_NAMEREL( a ) ((cell_t) (((ucell_t) a) - NAME_BASE )) +#define ABS_TO_CODEREL( a ) ((cell_t) (((ucell_t) a) - CODE_BASE )) +#define NAMEREL_TO_ABS( a ) ((ucell_t) (((cell_t) a) + NAME_BASE)) +#define CODEREL_TO_ABS( a ) ((ucell_t) (((cell_t) a) + CODE_BASE)) /* The check for >0 is only needed for CLONE testing. !!! */ #define IsTokenPrimitive(xt) ((xt=0)) -#define FREE_VAR(v) { if (v) { pfFreeMem(v); v = NULL; } } +#define FREE_VAR(v) { if (v) { pfFreeMem((void *)(v)); v = 0; } } #define DATA_STACK_DEPTH (gCurrentTask->td_StackBase - gCurrentTask->td_StackPtr) #define DROP_DATA_STACK (gCurrentTask->td_StackPtr++) diff --git a/csrc/pf_inner.c b/csrc/pf_inner.c index 0149da0..fe60d15 100644 --- a/csrc/pf_inner.c +++ b/csrc/pf_inner.c @@ -1024,7 +1024,7 @@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql )); sdSeekFile( FileID, 0, PF_SEEK_END ); endposition = sdTellFile( FileID ); M_PUSH(endposition); - // Just use a 0 if they are the same size. + /* Just use a 0 if they are the same size. */ offsetHi = (sizeof(off_t) > sizeof(cell_t)) ? (endposition >> (8*sizeof(cell_t))) : 0 ; M_PUSH(offsetHi); sdSeekFile( FileID, original, PF_SEEK_SET ); @@ -1042,9 +1042,10 @@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql )); case ID_FILE_REPOSITION: /* ( ud fid -- ior ) */ { + off_t offset; FileID = (FileStream *) TOS; - off_t offset = M_POP; - // Avoid compiler warnings on Mac. + offset = M_POP; + /* Avoid compiler warnings on Mac. */ offset = (sizeof(off_t) > sizeof(cell_t)) ? (offset << 8*sizeof(cell_t)) : 0 ; offset += M_POP; TOS = sdSeekFile( FileID, offset, PF_SEEK_SET ); @@ -1053,11 +1054,12 @@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql )); case ID_FILE_POSITION: /* ( fid -- ud ior ) */ { + off_t position; off_t offsetHi; FileID = (FileStream *) TOS; - off_t position = sdTellFile( FileID ); + position = sdTellFile( FileID ); M_PUSH(position); - // Just use a 0 if they are the same size. + /* Just use a 0 if they are the same size. */ offsetHi = (sizeof(off_t) > sizeof(cell_t)) ? (position >> (8*sizeof(cell_t))) : 0 ; M_PUSH(offsetHi); TOS = (position < 0) ? -4 : 0 ; /* !!! err num */ @@ -1459,11 +1461,11 @@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql )); case ID_RESIZE: /* ( addr1 u -- addr2 result ) */ { cell_t *Addr1 = (cell_t *) M_POP; - // Point to validator below users address. + /* Point to validator below users address. */ cell_t *FreePtr = Addr1 - 1; if( ((ucell_t)*FreePtr) != ((ucell_t)FreePtr ^ PF_MEMORY_VALIDATOR)) { - // 090218 - Fixed bug, was returning zero. + /* 090218 - Fixed bug, was returning zero. */ M_PUSH( Addr1 ); TOS = -3; } @@ -1476,17 +1478,17 @@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql )); /* Copy memory including validation. */ pfCopyMemory( (char *) CellPtr, (char *) FreePtr, TOS + sizeof(cell_t) ); *CellPtr = (cell_t)(((ucell_t)CellPtr) ^ (ucell_t)PF_MEMORY_VALIDATOR); - // 090218 - Fixed bug that was incrementing the address twice. Thanks Reinhold Straub. - // Increment past validator to user address. + /* 090218 - Fixed bug that was incrementing the address twice. Thanks Reinhold Straub. */ + /* Increment past validator to user address. */ M_PUSH( (cell_t) (CellPtr + 1) ); - TOS = 0; // Result code. - // Mark old cell as dead so we can't free it twice. + TOS = 0; /* Result code. */ + /* Mark old cell as dead so we can't free it twice. */ FreePtr[0] = 0xDeadBeef; pfFreeMem((char *) FreePtr); } else { - // 090218 - Fixed bug, was returning zero. + /* 090218 - Fixed bug, was returning zero. */ M_PUSH( Addr1 ); TOS = -4; /* FIXME Fix error code. */ } diff --git a/csrc/pf_io.h b/csrc/pf_io.h index 5ee78f4..2a4af4b 100644 --- a/csrc/pf_io.h +++ b/csrc/pf_io.h @@ -118,8 +118,14 @@ void ioTerm( void ); #define sdFlushFile fflush #define sdReadFile fread #define sdWriteFile fwrite - #define sdSeekFile fseeko - #define sdTellFile ftello + #if WIN32 + /* TODO To support 64-bit file offset we probably need fseeki64(). */ + #define sdSeekFile fseek + #define sdTellFile ftell + #else + #define sdSeekFile fseeko + #define sdTellFile ftello + #endif #define sdCloseFile fclose #define sdInputChar fgetc diff --git a/csrc/pf_main.c b/csrc/pf_main.c index df38d0c..71defbc 100644 --- a/csrc/pf_main.c +++ b/csrc/pf_main.c @@ -74,6 +74,7 @@ int main( int argc, char **argv ) argc = ccommand(&argv); #endif + pfSetQuiet( FALSE ); /* Parse command line. */ for( i=1; i>56); @@ -247,11 +247,11 @@ void Write16BigEndian( uint8_t *addr, uint16_t data ) /***************************************************************/ void WriteCellLittleEndian( uint8_t *addr, ucell_t data ) { - // Write should be in order of increasing address - // to optimize for burst writes to DRAM. + /* Write should be in order of increasing address + * to optimize for burst writes to DRAM. */ if( sizeof(ucell_t) == 8 ) { - *addr++ = (uint8_t) data; // LSB at near end + *addr++ = (uint8_t) data; /* LSB at near end */ data = data >> 8; *addr++ = (uint8_t) data; data = data >> 8; @@ -653,7 +653,7 @@ DBUG(("pfLoadDictionary( %s )\n", FileName )); gCurrentDictionary = dic; if( sd->sd_NameSize > 0 ) { - gVarContext = (char *) NAMEREL_TO_ABS(sd->sd_RelContext); /* Restore context. */ + gVarContext = NAMEREL_TO_ABS(sd->sd_RelContext); /* Restore context. */ gCurrentDictionary->dic_HeaderPtr = (ucell_t)(uint8_t *) NAMEREL_TO_ABS(sd->sd_RelHeaderPtr); } @@ -674,7 +674,7 @@ DBUG(("pfLoadDictionary( %s )\n", FileName )); pfReportError("pfLoadDictionary", PF_ERR_NO_SHELL ); goto error; #else - if( NAME_BASE == NULL ) + if( NAME_BASE == 0 ) { pfReportError("pfLoadDictionary", PF_ERR_NO_NAMES ); goto error; @@ -689,7 +689,7 @@ DBUG(("pfLoadDictionary( %s )\n", FileName )); pfReportError("pfLoadDictionary", PF_ERR_TOO_BIG); goto error; } - numr = sdReadFile( NAME_BASE, 1, ChunkSize, fid ); + numr = sdReadFile( (char *) NAME_BASE, 1, ChunkSize, fid ); if( numr != ChunkSize ) goto read_error; BytesLeft -= ChunkSize; #endif /* PF_NO_SHELL */ @@ -706,7 +706,7 @@ DBUG(("pfLoadDictionary( %s )\n", FileName )); pfReportError("pfLoadDictionary", PF_ERR_TOO_BIG); goto error; } - numr = sdReadFile( CODE_BASE, 1, ChunkSize, fid ); + numr = sdReadFile( (uint8_t *) CODE_BASE, 1, ChunkSize, fid ); if( numr != ChunkSize ) goto read_error; BytesLeft -= ChunkSize; break; @@ -720,7 +720,7 @@ DBUG(("pfLoadDictionary( %s )\n", FileName )); sdCloseFile( fid ); - if( NAME_BASE != NULL) + if( NAME_BASE != 0) { cell_t Result; /* Find special words in dictionary for global XTs. */ @@ -809,18 +809,18 @@ PForthDictionary pfLoadStaticDictionary( void ) gCurrentDictionary = dic = pfCreateDictionary( NewNameSize, NewCodeSize ); if( !dic ) goto nomem_error; - pfCopyMemory( dic->dic_HeaderBase, MinDicNames, sizeof(MinDicNames) ); - pfCopyMemory( dic->dic_CodeBase, MinDicCode, sizeof(MinDicCode) ); + pfCopyMemory( (uint8_t *) dic->dic_HeaderBase, MinDicNames, sizeof(MinDicNames) ); + pfCopyMemory( (uint8_t *) dic->dic_CodeBase, MinDicCode, sizeof(MinDicCode) ); DBUG(("Static data copied to newly allocated dictionaries.\n")); dic->dic_CodePtr.Byte = (uint8_t *) CODEREL_TO_ABS(CODEPTR); gNumPrimitives = NUM_PRIMITIVES; - if( NAME_BASE != NULL) + if( NAME_BASE != 0) { /* Setup name space. */ dic->dic_HeaderPtr = (ucell_t)(uint8_t *) NAMEREL_TO_ABS(HEADERPTR); - gVarContext = (char *) NAMEREL_TO_ABS(RELCONTEXT); /* Restore context. */ + gVarContext = NAMEREL_TO_ABS(RELCONTEXT); /* Restore context. */ /* Find special words in dictionary for global XTs. */ if( (Result = FindSpecialXTs()) < 0 ) diff --git a/csrc/pfcompil.c b/csrc/pfcompil.c index 84546c4..feb85db 100644 --- a/csrc/pfcompil.c +++ b/csrc/pfcompil.c @@ -77,8 +77,8 @@ void CreateDicEntry( ExecToken XT, const ForthStringPtr FName, ucell_t Flags ) gCurrentDictionary->dic_HeaderPtr += sizeof(cfNameLinks); /* Laydown name. */ - gVarContext = (char *) gCurrentDictionary->dic_HeaderPtr; - pfCopyMemory( (char *)gCurrentDictionary->dic_HeaderPtr, FName, (*FName)+1 ); + gVarContext = gCurrentDictionary->dic_HeaderPtr; + pfCopyMemory( (uint8_t *) gCurrentDictionary->dic_HeaderPtr, FName, (*FName)+1 ); gCurrentDictionary->dic_HeaderPtr += (*FName)+1; /* Set flags. */ @@ -117,7 +117,7 @@ const ForthString *NameToPrevious( const ForthString *NFA ) /* DBUG(("\nNameToPrevious: RelNamePtr = 0x%x\n", (cell_t) RelNamePtr )); */ if( RelNamePtr ) { - return ( NAMEREL_TO_ABS( RelNamePtr ) ); + return ( (ForthString *) NAMEREL_TO_ABS( RelNamePtr ) ); } else { @@ -408,7 +408,7 @@ cell_t ffTokenToName( ExecToken XT, const ForthString **NFAPtr ) cell_t Result = 0; ExecToken TempXT; - NameField = gVarContext; + NameField = (ForthString *) gVarContext; DBUGX(("\ffCodeToName: gVarContext = 0x%x\n", gVarContext)); do @@ -452,7 +452,7 @@ cell_t ffFindNFA( const ForthString *WordName, const ForthString **NFAPtr ) WordLen = (uint8_t) ((ucell_t)*WordName & 0x1F); WordChar = WordName+1; - NameField = gVarContext; + NameField = (ForthString *) gVarContext; DBUG(("\nffFindNFA: WordLen = %d, WordName = %*s\n", WordLen, WordLen, WordChar )); DBUG(("\nffFindNFA: gVarContext = 0x%x\n", gVarContext)); do @@ -600,7 +600,7 @@ static cell_t CheckRedefinition( const ForthStringPtr FName ) if ( flag && !gVarQuiet) { ioType( FName+1, (cell_t) *FName ); - MSG( " redefined.\n" ); // FIXME - allow user to run off this warning. + MSG( " redefined.\n" ); /* FIXME - allow user to run off this warning. */ } return flag; } diff --git a/csrc/pfcustom.c b/csrc/pfcustom.c index ca42f61..1cacea2 100644 --- a/csrc/pfcustom.c +++ b/csrc/pfcustom.c @@ -77,7 +77,7 @@ Err LoadCustomFunctionTable( void ) ** If your loader supports global initialization (most do.) then just ** create the table like this. */ -void *CustomFunctionTable[] = +CFunc0 CustomFunctionTable[] = { (CFunc0) CTest0, (CFunc0) CTest1 diff --git a/releases.txt b/releases.txt index 4830690..615d2cc 100644 --- a/releases.txt +++ b/releases.txt @@ -8,8 +8,10 @@ V27 - Delete object directories in Makefile clean. - Fixed "Issue 4: Filehandle remains locked upon INCLUDE error". http://code.google.com/p/pforth/issues/detail?id=4&can=1 - - Fixed scambled HISTORY on 64-bit systems. Was using CELL+ but really needed 4 +. + - Fixed scrambled HISTORY on 64-bit systems. Was using CELL+ but really needed 4 +. - Fixed floating point input. Now accepts "1E" as 1.0. Was Issue #2. + - Fixed lots of warning and made code compatible with C89 and ANSI with -pedantic. + - Use fseek and ftell on WIN32 instead of fseeko and ftello. V26 5/20/2010 - 64-bit support for M* UM/MOD etc by Aleksej Saushev. Thanks Aleksej! -- 2.20.1