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