instructions.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #if HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <string.h>
  22. #include "_scm.h"
  23. #include "threads.h"
  24. #include "instructions.h"
  25. SCM_SYMBOL (sym_left_arrow, "<-");
  26. SCM_SYMBOL (sym_bang, "!");
  27. #define FOR_EACH_INSTRUCTION_WORD_TYPE(M) \
  28. M(X32) \
  29. M(X8_S24) \
  30. M(X8_F24) \
  31. M(X8_L24) \
  32. M(X8_C24) \
  33. M(X8_S8_I16) \
  34. M(X8_S12_S12) \
  35. M(X8_S12_C12) \
  36. M(X8_C12_C12) \
  37. M(X8_F12_F12) \
  38. M(X8_S8_S8_S8) \
  39. M(X8_S8_C8_S8) \
  40. M(X8_S8_S8_C8) \
  41. M(C8_C24) \
  42. M(C32) /* Unsigned. */ \
  43. M(I32) /* Immediate. */ \
  44. M(A32) /* Immediate, high bits. */ \
  45. M(B32) /* Immediate, low bits. */ \
  46. M(AF32) /* Immediate double, high bits. */ \
  47. M(BF32) /* Immediate double, low bits. */ \
  48. M(AU32) /* Immediate uint64, high bits. */ \
  49. M(BU32) /* Immediate uint64, low bits. */ \
  50. M(AS32) /* Immediate int64, high bits. */ \
  51. M(BS32) /* Immediate int64, low bits. */ \
  52. M(N32) /* Non-immediate. */ \
  53. M(R32) /* Scheme value (indirected). */ \
  54. M(L32) /* Label. */ \
  55. M(LO32) /* Label with offset. */ \
  56. M(B1_C7_L24) \
  57. M(B1_X7_L24) \
  58. M(B1_X7_C24) \
  59. M(B1_X7_S24) \
  60. M(B1_X7_F24) \
  61. M(B1_X31)
  62. #define TYPE_WIDTH 6
  63. enum word_type
  64. {
  65. #define ENUM(type) type,
  66. FOR_EACH_INSTRUCTION_WORD_TYPE (ENUM)
  67. #undef ENUM
  68. };
  69. static SCM word_type_symbols[] =
  70. {
  71. #define FALSE(type) SCM_BOOL_F,
  72. FOR_EACH_INSTRUCTION_WORD_TYPE (FALSE)
  73. #undef FALSE
  74. };
  75. #define OP(n,type) (((type) + 1) << (n*TYPE_WIDTH))
  76. /* The VM_DEFINE_OP macro uses a CPP-based DSL to describe what kinds of
  77. arguments each instruction takes. This piece of code is the only
  78. bit that actually interprets that language. These macro definitions
  79. encode the operand types into bits in a 64-bit integer.
  80. (instruction-list) parses those encoded values into lists of symbols,
  81. one for each 64-bit word that the operator takes. This list is used
  82. by Scheme to generate assemblers and disassemblers for the
  83. instructions. */
  84. #define NOP SCM_T_UINT64_MAX
  85. #define OP1(type0) \
  86. (OP (0, type0))
  87. #define OP2(type0, type1) \
  88. (OP (0, type0) | OP (1, type1))
  89. #define OP3(type0, type1, type2) \
  90. (OP (0, type0) | OP (1, type1) | OP (2, type2))
  91. #define OP4(type0, type1, type2, type3) \
  92. (OP (0, type0) | OP (1, type1) | OP (2, type2) | OP (3, type3))
  93. #define OP5(type0, type1, type2, type3, type4) \
  94. (OP (0, type0) | OP (1, type1) | OP (2, type2) | OP (3, type3) | OP (4, type4))
  95. #define OP_DST (1 << (TYPE_WIDTH * 5))
  96. #define WORD_TYPE_AND_FLAG(n, word) \
  97. (((word) >> ((n) * TYPE_WIDTH)) & ((1 << TYPE_WIDTH) - 1))
  98. #define WORD_TYPE(n, word) \
  99. (WORD_TYPE_AND_FLAG (n, word) - 1)
  100. #define HAS_WORD(n, word) \
  101. (WORD_TYPE_AND_FLAG (n, word) != 0)
  102. /* Scheme interface */
  103. static SCM
  104. parse_instruction (scm_t_uint8 opcode, const char *name, scm_t_uint64 meta)
  105. {
  106. SCM tail = SCM_EOL;
  107. int len;
  108. /* Format: (name opcode word0 word1 ...) */
  109. if (HAS_WORD (4, meta))
  110. len = 5;
  111. else if (HAS_WORD (3, meta))
  112. len = 4;
  113. else if (HAS_WORD (2, meta))
  114. len = 3;
  115. else if (HAS_WORD (1, meta))
  116. len = 2;
  117. else if (HAS_WORD (0, meta))
  118. len = 1;
  119. else
  120. abort ();
  121. switch (len)
  122. {
  123. case 5:
  124. tail = scm_cons (word_type_symbols[WORD_TYPE (4, meta)], tail);
  125. case 4:
  126. tail = scm_cons (word_type_symbols[WORD_TYPE (3, meta)], tail);
  127. case 3:
  128. tail = scm_cons (word_type_symbols[WORD_TYPE (2, meta)], tail);
  129. case 2:
  130. tail = scm_cons (word_type_symbols[WORD_TYPE (1, meta)], tail);
  131. case 1:
  132. tail = scm_cons (word_type_symbols[WORD_TYPE (0, meta)], tail);
  133. default:
  134. tail = scm_cons ((meta & OP_DST) ? sym_left_arrow : sym_bang, tail);
  135. tail = scm_cons (scm_from_int (opcode), tail);
  136. tail = scm_cons (scm_from_utf8_symbol (name), tail);
  137. return tail;
  138. }
  139. }
  140. SCM_DEFINE (scm_instruction_list, "instruction-list", 0, 0, 0,
  141. (void),
  142. "")
  143. #define FUNC_NAME s_scm_instruction_list
  144. {
  145. SCM list = SCM_EOL;
  146. #define INIT(opcode, tag, name, meta) \
  147. if (name) list = scm_cons (parse_instruction (opcode, name, meta), list);
  148. FOR_EACH_VM_OPERATION (INIT);
  149. #undef INIT
  150. return scm_reverse_x (list, SCM_EOL);
  151. }
  152. #undef FUNC_NAME
  153. void
  154. scm_bootstrap_instructions (void)
  155. {
  156. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  157. "scm_init_instructions",
  158. (scm_t_extension_init_func)scm_init_instructions,
  159. NULL);
  160. }
  161. void
  162. scm_init_instructions (void)
  163. {
  164. #define INIT(type) \
  165. word_type_symbols[type] = scm_from_utf8_symbol (#type);
  166. FOR_EACH_INSTRUCTION_WORD_TYPE (INIT)
  167. #undef INIT
  168. #ifndef SCM_MAGIC_SNARFER
  169. #include "libguile/instructions.x"
  170. #endif
  171. }
  172. /*
  173. Local Variables:
  174. c-file-style: "gnu"
  175. End:
  176. */