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