H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 1 | /* parser.c source line parser for the Netwide Assembler |
| 2 | * |
| 3 | * The Netwide Assembler is copyright (C) 1996 Simon Tatham and |
| 4 | * Julian Hall. All rights reserved. The software is |
| 5 | * redistributable under the licence given in the file "Licence" |
| 6 | * distributed in the NASM archive. |
| 7 | * |
| 8 | * initial version 27/iii/95 by Simon Tatham |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <stddef.h> |
| 14 | #include <string.h> |
| 15 | #include <ctype.h> |
| 16 | |
| 17 | #include "nasm.h" |
| 18 | #include "nasmlib.h" |
| 19 | #include "parser.h" |
| 20 | #include "float.h" |
| 21 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 22 | static long reg_flags[] = { /* sizes and special flags */ |
| 23 | 0, REG8, REG_AL, REG_AX, REG8, REG8, REG16, REG16, REG8, REG_CL, |
| 24 | REG_CREG, REG_CREG, REG_CREG, REG_CR4, REG_CS, REG_CX, REG8, |
| 25 | REG16, REG8, REG_DREG, REG_DREG, REG_DREG, REG_DREG, REG_DREG, |
| 26 | REG_DREG, REG_DESS, REG_DX, REG_EAX, REG32, REG32, REG_ECX, |
| 27 | REG32, REG32, REG_DESS, REG32, REG32, REG_FSGS, REG_FSGS, |
| 28 | MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, |
| 29 | REG16, REG16, REG_DESS, FPU0, FPUREG, FPUREG, FPUREG, FPUREG, |
| 30 | FPUREG, FPUREG, FPUREG, REG_TREG, REG_TREG, REG_TREG, REG_TREG, |
H. Peter Anvin | 4836e33 | 2002-04-30 20:56:43 +0000 | [diff] [blame] | 31 | REG_TREG, |
| 32 | XMMREG, XMMREG, XMMREG, XMMREG, XMMREG, XMMREG, XMMREG, XMMREG |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | enum { /* special tokens */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 36 | S_BYTE, S_DWORD, S_FAR, S_LONG, S_NEAR, S_NOSPLIT, S_QWORD, |
| 37 | S_SHORT, S_TO, S_TWORD, S_WORD |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 40 | static int is_comma_next (void); |
| 41 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 42 | static int i; |
| 43 | static struct tokenval tokval; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 44 | static efunc error; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 45 | static struct ofmt *outfmt; /* Structure of addresses of output routines */ |
| 46 | static loc_t *location; /* Pointer to current line's segment,offset */ |
| 47 | |
| 48 | void parser_global_info (struct ofmt *output, loc_t *locp) |
| 49 | { |
| 50 | outfmt = output; |
| 51 | location = locp; |
| 52 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 53 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 54 | insn *parse_line (int pass, char *buffer, insn *result, |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 55 | efunc errfunc, evalfunc evaluate, ldfunc ldef) |
| 56 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 57 | int operand; |
| 58 | int critical; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 59 | struct eval_hints hints; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 60 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 61 | result->forw_ref = FALSE; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 62 | error = errfunc; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 63 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 64 | stdscan_reset(); |
| 65 | stdscan_bufptr = buffer; |
| 66 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 67 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 68 | result->label = NULL; /* Assume no label */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 69 | result->eops = NULL; /* must do this, whatever happens */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 70 | result->operands = 0; /* must initialise this */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 71 | |
| 72 | if (i==0) { /* blank line - ignore */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 73 | result->opcode = -1; /* and no instruction either */ |
| 74 | return result; |
| 75 | } |
| 76 | if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX && |
| 77 | (i!=TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) { |
| 78 | error (ERR_NONFATAL, "label or instruction expected" |
| 79 | " at start of line"); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 80 | result->opcode = -1; |
| 81 | return result; |
| 82 | } |
| 83 | |
| 84 | if (i == TOKEN_ID) { /* there's a label here */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 85 | result->label = tokval.t_charptr; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 86 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 87 | if (i == ':') { /* skip over the optional colon */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 88 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 89 | } else if (i == 0) { |
| 90 | error (ERR_WARNING|ERR_WARN_OL|ERR_PASS1, |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 91 | "label alone on a line without a colon might be in error"); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 92 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 93 | if (i != TOKEN_INSN || tokval.t_integer != I_EQU) |
| 94 | { |
| 95 | /* |
| 96 | * FIXME: location->segment could be NO_SEG, in which case |
| 97 | * it is possible we should be passing 'abs_seg'. Look into this. |
| 98 | * Work out whether that is *really* what we should be doing. |
| 99 | * Generally fix things. I think this is right as it is, but |
| 100 | * am still not certain. |
| 101 | */ |
| 102 | ldef (result->label, location->segment, |
| 103 | location->offset, NULL, TRUE, FALSE, outfmt, errfunc); |
| 104 | } |
| 105 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 106 | |
| 107 | if (i==0) { |
| 108 | result->opcode = -1; /* this line contains just a label */ |
| 109 | return result; |
| 110 | } |
| 111 | |
| 112 | result->nprefix = 0; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 113 | result->times = 1L; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 114 | |
| 115 | while (i == TOKEN_PREFIX || |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 116 | (i==TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer]))) |
| 117 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 118 | /* |
| 119 | * Handle special case: the TIMES prefix. |
| 120 | */ |
| 121 | if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) { |
| 122 | expr *value; |
| 123 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 124 | i = stdscan(NULL, &tokval); |
| 125 | value = evaluate (stdscan, NULL, &tokval, NULL, pass, error, NULL); |
| 126 | i = tokval.t_type; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 127 | if (!value) { /* but, error in evaluator */ |
| 128 | result->opcode = -1; /* unrecoverable parse error: */ |
| 129 | return result; /* ignore this instruction */ |
| 130 | } |
| 131 | if (!is_simple (value)) { |
| 132 | error (ERR_NONFATAL, |
| 133 | "non-constant argument supplied to TIMES"); |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 134 | result->times = 1L; |
| 135 | } else { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 136 | result->times = value->value; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 137 | if (value->value < 0) { |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 138 | error(ERR_NONFATAL, "TIMES value %d is negative", |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 139 | value->value); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 140 | result->times = 0; |
| 141 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 142 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 143 | } else { |
| 144 | if (result->nprefix == MAXPREFIX) |
| 145 | error (ERR_NONFATAL, |
| 146 | "instruction has more than %d prefixes", MAXPREFIX); |
| 147 | else |
| 148 | result->prefixes[result->nprefix++] = tokval.t_integer; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 149 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | if (i != TOKEN_INSN) { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 154 | if (result->nprefix > 0 && i == 0) { |
| 155 | /* |
| 156 | * Instruction prefixes are present, but no actual |
| 157 | * instruction. This is allowed: at this point we |
| 158 | * invent a notional instruction of RESB 0. |
| 159 | */ |
| 160 | result->opcode = I_RESB; |
| 161 | result->operands = 1; |
| 162 | result->oprs[0].type = IMMEDIATE; |
| 163 | result->oprs[0].offset = 0L; |
| 164 | result->oprs[0].segment = result->oprs[0].wrt = NO_SEG; |
| 165 | return result; |
| 166 | } else { |
| 167 | error (ERR_NONFATAL, "parser: instruction expected"); |
| 168 | result->opcode = -1; |
| 169 | return result; |
| 170 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | result->opcode = tokval.t_integer; |
| 174 | result->condition = tokval.t_inttwo; |
| 175 | |
| 176 | /* |
| 177 | * RESB, RESW and RESD cannot be satisfied with incorrectly |
| 178 | * evaluated operands, since the correct values _must_ be known |
| 179 | * on the first pass. Hence, even in pass one, we set the |
| 180 | * `critical' flag on calling evaluate(), so that it will bomb |
| 181 | * out on undefined symbols. Nasty, but there's nothing we can |
| 182 | * do about it. |
| 183 | * |
| 184 | * For the moment, EQU has the same difficulty, so we'll |
| 185 | * include that. |
| 186 | */ |
| 187 | if (result->opcode == I_RESB || |
| 188 | result->opcode == I_RESW || |
| 189 | result->opcode == I_RESD || |
| 190 | result->opcode == I_RESQ || |
| 191 | result->opcode == I_REST || |
| 192 | result->opcode == I_EQU) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 193 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 194 | critical = pass; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 195 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 196 | else |
| 197 | critical = (pass==2 ? 2 : 0); |
| 198 | |
| 199 | if (result->opcode == I_DB || |
| 200 | result->opcode == I_DW || |
| 201 | result->opcode == I_DD || |
| 202 | result->opcode == I_DQ || |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 203 | result->opcode == I_DT || |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 204 | result->opcode == I_INCBIN) |
| 205 | { |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 206 | extop *eop, **tail = &result->eops, **fixptr; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 207 | int oper_num = 0; |
| 208 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 209 | result->eops_float = FALSE; |
| 210 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 211 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 212 | * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands. |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 213 | */ |
| 214 | while (1) { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 215 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 216 | if (i == 0) |
| 217 | break; |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 218 | fixptr = tail; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 219 | eop = *tail = nasm_malloc(sizeof(extop)); |
| 220 | tail = &eop->next; |
| 221 | eop->next = NULL; |
| 222 | eop->type = EOT_NOTHING; |
| 223 | oper_num++; |
| 224 | |
| 225 | if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) { |
| 226 | eop->type = EOT_DB_STRING; |
| 227 | eop->stringval = tokval.t_charptr; |
| 228 | eop->stringlen = tokval.t_inttwo; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 229 | i = stdscan(NULL, &tokval); /* eat the comma */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 230 | continue; |
| 231 | } |
| 232 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 233 | if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 234 | long sign = +1L; |
| 235 | |
| 236 | if (i == '-') { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 237 | char *save = stdscan_bufptr; |
| 238 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 239 | sign = -1L; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 240 | if (i != TOKEN_FLOAT || !is_comma_next()) { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 241 | stdscan_bufptr = save; |
| 242 | i = tokval.t_type = '-'; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | if (i == TOKEN_FLOAT) { |
| 247 | eop->type = EOT_DB_STRING; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 248 | result->eops_float = TRUE; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 249 | if (result->opcode == I_DD) |
| 250 | eop->stringlen = 4; |
| 251 | else if (result->opcode == I_DQ) |
| 252 | eop->stringlen = 8; |
| 253 | else if (result->opcode == I_DT) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 254 | eop->stringlen = 10; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 255 | else { |
| 256 | error(ERR_NONFATAL, "floating-point constant" |
| 257 | " encountered in `D%c' instruction", |
| 258 | result->opcode == I_DW ? 'W' : 'B'); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 259 | /* |
| 260 | * fix suggested by Pedro Gimeno... original line |
| 261 | * was: |
| 262 | * eop->type = EOT_NOTHING; |
| 263 | */ |
| 264 | eop->stringlen = 0; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 265 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 266 | eop = nasm_realloc(eop, sizeof(extop)+eop->stringlen); |
H. Peter Anvin | 87bc619 | 2002-04-30 20:53:16 +0000 | [diff] [blame] | 267 | tail = &eop->next; |
| 268 | *fixptr = eop; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 269 | eop->stringval = (char *)eop + sizeof(extop); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 270 | if (eop->stringlen < 4 || |
| 271 | !float_const (tokval.t_charptr, sign, |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 272 | (unsigned char *)eop->stringval, |
| 273 | eop->stringlen, error)) |
| 274 | eop->type = EOT_NOTHING; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 275 | i = stdscan(NULL, &tokval); /* eat the comma */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 276 | continue; |
| 277 | } |
| 278 | } |
| 279 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 280 | /* anything else */ |
| 281 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 282 | expr *value; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 283 | value = evaluate (stdscan, NULL, &tokval, NULL, |
| 284 | critical, error, NULL); |
| 285 | i = tokval.t_type; |
| 286 | if (!value) { /* error in evaluator */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 287 | result->opcode = -1;/* unrecoverable parse error: */ |
| 288 | return result; /* ignore this instruction */ |
| 289 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 290 | if (is_unknown(value)) { |
| 291 | eop->type = EOT_DB_NUMBER; |
| 292 | eop->offset = 0; /* doesn't matter what we put */ |
| 293 | eop->segment = eop->wrt = NO_SEG; /* likewise */ |
| 294 | } else if (is_reloc(value)) { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 295 | eop->type = EOT_DB_NUMBER; |
| 296 | eop->offset = reloc_value(value); |
| 297 | eop->segment = reloc_seg(value); |
| 298 | eop->wrt = reloc_wrt(value); |
| 299 | } else { |
| 300 | error (ERR_NONFATAL, |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 301 | "operand %d: expression is not simple" |
| 302 | " or relocatable", oper_num); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 305 | |
| 306 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 307 | * We're about to call stdscan(), which will eat the |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 308 | * comma that we're currently sitting on between |
| 309 | * arguments. However, we'd better check first that it |
| 310 | * _is_ a comma. |
| 311 | */ |
| 312 | if (i == 0) /* also could be EOL */ |
| 313 | break; |
| 314 | if (i != ',') { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 315 | error (ERR_NONFATAL, "comma expected after operand %d", |
| 316 | oper_num); |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 317 | result->opcode = -1;/* unrecoverable parse error: */ |
| 318 | return result; /* ignore this instruction */ |
| 319 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 320 | } |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 321 | |
| 322 | if (result->opcode == I_INCBIN) { |
| 323 | /* |
| 324 | * Correct syntax for INCBIN is that there should be |
| 325 | * one string operand, followed by one or two numeric |
| 326 | * operands. |
| 327 | */ |
| 328 | if (!result->eops || result->eops->type != EOT_DB_STRING) |
| 329 | error (ERR_NONFATAL, "`incbin' expects a file name"); |
| 330 | else if (result->eops->next && |
| 331 | result->eops->next->type != EOT_DB_NUMBER) |
| 332 | error (ERR_NONFATAL, "`incbin': second parameter is", |
| 333 | " non-numeric"); |
| 334 | else if (result->eops->next && result->eops->next->next && |
| 335 | result->eops->next->next->type != EOT_DB_NUMBER) |
| 336 | error (ERR_NONFATAL, "`incbin': third parameter is", |
| 337 | " non-numeric"); |
| 338 | else if (result->eops->next && result->eops->next->next && |
| 339 | result->eops->next->next->next) |
| 340 | error (ERR_NONFATAL, "`incbin': more than three parameters"); |
| 341 | else |
| 342 | return result; |
| 343 | /* |
| 344 | * If we reach here, one of the above errors happened. |
| 345 | * Throw the instruction away. |
| 346 | */ |
| 347 | result->opcode = -1; |
| 348 | return result; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 349 | } else /* DB ... */ |
| 350 | if (oper_num == 0) |
| 351 | error (ERR_WARNING|ERR_PASS1, |
| 352 | "no operand for data declaration"); |
| 353 | else |
| 354 | result->operands = oper_num; |
H. Peter Anvin | d7ed89e | 2002-04-30 20:52:08 +0000 | [diff] [blame] | 355 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 356 | return result; |
| 357 | } |
| 358 | |
| 359 | /* right. Now we begin to parse the operands. There may be up to three |
| 360 | * of these, separated by commas, and terminated by a zero token. */ |
| 361 | |
| 362 | for (operand = 0; operand < 3; operand++) { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 363 | expr *value; /* used most of the time */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 364 | int mref; /* is this going to be a memory ref? */ |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 365 | int bracket; /* is it a [] mref, or a & mref? */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 366 | int setsize = 0; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 367 | |
| 368 | result->oprs[operand].addr_size = 0;/* have to zero this whatever */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 369 | result->oprs[operand].eaflags = 0; /* and this */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 370 | result->oprs[operand].opflags = 0; |
| 371 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 372 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 373 | if (i == 0) break; /* end of operands: get out of here */ |
| 374 | result->oprs[operand].type = 0; /* so far, no override */ |
| 375 | while (i == TOKEN_SPECIAL) {/* size specifiers */ |
| 376 | switch ((int)tokval.t_integer) { |
| 377 | case S_BYTE: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 378 | if (!setsize) /* we want to use only the first */ |
| 379 | result->oprs[operand].type |= BITS8; |
| 380 | setsize = 1; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 381 | break; |
| 382 | case S_WORD: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 383 | if (!setsize) |
| 384 | result->oprs[operand].type |= BITS16; |
| 385 | setsize = 1; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 386 | break; |
| 387 | case S_DWORD: |
| 388 | case S_LONG: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 389 | if (!setsize) |
| 390 | result->oprs[operand].type |= BITS32; |
| 391 | setsize = 1; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 392 | break; |
| 393 | case S_QWORD: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 394 | if (!setsize) |
| 395 | result->oprs[operand].type |= BITS64; |
| 396 | setsize = 1; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 397 | break; |
| 398 | case S_TWORD: |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 399 | if (!setsize) |
| 400 | result->oprs[operand].type |= BITS80; |
| 401 | setsize = 1; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 402 | break; |
| 403 | case S_TO: |
| 404 | result->oprs[operand].type |= TO; |
| 405 | break; |
| 406 | case S_FAR: |
| 407 | result->oprs[operand].type |= FAR; |
| 408 | break; |
| 409 | case S_NEAR: |
| 410 | result->oprs[operand].type |= NEAR; |
| 411 | break; |
| 412 | case S_SHORT: |
| 413 | result->oprs[operand].type |= SHORT; |
| 414 | break; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 415 | default: |
| 416 | error (ERR_NONFATAL, "invalid operand size specification"); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 417 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 418 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 419 | } |
| 420 | |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 421 | if (i == '[' || i == '&') { /* memory reference */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 422 | mref = TRUE; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 423 | bracket = (i == '['); |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 424 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 425 | if (i == TOKEN_SPECIAL) { /* check for address size override */ |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 426 | if (tasm_compatible_mode) { |
| 427 | switch ((int)tokval.t_integer) { |
| 428 | /* For TASM compatibility a size override inside the |
| 429 | * brackets changes the size of the operand, not the |
| 430 | * address type of the operand as it does in standard |
| 431 | * NASM syntax. Hence: |
| 432 | * |
| 433 | * mov eax,[DWORD val] |
| 434 | * |
| 435 | * is valid syntax in TASM compatibility mode. Note that |
| 436 | * you lose the ability to override the default address |
| 437 | * type for the instruction, but we never use anything |
| 438 | * but 32-bit flat model addressing in our code. |
| 439 | */ |
| 440 | case S_BYTE: |
| 441 | result->oprs[operand].type |= BITS8; |
| 442 | break; |
| 443 | case S_WORD: |
| 444 | result->oprs[operand].type |= BITS16; |
| 445 | break; |
| 446 | case S_DWORD: |
| 447 | case S_LONG: |
| 448 | result->oprs[operand].type |= BITS32; |
| 449 | break; |
| 450 | case S_QWORD: |
| 451 | result->oprs[operand].type |= BITS64; |
| 452 | break; |
| 453 | case S_TWORD: |
| 454 | result->oprs[operand].type |= BITS80; |
| 455 | break; |
| 456 | default: |
| 457 | error (ERR_NONFATAL, "invalid operand size specification"); |
| 458 | } |
| 459 | } else { |
H. Peter Anvin | 1cd0e2d | 2002-04-30 21:00:33 +0000 | [diff] [blame] | 460 | /* Standard NASM compatible syntax */ |
| 461 | switch ((int)tokval.t_integer) { |
| 462 | case S_NOSPLIT: |
| 463 | result->oprs[operand].eaflags |= EAF_TIMESTWO; |
| 464 | break; |
| 465 | case S_BYTE: |
| 466 | result->oprs[operand].eaflags |= EAF_BYTEOFFS; |
| 467 | break; |
| 468 | case S_WORD: |
| 469 | result->oprs[operand].addr_size = 16; |
| 470 | result->oprs[operand].eaflags |= EAF_WORDOFFS; |
| 471 | break; |
| 472 | case S_DWORD: |
| 473 | case S_LONG: |
| 474 | result->oprs[operand].addr_size = 32; |
| 475 | result->oprs[operand].eaflags |= EAF_WORDOFFS; |
| 476 | break; |
| 477 | default: |
| 478 | error (ERR_NONFATAL, "invalid size specification in" |
| 479 | " effective address"); |
| 480 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 481 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 482 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 483 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 484 | } else { /* immediate operand, or register */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 485 | mref = FALSE; |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 486 | bracket = FALSE; /* placate optimisers */ |
| 487 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 488 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 489 | value = evaluate (stdscan, NULL, &tokval, |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 490 | &result->oprs[operand].opflags, |
| 491 | critical, error, &hints); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 492 | i = tokval.t_type; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 493 | if (result->oprs[operand].opflags & OPFLAG_FORWARD) { |
| 494 | result->forw_ref = TRUE; |
| 495 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 496 | if (!value) { /* error in evaluator */ |
| 497 | result->opcode = -1; /* unrecoverable parse error: */ |
| 498 | return result; /* ignore this instruction */ |
| 499 | } |
| 500 | if (i == ':' && mref) { /* it was seg:offset */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 501 | /* |
| 502 | * Process the segment override. |
| 503 | */ |
| 504 | if (value[1].type!=0 || value->value!=1 || |
| 505 | REG_SREG & ~reg_flags[value->type]) |
| 506 | error (ERR_NONFATAL, "invalid segment override"); |
| 507 | else if (result->nprefix == MAXPREFIX) |
| 508 | error (ERR_NONFATAL, |
| 509 | "instruction has more than %d prefixes", |
| 510 | MAXPREFIX); |
| 511 | else |
| 512 | result->prefixes[result->nprefix++] = value->type; |
| 513 | |
| 514 | i = stdscan(NULL, &tokval); /* then skip the colon */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 515 | if (i == TOKEN_SPECIAL) { /* another check for size override */ |
| 516 | switch ((int)tokval.t_integer) { |
| 517 | case S_WORD: |
| 518 | result->oprs[operand].addr_size = 16; |
| 519 | break; |
| 520 | case S_DWORD: |
| 521 | case S_LONG: |
| 522 | result->oprs[operand].addr_size = 32; |
| 523 | break; |
| 524 | default: |
| 525 | error (ERR_NONFATAL, "invalid size specification in" |
| 526 | " effective address"); |
| 527 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 528 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 529 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 530 | value = evaluate (stdscan, NULL, &tokval, |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 531 | &result->oprs[operand].opflags, |
| 532 | critical, error, &hints); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 533 | i = tokval.t_type; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 534 | if (result->oprs[operand].opflags & OPFLAG_FORWARD) { |
| 535 | result->forw_ref = TRUE; |
| 536 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 537 | /* and get the offset */ |
| 538 | if (!value) { /* but, error in evaluator */ |
| 539 | result->opcode = -1; /* unrecoverable parse error: */ |
| 540 | return result; /* ignore this instruction */ |
| 541 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 542 | } |
H. Peter Anvin | 6768eb7 | 2002-04-30 20:52:26 +0000 | [diff] [blame] | 543 | if (mref && bracket) { /* find ] at the end */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 544 | if (i != ']') { |
| 545 | error (ERR_NONFATAL, "parser: expecting ]"); |
| 546 | do { /* error recovery again */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 547 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 548 | } while (i != 0 && i != ','); |
| 549 | } else /* we got the required ] */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 550 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 551 | } else { /* immediate operand */ |
| 552 | if (i != 0 && i != ',' && i != ':') { |
| 553 | error (ERR_NONFATAL, "comma or end of line expected"); |
| 554 | do { /* error recovery */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 555 | i = stdscan(NULL, &tokval); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 556 | } while (i != 0 && i != ','); |
| 557 | } else if (i == ':') { |
| 558 | result->oprs[operand].type |= COLON; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /* now convert the exprs returned from evaluate() into operand |
| 563 | * descriptions... */ |
| 564 | |
| 565 | if (mref) { /* it's a memory reference */ |
| 566 | expr *e = value; |
| 567 | int b, i, s; /* basereg, indexreg, scale */ |
| 568 | long o; /* offset */ |
| 569 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 570 | b = i = -1, o = s = 0; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 571 | result->oprs[operand].hintbase = hints.base; |
| 572 | result->oprs[operand].hinttype = hints.type; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 573 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 574 | if (e->type <= EXPR_REG_END) { /* this bit's a register */ |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 575 | if (e->value == 1) /* in fact it can be basereg */ |
| 576 | b = e->type; |
| 577 | else /* no, it has to be indexreg */ |
| 578 | i = e->type, s = e->value; |
| 579 | e++; |
| 580 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 581 | if (e->type && e->type <= EXPR_REG_END) /* it's a 2nd register */ |
| 582 | { |
| 583 | if (b != -1) /* If the first was the base, ... */ |
| 584 | i = e->type, s = e->value; /* second has to be indexreg */ |
| 585 | |
| 586 | else if (e->value != 1) /* If both want to be index */ |
| 587 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 588 | error(ERR_NONFATAL, "beroset-p-592-invalid effective address"); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 589 | result->opcode = -1; |
| 590 | return result; |
| 591 | } |
| 592 | else |
| 593 | b = e->type; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 594 | e++; |
| 595 | } |
| 596 | if (e->type != 0) { /* is there an offset? */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 597 | if (e->type <= EXPR_REG_END) /* in fact, is there an error? */ |
| 598 | { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 599 | error (ERR_NONFATAL, "beroset-p-603-invalid effective address"); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 600 | result->opcode = -1; |
| 601 | return result; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 602 | } |
| 603 | else |
| 604 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 605 | if (e->type == EXPR_UNKNOWN) { |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 606 | o = 0; /* doesn't matter what */ |
| 607 | result->oprs[operand].wrt = NO_SEG; /* nor this */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 608 | result->oprs[operand].segment = NO_SEG; /* or this */ |
| 609 | while (e->type) e++; /* go to the end of the line */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 610 | } |
| 611 | else |
| 612 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 613 | if (e->type == EXPR_SIMPLE) { |
| 614 | o = e->value; |
| 615 | e++; |
| 616 | } |
| 617 | if (e->type == EXPR_WRT) { |
| 618 | result->oprs[operand].wrt = e->value; |
| 619 | e++; |
| 620 | } else |
| 621 | result->oprs[operand].wrt = NO_SEG; |
| 622 | /* |
| 623 | * Look for a segment base type. |
| 624 | */ |
| 625 | if (e->type && e->type < EXPR_SEGBASE) { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 626 | error (ERR_NONFATAL, "beroset-p-630-invalid effective address"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 627 | result->opcode = -1; |
| 628 | return result; |
| 629 | } |
| 630 | while (e->type && e->value == 0) |
| 631 | e++; |
| 632 | if (e->type && e->value != 1) { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 633 | error (ERR_NONFATAL, "beroset-p-637-invalid effective address"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 634 | result->opcode = -1; |
| 635 | return result; |
| 636 | } |
| 637 | if (e->type) { |
| 638 | result->oprs[operand].segment = |
| 639 | e->type - EXPR_SEGBASE; |
| 640 | e++; |
| 641 | } else |
| 642 | result->oprs[operand].segment = NO_SEG; |
| 643 | while (e->type && e->value == 0) |
| 644 | e++; |
| 645 | if (e->type) { |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 646 | error (ERR_NONFATAL, "beroset-p-650-invalid effective address"); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 647 | result->opcode = -1; |
| 648 | return result; |
| 649 | } |
H. Peter Anvin | ea83827 | 2002-04-30 20:51:53 +0000 | [diff] [blame] | 650 | } |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 651 | } |
| 652 | } else { |
| 653 | o = 0; |
| 654 | result->oprs[operand].wrt = NO_SEG; |
| 655 | result->oprs[operand].segment = NO_SEG; |
| 656 | } |
| 657 | |
| 658 | if (e->type != 0) { /* there'd better be nothing left! */ |
H. Peter Anvin | 734b188 | 2002-04-30 21:01:08 +0000 | [diff] [blame] | 659 | error (ERR_NONFATAL, "beroset-p-663-invalid effective address"); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 660 | result->opcode = -1; |
| 661 | return result; |
| 662 | } |
| 663 | |
| 664 | result->oprs[operand].type |= MEMORY; |
| 665 | if (b==-1 && (i==-1 || s==0)) |
| 666 | result->oprs[operand].type |= MEM_OFFS; |
| 667 | result->oprs[operand].basereg = b; |
| 668 | result->oprs[operand].indexreg = i; |
| 669 | result->oprs[operand].scale = s; |
| 670 | result->oprs[operand].offset = o; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 671 | } |
| 672 | else /* it's not a memory reference */ |
| 673 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 674 | if (is_just_unknown(value)) { /* it's immediate but unknown */ |
| 675 | result->oprs[operand].type |= IMMEDIATE; |
| 676 | result->oprs[operand].offset = 0; /* don't care */ |
| 677 | result->oprs[operand].segment = NO_SEG; /* don't care again */ |
| 678 | result->oprs[operand].wrt = NO_SEG;/* still don't care */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 679 | } |
| 680 | else if (is_reloc(value)) /* it's immediate */ |
| 681 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 682 | result->oprs[operand].type |= IMMEDIATE; |
| 683 | result->oprs[operand].offset = reloc_value(value); |
| 684 | result->oprs[operand].segment = reloc_seg(value); |
| 685 | result->oprs[operand].wrt = reloc_wrt(value); |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 686 | if (is_simple(value)) { |
| 687 | if (reloc_value(value)==1) |
| 688 | result->oprs[operand].type |= UNITY; |
H. Peter Anvin | 788e6c1 | 2002-04-30 21:02:01 +0000 | [diff] [blame^] | 689 | if (optimizing>0) { |
H. Peter Anvin | 4cf1748 | 2002-04-30 21:01:38 +0000 | [diff] [blame] | 690 | if (reloc_value(value) >= -128 && |
| 691 | reloc_value(value) <= 127) |
| 692 | result->oprs[operand].type |= SBYTE; |
| 693 | else result->oprs[operand].type |= SBIG; |
| 694 | } |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 695 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 696 | } |
| 697 | else /* it's a register */ |
| 698 | { |
| 699 | int i; |
| 700 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 701 | if (value->type>=EXPR_SIMPLE || value->value!=1) { |
| 702 | error (ERR_NONFATAL, "invalid operand type"); |
| 703 | result->opcode = -1; |
| 704 | return result; |
| 705 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 706 | |
| 707 | /* |
| 708 | * check that its only 1 register, not an expression... |
| 709 | */ |
| 710 | for (i = 1; value[i].type; i++) |
| 711 | if (value[i].value) { |
| 712 | error (ERR_NONFATAL, "invalid operand type"); |
| 713 | result->opcode = -1; |
| 714 | return result; |
| 715 | } |
| 716 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 717 | /* clear overrides, except TO which applies to FPU regs */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 718 | if (result->oprs[operand].type & ~TO) { |
| 719 | /* |
| 720 | * we want to produce a warning iff the specified size |
| 721 | * is different from the register size |
| 722 | */ |
| 723 | i = result->oprs[operand].type & SIZE_MASK; |
| 724 | } |
| 725 | else |
| 726 | i = 0; |
| 727 | |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 728 | result->oprs[operand].type &= TO; |
| 729 | result->oprs[operand].type |= REGISTER; |
| 730 | result->oprs[operand].type |= reg_flags[value->type]; |
| 731 | result->oprs[operand].basereg = value->type; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 732 | |
| 733 | if (i && (result->oprs[operand].type & SIZE_MASK) != i) |
| 734 | error (ERR_WARNING|ERR_PASS1, |
| 735 | "register size specification ignored"); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | result->operands = operand; /* set operand count */ |
| 741 | |
| 742 | while (operand<3) /* clear remaining operands */ |
| 743 | result->oprs[operand++].type = 0; |
| 744 | |
| 745 | /* |
| 746 | * Transform RESW, RESD, RESQ, REST into RESB. |
| 747 | */ |
| 748 | switch (result->opcode) { |
| 749 | case I_RESW: result->opcode=I_RESB; result->oprs[0].offset*=2; break; |
| 750 | case I_RESD: result->opcode=I_RESB; result->oprs[0].offset*=4; break; |
| 751 | case I_RESQ: result->opcode=I_RESB; result->oprs[0].offset*=8; break; |
| 752 | case I_REST: result->opcode=I_RESB; result->oprs[0].offset*=10; break; |
| 753 | } |
| 754 | |
| 755 | return result; |
| 756 | } |
| 757 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 758 | static int is_comma_next (void) |
| 759 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 760 | char *p; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 761 | int i; |
| 762 | struct tokenval tv; |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 763 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 764 | p = stdscan_bufptr; |
| 765 | i = stdscan (NULL, &tv); |
| 766 | stdscan_bufptr = p; |
| 767 | return (i == ',' || i == ';' || !i); |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 768 | } |
| 769 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 770 | void cleanup_insn (insn *i) |
| 771 | { |
H. Peter Anvin | ea6e34d | 2002-04-30 20:51:32 +0000 | [diff] [blame] | 772 | extop *e; |
| 773 | |
| 774 | while (i->eops) { |
| 775 | e = i->eops; |
| 776 | i->eops = i->eops->next; |
| 777 | nasm_free (e); |
| 778 | } |
| 779 | } |