Commit | Line | Data |
---|---|---|
f7e42eb4 | 1 | /* This file is tc-tahoe.c |
252b5132 | 2 | |
3882b010 | 3 | Copyright 1987, 1988, 1989, 1990, 1991, 1992, 1995, 2000, 2001 |
f7e42eb4 NC |
4 | Free Software Foundation, Inc. |
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. */ | |
252b5132 | 22 | #include "as.h" |
3882b010 | 23 | #include "safe-ctype.h" |
252b5132 RH |
24 | #include "obstack.h" |
25 | ||
f7e42eb4 | 26 | /* This bit glommed from tahoe-inst.h. */ |
252b5132 RH |
27 | |
28 | typedef unsigned char byte; | |
29 | typedef byte tahoe_opcodeT; | |
30 | ||
f7e42eb4 NC |
31 | /* This is part of tahoe-ins-parse.c & friends. |
32 | We want to parse a tahoe instruction text into a tree defined here. */ | |
252b5132 RH |
33 | |
34 | #define TIT_MAX_OPERANDS (4) /* maximum number of operands in one | |
35 | single tahoe instruction */ | |
36 | ||
37 | struct top /* tahoe instruction operand */ | |
f7e42eb4 NC |
38 | { |
39 | int top_ndx; /* -1, or index register. eg 7=[R7] */ | |
40 | int top_reg; /* -1, or register number. eg 7 = R7 or (R7) */ | |
41 | byte top_mode; /* Addressing mode byte. This byte, defines | |
a4d24084 | 42 | which of the 11 modes opcode is. */ |
252b5132 | 43 | |
f7e42eb4 | 44 | char top_access; /* Access type wanted for this opperand |
252b5132 | 45 | 'b'branch ' 'no-instruction 'amrvw' */ |
f7e42eb4 | 46 | char top_width; /* Operand width expected, one of "bwlq?-:!" */ |
252b5132 | 47 | |
f7e42eb4 | 48 | char * top_error; /* Say if operand is inappropriate */ |
252b5132 | 49 | |
f7e42eb4 | 50 | segT seg_of_operand; /* segment as returned by expression()*/ |
252b5132 | 51 | |
f7e42eb4 | 52 | expressionS exp_of_operand; /* The expression as parsed by expression()*/ |
252b5132 | 53 | |
f7e42eb4 | 54 | byte top_dispsize; /* Number of bytes in the displacement if we |
252b5132 | 55 | can figure it out */ |
f7e42eb4 | 56 | }; |
252b5132 RH |
57 | |
58 | /* The addressing modes for an operand. These numbers are the acutal values | |
a4d24084 | 59 | for certain modes, so be carefull if you screw with them. */ |
252b5132 RH |
60 | #define TAHOE_DIRECT_REG (0x50) |
61 | #define TAHOE_REG_DEFERRED (0x60) | |
62 | ||
63 | #define TAHOE_REG_DISP (0xE0) | |
64 | #define TAHOE_REG_DISP_DEFERRED (0xF0) | |
65 | ||
66 | #define TAHOE_IMMEDIATE (0x8F) | |
67 | #define TAHOE_IMMEDIATE_BYTE (0x88) | |
68 | #define TAHOE_IMMEDIATE_WORD (0x89) | |
69 | #define TAHOE_IMMEDIATE_LONGWORD (0x8F) | |
70 | #define TAHOE_ABSOLUTE_ADDR (0x9F) | |
71 | ||
72 | #define TAHOE_DISPLACED_RELATIVE (0xEF) | |
73 | #define TAHOE_DISP_REL_DEFERRED (0xFF) | |
74 | ||
75 | #define TAHOE_AUTO_DEC (0x7E) | |
76 | #define TAHOE_AUTO_INC (0x8E) | |
77 | #define TAHOE_AUTO_INC_DEFERRED (0x9E) | |
f7e42eb4 | 78 | /* INDEXED_REG is decided by the existance or lack of a [reg]. */ |
252b5132 RH |
79 | |
80 | /* These are encoded into top_width when top_access=='b' | |
f7e42eb4 | 81 | and it's a psuedo op. */ |
252b5132 RH |
82 | #define TAHOE_WIDTH_ALWAYS_JUMP '-' |
83 | #define TAHOE_WIDTH_CONDITIONAL_JUMP '?' | |
84 | #define TAHOE_WIDTH_BIG_REV_JUMP '!' | |
85 | #define TAHOE_WIDTH_BIG_NON_REV_JUMP ':' | |
86 | ||
87 | /* The hex code for certain tahoe commands and modes. | |
a4d24084 | 88 | This is just for readability. */ |
252b5132 RH |
89 | #define TAHOE_JMP (0x71) |
90 | #define TAHOE_PC_REL_LONG (0xEF) | |
91 | #define TAHOE_BRB (0x11) | |
92 | #define TAHOE_BRW (0x13) | |
93 | /* These, when 'ored' with, or added to, a register number, | |
a4d24084 | 94 | set up the number for the displacement mode. */ |
252b5132 RH |
95 | #define TAHOE_PC_OR_BYTE (0xA0) |
96 | #define TAHOE_PC_OR_WORD (0xC0) | |
97 | #define TAHOE_PC_OR_LONG (0xE0) | |
98 | ||
f7e42eb4 NC |
99 | struct tit /* Get it out of the sewer, it stands for |
100 | tahoe instruction tree (Geeze!). */ | |
252b5132 | 101 | { |
a4d24084 KH |
102 | tahoe_opcodeT tit_opcode; /* The opcode. */ |
103 | byte tit_operands; /* How many operands are here. */ | |
252b5132 RH |
104 | struct top tit_operand[TIT_MAX_OPERANDS]; /* Operands */ |
105 | char *tit_error; /* "" or fatal error text */ | |
106 | }; | |
107 | ||
108 | /* end: tahoe-inst.h */ | |
109 | ||
110 | /* tahoe.c - tahoe-specific - | |
111 | Not part of gas yet. | |
112 | */ | |
113 | ||
114 | #include "opcode/tahoe.h" | |
115 | ||
116 | /* This is the number to put at the beginning of the a.out file */ | |
117 | long omagic = OMAGIC; | |
118 | ||
119 | /* These chars start a comment anywhere in a source file (except inside | |
a4d24084 | 120 | another comment or a quoted string. */ |
252b5132 RH |
121 | const char comment_chars[] = "#;"; |
122 | ||
a4d24084 | 123 | /* These chars only start a comment at the beginning of a line. */ |
252b5132 RH |
124 | const char line_comment_chars[] = "#"; |
125 | ||
126 | /* Chars that can be used to separate mant from exp in floating point nums */ | |
127 | const char EXP_CHARS[] = "eE"; | |
128 | ||
129 | /* Chars that mean this number is a floating point constant | |
130 | as in 0f123.456 | |
131 | or 0d1.234E-12 (see exp chars above) | |
132 | Note: The Tahoe port doesn't support floating point constants. This is | |
a4d24084 | 133 | consistant with 'as' If it's needed, I can always add it later. */ |
252b5132 RH |
134 | const char FLT_CHARS[] = "df"; |
135 | ||
136 | /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be | |
137 | changed in read.c . Ideally it shouldn't have to know about it at all, | |
138 | but nothing is ideal around here. | |
139 | (The tahoe has plenty of room, so the change currently isn't needed.) | |
140 | */ | |
141 | ||
a4d24084 | 142 | static struct tit t; /* A tahoe instruction after decoding. */ |
252b5132 RH |
143 | |
144 | void float_cons (); | |
145 | /* A table of pseudo ops (sans .), the function called, and an integer op | |
a4d24084 | 146 | that the function is called with. */ |
252b5132 RH |
147 | |
148 | const pseudo_typeS md_pseudo_table[] = | |
149 | { | |
150 | {"dfloat", float_cons, 'd'}, | |
151 | {"ffloat", float_cons, 'f'}, | |
152 | {0} | |
153 | }; | |
154 | \f | |
155 | /* | |
156 | * For Tahoe, relative addresses of "just the right length" are pretty easy. | |
157 | * The branch displacement is always the last operand, even in | |
158 | * synthetic instructions. | |
159 | * For Tahoe, we encode the relax_substateTs (in e.g. fr_substate) as: | |
160 | * | |
161 | * 4 3 2 1 0 bit number | |
162 | * ---/ /--+-------+-------+-------+-------+-------+ | |
163 | * | what state ? | how long ? | | |
164 | * ---/ /--+-------+-------+-------+-------+-------+ | |
165 | * | |
166 | * The "how long" bits are 00=byte, 01=word, 10=long. | |
167 | * This is a Un*x convention. | |
168 | * Not all lengths are legit for a given value of (what state). | |
169 | * The four states are listed below. | |
170 | * The "how long" refers merely to the displacement length. | |
171 | * The address usually has some constant bytes in it as well. | |
172 | * | |
173 | ||
174 | States for Tahoe address relaxing. | |
175 | 1. TAHOE_WIDTH_ALWAYS_JUMP (-) | |
176 | Format: "b-" | |
177 | Tahoe opcodes are: (Hex) | |
178 | jr 11 | |
179 | jbr 11 | |
180 | Simple branch. | |
181 | Always, 1 byte opcode, then displacement/absolute. | |
182 | If word or longword, change opcode to brw or jmp. | |
183 | ||
252b5132 RH |
184 | 2. TAHOE_WIDTH_CONDITIONAL_JUMP (?) |
185 | J<cond> where <cond> is a simple flag test. | |
186 | Format: "b?" | |
187 | Tahoe opcodes are: (Hex) | |
188 | jneq/jnequ 21 | |
189 | jeql/jeqlu 31 | |
190 | jgtr 41 | |
191 | jleq 51 | |
192 | jgeq 81 | |
193 | jlss 91 | |
194 | jgtru a1 | |
195 | jlequ b1 | |
196 | jvc c1 | |
197 | jvs d1 | |
198 | jlssu/jcs e1 | |
199 | jgequ/jcc f1 | |
200 | Always, you complement 4th bit to reverse the condition. | |
201 | Always, 1-byte opcode, then 1-byte displacement. | |
202 | ||
203 | 3. TAHOE_WIDTH_BIG_REV_JUMP (!) | |
204 | Jbc/Jbs where cond tests a memory bit. | |
205 | Format: "rlvlb!" | |
206 | Tahoe opcodes are: (Hex) | |
207 | jbs 0e | |
208 | jbc 1e | |
209 | Always, you complement 4th bit to reverse the condition. | |
210 | Always, 1-byte opcde, longword, longword-address, 1-word-displacement | |
211 | ||
212 | 4. TAHOE_WIDTH_BIG_NON_REV_JUMP (:) | |
213 | JaoblXX/Jbssi | |
214 | Format: "rlmlb:" | |
215 | Tahoe opcodes are: (Hex) | |
216 | aojlss 2f | |
217 | jaoblss 2f | |
218 | aojleq 3f | |
219 | jaobleq 3f | |
220 | jbssi 5f | |
221 | Always, we cannot reverse the sense of the branch; we have a word | |
222 | displacement. | |
223 | ||
224 | We need to modify the opcode is for class 1, 2 and 3 instructions. | |
225 | After relax() we may complement the 4th bit of 2 or 3 to reverse sense of | |
226 | branch. | |
227 | ||
228 | We sometimes store context in the operand literal. This way we can figure out | |
229 | after relax() what the original addressing mode was. (Was is pc_rel, or | |
230 | pc_rel_disp? That sort of thing.) */ | |
231 | \f | |
232 | /* These displacements are relative to the START address of the | |
233 | displacement which is at the start of the displacement, not the end of | |
234 | the instruction. The hardware pc_rel is at the end of the instructions. | |
235 | That's why all the displacements have the length of the displacement added | |
236 | to them. (WF + length(word)) | |
237 | ||
238 | The first letter is Byte, Word. | |
a4d24084 | 239 | 2nd letter is Forward, Backward. */ |
252b5132 RH |
240 | #define BF (1+ 127) |
241 | #define BB (1+-128) | |
242 | #define WF (2+ 32767) | |
243 | #define WB (2+-32768) | |
244 | /* Dont need LF, LB because they always reach. [They are coded as 0.] */ | |
245 | ||
246 | #define C(a,b) ENCODE_RELAX(a,b) | |
a4d24084 | 247 | /* This macro has no side-effects. */ |
252b5132 | 248 | #define ENCODE_RELAX(what,length) (((what) << 2) + (length)) |
606ab118 AM |
249 | #define RELAX_STATE(s) ((s) >> 2) |
250 | #define RELAX_LENGTH(s) ((s) & 3) | |
252b5132 RH |
251 | |
252 | #define STATE_ALWAYS_BRANCH (1) | |
253 | #define STATE_CONDITIONAL_BRANCH (2) | |
254 | #define STATE_BIG_REV_BRANCH (3) | |
255 | #define STATE_BIG_NON_REV_BRANCH (4) | |
256 | #define STATE_PC_RELATIVE (5) | |
257 | ||
258 | #define STATE_BYTE (0) | |
259 | #define STATE_WORD (1) | |
260 | #define STATE_LONG (2) | |
261 | #define STATE_UNDF (3) /* Symbol undefined in pass1 */ | |
262 | ||
263 | /* This is the table used by gas to figure out relaxing modes. The fields are | |
264 | forward_branch reach, backward_branch reach, number of bytes it would take, | |
a4d24084 | 265 | where the next biggest branch is. */ |
252b5132 RH |
266 | const relax_typeS md_relax_table[] = |
267 | { | |
268 | { | |
269 | 1, 1, 0, 0 | |
270 | }, /* error sentinel 0,0 */ | |
271 | { | |
272 | 1, 1, 0, 0 | |
273 | }, /* unused 0,1 */ | |
274 | { | |
275 | 1, 1, 0, 0 | |
276 | }, /* unused 0,2 */ | |
277 | { | |
278 | 1, 1, 0, 0 | |
279 | }, /* unused 0,3 */ | |
280 | /* Unconditional branch cases "jrb" | |
281 | The relax part is the actual displacement */ | |
282 | { | |
283 | BF, BB, 1, C (1, 1) | |
284 | }, /* brb B`foo 1,0 */ | |
285 | { | |
286 | WF, WB, 2, C (1, 2) | |
287 | }, /* brw W`foo 1,1 */ | |
288 | { | |
289 | 0, 0, 5, 0 | |
290 | }, /* Jmp L`foo 1,2 */ | |
291 | { | |
292 | 1, 1, 0, 0 | |
293 | }, /* unused 1,3 */ | |
294 | /* Reversible Conditional Branch. If the branch won't reach, reverse | |
295 | it, and jump over a brw or a jmp that will reach. The relax part is the | |
a4d24084 | 296 | actual address. */ |
252b5132 RH |
297 | { |
298 | BF, BB, 1, C (2, 1) | |
299 | }, /* b<cond> B`foo 2,0 */ | |
300 | { | |
301 | WF + 2, WB + 2, 4, C (2, 2) | |
302 | }, /* brev over, brw W`foo, over: 2,1 */ | |
303 | { | |
304 | 0, 0, 7, 0 | |
305 | }, /* brev over, jmp L`foo, over: 2,2 */ | |
306 | { | |
307 | 1, 1, 0, 0 | |
308 | }, /* unused 2,3 */ | |
309 | /* Another type of reversable branch. But this only has a word | |
a4d24084 | 310 | displacement. */ |
252b5132 RH |
311 | { |
312 | 1, 1, 0, 0 | |
313 | }, /* unused 3,0 */ | |
314 | { | |
315 | WF, WB, 2, C (3, 2) | |
316 | }, /* jbX W`foo 3,1 */ | |
317 | { | |
318 | 0, 0, 8, 0 | |
319 | }, /* jrevX over, jmp L`foo, over: 3,2 */ | |
320 | { | |
321 | 1, 1, 0, 0 | |
322 | }, /* unused 3,3 */ | |
323 | /* These are the non reversable branches, all of which have a word | |
324 | displacement. If I can't reach, branch over a byte branch, to a | |
325 | jump that will reach. The jumped branch jumps over the reaching | |
326 | branch, to continue with the flow of the program. It's like playing | |
a4d24084 | 327 | leap frog. */ |
252b5132 RH |
328 | { |
329 | 1, 1, 0, 0 | |
330 | }, /* unused 4,0 */ | |
331 | { | |
332 | WF, WB, 2, C (4, 2) | |
333 | }, /* aobl_ W`foo 4,1 */ | |
334 | { | |
335 | 0, 0, 10, 0 | |
336 | }, /*aobl_ W`hop,br over,hop: jmp L^foo,over 4,2*/ | |
337 | { | |
338 | 1, 1, 0, 0 | |
339 | }, /* unused 4,3 */ | |
340 | /* Normal displacement mode, no jumping or anything like that. | |
341 | The relax points to one byte before the address, thats why all | |
a4d24084 | 342 | the numbers are up by one. */ |
252b5132 RH |
343 | { |
344 | BF + 1, BB + 1, 2, C (5, 1) | |
345 | }, /* B^"foo" 5,0 */ | |
346 | { | |
347 | WF + 1, WB + 1, 3, C (5, 2) | |
348 | }, /* W^"foo" 5,1 */ | |
349 | { | |
350 | 0, 0, 5, 0 | |
351 | }, /* L^"foo" 5,2 */ | |
352 | { | |
353 | 1, 1, 0, 0 | |
354 | }, /* unused 5,3 */ | |
355 | }; | |
356 | ||
357 | #undef C | |
358 | #undef BF | |
359 | #undef BB | |
360 | #undef WF | |
361 | #undef WB | |
362 | /* End relax stuff */ | |
363 | \f | |
364 | /* Handle of the OPCODE hash table. NULL means any use before | |
365 | md_begin() will crash. */ | |
366 | static struct hash_control *op_hash; | |
367 | ||
a4d24084 | 368 | /* Init function. Build the hash table. */ |
252b5132 RH |
369 | void |
370 | md_begin () | |
371 | { | |
372 | struct tot *tP; | |
373 | char *errorval = 0; | |
a4d24084 | 374 | int synthetic_too = 1; /* If 0, just use real opcodes. */ |
252b5132 RH |
375 | |
376 | op_hash = hash_new (); | |
377 | ||
378 | for (tP = totstrs; *tP->name && !errorval; tP++) | |
379 | errorval = hash_insert (op_hash, tP->name, &tP->detail); | |
380 | ||
381 | if (synthetic_too) | |
382 | for (tP = synthetic_totstrs; *tP->name && !errorval; tP++) | |
383 | errorval = hash_insert (op_hash, tP->name, &tP->detail); | |
384 | ||
385 | if (errorval) | |
386 | as_fatal (errorval); | |
387 | } | |
388 | \f | |
389 | CONST char *md_shortopts = "ad:STt:V"; | |
390 | struct option md_longopts[] = { | |
391 | {NULL, no_argument, NULL, 0} | |
392 | }; | |
bc805888 | 393 | size_t md_longopts_size = sizeof (md_longopts); |
252b5132 RH |
394 | |
395 | int | |
396 | md_parse_option (c, arg) | |
397 | int c; | |
398 | char *arg; | |
399 | { | |
400 | switch (c) | |
401 | { | |
402 | case 'a': | |
403 | as_warn (_("The -a option doesn't exist. (Despite what the man page says!")); | |
404 | break; | |
405 | ||
406 | case 'd': | |
407 | as_warn (_("Displacement length %s ignored!"), arg); | |
408 | break; | |
409 | ||
410 | case 'S': | |
411 | as_warn (_("SYMBOL TABLE not implemented")); | |
412 | break; | |
413 | ||
414 | case 'T': | |
415 | as_warn (_("TOKEN TRACE not implemented")); | |
416 | break; | |
417 | ||
418 | case 't': | |
419 | as_warn (_("I don't need or use temp. file \"%s\"."), arg); | |
420 | break; | |
421 | ||
422 | case 'V': | |
423 | as_warn (_("I don't use an interpass file! -V ignored")); | |
424 | break; | |
425 | ||
426 | default: | |
427 | return 0; | |
428 | } | |
429 | ||
430 | return 1; | |
431 | } | |
432 | ||
433 | void | |
434 | md_show_usage (stream) | |
435 | FILE *stream; | |
436 | { | |
bc805888 | 437 | fprintf (stream, _("\ |
252b5132 RH |
438 | Tahoe options:\n\ |
439 | -a ignored\n\ | |
440 | -d LENGTH ignored\n\ | |
441 | -J ignored\n\ | |
442 | -S ignored\n\ | |
443 | -t FILE ignored\n\ | |
444 | -T ignored\n\ | |
445 | -V ignored\n")); | |
446 | } | |
447 | \f | |
448 | /* The functions in this section take numbers in the machine format, and | |
449 | munges them into Tahoe byte order. | |
a4d24084 KH |
450 | They exist primarily for cross assembly purpose. */ |
451 | void /* Knows about order of bytes in address. */ | |
252b5132 | 452 | md_number_to_chars (con, value, nbytes) |
a4d24084 KH |
453 | char con[]; /* Return 'nbytes' of chars here. */ |
454 | valueT value; /* The value of the bits. */ | |
455 | int nbytes; /* Number of bytes in the output. */ | |
252b5132 RH |
456 | { |
457 | number_to_chars_bigendian (con, value, nbytes); | |
458 | } | |
459 | ||
460 | #ifdef comment | |
a4d24084 | 461 | void /* Knows about order of bytes in address. */ |
252b5132 | 462 | md_number_to_imm (con, value, nbytes) |
a4d24084 KH |
463 | char con[]; /* Return 'nbytes' of chars here. */ |
464 | long int value; /* The value of the bits. */ | |
465 | int nbytes; /* Number of bytes in the output. */ | |
252b5132 RH |
466 | { |
467 | md_number_to_chars (con, value, nbytes); | |
468 | } | |
469 | ||
470 | #endif /* comment */ | |
471 | ||
472 | void | |
473 | tc_apply_fix (fixP, val) | |
474 | fixS *fixP; | |
475 | long val; | |
476 | { | |
477 | /* should never be called */ | |
478 | know (0); | |
479 | } | |
480 | ||
a4d24084 | 481 | void /* Knows about order of bytes in address. */ |
252b5132 | 482 | md_number_to_disp (con, value, nbytes) |
a4d24084 KH |
483 | char con[]; /* Return 'nbytes' of chars here. */ |
484 | long int value; /* The value of the bits. */ | |
485 | int nbytes; /* Number of bytes in the output. */ | |
252b5132 RH |
486 | { |
487 | md_number_to_chars (con, value, nbytes); | |
488 | } | |
489 | ||
a4d24084 | 490 | void /* Knows about order of bytes in address. */ |
252b5132 | 491 | md_number_to_field (con, value, nbytes) |
a4d24084 KH |
492 | char con[]; /* Return 'nbytes' of chars here. */ |
493 | long int value; /* The value of the bits. */ | |
494 | int nbytes; /* Number of bytes in the output. */ | |
252b5132 RH |
495 | { |
496 | md_number_to_chars (con, value, nbytes); | |
497 | } | |
498 | ||
499 | /* Put the bits in an order that a tahoe will understand, despite the ordering | |
500 | of the native machine. | |
501 | On Tahoe: first 4 bytes are normal unsigned big endian long, | |
502 | next three bytes are symbolnum, in kind of 3 byte big endian (least sig. byte last). | |
503 | The last byte is broken up with bit 7 as pcrel, | |
504 | bits 6 & 5 as length, | |
a4d24084 | 505 | bit 4 as extern and the last nibble as 'undefined'. */ |
252b5132 RH |
506 | |
507 | #if comment | |
508 | void | |
509 | md_ri_to_chars (ri_p, ri) | |
510 | struct relocation_info *ri_p, ri; | |
511 | { | |
512 | byte the_bytes[sizeof (struct relocation_info)]; | |
513 | /* The reason I can't just encode these directly into ri_p is that | |
a4d24084 | 514 | ri_p may point to ri. */ |
252b5132 RH |
515 | |
516 | /* This is easy */ | |
517 | md_number_to_chars (the_bytes, ri.r_address, sizeof (ri.r_address)); | |
518 | ||
519 | /* now the fun stuff */ | |
520 | the_bytes[4] = (ri.r_symbolnum >> 16) & 0x0ff; | |
521 | the_bytes[5] = (ri.r_symbolnum >> 8) & 0x0ff; | |
522 | the_bytes[6] = ri.r_symbolnum & 0x0ff; | |
523 | the_bytes[7] = (((ri.r_extern << 4) & 0x10) | ((ri.r_length << 5) & 0x60) | | |
524 | ((ri.r_pcrel << 7) & 0x80)) & 0xf0; | |
525 | ||
526 | bcopy (the_bytes, (char *) ri_p, sizeof (struct relocation_info)); | |
527 | } | |
528 | ||
529 | #endif /* comment */ | |
530 | ||
531 | /* Put the bits in an order that a tahoe will understand, despite the ordering | |
532 | of the native machine. | |
533 | On Tahoe: first 4 bytes are normal unsigned big endian long, | |
534 | next three bytes are symbolnum, in kind of 3 byte big endian (least sig. byte last). | |
535 | The last byte is broken up with bit 7 as pcrel, | |
536 | bits 6 & 5 as length, | |
a4d24084 | 537 | bit 4 as extern and the last nibble as 'undefined'. */ |
252b5132 | 538 | |
a4d24084 | 539 | void |
252b5132 RH |
540 | tc_aout_fix_to_chars (where, fixP, segment_address_in_file) |
541 | char *where; | |
542 | fixS *fixP; | |
543 | relax_addressT segment_address_in_file; | |
544 | { | |
545 | long r_symbolnum; | |
546 | ||
547 | know (fixP->fx_addsy != NULL); | |
548 | ||
549 | md_number_to_chars (where, | |
550 | fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file, | |
551 | 4); | |
552 | ||
553 | r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy) | |
554 | ? S_GET_TYPE (fixP->fx_addsy) | |
555 | : fixP->fx_addsy->sy_number); | |
556 | ||
557 | where[4] = (r_symbolnum >> 16) & 0x0ff; | |
558 | where[5] = (r_symbolnum >> 8) & 0x0ff; | |
559 | where[6] = r_symbolnum & 0x0ff; | |
560 | where[7] = (((is_pcrel (fixP) << 7) & 0x80) | |
561 | | ((((fixP->fx_type == FX_8 || fixP->fx_type == FX_PCREL8 | |
562 | ? 0 | |
563 | : (fixP->fx_type == FX_16 || fixP->fx_type == FX_PCREL16 | |
564 | ? 1 | |
565 | : (fixP->fx_type == FX_32 || fixP->fx_type == FX_PCREL32 | |
566 | ? 2 | |
567 | : 42)))) << 5) & 0x60) | |
568 | | ((!S_IS_DEFINED (fixP->fx_addsy) << 4) & 0x10)); | |
569 | } | |
570 | ||
571 | /* Relocate byte stuff */ | |
572 | \f | |
a4d24084 | 573 | /* This is for broken word. */ |
252b5132 RH |
574 | const int md_short_jump_size = 3; |
575 | ||
576 | void | |
577 | md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol) | |
578 | char *ptr; | |
579 | addressT from_addr, to_addr; | |
580 | fragS *frag; | |
581 | symbolS *to_symbol; | |
582 | { | |
583 | valueT offset; | |
584 | ||
585 | offset = to_addr - (from_addr + 1); | |
586 | *ptr++ = TAHOE_BRW; | |
587 | md_number_to_chars (ptr, offset, 2); | |
588 | } | |
589 | ||
590 | const int md_long_jump_size = 6; | |
591 | const int md_reloc_size = 8; /* Size of relocation record */ | |
592 | ||
593 | void | |
594 | md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol) | |
595 | char *ptr; | |
596 | addressT from_addr, to_addr; | |
597 | fragS *frag; | |
598 | symbolS *to_symbol; | |
599 | { | |
600 | valueT offset; | |
601 | ||
602 | offset = to_addr - (from_addr + 4); | |
603 | *ptr++ = TAHOE_JMP; | |
604 | *ptr++ = TAHOE_PC_REL_LONG; | |
605 | md_number_to_chars (ptr, offset, 4); | |
606 | } | |
607 | \f | |
606ab118 AM |
608 | /* md_estimate_size_before_relax(), called just before relax(). |
609 | Any symbol that is now undefined will not become defined. | |
610 | Return the correct fr_subtype in the frag and the growth beyond | |
611 | fr_fix. */ | |
252b5132 RH |
612 | int |
613 | md_estimate_size_before_relax (fragP, segment_type) | |
614 | register fragS *fragP; | |
a4d24084 | 615 | segT segment_type; /* N_DATA or N_TEXT. */ |
252b5132 | 616 | { |
606ab118 | 617 | if (RELAX_LENGTH (fragP->fr_subtype) == STATE_UNDF) |
252b5132 | 618 | { |
606ab118 | 619 | if (S_GET_SEGMENT (fragP->fr_symbol) != segment) |
252b5132 | 620 | { |
606ab118 AM |
621 | /* Non-relaxable cases. */ |
622 | char *p; | |
623 | int old_fr_fix; | |
252b5132 | 624 | |
606ab118 | 625 | old_fr_fix = fragP->fr_fix; |
252b5132 | 626 | p = fragP->fr_literal + old_fr_fix; |
606ab118 AM |
627 | switch (RELAX_STATE (fragP->fr_subtype)) |
628 | { | |
629 | case STATE_PC_RELATIVE: | |
630 | *p |= TAHOE_PC_OR_LONG; | |
631 | /* We now know how big it will be, one long word. */ | |
632 | fragP->fr_fix += 1 + 4; | |
633 | fix_new (fragP, old_fr_fix + 1, fragP->fr_symbol, | |
634 | fragP->fr_offset, FX_PCREL32, NULL); | |
635 | break; | |
636 | ||
637 | case STATE_CONDITIONAL_BRANCH: | |
638 | *fragP->fr_opcode ^= 0x10; /* Reverse sense of branch. */ | |
639 | *p++ = 6; | |
640 | *p++ = TAHOE_JMP; | |
641 | *p++ = TAHOE_PC_REL_LONG; | |
642 | fragP->fr_fix += 1 + 1 + 1 + 4; | |
643 | fix_new (fragP, old_fr_fix + 3, fragP->fr_symbol, | |
644 | fragP->fr_offset, FX_PCREL32, NULL); | |
645 | break; | |
646 | ||
647 | case STATE_BIG_REV_BRANCH: | |
648 | *fragP->fr_opcode ^= 0x10; /* Reverse sense of branch. */ | |
649 | *p++ = 0; | |
650 | *p++ = 6; | |
651 | *p++ = TAHOE_JMP; | |
652 | *p++ = TAHOE_PC_REL_LONG; | |
653 | fragP->fr_fix += 2 + 2 + 4; | |
654 | fix_new (fragP, old_fr_fix + 4, fragP->fr_symbol, | |
655 | fragP->fr_offset, FX_PCREL32, NULL); | |
656 | break; | |
657 | ||
658 | case STATE_BIG_NON_REV_BRANCH: | |
659 | *p++ = 2; | |
660 | *p++ = 0; | |
661 | *p++ = TAHOE_BRB; | |
662 | *p++ = 6; | |
663 | *p++ = TAHOE_JMP; | |
664 | *p++ = TAHOE_PC_REL_LONG; | |
665 | fragP->fr_fix += 2 + 2 + 2 + 4; | |
666 | fix_new (fragP, old_fr_fix + 6, fragP->fr_symbol, | |
667 | fragP->fr_offset, FX_PCREL32, NULL); | |
668 | break; | |
669 | ||
670 | case STATE_ALWAYS_BRANCH: | |
671 | *fragP->fr_opcode = TAHOE_JMP; | |
672 | *p++ = TAHOE_PC_REL_LONG; | |
673 | fragP->fr_fix += 1 + 4; | |
674 | fix_new (fragP, old_fr_fix + 1, fragP->fr_symbol, | |
675 | fragP->fr_offset, FX_PCREL32, NULL); | |
676 | break; | |
677 | ||
678 | default: | |
679 | abort (); | |
680 | } | |
252b5132 | 681 | frag_wane (fragP); |
252b5132 | 682 | |
606ab118 AM |
683 | /* Return the growth in the fixed part of the frag. */ |
684 | return fragP->fr_fix - old_fr_fix; | |
252b5132 | 685 | } |
252b5132 | 686 | |
606ab118 AM |
687 | /* Relaxable cases. Set up the initial guess for the variable |
688 | part of the frag. */ | |
689 | switch (RELAX_STATE (fragP->fr_subtype)) | |
252b5132 | 690 | { |
606ab118 AM |
691 | case STATE_PC_RELATIVE: |
692 | fragP->fr_subtype = ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE); | |
693 | break; | |
694 | case STATE_CONDITIONAL_BRANCH: | |
695 | fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE); | |
696 | break; | |
697 | case STATE_BIG_REV_BRANCH: | |
698 | fragP->fr_subtype = ENCODE_RELAX (STATE_BIG_REV_BRANCH, STATE_WORD); | |
699 | break; | |
700 | case STATE_BIG_NON_REV_BRANCH: | |
252b5132 | 701 | fragP->fr_subtype = ENCODE_RELAX (STATE_BIG_NON_REV_BRANCH, STATE_WORD); |
606ab118 AM |
702 | break; |
703 | case STATE_ALWAYS_BRANCH: | |
252b5132 | 704 | fragP->fr_subtype = ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE); |
606ab118 | 705 | break; |
252b5132 | 706 | } |
252b5132 | 707 | } |
606ab118 AM |
708 | |
709 | if (fragP->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0])) | |
710 | abort (); | |
711 | ||
712 | /* Return the size of the variable part of the frag. */ | |
713 | return md_relax_table[fragP->fr_subtype].rlx_length; | |
714 | } | |
252b5132 RH |
715 | \f |
716 | /* | |
717 | * md_convert_frag(); | |
718 | * | |
719 | * Called after relax() is finished. | |
720 | * In: Address of frag. | |
721 | * fr_type == rs_machine_dependent. | |
722 | * fr_subtype is what the address relaxed to. | |
723 | * | |
724 | * Out: Any fixSs and constants are set up. | |
725 | * Caller will turn frag into a ".space 0". | |
726 | */ | |
727 | void | |
728 | md_convert_frag (headers, seg, fragP) | |
729 | object_headers *headers; | |
730 | segT seg; | |
731 | register fragS *fragP; | |
732 | { | |
a4d24084 KH |
733 | register char *addressP; /* -> _var to change. */ |
734 | register char *opcodeP; /* -> opcode char(s) to change. */ | |
252b5132 | 735 | register short int extension = 0; /* Size of relaxed address. |
a4d24084 | 736 | Added to fr_fix: incl. ALL var chars. */ |
252b5132 RH |
737 | register symbolS *symbolP; |
738 | register long int where; | |
739 | register long int address_of_var; | |
740 | /* Where, in file space, is _var of *fragP? */ | |
741 | register long int target_address; | |
742 | /* Where, in file space, does addr point? */ | |
743 | ||
744 | know (fragP->fr_type == rs_machine_dependent); | |
252b5132 RH |
745 | where = fragP->fr_fix; |
746 | addressP = fragP->fr_literal + where; | |
747 | opcodeP = fragP->fr_opcode; | |
748 | symbolP = fragP->fr_symbol; | |
749 | know (symbolP); | |
750 | target_address = S_GET_VALUE (symbolP) + fragP->fr_offset; | |
751 | address_of_var = fragP->fr_address + where; | |
752 | switch (fragP->fr_subtype) | |
753 | { | |
754 | case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE): | |
755 | /* *addressP holds the registers number, plus 0x10, if it's deferred | |
756 | mode. To set up the right mode, just OR the size of this displacement */ | |
a4d24084 | 757 | /* Byte displacement. */ |
252b5132 RH |
758 | *addressP++ |= TAHOE_PC_OR_BYTE; |
759 | *addressP = target_address - (address_of_var + 2); | |
760 | extension = 2; | |
761 | break; | |
762 | ||
763 | case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD): | |
a4d24084 | 764 | /* Word displacement. */ |
252b5132 RH |
765 | *addressP++ |= TAHOE_PC_OR_WORD; |
766 | md_number_to_chars (addressP, target_address - (address_of_var + 3), 2); | |
767 | extension = 3; | |
768 | break; | |
769 | ||
770 | case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_LONG): | |
a4d24084 | 771 | /* Long word displacement. */ |
252b5132 RH |
772 | *addressP++ |= TAHOE_PC_OR_LONG; |
773 | md_number_to_chars (addressP, target_address - (address_of_var + 5), 4); | |
774 | extension = 5; | |
775 | break; | |
776 | ||
777 | case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE): | |
778 | *addressP = target_address - (address_of_var + 1); | |
779 | extension = 1; | |
780 | break; | |
781 | ||
782 | case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD): | |
a4d24084 | 783 | *opcodeP ^= 0x10; /* Reverse sense of test. */ |
252b5132 RH |
784 | *addressP++ = 3; /* Jump over word branch */ |
785 | *addressP++ = TAHOE_BRW; | |
786 | md_number_to_chars (addressP, target_address - (address_of_var + 4), 2); | |
787 | extension = 4; | |
788 | break; | |
789 | ||
790 | case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_LONG): | |
a4d24084 | 791 | *opcodeP ^= 0x10; /* Reverse sense of test. */ |
252b5132 RH |
792 | *addressP++ = 6; |
793 | *addressP++ = TAHOE_JMP; | |
794 | *addressP++ = TAHOE_PC_REL_LONG; | |
795 | md_number_to_chars (addressP, target_address, 4); | |
796 | extension = 7; | |
797 | break; | |
798 | ||
799 | case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_BYTE): | |
800 | *addressP = target_address - (address_of_var + 1); | |
801 | extension = 1; | |
802 | break; | |
803 | ||
804 | case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_WORD): | |
805 | *opcodeP = TAHOE_BRW; | |
806 | md_number_to_chars (addressP, target_address - (address_of_var + 2), 2); | |
807 | extension = 2; | |
808 | break; | |
809 | ||
810 | case ENCODE_RELAX (STATE_ALWAYS_BRANCH, STATE_LONG): | |
811 | *opcodeP = TAHOE_JMP; | |
812 | *addressP++ = TAHOE_PC_REL_LONG; | |
813 | md_number_to_chars (addressP, target_address - (address_of_var + 5), 4); | |
814 | extension = 5; | |
815 | break; | |
816 | ||
817 | case ENCODE_RELAX (STATE_BIG_REV_BRANCH, STATE_WORD): | |
818 | md_number_to_chars (addressP, target_address - (address_of_var + 2), 2); | |
819 | extension = 2; | |
820 | break; | |
821 | ||
822 | case ENCODE_RELAX (STATE_BIG_REV_BRANCH, STATE_LONG): | |
823 | *opcodeP ^= 0x10; | |
824 | *addressP++ = 0; | |
825 | *addressP++ = 6; | |
826 | *addressP++ = TAHOE_JMP; | |
827 | *addressP++ = TAHOE_PC_REL_LONG; | |
828 | md_number_to_chars (addressP, target_address, 4); | |
829 | extension = 8; | |
830 | break; | |
831 | ||
832 | case ENCODE_RELAX (STATE_BIG_NON_REV_BRANCH, STATE_WORD): | |
833 | md_number_to_chars (addressP, target_address - (address_of_var + 2), 2); | |
834 | extension = 2; | |
835 | break; | |
836 | ||
837 | case ENCODE_RELAX (STATE_BIG_NON_REV_BRANCH, STATE_LONG): | |
838 | *addressP++ = 0; | |
839 | *addressP++ = 2; | |
840 | *addressP++ = TAHOE_BRB; | |
841 | *addressP++ = 6; | |
842 | *addressP++ = TAHOE_JMP; | |
843 | *addressP++ = TAHOE_PC_REL_LONG; | |
844 | md_number_to_chars (addressP, target_address, 4); | |
845 | extension = 10; | |
846 | break; | |
847 | ||
848 | default: | |
849 | BAD_CASE (fragP->fr_subtype); | |
850 | break; | |
851 | } | |
852 | fragP->fr_fix += extension; | |
853 | } /* md_convert_frag */ | |
854 | \f | |
855 | ||
a4d24084 | 856 | /* This is the stuff for md_assemble. */ |
252b5132 RH |
857 | #define FP_REG 13 |
858 | #define SP_REG 14 | |
859 | #define PC_REG 15 | |
860 | #define BIGGESTREG PC_REG | |
861 | ||
862 | /* | |
863 | * Parse the string pointed to by START | |
864 | * If it represents a valid register, point START to the character after | |
865 | * the last valid register char, and return the register number (0-15). | |
866 | * If invalid, leave START alone, return -1. | |
867 | * The format has to be exact. I don't do things like eat leading zeros | |
868 | * or the like. | |
869 | * Note: This doesn't check for the next character in the string making | |
870 | * this invalid. Ex: R123 would return 12, it's the callers job to check | |
871 | * what start is point to apon return. | |
872 | * | |
873 | * Valid registers are R1-R15, %1-%15, FP (13), SP (14), PC (15) | |
874 | * Case doesn't matter. | |
875 | */ | |
876 | int | |
877 | tahoe_reg_parse (start) | |
a4d24084 | 878 | char **start; /* A pointer to the string to parse. */ |
252b5132 RH |
879 | { |
880 | register char *regpoint = *start; | |
881 | register int regnum = -1; | |
882 | ||
883 | switch (*regpoint++) | |
884 | { | |
885 | case '%': /* Registers can start with a %, | |
a4d24084 | 886 | R or r, and then a number. */ |
252b5132 RH |
887 | case 'R': |
888 | case 'r': | |
3882b010 | 889 | if (ISDIGIT (*regpoint)) |
252b5132 | 890 | { |
a4d24084 | 891 | /* Got the first digit. */ |
252b5132 | 892 | regnum = *regpoint++ - '0'; |
3882b010 | 893 | if ((regnum == 1) && ISDIGIT (*regpoint)) |
252b5132 | 894 | { |
a4d24084 | 895 | /* Its a two digit number. */ |
252b5132 RH |
896 | regnum = 10 + (*regpoint++ - '0'); |
897 | if (regnum > BIGGESTREG) | |
898 | { /* Number too big? */ | |
899 | regnum = -1; | |
900 | } | |
901 | } | |
902 | } | |
903 | break; | |
904 | case 'F': /* Is it the FP */ | |
905 | case 'f': | |
906 | switch (*regpoint++) | |
907 | { | |
908 | case 'p': | |
909 | case 'P': | |
910 | regnum = FP_REG; | |
911 | } | |
912 | break; | |
913 | case 's': /* How about the SP */ | |
914 | case 'S': | |
915 | switch (*regpoint++) | |
916 | { | |
917 | case 'p': | |
918 | case 'P': | |
919 | regnum = SP_REG; | |
920 | } | |
921 | break; | |
922 | case 'p': /* OR the PC even */ | |
923 | case 'P': | |
924 | switch (*regpoint++) | |
925 | { | |
926 | case 'c': | |
927 | case 'C': | |
928 | regnum = PC_REG; | |
929 | } | |
930 | break; | |
931 | } | |
932 | ||
933 | if (regnum != -1) | |
934 | { /* No error, so move string pointer */ | |
935 | *start = regpoint; | |
936 | } | |
937 | return regnum; /* Return results */ | |
938 | } /* tahoe_reg_parse */ | |
939 | \f | |
940 | /* | |
941 | * This chops up an operand and figures out its modes and stuff. | |
942 | * It's a little touchy about extra characters. | |
943 | * Optex to start with one extra character so it can be overwritten for | |
944 | * the backward part of the parsing. | |
945 | * You can't put a bunch of extra characters in side to | |
946 | * make the command look cute. ie: * foo ( r1 ) [ r0 ] | |
947 | * If you like doing a lot of typing, try COBOL! | |
948 | * Actually, this parser is a little weak all around. It's designed to be | |
949 | * used with compliers, so I emphisise correct decoding of valid code quickly | |
950 | * rather that catching every possable error. | |
951 | * Note: This uses the expression function, so save input_line_pointer before | |
952 | * calling. | |
953 | * | |
954 | * Sperry defines the semantics of address modes (and values) | |
955 | * by a two-letter code, explained here. | |
956 | * | |
957 | * letter 1: access type | |
958 | * | |
959 | * a address calculation - no data access, registers forbidden | |
960 | * b branch displacement | |
961 | * m read - let go of bus - write back "modify" | |
962 | * r read | |
963 | * w write | |
964 | * v bit field address: like 'a' but registers are OK | |
965 | * | |
966 | * letter 2: data type (i.e. width, alignment) | |
967 | * | |
968 | * b byte | |
969 | * w word | |
970 | * l longword | |
971 | * q quadword (Even regs < 14 allowed) (if 12, you get a warning) | |
972 | * - unconditional synthetic jbr operand | |
973 | * ? simple synthetic reversable branch operand | |
974 | * ! complex synthetic reversable branch operand | |
975 | * : complex synthetic non-reversable branch operand | |
976 | * | |
977 | * The '-?!:' letter 2's are not for external consumption. They are used | |
978 | * by GAS for psuedo ops relaxing code. | |
979 | * | |
980 | * After parsing topP has: | |
981 | * | |
982 | * top_ndx: -1, or the index register. eg 7=[R7] | |
983 | * top_reg: -1, or register number. eg 7 = R7 or (R7) | |
984 | * top_mode: The addressing mode byte. This byte, defines which of | |
985 | * the 11 modes opcode is. | |
986 | * top_access: Access type wanted for this opperand 'b'branch ' ' | |
987 | * no-instruction 'amrvw' | |
988 | * top_width: Operand width expected, one of "bwlq?-:!" | |
989 | * exp_of_operand: The expression as parsed by expression() | |
990 | * top_dispsize: Number of bytes in the displacement if we can figure it | |
991 | * out and it's relavent. | |
992 | * | |
993 | * Need syntax checks built. | |
994 | */ | |
995 | ||
996 | void | |
997 | tip_op (optex, topP) | |
998 | char *optex; /* The users text input, with one leading character */ | |
999 | struct top *topP; /* The tahoe instruction with some fields already set: | |
1000 | in: access, width | |
1001 | out: ndx, reg, mode, error, dispsize */ | |
1002 | ||
1003 | { | |
a4d24084 KH |
1004 | int mode = 0; /* This operand's mode. */ |
1005 | char segfault = *optex; /* To keep the back parsing from freaking. */ | |
1006 | char *point = optex + 1; /* Parsing from front to back. */ | |
1007 | char *end; /* Parsing from back to front. */ | |
252b5132 RH |
1008 | int reg = -1; /* major register, -1 means absent */ |
1009 | int imreg = -1; /* Major register in immediate mode */ | |
1010 | int ndx = -1; /* index register number, -1 means absent */ | |
1011 | char dec_inc = ' '; /* Is the SP auto-incremented '+' or | |
a4d24084 | 1012 | auto-decremented '-' or neither ' '. */ |
252b5132 RH |
1013 | int immediate = 0; /* 1 if '$' immediate mode */ |
1014 | int call_width = 0; /* If the caller casts the displacement */ | |
1015 | int abs_width = 0; /* The width of the absolute displacment */ | |
1016 | int com_width = 0; /* Displacement width required by branch */ | |
1017 | int deferred = 0; /* 1 if '*' deferral is used */ | |
1018 | byte disp_size = 0; /* How big is this operand. 0 == don't know */ | |
1019 | char *op_bad = ""; /* Bad operand error */ | |
1020 | ||
1021 | char *tp, *temp, c; /* Temporary holders */ | |
1022 | ||
a4d24084 | 1023 | char access = topP->top_access; /* Save on a deref. */ |
252b5132 RH |
1024 | char width = topP->top_width; |
1025 | ||
1026 | int really_none = 0; /* Empty expressions evaluate to 0 | |
1027 | but I need to know if it's there or not */ | |
1028 | expressionS *expP; /* -> expression values for this operand */ | |
1029 | ||
a4d24084 | 1030 | /* Does this command restrict the displacement size. */ |
252b5132 RH |
1031 | if (access == 'b') |
1032 | com_width = (width == 'b' ? 1 : | |
1033 | (width == 'w' ? 2 : | |
1034 | (width == 'l' ? 4 : 0))); | |
1035 | ||
1036 | *optex = '\0'; /* This is kind of a back stop for all | |
1037 | the searches to fail on if needed.*/ | |
1038 | if (*point == '*') | |
1039 | { /* A dereference? */ | |
1040 | deferred = 1; | |
1041 | point++; | |
1042 | } | |
1043 | ||
1044 | /* Force words into a certain mode */ | |
1045 | /* Bitch, Bitch, Bitch! */ | |
1046 | /* | |
1047 | * Using the ^ operator is ambigous. If I have an absolute label | |
1048 | * called 'w' set to, say 2, and I have the expression 'w^1', do I get | |
1049 | * 1, forced to be in word displacement mode, or do I get the value of | |
1050 | * 'w' or'ed with 1 (3 in this case). | |
1051 | * The default is 'w' as an offset, so that's what I use. | |
1052 | * Stick with `, it does the same, and isn't ambig. | |
1053 | */ | |
1054 | ||
1055 | if (*point != '\0' && ((point[1] == '^') || (point[1] == '`'))) | |
1056 | switch (*point) | |
1057 | { | |
1058 | case 'b': | |
1059 | case 'B': | |
1060 | case 'w': | |
1061 | case 'W': | |
1062 | case 'l': | |
1063 | case 'L': | |
1064 | if (com_width) | |
1065 | as_warn (_("Casting a branch displacement is bad form, and is ignored.")); | |
1066 | else | |
1067 | { | |
3882b010 | 1068 | c = TOLOWER (*point); |
252b5132 RH |
1069 | call_width = ((c == 'b') ? 1 : |
1070 | ((c == 'w') ? 2 : 4)); | |
1071 | } | |
1072 | point += 2; | |
1073 | break; | |
1074 | } | |
1075 | ||
1076 | /* Setting immediate mode */ | |
1077 | if (*point == '$') | |
1078 | { | |
1079 | immediate = 1; | |
1080 | point++; | |
1081 | } | |
1082 | ||
1083 | /* | |
1084 | * I've pulled off all the easy stuff off the front, move to the end and | |
1085 | * yank. | |
1086 | */ | |
1087 | ||
a4d24084 | 1088 | for (end = point; *end != '\0'; end++) /* Move to the end. */ |
252b5132 RH |
1089 | ; |
1090 | ||
1091 | if (end != point) /* Null string? */ | |
1092 | end--; | |
1093 | ||
1094 | if (end > point && *end == ' ' && end[-1] != '\'') | |
1095 | end--; /* Hop white space */ | |
1096 | ||
a4d24084 | 1097 | /* Is this an index reg. */ |
252b5132 RH |
1098 | if ((*end == ']') && (end[-1] != '\'')) |
1099 | { | |
1100 | temp = end; | |
1101 | ||
a4d24084 | 1102 | /* Find opening brace. */ |
252b5132 RH |
1103 | for (--end; (*end != '[' && end != point); end--) |
1104 | ; | |
1105 | ||
a4d24084 | 1106 | /* If I found the opening brace, get the index register number. */ |
252b5132 RH |
1107 | if (*end == '[') |
1108 | { | |
a4d24084 | 1109 | tp = end + 1; /* tp should point to the start of a reg. */ |
252b5132 RH |
1110 | ndx = tahoe_reg_parse (&tp); |
1111 | if (tp != temp) | |
a4d24084 | 1112 | { /* Reg. parse error. */ |
252b5132 RH |
1113 | ndx = -1; |
1114 | } | |
1115 | else | |
1116 | { | |
a4d24084 | 1117 | end--; /* Found it, move past brace. */ |
252b5132 RH |
1118 | } |
1119 | if (ndx == -1) | |
1120 | { | |
1121 | op_bad = _("Couldn't parse the [index] in this operand."); | |
a4d24084 | 1122 | end = point; /* Force all the rest of the tests to fail. */ |
252b5132 RH |
1123 | } |
1124 | } | |
1125 | else | |
1126 | { | |
1127 | op_bad = _("Couldn't find the opening '[' for the index of this operand."); | |
a4d24084 | 1128 | end = point; /* Force all the rest of the tests to fail. */ |
252b5132 RH |
1129 | } |
1130 | } | |
1131 | ||
1132 | /* Post increment? */ | |
1133 | if (*end == '+') | |
1134 | { | |
1135 | dec_inc = '+'; | |
1136 | /* was: *end--; */ | |
1137 | end--; | |
1138 | } | |
1139 | ||
1140 | /* register in parens? */ | |
1141 | if ((*end == ')') && (end[-1] != '\'')) | |
1142 | { | |
1143 | temp = end; | |
1144 | ||
a4d24084 | 1145 | /* Find opening paren. */ |
252b5132 RH |
1146 | for (--end; (*end != '(' && end != point); end--) |
1147 | ; | |
1148 | ||
a4d24084 | 1149 | /* If I found the opening paren, get the register number. */ |
252b5132 RH |
1150 | if (*end == '(') |
1151 | { | |
1152 | tp = end + 1; | |
1153 | reg = tahoe_reg_parse (&tp); | |
1154 | if (tp != temp) | |
1155 | { | |
a4d24084 | 1156 | /* Not a register, but could be part of the expression. */ |
252b5132 RH |
1157 | reg = -1; |
1158 | end = temp; /* Rest the pointer back */ | |
1159 | } | |
1160 | else | |
1161 | { | |
a4d24084 | 1162 | end--; /* Found the reg. move before opening paren. */ |
252b5132 RH |
1163 | } |
1164 | } | |
1165 | else | |
1166 | { | |
1167 | op_bad = _("Couldn't find the opening '(' for the deref of this operand."); | |
a4d24084 | 1168 | end = point; /* Force all the rest of the tests to fail. */ |
252b5132 RH |
1169 | } |
1170 | } | |
1171 | ||
1172 | /* Pre decrement? */ | |
1173 | if (*end == '-') | |
1174 | { | |
1175 | if (dec_inc != ' ') | |
1176 | { | |
1177 | op_bad = _("Operand can't be both pre-inc and post-dec."); | |
1178 | end = point; | |
1179 | } | |
1180 | else | |
1181 | { | |
1182 | dec_inc = '-'; | |
1183 | /* was: *end--; */ | |
1184 | end--; | |
1185 | } | |
1186 | } | |
1187 | ||
1188 | /* | |
1189 | * Everything between point and end is the 'expression', unless it's | |
1190 | * a register name. | |
1191 | */ | |
1192 | ||
1193 | c = end[1]; | |
1194 | end[1] = '\0'; | |
1195 | ||
1196 | tp = point; | |
1197 | imreg = tahoe_reg_parse (&point); /* Get the immediate register | |
1198 | if it is there.*/ | |
1199 | if (*point != '\0') | |
1200 | { | |
a4d24084 | 1201 | /* If there is junk after point, then the it's not immediate reg. */ |
252b5132 RH |
1202 | point = tp; |
1203 | imreg = -1; | |
1204 | } | |
1205 | ||
1206 | if (imreg != -1 && reg != -1) | |
1207 | op_bad = _("I parsed 2 registers in this operand."); | |
1208 | ||
1209 | /* | |
1210 | * Evaluate whats left of the expression to see if it's valid. | |
1211 | * Note again: This assumes that the calling expression has saved | |
1212 | * input_line_pointer. (Nag, nag, nag!) | |
1213 | */ | |
1214 | ||
1215 | if (*op_bad == '\0') | |
1216 | { | |
1994a7c7 | 1217 | /* Statement has no syntax goofs yet: let's sniff the expression. */ |
252b5132 RH |
1218 | input_line_pointer = point; |
1219 | expP = &(topP->exp_of_operand); | |
1220 | topP->seg_of_operand = expression (expP); | |
1221 | switch (expP->X_op) | |
1222 | { | |
1223 | case O_absent: | |
1224 | /* No expression. For BSD4.2 compatibility, missing expression is | |
1225 | absolute 0 */ | |
1226 | expP->X_op = O_constant; | |
1227 | expP->X_add_number = 0; | |
1228 | really_none = 1; | |
1229 | case O_constant: | |
1230 | /* for SEG_ABSOLUTE, we shouldnt need to set X_op_symbol, | |
a4d24084 | 1231 | X_add_symbol to any particular value. */ |
252b5132 | 1232 | /* But, we will program defensively. Since this situation occurs |
a4d24084 | 1233 | rarely so it costs us little to do so. */ |
252b5132 RH |
1234 | expP->X_add_symbol = NULL; |
1235 | expP->X_op_symbol = NULL; | |
1236 | /* How many bytes are needed to express this abs value? */ | |
1237 | abs_width = | |
1238 | ((((expP->X_add_number & 0xFFFFFF80) == 0) || | |
1239 | ((expP->X_add_number & 0xFFFFFF80) == 0xFFFFFF80)) ? 1 : | |
1240 | (((expP->X_add_number & 0xFFFF8000) == 0) || | |
1241 | ((expP->X_add_number & 0xFFFF8000) == 0xFFFF8000)) ? 2 : 4); | |
1242 | ||
1243 | case O_symbol: | |
1244 | break; | |
1245 | ||
1246 | default: | |
1247 | /* | |
1248 | * Major bug. We can't handle the case of a operator | |
1249 | * expression in a synthetic opcode variable-length | |
1250 | * instruction. We don't have a frag type that is smart | |
1251 | * enough to relax a operator, and so we just force all | |
1252 | * operators to behave like SEG_PASS1s. Clearly, if there is | |
1253 | * a demand we can invent a new or modified frag type and | |
1254 | * then coding up a frag for this case will be easy. | |
1255 | */ | |
1256 | need_pass_2 = 1; | |
1257 | op_bad = _("Can't relocate expression error."); | |
1258 | break; | |
1259 | ||
1260 | case O_big: | |
1261 | /* This is an error. Tahoe doesn't allow any expressions | |
1262 | bigger that a 32 bit long word. Any bigger has to be referenced | |
a4d24084 | 1263 | by address. */ |
252b5132 RH |
1264 | op_bad = _("Expression is too large for a 32 bits."); |
1265 | break; | |
1266 | } | |
1267 | if (*input_line_pointer != '\0') | |
1268 | { | |
1269 | op_bad = _("Junk at end of expression."); | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | end[1] = c; | |
1274 | ||
1275 | /* I'm done, so restore optex */ | |
1276 | *optex = segfault; | |
1277 | ||
252b5132 RH |
1278 | /* |
1279 | * At this point in the game, we (in theory) have all the components of | |
1280 | * the operand at least parsed. Now it's time to check for syntax/semantic | |
1281 | * errors, and build the mode. | |
1282 | * This is what I have: | |
1283 | * deferred = 1 if '*' | |
1284 | * call_width = 0,1,2,4 | |
1285 | * abs_width = 0,1,2,4 | |
1286 | * com_width = 0,1,2,4 | |
1287 | * immediate = 1 if '$' | |
1288 | * ndx = -1 or reg num | |
1289 | * dec_inc = '-' or '+' or ' ' | |
1290 | * reg = -1 or reg num | |
1291 | * imreg = -1 or reg num | |
1292 | * topP->exp_of_operand | |
1293 | * really_none | |
1294 | */ | |
1295 | /* Is there a displacement size? */ | |
1296 | disp_size = (call_width ? call_width : | |
1297 | (com_width ? com_width : | |
1298 | abs_width ? abs_width : 0)); | |
1299 | ||
1300 | if (*op_bad == '\0') | |
1301 | { | |
1302 | if (imreg != -1) | |
1303 | { | |
1304 | /* Rn */ | |
1305 | mode = TAHOE_DIRECT_REG; | |
1306 | if (deferred || immediate || (dec_inc != ' ') || | |
1307 | (reg != -1) || !really_none) | |
1308 | op_bad = _("Syntax error in direct register mode."); | |
1309 | else if (ndx != -1) | |
1310 | op_bad = _("You can't index a register in direct register mode."); | |
1311 | else if (imreg == SP_REG && access == 'r') | |
1312 | op_bad = | |
1313 | _("SP can't be the source operand with direct register addressing."); | |
1314 | else if (access == 'a') | |
1315 | op_bad = _("Can't take the address of a register."); | |
1316 | else if (access == 'b') | |
1317 | op_bad = _("Direct Register can't be used in a branch."); | |
1318 | else if (width == 'q' && ((imreg % 2) || (imreg > 13))) | |
1319 | op_bad = _("For quad access, the register must be even and < 14."); | |
1320 | else if (call_width) | |
1321 | op_bad = _("You can't cast a direct register."); | |
1322 | ||
1323 | if (*op_bad == '\0') | |
1324 | { | |
1325 | /* No errors, check for warnings */ | |
1326 | if (width == 'q' && imreg == 12) | |
1327 | as_warn (_("Using reg 14 for quadwords can tromp the FP register.")); | |
1328 | ||
1329 | reg = imreg; | |
1330 | } | |
1331 | ||
1332 | /* We know: imm = -1 */ | |
1333 | } | |
1334 | else if (dec_inc == '-') | |
1335 | { | |
1336 | /* -(SP) */ | |
1337 | mode = TAHOE_AUTO_DEC; | |
1338 | if (deferred || immediate || !really_none) | |
1339 | op_bad = _("Syntax error in auto-dec mode."); | |
1340 | else if (ndx != -1) | |
1341 | op_bad = _("You can't have an index auto dec mode."); | |
1342 | else if (access == 'r') | |
1343 | op_bad = _("Auto dec mode cant be used for reading."); | |
1344 | else if (reg != SP_REG) | |
1345 | op_bad = _("Auto dec only works of the SP register."); | |
1346 | else if (access == 'b') | |
1347 | op_bad = _("Auto dec can't be used in a branch."); | |
1348 | else if (width == 'q') | |
1349 | op_bad = _("Auto dec won't work with quadwords."); | |
1350 | ||
1351 | /* We know: imm = -1, dec_inc != '-' */ | |
1352 | } | |
1353 | else if (dec_inc == '+') | |
1354 | { | |
1355 | if (immediate || !really_none) | |
1356 | op_bad = _("Syntax error in one of the auto-inc modes."); | |
1357 | else if (deferred) | |
1358 | { | |
1359 | /* *(SP)+ */ | |
1360 | mode = TAHOE_AUTO_INC_DEFERRED; | |
1361 | if (reg != SP_REG) | |
1362 | op_bad = _("Auto inc deferred only works of the SP register."); | |
1363 | else if (ndx != -1) | |
1364 | op_bad = _("You can't have an index auto inc deferred mode."); | |
1365 | else if (access == 'b') | |
1366 | op_bad = _("Auto inc can't be used in a branch."); | |
1367 | } | |
1368 | else | |
1369 | { | |
1370 | /* (SP)+ */ | |
1371 | mode = TAHOE_AUTO_INC; | |
1372 | if (access == 'm' || access == 'w') | |
1373 | op_bad = _("You can't write to an auto inc register."); | |
1374 | else if (reg != SP_REG) | |
1375 | op_bad = _("Auto inc only works of the SP register."); | |
1376 | else if (access == 'b') | |
1377 | op_bad = _("Auto inc can't be used in a branch."); | |
1378 | else if (width == 'q') | |
1379 | op_bad = _("Auto inc won't work with quadwords."); | |
1380 | else if (ndx != -1) | |
1381 | op_bad = _("You can't have an index in auto inc mode."); | |
1382 | } | |
1383 | ||
1384 | /* We know: imm = -1, dec_inc == ' ' */ | |
1385 | } | |
1386 | else if (reg != -1) | |
1387 | { | |
1388 | if ((ndx != -1) && (reg == SP_REG)) | |
1389 | op_bad = _("You can't index the sp register."); | |
1390 | if (deferred) | |
1391 | { | |
1392 | /* *<disp>(Rn) */ | |
1393 | mode = TAHOE_REG_DISP_DEFERRED; | |
1394 | if (immediate) | |
1395 | op_bad = _("Syntax error in register displaced mode."); | |
1396 | } | |
1397 | else if (really_none) | |
1398 | { | |
1399 | /* (Rn) */ | |
1400 | mode = TAHOE_REG_DEFERRED; | |
1401 | /* if reg = SP then cant be indexed */ | |
1402 | } | |
1403 | else | |
1404 | { | |
1405 | /* <disp>(Rn) */ | |
1406 | mode = TAHOE_REG_DISP; | |
1407 | } | |
1408 | ||
1409 | /* We know: imm = -1, dec_inc == ' ', Reg = -1 */ | |
1410 | } | |
1411 | else | |
1412 | { | |
1413 | if (really_none) | |
1414 | op_bad = _("An offest is needed for this operand."); | |
1415 | if (deferred && immediate) | |
1416 | { | |
1417 | /* *$<ADDR> */ | |
1418 | mode = TAHOE_ABSOLUTE_ADDR; | |
1419 | disp_size = 4; | |
1420 | } | |
1421 | else if (immediate) | |
1422 | { | |
1423 | /* $<disp> */ | |
1424 | mode = TAHOE_IMMEDIATE; | |
1425 | if (ndx != -1) | |
1426 | op_bad = _("You can't index a register in immediate mode."); | |
1427 | if (access == 'a') | |
1428 | op_bad = _("Immediate access can't be used as an address."); | |
a4d24084 | 1429 | /* ponder the wisdom of a cast because it doesn't do any good. */ |
252b5132 RH |
1430 | } |
1431 | else if (deferred) | |
1432 | { | |
1433 | /* *<disp> */ | |
1434 | mode = TAHOE_DISP_REL_DEFERRED; | |
1435 | } | |
1436 | else | |
1437 | { | |
1438 | /* <disp> */ | |
1439 | mode = TAHOE_DISPLACED_RELATIVE; | |
1440 | } | |
1441 | } | |
1442 | } | |
1443 | ||
1444 | /* | |
1445 | * At this point, all the errors we can do have be checked for. | |
a4d24084 | 1446 | * We can build the 'top'. */ |
252b5132 RH |
1447 | |
1448 | topP->top_ndx = ndx; | |
1449 | topP->top_reg = reg; | |
1450 | topP->top_mode = mode; | |
1451 | topP->top_error = op_bad; | |
1452 | topP->top_dispsize = disp_size; | |
1453 | } /* tip_op */ | |
1454 | \f | |
1455 | /* | |
1456 | * t i p ( ) | |
1457 | * | |
1458 | * This converts a string into a tahoe instruction. | |
1459 | * The string must be a bare single instruction in tahoe (with BSD4 frobs) | |
1460 | * format. | |
1461 | * It provides at most one fatal error message (which stops the scan) | |
1462 | * some warning messages as it finds them. | |
1463 | * The tahoe instruction is returned in exploded form. | |
1464 | * | |
1465 | * The exploded instruction is returned to a struct tit of your choice. | |
1466 | * #include "tahoe-inst.h" to know what a struct tit is. | |
1467 | * | |
1468 | */ | |
1469 | ||
1470 | static void | |
1471 | tip (titP, instring) | |
a4d24084 KH |
1472 | struct tit *titP; /* We build an exploded instruction here. */ |
1473 | char *instring; /* Text of a vax instruction: we modify. */ | |
252b5132 | 1474 | { |
a4d24084 | 1475 | register struct tot_wot *twP = NULL; /* How to bit-encode this opcode. */ |
252b5132 RH |
1476 | register char *p; /* 1/skip whitespace.2/scan vot_how */ |
1477 | register char *q; /* */ | |
1478 | register unsigned char count; /* counts number of operands seen */ | |
1479 | register struct top *operandp;/* scan operands in struct tit */ | |
1480 | register char *alloperr = ""; /* error over all operands */ | |
1481 | register char c; /* Remember char, (we clobber it | |
a4d24084 | 1482 | with '\0' temporarily). */ |
252b5132 RH |
1483 | char *save_input_line_pointer; |
1484 | ||
1485 | if (*instring == ' ') | |
a4d24084 | 1486 | ++instring; /* Skip leading whitespace. */ |
252b5132 RH |
1487 | for (p = instring; *p && *p != ' '; p++) |
1488 | ; /* MUST end in end-of-string or | |
a4d24084 KH |
1489 | exactly 1 space. */ |
1490 | /* Scanned up to end of operation-code. */ | |
1491 | /* Operation-code is ended with whitespace. */ | |
252b5132 RH |
1492 | if (p == instring) |
1493 | { | |
1494 | titP->tit_error = _("No operator"); | |
1495 | count = 0; | |
1496 | titP->tit_opcode = 0; | |
1497 | } | |
1498 | else | |
1499 | { | |
1500 | c = *p; | |
1501 | *p = '\0'; | |
1502 | /* | |
1503 | * Here with instring pointing to what better be an op-name, and p | |
1504 | * pointing to character just past that. | |
1505 | * We trust instring points to an op-name, with no whitespace. | |
1506 | */ | |
1507 | twP = (struct tot_wot *) hash_find (op_hash, instring); | |
a4d24084 | 1508 | *p = c; /* Restore char after op-code. */ |
252b5132 RH |
1509 | if (twP == 0) |
1510 | { | |
1511 | titP->tit_error = _("Unknown operator"); | |
1512 | count = 0; | |
1513 | titP->tit_opcode = 0; | |
1514 | } | |
1515 | else | |
1516 | { | |
1517 | /* | |
1994a7c7 | 1518 | * We found a match! So let's pick up as many operands as the |
252b5132 RH |
1519 | * instruction wants, and even gripe if there are too many. |
1520 | * We expect comma to seperate each operand. | |
1521 | * We let instring track the text, while p tracks a part of the | |
1522 | * struct tot. | |
1523 | */ | |
1524 | ||
1525 | count = 0; /* no operands seen yet */ | |
1526 | instring = p + (*p != '\0'); /* point past the operation code */ | |
1527 | /* tip_op() screws with the input_line_pointer, so save it before | |
1528 | I jump in */ | |
1529 | save_input_line_pointer = input_line_pointer; | |
1530 | for (p = twP->args, operandp = titP->tit_operand; | |
1531 | !*alloperr && *p; | |
1532 | operandp++, p += 2) | |
1533 | { | |
1534 | /* | |
1535 | * Here to parse one operand. Leave instring pointing just | |
1536 | * past any one ',' that marks the end of this operand. | |
1537 | */ | |
1538 | if (!p[1]) | |
1539 | as_fatal (_("Compiler bug: ODD number of bytes in arg structure %s."), | |
1540 | twP->args); | |
1541 | else if (*instring) | |
1542 | { | |
1543 | for (q = instring; (*q != ',' && *q != '\0'); q++) | |
1544 | { | |
1545 | if (*q == '\'' && q[1] != '\0') /* Jump quoted characters */ | |
1546 | q++; | |
1547 | } | |
1548 | c = *q; | |
1549 | /* | |
1550 | * Q points to ',' or '\0' that ends argument. C is that | |
1551 | * character. | |
1552 | */ | |
1553 | *q = '\0'; | |
1554 | operandp->top_access = p[0]; | |
1555 | operandp->top_width = p[1]; | |
1556 | tip_op (instring - 1, operandp); | |
a4d24084 | 1557 | *q = c; /* Restore input text. */ |
252b5132 RH |
1558 | if (*(operandp->top_error)) |
1559 | { | |
1560 | alloperr = operandp->top_error; | |
1561 | } | |
1562 | instring = q + (c ? 1 : 0); /* next operand (if any) */ | |
1563 | count++; /* won another argument, may have an operr */ | |
1564 | } | |
1565 | else | |
1566 | alloperr = _("Not enough operands"); | |
1567 | } | |
a4d24084 | 1568 | /* Restore the pointer. */ |
252b5132 RH |
1569 | input_line_pointer = save_input_line_pointer; |
1570 | ||
1571 | if (!*alloperr) | |
1572 | { | |
1573 | if (*instring == ' ') | |
a4d24084 | 1574 | instring++; /* Skip whitespace. */ |
252b5132 RH |
1575 | if (*instring) |
1576 | alloperr = _("Too many operands"); | |
1577 | } | |
1578 | titP->tit_error = alloperr; | |
1579 | } | |
1580 | } | |
1581 | ||
a4d24084 | 1582 | titP->tit_opcode = twP->code; /* The op-code. */ |
252b5132 RH |
1583 | titP->tit_operands = count; |
1584 | } /* tip */ | |
1585 | \f | |
1586 | /* md_assemble() emit frags for 1 instruction */ | |
1587 | void | |
1588 | md_assemble (instruction_string) | |
a4d24084 | 1589 | char *instruction_string; /* A string: assemble 1 instruction. */ |
252b5132 RH |
1590 | { |
1591 | char *p; | |
a4d24084 KH |
1592 | register struct top *operandP;/* An operand. Scans all operands. */ |
1593 | /* char c_save; fixme: remove this line *//* What used to live after an expression. */ | |
1594 | /* struct frag *fragP; fixme: remove this line *//* Fragment of code we just made. */ | |
252b5132 | 1595 | /* register struct top *end_operandP; fixme: remove this line *//* -> slot just after last operand |
a4d24084 | 1596 | Limit of the for (each operand). */ |
252b5132 RH |
1597 | register expressionS *expP; /* -> expression values for this operand */ |
1598 | ||
a4d24084 | 1599 | /* These refer to an instruction operand expression. */ |
252b5132 RH |
1600 | segT to_seg; /* Target segment of the address. */ |
1601 | ||
1602 | register valueT this_add_number; | |
a4d24084 | 1603 | register symbolS *this_add_symbol; /* +ve (minuend) symbol. */ |
252b5132 | 1604 | |
a4d24084 KH |
1605 | /* tahoe_opcodeT opcode_as_number; fixme: remove this line *//* The opcode as a number. */ |
1606 | char *opcodeP; /* Where it is in a frag. */ | |
1607 | /* char *opmodeP; fixme: remove this line *//* Where opcode type is, in a frag. */ | |
252b5132 RH |
1608 | |
1609 | int dispsize; /* From top_dispsize: tahoe_operand_width | |
1610 | (in bytes) */ | |
1611 | int is_undefined; /* 1 if operand expression's | |
a4d24084 | 1612 | segment not known yet. */ |
252b5132 RH |
1613 | int pc_rel; /* Is this operand pc relative? */ |
1614 | ||
a4d24084 | 1615 | /* Decode the operand. */ |
252b5132 RH |
1616 | tip (&t, instruction_string); |
1617 | ||
1618 | /* | |
1619 | * Check to see if this operand decode properly. | |
1620 | * Notice that we haven't made any frags yet. | |
1621 | * If it goofed, then this instruction will wedge in any pass, | |
1622 | * and we can safely flush it, without causing interpass symbol phase | |
1623 | * errors. That is, without changing label values in different passes. | |
1624 | */ | |
1625 | if (*t.tit_error) | |
1626 | { | |
1627 | as_warn (_("Ignoring statement due to \"%s\""), t.tit_error); | |
1628 | } | |
1629 | else | |
1630 | { | |
1631 | /* We saw no errors in any operands - try to make frag(s) */ | |
a4d24084 KH |
1632 | /* Emit op-code. */ |
1633 | /* Remember where it is, in case we want to modify the op-code later. */ | |
252b5132 RH |
1634 | opcodeP = frag_more (1); |
1635 | *opcodeP = t.tit_opcode; | |
a4d24084 | 1636 | /* Now do each operand. */ |
252b5132 RH |
1637 | for (operandP = t.tit_operand; |
1638 | operandP < t.tit_operand + t.tit_operands; | |
1639 | operandP++) | |
1640 | { /* for each operand */ | |
1641 | expP = &(operandP->exp_of_operand); | |
1642 | if (operandP->top_ndx >= 0) | |
1643 | { | |
1644 | /* Indexed addressing byte | |
1645 | Legality of indexed mode already checked: it is OK */ | |
1646 | FRAG_APPEND_1_CHAR (0x40 + operandP->top_ndx); | |
1647 | } /* if(top_ndx>=0) */ | |
1648 | ||
a4d24084 | 1649 | /* Here to make main operand frag(s). */ |
252b5132 RH |
1650 | this_add_number = expP->X_add_number; |
1651 | this_add_symbol = expP->X_add_symbol; | |
1652 | to_seg = operandP->seg_of_operand; | |
1653 | know (to_seg == SEG_UNKNOWN || \ | |
1654 | to_seg == SEG_ABSOLUTE || \ | |
1655 | to_seg == SEG_DATA || \ | |
1656 | to_seg == SEG_TEXT || \ | |
1657 | to_seg == SEG_BSS); | |
1658 | is_undefined = (to_seg == SEG_UNKNOWN); | |
1659 | /* Do we know how big this opperand is? */ | |
1660 | dispsize = operandP->top_dispsize; | |
1661 | pc_rel = 0; | |
1662 | /* Deal with the branch possabilities. (Note, this doesn't include | |
1663 | jumps.)*/ | |
1664 | if (operandP->top_access == 'b') | |
1665 | { | |
1666 | /* Branches must be expressions. A psuedo branch can also jump to | |
a4d24084 | 1667 | an absolute address. */ |
252b5132 RH |
1668 | if (to_seg == now_seg || is_undefined) |
1669 | { | |
a4d24084 | 1670 | /* If is_undefined, then it might BECOME now_seg by relax time. */ |
252b5132 RH |
1671 | if (dispsize) |
1672 | { | |
1673 | /* I know how big the branch is supposed to be (it's a normal | |
a4d24084 | 1674 | branch), so I set up the frag, and let GAS do the rest. */ |
252b5132 RH |
1675 | p = frag_more (dispsize); |
1676 | fix_new (frag_now, p - frag_now->fr_literal, | |
1677 | this_add_symbol, this_add_number, | |
1678 | size_to_fx (dispsize, 1), | |
1679 | NULL); | |
1680 | } | |
1681 | else | |
1682 | { | |
1683 | /* (to_seg==now_seg || to_seg == SEG_UNKNOWN) && dispsize==0 */ | |
1684 | /* If we don't know how big it is, then its a synthetic branch, | |
a4d24084 | 1685 | so we set up a simple relax state. */ |
252b5132 RH |
1686 | switch (operandP->top_width) |
1687 | { | |
1688 | case TAHOE_WIDTH_CONDITIONAL_JUMP: | |
1689 | /* Simple (conditional) jump. I may have to reverse the | |
1690 | condition of opcodeP, and then jump to my destination. | |
1691 | I set 1 byte aside for the branch off set, and could need 6 | |
1692 | more bytes for the pc_rel jump */ | |
1693 | frag_var (rs_machine_dependent, 7, 1, | |
1694 | ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, | |
1695 | is_undefined ? STATE_UNDF : STATE_BYTE), | |
1696 | this_add_symbol, this_add_number, opcodeP); | |
1697 | break; | |
1698 | case TAHOE_WIDTH_ALWAYS_JUMP: | |
1699 | /* Simple (unconditional) jump. I may have to convert this to | |
a4d24084 | 1700 | a word branch, or an absolute jump. */ |
252b5132 RH |
1701 | frag_var (rs_machine_dependent, 5, 1, |
1702 | ENCODE_RELAX (STATE_ALWAYS_BRANCH, | |
1703 | is_undefined ? STATE_UNDF : STATE_BYTE), | |
1704 | this_add_symbol, this_add_number, opcodeP); | |
1705 | break; | |
a4d24084 | 1706 | /* The smallest size for the next 2 cases is word. */ |
252b5132 RH |
1707 | case TAHOE_WIDTH_BIG_REV_JUMP: |
1708 | frag_var (rs_machine_dependent, 8, 2, | |
1709 | ENCODE_RELAX (STATE_BIG_REV_BRANCH, | |
1710 | is_undefined ? STATE_UNDF : STATE_WORD), | |
1711 | this_add_symbol, this_add_number, | |
1712 | opcodeP); | |
1713 | break; | |
1714 | case TAHOE_WIDTH_BIG_NON_REV_JUMP: | |
1715 | frag_var (rs_machine_dependent, 10, 2, | |
1716 | ENCODE_RELAX (STATE_BIG_NON_REV_BRANCH, | |
1717 | is_undefined ? STATE_UNDF : STATE_WORD), | |
1718 | this_add_symbol, this_add_number, | |
1719 | opcodeP); | |
1720 | break; | |
1721 | default: | |
1722 | as_fatal (_("Compliler bug: Got a case (%d) I wasn't expecting."), | |
1723 | operandP->top_width); | |
1724 | } | |
1725 | } | |
1726 | } | |
1727 | else | |
1728 | { | |
1729 | /* to_seg != now_seg && to_seg != seg_unknown (still in branch) | |
1730 | In other words, I'm jumping out of my segment so extend the | |
a4d24084 | 1731 | branches to jumps, and let GAS fix them. */ |
252b5132 RH |
1732 | |
1733 | /* These are "branches" what will always be branches around a jump | |
1734 | to the correct addresss in real life. | |
1735 | If to_seg is SEG_ABSOLUTE, just encode the branch in, | |
a4d24084 | 1736 | else let GAS fix the address. */ |
252b5132 RH |
1737 | |
1738 | switch (operandP->top_width) | |
1739 | { | |
1740 | /* The theory: | |
1741 | For SEG_ABSOLUTE, then mode is ABSOLUTE_ADDR, jump | |
1742 | to that addresss (not pc_rel). | |
a4d24084 | 1743 | For other segs, address is a long word PC rel jump. */ |
252b5132 RH |
1744 | case TAHOE_WIDTH_CONDITIONAL_JUMP: |
1745 | /* b<cond> */ | |
1746 | /* To reverse the condition in a TAHOE branch, | |
1747 | complement bit 4 */ | |
1748 | *opcodeP ^= 0x10; | |
1749 | p = frag_more (7); | |
1750 | *p++ = 6; | |
1751 | *p++ = TAHOE_JMP; | |
1752 | *p++ = (operandP->top_mode == | |
1753 | TAHOE_ABSOLUTE_ADDR ? TAHOE_ABSOLUTE_ADDR : | |
1754 | TAHOE_PC_REL_LONG); | |
1755 | fix_new (frag_now, p - frag_now->fr_literal, | |
1756 | this_add_symbol, this_add_number, | |
1757 | (to_seg != SEG_ABSOLUTE) ? FX_PCREL32 : FX_32, NULL); | |
1758 | /* | |
1759 | * Now (eg) BLEQ 1f | |
1760 | * JMP foo | |
1761 | * 1: | |
1762 | */ | |
1763 | break; | |
1764 | case TAHOE_WIDTH_ALWAYS_JUMP: | |
1765 | /* br, just turn it into a jump */ | |
1766 | *opcodeP = TAHOE_JMP; | |
1767 | p = frag_more (5); | |
1768 | *p++ = (operandP->top_mode == | |
1769 | TAHOE_ABSOLUTE_ADDR ? TAHOE_ABSOLUTE_ADDR : | |
1770 | TAHOE_PC_REL_LONG); | |
1771 | fix_new (frag_now, p - frag_now->fr_literal, | |
1772 | this_add_symbol, this_add_number, | |
1773 | (to_seg != SEG_ABSOLUTE) ? FX_PCREL32 : FX_32, NULL); | |
1774 | /* Now (eg) JMP foo */ | |
1775 | break; | |
1776 | case TAHOE_WIDTH_BIG_REV_JUMP: | |
1777 | p = frag_more (8); | |
1778 | *opcodeP ^= 0x10; | |
1779 | *p++ = 0; | |
1780 | *p++ = 6; | |
1781 | *p++ = TAHOE_JMP; | |
1782 | *p++ = (operandP->top_mode == | |
1783 | TAHOE_ABSOLUTE_ADDR ? TAHOE_ABSOLUTE_ADDR : | |
1784 | TAHOE_PC_REL_LONG); | |
1785 | fix_new (frag_now, p - frag_now->fr_literal, | |
1786 | this_add_symbol, this_add_number, | |
1787 | (to_seg != SEG_ABSOLUTE) ? FX_PCREL32 : FX_32, NULL); | |
1788 | /* | |
1789 | * Now (eg) ACBx 1f | |
1790 | * JMP foo | |
1791 | * 1: | |
1792 | */ | |
1793 | break; | |
1794 | case TAHOE_WIDTH_BIG_NON_REV_JUMP: | |
1795 | p = frag_more (10); | |
1796 | *p++ = 0; | |
1797 | *p++ = 2; | |
1798 | *p++ = TAHOE_BRB; | |
1799 | *p++ = 6; | |
1800 | *p++ = TAHOE_JMP; | |
1801 | *p++ = (operandP->top_mode == | |
1802 | TAHOE_ABSOLUTE_ADDR ? TAHOE_ABSOLUTE_ADDR : | |
1803 | TAHOE_PC_REL_LONG); | |
1804 | fix_new (frag_now, p - frag_now->fr_literal, | |
1805 | this_add_symbol, this_add_number, | |
1806 | (to_seg != SEG_ABSOLUTE) ? FX_PCREL32 : FX_32, NULL); | |
1807 | /* | |
1808 | * Now (eg) xOBxxx 1f | |
1809 | * BRB 2f | |
1810 | * 1: JMP @#foo | |
1811 | * 2: | |
1812 | */ | |
1813 | break; | |
1814 | case 'b': | |
1815 | case 'w': | |
1816 | as_warn (_("Real branch displacements must be expressions.")); | |
1817 | break; | |
1818 | default: | |
1819 | as_fatal (_("Complier error: I got an unknown synthetic branch :%c"), | |
1820 | operandP->top_width); | |
1821 | break; | |
1822 | } | |
1823 | } | |
1824 | } | |
1825 | else | |
1826 | { | |
a4d24084 | 1827 | /* It ain't a branch operand. */ |
252b5132 RH |
1828 | switch (operandP->top_mode) |
1829 | { | |
1830 | /* Auto-foo access, only works for one reg (SP) | |
a4d24084 | 1831 | so the only thing needed is the mode. */ |
252b5132 RH |
1832 | case TAHOE_AUTO_DEC: |
1833 | case TAHOE_AUTO_INC: | |
1834 | case TAHOE_AUTO_INC_DEFERRED: | |
1835 | FRAG_APPEND_1_CHAR (operandP->top_mode); | |
1836 | break; | |
1837 | ||
1838 | /* Numbered Register only access. Only thing needed is the | |
1839 | mode + Register number */ | |
1840 | case TAHOE_DIRECT_REG: | |
1841 | case TAHOE_REG_DEFERRED: | |
1842 | FRAG_APPEND_1_CHAR (operandP->top_mode + operandP->top_reg); | |
1843 | break; | |
1844 | ||
1845 | /* An absolute address. It's size is always 5 bytes. | |
a4d24084 | 1846 | (mode_type + 4 byte address). */ |
252b5132 RH |
1847 | case TAHOE_ABSOLUTE_ADDR: |
1848 | know ((this_add_symbol == NULL)); | |
1849 | p = frag_more (5); | |
1850 | *p = TAHOE_ABSOLUTE_ADDR; | |
1851 | md_number_to_chars (p + 1, this_add_number, 4); | |
1852 | break; | |
1853 | ||
1854 | /* Immediate data. If the size isn't known, then it's an address | |
a4d24084 | 1855 | + and offset, which is 4 bytes big. */ |
252b5132 RH |
1856 | case TAHOE_IMMEDIATE: |
1857 | if (this_add_symbol != NULL) | |
1858 | { | |
1859 | p = frag_more (5); | |
1860 | *p++ = TAHOE_IMMEDIATE_LONGWORD; | |
1861 | fix_new (frag_now, p - frag_now->fr_literal, | |
1862 | this_add_symbol, this_add_number, | |
1863 | FX_32, NULL); | |
1864 | } | |
1865 | else | |
1866 | { | |
a4d24084 | 1867 | /* It's a integer, and I know it's size. */ |
252b5132 RH |
1868 | if ((unsigned) this_add_number < 0x40) |
1869 | { | |
1870 | /* Will it fit in a literal? */ | |
1871 | FRAG_APPEND_1_CHAR ((byte) this_add_number); | |
1872 | } | |
1873 | else | |
1874 | { | |
1875 | p = frag_more (dispsize + 1); | |
1876 | switch (dispsize) | |
1877 | { | |
1878 | case 1: | |
1879 | *p++ = TAHOE_IMMEDIATE_BYTE; | |
1880 | *p = (byte) this_add_number; | |
1881 | break; | |
1882 | case 2: | |
1883 | *p++ = TAHOE_IMMEDIATE_WORD; | |
1884 | md_number_to_chars (p, this_add_number, 2); | |
1885 | break; | |
1886 | case 4: | |
1887 | *p++ = TAHOE_IMMEDIATE_LONGWORD; | |
1888 | md_number_to_chars (p, this_add_number, 4); | |
1889 | break; | |
1890 | } | |
1891 | } | |
1892 | } | |
1893 | break; | |
1894 | ||
1895 | /* Distance from the PC. If the size isn't known, we have to relax | |
1896 | into it. The difference between this and disp(sp) is that | |
1897 | this offset is pc_rel, and disp(sp) isn't. | |
a4d24084 | 1898 | Note the drop through code. */ |
252b5132 RH |
1899 | |
1900 | case TAHOE_DISPLACED_RELATIVE: | |
1901 | case TAHOE_DISP_REL_DEFERRED: | |
1902 | operandP->top_reg = PC_REG; | |
1903 | pc_rel = 1; | |
1904 | ||
1905 | /* Register, plus a displacement mode. Save the register number, | |
1906 | and weather its deffered or not, and relax the size if it isn't | |
a4d24084 | 1907 | known. */ |
252b5132 RH |
1908 | case TAHOE_REG_DISP: |
1909 | case TAHOE_REG_DISP_DEFERRED: | |
1910 | if (operandP->top_mode == TAHOE_DISP_REL_DEFERRED || | |
1911 | operandP->top_mode == TAHOE_REG_DISP_DEFERRED) | |
1912 | operandP->top_reg += 0x10; /* deffered mode is always 0x10 higher | |
a4d24084 | 1913 | than it's non-deffered sibling. */ |
252b5132 RH |
1914 | |
1915 | /* Is this a value out of this segment? | |
1916 | The first part of this conditional is a cludge to make gas | |
1917 | produce the same output as 'as' when there is a lable, in | |
1918 | the current segment, displaceing a register. It's strange, | |
1919 | and no one in their right mind would do it, but it's easy | |
a4d24084 | 1920 | to cludge. */ |
252b5132 RH |
1921 | if ((dispsize == 0 && !pc_rel) || |
1922 | (to_seg != now_seg && !is_undefined && to_seg != SEG_ABSOLUTE)) | |
1923 | dispsize = 4; | |
1924 | ||
1925 | if (dispsize == 0) | |
1926 | { | |
1927 | /* | |
1928 | * We have a SEG_UNKNOWN symbol, or the size isn't cast. | |
1929 | * It might turn out to be in the same segment as | |
1930 | * the instruction, permitting relaxation. | |
1931 | */ | |
1932 | p = frag_var (rs_machine_dependent, 5, 2, | |
1933 | ENCODE_RELAX (STATE_PC_RELATIVE, | |
1934 | is_undefined ? STATE_UNDF : STATE_BYTE), | |
1935 | this_add_symbol, this_add_number, 0); | |
1936 | *p = operandP->top_reg; | |
1937 | } | |
1938 | else | |
1939 | { | |
a4d24084 | 1940 | /* Either this is an abs, or a cast. */ |
252b5132 RH |
1941 | p = frag_more (dispsize + 1); |
1942 | switch (dispsize) | |
1943 | { | |
1944 | case 1: | |
1945 | *p = TAHOE_PC_OR_BYTE + operandP->top_reg; | |
1946 | break; | |
1947 | case 2: | |
1948 | *p = TAHOE_PC_OR_WORD + operandP->top_reg; | |
1949 | break; | |
1950 | case 4: | |
1951 | *p = TAHOE_PC_OR_LONG + operandP->top_reg; | |
1952 | break; | |
1953 | }; | |
1954 | fix_new (frag_now, p + 1 - frag_now->fr_literal, | |
1955 | this_add_symbol, this_add_number, | |
1956 | size_to_fx (dispsize, pc_rel), NULL); | |
1957 | } | |
1958 | break; | |
1959 | default: | |
1960 | as_fatal (_("Barf, bad mode %x\n"), operandP->top_mode); | |
1961 | } | |
1962 | } | |
1963 | } /* for(operandP) */ | |
1964 | } /* if(!need_pass_2 && !goofed) */ | |
1965 | } /* tahoe_assemble() */ | |
1966 | ||
a4d24084 | 1967 | /* We have no need to default values of symbols. */ |
252b5132 | 1968 | |
252b5132 RH |
1969 | symbolS * |
1970 | md_undefined_symbol (name) | |
1971 | char *name; | |
1972 | { | |
1973 | return 0; | |
1974 | } /* md_undefined_symbol() */ | |
1975 | ||
a4d24084 | 1976 | /* Round up a section size to the appropriate boundary. */ |
252b5132 RH |
1977 | valueT |
1978 | md_section_align (segment, size) | |
1979 | segT segment; | |
1980 | valueT size; | |
1981 | { | |
1982 | return ((size + 7) & ~7); /* Round all sects to multiple of 8 */ | |
1983 | } /* md_section_align() */ | |
1984 | ||
1985 | /* Exactly what point is a PC-relative offset relative TO? | |
1986 | On the sparc, they're relative to the address of the offset, plus | |
1987 | its size. This gets us to the following instruction. | |
1988 | (??? Is this right? FIXME-SOON) */ | |
a4d24084 | 1989 | long |
252b5132 RH |
1990 | md_pcrel_from (fixP) |
1991 | fixS *fixP; | |
1992 | { | |
1993 | return (((fixP->fx_type == FX_8 | |
1994 | || fixP->fx_type == FX_PCREL8) | |
1995 | ? 1 | |
1996 | : ((fixP->fx_type == FX_16 | |
1997 | || fixP->fx_type == FX_PCREL16) | |
1998 | ? 2 | |
1999 | : ((fixP->fx_type == FX_32 | |
2000 | || fixP->fx_type == FX_PCREL32) | |
2001 | ? 4 | |
2002 | : 0))) + fixP->fx_where + fixP->fx_frag->fr_address); | |
2003 | } /* md_pcrel_from() */ | |
2004 | ||
a4d24084 | 2005 | int |
252b5132 RH |
2006 | tc_is_pcrel (fixP) |
2007 | fixS *fixP; | |
2008 | { | |
2009 | /* should never be called */ | |
2010 | know (0); | |
2011 | return (0); | |
2012 | } /* tc_is_pcrel() */ |