tc-xc16x.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* tc-xc16x.c -- Assembler for the Infineon XC16X.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. Contributed by KPIT Cummins Infosystems
  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 the Free
  15. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  16. 02110-1301, USA. */
  17. #include "as.h"
  18. #include "safe-ctype.h"
  19. #include "subsegs.h"
  20. #include "symcat.h"
  21. #include "opcodes/xc16x-desc.h"
  22. #include "opcodes/xc16x-opc.h"
  23. #include "cgen.h"
  24. #include "dwarf2dbg.h"
  25. #ifdef OBJ_ELF
  26. #include "elf/xc16x.h"
  27. #endif
  28. /* Structure to hold all of the different components describing
  29. an individual instruction. */
  30. typedef struct
  31. {
  32. const CGEN_INSN * insn;
  33. const CGEN_INSN * orig_insn;
  34. CGEN_FIELDS fields;
  35. #if CGEN_INT_INSN_P
  36. CGEN_INSN_INT buffer [1];
  37. #define INSN_VALUE(buf) (*(buf))
  38. #else
  39. unsigned char buffer [CGEN_MAX_INSN_SIZE];
  40. #define INSN_VALUE(buf) (buf)
  41. #endif
  42. char * addr;
  43. fragS * frag;
  44. int num_fixups;
  45. fixS * fixups [GAS_CGEN_MAX_FIXUPS];
  46. int indices [MAX_OPERAND_INSTANCES];
  47. }
  48. xc16x_insn;
  49. const char comment_chars[] = ";";
  50. const char line_comment_chars[] = "#";
  51. const char line_separator_chars[] = "";
  52. const char EXP_CHARS[] = "eE";
  53. const char FLT_CHARS[] = "dD";
  54. #define XC16X_SHORTOPTS ""
  55. const char * md_shortopts = XC16X_SHORTOPTS;
  56. struct option md_longopts[] =
  57. {
  58. {NULL, no_argument, NULL, 0}
  59. };
  60. size_t md_longopts_size = sizeof (md_longopts);
  61. static void
  62. xc16xlmode (int arg ATTRIBUTE_UNUSED)
  63. {
  64. if (stdoutput != NULL)
  65. if (!bfd_set_arch_mach (stdoutput, bfd_arch_xc16x, bfd_mach_xc16xl))
  66. as_warn (_("could not set architecture and machine"));
  67. }
  68. static void
  69. xc16xsmode (int arg ATTRIBUTE_UNUSED)
  70. {
  71. if (!bfd_set_arch_mach (stdoutput, bfd_arch_xc16x, bfd_mach_xc16xs))
  72. as_warn (_("could not set architecture and machine"));
  73. }
  74. static void
  75. xc16xmode (int arg ATTRIBUTE_UNUSED)
  76. {
  77. if (!bfd_set_arch_mach (stdoutput, bfd_arch_xc16x, bfd_mach_xc16x))
  78. as_warn (_("could not set architecture and machine"));
  79. }
  80. /* The target specific pseudo-ops which we support. */
  81. const pseudo_typeS md_pseudo_table[] =
  82. {
  83. { "word", cons, 2 },
  84. {"xc16xl", xc16xlmode, 0},
  85. {"xc16xs", xc16xsmode, 0},
  86. {"xc16x", xc16xmode, 0},
  87. { NULL, NULL, 0 }
  88. };
  89. void
  90. md_begin (void)
  91. {
  92. /* Initialize the `cgen' interface. */
  93. /* Set the machine number and endian. */
  94. gas_cgen_cpu_desc = xc16x_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0,
  95. CGEN_CPU_OPEN_ENDIAN,
  96. CGEN_ENDIAN_LITTLE,
  97. CGEN_CPU_OPEN_END);
  98. xc16x_cgen_init_asm (gas_cgen_cpu_desc);
  99. /* This is a callback from cgen to gas to parse operands. */
  100. cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
  101. }
  102. void
  103. md_assemble (char *str)
  104. {
  105. xc16x_insn insn;
  106. char *errmsg;
  107. /* Initialize GAS's cgen interface for a new instruction. */
  108. gas_cgen_init_parse ();
  109. insn.insn = xc16x_cgen_assemble_insn
  110. (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
  111. if (!insn.insn)
  112. {
  113. as_bad ("%s", errmsg);
  114. return;
  115. }
  116. /* Doesn't really matter what we pass for RELAX_P here. */
  117. gas_cgen_finish_insn (insn.insn, insn.buffer,
  118. CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
  119. }
  120. /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
  121. Returns BFD_RELOC_NONE if no reloc type can be found.
  122. *FIXP may be modified if desired. */
  123. bfd_reloc_code_real_type
  124. md_cgen_lookup_reloc (const CGEN_INSN *insn ATTRIBUTE_UNUSED,
  125. const CGEN_OPERAND *operand,
  126. fixS *fixP)
  127. {
  128. switch (operand->type)
  129. {
  130. case XC16X_OPERAND_REL:
  131. /* ??? Adjust size? */
  132. fixP->fx_where += 1;
  133. fixP->fx_pcrel = 1;
  134. return BFD_RELOC_8_PCREL;
  135. case XC16X_OPERAND_CADDR:
  136. fixP->fx_size = 2;
  137. fixP->fx_where += 2;
  138. return BFD_RELOC_16;
  139. case XC16X_OPERAND_UIMM7:
  140. /* ??? Adjust size? */
  141. fixP->fx_where += 1;
  142. fixP->fx_pcrel = 1;
  143. return BFD_RELOC_8_PCREL;
  144. case XC16X_OPERAND_UIMM16:
  145. case XC16X_OPERAND_MEMORY:
  146. fixP->fx_size = 2;
  147. fixP->fx_where += 2;
  148. return BFD_RELOC_16;
  149. case XC16X_OPERAND_UPOF16:
  150. fixP->fx_size = 2;
  151. fixP->fx_where += 2;
  152. return BFD_RELOC_XC16X_POF;
  153. case XC16X_OPERAND_UPAG16:
  154. fixP->fx_size = 2;
  155. fixP->fx_where += 2;
  156. return BFD_RELOC_XC16X_PAG;
  157. case XC16X_OPERAND_USEG8:
  158. /* ??? This is an 8 bit field, why the 16 bit reloc? */
  159. fixP->fx_where += 1;
  160. return BFD_RELOC_XC16X_SEG;
  161. case XC16X_OPERAND_USEG16:
  162. case XC16X_OPERAND_USOF16:
  163. fixP->fx_size = 2;
  164. fixP->fx_where += 2;
  165. return BFD_RELOC_XC16X_SOF;
  166. default : /* Avoid -Wall warning. */
  167. break;
  168. }
  169. return BFD_RELOC_NONE;
  170. }
  171. /* Write a value out to the object file, using the appropriate endianness. */
  172. void
  173. md_number_to_chars (char * buf, valueT val, int n)
  174. {
  175. number_to_chars_littleendian (buf, val, n);
  176. }
  177. void
  178. md_show_usage (FILE * stream)
  179. {
  180. fprintf (stream, _(" XC16X specific command line options:\n"));
  181. }
  182. int
  183. md_parse_option (int c ATTRIBUTE_UNUSED,
  184. const char *arg ATTRIBUTE_UNUSED)
  185. {
  186. return 0;
  187. }
  188. const char *
  189. md_atof (int type, char *litP, int *sizeP)
  190. {
  191. return ieee_md_atof (type, litP, sizeP, false);
  192. }
  193. valueT
  194. md_section_align (segT segment, valueT size)
  195. {
  196. int align = bfd_section_alignment (segment);
  197. return ((size + (1 << align) - 1) & -(1 << align));
  198. }
  199. symbolS *
  200. md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
  201. {
  202. return NULL;
  203. }
  204. int
  205. md_estimate_size_before_relax (fragS *fragP ATTRIBUTE_UNUSED,
  206. segT segment_type ATTRIBUTE_UNUSED)
  207. {
  208. printf (_("call to md_estimate_size_before_relax \n"));
  209. abort ();
  210. }
  211. long
  212. md_pcrel_from (fixS *fixP)
  213. {
  214. long temp_val;
  215. temp_val=fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
  216. return temp_val;
  217. }
  218. long
  219. md_pcrel_from_section (fixS *fixP, segT sec)
  220. {
  221. if (fixP->fx_addsy != (symbolS *) NULL
  222. && (! S_IS_DEFINED (fixP->fx_addsy)
  223. || S_GET_SEGMENT (fixP->fx_addsy) != sec
  224. || S_IS_EXTERNAL (fixP->fx_addsy)
  225. || S_IS_WEAK (fixP->fx_addsy)))
  226. {
  227. return 0;
  228. }
  229. return md_pcrel_from (fixP);
  230. }
  231. arelent *
  232. tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
  233. {
  234. arelent *rel;
  235. bfd_reloc_code_real_type r_type;
  236. if (fixp->fx_subsy)
  237. {
  238. if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy))
  239. || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section)
  240. {
  241. as_bad_subtract (fixp);
  242. return NULL;
  243. }
  244. }
  245. rel = XNEW (arelent);
  246. rel->sym_ptr_ptr = XNEW (asymbol *);
  247. *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
  248. rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
  249. rel->addend = fixp->fx_offset;
  250. r_type = fixp->fx_r_type;
  251. #define DEBUG 0
  252. #if DEBUG
  253. fprintf (stderr, "%s\n", bfd_get_reloc_code_name (r_type));
  254. fflush (stderr);
  255. #endif
  256. rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
  257. if (rel->howto == NULL)
  258. {
  259. as_bad_where (fixp->fx_file, fixp->fx_line,
  260. _("Cannot represent relocation type %s"),
  261. bfd_get_reloc_code_name (r_type));
  262. return NULL;
  263. }
  264. return rel;
  265. }
  266. void
  267. md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
  268. {
  269. /* FIXME: This absolute nonsense apparently is to make relative
  270. jumps "work". Of course, it breaks any other type of fixup. */
  271. if (!strstr (seg->name,".debug"))
  272. {
  273. if (*valP < 128)
  274. *valP /= 2;
  275. if (*valP>268435455)
  276. {
  277. *valP = *valP * (-1);
  278. *valP /= 2;
  279. *valP = 256 - (*valP);
  280. }
  281. }
  282. gas_cgen_md_apply_fix (fixP, valP, seg);
  283. return;
  284. }
  285. void
  286. md_convert_frag (bfd *headers ATTRIBUTE_UNUSED,
  287. segT seg ATTRIBUTE_UNUSED,
  288. fragS *fragP ATTRIBUTE_UNUSED)
  289. {
  290. printf (_("call to md_convert_frag \n"));
  291. abort ();
  292. }