From df70ebb5f7378d4859ac93886f85feb0790021da Mon Sep 17 00:00:00 2001 From: suborb Date: Wed, 22 Feb 2017 20:33:15 +0000 Subject: [PATCH 01/16] Start a bit of cleanup and get my head into adding port support --- src/sccz80/codegen.c | 3 ++- src/sccz80/const.c | 7 ++----- src/sccz80/declfunc.c | 4 ++-- src/sccz80/declfunc.h | 4 ++-- src/sccz80/define.h | 32 +++++++++++++++++--------------- src/sccz80/sym.c | 8 ++++---- src/sccz80/sym.h | 8 ++++---- 7 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/sccz80/codegen.c b/src/sccz80/codegen.c index fa916e9928..7252ca1440 100644 --- a/src/sccz80/codegen.c +++ b/src/sccz80/codegen.c @@ -394,8 +394,9 @@ void PutFrame(char typeobj, int offset) /* at the address on the top of the stack */ void putstk(char typeobj) { - SYMBOL* ptr; char flags; + SYMBOL *ptr; + /* Store via long pointer... */ ptr = retrstk(&flags); if (flags & FARACC) { diff --git a/src/sccz80/const.c b/src/sccz80/const.c index b653485c52..b804025d7d 100644 --- a/src/sccz80/const.c +++ b/src/sccz80/const.c @@ -92,7 +92,6 @@ int fnumber(int32_t* val) char minus; /* is if negative! */ char* start; /* copy of pointer to starting point */ char* s; /* points into source code */ - char* dp1; /* First number after dp */ char* end; double dval; @@ -122,7 +121,6 @@ int fnumber(int32_t* val) s++; return 0; } - dp1 = ++s; while (numeric(*s)) ++s; lptr = (s--) - line; /* save ending point */ @@ -317,9 +315,8 @@ int pstr(int32_t* val) int tstr(int32_t* val) { - int k, j; + int k = 0; - j = k = 0; if (cmatch('"') == 0) return (0); do { @@ -495,7 +492,7 @@ void size_of(LVALUE* lval) TAG_SYMBOL* otag; SYMBOL* ptr; struct varid var; - char ident; + enum ident_type ident; needchar('('); otag = GetVarID(&var, NO); diff --git a/src/sccz80/declfunc.c b/src/sccz80/declfunc.c index 9852cdb13b..3eaf3a20ab 100644 --- a/src/sccz80/declfunc.c +++ b/src/sccz80/declfunc.c @@ -65,7 +65,7 @@ int AddNewFunc( char zfar, char sign, TAG_SYMBOL* otag, - int ident, + enum ident_type ident, int32_t* addr) { SYMBOL* ptr; @@ -157,7 +157,7 @@ void newfunc() SYMBOL* #endif -AddFuncCode(char* n, char type, char ident, char sign, char zfar, int storage, int more, char check, char simple, TAG_SYMBOL* otag, int32_t* addr) +AddFuncCode(char* n, char type, enum ident_type ident, char sign, char zfar, int storage, int more, char check, char simple, TAG_SYMBOL* otag, int32_t* addr) { unsigned char tvalue; /* Used to hold protot value */ char typ; /* Temporary type */ diff --git a/src/sccz80/declfunc.h b/src/sccz80/declfunc.h index 297ac85c37..0b54c985a2 100644 --- a/src/sccz80/declfunc.h +++ b/src/sccz80/declfunc.h @@ -1,8 +1,8 @@ /* declfunc.c */ -extern int AddNewFunc(char *sname, int type, int storage, char zfar, char sign, TAG_SYMBOL *otag, int ident, int32_t *addr); +extern int AddNewFunc(char *sname, int type, int storage, char zfar, char sign, TAG_SYMBOL *otag, enum ident_type ident, int32_t *addr); extern void newfunc(void); -extern SYMBOL *AddFuncCode(char *n, char type, char ident, char sign, char zfar, int storage, int more, char check, char simple, TAG_SYMBOL *otag, int32_t *addr); +extern SYMBOL *AddFuncCode(char *n, char type, enum ident_type ident, char sign, char zfar, int storage, int more, char check, char simple, TAG_SYMBOL *otag, int32_t *addr); extern void DoFnKR(SYMBOL *currfn, char simple); extern void setlocvar(SYMBOL *prevarg, SYMBOL *currfn); extern SYMBOL *dofnansi(SYMBOL *currfn, int32_t *addr); diff --git a/src/sccz80/define.h b/src/sccz80/define.h index 498b273a13..8fde7bcb9c 100644 --- a/src/sccz80/define.h +++ b/src/sccz80/define.h @@ -50,6 +50,19 @@ #define STARTLOC loctab #define ENDLOC (STARTLOC+NUMLOC) +enum ident_type { + VARIABLE, + ARRAY, + POINTER, + FUNCTION, + MACRO, + FUNCTIONP, + GOTOLABEL, + /* Only used is processing, not in symbol table */ + PTR_TO_FN, + PTR_TO_PTR, + PTR_TO_FNP +}; /* Define symbol table entry format */ #define SYMBOL struct symb @@ -57,7 +70,7 @@ SYMBOL { char name[NAMESIZE] ; - char ident ; /*VARIABLE, ARRAY, POINTER, FUNCTION, MACRO */ + enum ident_type ident; char type ; /* DOUBLE, CINT, CCHAR, STRUCT */ char storage ; /* STATIK, STKLOC, EXTERNAL */ union xx { /* offset has a number of interpretations: */ @@ -72,7 +85,7 @@ SYMBOL { int size ; /* djm, storage reqd! */ char handled; /* djm, whether we've written the type or not */ char prototyped; - unsigned char args[MAXARGS]; /* arguments */ + uint32_t args[MAXARGS]; /* arguments */ unsigned char tagarg[MAXARGS]; /* ptrs to tagsymbol entries*/ int flags ; /* djm, various flags: bit 0 = unsigned @@ -90,18 +103,6 @@ SYMBOL { /* Define possible entries for "ident" */ -#define VARIABLE 1 -#define ARRAY 2 -#define POINTER 3 -#define FUNCTION 4 -#define MACRO 5 -/* function returning pointer */ -#define FUNCTIONP 6 -#define GOTOLABEL 9 -/* the following only used in processing, not in symbol table */ -#define PTR_TO_FN 7 -#define PTR_TO_PTR 8 -#define PTR_TO_FNP 10 /* Define possible entries for "type" */ @@ -152,6 +153,7 @@ SYMBOL { #define FAR 7 #define LSTKEXT 8 #define TYPDEF 9 +#define PORT 10 /* Flags */ @@ -325,7 +327,7 @@ struct varid { unsigned char sign; unsigned char sflag; unsigned char defstatus; /* APPFUNC, LIBFUNC etc */ - unsigned char ident; + enum ident_type ident; int more; }; diff --git a/src/sccz80/sym.c b/src/sccz80/sym.c index 99a462edb0..8b76372a92 100644 --- a/src/sccz80/sym.c +++ b/src/sccz80/sym.c @@ -116,7 +116,7 @@ SYMBOL* findmemb(TAG_SYMBOL* tag, char* sname) } SYMBOL* addglb( - char* sname, char id, char typ, + char* sname, enum ident_type id, char typ, int value, int storage, int more, int itag) { SYMBOL* ptr; @@ -151,7 +151,7 @@ SYMBOL* addglb( SYMBOL* addloc( char* sname, - char id, + enum ident_type id, char typ, int more, int itag) @@ -176,7 +176,7 @@ SYMBOL* addloc( */ SYMBOL* addmemb( char* sname, - char id, + enum ident_type id, char typ, int value, int storage, @@ -200,7 +200,7 @@ SYMBOL* addmemb( void addsym( SYMBOL* ptr, char* sname, - char id, + enum ident_type id, char typ, int storage, int more, diff --git a/src/sccz80/sym.h b/src/sccz80/sym.h index 95c4036045..88452ab794 100644 --- a/src/sccz80/sym.h +++ b/src/sccz80/sym.h @@ -6,7 +6,7 @@ extern SYMBOL *findenum(char *sname); extern SYMBOL *findloc(char *sname); extern TAG_SYMBOL *findtag(char *sname); extern SYMBOL *findmemb(TAG_SYMBOL *tag, char *sname); -extern SYMBOL *addglb(char *sname, char id, char typ, int value, int storage, int more, int itag); -extern SYMBOL *addloc(char *sname, char id, char typ, int more, int itag); -extern SYMBOL *addmemb(char *sname, char id, char typ, int value, int storage, int more, int itag); -extern void addsym(SYMBOL *ptr, char *sname, char id, char typ, int storage, int more, int itag); +extern SYMBOL *addglb(char *sname, enum ident_type id, char typ, int value, int storage, int more, int itag); +extern SYMBOL *addloc(char *sname, enum ident_type id, char typ, int more, int itag); +extern SYMBOL *addmemb(char *sname, enum ident_type id, char typ, int value, int storage, int more, int itag); +extern void addsym(SYMBOL *ptr, char *sname, enum ident_type id, char typ, int storage, int more, int itag); From 8b7f29b695242bcc18625c615493ad53d43a3700 Mon Sep 17 00:00:00 2001 From: suborb Date: Wed, 22 Feb 2017 21:38:44 +0000 Subject: [PATCH 02/16] Bit more cleanup --- src/sccz80/declfunc.c | 23 ++++-------- src/sccz80/declfunc.h | 4 +-- src/sccz80/declvar.c | 12 +++---- src/sccz80/declvar.h | 8 ++--- src/sccz80/define.h | 82 +++++++++++++++---------------------------- src/sccz80/expr.c | 25 ++++++------- src/sccz80/io.c | 18 ++-------- src/sccz80/lvalue.h | 7 ++-- src/sccz80/main.c | 50 +++++++++++--------------- src/sccz80/plunge.c | 2 +- src/sccz80/preproc.c | 6 ++-- src/sccz80/primary.c | 10 +++--- src/sccz80/primary.h | 8 ++--- src/sccz80/stmt.c | 2 +- src/sccz80/sym.c | 6 ++-- src/sccz80/sym.h | 6 ++-- src/sccz80/while.c | 5 +-- 17 files changed, 104 insertions(+), 170 deletions(-) diff --git a/src/sccz80/declfunc.c b/src/sccz80/declfunc.c index 3eaf3a20ab..30685f835e 100644 --- a/src/sccz80/declfunc.c +++ b/src/sccz80/declfunc.c @@ -61,7 +61,7 @@ void StoreFunctionSignature(SYMBOL* ptr) int AddNewFunc( char* sname, int type, - int storage, + enum storage_type storage, char zfar, char sign, TAG_SYMBOL* otag, @@ -152,12 +152,7 @@ void newfunc() * and also from AddNewFunc(), returns 0 if added a real * function (code etc) */ - -#ifndef SMALL_C -SYMBOL* -#endif - -AddFuncCode(char* n, char type, enum ident_type ident, char sign, char zfar, int storage, int more, char check, char simple, TAG_SYMBOL* otag, int32_t* addr) +SYMBOL *AddFuncCode(char* n, char type, enum ident_type ident, char sign, char zfar, enum storage_type storage, int more, char check, char simple, TAG_SYMBOL* otag, int32_t* addr) { unsigned char tvalue; /* Used to hold protot value */ char typ; /* Temporary type */ @@ -307,7 +302,7 @@ void DoFnKR( } else if (var.type || regit) { if (regit && var.type == NO) var.type = CINT; - getarg(var.type, NULL_TAG, NO, 0, var.sign, var.zfar, NO); + getarg(var.type, NULL, NO, 0, var.sign, var.zfar, NO); } else { error(E_BADARG); break; @@ -461,10 +456,7 @@ void setlocvar(SYMBOL* prevarg, SYMBOL* currfn) } /* djm Declare a function in the ansi style! */ -#ifndef SMALL_C -SYMBOL* -#endif -dofnansi(SYMBOL* currfn, int32_t* addr) +SYMBOL *dofnansi(SYMBOL* currfn, int32_t* addr) { SYMBOL* prevarg; /* ptr to symbol table entry of most recent argument */ SYMBOL* argptr; /* Temporary holder.. */ @@ -501,7 +493,7 @@ dofnansi(SYMBOL* currfn, int32_t* addr) if (var.type == STRUCT) { prevarg = getarg(STRUCT, otag, YES, prevarg, 0, var.zfar, proto); } else if (var.type) { - prevarg = getarg(var.type, NULL_TAG, YES, prevarg, var.sign, var.zfar, proto); + prevarg = getarg(var.type, NULL, YES, prevarg, var.sign, var.zfar, proto); } else { warning(W_EXPARG); @@ -585,10 +577,7 @@ int CheckANSI() * called from "newfunc" this routine adds an entry in the * local symbol table for each named argument */ -#ifndef SMALL_C -SYMBOL* -#endif -getarg( +SYMBOL *getarg( int typ, /* typ = CCHAR, CINT, DOUBLE or STRUCT */ TAG_SYMBOL* otag, /* structure tag for STRUCT type objects */ int deftype, /* YES=ANSI -> addloc NO=K&R -> findloc */ diff --git a/src/sccz80/declfunc.h b/src/sccz80/declfunc.h index 0b54c985a2..48bb2b3d76 100644 --- a/src/sccz80/declfunc.h +++ b/src/sccz80/declfunc.h @@ -1,8 +1,8 @@ /* declfunc.c */ -extern int AddNewFunc(char *sname, int type, int storage, char zfar, char sign, TAG_SYMBOL *otag, enum ident_type ident, int32_t *addr); +extern int AddNewFunc(char *sname, int type, enum storage_type storage, char zfar, char sign, TAG_SYMBOL *otag, enum ident_type ident, int32_t *addr); extern void newfunc(void); -extern SYMBOL *AddFuncCode(char *n, char type, enum ident_type ident, char sign, char zfar, int storage, int more, char check, char simple, TAG_SYMBOL *otag, int32_t *addr); +extern SYMBOL *AddFuncCode(char *n, char type, enum ident_type ident, char sign, char zfar, enum storage_type storage, int more, char check, char simple, TAG_SYMBOL *otag, int32_t *addr); extern void DoFnKR(SYMBOL *currfn, char simple); extern void setlocvar(SYMBOL *prevarg, SYMBOL *currfn); extern SYMBOL *dofnansi(SYMBOL *currfn, int32_t *addr); diff --git a/src/sccz80/declvar.c b/src/sccz80/declvar.c index fdb899aebe..3218b7ffdb 100644 --- a/src/sccz80/declvar.c +++ b/src/sccz80/declvar.c @@ -32,7 +32,7 @@ */ int dodeclare( - int storage, + enum storage_type storage, TAG_SYMBOL* mtag, /* tag of struct whose members are being declared, or zero */ int is_struct /* TRUE if struct member is being declared, zero for union */ ) @@ -54,7 +54,7 @@ int dodeclare( declglb(STRUCT, storage, mtag, otag, is_struct, var.sign, var.zfar); return (1); } else { - declglb(var.type, storage, mtag, NULL_TAG, is_struct, var.sign, var.zfar); + declglb(var.type, storage, mtag, NULL, is_struct, var.sign, var.zfar); return (1); } } @@ -65,7 +65,7 @@ int dodeclare( */ TAG_SYMBOL* -defstruct(char* sname, int storage, int is_struct) +defstruct(char* sname, enum storage_type storage, int is_struct) { int itag; /* index of tag in tag symbol table */ char nam[20]; /* Dummy name */ @@ -120,7 +120,7 @@ defstruct(char* sname, int storage, int is_struct) * declare an enum, ATP we have to parse between the curly brackets */ -void defenum(char* sname, char storage) +void defenum(char* sname, enum storage_type storage) { SYMBOL* ptr; char name[NAMEMAX]; @@ -181,7 +181,7 @@ int dummy_idx(int typ, TAG_SYMBOL* otag) */ void declglb( int typ, /* typ is CCHAR, CINT, DOUBLE, STRUCT, LONG, */ - int storage, + enum storage_type storage, TAG_SYMBOL* mtag, /* tag of struct whose members are being declared, or zero */ TAG_SYMBOL* otag, /* tag of struct for object being declared */ int is_struct, /* TRUE if struct member being declared, zero if union */ @@ -757,7 +757,7 @@ int needsub(void) * djm 18/3/99 */ -TAG_SYMBOL* GetVarID(struct varid* var, char storage) +TAG_SYMBOL* GetVarID(struct varid* var, enum storage_type storage) { TAG_SYMBOL* otag = NULL; char sname[NAMEMAX]; diff --git a/src/sccz80/declvar.h b/src/sccz80/declvar.h index 9918466243..be77299af9 100644 --- a/src/sccz80/declvar.h +++ b/src/sccz80/declvar.h @@ -1,15 +1,15 @@ /* decl.c */ -extern int dodeclare(int storage, TAG_SYMBOL *mtag, int is_struct); -extern TAG_SYMBOL *defstruct(char *sname, int storage, int is_struct); +extern int dodeclare(enum storage_type storage, TAG_SYMBOL *mtag, int is_struct); +extern TAG_SYMBOL *defstruct(char *sname, enum storage_type storage, int is_struct); extern int get_ident(void); extern int dummy_idx(int typ, TAG_SYMBOL *otag); -extern void declglb(int typ, int storage, TAG_SYMBOL *mtag, TAG_SYMBOL *otag, int is_struct, char sign, char zfar); +extern void declglb(int typ, enum storage_type storage, TAG_SYMBOL *mtag, TAG_SYMBOL *otag, int is_struct, char sign, char zfar); extern void declloc(int typ, TAG_SYMBOL *otag, char sign, char locstatic, char zfar); extern unsigned char CalcArgValue(char type, char ident, char flags); extern char *ExpandArgValue(unsigned char value, char *buffer, char tagidx); extern void ptrerror(int ident); extern int needsub(void); -extern TAG_SYMBOL *GetVarID(struct varid *var,char storage); +extern TAG_SYMBOL *GetVarID(struct varid *var,enum storage_type storage); extern void BitFieldSwallow(void); extern char *ExpandType(int type, char **sign, char tagidx); extern int get_type_size(int type, TAG_SYMBOL *otag); diff --git a/src/sccz80/define.h b/src/sccz80/define.h index 8fde7bcb9c..520a3a12d3 100644 --- a/src/sccz80/define.h +++ b/src/sccz80/define.h @@ -7,13 +7,7 @@ #define NO 0 #define YES 1 -#ifndef NULL -#define NULL 0 -#endif -#define NULL_FD 0 -#define NULL_FN 0 -#define NULL_CHAR 0 #define alloc malloc @@ -51,7 +45,7 @@ #define ENDLOC (STARTLOC+NUMLOC) enum ident_type { - VARIABLE, + VARIABLE = 1, ARRAY, POINTER, FUNCTION, @@ -63,16 +57,35 @@ enum ident_type { PTR_TO_PTR, PTR_TO_FNP }; + +enum storage_type { + UNKNOWN = 0, + STATIK, + STKLOC, + EXTERNAL, + EXTERNP, + DECLEXTN, + LSTATIC, + FAR, + LSTKEXT, + TYPDEF, + PORT8, + PORT16 +}; + + + /* Define symbol table entry format */ -#define SYMBOL struct symb -#define TAG_SYMBOL struct tag_symbol +typedef struct tagsymbol_s TAG_SYMBOL; +typedef struct symbol_s SYMBOL; -SYMBOL { + +struct symbol_s { char name[NAMESIZE] ; enum ident_type ident; char type ; /* DOUBLE, CINT, CCHAR, STRUCT */ - char storage ; /* STATIK, STKLOC, EXTERNAL */ + enum storage_type storage ; /* STATIK, STKLOC, EXTERNAL */ union xx { /* offset has a number of interpretations: */ int i ; /* local symbol: offset into stack */ /* struct member: offset into struct */ @@ -93,15 +106,7 @@ SYMBOL { bit 2 = access via far methods */ -} ; - -#ifdef SMALL_C -#define NULL_SYM 0 -#else -#define NULL_SYM (SYMBOL *)0 -#endif - -/* Define possible entries for "ident" */ +}; /* Define possible entries for "type" */ @@ -144,16 +149,8 @@ SYMBOL { /* Define possible entries for "storage" */ -#define STATIK 1 -#define STKLOC 2 -#define EXTERNAL 3 -#define EXTERNP 4 -#define DECLEXTN 5 -#define LSTATIC 6 -#define FAR 7 -#define LSTKEXT 8 -#define TYPDEF 9 -#define PORT 10 + + /* Flags */ @@ -183,7 +180,7 @@ SYMBOL { #define STARTTAG tagtab #define ENDTAG tagtab+NUMTAG -struct tag_symbol { +struct tagsymbol_s { char name[NAMESIZE] ; /* structure tag name */ int size ; /* size of struct in bytes */ char weak; /* Not fully defined */ @@ -193,12 +190,6 @@ struct tag_symbol { -#ifdef SMALL_C -#define NULL_TAG 0 -#else -#define NULL_TAG (TAG_SYMBOL *)0 -#endif - /* Define the structure member table parameters */ #define NUMMEMB 2000 @@ -300,33 +291,16 @@ GOTO_TAB { #define MAX_LEVELS 100 -#ifdef SMALL_C -#define SYM_CAST -#define TAG_CAST -#define WQ_CAST -#define SW_CAST -#else -#define SYM_CAST (SYMBOL *) -#define TAG_CAST (TAG_SYMBOL *) -#define WQ_CAST (WHILE_TAB *) -#define SW_CAST (SW_TAB *) -#endif - - /* * djm, function for variable definitions now */ -#define APPFUNC 1 -#define LIBFUNC 2 - struct varid { unsigned char type; unsigned char zfar; unsigned char sign; unsigned char sflag; - unsigned char defstatus; /* APPFUNC, LIBFUNC etc */ enum ident_type ident; int more; }; diff --git a/src/sccz80/expr.c b/src/sccz80/expr.c index f5ae3aa71d..10ddd872fb 100644 --- a/src/sccz80/expr.c +++ b/src/sccz80/expr.c @@ -456,12 +456,7 @@ int heir9(LVALUE* lval) /* djm, I can't make this routine distinguish between ptr->ptr and ptr * so if address loads dummy de,0 to ensure everything works out */ - -#ifndef SMALL_C -SYMBOL* -#endif - -deref(LVALUE* lval, char isaddr) +SYMBOL *deref(LVALUE* lval, char isaddr) { char flags; flags = lval->flags; @@ -482,7 +477,7 @@ deref(LVALUE* lval, char isaddr) // else flags &= ~FARACC; lval->val_type = lval->indirect = lval->symbol->type; lval->flags = flags; - lval->symbol = NULL_SYM; /* forget symbol table entry */ + lval->symbol = NULL; /* forget symbol table entry */ lval->ptr_type = 0; /* flag as not symbol or array */ lval->ident = VARIABLE; /* We're now a variable! */ } else { @@ -554,7 +549,7 @@ int heira(LVALUE* lval) intcheck(lval, lval); com(lval); lval->const_val = ~lval->const_val; - lval->stage_add = NULL_CHAR; + lval->stage_add = NULL; return 0; } else if (cmatch('!')) { if (heira(lval)) @@ -562,7 +557,7 @@ int heira(LVALUE* lval) lneg(lval); lval->binop = lneg; lval->const_val = !lval->const_val; - lval->stage_add = NULL_CHAR; + lval->stage_add = NULL; return 0; } else if (cmatch('-')) { if (heira(lval)) @@ -570,7 +565,7 @@ int heira(LVALUE* lval) neg(lval); if (lval->val_type != DOUBLE) lval->const_val = -lval->const_val; - lval->stage_add = NULL_CHAR; + lval->stage_add = NULL; return 0; } else if (cmatch('*')) { /* unary * */ if (heira(lval)) @@ -722,14 +717,14 @@ int heirb(LVALUE* lval) k = 1; } else if (cmatch('(')) { if (ptr == NULL) { - callfunction(NULL_SYM); + callfunction(NULL); /* Bugger knows what ya doing..stop SEGV */ ptr = dummy_sym[VOID]; warning(W_INTERNAL); } else if (ptr->ident != FUNCTION) { if (k && lval->const_val == 0) rvalue(lval); - callfunction(NULL_SYM); + callfunction(NULL); } else callfunction(ptr); k = lval->is_const = lval->const_val = 0; @@ -801,9 +796,9 @@ int heirb(LVALUE* lval) lval->indirect = lval->val_type = ptr->type; lval->ptr_type = lval->is_const = lval->const_val = 0; lval->ident = VARIABLE; - lval->stage_add = NULL_CHAR; - lval->tagsym = NULL_TAG; - lval->binop = NULL_FN; + lval->stage_add = NULL; + lval->tagsym = NULL; + lval->binop = NULL; if (ptr->type == STRUCT) lval->tagsym = tagtab + ptr->tag_idx; if (ptr->ident == POINTER) { diff --git a/src/sccz80/io.c b/src/sccz80/io.c index 4951e11a20..82f97db821 100644 --- a/src/sccz80/io.c +++ b/src/sccz80/io.c @@ -46,20 +46,6 @@ int symname(char* sname) { int k; -#ifdef SMALL_C - { - char* p; - char c; - - /* this is about as deep as nesting goes, check memory left */ - p = alloc(1); - /* &c is top of stack, p is end of heap */ - if ((k = &c - p) < minavail) - minavail = k; - free(p); - } -#endif - blanks(); if (alpha(ch()) == 0) return (*sname = 0); @@ -198,7 +184,7 @@ void clearstage(char* before, char* start) if ((stagenext = before)) return; if (start) { - if (output != NULL_FD) { + if (output != NULL) { #ifdef INBUILT_OPTIMIZER if (infunc) AddBuffer(start); @@ -235,7 +221,7 @@ void tofile() int outbyte(char c) { if (c) { - if (output != NULL_FD) { + if (output != NULL) { if (stagenext) { return (outstage(c)); } else { diff --git a/src/sccz80/lvalue.h b/src/sccz80/lvalue.h index d5dc15f744..6bb4f9f8ef 100644 --- a/src/sccz80/lvalue.h +++ b/src/sccz80/lvalue.h @@ -4,7 +4,9 @@ * $Id: lvalue.h,v 1.4 2009-09-06 18:58:37 dom Exp $ */ -struct lvalue { +typedef struct lvalue_s LVALUE; + +struct lvalue_s { SYMBOL *symbol ; /* symbol table address, or 0 for constant */ int indirect ; /* type of indirect object, 0 for static object */ int ptr_type ; /* type of pointer or array, 0 for other idents */ @@ -19,7 +21,7 @@ struct lvalue { char oflags; /* Needed for deref of far str*/ int type; /* type (from symbol table) */ int ident; /* ident (from symbol table) */ - char storage; /* storage (from sym tab) */ + enum storage_type storage; /* storage (from sym tab) */ char c_id; /* ident of cast */ char c_vtype; /* type of value calc if cast */ char c_flags; /* flags for casting */ @@ -29,4 +31,3 @@ struct lvalue { TAG_SYMBOL *c_tag; } ; -#define LVALUE struct lvalue diff --git a/src/sccz80/main.c b/src/sccz80/main.c index dfbef4cf79..bfb3462645 100644 --- a/src/sccz80/main.c +++ b/src/sccz80/main.c @@ -68,17 +68,14 @@ int main(int argc, char** argv) dubq = mymalloc(FNLITQ); /* Doubles */ tempq = mymalloc(LITABSZ); /* Temp strings... */ glbq = mymalloc(LITABSZ); /* Used for glb lits, dumped now */ - symtab = SYM_CAST mymalloc(NUMGLBS * sizeof(SYMBOL)); - loctab = SYM_CAST mymalloc(NUMLOC * sizeof(SYMBOL)); - wqueue = WQ_CAST mymalloc(NUMWHILE * sizeof(WHILE_TAB)); - gotoq = (GOTO_TAB*)calloc(NUMGOTO, sizeof(GOTO_TAB)); - if (gotoq == NULL) - OutOfMem(); - - tagptr = tagtab = TAG_CAST mymalloc(NUMTAG * sizeof(TAG_SYMBOL)); - membptr = membtab = SYM_CAST mymalloc(NUMMEMB * sizeof(SYMBOL)); - - swnext = SW_CAST mymalloc(NUMCASE * sizeof(SW_TAB)); + symtab = mymalloc(NUMGLBS * sizeof(SYMBOL)); + loctab = mymalloc(NUMLOC * sizeof(SYMBOL)); + wqueue = mymalloc(NUMWHILE * sizeof(WHILE_TAB)); + gotoq = mymalloc(NUMGOTO * sizeof(GOTO_TAB)); + tagptr = tagtab = mymalloc(NUMTAG * sizeof(TAG_SYMBOL)); + membptr = membtab = mymalloc(NUMMEMB * sizeof(SYMBOL)); + + swnext = mymalloc(NUMCASE * sizeof(SW_TAB)); swend = swnext + (NUMCASE - 1); stage = mymalloc(STAGESIZE); @@ -112,14 +109,14 @@ int main(int argc, char** argv) infunc = /* not in function now */ 0; /* ...all set to zero.... */ - stagenext = NULL_CHAR; /* direct output mode */ + stagenext = NULL; /* direct output mode */ input = /* no input file */ inpt2 = /* or include file */ saveout = /* no diverted output */ - output = NULL_FD; /* no open units */ + output = NULL; /* no open units */ - currfn = NULL_SYM; /* no function yet */ + currfn = NULL; /* no function yet */ macptr = cmode = 1; /* clear macro pool and enable preprocessing */ ncomp = doinline = mathz88 = incfloat = compactcode = 0; cppcom = 0; @@ -187,9 +184,9 @@ int main(int argc, char** argv) */ void ccabort() { - if (inpt2 != NULL_FD) + if (inpt2 != NULL) endinclude(); - if (input != NULL_FD) + if (input != NULL) fclose(input); closeout(); fprintf(stderr, "Compilation aborted\n"); @@ -207,12 +204,12 @@ void parse() { while (eof == 0) { /* do until no more input */ if (amatch("extern")) { - dodeclare(EXTERNAL, NULL_TAG, 0); + dodeclare(EXTERNAL, NULL, 0); } else if (amatch("static")) { - dodeclare(LSTATIC, NULL_TAG, 0); + dodeclare(LSTATIC, NULL, 0); } else if (amatch("typedef")) { - dodeclare(TYPDEF, NULL_TAG, 0); - } else if (dodeclare(STATIK, NULL_TAG, 0)) { + dodeclare(TYPDEF, NULL, 0); + } else if (dodeclare(STATIK, NULL, 0)) { ; } else if (ch() == '#') { if (match("#asm")) { @@ -235,12 +232,7 @@ void parse() /* * Report errors for user */ - -#ifndef SMALL_C -void -#endif - -errsummary() +void errsummary() { /* see if anything left hanging... */ if (ncmp) { @@ -268,13 +260,13 @@ char *nextarg(int n, char* s, int size) int i; if (n < 1 || n >= gargc) - return NULL_CHAR; + return NULL; i = 0; str = str2 = gargv[n]; while (++i < size && (*s++ = *str++)) ; if (*str2 == '\0') - return NULL_CHAR; + return NULL; return s; } @@ -621,7 +613,7 @@ void openout() FILE* fp; clear(); /* erase line */ output = 0; /* start with none */ - if (nextarg(filenum, filen2, FILENAME_LEN) == NULL_CHAR) + if (nextarg(filenum, filen2, FILENAME_LEN) == NULL) return; if ((fp = fopen(filen2, "r")) == NULL) { fprintf(stderr, "Cannot open source file: %s\n", filen2); diff --git a/src/sccz80/plunge.c b/src/sccz80/plunge.c index 8331199071..7a07fc5df8 100644 --- a/src/sccz80/plunge.c +++ b/src/sccz80/plunge.c @@ -41,7 +41,7 @@ int skim(char* opstr, void (*testfuncz)(), void (*testfuncq)(), int dropval, int postlabel(endlab); lval->val_type = lval->oldval_type = CINT; /* stops the carry stuff coming in */ lval->indirect = lval->ptr_type = lval->is_const = lval->const_val = 0; - lval->stage_add = NULL_CHAR; + lval->stage_add = NULL; lval->binop = dummy; return (0); } else diff --git a/src/sccz80/preproc.c b/src/sccz80/preproc.c index 9db8e780b9..68d430c3b0 100644 --- a/src/sccz80/preproc.c +++ b/src/sccz80/preproc.c @@ -67,11 +67,11 @@ void vinline() int k; while (1) { - if (input == NULL_FD) + if (input == NULL) openin(); if (eof) return; - if ((unit = inpt2) == NULL_FD) + if ((unit = inpt2) == NULL) unit = input; clear(); while ((k = getc(unit)) > 0) { @@ -84,7 +84,7 @@ void vinline() ++lineno; /* read one more line */ if (k <= 0) { fclose(unit); - if (inpt2 != NULL_FD) + if (inpt2 != NULL) endinclude(); else { input = 0; diff --git a/src/sccz80/primary.c b/src/sccz80/primary.c index 7ed6b6a0bd..7e3c8468f1 100644 --- a/src/sccz80/primary.c +++ b/src/sccz80/primary.c @@ -87,7 +87,7 @@ int primary(LVALUE* lval) if (ptr->ident == ENUM) error(E_UNSYMB, sname); if (ptr->type == ENUM) { - lval->symbol = NULL_SYM; + lval->symbol = NULL; lval->indirect = 0; lval->is_const = 1; lval->const_val = ptr->size; @@ -148,7 +148,7 @@ int primary(LVALUE* lval) return (0); } if (constant(lval)) { - lval->symbol = NULL_SYM; + lval->symbol = NULL; lval->indirect = 0; lval->ident = VARIABLE; return (0); @@ -175,7 +175,7 @@ void dcerror(LVALUE* lval) */ int calc( int left, - void (*oper)(struct lvalue*), + void (*oper)(LVALUE *), int right) { if (oper == zdiv) @@ -198,7 +198,7 @@ int calc( int calcun( unsigned int left, - void (*oper)(struct lvalue*), + void (*oper)(LVALUE *), unsigned int right) { if (oper == zdiv) @@ -225,7 +225,7 @@ int calcun( int CalcStand( int left, - void (*oper)(struct lvalue*), + void (*oper)(LVALUE *), int right) { if (oper == zor) diff --git a/src/sccz80/primary.h b/src/sccz80/primary.h index c6dd37a505..4fd8f452c1 100644 --- a/src/sccz80/primary.h +++ b/src/sccz80/primary.h @@ -1,9 +1,9 @@ /* primary.c */ extern int primary(LVALUE *lval); extern void dcerror(LVALUE *lval); -extern int calc(int left, void (*oper)(struct lvalue *), int right); -extern int calcun(unsigned int left, void (*oper)(struct lvalue *),unsigned int right); -extern int CalcStand(int left, void (*oper)(struct lvalue *), int right); +extern int calc(int left, void (*oper)(LVALUE *), int right); +extern int calcun(unsigned int left, void (*oper)(LVALUE *),unsigned int right); +extern int CalcStand(int left, void (*oper)(LVALUE *), int right); extern void intcheck(LVALUE *lval, LVALUE *lval2); extern void force(int t1, int t2, char sign1, char sign2, int lconst); extern int widen(LVALUE *lval, LVALUE *lval2); @@ -21,7 +21,7 @@ extern void rvalload(LVALUE *lval); extern void rvalue(LVALUE *lval); extern void test(int label, int parens); extern int constexpr(int32_t *val,int flag); -extern void cscale(int type, struct tag_symbol *tag, int *val); +extern void cscale(int type, TAG_SYMBOL *tag, int *val); extern void addconst(int val, int opr, char zfar); extern int docast(LVALUE *lval,char df); extern void DoDoubConv(char type, char zunsign); diff --git a/src/sccz80/stmt.c b/src/sccz80/stmt.c index a9dc21cbbc..ec948ff7fd 100644 --- a/src/sccz80/stmt.c +++ b/src/sccz80/stmt.c @@ -64,7 +64,7 @@ int statement() } else if (var.type || regit) { if (regit && var.type == NO) var.type = CINT; - declloc(var.type, NULL_TAG, var.sign, locstatic, var.zfar); + declloc(var.type, NULL, var.sign, locstatic, var.zfar); return (lastst); } diff --git a/src/sccz80/sym.c b/src/sccz80/sym.c index 8b76372a92..86f9f1fa10 100644 --- a/src/sccz80/sym.c +++ b/src/sccz80/sym.c @@ -117,7 +117,7 @@ SYMBOL* findmemb(TAG_SYMBOL* tag, char* sname) SYMBOL* addglb( char* sname, enum ident_type id, char typ, - int value, int storage, int more, int itag) + int value, enum storage_type storage, int more, int itag) { SYMBOL* ptr; if ((ptr = findglb(sname))) { @@ -179,7 +179,7 @@ SYMBOL* addmemb( enum ident_type id, char typ, int value, - int storage, + enum storage_type storage, int more, int itag) { @@ -202,7 +202,7 @@ void addsym( char* sname, enum ident_type id, char typ, - int storage, + enum storage_type storage, int more, int itag) { diff --git a/src/sccz80/sym.h b/src/sccz80/sym.h index 88452ab794..29c6bf26d2 100644 --- a/src/sccz80/sym.h +++ b/src/sccz80/sym.h @@ -6,7 +6,7 @@ extern SYMBOL *findenum(char *sname); extern SYMBOL *findloc(char *sname); extern TAG_SYMBOL *findtag(char *sname); extern SYMBOL *findmemb(TAG_SYMBOL *tag, char *sname); -extern SYMBOL *addglb(char *sname, enum ident_type id, char typ, int value, int storage, int more, int itag); +extern SYMBOL *addglb(char *sname, enum ident_type id, char typ, int value, enum storage_type storage, int more, int itag); extern SYMBOL *addloc(char *sname, enum ident_type id, char typ, int more, int itag); -extern SYMBOL *addmemb(char *sname, enum ident_type id, char typ, int value, int storage, int more, int itag); -extern void addsym(SYMBOL *ptr, char *sname, enum ident_type id, char typ, int storage, int more, int itag); +extern SYMBOL *addmemb(char *sname, enum ident_type id, char typ, int value, enum storage_type storage, int more, int itag); +extern void addsym(SYMBOL *ptr, char *sname, enum ident_type id, char typ, enum storage_type storage, int more, int itag); diff --git a/src/sccz80/while.c b/src/sccz80/while.c index 02ea3070c9..da55e71fa5 100644 --- a/src/sccz80/while.c +++ b/src/sccz80/while.c @@ -26,11 +26,8 @@ void delwhile() --wqptr; } -#ifndef SMALL_C -WHILE_TAB* -#endif -readwhile(WHILE_TAB* ptr) +WHILE_TAB *readwhile(WHILE_TAB *ptr) { if (ptr <= wqueue) { error(E_CONTEXT); From 47f90d128935f8327f9c395a5e448fc910fb997e Mon Sep 17 00:00:00 2001 From: suborb Date: Wed, 22 Feb 2017 22:39:32 +0000 Subject: [PATCH 03/16] More cleanup --- src/sccz80/const.c | 2 +- src/sccz80/data.h | 4 ++-- src/sccz80/declfunc.c | 1 - src/sccz80/declvar.c | 11 ++++----- src/sccz80/declvar.h | 4 ++-- src/sccz80/define.h | 55 +++++++++++++++++++++---------------------- src/sccz80/expr.c | 2 +- src/sccz80/plunge.c | 4 ++-- 8 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/sccz80/const.c b/src/sccz80/const.c index b804025d7d..2ee97329cd 100644 --- a/src/sccz80/const.c +++ b/src/sccz80/const.c @@ -64,7 +64,7 @@ int constant(LVALUE* lval) constype = LONG; lval->val_type = constype; - lval->flags = (lval->flags & MKSIGN) | conssign; + lval->flags = (lval->flags & (~UNSIGNED)) | conssign; if (constype == LONG) vlongconst(lval->const_val); else diff --git a/src/sccz80/data.h b/src/sccz80/data.h index 27598d4826..b6e1b0fcf4 100644 --- a/src/sccz80/data.h +++ b/src/sccz80/data.h @@ -28,8 +28,8 @@ extern SYMBOL *membptr; extern char *stage; extern char *stagenext; extern char *stagelast; -extern struct sw_tab *swnext; -extern struct sw_tab *swend; +extern SW_TAB *swnext; +extern SW_TAB *swend; extern char line[]; extern char mline[]; extern int lptr; diff --git a/src/sccz80/declfunc.c b/src/sccz80/declfunc.c index 30685f835e..359639dbe3 100644 --- a/src/sccz80/declfunc.c +++ b/src/sccz80/declfunc.c @@ -420,7 +420,6 @@ void setlocvar(SYMBOL* prevarg, SYMBOL* currfn) where += lgh; /* calculate next offset */ } pushframe(); - currfn->handled = YES; if (currfn->prototyped == 1 && (currfn->flags & REGCALL)) { /* * Fast call routine.. diff --git a/src/sccz80/declvar.c b/src/sccz80/declvar.c index 3218b7ffdb..5cb6077f0a 100644 --- a/src/sccz80/declvar.c +++ b/src/sccz80/declvar.c @@ -417,7 +417,6 @@ void declglb( } /* Make library routines so.. */ if (storage == EXTERNAL && ident == FUNCTION) { - myptr->handled = 0; myptr->flags &= (~LIBRARY); if (libdef) myptr->flags |= LIBRARY; @@ -617,13 +616,13 @@ void declloc( * bits 0-2 = type ; 3-5 = ident, 7-8=flags (signed & zfar) */ -unsigned char CalcArgValue(char type, char ident, char flags) +uint32_t CalcArgValue(char type, char ident, enum symbol_flags flags) { if (type == ELLIPSES) return PELLIPSES; if (type == VOID) - flags &= MKSIGN; /* remove sign from void */ - return (type + (ident * 8) + ((flags & MKDEF) * 64)); + flags &= ~UNSIGNED; /* remove sign from void */ + return (type + (ident * 8) + ((flags & (UNSIGNED|FARPTR)) * 64)); } /* @@ -671,9 +670,7 @@ char* ExpandType(int type, char** dosign, char tagidx) * is.. */ -char* - -ExpandArgValue(unsigned char value, char* buffer, char tagidx) +char *ExpandArgValue(uint32_t value, char* buffer, char tagidx) { char ident, type, isfar, issigned; char *id, *typ, *dofar, *dosign; diff --git a/src/sccz80/declvar.h b/src/sccz80/declvar.h index be77299af9..34b1d6ae80 100644 --- a/src/sccz80/declvar.h +++ b/src/sccz80/declvar.h @@ -5,8 +5,8 @@ extern int get_ident(void); extern int dummy_idx(int typ, TAG_SYMBOL *otag); extern void declglb(int typ, enum storage_type storage, TAG_SYMBOL *mtag, TAG_SYMBOL *otag, int is_struct, char sign, char zfar); extern void declloc(int typ, TAG_SYMBOL *otag, char sign, char locstatic, char zfar); -extern unsigned char CalcArgValue(char type, char ident, char flags); -extern char *ExpandArgValue(unsigned char value, char *buffer, char tagidx); +extern uint32_t CalcArgValue(char type, char ident, enum symbol_flags flags); +extern char *ExpandArgValue(uint32_t, char *buffer, char tagidx); extern void ptrerror(int ident); extern int needsub(void); extern TAG_SYMBOL *GetVarID(struct varid *var,enum storage_type storage); diff --git a/src/sccz80/define.h b/src/sccz80/define.h index 520a3a12d3..84e08d5bdb 100644 --- a/src/sccz80/define.h +++ b/src/sccz80/define.h @@ -9,8 +9,6 @@ #define YES 1 -#define alloc malloc - /* Offset to stack params for shared lib funcs */ #define SHAREOFFSET 4 @@ -74,6 +72,21 @@ enum storage_type { }; +/* Symbol flags, | against each other */ +enum symbol_flags { + UNSIGNED = 1, + FARPTR = 2, + FARACC = 4, + REGCALL = 8, /* for certain lib calls only */ + SHARED = 16, /* Call via shared library method (append _s) */ + SHAREDC = 32, /* Call via rst (library is C code) */ + CALLEE = 64, /* Called function pops regs */ + LIBRARY = 128, /* Lib routine */ + SAVEFRAME = 256, /* Save framepointer */ + SMALLC = 512 /* L->R calling order */ +}; + + /* Define symbol table entry format */ @@ -96,11 +109,10 @@ struct symbol_s { int more ; /* index of linked entry in dummy_sym */ char tag_idx ; /* index of struct tag in tag table */ int size ; /* djm, storage reqd! */ - char handled; /* djm, whether we've written the type or not */ char prototyped; uint32_t args[MAXARGS]; /* arguments */ unsigned char tagarg[MAXARGS]; /* ptrs to tagsymbol entries*/ - int flags ; /* djm, various flags: + enum symbol_flags flags ; /* djm, various flags: bit 0 = unsigned bit 1 = far data/pointer bit 2 = access via far methods @@ -153,26 +165,9 @@ struct symbol_s { -/* Flags */ -#define UNSIGNED 1 -#define FARPTR 2 -#define FARACC 4 -#define REGCALL 8 /* for certain lib calls only */ -#define SHARED 16 /* Call via shared library method (append _s) */ -#define SHAREDC 32 /* Call via rst (library is C code) */ -#define CALLEE 64 /* Called function pops regs */ -#define LIBRARY 128 /* Lib routine */ -#define SAVEFRAME 256 /* Save framepointer */ -#define SMALLC 512 /* L->R calling order */ -/* - * MKDEF is for masking unsigned and far - */ -#define MKDEF 3 -#define MKSIGN 254 -#define MKFARP 253 -#define MKFARA 251 + /* Define the structure tag table parameters */ @@ -200,29 +195,33 @@ struct tagsymbol_s { #define NUMCASE 256 -struct sw_tab { +typedef struct switchtab_s SW_TAB; + +struct switchtab_s { int label ; /* label for start of case */ int32_t value ; /* value associated with case */ } ; -#define SW_TAB struct sw_tab /* Define the "while" statement queue */ #define NUMWHILE 20 #define WQMAX wqueue+(NUMWHILE-1) -#define WHILE_TAB struct while_tab -struct while_tab { + +typedef struct whiletab_s WHILE_TAB; + +struct whiletab_s { int sp ; /* stack pointer */ int loop ; /* label for top of loop */ int exit ; /* label at end of loop */ } ; #define NUMGOTO 100 -#define GOTO_TAB struct goto_tab -GOTO_TAB { +typedef struct gototab_s GOTO_TAB; + +struct gototab_s { int sp; /* Stack pointer to correct to */ SYMBOL *sym; /* Pointer to goto label */ int lineno; /* line where goto was */ diff --git a/src/sccz80/expr.c b/src/sccz80/expr.c index 10ddd872fb..f315de7cee 100644 --- a/src/sccz80/expr.c +++ b/src/sccz80/expr.c @@ -467,7 +467,7 @@ SYMBOL *deref(LVALUE* lval, char isaddr) if (flags & FARPTR) flags |= FARACC; else - flags &= MKFARA; + flags &= ~FARACC; } /* NB it has already been determind that lval->symbol is non-zero */ if (lval->symbol->more == 0) { diff --git a/src/sccz80/plunge.c b/src/sccz80/plunge.c index 7a07fc5df8..2a806aec56 100644 --- a/src/sccz80/plunge.c +++ b/src/sccz80/plunge.c @@ -211,9 +211,9 @@ void plnge2a(int (*heir)(), LVALUE* lval, LVALUE* lval2, void (*oper)(), void (* * problems if we allow specifiers after numbers */ if (lval->is_const) - lval->flags = (lval->flags & MKSIGN) | (lval2->flags & UNSIGNED); + lval->flags = (lval->flags & ~UNSIGNED) | (lval2->flags & UNSIGNED); if (lval2->is_const) - lval2->flags = (lval2->flags & MKSIGN) | (lval->flags & UNSIGNED); + lval2->flags = (lval2->flags & ~UNSIGNED) | (lval->flags & UNSIGNED); if ((lval->flags & UNSIGNED) != (lval2->flags & UNSIGNED) && (oper == zmod || oper == mult || oper == zdiv)) warning(W_OPSG); From 855ebde1f7feec36a9b76a284d2adb49e7960c84 Mon Sep 17 00:00:00 2001 From: suborb Date: Wed, 22 Feb 2017 22:55:10 +0000 Subject: [PATCH 04/16] More cleanup - this may take a while... --- src/sccz80/declinit.c | 38 +++---------- src/sccz80/declvar.c | 2 +- src/sccz80/preproc.c | 121 +----------------------------------------- src/sccz80/stmt.c | 4 +- 4 files changed, 11 insertions(+), 154 deletions(-) diff --git a/src/sccz80/declinit.c b/src/sccz80/declinit.c index 5fc41ba25e..453bb73205 100644 --- a/src/sccz80/declinit.c +++ b/src/sccz80/declinit.c @@ -222,31 +222,10 @@ void init(int size, int ident, int* dim, int more, int dump, int is_struct) if ((sz = qstr(&value)) != -1) { sz++; -#if 0 - if (ident == VARIABLE || (size != 1 && more != CCHAR)) - error(E_ASSIGN); -#endif -#ifdef INIT_TEST - outstr("ident="); - outdec(ident); - outstr("size="); - outdec(size); - outstr("more="); - outdec(more); - outstr("dim="); - outdec(*dim); - outstr("sz="); - outdec(sz); - nl(); -#endif if (ident == ARRAY && more == 0) { - /* - * Dump the literals where they are, padding out as appropriate - */ + /* Dump the literals where they are, padding out as appropriate */ if (*dim != -1 && sz > *dim) { - /* - * Ooops, initialised to long a string! - */ + /* Ooops, initialised to long a string! */ warning(W_INIT2LONG); sz = *dim; gltptr = sz; @@ -258,9 +237,7 @@ void init(int size, int ident, int* dim, int more, int dump, int is_struct) dumpzero(size, *dim); return; } else { - /* - * Store the literals in the queue! - */ + /* Store the literals in the queue! */ storeq(sz, glbq, &value); gltptr = 0; defword(); @@ -271,13 +248,10 @@ void init(int size, int ident, int* dim, int more, int dump, int is_struct) --*dim; return; } - } - /* - * djm, catch label names in structures (for (*name)() initialisation - */ - else { + } else { + /* djm, catch label names in structures (for (*name)() initialisation */ char sname[NAMEMAX + 1]; - SYMBOL* ptr; + SYMBOL *ptr; if (symname(sname) && strcmp(sname, "sizeof")) { /* We have got something.. */ if ((ptr = findglb(sname))) { /* Actually found sommat..very good! */ diff --git a/src/sccz80/declvar.c b/src/sccz80/declvar.c index 5cb6077f0a..9f489abbb8 100644 --- a/src/sccz80/declvar.c +++ b/src/sccz80/declvar.c @@ -754,7 +754,7 @@ int needsub(void) * djm 18/3/99 */ -TAG_SYMBOL* GetVarID(struct varid* var, enum storage_type storage) +TAG_SYMBOL* GetVarID(struct varid *var, enum storage_type storage) { TAG_SYMBOL* otag = NULL; char sname[NAMEMAX]; diff --git a/src/sccz80/preproc.c b/src/sccz80/preproc.c index 68d430c3b0..41f7cf593f 100644 --- a/src/sccz80/preproc.c +++ b/src/sccz80/preproc.c @@ -208,125 +208,11 @@ void keepch(char c) ++mptr; } -/* The preprocessor here is pants, and messes up all sorts of - things - best leave it to the external preprocessor to do all - the dirty work -*/ +/* Preprocessing is minimal - we need an external preprocessor */ void preprocess() { -#if 0 - char c,sname[NAMESIZE]; - int k; -#endif - ifline(); return; -#if 0 - if ( eof || cmode == 0 ) { - /* while passing through assembler, only do #if, etc */ - return ; - } - mptr = lptr = 0 ; - while ( ch() ) { - if ( ch()==' ' || ch()=='\t' ) { - keepch(' '); - while ( ch()==' ' || ch()=='\t' ) - gch(); - } - else if(ch()=='"') { - keepch(ch()); - gch(); - do { - while ( ch()!='"' || (line[lptr-1]==92 && line[lptr-2]!=92 ) ) { - if(ch()==0) { - warning(W_EXPQT); - break; - } - keepch(gch()); - } - } while (gch() && cmatch('"') ); - keepch('"'); - } - else if(ch()==39) { - keepch(39); - gch(); - while ( ch()!=39 || (line[lptr-1]==92 && line[lptr-2]!=92) ) { - if(ch()==0) { - warning(W_EXPAPO); - break; - } - keepch(gch()); - } - gch(); - keepch(39); - } -/* - else if (amatch("typedef")) - { - warning(W_TYPEDEF); - junk(); - vinline(); - if (eof) break; - } - */ - else if (ch()=='/' && nch()=='/' && (cppcom)) - { - junk(); - vinline(); - if (eof) break; - } - else if ( ch()=='/' && nch()=='*' ) { - lptr += 2; - while ( ch()!='*' || nch()!='/' ) { - if ( ch() ) { - ++lptr; - } - else { - vinline() ; - if(eof)break; - } - } - lptr += 2; - } -/* - * Some preprocessor directives, if they get this far then we are running - * the compiler directly, so we need the quotes around filename - */ - else if ( amatch("__LINE__")){ - sprintf(sname,"%d",lineno); - for (k=0 ; k= MPMAX ) error(E_TOOLONG) ; - strcpy(line, mline) ; - lptr = 0 ; -#endif } void addmac() @@ -347,10 +233,7 @@ void addmac() error(E_MACOV); } -/* - * delete macro from symbol table, but leave entry so hashing still works - */ - +/* delete macro from symbol table, but leave entry so hashing still works */ void delmac() { char sname[NAMESIZE]; diff --git a/src/sccz80/stmt.c b/src/sccz80/stmt.c index ec948ff7fd..ae250a1b88 100644 --- a/src/sccz80/stmt.c +++ b/src/sccz80/stmt.c @@ -16,8 +16,8 @@ extern void dogoto(void); extern int dolabel(void); -int stkstor[MAX_LEVELS]; /* ZSp for each compound level */ -int lastline = 0; +static int stkstor[MAX_LEVELS]; /* ZSp for each compound level */ +static int lastline = 0; /* * Statement parser From 1174b8c65567e21879bbf46e6d187a3a97292a83 Mon Sep 17 00:00:00 2001 From: suborb Date: Thu, 23 Feb 2017 20:23:11 +0000 Subject: [PATCH 05/16] Checkpoint some more cleanup --- src/sccz80/callfunc.c | 4 +-- src/sccz80/const.c | 2 +- src/sccz80/declfunc.c | 1 - src/sccz80/define.h | 31 ++++++++++++++--------- src/sccz80/io.c | 12 ++++----- src/sccz80/main.c | 58 ++++++++++++++----------------------------- src/sccz80/main.h | 1 - src/sccz80/preproc.c | 4 +-- 8 files changed, 48 insertions(+), 65 deletions(-) diff --git a/src/sccz80/callfunc.c b/src/sccz80/callfunc.c index 44a1987614..72ebb95cdb 100644 --- a/src/sccz80/callfunc.c +++ b/src/sccz80/callfunc.c @@ -90,7 +90,7 @@ void callfunction(SYMBOL* ptr) /* Don't rewrite expressions whilst we are evaluating */ save_fps_num = buffer_fps_num; - save_fps = mymalloc(buffer_fps_num * sizeof(buffer_fps[0])); + save_fps = MALLOC(buffer_fps_num * sizeof(buffer_fps[0])); memcpy(save_fps, buffer_fps, save_fps_num * sizeof(buffer_fps[0])); buffer_fps_num = 0; while ( tmpfiles[argnumber+1] ) { @@ -163,7 +163,7 @@ void callfunction(SYMBOL* ptr) } memcpy(buffer_fps, save_fps, save_fps_num * sizeof(buffer_fps[0])); buffer_fps_num = save_fps_num ; - free(save_fps); + FREENULL(save_fps); if (ptr) debug(DBG_ARG2, "arg %d proto %d", argnumber, ptr->args[1]); diff --git a/src/sccz80/const.c b/src/sccz80/const.c index 2ee97329cd..68879dc76c 100644 --- a/src/sccz80/const.c +++ b/src/sccz80/const.c @@ -165,7 +165,7 @@ int stash_double_str(char* start, char* end) strncpy(buf, start, len); *(buf + len) = 0; storeq(len + 1, (unsigned char*)buf, &val); - free(buf); + FREENULL(buf); return (val); } diff --git a/src/sccz80/declfunc.c b/src/sccz80/declfunc.c index 359639dbe3..c9b413a868 100644 --- a/src/sccz80/declfunc.c +++ b/src/sccz80/declfunc.c @@ -527,7 +527,6 @@ SYMBOL *dofnansi(SYMBOL* currfn, int32_t* addr) } if (amatch("__smallc")) { currfn->flags |= SMALLC; - /* Just swallow */ continue; } if (amatch("__preserves_regs")) { diff --git a/src/sccz80/define.h b/src/sccz80/define.h index 84e08d5bdb..2e18796604 100644 --- a/src/sccz80/define.h +++ b/src/sccz80/define.h @@ -3,6 +3,15 @@ * $Id: define.h,v 1.18 2016-09-19 09:17:50 dom Exp $ */ + + #ifndef DEFINE_H + #define DEFINE_H + + +#define MALLOC(x) mymalloc(x) +#define CALLOC(x,y) mymalloc(x * y) +#define FREENULL(x) do { if (x != NULL ) { free(x); x = NULL; } } while (0) + /* Stand-alone definitions */ #define NO 0 @@ -23,7 +32,7 @@ #define NAMEMAX 126 #endif -#define MAXARGS 10 +#define MAXARGS 20 /* Define the symbol table parameters */ @@ -123,6 +132,7 @@ struct symbol_s { /* Define possible entries for "type" */ + #define DOUBLE 1 #define CINT 2 #define CCHAR 3 @@ -159,14 +169,6 @@ struct symbol_s { #define NTYPE 15 -/* Define possible entries for "storage" */ - - - - - - - /* Define the structure tag table parameters */ @@ -205,10 +207,8 @@ struct switchtab_s { /* Define the "while" statement queue */ -#define NUMWHILE 20 +#define NUMWHILE 100 #define WQMAX wqueue+(NUMWHILE-1) - - typedef struct whiletab_s WHILE_TAB; struct whiletab_s { @@ -345,3 +345,10 @@ struct parser_stack { int slineno; struct parser_stack *next; }; + + + +#define WEM_FATAL 1 +#define WEM_SUPPRESS 2 + +#endif \ No newline at end of file diff --git a/src/sccz80/io.c b/src/sccz80/io.c index 82f97db821..41c6ee6eba 100644 --- a/src/sccz80/io.c +++ b/src/sccz80/io.c @@ -122,10 +122,10 @@ t_buffer* currentbuffer = NULL; t_buffer* startbuffer(int blocks) { - t_buffer* buf = (t_buffer*)mymalloc(sizeof(t_buffer)); + t_buffer* buf = (t_buffer*)MALLOC(sizeof(t_buffer)); int size = blocks * STAGESIZE; buf->size = size; - buf->start = (char*)mymalloc(size); + buf->start = (char*)MALLOC(size); buf->end = buf->start + blocks * size - 1; buf->next = buf->start; buf->before = currentbuffer; /* <-- DON'T USE NULL HERE TO SUPPRESS WARNING !! */ @@ -147,18 +147,18 @@ void clearbuffer(t_buffer* buf) currentbuffer = (t_buffer*)currentbuffer->before; *buf->next = '\0'; outstr(buf->start); - free(buf->start); + FREENULL(buf->start); buf->start = buf->next = 0; - free(buf); + FREENULL(buf); } int outbuffer(char c) { if (currentbuffer->next == currentbuffer->end) { size_t size = currentbuffer->size * 2; - char* tmp = (char*)mymalloc(size); + char* tmp = (char*)MALLOC(size); memcpy(tmp, currentbuffer->start, currentbuffer->size); - free(currentbuffer->start); + FREENULL(currentbuffer->start); currentbuffer->next = tmp + (currentbuffer->start - currentbuffer->next); currentbuffer->start = tmp; currentbuffer->end = tmp + size - 1; diff --git a/src/sccz80/main.c b/src/sccz80/main.c index bfb3462645..1e91acabdc 100644 --- a/src/sccz80/main.c +++ b/src/sccz80/main.c @@ -64,21 +64,21 @@ int main(int argc, char** argv) gargv = argv; /* allocate space for arrays */ - litq = mymalloc(FNLITQ); /* literals, these 2 dumped end */ - dubq = mymalloc(FNLITQ); /* Doubles */ - tempq = mymalloc(LITABSZ); /* Temp strings... */ - glbq = mymalloc(LITABSZ); /* Used for glb lits, dumped now */ - symtab = mymalloc(NUMGLBS * sizeof(SYMBOL)); - loctab = mymalloc(NUMLOC * sizeof(SYMBOL)); - wqueue = mymalloc(NUMWHILE * sizeof(WHILE_TAB)); - gotoq = mymalloc(NUMGOTO * sizeof(GOTO_TAB)); - tagptr = tagtab = mymalloc(NUMTAG * sizeof(TAG_SYMBOL)); - membptr = membtab = mymalloc(NUMMEMB * sizeof(SYMBOL)); - - swnext = mymalloc(NUMCASE * sizeof(SW_TAB)); + litq = MALLOC(FNLITQ); /* literals, these 2 dumped end */ + dubq = MALLOC(FNLITQ); /* Doubles */ + tempq = MALLOC(LITABSZ); /* Temp strings... */ + glbq = MALLOC(LITABSZ); /* Used for glb lits, dumped now */ + symtab = MALLOC(NUMGLBS * sizeof(SYMBOL)); + loctab = MALLOC(NUMLOC * sizeof(SYMBOL)); + wqueue = MALLOC(NUMWHILE * sizeof(WHILE_TAB)); + gotoq = MALLOC(NUMGOTO * sizeof(GOTO_TAB)); + tagptr = tagtab = MALLOC(NUMTAG * sizeof(TAG_SYMBOL)); + membptr = membtab = MALLOC(NUMMEMB * sizeof(SYMBOL)); + + swnext = MALLOC(NUMCASE * sizeof(SW_TAB)); swend = swnext + (NUMCASE - 1); - stage = mymalloc(STAGESIZE); + stage = MALLOC(STAGESIZE); stagelast = stage + STAGELIMIT; /* empty symbol table */ @@ -1076,73 +1076,51 @@ void MemCleanup() { if (litq) { free(litq); - litq = 0; } if (dubq) { free(dubq); - dubq = 0; } if (tempq) { free(tempq); - tempq = 0; } if (glbq) { free(glbq); - glbq = 0; } if (symtab) { free(symtab); - symtab = 0; } if (loctab) { free(loctab); - loctab = 0; } if (wqueue) { free(wqueue); - wqueue = 0; } if (tagtab) { free(tagtab); - tagtab = 0; } if (membtab) { free(membtab); - membtab = 0; } if (swnext) { free(swnext); - swnext = 0; } if (stage) { free(stage); - stage = 0; } if (gotoq) { free(gotoq); - gotoq = 0; } } -/* - * Routine to keep DOG happy and avoid nastiness - * should really do this any case..so I'll let it - * pass! - */ - void* mymalloc(size_t size) { void* ptr; if ((ptr = calloc(size, 1)) != NULL) return ptr; - else - OutOfMem(); + else { + fprintf(stderr, "Out of memory...\n"); + exit(1); + } return 0; /* Sigh */ -} - -void OutOfMem() -{ - fprintf(stderr, "Out of memory...\n"); - exit(1); -} +} \ No newline at end of file diff --git a/src/sccz80/main.h b/src/sccz80/main.h index a72aeeadcd..8e1710b000 100644 --- a/src/sccz80/main.h +++ b/src/sccz80/main.h @@ -54,5 +54,4 @@ extern void SetFrameIX(char *); extern void SetFrameIY(char *); extern void SetStandardEscape(char *); extern void SetNoFrame(char *); -extern void OutOfMem(void); extern void SetPfLevel(char *); diff --git a/src/sccz80/preproc.c b/src/sccz80/preproc.c index 41f7cf593f..85db6bf065 100644 --- a/src/sccz80/preproc.c +++ b/src/sccz80/preproc.c @@ -290,7 +290,7 @@ void defmac(char* text) void set_temporary_input(FILE *temp) { - struct parser_stack *stack = mymalloc(sizeof(*stack)); + struct parser_stack *stack = MALLOC(sizeof(*stack)); /* Save the current positions */ memcpy(stack->sline, line, LINESIZE); stack->slineno = lineno; @@ -311,7 +311,7 @@ void restore_input(void) lineno = stack->slineno; lptr = stack->slptr; input = stack->sinput; - free(stack); + FREENULL(stack); } } From 2cec86b07418fe1aa0b1598c3ed909a5138e94bd Mon Sep 17 00:00:00 2001 From: suborb Date: Thu, 23 Feb 2017 20:35:24 +0000 Subject: [PATCH 06/16] Remove the jp over static - not needed with sections in sccz80 --- src/sccz80/data.c | 6 ------ src/sccz80/data.h | 2 -- src/sccz80/declfunc.c | 4 ---- src/sccz80/declvar.c | 9 --------- src/sccz80/stmt.c | 3 --- 5 files changed, 24 deletions(-) diff --git a/src/sccz80/data.c b/src/sccz80/data.c index f163a1f488..c672883b3a 100644 --- a/src/sccz80/data.c +++ b/src/sccz80/data.c @@ -124,12 +124,6 @@ int ltype; /* Long? */ int opertype; /* Saves a lot of code! Reference in cc6 to grab long type operations! */ -/* - * A couple of variables for dealing with local statics - */ - -int lstdecl, lstlab; - /* * Variable for the offset to a shared library routine */ diff --git a/src/sccz80/data.h b/src/sccz80/data.h index b6e1b0fcf4..8be502de0f 100644 --- a/src/sccz80/data.h +++ b/src/sccz80/data.h @@ -88,8 +88,6 @@ extern char fnargvalue; extern int ltype; extern int opertype; extern char margtag; -extern int lstdecl; -extern int lstlab; extern int shareoffset; extern int debuglevel; extern int assemtype; diff --git a/src/sccz80/declfunc.c b/src/sccz80/declfunc.c index c9b413a868..34e2db4265 100644 --- a/src/sccz80/declfunc.c +++ b/src/sccz80/declfunc.c @@ -436,11 +436,7 @@ void setlocvar(SYMBOL* prevarg, SYMBOL* currfn) } stackargs = where; - lstdecl = 0; /* Set number of local statics to zero */ if (statement() != STRETURN) { - if (lstdecl) - postlabel(lstlab); - lstdecl = 0; /* do a statement, but if it's a return, skip */ /* cleaning up the stack */ leave(NO, NO); diff --git a/src/sccz80/declvar.c b/src/sccz80/declvar.c index 9f489abbb8..174f74c356 100644 --- a/src/sccz80/declvar.c +++ b/src/sccz80/declvar.c @@ -564,12 +564,6 @@ void declloc( cptr->size = size; } if (rcmatch('=')) { - /* - * Insert the jump in... (remove this z80asm hack) - */ - if (lstdecl++ == 0 && ISASM(ASM_Z80ASM)) { - jump(lstlab = getlabel()); - } initials(sname2, type, ident, dsize, more, otag, zfar); ns(); cptr->storage = LSTKEXT; @@ -592,9 +586,6 @@ void declloc( char *before, *start; if ((typ == STRUCT && ident != POINTER) || ident == ARRAY) error(E_AUTOASSIGN, sname); - if (lstdecl) - postlabel(lstlab); - lstdecl = 0; Zsp = modstk(Zsp - (declared - size), NO, NO); declared = 0; setstage(&before, &start); diff --git a/src/sccz80/stmt.c b/src/sccz80/stmt.c index ae250a1b88..753abfea67 100644 --- a/src/sccz80/stmt.c +++ b/src/sccz80/stmt.c @@ -70,9 +70,6 @@ int statement() /* not a definition */ if (declared >= 0) { - if (lstdecl) - postlabel(lstlab); - lstdecl = 0; Zsp = modstk(Zsp - declared, NO, NO); declared = -1; } From b1edbee9a0872260d709673ca5b10f4b67bcd50a Mon Sep 17 00:00:00 2001 From: suborb Date: Thu, 23 Feb 2017 21:03:22 +0000 Subject: [PATCH 07/16] Half implement const. - Puts data in the rodata section for statics - Warns if you try to modify a const TODO: - Assigning away const --- src/sccz80/declinit.c | 8 ++++-- src/sccz80/declinit.h | 2 +- src/sccz80/declvar.c | 67 ++++++++++++++++++++++--------------------- src/sccz80/declvar.h | 4 +-- src/sccz80/define.h | 28 +++++++++--------- src/sccz80/error.c | 2 ++ src/sccz80/error.h | 2 ++ src/sccz80/primary.c | 13 +++++++-- src/sccz80/stmt.c | 4 +-- 9 files changed, 74 insertions(+), 56 deletions(-) diff --git a/src/sccz80/declinit.c b/src/sccz80/declinit.c index 453bb73205..9af925481f 100644 --- a/src/sccz80/declinit.c +++ b/src/sccz80/declinit.c @@ -20,7 +20,7 @@ */ int initials(char* sname, int type, int ident, int dim, int more, - TAG_SYMBOL* tag, char zfar) + TAG_SYMBOL* tag, char zfar, char isconst) { int size, desize = 0; int olddim = dim; @@ -44,7 +44,11 @@ int initials(char* sname, size = 2; } - output_section("data_compiler"); // output_section("text"); + if ( isconst ) { + output_section("rodata_compiler"); + } else { + output_section("data_compiler"); // output_section("text"); + } prefix(); outname(sname, YES); col(); diff --git a/src/sccz80/declinit.h b/src/sccz80/declinit.h index 46126b359d..f044c80247 100644 --- a/src/sccz80/declinit.h +++ b/src/sccz80/declinit.h @@ -1,4 +1,4 @@ -extern int initials(char *sname, int type, int ident, int dim, int more, TAG_SYMBOL *tag, char zfar); +extern int initials(char *sname, int type, int ident, int dim, int more, TAG_SYMBOL *tag, char zfar, char isconst); extern int str_init(TAG_SYMBOL *tag); extern void agg_init(int size, int type, int ident, int *dim, int more, TAG_SYMBOL *tag); extern void init(int size, int ident, int *dim, int more, int dump, int is_struct); diff --git a/src/sccz80/declvar.c b/src/sccz80/declvar.c index 174f74c356..4d329e0a6f 100644 --- a/src/sccz80/declvar.c +++ b/src/sccz80/declvar.c @@ -51,10 +51,10 @@ int dodeclare( return (0); /* fail */ } if (var.type == STRUCT) { - declglb(STRUCT, storage, mtag, otag, is_struct, var.sign, var.zfar); + declglb(STRUCT, storage, mtag, otag, is_struct, &var); return (1); } else { - declglb(var.type, storage, mtag, NULL, is_struct, var.sign, var.zfar); + declglb(var.type, storage, mtag, NULL, is_struct, &var); return (1); } } @@ -185,8 +185,7 @@ void declglb( TAG_SYMBOL* mtag, /* tag of struct whose members are being declared, or zero */ TAG_SYMBOL* otag, /* tag of struct for object being declared */ int is_struct, /* TRUE if struct member being declared, zero if union */ - char sign, /* TRUE if need signed */ - char zfar) /* TRUE if far */ + struct varid *var) { char sname[NAMESIZE]; int size, ident, more, itag, type, size_st; @@ -250,7 +249,7 @@ void declglb( if (ident == PTR_TO_FNP) { /* function returning pointer needs dummy symbol */ more = dummy_idx(typ, otag); - type = (zfar ? CPTR : CINT); + type = (var->zfar ? CPTR : CINT); size = 0; ptrtofn = YES; } else if (ident == PTR_TO_FN) { @@ -269,7 +268,7 @@ void declglb( * since they can't contain functions, only pointers to functions * this, understandably(!) makes the work here a lot, lot easier! */ - storage = AddNewFunc(sname, type, storage, zfar, sign, otag, ident, &addr); + storage = AddNewFunc(sname, type, storage, var->zfar, var->sign, otag, ident, &addr); /* * On return from AddNewFunc, storage will be: * EXTERNP = external pointer, in which case addr will be set @@ -330,7 +329,7 @@ void declglb( if (ident == POINTER) { /* function returning pointer needs dummy symbol */ more = dummy_idx(typ, otag); - type = (zfar ? CPTR : CINT); + type = (var->zfar ? CPTR : CINT); ident = FUNCTIONP; } else { ident = FUNCTION; @@ -343,7 +342,7 @@ void declglb( if (ident == POINTER) { /* array of pointers needs dummy symbol */ more = dummy_idx(typ, otag); - type = (zfar ? CPTR : CINT); + type = (var->zfar ? CPTR : CINT); } size = needsub(); /* get size */ if (size == 0 && ident == POINTER) @@ -356,7 +355,7 @@ void declglb( } else if (ident == PTR_TO_PTR) { ident = POINTER; more = dummy_idx(typ, otag); - type = (zfar ? CPTR : CINT); + type = (var->zfar ? CPTR : CINT); } if (cmatch('@')) { @@ -365,9 +364,9 @@ void declglb( } /* Check to see if far has been defined when we haven't got a pointer */ - if (zfar && !(ident == POINTER || (ident == ARRAY && more) || (ident == FUNCTIONP && more))) { + if (var->zfar && !(ident == POINTER || (ident == ARRAY && more) || (ident == FUNCTIONP && more))) { warning(W_FAR); - zfar = NO; + var->zfar = NO; } if (otag) { @@ -384,7 +383,8 @@ void declglb( myptr = addglb(sname, ident, type, 0, storage, more, itag); /* What happens if we have an array which will be initialised? */ - myptr->flags = (sign | zfar | fastcall); + myptr->flags = (var->sign | var->zfar | fastcall); + myptr->isconst = var->isconst; /* initialise variable (allocate storage space) */ /* defstatic to prevent repetition of def for declared statics */ @@ -398,7 +398,7 @@ void declglb( myptr->tagarg[0] = itag; } if (storage != EXTERNAL && ident != FUNCTION) { - size_st = initials(sname, type, ident, size, more, otag, zfar); + size_st = initials(sname, type, ident, size, more, otag, var->zfar, var->isconst); if (storage == EXTERNP) myptr->size = addr; @@ -433,14 +433,14 @@ void declglb( /* are adding structure member, mtag->size is offset */ myptr = addmemb(sname, ident, type, mtag->size, storage, more, itag); myptr--; /* addmemb returns myptr+1 */ - myptr->flags = ((sign & UNSIGNED) | (zfar & FARPTR)); + myptr->flags = ((var->sign & UNSIGNED) | (var->zfar & FARPTR)); myptr->size = size; /* store (correctly scaled) size of member in tag table entry */ /* 15/2/99 djm - screws up sizing of arrays - quite obviously!! - removing */ if (ident == POINTER) { /* || ident== ARRAY ) { */ - type = (zfar ? CPTR : CINT); + type = (var->zfar ? CPTR : CINT); } cscale(type, otag, &size); @@ -450,13 +450,13 @@ void declglb( /* are adding union member, offset is always zero */ myptr = addmemb(sname, ident, type, 0, storage, more, itag); myptr--; - myptr->flags = ((sign & UNSIGNED) | (zfar & FARPTR)); + myptr->flags = ((var->sign & UNSIGNED) | (var->zfar & FARPTR)); myptr->size = size; /* store maximum member size in tag table entry */ /* 2/11/2002 djm - fix from above */ if (ident == POINTER /* || ident==ARRAY */) { - type = (zfar ? CPTR : CINT); + type = (var->zfar ? CPTR : CINT); } cscale(type, otag, &size); if (mtag->size < size) @@ -476,9 +476,8 @@ void declglb( void declloc( int typ, /* typ is CCHAR, CINT DOUBLE or STRUCT, LONG */ TAG_SYMBOL* otag, /* tag of struct for object being declared */ - char sign, /* Are we signed or not? */ - char locstatic, /* Is this as static local variable? */ - char zfar) /* Far pointer thing.. */ + char locstatic, + struct varid *var) { char sname[NAMESIZE]; char sname2[3 * NAMESIZE]; /* More than enuff overhead! */ @@ -505,8 +504,8 @@ void declloc( needtoken(")()"); /* function returning pointer needs dummy symbol */ more = dummy_idx(typ, otag); - type = (zfar ? CPTR : CINT); - dsize = size = (zfar ? 3 : 2); + type = (var->zfar ? CPTR : CINT); + dsize = size = (var->zfar ? 3 : 2); } if (ident == PTR_TO_FN) { @@ -519,7 +518,7 @@ void declloc( if (ident == POINTER) { /* array of pointers needs dummy symbol */ more = dummy_idx(typ, otag); - type = (zfar ? CPTR : CINT); + type = (var->zfar ? CPTR : CINT); } dsize = size = needsub(); ident = ARRAY; /* null subscript array is NOT a pointer */ @@ -527,15 +526,15 @@ void declloc( } else if (ident == PTR_TO_PTR) { ident = POINTER; more = dummy_idx(typ, otag); - type = (zfar ? CPTR : CINT); - dsize = size = (zfar ? 3 : 2); + type = (var->zfar ? CPTR : CINT); + dsize = size = (var->zfar ? 3 : 2); } else { size = get_type_size(type, otag); } /* Check to see if far has been defined when we haven't got a pointer */ - if (zfar && !(ident == POINTER || (ident == ARRAY && more))) { + if (var->zfar && !(ident == POINTER || (ident == ARRAY && more))) { warning(W_FAR); - zfar = NO; + var->zfar = NO; } if (typ == VOID && ident != FUNCTION && ident != POINTER) { @@ -543,8 +542,8 @@ void declloc( typ = type = CINT; } if (ident == POINTER) { - decltype = (zfar ? CPTR : CINT); - size = (zfar ? 3 : 2); + decltype = (var->zfar ? CPTR : CINT); + size = (var->zfar ? 3 : 2); } /* declared += size ; Moved down djm */ @@ -560,11 +559,12 @@ void declloc( strcat(sname2, sname); cptr = addglb(sname2, ident, type, 0, LSTATIC, more, itag); if (cptr) { - cptr->flags = ((sign & UNSIGNED) | (zfar & FARPTR)); + cptr->flags = ((var->sign & UNSIGNED) | (var->zfar & FARPTR)); cptr->size = size; + cptr->isconst = var->isconst; } if (rcmatch('=')) { - initials(sname2, type, ident, dsize, more, otag, zfar); + initials(sname2, type, ident, dsize, more, otag, var->zfar, var->isconst); ns(); cptr->storage = LSTKEXT; return; @@ -580,7 +580,8 @@ void declloc( if (cptr) { cptr->size = size; cptr->offset.i = Zsp - declared; - cptr->flags = ((sign & UNSIGNED) | (zfar & FARPTR)); + cptr->flags = ((var->sign & UNSIGNED) | (var->zfar & FARPTR)); + cptr->isconst = var->isconst; if (cmatch('=')) { int vconst, val, expr; char *before, *start; @@ -755,8 +756,10 @@ TAG_SYMBOL* GetVarID(struct varid *var, enum storage_type storage) var->zfar = NO; var->type = NO; var->sflag = NO; + var->isconst = NO; if (swallow("const")) { + var->isconst = YES; // warning(W_CONST); } else if (swallow("volatile")) warning(W_VOLATILE); diff --git a/src/sccz80/declvar.h b/src/sccz80/declvar.h index 34b1d6ae80..5ab9d8575a 100644 --- a/src/sccz80/declvar.h +++ b/src/sccz80/declvar.h @@ -3,8 +3,8 @@ extern int dodeclare(enum storage_type storage, TAG_SYMBOL *mtag, int is_struct) extern TAG_SYMBOL *defstruct(char *sname, enum storage_type storage, int is_struct); extern int get_ident(void); extern int dummy_idx(int typ, TAG_SYMBOL *otag); -extern void declglb(int typ, enum storage_type storage, TAG_SYMBOL *mtag, TAG_SYMBOL *otag, int is_struct, char sign, char zfar); -extern void declloc(int typ, TAG_SYMBOL *otag, char sign, char locstatic, char zfar); +extern void declglb(int typ, enum storage_type storage, TAG_SYMBOL *mtag, TAG_SYMBOL *otag, int is_struct, struct varid *var); +extern void declloc(int typ, TAG_SYMBOL *otag, char locstatic, struct varid *var); extern uint32_t CalcArgValue(char type, char ident, enum symbol_flags flags); extern char *ExpandArgValue(uint32_t, char *buffer, char tagidx); extern void ptrerror(int ident); diff --git a/src/sccz80/define.h b/src/sccz80/define.h index 2e18796604..032bc28ab9 100644 --- a/src/sccz80/define.h +++ b/src/sccz80/define.h @@ -67,17 +67,17 @@ enum ident_type { enum storage_type { UNKNOWN = 0, - STATIK, - STKLOC, - EXTERNAL, - EXTERNP, - DECLEXTN, - LSTATIC, - FAR, - LSTKEXT, - TYPDEF, - PORT8, - PORT16 + STATIK = 1, + STKLOC = 2, + EXTERNAL = 3, + EXTERNP = 4, + DECLEXTN = 5, + LSTATIC = 6, + FAR = 7 , + LSTKEXT = 8, + TYPDEF = 9, + PORT8 = 10, + PORT16 = 11, }; @@ -119,6 +119,7 @@ struct symbol_s { char tag_idx ; /* index of struct tag in tag table */ int size ; /* djm, storage reqd! */ char prototyped; + char isconst; /* Set if const, affects the section the data goes into */ uint32_t args[MAXARGS]; /* arguments */ unsigned char tagarg[MAXARGS]; /* ptrs to tagsymbol entries*/ enum symbol_flags flags ; /* djm, various flags: @@ -300,6 +301,7 @@ struct varid { unsigned char zfar; unsigned char sign; unsigned char sflag; + unsigned char isconst; enum ident_type ident; int more; }; @@ -347,8 +349,4 @@ struct parser_stack { }; - -#define WEM_FATAL 1 -#define WEM_SUPPRESS 2 - #endif \ No newline at end of file diff --git a/src/sccz80/error.c b/src/sccz80/error.c index 26ba811c27..c01464fd07 100644 --- a/src/sccz80/error.c +++ b/src/sccz80/error.c @@ -176,6 +176,8 @@ struct errors { { "Unknown goto label: %s at line %d", 1 }, { "Cannot assign to compound auto variable \'%s\'", 1 }, + { "Attempt to modify const lvalue \'%s\''", 1}, + }; /* diff --git a/src/sccz80/error.h b/src/sccz80/error.h index ce86ed7f6d..6e45f1835d 100644 --- a/src/sccz80/error.h +++ b/src/sccz80/error.h @@ -68,5 +68,7 @@ enum errorct { E_MISSIF, E_MAXLEVELS, E_MAXGOTO, E_UNGOTO, E_AUTOASSIGN, + E_CHANGING_CONST, + E_MAXIMUM }; diff --git a/src/sccz80/primary.c b/src/sccz80/primary.c index 7e3c8468f1..e88edda93f 100644 --- a/src/sccz80/primary.c +++ b/src/sccz80/primary.c @@ -520,6 +520,9 @@ void nstep( void store(LVALUE* lval) { + if ( lval->symbol && lval->symbol->isconst ) { + error(E_CHANGING_CONST, lval->symbol); + } if (lval->indirect == 0) putmem(lval->symbol); else @@ -564,14 +567,20 @@ void smartpush(LVALUE* lval, char* before) */ void smartstore(LVALUE* lval) { - if (lval->indirect != CINT || lval->symbol == 0 || lval->symbol->storage != STKLOC) + if (lval->indirect != CINT || lval->symbol == NULL || lval->symbol->storage != STKLOC) { store(lval); - else { + } else { switch (lval->symbol->offset.i - Zsp) { case 0: + if ( lval->symbol->isconst ) { + error(E_CHANGING_CONST, lval->symbol); + } puttos(); break; case 2: + if ( lval->symbol->isconst ) { + error(E_CHANGING_CONST, lval->symbol); + } put2tos(); break; default: diff --git a/src/sccz80/stmt.c b/src/sccz80/stmt.c index 753abfea67..51e3ccd4e2 100644 --- a/src/sccz80/stmt.c +++ b/src/sccz80/stmt.c @@ -59,12 +59,12 @@ int statement() otag = GetVarID(&var, STATIK); if (var.type == STRUCT) { - declloc(STRUCT, otag, var.sign, locstatic, var.zfar); + declloc(STRUCT, otag, locstatic, &var); return (lastst); } else if (var.type || regit) { if (regit && var.type == NO) var.type = CINT; - declloc(var.type, NULL, var.sign, locstatic, var.zfar); + declloc(var.type, NULL, locstatic, &var); return (lastst); } From 34d9aaa2ef65930991af46422b22813f14c2a14e Mon Sep 17 00:00:00 2001 From: suborb Date: Thu, 23 Feb 2017 21:54:57 +0000 Subject: [PATCH 08/16] Prototyping, hiding, renaming. Just cleanup stuff again. --- src/sccz80/Makefile | 80 +++++++++++++++++++++---------------------- src/sccz80/callfunc.c | 3 +- src/sccz80/callfunc.h | 4 --- src/sccz80/ccdefs.h | 20 +++++++---- src/sccz80/codegen.c | 4 +-- src/sccz80/codegen.h | 2 +- src/sccz80/const.c | 12 ++++--- src/sccz80/const.h | 2 -- src/sccz80/define.h | 31 ++++++++++++++++- src/sccz80/expr.c | 4 +-- src/sccz80/expr.h | 2 +- src/sccz80/lvalue.h | 33 ------------------ src/sccz80/plunge.c | 10 +++--- src/sccz80/plunge.h | 6 ---- src/sccz80/primary.c | 8 ++--- src/sccz80/primary.h | 6 ++-- src/sccz80/sym.c | 15 ++++---- src/sccz80/sym.h | 2 -- 18 files changed, 119 insertions(+), 125 deletions(-) delete mode 100644 src/sccz80/callfunc.h delete mode 100644 src/sccz80/lvalue.h delete mode 100644 src/sccz80/plunge.h diff --git a/src/sccz80/Makefile b/src/sccz80/Makefile index c9f7b9b61c..cccc49d237 100644 --- a/src/sccz80/Makefile +++ b/src/sccz80/Makefile @@ -42,71 +42,71 @@ clean : $(RM) -rf Debug Release # Dependencies (gcc -MM) -callfunc.o: callfunc.c ccdefs.h define.h lvalue.h callfunc.h codegen.h \ +callfunc.o: callfunc.c ccdefs.h define.h codegen.h \ const.h data.h declvar.h declfunc.h declinit.h error.h expr.h \ - io.h lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h \ + io.h lex.h main.h misc.h preproc.h primary.h stmt.h sym.h \ while.h -codegen.o: codegen.c ccdefs.h define.h lvalue.h callfunc.h codegen.h \ +codegen.o: codegen.c ccdefs.h define.h codegen.h \ const.h data.h declvar.h declfunc.h declinit.h error.h expr.h \ - io.h lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h \ + io.h lex.h main.h misc.h preproc.h primary.h stmt.h sym.h \ while.h -const.o: const.c ccdefs.h define.h lvalue.h callfunc.h codegen.h const.h \ +const.o: const.c ccdefs.h define.h codegen.h const.h \ data.h declvar.h declfunc.h declinit.h error.h expr.h io.h \ - lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h while.h -data.o: data.c ccdefs.h define.h lvalue.h callfunc.h codegen.h const.h \ + lex.h main.h misc.h preproc.h primary.h stmt.h sym.h while.h +data.o: data.c ccdefs.h define.h codegen.h const.h \ data.h declvar.h declfunc.h declinit.h error.h expr.h io.h \ - lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h while.h -declfunc.o: declfunc.c ccdefs.h define.h lvalue.h callfunc.h codegen.h \ + lex.h main.h misc.h preproc.h primary.h stmt.h sym.h while.h +declfunc.o: declfunc.c ccdefs.h define.h codegen.h \ const.h data.h declvar.h declfunc.h declinit.h error.h expr.h \ - io.h lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h \ + io.h lex.h main.h misc.h preproc.h primary.h stmt.h sym.h \ while.h -declinit.o: declinit.c ccdefs.h define.h lvalue.h callfunc.h codegen.h \ +declinit.o: declinit.c ccdefs.h define.h codegen.h \ const.h data.h declvar.h declfunc.h declinit.h error.h expr.h \ - io.h lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h \ + io.h lex.h main.h misc.h preproc.h primary.h stmt.h sym.h \ while.h -declvar.o: declvar.c ccdefs.h define.h lvalue.h callfunc.h codegen.h \ +declvar.o: declvar.c ccdefs.h define.h codegen.h \ const.h data.h declvar.h declfunc.h declinit.h error.h expr.h \ - io.h lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h \ + io.h lex.h main.h misc.h preproc.h primary.h stmt.h sym.h \ while.h -error.o: error.c ccdefs.h define.h lvalue.h callfunc.h codegen.h const.h \ +error.o: error.c ccdefs.h define.h codegen.h const.h \ data.h declvar.h declfunc.h declinit.h error.h expr.h io.h \ - lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h while.h -expr.o: expr.c ccdefs.h define.h lvalue.h callfunc.h codegen.h const.h \ + lex.h main.h misc.h preproc.h primary.h stmt.h sym.h while.h +expr.o: expr.c ccdefs.h define.h codegen.h const.h \ data.h declvar.h declfunc.h declinit.h error.h expr.h io.h \ - lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h while.h -goto.o: goto.c ccdefs.h define.h lvalue.h callfunc.h codegen.h const.h \ + lex.h main.h misc.h preproc.h primary.h stmt.h sym.h while.h +goto.o: goto.c ccdefs.h define.h codegen.h const.h \ data.h declvar.h declfunc.h declinit.h error.h expr.h io.h \ - lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h while.h -io.o: io.c ccdefs.h define.h lvalue.h callfunc.h codegen.h const.h data.h \ + lex.h main.h misc.h preproc.h primary.h stmt.h sym.h while.h +io.o: io.c ccdefs.h define.h codegen.h const.h data.h \ declvar.h declfunc.h declinit.h error.h expr.h io.h lex.h \ - main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h while.h -lex.o: lex.c ccdefs.h define.h lvalue.h callfunc.h codegen.h const.h \ + main.h misc.h preproc.h primary.h stmt.h sym.h while.h +lex.o: lex.c ccdefs.h define.h codegen.h const.h \ data.h declvar.h declfunc.h declinit.h error.h expr.h io.h \ - lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h while.h -main.o: main.c ccdefs.h define.h lvalue.h callfunc.h codegen.h const.h \ + lex.h main.h misc.h preproc.h primary.h stmt.h sym.h while.h +main.o: main.c ccdefs.h define.h codegen.h const.h \ data.h declvar.h declfunc.h declinit.h error.h expr.h io.h \ - lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h while.h -misc.o: misc.c ccdefs.h define.h lvalue.h callfunc.h codegen.h const.h \ + lex.h main.h misc.h preproc.h primary.h stmt.h sym.h while.h +misc.o: misc.c ccdefs.h define.h codegen.h const.h \ data.h declvar.h declfunc.h declinit.h error.h expr.h io.h \ - lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h while.h -plunge.o: plunge.c ccdefs.h define.h lvalue.h callfunc.h codegen.h \ + lex.h main.h misc.h preproc.h primary.h stmt.h sym.h while.h +plunge.o: plunge.c ccdefs.h define.h codegen.h \ const.h data.h declvar.h declfunc.h declinit.h error.h expr.h \ - io.h lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h \ + io.h lex.h main.h misc.h preproc.h primary.h stmt.h sym.h \ while.h -preproc.o: preproc.c ccdefs.h define.h lvalue.h callfunc.h codegen.h \ +preproc.o: preproc.c ccdefs.h define.h codegen.h \ const.h data.h declvar.h declfunc.h declinit.h error.h expr.h \ - io.h lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h \ + io.h lex.h main.h misc.h preproc.h primary.h stmt.h sym.h \ while.h -primary.o: primary.c ccdefs.h define.h lvalue.h callfunc.h codegen.h \ +primary.o: primary.c ccdefs.h define.h codegen.h \ const.h data.h declvar.h declfunc.h declinit.h error.h expr.h \ - io.h lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h \ + io.h lex.h main.h misc.h preproc.h primary.h stmt.h sym.h \ while.h -stmt.o: stmt.c ccdefs.h define.h lvalue.h callfunc.h codegen.h const.h \ +stmt.o: stmt.c ccdefs.h define.h codegen.h const.h \ data.h declvar.h declfunc.h declinit.h error.h expr.h io.h \ - lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h while.h -sym.o: sym.c ccdefs.h define.h lvalue.h callfunc.h codegen.h const.h \ + lex.h main.h misc.h preproc.h primary.h stmt.h sym.h while.h +sym.o: sym.c ccdefs.h define.h codegen.h const.h \ data.h declvar.h declfunc.h declinit.h error.h expr.h io.h \ - lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h while.h -while.o: while.c ccdefs.h define.h lvalue.h callfunc.h codegen.h const.h \ + lex.h main.h misc.h preproc.h primary.h stmt.h sym.h while.h +while.o: while.c ccdefs.h define.h codegen.h const.h \ data.h declvar.h declfunc.h declinit.h error.h expr.h io.h \ - lex.h main.h misc.h plunge.h preproc.h primary.h stmt.h sym.h while.h + lex.h main.h misc.h preproc.h primary.h stmt.h sym.h while.h diff --git a/src/sccz80/callfunc.c b/src/sccz80/callfunc.c index 72ebb95cdb..5522a8ced7 100644 --- a/src/sccz80/callfunc.c +++ b/src/sccz80/callfunc.c @@ -14,6 +14,7 @@ static int SetWatch(char* sym, int* isscanf); static int SetMiniFunc(unsigned char* arg, uint32_t* format_option_ptr); +static int ForceArgs(char dest, char src, int expr, char functab); /* * External variables used @@ -277,7 +278,7 @@ static int SetWatch(char* sym, int* type) * djm routine to force arguments to switch type */ -int ForceArgs(char dest, char src, int expr, char functab) +static int ForceArgs(char dest, char src, int expr, char functab) { char did, dtype, disfar, dissign; char sid, stype, sisfar, sissign; diff --git a/src/sccz80/callfunc.h b/src/sccz80/callfunc.h deleted file mode 100644 index c524caf1cf..0000000000 --- a/src/sccz80/callfunc.h +++ /dev/null @@ -1,4 +0,0 @@ -/* callfunction.c */ -extern void callfunction(SYMBOL *ptr); -extern int nospread(char *sym); -extern int ForceArgs(char dest, char src, int expr, char tagtab); diff --git a/src/sccz80/ccdefs.h b/src/sccz80/ccdefs.h index f2b0fc3c4f..d5838b84c7 100644 --- a/src/sccz80/ccdefs.h +++ b/src/sccz80/ccdefs.h @@ -7,18 +7,15 @@ * $Id: ccdefs.h,v 1.5 2016-08-26 05:44:47 aralbrec Exp $ */ -/* - * System wide definitions - */ +#ifndef CCDEFS_H +#define CCDEFS_H #include -//#include #include #include #include "define.h" -#include "lvalue.h" /* * Now the fix for HP-UX @@ -45,7 +42,8 @@ * Prototypes */ -#include "callfunc.h" +extern void callfunction(SYMBOL *ptr); + #include "codegen.h" #include "const.h" #include "data.h" @@ -58,10 +56,18 @@ #include "lex.h" #include "main.h" #include "misc.h" -#include "plunge.h" + +/* plunge.c */ +extern int skim(char *opstr, void (*testfuncz)(LVALUE* lval, int label), void (*testfuncq)(int label), int dropval, int endval, int (*heir)(LVALUE* lval), LVALUE *lval); +extern void dropout(int k, void (*testfuncz)(LVALUE* lval, int label), void (*testfuncq)(int label), int exit1, LVALUE *lval); +extern int plnge1(int (*heir)(LVALUE* lval), LVALUE *lval); +extern void plnge2a(int (*heir)(LVALUE* lval), LVALUE *lval, LVALUE *lval2, void (*oper)(LVALUE *lval), void (*doper)(LVALUE *lval)); +extern void plnge2b(int (*heir)(LVALUE* lval), LVALUE *lval, LVALUE *lval2, void (*oper)(LVALUE *lval)); + #include "preproc.h" #include "primary.h" #include "stmt.h" #include "sym.h" #include "while.h" +#endif \ No newline at end of file diff --git a/src/sccz80/codegen.c b/src/sccz80/codegen.c index 7252ca1440..176a240f95 100644 --- a/src/sccz80/codegen.c +++ b/src/sccz80/codegen.c @@ -1320,7 +1320,7 @@ void zor(LVALUE* lval) /* Exclusive 'or' the primary and secondary */ /* (results in primary) */ -void zxor(LVALUE* lval) +void zxor(LVALUE *lval) { switch (lval->val_type) { case LONG: @@ -1478,7 +1478,7 @@ void dec(LVALUE* lval) /* and put a literal 1 in the primary if the condition is */ /* true, otherwise they clear the primary register */ -void dummy(LVALUE* lval, int label) +void dummy(LVALUE *lval) { /* Dummy function to allows us to check for c/nc at end of if clause */ } diff --git a/src/sccz80/codegen.h b/src/sccz80/codegen.h index 760df4662b..4ca02ff905 100644 --- a/src/sccz80/codegen.h +++ b/src/sccz80/codegen.h @@ -108,7 +108,7 @@ extern void jumpc(int); extern void jumpr(int); extern void opjumpr(char *, int); extern void setcond(int); -extern void dummy(LVALUE *, int); +extern void dummy(LVALUE *); extern void LoadAccum(void); extern void CpCharVal(int); extern void EmitLine(int); diff --git a/src/sccz80/const.c b/src/sccz80/const.c index 68879dc76c..7b711a53f2 100644 --- a/src/sccz80/const.c +++ b/src/sccz80/const.c @@ -22,6 +22,8 @@ #include +static int get_member_size(TAG_SYMBOL *ptr);; +static int32_t search_litq_for_doublestr(unsigned char *num);; static void dofloat(double raw, unsigned char fa[6], int mant_bytes, int exp_bias); /* @@ -139,7 +141,7 @@ int fnumber(int32_t* val) *val = stash_double_str(start, lptr + line); return (1); } else { - *val = searchdub(sum); + *val = search_litq_for_doublestr(sum); } return (1); /* report success */ } @@ -173,7 +175,7 @@ int stash_double_str(char* start, char* end) * number - saves space etc etc */ -int32_t searchdub(unsigned char* num) +static int32_t search_litq_for_doublestr(unsigned char* num) { unsigned char* tempdub; int dubleft, k, match; @@ -521,7 +523,7 @@ void size_of(LVALUE* lval) lval->const_val = 6; break; case STRUCT: - lval->const_val = GetMembSize(otag); + lval->const_val = get_member_size(otag); if (lval->const_val == 0) lval->const_val = otag->size; } @@ -540,7 +542,7 @@ void size_of(LVALUE* lval) if (ptr->type != STRUCT) { lval->const_val = ptr->size; } else { - lval->const_val = GetMembSize(tagtab + ptr->tag_idx); + lval->const_val = get_member_size(tagtab + ptr->tag_idx); if (lval->const_val == 0) lval->const_val = ptr->size; } @@ -566,7 +568,7 @@ void size_of(LVALUE* lval) vconst(lval->const_val); } -int GetMembSize(TAG_SYMBOL* ptr) +static int get_member_size(TAG_SYMBOL* ptr) { char sname[NAMEMAX]; SYMBOL* ptr2; diff --git a/src/sccz80/const.h b/src/sccz80/const.h index 3cb7142d9b..ed927889fa 100644 --- a/src/sccz80/const.h +++ b/src/sccz80/const.h @@ -2,7 +2,6 @@ extern int constant(LVALUE *lval); extern int fnumber(int32_t *val); extern int stash_double_str(char *start,char *end); -extern int32_t searchdub(unsigned char *num); extern int number(int32_t *val); extern int hex(char c); extern void address(SYMBOL *ptr); @@ -13,4 +12,3 @@ extern int qstr(int32_t *val); extern void stowlit(int value, int size); extern unsigned char litchar(void); extern void size_of(LVALUE *lval); -extern int GetMembSize(TAG_SYMBOL *ptr); diff --git a/src/sccz80/define.h b/src/sccz80/define.h index 032bc28ab9..441b24ffba 100644 --- a/src/sccz80/define.h +++ b/src/sccz80/define.h @@ -39,7 +39,6 @@ /* Stefano - doubled the global symbol table size */ /* Aralbrec - doubled the global symbol table size again! */ #define NUMGLBS 2048 -#define MASKGLBS 2047 #define STARTGLB symtab #define ENDGLB (STARTGLB+NUMGLBS) @@ -349,4 +348,34 @@ struct parser_stack { }; + +typedef struct lvalue_s LVALUE; + +struct lvalue_s { + SYMBOL *symbol ; /* symbol table address, or 0 for constant */ + int indirect ; /* type of indirect object, 0 for static object */ + int ptr_type ; /* type of pointer or array, 0 for other idents */ + int is_const ; /* true if constant expression */ + int32_t const_val ; /* value of constant expression (& other uses) */ + TAG_SYMBOL *tagsym ; /* tag symbol address, 0 if not struct */ + void (*binop)(LVALUE *lval) ; /* function address of highest/last binary operator */ + char *stage_add ; /* stage addess of "oper 0" code, else 0 */ + int val_type ; /* type of value calculated */ + int oldval_type; /* What the valtype was */ + enum symbol_flags flags ; /* As per symbol */ + char oflags; /* Needed for deref of far str*/ + int type; /* type (from symbol table) */ + int ident; /* ident (from symbol table) */ + enum storage_type storage; /* storage (from sym tab) */ + char c_id; /* ident of cast */ + char c_vtype; /* type of value calc if cast */ + char c_flags; /* flags for casting */ + int level; /* Parenth level (cast) */ + int castlevel; + int offset; + TAG_SYMBOL *c_tag; +} ; + + + #endif \ No newline at end of file diff --git a/src/sccz80/expr.c b/src/sccz80/expr.c index f315de7cee..e33f8fed5e 100644 --- a/src/sccz80/expr.c +++ b/src/sccz80/expr.c @@ -61,7 +61,7 @@ int heir1(LVALUE* lval) { char *before, *start; LVALUE lval2, lval3; - void (*oper)(), (*doper)(); + void (*oper)(LVALUE *lval), (*doper)(LVALUE *lval); int k; ClearCast(&lval2); @@ -269,7 +269,7 @@ int heir2b(LVALUE* lval) return skim("&&", testjump, jumpnc, 0, 1, heir2, lval); } -int heir234(LVALUE* lval, int (*heir)(), char opch, void (*oper)()) +int heir234(LVALUE* lval, int (*heir)(LVALUE *lval), char opch, void (*oper)(LVALUE *lval)) { LVALUE lval2; int k; diff --git a/src/sccz80/expr.h b/src/sccz80/expr.h index 5dc2d38846..118fc94f62 100644 --- a/src/sccz80/expr.h +++ b/src/sccz80/expr.h @@ -5,7 +5,7 @@ extern int heir1(LVALUE *lval); extern int heir1a(LVALUE *lval); extern int heir2a(LVALUE *lval); extern int heir2b(LVALUE *lval); -extern int heir234(LVALUE *lval, int (*heir)(), char opch, void (*oper)()); +extern int heir234(LVALUE *lval, int (*heir)(LVALUE *lval), char opch, void (*oper)(LVALUE *lval)); extern int heir2(LVALUE *lval); extern int heir3(LVALUE *lval); extern int heir4(LVALUE *lval); diff --git a/src/sccz80/lvalue.h b/src/sccz80/lvalue.h deleted file mode 100644 index 6bb4f9f8ef..0000000000 --- a/src/sccz80/lvalue.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * structure for lvalue's - (cclvalue.h) - * - * $Id: lvalue.h,v 1.4 2009-09-06 18:58:37 dom Exp $ - */ - -typedef struct lvalue_s LVALUE; - -struct lvalue_s { - SYMBOL *symbol ; /* symbol table address, or 0 for constant */ - int indirect ; /* type of indirect object, 0 for static object */ - int ptr_type ; /* type of pointer or array, 0 for other idents */ - int is_const ; /* true if constant expression */ - int32_t const_val ; /* value of constant expression (& other uses) */ - TAG_SYMBOL *tagsym ; /* tag symbol address, 0 if not struct */ - void (*binop)() ; /* function address of highest/last binary operator */ - char *stage_add ; /* stage addess of "oper 0" code, else 0 */ - int val_type ; /* type of value calculated */ - int oldval_type; /* What the valtype was */ - char flags ; /* As per symbol */ - char oflags; /* Needed for deref of far str*/ - int type; /* type (from symbol table) */ - int ident; /* ident (from symbol table) */ - enum storage_type storage; /* storage (from sym tab) */ - char c_id; /* ident of cast */ - char c_vtype; /* type of value calc if cast */ - char c_flags; /* flags for casting */ - int level; /* Parenth level (cast) */ - int castlevel; - int offset; - TAG_SYMBOL *c_tag; -} ; - diff --git a/src/sccz80/plunge.c b/src/sccz80/plunge.c index 2a806aec56..8e7c200ba1 100644 --- a/src/sccz80/plunge.c +++ b/src/sccz80/plunge.c @@ -17,7 +17,7 @@ /* * skim over text adjoining || and && operators */ -int skim(char* opstr, void (*testfuncz)(), void (*testfuncq)(), int dropval, int endval, int (*heir)(), LVALUE* lval) +int skim(char* opstr, void (*testfuncz)(LVALUE* lval, int label), void (*testfuncq)(int label), int dropval, int endval, int (*heir)(LVALUE* lval), LVALUE* lval) { int droplab, endlab, hits, k; @@ -52,7 +52,7 @@ int skim(char* opstr, void (*testfuncz)(), void (*testfuncq)(), int dropval, int /* * test for early dropout from || or && evaluations */ -void dropout(int k, void (*testfuncz)(), void (*testfuncq)(), int exit1, LVALUE* lval) +void dropout(int k, void (*testfuncz)(LVALUE* lval, int label), void (*testfuncq)(int label), int exit1, LVALUE* lval) { if (k) rvalue(lval); @@ -75,7 +75,7 @@ void dropout(int k, void (*testfuncz)(), void (*testfuncq)(), int exit1, LVALUE* /* * unary plunge to lower level */ -int plnge1(int (*heir)(), LVALUE* lval) +int plnge1(int (*heir)(LVALUE* lval), LVALUE* lval) { char *before, *start; int k; @@ -92,7 +92,7 @@ int plnge1(int (*heir)(), LVALUE* lval) /* * binary plunge to lower level (not for +/-) */ -void plnge2a(int (*heir)(), LVALUE* lval, LVALUE* lval2, void (*oper)(), void (*doper)()) +void plnge2a(int (*heir)(LVALUE* lval), LVALUE* lval, LVALUE* lval2, void (*oper)(), void (*doper)()) { char *before, *start; @@ -233,7 +233,7 @@ void plnge2a(int (*heir)(), LVALUE* lval, LVALUE* lval2, void (*oper)(), void (* /* * binary plunge to lower level (for +/-) */ -void plnge2b(int (*heir)(), LVALUE* lval, LVALUE* lval2, void (*oper)()) +void plnge2b(int (*heir)(LVALUE* lval), LVALUE* lval, LVALUE* lval2, void (*oper)(LVALUE *lval)) { char *before, *start, *before1, *start1; int val, oldsp = Zsp; diff --git a/src/sccz80/plunge.h b/src/sccz80/plunge.h deleted file mode 100644 index 0a95d4d948..0000000000 --- a/src/sccz80/plunge.h +++ /dev/null @@ -1,6 +0,0 @@ -/* plunge.c */ -extern int skim(char *opstr, void (*testfuncz)(), void (*testfuncq)(), int dropval, int endval, int (*heir)(), LVALUE *lval); -extern void dropout(int k, void (*testfuncz)(), void (*testfuncq)(), int exit1, LVALUE *lval); -extern int plnge1(int (*heir)(), LVALUE *lval); -extern void plnge2a(int (*heir)(), LVALUE *lval, LVALUE *lval2, void (*oper)(), void (*doper)()); -extern void plnge2b(int (*heir)(), LVALUE *lval, LVALUE *lval2, void (*oper)()); diff --git a/src/sccz80/primary.c b/src/sccz80/primary.c index e88edda93f..ace2c3d687 100644 --- a/src/sccz80/primary.c +++ b/src/sccz80/primary.c @@ -417,7 +417,7 @@ void result(LVALUE* lval, LVALUE* lval2) void prestep( LVALUE* lval, int n, - void (*step)()) + void (*step)(LVALUE *lval)) { if (heira(lval) == 0) { needlval(); @@ -459,8 +459,8 @@ void poststep( int k, LVALUE* lval, int n, - void (*step)(), - void (*unstep)()) + void (*step)(LVALUE *lval), + void (*unstep)(LVALUE *lval)) { if (k == 0) { needlval(); @@ -510,7 +510,7 @@ void poststep( void nstep( LVALUE* lval, int n, - void (*unstep)()) + void (*unstep)(LVALUE *lval)) { addconst(n, 1, lval->symbol->flags & FARPTR); store(lval); diff --git a/src/sccz80/primary.h b/src/sccz80/primary.h index 4fd8f452c1..f94b0ce460 100644 --- a/src/sccz80/primary.h +++ b/src/sccz80/primary.h @@ -10,9 +10,9 @@ extern int widen(LVALUE *lval, LVALUE *lval2); extern void widenlong(LVALUE *lval, LVALUE *lval2); extern int dbltest(LVALUE *lval, LVALUE *lval2); extern void result(LVALUE *lval, LVALUE *lval2); -extern void prestep(LVALUE *lval, int n, void (*step)()); -extern void poststep(int k, LVALUE *lval, int n, void (*step)(), void (*unstep)()); -extern void nstep(LVALUE *lval, int n, void (*unstep)()); +extern void prestep(LVALUE *lval, int n, void (*step)(LVALUE *lval)); +extern void poststep(int k, LVALUE *lval, int n, void (*step)(LVALUE *lval), void (*unstep)(LVALUE *lval)); +extern void nstep(LVALUE *lval, int n, void (*unstep)(LVALUE *lval)); extern void store(LVALUE *lval); extern void smartpush(LVALUE *lval, char *before); extern void smartstore(LVALUE *lval); diff --git a/src/sccz80/sym.c b/src/sccz80/sym.c index 86f9f1fa10..05bba4ded5 100644 --- a/src/sccz80/sym.c +++ b/src/sccz80/sym.c @@ -8,14 +8,17 @@ #include "ccdefs.h" -int hash(char* sname) +static int hash(char *sname); +static void initialise_sym(SYMBOL *ptr, char *sname, enum ident_type id, char typ, enum storage_type storage, int more, int itag); + +static int hash(char* sname) { int c, h; h = *sname; while ((c = *(++sname))) h = (h << 1) + c; - return (h & MASKGLBS); + return (h & (NUMGLBS-1)); } /* djm @@ -143,7 +146,7 @@ SYMBOL* addglb( error(E_GLBOV); return 0; } - addsym(glbptr, sname, id, typ, storage, more, itag); + initialise_sym(glbptr, sname, id, typ, storage, more, itag); glbptr->offset.i = value; ++glbcnt; return (glbptr); @@ -167,7 +170,7 @@ SYMBOL* addloc( return 0; } cptr = locptr++; - addsym(cptr, sname, id, typ, STKLOC, more, itag); + initialise_sym(cptr, sname, id, typ, STKLOC, more, itag); return cptr; } @@ -187,7 +190,7 @@ SYMBOL* addmemb( error(E_MEMOV); return 0; } - addsym(membptr, sname, id, typ, storage, more, itag); + initialise_sym(membptr, sname, id, typ, storage, more, itag); membptr->offset.i = value; ++membptr; return (membptr); @@ -197,7 +200,7 @@ SYMBOL* addmemb( * insert values into symbol table */ -void addsym( +static void initialise_sym( SYMBOL* ptr, char* sname, enum ident_type id, diff --git a/src/sccz80/sym.h b/src/sccz80/sym.h index 29c6bf26d2..9c2caa3977 100644 --- a/src/sccz80/sym.h +++ b/src/sccz80/sym.h @@ -1,5 +1,4 @@ /* sym.c */ -extern int hash(char *sname); extern SYMBOL *findstc(char *sname); extern SYMBOL *findglb(char *sname); extern SYMBOL *findenum(char *sname); @@ -9,4 +8,3 @@ extern SYMBOL *findmemb(TAG_SYMBOL *tag, char *sname); extern SYMBOL *addglb(char *sname, enum ident_type id, char typ, int value, enum storage_type storage, int more, int itag); extern SYMBOL *addloc(char *sname, enum ident_type id, char typ, int more, int itag); extern SYMBOL *addmemb(char *sname, enum ident_type id, char typ, int value, enum storage_type storage, int more, int itag); -extern void addsym(SYMBOL *ptr, char *sname, enum ident_type id, char typ, enum storage_type storage, int more, int itag); From 4c1eda5a2cdd523243308f04e6cdcad82ab6f3f6 Mon Sep 17 00:00:00 2001 From: suborb Date: Thu, 23 Feb 2017 22:08:42 +0000 Subject: [PATCH 09/16] Another bit of cleanup --- src/sccz80/codegen.c | 82 +++++++++++++++++++------------------------- src/sccz80/codegen.h | 7 ---- 2 files changed, 36 insertions(+), 53 deletions(-) diff --git a/src/sccz80/codegen.c b/src/sccz80/codegen.c index 176a240f95..a0e806940c 100644 --- a/src/sccz80/codegen.c +++ b/src/sccz80/codegen.c @@ -29,8 +29,16 @@ #include extern int WasComp(LVALUE* lval); + + extern char Filenorig[]; + +static void threereg(void); +static void fivereg(void); +static void sixreg(void); + + /* * Data for this module */ @@ -1090,14 +1098,14 @@ void scale(int type, TAG_SYMBOL* tag) { switch (type) { case CINT: - doublereg(); + ol("add\thl,hl");; break; case CPTR: threereg(); break; case LONG: - doublereg(); - doublereg(); + ol("add\thl,hl");; + ol("add\thl,hl");; break; case DOUBLE: sixreg(); @@ -1112,16 +1120,16 @@ void quikmult(int size, char preserve) { switch (size) { case 16: - doublereg(); + ol("add\thl,hl");; case 8: - doublereg(); + ol("add\thl,hl");; case 4: - doublereg(); + ol("add\thl,hl");; case 2: - doublereg(); + ol("add\thl,hl");; break; case 12: - doublereg(); + ol("add\thl,hl");; case 6: sixreg(); break; @@ -1137,13 +1145,13 @@ void quikmult(int size, char preserve) break; case 10: fivereg(); - doublereg(); + ol("add\thl,hl");; break; case 14: - doublereg(); + ol("add\thl,hl");; case 7: sixreg(); - addbc(); /* BC contains original value */ + ol("add\thl,bc"); /* BC contains original value */ break; default: if (preserve) @@ -1156,47 +1164,34 @@ void quikmult(int size, char preserve) } } -/* add BC to the primary register */ -void addbc(void) -{ - ol("add\thl,bc"); -} -/* load BC from the primary register */ -void ldbc(void) -{ - ol("ld\tb,h"); - ol("ld\tc,l"); -} -/* Double the primary register */ -void doublereg(void) -{ - ol("add\thl,hl"); -} + /* Multiply the primary register by three */ -void threereg(void) +static void threereg(void) { - ldbc(); - addbc(); - addbc(); + ol("ld\tb,h"); + ol("ld\tc,l"); + ol("add\thl,bc"); + ol("add\thl,bc"); } /* Multiply the primary register by five */ -void fivereg(void) +static void fivereg(void) { - ldbc(); - doublereg(); - doublereg(); - addbc(); + ol("ld\tb,h"); + ol("ld\tc,l"); + ol("add\thl,hl");; + ol("add\thl,hl");; + ol("add\thl,bc"); } /* Multiply the primary register by six */ -void sixreg(void) +static void sixreg(void) { threereg(); - doublereg(); + ol("add\thl,hl");; } /* @@ -1396,7 +1391,7 @@ void lneg(LVALUE* lval) break; case CARRY: lval->val_type = CARRY; - ccf(); + ol("ccf"); break; case DOUBLE: convdoub2int(); @@ -1435,11 +1430,6 @@ void com(LVALUE* lval) } } -/* Complement the carry flag (used after arithmetic before !) */ -void ccf(void) -{ - ol("ccf"); -} /* * Increment value held in main register @@ -1793,7 +1783,7 @@ void zgt(LVALUE* lval) ol("xor\te"); ol("xor\tl"); ol("rlca"); - ccf(); + ol("ccf"); } lval->val_type = CARRY; break; @@ -1847,7 +1837,7 @@ void zge(LVALUE* lval) ol("xor\te"); ol("xor\tl"); ol("rlca"); - ccf(); + ol("ccf"); postlabel(label); } lval->val_type = CARRY; diff --git a/src/sccz80/codegen.h b/src/sccz80/codegen.h index 4ca02ff905..a231d36aff 100644 --- a/src/sccz80/codegen.h +++ b/src/sccz80/codegen.h @@ -51,12 +51,6 @@ extern void point(void); extern int modstk(int newsp, int save,int saveaf); extern void scale(int type, TAG_SYMBOL *tag); extern void quikmult(int size, char preserve); -extern void addbc(void); -extern void ldbc(void); -extern void doublereg(void); -extern void threereg(void); -extern void fivereg(void); -extern void sixreg(void); extern void zadd(LVALUE *); extern void zsub(LVALUE *); extern void mult(LVALUE *); @@ -70,7 +64,6 @@ extern void asl(LVALUE *); extern void lneg(LVALUE *); extern void neg(LVALUE *); extern void com(LVALUE *); -extern void ccf(void); extern void inc(LVALUE *); extern void dec(LVALUE *); extern void zeq(LVALUE *); From 962d9663dd67f7bf9edd258fee9e6008f357f760 Mon Sep 17 00:00:00 2001 From: suborb Date: Thu, 23 Feb 2017 22:29:48 +0000 Subject: [PATCH 10/16] Remove unused, rename etc --- src/sccz80/const.c | 4 +-- src/sccz80/data.c | 40 +++++++++++----------------- src/sccz80/data.h | 9 ++----- src/sccz80/declfunc.c | 6 ++--- src/sccz80/declvar.c | 20 +++++++------- src/sccz80/declvar.h | 3 --- src/sccz80/expr.c | 2 +- src/sccz80/main.c | 62 +++++++++++-------------------------------- src/sccz80/main.h | 1 - src/sccz80/stmt.c | 4 +-- 10 files changed, 52 insertions(+), 99 deletions(-) diff --git a/src/sccz80/const.c b/src/sccz80/const.c index 7b711a53f2..66dd9bd26d 100644 --- a/src/sccz80/const.c +++ b/src/sccz80/const.c @@ -41,7 +41,7 @@ char conssign; int constant(LVALUE* lval) { constype = CINT; - conssign = dosigned; + conssign = c_default_unsigned; lval->is_const = 1; /* assume constant will be found */ if (fnumber(&lval->const_val)) { lval->val_type = DOUBLE; @@ -299,7 +299,7 @@ int pstr(int32_t* val) int k; constype = CINT; - conssign = dosigned; + conssign = c_default_unsigned; if (cmatch('\'')) { k = 0; while (ch() && ch() != '\'') diff --git a/src/sccz80/data.c b/src/sccz80/data.c index c672883b3a..98a961c391 100644 --- a/src/sccz80/data.c +++ b/src/sccz80/data.c @@ -23,7 +23,7 @@ SYMBOL *symtab, *loctab; /* global and local symbol tables */ SYMBOL *glbptr, *locptr; /* ptrs to next entries */ int glbcnt; /* number of globals used */ -SYMBOL* dummy_sym[NTYPE + NUMTAG + 1]; +SYMBOL *dummy_sym[NTYPE + NUMTAG + 1]; WHILE_TAB* wqueue; /* start of while queue */ WHILE_TAB* wqptr; /* ptr to next entry */ @@ -38,11 +38,11 @@ int gltptr, litptr, dubptr; /* index of next entry */ char macq[MACQSIZE]; /* macro string buffer */ int macptr; /* and its index */ -TAG_SYMBOL* tagtab; /* start of structure tag table */ -TAG_SYMBOL* tagptr; /* ptr to next entry */ +TAG_SYMBOL *tagtab; /* start of structure tag table */ +TAG_SYMBOL *tagptr; /* ptr to next entry */ -SYMBOL* membtab; /* structure member table */ -SYMBOL* membptr; /* ptr to next member */ +SYMBOL *membtab; /* structure member table */ +SYMBOL *membptr; /* ptr to next member */ char* stage; /* staging buffer */ char* stagenext; /* next address in stage */ @@ -60,13 +60,13 @@ char Filename[FILENAME_LEN + 1]; /* output file name */ /* My stuff for LIB of long common functions */ -int incfloat, cppcom, doinline, ncomp; +int need_floatpack, doinline, ncomp; int stackargs; -int defstatic, appz88, filenum; /* next argument to be used */ +int defstatic, filenum; /* next argument to be used */ -char dosigned, makelib, fnflags, mathz88, compactcode; +char c_default_unsigned, fnflags, mathz88, compactcode; int nxtlab, /* next avail label # */ dublab, /* label # relative to double pool */ @@ -101,17 +101,15 @@ int nxtlab, /* next avail label # */ where local[0] is 1st word on stack after ret addr */ fname; /* label for name of current fct */ -FILE *input, /* iob # for input file */ - *output, /* iob # for output file (if any) */ - *inpt2, /* iob # for "include" file */ - *saveout; /* holds output ptr when diverted to console */ +FILE *input; /* iob # for input file */ +FILE *output; /* iob # for output file (if any) */ +FILE *inpt2; /* iob # for "include" file */ +FILE *saveout; /* holds output ptr when diverted to console */ -#ifdef SMALL_C -int minavail = 32000; /* minimum memory available */ -#endif -SYMBOL *currfn, /* ptr to symtab entry for current fn. */ - *savecurr; /* copy of currfn for #include */ + +SYMBOL *currfn; /* ptr to symtab entry for current fn. */ +SYMBOL *savecurr; /* copy of currfn for #include */ int gargc; /* global copies of command line args */ char** gargv; @@ -121,8 +119,7 @@ char endasm; char margtag; /* Struct tag number for arg value */ char fnargvalue; /* Type of argument value (as per proto) */ int ltype; /* Long? */ -int opertype; /* Saves a lot of code! Reference in cc6 to grab - long type operations! */ + /* * Variable for the offset to a shared library routine @@ -155,11 +152,6 @@ char* outext; int defdenums; -/* - * Size of far heap in bytes/units/twiddle - */ - -int farheapsz; /* * Max level for printf (i.e. what routine do we want?) diff --git a/src/sccz80/data.h b/src/sccz80/data.h index 8be502de0f..b2e64b566a 100644 --- a/src/sccz80/data.h +++ b/src/sccz80/data.h @@ -35,16 +35,13 @@ extern char mline[]; extern int lptr; extern int mptr; extern char Filename[]; -extern int incfloat; -extern int cppcom; +extern int need_floatpack; extern int doinline; extern int ncomp; extern int stackargs; extern int defstatic; -extern int appz88; extern int filenum; -extern char dosigned; -extern char makelib; +extern char c_default_unsigned; extern char fnflags; extern char mathz88; extern char compactcode; @@ -86,7 +83,6 @@ extern char **gargv; extern char endasm; extern char fnargvalue; extern int ltype; -extern int opertype; extern char margtag; extern int shareoffset; extern int debuglevel; @@ -95,7 +91,6 @@ extern char *outext; extern int indexix; extern int useframe; extern int defdenums; -extern int farheapsz; extern int printflevel; extern int doublestrings; extern int usempm; diff --git a/src/sccz80/declfunc.c b/src/sccz80/declfunc.c index 34e2db4265..2b398a8db5 100644 --- a/src/sccz80/declfunc.c +++ b/src/sccz80/declfunc.c @@ -144,7 +144,7 @@ void newfunc() return; } warning(W_RETINT); - AddFuncCode(n, CINT, FUNCTION, dosigned, 0, STATIK, 0, 1, NO, 0, &addr); + AddFuncCode(n, CINT, FUNCTION, c_default_unsigned, 0, STATIK, 0, 1, NO, 0, &addr); } /* @@ -587,11 +587,11 @@ SYMBOL *getarg( /* * This is of dubious need since prototyping came about, we could * inadvertently include fp packages if the user includes but - * didn't actually use them, we'll save the incfloat business for + * didn't actually use them, we'll save the need_floatpack business for * static doubles and definitions of local doubles * * if (typ == DOUBLE) - * incfloat=1; + * need_floatpack=1; */ argptr = NULL; diff --git a/src/sccz80/declvar.c b/src/sccz80/declvar.c index 4d329e0a6f..715998fe8b 100644 --- a/src/sccz80/declvar.c +++ b/src/sccz80/declvar.c @@ -27,6 +27,10 @@ #include "ccdefs.h" +static TAG_SYMBOL *defstruct(char *sname, enum storage_type storage, int is_struct); +static int needsub(void); +static void swallow_bitfield(void); + /* * test for global declarations/structure member declarations */ @@ -429,7 +433,7 @@ void declglb( } else if (is_struct) { if (type == CINT && ident == VARIABLE) - BitFieldSwallow(); + swallow_bitfield(); /* are adding structure member, mtag->size is offset */ myptr = addmemb(sname, ident, type, mtag->size, storage, more, itag); myptr--; /* addmemb returns myptr+1 */ @@ -721,7 +725,7 @@ void ptrerror(int ident) * this routine makes subscript the absolute * size of the array. */ -int needsub(void) +static int needsub(void) { int32_t num; @@ -752,7 +756,7 @@ TAG_SYMBOL* GetVarID(struct varid *var, enum storage_type storage) char sname[NAMEMAX]; SYMBOL* ptr; - var->sign = dosigned; + var->sign = c_default_unsigned; var->zfar = NO; var->type = NO; var->sflag = NO; @@ -788,7 +792,7 @@ TAG_SYMBOL* GetVarID(struct varid *var, enum storage_type storage) swallow("int"); var->type = CINT; } else if (amatch("float") || amatch("double")) { - incfloat = 1; + need_floatpack = 1; var->type = DOUBLE; } else if (amatch("void")) var->type = VOID; @@ -799,7 +803,7 @@ TAG_SYMBOL* GetVarID(struct varid *var, enum storage_type storage) if (ptr == 0) /* not defined */ defenum(sname, storage); var->type = CINT; - var->sign = dosigned; + var->sign = c_default_unsigned; } else if ((var->sflag = amatch("struct")) || amatch("union")) { var->type = STRUCT; /* find structure tag */ @@ -840,11 +844,7 @@ TAG_SYMBOL* GetVarID(struct varid *var, enum storage_type storage) return (0); } -/* - * Swallow bitfield definition - */ - -void BitFieldSwallow(void) +static void swallow_bitfield(void) { int32_t val; if (cmatch(':')) { diff --git a/src/sccz80/declvar.h b/src/sccz80/declvar.h index 5ab9d8575a..1f3b1f5d78 100644 --- a/src/sccz80/declvar.h +++ b/src/sccz80/declvar.h @@ -1,6 +1,5 @@ /* decl.c */ extern int dodeclare(enum storage_type storage, TAG_SYMBOL *mtag, int is_struct); -extern TAG_SYMBOL *defstruct(char *sname, enum storage_type storage, int is_struct); extern int get_ident(void); extern int dummy_idx(int typ, TAG_SYMBOL *otag); extern void declglb(int typ, enum storage_type storage, TAG_SYMBOL *mtag, TAG_SYMBOL *otag, int is_struct, struct varid *var); @@ -8,8 +7,6 @@ extern void declloc(int typ, TAG_SYMBOL *otag, char locstatic, struct varid *var extern uint32_t CalcArgValue(char type, char ident, enum symbol_flags flags); extern char *ExpandArgValue(uint32_t, char *buffer, char tagidx); extern void ptrerror(int ident); -extern int needsub(void); extern TAG_SYMBOL *GetVarID(struct varid *var,enum storage_type storage); -extern void BitFieldSwallow(void); extern char *ExpandType(int type, char **sign, char tagidx); extern int get_type_size(int type, TAG_SYMBOL *otag); diff --git a/src/sccz80/expr.c b/src/sccz80/expr.c index e33f8fed5e..93d6474d33 100644 --- a/src/sccz80/expr.c +++ b/src/sccz80/expr.c @@ -192,7 +192,7 @@ int heir1a(LVALUE* lval) /* * Always evaluated as an integer, so fake it temporarily */ - force(CINT, lval->val_type, dosigned, lval->flags & UNSIGNED, 0); + force(CINT, lval->val_type, c_default_unsigned, lval->flags & UNSIGNED, 0); temptype = lval->val_type; lval->val_type = CINT; /* Force to integer */ testjump(lval, falselab = getlabel()); diff --git a/src/sccz80/main.c b/src/sccz80/main.c index 1e91acabdc..f5bec4b6bc 100644 --- a/src/sccz80/main.c +++ b/src/sccz80/main.c @@ -118,9 +118,8 @@ int main(int argc, char** argv) currfn = NULL; /* no function yet */ macptr = cmode = 1; /* clear macro pool and enable preprocessing */ - ncomp = doinline = mathz88 = incfloat = compactcode = 0; - cppcom = 0; - dosigned = NO; + ncomp = doinline = mathz88 = need_floatpack = compactcode = 0; + c_default_unsigned = NO; useshare = makeshare = sharedfile = NO; smartprintf = YES; nxtlab = /* start numbers at lowest possible */ @@ -383,7 +382,7 @@ void dumpfns() error(E_ZCCOPT); } - if (incfloat) { + if (need_floatpack) { fprintf(fp, "\nIF !NEED_floatpack\n"); fprintf(fp, "\tDEFINE\tNEED_floatpack\n"); fprintf(fp, "ENDIF\n\n"); @@ -755,7 +754,6 @@ struct args { struct args myargs[] = { { "math-z88", NO, SetMathZ88, "Enable machine native maths mode" }, { "unsigned", NO, SetUnsigned, "Make all types unsigned" }, - { "//", NO, SetCppComm, "Accept C++ style // comments" }, { "do-inline", NO, SetDoInline, "Inline certain common functions" }, { "stop-error", NO, SetStopError, "Stop when an error is received" }, { "make-shared", NO, SetMakeShared, "This Library file is shared" }, @@ -905,7 +903,7 @@ void SetMathZ88(char* arg) void SetUnsigned(char* arg) { - dosigned = YES; + c_default_unsigned = YES; } void SetNoWarn(char* arg) @@ -922,10 +920,6 @@ void SetAllWarn(char* arg) mywarn[i].suppress = 0; } -void SetCppComm(char* arg) -{ - cppcom = YES; -} void SetDoInline(char* arg) { @@ -1074,42 +1068,18 @@ void ParseArgs(char* arg) void MemCleanup() { - if (litq) { - free(litq); - } - if (dubq) { - free(dubq); - } - if (tempq) { - free(tempq); - } - if (glbq) { - free(glbq); - } - if (symtab) { - free(symtab); - } - if (loctab) { - free(loctab); - } - if (wqueue) { - free(wqueue); - } - if (tagtab) { - free(tagtab); - } - if (membtab) { - free(membtab); - } - if (swnext) { - free(swnext); - } - if (stage) { - free(stage); - } - if (gotoq) { - free(gotoq); - } + FREENULL(litq); + FREENULL(dubq); + FREENULL(tempq); + FREENULL(glbq); + FREENULL(symtab); + FREENULL(loctab); + FREENULL(wqueue); + FREENULL(tagtab); + FREENULL(membtab); + FREENULL(swnext); + FREENULL(stage); + FREENULL(gotoq); } void* mymalloc(size_t size) diff --git a/src/sccz80/main.h b/src/sccz80/main.h index 8e1710b000..a332d37dc5 100644 --- a/src/sccz80/main.h +++ b/src/sccz80/main.h @@ -20,7 +20,6 @@ extern void closeout(void); extern void SetNoWarn(char *arg); extern void SetMathZ88(char *arg); extern void SetUnsigned(char *arg); -extern void SetCppComm(char *arg); extern void SetMakeApp(char *arg); extern void SetDoInline(char *arg); extern void SetStopError(char *arg); diff --git a/src/sccz80/stmt.c b/src/sccz80/stmt.c index 51e3ccd4e2..04a6937289 100644 --- a/src/sccz80/stmt.c +++ b/src/sccz80/stmt.c @@ -486,11 +486,11 @@ void doreturn(char type) if (endst() == 0) { if (currfn->more) { /* return pointer to value */ - force(CINT, doexpr(), YES, dosigned, 0); + force(CINT, doexpr(), YES, c_default_unsigned, 0); leave(CINT, type); } else { /* return actual value */ - force(currfn->type, doexpr(), currfn->flags & UNSIGNED, dosigned, 0); + force(currfn->type, doexpr(), currfn->flags & UNSIGNED, c_default_unsigned, 0); leave(currfn->type, type); } } else From 2d276fb5ffadef7c8c0679386f24e13215975db2 Mon Sep 17 00:00:00 2001 From: suborb Date: Thu, 23 Feb 2017 22:41:00 +0000 Subject: [PATCH 11/16] Checkpoint more --- src/sccz80/const.c | 4 ++-- src/sccz80/declfunc.c | 6 +++--- src/sccz80/define.h | 10 ++++++---- src/sccz80/primary.c | 4 ++-- src/sccz80/sym.c | 2 +- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/sccz80/const.c b/src/sccz80/const.c index 66dd9bd26d..0ceeb7031d 100644 --- a/src/sccz80/const.c +++ b/src/sccz80/const.c @@ -58,7 +58,7 @@ int constant(LVALUE* lval) callrts("dload"); } lval->is_const = 0; /* floating point not constant */ - lval->flags = 0; + lval->flags = FLAGS_NONE; return (1); } else if (number(&lval->const_val) || pstr(&lval->const_val)) { /* Insert int32_t stuff/int32_t pointer here? */ @@ -76,7 +76,7 @@ int constant(LVALUE* lval) lval->is_const = 0; /* string address not constant */ lval->ptr_type = CCHAR; /* djm 9/3/99 */ lval->val_type = CINT; - lval->flags = 0; + lval->flags = FLAGS_NONE; immedlit(litlab); } else { lval->is_const = 0; diff --git a/src/sccz80/declfunc.c b/src/sccz80/declfunc.c index 2b398a8db5..74b828e748 100644 --- a/src/sccz80/declfunc.c +++ b/src/sccz80/declfunc.c @@ -266,7 +266,7 @@ void DoFnKR( /* any legal name bumps arg count */ if (symname(n)) { /* add link to argument chain */ - if ((cptr = addloc(n, 0, CINT, 0, 0))) + if ((cptr = addloc(n, NO_IDENT, CINT, 0, 0))) cptr->offset.p = prevarg; prevarg = cptr; ++undeclared; @@ -478,7 +478,7 @@ SYMBOL *dofnansi(SYMBOL* currfn, int32_t* addr) if (proto == 1) warning(W_ELLIP); needchar(')'); - argptr = addloc("ellp", 0, ELLIPSES, 0, 0); + argptr = addloc("ellp", NO_IDENT, ELLIPSES, 0, 0); argptr->offset.p = prevarg; prevarg = argptr; break; @@ -636,7 +636,7 @@ SYMBOL *getarg( * temporary */ if (deftype) { - argptr = addloc(n, 0, CINT, 0, 0); + argptr = addloc(n, NO_IDENT, CINT, 0, 0); argptr->offset.p = prevarg; } /* diff --git a/src/sccz80/define.h b/src/sccz80/define.h index 441b24ffba..187122790a 100644 --- a/src/sccz80/define.h +++ b/src/sccz80/define.h @@ -51,6 +51,7 @@ #define ENDLOC (STARTLOC+NUMLOC) enum ident_type { + NO_IDENT = 0, VARIABLE = 1, ARRAY, POINTER, @@ -82,6 +83,7 @@ enum storage_type { /* Symbol flags, | against each other */ enum symbol_flags { + FLAGS_NONE = 0, UNSIGNED = 1, FARPTR = 2, FARACC = 4, @@ -362,18 +364,18 @@ struct lvalue_s { char *stage_add ; /* stage addess of "oper 0" code, else 0 */ int val_type ; /* type of value calculated */ int oldval_type; /* What the valtype was */ - enum symbol_flags flags ; /* As per symbol */ + enum symbol_flags flags; /* As per symbol */ char oflags; /* Needed for deref of far str*/ int type; /* type (from symbol table) */ - int ident; /* ident (from symbol table) */ - enum storage_type storage; /* storage (from sym tab) */ + enum ident_type ident; /* ident (from symbol table) */ + enum storage_type storage; /* storage (from sym tab) */ char c_id; /* ident of cast */ char c_vtype; /* type of value calc if cast */ char c_flags; /* flags for casting */ int level; /* Parenth level (cast) */ int castlevel; int offset; - TAG_SYMBOL *c_tag; + TAG_SYMBOL *c_tag; } ; diff --git a/src/sccz80/primary.c b/src/sccz80/primary.c index ace2c3d687..06b21c8972 100644 --- a/src/sccz80/primary.c +++ b/src/sccz80/primary.c @@ -91,7 +91,7 @@ int primary(LVALUE* lval) lval->indirect = 0; lval->is_const = 1; lval->const_val = ptr->size; - lval->flags = 0; + lval->flags = FLAGS_NONE; lval->ident = VARIABLE; return (0); } @@ -143,7 +143,7 @@ int primary(LVALUE* lval) lval->symbol = ptr; lval->indirect = 0; lval->val_type = CINT; /* Null function, always int */ - lval->flags = 0; /* Assume signed, no far */ + lval->flags = FLAGS_NONE; /* Assume signed, no far */ lval->ident = FUNCTION; return (0); } diff --git a/src/sccz80/sym.c b/src/sccz80/sym.c index 05bba4ded5..1b1f5bee8c 100644 --- a/src/sccz80/sym.c +++ b/src/sccz80/sym.c @@ -215,5 +215,5 @@ static void initialise_sym( ptr->storage = storage; ptr->more = more; ptr->tag_idx = itag; - ptr->flags = 0; + ptr->flags = FLAGS_NONE; } From 35cad85cd6183eb06c4e4455f069801410499255 Mon Sep 17 00:00:00 2001 From: suborb Date: Thu, 23 Feb 2017 23:27:08 +0000 Subject: [PATCH 12/16] Use uthash for the global (extern, static, enum) symbol table. Allocate the objects as well so there should be no limit on the number of symbols anymore. auto variables + struct members are still statically allocated --- src/sccz80/data.c | 2 +- src/sccz80/data.h | 1 - src/sccz80/declvar.c | 10 +- src/sccz80/define.h | 7 +- src/sccz80/lib/uthash.h | 1074 +++++++++++++++++++++++++++++++++++++++ src/sccz80/main.c | 23 +- src/sccz80/preproc.c | 5 +- src/sccz80/sym.c | 54 +- 8 files changed, 1110 insertions(+), 66 deletions(-) create mode 100644 src/sccz80/lib/uthash.h diff --git a/src/sccz80/data.c b/src/sccz80/data.c index 98a961c391..29dd124eec 100644 --- a/src/sccz80/data.c +++ b/src/sccz80/data.c @@ -20,7 +20,7 @@ char Banner[] = "* * * * * Small-C/Plus z88dk * * * * *"; char Version[] = " Version: " Z88DK_VERSION; SYMBOL *symtab, *loctab; /* global and local symbol tables */ -SYMBOL *glbptr, *locptr; /* ptrs to next entries */ +SYMBOL *locptr; /* ptrs to next entries */ int glbcnt; /* number of globals used */ SYMBOL *dummy_sym[NTYPE + NUMTAG + 1]; diff --git a/src/sccz80/data.h b/src/sccz80/data.h index b2e64b566a..f5f511d5a6 100644 --- a/src/sccz80/data.h +++ b/src/sccz80/data.h @@ -6,7 +6,6 @@ extern char Version[]; extern char Overflow[]; extern SYMBOL *symtab; extern SYMBOL *loctab; -extern SYMBOL *glbptr; extern SYMBOL *locptr; extern int glbcnt; extern SYMBOL *dummy_sym[]; diff --git a/src/sccz80/declvar.c b/src/sccz80/declvar.c index 715998fe8b..88cd609869 100644 --- a/src/sccz80/declvar.c +++ b/src/sccz80/declvar.c @@ -823,9 +823,8 @@ TAG_SYMBOL* GetVarID(struct varid *var, enum storage_type storage) return (otag); } else { SYMBOL* ptr; - ptr = STARTGLB; - while (ptr < ENDGLB) { - if (ptr->name[0] && ptr->storage == TYPDEF && amatch(ptr->name)) { + for ( ptr = symtab; ptr != NULL; ptr = ptr->hh.next ) { + if (ptr->storage == TYPDEF && amatch(ptr->name)) { /* Found a typedef match */ var->sign = ptr->flags & UNSIGNED; var->zfar = ptr->flags & FARPTR; @@ -833,15 +832,14 @@ TAG_SYMBOL* GetVarID(struct varid *var, enum storage_type storage) if (var->type == STRUCT) return (tagtab + ptr->tag_idx); else - return (0); + return NULL; } - ++ptr; } } if (swallow("const")) { // warning(W_CONST); } - return (0); + return NULL; } static void swallow_bitfield(void) diff --git a/src/sccz80/define.h b/src/sccz80/define.h index 187122790a..4f6c67e32c 100644 --- a/src/sccz80/define.h +++ b/src/sccz80/define.h @@ -7,6 +7,8 @@ #ifndef DEFINE_H #define DEFINE_H + #include "lib/uthash.h" + #define MALLOC(x) mymalloc(x) #define CALLOC(x,y) mymalloc(x * y) @@ -38,9 +40,7 @@ /* Stefano - doubled the global symbol table size */ /* Aralbrec - doubled the global symbol table size again! */ -#define NUMGLBS 2048 -#define STARTGLB symtab -#define ENDGLB (STARTGLB+NUMGLBS) + #if defined(__MSDOS__) && defined(__TURBOC__) #define NUMLOC 33 @@ -128,6 +128,7 @@ struct symbol_s { bit 1 = far data/pointer bit 2 = access via far methods */ + UT_hash_handle hh; }; diff --git a/src/sccz80/lib/uthash.h b/src/sccz80/lib/uthash.h new file mode 100644 index 0000000000..775599c9a8 --- /dev/null +++ b/src/sccz80/lib/uthash.h @@ -0,0 +1,1074 @@ +/* +Copyright (c) 2003-2016, Troy D. Hanson http://troydhanson.github.com/uthash/ +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER +OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef UTHASH_H +#define UTHASH_H + +#define UTHASH_VERSION 2.0.1 + +#include /* memcmp,strlen */ +#include /* ptrdiff_t */ +#include /* exit() */ + +/* These macros use decltype or the earlier __typeof GNU extension. + As decltype is only available in newer compilers (VS2010 or gcc 4.3+ + when compiling c++ source) this code uses whatever method is needed + or, for VS2008 where neither is available, uses casting workarounds. */ +#if defined(_MSC_VER) /* MS compiler */ +#if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */ +#define DECLTYPE(x) (decltype(x)) +#else /* VS2008 or older (or VS2010 in C mode) */ +#define NO_DECLTYPE +#define DECLTYPE(x) +#endif +#elif defined(__BORLANDC__) || defined(__LCC__) || defined(__WATCOMC__) +#define NO_DECLTYPE +#define DECLTYPE(x) +#else /* GNU, Sun and other compilers */ +#define DECLTYPE(x) (__typeof(x)) +#endif + +#ifdef NO_DECLTYPE +#define DECLTYPE_ASSIGN(dst,src) \ +do { \ + char **_da_dst = (char**)(&(dst)); \ + *_da_dst = (char*)(src); \ +} while (0) +#else +#define DECLTYPE_ASSIGN(dst,src) \ +do { \ + (dst) = DECLTYPE(dst)(src); \ +} while (0) +#endif + +/* a number of the hash function use uint32_t which isn't defined on Pre VS2010 */ +#if defined(_WIN32) +#if defined(_MSC_VER) && _MSC_VER >= 1600 +#include +#elif defined(__WATCOMC__) || defined(__MINGW32__) || defined(__CYGWIN__) +#include +#else +typedef unsigned int uint32_t; +typedef unsigned char uint8_t; +#endif +#elif defined(__GNUC__) && !defined(__VXWORKS__) +#include +#else +typedef unsigned int uint32_t; +typedef unsigned char uint8_t; +#endif + +#ifndef uthash_fatal +#define uthash_fatal(msg) exit(-1) /* fatal error (out of memory,etc) */ +#endif +#ifndef uthash_malloc +#define uthash_malloc(sz) malloc(sz) /* malloc fcn */ +#endif +#ifndef uthash_free +#define uthash_free(ptr,sz) free(ptr) /* free fcn */ +#endif +#ifndef uthash_strlen +#define uthash_strlen(s) strlen(s) +#endif +#ifndef uthash_memcmp +#define uthash_memcmp(a,b,n) memcmp(a,b,n) +#endif + +#ifndef uthash_noexpand_fyi +#define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */ +#endif +#ifndef uthash_expand_fyi +#define uthash_expand_fyi(tbl) /* can be defined to log expands */ +#endif + +/* initial number of buckets */ +#define HASH_INITIAL_NUM_BUCKETS 32U /* initial number of buckets */ +#define HASH_INITIAL_NUM_BUCKETS_LOG2 5U /* lg2 of initial number of buckets */ +#define HASH_BKT_CAPACITY_THRESH 10U /* expand when bucket count reaches */ + +/* calculate the element whose hash handle address is hhp */ +#define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)(hhp)) - ((tbl)->hho))) +/* calculate the hash handle from element address elp */ +#define HH_FROM_ELMT(tbl,elp) ((UT_hash_handle *)(((char*)(elp)) + ((tbl)->hho))) + +#define HASH_VALUE(keyptr,keylen,hashv) \ +do { \ + HASH_FCN(keyptr, keylen, hashv); \ +} while (0) + +#define HASH_FIND_BYHASHVALUE(hh,head,keyptr,keylen,hashval,out) \ +do { \ + (out) = NULL; \ + if (head) { \ + unsigned _hf_bkt; \ + HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _hf_bkt); \ + if (HASH_BLOOM_TEST((head)->hh.tbl, hashval) != 0) { \ + HASH_FIND_IN_BKT((head)->hh.tbl, hh, (head)->hh.tbl->buckets[ _hf_bkt ], keyptr, keylen, hashval, out); \ + } \ + } \ +} while (0) + +#define HASH_FIND(hh,head,keyptr,keylen,out) \ +do { \ + unsigned _hf_hashv; \ + HASH_VALUE(keyptr, keylen, _hf_hashv); \ + HASH_FIND_BYHASHVALUE(hh, head, keyptr, keylen, _hf_hashv, out); \ +} while (0) + +#ifdef HASH_BLOOM +#define HASH_BLOOM_BITLEN (1UL << HASH_BLOOM) +#define HASH_BLOOM_BYTELEN (HASH_BLOOM_BITLEN/8UL) + (((HASH_BLOOM_BITLEN%8UL)!=0UL) ? 1UL : 0UL) +#define HASH_BLOOM_MAKE(tbl) \ +do { \ + (tbl)->bloom_nbits = HASH_BLOOM; \ + (tbl)->bloom_bv = (uint8_t*)uthash_malloc(HASH_BLOOM_BYTELEN); \ + if (!((tbl)->bloom_bv)) { uthash_fatal( "out of memory"); } \ + memset((tbl)->bloom_bv, 0, HASH_BLOOM_BYTELEN); \ + (tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \ +} while (0) + +#define HASH_BLOOM_FREE(tbl) \ +do { \ + uthash_free((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \ +} while (0) + +#define HASH_BLOOM_BITSET(bv,idx) (bv[(idx)/8U] |= (1U << ((idx)%8U))) +#define HASH_BLOOM_BITTEST(bv,idx) (bv[(idx)/8U] & (1U << ((idx)%8U))) + +#define HASH_BLOOM_ADD(tbl,hashv) \ + HASH_BLOOM_BITSET((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1U))) + +#define HASH_BLOOM_TEST(tbl,hashv) \ + HASH_BLOOM_BITTEST((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1U))) + +#else +#define HASH_BLOOM_MAKE(tbl) +#define HASH_BLOOM_FREE(tbl) +#define HASH_BLOOM_ADD(tbl,hashv) +#define HASH_BLOOM_TEST(tbl,hashv) (1) +#define HASH_BLOOM_BYTELEN 0U +#endif + +#define HASH_MAKE_TABLE(hh,head) \ +do { \ + (head)->hh.tbl = (UT_hash_table*)uthash_malloc( \ + sizeof(UT_hash_table)); \ + if (!((head)->hh.tbl)) { uthash_fatal( "out of memory"); } \ + memset((head)->hh.tbl, 0, sizeof(UT_hash_table)); \ + (head)->hh.tbl->tail = &((head)->hh); \ + (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \ + (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \ + (head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \ + (head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_malloc( \ + HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \ + if (! (head)->hh.tbl->buckets) { uthash_fatal( "out of memory"); } \ + memset((head)->hh.tbl->buckets, 0, \ + HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \ + HASH_BLOOM_MAKE((head)->hh.tbl); \ + (head)->hh.tbl->signature = HASH_SIGNATURE; \ +} while (0) + +#define HASH_REPLACE_BYHASHVALUE_INORDER(hh,head,fieldname,keylen_in,hashval,add,replaced,cmpfcn) \ +do { \ + (replaced) = NULL; \ + HASH_FIND_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, replaced); \ + if (replaced) { \ + HASH_DELETE(hh, head, replaced); \ + } \ + HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, head, &((add)->fieldname), keylen_in, hashval, add, cmpfcn); \ +} while (0) + +#define HASH_REPLACE_BYHASHVALUE(hh,head,fieldname,keylen_in,hashval,add,replaced) \ +do { \ + (replaced) = NULL; \ + HASH_FIND_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, replaced); \ + if (replaced) { \ + HASH_DELETE(hh, head, replaced); \ + } \ + HASH_ADD_KEYPTR_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, add); \ +} while (0) + +#define HASH_REPLACE(hh,head,fieldname,keylen_in,add,replaced) \ +do { \ + unsigned _hr_hashv; \ + HASH_VALUE(&((add)->fieldname), keylen_in, _hr_hashv); \ + HASH_REPLACE_BYHASHVALUE(hh, head, fieldname, keylen_in, _hr_hashv, add, replaced); \ +} while (0) + +#define HASH_REPLACE_INORDER(hh,head,fieldname,keylen_in,add,replaced,cmpfcn) \ +do { \ + unsigned _hr_hashv; \ + HASH_VALUE(&((add)->fieldname), keylen_in, _hr_hashv); \ + HASH_REPLACE_BYHASHVALUE_INORDER(hh, head, fieldname, keylen_in, _hr_hashv, add, replaced, cmpfcn); \ +} while (0) + +#define HASH_APPEND_LIST(hh, head, add) \ +do { \ + (add)->hh.next = NULL; \ + (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \ + (head)->hh.tbl->tail->next = (add); \ + (head)->hh.tbl->tail = &((add)->hh); \ +} while (0) + +#define HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh,head,keyptr,keylen_in,hashval,add,cmpfcn) \ +do { \ + unsigned _ha_bkt; \ + (add)->hh.hashv = (hashval); \ + (add)->hh.key = (char*) (keyptr); \ + (add)->hh.keylen = (unsigned) (keylen_in); \ + if (!(head)) { \ + (add)->hh.next = NULL; \ + (add)->hh.prev = NULL; \ + (head) = (add); \ + HASH_MAKE_TABLE(hh, head); \ + } else { \ + void *_hs_iter = (head); \ + (add)->hh.tbl = (head)->hh.tbl; \ + do { \ + if (cmpfcn(DECLTYPE(head)(_hs_iter), add) > 0) \ + break; \ + } while ((_hs_iter = HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->next)); \ + if (_hs_iter) { \ + (add)->hh.next = _hs_iter; \ + if (((add)->hh.prev = HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->prev)) { \ + HH_FROM_ELMT((head)->hh.tbl, (add)->hh.prev)->next = (add); \ + } else { \ + (head) = (add); \ + } \ + HH_FROM_ELMT((head)->hh.tbl, _hs_iter)->prev = (add); \ + } else { \ + HASH_APPEND_LIST(hh, head, add); \ + } \ + } \ + (head)->hh.tbl->num_items++; \ + HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _ha_bkt); \ + HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt], &(add)->hh); \ + HASH_BLOOM_ADD((head)->hh.tbl, hashval); \ + HASH_EMIT_KEY(hh, head, keyptr, keylen_in); \ + HASH_FSCK(hh, head); \ +} while (0) + +#define HASH_ADD_KEYPTR_INORDER(hh,head,keyptr,keylen_in,add,cmpfcn) \ +do { \ + unsigned _hs_hashv; \ + HASH_VALUE(keyptr, keylen_in, _hs_hashv); \ + HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, head, keyptr, keylen_in, _hs_hashv, add, cmpfcn); \ +} while (0) + +#define HASH_ADD_BYHASHVALUE_INORDER(hh,head,fieldname,keylen_in,hashval,add,cmpfcn) \ + HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, head, &((add)->fieldname), keylen_in, hashval, add, cmpfcn) + +#define HASH_ADD_INORDER(hh,head,fieldname,keylen_in,add,cmpfcn) \ + HASH_ADD_KEYPTR_INORDER(hh, head, &((add)->fieldname), keylen_in, add, cmpfcn) + +#define HASH_ADD_KEYPTR_BYHASHVALUE(hh,head,keyptr,keylen_in,hashval,add) \ +do { \ + unsigned _ha_bkt; \ + (add)->hh.hashv = (hashval); \ + (add)->hh.key = (char*) (keyptr); \ + (add)->hh.keylen = (unsigned) (keylen_in); \ + if (!(head)) { \ + (add)->hh.next = NULL; \ + (add)->hh.prev = NULL; \ + (head) = (add); \ + HASH_MAKE_TABLE(hh, head); \ + } else { \ + (add)->hh.tbl = (head)->hh.tbl; \ + HASH_APPEND_LIST(hh, head, add); \ + } \ + (head)->hh.tbl->num_items++; \ + HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _ha_bkt); \ + HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt], &(add)->hh); \ + HASH_BLOOM_ADD((head)->hh.tbl, hashval); \ + HASH_EMIT_KEY(hh, head, keyptr, keylen_in); \ + HASH_FSCK(hh, head); \ +} while (0) + +#define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \ +do { \ + unsigned _ha_hashv; \ + HASH_VALUE(keyptr, keylen_in, _ha_hashv); \ + HASH_ADD_KEYPTR_BYHASHVALUE(hh, head, keyptr, keylen_in, _ha_hashv, add); \ +} while (0) + +#define HASH_ADD_BYHASHVALUE(hh,head,fieldname,keylen_in,hashval,add) \ + HASH_ADD_KEYPTR_BYHASHVALUE(hh, head, &((add)->fieldname), keylen_in, hashval, add) + +#define HASH_ADD(hh,head,fieldname,keylen_in,add) \ + HASH_ADD_KEYPTR(hh, head, &((add)->fieldname), keylen_in, add) + +#define HASH_TO_BKT(hashv,num_bkts,bkt) \ +do { \ + bkt = ((hashv) & ((num_bkts) - 1U)); \ +} while (0) + +/* delete "delptr" from the hash table. + * "the usual" patch-up process for the app-order doubly-linked-list. + * The use of _hd_hh_del below deserves special explanation. + * These used to be expressed using (delptr) but that led to a bug + * if someone used the same symbol for the head and deletee, like + * HASH_DELETE(hh,users,users); + * We want that to work, but by changing the head (users) below + * we were forfeiting our ability to further refer to the deletee (users) + * in the patch-up process. Solution: use scratch space to + * copy the deletee pointer, then the latter references are via that + * scratch pointer rather than through the repointed (users) symbol. + */ +#define HASH_DELETE(hh,head,delptr) \ +do { \ + struct UT_hash_handle *_hd_hh_del; \ + if ( ((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL) ) { \ + uthash_free((head)->hh.tbl->buckets, \ + (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket) ); \ + HASH_BLOOM_FREE((head)->hh.tbl); \ + uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ + head = NULL; \ + } else { \ + unsigned _hd_bkt; \ + _hd_hh_del = &((delptr)->hh); \ + if ((delptr) == ELMT_FROM_HH((head)->hh.tbl,(head)->hh.tbl->tail)) { \ + (head)->hh.tbl->tail = \ + (UT_hash_handle*)((ptrdiff_t)((delptr)->hh.prev) + \ + (head)->hh.tbl->hho); \ + } \ + if ((delptr)->hh.prev != NULL) { \ + ((UT_hash_handle*)((ptrdiff_t)((delptr)->hh.prev) + \ + (head)->hh.tbl->hho))->next = (delptr)->hh.next; \ + } else { \ + DECLTYPE_ASSIGN(head,(delptr)->hh.next); \ + } \ + if (_hd_hh_del->next != NULL) { \ + ((UT_hash_handle*)((ptrdiff_t)_hd_hh_del->next + \ + (head)->hh.tbl->hho))->prev = \ + _hd_hh_del->prev; \ + } \ + HASH_TO_BKT( _hd_hh_del->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \ + HASH_DEL_IN_BKT(hh,(head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \ + (head)->hh.tbl->num_items--; \ + } \ + HASH_FSCK(hh,head); \ +} while (0) + + +/* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */ +#define HASH_FIND_STR(head,findstr,out) \ + HASH_FIND(hh,head,findstr,(unsigned)uthash_strlen(findstr),out) +#define HASH_ADD_STR(head,strfield,add) \ + HASH_ADD(hh,head,strfield[0],(unsigned)uthash_strlen(add->strfield),add) +#define HASH_REPLACE_STR(head,strfield,add,replaced) \ + HASH_REPLACE(hh,head,strfield[0],(unsigned)uthash_strlen(add->strfield),add,replaced) +#define HASH_FIND_INT(head,findint,out) \ + HASH_FIND(hh,head,findint,sizeof(int),out) +#define HASH_ADD_INT(head,intfield,add) \ + HASH_ADD(hh,head,intfield,sizeof(int),add) +#define HASH_REPLACE_INT(head,intfield,add,replaced) \ + HASH_REPLACE(hh,head,intfield,sizeof(int),add,replaced) +#define HASH_FIND_PTR(head,findptr,out) \ + HASH_FIND(hh,head,findptr,sizeof(void *),out) +#define HASH_ADD_PTR(head,ptrfield,add) \ + HASH_ADD(hh,head,ptrfield,sizeof(void *),add) +#define HASH_REPLACE_PTR(head,ptrfield,add,replaced) \ + HASH_REPLACE(hh,head,ptrfield,sizeof(void *),add,replaced) +#define HASH_DEL(head,delptr) \ + HASH_DELETE(hh,head,delptr) + +/* HASH_FSCK checks hash integrity on every add/delete when HASH_DEBUG is defined. + * This is for uthash developer only; it compiles away if HASH_DEBUG isn't defined. + */ +#ifdef HASH_DEBUG +#define HASH_OOPS(...) do { fprintf(stderr,__VA_ARGS__); exit(-1); } while (0) +#define HASH_FSCK(hh,head) \ +do { \ + struct UT_hash_handle *_thh; \ + if (head) { \ + unsigned _bkt_i; \ + unsigned _count; \ + char *_prev; \ + _count = 0; \ + for( _bkt_i = 0; _bkt_i < (head)->hh.tbl->num_buckets; _bkt_i++) { \ + unsigned _bkt_count = 0; \ + _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \ + _prev = NULL; \ + while (_thh) { \ + if (_prev != (char*)(_thh->hh_prev)) { \ + HASH_OOPS("invalid hh_prev %p, actual %p\n", \ + _thh->hh_prev, _prev ); \ + } \ + _bkt_count++; \ + _prev = (char*)(_thh); \ + _thh = _thh->hh_next; \ + } \ + _count += _bkt_count; \ + if ((head)->hh.tbl->buckets[_bkt_i].count != _bkt_count) { \ + HASH_OOPS("invalid bucket count %u, actual %u\n", \ + (head)->hh.tbl->buckets[_bkt_i].count, _bkt_count); \ + } \ + } \ + if (_count != (head)->hh.tbl->num_items) { \ + HASH_OOPS("invalid hh item count %u, actual %u\n", \ + (head)->hh.tbl->num_items, _count ); \ + } \ + /* traverse hh in app order; check next/prev integrity, count */ \ + _count = 0; \ + _prev = NULL; \ + _thh = &(head)->hh; \ + while (_thh) { \ + _count++; \ + if (_prev !=(char*)(_thh->prev)) { \ + HASH_OOPS("invalid prev %p, actual %p\n", \ + _thh->prev, _prev ); \ + } \ + _prev = (char*)ELMT_FROM_HH((head)->hh.tbl, _thh); \ + _thh = ( _thh->next ? (UT_hash_handle*)((char*)(_thh->next) + \ + (head)->hh.tbl->hho) : NULL ); \ + } \ + if (_count != (head)->hh.tbl->num_items) { \ + HASH_OOPS("invalid app item count %u, actual %u\n", \ + (head)->hh.tbl->num_items, _count ); \ + } \ + } \ +} while (0) +#else +#define HASH_FSCK(hh,head) +#endif + +/* When compiled with -DHASH_EMIT_KEYS, length-prefixed keys are emitted to + * the descriptor to which this macro is defined for tuning the hash function. + * The app can #include to get the prototype for write(2). */ +#ifdef HASH_EMIT_KEYS +#define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) \ +do { \ + unsigned _klen = fieldlen; \ + write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \ + write(HASH_EMIT_KEYS, keyptr, (unsigned long)fieldlen); \ +} while (0) +#else +#define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) +#endif + +/* default to Jenkin's hash unless overridden e.g. DHASH_FUNCTION=HASH_SAX */ +#ifdef HASH_FUNCTION +#define HASH_FCN HASH_FUNCTION +#else +#define HASH_FCN HASH_JEN +#endif + +/* The Bernstein hash function, used in Perl prior to v5.6. Note (x<<5+x)=x*33. */ +#define HASH_BER(key,keylen,hashv) \ +do { \ + unsigned _hb_keylen=(unsigned)keylen; \ + const unsigned char *_hb_key=(const unsigned char*)(key); \ + (hashv) = 0; \ + while (_hb_keylen-- != 0U) { \ + (hashv) = (((hashv) << 5) + (hashv)) + *_hb_key++; \ + } \ +} while (0) + + +/* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at + * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */ +#define HASH_SAX(key,keylen,hashv) \ +do { \ + unsigned _sx_i; \ + const unsigned char *_hs_key=(const unsigned char*)(key); \ + hashv = 0; \ + for(_sx_i=0; _sx_i < keylen; _sx_i++) { \ + hashv ^= (hashv << 5) + (hashv >> 2) + _hs_key[_sx_i]; \ + } \ +} while (0) +/* FNV-1a variation */ +#define HASH_FNV(key,keylen,hashv) \ +do { \ + unsigned _fn_i; \ + const unsigned char *_hf_key=(const unsigned char*)(key); \ + hashv = 2166136261U; \ + for(_fn_i=0; _fn_i < keylen; _fn_i++) { \ + hashv = hashv ^ _hf_key[_fn_i]; \ + hashv = hashv * 16777619U; \ + } \ +} while (0) + +#define HASH_OAT(key,keylen,hashv) \ +do { \ + unsigned _ho_i; \ + const unsigned char *_ho_key=(const unsigned char*)(key); \ + hashv = 0; \ + for(_ho_i=0; _ho_i < keylen; _ho_i++) { \ + hashv += _ho_key[_ho_i]; \ + hashv += (hashv << 10); \ + hashv ^= (hashv >> 6); \ + } \ + hashv += (hashv << 3); \ + hashv ^= (hashv >> 11); \ + hashv += (hashv << 15); \ +} while (0) + +#define HASH_JEN_MIX(a,b,c) \ +do { \ + a -= b; a -= c; a ^= ( c >> 13 ); \ + b -= c; b -= a; b ^= ( a << 8 ); \ + c -= a; c -= b; c ^= ( b >> 13 ); \ + a -= b; a -= c; a ^= ( c >> 12 ); \ + b -= c; b -= a; b ^= ( a << 16 ); \ + c -= a; c -= b; c ^= ( b >> 5 ); \ + a -= b; a -= c; a ^= ( c >> 3 ); \ + b -= c; b -= a; b ^= ( a << 10 ); \ + c -= a; c -= b; c ^= ( b >> 15 ); \ +} while (0) + +#define HASH_JEN(key,keylen,hashv) \ +do { \ + unsigned _hj_i,_hj_j,_hj_k; \ + unsigned const char *_hj_key=(unsigned const char*)(key); \ + hashv = 0xfeedbeefu; \ + _hj_i = _hj_j = 0x9e3779b9u; \ + _hj_k = (unsigned)(keylen); \ + while (_hj_k >= 12U) { \ + _hj_i += (_hj_key[0] + ( (unsigned)_hj_key[1] << 8 ) \ + + ( (unsigned)_hj_key[2] << 16 ) \ + + ( (unsigned)_hj_key[3] << 24 ) ); \ + _hj_j += (_hj_key[4] + ( (unsigned)_hj_key[5] << 8 ) \ + + ( (unsigned)_hj_key[6] << 16 ) \ + + ( (unsigned)_hj_key[7] << 24 ) ); \ + hashv += (_hj_key[8] + ( (unsigned)_hj_key[9] << 8 ) \ + + ( (unsigned)_hj_key[10] << 16 ) \ + + ( (unsigned)_hj_key[11] << 24 ) ); \ + \ + HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ + \ + _hj_key += 12; \ + _hj_k -= 12U; \ + } \ + hashv += (unsigned)(keylen); \ + switch ( _hj_k ) { \ + case 11: hashv += ( (unsigned)_hj_key[10] << 24 ); /* FALLTHROUGH */ \ + case 10: hashv += ( (unsigned)_hj_key[9] << 16 ); /* FALLTHROUGH */ \ + case 9: hashv += ( (unsigned)_hj_key[8] << 8 ); /* FALLTHROUGH */ \ + case 8: _hj_j += ( (unsigned)_hj_key[7] << 24 ); /* FALLTHROUGH */ \ + case 7: _hj_j += ( (unsigned)_hj_key[6] << 16 ); /* FALLTHROUGH */ \ + case 6: _hj_j += ( (unsigned)_hj_key[5] << 8 ); /* FALLTHROUGH */ \ + case 5: _hj_j += _hj_key[4]; /* FALLTHROUGH */ \ + case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); /* FALLTHROUGH */ \ + case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); /* FALLTHROUGH */ \ + case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); /* FALLTHROUGH */ \ + case 1: _hj_i += _hj_key[0]; \ + } \ + HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ +} while (0) + +/* The Paul Hsieh hash function */ +#undef get16bits +#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ + || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__) +#define get16bits(d) (*((const uint16_t *) (d))) +#endif + +#if !defined (get16bits) +#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) \ + +(uint32_t)(((const uint8_t *)(d))[0]) ) +#endif +#define HASH_SFH(key,keylen,hashv) \ +do { \ + unsigned const char *_sfh_key=(unsigned const char*)(key); \ + uint32_t _sfh_tmp, _sfh_len = (uint32_t)keylen; \ + \ + unsigned _sfh_rem = _sfh_len & 3U; \ + _sfh_len >>= 2; \ + hashv = 0xcafebabeu; \ + \ + /* Main loop */ \ + for (;_sfh_len > 0U; _sfh_len--) { \ + hashv += get16bits (_sfh_key); \ + _sfh_tmp = ((uint32_t)(get16bits (_sfh_key+2)) << 11) ^ hashv; \ + hashv = (hashv << 16) ^ _sfh_tmp; \ + _sfh_key += 2U*sizeof (uint16_t); \ + hashv += hashv >> 11; \ + } \ + \ + /* Handle end cases */ \ + switch (_sfh_rem) { \ + case 3: hashv += get16bits (_sfh_key); \ + hashv ^= hashv << 16; \ + hashv ^= (uint32_t)(_sfh_key[sizeof (uint16_t)]) << 18; \ + hashv += hashv >> 11; \ + break; \ + case 2: hashv += get16bits (_sfh_key); \ + hashv ^= hashv << 11; \ + hashv += hashv >> 17; \ + break; \ + case 1: hashv += *_sfh_key; \ + hashv ^= hashv << 10; \ + hashv += hashv >> 1; \ + } \ + \ + /* Force "avalanching" of final 127 bits */ \ + hashv ^= hashv << 3; \ + hashv += hashv >> 5; \ + hashv ^= hashv << 4; \ + hashv += hashv >> 17; \ + hashv ^= hashv << 25; \ + hashv += hashv >> 6; \ +} while (0) + +#ifdef HASH_USING_NO_STRICT_ALIASING +/* The MurmurHash exploits some CPU's (x86,x86_64) tolerance for unaligned reads. + * For other types of CPU's (e.g. Sparc) an unaligned read causes a bus error. + * MurmurHash uses the faster approach only on CPU's where we know it's safe. + * + * Note the preprocessor built-in defines can be emitted using: + * + * gcc -m64 -dM -E - < /dev/null (on gcc) + * cc -## a.c (where a.c is a simple test file) (Sun Studio) + */ +#if (defined(__i386__) || defined(__x86_64__) || defined(_M_IX86)) +#define MUR_GETBLOCK(p,i) p[i] +#else /* non intel */ +#define MUR_PLUS0_ALIGNED(p) (((unsigned long)p & 3UL) == 0UL) +#define MUR_PLUS1_ALIGNED(p) (((unsigned long)p & 3UL) == 1UL) +#define MUR_PLUS2_ALIGNED(p) (((unsigned long)p & 3UL) == 2UL) +#define MUR_PLUS3_ALIGNED(p) (((unsigned long)p & 3UL) == 3UL) +#define WP(p) ((uint32_t*)((unsigned long)(p) & ~3UL)) +#if (defined(__BIG_ENDIAN__) || defined(SPARC) || defined(__ppc__) || defined(__ppc64__)) +#define MUR_THREE_ONE(p) ((((*WP(p))&0x00ffffff) << 8) | (((*(WP(p)+1))&0xff000000) >> 24)) +#define MUR_TWO_TWO(p) ((((*WP(p))&0x0000ffff) <<16) | (((*(WP(p)+1))&0xffff0000) >> 16)) +#define MUR_ONE_THREE(p) ((((*WP(p))&0x000000ff) <<24) | (((*(WP(p)+1))&0xffffff00) >> 8)) +#else /* assume little endian non-intel */ +#define MUR_THREE_ONE(p) ((((*WP(p))&0xffffff00) >> 8) | (((*(WP(p)+1))&0x000000ff) << 24)) +#define MUR_TWO_TWO(p) ((((*WP(p))&0xffff0000) >>16) | (((*(WP(p)+1))&0x0000ffff) << 16)) +#define MUR_ONE_THREE(p) ((((*WP(p))&0xff000000) >>24) | (((*(WP(p)+1))&0x00ffffff) << 8)) +#endif +#define MUR_GETBLOCK(p,i) (MUR_PLUS0_ALIGNED(p) ? ((p)[i]) : \ + (MUR_PLUS1_ALIGNED(p) ? MUR_THREE_ONE(p) : \ + (MUR_PLUS2_ALIGNED(p) ? MUR_TWO_TWO(p) : \ + MUR_ONE_THREE(p)))) +#endif +#define MUR_ROTL32(x,r) (((x) << (r)) | ((x) >> (32 - (r)))) +#define MUR_FMIX(_h) \ +do { \ + _h ^= _h >> 16; \ + _h *= 0x85ebca6bu; \ + _h ^= _h >> 13; \ + _h *= 0xc2b2ae35u; \ + _h ^= _h >> 16; \ +} while (0) + +#define HASH_MUR(key,keylen,hashv) \ +do { \ + const uint8_t *_mur_data = (const uint8_t*)(key); \ + const int _mur_nblocks = (int)(keylen) / 4; \ + uint32_t _mur_h1 = 0xf88D5353u; \ + uint32_t _mur_c1 = 0xcc9e2d51u; \ + uint32_t _mur_c2 = 0x1b873593u; \ + uint32_t _mur_k1 = 0; \ + const uint8_t *_mur_tail; \ + const uint32_t *_mur_blocks = (const uint32_t*)(_mur_data+(_mur_nblocks*4)); \ + int _mur_i; \ + for(_mur_i = -_mur_nblocks; _mur_i!=0; _mur_i++) { \ + _mur_k1 = MUR_GETBLOCK(_mur_blocks,_mur_i); \ + _mur_k1 *= _mur_c1; \ + _mur_k1 = MUR_ROTL32(_mur_k1,15); \ + _mur_k1 *= _mur_c2; \ + \ + _mur_h1 ^= _mur_k1; \ + _mur_h1 = MUR_ROTL32(_mur_h1,13); \ + _mur_h1 = (_mur_h1*5U) + 0xe6546b64u; \ + } \ + _mur_tail = (const uint8_t*)(_mur_data + (_mur_nblocks*4)); \ + _mur_k1=0; \ + switch((keylen) & 3U) { \ + case 3: _mur_k1 ^= (uint32_t)_mur_tail[2] << 16; /* FALLTHROUGH */ \ + case 2: _mur_k1 ^= (uint32_t)_mur_tail[1] << 8; /* FALLTHROUGH */ \ + case 1: _mur_k1 ^= (uint32_t)_mur_tail[0]; \ + _mur_k1 *= _mur_c1; \ + _mur_k1 = MUR_ROTL32(_mur_k1,15); \ + _mur_k1 *= _mur_c2; \ + _mur_h1 ^= _mur_k1; \ + } \ + _mur_h1 ^= (uint32_t)(keylen); \ + MUR_FMIX(_mur_h1); \ + hashv = _mur_h1; \ +} while (0) +#endif /* HASH_USING_NO_STRICT_ALIASING */ + +/* iterate over items in a known bucket to find desired item */ +#define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,hashval,out) \ +do { \ + if ((head).hh_head != NULL) { \ + DECLTYPE_ASSIGN(out, ELMT_FROM_HH(tbl, (head).hh_head)); \ + } else { \ + (out) = NULL; \ + } \ + while ((out) != NULL) { \ + if ((out)->hh.hashv == (hashval) && (out)->hh.keylen == (keylen_in)) { \ + if (uthash_memcmp((out)->hh.key, keyptr, keylen_in) == 0) { \ + break; \ + } \ + } \ + if ((out)->hh.hh_next != NULL) { \ + DECLTYPE_ASSIGN(out, ELMT_FROM_HH(tbl, (out)->hh.hh_next)); \ + } else { \ + (out) = NULL; \ + } \ + } \ +} while (0) + +/* add an item to a bucket */ +#define HASH_ADD_TO_BKT(head,addhh) \ +do { \ + head.count++; \ + (addhh)->hh_next = head.hh_head; \ + (addhh)->hh_prev = NULL; \ + if (head.hh_head != NULL) { (head).hh_head->hh_prev = (addhh); } \ + (head).hh_head=addhh; \ + if ((head.count >= ((head.expand_mult+1U) * HASH_BKT_CAPACITY_THRESH)) \ + && ((addhh)->tbl->noexpand != 1U)) { \ + HASH_EXPAND_BUCKETS((addhh)->tbl); \ + } \ +} while (0) + +/* remove an item from a given bucket */ +#define HASH_DEL_IN_BKT(hh,head,hh_del) \ + (head).count--; \ + if ((head).hh_head == hh_del) { \ + (head).hh_head = hh_del->hh_next; \ + } \ + if (hh_del->hh_prev) { \ + hh_del->hh_prev->hh_next = hh_del->hh_next; \ + } \ + if (hh_del->hh_next) { \ + hh_del->hh_next->hh_prev = hh_del->hh_prev; \ + } + +/* Bucket expansion has the effect of doubling the number of buckets + * and redistributing the items into the new buckets. Ideally the + * items will distribute more or less evenly into the new buckets + * (the extent to which this is true is a measure of the quality of + * the hash function as it applies to the key domain). + * + * With the items distributed into more buckets, the chain length + * (item count) in each bucket is reduced. Thus by expanding buckets + * the hash keeps a bound on the chain length. This bounded chain + * length is the essence of how a hash provides constant time lookup. + * + * The calculation of tbl->ideal_chain_maxlen below deserves some + * explanation. First, keep in mind that we're calculating the ideal + * maximum chain length based on the *new* (doubled) bucket count. + * In fractions this is just n/b (n=number of items,b=new num buckets). + * Since the ideal chain length is an integer, we want to calculate + * ceil(n/b). We don't depend on floating point arithmetic in this + * hash, so to calculate ceil(n/b) with integers we could write + * + * ceil(n/b) = (n/b) + ((n%b)?1:0) + * + * and in fact a previous version of this hash did just that. + * But now we have improved things a bit by recognizing that b is + * always a power of two. We keep its base 2 log handy (call it lb), + * so now we can write this with a bit shift and logical AND: + * + * ceil(n/b) = (n>>lb) + ( (n & (b-1)) ? 1:0) + * + */ +#define HASH_EXPAND_BUCKETS(tbl) \ +do { \ + unsigned _he_bkt; \ + unsigned _he_bkt_i; \ + struct UT_hash_handle *_he_thh, *_he_hh_nxt; \ + UT_hash_bucket *_he_new_buckets, *_he_newbkt; \ + _he_new_buckets = (UT_hash_bucket*)uthash_malloc( \ + 2UL * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \ + if (!_he_new_buckets) { uthash_fatal( "out of memory"); } \ + memset(_he_new_buckets, 0, \ + 2UL * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \ + tbl->ideal_chain_maxlen = \ + (tbl->num_items >> (tbl->log2_num_buckets+1U)) + \ + (((tbl->num_items & ((tbl->num_buckets*2U)-1U)) != 0U) ? 1U : 0U); \ + tbl->nonideal_items = 0; \ + for(_he_bkt_i = 0; _he_bkt_i < tbl->num_buckets; _he_bkt_i++) \ + { \ + _he_thh = tbl->buckets[ _he_bkt_i ].hh_head; \ + while (_he_thh != NULL) { \ + _he_hh_nxt = _he_thh->hh_next; \ + HASH_TO_BKT( _he_thh->hashv, tbl->num_buckets*2U, _he_bkt); \ + _he_newbkt = &(_he_new_buckets[ _he_bkt ]); \ + if (++(_he_newbkt->count) > tbl->ideal_chain_maxlen) { \ + tbl->nonideal_items++; \ + _he_newbkt->expand_mult = _he_newbkt->count / \ + tbl->ideal_chain_maxlen; \ + } \ + _he_thh->hh_prev = NULL; \ + _he_thh->hh_next = _he_newbkt->hh_head; \ + if (_he_newbkt->hh_head != NULL) { _he_newbkt->hh_head->hh_prev = \ + _he_thh; } \ + _he_newbkt->hh_head = _he_thh; \ + _he_thh = _he_hh_nxt; \ + } \ + } \ + uthash_free( tbl->buckets, tbl->num_buckets*sizeof(struct UT_hash_bucket) ); \ + tbl->num_buckets *= 2U; \ + tbl->log2_num_buckets++; \ + tbl->buckets = _he_new_buckets; \ + tbl->ineff_expands = (tbl->nonideal_items > (tbl->num_items >> 1)) ? \ + (tbl->ineff_expands+1U) : 0U; \ + if (tbl->ineff_expands > 1U) { \ + tbl->noexpand=1; \ + uthash_noexpand_fyi(tbl); \ + } \ + uthash_expand_fyi(tbl); \ +} while (0) + + +/* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */ +/* Note that HASH_SORT assumes the hash handle name to be hh. + * HASH_SRT was added to allow the hash handle name to be passed in. */ +#define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn) +#define HASH_SRT(hh,head,cmpfcn) \ +do { \ + unsigned _hs_i; \ + unsigned _hs_looping,_hs_nmerges,_hs_insize,_hs_psize,_hs_qsize; \ + struct UT_hash_handle *_hs_p, *_hs_q, *_hs_e, *_hs_list, *_hs_tail; \ + if (head != NULL) { \ + _hs_insize = 1; \ + _hs_looping = 1; \ + _hs_list = &((head)->hh); \ + while (_hs_looping != 0U) { \ + _hs_p = _hs_list; \ + _hs_list = NULL; \ + _hs_tail = NULL; \ + _hs_nmerges = 0; \ + while (_hs_p != NULL) { \ + _hs_nmerges++; \ + _hs_q = _hs_p; \ + _hs_psize = 0; \ + for ( _hs_i = 0; _hs_i < _hs_insize; _hs_i++ ) { \ + _hs_psize++; \ + _hs_q = (UT_hash_handle*)((_hs_q->next != NULL) ? \ + ((void*)((char*)(_hs_q->next) + \ + (head)->hh.tbl->hho)) : NULL); \ + if (! (_hs_q) ) { break; } \ + } \ + _hs_qsize = _hs_insize; \ + while ((_hs_psize > 0U) || ((_hs_qsize > 0U) && (_hs_q != NULL))) {\ + if (_hs_psize == 0U) { \ + _hs_e = _hs_q; \ + _hs_q = (UT_hash_handle*)((_hs_q->next != NULL) ? \ + ((void*)((char*)(_hs_q->next) + \ + (head)->hh.tbl->hho)) : NULL); \ + _hs_qsize--; \ + } else if ( (_hs_qsize == 0U) || (_hs_q == NULL) ) { \ + _hs_e = _hs_p; \ + if (_hs_p != NULL){ \ + _hs_p = (UT_hash_handle*)((_hs_p->next != NULL) ? \ + ((void*)((char*)(_hs_p->next) + \ + (head)->hh.tbl->hho)) : NULL); \ + } \ + _hs_psize--; \ + } else if (( \ + cmpfcn(DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_p)), \ + DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_q))) \ + ) <= 0) { \ + _hs_e = _hs_p; \ + if (_hs_p != NULL){ \ + _hs_p = (UT_hash_handle*)((_hs_p->next != NULL) ? \ + ((void*)((char*)(_hs_p->next) + \ + (head)->hh.tbl->hho)) : NULL); \ + } \ + _hs_psize--; \ + } else { \ + _hs_e = _hs_q; \ + _hs_q = (UT_hash_handle*)((_hs_q->next != NULL) ? \ + ((void*)((char*)(_hs_q->next) + \ + (head)->hh.tbl->hho)) : NULL); \ + _hs_qsize--; \ + } \ + if ( _hs_tail != NULL ) { \ + _hs_tail->next = ((_hs_e != NULL) ? \ + ELMT_FROM_HH((head)->hh.tbl,_hs_e) : NULL); \ + } else { \ + _hs_list = _hs_e; \ + } \ + if (_hs_e != NULL) { \ + _hs_e->prev = ((_hs_tail != NULL) ? \ + ELMT_FROM_HH((head)->hh.tbl,_hs_tail) : NULL); \ + } \ + _hs_tail = _hs_e; \ + } \ + _hs_p = _hs_q; \ + } \ + if (_hs_tail != NULL){ \ + _hs_tail->next = NULL; \ + } \ + if ( _hs_nmerges <= 1U ) { \ + _hs_looping=0; \ + (head)->hh.tbl->tail = _hs_tail; \ + DECLTYPE_ASSIGN(head,ELMT_FROM_HH((head)->hh.tbl, _hs_list)); \ + } \ + _hs_insize *= 2U; \ + } \ + HASH_FSCK(hh,head); \ + } \ +} while (0) + +/* This function selects items from one hash into another hash. + * The end result is that the selected items have dual presence + * in both hashes. There is no copy of the items made; rather + * they are added into the new hash through a secondary hash + * hash handle that must be present in the structure. */ +#define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \ +do { \ + unsigned _src_bkt, _dst_bkt; \ + void *_last_elt=NULL, *_elt; \ + UT_hash_handle *_src_hh, *_dst_hh, *_last_elt_hh=NULL; \ + ptrdiff_t _dst_hho = ((char*)(&(dst)->hh_dst) - (char*)(dst)); \ + if (src != NULL) { \ + for(_src_bkt=0; _src_bkt < (src)->hh_src.tbl->num_buckets; _src_bkt++) { \ + for(_src_hh = (src)->hh_src.tbl->buckets[_src_bkt].hh_head; \ + _src_hh != NULL; \ + _src_hh = _src_hh->hh_next) { \ + _elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \ + if (cond(_elt)) { \ + _dst_hh = (UT_hash_handle*)(((char*)_elt) + _dst_hho); \ + _dst_hh->key = _src_hh->key; \ + _dst_hh->keylen = _src_hh->keylen; \ + _dst_hh->hashv = _src_hh->hashv; \ + _dst_hh->prev = _last_elt; \ + _dst_hh->next = NULL; \ + if (_last_elt_hh != NULL) { _last_elt_hh->next = _elt; } \ + if (dst == NULL) { \ + DECLTYPE_ASSIGN(dst,_elt); \ + HASH_MAKE_TABLE(hh_dst,dst); \ + } else { \ + _dst_hh->tbl = (dst)->hh_dst.tbl; \ + } \ + HASH_TO_BKT(_dst_hh->hashv, _dst_hh->tbl->num_buckets, _dst_bkt); \ + HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt],_dst_hh); \ + (dst)->hh_dst.tbl->num_items++; \ + _last_elt = _elt; \ + _last_elt_hh = _dst_hh; \ + } \ + } \ + } \ + } \ + HASH_FSCK(hh_dst,dst); \ +} while (0) + +#define HASH_CLEAR(hh,head) \ +do { \ + if (head != NULL) { \ + uthash_free((head)->hh.tbl->buckets, \ + (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket)); \ + HASH_BLOOM_FREE((head)->hh.tbl); \ + uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ + (head)=NULL; \ + } \ +} while (0) + +#define HASH_OVERHEAD(hh,head) \ + ((head != NULL) ? ( \ + (size_t)(((head)->hh.tbl->num_items * sizeof(UT_hash_handle)) + \ + ((head)->hh.tbl->num_buckets * sizeof(UT_hash_bucket)) + \ + sizeof(UT_hash_table) + \ + (HASH_BLOOM_BYTELEN))) : 0U) + +#ifdef NO_DECLTYPE +#define HASH_ITER(hh,head,el,tmp) \ +for(((el)=(head)), ((*(char**)(&(tmp)))=(char*)((head!=NULL)?(head)->hh.next:NULL)); \ + (el) != NULL; ((el)=(tmp)), ((*(char**)(&(tmp)))=(char*)((tmp!=NULL)?(tmp)->hh.next:NULL))) +#else +#define HASH_ITER(hh,head,el,tmp) \ +for(((el)=(head)), ((tmp)=DECLTYPE(el)((head!=NULL)?(head)->hh.next:NULL)); \ + (el) != NULL; ((el)=(tmp)), ((tmp)=DECLTYPE(el)((tmp!=NULL)?(tmp)->hh.next:NULL))) +#endif + +/* obtain a count of items in the hash */ +#define HASH_COUNT(head) HASH_CNT(hh,head) +#define HASH_CNT(hh,head) ((head != NULL)?((head)->hh.tbl->num_items):0U) + +typedef struct UT_hash_bucket { + struct UT_hash_handle *hh_head; + unsigned count; + + /* expand_mult is normally set to 0. In this situation, the max chain length + * threshold is enforced at its default value, HASH_BKT_CAPACITY_THRESH. (If + * the bucket's chain exceeds this length, bucket expansion is triggered). + * However, setting expand_mult to a non-zero value delays bucket expansion + * (that would be triggered by additions to this particular bucket) + * until its chain length reaches a *multiple* of HASH_BKT_CAPACITY_THRESH. + * (The multiplier is simply expand_mult+1). The whole idea of this + * multiplier is to reduce bucket expansions, since they are expensive, in + * situations where we know that a particular bucket tends to be overused. + * It is better to let its chain length grow to a longer yet-still-bounded + * value, than to do an O(n) bucket expansion too often. + */ + unsigned expand_mult; + +} UT_hash_bucket; + +/* random signature used only to find hash tables in external analysis */ +#define HASH_SIGNATURE 0xa0111fe1u +#define HASH_BLOOM_SIGNATURE 0xb12220f2u + +typedef struct UT_hash_table { + UT_hash_bucket *buckets; + unsigned num_buckets, log2_num_buckets; + unsigned num_items; + struct UT_hash_handle *tail; /* tail hh in app order, for fast append */ + ptrdiff_t hho; /* hash handle offset (byte pos of hash handle in element */ + + /* in an ideal situation (all buckets used equally), no bucket would have + * more than ceil(#items/#buckets) items. that's the ideal chain length. */ + unsigned ideal_chain_maxlen; + + /* nonideal_items is the number of items in the hash whose chain position + * exceeds the ideal chain maxlen. these items pay the penalty for an uneven + * hash distribution; reaching them in a chain traversal takes >ideal steps */ + unsigned nonideal_items; + + /* ineffective expands occur when a bucket doubling was performed, but + * afterward, more than half the items in the hash had nonideal chain + * positions. If this happens on two consecutive expansions we inhibit any + * further expansion, as it's not helping; this happens when the hash + * function isn't a good fit for the key domain. When expansion is inhibited + * the hash will still work, albeit no longer in constant time. */ + unsigned ineff_expands, noexpand; + + uint32_t signature; /* used only to find hash tables in external analysis */ +#ifdef HASH_BLOOM + uint32_t bloom_sig; /* used only to test bloom exists in external analysis */ + uint8_t *bloom_bv; + uint8_t bloom_nbits; +#endif + +} UT_hash_table; + +typedef struct UT_hash_handle { + struct UT_hash_table *tbl; + void *prev; /* prev element in app order */ + void *next; /* next element in app order */ + struct UT_hash_handle *hh_prev; /* previous hh in bucket order */ + struct UT_hash_handle *hh_next; /* next hh in bucket order */ + void *key; /* ptr to enclosing struct's key */ + unsigned keylen; /* enclosing struct's key len */ + unsigned hashv; /* result of hash-fcn(key) */ +} UT_hash_handle; + +#endif /* UTHASH_H */ diff --git a/src/sccz80/main.c b/src/sccz80/main.c index f5bec4b6bc..a49d63f8f5 100644 --- a/src/sccz80/main.c +++ b/src/sccz80/main.c @@ -68,7 +68,7 @@ int main(int argc, char** argv) dubq = MALLOC(FNLITQ); /* Doubles */ tempq = MALLOC(LITABSZ); /* Temp strings... */ glbq = MALLOC(LITABSZ); /* Used for glb lits, dumped now */ - symtab = MALLOC(NUMGLBS * sizeof(SYMBOL)); + symtab = NULL; loctab = MALLOC(NUMLOC * sizeof(SYMBOL)); wqueue = MALLOC(NUMWHILE * sizeof(WHILE_TAB)); gotoq = MALLOC(NUMGOTO * sizeof(GOTO_TAB)); @@ -81,12 +81,6 @@ int main(int argc, char** argv) stage = MALLOC(STAGESIZE); stagelast = stage + STAGELIMIT; - /* empty symbol table */ - glbptr = STARTGLB; - while (glbptr < ENDGLB) { - glbptr->name[0] = 0; - ++glbptr; - } glbcnt = 0; /* clear global symbols */ locptr = STARTLOC; /* clear local symbols */ @@ -318,11 +312,7 @@ void dumpfns() if (!glbcnt) return; - /* Start at the start! */ - glbptr = STARTGLB; - - ptr = STARTGLB; - while (ptr < ENDGLB) { + for ( ptr = symtab; ptr != NULL; ptr = ptr->hh.next ) { if (ptr->name[0] != 0 && ptr->name[0] != '0') { ident = ptr->ident; if (ident == FUNCTIONP) @@ -375,7 +365,6 @@ void dumpfns() } } } - ++ptr; } if ((fp = fopen("zcc_opt.def", "a")) == NULL) { @@ -484,14 +473,12 @@ void dumpvars() return; /* Start at the start! */ - glbptr = STARTGLB; outstr("; --- Start of Static Variables ---\n\n"); output_section("bss_compiler"); // output_section("bss"); - ptr = STARTGLB; - while (ptr < ENDGLB) { - if (ptr->name[0] != 0 && ptr->name[0] != '0') { + for ( ptr = symtab; ptr != NULL; ptr = ptr->hh.next ) { + if (ptr->name[0] != '0') { ident = ptr->ident; type = ptr->type; storage = ptr->storage; @@ -504,7 +491,6 @@ void dumpvars() nl(); } } - ++ptr; } /* Switch back to standard section */ @@ -1072,7 +1058,6 @@ void MemCleanup() FREENULL(dubq); FREENULL(tempq); FREENULL(glbq); - FREENULL(symtab); FREENULL(loctab); FREENULL(wqueue); FREENULL(tagtab); diff --git a/src/sccz80/preproc.c b/src/sccz80/preproc.c index 85db6bf065..3d94f956d7 100644 --- a/src/sccz80/preproc.c +++ b/src/sccz80/preproc.c @@ -257,8 +257,9 @@ char putmac(char c) int findmac(char* sname) { - if (findglb(sname) != 0 && glbptr->ident == MACRO) { - return (glbptr->offset.i); + SYMBOL *ptr; + if ( ( ptr = findglb(sname)) != NULL && ptr->ident == MACRO) { + return (ptr->offset.i); } return (0); } diff --git a/src/sccz80/sym.c b/src/sccz80/sym.c index 1b1f5bee8c..34c6f9d396 100644 --- a/src/sccz80/sym.c +++ b/src/sccz80/sym.c @@ -8,25 +8,16 @@ #include "ccdefs.h" -static int hash(char *sname); static void initialise_sym(SYMBOL *ptr, char *sname, enum ident_type id, char typ, enum storage_type storage, int more, int itag); -static int hash(char* sname) -{ - int c, h; - h = *sname; - while ((c = *(++sname))) - h = (h << 1) + c; - return (h & (NUMGLBS-1)); -} /* djm * Find a local static variable - uses findglb after first kludging the * name to a hopefully unique identifier! */ -SYMBOL* findstc(char* sname) +SYMBOL *findstc(char* sname) { char sname2[3 * NAMESIZE]; /* Should be enuff! */ strcpy(sname2, "st_"); @@ -46,27 +37,24 @@ SYMBOL* findstc(char* sname) SYMBOL* findenum(char* sname) { - SYMBOL* ptr; - if ((ptr = findglb(sname))) { - if (ptr->ident == ENUM) - return (ptr); - else - error(E_ENUMDEF); + SYMBOL *ptr = findglb(sname); + + if ( ptr != NULL ) { + if ( ptr->ident == ENUM ) { + return ptr; + } + error(E_ENUMDEF); } - return (0); + return NULL; } SYMBOL* findglb(char* sname) { - glbptr = STARTGLB + hash(sname); - while (strcmp(sname, glbptr->name)) { - if (glbptr->name[0] == 0) - return 0; - ++glbptr; - if (glbptr == ENDGLB) - glbptr = STARTGLB; - } - return glbptr; + SYMBOL *ptr; + + HASH_FIND_STR(symtab, sname, ptr); + + return ptr; } SYMBOL* findloc(char* sname) @@ -140,16 +128,14 @@ SYMBOL* addglb( } multidef(); - return (glbptr); - } - if (glbcnt >= NUMGLBS - 1) { - error(E_GLBOV); - return 0; + return (ptr); } - initialise_sym(glbptr, sname, id, typ, storage, more, itag); - glbptr->offset.i = value; + ptr = CALLOC(1, sizeof(*ptr)); + initialise_sym(ptr, sname, id, typ, storage, more, itag); + ptr->offset.i = value; + HASH_ADD_STR(symtab, name, ptr); ++glbcnt; - return (glbptr); + return (ptr); } SYMBOL* addloc( From af79945a1801accad01cf7d28df6b4f1e09249c1 Mon Sep 17 00:00:00 2001 From: suborb Date: Thu, 23 Feb 2017 23:45:48 +0000 Subject: [PATCH 13/16] Rename config option variables --- src/sccz80/callfunc.c | 8 ++++---- src/sccz80/codegen.c | 26 +++++++++++------------ src/sccz80/const.c | 2 +- src/sccz80/data.c | 6 +++--- src/sccz80/data.h | 8 ++++---- src/sccz80/declfunc.c | 4 ++-- src/sccz80/main.c | 48 +++++++++++++++++++++---------------------- src/sccz80/main.h | 8 ++++---- src/sccz80/plunge.c | 2 +- src/sccz80/stmt.c | 10 ++++----- 10 files changed, 61 insertions(+), 61 deletions(-) diff --git a/src/sccz80/callfunc.c b/src/sccz80/callfunc.c index 5522a8ced7..9df175e24d 100644 --- a/src/sccz80/callfunc.c +++ b/src/sccz80/callfunc.c @@ -203,7 +203,7 @@ void callfunction(SYMBOL* ptr) printflevel = minifunc; } outname(ptr->name, dopref(ptr)); - if ((ptr->flags & SHARED) && useshare) + if ((ptr->flags & SHARED) && c_useshared) outstr("_sl"); else if (ptr->flags & SHAREDC) outstr("_rst"); @@ -218,11 +218,11 @@ void callfunction(SYMBOL* ptr) * * We should modify stack if: * - __CALLEE__ isn't set - * - Function is __LIB__ even if compactcode is set - * - compactcode isn't set and __CALLEE__ isn't set + * - Function is __LIB__ even if c_compact_code is set + * - c_compact_code isn't set and __CALLEE__ isn't set */ - if ((ptr && ptr->flags & CALLEE) || (compactcode && ptr == NULL) || (compactcode && ((ptr->flags & LIBRARY) == 0))) { + if ((ptr && ptr->flags & CALLEE) || (c_compact_code && ptr == NULL) || (c_compact_code && ((ptr->flags & LIBRARY) == 0))) { Zsp += nargs; } else { /* If we have a frame pointer then ix holds it */ diff --git a/src/sccz80/codegen.c b/src/sccz80/codegen.c index a0e806940c..f5b2e65393 100644 --- a/src/sccz80/codegen.c +++ b/src/sccz80/codegen.c @@ -167,7 +167,7 @@ void DoLibHeader(void) outstr("z80_crt0.hdx\"\n\n"); ol(".area _CODE\n"); ol(".radix d\n"); - if (noaltreg) { + if (c_notaltreg) { ol(".globl\tsaved_hl"); ol(".globl\tsaved_de"); } @@ -175,7 +175,7 @@ void DoLibHeader(void) } else { outstr("\n\n\tINCLUDE \"z80_crt0.hdr\"\n\n\n"); - if (noaltreg) { + if (c_notaltreg) { ol("EXTERN\tsaved_hl"); ol("EXTERN\tsaved_de"); } @@ -452,7 +452,7 @@ void putstk(char typeobj) break; default: zpop(); - if (doinline) { + if (c_doinline) { LoadAccum(); ol("ld\t(de),a"); ol("inc\tde"); @@ -576,7 +576,7 @@ void indirect(LVALUE* lval) callrts("dload"); break; default: - if (doinline) { + if (c_doinline) { ol("ld\ta,(hl)"); ol("inc\thl"); ol("ld\th,(hl)"); @@ -1045,7 +1045,7 @@ int modstk(int newsp, int save, int saveaf) modstkcht: #endif if (saveaf) { - if (noaltreg) { + if (c_notaltreg) { zpushflags(); zpopbc(); } else { @@ -1082,7 +1082,7 @@ int modstk(int newsp, int save, int saveaf) doexx(); #endif if (saveaf) { - if (noaltreg) { + if (c_notaltreg) { ol("push\tbc"); Zsp -= 2; zpopflags(); @@ -1287,7 +1287,7 @@ void zdiv(LVALUE* lval) */ void zmod(LVALUE* lval) { - if (noaltreg && (lval->val_type == LONG || lval->val_type == CPTR)) { + if (c_notaltreg && (lval->val_type == LONG || lval->val_type == CPTR)) { callrts("l_long_mod2"); } else { zdiv(lval); @@ -1594,7 +1594,7 @@ void zeq(LVALUE* lval) Zsp += 6; break; case CCHAR: - if (doinline) { + if (c_doinline) { lval->val_type = CARRY; ol("ld\ta,l"); ol("sub\te"); @@ -1629,7 +1629,7 @@ void zne(LVALUE* lval) Zsp += 6; break; case CCHAR: - if (doinline) { + if (c_doinline) { lval->val_type = CARRY; ol("ld\ta,l"); ol("sub\te"); @@ -1668,7 +1668,7 @@ void zlt(LVALUE* lval) Zsp += 6; break; case CCHAR: - if (doinline) { + if (c_doinline) { if (utype(lval)) { ol("ld\ta,e"); ol("sub\tl"); @@ -1712,7 +1712,7 @@ void zle(LVALUE* lval) Zsp += 6; break; case CCHAR: - if (doinline) { + if (c_doinline) { if (utype(lval)) { /* unsigned */ ol("ld\ta,e"); ol("sub\tl"); /* If l < e then carry set */ @@ -1766,7 +1766,7 @@ void zgt(LVALUE* lval) Zsp += 6; break; case CCHAR: - if (doinline) { + if (c_doinline) { if (utype(lval)) { ol("ld\ta,e"); ol("sub\tl"); @@ -1817,7 +1817,7 @@ void zge(LVALUE* lval) Zsp += 6; break; case CCHAR: - if (doinline) { + if (c_doinline) { if (utype(lval)) { ol("ld\ta,l"); ol("sub\te"); /* If l > e, carry set */ diff --git a/src/sccz80/const.c b/src/sccz80/const.c index 0ceeb7031d..1d4f982317 100644 --- a/src/sccz80/const.c +++ b/src/sccz80/const.c @@ -130,7 +130,7 @@ int fnumber(int32_t* val) dval = strtod(start, &end); if (end == start) return 0; - dofloat(dval, sum, mathz88 ? 4 : 5, mathz88 ? 127 : 128); + dofloat(dval, sum, c_mathz88 ? 4 : 5, c_mathz88 ? 127 : 128); for ( i = 0; i < buffer_fps_num; i++ ) fprintf(buffer_fps[i], "%.*s", (int)(end-start), start); diff --git a/src/sccz80/data.c b/src/sccz80/data.c index 29dd124eec..d7600842e6 100644 --- a/src/sccz80/data.c +++ b/src/sccz80/data.c @@ -60,13 +60,13 @@ char Filename[FILENAME_LEN + 1]; /* output file name */ /* My stuff for LIB of long common functions */ -int need_floatpack, doinline, ncomp; +int need_floatpack, c_doinline, ncomp; int stackargs; int defstatic, filenum; /* next argument to be used */ -char c_default_unsigned, fnflags, mathz88, compactcode; +char c_default_unsigned, fnflags, c_mathz88, c_compact_code; int nxtlab, /* next avail label # */ dublab, /* label # relative to double pool */ @@ -94,7 +94,7 @@ int nxtlab, /* next avail label # */ saveinfn, /* copy of infunc " " */ swactive, /* true inside a switch */ swdefault, /* default label number, else 0 */ - verbose, /* Verbose to screen */ + c_verbose, /* Verbose to screen */ caller, /* stack offset for caller links... local[caller] points to name of current fct local[caller-1] points to link for calling fct, diff --git a/src/sccz80/data.h b/src/sccz80/data.h index f5f511d5a6..38a437c15b 100644 --- a/src/sccz80/data.h +++ b/src/sccz80/data.h @@ -35,15 +35,15 @@ extern int lptr; extern int mptr; extern char Filename[]; extern int need_floatpack; -extern int doinline; +extern int c_doinline; extern int ncomp; extern int stackargs; extern int defstatic; extern int filenum; extern char c_default_unsigned; extern char fnflags; -extern char mathz88; -extern char compactcode; +extern char c_mathz88; +extern char c_compact_code; extern int nxtlab; extern int dublab; extern int glblab; @@ -68,7 +68,7 @@ extern int saveline; extern int saveinfn; extern int swactive; extern int swdefault; -extern int verbose; +extern int c_verbose; extern int caller; extern int fname; extern FILE *input; diff --git a/src/sccz80/declfunc.c b/src/sccz80/declfunc.c index 74b828e748..5d4135b007 100644 --- a/src/sccz80/declfunc.c +++ b/src/sccz80/declfunc.c @@ -344,7 +344,7 @@ void setlocvar(SYMBOL* prevarg, SYMBOL* currfn) /* * Dump some info about defining the function etc */ - if (verbose) { + if (c_verbose) { toconsole(); outstr("Defining function: "); outstr(currfn->name); @@ -370,7 +370,7 @@ void setlocvar(SYMBOL* prevarg, SYMBOL* currfn) infunc = 1; /* In a function for sure! */ copyarg = prevarg; - if (((currfn->flags & SHARED) && makeshare) || sharedfile) { + if (((currfn->flags & SHARED) && c_makeshare) || c_shared_file) { /* Shared library definition, offset the stack */ where = 2 + shareoffset; } else diff --git a/src/sccz80/main.c b/src/sccz80/main.c index a49d63f8f5..a1a6abed59 100644 --- a/src/sccz80/main.c +++ b/src/sccz80/main.c @@ -21,14 +21,14 @@ char Filenorig[FILENAME_LEN + 1]; unsigned int zorg; /* Origin for applications */ int smartprintf; /* Map printf -> miniprintf */ -int makeshare; /* Do we want to make a shared library? */ -int useshare; /* Use shared lib routines? */ -int sharedfile; /* File contains routines which are to be +int c_makeshare; /* Do we want to make a shared library? */ +int c_useshared; /* Use shared lib routines? */ +int c_shared_file; /* File contains routines which are to be * called via lib package - basically jimmy * the stack but that's it.. */ -int noaltreg; /* No alternate registers */ +int c_notaltreg; /* No alternate registers */ int standard_escapes = 0; /* \n = 10, \r = 13 */ /* @@ -112,18 +112,18 @@ int main(int argc, char** argv) currfn = NULL; /* no function yet */ macptr = cmode = 1; /* clear macro pool and enable preprocessing */ - ncomp = doinline = mathz88 = need_floatpack = compactcode = 0; + ncomp = c_doinline = c_mathz88 = need_floatpack = c_compact_code = 0; c_default_unsigned = NO; - useshare = makeshare = sharedfile = NO; + c_useshared = c_makeshare = c_shared_file = NO; smartprintf = YES; nxtlab = /* start numbers at lowest possible */ ctext = /* don't include the C text as comments */ errstop = /* don't stop after errors */ - verbose = 0; + c_verbose = 0; gotocnt = 0; defdenums = 0; doublestrings = 0; - noaltreg = NO; + c_notaltreg = NO; shareoffset = SHAREOFFSET; /* Offset for shared libs */ debuglevel = NO; assemtype = ASM_Z80ASM; @@ -232,7 +232,7 @@ void errsummary() error(E_BRACKET); nl(); } - if (verbose) { + if (c_verbose) { printf("Symbol table usage: %d\n", glbcnt); printf("Structures defined: %ld\n", (long)(tagptr - tagtab)); printf("Members defined: %ld\n", (long)(membptr - membtab)); @@ -326,7 +326,7 @@ void dumpfns() if (storage == EXTERNAL) { if (ptr->flags & LIBRARY) { GlobalPrefix(LIB); - if ((ptr->flags & SHARED) && useshare) { + if ((ptr->flags & SHARED) && c_useshared) { outstr(ptr->name); outstr("_sl\n"); GlobalPrefix(LIB); @@ -376,9 +376,9 @@ void dumpfns() fprintf(fp, "\tDEFINE\tNEED_floatpack\n"); fprintf(fp, "ENDIF\n\n"); } - if (mathz88) { - fprintf(fp, "\nIF !NEED_mathz88\n"); - fprintf(fp, "\tDEFINE\tNEED_mathz88\n"); + if (c_mathz88) { + fprintf(fp, "\nIF !NEED_c_mathz88\n"); + fprintf(fp, "\tDEFINE\tNEED_c_mathz88\n"); fprintf(fp, "ENDIF\n\n"); } /* @@ -637,7 +637,7 @@ void openin() fprintf(stderr, "Can't open: %s\n", Filename); exit(1); } else { - if (verbose) + if (c_verbose) fprintf(stderr, "Compiling: %s\n", Filename); ncomp++; newfile(); @@ -665,7 +665,7 @@ void doinclude() char name[FILENAME_LEN + 1], *cp; blanks(); /* skip over to name */ - if (verbose) { + if (c_verbose) { toconsole(); outstr(line); nl(); @@ -704,7 +704,7 @@ void doinclude() */ void endinclude() { - if (verbose) { + if (c_verbose) { toconsole(); outstr("#end include\n"); tofile(); @@ -811,7 +811,7 @@ void SetDoubleStrings(char* arg) void SetNoAltReg(char* arg) { - noaltreg = YES; + c_notaltreg = YES; } void SetASXX(char* arg) @@ -884,7 +884,7 @@ void SetWarning(char* arg) void SetMathZ88(char* arg) { - mathz88 = YES; + c_mathz88 = YES; } void SetUnsigned(char* arg) @@ -909,7 +909,7 @@ void SetAllWarn(char* arg) void SetDoInline(char* arg) { - doinline = YES; + c_doinline = YES; } void SetStopError(char* arg) @@ -919,22 +919,22 @@ void SetStopError(char* arg) void SetUseShared(char* arg) { - useshare = YES; + c_useshared = YES; } void SetSharedFile(char* arg) { - sharedfile = YES; + c_shared_file = YES; } void SetMakeShared(char* arg) { - makeshare = YES; + c_makeshare = YES; } void SetCompactCode(char* arg) { - compactcode = YES; + c_compact_code = YES; } void SetCCode(char* arg) @@ -1004,7 +1004,7 @@ void DispVersion(char* arg) void SetVerbose(char* arg) { - verbose = YES; + c_verbose = YES; } void ShowNotFunc(char* arg) diff --git a/src/sccz80/main.h b/src/sccz80/main.h index a332d37dc5..83d936401d 100644 --- a/src/sccz80/main.h +++ b/src/sccz80/main.h @@ -41,10 +41,10 @@ extern void WriteDefined(char *sname, int value); extern void SetShareOffset(char *); extern void SetDoubleStrings(char *); extern void SetNoAltReg(char *); -extern int noaltreg; -extern int makeshare; -extern int useshare; -extern int sharedfile; +extern int c_notaltreg; +extern int c_makeshare; +extern int c_useshared; +extern int c_shared_file; extern void SetSharedFile(char *); extern void *mymalloc(size_t); extern void SetDebug(char *); diff --git a/src/sccz80/plunge.c b/src/sccz80/plunge.c index 8e7c200ba1..8b7f0274f8 100644 --- a/src/sccz80/plunge.c +++ b/src/sccz80/plunge.c @@ -250,7 +250,7 @@ void plnge2b(int (*heir)(LVALUE* lval), LVALUE* lval, LVALUE* lval2, void (*oper } if (lval->val_type == LONG) { widenlong(lval, lval2); - if (noaltreg) { + if (c_notaltreg) { vlongconst_noalt(val); } else { doexx(); diff --git a/src/sccz80/stmt.c b/src/sccz80/stmt.c index 04a6937289..c3ff72a23a 100644 --- a/src/sccz80/stmt.c +++ b/src/sccz80/stmt.c @@ -511,7 +511,7 @@ void leave(int vartype, char type) else if (vartype == DOUBLE) vartype = NO; - if (noaltreg) { + if (c_notaltreg) { if (vartype == LONG) savehl(); modstk(0, 0, NO); @@ -519,7 +519,7 @@ void leave(int vartype, char type) modstk(0, vartype, NO); } - if ((compactcode || currfn->flags & CALLEE) && (stackargs > 2)) { + if ((c_compact_code || currfn->flags & CALLEE) && (stackargs > 2)) { /* * We're exiting a function and we want to clean up after ourselves * (so calling function doesn't have to do this) (first of all we @@ -527,7 +527,7 @@ void leave(int vartype, char type) */ savesp = Zsp; - if (noaltreg) { + if (c_notaltreg) { if (vartype == LONG) /* If long, then dump de somewhere */ savede(); } else if (vartype == LONG) { @@ -537,7 +537,7 @@ void leave(int vartype, char type) Zsp -= stackargs; modstk(0, NO, NO); zpushde(); /* return address back on stack */ - if (noaltreg) { + if (c_notaltreg) { if (vartype == LONG) restorede(); } else if (vartype == LONG) { @@ -546,7 +546,7 @@ void leave(int vartype, char type) Zsp = savesp; } popframe(); /* Restore previous frame pointer */ - if (noaltreg && vartype == LONG) + if (c_notaltreg && vartype == LONG) restorehl(); if (type) setcond(type); From 47777969baf91fa4034d3b31648cb323f0529c96 Mon Sep 17 00:00:00 2001 From: suborb Date: Fri, 24 Feb 2017 20:42:16 +0000 Subject: [PATCH 14/16] More cleanup --- src/sccz80/callfunc.c | 2 +- src/sccz80/ccdefs.h | 4 +++ src/sccz80/codegen.c | 22 ++++++++-------- src/sccz80/const.c | 8 +++--- src/sccz80/data.c | 33 +++++------------------- src/sccz80/data.h | 22 +++++----------- src/sccz80/declfunc.c | 5 ++-- src/sccz80/declvar.c | 6 +++-- src/sccz80/define.h | 2 +- src/sccz80/goto.c | 18 ++++++------- src/sccz80/main.c | 59 ++++++++++++++++++++++--------------------- 11 files changed, 78 insertions(+), 103 deletions(-) diff --git a/src/sccz80/callfunc.c b/src/sccz80/callfunc.c index b0905abd4f..fe53a93635 100644 --- a/src/sccz80/callfunc.c +++ b/src/sccz80/callfunc.c @@ -227,7 +227,7 @@ void callfunction(SYMBOL* ptr) } else { /* If we have a frame pointer then ix holds it */ #ifdef USEFRAME - if (useframe) { + if (c_useframepointer) { if (nargs) RestoreSP(preserve); Zsp += nargs; diff --git a/src/sccz80/ccdefs.h b/src/sccz80/ccdefs.h index d5838b84c7..0e66815d91 100644 --- a/src/sccz80/ccdefs.h +++ b/src/sccz80/ccdefs.h @@ -52,6 +52,10 @@ extern void callfunction(SYMBOL *ptr); #include "declinit.h" #include "error.h" #include "expr.h" +extern GOTO_TAB *gotoq; /* Pointer for gotoq */ +extern int dolabel(void); +extern void dogoto(void); +extern void goto_cleanup(void); #include "io.h" #include "lex.h" #include "main.h" diff --git a/src/sccz80/codegen.c b/src/sccz80/codegen.c index f5b2e65393..970f3e1b92 100644 --- a/src/sccz80/codegen.c +++ b/src/sccz80/codegen.c @@ -467,7 +467,7 @@ void putstk(char typeobj) void puttos(void) { #ifdef USEFRAME - if (useframe) { + if (c_useframepointer) { ot("ld\t"); OutIndex(0); outstr(",l\n"); @@ -485,7 +485,7 @@ void puttos(void) void put2tos(void) { #ifdef USEFRAME - if (useframe) { + if (c_useframepointer) { ot("ld\t"); OutIndex(2); outstr(",l\n"); @@ -584,7 +584,7 @@ void indirect(LVALUE* lval) } else { ot("call\tl_gint\t;"); #ifdef USEFRAME - if (useframe && CheckOffset(lval->offset)) { + if (c_useframepointer && CheckOffset(lval->offset)) { OutIndex(lval->offset); } #endif @@ -1006,7 +1006,7 @@ int modstk(int newsp, int save, int saveaf) if (k == 0) return newsp; #ifdef USEFRAME - if (useframe) + if (c_useframepointer) goto modstkcht; #endif if (k > 0) { @@ -1053,7 +1053,7 @@ int modstk(int newsp, int save, int saveaf) } } #ifdef USEFRAME - if (useframe) { + if (c_useframepointer) { ot("ld\t"); FrameP(); outstr(","); @@ -2151,14 +2151,14 @@ void OutIndex(int val) if (ISASM(ASM_ASXX))) { outdec(val); - if (indexix) + if (c_framepointer_is_ix) outstr("(ix)"); else outstr("(iy)"); } else { outstr("("); - if (indexix) + if (c_framepointer_is_ix) outstr("ix "); else outstr("iy "); @@ -2183,7 +2183,7 @@ void RestoreSP(char saveaf) void setframe(void) { #ifdef USEFRAME - if (!useframe) + if (!c_useframepointer) return; ot("ld\t"); FrameP(); @@ -2198,12 +2198,12 @@ void setframe(void) void FrameP(void) { - outstr(indexix ? "ix" : "iy"); + outstr(c_framepointer_is_ix ? "ix" : "iy"); } void pushframe(void) { - if (useframe || (currfn->flags & SAVEFRAME)) { + if (c_useframepointer || (currfn->flags & SAVEFRAME)) { ot("push\t"); FrameP(); nl(); @@ -2212,7 +2212,7 @@ void pushframe(void) void popframe(void) { - if (useframe || (currfn->flags & SAVEFRAME)) { + if (c_useframepointer || (currfn->flags & SAVEFRAME)) { ot("pop\t"); FrameP(); nl(); diff --git a/src/sccz80/const.c b/src/sccz80/const.c index 1d4f982317..6a5e04c2e5 100644 --- a/src/sccz80/const.c +++ b/src/sccz80/const.c @@ -45,7 +45,7 @@ int constant(LVALUE* lval) lval->is_const = 1; /* assume constant will be found */ if (fnumber(&lval->const_val)) { lval->val_type = DOUBLE; - if (doublestrings) { + if (c_double_strings) { immedlit(litlab); outdec(lval->const_val); nl(); @@ -137,7 +137,7 @@ int fnumber(int32_t* val) lptr = end - line; /* get location for result & bump litptr */ - if (doublestrings) { + if (c_double_strings) { *val = stash_double_str(start, lptr + line); return (1); } else { @@ -427,7 +427,7 @@ unsigned char litchar() return 9; case 'r': /* LF */ gch(); - return standard_escapes ? 13 : 10; + return c_standard_escapecodes ? 13 : 10; case 'v': /* VT */ gch(); return 11; @@ -436,7 +436,7 @@ unsigned char litchar() return 12; case 'n': /* CR */ gch(); - return standard_escapes ? 10 : 13; + return c_standard_escapecodes ? 10 : 13; case '\"': /* " */ gch(); return 34; diff --git a/src/sccz80/data.c b/src/sccz80/data.c index d7600842e6..022923fe8c 100644 --- a/src/sccz80/data.c +++ b/src/sccz80/data.c @@ -89,17 +89,9 @@ int nxtlab, /* next avail label # */ fnstart, /* line# of start of current fn. */ lineno, /* line# in current file */ infunc, /* "inside function" flag */ - savestart, /* copy of fnstart " " */ - saveline, /* copy of lineno " " */ - saveinfn, /* copy of infunc " " */ swactive, /* true inside a switch */ swdefault, /* default label number, else 0 */ - c_verbose, /* Verbose to screen */ - caller, /* stack offset for caller links... - local[caller] points to name of current fct - local[caller-1] points to link for calling fct, - where local[0] is 1st word on stack after ret addr */ - fname; /* label for name of current fct */ + c_verbose; /* Verbose to screen */ FILE *input; /* iob # for input file */ FILE *output; /* iob # for output file (if any) */ @@ -109,10 +101,8 @@ FILE *saveout; /* holds output ptr when diverted to console */ SYMBOL *currfn; /* ptr to symtab entry for current fn. */ -SYMBOL *savecurr; /* copy of currfn for #include */ -int gargc; /* global copies of command line args */ -char** gargv; + char endasm; @@ -137,20 +127,9 @@ int debuglevel; * Assembler type */ -int assemtype; - -/* - * Output file extension - * - */ - -char* outext; +int c_assembler_type; -/* - * Enums defined - */ -int defdenums; /* @@ -161,13 +140,13 @@ int printflevel; /* Doubles stored as strings? */ -int doublestrings; +int c_double_strings; /* * Framepointer stuff - tis broken! */ -int useframe; -int indexix; +int c_useframepointer; +int c_framepointer_is_ix; int use_r2l_calling_convention; diff --git a/src/sccz80/data.h b/src/sccz80/data.h index 38a437c15b..7012bb99a3 100644 --- a/src/sccz80/data.h +++ b/src/sccz80/data.h @@ -63,38 +63,30 @@ extern int skiplevel; extern int fnstart; extern int lineno; extern int infunc; -extern int savestart; -extern int saveline; -extern int saveinfn; + extern int swactive; extern int swdefault; extern int c_verbose; -extern int caller; -extern int fname; extern FILE *input; extern FILE *output; extern FILE *inpt2; extern FILE *saveout; extern SYMBOL *currfn; -extern SYMBOL *savecurr; -extern int gargc; -extern char **gargv; + extern char endasm; extern char fnargvalue; extern int ltype; extern char margtag; extern int shareoffset; extern int debuglevel; -extern int assemtype; -extern char *outext; -extern int indexix; -extern int useframe; -extern int defdenums; +extern int c_assembler_type; +extern int c_framepointer_is_ix; +extern int c_useframepointer; extern int printflevel; -extern int doublestrings; +extern int c_double_strings; extern int usempm; extern int z80asm_sections; -extern int standard_escapes; +extern int c_standard_escapecodes; extern int scanf_level; extern uint32_t scanf_format_option; extern uint32_t printf_format_option; diff --git a/src/sccz80/declfunc.c b/src/sccz80/declfunc.c index 5d4135b007..328dfcd8d8 100644 --- a/src/sccz80/declfunc.c +++ b/src/sccz80/declfunc.c @@ -7,7 +7,6 @@ #include "ccdefs.h" -extern void CleanGoto(void); /** \brief Given an argument train, add in signature information to currfn * @@ -378,7 +377,7 @@ void setlocvar(SYMBOL* prevarg, SYMBOL* currfn) /* If we use frame pointer we preserve previous framepointer on entry * to each function */ - if (useframe || (currfn->flags & SAVEFRAME)) + if (c_useframepointer || (currfn->flags & SAVEFRAME)) where += 2; while (prevarg) { lgh = 2; /* Default length */ @@ -441,7 +440,7 @@ void setlocvar(SYMBOL* prevarg, SYMBOL* currfn) /* cleaning up the stack */ leave(NO, NO); } - CleanGoto(); + goto_cleanup(); function_appendix(currfn); #ifdef INBUILT_OPTIMIZER diff --git a/src/sccz80/declvar.c b/src/sccz80/declvar.c index 88cd609869..45eff8fa6c 100644 --- a/src/sccz80/declvar.c +++ b/src/sccz80/declvar.c @@ -755,6 +755,8 @@ TAG_SYMBOL* GetVarID(struct varid *var, enum storage_type storage) TAG_SYMBOL* otag = NULL; char sname[NAMEMAX]; SYMBOL* ptr; + static int num_enums_defined = 0; + var->sign = c_default_unsigned; var->zfar = NO; @@ -798,9 +800,9 @@ TAG_SYMBOL* GetVarID(struct varid *var, enum storage_type storage) var->type = VOID; else if (amatch("enum")) { if (symname(sname) == 0) - sprintf(sname, "sc_i_enumb%d", defdenums++); + sprintf(sname, "sc_i_enumb%d", num_enums_defined++); ptr = findenum(sname); - if (ptr == 0) /* not defined */ + if (ptr == NULL) /* not defined */ defenum(sname, storage); var->type = CINT; var->sign = c_default_unsigned; diff --git a/src/sccz80/define.h b/src/sccz80/define.h index 4f6c67e32c..79c95f1b99 100644 --- a/src/sccz80/define.h +++ b/src/sccz80/define.h @@ -337,7 +337,7 @@ struct varid { #define ASM_VASM 2 #define ASM_GNU 3 -#define ISASM(x) ( assemtype == (x) ) +#define ISASM(x) ( c_assembler_type == (x) ) struct parser_stack; diff --git a/src/sccz80/goto.c b/src/sccz80/goto.c index 5bf8808f3e..8fc2365565 100644 --- a/src/sccz80/goto.c +++ b/src/sccz80/goto.c @@ -31,21 +31,19 @@ #include "ccdefs.h" -int dolabel(void); -void dogoto(void); -int AddGoto(SYMBOL*); -void ChaseGoto(SYMBOL* ptr); -void CleanGoto(void); -GOTO_TAB* SearchGoto(SYMBOL* ptr); -SYMBOL* findgoto(char*); -SYMBOL* addgotosym(char*); + +static int AddGoto(SYMBOL*); +static void ChaseGoto(SYMBOL* ptr); +static GOTO_TAB* SearchGoto(SYMBOL* ptr); +static SYMBOL* findgoto(char*); +static SYMBOL* addgotosym(char*); /* * Some nice variables for us! */ GOTO_TAB* gotoq; -int gotocnt = 0; +static int gotocnt = 0; /* * Endeavour to find a label for a goto statement @@ -198,7 +196,7 @@ void ChaseGoto(SYMBOL* ptr) * Should remedy all stack problems etc(!) */ -void CleanGoto(void) +void goto_cleanup(void) { int i; GOTO_TAB* gptr; diff --git a/src/sccz80/main.c b/src/sccz80/main.c index a1a6abed59..5499cef941 100644 --- a/src/sccz80/main.c +++ b/src/sccz80/main.c @@ -13,12 +13,18 @@ extern unsigned _stklen = 8192U; /* Default stack size 4096 bytes is too small. */ #endif -/* - * Data used in this file only - */ +static char *c_output_extension = ".asm"; + + +static int gargc; /* global copies of command line args */ +static char **gargv; +static SYMBOL *savecurr; /* copy of currfn for #include */ +static int saveline; /* copy of lineno " " */ +static int saveinfn; /* copy of infunc " " */ +static int savestart; /* copy of fnstart " " */ + char Filenorig[FILENAME_LEN + 1]; -unsigned int zorg; /* Origin for applications */ int smartprintf; /* Map printf -> miniprintf */ int c_makeshare; /* Do we want to make a shared library? */ @@ -29,14 +35,12 @@ int c_shared_file; /* File contains routines which are to be */ int c_notaltreg; /* No alternate registers */ -int standard_escapes = 0; /* \n = 10, \r = 13 */ +int c_standard_escapecodes = 0; /* \n = 10, \r = 13 */ /* * Some external data */ -extern int gotocnt; /* No of gotos */ -extern GOTO_TAB* gotoq; /* Pointer for gotoq */ void DispVersion(char*); void SetMPM(char*); @@ -120,17 +124,14 @@ int main(int argc, char** argv) ctext = /* don't include the C text as comments */ errstop = /* don't stop after errors */ c_verbose = 0; - gotocnt = 0; - defdenums = 0; - doublestrings = 0; + c_double_strings = 0; c_notaltreg = NO; shareoffset = SHAREOFFSET; /* Offset for shared libs */ debuglevel = NO; - assemtype = ASM_Z80ASM; - outext = NULL; + c_assembler_type = ASM_Z80ASM; printflevel = 0; - indexix = YES; - useframe = NO; + c_framepointer_is_ix = YES; + c_useframepointer = NO; use_r2l_calling_convention = NO; setup_sym(); /* define some symbols */ @@ -609,7 +610,7 @@ void openout() /* copy file name to string */ strcpy(Filename, filen2); strcpy(Filenorig, filen2); - changesuffix(filen2, (outext == NULL) ? ".asm" : outext); /* Change appendix to .asm */ + changesuffix(filen2, c_output_extension); /* Change appendix to .asm */ if ((output = fopen(filen2, "w")) == NULL && (!eof)) { fprintf(stderr, "Cannot open output file: %s\n", line); exit(1); @@ -782,31 +783,31 @@ struct args myargs[] = { #ifdef USEFRAME void SetNoFrame(char* arg) { - useframe = NO; - indexix = NO; + c_useframepointer = NO; + c_framepointer_is_ix = NO; } void SetFrameIX(char* arg) { - useframe = YES; - indexix = YES; + c_useframepointer = YES; + c_framepointer_is_ix = YES; } void SetFrameIY(char* arg) { - useframe = YES; - indexix = NO; + c_useframepointer = YES; + c_framepointer_is_ix = NO; } #endif void SetStandardEscape(char* arg) { - standard_escapes = YES; + c_standard_escapecodes = YES; } void SetDoubleStrings(char* arg) { - doublestrings = YES; + c_double_strings = YES; } void SetNoAltReg(char* arg) @@ -816,7 +817,7 @@ void SetNoAltReg(char* arg) void SetASXX(char* arg) { - assemtype = ASM_ASXX; + c_assembler_type = ASM_ASXX; } /* debug= */ @@ -959,13 +960,13 @@ void SetAssembler(char* arg) if (1 == sscanf(arg + 4, "%s", assembler)) { if (strcmp(assembler, "z80asm") == 0 || strcmp(assembler, "mpm") == 0) { - assemtype = ASM_Z80ASM; + c_assembler_type = ASM_Z80ASM; } else if (strcmp(assembler, "asxx") == 0) { - assemtype = ASM_ASXX; + c_assembler_type = ASM_ASXX; } else if (strcmp(assembler, "vasm") == 0) { - assemtype = ASM_VASM; + c_assembler_type = ASM_VASM; } else if (strcmp(assembler, "gnu") == 0) { - assemtype = ASM_GNU; + c_assembler_type = ASM_GNU; } } } @@ -977,7 +978,7 @@ void SetOutExt(char* arg) temp[0] = '.'; strncpy(temp + 1, arg + 4, sizeof(temp) - 2); temp[sizeof(temp) - 1] = '\0'; - outext = strdup(temp); + c_output_extension = strdup(temp); } void DispInfo(char* arg) From 7dfe47a5f222ca37399e4d589273961243dd13d2 Mon Sep 17 00:00:00 2001 From: suborb Date: Fri, 24 Feb 2017 21:06:35 +0000 Subject: [PATCH 15/16] Some potential buffer problems around the max length of a symbol name --- src/sccz80/const.c | 2 +- src/sccz80/declinit.c | 2 +- src/sccz80/declvar.c | 11 ++++------- src/sccz80/define.h | 2 -- src/sccz80/goto.c | 4 ++-- src/sccz80/io.c | 2 +- src/sccz80/lex.c | 4 ++-- src/sccz80/stmt.c | 2 +- 8 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/sccz80/const.c b/src/sccz80/const.c index 6a5e04c2e5..2c08e55099 100644 --- a/src/sccz80/const.c +++ b/src/sccz80/const.c @@ -570,7 +570,7 @@ void size_of(LVALUE* lval) static int get_member_size(TAG_SYMBOL* ptr) { - char sname[NAMEMAX]; + char sname[NAMESIZE]; SYMBOL* ptr2; if (cmatch('.') == NO && match("->") == NO) return (0); diff --git a/src/sccz80/declinit.c b/src/sccz80/declinit.c index 9af925481f..a260117ce6 100644 --- a/src/sccz80/declinit.c +++ b/src/sccz80/declinit.c @@ -254,7 +254,7 @@ void init(int size, int ident, int* dim, int more, int dump, int is_struct) } } else { /* djm, catch label names in structures (for (*name)() initialisation */ - char sname[NAMEMAX + 1]; + char sname[NAMESIZE]; SYMBOL *ptr; if (symname(sname) && strcmp(sname, "sizeof")) { /* We have got something.. */ if ((ptr = findglb(sname))) { diff --git a/src/sccz80/declvar.c b/src/sccz80/declvar.c index 45eff8fa6c..a42bcda2f4 100644 --- a/src/sccz80/declvar.c +++ b/src/sccz80/declvar.c @@ -127,7 +127,7 @@ defstruct(char* sname, enum storage_type storage, int is_struct) void defenum(char* sname, enum storage_type storage) { SYMBOL* ptr; - char name[NAMEMAX]; + char name[NAMESIZE]; int32_t value; /* Add it into the symbol table, we do not need to keep track of the * tag because we treat enums as constants @@ -553,9 +553,7 @@ void declloc( /* declared += size ; Moved down djm */ if (otag) itag = otag - tagtab; - /* djm, add in local statics - use the global symbol table to ensure - * that they will be placed in RAM and not in ROM - */ + if (locstatic) { strcpy(sname2, "st_"); strcat(sname2, currfn->name); @@ -577,7 +575,6 @@ void declloc( typ = ExpandType(more, &dosign, otag - tagtab); warning(W_NULLARRAY, dosign, typ); } - } else { declared += size; cptr = addloc(sname, ident, type, more, itag); @@ -627,7 +624,7 @@ uint32_t CalcArgValue(char type, char ident, enum symbol_flags flags) char* ExpandType(int type, char** dosign, char tagidx) { - char* typ; + char *typ; switch (type) { case DOUBLE: @@ -753,7 +750,7 @@ static int needsub(void) TAG_SYMBOL* GetVarID(struct varid *var, enum storage_type storage) { TAG_SYMBOL* otag = NULL; - char sname[NAMEMAX]; + char sname[NAMESIZE]; SYMBOL* ptr; static int num_enums_defined = 0; diff --git a/src/sccz80/define.h b/src/sccz80/define.h index 79c95f1b99..97bfb8b70c 100644 --- a/src/sccz80/define.h +++ b/src/sccz80/define.h @@ -28,10 +28,8 @@ #if defined(__MSDOS__) && defined(__TURBOC__) #define NAMESIZE 33 - #define NAMEMAX 32 #else #define NAMESIZE 127 - #define NAMEMAX 126 #endif #define MAXARGS 20 diff --git a/src/sccz80/goto.c b/src/sccz80/goto.c index 8fc2365565..4a74867ab9 100644 --- a/src/sccz80/goto.c +++ b/src/sccz80/goto.c @@ -121,7 +121,7 @@ void dogoto() SYMBOL* addgotosym(char* sname) { - char sname2[NAMEMAX * 3]; + char sname2[NAMESIZE * 3]; strcpy(sname2, "goto_"); strcat(sname2, currfn->name); strcat(sname2, "_"); @@ -131,7 +131,7 @@ SYMBOL* addgotosym(char* sname) SYMBOL* findgoto(char* sname) { - char sname2[NAMEMAX * 3]; + char sname2[NAMESIZE * 3]; strcpy(sname2, "goto_"); strcat(sname2, currfn->name); strcat(sname2, "_"); diff --git a/src/sccz80/io.c b/src/sccz80/io.c index 41c6ee6eba..2db8a63397 100644 --- a/src/sccz80/io.c +++ b/src/sccz80/io.c @@ -52,7 +52,7 @@ int symname(char* sname) k = 0; while (an(ch())) { sname[k] = gch(); - if (k < NAMEMAX) + if (k < (NAMESIZE-1)) ++k; } sname[k] = 0; diff --git a/src/sccz80/lex.c b/src/sccz80/lex.c index a6464b2dab..fab5af79aa 100644 --- a/src/sccz80/lex.c +++ b/src/sccz80/lex.c @@ -96,7 +96,7 @@ int acmatch(char lit) int CheckTypDef(void) { - char sname[NAMEMAX]; + char sname[NAMESIZE]; char* ptr; SYMBOL* sym; int i = 0; @@ -104,7 +104,7 @@ int CheckTypDef(void) blanks(); /* Skip white space */ ptr = line + lptr; /* Where label starts */ - while (an(*ptr) && i < NAMEMAX) + while (an(*ptr) && i < (NAMESIZE -1)) sname[i++] = *ptr++; sname[i] = 0; sym = findglb(sname); diff --git a/src/sccz80/stmt.c b/src/sccz80/stmt.c index c3ff72a23a..fb2e149cc2 100644 --- a/src/sccz80/stmt.c +++ b/src/sccz80/stmt.c @@ -634,7 +634,7 @@ void doasmfunc(char wantbr) void doasm() { - char label[NAMEMAX]; + char label[NAMESIZE]; int k; char lab = 0; /* Got an good asm label */ SYMBOL* myptr; From a2bf656d1943e0dd96bc89fe54e7a7f4aa5da8b1 Mon Sep 17 00:00:00 2001 From: suborb Date: Sat, 25 Feb 2017 11:22:22 +0000 Subject: [PATCH 16/16] Allow initial assignment of a const local --- src/sccz80/declvar.c | 2 ++ src/sccz80/define.h | 1 + src/sccz80/primary.c | 13 ++++++++++--- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/sccz80/declvar.c b/src/sccz80/declvar.c index a42bcda2f4..129cd46338 100644 --- a/src/sccz80/declvar.c +++ b/src/sccz80/declvar.c @@ -567,6 +567,7 @@ void declloc( } if (rcmatch('=')) { initials(sname2, type, ident, dsize, more, otag, var->zfar, var->isconst); + cptr->isassigned = YES; ns(); cptr->storage = LSTKEXT; return; @@ -593,6 +594,7 @@ void declloc( setstage(&before, &start); expr = expression(&vconst, &val); clearstage(before, start); + cptr->isassigned = YES; //conv type force(decltype, expr, 0, 0, 0); StoreTOS(decltype); diff --git a/src/sccz80/define.h b/src/sccz80/define.h index 97bfb8b70c..bf18e03d23 100644 --- a/src/sccz80/define.h +++ b/src/sccz80/define.h @@ -119,6 +119,7 @@ struct symbol_s { int size ; /* djm, storage reqd! */ char prototyped; char isconst; /* Set if const, affects the section the data goes into */ + char isassigned; /* Set if we have assigned to it once */ uint32_t args[MAXARGS]; /* arguments */ unsigned char tagarg[MAXARGS]; /* ptrs to tagsymbol entries*/ enum symbol_flags flags ; /* djm, various flags: diff --git a/src/sccz80/primary.c b/src/sccz80/primary.c index 06b21c8972..e7c827b53d 100644 --- a/src/sccz80/primary.c +++ b/src/sccz80/primary.c @@ -521,7 +521,10 @@ void nstep( void store(LVALUE* lval) { if ( lval->symbol && lval->symbol->isconst ) { - error(E_CHANGING_CONST, lval->symbol); + if ( lval->symbol->isassigned ) + error(E_CHANGING_CONST, lval->symbol); + else + lval->symbol->isassigned = YES; } if (lval->indirect == 0) putmem(lval->symbol); @@ -572,14 +575,18 @@ void smartstore(LVALUE* lval) } else { switch (lval->symbol->offset.i - Zsp) { case 0: - if ( lval->symbol->isconst ) { + if ( lval->symbol->isconst && lval->symbol->isassigned ) { error(E_CHANGING_CONST, lval->symbol); + } else { + lval->symbol->isassigned = YES; } puttos(); break; case 2: - if ( lval->symbol->isconst ) { + if ( lval->symbol->isconst && lval->symbol->isassigned ) { error(E_CHANGING_CONST, lval->symbol); + } else { + lval->symbol->isassigned = YES; } put2tos(); break;