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