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