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