From 73d4934d61d0825c32faa461e9e9e7bbc99cbf38 Mon Sep 17 00:00:00 2001 From: Alex Martin Date: Sun, 15 Oct 2017 12:55:51 +0200 Subject: [PATCH] store symbol children for faster lookup --- source/compiler/sc.h | 1 + source/compiler/sc1.c | 6 ++++++ source/compiler/sc2.c | 10 ++++------ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/source/compiler/sc.h b/source/compiler/sc.h index cec8ca6..f3c6ea7 100644 --- a/source/compiler/sc.h +++ b/source/compiler/sc.h @@ -131,6 +131,7 @@ typedef struct s_constvalue { typedef struct s_symbol { struct s_symbol *next; struct s_symbol *parent; /* hierarchical types (multi-dimensional arrays) */ + struct s_symbol *child; char name[sNAMEMAX+1]; cell addr; /* address or offset (or value for constant, index for native function) */ cell codeaddr; /* address (in the code segment) where the symbol declaration starts */ diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index c7a8be4..592fba8 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -2929,6 +2929,8 @@ static void decl_enum(int vclass,int fstatic) sym->dim.array.length=size; sym->dim.array.level=0; sym->parent=enumsym; + if (enumsym) + enumsym->child=sym; if (fstatic) sym->fnumber=filenum; @@ -3522,6 +3524,8 @@ static void funcstub(int fnative) assert(sym!=NULL); sub=addvariable(symbolname,0,iARRAY,sGLOBAL,tag,dim,numdim,idxtag,0); sub->parent=sym; + if (sym) + sym->child=sub; } /* if */ litidx=0; /* clear the literal pool */ @@ -6513,6 +6517,8 @@ static void doreturn(void) sub=addvariable(curfunc->name,(argcount+3)*sizeof(cell),iREFARRAY,sGLOBAL, curfunc->tag,dim,numdim,idxtag,0); sub->parent=curfunc; + if (curfunc) + curfunc->child=sub; } /* if */ /* get the hidden parameter, copy the array (the array is on the heap; * it stays on the heap for the moment, and it is removed -usually- at diff --git a/source/compiler/sc2.c b/source/compiler/sc2.c index ddabd11..9a0fb90 100644 --- a/source/compiler/sc2.c +++ b/source/compiler/sc2.c @@ -2855,12 +2855,8 @@ static symbol *find_symbol(const symbol *root,const char *name,int fnumber,int a static symbol *find_symbol_child(const symbol *root,const symbol *sym) { - symbol *ptr=root->next; - while (ptr!=NULL) { - if (ptr->parent==sym) - return ptr; - ptr=ptr->next; - } /* while */ + if (sym->child && sym->child->parent == sym) + return sym->child; return NULL; } @@ -3062,6 +3058,8 @@ SC_FUNC symbol *addvariable(const char *name,cell addr,int ident,int vclass,int top->dim.array.level=(short)(numdim-level-1); top->x.tags.index=idxtag[level]; top->parent=parent; + if (parent) + parent->child=top; if (vclass==sLOCAL || vclass==sSTATIC) { top->compound=compound; /* for multiple declaration/shadowing check */ } /* if */