1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
/*
* Pass 3 for Disassemblers
* Copyright (C) 1995-2007 by Jeffery L. Post
* j_post <AT> pacbell <DOT> net
*
* Version 3.4.1 - 2007/09/02
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <ctype.h>
#include "common.h"
//
// Defined Constants
//
/*
MISC_EQU_TAG is used in pass 3 to determine whether a miscellaneous
equate statement should be generated. The equate statement will be
written to the disassembly file if the location is referenced by some
instruction and either it is a split reference or the location is
uninitialized and the location does not have an entry in the label
table. If the above requirements are met except that the location has
an entry in the label table, then an equate statement will be generated
as a label equate.
*/
#define MISC_EQU_TAG ((pflag & PF_REF) && ((pflag & PF_SPLIT) | (pflag & PF_NOINIT)))
bool isnumeric(char *str);
//
// Global variables
//
/* none */
// Return TRUE if string is a simple expression based on a defined
// symbol, label, or operand name, eg: label+10 or symbol*4
bool isnumexpression(char *str)
{
int i, len;
bool arith = FALSE, valid = FALSE;
char name[MAX_LINE];
len = strlen(str);
for (i=0; i<len && !arith; i++)
{
switch (str[i])
{
case '+':
case '-':
case '*':
case '/':
arith = TRUE;
name[i] = '\0';
break;
default:
name[i] = str[i];
break;
}
}
name[i] = '\0';
if (arith) // appears to be an arithmetic expression
{
if (find_name(name, label_count, lab_val_index)) // is it a valid label?
valid = TRUE;
else if (find_name(name, symbol_count, sym_val_index)) // is it a valid symbol?
valid = TRUE;
else if (find_name(name, name_count, name_val_index)) // is it a valid operand name?
valid = TRUE;
}
if (arith && valid) // arithmetic expression based on a defined label, symbol, or name
return TRUE;
return FALSE;
}
// Return TRUE if string is purely numeric (eg: "0800h" or "1024" or "10110b")
bool isnumeric(char *str)
{
bool result = TRUE;
bool hex = FALSE;
bool binary = FALSE;
int i, len;
len = strlen(str);
if (toupper(str[len - 1]) == 'H')
{
hex = TRUE;
--len;
}
else if (toupper(str[len - 1]) == 'B')
{
binary = TRUE;
--len;
}
i = 0;
if (!hex && !binary) // if decimal number
{
if (str[0] == '-' && len > 1) // allow negative
i = 1;
}
for ( ; i<len; i++)
{
if (!str[i] || str[i] == '\n')
break;
if (hex) // hexadecimal number
{
if (!isxdigit(str[i]))
{
result = FALSE;
break;
}
}
else if (binary) // binary number
{
if (str[i] != '0' && str[i] != '1')
{
result = FALSE;
break;
}
}
else // decimal number
{
if (!isdigit(str[i]))
{
result = isnumexpression(str); // allow expressions in decimal data, eg: symbol+100
break;
}
}
}
return result;
}
//
// Pass three of disassembly
//
// Search for references to un-initialized data or split references
// and, if found, generate EQU statements for them.
//
void pass3(void)
{
int i, j, k, index, val, adrs, next_adrs, next_val, ok;
struct sym *ptr = NULL;
char *cptr;
int pflag;
bool isnum = FALSE;
printf("\nPass 3 0000");
// Sort tables by name for checking numeric entries
if (label_count)
{
lab_tab = sort_by_name(lab_tab);
ptr = lab_tab;
for (i=0; i<label_count; i++)
{
lab_val_index[i] = ptr;
ptr = ptr->next;
}
}
if (symbol_count)
{
sym_tab = sort_by_name(sym_tab);
ptr = sym_tab;
for (i=0; i<symbol_count; i++)
{
sym_val_index[i] = ptr;
ptr = ptr->next;
}
}
if (name_count)
{
name_tab = sort_by_name(name_tab);
ptr = name_tab;
for (i=0; i<name_count; i++)
{
name_val_index[i] = ptr;
ptr = ptr->next;
}
}
// search label table for labels referenced but not generated
j = TRUE;
for (index=0; index<label_count; index++)
{
ptr = lab_val_index[index];
isnum = isnumeric(ptr->name); // ignore if numeric expression
if (ptr->used && !isnum)
{
val = ptr->val;
val = pgmflags[val];
val &= (PF_NOINIT | PF_SPLIT | PF_REF);
if (val == (PF_REF | PF_SPLIT) || val == (PF_REF | PF_NOINIT))
{
if (j) // do header if first one
{
j = FALSE;
if (!newline || dump)
fprintf(fp, "\n;");
fprintf(fp, "\n;\tLabel equates\n;\n;"
" These are labels in the control file that reference\n;"
" the middle of a multibyte instruction or reference\n;"
" an address outside the initialized space\n;");
}
fprintf(fp, "\n%s\t%s\t", ptr->name, equstr);
puthex(ptr->val);
newline = FALSE;
}
}
}
// now do equates for symbol table
j = TRUE;
for (index=0; index<symbol_count; index++)
{
ptr = sym_val_index[index];
isnum = isnumeric(ptr->name); // ignore if numeric expression
if (ptr->used && !isnum)
{
if (j) // do header if first one
{
j = FALSE;
if (!newline || dump)
fprintf(fp, "\n;");
fprintf(fp, "\n;\tSymbol equates\n;\n;"
" These are symbols from the control\n;"
" file that are referenced in the code\n;");
}
fprintf(fp, "\n%s\t%s\t", ptr->name, equstr);
puthex(ptr->val);
newline = FALSE;
}
}
// now do equates for operand name table
j = TRUE;
ptr = name_tab;
for (index=0; index<name_count; index++)
{
ok = FALSE;
isnum = isnumeric(ptr->name); // ignore if numeric expression
if ((ptr->used != 255) && !isnum)
{
if (!strcasecmp(ptr->name, ptr->next->name))
{
adrs = ptr->val;
val = pgmmem[adrs];
if (pgmflags[adrs] & PF_2BYTE)
#ifdef CPU_BIG_ENDIAN
{
val <<= 8;
val |= pgmmem[adrs + 1];
}
#else
val |= (pgmmem[adrs + 1] << 8);
#endif
next_adrs = ptr->next->val;
next_val = pgmmem[next_adrs];
if (pgmflags[next_adrs] & PF_2BYTE)
#ifdef CPU_BIG_ENDIAN
{
next_val <<= 8;
next_val |= pgmmem[next_adrs + 1];
}
#else
next_val |= (pgmmem[next_adrs + 1] << 8);
#endif
if (val != next_val)
ok = TRUE;
}
else
ok = TRUE;
if (ok)
{
if (j) // do header if first one
{
j = FALSE;
if (!newline || dump)
fprintf(fp, "\n;");
fprintf(fp, "\n;\tOperand symbol equates\n;\n;"
" These are operand symbols from the control\n;"
" file that are referenced in the code\n;");
}
adrs = ptr->val;
val = pgmmem[adrs] & 0xff;
if (pgmflags[adrs] & PF_2BYTE)
#ifdef CPU_BIG_ENDIAN
{
val <<= 8;
val |= pgmmem[adrs + 1];
}
#else
val |= (pgmmem[adrs + 1] << 8);
#endif
fprintf(fp, "\n%s\t%s\t", ptr->name, equstr);
puthex(val);
newline = FALSE;
}
}
ptr = ptr->next;
}
// to do miscellaneous equates, we need to resort labels by value
if (label_count)
lab_tab = sort(lab_tab, lab_val_index, label_count);
j = TRUE;
for (i=0; ; )
{
k = i & 0xfff;
pflag = pgmflags[i];
// if location is referenced and un-initialized or is a split ref
if (MISC_EQU_TAG)
{
cptr = find_entry(i, label_count, lab_val_index);
if ((cptr == NULL) && !(pflag & PF_LABGEN)) // if not in label list
{
if (j) // do header if first one
{
j = FALSE;
if (!newline || dump)
fprintf(fp, "\n;");
fprintf(fp, "\n;\tMiscellaneous equates\n;\n;"
" These are addresses referenced in the code but\n;"
" which are in the middle of a multibyte instruction\n;"
" or are addresses outside the initialized space\n;");
newline = FALSE;
}
if (!upperflag)
fprintf(fp, "\nX%04x\t%s\t", i, equstr); // do EQU statement
else
fprintf(fp, "\nX%04X\t%s\t", i, equstr);
puthex(i);
}
}
i++;
if (!(i & WORD_MASK))
break;
if ((i & 0xfff) < k)
printf("\rPass 3 %04x", i);
}
printf("\rPass 3 - Equate generation complete");
if (!newline || dump)
fprintf(fp, "\n;");
if (upperflag)
fprintf(fp, "\n\tEND\n;\n\n");
else
fprintf(fp, "\n\tend\n;\n\n");
fflush(fp);
fclose(fp);
} // End of Pass 3
// end of dispass3.c
|