1 /* Copyright (C) 2001 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program 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
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
47 #include "vm-bootstrap.h"
48 #include "instructions.h"
50 struct scm_instruction
{
51 enum scm_opcode opcode
; /* opcode */
52 const char *name
; /* instruction name */
53 signed char len
; /* Instruction length. This may be -1 for
54 the loader (see the `VM_LOADER'
56 signed char npop
; /* The number of values popped. This may be
57 -1 for insns like `call' which can take
58 any number of arguments. */
59 char npush
; /* the number of values pushed */
60 SCM symname
; /* filled in later */
63 #define SCM_VALIDATE_LOOKUP_INSTRUCTION(pos, var, cvar) \
65 cvar = scm_lookup_instruction_by_name (var); \
66 SCM_ASSERT_TYPE (cvar, var, pos, FUNC_NAME, "INSTRUCTION_P"); \
70 static struct scm_instruction
*
71 fetch_instruction_table ()
73 static struct scm_instruction
*table
= NULL
;
75 if (SCM_UNLIKELY (!table
))
77 size_t bytes
= scm_op_last
* sizeof(struct scm_instruction
);
79 table
= malloc (bytes
);
80 memset (table
, 0, bytes
);
81 #define VM_INSTRUCTION_TO_TABLE 1
82 #include "vm-expand.h"
83 #include "vm-i-system.i"
84 #include "vm-i-scheme.i"
85 #include "vm-i-loader.i"
86 #undef VM_INSTRUCTION_TO_TABLE
87 for (i
= 0; i
< scm_op_last
; i
++)
91 table
[i
].symname
= scm_from_locale_symbol (table
[i
].name
);
93 table
[i
].symname
= SCM_BOOL_F
;
99 static struct scm_instruction
*
100 scm_lookup_instruction_by_name (SCM name
)
102 static SCM instructions_by_name
= SCM_BOOL_F
;
103 struct scm_instruction
*table
= fetch_instruction_table ();
106 if (SCM_UNLIKELY (SCM_FALSEP (instructions_by_name
)))
109 instructions_by_name
= scm_make_hash_table (SCM_I_MAKINUM (scm_op_last
));
110 for (i
= 0; i
< scm_op_last
; i
++)
111 if (scm_is_true (table
[i
].symname
))
112 scm_hashq_set_x (instructions_by_name
, table
[i
].symname
,
114 instructions_by_name
= scm_permanent_object (instructions_by_name
);
117 op
= scm_hashq_ref (instructions_by_name
, name
, SCM_UNDEFINED
);
118 if (SCM_I_INUMP (op
))
119 return &table
[SCM_I_INUM (op
)];
125 /* Scheme interface */
127 SCM_DEFINE (scm_instruction_list
, "instruction-list", 0, 0, 0,
130 #define FUNC_NAME s_scm_instruction_list
133 struct scm_instruction
*ip
;
134 for (ip
= fetch_instruction_table (); ip
->opcode
!= scm_op_last
; ip
++)
136 list
= scm_cons (ip
->symname
, list
);
137 return scm_reverse_x (list
, SCM_EOL
);
141 SCM_DEFINE (scm_instruction_p
, "instruction?", 1, 0, 0,
144 #define FUNC_NAME s_scm_instruction_p
146 return SCM_BOOL (scm_lookup_instruction_by_name (obj
));
150 SCM_DEFINE (scm_instruction_length
, "instruction-length", 1, 0, 0,
153 #define FUNC_NAME s_scm_instruction_length
155 struct scm_instruction
*ip
;
156 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
157 return SCM_I_MAKINUM (ip
->len
);
161 SCM_DEFINE (scm_instruction_pops
, "instruction-pops", 1, 0, 0,
164 #define FUNC_NAME s_scm_instruction_pops
166 struct scm_instruction
*ip
;
167 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
168 return SCM_I_MAKINUM (ip
->npop
);
172 SCM_DEFINE (scm_instruction_pushes
, "instruction-pushes", 1, 0, 0,
175 #define FUNC_NAME s_scm_instruction_pushes
177 struct scm_instruction
*ip
;
178 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
179 return SCM_I_MAKINUM (ip
->npush
);
183 SCM_DEFINE (scm_instruction_to_opcode
, "instruction->opcode", 1, 0, 0,
186 #define FUNC_NAME s_scm_instruction_to_opcode
188 struct scm_instruction
*ip
;
189 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst
, ip
);
190 return SCM_I_MAKINUM (ip
->opcode
);
194 SCM_DEFINE (scm_opcode_to_instruction
, "opcode->instruction", 1, 0, 0,
197 #define FUNC_NAME s_scm_opcode_to_instruction
200 SCM ret
= SCM_BOOL_F
;
202 SCM_MAKE_VALIDATE (1, op
, I_INUMP
);
203 opcode
= SCM_I_INUM (op
);
205 if (opcode
< scm_op_last
)
206 ret
= fetch_instruction_table ()[opcode
].symname
;
208 if (scm_is_false (ret
))
209 scm_wrong_type_arg_msg (FUNC_NAME
, 1, op
, "INSTRUCTION_P");
216 scm_bootstrap_instructions (void)
221 scm_init_instructions (void)
225 #ifndef SCM_MAGIC_SNARFER
226 #include "instructions.x"