Commit | Line | Data |
---|---|---|
a6029b97 | 1 | /* Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc. |
17e90c5e | 2 | * |
560b9c25 | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
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. | |
17e90c5e | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
560b9c25 AW |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
17e90c5e | 12 | * |
560b9c25 AW |
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 | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
560b9c25 | 17 | */ |
17e90c5e | 18 | |
13c47753 AW |
19 | #if HAVE_CONFIG_H |
20 | # include <config.h> | |
21 | #endif | |
22 | ||
17e90c5e | 23 | #include <string.h> |
560b9c25 AW |
24 | |
25 | #include "_scm.h" | |
17e90c5e KN |
26 | #include "instructions.h" |
27 | ||
53e28ed9 AW |
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' | |
33 | macro). */ | |
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 */ | |
f775e51b | 38 | SCM symname; /* filled in later */ |
17e90c5e KN |
39 | }; |
40 | ||
53e28ed9 AW |
41 | #define SCM_VALIDATE_LOOKUP_INSTRUCTION(pos, var, cvar) \ |
42 | do { \ | |
43 | cvar = scm_lookup_instruction_by_name (var); \ | |
44 | SCM_ASSERT_TYPE (cvar, var, pos, FUNC_NAME, "INSTRUCTION_P"); \ | |
45 | } while (0) | |
17e90c5e | 46 | |
53e28ed9 | 47 | |
f775e51b AW |
48 | static struct scm_instruction* |
49 | fetch_instruction_table () | |
50 | { | |
51 | static struct scm_instruction *table = NULL; | |
52 | ||
53 | if (SCM_UNLIKELY (!table)) | |
54 | { | |
ad47e359 | 55 | size_t bytes = SCM_VM_NUM_INSTRUCTIONS * sizeof(struct scm_instruction); |
f775e51b AW |
56 | int i; |
57 | table = malloc (bytes); | |
58 | memset (table, 0, bytes); | |
59 | #define VM_INSTRUCTION_TO_TABLE 1 | |
aeeff258 AW |
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> | |
f775e51b | 64 | #undef VM_INSTRUCTION_TO_TABLE |
ad47e359 | 65 | for (i = 0; i < SCM_VM_NUM_INSTRUCTIONS; i++) |
f775e51b AW |
66 | { |
67 | table[i].opcode = i; | |
68 | if (table[i].name) | |
f39448c5 | 69 | table[i].symname = scm_from_locale_symbol (table[i].name); |
f775e51b AW |
70 | else |
71 | table[i].symname = SCM_BOOL_F; | |
72 | } | |
73 | } | |
74 | return table; | |
75 | } | |
76 | ||
53e28ed9 AW |
77 | static struct scm_instruction * |
78 | scm_lookup_instruction_by_name (SCM name) | |
17e90c5e | 79 | { |
f775e51b AW |
80 | static SCM instructions_by_name = SCM_BOOL_F; |
81 | struct scm_instruction *table = fetch_instruction_table (); | |
82 | SCM op; | |
83 | ||
5c8cefe5 LC |
84 | if (SCM_UNLIKELY (scm_is_false (instructions_by_name))) |
85 | { | |
86 | unsigned int i; | |
87 | ||
88 | instructions_by_name = | |
89 | scm_make_hash_table (SCM_I_MAKINUM (SCM_VM_NUM_INSTRUCTIONS)); | |
90 | ||
ad47e359 | 91 | for (i = 0; i < SCM_VM_NUM_INSTRUCTIONS; i++) |
f775e51b AW |
92 | if (scm_is_true (table[i].symname)) |
93 | scm_hashq_set_x (instructions_by_name, table[i].symname, | |
94 | SCM_I_MAKINUM (i)); | |
f775e51b | 95 | } |
5c8cefe5 | 96 | |
f775e51b AW |
97 | op = scm_hashq_ref (instructions_by_name, name, SCM_UNDEFINED); |
98 | if (SCM_I_INUMP (op)) | |
99 | return &table[SCM_I_INUM (op)]; | |
100 | ||
101 | return NULL; | |
17e90c5e KN |
102 | } |
103 | ||
53e28ed9 | 104 | |
17e90c5e KN |
105 | /* Scheme interface */ |
106 | ||
107 | SCM_DEFINE (scm_instruction_list, "instruction-list", 0, 0, 0, | |
108 | (void), | |
109 | "") | |
110 | #define FUNC_NAME s_scm_instruction_list | |
111 | { | |
112 | SCM list = SCM_EOL; | |
39141c87 AW |
113 | int i; |
114 | struct scm_instruction *ip = fetch_instruction_table (); | |
ad47e359 | 115 | for (i = 0; i < SCM_VM_NUM_INSTRUCTIONS; i++) |
39141c87 AW |
116 | if (ip[i].name) |
117 | list = scm_cons (ip[i].symname, list); | |
17e90c5e KN |
118 | return scm_reverse_x (list, SCM_EOL); |
119 | } | |
120 | #undef FUNC_NAME | |
121 | ||
122 | SCM_DEFINE (scm_instruction_p, "instruction?", 1, 0, 0, | |
123 | (SCM obj), | |
124 | "") | |
125 | #define FUNC_NAME s_scm_instruction_p | |
126 | { | |
5c8cefe5 | 127 | return scm_from_bool (scm_lookup_instruction_by_name (obj) != NULL); |
17e90c5e KN |
128 | } |
129 | #undef FUNC_NAME | |
130 | ||
131 | SCM_DEFINE (scm_instruction_length, "instruction-length", 1, 0, 0, | |
132 | (SCM inst), | |
133 | "") | |
134 | #define FUNC_NAME s_scm_instruction_length | |
135 | { | |
53e28ed9 AW |
136 | struct scm_instruction *ip; |
137 | SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip); | |
138 | return SCM_I_MAKINUM (ip->len); | |
46cd9a34 KN |
139 | } |
140 | #undef FUNC_NAME | |
141 | ||
142 | SCM_DEFINE (scm_instruction_pops, "instruction-pops", 1, 0, 0, | |
143 | (SCM inst), | |
144 | "") | |
145 | #define FUNC_NAME s_scm_instruction_pops | |
146 | { | |
53e28ed9 AW |
147 | struct scm_instruction *ip; |
148 | SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip); | |
149 | return SCM_I_MAKINUM (ip->npop); | |
46cd9a34 KN |
150 | } |
151 | #undef FUNC_NAME | |
152 | ||
153 | SCM_DEFINE (scm_instruction_pushes, "instruction-pushes", 1, 0, 0, | |
154 | (SCM inst), | |
155 | "") | |
156 | #define FUNC_NAME s_scm_instruction_pushes | |
157 | { | |
53e28ed9 AW |
158 | struct scm_instruction *ip; |
159 | SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip); | |
160 | return SCM_I_MAKINUM (ip->npush); | |
17e90c5e KN |
161 | } |
162 | #undef FUNC_NAME | |
163 | ||
164 | SCM_DEFINE (scm_instruction_to_opcode, "instruction->opcode", 1, 0, 0, | |
165 | (SCM inst), | |
166 | "") | |
167 | #define FUNC_NAME s_scm_instruction_to_opcode | |
168 | { | |
53e28ed9 AW |
169 | struct scm_instruction *ip; |
170 | SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip); | |
171 | return SCM_I_MAKINUM (ip->opcode); | |
17e90c5e KN |
172 | } |
173 | #undef FUNC_NAME | |
174 | ||
175 | SCM_DEFINE (scm_opcode_to_instruction, "opcode->instruction", 1, 0, 0, | |
176 | (SCM op), | |
177 | "") | |
178 | #define FUNC_NAME s_scm_opcode_to_instruction | |
179 | { | |
e25f3727 | 180 | scm_t_signed_bits opcode; |
f775e51b | 181 | SCM ret = SCM_BOOL_F; |
53e28ed9 | 182 | |
62082959 | 183 | SCM_MAKE_VALIDATE (1, op, I_INUMP); |
53e28ed9 AW |
184 | opcode = SCM_I_INUM (op); |
185 | ||
ad47e359 | 186 | if (opcode >= 0 && opcode < SCM_VM_NUM_INSTRUCTIONS) |
f775e51b AW |
187 | ret = fetch_instruction_table ()[opcode].symname; |
188 | ||
189 | if (scm_is_false (ret)) | |
190 | scm_wrong_type_arg_msg (FUNC_NAME, 1, op, "INSTRUCTION_P"); | |
53e28ed9 | 191 | |
f775e51b | 192 | return ret; |
17e90c5e KN |
193 | } |
194 | #undef FUNC_NAME | |
195 | ||
07e56b27 AW |
196 | void |
197 | scm_bootstrap_instructions (void) | |
198 | { | |
44602b08 AW |
199 | scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION, |
200 | "scm_init_instructions", | |
60ae5ca2 AW |
201 | (scm_t_extension_init_func)scm_init_instructions, |
202 | NULL); | |
07e56b27 AW |
203 | } |
204 | ||
17e90c5e KN |
205 | void |
206 | scm_init_instructions (void) | |
207 | { | |
208 | #ifndef SCM_MAGIC_SNARFER | |
aeeff258 | 209 | #include "libguile/instructions.x" |
17e90c5e KN |
210 | #endif |
211 | } | |
212 | ||
213 | /* | |
214 | Local Variables: | |
215 | c-file-style: "gnu" | |
216 | End: | |
217 | */ |