instructions.c 6.7 KB

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