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