lc3barch.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * LC-3b architecture description
  3. *
  4. * Copyright (C) 2003-2007 Peter Johnson
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <util.h>
  28. #include <libyasm.h>
  29. #include "lc3barch.h"
  30. yasm_arch_module yasm_lc3b_LTX_arch;
  31. static /*@only@*/ yasm_arch *
  32. lc3b_create(const char *machine, const char *parser,
  33. /*@out@*/ yasm_arch_create_error *error)
  34. {
  35. yasm_arch_base *arch;
  36. *error = YASM_ARCH_CREATE_OK;
  37. if (yasm__strcasecmp(machine, "lc3b") != 0) {
  38. *error = YASM_ARCH_CREATE_BAD_MACHINE;
  39. return NULL;
  40. }
  41. if (yasm__strcasecmp(parser, "nasm") != 0) {
  42. *error = YASM_ARCH_CREATE_BAD_PARSER;
  43. return NULL;
  44. }
  45. arch = yasm_xmalloc(sizeof(yasm_arch_base));
  46. arch->module = &yasm_lc3b_LTX_arch;
  47. return (yasm_arch *)arch;
  48. }
  49. static void
  50. lc3b_destroy(/*@only@*/ yasm_arch *arch)
  51. {
  52. yasm_xfree(arch);
  53. }
  54. static const char *
  55. lc3b_get_machine(/*@unused@*/ const yasm_arch *arch)
  56. {
  57. return "lc3b";
  58. }
  59. static unsigned int
  60. lc3b_get_address_size(/*@unused@*/ const yasm_arch *arch)
  61. {
  62. return 16;
  63. }
  64. static int
  65. lc3b_set_var(yasm_arch *arch, const char *var, unsigned long val)
  66. {
  67. return 1;
  68. }
  69. static const unsigned char **
  70. lc3b_get_fill(const yasm_arch *arch)
  71. {
  72. /* NOP pattern is all 0's per LC-3b Assembler 3.50 output */
  73. static const unsigned char *fill[16] = {
  74. NULL, /* unused */
  75. NULL, /* 1 - illegal; all opcodes are 2 bytes long */
  76. (const unsigned char *)
  77. "\x00\x00", /* 4 */
  78. NULL, /* 3 - illegal */
  79. (const unsigned char *)
  80. "\x00\x00\x00\x00", /* 4 */
  81. NULL, /* 5 - illegal */
  82. (const unsigned char *)
  83. "\x00\x00\x00\x00\x00\x00", /* 6 */
  84. NULL, /* 7 - illegal */
  85. (const unsigned char *)
  86. "\x00\x00\x00\x00\x00\x00" /* 8 */
  87. "\x00\x00",
  88. NULL, /* 9 - illegal */
  89. (const unsigned char *)
  90. "\x00\x00\x00\x00\x00\x00" /* 10 */
  91. "\x00\x00\x00\x00",
  92. NULL, /* 11 - illegal */
  93. (const unsigned char *)
  94. "\x00\x00\x00\x00\x00\x00" /* 12 */
  95. "\x00\x00\x00\x00\x00\x00",
  96. NULL, /* 13 - illegal */
  97. (const unsigned char *)
  98. "\x00\x00\x00\x00\x00\x00" /* 14 */
  99. "\x00\x00\x00\x00\x00\x00\x00\x00",
  100. NULL /* 15 - illegal */
  101. };
  102. return fill;
  103. }
  104. static unsigned int
  105. lc3b_get_reg_size(/*@unused@*/ yasm_arch *arch, /*@unused@*/ uintptr_t reg)
  106. {
  107. return 16;
  108. }
  109. static uintptr_t
  110. lc3b_reggroup_get_reg(/*@unused@*/ yasm_arch *arch,
  111. /*@unused@*/ uintptr_t reggroup,
  112. /*@unused@*/ unsigned long regindex)
  113. {
  114. return 0;
  115. }
  116. static void
  117. lc3b_reg_print(/*@unused@*/ yasm_arch *arch, uintptr_t reg, FILE *f)
  118. {
  119. fprintf(f, "r%u", (unsigned int)(reg&7));
  120. }
  121. static int
  122. lc3b_floatnum_tobytes(yasm_arch *arch, const yasm_floatnum *flt,
  123. unsigned char *buf, size_t destsize, size_t valsize,
  124. size_t shift, int warn)
  125. {
  126. yasm_error_set(YASM_ERROR_FLOATING_POINT,
  127. N_("LC-3b does not support floating point"));
  128. return 1;
  129. }
  130. static yasm_effaddr *
  131. lc3b_ea_create_expr(yasm_arch *arch, yasm_expr *e)
  132. {
  133. yasm_effaddr *ea = yasm_xmalloc(sizeof(yasm_effaddr));
  134. yasm_value_initialize(&ea->disp, e, 0);
  135. ea->need_nonzero_len = 0;
  136. ea->need_disp = 1;
  137. ea->nosplit = 0;
  138. ea->strong = 0;
  139. ea->segreg = 0;
  140. ea->pc_rel = 0;
  141. ea->not_pc_rel = 0;
  142. return ea;
  143. }
  144. void
  145. yasm_lc3b__ea_destroy(/*@only@*/ yasm_effaddr *ea)
  146. {
  147. yasm_value_delete(&ea->disp);
  148. yasm_xfree(ea);
  149. }
  150. static void
  151. lc3b_ea_print(const yasm_effaddr *ea, FILE *f, int indent_level)
  152. {
  153. fprintf(f, "%*sDisp:\n", indent_level, "");
  154. yasm_value_print(&ea->disp, f, indent_level+1);
  155. }
  156. /* Define lc3b machines -- see arch.h for details */
  157. static yasm_arch_machine lc3b_machines[] = {
  158. { "LC-3b", "lc3b" },
  159. { NULL, NULL }
  160. };
  161. /* Define arch structure -- see arch.h for details */
  162. yasm_arch_module yasm_lc3b_LTX_arch = {
  163. "LC-3b",
  164. "lc3b",
  165. NULL,
  166. lc3b_create,
  167. lc3b_destroy,
  168. lc3b_get_machine,
  169. lc3b_get_address_size,
  170. lc3b_set_var,
  171. yasm_lc3b__parse_check_insnprefix,
  172. yasm_lc3b__parse_check_regtmod,
  173. lc3b_get_fill,
  174. lc3b_floatnum_tobytes,
  175. yasm_lc3b__intnum_tobytes,
  176. lc3b_get_reg_size,
  177. lc3b_reggroup_get_reg,
  178. lc3b_reg_print,
  179. NULL, /*yasm_lc3b__segreg_print*/
  180. lc3b_ea_create_expr,
  181. yasm_lc3b__ea_destroy,
  182. lc3b_ea_print,
  183. yasm_lc3b__create_empty_insn,
  184. lc3b_machines,
  185. "lc3b",
  186. 16,
  187. 2
  188. };