From 774b4bb424a03c1fa972a8ebd275ce99ac484ad4 Mon Sep 17 00:00:00 2001 From: cuz Date: Thu, 8 Jun 2000 20:27:05 +0000 Subject: [PATCH] Some work on function stuff. Use xsprintf from the common directory. Use hashstr from the common directory. git-svn-id: svn://svn.cc65.org/cc65/trunk@36 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/asmline.c | 13 ++------- src/cc65/codegen.c | 5 ++-- src/cc65/codegen.h | 2 +- src/cc65/function.c | 46 ++++++++++++++++++++++++------ src/cc65/function.h | 8 +++++- src/cc65/hashstr.c | 60 ---------------------------------------- src/cc65/hashstr.h | 57 -------------------------------------- src/cc65/macrotab.c | 3 +- src/cc65/make/gcc.mak | 5 ++-- src/cc65/make/watcom.mak | 2 -- src/cc65/symtab.c | 7 +++-- 11 files changed, 60 insertions(+), 148 deletions(-) delete mode 100644 src/cc65/hashstr.c delete mode 100644 src/cc65/hashstr.h diff --git a/src/cc65/asmline.c b/src/cc65/asmline.c index 5559e880e..3c3593b2d 100644 --- a/src/cc65/asmline.c +++ b/src/cc65/asmline.c @@ -35,6 +35,8 @@ #include +#include "../common/xsprintf.h" + #include "error.h" #include "mem.h" #include "asmline.h" @@ -66,21 +68,12 @@ static Line* NewLine (const char* Format, va_list ap) /* Interal routine to create a new line from the given text */ { char Buf [8192]; - int OVF; unsigned Len; Line* L; /* Make a string from the given format and arguments */ -#if defined(__WATCOMC__) - OVF = (_vbprintf (Buf, sizeof (Buf), Format, ap) >= sizeof (S)); -#else - /* Assume gcc running on a Unix OS */ - OVF = (vsnprintf (Buf, sizeof (Buf), Format, ap) < 0); -#endif - if (OVF) { - Internal ("String size overflow"); - } + xvsprintf (Buf, sizeof (Buf), Format, ap); /* Get the length of the line */ Len = strlen (Buf); diff --git a/src/cc65/codegen.c b/src/cc65/codegen.c index b805898f1..7fe2c031a 100644 --- a/src/cc65/codegen.c +++ b/src/cc65/codegen.c @@ -437,10 +437,9 @@ static void ldyconst (unsigned val) static int funcargs; -void g_enter (unsigned flags, const char* Name, unsigned argsize) -/* Function prologue */ +void g_enter (unsigned flags, unsigned argsize) +/* Function prologue */ { - g_defgloblabel (Name); /* Define function name as label */ if ((flags & CF_FIXARGC) != 0) { /* Just remember the argument size for the leave */ funcargs = argsize; diff --git a/src/cc65/codegen.h b/src/cc65/codegen.h index 33bad1d22..6321e6017 100644 --- a/src/cc65/codegen.h +++ b/src/cc65/codegen.h @@ -185,7 +185,7 @@ void g_scale (unsigned flags, long val); -void g_enter (unsigned flags, const char* Name, unsigned argsize); +void g_enter (unsigned flags, unsigned argsize); /* Function prologue */ void g_leave (int flags, int val); diff --git a/src/cc65/function.c b/src/cc65/function.c index f1a160b6d..4915286ce 100644 --- a/src/cc65/function.c +++ b/src/cc65/function.c @@ -71,7 +71,7 @@ Function* CurrentFunc = 0; /*****************************************************************************/ -/* code */ +/* Subroutines working with struct Function */ /*****************************************************************************/ @@ -86,7 +86,7 @@ static Function* NewFunction (struct SymEntry* Sym) F->FuncEntry = Sym; F->ReturnType = Sym->Type + 1 + DECODE_SIZE; F->Desc = DecodePtr (Sym->Type + 1); - F->EntryCode = GetCodePos (); + F->EntryCode = 0; F->LocalMax = 0; F->LocalSize = 0; F->RetLab = GetLabel (); @@ -137,6 +137,14 @@ int HasVoidReturn (const Function* F) +void RememberEntry (Function* F) +/* Remember the current output position for local space creation later */ +{ + F->EntryCode = GetCodePos (); +} + + + unsigned GetRetLab (const Function* F) /* Return the return jump label */ { @@ -145,7 +153,7 @@ unsigned GetRetLab (const Function* F) -unsigned AllocLocalSpace (Function* F, unsigned Size) +int AllocLocalSpace (Function* F, unsigned Size) /* Allocate space for the function locals, return stack offset */ { /* Remember the current offset */ @@ -157,8 +165,8 @@ unsigned AllocLocalSpace (Function* F, unsigned Size) F->LocalMax = F->LocalSize; } - /* Return the offset */ - return Offs; + /* Return the offset, it is below the initial stack pointer */ + return -(int)Offs; } @@ -171,6 +179,20 @@ void FreeLocalSpace (Function* F, unsigned Size) +unsigned GetLocalSpace (const Function* F) +/* Get the local variable space needed for the function */ +{ + return F->LocalMax; +} + + + +/*****************************************************************************/ +/* code */ +/*****************************************************************************/ + + + void NewFunc (SymEntry* Func) /* Parse argument declarations and function body. */ { @@ -190,7 +212,7 @@ void NewFunc (SymEntry* Func) /* C functions cannot currently have __fastcall__ calling conventions */ if (IsFastCallFunc (Func->Type)) { - Error (ERR_FASTCALL); + Error (ERR_FASTCALL); } /* Need a starting curly brace */ @@ -201,9 +223,17 @@ void NewFunc (SymEntry* Func) /* Setup register variables */ InitRegVars (); - /* Switch to the code segment and generate function entry code */ + /* Switch to the code segment and define the function name label */ g_usecode (); - g_enter (TypeOf (Func->Type), Func->Name, GetParamSize (CurrentFunc)); + g_defgloblabel (Func->Name); + + /* Generate function entry code if needed */ + g_enter (TypeOf (Func->Type), GetParamSize (CurrentFunc)); + + /* Remember the current code position to create local variable space once + * we have created the function body itself. + */ + RememberEntry (Func); /* Parse the function body */ oursp = 0; diff --git a/src/cc65/function.h b/src/cc65/function.h index fed098b74..a07683637 100644 --- a/src/cc65/function.h +++ b/src/cc65/function.h @@ -43,10 +43,13 @@ type* GetReturnType (Function* F); int HasVoidReturn (const Function* F); /* Return true if the function does not have a return value */ +void RememberEntry (Function* F); +/* Remember the current output position for local space creation later */ + unsigned GetRetLab (const Function* F); /* Return the return jump label */ -unsigned AllocLocalSpace (Function* F, unsigned Size); +int AllocLocalSpace (Function* F, unsigned Size); /* Allocate space for the function locals, return stack offset */ void FreeLocalSpace (Function* F, unsigned Size); @@ -55,7 +58,10 @@ void FreeLocalSpace (Function* F, unsigned Size); void NewFunc (struct SymEntry* Func); /* Parse argument declarations and function body. */ +unsigned GetLocalSpace (const Function* F); +/* Get the local variable space needed for the function */ + /* End of function.h */ #endif diff --git a/src/cc65/hashstr.c b/src/cc65/hashstr.c deleted file mode 100644 index 23b712119..000000000 --- a/src/cc65/hashstr.c +++ /dev/null @@ -1,60 +0,0 @@ -/*****************************************************************************/ -/* */ -/* hashstr.c */ -/* */ -/* Hash function for strings */ -/* */ -/* */ -/* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ -/* */ -/* */ -/* This software is provided 'as-is', without any expressed or implied */ -/* warranty. In no event will the authors be held liable for any damages */ -/* arising from the use of this software. */ -/* */ -/* Permission is granted to anyone to use this software for any purpose, */ -/* including commercial applications, and to alter it and redistribute it */ -/* freely, subject to the following restrictions: */ -/* */ -/* 1. The origin of this software must not be misrepresented; you must not */ -/* claim that you wrote the original software. If you use this software */ -/* in a product, an acknowledgment in the product documentation would be */ -/* appreciated but is not required. */ -/* 2. Altered source versions must be plainly marked as such, and must not */ -/* be misrepresented as being the original software. */ -/* 3. This notice may not be removed or altered from any source */ -/* distribution. */ -/* */ -/*****************************************************************************/ - - - -#include "hashstr.h" - - - -/*****************************************************************************/ -/* Code */ -/*****************************************************************************/ - - - -unsigned HashStr (const char* S) -/* Return a hash value for the given string */ -{ - unsigned L, H; - - /* Do the hash */ - H = L = 0; - while (*S) { - H = ((H << 3) ^ ((unsigned char) *S++)) + L++; - } - return H; -} - - - diff --git a/src/cc65/hashstr.h b/src/cc65/hashstr.h deleted file mode 100644 index af7f2796c..000000000 --- a/src/cc65/hashstr.h +++ /dev/null @@ -1,57 +0,0 @@ -/*****************************************************************************/ -/* */ -/* hashstr.h */ -/* */ -/* Hash function for strings */ -/* */ -/* */ -/* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ -/* */ -/* */ -/* This software is provided 'as-is', without any expressed or implied */ -/* warranty. In no event will the authors be held liable for any damages */ -/* arising from the use of this software. */ -/* */ -/* Permission is granted to anyone to use this software for any purpose, */ -/* including commercial applications, and to alter it and redistribute it */ -/* freely, subject to the following restrictions: */ -/* */ -/* 1. The origin of this software must not be misrepresented; you must not */ -/* claim that you wrote the original software. If you use this software */ -/* in a product, an acknowledgment in the product documentation would be */ -/* appreciated but is not required. */ -/* 2. Altered source versions must be plainly marked as such, and must not */ -/* be misrepresented as being the original software. */ -/* 3. This notice may not be removed or altered from any source */ -/* distribution. */ -/* */ -/*****************************************************************************/ - - - -#ifndef HASHSTR_H -#define HASHSTR_H - - - -/*****************************************************************************/ -/* Code */ -/*****************************************************************************/ - - - -unsigned HashStr (const char* S); -/* Return a hash value for the given string */ - - - -/* End of hashstr.h */ - -#endif - - - diff --git a/src/cc65/macrotab.c b/src/cc65/macrotab.c index fd41abe46..d0a0a3a67 100644 --- a/src/cc65/macrotab.c +++ b/src/cc65/macrotab.c @@ -36,8 +36,9 @@ #include #include +#include "../common/hashstr.h" + #include "error.h" -#include "hashstr.h" #include "mem.h" #include "macrotab.h" diff --git a/src/cc65/make/gcc.mak b/src/cc65/make/gcc.mak index c50679a8a..9f4a75bcd 100644 --- a/src/cc65/make/gcc.mak +++ b/src/cc65/make/gcc.mak @@ -24,7 +24,6 @@ OBJS = anonname.o \ function.o \ global.o \ goto.o \ - hashstr.o \ ident.o \ include.o \ io.o \ @@ -44,6 +43,8 @@ OBJS = anonname.o \ symtab.o \ util.o +LIBS = ../common/common.a + EXECS = cc65 @@ -58,7 +59,7 @@ endif cc65: $(OBJS) - $(CC) $(LDFLAGS) -o cc65 $(CFLAGS) $(OBJS) + $(CC) $(LDFLAGS) -o cc65 $(CFLAGS) $(OBJS) $(LIBS) clean: rm -f *~ core *.map diff --git a/src/cc65/make/watcom.mak b/src/cc65/make/watcom.mak index 11966fd9d..b2fde194c 100644 --- a/src/cc65/make/watcom.mak +++ b/src/cc65/make/watcom.mak @@ -78,7 +78,6 @@ OBJS = anonname.obj \ function.obj \ global.obj \ goto.obj \ - hashstr.obj \ ident.obj \ include.obj \ io.obj \ @@ -134,7 +133,6 @@ FILE funcdesc.obj FILE function.obj FILE global.obj FILE goto.obj -FILE hashstr.obj FILE ident.obj FILE include.obj FILE io.obj diff --git a/src/cc65/symtab.c b/src/cc65/symtab.c index 6db3fc4a1..f5ccf6a51 100644 --- a/src/cc65/symtab.c +++ b/src/cc65/symtab.c @@ -37,6 +37,8 @@ #include #include #include + +#include "../common/hashstr.h" #include "asmcode.h" #include "asmlabel.h" @@ -47,7 +49,6 @@ #include "error.h" #include "funcdesc.h" #include "global.h" -#include "hashstr.h" #include "io.h" #include "mem.h" #include "symentry.h" @@ -170,7 +171,7 @@ static void CheckSymTable (SymTable* Tab) } } } - + /* If the entry is a label, check if it was defined in the function */ if (Flags & SC_LABEL) { if ((Flags & SC_DEF) == 0) { @@ -181,7 +182,7 @@ static void CheckSymTable (SymTable* Tab) Warning (WARN_UNUSED_ITEM, Entry->Name); } } - + } /* Next entry */