]>
Commit | Line | Data |
---|---|---|
fecd2382 RP |
1 | /* vax.c - vax-specific - |
2 | Copyright (C) 1987, 1991 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GAS, the GNU Assembler. | |
5 | ||
6 | GAS is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 1, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GAS is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GAS; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | /* $Id$ */ | |
21 | ||
22 | /* JF I moved almost all the vax specific stuff into this one file 'cuz RMS | |
23 | seems to think its a good idea. I hope I managed to get all the VAX-isms */ | |
24 | ||
25 | ||
26 | #include "as.h" | |
27 | ||
28 | #include "read.h" | |
29 | #include "flonum.h" | |
30 | #include "vax-inst.h" | |
31 | #include "obstack.h" /* For FRAG_APPEND_1_CHAR macro in "frags.h" */ | |
32 | #include "frags.h" | |
33 | #include "expr.h" | |
34 | #include "symbols.h" | |
35 | ||
36 | /* These chars start a comment anywhere in a source file (except inside | |
37 | another comment */ | |
38 | const char comment_chars[] = "#"; | |
39 | ||
40 | /* These chars only start a comment at the beginning of a line. */ | |
41 | /* Note that for the VAX the are the same as comment_chars above. */ | |
42 | const char line_comment_chars[] = "#"; | |
43 | ||
44 | /* Chars that can be used to separate mant from exp in floating point nums */ | |
45 | const char EXP_CHARS[] = "eE"; | |
46 | ||
47 | /* Chars that mean this number is a floating point constant */ | |
48 | /* as in 0f123.456 */ | |
49 | /* or 0H1.234E-12 (see exp chars above) */ | |
50 | const char FLT_CHARS[] = "dDfFgGhH"; | |
51 | ||
52 | /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be | |
53 | changed in read.c . Ideally it shouldn't have to know about it at all, | |
54 | but nothing is ideal around here. | |
55 | */ | |
56 | ||
57 | static expressionS /* Hold details of an operand expression */ | |
58 | exp_of_operand[VIT_MAX_OPERANDS]; | |
59 | ||
60 | static struct vit | |
61 | v; /* A vax instruction after decoding. */ | |
62 | ||
63 | LITTLENUM_TYPE big_operand_bits[VIT_MAX_OPERANDS][SIZE_OF_LARGE_NUMBER]; | |
64 | /* Hold details of big operands. */ | |
65 | FLONUM_TYPE float_operand[VIT_MAX_OPERANDS]; | |
66 | /* Above is made to point into */ | |
67 | /* big_operand_bits by md_begin(). */ | |
68 | \f | |
69 | /* | |
70 | * For VAX, relative addresses of "just the right length" are easy. | |
71 | * The branch displacement is always the last operand, even in | |
72 | * synthetic instructions. | |
73 | * For VAX, we encode the relax_substateTs (in e.g. fr_substate) as: | |
74 | * | |
75 | * 4 3 2 1 0 bit number | |
76 | * ---/ /--+-------+-------+-------+-------+-------+ | |
77 | * | what state ? | how long ? | | |
78 | * ---/ /--+-------+-------+-------+-------+-------+ | |
79 | * | |
80 | * The "how long" bits are 00=byte, 01=word, 10=long. | |
81 | * This is a Un*x convention. | |
82 | * Not all lengths are legit for a given value of (what state). | |
83 | * The "how long" refers merely to the displacement length. | |
84 | * The address usually has some constant bytes in it as well. | |
85 | * | |
86 | ||
87 | groups for VAX address relaxing. | |
88 | ||
89 | 1. "foo" pc-relative. | |
90 | length of byte, word, long | |
91 | ||
92 | 2a. J<cond> where <cond> is a simple flag test. | |
93 | length of byte, word, long. | |
94 | VAX opcodes are: (Hex) | |
95 | bneq/bnequ 12 | |
96 | beql/beqlu 13 | |
97 | bgtr 14 | |
98 | bleq 15 | |
99 | bgeq 18 | |
100 | blss 19 | |
101 | bgtru 1a | |
102 | blequ 1b | |
103 | bvc 1c | |
104 | bvs 1d | |
105 | bgequ/bcc 1e | |
106 | blssu/bcs 1f | |
107 | Always, you complement 0th bit to reverse condition. | |
108 | Always, 1-byte opcode, then 1-byte displacement. | |
109 | ||
110 | 2b. J<cond> where cond tests a memory bit. | |
111 | length of byte, word, long. | |
112 | Vax opcodes are: (Hex) | |
113 | bbs e0 | |
114 | bbc e1 | |
115 | bbss e2 | |
116 | bbcs e3 | |
117 | bbsc e4 | |
118 | bbcc e5 | |
119 | bbssi e6 | |
120 | bbcci e7 | |
121 | Always, you complement 0th bit to reverse condition. | |
122 | Always, 1-byte opcde, longword-address, byte-address, 1-byte-displacement | |
123 | ||
124 | 2c. J<cond> where cond tests low-order memory bit | |
125 | length of byte,word,long. | |
126 | Vax opcodes are: (Hex) | |
127 | blbs e8 | |
128 | blbc e9 | |
129 | Always, you complement 0th bit to reverse condition. | |
130 | Always, 1-byte opcode, longword-address, 1-byte displacement. | |
131 | ||
132 | 3. Jbs/Jbr. | |
133 | length of byte,word,long. | |
134 | Vax opcodes are: (Hex) | |
135 | bsbb 10 | |
136 | brb 11 | |
137 | These are like (2) but there is no condition to reverse. | |
138 | Always, 1 byte opcode, then displacement/absolute. | |
139 | ||
140 | 4a. JacbX | |
141 | length of word, long. | |
142 | Vax opcodes are: (Hex) | |
143 | acbw 3d | |
144 | acbf 4f | |
145 | acbd 6f | |
146 | abcb 9d | |
147 | acbl f1 | |
148 | acbg 4ffd | |
149 | acbh 6ffd | |
150 | Always, we cannot reverse the sense of the branch; we have a word | |
151 | displacement. | |
152 | The double-byte op-codes don't hurt: we never want to modify the | |
153 | opcode, so we don't care how many bytes are between the opcode and | |
154 | the operand. | |
155 | ||
156 | 4b. JXobXXX | |
157 | length of long, long, byte. | |
158 | Vax opcodes are: (Hex) | |
159 | aoblss f2 | |
160 | aobleq f3 | |
161 | sobgeq f4 | |
162 | sobgtr f5 | |
163 | Always, we cannot reverse the sense of the branch; we have a byte | |
164 | displacement. | |
165 | ||
166 | The only time we need to modify the opcode is for class 2 instructions. | |
167 | After relax() we may complement the lowest order bit of such instruction | |
168 | to reverse sense of branch. | |
169 | ||
170 | For class 2 instructions, we store context of "where is the opcode literal". | |
171 | We can change an opcode's lowest order bit without breaking anything else. | |
172 | ||
173 | We sometimes store context in the operand literal. This way we can figure out | |
174 | after relax() what the original addressing mode was. | |
175 | */ | |
176 | \f | |
177 | /* These displacements are relative to */ | |
178 | /* the start address of the displacement. */ | |
179 | /* The first letter is Byte, Word. */ | |
180 | /* 2nd letter is Forward, Backward. */ | |
181 | #define BF (1+ 127) | |
182 | #define BB (1+-128) | |
183 | #define WF (2+ 32767) | |
184 | #define WB (2+-32768) | |
185 | /* Dont need LF, LB because they always */ | |
186 | /* reach. [They are coded as 0.] */ | |
187 | ||
188 | ||
189 | #define C(a,b) ENCODE_RELAX(a,b) | |
190 | /* This macro has no side-effects. */ | |
191 | #define ENCODE_RELAX(what,length) (((what) << 2) + (length)) | |
192 | ||
193 | const relax_typeS | |
194 | md_relax_table[] = | |
195 | { | |
196 | { | |
197 | 1, 1, 0, 0 | |
198 | }, /* error sentinel 0,0 */ | |
199 | { | |
200 | 1, 1, 0, 0 | |
201 | }, /* unused 0,1 */ | |
202 | { | |
203 | 1, 1, 0, 0 | |
204 | }, /* unused 0,2 */ | |
205 | { | |
206 | 1, 1, 0, 0 | |
207 | }, /* unused 0,3 */ | |
208 | { | |
209 | BF + 1, BB + 1, 2, C (1, 1) | |
210 | }, /* B^"foo" 1,0 */ | |
211 | { | |
212 | WF + 1, WB + 1, 3, C (1, 2) | |
213 | }, /* W^"foo" 1,1 */ | |
214 | { | |
215 | 0, 0, 5, 0 | |
216 | }, /* L^"foo" 1,2 */ | |
217 | { | |
218 | 1, 1, 0, 0 | |
219 | }, /* unused 1,3 */ | |
220 | { | |
221 | BF, BB, 1, C (2, 1) | |
222 | }, /* b<cond> B^"foo" 2,0 */ | |
223 | { | |
224 | WF + 2, WB + 2, 4, C (2, 2) | |
225 | }, /* br.+? brw X 2,1 */ | |
226 | { | |
227 | 0, 0, 7, 0 | |
228 | }, /* br.+? jmp X 2,2 */ | |
229 | { | |
230 | 1, 1, 0, 0 | |
231 | }, /* unused 2,3 */ | |
232 | { | |
233 | BF, BB, 1, C (3, 1) | |
234 | }, /* brb B^foo 3,0 */ | |
235 | { | |
236 | WF, WB, 2, C (3, 2) | |
237 | }, /* brw W^foo 3,1 */ | |
238 | { | |
239 | 0, 0, 5, 0 | |
240 | }, /* Jmp L^foo 3,2 */ | |
241 | { | |
242 | 1, 1, 0, 0 | |
243 | }, /* unused 3,3 */ | |
244 | { | |
245 | 1, 1, 0, 0 | |
246 | }, /* unused 4,0 */ | |
247 | { | |
248 | WF, WB, 2, C (4, 2) | |
249 | }, /* acb_ ^Wfoo 4,1 */ | |
250 | { | |
251 | 0, 0, 10, 0 | |
252 | }, /* acb_,br,jmp L^foo4,2 */ | |
253 | { | |
254 | 1, 1, 0, 0 | |
255 | }, /* unused 4,3 */ | |
256 | { | |
257 | BF, BB, 1, C (5, 1) | |
258 | }, /* Xob___,,foo 5,0 */ | |
259 | { | |
260 | WF + 4, WB + 4, 6, C (5, 2) | |
261 | }, /* Xob.+2,brb.+3,brw5,1 */ | |
262 | { | |
263 | 0, 0, 9, 0 | |
264 | }, /* Xob.+2,brb.+6,jmp5,2 */ | |
265 | }; | |
266 | ||
267 | #undef C | |
268 | #undef BF | |
269 | #undef BB | |
270 | #undef WF | |
271 | #undef WB | |
272 | ||
273 | void float_cons (); | |
274 | ||
275 | const pseudo_typeS md_pseudo_table[] = | |
276 | { | |
277 | {"dfloat", float_cons, 'd'}, | |
278 | {"ffloat", float_cons, 'f'}, | |
279 | {"gfloat", float_cons, 'g'}, | |
280 | {"hfloat", float_cons, 'h'}, | |
281 | {0} | |
282 | }; | |
283 | ||
284 | #define STATE_PC_RELATIVE (1) | |
285 | #define STATE_CONDITIONAL_BRANCH (2) | |
286 | #define STATE_ALWAYS_BRANCH (3) /* includes BSB... */ | |
287 | #define STATE_COMPLEX_BRANCH (4) | |
288 | #define STATE_COMPLEX_HOP (5) | |
289 | ||
290 | #define STATE_BYTE (0) | |
291 | #define STATE_WORD (1) | |
292 | #define STATE_LONG (2) | |
293 | #define STATE_UNDF (3) /* Symbol undefined in pass1 */ | |
294 | ||
295 | ||
296 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
297 | \f | |
298 | ||
299 | void | |
300 | md_begin () | |
301 | { | |
302 | char *vip_begin (); | |
303 | char *errtxt; | |
304 | FLONUM_TYPE *fP; | |
305 | int i; | |
306 | ||
307 | if (*(errtxt = vip_begin (TRUE, "$", "*", "`"))) | |
308 | { | |
309 | as_fatal("VIP_BEGIN error:%s", errtxt); | |
310 | } | |
311 | ||
312 | for (i = 0, fP = float_operand; | |
313 | fP < float_operand + VIT_MAX_OPERANDS; | |
314 | i++, fP++) | |
315 | { | |
316 | fP->low = &big_operand_bits[i][0]; | |
317 | fP->high = &big_operand_bits[i][SIZE_OF_LARGE_NUMBER - 1]; | |
318 | } | |
319 | } | |
320 | ||
321 | void | |
322 | md_end () | |
323 | { | |
324 | vip_end (); | |
325 | } | |
326 | \f | |
327 | void /* Knows about order of bytes in address. */ | |
328 | md_number_to_chars (con, value, nbytes) | |
329 | char con[]; /* Return 'nbytes' of chars here. */ | |
330 | long value; /* The value of the bits. */ | |
331 | int nbytes; /* Number of bytes in the output. */ | |
332 | { | |
333 | int n; | |
334 | long v; | |
335 | ||
336 | n = nbytes; | |
337 | v = value; | |
338 | while (nbytes--) | |
339 | { | |
340 | *con++ = value; /* Lint wants & MASK_CHAR. */ | |
341 | value >>= BITS_PER_CHAR; | |
342 | } | |
343 | /* XXX line number probably botched for this warning message. */ | |
344 | if (value != 0 && value != -1) | |
345 | as_bad("Displacement (%ld) long for instruction field length (%d).", v, n); | |
346 | } | |
347 | ||
348 | /* Fix up some data or instructions after we find out the value of a symbol | |
349 | that they reference. */ | |
350 | ||
351 | void /* Knows about order of bytes in address. */ | |
352 | md_apply_fix(fixP, value) | |
353 | fixS *fixP; /* Fixup struct pointer */ | |
354 | long value; /* The value of the bits. */ | |
355 | { | |
356 | char *buf = fixP->fx_where + fixP->fx_frag->fr_literal; | |
357 | int nbytes; /* Number of bytes in the output. */ | |
358 | ||
359 | nbytes = fixP->fx_size; | |
360 | while (nbytes--) | |
361 | { | |
362 | *buf++ = value; /* Lint wants & MASK_CHAR. */ | |
363 | value >>= BITS_PER_CHAR; | |
364 | } | |
365 | } | |
366 | ||
367 | long /* Knows about the byte order in a word. */ | |
368 | md_chars_to_number (con, nbytes) | |
369 | unsigned char con[]; /* Low order byte 1st. */ | |
370 | int nbytes; /* Number of bytes in the input. */ | |
371 | { | |
372 | long retval; | |
373 | for (retval = 0, con += nbytes - 1; nbytes--; con--) | |
374 | { | |
375 | retval <<= BITS_PER_CHAR; | |
376 | retval |= *con; | |
377 | } | |
378 | return retval; | |
379 | } | |
380 | \f | |
381 | /* vax:md_assemble() emit frags for 1 instruction */ | |
382 | ||
383 | void | |
384 | md_assemble (instruction_string) | |
385 | char *instruction_string; /* A string: assemble 1 instruction. */ | |
386 | { | |
387 | char *p; | |
388 | register struct vop *operandP;/* An operand. Scans all operands. */ | |
389 | char *save_input_line_pointer; | |
390 | char c_save; /* What used to live after an expression. */ | |
391 | struct frag *fragP; /* Fragment of code we just made. */ | |
392 | register int goofed; /* TRUE: instruction_string bad for all passes. */ | |
393 | register struct vop *end_operandP; /* -> slot just after last operand */ | |
394 | /* Limit of the for (each operand). */ | |
395 | register expressionS *expP; /* -> expression values for this operand */ | |
396 | ||
397 | /* These refer to an instruction operand expression. */ | |
398 | segT to_seg; /* Target segment of the address. */ | |
399 | register valueT this_add_number; | |
400 | register struct symbol *this_add_symbol; /* +ve (minuend) symbol. */ | |
401 | register struct symbol *this_subtract_symbol; /* -ve(subtrahend) symbol. */ | |
402 | ||
403 | long opcode_as_number; /* As a number. */ | |
404 | char *opcode_as_chars; /* Least significant byte 1st. */ | |
405 | /* As an array of characters. */ | |
406 | char *opcode_low_byteP; /* Least significant byte 1st */ | |
407 | struct details *detP; /* The details of an ADxxx frag. */ | |
408 | int length; /* length (bytes) meant by vop_short. */ | |
409 | int at; /* 0, or 1 if '@' is in addressing mode. */ | |
410 | int nbytes; /* From vop_nbytes: vax_operand_width (in bytes) */ | |
411 | FLONUM_TYPE *floatP; | |
412 | char *vip (); | |
413 | LITTLENUM_TYPE literal_float[8]; | |
414 | /* Big enough for any floating point literal. */ | |
415 | ||
416 | if (*(p = vip (&v, instruction_string))) | |
417 | { | |
418 | as_fatal("vax_assemble\"%s\" in=\"%s\"", p, instruction_string); | |
419 | } | |
420 | /* | |
421 | * Now we try to find as many as_warn()s as we can. If we do any as_warn()s | |
422 | * then goofed=TRUE. Notice that we don't make any frags yet. | |
423 | * Should goofed be TRUE, then this instruction will wedge in any pass, | |
424 | * and we can safely flush it, without causing interpass symbol phase | |
425 | * errors. That is, without changing label values in different passes. | |
426 | */ | |
427 | if (goofed = (*v.vit_error)) | |
428 | { | |
429 | as_warn ("Ignoring statement due to \"%s\"", v.vit_error); | |
430 | } | |
431 | /* | |
432 | * We need to use expression() and friends, which require us to diddle | |
433 | * input_line_pointer. So we save it and restore it later. | |
434 | */ | |
435 | save_input_line_pointer = input_line_pointer; | |
436 | for (operandP = v.vit_operand, | |
437 | expP = exp_of_operand, | |
438 | floatP = float_operand, | |
439 | end_operandP = v.vit_operand + v.vit_operands; | |
440 | ||
441 | operandP < end_operandP; | |
442 | ||
443 | operandP++, | |
444 | expP++, | |
445 | floatP++ | |
446 | ) /* for each operand */ | |
447 | { | |
448 | if (*(operandP->vop_error)) | |
449 | { | |
450 | as_warn ("Ignoring statement because \"%s\"", (operandP->vop_error)); | |
451 | goofed = TRUE; | |
452 | } | |
453 | else | |
454 | { /* statement has no syntax goofs: lets sniff the expression */ | |
455 | int can_be_short; /* TRUE if a bignum can be reduced to a short literal. */ | |
456 | ||
457 | input_line_pointer = operandP->vop_expr_begin; | |
458 | c_save = operandP->vop_expr_end[1]; | |
459 | operandP->vop_expr_end[1] = '\0'; | |
460 | /* If to_seg == SEG_PASS1, expression() will have set need_pass_2 = TRUE. */ | |
461 | switch (to_seg = expression (expP)) | |
462 | { | |
463 | case SEG_ABSENT: | |
464 | /* for BSD4.2 compatibility, missing expression is absolute 0 */ | |
465 | to_seg = expP->X_seg = SEG_ABSOLUTE; | |
466 | expP->X_add_number = 0; | |
467 | /* for SEG_ABSOLUTE, we shouldnt need to set X_subtract_symbol, X_add_symbol to any particular value. */ | |
468 | /* But, we will program defensively. Since this situation occurs */ | |
469 | /* rarely so it costs us little to do, and stops Dean */ | |
470 | /* worrying about the origin of random bits in expressionS's. */ | |
471 | expP->X_add_symbol = NULL; | |
472 | expP->X_subtract_symbol = NULL; | |
473 | case SEG_TEXT: | |
474 | case SEG_DATA: | |
475 | case SEG_BSS: | |
476 | case SEG_ABSOLUTE: | |
477 | case SEG_UNKNOWN: | |
478 | break; | |
479 | ||
480 | case SEG_DIFFERENCE: | |
481 | case SEG_PASS1: | |
482 | /* | |
483 | * Major bug. We can't handle the case of a | |
484 | * SEG_DIFFERENCE expression in a VIT_OPCODE_SYNTHETIC | |
485 | * variable-length instruction. | |
486 | * We don't have a frag type that is smart enough to | |
487 | * relax a SEG_DIFFERENCE, and so we just force all | |
488 | * SEG_DIFFERENCEs to behave like SEG_PASS1s. | |
489 | * Clearly, if there is a demand we can invent a new or | |
490 | * modified frag type and then coding up a frag for this | |
491 | * case will be easy. SEG_DIFFERENCE was invented for the | |
492 | * .words after a CASE opcode, and was never intended for | |
493 | * instruction operands. | |
494 | */ | |
495 | need_pass_2 = TRUE; | |
496 | as_warn("Can't relocate expression"); | |
497 | break; | |
498 | ||
499 | case SEG_BIG: | |
500 | /* Preserve the bits. */ | |
501 | if (expP->X_add_number > 0) | |
502 | { | |
503 | bignum_copy (generic_bignum, expP->X_add_number, | |
504 | floatP->low, SIZE_OF_LARGE_NUMBER); | |
505 | } | |
506 | else | |
507 | { | |
508 | know (expP->X_add_number < 0); | |
509 | flonum_copy (&generic_floating_point_number, | |
510 | floatP); | |
511 | if (strchr ("s i", operandP->vop_short)) | |
512 | { /* Could possibly become S^# */ | |
513 | flonum_gen2vax (-expP->X_add_number, floatP, literal_float); | |
514 | switch (-expP->X_add_number) | |
515 | { | |
516 | case 'f': | |
517 | can_be_short = | |
518 | (literal_float[0] & 0xFC0F) == 0x4000 | |
519 | && literal_float[1] == 0; | |
520 | break; | |
521 | ||
522 | case 'd': | |
523 | can_be_short = | |
524 | (literal_float[0] & 0xFC0F) == 0x4000 | |
525 | && literal_float[1] == 0 | |
526 | && literal_float[2] == 0 | |
527 | && literal_float[3] == 0; | |
528 | break; | |
529 | ||
530 | case 'g': | |
531 | can_be_short = | |
532 | (literal_float[0] & 0xFF81) == 0x4000 | |
533 | && literal_float[1] == 0 | |
534 | && literal_float[2] == 0 | |
535 | && literal_float[3] == 0; | |
536 | break; | |
537 | ||
538 | case 'h': | |
539 | can_be_short = | |
540 | (literal_float[0] & 0xFFF8) == 0x4000 | |
541 | && (literal_float[1] & 0xE000) == 0 | |
542 | && literal_float[2] == 0 | |
543 | && literal_float[3] == 0 | |
544 | && literal_float[4] == 0 | |
545 | && literal_float[5] == 0 | |
546 | && literal_float[6] == 0 | |
547 | && literal_float[7] == 0; | |
548 | break; | |
549 | ||
550 | default: | |
551 | BAD_CASE (-expP->X_add_number); | |
552 | break; | |
553 | } /* switch (float type) */ | |
554 | } /* if (could want to become S^#...) */ | |
555 | } /* bignum or flonum ? */ | |
556 | ||
557 | if (operandP->vop_short == 's' | |
558 | || operandP->vop_short == 'i' | |
559 | || (operandP->vop_short == ' ' | |
560 | && operandP->vop_reg == 0xF | |
561 | && (operandP->vop_mode & 0xE) == 0x8)) | |
562 | { | |
563 | /* Saw a '#'. */ | |
564 | if (operandP->vop_short == ' ') | |
565 | { /* We must chose S^ or I^. */ | |
566 | if (expP->X_add_number > 0) | |
567 | { /* Bignum: Short literal impossible. */ | |
568 | operandP->vop_short = 'i'; | |
569 | operandP->vop_mode = 8; | |
570 | operandP->vop_reg = 0xF; /* VAX PC. */ | |
571 | } | |
572 | else | |
573 | { /* Flonum: Try to do it. */ | |
574 | if (can_be_short) | |
575 | { | |
576 | operandP->vop_short = 's'; | |
577 | operandP->vop_mode = 0; | |
578 | operandP->vop_ndx = -1; | |
579 | operandP->vop_reg = -1; | |
580 | /* JF hope this is the right thing */ | |
581 | expP->X_seg = SEG_ABSOLUTE; | |
582 | } | |
583 | else | |
584 | { | |
585 | operandP->vop_short = 'i'; | |
586 | operandP->vop_mode = 8; | |
587 | operandP->vop_reg = 0xF; /* VAX PC */ | |
588 | } | |
589 | } /* bignum or flonum ? */ | |
590 | } /* if #, but no S^ or I^ seen. */ | |
591 | /* No more ' ' case: either 's' or 'i'. */ | |
592 | if (operandP->vop_short == 's') | |
593 | { | |
594 | /* Wants to be a short literal. */ | |
595 | if (expP->X_add_number > 0) | |
596 | { | |
597 | as_warn ("Bignum not permitted in short literal. Immediate mode assumed."); | |
598 | operandP->vop_short = 'i'; | |
599 | operandP->vop_mode = 8; | |
600 | operandP->vop_reg = 0xF; /* VAX PC. */ | |
601 | } | |
602 | else | |
603 | { | |
604 | if (!can_be_short) | |
605 | { | |
606 | as_warn ("Can't do flonum short literal: immediate mode used."); | |
607 | operandP->vop_short = 'i'; | |
608 | operandP->vop_mode = 8; | |
609 | operandP->vop_reg = 0xF; /* VAX PC. */ | |
610 | } | |
611 | else | |
612 | { /* Encode short literal now. */ | |
613 | register int temp; | |
614 | ||
615 | switch (-expP->X_add_number) | |
616 | { | |
617 | case 'f': | |
618 | case 'd': | |
619 | temp = literal_float[0] >> 4; | |
620 | break; | |
621 | ||
622 | case 'g': | |
623 | temp = literal_float[0] >> 1; | |
624 | break; | |
625 | ||
626 | case 'h': | |
627 | temp = ((literal_float[0] << 3) & 070) | |
628 | | ((literal_float[1] >> 13) & 07); | |
629 | break; | |
630 | ||
631 | default: | |
632 | BAD_CASE (-expP->X_add_number); | |
633 | break; | |
634 | } | |
635 | ||
636 | floatP->low[0] = temp & 077; | |
637 | floatP->low[1] = 0; | |
638 | } /* if can be short literal float */ | |
639 | } /* flonum or bignum ? */ | |
640 | } | |
641 | else | |
642 | { /* I^# seen: set it up if float. */ | |
643 | if (expP->X_add_number < 0) | |
644 | { | |
645 | bcopy (literal_float, floatP->low, sizeof (literal_float)); | |
646 | } | |
647 | } /* if S^# seen. */ | |
648 | } | |
649 | else | |
650 | { | |
651 | as_warn ("A bignum/flonum may not be a displacement: 0x%x used", | |
652 | expP->X_add_number = 0x80000000); | |
653 | /* Chosen so luser gets the most offset bits to patch later. */ | |
654 | } | |
655 | expP->X_add_number = floatP->low[0] | |
656 | | ((LITTLENUM_MASK & (floatP->low[1])) << LITTLENUM_NUMBER_OF_BITS); | |
657 | /* | |
658 | * For the SEG_BIG case we have: | |
659 | * If vop_short == 's' then a short floating literal is in the | |
660 | * lowest 6 bits of floatP -> low [0], which is | |
661 | * big_operand_bits [---] [0]. | |
662 | * If vop_short == 'i' then the appropriate number of elements | |
663 | * of big_operand_bits [---] [...] are set up with the correct | |
664 | * bits. | |
665 | * Also, just in case width is byte word or long, we copy the lowest | |
666 | * 32 bits of the number to X_add_number. | |
667 | */ | |
668 | break; | |
669 | ||
670 | default: | |
671 | BAD_CASE (to_seg); | |
672 | break; | |
673 | } | |
674 | if (input_line_pointer != operandP->vop_expr_end + 1) | |
675 | { | |
676 | as_warn ("Junk at end of expression \"%s\"", input_line_pointer); | |
677 | goofed = TRUE; | |
678 | } | |
679 | operandP->vop_expr_end[1] = c_save; | |
680 | } | |
681 | } /* for(each operand) */ | |
682 | input_line_pointer = save_input_line_pointer; | |
683 | ||
684 | if (!need_pass_2 && !goofed) | |
685 | { | |
686 | /* We saw no errors in any operands - try to make frag(s) */ | |
687 | int is_undefined; /* True if operand expression's */ | |
688 | /* segment not known yet. */ | |
689 | int length_code; | |
690 | ||
691 | /* Emit op-code. */ | |
692 | /* Remember where it is, in case we want to modify the op-code later. */ | |
693 | opcode_low_byteP = frag_more (v.vit_opcode_nbytes); | |
694 | bcopy (v.vit_opcode, opcode_low_byteP, v.vit_opcode_nbytes); | |
695 | opcode_as_number = md_chars_to_number (opcode_as_chars = v.vit_opcode, 4); | |
696 | for (operandP = v.vit_operand, | |
697 | expP = exp_of_operand, | |
698 | floatP = float_operand, | |
699 | end_operandP = v.vit_operand + v.vit_operands; | |
700 | ||
701 | operandP < end_operandP; | |
702 | ||
703 | operandP++, | |
704 | floatP++, | |
705 | expP++ | |
706 | ) /* for each operand */ | |
707 | { | |
708 | if (operandP->vop_ndx >= 0) | |
709 | { | |
710 | /* indexed addressing byte */ | |
711 | /* Legality of indexed mode already checked: it is OK */ | |
712 | FRAG_APPEND_1_CHAR (0x40 + operandP->vop_ndx); | |
713 | } /* if(vop_ndx>=0) */ | |
714 | ||
715 | /* Here to make main operand frag(s). */ | |
716 | this_add_number = expP->X_add_number; | |
717 | this_add_symbol = expP->X_add_symbol; | |
718 | this_subtract_symbol = expP->X_subtract_symbol; | |
719 | to_seg = expP->X_seg; | |
720 | is_undefined = (to_seg == SEG_UNKNOWN); | |
721 | know (to_seg == SEG_UNKNOWN | |
722 | ||to_seg == SEG_ABSOLUTE | |
723 | ||to_seg == SEG_DATA | |
724 | ||to_seg == SEG_TEXT | |
725 | ||to_seg == SEG_BSS | |
726 | ||to_seg == SEG_BIG | |
727 | ); | |
728 | at = operandP->vop_mode & 1; | |
729 | length = operandP->vop_short == 'b' ? 1 : operandP->vop_short == 'w' ? 2 : operandP->vop_short == 'l' ? 4 : 0; | |
730 | nbytes = operandP->vop_nbytes; | |
731 | if (operandP->vop_access == 'b') | |
732 | { | |
733 | if (to_seg == now_seg || is_undefined) | |
734 | { /* If is_undefined, then it might BECOME now_seg. */ | |
735 | if (nbytes) | |
736 | { | |
737 | p = frag_more (nbytes); | |
738 | fix_new (frag_now, p - frag_now->fr_literal, nbytes, | |
739 | this_add_symbol, 0, this_add_number, 1); | |
740 | } | |
741 | else | |
742 | { /* to_seg==now_seg || to_seg == SEG_UNKNOWN */ | |
743 | /* nbytes==0 */ | |
744 | length_code = is_undefined ? STATE_UNDF : STATE_BYTE; | |
745 | if (opcode_as_number & VIT_OPCODE_SPECIAL) | |
746 | { | |
747 | if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP) | |
748 | { | |
749 | /* br or jsb */ | |
750 | frag_var (rs_machine_dependent, 5, 1, | |
751 | ENCODE_RELAX (STATE_ALWAYS_BRANCH, length_code), | |
752 | this_add_symbol, this_add_number, | |
753 | opcode_low_byteP); | |
754 | } | |
755 | else | |
756 | { | |
757 | if (operandP->vop_width == VAX_WIDTH_WORD_JUMP) | |
758 | { | |
759 | length_code = STATE_WORD; /* JF: There is no state_byte for this one! */ | |
760 | frag_var (rs_machine_dependent, 10, 2, | |
761 | ENCODE_RELAX (STATE_COMPLEX_BRANCH, length_code), | |
762 | this_add_symbol, this_add_number, | |
763 | opcode_low_byteP); | |
764 | } | |
765 | else | |
766 | { | |
767 | know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP); | |
768 | frag_var (rs_machine_dependent, 9, 1, | |
769 | ENCODE_RELAX (STATE_COMPLEX_HOP, length_code), | |
770 | this_add_symbol, this_add_number, | |
771 | opcode_low_byteP); | |
772 | } | |
773 | } | |
774 | } | |
775 | else | |
776 | { | |
777 | know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP); | |
778 | frag_var (rs_machine_dependent, 7, 1, | |
779 | ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, length_code), | |
780 | this_add_symbol, this_add_number, | |
781 | opcode_low_byteP); | |
782 | } | |
783 | } | |
784 | } | |
785 | else | |
786 | { /* to_seg != now_seg && to_seg != SEG_UNKNOWN */ | |
787 | /* | |
788 | * --- SEG FLOAT MAY APPEAR HERE ---- | |
789 | */ | |
790 | if (to_seg == SEG_ABSOLUTE) | |
791 | { | |
792 | if (nbytes) | |
793 | { | |
794 | know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC)); | |
795 | p = frag_more (nbytes); | |
796 | /* Conventional relocation. */ | |
797 | fix_new (frag_now, p - frag_now->fr_literal, | |
798 | nbytes, &abs_symbol, 0, this_add_number, 1); | |
799 | } | |
800 | else | |
801 | { | |
802 | know (opcode_as_number & VIT_OPCODE_SYNTHETIC); | |
803 | if (opcode_as_number & VIT_OPCODE_SPECIAL) | |
804 | { | |
805 | if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP) | |
806 | { | |
807 | /* br or jsb */ | |
808 | *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG; | |
809 | know (opcode_as_chars[1] == 0); | |
810 | p = frag_more (5); | |
811 | p[0] = VAX_ABSOLUTE_MODE; /* @#... */ | |
812 | md_number_to_chars (p + 1, this_add_number, 4); | |
813 | /* Now (eg) JMP @#foo or JSB @#foo. */ | |
814 | } | |
815 | else | |
816 | { | |
817 | if (operandP->vop_width == VAX_WIDTH_WORD_JUMP) | |
818 | { | |
819 | p = frag_more (10); | |
820 | p[0] = 2; | |
821 | p[1] = 0; | |
822 | p[2] = VAX_BRB; | |
823 | p[3] = 6; | |
824 | p[4] = VAX_JMP; | |
825 | p[5] = VAX_ABSOLUTE_MODE; /* @#... */ | |
826 | md_number_to_chars (p + 6, this_add_number, 4); | |
827 | /* | |
828 | * Now (eg) ACBx 1f | |
829 | * BRB 2f | |
830 | * 1: JMP @#foo | |
831 | * 2: | |
832 | */ | |
833 | } | |
834 | else | |
835 | { | |
836 | know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP); | |
837 | p = frag_more (9); | |
838 | p[0] = 2; | |
839 | p[1] = VAX_BRB; | |
840 | p[2] = 6; | |
841 | p[3] = VAX_JMP; | |
842 | p[4] = VAX_PC_RELATIVE_MODE + 1; /* @#... */ | |
843 | md_number_to_chars (p + 5, this_add_number, 4); | |
844 | /* | |
845 | * Now (eg) xOBxxx 1f | |
846 | * BRB 2f | |
847 | * 1: JMP @#foo | |
848 | * 2: | |
849 | */ | |
850 | } | |
851 | } | |
852 | } | |
853 | else | |
854 | { | |
855 | /* b<cond> */ | |
856 | *opcode_low_byteP ^= 1; /* To reverse the condition in a VAX branch, complement the lowest order bit. */ | |
857 | p = frag_more (7); | |
858 | p[0] = 6; | |
859 | p[1] = VAX_JMP; | |
860 | p[2] = VAX_ABSOLUTE_MODE; /* @#... */ | |
861 | md_number_to_chars (p + 3, this_add_number, 4); | |
862 | /* | |
863 | * Now (eg) BLEQ 1f | |
864 | * JMP @#foo | |
865 | * 1: | |
866 | */ | |
867 | } | |
868 | } | |
869 | } | |
870 | else | |
871 | { /* to_seg != now_seg && to_seg != SEG_UNKNOWN && to_Seg != SEG_ABSOLUTE */ | |
872 | if (nbytes > 0) | |
873 | { | |
874 | /* Pc-relative. Conventional relocation. */ | |
875 | know (!(opcode_as_number & VIT_OPCODE_SYNTHETIC)); | |
876 | p = frag_more (nbytes); | |
877 | fix_new (frag_now, p - frag_now->fr_literal, | |
878 | nbytes, &abs_symbol, 0, this_add_number, 1); | |
879 | } | |
880 | else | |
881 | { | |
882 | know (opcode_as_number & VIT_OPCODE_SYNTHETIC); | |
883 | if (opcode_as_number & VIT_OPCODE_SPECIAL) | |
884 | { | |
885 | if (operandP->vop_width == VAX_WIDTH_UNCONDITIONAL_JUMP) | |
886 | { | |
887 | /* br or jsb */ | |
888 | know (opcode_as_chars[1] == 0); | |
889 | *opcode_low_byteP = opcode_as_chars[0] + VAX_WIDEN_LONG; | |
890 | p = frag_more (5); | |
891 | p[0] = VAX_PC_RELATIVE_MODE; | |
892 | fix_new (frag_now, | |
893 | p + 1 - frag_now->fr_literal, 4, | |
894 | this_add_symbol, 0, | |
895 | this_add_number, 1); | |
896 | /* Now eg JMP foo or JSB foo. */ | |
897 | } | |
898 | else | |
899 | { | |
900 | if (operandP->vop_width == VAX_WIDTH_WORD_JUMP) | |
901 | { | |
902 | p = frag_more (10); | |
903 | p[0] = 0; | |
904 | p[1] = 2; | |
905 | p[2] = VAX_BRB; | |
906 | p[3] = 6; | |
907 | p[4] = VAX_JMP; | |
908 | p[5] = VAX_PC_RELATIVE_MODE; | |
909 | fix_new (frag_now, | |
910 | p + 6 - frag_now->fr_literal, 4, | |
911 | this_add_symbol, 0, | |
912 | this_add_number, 1); | |
913 | /* | |
914 | * Now (eg) ACBx 1f | |
915 | * BRB 2f | |
916 | * 1: JMP foo | |
917 | * 2: | |
918 | */ | |
919 | } | |
920 | else | |
921 | { | |
922 | know (operandP->vop_width == VAX_WIDTH_BYTE_JUMP); | |
923 | p = frag_more (10); | |
924 | p[0] = 2; | |
925 | p[1] = VAX_BRB; | |
926 | p[2] = 6; | |
927 | p[3] = VAX_JMP; | |
928 | p[4] = VAX_PC_RELATIVE_MODE; | |
929 | fix_new (frag_now, | |
930 | p + 5 - frag_now->fr_literal, | |
931 | 4, this_add_symbol, 0, | |
932 | this_add_number, 1); | |
933 | /* | |
934 | * Now (eg) xOBxxx 1f | |
935 | * BRB 2f | |
936 | * 1: JMP foo | |
937 | * 2: | |
938 | */ | |
939 | } | |
940 | } | |
941 | } | |
942 | else | |
943 | { | |
944 | know (operandP->vop_width == VAX_WIDTH_CONDITIONAL_JUMP); | |
945 | *opcode_low_byteP ^= 1; /* Reverse branch condition. */ | |
946 | p = frag_more (7); | |
947 | p[0] = 6; | |
948 | p[1] = VAX_JMP; | |
949 | p[2] = VAX_PC_RELATIVE_MODE; | |
950 | fix_new (frag_now, p + 3 - frag_now->fr_literal, | |
951 | 4, this_add_symbol, 0, | |
952 | this_add_number, 1); | |
953 | } | |
954 | } | |
955 | } | |
956 | } | |
957 | } | |
958 | else | |
959 | { | |
960 | know (operandP->vop_access != 'b'); /* So it is ordinary operand. */ | |
961 | know (operandP->vop_access != ' '); /* ' ' target-independent: elsewhere. */ | |
962 | know (operandP->vop_access == 'a' || operandP->vop_access == 'm' || operandP->vop_access == 'r' || operandP->vop_access == 'v' || operandP->vop_access == 'w'); | |
963 | if (operandP->vop_short == 's') | |
964 | { | |
965 | if (to_seg == SEG_ABSOLUTE) | |
966 | { | |
967 | if (this_add_number < 0 || this_add_number >= 64) | |
968 | { | |
969 | as_warn ("Short literal overflow(%d.), immediate mode assumed.", this_add_number); | |
970 | operandP->vop_short = 'i'; | |
971 | operandP->vop_mode = 8; | |
972 | operandP->vop_reg = 0xF; | |
973 | } | |
974 | } | |
975 | else | |
976 | { | |
977 | as_warn ("Forced short literal to immediate mode. now_seg=%s to_seg=%s", segment_name(now_seg), segment_name(to_seg)); | |
978 | operandP->vop_short = 'i'; | |
979 | operandP->vop_mode = 8; | |
980 | operandP->vop_reg = 0xF; | |
981 | } | |
982 | } | |
983 | if (operandP->vop_reg >= 0 && (operandP->vop_mode < 8 || (operandP->vop_reg != 0xF && operandP->vop_mode < 10))) | |
984 | { /* One byte operand. */ | |
985 | know (operandP->vop_mode > 3); | |
986 | FRAG_APPEND_1_CHAR (operandP->vop_mode << 4 | operandP->vop_reg); | |
987 | /* All 1-bytes except S^# happen here. */ | |
988 | } | |
989 | else | |
990 | { /* {@}{q^}foo{(Rn)} or S^#foo */ | |
991 | if (operandP->vop_reg == -1 && operandP->vop_short != 's') | |
992 | { /* "{@}{q^}foo" */ | |
993 | if (to_seg == now_seg) | |
994 | { | |
995 | if (length == 0) | |
996 | { | |
997 | know (operandP->vop_short == ' '); | |
998 | p = frag_var (rs_machine_dependent, 10, 2, | |
999 | ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE), | |
1000 | this_add_symbol, this_add_number, | |
1001 | opcode_low_byteP); | |
1002 | know (operandP->vop_mode == 10 + at); | |
1003 | *p = at << 4; | |
1004 | /* At is the only context we need to carry to */ | |
1005 | /* other side of relax() process. */ | |
1006 | /* Must be in the correct bit position of VAX */ | |
1007 | /* operand spec. byte. */ | |
1008 | } | |
1009 | else | |
1010 | { | |
1011 | know (length); | |
1012 | know (operandP->vop_short != ' '); | |
1013 | p = frag_more (length + 1); | |
1014 | /* JF is this array stuff really going to work? */ | |
1015 | p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4); | |
1016 | fix_new (frag_now, p + 1 - frag_now->fr_literal, | |
1017 | length, this_add_symbol, 0, | |
1018 | this_add_number, 1); | |
1019 | } | |
1020 | } | |
1021 | else | |
1022 | { /* to_seg != now_seg */ | |
1023 | if (this_add_symbol == NULL) | |
1024 | { | |
1025 | know (to_seg == SEG_ABSOLUTE); | |
1026 | /* Do @#foo: simpler relocation than foo-.(pc) anyway. */ | |
1027 | p = frag_more (5); | |
1028 | p[0] = VAX_ABSOLUTE_MODE; /* @#... */ | |
1029 | md_number_to_chars (p + 1, this_add_number, 4); | |
1030 | if (length && length != 4) | |
1031 | { | |
1032 | as_warn ("Length specification ignored. Address mode 9F used"); | |
1033 | } | |
1034 | } | |
1035 | else | |
1036 | { | |
1037 | /* {@}{q^}other_seg */ | |
1038 | know ((length == 0 && operandP->vop_short == ' ') | |
1039 | ||(length > 0 && operandP->vop_short != ' ')); | |
1040 | if (is_undefined) | |
1041 | { | |
1042 | /* | |
1043 | * We have a SEG_UNKNOWN symbol. It might | |
1044 | * turn out to be in the same segment as | |
1045 | * the instruction, permitting relaxation. | |
1046 | */ | |
1047 | p = frag_var (rs_machine_dependent, 5, 2, | |
1048 | ENCODE_RELAX (STATE_PC_RELATIVE, STATE_UNDF), | |
1049 | this_add_symbol, this_add_number, | |
1050 | 0); | |
1051 | p[0] = at << 4; | |
1052 | } | |
1053 | else | |
1054 | { | |
1055 | if (length == 0) | |
1056 | { | |
1057 | know (operandP->vop_short == ' '); | |
1058 | length = 4; /* Longest possible. */ | |
1059 | } | |
1060 | p = frag_more (length + 1); | |
1061 | p[0] = 0xF | ((at + "?\12\14?\16"[length]) << 4); | |
1062 | md_number_to_chars (p + 1, this_add_number, length); | |
1063 | fix_new (frag_now, | |
1064 | p + 1 - frag_now->fr_literal, | |
1065 | length, this_add_symbol, 0, | |
1066 | this_add_number, 1); | |
1067 | } | |
1068 | } | |
1069 | } | |
1070 | } | |
1071 | else | |
1072 | { /* {@}{q^}foo(Rn) or S^# or I^# or # */ | |
1073 | if (operandP->vop_mode < 0xA) | |
1074 | { /* # or S^# or I^# */ | |
1075 | /* know( (length == 0 && operandP->vop_short == ' ') | |
1076 | || (length > 0 && operandP->vop_short != ' ')); */ | |
1077 | if (length == 0 | |
1078 | && to_seg == SEG_ABSOLUTE | |
1079 | && operandP->vop_mode == 8 /* No '@'. */ | |
1080 | && this_add_number < 64 | |
1081 | && this_add_number >= 0) | |
1082 | { | |
1083 | operandP->vop_short = 's'; | |
1084 | } | |
1085 | if (operandP->vop_short == 's') | |
1086 | { | |
1087 | FRAG_APPEND_1_CHAR (this_add_number); | |
1088 | } | |
1089 | else | |
1090 | { /* I^#... */ | |
1091 | know (nbytes); | |
1092 | p = frag_more (nbytes + 1); | |
1093 | know (operandP->vop_reg == 0xF); | |
1094 | p[0] = (operandP->vop_mode << 4) | 0xF; | |
1095 | if (to_seg == SEG_ABSOLUTE) | |
1096 | { | |
1097 | /* | |
1098 | * If nbytes > 4, then we are scrod. We don't know if the | |
1099 | * high order bytes are to be 0xFF or 0x00. | |
1100 | * BSD4.2 & RMS say use 0x00. OK --- but this | |
1101 | * assembler needs ANOTHER rewrite to | |
1102 | * cope properly with this bug. | |
1103 | */ | |
1104 | md_number_to_chars (p + 1, this_add_number, min (4, nbytes)); | |
1105 | if (nbytes > 4) | |
1106 | { | |
1107 | bzero (p + 5, nbytes - 4); | |
1108 | } | |
1109 | } | |
1110 | else | |
1111 | { | |
1112 | if (to_seg == SEG_BIG) | |
1113 | { | |
1114 | /* | |
1115 | * Problem here is to get the bytes in the right order. | |
1116 | * We stored our constant as LITTLENUMs, not bytes. | |
1117 | */ | |
1118 | LITTLENUM_TYPE *lP; | |
1119 | ||
1120 | lP = floatP->low; | |
1121 | if (nbytes & 1) | |
1122 | { | |
1123 | know (nbytes == 1); | |
1124 | p[1] = *lP; | |
1125 | } | |
1126 | else | |
1127 | { | |
1128 | for (p++; nbytes; nbytes -= 2, p += 2, lP++) | |
1129 | { | |
1130 | md_number_to_chars (p, *lP, 2); | |
1131 | } | |
1132 | } | |
1133 | } | |
1134 | else | |
1135 | { | |
1136 | fix_new (frag_now, p + 1 - frag_now->fr_literal, | |
1137 | nbytes, this_add_symbol, 0, | |
1138 | this_add_number, 0); | |
1139 | } | |
1140 | } | |
1141 | } | |
1142 | } | |
1143 | else | |
1144 | { /* {@}{q^}foo(Rn) */ | |
1145 | know ((length == 0 && operandP->vop_short == ' ') | |
1146 | ||(length > 0 && operandP->vop_short != ' ')); | |
1147 | if (length == 0) | |
1148 | { | |
1149 | if (to_seg == SEG_ABSOLUTE) | |
1150 | { | |
1151 | register long test; | |
1152 | ||
1153 | test = this_add_number; | |
1154 | ||
1155 | if (test < 0) | |
1156 | test = ~test; | |
1157 | ||
1158 | length = test & 0xffff8000 ? 4 | |
1159 | : test & 0xffffff80 ? 2 | |
1160 | : 1; | |
1161 | } | |
1162 | else | |
1163 | { | |
1164 | length = 4; | |
1165 | } | |
1166 | } | |
1167 | p = frag_more (1 + length); | |
1168 | know (operandP->vop_reg >= 0); | |
1169 | p[0] = operandP->vop_reg | |
1170 | | ((at | "?\12\14?\16"[length]) << 4); | |
1171 | if (to_seg == SEG_ABSOLUTE) | |
1172 | { | |
1173 | md_number_to_chars (p + 1, this_add_number, length); | |
1174 | } | |
1175 | else | |
1176 | { | |
1177 | fix_new (frag_now, p + 1 - frag_now->fr_literal, | |
1178 | length, this_add_symbol, 0, | |
1179 | this_add_number, 0); | |
1180 | } | |
1181 | } | |
1182 | } | |
1183 | } /* if(single-byte-operand) */ | |
1184 | } | |
1185 | } /* for(operandP) */ | |
1186 | } /* if(!need_pass_2&&!goofed) */ | |
1187 | } /* vax_assemble() */ | |
1188 | \f | |
1189 | /* | |
1190 | * md_estimate_size_before_relax() | |
1191 | * | |
1192 | * Called just before relax(). | |
1193 | * Any symbol that is now undefined will not become defined. | |
1194 | * Return the correct fr_subtype in the frag. | |
1195 | * Return the initial "guess for fr_var" to caller. | |
1196 | * The guess for fr_var is ACTUALLY the growth beyond fr_fix. | |
1197 | * Whatever we do to grow fr_fix or fr_var contributes to our returned value. | |
1198 | * Although it may not be explicit in the frag, pretend fr_var starts with a | |
1199 | * 0 value. | |
1200 | */ | |
1201 | int | |
1202 | md_estimate_size_before_relax (fragP, segment) | |
1203 | register fragS *fragP; | |
1204 | register segT segment; | |
1205 | { | |
1206 | register char *p; | |
1207 | register int old_fr_fix; | |
1208 | ||
1209 | old_fr_fix = fragP->fr_fix; | |
1210 | switch (fragP->fr_subtype) | |
1211 | { | |
1212 | case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_UNDF): | |
1213 | if (S_GET_SEGMENT(fragP->fr_symbol) == segment) | |
1214 | { /* A relaxable case. */ | |
1215 | fragP->fr_subtype = ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE); | |
1216 | } | |
1217 | else | |
1218 | { | |
1219 | p = fragP->fr_literal + old_fr_fix; | |
1220 | p[0] |= VAX_PC_RELATIVE_MODE; /* Preserve @ bit. */ | |
1221 | fragP->fr_fix += 1 + 4; | |
1222 | fix_new (fragP, old_fr_fix + 1, 4, fragP->fr_symbol, 0, | |
1223 | fragP->fr_offset, 1); | |
1224 | frag_wane (fragP); | |
1225 | } | |
1226 | break; | |
1227 | ||
1228 | case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_UNDF): | |
1229 | if (S_GET_SEGMENT(fragP->fr_symbol) == segment) | |
1230 | { | |
1231 | fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE); | |
1232 | } | |
1233 | else | |
1234 | { | |
1235 | p = fragP->fr_literal + old_fr_fix; | |
1236 | *fragP->fr_opcode ^= 1; /* Reverse sense of branch. */ | |
1237 | p[0] = 6; | |
1238 | p[1] = VAX_JMP; | |
1239 | p[2] = VAX_PC_RELATIVE_MODE; /* ...(PC) */ | |
1240 | fragP->fr_fix += 1 + 1 + 1 + 4; | |
1241 | fix_new (fragP, old_fr_fix + 3, 4, fragP->fr_symbol, 0, | |
1242 | fragP->fr_offset, 1); | |
1243 | frag_wane (fragP); | |
1244 | } | |
1245 | break; | |
1246 | ||
1247 | case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_UNDF): | |
1248 | if (S_GET_SEGMENT(fragP->fr_symbol) == segment) | |
1249 | { | |
1250 | fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD); | |
1251 | } | |
1252 | else | |
1253 | { | |
1254 | p = fragP->fr_literal + old_fr_fix; | |
1255 | p[0] = 2; | |
1256 | p[1] = 0; | |
1257 | p[2] = VAX_BRB; | |
1258 | p[3] = 6; | |
1259 | p[4] = VAX_JMP; | |
1260 | p[5] = VAX_PC_RELATIVE_MODE; /* ...(pc) */ | |
1261 | fragP->fr_fix += 2 + 2 + 1 + 1 + 4; | |
1262 | fix_new (fragP, old_fr_fix + 6, 4, fragP->fr_symbol, 0, | |
1263 | fragP->fr_offset, 1); | |
1264 | frag_wane (fragP); | |
1265 | } | |
1266 | break; | |
1267 | ||
1268 | case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_UNDF): | |
1269 | if (S_GET_SEGMENT(fragP->fr_symbol) == segment) | |
1270 | { | |
1271 | fragP->fr_subtype = ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE); | |
1272 | } | |
1273 | else | |
1274 | { | |
1275 | p = fragP->fr_literal + old_fr_fix; | |
1276 | p[0] = 2; | |
1277 | p[1] = VAX_BRB; | |
1278 | p[2] = 6; | |
1279 | p[3] = VAX_JMP; | |
1280 | p[4] = VAX_PC_RELATIVE_MODE; /* ...(pc) */ | |
1281 | fragP->fr_fix += 1 + 2 + 1 + 1 + 4; | |
1282 | fix_new (fragP, old_fr_fix + 5, 4, fragP->fr_symbol, 0, | |
1283 | fragP->fr_offset, 1); | |
1284 | frag_wane (fragP); | |
1285 | } | |
1286 | break; | |
1287 | ||
1288 | case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_UNDF): | |
1289 | if (S_GET_SEGMENT(fragP->fr_symbol) == segment) | |
1290 | { | |
1291 | fragP->fr_subtype = ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE); | |
1292 | } | |
1293 | else | |
1294 | { | |
1295 | p = fragP->fr_literal + old_fr_fix; | |
1296 | *fragP->fr_opcode += VAX_WIDEN_LONG; | |
1297 | p[0] = VAX_PC_RELATIVE_MODE; /* ...(PC) */ | |
1298 | fragP->fr_fix += 1 + 4; | |
1299 | fix_new (fragP, old_fr_fix + 1, 4, fragP->fr_symbol, 0, | |
1300 | fragP->fr_offset, 1); | |
1301 | frag_wane (fragP); | |
1302 | } | |
1303 | break; | |
1304 | ||
1305 | default: | |
1306 | break; | |
1307 | } | |
1308 | return (fragP->fr_var + fragP->fr_fix - old_fr_fix); | |
1309 | } /* md_estimate_size_before_relax() */ | |
1310 | \f | |
1311 | /* | |
1312 | * md_convert_frag(); | |
1313 | * | |
1314 | * Called after relax() is finished. | |
1315 | * In: Address of frag. | |
1316 | * fr_type == rs_machine_dependent. | |
1317 | * fr_subtype is what the address relaxed to. | |
1318 | * | |
1319 | * Out: Any fixSs and constants are set up. | |
1320 | * Caller will turn frag into a ".space 0". | |
1321 | */ | |
1322 | void | |
1323 | md_convert_frag (fragP) | |
1324 | register fragS *fragP; | |
1325 | { | |
1326 | register char *addressP; /* -> _var to change. */ | |
1327 | register char *opcodeP; /* -> opcode char(s) to change. */ | |
1328 | register short int length_code; /* 2=long 1=word 0=byte */ | |
1329 | register short int extension; /* Size of relaxed address. */ | |
1330 | /* Added to fr_fix: incl. ALL var chars. */ | |
1331 | register symbolS *symbolP; | |
1332 | register long where; | |
1333 | register long address_of_var; | |
1334 | /* Where, in file space, is _var of *fragP? */ | |
1335 | register long target_address; | |
1336 | /* Where, in file space, does addr point? */ | |
1337 | ||
1338 | know (fragP->fr_type == rs_machine_dependent); | |
1339 | length_code = fragP->fr_subtype & 3; /* depends on ENCODE_RELAX() */ | |
1340 | know (length_code >= 0 && length_code < 3); | |
1341 | where = fragP->fr_fix; | |
1342 | addressP = fragP->fr_literal + where; | |
1343 | opcodeP = fragP->fr_opcode; | |
1344 | symbolP = fragP->fr_symbol; | |
1345 | know (symbolP); | |
1346 | target_address = symbolP->sy_value + fragP->fr_offset; | |
1347 | address_of_var = fragP->fr_address + where; | |
1348 | switch (fragP->fr_subtype) | |
1349 | { | |
1350 | case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE): | |
1351 | know (*addressP == 0 || *addressP == 0x10); /* '@' bit. */ | |
1352 | addressP[0] |= 0xAF; /* Byte displacement. */ | |
1353 | addressP[1] = target_address - (address_of_var + 2); | |
1354 | extension = 2; | |
1355 | break; | |
1356 | ||
1357 | case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD): | |
1358 | know (*addressP == 0 || *addressP == 0x10); /* '@' bit. */ | |
1359 | addressP[0] |= 0xCF; /* Word displacement. */ | |
1360 | md_number_to_chars (addressP + 1, target_address - (address_of_var + 3), 2); | |
1361 | extension = 3; | |
1362 | break; | |
1363 | ||
1364 | case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_LONG): | |
1365 | know (*addressP == 0 || *addressP == 0x10); /* '@' bit. */ | |
1366 | addressP[0] |= 0xEF; /* Long word displacement. */ | |
1367 | md_number_to_chars (addressP + 1, target_address - (address_of_var + 5), 4); | |
1368 | extension = 5; | |
1369 | break; | |
1370 | ||
1371 | case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE): | |
1372 | addressP[0] = target_address - (address_of_var + 1); | |
1373 | extension = 1; | |
1374 | break; | |
1375 | ||
1376 | case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD): | |
1377 | opcodeP[0] ^= 1; /* Reverse sense of test. */ | |
1378 | addressP[0] = 3; | |
1379 | addressP[1] = VAX_BRB + VAX_WIDEN_WORD; | |
1380 | md_number_to_chars (addressP + 2, target_address - (address_of_var + 4), 2); | |
1381 | extension = 4; | |
1382 | break; | |
1383 | ||
1384 | case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_LONG): | |
1385 | opcodeP[0] ^= 1; /* Reverse sense of test. */ | |
1386 | addressP[0] = 6; | |
1387 | addressP[1] = VAX_JMP; | |
1388 | addressP[2] = VAX_PC_RELATIVE_MODE; | |
1389 | md_number_to_chars (addressP + 3, target_address, 4); | |
1390 | extension = 7; | |
1391 | break; | |
1392 | ||
1393 | case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE): | |
1394 | addressP[0] = target_address - (address_of_var + 1); | |
1395 | extension = 1; | |
1396 | break; | |
1397 | ||
1398 | case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_WORD): | |
1399 | opcodeP[0] += VAX_WIDEN_WORD; /* brb -> brw, bsbb -> bsbw */ | |
1400 | md_number_to_chars (addressP, target_address - (address_of_var + 2), 2); | |
1401 | extension = 2; | |
1402 | break; | |
1403 | ||
1404 | case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_LONG): | |
1405 | opcodeP[0] += VAX_WIDEN_LONG; /* brb -> jmp, bsbb -> jsb */ | |
1406 | addressP[0] = VAX_PC_RELATIVE_MODE; | |
1407 | md_number_to_chars (addressP + 1, target_address - (address_of_var + 5), 4); | |
1408 | extension = 5; | |
1409 | break; | |
1410 | ||
1411 | case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_WORD): | |
1412 | md_number_to_chars (addressP, target_address - (address_of_var + 2), 2); | |
1413 | extension = 2; | |
1414 | break; | |
1415 | ||
1416 | case ENCODE_RELAX (STATE_COMPLEX_BRANCH, STATE_LONG): | |
1417 | addressP[0] = 2; | |
1418 | addressP[1] = 0; | |
1419 | addressP[2] = VAX_BRB; | |
1420 | addressP[3] = 6; | |
1421 | addressP[4] = VAX_JMP; | |
1422 | addressP[5] = VAX_PC_RELATIVE_MODE; | |
1423 | md_number_to_chars (addressP + 6, target_address, 4); | |
1424 | extension = 10; | |
1425 | break; | |
1426 | ||
1427 | case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_BYTE): | |
1428 | addressP[0] = target_address - (address_of_var + 1); | |
1429 | extension = 1; | |
1430 | break; | |
1431 | ||
1432 | case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_WORD): | |
1433 | addressP[0] = 2; | |
1434 | addressP[1] = VAX_BRB; | |
1435 | addressP[2] = 3; | |
1436 | addressP[3] = VAX_BRW; | |
1437 | md_number_to_chars (addressP + 4, target_address - (address_of_var + 6), 2); | |
1438 | extension = 6; | |
1439 | break; | |
1440 | ||
1441 | case ENCODE_RELAX (STATE_COMPLEX_HOP, STATE_LONG): | |
1442 | addressP[0] = 2; | |
1443 | addressP[1] = VAX_BRB; | |
1444 | addressP[2] = 6; | |
1445 | addressP[3] = VAX_JMP; | |
1446 | addressP[4] = VAX_PC_RELATIVE_MODE; | |
1447 | md_number_to_chars (addressP + 5, target_address, 4); | |
1448 | extension = 9; | |
1449 | break; | |
1450 | ||
1451 | default: | |
1452 | BAD_CASE (fragP->fr_subtype); | |
1453 | break; | |
1454 | } | |
1455 | fragP->fr_fix += extension; | |
1456 | } | |
1457 | ||
1458 | /* Translate internal format of relocation info into target format. | |
1459 | ||
1460 | On vax: first 4 bytes are normal unsigned long, next three bytes | |
1461 | are symbolnum, least sig. byte first. Last byte is broken up with | |
1462 | the upper nibble as nuthin, bit 3 as extern, bits 2 & 1 as length, and | |
1463 | bit 0 as pcrel. */ | |
1464 | void | |
1465 | md_ri_to_chars (the_bytes, ri) | |
1466 | char *the_bytes; | |
1467 | struct reloc_info_generic ri; | |
1468 | { | |
1469 | /* this is easy */ | |
1470 | md_number_to_chars (the_bytes, ri.r_address, sizeof (ri.r_address)); | |
1471 | /* now the fun stuff */ | |
1472 | the_bytes[6] = (ri.r_symbolnum >> 16) & 0x0ff; | |
1473 | the_bytes[5] = (ri.r_symbolnum >> 8) & 0x0ff; | |
1474 | the_bytes[4] = ri.r_symbolnum & 0x0ff; | |
1475 | the_bytes[7] = (((ri.r_extern << 3) & 0x08) | ((ri.r_length << 1) & 0x06) | | |
1476 | ((ri.r_pcrel << 0) & 0x01)) & 0x0F; | |
1477 | } | |
1478 | \f | |
1479 | /* | |
1480 | * BUGS, GRIPES, APOLOGIA, etc. | |
1481 | * | |
1482 | * The opcode table 'votstrs' needs to be sorted on opcode frequency. | |
1483 | * That is, AFTER we hash it with hash_...(), we want most-used opcodes | |
1484 | * to come out of the hash table faster. | |
1485 | * | |
1486 | * I am sorry to inflict | |
1487 | * yet another VAX assembler on the world, but RMS says we must | |
1488 | * do everything from scratch, to prevent pin-heads restricting | |
1489 | * this software. | |
1490 | */ | |
1491 | ||
1492 | /* | |
1493 | * This is a vaguely modular set of routines in C to parse VAX | |
1494 | * assembly code using DEC mnemonics. It is NOT un*x specific. | |
1495 | * | |
1496 | * The idea here is that the assembler has taken care of all: | |
1497 | * labels | |
1498 | * macros | |
1499 | * listing | |
1500 | * pseudo-ops | |
1501 | * line continuation | |
1502 | * comments | |
1503 | * condensing any whitespace down to exactly one space | |
1504 | * and all we have to do is parse 1 line into a vax instruction | |
1505 | * partially formed. We will accept a line, and deliver: | |
1506 | * an error message (hopefully empty) | |
1507 | * a skeleton VAX instruction (tree structure) | |
1508 | * textual pointers to all the operand expressions | |
1509 | * a warning message that notes a silly operand (hopefully empty) | |
1510 | */ | |
1511 | \f | |
1512 | /* | |
1513 | * E D I T H I S T O R Y | |
1514 | * | |
1515 | * 17may86 Dean Elsner. Bug if line ends immediately after opcode. | |
1516 | * 30apr86 Dean Elsner. New vip_op() uses arg block so change call. | |
1517 | * 6jan86 Dean Elsner. Crock vip_begin() to call vip_op_defaults(). | |
1518 | * 2jan86 Dean Elsner. Invent synthetic opcodes. | |
1519 | * Widen vax_opcodeT to 32 bits. Use a bit for VIT_OPCODE_SYNTHETIC, | |
1520 | * which means this is not a real opcode, it is like a macro; it will | |
1521 | * be relax()ed into 1 or more instructions. | |
1522 | * Use another bit for VIT_OPCODE_SPECIAL if the op-code is not optimised | |
1523 | * like a regular branch instruction. Option added to vip_begin(): | |
1524 | * exclude synthetic opcodes. Invent synthetic_votstrs[]. | |
1525 | * 31dec85 Dean Elsner. Invent vit_opcode_nbytes. | |
1526 | * Also make vit_opcode into a char[]. We now have n-byte vax opcodes, | |
1527 | * so caller's don't have to know the difference between a 1-byte & a | |
1528 | * 2-byte op-code. Still need vax_opcodeT concept, so we know how | |
1529 | * big an object must be to hold an op.code. | |
1530 | * 30dec85 Dean Elsner. Widen typedef vax_opcodeT in "vax-inst.h" | |
1531 | * because vax opcodes may be 16 bits. Our crufty C compiler was | |
1532 | * happily initialising 8-bit vot_codes with 16-bit numbers! | |
1533 | * (Wouldn't the 'phone company like to compress data so easily!) | |
1534 | * 29dec85 Dean Elsner. New static table vax_operand_width_size[]. | |
1535 | * Invented so we know hw many bytes a "I^#42" needs in its immediate | |
1536 | * operand. Revised struct vop in "vax-inst.h": explicitly include | |
1537 | * byte length of each operand, and it's letter-code datum type. | |
1538 | * 17nov85 Dean Elsner. Name Change. | |
1539 | * Due to ar(1) truncating names, we learned the hard way that | |
1540 | * "vax-inst-parse.c" -> "vax-inst-parse." dropping the "o" off | |
1541 | * the archived object name. SO... we shortened the name of this | |
1542 | * source file, and changed the makefile. | |
1543 | */ | |
1544 | ||
1545 | static char *op_hash = NULL; /* handle of the OPCODE hash table */ | |
1546 | /* NULL means any use before vip_begin() */ | |
1547 | /* will crash */ | |
1548 | ||
1549 | /* | |
1550 | * In: 1 character, from "bdfghloqpw" being the data-type of an operand | |
1551 | * of a vax instruction. | |
1552 | * | |
1553 | * Out: the length of an operand of that type, in bytes. | |
1554 | * Special branch operands types "-?!" have length 0. | |
1555 | */ | |
1556 | ||
1557 | static const short int vax_operand_width_size[256] = | |
1558 | { | |
1559 | ||
1560 | #define _ 0 | |
1561 | _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, | |
1562 | _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, | |
1563 | _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, | |
1564 | _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, | |
1565 | _, _, 1, _, 8, _, 4, 8, 16, _, _, _, 4, _, _, 16, /* ..b.d.fgh...l..o */ | |
1566 | _, 8, _, _, _, _, _, 2, _, _, _, _, _, _, _, _, /* .q.....w........ */ | |
1567 | _, _, 1, _, 8, _, 4, 8, 16, _, _, _, 4, _, _, 16, /* ..b.d.fgh...l..o */ | |
1568 | _, 8, _, _, _, _, _, 2, _, _, _, _, _, _, _, _, /* .q.....w........ */ | |
1569 | _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, | |
1570 | _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, | |
1571 | _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, | |
1572 | _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, | |
1573 | _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, | |
1574 | _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, | |
1575 | _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, | |
1576 | _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}; | |
1577 | #undef _ | |
1578 | \f | |
1579 | /* | |
1580 | * This perversion encodes all the vax opcodes as a bunch of strings. | |
1581 | * RMS says we should build our hash-table at run-time. Hmm. | |
1582 | * Please would someone arrange these in decreasing frequency of opcode? | |
1583 | * Because of the way hash_...() works, the most frequently used opcode | |
1584 | * should be textually first and so on. | |
1585 | * | |
1586 | * Input for this table was 'vax.opcodes', awk(1)ed by 'vax.opcodes.c.awk' . | |
1587 | * So change 'vax.opcodes', then re-generate this table. | |
1588 | */ | |
1589 | ||
1590 | #include "vax-opcode.h" | |
1591 | \f | |
1592 | /* | |
1593 | * This is a table of optional op-codes. All of them represent | |
1594 | * 'synthetic' instructions that seem popular. | |
1595 | * | |
1596 | * Here we make some pseudo op-codes. Every code has a bit set to say | |
1597 | * it is synthetic. This lets you catch them if you want to | |
1598 | * ban these opcodes. They are mnemonics for "elastic" instructions | |
1599 | * that are supposed to assemble into the fewest bytes needed to do a | |
1600 | * branch, or to do a conditional branch, or whatever. | |
1601 | * | |
1602 | * The opcode is in the usual place [low-order n*8 bits]. This means | |
1603 | * that if you mask off the bucky bits, the usual rules apply about | |
1604 | * how long the opcode is. | |
1605 | * | |
1606 | * All VAX branch displacements come at the end of the instruction. | |
1607 | * For simple branches (1-byte opcode + 1-byte displacement) the last | |
1608 | * operand is coded 'b?' where the "data type" '?' is a clue that we | |
1609 | * may reverse the sense of the branch (complement lowest order bit) | |
1610 | * and branch around a jump. This is by far the most common case. | |
1611 | * That is why the VIT_OPCODE_SYNTHETIC bit is set: it says this is | |
1612 | * a 0-byte op-code followed by 2 or more bytes of operand address. | |
1613 | * | |
1614 | * If the op-code has VIT_OPCODE_SPECIAL set, then we have a more unusual | |
1615 | * case. | |
1616 | * | |
1617 | * For JBSB & JBR the treatment is the similar, except (1) we have a 'bw' | |
1618 | * option before (2) we can directly JSB/JMP because there is no condition. | |
1619 | * These operands have 'b-' as their access/data type. | |
1620 | * | |
1621 | * That leaves a bunch of random opcodes: JACBx, JxOBxxx. In these | |
1622 | * cases, we do the same idea. JACBxxx are all marked with a 'b!' | |
1623 | * JAOBxxx & JSOBxxx are marked with a 'b:'. | |
1624 | * | |
1625 | */ | |
1626 | #if (VIT_OPCODE_SYNTHETIC != 0x80000000) | |
1627 | You have just broken the encoding below, which assumes the sign bit | |
1628 | means 'I am an imaginary instruction'. | |
1629 | #endif | |
1630 | ||
1631 | #if (VIT_OPCODE_SPECIAL != 0x40000000) | |
1632 | You have just broken the encoding below, which assumes the 0x40 M bit means | |
1633 | 'I am not to be "optimised" the way normal branches are'. | |
1634 | #endif | |
1635 | ||
1636 | static const struct vot | |
1637 | synthetic_votstrs[] = | |
1638 | { | |
1639 | {"jbsb", | |
1640 | {"b-", 0xC0000010}}, /* BSD 4.2 */ | |
1641 | /* jsb used already */ | |
1642 | {"jbr", | |
1643 | {"b-", 0xC0000011}}, /* BSD 4.2 */ | |
1644 | {"jr", | |
1645 | {"b-", 0xC0000011}}, /* consistent */ | |
1646 | {"jneq", | |
1647 | {"b?", 0x80000012}}, | |
1648 | {"jnequ", | |
1649 | {"b?", 0x80000012}}, | |
1650 | {"jeql", | |
1651 | {"b?", 0x80000013}}, | |
1652 | {"jeqlu", | |
1653 | {"b?", 0x80000013}}, | |
1654 | {"jgtr", | |
1655 | {"b?", 0x80000014}}, | |
1656 | {"jleq", | |
1657 | {"b?", 0x80000015}}, | |
1658 | /* un-used opcodes here */ | |
1659 | {"jgeq", | |
1660 | {"b?", 0x80000018}}, | |
1661 | {"jlss", | |
1662 | {"b?", 0x80000019}}, | |
1663 | {"jgtru", | |
1664 | {"b?", 0x8000001a}}, | |
1665 | {"jlequ", | |
1666 | {"b?", 0x8000001b}}, | |
1667 | {"jvc", | |
1668 | {"b?", 0x8000001c}}, | |
1669 | {"jvs", | |
1670 | {"b?", 0x8000001d}}, | |
1671 | {"jgequ", | |
1672 | {"b?", 0x8000001e}}, | |
1673 | {"jcc", | |
1674 | {"b?", 0x8000001e}}, | |
1675 | {"jlssu", | |
1676 | {"b?", 0x8000001f}}, | |
1677 | {"jcs", | |
1678 | {"b?", 0x8000001f}}, | |
1679 | ||
1680 | {"jacbw", | |
1681 | {"rwrwmwb!", 0xC000003d}}, | |
1682 | {"jacbf", | |
1683 | {"rfrfmfb!", 0xC000004f}}, | |
1684 | {"jacbd", | |
1685 | {"rdrdmdb!", 0xC000006f}}, | |
1686 | {"jacbb", | |
1687 | {"rbrbmbb!", 0xC000009d}}, | |
1688 | {"jacbl", | |
1689 | {"rlrlmlb!", 0xC00000f1}}, | |
1690 | {"jacbg", | |
1691 | {"rgrgmgb!", 0xC0004ffd}}, | |
1692 | {"jacbh", | |
1693 | {"rhrhmhb!", 0xC0006ffd}}, | |
1694 | ||
1695 | {"jbs", | |
1696 | {"rlvbb?", 0x800000e0}}, | |
1697 | {"jbc", | |
1698 | {"rlvbb?", 0x800000e1}}, | |
1699 | {"jbss", | |
1700 | {"rlvbb?", 0x800000e2}}, | |
1701 | {"jbcs", | |
1702 | {"rlvbb?", 0x800000e3}}, | |
1703 | {"jbsc", | |
1704 | {"rlvbb?", 0x800000e4}}, | |
1705 | {"jbcc", | |
1706 | {"rlvbb?", 0x800000e5}}, | |
1707 | {"jbssi", | |
1708 | {"rlvbb?", 0x800000e6}}, | |
1709 | {"jbcci", | |
1710 | {"rlvbb?", 0x800000e7}}, | |
1711 | {"jlbs", | |
1712 | {"rlb?", 0x800000e8}}, /* JF changed from rlvbb? */ | |
1713 | {"jlbc", | |
1714 | {"rlb?", 0x800000e9}}, /* JF changed from rlvbb? */ | |
1715 | ||
1716 | {"jaoblss", | |
1717 | {"rlmlb:", 0xC00000f2}}, | |
1718 | {"jaobleq", | |
1719 | {"rlmlb:", 0xC00000f3}}, | |
1720 | {"jsobgeq", | |
1721 | {"mlb:", 0xC00000f4}}, /* JF was rlmlb: */ | |
1722 | {"jsobgtr", | |
1723 | {"mlb:", 0xC00000f5}}, /* JF was rlmlb: */ | |
1724 | ||
1725 | /* CASEx has no branch addresses in our conception of it. */ | |
1726 | /* You should use ".word ..." statements after the "case ...". */ | |
1727 | ||
1728 | {"", ""} /* empty is end sentinel */ | |
1729 | ||
1730 | }; /* synthetic_votstrs */ | |
1731 | \f | |
1732 | /* | |
1733 | * v i p _ b e g i n ( ) | |
1734 | * | |
1735 | * Call me once before you decode any lines. | |
1736 | * I decode votstrs into a hash table at op_hash (which I create). | |
1737 | * I return an error text: hopefully "". | |
1738 | * If you want, I will include the 'synthetic' jXXX instructions in the | |
1739 | * instruction table. | |
1740 | * You must nominate metacharacters for eg DEC's "#", "@", "^". | |
1741 | */ | |
1742 | ||
1743 | char * | |
1744 | vip_begin (synthetic_too, immediate, indirect, displen) | |
1745 | int synthetic_too; /* TRUE means include jXXX op-codes. */ | |
1746 | char *immediate, *indirect, *displen; | |
1747 | { | |
1748 | register const struct vot *vP; /* scan votstrs */ | |
1749 | register char *retval; /* error text */ | |
1750 | ||
1751 | char *hash_insert (); /* */ | |
1752 | char *hash_new (); /* lies */ | |
1753 | ||
1754 | if ((op_hash = hash_new ())) | |
1755 | { | |
1756 | retval = ""; /* OK so far */ | |
1757 | for (vP = votstrs; *vP->vot_name && !*retval; vP++) | |
1758 | { | |
1759 | retval = hash_insert (op_hash, vP->vot_name, &vP->vot_detail); | |
1760 | } | |
1761 | if (synthetic_too) | |
1762 | { | |
1763 | for (vP = synthetic_votstrs; *vP->vot_name && !*retval; vP++) | |
1764 | { | |
1765 | retval = hash_insert (op_hash, vP->vot_name, &vP->vot_detail); | |
1766 | } | |
1767 | } | |
1768 | } | |
1769 | else | |
1770 | { | |
1771 | retval = "virtual memory exceeded"; | |
1772 | } | |
1773 | #ifndef CONST_TABLE | |
1774 | vip_op_defaults (immediate, indirect, displen); | |
1775 | #endif | |
1776 | ||
1777 | return (retval); | |
1778 | } | |
1779 | ||
1780 | ||
1781 | /* | |
1782 | * v i p _ e n d ( ) | |
1783 | * | |
1784 | * Call me once after you have decoded all lines. | |
1785 | * I do any cleaning-up needed. | |
1786 | * | |
1787 | * We don't have to do any cleanup ourselves: all of our operand | |
1788 | * symbol table is static, and free()ing it is naughty. | |
1789 | */ | |
1790 | vip_end () | |
1791 | { | |
1792 | } | |
1793 | \f | |
1794 | /* | |
1795 | * v i p ( ) | |
1796 | * | |
1797 | * This converts a string into a vax instruction. | |
1798 | * The string must be a bare single instruction in dec-vax (with BSD4 frobs) | |
1799 | * format. | |
1800 | * It provides some error messages: at most one fatal error message (which | |
1801 | * stops the scan) and at most one warning message for each operand. | |
1802 | * The vax instruction is returned in exploded form, since we have no | |
1803 | * knowledge of how you parse (or evaluate) your expressions. | |
1804 | * We do however strip off and decode addressing modes and operation | |
1805 | * mnemonic. | |
1806 | * | |
1807 | * The exploded instruction is returned to a struct vit of your choice. | |
1808 | * #include "vax-inst.h" to know what a struct vit is. | |
1809 | * | |
1810 | * This function's value is a string. If it is not "" then an internal | |
1811 | * logic error was found: read this code to assign meaning to the string. | |
1812 | * No argument string should generate such an error string: | |
1813 | * it means a bug in our code, not in the user's text. | |
1814 | * | |
1815 | * You MUST have called vip_begin() once and vip_end() never before using | |
1816 | * this function. | |
1817 | */ | |
1818 | ||
1819 | char * /* "" or bug string */ | |
1820 | vip (vitP, instring) | |
1821 | struct vit *vitP; /* We build an exploded instruction here. */ | |
1822 | char *instring; /* Text of a vax instruction: we modify. */ | |
1823 | { | |
1824 | register struct vot_wot *vwP; /* How to bit-encode this opcode. */ | |
1825 | register char *p; /* 1/skip whitespace.2/scan vot_how */ | |
1826 | register char *q; /* */ | |
1827 | register char *bug; /* "" or program logic error */ | |
1828 | register unsigned char count; /* counts number of operands seen */ | |
1829 | register struct vop *operandp;/* scan operands in struct vit */ | |
1830 | register char *alloperr; /* error over all operands */ | |
1831 | register char c; /* Remember char, (we clobber it */ | |
1832 | /* with '\0' temporarily). */ | |
1833 | register vax_opcodeT oc; /* Op-code of this instruction. */ | |
1834 | ||
1835 | struct vot_wot *hash_find (); | |
1836 | char *vip_op (); | |
1837 | ||
1838 | bug = ""; | |
1839 | if (*instring == ' ') | |
1840 | ++instring; /* Skip leading whitespace. */ | |
1841 | for (p = instring; *p && *p != ' '; p++) | |
1842 | ; /* MUST end in end-of-string or exactly 1 space. */ | |
1843 | /* Scanned up to end of operation-code. */ | |
1844 | /* Operation-code is ended with whitespace. */ | |
1845 | if (p - instring == 0) | |
1846 | { | |
1847 | vitP->vit_error = "No operator"; | |
1848 | count = 0; | |
1849 | bzero (vitP->vit_opcode, sizeof (vitP->vit_opcode)); | |
1850 | } | |
1851 | else | |
1852 | { | |
1853 | c = *p; | |
1854 | *p = '\0'; | |
1855 | /* | |
1856 | * Here with instring pointing to what better be an op-name, and p | |
1857 | * pointing to character just past that. | |
1858 | * We trust instring points to an op-name, with no whitespace. | |
1859 | */ | |
1860 | vwP = hash_find (op_hash, instring); | |
1861 | *p = c; /* Restore char after op-code. */ | |
1862 | if (vwP == 0) | |
1863 | { | |
1864 | vitP->vit_error = "Unknown operator"; | |
1865 | count = 0; | |
1866 | bzero (vitP->vit_opcode, sizeof (vitP->vit_opcode)); | |
1867 | } | |
1868 | else | |
1869 | { | |
1870 | /* | |
1871 | * We found a match! So lets pick up as many operands as the | |
1872 | * instruction wants, and even gripe if there are too many. | |
1873 | * We expect comma to seperate each operand. | |
1874 | * We let instring track the text, while p tracks a part of the | |
1875 | * struct vot. | |
1876 | */ | |
1877 | /* | |
1878 | * The lines below know about 2-byte opcodes starting FD,FE or FF. | |
1879 | * They also understand synthetic opcodes. Note: | |
1880 | * we return 32 bits of opcode, including bucky bits, BUT | |
1881 | * an opcode length is either 8 or 16 bits for vit_opcode_nbytes. | |
1882 | */ | |
1883 | oc = vwP->vot_code; /* The op-code. */ | |
1884 | vitP->vit_opcode_nbytes = (oc & 0xFF) >= 0xFD ? 2 : 1; | |
1885 | md_number_to_chars (vitP->vit_opcode, oc, 4); | |
1886 | count = 0; /* no operands seen yet */ | |
1887 | instring = p; /* point just past operation code */ | |
1888 | alloperr = ""; | |
1889 | for (p = vwP->vot_how, operandp = vitP->vit_operand; | |
1890 | !*alloperr && !*bug && *p; | |
1891 | operandp++, p += 2 | |
1892 | ) | |
1893 | { | |
1894 | /* | |
1895 | * Here to parse one operand. Leave instring pointing just | |
1896 | * past any one ',' that marks the end of this operand. | |
1897 | */ | |
1898 | if (!p[1]) | |
1899 | bug = "p"; /* ODD(!!) number of bytes in vot_how?? */ | |
1900 | else if (*instring) | |
1901 | { | |
1902 | for (q = instring; (c = *q) && c != ','; q++) | |
1903 | ; | |
1904 | /* | |
1905 | * Q points to ',' or '\0' that ends argument. C is that | |
1906 | * character. | |
1907 | */ | |
1908 | *q = 0; | |
1909 | operandp->vop_width = p[1]; | |
1910 | operandp->vop_nbytes = vax_operand_width_size[p[1]]; | |
1911 | operandp->vop_access = p[0]; | |
1912 | bug = vip_op (instring, operandp); | |
1913 | *q = c; /* Restore input text. */ | |
1914 | if (*(operandp->vop_error)) | |
1915 | alloperr = "Bad operand"; | |
1916 | instring = q + (c ? 1 : 0); /* next operand (if any) */ | |
1917 | count++; /* won another argument, may have an operr */ | |
1918 | } | |
1919 | else | |
1920 | alloperr = "Not enough operands"; | |
1921 | } | |
1922 | if (!*alloperr) | |
1923 | { | |
1924 | if (*instring == ' ') | |
1925 | instring++; /* Skip whitespace. */ | |
1926 | if (*instring) | |
1927 | alloperr = "Too many operands"; | |
1928 | } | |
1929 | vitP->vit_error = alloperr; | |
1930 | } | |
1931 | } | |
1932 | vitP->vit_operands = count; | |
1933 | return (bug); | |
1934 | } | |
1935 | \f | |
1936 | #ifdef test | |
1937 | ||
1938 | /* | |
1939 | * Test program for above. | |
1940 | */ | |
1941 | ||
1942 | struct vit myvit; /* build an exploded vax instruction here */ | |
1943 | char answer[100]; /* human types a line of vax assembler here */ | |
1944 | char *mybug; /* "" or an internal logic diagnostic */ | |
1945 | int mycount; /* number of operands */ | |
1946 | struct vop *myvop; /* scan operands from myvit */ | |
1947 | int mysynth; /* TRUE means want synthetic opcodes. */ | |
1948 | char my_immediate[200]; | |
1949 | char my_indirect[200]; | |
1950 | char my_displen[200]; | |
1951 | ||
1952 | char *vip (); | |
1953 | ||
1954 | main () | |
1955 | { | |
1956 | char *p; | |
1957 | char *vip_begin (); | |
1958 | ||
1959 | printf ("0 means no synthetic instructions. "); | |
1960 | printf ("Value for vip_begin? "); | |
1961 | gets (answer); | |
1962 | sscanf (answer, "%d", &mysynth); | |
1963 | printf ("Synthetic opcodes %s be included.\n", mysynth ? "will" : "will not"); | |
1964 | printf ("enter immediate symbols eg enter # "); | |
1965 | gets (my_immediate); | |
1966 | printf ("enter indirect symbols eg enter @ "); | |
1967 | gets (my_indirect); | |
1968 | printf ("enter displen symbols eg enter ^ "); | |
1969 | gets (my_displen); | |
1970 | if (*(p = vip_begin (mysynth, my_immediate, my_indirect, my_displen))) | |
1971 | { | |
1972 | error ("vip_begin=%s", p); | |
1973 | } | |
1974 | printf ("An empty input line will quit you from the vax instruction parser\n"); | |
1975 | for (;;) | |
1976 | { | |
1977 | printf ("vax instruction: "); | |
1978 | fflush (stdout); | |
1979 | gets (answer); | |
1980 | if (!*answer) | |
1981 | { | |
1982 | break; /* out of for each input text loop */ | |
1983 | } | |
1984 | mybug = vip (&myvit, answer); | |
1985 | if (*mybug) | |
1986 | { | |
1987 | printf ("BUG:\"%s\"\n", mybug); | |
1988 | } | |
1989 | if (*myvit.vit_error) | |
1990 | { | |
1991 | printf ("ERR:\"%s\"\n", myvit.vit_error); | |
1992 | } | |
1993 | printf ("opcode="); | |
1994 | for (mycount = myvit.vit_opcode_nbytes, p = myvit.vit_opcode; | |
1995 | mycount; | |
1996 | mycount--, p++ | |
1997 | ) | |
1998 | { | |
1999 | printf ("%02x ", *p & 0xFF); | |
2000 | } | |
2001 | printf (" operand count=%d.\n", mycount = myvit.vit_operands); | |
2002 | for (myvop = myvit.vit_operand; mycount; mycount--, myvop++) | |
2003 | { | |
2004 | printf ("mode=%xx reg=%xx ndx=%xx len='%c'=%c%c%d. expr=\"", | |
2005 | myvop->vop_mode, myvop->vop_reg, myvop->vop_ndx, | |
2006 | myvop->vop_short, myvop->vop_access, myvop->vop_width, | |
2007 | myvop->vop_nbytes); | |
2008 | for (p = myvop->vop_expr_begin; p <= myvop->vop_expr_end; p++) | |
2009 | { | |
2010 | putchar (*p); | |
2011 | } | |
2012 | printf ("\"\n"); | |
2013 | if (*myvop->vop_error) | |
2014 | { | |
2015 | printf (" err:\"%s\"\n", myvop->vop_error); | |
2016 | } | |
2017 | if (*myvop->vop_warn) | |
2018 | { | |
2019 | printf (" wrn:\"%s\"\n", myvop->vop_warn); | |
2020 | } | |
2021 | } | |
2022 | } | |
2023 | vip_end (); | |
2024 | exit (); | |
2025 | } | |
2026 | ||
2027 | #endif /* #ifdef test */ | |
2028 | ||
2029 | /* end of vax_ins_parse.c */ | |
2030 | ||
2031 | /* JF this used to be a separate file also */ | |
2032 | /* vax_reg_parse.c - convert a VAX register name to a number */ | |
2033 | ||
2034 | /* Copyright (C) 1987 Free Software Foundation, Inc. A part of GNU. */ | |
2035 | ||
2036 | /* | |
2037 | * v a x _ r e g _ p a r s e ( ) | |
2038 | * | |
2039 | * Take 3 char.s, the last of which may be `\0` (non-existent) | |
2040 | * and return the VAX register number that they represent. | |
2041 | * | |
2042 | * Return -1 if they don't form a register name. Good names return | |
2043 | * a number from 0:15 inclusive. | |
2044 | * | |
2045 | * Case is not important in a name. | |
2046 | * | |
2047 | * Register names understood are: | |
2048 | * | |
2049 | * R0 | |
2050 | * R1 | |
2051 | * R2 | |
2052 | * R3 | |
2053 | * R4 | |
2054 | * R5 | |
2055 | * R6 | |
2056 | * R7 | |
2057 | * R8 | |
2058 | * R9 | |
2059 | * R10 | |
2060 | * R11 | |
2061 | * R12 AP | |
2062 | * R13 FP | |
2063 | * R14 SP | |
2064 | * R15 PC | |
2065 | * | |
2066 | */ | |
2067 | ||
2068 | #include <ctype.h> | |
2069 | #define AP (12) | |
2070 | #define FP (13) | |
2071 | #define SP (14) | |
2072 | #define PC (15) | |
2073 | \f | |
2074 | int /* return -1 or 0:15 */ | |
2075 | vax_reg_parse (c1, c2, c3) /* 3 chars of register name */ | |
2076 | char c1, c2, c3; /* c3 == 0 if 2-character reg name */ | |
2077 | { | |
2078 | register int retval; /* return -1:15 */ | |
2079 | ||
2080 | retval = -1; | |
2081 | ||
2082 | if (isupper (c1)) | |
2083 | c1 = tolower (c1); | |
2084 | if (isupper (c2)) | |
2085 | c2 = tolower (c2); | |
2086 | if (isdigit (c2) && c1 == 'r') | |
2087 | { | |
2088 | retval = c2 - '0'; | |
2089 | if (isdigit (c3)) | |
2090 | { | |
2091 | retval = retval * 10 + c3 - '0'; | |
2092 | retval = (retval > 15) ? -1 : retval; | |
2093 | /* clamp the register value to 1 hex digit */ | |
2094 | } | |
2095 | else if (c3) | |
2096 | retval = -1; /* c3 must be '\0' or a digit */ | |
2097 | } | |
2098 | else if (c3) /* There are no three letter regs */ | |
2099 | retval = -1; | |
2100 | else if (c2 == 'p') | |
2101 | { | |
2102 | switch (c1) | |
2103 | { | |
2104 | case 's': | |
2105 | retval = SP; | |
2106 | break; | |
2107 | case 'f': | |
2108 | retval = FP; | |
2109 | break; | |
2110 | case 'a': | |
2111 | retval = AP; | |
2112 | break; | |
2113 | default: | |
2114 | retval = -1; | |
2115 | } | |
2116 | } | |
2117 | else if (c1 == 'p' && c2 == 'c') | |
2118 | retval = PC; | |
2119 | else | |
2120 | retval = -1; | |
2121 | return (retval); | |
2122 | } | |
2123 | ||
2124 | /* | |
2125 | * v i p _ o p ( ) | |
2126 | * | |
2127 | * Parse a vax operand in DEC assembler notation. | |
2128 | * For speed, expect a string of whitespace to be reduced to a single ' '. | |
2129 | * This is the case for GNU AS, and is easy for other DEC-compatible | |
2130 | * assemblers. | |
2131 | * | |
2132 | * Knowledge about DEC VAX assembler operand notation lives here. | |
2133 | * This doesn't even know what a register name is, except it believes | |
2134 | * all register names are 2 or 3 characters, and lets vax_reg_parse() say | |
2135 | * what number each name represents. | |
2136 | * It does, however, know that PC, SP etc are special registers so it can | |
2137 | * detect addressing modes that are silly for those registers. | |
2138 | * | |
2139 | * Where possible, it delivers 1 fatal or 1 warning message if the operand | |
2140 | * is suspect. Exactly what we test for is still evolving. | |
2141 | */ | |
2142 | ||
2143 | /* | |
2144 | * B u g s | |
2145 | * | |
2146 | * Arg block. | |
2147 | * | |
2148 | * There were a number of 'mismatched argument type' bugs to vip_op. | |
2149 | * The most general solution is to typedef each (of many) arguments. | |
2150 | * We used instead a typedef'd argument block. This is less modular | |
2151 | * than using seperate return pointers for each result, but runs faster | |
2152 | * on most engines, and seems to keep programmers happy. It will have | |
2153 | * to be done properly if we ever want to use vip_op as a general-purpose | |
2154 | * module (it was designed to be). | |
2155 | * | |
2156 | * G^ | |
2157 | * | |
2158 | * Doesn't support DEC "G^" format operands. These always take 5 bytes | |
2159 | * to express, and code as modes 8F or 9F. Reason: "G^" deprives you of | |
2160 | * optimising to (say) a "B^" if you are lucky in the way you link. | |
2161 | * When someone builds a linker smart enough to convert "G^" to "B^", "W^" | |
2162 | * whenever possible, then we should implement it. | |
2163 | * If there is some other use for "G^", feel free to code it in! | |
2164 | * | |
2165 | * | |
2166 | * speed | |
2167 | * | |
2168 | * If I nested if()s more, I could avoid testing (*err) which would save | |
2169 | * time, space and page faults. I didn't nest all those if()s for clarity | |
2170 | * and because I think the mode testing can be re-arranged 1st to test the | |
2171 | * commoner constructs 1st. Does anybody have statistics on this? | |
2172 | * | |
2173 | * | |
2174 | * | |
2175 | * error messages | |
2176 | * | |
2177 | * In future, we should be able to 'compose' error messages in a scratch area | |
2178 | * and give the user MUCH more informative error messages. Although this takes | |
2179 | * a little more code at run-time, it will make this module much more self- | |
2180 | * documenting. As an example of what sucks now: most error messages have | |
2181 | * hardwired into them the DEC VAX metacharacters "#^@" which are nothing like | |
2182 | * the Un*x characters "$`*", that most users will expect from this AS. | |
2183 | */ | |
2184 | \f | |
2185 | /* | |
2186 | * The input is a string, ending with '\0'. | |
2187 | * | |
2188 | * We also require a 'hint' of what kind of operand is expected: so | |
2189 | * we can remind caller not to write into literals for instance. | |
2190 | * | |
2191 | * The output is a skeletal instruction. | |
2192 | * | |
2193 | * The algorithm has two parts. | |
2194 | * 1. extract the syntactic features (parse off all the @^#-()+[] mode crud); | |
2195 | * 2. express the @^#-()+[] as some parameters suited to further analysis. | |
2196 | * | |
2197 | * 2nd step is where we detect the googles of possible invalid combinations | |
2198 | * a human (or compiler) might write. Note that if we do a half-way | |
2199 | * decent assembler, we don't know how long to make (eg) displacement | |
2200 | * fields when we first meet them (because they may not have defined values). | |
2201 | * So we must wait until we know how many bits are needed for each address, | |
2202 | * then we can know both length and opcodes of instructions. | |
2203 | * For reason(s) above, we will pass to our caller a 'broken' instruction | |
2204 | * of these major components, from which our caller can generate instructions: | |
2205 | * - displacement length I^ S^ L^ B^ W^ unspecified | |
2206 | * - mode (many) | |
2207 | * - register R0-R15 or absent | |
2208 | * - index register R0-R15 or absent | |
2209 | * - expression text what we don't parse | |
2210 | * - error text(s) why we couldn't understand the operand | |
2211 | */ | |
2212 | ||
2213 | /* | |
2214 | * To decode output of this, test errtxt. If errtxt[0] == '\0', then | |
2215 | * we had no errors that prevented parsing. Also, if we ever report | |
2216 | * an internal bug, errtxt[0] is set non-zero. So one test tells you | |
2217 | * if the other outputs are to be taken seriously. | |
2218 | */ | |
2219 | ||
2220 | ||
2221 | /* vax registers we need to know */ | |
2222 | /* JF #define SP (14) | |
2223 | /* JF for one big happy file #define PC (15) */ | |
2224 | ||
2225 | /* useful ideas */ | |
2226 | /* #define TRUE (1) */ | |
2227 | /* #define FALSE (0) */ | |
2228 | \f | |
2229 | /* | |
2230 | * Because this module is useful for both VMS and UN*X style assemblers | |
2231 | * and because of the variety of UN*X assemblers we must recognise | |
2232 | * the different conventions for assembler operand notation. For example | |
2233 | * VMS says "#42" for immediate mode, while most UN*X say "$42". | |
2234 | * We permit arbitrary sets of (single) characters to represent the | |
2235 | * 3 concepts that DEC writes '#', '@', '^'. | |
2236 | */ | |
2237 | ||
2238 | /* character tests */ | |
2239 | #define VIP_IMMEDIATE 01 /* Character is like DEC # */ | |
2240 | #define VIP_INDIRECT 02 /* Char is like DEC @ */ | |
2241 | #define VIP_DISPLEN 04 /* Char is like DEC ^ */ | |
2242 | ||
2243 | #define IMMEDIATEP(c) (vip_metacharacters [(c)&0xff]&VIP_IMMEDIATE) | |
2244 | #define INDIRECTP(c) (vip_metacharacters [(c)&0xff]&VIP_INDIRECT) | |
2245 | #define DISPLENP(c) (vip_metacharacters [(c)&0xff]&VIP_DISPLEN) | |
2246 | ||
2247 | /* We assume 8 bits per byte. Use vip_op_defaults() to set these up BEFORE we | |
2248 | * are ever called. | |
2249 | */ | |
2250 | ||
2251 | #if defined(CONST_TABLE) | |
2252 | #define _ 0, | |
2253 | #define I VIP_IMMEDIATE, | |
2254 | #define S VIP_INDIRECT, | |
2255 | #define D VIP_DISPLEN, | |
2256 | static const char | |
2257 | vip_metacharacters[256] = { | |
2258 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*^@ ^A ^B ^C ^D ^E ^F ^G ^H ^I ^J ^K ^L ^M ^N ^O*/ | |
2259 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*^P ^Q ^R ^S ^T ^U ^V ^W ^X ^Y ^Z ^[ ^\ ^] ^^ ^_*/ | |
2260 | _ _ _ _ I _ _ _ _ _ S _ _ _ _ _/*sp ! " # $ % & ' ( ) * + , - . /*/ | |
2261 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*0 1 2 3 4 5 6 7 8 9 : ; < = > ?*/ | |
2262 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*@ A B C D E F G H I J K L M N O*/ | |
2263 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*P Q R S T U V W X Y Z [ \ ] ^ _*/ | |
2264 | D _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*` a b c d e f g h i j k l m n o*/ | |
2265 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _/*p q r s t u v w x y z { | } ~ ^?*/ | |
2266 | ||
2267 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |
2268 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |
2269 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |
2270 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |
2271 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |
2272 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |
2273 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |
2274 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ | |
2275 | }; | |
2276 | #undef _ | |
2277 | #undef I | |
2278 | #undef S | |
2279 | #undef D | |
2280 | #else | |
2281 | static char vip_metacharacters[256]; | |
2282 | ||
2283 | /* Macro is faster under GCC; The constant table is faster yet, but only works with ASCII */ | |
2284 | #if 0 | |
2285 | static | |
2286 | #ifdef __GNUC__ | |
2287 | inline | |
2288 | #endif | |
2289 | static void | |
2290 | vip_op_1(bit,syms) | |
2291 | int bit; | |
2292 | char *syms; | |
2293 | { | |
2294 | unsigned char t; | |
2295 | ||
2296 | while(t= *syms++) | |
2297 | vip_metacharacters[t]|=bit; | |
2298 | } | |
2299 | #else | |
2300 | #define vip_op_1(bit,syms) { \ | |
2301 | unsigned char t; \ | |
2302 | char *table=vip_metacharacters; \ | |
2303 | while(t= *syms++) \ | |
2304 | table[t]|=bit; \ | |
2305 | } | |
2306 | #endif | |
2307 | ||
2308 | vip_op_defaults (immediate, indirect, displen) /* can be called any time */ | |
2309 | char *immediate, /* Strings of characters for each job. */ | |
2310 | *indirect, *displen; /* more arguments may appear in future! */ | |
2311 | { | |
2312 | vip_op_1 (VIP_IMMEDIATE, immediate); | |
2313 | vip_op_1 (VIP_INDIRECT, indirect); | |
2314 | vip_op_1 (VIP_DISPLEN, displen); | |
2315 | } | |
2316 | #endif | |
2317 | ||
2318 | \f | |
2319 | /* | |
2320 | * Dec defines the semantics of address modes (and values) | |
2321 | * by a two-letter code, explained here. | |
2322 | * | |
2323 | * letter 1: access type | |
2324 | * | |
2325 | * a address calculation - no data access, registers forbidden | |
2326 | * b branch displacement | |
2327 | * m read - let go of bus - write back "modify" | |
2328 | * r read | |
2329 | * v bit field address: like 'a' but registers are OK | |
2330 | * w write | |
2331 | * space no operator (eg ".long foo") [our convention] | |
2332 | * | |
2333 | * letter 2: data type (i.e. width, alignment) | |
2334 | * | |
2335 | * b byte | |
2336 | * d double precision floating point (D format) | |
2337 | * f single precision floating point (F format) | |
2338 | * g G format floating | |
2339 | * h H format floating | |
2340 | * l longword | |
2341 | * o octaword | |
2342 | * q quadword | |
2343 | * w word | |
2344 | * ? simple synthetic branch operand | |
2345 | * - unconditional synthetic JSB/JSR operand | |
2346 | * ! complex synthetic branch operand | |
2347 | * | |
2348 | * The '-?!' letter 2's are not for external consumption. They are used | |
2349 | * for various assemblers. Generally, all unknown widths are assumed 0. | |
2350 | * We don't limit your choice of width character. | |
2351 | * | |
2352 | * DEC operands are hard work to parse. For example, '@' as the first | |
2353 | * character means indirect (deferred) mode but elswhere it is a shift | |
2354 | * operator. | |
2355 | * The long-winded explanation of how this is supposed to work is | |
2356 | * cancelled. Read a DEC vax manual. | |
2357 | * We try hard not to parse anything that MIGHT be part of the expression | |
2358 | * buried in that syntax. For example if we see @...(Rn) we don't check | |
2359 | * for '-' before the '(' because mode @-(Rn) does not exist. | |
2360 | * | |
2361 | * After parsing we have: | |
2362 | * | |
2363 | * at TRUE if leading '@' (or Un*x '*') | |
2364 | * len takes one value from " bilsw". eg B^ -> 'b'. | |
2365 | * hash TRUE if leading '#' (or Un*x '$') | |
2366 | * expr_begin, expr_end the expression we did not parse | |
2367 | * even though we don't interpret it, we make use | |
2368 | * of its presence or absence. | |
2369 | * sign -1: -(Rn) 0: absent +1: (Rn)+ | |
2370 | * paren TRUE if () are around register | |
2371 | * reg major register number 0:15 -1 means absent | |
2372 | * ndx index register number 0:15 -1 means absent | |
2373 | * | |
2374 | * Again, I dare not explain it: just trace ALL the code! | |
2375 | */ | |
2376 | \f | |
2377 | char * /* (code here) bug message, "" = OK */ | |
2378 | /* our code bug, NOT bad assembly language */ | |
2379 | vip_op (optext, vopP) | |
2380 | char *optext; /* user's input string e.g.: */ | |
2381 | /* "@B^foo@bar(AP)[FP]:" */ | |
2382 | struct vop *vopP; /* In: vop_access, vop_width. */ | |
2383 | /* Out: _ndx, _reg, _mode, _short, _warn, */ | |
2384 | /* _error _expr_begin, _expr_end, _nbytes. */ | |
2385 | /* vop_nbytes : number of bytes in a datum. */ | |
2386 | { | |
2387 | char *p; /* track operand text forward */ | |
2388 | char *q; /* track operand text backward */ | |
2389 | int at; /* TRUE if leading '@' ('*') seen */ | |
2390 | char len; /* one of " bilsw" */ | |
2391 | int hash; /* TRUE if leading '#' ('$') seen */ | |
2392 | int sign; /* -1, 0 or +1 */ | |
2393 | int paren; /* TRUE if () surround register */ | |
2394 | int reg; /* register number, -1:absent */ | |
2395 | int ndx; /* index register number -1:absent */ | |
2396 | char *bug; /* report any logic error in here, ""==OK */ | |
2397 | char *err; /* report illegal operand, ""==OK */ | |
2398 | /* " " is a FAKE error: means we won */ | |
2399 | /* ANY err that begins with ' ' is a fake. */ | |
2400 | /* " " is converted to "" before return */ | |
2401 | char *wrn; /* warn about weird modes pf address */ | |
2402 | char *oldq; /* preserve q in case we backup */ | |
2403 | int mode; /* build up 4-bit operand mode here */ | |
2404 | /* note: index mode is in ndx, this is */ | |
2405 | /* the major mode of operand address */ | |
2406 | /* | |
2407 | * Notice how we move wrong-arg-type bugs INSIDE this module: if we | |
2408 | * get the types wrong below, we lose at compile time rather than at | |
2409 | * lint or run time. | |
2410 | */ | |
2411 | char access; /* vop_access. */ | |
2412 | char width; /* vop_width. */ | |
2413 | ||
2414 | int vax_reg_parse (); /* returns 0:15 or -1 if not a register */ | |
2415 | ||
2416 | access = vopP->vop_access; | |
2417 | width = vopP->vop_width; | |
2418 | bug = /* none of our code bugs (yet) */ | |
2419 | err = /* no user text errors */ | |
2420 | wrn = ""; /* no warnings even */ | |
2421 | ||
2422 | p = optext; | |
2423 | ||
2424 | if (*p == ' ') /* Expect all whitespace reduced to ' '. */ | |
2425 | p++; /* skip over whitespace */ | |
2426 | ||
2427 | if (at = INDIRECTP (*p)) | |
2428 | { /* TRUE if *p=='@'(or '*' for Un*x) */ | |
2429 | p++; /* at is determined */ | |
2430 | if (*p == ' ') /* Expect all whitespace reduced to ' '. */ | |
2431 | p++; /* skip over whitespace */ | |
2432 | } | |
2433 | ||
2434 | /* | |
2435 | * This code is subtle. It tries to detect all legal (letter)'^' | |
2436 | * but it doesn't waste time explicitly testing for premature '\0' because | |
2437 | * this case is rejected as a mismatch against either (letter) or '^'. | |
2438 | */ | |
2439 | { | |
2440 | register char c; | |
2441 | ||
2442 | c = *p; | |
2443 | if (isupper (c)) | |
2444 | c = tolower (c); | |
2445 | if (DISPLENP (p[1]) && strchr ("bilws", len = c)) | |
2446 | p += 2; /* skip (letter) '^' */ | |
2447 | else /* no (letter) '^' seen */ | |
2448 | len = ' '; /* len is determined */ | |
2449 | } | |
2450 | ||
2451 | if (*p == ' ') /* Expect all whitespace reduced to ' '. */ | |
2452 | p++; /* skip over whitespace */ | |
2453 | ||
2454 | if (hash = IMMEDIATEP (*p)) /* TRUE if *p=='#' ('$' for Un*x) */ | |
2455 | p++; /* hash is determined */ | |
2456 | ||
2457 | /* | |
2458 | * p points to what may be the beginning of an expression. | |
2459 | * We have peeled off the front all that is peelable. | |
2460 | * We know at, len, hash. | |
2461 | * | |
2462 | * Lets point q at the end of the text and parse that (backwards). | |
2463 | */ | |
2464 | ||
2465 | for (q = p; *q; q++) | |
2466 | ; | |
2467 | q--; /* now q points at last char of text */ | |
2468 | \f | |
2469 | if (*q == ' ' && q >= p) /* Expect all whitespace reduced to ' '. */ | |
2470 | q--; | |
2471 | /* reverse over whitespace, but don't */ | |
2472 | /* run back over *p */ | |
2473 | ||
2474 | /* | |
2475 | * As a matter of policy here, we look for [Rn], although both Rn and S^# | |
2476 | * forbid [Rn]. This is because it is easy, and because only a sick | |
2477 | * cyborg would have [...] trailing an expression in a VAX-like assembler. | |
2478 | * A meticulous parser would first check for Rn followed by '(' or '[' | |
2479 | * and not parse a trailing ']' if it found another. We just ban expressions | |
2480 | * ending in ']'. | |
2481 | */ | |
2482 | if (*q == ']') | |
2483 | { | |
2484 | while (q >= p && *q != '[') | |
2485 | q--; | |
2486 | /* either q<p or we got matching '[' */ | |
2487 | if (q < p) | |
2488 | err = "no '[' to match ']'"; | |
2489 | else | |
2490 | { | |
2491 | /* | |
2492 | * Confusers like "[]" will eventually lose with a bad register | |
2493 | * name error. So again we don't need to check for early '\0'. | |
2494 | */ | |
2495 | if (q[3] == ']') | |
2496 | ndx = vax_reg_parse (q[1], q[2], 0); | |
2497 | else if (q[4] == ']') | |
2498 | ndx = vax_reg_parse (q[1], q[2], q[3]); | |
2499 | else | |
2500 | ndx = -1; | |
2501 | /* | |
2502 | * Since we saw a ']' we will demand a register name in the []. | |
2503 | * If luser hasn't given us one: be rude. | |
2504 | */ | |
2505 | if (ndx < 0) | |
2506 | err = "bad register in []"; | |
2507 | else if (ndx == PC) | |
2508 | err = "[PC] index banned"; | |
2509 | else | |
2510 | q--; /* point q just before "[...]" */ | |
2511 | } | |
2512 | } | |
2513 | else | |
2514 | ndx = -1; /* no ']', so no iNDeX register */ | |
2515 | ||
2516 | /* | |
2517 | * If err = "..." then we lost: run away. | |
2518 | * Otherwise ndx == -1 if there was no "[...]". | |
2519 | * Otherwise, ndx is index register number, and q points before "[...]". | |
2520 | */ | |
2521 | \f | |
2522 | if (*q == ' ' && q >= p) /* Expect all whitespace reduced to ' '. */ | |
2523 | q--; | |
2524 | /* reverse over whitespace, but don't */ | |
2525 | /* run back over *p */ | |
2526 | if (!*err) | |
2527 | { | |
2528 | sign = 0; /* no ()+ or -() seen yet */ | |
2529 | ||
2530 | if (q > p + 3 && *q == '+' && q[-1] == ')') | |
2531 | { | |
2532 | sign = 1; /* we saw a ")+" */ | |
2533 | q--; /* q points to ')' */ | |
2534 | } | |
2535 | ||
2536 | if (*q == ')' && q > p + 2) | |
2537 | { | |
2538 | paren = TRUE; /* assume we have "(...)" */ | |
2539 | while (q >= p && *q != '(') | |
2540 | q--; | |
2541 | /* either q<p or we got matching '(' */ | |
2542 | if (q < p) | |
2543 | err = "no '(' to match ')'"; | |
2544 | else | |
2545 | { | |
2546 | /* | |
2547 | * Confusers like "()" will eventually lose with a bad register | |
2548 | * name error. So again we don't need to check for early '\0'. | |
2549 | */ | |
2550 | if (q[3] == ')') | |
2551 | reg = vax_reg_parse (q[1], q[2], 0); | |
2552 | else if (q[4] == ')') | |
2553 | reg = vax_reg_parse (q[1], q[2], q[3]); | |
2554 | else | |
2555 | reg = -1; | |
2556 | /* | |
2557 | * Since we saw a ')' we will demand a register name in the ')'. | |
2558 | * This is nasty: why can't our hypothetical assembler permit | |
2559 | * parenthesised expressions? BECAUSE I AM LAZY! That is why. | |
2560 | * Abuse luser if we didn't spy a register name. | |
2561 | */ | |
2562 | if (reg < 0) | |
2563 | { | |
2564 | /* JF allow parenthasized expressions. I hope this works */ | |
2565 | paren = FALSE; | |
2566 | while (*q != ')') | |
2567 | q++; | |
2568 | /* err = "unknown register in ()"; */ | |
2569 | } | |
2570 | else | |
2571 | q--; /* point just before '(' of "(...)" */ | |
2572 | /* | |
2573 | * If err == "..." then we lost. Run away. | |
2574 | * Otherwise if reg >= 0 then we saw (Rn). | |
2575 | */ | |
2576 | } | |
2577 | /* | |
2578 | * If err == "..." then we lost. | |
2579 | * Otherwise paren==TRUE and reg = register in "()". | |
2580 | */ | |
2581 | } | |
2582 | else | |
2583 | paren = FALSE; | |
2584 | /* | |
2585 | * If err == "..." then we lost. | |
2586 | * Otherwise, q points just before "(Rn)", if any. | |
2587 | * If there was a "(...)" then paren==TRUE, and reg is the register. | |
2588 | */ | |
2589 | \f | |
2590 | /* | |
2591 | * We should only seek '-' of "-(...)" if: | |
2592 | * we saw "(...)" paren == TRUE | |
2593 | * we have no errors so far ! *err | |
2594 | * we did not see '+' of "(...)+" sign < 1 | |
2595 | * We don't check len. We want a specific error message later if | |
2596 | * user tries "x^...-(Rn)". This is a feature not a bug. | |
2597 | */ | |
2598 | if (!*err) | |
2599 | { | |
2600 | if (paren && sign < 1)/* !sign is adequate test */ | |
2601 | { | |
2602 | if (*q == '-') | |
2603 | { | |
2604 | sign = -1; | |
2605 | q--; | |
2606 | } | |
2607 | } | |
2608 | /* | |
2609 | * We have back-tracked over most | |
2610 | * of the crud at the end of an operand. | |
2611 | * Unless err, we know: sign, paren. If paren, we know reg. | |
2612 | * The last case is of an expression "Rn". | |
2613 | * This is worth hunting for if !err, !paren. | |
2614 | * We wouldn't be here if err. | |
2615 | * We remember to save q, in case we didn't want "Rn" anyway. | |
2616 | */ | |
2617 | if (!paren) | |
2618 | { | |
2619 | if (*q == ' ' && q >= p) /* Expect all whitespace reduced to ' '. */ | |
2620 | q--; | |
2621 | /* reverse over whitespace, but don't */ | |
2622 | /* run back over *p */ | |
2623 | if (q > p && q < p + 3) /* room for Rn or Rnn exactly? */ | |
2624 | reg = vax_reg_parse (p[0], p[1], q < p + 2 ? 0 : p[2]); | |
2625 | else | |
2626 | reg = -1; /* always comes here if no register at all */ | |
2627 | /* | |
2628 | * Here with a definitive reg value. | |
2629 | */ | |
2630 | if (reg >= 0) | |
2631 | { | |
2632 | oldq = q; | |
2633 | q = p - 1; | |
2634 | } | |
2635 | } | |
2636 | } | |
2637 | } | |
2638 | /* | |
2639 | * have reg. -1:absent; else 0:15 | |
2640 | */ | |
2641 | ||
2642 | /* | |
2643 | * We have: err, at, len, hash, ndx, sign, paren, reg. | |
2644 | * Also, any remaining expression is from *p through *q inclusive. | |
2645 | * Should there be no expression, q==p-1. So expression length = q-p+1. | |
2646 | * This completes the first part: parsing the operand text. | |
2647 | */ | |
2648 | \f | |
2649 | /* | |
2650 | * We now want to boil the data down, checking consistency on the way. | |
2651 | * We want: len, mode, reg, ndx, err, p, q, wrn, bug. | |
2652 | * We will deliver a 4-bit reg, and a 4-bit mode. | |
2653 | */ | |
2654 | ||
2655 | /* | |
2656 | * Case of branch operand. Different. No L^B^W^I^S^ allowed for instance. | |
2657 | * | |
2658 | * in: at ? | |
2659 | * len ? | |
2660 | * hash ? | |
2661 | * p:q ? | |
2662 | * sign ? | |
2663 | * paren ? | |
2664 | * reg ? | |
2665 | * ndx ? | |
2666 | * | |
2667 | * out: mode 0 | |
2668 | * reg -1 | |
2669 | * len ' ' | |
2670 | * p:q whatever was input | |
2671 | * ndx -1 | |
2672 | * err " " or error message, and other outputs trashed | |
2673 | */ | |
2674 | /* branch operands have restricted forms */ | |
2675 | if (!*err && access == 'b') | |
2676 | { | |
2677 | if (at || hash || sign || paren || ndx >= 0 || reg >= 0 || len != ' ') | |
2678 | err = "invalid branch operand"; | |
2679 | else | |
2680 | err = " "; | |
2681 | } | |
2682 | \f | |
2683 | /* Since nobody seems to use it: comment this 'feature'(?) out for now. */ | |
2684 | #ifdef NEVER | |
2685 | /* | |
2686 | * Case of stand-alone operand. e.g. ".long foo" | |
2687 | * | |
2688 | * in: at ? | |
2689 | * len ? | |
2690 | * hash ? | |
2691 | * p:q ? | |
2692 | * sign ? | |
2693 | * paren ? | |
2694 | * reg ? | |
2695 | * ndx ? | |
2696 | * | |
2697 | * out: mode 0 | |
2698 | * reg -1 | |
2699 | * len ' ' | |
2700 | * p:q whatever was input | |
2701 | * ndx -1 | |
2702 | * err " " or error message, and other outputs trashed | |
2703 | */ | |
2704 | if (!*err) | |
2705 | { | |
2706 | if (access == ' ') | |
2707 | { /* addresses have restricted forms */ | |
2708 | if (at) | |
2709 | err = "address prohibits @"; | |
2710 | else | |
2711 | { | |
2712 | if (hash) | |
2713 | err = "address prohibits #"; | |
2714 | else | |
2715 | { | |
2716 | if (sign) | |
2717 | { | |
2718 | if (sign < 0) | |
2719 | err = "address prohibits -()"; | |
2720 | else | |
2721 | err = "address prohibits ()+"; | |
2722 | } | |
2723 | else | |
2724 | { | |
2725 | if (paren) | |
2726 | err = "address prohibits ()"; | |
2727 | else | |
2728 | { | |
2729 | if (ndx >= 0) | |
2730 | err = "address prohibits []"; | |
2731 | else | |
2732 | { | |
2733 | if (reg >= 0) | |
2734 | err = "address prohibits register"; | |
2735 | else | |
2736 | { | |
2737 | if (len != ' ') | |
2738 | err = "address prohibits displacement length specifier"; | |
2739 | else | |
2740 | { | |
2741 | err = " "; /* succeed */ | |
2742 | mode = 0; | |
2743 | } | |
2744 | } | |
2745 | } | |
2746 | } | |
2747 | } | |
2748 | } | |
2749 | } | |
2750 | } | |
2751 | } | |
2752 | #endif /*#Ifdef NEVER*/ | |
2753 | \f | |
2754 | /* | |
2755 | * Case of S^#. | |
2756 | * | |
2757 | * in: at FALSE | |
2758 | * len 's' definition | |
2759 | * hash TRUE demand | |
2760 | * p:q demand not empty | |
2761 | * sign 0 by paren==FALSE | |
2762 | * paren FALSE by "()" scan logic because "S^" seen | |
2763 | * reg -1 or nn by mistake | |
2764 | * ndx -1 | |
2765 | * | |
2766 | * out: mode 0 | |
2767 | * reg -1 | |
2768 | * len 's' | |
2769 | * exp | |
2770 | * ndx -1 | |
2771 | */ | |
2772 | if (!*err && len == 's') | |
2773 | { | |
2774 | if (!hash || paren || at || ndx >= 0) | |
2775 | err = "invalid operand of S^#"; | |
2776 | else | |
2777 | { | |
2778 | if (reg >= 0) | |
2779 | { | |
2780 | /* | |
2781 | * SHIT! we saw S^#Rnn ! put the Rnn back in | |
2782 | * expression. KLUDGE! Use oldq so we don't | |
2783 | * need to know exact length of reg name. | |
2784 | */ | |
2785 | q = oldq; | |
2786 | reg = 0; | |
2787 | } | |
2788 | /* | |
2789 | * We have all the expression we will ever get. | |
2790 | */ | |
2791 | if (p > q) | |
2792 | err = "S^# needs expression"; | |
2793 | else if (access == 'r') | |
2794 | { | |
2795 | err = " "; /* WIN! */ | |
2796 | mode = 0; | |
2797 | } | |
2798 | else | |
2799 | err = "S^# may only read-access"; | |
2800 | } | |
2801 | } | |
2802 | \f | |
2803 | /* | |
2804 | * Case of -(Rn), which is weird case. | |
2805 | * | |
2806 | * in: at FALSE | |
2807 | * len ' | |
2808 | * hash FALSE | |
2809 | * p:q q<p | |
2810 | * sign -1 by definition | |
2811 | * paren TRUE by definition | |
2812 | * reg present by definition | |
2813 | * ndx optional | |
2814 | * | |
2815 | * out: mode 7 | |
2816 | * reg present | |
2817 | * len ' ' | |
2818 | * exp "" enforce empty expression | |
2819 | * ndx optional warn if same as reg | |
2820 | */ | |
2821 | if (!*err && sign < 0) | |
2822 | { | |
2823 | if (len != ' ' || hash || at || p <= q) | |
2824 | err = "invalid operand of -()"; | |
2825 | else | |
2826 | { | |
2827 | err = " "; /* win */ | |
2828 | mode = 7; | |
2829 | if (reg == PC) | |
2830 | wrn = "-(PC) unpredictable"; | |
2831 | else if (reg == ndx) | |
2832 | wrn = "[]index same as -()register: unpredictable"; | |
2833 | } | |
2834 | } | |
2835 | \f | |
2836 | /* | |
2837 | * We convert "(Rn)" to "@Rn" for our convenience. | |
2838 | * (I hope this is convenient: has someone got a better way to parse this?) | |
2839 | * A side-effect of this is that "@Rn" is a valid operand. | |
2840 | */ | |
2841 | if (paren && !sign && !hash && !at && len == ' ' && p > q) | |
2842 | { | |
2843 | at = TRUE; | |
2844 | paren = FALSE; | |
2845 | } | |
2846 | ||
2847 | /* | |
2848 | * Case of (Rn)+, which is slightly different. | |
2849 | * | |
2850 | * in: at | |
2851 | * len ' ' | |
2852 | * hash FALSE | |
2853 | * p:q q<p | |
2854 | * sign +1 by definition | |
2855 | * paren TRUE by definition | |
2856 | * reg present by definition | |
2857 | * ndx optional | |
2858 | * | |
2859 | * out: mode 8+@ | |
2860 | * reg present | |
2861 | * len ' ' | |
2862 | * exp "" enforce empty expression | |
2863 | * ndx optional warn if same as reg | |
2864 | */ | |
2865 | if (!*err && sign > 0) | |
2866 | { | |
2867 | if (len != ' ' || hash || p <= q) | |
2868 | err = "invalid operand of ()+"; | |
2869 | else | |
2870 | { | |
2871 | err = " "; /* win */ | |
2872 | mode = 8 + (at ? 1 : 0); | |
2873 | if (reg == PC) | |
2874 | wrn = "(PC)+ unpredictable"; | |
2875 | else if (reg == ndx) | |
2876 | wrn = "[]index same as ()+register: unpredictable"; | |
2877 | } | |
2878 | } | |
2879 | \f | |
2880 | /* | |
2881 | * Case of #, without S^. | |
2882 | * | |
2883 | * in: at | |
2884 | * len ' ' or 'i' | |
2885 | * hash TRUE by definition | |
2886 | * p:q | |
2887 | * sign 0 | |
2888 | * paren FALSE | |
2889 | * reg absent | |
2890 | * ndx optional | |
2891 | * | |
2892 | * out: mode 8+@ | |
2893 | * reg PC | |
2894 | * len ' ' or 'i' | |
2895 | * exp | |
2896 | * ndx optional | |
2897 | */ | |
2898 | if (!*err && hash) | |
2899 | { | |
2900 | if (len != 'i' && len != ' ') | |
2901 | err = "# conflicts length"; | |
2902 | else if (paren) | |
2903 | err = "# bars register"; | |
2904 | else | |
2905 | { | |
2906 | if (reg >= 0) | |
2907 | { | |
2908 | /* | |
2909 | * SHIT! we saw #Rnn! Put the Rnn back into the expression. | |
2910 | * By using oldq, we don't need to know how long Rnn was. | |
2911 | * KLUDGE! | |
2912 | */ | |
2913 | q = oldq; | |
2914 | reg = -1; /* no register any more */ | |
2915 | } | |
2916 | err = " "; /* win */ | |
2917 | ||
2918 | /* JF a bugfix, I think! */ | |
2919 | if(at && access=='a') | |
2920 | vopP->vop_nbytes=4; | |
2921 | ||
2922 | mode = (at ? 9 : 8); | |
2923 | reg = PC; | |
2924 | if ((access == 'm' || access == 'w') && !at) | |
2925 | wrn = "writing or modifying # is unpredictable"; | |
2926 | } | |
2927 | } | |
2928 | /* | |
2929 | * If !*err, then sign == 0 | |
2930 | * hash == FALSE | |
2931 | */ | |
2932 | \f | |
2933 | /* | |
2934 | * Case of Rn. We seperate this one because it has a few special | |
2935 | * errors the remaining modes lack. | |
2936 | * | |
2937 | * in: at optional | |
2938 | * len ' ' | |
2939 | * hash FALSE by program logic | |
2940 | * p:q empty | |
2941 | * sign 0 by program logic | |
2942 | * paren FALSE by definition | |
2943 | * reg present by definition | |
2944 | * ndx optional | |
2945 | * | |
2946 | * out: mode 5+@ | |
2947 | * reg present | |
2948 | * len ' ' enforce no length | |
2949 | * exp "" enforce empty expression | |
2950 | * ndx optional warn if same as reg | |
2951 | */ | |
2952 | if (!*err && !paren && reg >= 0) | |
2953 | { | |
2954 | if (len != ' ') | |
2955 | err = "length not needed"; | |
2956 | else if (at) | |
2957 | { | |
2958 | err = " "; /* win */ | |
2959 | mode = 6; /* @Rn */ | |
2960 | } | |
2961 | else if (ndx >= 0) | |
2962 | err = "can't []index a register, because it has no address"; | |
2963 | else if (access == 'a') | |
2964 | err = "a register has no address"; | |
2965 | else | |
2966 | { | |
2967 | /* | |
2968 | * Idea here is to detect from length of datum | |
2969 | * and from register number if we will touch PC. | |
2970 | * Warn if we do. | |
2971 | * vop_nbytes is number of bytes in operand. | |
2972 | * Compute highest byte affected, compare to PC0. | |
2973 | */ | |
2974 | if ((vopP->vop_nbytes + reg * 4) > 60) | |
2975 | wrn = "PC part of operand unpredictable"; | |
2976 | err = " "; /* win */ | |
2977 | mode = 5; /* Rn */ | |
2978 | } | |
2979 | } | |
2980 | /* | |
2981 | * If !*err, sign == 0 | |
2982 | * hash == FALSE | |
2983 | * paren == TRUE OR reg==-1 | |
2984 | */ | |
2985 | \f | |
2986 | /* | |
2987 | * Rest of cases fit into one bunch. | |
2988 | * | |
2989 | * in: at optional | |
2990 | * len ' ' or 'b' or 'w' or 'l' | |
2991 | * hash FALSE by program logic | |
2992 | * p:q expected (empty is not an error) | |
2993 | * sign 0 by program logic | |
2994 | * paren optional | |
2995 | * reg optional | |
2996 | * ndx optional | |
2997 | * | |
2998 | * out: mode 10 + @ + len | |
2999 | * reg optional | |
3000 | * len ' ' or 'b' or 'w' or 'l' | |
3001 | * exp maybe empty | |
3002 | * ndx optional warn if same as reg | |
3003 | */ | |
3004 | if (!*err) | |
3005 | { | |
3006 | err = " "; /* win (always) */ | |
3007 | mode = 10 + (at ? 1 : 0); | |
3008 | switch (len) | |
3009 | { | |
3010 | case 'l': | |
3011 | mode += 2; | |
3012 | case 'w': | |
3013 | mode += 2; | |
3014 | case ' ': /* assumed B^ until our caller changes it */ | |
3015 | case 'b': | |
3016 | break; | |
3017 | } | |
3018 | } | |
3019 | ||
3020 | /* | |
3021 | * here with completely specified mode | |
3022 | * len | |
3023 | * reg | |
3024 | * expression p,q | |
3025 | * ndx | |
3026 | */ | |
3027 | ||
3028 | if (*err == ' ') | |
3029 | err = ""; /* " " is no longer an error */ | |
3030 | ||
3031 | vopP->vop_mode = mode; | |
3032 | vopP->vop_reg = reg; | |
3033 | vopP->vop_short = len; | |
3034 | vopP->vop_expr_begin = p; | |
3035 | vopP->vop_expr_end = q; | |
3036 | vopP->vop_ndx = ndx; | |
3037 | vopP->vop_error = err; | |
3038 | vopP->vop_warn = wrn; | |
3039 | return (bug); | |
3040 | ||
3041 | } /* vip_op() */ | |
3042 | \f | |
3043 | /* | |
3044 | ||
3045 | Summary of vip_op outputs. | |
3046 | ||
3047 | mode reg len ndx | |
3048 | (Rn) => @Rn | |
3049 | {@}Rn 5+@ n ' ' optional | |
3050 | branch operand 0 -1 ' ' -1 | |
3051 | S^#foo 0 -1 's' -1 | |
3052 | -(Rn) 7 n ' ' optional | |
3053 | {@}(Rn)+ 8+@ n ' ' optional | |
3054 | {@}#foo, no S^ 8+@ PC " i" optional | |
3055 | {@}{q^}{(Rn)} 10+@+q option " bwl" optional | |
3056 | ||
3057 | */ | |
3058 | \f | |
3059 | #ifdef TEST /* #Define to use this testbed. */ | |
3060 | ||
3061 | /* | |
3062 | * Follows a test program for this function. | |
3063 | * We declare arrays non-local in case some of our tiny-minded machines | |
3064 | * default to small stacks. Also, helps with some debuggers. | |
3065 | */ | |
3066 | ||
3067 | #include <stdio.h> | |
3068 | ||
3069 | char answer[100]; /* human types into here */ | |
3070 | char *p; /* */ | |
3071 | char *myerr; | |
3072 | char *mywrn; | |
3073 | char *mybug; | |
3074 | char myaccess; | |
3075 | char mywidth; | |
3076 | char mymode; | |
3077 | char myreg; | |
3078 | char mylen; | |
3079 | char *myleft; | |
3080 | char *myright; | |
3081 | char myndx; | |
3082 | int my_operand_length; | |
3083 | char my_immediate[200]; | |
3084 | char my_indirect[200]; | |
3085 | char my_displen[200]; | |
3086 | ||
3087 | main () | |
3088 | { | |
3089 | char *vip_op (); /* make cc happy */ | |
3090 | ||
3091 | printf ("enter immediate symbols eg enter # "); | |
3092 | gets (my_immediate); | |
3093 | printf ("enter indirect symbols eg enter @ "); | |
3094 | gets (my_indirect); | |
3095 | printf ("enter displen symbols eg enter ^ "); | |
3096 | gets (my_displen); | |
3097 | vip_op_defaults (my_immediate, my_indirect, my_displen); | |
3098 | for (;;) | |
3099 | { | |
3100 | printf ("access,width (eg 'ab' or 'wh') [empty line to quit] : "); | |
3101 | fflush (stdout); | |
3102 | gets (answer); | |
3103 | if (!answer[0]) | |
3104 | exit (0); | |
3105 | myaccess = answer[0]; | |
3106 | mywidth = answer[1]; | |
3107 | switch (mywidth) | |
3108 | { | |
3109 | case 'b': | |
3110 | my_operand_length = 1; | |
3111 | break; | |
3112 | case 'd': | |
3113 | my_operand_length = 8; | |
3114 | break; | |
3115 | case 'f': | |
3116 | my_operand_length = 4; | |
3117 | break; | |
3118 | case 'g': | |
3119 | my_operand_length = 16; | |
3120 | break; | |
3121 | case 'h': | |
3122 | my_operand_length = 32; | |
3123 | break; | |
3124 | case 'l': | |
3125 | my_operand_length = 4; | |
3126 | break; | |
3127 | case 'o': | |
3128 | my_operand_length = 16; | |
3129 | break; | |
3130 | case 'q': | |
3131 | my_operand_length = 8; | |
3132 | break; | |
3133 | case 'w': | |
3134 | my_operand_length = 2; | |
3135 | break; | |
3136 | case '!': | |
3137 | case '?': | |
3138 | case '-': | |
3139 | my_operand_length = 0; | |
3140 | break; | |
3141 | ||
3142 | default: | |
3143 | my_operand_length = 2; | |
3144 | printf ("I dn't understand access width %c\n", mywidth); | |
3145 | break; | |
3146 | } | |
3147 | printf ("VAX assembler instruction operand: "); | |
3148 | fflush (stdout); | |
3149 | gets (answer); | |
3150 | mybug = vip_op (answer, myaccess, mywidth, my_operand_length, | |
3151 | &mymode, &myreg, &mylen, &myleft, &myright, &myndx, | |
3152 | &myerr, &mywrn); | |
3153 | if (*myerr) | |
3154 | { | |
3155 | printf ("error: \"%s\"\n", myerr); | |
3156 | if (*mybug) | |
3157 | printf (" bug: \"%s\"\n", mybug); | |
3158 | } | |
3159 | else | |
3160 | { | |
3161 | if (*mywrn) | |
3162 | printf ("warning: \"%s\"\n", mywrn); | |
3163 | mumble ("mode", mymode); | |
3164 | mumble ("register", myreg); | |
3165 | mumble ("index", myndx); | |
3166 | printf ("width:'%c' ", mylen); | |
3167 | printf ("expression: \""); | |
3168 | while (myleft <= myright) | |
3169 | putchar (*myleft++); | |
3170 | printf ("\"\n"); | |
3171 | } | |
3172 | } | |
3173 | } | |
3174 | ||
3175 | mumble (text, value) | |
3176 | char *text; | |
3177 | int value; | |
3178 | { | |
3179 | printf ("%s:", text); | |
3180 | if (value >= 0) | |
3181 | printf ("%xx", value); | |
3182 | else | |
3183 | printf ("ABSENT"); | |
3184 | printf (" "); | |
3185 | } | |
3186 | ||
3187 | #endif /* ifdef TEST */ | |
3188 | ||
3189 | /* end: vip_op.c */ | |
3190 | ||
3191 | const int md_short_jump_size = 3; | |
3192 | const int md_long_jump_size = 6; | |
3193 | const int md_reloc_size = 8; /* Size of relocation record */ | |
3194 | ||
3195 | void | |
3196 | md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol) | |
3197 | char *ptr; | |
3198 | long from_addr, to_addr; | |
3199 | fragS *frag; | |
3200 | symbolS *to_symbol; | |
3201 | { | |
3202 | long offset; | |
3203 | ||
3204 | offset = to_addr - (from_addr + 1); | |
3205 | *ptr++ = 0x31; | |
3206 | md_number_to_chars (ptr, offset, 2); | |
3207 | } | |
3208 | ||
3209 | void | |
3210 | md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol) | |
3211 | char *ptr; | |
3212 | long from_addr, to_addr; | |
3213 | fragS *frag; | |
3214 | symbolS *to_symbol; | |
3215 | { | |
3216 | long offset; | |
3217 | ||
3218 | offset = to_addr - to_symbol->sy_value; | |
3219 | *ptr++ = 0x17; | |
3220 | *ptr++ = 0x9F; | |
3221 | md_number_to_chars (ptr, offset, 4); | |
3222 | fix_new (frag, ptr - frag->fr_literal, 4, to_symbol, (symbolS *) 0, (long) 0, 0); | |
3223 | } | |
3224 | ||
3225 | int | |
3226 | md_parse_option (argP, cntP, vecP) | |
3227 | char **argP; | |
3228 | int *cntP; | |
3229 | char ***vecP; | |
3230 | { | |
3231 | char *temp_name; /* name for -t or -d options */ | |
3232 | char opt; | |
3233 | ||
3234 | switch (**argP) | |
3235 | { | |
3236 | case 'J': | |
3237 | /* as_warn ("I can do better than -J!"); */ | |
3238 | break; | |
3239 | ||
3240 | case 'S': | |
3241 | as_warn ("SYMBOL TABLE not implemented"); | |
3242 | break; /* SYMBOL TABLE not implemented */ | |
3243 | ||
3244 | case 'T': | |
3245 | as_warn ("TOKEN TRACE not implemented"); | |
3246 | break; /* TOKEN TRACE not implemented */ | |
3247 | ||
3248 | case 'd': | |
3249 | case 't': | |
3250 | opt= **argP; | |
3251 | if (**argP) | |
3252 | { /* Rest of argument is filename. */ | |
3253 | temp_name = *argP; | |
3254 | while (**argP) | |
3255 | (*argP)++; | |
3256 | } | |
3257 | else if (*cntP) | |
3258 | { | |
3259 | while (**argP) | |
3260 | (*argP)++; | |
3261 | --(*cntP); | |
3262 | temp_name = *++(*vecP); | |
3263 | **vecP = NULL; /* Remember this is not a file-name. */ | |
3264 | } | |
3265 | else | |
3266 | { | |
3267 | as_warn ("I expected a filename after -%c.",opt); | |
3268 | temp_name = "{absent}"; | |
3269 | } | |
3270 | ||
3271 | if(opt=='d') | |
3272 | as_warn ("Displacement length %s ignored!", temp_name); | |
3273 | else | |
3274 | as_warn ("I don't need or use temp. file \"%s\".", temp_name); | |
3275 | break; | |
3276 | ||
3277 | case 'V': | |
3278 | as_warn ("I don't use an interpass file! -V ignored"); | |
3279 | break; | |
3280 | ||
3281 | #ifdef VMS | |
3282 | case '+': /* For g++ */ | |
3283 | break; | |
3284 | ||
3285 | case 'h': /* No hashing of mixed-case names */ | |
3286 | break; | |
3287 | ||
3288 | case 'H': /* Show new symbol after hash truncation */ | |
3289 | break; | |
3290 | #endif | |
3291 | ||
3292 | default: | |
3293 | return 0; | |
3294 | ||
3295 | } | |
3296 | return 1; | |
3297 | } | |
3298 | ||
3299 | /* We have no need to default values of symbols. */ | |
3300 | ||
3301 | /* ARGSUSED */ | |
3302 | symbolS * | |
3303 | md_undefined_symbol (name) | |
3304 | char *name; | |
3305 | { | |
3306 | return 0; | |
3307 | } | |
3308 | ||
3309 | /* Parse an operand that is machine-specific. | |
3310 | We just return without modifying the expression if we have nothing | |
3311 | to do. */ | |
3312 | ||
3313 | /* ARGSUSED */ | |
3314 | void | |
3315 | md_operand (expressionP) | |
3316 | expressionS *expressionP; | |
3317 | { | |
3318 | } | |
3319 | ||
3320 | /* Round up a section size to the appropriate boundary. */ | |
3321 | long | |
3322 | md_section_align (segment, size) | |
3323 | segT segment; | |
3324 | long size; | |
3325 | { | |
3326 | return size; /* Byte alignment is fine */ | |
3327 | } | |
3328 | ||
3329 | /* Exactly what point is a PC-relative offset relative TO? | |
3330 | On the vax, they're relative to the address of the offset, plus | |
3331 | its size. (??? Is this right? FIXME-SOON) */ | |
3332 | long | |
3333 | md_pcrel_from (fixP) | |
3334 | fixS *fixP; | |
3335 | { | |
3336 | return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address; | |
3337 | } |