| 1 | /* |
| 2 | the DASM macro assembler (aka small systems cross assembler) |
| 3 | |
| 4 | Copyright (c) 1988-2002 by Matthew Dillon. |
| 5 | Copyright (c) 1995 by Olaf "Rhialto" Seibert. |
| 6 | Copyright (c) 2003-2008 by Andrew Davie. |
| 7 | Copyright (c) 2008 by Peter H. Froehlich. |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License along |
| 20 | with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * SYMBOLS.C |
| 26 | */ |
| 27 | |
| 28 | #include "asm.h" |
| 29 | |
| 30 | static unsigned int hash1(const char *str, int len); |
| 31 | SYMBOL *allocsymbol(void); |
| 32 | |
| 33 | static SYMBOL org; |
| 34 | static SYMBOL special; |
| 35 | static SYMBOL specchk; |
| 36 | |
| 37 | void setspecial(int value, int flags) |
| 38 | { |
| 39 | special.value = value; |
| 40 | special.flags = flags; |
| 41 | } |
| 42 | |
| 43 | SYMBOL *findsymbol(const char *str, int len) |
| 44 | { |
| 45 | unsigned int h1; |
| 46 | SYMBOL *sym; |
| 47 | char buf[MAX_SYM_LEN + 14]; /* historical */ |
| 48 | |
| 49 | if ( len > MAX_SYM_LEN ) |
| 50 | len = MAX_SYM_LEN; |
| 51 | |
| 52 | if (str[0] == '.') |
| 53 | { |
| 54 | if (len == 1) |
| 55 | { |
| 56 | if (Csegment->flags & SF_RORG) |
| 57 | { |
| 58 | org.flags = Csegment->rflags & SYM_UNKNOWN; |
| 59 | org.value = Csegment->rorg; |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | org.flags = Csegment->flags & SYM_UNKNOWN; |
| 64 | org.value = Csegment->org; |
| 65 | } |
| 66 | return &org; |
| 67 | } |
| 68 | if (len == 2 && str[1] == '.') |
| 69 | return &special; |
| 70 | if (len == 3 && str[1] == '.' && str[2] == '.') |
| 71 | { |
| 72 | specchk.flags = 0; |
| 73 | specchk.value = CheckSum; |
| 74 | return &specchk; |
| 75 | } |
| 76 | sprintf(buf, "%ld%.*s", Localindex, len, str); |
| 77 | len = strlen(buf); |
| 78 | str = buf; |
| 79 | } |
| 80 | |
| 81 | else if (str[len - 1] == '$') |
| 82 | { |
| 83 | sprintf(buf, "%ld$%.*s", Localdollarindex, len, str); |
| 84 | len = strlen(buf); |
| 85 | str = buf; |
| 86 | } |
| 87 | |
| 88 | h1 = hash1(str, len); |
| 89 | for (sym = SHash[h1]; sym; sym = sym->next) |
| 90 | { |
| 91 | if ((sym->namelen == len) && !memcmp(sym->name, str, len)) |
| 92 | break; |
| 93 | } |
| 94 | return sym; |
| 95 | } |
| 96 | |
| 97 | SYMBOL *CreateSymbol( const char *str, int len ) |
| 98 | { |
| 99 | SYMBOL *sym; |
| 100 | unsigned int h1; |
| 101 | char buf[ MAX_SYM_LEN + 14 ]; /* historical */ |
| 102 | |
| 103 | if (len > MAX_SYM_LEN ) |
| 104 | len = MAX_SYM_LEN; |
| 105 | |
| 106 | if (str[0] == '.') |
| 107 | { |
| 108 | sprintf(buf, "%ld%.*s", Localindex, len, str); |
| 109 | len = strlen(buf); |
| 110 | str = buf; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | else if (str[len - 1] == '$') |
| 115 | { |
| 116 | sprintf(buf, "%ld$%.*s", Localdollarindex, len, str); |
| 117 | len = strlen(buf); |
| 118 | str = buf; |
| 119 | } |
| 120 | |
| 121 | sym = allocsymbol(); |
| 122 | sym->name = permalloc(len+1); |
| 123 | memcpy(sym->name, str, len); /* permalloc zeros the array for us */ |
| 124 | sym->namelen = len; |
| 125 | h1 = hash1(str, len); |
| 126 | sym->next = SHash[h1]; |
| 127 | sym->flags= SYM_UNKNOWN; |
| 128 | SHash[h1] = sym; |
| 129 | return sym; |
| 130 | } |
| 131 | |
| 132 | static unsigned int hash1(const char *str, int len) |
| 133 | { |
| 134 | uint8_t a = 0; |
| 135 | uint8_t b = 0; |
| 136 | while (len--) { // this is Fletcher's checksum, better distribution, faster |
| 137 | a += *str++; |
| 138 | b += a; |
| 139 | } |
| 140 | return ((((a << 8) & 0xFF00) | (b & 0xFF)) ) & SHASHAND; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Label Support Routines |
| 145 | */ |
| 146 | |
| 147 | void programlabel(void) |
| 148 | { |
| 149 | int len; |
| 150 | SYMBOL *sym; |
| 151 | SEGMENT *cseg = Csegment; |
| 152 | char *str; |
| 153 | unsigned char rorg = cseg->flags & SF_RORG; |
| 154 | unsigned char cflags = (rorg) ? cseg->rflags : cseg->flags; |
| 155 | unsigned long pc = (rorg) ? cseg->rorg : cseg->org; |
| 156 | |
| 157 | Plab = cseg->org; |
| 158 | Pflags = cseg->flags; |
| 159 | str = Av[0]; |
| 160 | if (*str == 0) |
| 161 | return; |
| 162 | len = strlen(str); |
| 163 | |
| 164 | |
| 165 | if (str[len-1] == ':') |
| 166 | --len; |
| 167 | |
| 168 | if (str[0] != '.' && str[len-1] != '$') |
| 169 | { |
| 170 | Lastlocaldollarindex++; |
| 171 | Localdollarindex = Lastlocaldollarindex; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Redo: unknown and referenced |
| 176 | * referenced and origin not known |
| 177 | * known and phase error (origin known) |
| 178 | */ |
| 179 | |
| 180 | if ((sym = findsymbol(str, len)) != NULL) |
| 181 | { |
| 182 | if ((sym->flags & (SYM_UNKNOWN|SYM_REF)) == (SYM_UNKNOWN|SYM_REF)) |
| 183 | { |
| 184 | ++Redo; |
| 185 | Redo_why |= REASON_FORWARD_REFERENCE; |
| 186 | if (Xdebug) |
| 187 | printf("redo 13: '%s' %04x %04x\n", sym->name, sym->flags, cflags); |
| 188 | } |
| 189 | else if ((cflags & SYM_UNKNOWN) && (sym->flags & SYM_REF)) |
| 190 | { |
| 191 | ++Redo; |
| 192 | Redo_why |= REASON_FORWARD_REFERENCE; |
| 193 | } |
| 194 | else if (!(cflags & SYM_UNKNOWN) && !(sym->flags & SYM_UNKNOWN)) |
| 195 | { |
| 196 | if (pc != sym->value) |
| 197 | { |
| 198 | |
| 199 | /* |
| 200 | * If we had an unevaluated IF expression in the |
| 201 | * previous pass, don't complain about phase errors |
| 202 | * too loudly. |
| 203 | */ |
| 204 | |
| 205 | //FIX: calling asmerr with ERROR_LABEL_MISMATCH is fatal. The clause |
| 206 | // below was causing aborts if verbosity was up, even when the |
| 207 | // phase errors were the result of unevaluated IF expressions in |
| 208 | // the previous pass. |
| 209 | |
| 210 | //if (F_verbose >= 1 || !(Redo_if & (REASON_OBSCURE))) |
| 211 | |
| 212 | if (!(Redo_if & (REASON_OBSCURE))) |
| 213 | { |
| 214 | char sBuffer[ MAX_SYM_LEN * 4 ]; |
| 215 | sprintf( sBuffer, "%s %s", sym->name, sftos( sym->value, 0 ) ); |
| 216 | asmerr( ERROR_LABEL_MISMATCH, false, sBuffer ); |
| 217 | } |
| 218 | ++Redo; |
| 219 | Redo_why |= REASON_PHASE_ERROR; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | sym = CreateSymbol( str, len ); |
| 226 | } |
| 227 | sym->value = pc; |
| 228 | sym->flags = (sym->flags & ~SYM_UNKNOWN) | (cflags & SYM_UNKNOWN); |
| 229 | } |
| 230 | |
| 231 | SYMBOL *SymAlloc; |
| 232 | |
| 233 | SYMBOL *allocsymbol(void) |
| 234 | { |
| 235 | SYMBOL *sym; |
| 236 | |
| 237 | if (SymAlloc) |
| 238 | { |
| 239 | sym = SymAlloc; |
| 240 | SymAlloc = SymAlloc->next; |
| 241 | memset(sym, 0, sizeof(SYMBOL)); |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | sym = (SYMBOL *)permalloc(sizeof(SYMBOL)); |
| 246 | } |
| 247 | return sym; |
| 248 | } |
| 249 | |
| 250 | /* defined but not used [phf] */ |
| 251 | /* |
| 252 | static void freesymbol(SYMBOL *sym) |
| 253 | { |
| 254 | sym->next = SymAlloc; |
| 255 | SymAlloc = sym; |
| 256 | } |
| 257 | */ |
| 258 | |
| 259 | |
| 260 | |
| 261 | void FreeSymbolList(SYMBOL *sym) |
| 262 | { |
| 263 | SYMBOL *next; |
| 264 | |
| 265 | while (sym) |
| 266 | { |
| 267 | next = sym->next; |
| 268 | sym->next = SymAlloc; |
| 269 | if (sym->flags & SYM_STRING) |
| 270 | free(sym->string); |
| 271 | SymAlloc = sym; |
| 272 | sym = next; |
| 273 | } |
| 274 | } |
| 275 |