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