tc-xgate.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. /* tc-xgate.c -- Assembler code for Freescale XGATE
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Contributed by Sean Keys <skeys@ipdatasys.com>
  4. This file is part of GAS, the GNU Assembler.
  5. GAS is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GAS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GAS; see the file COPYING. If not, write to
  15. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. #include "as.h"
  18. #include "safe-ctype.h"
  19. #include "subsegs.h"
  20. #include "opcode/xgate.h"
  21. #include "dwarf2dbg.h"
  22. #include "elf/xgate.h"
  23. const char comment_chars[] = ";!";
  24. const char line_comment_chars[] = "#*";
  25. const char line_separator_chars[] = "";
  26. const char EXP_CHARS[] = "eE";
  27. const char FLT_CHARS[] = "dD";
  28. /* Max opcodes per opcode handle. */
  29. #define MAX_OPCODES 0x05
  30. #define SIXTEENTH_BIT 0x8000
  31. #define N_BITS_IN_WORD 16
  32. #define MAX_NUM_OPERANDS 3
  33. /* #define STATE_CONDITIONAL_BRANCH (1) */
  34. #define STATE_PC_RELATIVE (2)
  35. #define REGISTER_P(ptr) (ptr == 'r')
  36. #define INCREMENT 01
  37. #define DECREMENT 02
  38. #define MAXREGISTER 07
  39. #define MINREGISTER 00
  40. #define OPTION_MMCU 'm'
  41. /* This macro has no side-effects. */
  42. #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
  43. /* Each unique opcode name has a handle. That handle may
  44. contain pointers to opcodes with the same name but
  45. different address modes. */
  46. struct xgate_opcode_handle
  47. {
  48. int number_of_modes;
  49. char *name;
  50. struct xgate_opcode *opc0[MAX_OPCODES];
  51. };
  52. /* XGATE's registers all are 16-bit general purpose.
  53. They are numbered according to the specifications. */
  54. typedef enum register_id
  55. {
  56. REG_NONE = -1,
  57. REG_R0 = 0,
  58. REG_R1 = 1,
  59. REG_R2 = 2,
  60. REG_R3 = 3,
  61. REG_R4 = 4,
  62. REG_R5 = 5,
  63. REG_R6 = 6,
  64. REG_R7 = 7,
  65. REG_PC = 8,
  66. REG_CCR = 9
  67. } register_id;
  68. /* Operand Modifiers */
  69. typedef enum op_modifiers
  70. {
  71. MOD_NONE = -1,
  72. MOD_POSTINC = 1,
  73. MOD_PREDEC = 2,
  74. MOD_CONSTANT = 3,
  75. MOD_LOAD_HIGH = 4,
  76. MOD_LOAD_LOW = 5
  77. }op_modifiers;
  78. typedef struct s_operand
  79. {
  80. expressionS exp;
  81. register_id reg;
  82. op_modifiers mod;
  83. } s_operand;
  84. /* Forward declarations. */
  85. static inline char *skip_whitespace (char *);
  86. static void get_default_target (void);
  87. static char *extract_word (char *, char *, int);
  88. static struct xgate_opcode *xgate_find_match (struct xgate_opcode_handle *,
  89. int, s_operand [], unsigned int);
  90. static int cmp_opcode (struct xgate_opcode *, struct xgate_opcode *);
  91. static void xgate_print_table (void);
  92. static unsigned int xgate_get_operands (char *, s_operand []);
  93. static register_id reg_name_search (char *);
  94. static op_modifiers xgate_determine_modifiers (char **);
  95. static void xgate_scan_operands (struct xgate_opcode *opcode, s_operand []);
  96. static unsigned int xgate_parse_operand (struct xgate_opcode *, int *, int,
  97. char **, s_operand);
  98. static htab_t xgate_hash;
  99. /* Previous opcode. */
  100. static unsigned int prev = 0;
  101. static unsigned char fixup_required = 0;
  102. /* Used to enable clipping of 16 bit operands into 8 bit constraints. */
  103. static unsigned char autoHiLo = 0;
  104. static char oper_check;
  105. static char flag_print_insn_syntax = 0;
  106. static char flag_print_opcodes = 0;
  107. static int current_architecture;
  108. static const char *default_cpu;
  109. /* ELF flags to set in the output file header. */
  110. static int elf_flags = E_XGATE_F64;
  111. /* This table describes how you change sizes for the various types of variable
  112. size expressions. This version only supports two kinds. */
  113. /* The fields are:
  114. How far Forward this mode will reach.
  115. How far Backward this mode will reach.
  116. How many bytes this mode will add to the size of the frag.
  117. Which mode to go to if the offset won't fit in this one. */
  118. relax_typeS md_relax_table[] =
  119. {
  120. {1, 1, 0, 0}, /* First entries aren't used. */
  121. {1, 1, 0, 0}, /* For no good reason except. */
  122. {1, 1, 0, 0}, /* that the VAX doesn't either. */
  123. {1, 1, 0, 0},
  124. /* XGATE 9 and 10 bit pc rel todo complete and test */
  125. /*{(511), (-512), 0, ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD)},
  126. {(1023), (-1024), 0, ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD)}, */
  127. {0, 0, 0, 0}
  128. };
  129. /* This table describes all the machine specific pseudo-ops the assembler
  130. has to support. The fields are: pseudo-op name without dot function to
  131. call to execute this pseudo-op Integer arg to pass to the function. */
  132. const pseudo_typeS md_pseudo_table[] =
  133. {
  134. /* The following pseudo-ops are supported for MRI compatibility. */
  135. {0, 0, 0}
  136. };
  137. const char *md_shortopts = "m:";
  138. struct option md_longopts[] =
  139. {
  140. #define OPTION_PRINT_INSN_SYNTAX (OPTION_MD_BASE + 0)
  141. { "print-insn-syntax", no_argument, NULL, OPTION_PRINT_INSN_SYNTAX },
  142. #define OPTION_PRINT_OPCODES (OPTION_MD_BASE + 1)
  143. { "print-opcodes", no_argument, NULL, OPTION_PRINT_OPCODES },
  144. #define OPTION_GENERATE_EXAMPLE (OPTION_MD_BASE + 2)
  145. { "generate-example", no_argument, NULL, OPTION_GENERATE_EXAMPLE },
  146. #define OPTION_MSHORT (OPTION_MD_BASE + 3)
  147. { "mshort", no_argument, NULL, OPTION_MSHORT },
  148. #define OPTION_MLONG (OPTION_MD_BASE + 4)
  149. { "mlong", no_argument, NULL, OPTION_MLONG },
  150. #define OPTION_MSHORT_DOUBLE (OPTION_MD_BASE + 5)
  151. { "mshort-double", no_argument, NULL, OPTION_MSHORT_DOUBLE },
  152. #define OPTION_MLONG_DOUBLE (OPTION_MD_BASE + 6)
  153. { "mlong-double", no_argument, NULL, OPTION_MLONG_DOUBLE },
  154. { NULL, no_argument, NULL, 0 }
  155. };
  156. size_t md_longopts_size = sizeof (md_longopts);
  157. const char *
  158. md_atof (int type, char *litP, int *sizeP)
  159. {
  160. return ieee_md_atof (type, litP, sizeP, true);
  161. }
  162. int
  163. md_parse_option (int c, const char *arg)
  164. {
  165. switch (c)
  166. {
  167. case OPTION_MMCU:
  168. if (strcasecmp (arg, "v1") == 0)
  169. current_architecture = XGATE_V1;
  170. else if (strcasecmp (arg, "v2") == 0)
  171. current_architecture = XGATE_V2;
  172. else if (strcasecmp (arg, "v3") == 0)
  173. current_architecture = XGATE_V3;
  174. else
  175. as_bad (_("architecture variant invalid"));
  176. break;
  177. case OPTION_PRINT_INSN_SYNTAX:
  178. flag_print_insn_syntax = 1;
  179. break;
  180. case OPTION_PRINT_OPCODES:
  181. flag_print_opcodes = 1;
  182. break;
  183. case OPTION_GENERATE_EXAMPLE:
  184. flag_print_opcodes = 2;
  185. break;
  186. case OPTION_MSHORT:
  187. elf_flags &= ~E_XGATE_I32;
  188. break;
  189. case OPTION_MLONG:
  190. elf_flags |= E_XGATE_I32;
  191. break;
  192. case OPTION_MSHORT_DOUBLE:
  193. elf_flags &= ~E_XGATE_F64;
  194. break;
  195. case OPTION_MLONG_DOUBLE:
  196. elf_flags |= E_XGATE_F64;
  197. break;
  198. default:
  199. return 0;
  200. }
  201. return 1;
  202. }
  203. const char *
  204. xgate_arch_format (void)
  205. {
  206. get_default_target ();
  207. if (current_architecture & cpuxgate)
  208. return "elf32-xgate";
  209. return "error";
  210. }
  211. static void
  212. get_default_target (void)
  213. {
  214. const bfd_target *target;
  215. bfd abfd;
  216. if (current_architecture != 0)
  217. return;
  218. default_cpu = "unknown";
  219. target = bfd_find_target (0, &abfd);
  220. if (target && target->name)
  221. {
  222. if (strcmp (target->name, "elf32-xgate") == 0)
  223. {
  224. current_architecture = cpuxgate;
  225. default_cpu = "XGATE V1";
  226. return;
  227. }
  228. as_bad (_("Default target `%s' is not supported."), target->name);
  229. }
  230. }
  231. void
  232. md_begin (void)
  233. {
  234. struct xgate_opcode *xgate_opcode_ptr = NULL;
  235. struct xgate_opcode *xgate_op_table = NULL;
  236. struct xgate_opcode_handle *op_handles = 0;
  237. const char *prev_op_name = 0;
  238. int handle_enum = 0;
  239. int number_of_op_handles = 0;
  240. int i, j = 0;
  241. /* Create a local copy of our opcode table
  242. including an extra line for NULL termination. */
  243. xgate_op_table = XNEWVEC (struct xgate_opcode, xgate_num_opcodes);
  244. memset (xgate_op_table, 0,
  245. sizeof (struct xgate_opcode) * (xgate_num_opcodes));
  246. for (xgate_opcode_ptr = (struct xgate_opcode*) xgate_opcodes, i = 0;
  247. i < xgate_num_opcodes; i++)
  248. xgate_op_table[i] = xgate_opcode_ptr[i];
  249. qsort (xgate_op_table, xgate_num_opcodes, sizeof (struct xgate_opcode),
  250. (int (*)(const void *, const void *)) cmp_opcode);
  251. /* Calculate number of handles since this will be
  252. smaller than the raw number of opcodes in the table. */
  253. prev_op_name = "";
  254. for (xgate_opcode_ptr = xgate_op_table, i = 0; i < xgate_num_opcodes;
  255. xgate_opcode_ptr++, i++)
  256. {
  257. if (strcmp (prev_op_name, xgate_opcode_ptr->name))
  258. number_of_op_handles++;
  259. prev_op_name = xgate_opcode_ptr->name;
  260. }
  261. op_handles = XNEWVEC (struct xgate_opcode_handle, number_of_op_handles);
  262. /* Insert unique opcode names into hash table, aliasing duplicates. */
  263. xgate_hash = str_htab_create ();
  264. prev_op_name = "";
  265. for (xgate_opcode_ptr = xgate_op_table, i = 0, j = 0; i < xgate_num_opcodes;
  266. i++, xgate_opcode_ptr++)
  267. {
  268. if (!strcmp (prev_op_name, xgate_opcode_ptr->name))
  269. {
  270. handle_enum++;
  271. op_handles[j].opc0[handle_enum] = xgate_opcode_ptr;
  272. }
  273. else
  274. {
  275. handle_enum = 0;
  276. if (i)
  277. j++;
  278. op_handles[j].name = xgate_opcode_ptr->name;
  279. op_handles[j].opc0[0] = xgate_opcode_ptr;
  280. str_hash_insert (xgate_hash, op_handles[j].name, &op_handles[j], 0);
  281. }
  282. op_handles[j].number_of_modes = handle_enum;
  283. prev_op_name = op_handles[j].name;
  284. }
  285. if (flag_print_opcodes)
  286. {
  287. xgate_print_table ();
  288. exit (EXIT_SUCCESS);
  289. }
  290. }
  291. void
  292. xgate_init_after_args (void)
  293. {
  294. }
  295. void
  296. md_show_usage (FILE * stream)
  297. {
  298. get_default_target ();
  299. fprintf (stream,
  300. _("\
  301. Freescale XGATE co-processor options:\n\
  302. -mshort use 16-bit int ABI (default)\n\
  303. -mlong use 32-bit int ABI\n\
  304. -mshort-double use 32-bit double ABI\n\
  305. -mlong-double use 64-bit double ABI (default)\n\
  306. --mxgate specify the processor variant [default %s]\n\
  307. --print-insn-syntax print the syntax of instruction in case of error\n\
  308. --print-opcodes print the list of instructions with syntax\n\
  309. --generate-example generate an example of each instruction"),
  310. default_cpu);
  311. }
  312. enum bfd_architecture
  313. xgate_arch (void)
  314. {
  315. get_default_target ();
  316. return bfd_arch_xgate;
  317. }
  318. int
  319. xgate_mach (void)
  320. {
  321. return 0;
  322. }
  323. static void
  324. xgate_print_syntax (char *name)
  325. {
  326. int i;
  327. for (i = 0; i < xgate_num_opcodes; i++)
  328. {
  329. if (!strcmp (xgate_opcodes[i].name, name))
  330. {
  331. if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_IDR))
  332. printf ("\tFormat is %s\tRx, Rx, Rx+|-Rx|Rx\n",
  333. xgate_opcodes[i].name);
  334. if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_INH))
  335. printf ("\tFormat is %s\n", xgate_opcodes[i].name);
  336. if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_TRI))
  337. printf ("\tFormat is %s\tRx, Rx, Rx\n", xgate_opcodes[i].name);
  338. if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_DYA))
  339. printf ("\tFormat is %s\tRx, Rx\n", xgate_opcodes[i].name);
  340. if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_IMM3))
  341. printf ("\tFormat is %s\t<3-bit value>\n", xgate_opcodes[i].name);
  342. if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_IMM4))
  343. printf ("\tFormat is %s\t<4 -bit value>\n", xgate_opcodes[i].name);
  344. if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_IMM8))
  345. printf ("\tFormat is %s\tRx, <8-bit value>\n",
  346. xgate_opcodes[i].name);
  347. if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_IMM16))
  348. printf ("\tFormat is %s\tRx, <16-bit value>\n",
  349. xgate_opcodes[i].name);
  350. if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_MON_R_C))
  351. printf ("\tFormat is %s\tRx, CCR\n", xgate_opcodes[i].name);
  352. if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_MON_C_R))
  353. printf ("\tFormat is %s\tCCR, Rx\n", xgate_opcodes[i].name);
  354. if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_MON_R_P))
  355. printf ("\tFormat is %s\tRx, PC\n", xgate_opcodes[i].name);
  356. if (!strcmp (xgate_opcodes[i].constraints, XGATE_OP_IMM16mLDW))
  357. printf ("\tFormat is %s\tRx, <16-bit value>\n",
  358. xgate_opcodes[i].name);
  359. }
  360. }
  361. }
  362. static void
  363. xgate_print_table (void)
  364. {
  365. int i;
  366. for (i = 0; i < xgate_num_opcodes; i++)
  367. xgate_print_syntax (xgate_opcodes[i].name);
  368. return;
  369. }
  370. const char *
  371. xgate_listing_header (void)
  372. {
  373. if (current_architecture & cpuxgate)
  374. return "XGATE GAS ";
  375. return "ERROR MC9S12X GAS ";
  376. }
  377. symbolS *
  378. md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
  379. {
  380. return NULL;
  381. }
  382. /* GAS will call this function for each section at the end of the assembly,
  383. to permit the CPU backend to adjust the alignment of a section. */
  384. valueT
  385. md_section_align (asection * seg, valueT addr)
  386. {
  387. int align = bfd_section_alignment (seg);
  388. return ((addr + (1 << align) - 1) & -(1 << align));
  389. }
  390. void
  391. md_assemble (char *input_line)
  392. {
  393. struct xgate_opcode *opcode = 0;
  394. struct xgate_opcode *macro_opcode = 0;
  395. struct xgate_opcode_handle *opcode_handle = 0;
  396. /* Caller expects it to be returned as it was passed. */
  397. char *saved_input_line = input_line;
  398. char op_name[9] = { 0 };
  399. unsigned int operandCount = 0;
  400. char *p = 0;
  401. s_operand new_operands[MAX_NUM_OPERANDS];
  402. fixup_required = 0;
  403. oper_check = 0; /* set error flags */
  404. input_line = extract_word (input_line, op_name, sizeof (op_name));
  405. /* Check to make sure we are not reading a bogus line. */
  406. if (!op_name[0])
  407. as_bad (_("opcode missing or not found on input line"));
  408. opcode_handle = (struct xgate_opcode_handle *) str_hash_find (xgate_hash,
  409. op_name);
  410. if (!opcode_handle)
  411. as_bad (_("opcode %s not found in opcode hash table"), op_name);
  412. else
  413. {
  414. /* Parse operands so we can find the proper opcode bin. */
  415. operandCount = xgate_get_operands (input_line, new_operands);
  416. opcode = xgate_find_match (opcode_handle, opcode_handle->number_of_modes,
  417. new_operands, operandCount);
  418. if (!opcode)
  419. {
  420. as_bad (_("matching operands to opcode"));
  421. xgate_print_syntax (opcode_handle->opc0[0]->name);
  422. }
  423. else if (opcode->size == 2)
  424. {
  425. /* Size is one word - assemble that native insn. */
  426. xgate_scan_operands (opcode, new_operands);
  427. }
  428. else
  429. {
  430. /* Insn is a simplified instruction - expand it out. */
  431. autoHiLo = 1;
  432. unsigned int i;
  433. /* skip past our ';' separator. */
  434. for (i = strlen (opcode->constraints), p = opcode->constraints; i > 0;
  435. i--, p++)
  436. {
  437. if (*p == ';')
  438. {
  439. p++;
  440. break;
  441. }
  442. }
  443. input_line = skip_whitespace (input_line);
  444. char *macro_inline = input_line;
  445. /* Loop though the macro's opcode list and apply operands to
  446. each real opcode. */
  447. for (i = 0; *p && i < (opcode->size / 2); i++)
  448. {
  449. /* Loop though macro operand list. */
  450. input_line = macro_inline; /* Rewind. */
  451. p = extract_word (p, op_name, 10);
  452. opcode_handle
  453. = (struct xgate_opcode_handle *) str_hash_find (xgate_hash,
  454. op_name);
  455. if (!opcode_handle)
  456. {
  457. as_bad (_(": processing macro, real opcode handle"
  458. " not found in hash"));
  459. break;
  460. }
  461. else
  462. {
  463. operandCount = xgate_get_operands (input_line, new_operands);
  464. macro_opcode = xgate_find_match (opcode_handle,
  465. opcode_handle->number_of_modes, new_operands,
  466. operandCount);
  467. xgate_scan_operands (macro_opcode, new_operands);
  468. }
  469. }
  470. }
  471. }
  472. autoHiLo = 0;
  473. input_line = saved_input_line;
  474. }
  475. /* Force truly undefined symbols to their maximum size, and generally set up
  476. the frag list to be relaxed. */
  477. int
  478. md_estimate_size_before_relax (fragS *fragp, asection *seg)
  479. {
  480. /* If symbol is undefined or located in a different section,
  481. select the largest supported relocation. */
  482. relax_substateT subtype;
  483. relax_substateT rlx_state[] = { 0, 2 };
  484. for (subtype = 0; subtype < ARRAY_SIZE (rlx_state); subtype += 2)
  485. {
  486. if (fragp->fr_subtype == rlx_state[subtype]
  487. && (!S_IS_DEFINED (fragp->fr_symbol)
  488. || seg != S_GET_SEGMENT (fragp->fr_symbol)))
  489. {
  490. fragp->fr_subtype = rlx_state[subtype + 1];
  491. break;
  492. }
  493. }
  494. if (fragp->fr_subtype >= ARRAY_SIZE (md_relax_table))
  495. abort ();
  496. return md_relax_table[fragp->fr_subtype].rlx_length;
  497. }
  498. /* Relocation, relaxation and frag conversions. */
  499. /* PC-relative offsets are relative to the start of the
  500. next instruction. That is, the address of the offset, plus its
  501. size, since the offset is always the last part of the insn. */
  502. long
  503. md_pcrel_from (fixS * fixP)
  504. {
  505. return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
  506. }
  507. /* If while processing a fixup, a reloc really needs to be created
  508. then it is done here. */
  509. arelent *
  510. tc_gen_reloc (asection * section ATTRIBUTE_UNUSED, fixS * fixp)
  511. {
  512. arelent * reloc;
  513. reloc = XNEW (arelent);
  514. reloc->sym_ptr_ptr = XNEW (asymbol *);
  515. *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
  516. reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
  517. if (fixp->fx_r_type == 0)
  518. reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
  519. else
  520. reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
  521. if (reloc->howto == (reloc_howto_type *) NULL)
  522. {
  523. as_bad_where (fixp->fx_file, fixp->fx_line, _
  524. ("Relocation %d is not supported by object file format."),
  525. (int) fixp->fx_r_type);
  526. return NULL;
  527. }
  528. /* Since we use Rel instead of Rela, encode the vtable entry to be
  529. used in the relocation's section offset. */
  530. if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
  531. reloc->address = fixp->fx_offset;
  532. reloc->addend = 0;
  533. return reloc;
  534. }
  535. /* Patch the instruction with the resolved operand. Elf relocation
  536. info will also be generated to take care of linker/loader fixups.
  537. The XGATE addresses only 16-bit addresses.The BFD_RELOC_32 is necessary
  538. for the support of --gstabs. */
  539. void
  540. md_apply_fix (fixS * fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
  541. {
  542. char *where;
  543. long value = *valP;
  544. int opcode = 0;
  545. ldiv_t result;
  546. /* If the fixup is done mark it done so no further symbol resolution
  547. will take place. */
  548. if (fixP->fx_addsy == (symbolS *) NULL)
  549. fixP->fx_done = 1;
  550. /* We don't actually support subtracting a symbol. */
  551. if (fixP->fx_subsy != (symbolS *) NULL)
  552. as_bad_subtract (fixP);
  553. where = fixP->fx_frag->fr_literal + fixP->fx_where;
  554. opcode = bfd_getl16 (where);
  555. int mask = 0;
  556. switch (fixP->fx_r_type)
  557. {
  558. case BFD_RELOC_XGATE_PCREL_9:
  559. if (value < -512 || value > 511)
  560. as_bad_where (fixP->fx_file, fixP->fx_line,
  561. _("Value %ld too large for 9-bit PC-relative branch."),
  562. value);
  563. result = ldiv (value, 2); /* from bytes to words */
  564. value = result.quot;
  565. if (result.rem)
  566. as_bad_where (fixP->fx_file, fixP->fx_line, _
  567. ("Value %ld not aligned by 2 for 9-bit"
  568. " PC-relative branch."), value);
  569. /* Clip into 8-bit field.
  570. FIXME I'm sure there is a more proper place for this. */
  571. mask = 0x1FF;
  572. value &= mask;
  573. number_to_chars_bigendian (where, (opcode | value), 2);
  574. break;
  575. case BFD_RELOC_XGATE_PCREL_10:
  576. if (value < -1024 || value > 1023)
  577. as_bad_where (fixP->fx_file, fixP->fx_line,
  578. _("Value %ld too large for 10-bit PC-relative branch."),
  579. value);
  580. result = ldiv (value, 2); /* from bytes to words */
  581. value = result.quot;
  582. if (result.rem)
  583. as_bad_where (fixP->fx_file, fixP->fx_line, _
  584. ("Value %ld not aligned by 2 for 10-bit"
  585. " PC-relative branch."), value);
  586. /* Clip into 9-bit field.
  587. FIXME I'm sure there is a more proper place for this. */
  588. mask = 0x3FF;
  589. value &= mask;
  590. number_to_chars_bigendian (where, (opcode | value), 2);
  591. break;
  592. case BFD_RELOC_XGATE_IMM8_HI:
  593. if (value < -65537 || value > 65535)
  594. as_bad_where (fixP->fx_file, fixP->fx_line,
  595. _("Value out of 16-bit range."));
  596. value >>= 8;
  597. value &= 0x00ff;
  598. bfd_putb16 ((bfd_vma) value | opcode, (void *) where);
  599. break;
  600. case BFD_RELOC_XGATE_24:
  601. case BFD_RELOC_XGATE_IMM8_LO:
  602. if (value < -65537 || value > 65535)
  603. as_bad_where (fixP->fx_file, fixP->fx_line,
  604. _("Value out of 16-bit range."));
  605. value &= 0x00ff;
  606. bfd_putb16 ((bfd_vma) value | opcode, (void *) where);
  607. break;
  608. case BFD_RELOC_XGATE_IMM3:
  609. if (value < 0 || value > 7)
  610. as_bad_where (fixP->fx_file, fixP->fx_line,
  611. _("Value out of 3-bit range."));
  612. value <<= 8; /* make big endian */
  613. number_to_chars_bigendian (where, (opcode | value), 2);
  614. break;
  615. case BFD_RELOC_XGATE_IMM4:
  616. if (value < 0 || value > 15)
  617. as_bad_where (fixP->fx_file, fixP->fx_line,
  618. _("Value out of 4-bit range."));
  619. value <<= 4; /* align the operand bits */
  620. number_to_chars_bigendian (where, (opcode | value), 2);
  621. break;
  622. case BFD_RELOC_XGATE_IMM5:
  623. if (value < 0 || value > 31)
  624. as_bad_where (fixP->fx_file, fixP->fx_line,
  625. _("Value out of 5-bit range."));
  626. value <<= 5; /* align the operand bits */
  627. number_to_chars_bigendian (where, (opcode | value), 2);
  628. break;
  629. case BFD_RELOC_8:
  630. ((bfd_byte *) where)[0] = (bfd_byte) value;
  631. break;
  632. case BFD_RELOC_32:
  633. bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
  634. break;
  635. case BFD_RELOC_16:
  636. bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
  637. break;
  638. default:
  639. as_fatal (_("Line %d: unknown relocation type: 0x%x."), fixP->fx_line,
  640. fixP->fx_r_type);
  641. break;
  642. }
  643. }
  644. /* See whether we need to force a relocation into the output file. */
  645. int
  646. tc_xgate_force_relocation (fixS * fixP)
  647. {
  648. if (fixP->fx_r_type == BFD_RELOC_XGATE_RL_GROUP)
  649. return 1;
  650. return generic_force_reloc (fixP);
  651. }
  652. /* Here we decide which fixups can be adjusted to make them relative
  653. to the beginning of the section instead of the symbol. Basically
  654. we need to make sure that the linker relaxation is done
  655. correctly, so in some cases we force the original symbol to be
  656. used. */
  657. int
  658. tc_xgate_fix_adjustable (fixS * fixP)
  659. {
  660. switch (fixP->fx_r_type)
  661. {
  662. /* For the linker relaxation to work correctly, these relocs
  663. need to be on the symbol itself. */
  664. case BFD_RELOC_16:
  665. case BFD_RELOC_XGATE_RL_JUMP:
  666. case BFD_RELOC_XGATE_RL_GROUP:
  667. case BFD_RELOC_VTABLE_INHERIT:
  668. case BFD_RELOC_VTABLE_ENTRY:
  669. case BFD_RELOC_32:
  670. return 0;
  671. default:
  672. return 1;
  673. }
  674. }
  675. void
  676. md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED,
  677. asection * sec ATTRIBUTE_UNUSED,
  678. fragS * fragP ATTRIBUTE_UNUSED)
  679. {
  680. as_bad (("md_convert_frag not implemented yet"));
  681. abort ();
  682. }
  683. /* Set the ELF specific flags. */
  684. void
  685. xgate_elf_final_processing (void)
  686. {
  687. elf_flags |= EF_XGATE_MACH;
  688. elf_elfheader (stdoutput)->e_flags &= ~EF_XGATE_ABI;
  689. elf_elfheader (stdoutput)->e_flags |= elf_flags;
  690. }
  691. static inline char *
  692. skip_whitespace (char *s)
  693. {
  694. while (*s == ' ' || *s == '\t' || *s == '(' || *s == ')')
  695. s++;
  696. return s;
  697. }
  698. /* Extract a word (continuous alpha-numeric chars) from the input line. */
  699. static char *
  700. extract_word (char *from, char *to, int limit)
  701. {
  702. char *op_end;
  703. int size = 0;
  704. /* Drop leading whitespace. */
  705. from = skip_whitespace (from);
  706. *to = 0;
  707. /* Find the op code end. */
  708. for (op_end = from; *op_end != 0 && is_part_of_name (*op_end);)
  709. {
  710. to[size++] = *op_end++;
  711. if (size + 1 >= limit)
  712. break;
  713. }
  714. to[size] = 0;
  715. return op_end;
  716. }
  717. static char *
  718. xgate_new_instruction (int size)
  719. {
  720. char *f = frag_more (size);
  721. dwarf2_emit_insn (size);
  722. return f;
  723. }
  724. static unsigned short
  725. xgate_apply_operand (unsigned short new_mask,
  726. unsigned short *availiable_mask_bits,
  727. unsigned short mask,
  728. unsigned char n_bits)
  729. {
  730. unsigned short n_shifts;
  731. unsigned int n_drop_bits;
  732. /* Shift until you find an available operand bit "1" and record
  733. the number of shifts. */
  734. for (n_shifts = 0;
  735. !(*availiable_mask_bits & SIXTEENTH_BIT) && n_shifts < 16;
  736. n_shifts++)
  737. *availiable_mask_bits <<= 1;
  738. /* Shift for the number of bits your operand requires while bits
  739. are available. */
  740. for (n_drop_bits = n_bits;
  741. n_drop_bits && (*availiable_mask_bits & SIXTEENTH_BIT);
  742. --n_drop_bits)
  743. *availiable_mask_bits <<= 1;
  744. if (n_drop_bits)
  745. as_bad (_(":operand has too many bits"));
  746. *availiable_mask_bits >>= n_shifts + n_bits;
  747. if ((n_drop_bits == 0) && (*availiable_mask_bits == 0))
  748. {
  749. oper_check = 1; /* flag operand check as good */
  750. }
  751. new_mask <<= N_BITS_IN_WORD - (n_shifts + n_bits);
  752. mask |= new_mask;
  753. return mask;
  754. }
  755. /* Parse ordinary expression. */
  756. static char *
  757. xgate_parse_exp (char *s, expressionS * op)
  758. {
  759. input_line_pointer = s;
  760. expression (op);
  761. if (op->X_op == O_absent)
  762. as_bad (_("missing operand"));
  763. return input_line_pointer;
  764. }
  765. static int
  766. cmp_opcode (struct xgate_opcode *op1, struct xgate_opcode *op2)
  767. {
  768. return strcmp (op1->name, op2->name);
  769. }
  770. static struct xgate_opcode *
  771. xgate_find_match (struct xgate_opcode_handle *opcode_handle,
  772. int numberOfModes, s_operand oprs[], unsigned int operandCount)
  773. {
  774. int i;
  775. if (numberOfModes == 0)
  776. return opcode_handle->opc0[0];
  777. for (i = 0; i <= numberOfModes; i++)
  778. {
  779. switch (operandCount)
  780. {
  781. case 0:
  782. if (!strcmp (opcode_handle->opc0[i]->constraints, XGATE_OP_INH))
  783. return opcode_handle->opc0[i];
  784. break;
  785. case 1:
  786. if (oprs[0].reg >= REG_R0 && oprs[0].reg <= REG_R7)
  787. {
  788. if (!strcmp (opcode_handle->opc0[i]->constraints, XGATE_OP_MON))
  789. return opcode_handle->opc0[i];
  790. if (!strcmp (opcode_handle->opc0[i]->constraints, XGATE_OP_DYA_MON))
  791. return opcode_handle->opc0[i];
  792. }
  793. if (oprs[0].reg == REG_NONE)
  794. if (!strcmp (opcode_handle->opc0[i]->constraints, XGATE_OP_IMM3))
  795. return opcode_handle->opc0[i];
  796. break;
  797. case 2:
  798. if (oprs[0].reg >= REG_R0 && oprs[0].reg <= REG_R7)
  799. {
  800. if (oprs[1].reg >= REG_R0 && oprs[1].reg <= REG_R7)
  801. {
  802. if (!strcmp (opcode_handle->opc0[i]->constraints, XGATE_OP_DYA))
  803. return opcode_handle->opc0[i];
  804. }
  805. if (oprs[1].reg == REG_CCR)
  806. if (!strcmp (opcode_handle->opc0[i]->constraints,
  807. XGATE_OP_MON_R_C))
  808. return opcode_handle->opc0[i];
  809. if (oprs[1].reg == REG_PC)
  810. if (!strcmp (opcode_handle->opc0[i]->constraints,
  811. XGATE_OP_MON_R_P))
  812. return opcode_handle->opc0[i];
  813. if (oprs[1].reg == REG_NONE)
  814. if (!strcmp (opcode_handle->opc0[i]->constraints, XGATE_OP_IMM16)
  815. || !strcmp (opcode_handle->opc0[i]->constraints, XGATE_OP_IMM8)
  816. || !strcmp (opcode_handle->opc0[i]->constraints, XGATE_OP_IMM4)
  817. || !strcmp (opcode_handle->opc0[i]->constraints,
  818. XGATE_OP_IMM16mADD)
  819. || !strcmp (opcode_handle->opc0[i]->constraints,
  820. XGATE_OP_IMM16mAND)
  821. || !strcmp (opcode_handle->opc0[i]->constraints,
  822. XGATE_OP_IMM16mCPC)
  823. || !strcmp (opcode_handle->opc0[i]->constraints,
  824. XGATE_OP_IMM16mSUB)
  825. || !strcmp (opcode_handle->opc0[i]->constraints,
  826. XGATE_OP_IMM16mLDW))
  827. return opcode_handle->opc0[i];
  828. }
  829. if (oprs[0].reg == REG_CCR)
  830. if (!strcmp (opcode_handle->opc0[i]->constraints, XGATE_OP_MON_C_R))
  831. return opcode_handle->opc0[i];
  832. break;
  833. case 3:
  834. if (oprs[0].reg >= REG_R0 && oprs[0].reg <= REG_R7)
  835. {
  836. if (oprs[1].reg >= REG_R0 && oprs[1].reg <= REG_R7)
  837. {
  838. if (oprs[2].reg >= REG_R0 && oprs[2].reg <= REG_R7)
  839. {
  840. if (!strcmp (opcode_handle->opc0[i]->constraints,
  841. XGATE_OP_IDR)
  842. || !strcmp (opcode_handle->opc0[i]->constraints,
  843. XGATE_OP_TRI))
  844. return opcode_handle->opc0[i];
  845. }
  846. if (oprs[2].reg == REG_NONE)
  847. if (!strcmp (opcode_handle->opc0[i]->constraints,
  848. XGATE_OP_IDO5))
  849. return opcode_handle->opc0[i];
  850. }
  851. }
  852. break;
  853. default:
  854. as_bad (_("unknown operand count"));
  855. break;
  856. }
  857. }
  858. return NULL ;
  859. }
  860. /* Because we are dealing with two different core that view the system
  861. memory with different offsets, we must differentiate what core a
  862. symbol belongs to, in order for the linker to cross-link. */
  863. int
  864. xgate_frob_symbol (symbolS *sym)
  865. {
  866. asymbol *bfdsym;
  867. elf_symbol_type *elfsym;
  868. bfdsym = symbol_get_bfdsym (sym);
  869. elfsym = elf_symbol_from (bfdsym);
  870. gas_assert (elfsym);
  871. /* Mark the symbol as being *from XGATE */
  872. elfsym->internal_elf_sym.st_target_internal = 1;
  873. return 0;
  874. }
  875. static unsigned int
  876. xgate_get_operands (char *line, s_operand oprs[])
  877. {
  878. int num_operands;
  879. /* If there are no operands, then it must be inherent. */
  880. if (*line == 0 || *line == '\n' || *line == '\r')
  881. return 0;
  882. for (num_operands = 0; strlen (line) && (num_operands < MAX_NUM_OPERANDS);
  883. num_operands++)
  884. {
  885. line = skip_whitespace (line);
  886. if (*line == '#')
  887. line++;
  888. oprs[num_operands].mod = xgate_determine_modifiers (&line);
  889. if ((oprs[num_operands].reg = reg_name_search (line)) == REG_NONE)
  890. line = xgate_parse_exp (line, &oprs[num_operands].exp);
  891. /* skip to next operand */
  892. while (*line != 0)
  893. {
  894. if (*line == ',')
  895. {
  896. line++;
  897. break;
  898. }
  899. line++;
  900. }
  901. }
  902. if (num_operands > MAX_NUM_OPERANDS)
  903. return 0;
  904. return num_operands;
  905. }
  906. /* reg_name_search() finds the register number given its name.
  907. Returns the register number or REG_NONE on failure. */
  908. static register_id
  909. reg_name_search (char *name)
  910. {
  911. if (strncasecmp (name, "r0", 2) == 0)
  912. return REG_R0;
  913. if (strncasecmp (name, "r1", 2) == 0)
  914. return REG_R1;
  915. if (strncasecmp (name, "r2", 2) == 0)
  916. return REG_R2;
  917. if (strncasecmp (name, "r3", 2) == 0)
  918. return REG_R3;
  919. if (strncasecmp (name, "r4", 2) == 0)
  920. return REG_R4;
  921. if (strncasecmp (name, "r5", 2) == 0)
  922. return REG_R5;
  923. if (strncasecmp (name, "r6", 2) == 0)
  924. return REG_R6;
  925. if (strncasecmp (name, "r7", 2) == 0)
  926. return REG_R7;
  927. if (strncasecmp (name, "pc", 2) == 0)
  928. return REG_PC;
  929. if (strncasecmp (name, "ccr", 3) == 0)
  930. return REG_CCR;
  931. return REG_NONE;
  932. }
  933. /* Parse operand modifiers such as inc/dec/hi/low. */
  934. static op_modifiers
  935. xgate_determine_modifiers (char **line)
  936. {
  937. char *local_line = line[0];
  938. if (strncasecmp (local_line, "%hi", 3) == 0)
  939. {
  940. *line += 3;
  941. return MOD_LOAD_HIGH;
  942. }
  943. if (strncasecmp (local_line, "%lo", 3) == 0)
  944. {
  945. *line += 3;
  946. return MOD_LOAD_LOW;
  947. }
  948. if (*(local_line + 2) == '+')
  949. return MOD_POSTINC;
  950. if (strncasecmp (local_line, "-r", 2) == 0)
  951. {
  952. *line += 1;
  953. return MOD_PREDEC;
  954. }
  955. return MOD_NONE;
  956. }
  957. /* Parse instruction operands. */
  958. static void
  959. xgate_scan_operands (struct xgate_opcode *opcode, s_operand oprs[])
  960. {
  961. char *frag = xgate_new_instruction (opcode->size);
  962. int where = frag - frag_now->fr_literal;
  963. char *op = opcode->constraints;
  964. unsigned int bin = (int) opcode->bin_opcode;
  965. unsigned short oper_mask = 0;
  966. int operand_bit_length = 0;
  967. unsigned int operand = 0;
  968. char n_operand_bits = 0;
  969. char first_operand_equals_second = 0;
  970. int i = 0;
  971. char c = 0;
  972. /* Generate available operand bits mask. */
  973. for (i = 0; (c = opcode->format[i]); i++)
  974. {
  975. if (ISDIGIT (c) || (c == 's'))
  976. {
  977. oper_mask <<= 1;
  978. }
  979. else
  980. {
  981. oper_mask <<= 1;
  982. oper_mask += 1;
  983. n_operand_bits++;
  984. }
  985. }
  986. /* Parse first operand. */
  987. if (*op)
  988. {
  989. if (*op == '=')
  990. {
  991. first_operand_equals_second = 1;
  992. ++op;
  993. }
  994. operand = xgate_parse_operand (opcode, &operand_bit_length, where,
  995. &op, oprs[0]);
  996. ++op;
  997. bin = xgate_apply_operand (operand, &oper_mask, bin, operand_bit_length);
  998. if (first_operand_equals_second)
  999. bin = xgate_apply_operand (operand, &oper_mask, bin,
  1000. operand_bit_length);
  1001. /* Parse second operand. */
  1002. if (*op)
  1003. {
  1004. if (*op == ',')
  1005. ++op;
  1006. if (first_operand_equals_second)
  1007. {
  1008. bin = xgate_apply_operand (operand, &oper_mask, bin,
  1009. operand_bit_length);
  1010. ++op;
  1011. }
  1012. else
  1013. {
  1014. operand = xgate_parse_operand (opcode, &operand_bit_length, where,
  1015. &op, oprs[1]);
  1016. bin = xgate_apply_operand (operand, &oper_mask, bin,
  1017. operand_bit_length);
  1018. ++op;
  1019. }
  1020. }
  1021. /* Parse the third register. */
  1022. if (*op)
  1023. {
  1024. if (*op == ',')
  1025. ++op;
  1026. operand = xgate_parse_operand (opcode, &operand_bit_length, where,
  1027. &op, oprs[2]);
  1028. bin = xgate_apply_operand (operand, &oper_mask, bin,
  1029. operand_bit_length);
  1030. }
  1031. }
  1032. if (opcode->size == 2 && fixup_required)
  1033. {
  1034. bfd_putl16 (bin, frag);
  1035. }
  1036. else if ( !strcmp (opcode->constraints, XGATE_OP_REL9)
  1037. || !strcmp (opcode->constraints, XGATE_OP_REL10))
  1038. {
  1039. /* Write our data to a frag for further processing. */
  1040. bfd_putl16 (opcode->bin_opcode, frag);
  1041. }
  1042. else
  1043. {
  1044. /* Apply operand mask(s)to bin opcode and write the output. */
  1045. /* Since we are done write this frag in xgate BE format. */
  1046. number_to_chars_bigendian (frag, bin, opcode->size);
  1047. }
  1048. prev = bin;
  1049. return;
  1050. }
  1051. static unsigned int
  1052. xgate_parse_operand (struct xgate_opcode *opcode,
  1053. int *bit_width,
  1054. int where,
  1055. char **op_con,
  1056. s_operand operand)
  1057. {
  1058. char *op_constraint = *op_con;
  1059. unsigned int op_mask = 0;
  1060. unsigned int pp_fix = 0;
  1061. unsigned short max_size = 0;
  1062. int i;
  1063. *bit_width = 0;
  1064. /* Reset. */
  1065. switch (*op_constraint)
  1066. {
  1067. case '+': /* Indexed register operand +/- or plain r. */
  1068. /* Default to neither inc or dec. */
  1069. pp_fix = 0;
  1070. *bit_width = 5;
  1071. if (operand.reg == REG_NONE)
  1072. as_bad (_(": expected register name r0-r7 ") );
  1073. op_mask = operand.reg;
  1074. if (operand.mod == MOD_POSTINC)
  1075. pp_fix = INCREMENT;
  1076. if (operand.mod == MOD_PREDEC)
  1077. pp_fix = DECREMENT;
  1078. op_mask <<= 2;
  1079. op_mask |= pp_fix;
  1080. break;
  1081. case 'r': /* Register operand. */
  1082. if (operand.reg == REG_NONE)
  1083. as_bad (_(": expected register name r0-r7 "));
  1084. *bit_width = 3;
  1085. op_mask = operand.reg;
  1086. break;
  1087. case 'i': /* Immediate value or expression expected. */
  1088. /* Advance the original format pointer. */
  1089. (*op_con)++;
  1090. op_constraint++;
  1091. if (ISDIGIT (*op_constraint))
  1092. *bit_width = (int) *op_constraint - '0';
  1093. else if (*op_constraint == 'a')
  1094. *bit_width = 0x0A;
  1095. else if (*op_constraint == 'f')
  1096. *bit_width = 0x0F;
  1097. /* http://tigcc.ticalc.org/doc/gnuasm.html#SEC31 */
  1098. if (operand.exp.X_op == O_constant)
  1099. {
  1100. op_mask = operand.exp.X_add_number;
  1101. if (((opcode->name[strlen (opcode->name) - 1] == 'l') && autoHiLo)
  1102. || operand.mod == MOD_LOAD_LOW)
  1103. op_mask &= 0x00FF;
  1104. else if (((opcode->name[strlen (opcode->name) - 1]) == 'h'
  1105. && autoHiLo) || operand.mod == MOD_LOAD_HIGH)
  1106. op_mask >>= 8;
  1107. /* Make sure it fits. */
  1108. for (i = *bit_width; i; i--)
  1109. {
  1110. max_size <<= 1;
  1111. max_size += 1;
  1112. }
  1113. if (op_mask > max_size)
  1114. as_bad (_(":operand value(%d) too big for constraint"), op_mask);
  1115. }
  1116. else
  1117. {
  1118. /* Should be BFD_RELOC_XGATE_IMM8_LO instead of BFD_RELOC_XGATE_24
  1119. TODO fix. */
  1120. fixup_required = 1;
  1121. if (*op_constraint == '8')
  1122. {
  1123. if (((opcode->name[strlen (opcode->name) - 1] == 'l')
  1124. && autoHiLo) || operand.mod == MOD_LOAD_LOW)
  1125. fix_new_exp (frag_now, where, 2, &operand.exp, false,
  1126. BFD_RELOC_XGATE_24);
  1127. else if (((opcode->name[strlen (opcode->name) - 1]) == 'h'
  1128. && autoHiLo) || operand.mod == MOD_LOAD_HIGH )
  1129. fix_new_exp (frag_now, where, 2, &operand.exp, false,
  1130. BFD_RELOC_XGATE_IMM8_HI);
  1131. else
  1132. as_bad (_("you must use a hi/lo directive or 16-bit macro "
  1133. "to load a 16-bit value."));
  1134. }
  1135. else if (*op_constraint == '5')
  1136. fix_new_exp (frag_now, where, 2, &operand.exp, false,
  1137. BFD_RELOC_XGATE_IMM5);
  1138. else if (*op_constraint == '4')
  1139. fix_new_exp (frag_now, where, 2, &operand.exp, false,
  1140. BFD_RELOC_XGATE_IMM4);
  1141. else if (*op_constraint == '3')
  1142. fix_new_exp (frag_now, where, 2, &operand.exp, false,
  1143. BFD_RELOC_XGATE_IMM3);
  1144. else
  1145. as_bad (_(":unknown relocation constraint size"));
  1146. }
  1147. break;
  1148. case 'c': /* CCR register expected. */
  1149. *bit_width = 0;
  1150. if (operand.reg != REG_CCR)
  1151. as_bad (_(": expected register name ccr "));
  1152. break;
  1153. case 'p': /* PC register expected. */
  1154. *bit_width = 0;
  1155. if (operand.reg != REG_PC)
  1156. as_bad (_(": expected register name pc "));
  1157. break;
  1158. case 'b': /* Branch expected. */
  1159. (*op_con)++;
  1160. op_constraint++;
  1161. if (operand.exp.X_op != O_register)
  1162. {
  1163. if (*op_constraint == '9')
  1164. fix_new_exp (frag_now, where, 2, &operand.exp, true,
  1165. BFD_RELOC_XGATE_PCREL_9);
  1166. else if (*op_constraint == 'a')
  1167. fix_new_exp (frag_now, where, 2, &operand.exp, true,
  1168. BFD_RELOC_XGATE_PCREL_10);
  1169. }
  1170. else
  1171. as_fatal (_("Operand `%x' not recognized in fixup8."),
  1172. operand.exp.X_op);
  1173. break;
  1174. case '?':
  1175. break;
  1176. default:
  1177. as_bad (_("unknown constraint `%c'"), *op_constraint);
  1178. break;
  1179. }
  1180. return op_mask;
  1181. }