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