|
|
1.1 ! root 1: /* ! 2: * static char ID_smblsc[] = "@(#) symbols.c: 1.5 3/11/83"; ! 3: */ ! 4: ! 5: #include <stdio.h> ! 6: #include <string.h> ! 7: #include "syms.h" ! 8: #include "systems.h" ! 9: #include "symbols.h" ! 10: #include "symbols2.h" ! 11: ! 12: /* ! 13: * "symbols.c" is a file containing functions for accessing the ! 14: * symbol table. The following functions are povided: ! 15: * ! 16: * newent(string) ! 17: * Creates a new symbol table entry for the symbol with name ! 18: * "string". The type of the symbol is set to undefined. ! 19: * All other fields of the entry are set to zero. ! 20: * ! 21: * lookup(string,install_flag,user_name_flag) ! 22: * Looks up the symbol whose name is "string" in the symbol ! 23: * table and returns a pointer to the entry for that symbol. ! 24: * Install_flag is INSTALL if symbol is to be installed, ! 25: * N_INSTALL if it is only to be looked up. User_name_flag ! 26: * is USRNAME if the symbol is user-defined, MNEMON if it is ! 27: * an instruction mnemonic. ! 28: * ! 29: * ! 30: * traverse(func) ! 31: * Goes through the symbol table and calls the function "func" ! 32: * for each entry. ! 33: * ! 34: * dmpstb() ! 35: * Dumps the symbol table out to the intermediate file ! 36: * "fdstab" after pass one. The file descriptor "fdstab" ! 37: * should be open for writing on the intermediate file that ! 38: * is to contain the symbol table before calling this ! 39: * function. This procedure is only used in the multiple- ! 40: * process version of the assembler. ! 41: * ! 42: * creasyms() ! 43: * Enters the instruction mnemonics found in instab[] into ! 44: * the symbol table. ! 45: * ! 46: * addstr(string) ! 47: * Enters the "string" into the string table. Called by ! 48: * newent(). Space for the string table is initially ! 49: * malloc()-ed in strtabinit(). If "string" would exceed ! 50: * the available space, then the string table is realloc()-ed ! 51: * with a larger size. This procedure is only used in the ! 52: * flexnames version of the assembler. ! 53: * ! 54: * strtabinit() ! 55: * Sets up the string table, with space malloc()-ed. This ! 56: * procedure is only used in the flexnames version of the ! 57: * assembler. ! 58: */ ! 59: ! 60: #if ONEPROC ! 61: extern short passnbr; ! 62: #endif ! 63: ! 64: symbol ! 65: symtab[NSYMS]; ! 66: upsymins ! 67: hashtab[NHASH]; ! 68: ! 69: #if FLEXNAMES ! 70: char *strtab; ! 71: long currindex; ! 72: ! 73: char *realloc(), ! 74: *malloc(); ! 75: #endif ! 76: ! 77: short symcnt = 0; ! 78: ! 79: ! 80: symbol * ! 81: newent(strptr) ! 82: register char *strptr; ! 83: { ! 84: register symbol *symptr; ! 85: register char *ptr1; ! 86: ! 87: if (symcnt >= NSYMS) { ! 88: aerror("Symbol table overflow"); ! 89: return(NULL); ! 90: } ! 91: symptr = &symtab[symcnt++]; ! 92: #if FLEXNAMES ! 93: if (strlen(strptr) > SYMNMLEN) ! 94: { ! 95: symptr->_name.tabentry.zeroes = 0L; ! 96: symptr->_name.tabentry.offset = currindex; ! 97: addstr(strptr); ! 98: } ! 99: else ! 100: #endif ! 101: for (ptr1 = symptr->_name.name; *ptr1++ = *strptr++; ) ! 102: ; ! 103: symptr->styp = UNDEF; ! 104: symptr->value = 0L; ! 105: symptr->tag = 0; ! 106: symptr->maxval = 0; ! 107: return(symptr); ! 108: } ! 109: ! 110: #if PASS1 || ONEPROC ! 111: #if DEBUG ! 112: ! 113: unsigned numcalls, ! 114: numids, ! 115: numcoll; ! 116: #endif ! 117: #endif ! 118: ! 119: upsymins * ! 120: lookup(sptr,install,usrname) ! 121: char *sptr; ! 122: BYTE install; ! 123: BYTE usrname; ! 124: { ! 125: register upsymins ! 126: *hp; ! 127: register char ! 128: *ptr1, ! 129: *ptr2; ! 130: unsigned short register ! 131: ihash = 0, ! 132: hash; ! 133: unsigned short probe; ! 134: upsymins ! 135: *ohp; ! 136: ! 137: ptr1 = sptr; ! 138: #if PASS1 || ONEPROC ! 139: #if DEBUG ! 140: numcalls++; ! 141: #endif ! 142: #endif ! 143: while (*ptr1) { ! 144: ihash = ihash*4 + *ptr1++; ! 145: } ! 146: probe = 1; ! 147: ihash += *--ptr1 * 32; ! 148: hash = ihash % NHASH; ! 149: hp = ohp = &hashtab[hash]; ! 150: do { ! 151: if ((*hp).stp == NULL) { /* free */ ! 152: if (install) { ! 153: #if PASS1 || ONEPROC ! 154: #if DEBUG ! 155: numids++; ! 156: #endif ! 157: #endif ! 158: (*hp).stp = newent(sptr); ! 159: } ! 160: return(hp); ! 161: } ! 162: else ! 163: { ! 164: #if FLEXNAMES ! 165: /* Compare the string given with the symbol string. */ ! 166: /* The symbol string can be in either the symbol entry */ ! 167: /* or the string table. */ ! 168: if (strcmp(sptr,(hp->stp->_name.tabentry.zeroes != 0) ? (hp->stp->_name.name) : (&strtab[hp->stp->_name.tabentry.offset])) == 0) ! 169: #else ! 170: for (ptr1=sptr,ptr2=((*hp).stp)->_name.name; *ptr2==*ptr1++; ) ! 171: if (*ptr2++ == '\0') ! 172: #endif ! 173: { ! 174: #if PASS1 || ONEPROC ! 175: #if ONEPROC ! 176: if (passnbr == 1) { ! 177: #endif ! 178: if (install && (*hp).itp->tag && ! 179: (*hp).itp->snext == NULL) ! 180: { ! 181: (*hp).itp->snext = newent(sptr); ! 182: } /* if (install ...) */ ! 183: if (!usrname && (*hp).itp->tag) { ! 184: return((upsymins *) &((*hp).itp->snext)); ! 185: } /* if (!usrname ...) */ ! 186: #if ONEPROC ! 187: } ! 188: if (!install && !usrname && ! 189: (*hp).itp->tag && (passnbr==2)) ! 190: return((upsymins *) &((*hp).itp->snext)); ! 191: else ! 192: #endif ! 193: #endif ! 194: return(hp); /* found it */ ! 195: } /* if (*ptr2++ == '\0') */ ! 196: ! 197: hash = (hash + probe) /*% NHASH*/; ! 198: hash -= (hash >= NHASH) ? NHASH : 0; ! 199: probe += 2; ! 200: #if PASS1 || ONEPROC ! 201: #if DEBUG ! 202: numcoll++; ! 203: #endif ! 204: #endif ! 205: } /* else */ ! 206: } while ((hp = &hashtab[hash]) != ohp); ! 207: aerror("Hash table overflow"); ! 208: return(NULL); /* can't reach here since `aerror' exits */ ! 209: } ! 210: ! 211: #if !PASS1 ! 212: traverse(func) ! 213: int (*func)(); ! 214: { ! 215: register short index; ! 216: ! 217: for (index=0; index < symcnt; ++index) { ! 218: (*func)(&symtab[index]); ! 219: } ! 220: } ! 221: #endif ! 222: #if PASS1 && !ONEPROC ! 223: ! 224: FILE *fdstab; ! 225: ! 226: dmpstb(){ ! 227: fwrite((char *)symtab,symcnt,SYMBOLL,fdstab); ! 228: } ! 229: ! 230: #endif ! 231: #if PASS1 || ONEPROC ! 232: ! 233: extern instr instab[]; ! 234: ! 235: creasyms() ! 236: { ! 237: register instr *ip; ! 238: register upsymins *hp; ! 239: ! 240: for (ip = instab; ip->name[0] != '\0'; ++ip) { ! 241: hp = lookup(ip->name,N_INSTALL,MNEMON); ! 242: #if DEBUG ! 243: /* Sanity check is "cpp-ed out" with the flexnames change. */ ! 244: if ((*hp).itp == NULL) ! 245: #endif ! 246: (*hp).itp = ip; ! 247: #if DEBUG ! 248: else ! 249: aerror("Duplicate instruction table name"); ! 250: #endif ! 251: } /* for */ ! 252: } ! 253: #endif ! 254: ! 255: ! 256: #if FLEXNAMES ! 257: long size, ! 258: basicsize = 4 * BUFSIZ; ! 259: ! 260: addstr(strptr) ! 261: register char *strptr; ! 262: { ! 263: register int length; ! 264: ! 265: length = strlen(strptr); ! 266: if (length + currindex >= size) ! 267: if ((strtab = realloc(strtab,size += basicsize)) == NULL) ! 268: aerror("cannot realloc string table"); ! 269: strcpy(&strtab[currindex],strptr); ! 270: currindex += length + 1; ! 271: } /* addstr(strptr) */ ! 272: ! 273: ! 274: strtabinit() ! 275: { ! 276: if ((strtab = malloc(size = basicsize)) == NULL) ! 277: aerror("cannot malloc string table"); ! 278: currindex = 4; ! 279: } /* strtabinit() */ ! 280: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.