1 /* Copyright (C) 2001 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
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but 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 02110-1301 USA
25 #include "vm-bootstrap.h"
26 #include "instructions.h"
28 struct scm_instruction
{
29 enum scm_opcode opcode
; /* opcode */
30 const char *name
; /* instruction name */
31 signed char len
; /* Instruction length. This may be -1 for
32 the loader (see the `VM_LOADER'
34 signed char npop
; /* The number of values popped. This may be
35 -1 for insns like `call' which can take
36 any number of arguments. */
37 char npush
; /* the number of values pushed */
38 SCM symname
; /* filled in later */
41 #define SCM_VALIDATE_LOOKUP_INSTRUCTION(pos, var, cvar) \
43 cvar = scm_lookup_instruction_by_name (var); \
44 SCM_ASSERT_TYPE (cvar, var, pos, FUNC_NAME, "INSTRUCTION_P"); \
48 static struct scm_instruction
*
49 fetch_instruction_table ()
51 static struct scm_instruction
*table
= NULL
;
53 if (SCM_UNLIKELY (!table
))
55 size_t bytes
= scm_op_last
* sizeof(struct scm_instruction
);
57 table
= malloc (bytes
);
58 memset (table
, 0, bytes
);
59 #define VM_INSTRUCTION_TO_TABLE 1
60 #include <libguile/vm-expand.h>
61 #include <libguile/vm-i-system.i>
62 #include <libguile/vm-i-scheme.i>
63 #include <libguile/vm-i-loader.i>
64 #undef VM_INSTRUCTION_TO_TABLE
65 for (i
= 0; i
< scm_op_last
; i
++)
69 table
[i
].symname
= scm_from_locale_symbol (table
[i
].name
);
71 table
[i
].symname
= SCM_BOOL_F
;
77 static struct scm_instruction
*
78 scm_lookup_instruction_by_name (SCM name
)
80 static SCM instructions_by_name
= SCM_BOOL_F
;
81 struct scm_instruction
*table
= fetch_instruction_table ();
84 if (SCM_UNLIKELY (SCM_FALSEP (instructions_by_name
)))
87 instructions_by_name
= scm_make_hash_table (SCM_I_MAKINUM (scm_op_last
));
88 for (i
= 0; i
< scm_op_last
; i
++)
89 if (scm_is_true (table
[i
].symname
))
90 scm_hashq_set_x (instructions_by_name
, table
[i
].symname
,
92 instructions_by_name
= scm_permanent_object (instructions_by_name
);
95 op
= scm_hashq_ref (instructions_by_name
, name
, SCM_UNDEFINED
);
97 return &table
[SCM_I_INUM (op
)];
103 /* Scheme interface */
105 SCM_DEFINE (scm_instruction_list
, "instruction-list", 0, 0, 0,
108 #define FUNC_NAME s_scm_instruction_list
111 struct scm_instruction
*ip
;
112 for (ip
= fetch_instruction_table (); ip
->opcode
!= scm_op_last
; ip
++)
114 list
= scm_cons (ip
->symname
, list
);
115 return scm_reverse_x (list
, SCM_EOL
);
119 SCM_DEFINE (scm_instruction_p
, "instruction?", 1, 0, 0,
122 #define FUNC_NAME s_scm_instruction_p
124 return SCM_BOOL (scm_lookup_instruction_by_name (obj
));
128 SCM_DEFINE (scm_instruction_length
, "instruction-length", 1, 0, 0,
131 #define FUNC_NAME s_scm_instruction_length
133 struct scm_instruction
*ip
;
134 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
135 return SCM_I_MAKINUM (ip
->len
);
139 SCM_DEFINE (scm_instruction_pops
, "instruction-pops", 1, 0, 0,
142 #define FUNC_NAME s_scm_instruction_pops
144 struct scm_instruction
*ip
;
145 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
146 return SCM_I_MAKINUM (ip
->npop
);
150 SCM_DEFINE (scm_instruction_pushes
, "instruction-pushes", 1, 0, 0,
153 #define FUNC_NAME s_scm_instruction_pushes
155 struct scm_instruction
*ip
;
156 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
157 return SCM_I_MAKINUM (ip
->npush
);
161 SCM_DEFINE (scm_instruction_to_opcode
, "instruction->opcode", 1, 0, 0,
164 #define FUNC_NAME s_scm_instruction_to_opcode
166 struct scm_instruction
*ip
;
167 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
168 return SCM_I_MAKINUM (ip
->opcode
);
172 SCM_DEFINE (scm_opcode_to_instruction
, "opcode->instruction", 1, 0, 0,
175 #define FUNC_NAME s_scm_opcode_to_instruction
178 SCM ret
= SCM_BOOL_F
;
180 SCM_MAKE_VALIDATE (1, op
, I_INUMP
);
181 opcode
= SCM_I_INUM (op
);
183 if (opcode
< scm_op_last
)
184 ret
= fetch_instruction_table ()[opcode
].symname
;
186 if (scm_is_false (ret
))
187 scm_wrong_type_arg_msg (FUNC_NAME
, 1, op
, "INSTRUCTION_P");
194 scm_bootstrap_instructions (void)
196 scm_c_register_extension ("libguile", "scm_init_instructions",
197 (scm_t_extension_init_func
)scm_init_instructions
,
202 scm_init_instructions (void)
206 #ifndef SCM_MAGIC_SNARFER
207 #include "libguile/instructions.x"