compiler-init/sclist.c
branchfaluco
changeset 2282 05c25f435814
parent 16 1feb2e0ff0ce
child 2285 9dd80a672eb9
     1.1 --- a/compiler-init/sclist.c	Sat Jul 15 01:36:48 2006 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,486 +0,0 @@
     1.4 -/*  Pawn compiler  - maintenance of various lists
     1.5 - *
     1.6 - *  o  Name list (aliases)
     1.7 - *  o  Include path list
     1.8 - *  o  Macro defintions (text substitutions)
     1.9 - *
    1.10 - *  Copyright (c) ITB CompuPhase, 2001-2006
    1.11 - *
    1.12 - *  This software is provided "as-is", without any express or implied warranty.
    1.13 - *  In no event will the authors be held liable for any damages arising from
    1.14 - *  the use of this software.
    1.15 - *
    1.16 - *  Permission is granted to anyone to use this software for any purpose,
    1.17 - *  including commercial applications, and to alter it and redistribute it
    1.18 - *  freely, subject to the following restrictions:
    1.19 - *
    1.20 - *  1.  The origin of this software must not be misrepresented; you must not
    1.21 - *      claim that you wrote the original software. If you use this software in
    1.22 - *      a product, an acknowledgment in the product documentation would be
    1.23 - *      appreciated but is not required.
    1.24 - *  2.  Altered source versions must be plainly marked as such, and must not be
    1.25 - *      misrepresented as being the original software.
    1.26 - *  3.  This notice may not be removed or altered from any source distribution.
    1.27 - *
    1.28 - *  Version: $Id: sclist.c 3579 2006-06-06 13:35:29Z thiadmer $
    1.29 - */
    1.30 -#include <assert.h>
    1.31 -#include <limits.h>
    1.32 -#include <stdlib.h>
    1.33 -#include <string.h>
    1.34 -#include "sc.h"
    1.35 -
    1.36 -#if defined FORTIFY
    1.37 -  #include <alloc/fortify.h>
    1.38 -#endif
    1.39 -
    1.40 -/* a "private" implementation of strdup(), so that porting
    1.41 - * to other memory allocators becomes easier.
    1.42 - * By S�ren Hannibal.
    1.43 - */
    1.44 -SC_FUNC char* duplicatestring(const char* sourcestring)
    1.45 -{
    1.46 -  char* result=(char*)malloc(strlen(sourcestring)+1);
    1.47 -  strcpy(result,sourcestring);
    1.48 -  return result;
    1.49 -}
    1.50 -
    1.51 -
    1.52 -static stringpair *insert_stringpair(stringpair *root,char *first,char *second,int matchlength)
    1.53 -{
    1.54 -  stringpair *cur,*pred;
    1.55 -
    1.56 -  assert(root!=NULL);
    1.57 -  assert(first!=NULL);
    1.58 -  assert(second!=NULL);
    1.59 -  /* create a new node, and check whether all is okay */
    1.60 -  if ((cur=(stringpair*)malloc(sizeof(stringpair)))==NULL)
    1.61 -    return NULL;
    1.62 -  cur->first=duplicatestring(first);
    1.63 -  cur->second=duplicatestring(second);
    1.64 -  cur->matchlength=matchlength;
    1.65 -  if (cur->first==NULL || cur->second==NULL) {
    1.66 -    if (cur->first!=NULL)
    1.67 -      free(cur->first);
    1.68 -    if (cur->second!=NULL)
    1.69 -      free(cur->second);
    1.70 -    free(cur);
    1.71 -    return NULL;
    1.72 -  } /* if */
    1.73 -  /* link the node to the tree, find the position */
    1.74 -  for (pred=root; pred->next!=NULL && strcmp(pred->next->first,first)<0; pred=pred->next)
    1.75 -    /* nothing */;
    1.76 -  cur->next=pred->next;
    1.77 -  pred->next=cur;
    1.78 -  return cur;
    1.79 -}
    1.80 -
    1.81 -static void delete_stringpairtable(stringpair *root)
    1.82 -{
    1.83 -  stringpair *cur, *next;
    1.84 -
    1.85 -  assert(root!=NULL);
    1.86 -  cur=root->next;
    1.87 -  while (cur!=NULL) {
    1.88 -    next=cur->next;
    1.89 -    assert(cur->first!=NULL);
    1.90 -    assert(cur->second!=NULL);
    1.91 -    free(cur->first);
    1.92 -    free(cur->second);
    1.93 -    free(cur);
    1.94 -    cur=next;
    1.95 -  } /* while */
    1.96 -  memset(root,0,sizeof(stringpair));
    1.97 -}
    1.98 -
    1.99 -static stringpair *find_stringpair(stringpair *cur,char *first,int matchlength)
   1.100 -{
   1.101 -  int result=0;
   1.102 -
   1.103 -  assert(matchlength>0);  /* the function cannot handle zero-length comparison */
   1.104 -  assert(first!=NULL);
   1.105 -  while (cur!=NULL && result<=0) {
   1.106 -    result=(int)*cur->first - (int)*first;
   1.107 -    if (result==0 && matchlength==cur->matchlength) {
   1.108 -      result=strncmp(cur->first,first,matchlength);
   1.109 -      if (result==0)
   1.110 -        return cur;
   1.111 -    } /* if */
   1.112 -    cur=cur->next;
   1.113 -  } /* while */
   1.114 -  return NULL;
   1.115 -}
   1.116 -
   1.117 -static int delete_stringpair(stringpair *root,stringpair *item)
   1.118 -{
   1.119 -  stringpair *cur;
   1.120 -
   1.121 -  assert(root!=NULL);
   1.122 -  cur=root;
   1.123 -  while (cur->next!=NULL) {
   1.124 -    if (cur->next==item) {
   1.125 -      cur->next=item->next;     /* unlink from list */
   1.126 -      assert(item->first!=NULL);
   1.127 -      assert(item->second!=NULL);
   1.128 -      free(item->first);
   1.129 -      free(item->second);
   1.130 -      free(item);
   1.131 -      return TRUE;
   1.132 -    } /* if */
   1.133 -    cur=cur->next;
   1.134 -  } /* while */
   1.135 -  return FALSE;
   1.136 -}
   1.137 -
   1.138 -/* ----- string list functions ----------------------------------- */
   1.139 -static stringlist *insert_string(stringlist *root,char *string)
   1.140 -{
   1.141 -  stringlist *cur;
   1.142 -
   1.143 -  assert(string!=NULL);
   1.144 -  if ((cur=(stringlist*)malloc(sizeof(stringlist)))==NULL)
   1.145 -    error(103);       /* insufficient memory (fatal error) */
   1.146 -  if ((cur->line=duplicatestring(string))==NULL)
   1.147 -    error(103);       /* insufficient memory (fatal error) */
   1.148 -  /* insert as "last" */
   1.149 -  assert(root!=NULL);
   1.150 -  while (root->next!=NULL)
   1.151 -    root=root->next;
   1.152 -  cur->next=root->next;
   1.153 -  root->next=cur;
   1.154 -  return cur;
   1.155 -}
   1.156 -
   1.157 -static char *get_string(stringlist *root,int index)
   1.158 -{
   1.159 -  stringlist *cur;
   1.160 -
   1.161 -  assert(root!=NULL);
   1.162 -  cur=root->next;
   1.163 -  while (cur!=NULL && index-->0)
   1.164 -    cur=cur->next;
   1.165 -  if (cur!=NULL) {
   1.166 -    assert(cur->line!=NULL);
   1.167 -    return cur->line;
   1.168 -  } /* if */
   1.169 -  return NULL;
   1.170 -}
   1.171 -
   1.172 -static int delete_string(stringlist *root,int index)
   1.173 -{
   1.174 -  stringlist *cur,*item;
   1.175 -
   1.176 -  assert(root!=NULL);
   1.177 -  for (cur=root; cur->next!=NULL && index>0; cur=cur->next,index--)
   1.178 -    /* nothing */;
   1.179 -  if (cur->next!=NULL) {
   1.180 -    item=cur->next;
   1.181 -    cur->next=item->next;       /* unlink from list */
   1.182 -    assert(item->line!=NULL);
   1.183 -    free(item->line);
   1.184 -    free(item);
   1.185 -    return TRUE;
   1.186 -  } /* if */
   1.187 -  return FALSE;
   1.188 -}
   1.189 -
   1.190 -SC_FUNC void delete_stringtable(stringlist *root)
   1.191 -{
   1.192 -  stringlist *cur,*next;
   1.193 -
   1.194 -  assert(root!=NULL);
   1.195 -  cur=root->next;
   1.196 -  while (cur!=NULL) {
   1.197 -    next=cur->next;
   1.198 -    assert(cur->line!=NULL);
   1.199 -    free(cur->line);
   1.200 -    free(cur);
   1.201 -    cur=next;
   1.202 -  } /* while */
   1.203 -  memset(root,0,sizeof(stringlist));
   1.204 -}
   1.205 -
   1.206 -
   1.207 -/* ----- alias table --------------------------------------------- */
   1.208 -static stringpair alias_tab = {NULL, NULL, NULL};   /* alias table */
   1.209 -
   1.210 -SC_FUNC stringpair *insert_alias(char *name,char *alias)
   1.211 -{
   1.212 -  stringpair *cur;
   1.213 -
   1.214 -  assert(name!=NULL);
   1.215 -  assert(strlen(name)<=sNAMEMAX);
   1.216 -  assert(alias!=NULL);
   1.217 -  assert(strlen(alias)<=sNAMEMAX);
   1.218 -  if ((cur=insert_stringpair(&alias_tab,name,alias,strlen(name)))==NULL)
   1.219 -    error(103);       /* insufficient memory (fatal error) */
   1.220 -  return cur;
   1.221 -}
   1.222 -
   1.223 -SC_FUNC int lookup_alias(char *target,char *name)
   1.224 -{
   1.225 -  stringpair *cur=find_stringpair(alias_tab.next,name,strlen(name));
   1.226 -  if (cur!=NULL) {
   1.227 -    assert(strlen(cur->second)<=sNAMEMAX);
   1.228 -    strcpy(target,cur->second);
   1.229 -  } /* if */
   1.230 -  return cur!=NULL;
   1.231 -}
   1.232 -
   1.233 -SC_FUNC void delete_aliastable(void)
   1.234 -{
   1.235 -  delete_stringpairtable(&alias_tab);
   1.236 -}
   1.237 -
   1.238 -/* ----- include paths list -------------------------------------- */
   1.239 -static stringlist includepaths = {NULL, NULL};  /* directory list for include files */
   1.240 -
   1.241 -SC_FUNC stringlist *insert_path(char *path)
   1.242 -{
   1.243 -  return insert_string(&includepaths,path);
   1.244 -}
   1.245 -
   1.246 -SC_FUNC char *get_path(int index)
   1.247 -{
   1.248 -  return get_string(&includepaths,index);
   1.249 -}
   1.250 -
   1.251 -SC_FUNC void delete_pathtable(void)
   1.252 -{
   1.253 -  delete_stringtable(&includepaths);
   1.254 -  assert(includepaths.next==NULL);
   1.255 -}
   1.256 -
   1.257 -
   1.258 -/* ----- text substitution patterns ------------------------------ */
   1.259 -#if !defined NO_DEFINE
   1.260 -
   1.261 -static stringpair substpair = { NULL, NULL, NULL};  /* list of substitution pairs */
   1.262 -
   1.263 -static stringpair *substindex['z'-PUBLIC_CHAR+1]; /* quick index to first character */
   1.264 -static void adjustindex(char c)
   1.265 -{
   1.266 -  stringpair *cur;
   1.267 -  assert(c>='A' && c<='Z' || c>='a' && c<='z' || c=='_' || c==PUBLIC_CHAR);
   1.268 -  assert(PUBLIC_CHAR<'A' && 'A'<'_' && '_'<'z');
   1.269 -
   1.270 -  for (cur=substpair.next; cur!=NULL && cur->first[0]!=c; cur=cur->next)
   1.271 -    /* nothing */;
   1.272 -  substindex[(int)c-PUBLIC_CHAR]=cur;
   1.273 -}
   1.274 -
   1.275 -SC_FUNC stringpair *insert_subst(char *pattern,char *substitution,int prefixlen)
   1.276 -{
   1.277 -  stringpair *cur;
   1.278 -
   1.279 -  assert(pattern!=NULL);
   1.280 -  assert(substitution!=NULL);
   1.281 -  if ((cur=insert_stringpair(&substpair,pattern,substitution,prefixlen))==NULL)
   1.282 -    error(103);       /* insufficient memory (fatal error) */
   1.283 -  adjustindex(*pattern);
   1.284 -  return cur;
   1.285 -}
   1.286 -
   1.287 -SC_FUNC stringpair *find_subst(char *name,int length)
   1.288 -{
   1.289 -  stringpair *item;
   1.290 -  assert(name!=NULL);
   1.291 -  assert(length>0);
   1.292 -  assert(*name>='A' && *name<='Z' || *name>='a' && *name<='z' || *name=='_' || *name==PUBLIC_CHAR);
   1.293 -  item=substindex[(int)*name-PUBLIC_CHAR];
   1.294 -  if (item!=NULL)
   1.295 -    item=find_stringpair(item,name,length);
   1.296 -  return item;
   1.297 -}
   1.298 -
   1.299 -SC_FUNC int delete_subst(char *name,int length)
   1.300 -{
   1.301 -  stringpair *item;
   1.302 -  assert(name!=NULL);
   1.303 -  assert(length>0);
   1.304 -  assert(*name>='A' && *name<='Z' || *name>='a' && *name<='z' || *name=='_' || *name==PUBLIC_CHAR);
   1.305 -  item=substindex[(int)*name-PUBLIC_CHAR];
   1.306 -  if (item!=NULL)
   1.307 -    item=find_stringpair(item,name,length);
   1.308 -  if (item==NULL)
   1.309 -    return FALSE;
   1.310 -  delete_stringpair(&substpair,item);
   1.311 -  adjustindex(*name);
   1.312 -  return TRUE;
   1.313 -}
   1.314 -
   1.315 -SC_FUNC void delete_substtable(void)
   1.316 -{
   1.317 -  int i;
   1.318 -  delete_stringpairtable(&substpair);
   1.319 -  for (i=0; i<sizeof substindex/sizeof substindex[0]; i++)
   1.320 -    substindex[i]=NULL;
   1.321 -}
   1.322 -
   1.323 -#endif /* !defined NO_SUBST */
   1.324 -
   1.325 -
   1.326 -/* ----- input file list ----------------------------------------- */
   1.327 -static stringlist sourcefiles = {NULL, NULL};
   1.328 -
   1.329 -SC_FUNC stringlist *insert_sourcefile(char *string)
   1.330 -{
   1.331 -  return insert_string(&sourcefiles,string);
   1.332 -}
   1.333 -
   1.334 -SC_FUNC char *get_sourcefile(int index)
   1.335 -{
   1.336 -  return get_string(&sourcefiles,index);
   1.337 -}
   1.338 -
   1.339 -SC_FUNC void delete_sourcefiletable(void)
   1.340 -{
   1.341 -  delete_stringtable(&sourcefiles);
   1.342 -  assert(sourcefiles.next==NULL);
   1.343 -}
   1.344 -
   1.345 -
   1.346 -/* ----- documentation tags -------------------------------------- */
   1.347 -#if !defined SC_LIGHT
   1.348 -static stringlist docstrings = {NULL, NULL};
   1.349 -
   1.350 -SC_FUNC stringlist *insert_docstring(char *string)
   1.351 -{
   1.352 -  return insert_string(&docstrings,string);
   1.353 -}
   1.354 -
   1.355 -SC_FUNC char *get_docstring(int index)
   1.356 -{
   1.357 -  return get_string(&docstrings,index);
   1.358 -}
   1.359 -
   1.360 -SC_FUNC void delete_docstring(int index)
   1.361 -{
   1.362 -  delete_string(&docstrings,index);
   1.363 -}
   1.364 -
   1.365 -SC_FUNC void delete_docstringtable(void)
   1.366 -{
   1.367 -  delete_stringtable(&docstrings);
   1.368 -  assert(docstrings.next==NULL);
   1.369 -}
   1.370 -#endif /* !defined SC_LIGHT */
   1.371 -
   1.372 -
   1.373 -/* ----- autolisting --------------------------------------------- */
   1.374 -static stringlist autolist = {NULL, NULL};
   1.375 -
   1.376 -SC_FUNC stringlist *insert_autolist(char *string)
   1.377 -{
   1.378 -  return insert_string(&autolist,string);
   1.379 -}
   1.380 -
   1.381 -SC_FUNC char *get_autolist(int index)
   1.382 -{
   1.383 -  return get_string(&autolist,index);
   1.384 -}
   1.385 -
   1.386 -SC_FUNC void delete_autolisttable(void)
   1.387 -{
   1.388 -  delete_stringtable(&autolist);
   1.389 -  assert(autolist.next==NULL);
   1.390 -}
   1.391 -
   1.392 -
   1.393 -/* ----- debug information --------------------------------------- */
   1.394 -
   1.395 -/* These macros are adapted from LibDGG libdgg-int64.h, see
   1.396 - * http://www.dennougedougakkai-ndd.org/pub/libdgg/
   1.397 - */
   1.398 -#if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
   1.399 -  #define __STDC_FORMAT_MACROS
   1.400 -  #define __STDC_CONSTANT_MACROS
   1.401 -  #include <inttypes.h>         /* automatically includes stdint.h */
   1.402 -#elif (defined _MSC_VER || defined __BORLANDC__) && (defined _I64_MAX || defined HAVE_I64)
   1.403 -  #define PRId64 "I64d"
   1.404 -  #define PRIx64 "I64x"
   1.405 -#else
   1.406 -  #define PRId64 "lld"
   1.407 -  #define PRIx64 "llx"
   1.408 -#endif
   1.409 -#if PAWN_CELL_SIZE==64
   1.410 -  #define PRIdC  PRId64
   1.411 -  #define PRIxC  PRIx64
   1.412 -#elif PAWN_CELL_SIZE==32
   1.413 -  #define PRIdC  "ld"
   1.414 -  #define PRIxC  "lx"
   1.415 -#else
   1.416 -  #define PRIdC  "d"
   1.417 -  #define PRIxC  "x"
   1.418 -#endif
   1.419 -
   1.420 -static stringlist dbgstrings = {NULL, NULL};
   1.421 -
   1.422 -SC_FUNC stringlist *insert_dbgfile(const char *filename)
   1.423 -{
   1.424 -
   1.425 -  if (sc_status==statWRITE && (sc_debug & sSYMBOLIC)!=0) {
   1.426 -    char string[_MAX_PATH+40];
   1.427 -    assert(filename!=NULL);
   1.428 -    assert(strlen(filename)+40<sizeof string);
   1.429 -    sprintf(string,"F:%" PRIxC " %s",code_idx,filename);
   1.430 -    return insert_string(&dbgstrings,string);
   1.431 -  } /* if */
   1.432 -  return NULL;
   1.433 -}
   1.434 -
   1.435 -SC_FUNC stringlist *insert_dbgline(int linenr)
   1.436 -{
   1.437 -  if (sc_status==statWRITE && (sc_debug & sSYMBOLIC)!=0) {
   1.438 -    char string[40];
   1.439 -    if (linenr>0)
   1.440 -      linenr--;         /* line numbers are zero-based in the debug information */
   1.441 -    sprintf(string,"L:%" PRIxC " %x",code_idx,linenr);
   1.442 -    return insert_string(&dbgstrings,string);
   1.443 -  } /* if */
   1.444 -  return NULL;
   1.445 -}
   1.446 -
   1.447 -SC_FUNC stringlist *insert_dbgsymbol(symbol *sym)
   1.448 -{
   1.449 -  if (sc_status==statWRITE && (sc_debug & sSYMBOLIC)!=0) {
   1.450 -    char string[2*sNAMEMAX+128];
   1.451 -    char symname[2*sNAMEMAX+16];
   1.452 -
   1.453 -    funcdisplayname(symname,sym->name);
   1.454 -    /* address tag:name codestart codeend ident vclass [tag:dim ...] */
   1.455 -    if (sym->ident==iFUNCTN) {
   1.456 -      sprintf(string,"S:%" PRIxC " %x:%s %" PRIxC " %" PRIxC " %x %x",
   1.457 -              sym->addr,sym->tag,symname,sym->addr,sym->codeaddr,sym->ident,sym->vclass);
   1.458 -    } else {
   1.459 -      sprintf(string,"S:%" PRIxC " %x:%s %" PRIxC " %" PRIxC " %x %x",
   1.460 -              sym->addr,sym->tag,symname,sym->codeaddr,code_idx,sym->ident,sym->vclass);
   1.461 -    } /* if */
   1.462 -    if (sym->ident==iARRAY || sym->ident==iREFARRAY) {
   1.463 -      #if !defined NDEBUG
   1.464 -        int count=sym->dim.array.level;
   1.465 -      #endif
   1.466 -      symbol *sub;
   1.467 -      strcat(string," [ ");
   1.468 -      for (sub=sym; sub!=NULL; sub=finddepend(sub)) {
   1.469 -        assert(sub->dim.array.level==count--);
   1.470 -        sprintf(string+strlen(string),"%x:%x ",sub->x.tags.index,sub->dim.array.length);
   1.471 -      } /* for */
   1.472 -      strcat(string,"]");
   1.473 -    } /* if */
   1.474 -
   1.475 -    return insert_string(&dbgstrings,string);
   1.476 -  } /* if */
   1.477 -  return NULL;
   1.478 -}
   1.479 -
   1.480 -SC_FUNC char *get_dbgstring(int index)
   1.481 -{
   1.482 -  return get_string(&dbgstrings,index);
   1.483 -}
   1.484 -
   1.485 -SC_FUNC void delete_dbgstringtable(void)
   1.486 -{
   1.487 -  delete_stringtable(&dbgstrings);
   1.488 -  assert(dbgstrings.next==NULL);
   1.489 -}