Commit | Line | Data |
---|---|---|
e9f53129 AM |
1 | /* spu.c -- Assembler for the IBM Synergistic Processing Unit (SPU) |
2 | ||
840edabd | 3 | Copyright 2006, 2007 Free Software Foundation, Inc. |
e9f53129 AM |
4 | |
5 | This file is part of GAS, the GNU Assembler. | |
6 | ||
7 | GAS is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GAS is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GAS; see the file COPYING. If not, write to the Free | |
19 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA | |
20 | 02110-1301, USA. */ | |
21 | ||
22 | #include "as.h" | |
23 | #include "safe-ctype.h" | |
24 | #include "subsegs.h" | |
e9f53129 AM |
25 | #include "dwarf2dbg.h" |
26 | ||
27 | const struct spu_opcode spu_opcodes[] = { | |
28 | #define APUOP(TAG,MACFORMAT,OPCODE,MNEMONIC,ASMFORMAT,DEP,PIPE) \ | |
29 | { MACFORMAT, (OPCODE) << (32-11), MNEMONIC, ASMFORMAT }, | |
30 | #define APUOPFB(TAG,MACFORMAT,OPCODE,FB,MNEMONIC,ASMFORMAT,DEP,PIPE) \ | |
31 | { MACFORMAT, ((OPCODE) << (32-11)) | ((FB) << (32-18)), MNEMONIC, ASMFORMAT }, | |
32 | #include "opcode/spu-insns.h" | |
33 | #undef APUOP | |
34 | #undef APUOPFB | |
35 | }; | |
36 | ||
37 | static const int spu_num_opcodes = | |
38 | sizeof (spu_opcodes) / sizeof (spu_opcodes[0]); | |
39 | ||
40 | #define MAX_RELOCS 2 | |
41 | ||
42 | struct spu_insn | |
43 | { | |
44 | unsigned int opcode; | |
45 | expressionS exp[MAX_RELOCS]; | |
46 | int reloc_arg[MAX_RELOCS]; | |
47 | int flag[MAX_RELOCS]; | |
48 | enum spu_insns tag; | |
49 | }; | |
50 | ||
51 | static const char *get_imm (const char *param, struct spu_insn *insn, int arg); | |
52 | static const char *get_reg (const char *param, struct spu_insn *insn, int arg, | |
53 | int accept_expr); | |
54 | ||
55 | static int calcop (struct spu_opcode *format, const char *param, | |
56 | struct spu_insn *insn); | |
57 | ||
58 | extern char *myname; | |
59 | static struct hash_control *op_hash = NULL; | |
60 | ||
61 | /* These bits should be turned off in the first address of every segment */ | |
62 | int md_seg_align = 7; | |
63 | ||
64 | /* These chars start a comment anywhere in a source file (except inside | |
65 | another comment */ | |
66 | const char comment_chars[] = "#"; | |
67 | ||
68 | /* These chars only start a comment at the beginning of a line. */ | |
69 | const char line_comment_chars[] = "#"; | |
70 | ||
71 | /* gods own line continuation char */ | |
72 | const char line_separator_chars[] = ";"; | |
73 | ||
74 | /* Chars that can be used to separate mant from exp in floating point nums */ | |
75 | const char EXP_CHARS[] = "eE"; | |
76 | ||
77 | /* Chars that mean this number is a floating point constant */ | |
78 | /* as in 0f123.456 */ | |
79 | /* or 0H1.234E-12 (see exp chars above) */ | |
80 | const char FLT_CHARS[] = "dDfF"; | |
81 | ||
82 | const pseudo_typeS md_pseudo_table[] = | |
83 | { | |
84 | {"align", s_align_ptwo, 4}, | |
85 | {"def", s_set, 0}, | |
86 | {"dfloat", float_cons, 'd'}, | |
87 | {"ffloat", float_cons, 'f'}, | |
88 | {"global", s_globl, 0}, | |
89 | {"half", cons, 2}, | |
90 | {"bss", s_lcomm_bytes, 1}, | |
91 | {"string", stringer, 1}, | |
92 | {"word", cons, 4}, | |
93 | /* Force set to be treated as an instruction. */ | |
94 | {"set", NULL, 0}, | |
95 | {".set", s_set, 0}, | |
cefdba39 AM |
96 | /* Likewise for eqv. */ |
97 | {"eqv", NULL, 0}, | |
98 | {".eqv", s_set, -1}, | |
e9f53129 AM |
99 | {"file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0 }, |
100 | {"loc", dwarf2_directive_loc, 0}, | |
101 | {0,0,0} | |
102 | }; | |
103 | ||
104 | void | |
105 | md_begin (void) | |
106 | { | |
107 | const char *retval = NULL; | |
108 | int i; | |
109 | ||
110 | /* initialize hash table */ | |
111 | ||
112 | op_hash = hash_new (); | |
113 | ||
114 | /* loop until you see the end of the list */ | |
115 | ||
116 | for (i = 0; i < spu_num_opcodes; i++) | |
117 | { | |
118 | /* hash each mnemonic and record its position */ | |
119 | ||
120 | retval = hash_insert (op_hash, spu_opcodes[i].mnemonic, (PTR)&spu_opcodes[i]); | |
121 | ||
122 | if (retval != NULL && strcmp (retval, "exists") != 0) | |
123 | as_fatal (_("Can't hash instruction '%s':%s"), | |
124 | spu_opcodes[i].mnemonic, retval); | |
125 | } | |
126 | } | |
127 | \f | |
128 | const char *md_shortopts = ""; | |
129 | struct option md_longopts[] = { | |
130 | #define OPTION_APUASM (OPTION_MD_BASE) | |
131 | {"apuasm", no_argument, NULL, OPTION_APUASM}, | |
132 | #define OPTION_DD2 (OPTION_MD_BASE+1) | |
133 | {"mdd2.0", no_argument, NULL, OPTION_DD2}, | |
134 | #define OPTION_DD1 (OPTION_MD_BASE+2) | |
135 | {"mdd1.0", no_argument, NULL, OPTION_DD1}, | |
136 | #define OPTION_DD3 (OPTION_MD_BASE+3) | |
137 | {"mdd3.0", no_argument, NULL, OPTION_DD3}, | |
138 | { NULL, no_argument, NULL, 0 } | |
139 | }; | |
140 | size_t md_longopts_size = sizeof (md_longopts); | |
141 | ||
142 | /* When set (by -apuasm) our assembler emulates the behaviour of apuasm. | |
143 | * e.g. don't add bias to float conversion and don't right shift | |
144 | * immediate values. */ | |
145 | static int emulate_apuasm; | |
146 | ||
147 | /* Use the dd2.0 instructions set. The only differences are some new | |
148 | * register names and the orx insn */ | |
149 | static int use_dd2 = 1; | |
150 | ||
151 | int | |
152 | md_parse_option (int c, char *arg ATTRIBUTE_UNUSED) | |
153 | { | |
154 | switch (c) | |
155 | { | |
156 | case OPTION_APUASM: | |
157 | emulate_apuasm = 1; | |
158 | break; | |
159 | case OPTION_DD3: | |
160 | use_dd2 = 1; | |
161 | break; | |
162 | case OPTION_DD2: | |
163 | use_dd2 = 1; | |
164 | break; | |
165 | case OPTION_DD1: | |
166 | use_dd2 = 0; | |
167 | break; | |
168 | default: | |
169 | return 0; | |
170 | } | |
171 | return 1; | |
172 | } | |
173 | ||
174 | void | |
175 | md_show_usage (FILE *stream) | |
176 | { | |
177 | fputs (_("\ | |
178 | SPU options:\n\ | |
179 | --apuasm emulate behaviour of apuasm\n"), | |
180 | stream); | |
181 | } | |
182 | \f | |
183 | ||
184 | struct arg_encode { | |
185 | int size; | |
186 | int pos; | |
187 | int rshift; | |
188 | int lo, hi; | |
189 | int wlo, whi; | |
190 | bfd_reloc_code_real_type reloc; | |
191 | }; | |
192 | ||
193 | static struct arg_encode arg_encode[A_MAX] = { | |
194 | { 7, 0, 0, 0, 127, 0, -1, 0 }, /* A_T */ | |
195 | { 7, 7, 0, 0, 127, 0, -1, 0 }, /* A_A */ | |
196 | { 7, 14, 0, 0, 127, 0, -1, 0 }, /* A_B */ | |
197 | { 7, 21, 0, 0, 127, 0, -1, 0 }, /* A_C */ | |
198 | { 7, 7, 0, 0, 127, 0, -1, 0 }, /* A_S */ | |
199 | { 7, 7, 0, 0, 127, 0, -1, 0 }, /* A_H */ | |
200 | { 0, 0, 0, 0, -1, 0, -1, 0 }, /* A_P */ | |
201 | { 7, 14, 0, 0, -1, 0, -1, BFD_RELOC_SPU_IMM7 }, /* A_S3 */ | |
202 | { 7, 14, 0, -32, 31, -31, 0, BFD_RELOC_SPU_IMM7 }, /* A_S6 */ | |
203 | { 7, 14, 0, 0, -1, 0, -1, BFD_RELOC_SPU_IMM7 }, /* A_S7N */ | |
204 | { 7, 14, 0, -64, 63, -63, 0, BFD_RELOC_SPU_IMM7 }, /* A_S7 */ | |
205 | { 8, 14, 0, 0, 127, 0, -1, BFD_RELOC_SPU_IMM8 }, /* A_U7A */ | |
206 | { 8, 14, 0, 0, 127, 0, -1, BFD_RELOC_SPU_IMM8 }, /* A_U7B */ | |
207 | { 10, 14, 0, -512, 511, -128, 255, BFD_RELOC_SPU_IMM10 }, /* A_S10B */ | |
208 | { 10, 14, 0, -512, 511, 0, -1, BFD_RELOC_SPU_IMM10 }, /* A_S10 */ | |
209 | { 2, 23, 9, -1024, 1023, 0, -1, BFD_RELOC_SPU_PCREL9a }, /* A_S11 */ | |
210 | { 2, 14, 9, -1024, 1023, 0, -1, BFD_RELOC_SPU_PCREL9b }, /* A_S11I */ | |
211 | { 10, 14, 4, -8192, 8191, 0, -1, BFD_RELOC_SPU_IMM10W }, /* A_S14 */ | |
212 | { 16, 7, 0, -32768, 32767, 0, -1, BFD_RELOC_SPU_IMM16 }, /* A_S16 */ | |
213 | { 16, 7, 2, -131072, 262143, 0, -1, BFD_RELOC_SPU_IMM16W }, /* A_S18 */ | |
214 | { 16, 7, 2, -262144, 262143, 0, -1, BFD_RELOC_SPU_PCREL16 }, /* A_R18 */ | |
215 | { 7, 14, 0, 0, -1, 0, -1, BFD_RELOC_SPU_IMM7 }, /* A_U3 */ | |
216 | { 7, 14, 0, 0, 127, 0, 31, BFD_RELOC_SPU_IMM7 }, /* A_U5 */ | |
217 | { 7, 14, 0, 0, 127, 0, 63, BFD_RELOC_SPU_IMM7 }, /* A_U6 */ | |
218 | { 7, 14, 0, 0, -1, 0, -1, BFD_RELOC_SPU_IMM7 }, /* A_U7 */ | |
219 | { 14, 0, 0, 0, 16383, 0, -1, 0 }, /* A_U14 */ | |
220 | { 16, 7, 0, -32768, 65535, 0, -1, BFD_RELOC_SPU_IMM16 }, /* A_X16 */ | |
221 | { 18, 7, 0, 0, 262143, 0, -1, BFD_RELOC_SPU_IMM18 }, /* A_U18 */ | |
222 | }; | |
223 | ||
224 | /* Some flags for handling errors. This is very hackish and added after | |
225 | * the fact. */ | |
226 | static int syntax_error_arg; | |
227 | static const char *syntax_error_param; | |
228 | static int syntax_reg; | |
229 | ||
230 | static char * | |
231 | insn_fmt_string (struct spu_opcode *format) | |
232 | { | |
233 | static char buf[64]; | |
234 | int len = 0; | |
235 | int i; | |
236 | ||
237 | len += sprintf (&buf[len], "%s\t", format->mnemonic); | |
238 | for (i = 1; i <= format->arg[0]; i++) | |
239 | { | |
240 | int arg = format->arg[i]; | |
241 | char *exp; | |
242 | if (i > 1 && arg != A_P && format->arg[i-1] != A_P) | |
243 | buf[len++] = ','; | |
244 | if (arg == A_P) | |
245 | exp = "("; | |
246 | else if (arg < A_P) | |
247 | exp = i == syntax_error_arg ? "REG" : "reg"; | |
248 | else | |
249 | exp = i == syntax_error_arg ? "IMM" : "imm"; | |
250 | len += sprintf (&buf[len], "%s", exp); | |
251 | if (i > 1 && format->arg[i-1] == A_P) | |
252 | buf[len++] = ')'; | |
253 | } | |
254 | buf[len] = 0; | |
255 | return buf; | |
256 | } | |
257 | ||
258 | void | |
259 | md_assemble (char *op) | |
260 | { | |
261 | char *param, *thisfrag; | |
262 | char c; | |
263 | struct spu_opcode *format; | |
264 | struct spu_insn insn; | |
265 | int i; | |
266 | ||
267 | assert (op); | |
268 | ||
269 | /* skip over instruction to find parameters */ | |
270 | ||
271 | for (param = op; *param != 0 && !ISSPACE (*param); param++) | |
272 | ; | |
273 | c = *param; | |
274 | *param = 0; | |
275 | ||
276 | if (c != 0 && c != '\n') | |
277 | param++; | |
278 | ||
279 | /* try to find the instruction in the hash table */ | |
280 | ||
281 | if ((format = (struct spu_opcode *) hash_find (op_hash, op)) == NULL) | |
282 | { | |
283 | as_bad (_("Invalid mnemonic '%s'"), op); | |
284 | return; | |
285 | } | |
286 | ||
287 | if (!use_dd2 && strcmp (format->mnemonic, "orx") == 0) | |
288 | { | |
289 | as_bad (_("'%s' is only available in DD2.0 or higher."), op); | |
290 | return; | |
291 | } | |
292 | ||
293 | while (1) | |
294 | { | |
295 | /* try parsing this instruction into insn */ | |
296 | for (i = 0; i < MAX_RELOCS; i++) | |
297 | { | |
298 | insn.exp[i].X_add_symbol = 0; | |
299 | insn.exp[i].X_op_symbol = 0; | |
300 | insn.exp[i].X_add_number = 0; | |
301 | insn.exp[i].X_op = O_illegal; | |
302 | insn.reloc_arg[i] = -1; | |
303 | insn.flag[i] = 0; | |
304 | } | |
305 | insn.opcode = format->opcode; | |
306 | insn.tag = (enum spu_insns) (format - spu_opcodes); | |
307 | ||
308 | syntax_error_arg = 0; | |
309 | syntax_error_param = 0; | |
310 | syntax_reg = 0; | |
311 | if (calcop (format, param, &insn)) | |
312 | break; | |
313 | ||
314 | /* if it doesn't parse try the next instruction */ | |
315 | if (!strcmp (format[0].mnemonic, format[1].mnemonic)) | |
316 | format++; | |
317 | else | |
318 | { | |
319 | int parg = format[0].arg[syntax_error_arg-1]; | |
320 | ||
321 | as_fatal (_("Error in argument %d. Expecting: \"%s\""), | |
322 | syntax_error_arg - (parg == A_P), | |
323 | insn_fmt_string (format)); | |
324 | return; | |
325 | } | |
326 | } | |
327 | ||
328 | if ((syntax_reg & 4) | |
329 | && ! (insn.tag == M_RDCH | |
330 | || insn.tag == M_RCHCNT | |
331 | || insn.tag == M_WRCH)) | |
332 | as_warn (_("Mixing register syntax, with and without '$'.")); | |
333 | if (syntax_error_param) | |
334 | { | |
335 | const char *d = syntax_error_param; | |
336 | while (*d != '$') | |
337 | d--; | |
e2785c44 | 338 | as_warn (_("Treating '%-*s' as a symbol."), (int)(syntax_error_param - d), d); |
e9f53129 AM |
339 | } |
340 | ||
341 | /* grow the current frag and plop in the opcode */ | |
342 | ||
343 | thisfrag = frag_more (4); | |
344 | md_number_to_chars (thisfrag, insn.opcode, 4); | |
345 | ||
346 | /* if this instruction requires labels mark it for later */ | |
347 | ||
348 | for (i = 0; i < MAX_RELOCS; i++) | |
349 | if (insn.reloc_arg[i] >= 0) | |
350 | { | |
351 | fixS *fixP; | |
352 | bfd_reloc_code_real_type reloc = arg_encode[insn.reloc_arg[i]].reloc; | |
353 | int pcrel = 0; | |
354 | if (reloc == BFD_RELOC_SPU_PCREL9a | |
355 | || reloc == BFD_RELOC_SPU_PCREL9b | |
356 | || reloc == BFD_RELOC_SPU_PCREL16) | |
357 | pcrel = 1; | |
358 | if (insn.flag[i] & 1) | |
359 | reloc = BFD_RELOC_SPU_HI16; | |
360 | else if (insn.flag[i] & 2) | |
361 | reloc = BFD_RELOC_SPU_LO16; | |
362 | fixP = fix_new_exp (frag_now, | |
363 | thisfrag - frag_now->fr_literal, | |
364 | 4, | |
365 | &insn.exp[i], | |
366 | pcrel, | |
367 | reloc); | |
840edabd AM |
368 | fixP->tc_fix_data.arg_format = insn.reloc_arg[i]; |
369 | fixP->tc_fix_data.insn_tag = insn.tag; | |
e9f53129 AM |
370 | } |
371 | dwarf2_emit_insn (4); | |
372 | } | |
373 | ||
374 | static int | |
375 | calcop (struct spu_opcode *format, const char *param, struct spu_insn *insn) | |
376 | { | |
377 | int i; | |
378 | int paren = 0; | |
379 | int arg; | |
380 | ||
381 | for (i = 1; i <= format->arg[0]; i++) | |
382 | { | |
383 | arg = format->arg[i]; | |
384 | syntax_error_arg = i; | |
385 | ||
386 | while (ISSPACE (*param)) | |
387 | param++; | |
388 | if (*param == 0 || *param == ',') | |
389 | return 0; | |
390 | if (arg < A_P) | |
391 | param = get_reg (param, insn, arg, 1); | |
392 | else if (arg > A_P) | |
393 | param = get_imm (param, insn, arg); | |
394 | else if (arg == A_P) | |
395 | { | |
396 | paren++; | |
397 | if ('(' != *param++) | |
398 | return 0; | |
399 | } | |
400 | ||
401 | if (!param) | |
402 | return 0; | |
403 | ||
404 | while (ISSPACE (*param)) | |
405 | param++; | |
406 | ||
407 | if (arg != A_P && paren) | |
408 | { | |
409 | paren--; | |
410 | if (')' != *param++) | |
411 | return 0; | |
412 | } | |
413 | else if (i < format->arg[0] | |
414 | && format->arg[i] != A_P | |
415 | && format->arg[i+1] != A_P) | |
416 | { | |
417 | if (',' != *param++) | |
418 | { | |
419 | syntax_error_arg++; | |
420 | return 0; | |
421 | } | |
422 | } | |
423 | } | |
424 | while (ISSPACE (*param)) | |
425 | param++; | |
426 | return !paren && (*param == 0 || *param == '\n'); | |
427 | } | |
428 | ||
429 | struct reg_name { | |
430 | unsigned int regno; | |
431 | unsigned int length; | |
432 | char name[32]; | |
433 | }; | |
434 | ||
435 | #define REG_NAME(NO,NM) { NO, sizeof (NM) - 1, NM } | |
436 | ||
437 | static struct reg_name reg_name[] = { | |
438 | REG_NAME (0, "lr"), /* link register */ | |
439 | REG_NAME (1, "sp"), /* stack pointer */ | |
440 | REG_NAME (0, "rp"), /* link register */ | |
441 | REG_NAME (127, "fp"), /* frame pointer */ | |
442 | }; | |
443 | ||
444 | static struct reg_name sp_reg_name[] = { | |
445 | }; | |
446 | ||
447 | static struct reg_name ch_reg_name[] = { | |
448 | REG_NAME ( 0, "SPU_RdEventStat"), | |
449 | REG_NAME ( 1, "SPU_WrEventMask"), | |
450 | REG_NAME ( 2, "SPU_WrEventAck"), | |
451 | REG_NAME ( 3, "SPU_RdSigNotify1"), | |
452 | REG_NAME ( 4, "SPU_RdSigNotify2"), | |
453 | REG_NAME ( 7, "SPU_WrDec"), | |
454 | REG_NAME ( 8, "SPU_RdDec"), | |
455 | REG_NAME ( 11, "SPU_RdEventMask"), /* DD2.0 only */ | |
456 | REG_NAME ( 13, "SPU_RdMachStat"), | |
457 | REG_NAME ( 14, "SPU_WrSRR0"), | |
458 | REG_NAME ( 15, "SPU_RdSRR0"), | |
459 | REG_NAME ( 28, "SPU_WrOutMbox"), | |
460 | REG_NAME ( 29, "SPU_RdInMbox"), | |
461 | REG_NAME ( 30, "SPU_WrOutIntrMbox"), | |
462 | REG_NAME ( 9, "MFC_WrMSSyncReq"), | |
463 | REG_NAME ( 12, "MFC_RdTagMask"), /* DD2.0 only */ | |
464 | REG_NAME ( 16, "MFC_LSA"), | |
465 | REG_NAME ( 17, "MFC_EAH"), | |
466 | REG_NAME ( 18, "MFC_EAL"), | |
467 | REG_NAME ( 19, "MFC_Size"), | |
468 | REG_NAME ( 20, "MFC_TagID"), | |
469 | REG_NAME ( 21, "MFC_Cmd"), | |
470 | REG_NAME ( 22, "MFC_WrTagMask"), | |
471 | REG_NAME ( 23, "MFC_WrTagUpdate"), | |
472 | REG_NAME ( 24, "MFC_RdTagStat"), | |
473 | REG_NAME ( 25, "MFC_RdListStallStat"), | |
474 | REG_NAME ( 26, "MFC_WrListStallAck"), | |
475 | REG_NAME ( 27, "MFC_RdAtomicStat"), | |
476 | }; | |
477 | #undef REG_NAME | |
478 | ||
479 | static const char * | |
480 | get_reg (const char *param, struct spu_insn *insn, int arg, int accept_expr) | |
481 | { | |
482 | unsigned regno; | |
483 | int saw_prefix = 0; | |
484 | ||
485 | if (*param == '$') | |
486 | { | |
487 | saw_prefix = 1; | |
488 | param++; | |
489 | } | |
490 | ||
491 | if (arg == A_H) /* Channel */ | |
492 | { | |
493 | if ((param[0] == 'c' || param[0] == 'C') | |
494 | && (param[1] == 'h' || param[1] == 'H') | |
495 | && ISDIGIT (param[2])) | |
496 | param += 2; | |
497 | } | |
498 | else if (arg == A_S) /* Special purpose register */ | |
499 | { | |
500 | if ((param[0] == 's' || param[0] == 'S') | |
501 | && (param[1] == 'p' || param[1] == 'P') | |
502 | && ISDIGIT (param[2])) | |
503 | param += 2; | |
504 | } | |
505 | ||
506 | if (ISDIGIT (*param)) | |
507 | { | |
508 | regno = 0; | |
509 | while (ISDIGIT (*param)) | |
510 | regno = regno * 10 + *param++ - '0'; | |
511 | } | |
512 | else | |
513 | { | |
514 | struct reg_name *rn; | |
515 | unsigned int i, n, l = 0; | |
516 | ||
517 | if (arg == A_H) /* Channel */ | |
518 | { | |
519 | rn = ch_reg_name; | |
520 | n = sizeof (ch_reg_name) / sizeof (*ch_reg_name); | |
521 | } | |
522 | else if (arg == A_S) /* Special purpose register */ | |
523 | { | |
524 | rn = sp_reg_name; | |
525 | n = sizeof (sp_reg_name) / sizeof (*sp_reg_name); | |
526 | } | |
527 | else | |
528 | { | |
529 | rn = reg_name; | |
530 | n = sizeof (reg_name) / sizeof (*reg_name); | |
531 | } | |
532 | regno = 128; | |
533 | for (i = 0; i < n; i++) | |
534 | if (rn[i].length > l | |
535 | && 0 == strncasecmp (param, rn[i].name, rn[i].length)) | |
536 | { | |
537 | l = rn[i].length; | |
538 | regno = rn[i].regno; | |
539 | } | |
540 | param += l; | |
541 | } | |
542 | ||
543 | if (!use_dd2 | |
544 | && arg == A_H) | |
545 | { | |
546 | if (regno == 11) | |
547 | as_bad (_("'SPU_RdEventMask' (channel 11) is only available in DD2.0 or higher.")); | |
548 | else if (regno == 12) | |
549 | as_bad (_("'MFC_RdTagMask' (channel 12) is only available in DD2.0 or higher.")); | |
550 | } | |
551 | ||
552 | if (regno < 128) | |
553 | { | |
554 | insn->opcode |= regno << arg_encode[arg].pos; | |
555 | if ((!saw_prefix && syntax_reg == 1) | |
556 | || (saw_prefix && syntax_reg == 2)) | |
557 | syntax_reg |= 4; | |
558 | syntax_reg |= saw_prefix ? 1 : 2; | |
559 | return param; | |
560 | } | |
561 | ||
562 | if (accept_expr) | |
563 | { | |
564 | char *save_ptr; | |
565 | expressionS ex; | |
566 | save_ptr = input_line_pointer; | |
567 | input_line_pointer = (char *)param; | |
568 | expression (&ex); | |
569 | param = input_line_pointer; | |
570 | input_line_pointer = save_ptr; | |
571 | if (ex.X_op == O_register || ex.X_op == O_constant) | |
572 | { | |
573 | insn->opcode |= ex.X_add_number << arg_encode[arg].pos; | |
574 | return param; | |
575 | } | |
576 | } | |
577 | return 0; | |
578 | } | |
579 | ||
580 | static const char * | |
581 | get_imm (const char *param, struct spu_insn *insn, int arg) | |
582 | { | |
583 | int val; | |
584 | char *save_ptr; | |
585 | int low = 0, high = 0; | |
586 | int reloc_i = insn->reloc_arg[0] >= 0 ? 1 : 0; | |
587 | ||
588 | if (strncmp (param, "%lo(", 4) == 0) | |
589 | { | |
590 | param += 3; | |
591 | low = 1; | |
592 | as_warn (_("Using old style, %%lo(expr), please change to PPC style, expr@l.")); | |
593 | } | |
594 | else if (strncmp (param, "%hi(", 4) == 0) | |
595 | { | |
596 | param += 3; | |
597 | high = 1; | |
598 | as_warn (_("Using old style, %%hi(expr), please change to PPC style, expr@h.")); | |
599 | } | |
600 | else if (strncmp (param, "%pic(", 5) == 0) | |
601 | { | |
602 | /* Currently we expect %pic(expr) == expr, so do nothing here. | |
603 | * i.e. for code loaded at address 0 $toc will be 0. */ | |
604 | param += 4; | |
605 | } | |
606 | ||
607 | if (*param == '$') | |
608 | { | |
609 | /* Symbols can start with $, but if this symbol matches a register | |
610 | * name, it's probably a mistake. The only way to avoid this | |
611 | * warning is to rename the symbol. */ | |
612 | struct spu_insn tmp_insn; | |
613 | const char *np = get_reg (param, &tmp_insn, arg, 0); | |
614 | ||
615 | if (np) | |
616 | syntax_error_param = np; | |
617 | } | |
618 | ||
619 | save_ptr = input_line_pointer; | |
620 | input_line_pointer = (char *) param; | |
621 | expression (&insn->exp[reloc_i]); | |
622 | param = input_line_pointer; | |
623 | input_line_pointer = save_ptr; | |
624 | ||
625 | /* Similar to ppc_elf_suffix in tc-ppc.c. We have so few cases to | |
626 | * handle we do it inlined here. */ | |
627 | if (param[0] == '@' && !ISALNUM (param[2]) && param[2] != '@') | |
628 | { | |
629 | if (param[1] == 'h' || param[1] == 'H') | |
630 | { | |
631 | high = 1; | |
632 | param += 2; | |
633 | } | |
634 | else if (param[1] == 'l' || param[1] == 'L') | |
635 | { | |
636 | low = 1; | |
637 | param += 2; | |
638 | } | |
639 | } | |
640 | ||
641 | val = insn->exp[reloc_i].X_add_number; | |
642 | ||
643 | if (insn->exp[reloc_i].X_op == O_constant) | |
644 | { | |
645 | if (emulate_apuasm) | |
646 | { | |
647 | /* Convert the value to a format we expect. */ | |
648 | val <<= arg_encode[arg].rshift; | |
649 | if (arg == A_U7A) | |
650 | val = 173 - val; | |
651 | else if (arg == A_U7B) | |
652 | val = 155 - val; | |
653 | } | |
654 | ||
655 | if (high) | |
656 | val = val >> 16; | |
657 | else if (low) | |
658 | val = val & 0xffff; | |
659 | ||
660 | /* Warn about out of range expressions. */ | |
661 | { | |
662 | int hi = arg_encode[arg].hi; | |
663 | int lo = arg_encode[arg].lo; | |
664 | int whi = arg_encode[arg].whi; | |
665 | int wlo = arg_encode[arg].wlo; | |
666 | ||
667 | if (hi > lo && (val < lo || val > hi)) | |
668 | as_fatal (_("Constant expression %d out of range, [%d, %d]."), | |
669 | val, lo, hi); | |
670 | else if (whi > wlo && (val < wlo || val > whi)) | |
671 | as_warn (_("Constant expression %d out of range, [%d, %d]."), | |
672 | val, wlo, whi); | |
673 | } | |
674 | ||
675 | if (arg == A_U7A) | |
676 | val = 173 - val; | |
677 | else if (arg == A_U7B) | |
678 | val = 155 - val; | |
679 | ||
680 | /* Branch hints have a split encoding. Do the bottom part. */ | |
681 | if (arg == A_S11 || arg == A_S11I) | |
682 | insn->opcode |= ((val >> 2) & 0x7f); | |
683 | ||
684 | insn->opcode |= (((val >> arg_encode[arg].rshift) | |
685 | & ((1 << arg_encode[arg].size) - 1)) | |
686 | << arg_encode[arg].pos); | |
687 | insn->reloc_arg[reloc_i] = -1; | |
688 | insn->flag[reloc_i] = 0; | |
689 | } | |
690 | else | |
691 | { | |
692 | insn->reloc_arg[reloc_i] = arg; | |
693 | if (high) | |
694 | insn->flag[reloc_i] |= 1; | |
695 | if (low) | |
696 | insn->flag[reloc_i] |= 2; | |
697 | } | |
698 | ||
699 | return param; | |
700 | } | |
701 | ||
702 | #define MAX_LITTLENUMS 6 | |
703 | ||
704 | /* Turn a string in input_line_pointer into a floating point constant of type | |
705 | type, and store the appropriate bytes in *litP. The number of LITTLENUMS | |
706 | emitted is stored in *sizeP . An error message is returned, or NULL on OK. | |
707 | */ | |
708 | char * | |
709 | md_atof (int type, char *litP, int *sizeP) | |
710 | { | |
711 | int prec; | |
712 | LITTLENUM_TYPE words[MAX_LITTLENUMS]; | |
713 | LITTLENUM_TYPE *wordP; | |
714 | char *t; | |
715 | ||
716 | switch (type) | |
717 | { | |
718 | case 'f': | |
719 | case 'F': | |
720 | case 's': | |
721 | case 'S': | |
722 | prec = 2; | |
723 | break; | |
724 | ||
725 | case 'd': | |
726 | case 'D': | |
727 | case 'r': | |
728 | case 'R': | |
729 | prec = 4; | |
730 | break; | |
731 | ||
732 | case 'x': | |
733 | case 'X': | |
734 | prec = 6; | |
735 | break; | |
736 | ||
737 | case 'p': | |
738 | case 'P': | |
739 | prec = 6; | |
740 | break; | |
741 | ||
742 | default: | |
743 | *sizeP = 0; | |
744 | return _("Bad call to MD_ATOF()"); | |
745 | } | |
746 | t = atof_ieee (input_line_pointer, type, words); | |
747 | if (t) | |
748 | input_line_pointer = t; | |
749 | ||
750 | *sizeP = prec * sizeof (LITTLENUM_TYPE); | |
751 | for (wordP = words; prec--;) | |
752 | { | |
753 | md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE)); | |
754 | litP += sizeof (LITTLENUM_TYPE); | |
755 | } | |
756 | return 0; | |
757 | } | |
758 | ||
759 | #ifndef WORKING_DOT_WORD | |
760 | int md_short_jump_size = 4; | |
761 | ||
762 | void | |
763 | md_create_short_jump (char *ptr, | |
764 | addressT from_addr ATTRIBUTE_UNUSED, | |
765 | addressT to_addr ATTRIBUTE_UNUSED, | |
766 | fragS *frag, | |
767 | symbolS *to_symbol) | |
768 | { | |
769 | ptr[0] = (char) 0xc0; | |
770 | ptr[1] = 0x00; | |
771 | ptr[2] = 0x00; | |
772 | ptr[3] = 0x00; | |
773 | fix_new (frag, | |
774 | ptr - frag->fr_literal, | |
775 | 4, | |
776 | to_symbol, | |
777 | (offsetT) 0, | |
778 | 0, | |
779 | BFD_RELOC_SPU_PCREL16); | |
780 | } | |
781 | ||
782 | int md_long_jump_size = 4; | |
783 | ||
784 | void | |
785 | md_create_long_jump (char *ptr, | |
786 | addressT from_addr ATTRIBUTE_UNUSED, | |
787 | addressT to_addr ATTRIBUTE_UNUSED, | |
788 | fragS *frag, | |
789 | symbolS *to_symbol) | |
790 | { | |
791 | ptr[0] = (char) 0xc0; | |
792 | ptr[1] = 0x00; | |
793 | ptr[2] = 0x00; | |
794 | ptr[3] = 0x00; | |
795 | fix_new (frag, | |
796 | ptr - frag->fr_literal, | |
797 | 4, | |
798 | to_symbol, | |
799 | (offsetT) 0, | |
800 | 0, | |
801 | BFD_RELOC_SPU_PCREL16); | |
802 | } | |
803 | #endif | |
804 | ||
805 | int | |
806 | md_estimate_size_before_relax (fragS *fragP ATTRIBUTE_UNUSED, | |
807 | segT segment_type ATTRIBUTE_UNUSED) | |
808 | { | |
809 | as_fatal (_("Relaxation should never occur")); | |
810 | return -1; | |
811 | } | |
812 | ||
813 | /* If while processing a fixup, a reloc really needs to be created, | |
814 | then it is done here. */ | |
815 | ||
816 | arelent * | |
817 | tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp) | |
818 | { | |
819 | arelent *reloc; | |
820 | reloc = (arelent *) xmalloc (sizeof (arelent)); | |
821 | reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *)); | |
822 | if (fixp->fx_addsy) | |
823 | *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy); | |
824 | else if (fixp->fx_subsy) | |
825 | *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_subsy); | |
826 | reloc->address = fixp->fx_frag->fr_address + fixp->fx_where; | |
827 | reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type); | |
828 | if (reloc->howto == (reloc_howto_type *) NULL) | |
829 | { | |
830 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
831 | _("reloc %d not supported by object file format"), | |
832 | (int) fixp->fx_r_type); | |
833 | return NULL; | |
834 | } | |
835 | reloc->addend = fixp->fx_addnumber; | |
836 | return reloc; | |
837 | } | |
838 | ||
839 | /* Round up a section's size to the appropriate boundary. */ | |
840 | ||
841 | valueT | |
842 | md_section_align (segT seg, valueT size) | |
843 | { | |
844 | int align = bfd_get_section_alignment (stdoutput, seg); | |
845 | valueT mask = ((valueT) 1 << align) - 1; | |
846 | ||
847 | return (size + mask) & ~mask; | |
848 | } | |
849 | ||
850 | /* Where a PC relative offset is calculated from. On the spu they | |
851 | are calculated from the beginning of the branch instruction. */ | |
852 | ||
853 | long | |
854 | md_pcrel_from (fixS *fixp) | |
855 | { | |
856 | return fixp->fx_frag->fr_address + fixp->fx_where; | |
857 | } | |
858 | ||
859 | /* Fill in rs_align_code fragments. */ | |
860 | ||
861 | void | |
862 | spu_handle_align (fragS *fragp) | |
863 | { | |
864 | static const unsigned char nop_pattern[8] = { | |
865 | 0x40, 0x20, 0x00, 0x00, /* even nop */ | |
866 | 0x00, 0x20, 0x00, 0x00, /* odd nop */ | |
867 | }; | |
868 | ||
869 | int bytes; | |
870 | char *p; | |
871 | ||
872 | if (fragp->fr_type != rs_align_code) | |
873 | return; | |
874 | ||
875 | bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix; | |
876 | p = fragp->fr_literal + fragp->fr_fix; | |
877 | ||
878 | if (bytes & 3) | |
879 | { | |
880 | int fix = bytes & 3; | |
881 | memset (p, 0, fix); | |
882 | p += fix; | |
883 | bytes -= fix; | |
884 | fragp->fr_fix += fix; | |
885 | } | |
886 | if (bytes & 4) | |
887 | { | |
888 | memcpy (p, &nop_pattern[4], 4); | |
889 | p += 4; | |
890 | bytes -= 4; | |
891 | fragp->fr_fix += 4; | |
892 | } | |
893 | ||
894 | memcpy (p, nop_pattern, 8); | |
895 | fragp->fr_var = 8; | |
896 | } | |
897 | ||
898 | void | |
899 | md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED) | |
900 | { | |
901 | unsigned int res; | |
902 | valueT val = *valP; | |
903 | char *place = fixP->fx_where + fixP->fx_frag->fr_literal; | |
904 | ||
905 | if (fixP->fx_subsy != (symbolS *) NULL) | |
906 | { | |
907 | /* We can't actually support subtracting a symbol. */ | |
908 | as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex")); | |
909 | } | |
910 | ||
911 | if (fixP->fx_addsy != NULL) | |
912 | { | |
913 | if (fixP->fx_pcrel) | |
914 | { | |
915 | /* Hack around bfd_install_relocation brain damage. */ | |
916 | val += fixP->fx_frag->fr_address + fixP->fx_where; | |
917 | ||
918 | switch (fixP->fx_r_type) | |
919 | { | |
920 | case BFD_RELOC_32: | |
921 | fixP->fx_r_type = BFD_RELOC_32_PCREL; | |
922 | break; | |
923 | ||
924 | case BFD_RELOC_SPU_PCREL16: | |
925 | case BFD_RELOC_SPU_PCREL9a: | |
926 | case BFD_RELOC_SPU_PCREL9b: | |
927 | case BFD_RELOC_32_PCREL: | |
928 | break; | |
929 | ||
930 | default: | |
931 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
932 | _("expression too complex")); | |
933 | break; | |
934 | } | |
935 | } | |
936 | } | |
937 | ||
938 | fixP->fx_addnumber = val; | |
939 | ||
940 | if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0) | |
941 | { | |
942 | fixP->fx_done = 1; | |
943 | res = 0; | |
840edabd | 944 | if (fixP->tc_fix_data.arg_format > A_P) |
e9f53129 | 945 | { |
840edabd AM |
946 | int hi = arg_encode[fixP->tc_fix_data.arg_format].hi; |
947 | int lo = arg_encode[fixP->tc_fix_data.arg_format].lo; | |
e9f53129 AM |
948 | if (hi > lo && ((offsetT) val < lo || (offsetT) val > hi)) |
949 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
950 | "Relocation doesn't fit. (relocation value = 0x%lx)", | |
951 | (long) val); | |
952 | } | |
953 | ||
954 | switch (fixP->fx_r_type) | |
955 | { | |
956 | case BFD_RELOC_8: | |
957 | md_number_to_chars (place, val, 1); | |
958 | return; | |
959 | ||
960 | case BFD_RELOC_16: | |
961 | md_number_to_chars (place, val, 2); | |
962 | return; | |
963 | ||
964 | case BFD_RELOC_32: | |
965 | md_number_to_chars (place, val, 4); | |
966 | return; | |
967 | ||
968 | case BFD_RELOC_64: | |
969 | md_number_to_chars (place, val, 8); | |
970 | return; | |
971 | ||
972 | case BFD_RELOC_SPU_IMM7: | |
973 | res = (val & 0x7f) << 14; | |
974 | break; | |
975 | ||
976 | case BFD_RELOC_SPU_IMM8: | |
977 | res = (val & 0xff) << 14; | |
978 | break; | |
979 | ||
980 | case BFD_RELOC_SPU_IMM10: | |
981 | res = (val & 0x3ff) << 14; | |
982 | break; | |
983 | ||
984 | case BFD_RELOC_SPU_IMM10W: | |
985 | res = (val & 0x3ff0) << 10; | |
986 | break; | |
987 | ||
988 | case BFD_RELOC_SPU_IMM16: | |
989 | res = (val & 0xffff) << 7; | |
990 | break; | |
991 | ||
992 | case BFD_RELOC_SPU_IMM16W: | |
993 | res = (val & 0x3fffc) << 5; | |
994 | break; | |
995 | ||
996 | case BFD_RELOC_SPU_IMM18: | |
997 | res = (val & 0x3ffff) << 7; | |
998 | break; | |
999 | ||
1000 | case BFD_RELOC_SPU_PCREL9a: | |
1001 | res = ((val & 0x1fc) >> 2) | ((val & 0x600) << 14); | |
1002 | break; | |
1003 | ||
1004 | case BFD_RELOC_SPU_PCREL9b: | |
1005 | res = ((val & 0x1fc) >> 2) | ((val & 0x600) << 5); | |
1006 | break; | |
1007 | ||
1008 | case BFD_RELOC_SPU_PCREL16: | |
1009 | res = (val & 0x3fffc) << 5; | |
1010 | break; | |
1011 | ||
1012 | default: | |
1013 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
1014 | _("reloc %d not supported by object file format"), | |
1015 | (int) fixP->fx_r_type); | |
1016 | } | |
1017 | ||
1018 | if (res != 0) | |
1019 | { | |
1020 | place[0] |= (res >> 24) & 0xff; | |
1021 | place[1] |= (res >> 16) & 0xff; | |
1022 | place[2] |= (res >> 8) & 0xff; | |
1023 | place[3] |= (res) & 0xff; | |
1024 | } | |
1025 | } | |
1026 | } |