instructions.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* Copyright (C) 2001, 2009, 2010, 2011, 2012 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. struct scm_instruction {
  26. enum scm_opcode opcode; /* opcode */
  27. const char *name; /* instruction name */
  28. signed char len; /* Instruction length. This may be -1 for
  29. the loader (see the `VM_LOADER'
  30. macro). */
  31. signed char npop; /* The number of values popped. This may be
  32. -1 for insns like `call' which can take
  33. any number of arguments. */
  34. char npush; /* the number of values pushed */
  35. SCM symname; /* filled in later */
  36. };
  37. #define SCM_VALIDATE_LOOKUP_INSTRUCTION(pos, var, cvar) \
  38. do { \
  39. cvar = scm_lookup_instruction_by_name (var); \
  40. SCM_ASSERT_TYPE (cvar, var, pos, FUNC_NAME, "INSTRUCTION_P"); \
  41. } while (0)
  42. static scm_i_pthread_mutex_t itable_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  43. static struct scm_instruction*
  44. fetch_instruction_table ()
  45. {
  46. static struct scm_instruction *table = NULL;
  47. scm_i_pthread_mutex_lock (&itable_lock);
  48. if (SCM_UNLIKELY (!table))
  49. {
  50. size_t bytes = SCM_VM_NUM_INSTRUCTIONS * sizeof(struct scm_instruction);
  51. int i;
  52. table = malloc (bytes);
  53. memset (table, 0, bytes);
  54. #define VM_INSTRUCTION_TO_TABLE 1
  55. #include <libguile/vm-expand.h>
  56. #include <libguile/vm-i-system.i>
  57. #include <libguile/vm-i-scheme.i>
  58. #include <libguile/vm-i-loader.i>
  59. #undef VM_INSTRUCTION_TO_TABLE
  60. for (i = 0; i < SCM_VM_NUM_INSTRUCTIONS; i++)
  61. {
  62. table[i].opcode = i;
  63. if (table[i].name)
  64. table[i].symname = scm_from_utf8_symbol (table[i].name);
  65. else
  66. table[i].symname = SCM_BOOL_F;
  67. }
  68. }
  69. scm_i_pthread_mutex_unlock (&itable_lock);
  70. return table;
  71. }
  72. static struct scm_instruction *
  73. scm_lookup_instruction_by_name (SCM name)
  74. {
  75. static SCM instructions_by_name = SCM_BOOL_F;
  76. struct scm_instruction *table = fetch_instruction_table ();
  77. SCM op;
  78. if (SCM_UNLIKELY (scm_is_false (instructions_by_name)))
  79. {
  80. unsigned int i;
  81. instructions_by_name =
  82. scm_make_hash_table (SCM_I_MAKINUM (SCM_VM_NUM_INSTRUCTIONS));
  83. for (i = 0; i < SCM_VM_NUM_INSTRUCTIONS; i++)
  84. if (scm_is_true (table[i].symname))
  85. scm_hashq_set_x (instructions_by_name, table[i].symname,
  86. SCM_I_MAKINUM (i));
  87. }
  88. op = scm_hashq_ref (instructions_by_name, name, SCM_UNDEFINED);
  89. if (SCM_I_INUMP (op))
  90. return &table[SCM_I_INUM (op)];
  91. return NULL;
  92. }
  93. /* Scheme interface */
  94. SCM_DEFINE (scm_instruction_list, "instruction-list", 0, 0, 0,
  95. (void),
  96. "")
  97. #define FUNC_NAME s_scm_instruction_list
  98. {
  99. SCM list = SCM_EOL;
  100. int i;
  101. struct scm_instruction *ip = fetch_instruction_table ();
  102. for (i = 0; i < SCM_VM_NUM_INSTRUCTIONS; i++)
  103. if (ip[i].name)
  104. list = scm_cons (ip[i].symname, list);
  105. return scm_reverse_x (list, SCM_EOL);
  106. }
  107. #undef FUNC_NAME
  108. SCM_DEFINE (scm_instruction_p, "instruction?", 1, 0, 0,
  109. (SCM obj),
  110. "")
  111. #define FUNC_NAME s_scm_instruction_p
  112. {
  113. return scm_from_bool (scm_lookup_instruction_by_name (obj) != NULL);
  114. }
  115. #undef FUNC_NAME
  116. SCM_DEFINE (scm_instruction_length, "instruction-length", 1, 0, 0,
  117. (SCM inst),
  118. "")
  119. #define FUNC_NAME s_scm_instruction_length
  120. {
  121. struct scm_instruction *ip;
  122. SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip);
  123. return SCM_I_MAKINUM (ip->len);
  124. }
  125. #undef FUNC_NAME
  126. SCM_DEFINE (scm_instruction_pops, "instruction-pops", 1, 0, 0,
  127. (SCM inst),
  128. "")
  129. #define FUNC_NAME s_scm_instruction_pops
  130. {
  131. struct scm_instruction *ip;
  132. SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip);
  133. return SCM_I_MAKINUM (ip->npop);
  134. }
  135. #undef FUNC_NAME
  136. SCM_DEFINE (scm_instruction_pushes, "instruction-pushes", 1, 0, 0,
  137. (SCM inst),
  138. "")
  139. #define FUNC_NAME s_scm_instruction_pushes
  140. {
  141. struct scm_instruction *ip;
  142. SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip);
  143. return SCM_I_MAKINUM (ip->npush);
  144. }
  145. #undef FUNC_NAME
  146. SCM_DEFINE (scm_instruction_to_opcode, "instruction->opcode", 1, 0, 0,
  147. (SCM inst),
  148. "")
  149. #define FUNC_NAME s_scm_instruction_to_opcode
  150. {
  151. struct scm_instruction *ip;
  152. SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip);
  153. return SCM_I_MAKINUM (ip->opcode);
  154. }
  155. #undef FUNC_NAME
  156. SCM_DEFINE (scm_opcode_to_instruction, "opcode->instruction", 1, 0, 0,
  157. (SCM op),
  158. "")
  159. #define FUNC_NAME s_scm_opcode_to_instruction
  160. {
  161. scm_t_signed_bits opcode;
  162. SCM ret = SCM_BOOL_F;
  163. SCM_MAKE_VALIDATE (1, op, I_INUMP);
  164. opcode = SCM_I_INUM (op);
  165. if (opcode >= 0 && opcode < SCM_VM_NUM_INSTRUCTIONS)
  166. ret = fetch_instruction_table ()[opcode].symname;
  167. if (scm_is_false (ret))
  168. scm_wrong_type_arg_msg (FUNC_NAME, 1, op, "INSTRUCTION_P");
  169. return ret;
  170. }
  171. #undef FUNC_NAME
  172. void
  173. scm_bootstrap_instructions (void)
  174. {
  175. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  176. "scm_init_instructions",
  177. (scm_t_extension_init_func)scm_init_instructions,
  178. NULL);
  179. scm_c_atfork_lock_static_mutex (&itable_lock);
  180. }
  181. void
  182. scm_init_instructions (void)
  183. {
  184. #ifndef SCM_MAGIC_SNARFER
  185. #include "libguile/instructions.x"
  186. #endif
  187. }
  188. /*
  189. Local Variables:
  190. c-file-style: "gnu"
  191. End:
  192. */