1 /* Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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
27 #include "instructions.h"
30 struct scm_instruction
{
31 enum scm_opcode opcode
; /* opcode */
32 const char *name
; /* instruction name */
33 signed char len
; /* Instruction length. This may be -1 for
34 the loader (see the `VM_LOADER'
36 signed char npop
; /* The number of values popped. This may be
37 -1 for insns like `call' which can take
38 any number of arguments. */
39 char npush
; /* the number of values pushed */
40 SCM symname
; /* filled in later */
43 #define SCM_VALIDATE_LOOKUP_INSTRUCTION(pos, var, cvar) \
45 cvar = scm_lookup_instruction_by_name (var); \
46 SCM_ASSERT_TYPE (cvar, var, pos, FUNC_NAME, "INSTRUCTION_P"); \
50 static scm_i_pthread_mutex_t itable_lock
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
53 static struct scm_instruction
*
54 fetch_instruction_table ()
56 static struct scm_instruction
*table
= NULL
;
58 scm_i_pthread_mutex_lock (&itable_lock
);
59 if (SCM_UNLIKELY (!table
))
61 size_t bytes
= SCM_VM_NUM_INSTRUCTIONS
* sizeof(struct scm_instruction
);
63 table
= malloc (bytes
);
64 memset (table
, 0, bytes
);
65 #define VM_INSTRUCTION_TO_TABLE 1
66 #include <libguile/vm-expand.h>
67 #include <libguile/vm-i-system.i>
68 #include <libguile/vm-i-scheme.i>
69 #include <libguile/vm-i-loader.i>
70 #undef VM_INSTRUCTION_TO_TABLE
71 for (i
= 0; i
< SCM_VM_NUM_INSTRUCTIONS
; i
++)
75 table
[i
].symname
= scm_from_locale_symbol (table
[i
].name
);
77 table
[i
].symname
= SCM_BOOL_F
;
80 scm_i_pthread_mutex_unlock (&itable_lock
);
85 static SCM instructions_by_name
;
88 init_instructions_by_name (void)
90 struct scm_instruction
*table
= fetch_instruction_table ();
93 instructions_by_name
=
94 scm_make_hash_table (SCM_I_MAKINUM (SCM_VM_NUM_INSTRUCTIONS
));
96 for (i
= 0; i
< SCM_VM_NUM_INSTRUCTIONS
; i
++)
97 if (scm_is_true (table
[i
].symname
))
98 scm_hashq_set_x (instructions_by_name
, table
[i
].symname
,
102 static struct scm_instruction
*
103 scm_lookup_instruction_by_name (SCM name
)
105 static scm_i_pthread_once_t once
= SCM_I_PTHREAD_ONCE_INIT
;
106 struct scm_instruction
*table
= fetch_instruction_table ();
109 scm_i_pthread_once (&once
, init_instructions_by_name
);
111 op
= scm_hashq_ref (instructions_by_name
, name
, SCM_UNDEFINED
);
112 if (SCM_I_INUMP (op
))
113 return &table
[SCM_I_INUM (op
)];
119 /* Scheme interface */
121 SCM_DEFINE (scm_instruction_list
, "instruction-list", 0, 0, 0,
124 #define FUNC_NAME s_scm_instruction_list
128 struct scm_instruction
*ip
= fetch_instruction_table ();
129 for (i
= 0; i
< SCM_VM_NUM_INSTRUCTIONS
; i
++)
131 list
= scm_cons (ip
[i
].symname
, list
);
132 return scm_reverse_x (list
, SCM_EOL
);
136 SCM_DEFINE (scm_instruction_p
, "instruction?", 1, 0, 0,
139 #define FUNC_NAME s_scm_instruction_p
141 return scm_from_bool (scm_lookup_instruction_by_name (obj
) != NULL
);
145 SCM_DEFINE (scm_instruction_length
, "instruction-length", 1, 0, 0,
148 #define FUNC_NAME s_scm_instruction_length
150 struct scm_instruction
*ip
;
151 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
152 return SCM_I_MAKINUM (ip
->len
);
156 SCM_DEFINE (scm_instruction_pops
, "instruction-pops", 1, 0, 0,
159 #define FUNC_NAME s_scm_instruction_pops
161 struct scm_instruction
*ip
;
162 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
163 return SCM_I_MAKINUM (ip
->npop
);
167 SCM_DEFINE (scm_instruction_pushes
, "instruction-pushes", 1, 0, 0,
170 #define FUNC_NAME s_scm_instruction_pushes
172 struct scm_instruction
*ip
;
173 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
174 return SCM_I_MAKINUM (ip
->npush
);
178 SCM_DEFINE (scm_instruction_to_opcode
, "instruction->opcode", 1, 0, 0,
181 #define FUNC_NAME s_scm_instruction_to_opcode
183 struct scm_instruction
*ip
;
184 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
185 return SCM_I_MAKINUM (ip
->opcode
);
189 SCM_DEFINE (scm_opcode_to_instruction
, "opcode->instruction", 1, 0, 0,
192 #define FUNC_NAME s_scm_opcode_to_instruction
194 scm_t_signed_bits opcode
;
195 SCM ret
= SCM_BOOL_F
;
197 SCM_MAKE_VALIDATE (1, op
, I_INUMP
);
198 opcode
= SCM_I_INUM (op
);
200 if (opcode
>= 0 && opcode
< SCM_VM_NUM_INSTRUCTIONS
)
201 ret
= fetch_instruction_table ()[opcode
].symname
;
203 if (scm_is_false (ret
))
204 scm_wrong_type_arg_msg (FUNC_NAME
, 1, op
, "INSTRUCTION_P");
211 scm_bootstrap_instructions (void)
213 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
214 "scm_init_instructions",
215 (scm_t_extension_init_func
)scm_init_instructions
,
220 scm_init_instructions (void)
222 #ifndef SCM_MAGIC_SNARFER
223 #include "libguile/instructions.x"