1 /* Copyright (C) 2001, 2009 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
26 #include "vm-bootstrap.h"
27 #include "instructions.h"
29 struct scm_instruction
{
30 enum scm_opcode opcode
; /* opcode */
31 const char *name
; /* instruction name */
32 signed char len
; /* Instruction length. This may be -1 for
33 the loader (see the `VM_LOADER'
35 signed char npop
; /* The number of values popped. This may be
36 -1 for insns like `call' which can take
37 any number of arguments. */
38 char npush
; /* the number of values pushed */
39 SCM symname
; /* filled in later */
42 #define SCM_VALIDATE_LOOKUP_INSTRUCTION(pos, var, cvar) \
44 cvar = scm_lookup_instruction_by_name (var); \
45 SCM_ASSERT_TYPE (cvar, var, pos, FUNC_NAME, "INSTRUCTION_P"); \
49 static struct scm_instruction
*
50 fetch_instruction_table ()
52 static struct scm_instruction
*table
= NULL
;
54 if (SCM_UNLIKELY (!table
))
56 size_t bytes
= SCM_VM_NUM_INSTRUCTIONS
* sizeof(struct scm_instruction
);
58 table
= malloc (bytes
);
59 memset (table
, 0, bytes
);
60 #define VM_INSTRUCTION_TO_TABLE 1
61 #include <libguile/vm-expand.h>
62 #include <libguile/vm-i-system.i>
63 #include <libguile/vm-i-scheme.i>
64 #include <libguile/vm-i-loader.i>
65 #undef VM_INSTRUCTION_TO_TABLE
66 for (i
= 0; i
< SCM_VM_NUM_INSTRUCTIONS
; i
++)
70 table
[i
].symname
= scm_from_locale_symbol (table
[i
].name
);
72 table
[i
].symname
= SCM_BOOL_F
;
78 static struct scm_instruction
*
79 scm_lookup_instruction_by_name (SCM name
)
81 static SCM instructions_by_name
= SCM_BOOL_F
;
82 struct scm_instruction
*table
= fetch_instruction_table ();
85 if (SCM_UNLIKELY (scm_is_false (instructions_by_name
)))
89 instructions_by_name
=
90 scm_make_hash_table (SCM_I_MAKINUM (SCM_VM_NUM_INSTRUCTIONS
));
92 for (i
= 0; i
< SCM_VM_NUM_INSTRUCTIONS
; i
++)
93 if (scm_is_true (table
[i
].symname
))
94 scm_hashq_set_x (instructions_by_name
, table
[i
].symname
,
98 op
= scm_hashq_ref (instructions_by_name
, name
, SCM_UNDEFINED
);
100 return &table
[SCM_I_INUM (op
)];
106 /* Scheme interface */
108 SCM_DEFINE (scm_instruction_list
, "instruction-list", 0, 0, 0,
111 #define FUNC_NAME s_scm_instruction_list
115 struct scm_instruction
*ip
= fetch_instruction_table ();
116 for (i
= 0; i
< SCM_VM_NUM_INSTRUCTIONS
; i
++)
118 list
= scm_cons (ip
[i
].symname
, list
);
119 return scm_reverse_x (list
, SCM_EOL
);
123 SCM_DEFINE (scm_instruction_p
, "instruction?", 1, 0, 0,
126 #define FUNC_NAME s_scm_instruction_p
128 return scm_from_bool (scm_lookup_instruction_by_name (obj
) != NULL
);
132 SCM_DEFINE (scm_instruction_length
, "instruction-length", 1, 0, 0,
135 #define FUNC_NAME s_scm_instruction_length
137 struct scm_instruction
*ip
;
138 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
139 return SCM_I_MAKINUM (ip
->len
);
143 SCM_DEFINE (scm_instruction_pops
, "instruction-pops", 1, 0, 0,
146 #define FUNC_NAME s_scm_instruction_pops
148 struct scm_instruction
*ip
;
149 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
150 return SCM_I_MAKINUM (ip
->npop
);
154 SCM_DEFINE (scm_instruction_pushes
, "instruction-pushes", 1, 0, 0,
157 #define FUNC_NAME s_scm_instruction_pushes
159 struct scm_instruction
*ip
;
160 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
161 return SCM_I_MAKINUM (ip
->npush
);
165 SCM_DEFINE (scm_instruction_to_opcode
, "instruction->opcode", 1, 0, 0,
168 #define FUNC_NAME s_scm_instruction_to_opcode
170 struct scm_instruction
*ip
;
171 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
172 return SCM_I_MAKINUM (ip
->opcode
);
176 SCM_DEFINE (scm_opcode_to_instruction
, "opcode->instruction", 1, 0, 0,
179 #define FUNC_NAME s_scm_opcode_to_instruction
182 SCM ret
= SCM_BOOL_F
;
184 SCM_MAKE_VALIDATE (1, op
, I_INUMP
);
185 opcode
= SCM_I_INUM (op
);
187 if (opcode
>= 0 && opcode
< SCM_VM_NUM_INSTRUCTIONS
)
188 ret
= fetch_instruction_table ()[opcode
].symname
;
190 if (scm_is_false (ret
))
191 scm_wrong_type_arg_msg (FUNC_NAME
, 1, op
, "INSTRUCTION_P");
198 scm_bootstrap_instructions (void)
200 scm_c_register_extension ("libguile", "scm_init_instructions",
201 (scm_t_extension_init_func
)scm_init_instructions
,
206 scm_init_instructions (void)
210 #ifndef SCM_MAGIC_SNARFER
211 #include "libguile/instructions.x"