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