Commit | Line | Data |
---|---|---|
8f5cfc81 | 1 | /* Copyright (C) 2001 Free Software Foundation, Inc. |
17e90c5e KN |
2 | * |
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) | |
6 | * any later version. | |
7 | * | |
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. | |
12 | * | |
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 | |
17 | * | |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
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. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
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. | |
37 | * | |
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. */ | |
41 | ||
13c47753 AW |
42 | #if HAVE_CONFIG_H |
43 | # include <config.h> | |
44 | #endif | |
45 | ||
17e90c5e | 46 | #include <string.h> |
83495480 | 47 | #include "vm-bootstrap.h" |
17e90c5e KN |
48 | #include "instructions.h" |
49 | ||
53e28ed9 AW |
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' | |
55 | macro). */ | |
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 */ | |
f775e51b | 60 | SCM symname; /* filled in later */ |
17e90c5e KN |
61 | }; |
62 | ||
53e28ed9 AW |
63 | #define SCM_VALIDATE_LOOKUP_INSTRUCTION(pos, var, cvar) \ |
64 | do { \ | |
65 | cvar = scm_lookup_instruction_by_name (var); \ | |
66 | SCM_ASSERT_TYPE (cvar, var, pos, FUNC_NAME, "INSTRUCTION_P"); \ | |
67 | } while (0) | |
17e90c5e | 68 | |
53e28ed9 | 69 | |
f775e51b AW |
70 | static struct scm_instruction* |
71 | fetch_instruction_table () | |
72 | { | |
73 | static struct scm_instruction *table = NULL; | |
74 | ||
75 | if (SCM_UNLIKELY (!table)) | |
76 | { | |
77 | size_t bytes = scm_op_last * sizeof(struct scm_instruction); | |
78 | int i; | |
79 | table = malloc (bytes); | |
80 | memset (table, 0, bytes); | |
81 | #define VM_INSTRUCTION_TO_TABLE 1 | |
aeeff258 AW |
82 | #include <libguile/vm-expand.h> |
83 | #include <libguile/vm-i-system.i> | |
84 | #include <libguile/vm-i-scheme.i> | |
85 | #include <libguile/vm-i-loader.i> | |
f775e51b AW |
86 | #undef VM_INSTRUCTION_TO_TABLE |
87 | for (i = 0; i < scm_op_last; i++) | |
88 | { | |
89 | table[i].opcode = i; | |
90 | if (table[i].name) | |
91 | table[i].symname = scm_from_locale_symbol (table[i].name); | |
92 | else | |
93 | table[i].symname = SCM_BOOL_F; | |
94 | } | |
95 | } | |
96 | return table; | |
97 | } | |
98 | ||
53e28ed9 AW |
99 | static struct scm_instruction * |
100 | scm_lookup_instruction_by_name (SCM name) | |
17e90c5e | 101 | { |
f775e51b AW |
102 | static SCM instructions_by_name = SCM_BOOL_F; |
103 | struct scm_instruction *table = fetch_instruction_table (); | |
104 | SCM op; | |
105 | ||
106 | if (SCM_UNLIKELY (SCM_FALSEP (instructions_by_name))) | |
107 | { | |
108 | int i; | |
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, | |
113 | SCM_I_MAKINUM (i)); | |
114 | instructions_by_name = scm_permanent_object (instructions_by_name); | |
115 | } | |
116 | ||
117 | op = scm_hashq_ref (instructions_by_name, name, SCM_UNDEFINED); | |
118 | if (SCM_I_INUMP (op)) | |
119 | return &table[SCM_I_INUM (op)]; | |
120 | ||
121 | return NULL; | |
17e90c5e KN |
122 | } |
123 | ||
53e28ed9 | 124 | |
17e90c5e KN |
125 | /* Scheme interface */ |
126 | ||
127 | SCM_DEFINE (scm_instruction_list, "instruction-list", 0, 0, 0, | |
128 | (void), | |
129 | "") | |
130 | #define FUNC_NAME s_scm_instruction_list | |
131 | { | |
132 | SCM list = SCM_EOL; | |
133 | struct scm_instruction *ip; | |
f775e51b AW |
134 | for (ip = fetch_instruction_table (); ip->opcode != scm_op_last; ip++) |
135 | if (ip->name) | |
136 | list = scm_cons (ip->symname, list); | |
17e90c5e KN |
137 | return scm_reverse_x (list, SCM_EOL); |
138 | } | |
139 | #undef FUNC_NAME | |
140 | ||
141 | SCM_DEFINE (scm_instruction_p, "instruction?", 1, 0, 0, | |
142 | (SCM obj), | |
143 | "") | |
144 | #define FUNC_NAME s_scm_instruction_p | |
145 | { | |
53e28ed9 | 146 | return SCM_BOOL (scm_lookup_instruction_by_name (obj)); |
17e90c5e KN |
147 | } |
148 | #undef FUNC_NAME | |
149 | ||
150 | SCM_DEFINE (scm_instruction_length, "instruction-length", 1, 0, 0, | |
151 | (SCM inst), | |
152 | "") | |
153 | #define FUNC_NAME s_scm_instruction_length | |
154 | { | |
53e28ed9 AW |
155 | struct scm_instruction *ip; |
156 | SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip); | |
157 | return SCM_I_MAKINUM (ip->len); | |
46cd9a34 KN |
158 | } |
159 | #undef FUNC_NAME | |
160 | ||
161 | SCM_DEFINE (scm_instruction_pops, "instruction-pops", 1, 0, 0, | |
162 | (SCM inst), | |
163 | "") | |
164 | #define FUNC_NAME s_scm_instruction_pops | |
165 | { | |
53e28ed9 AW |
166 | struct scm_instruction *ip; |
167 | SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip); | |
168 | return SCM_I_MAKINUM (ip->npop); | |
46cd9a34 KN |
169 | } |
170 | #undef FUNC_NAME | |
171 | ||
172 | SCM_DEFINE (scm_instruction_pushes, "instruction-pushes", 1, 0, 0, | |
173 | (SCM inst), | |
174 | "") | |
175 | #define FUNC_NAME s_scm_instruction_pushes | |
176 | { | |
53e28ed9 AW |
177 | struct scm_instruction *ip; |
178 | SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip); | |
179 | return SCM_I_MAKINUM (ip->npush); | |
17e90c5e KN |
180 | } |
181 | #undef FUNC_NAME | |
182 | ||
183 | SCM_DEFINE (scm_instruction_to_opcode, "instruction->opcode", 1, 0, 0, | |
184 | (SCM inst), | |
185 | "") | |
186 | #define FUNC_NAME s_scm_instruction_to_opcode | |
187 | { | |
53e28ed9 AW |
188 | struct scm_instruction *ip; |
189 | SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip); | |
190 | return SCM_I_MAKINUM (ip->opcode); | |
17e90c5e KN |
191 | } |
192 | #undef FUNC_NAME | |
193 | ||
194 | SCM_DEFINE (scm_opcode_to_instruction, "opcode->instruction", 1, 0, 0, | |
195 | (SCM op), | |
196 | "") | |
197 | #define FUNC_NAME s_scm_opcode_to_instruction | |
198 | { | |
53e28ed9 | 199 | int opcode; |
f775e51b | 200 | SCM ret = SCM_BOOL_F; |
53e28ed9 | 201 | |
62082959 | 202 | SCM_MAKE_VALIDATE (1, op, I_INUMP); |
53e28ed9 AW |
203 | opcode = SCM_I_INUM (op); |
204 | ||
f775e51b AW |
205 | if (opcode < scm_op_last) |
206 | ret = fetch_instruction_table ()[opcode].symname; | |
207 | ||
208 | if (scm_is_false (ret)) | |
209 | scm_wrong_type_arg_msg (FUNC_NAME, 1, op, "INSTRUCTION_P"); | |
53e28ed9 | 210 | |
f775e51b | 211 | return ret; |
17e90c5e KN |
212 | } |
213 | #undef FUNC_NAME | |
214 | ||
07e56b27 AW |
215 | void |
216 | scm_bootstrap_instructions (void) | |
217 | { | |
60ae5ca2 AW |
218 | scm_c_register_extension ("libguile", "scm_init_instructions", |
219 | (scm_t_extension_init_func)scm_init_instructions, | |
220 | NULL); | |
07e56b27 AW |
221 | } |
222 | ||
17e90c5e KN |
223 | void |
224 | scm_init_instructions (void) | |
225 | { | |
07e56b27 AW |
226 | scm_bootstrap_vm (); |
227 | ||
17e90c5e | 228 | #ifndef SCM_MAGIC_SNARFER |
aeeff258 | 229 | #include "libguile/instructions.x" |
17e90c5e KN |
230 | #endif |
231 | } | |
232 | ||
233 | /* | |
234 | Local Variables: | |
235 | c-file-style: "gnu" | |
236 | End: | |
237 | */ |