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
This commit is contained in:
cuz 2000-06-08 20:27:05 +00:00
parent 012470bdcb
commit 774b4bb424
11 changed files with 60 additions and 148 deletions

View File

@ -35,6 +35,8 @@
#include <stdio.h>
#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);

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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

View File

@ -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;
}

View File

@ -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

View File

@ -36,8 +36,9 @@
#include <stdio.h>
#include <string.h>
#include "../common/hashstr.h"
#include "error.h"
#include "hashstr.h"
#include "mem.h"
#include "macrotab.h"

View File

@ -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

View File

@ -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

View File

@ -37,6 +37,8 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#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 */