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