avoid 8 words of allocation per lambda, whoooo
[bpt/guile.git] / libguile / instructions.c
CommitLineData
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
50struct 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 */
60};
61
62static struct scm_instruction scm_instruction_table[] = {
17e90c5e 63#define VM_INSTRUCTION_TO_TABLE 1
83495480
AW
64#include "vm-expand.h"
65#include "vm-i-system.i"
66#include "vm-i-scheme.i"
67#include "vm-i-loader.i"
17e90c5e
KN
68#undef VM_INSTRUCTION_TO_TABLE
69 {scm_op_last}
70};
71
53e28ed9
AW
72#define SCM_VALIDATE_LOOKUP_INSTRUCTION(pos, var, cvar) \
73 do { \
74 cvar = scm_lookup_instruction_by_name (var); \
75 SCM_ASSERT_TYPE (cvar, var, pos, FUNC_NAME, "INSTRUCTION_P"); \
76 } while (0)
17e90c5e 77
53e28ed9
AW
78
79static struct scm_instruction *
80scm_lookup_instruction_by_name (SCM name)
17e90c5e
KN
81{
82 struct scm_instruction *ip;
d8eeb67c
LC
83 char *symbol;
84
17e90c5e
KN
85 if (SCM_SYMBOLP (name))
86 for (ip = scm_instruction_table; ip->opcode != scm_op_last; ip++)
d8eeb67c
LC
87 {
88 symbol = scm_to_locale_string (scm_symbol_to_string (name));
89 if ((symbol) && (strcmp (ip->name, symbol) == 0))
90 {
91 free (symbol);
92 return ip;
93 }
94
95 if (symbol)
96 free (symbol);
97 }
98
17e90c5e
KN
99 return 0;
100}
101
53e28ed9 102
17e90c5e
KN
103/* Scheme interface */
104
105SCM_DEFINE (scm_instruction_list, "instruction-list", 0, 0, 0,
106 (void),
107 "")
108#define FUNC_NAME s_scm_instruction_list
109{
110 SCM list = SCM_EOL;
111 struct scm_instruction *ip;
112 for (ip = scm_instruction_table; ip->opcode != scm_op_last; ip++)
fa19602c 113 list = scm_cons (scm_from_locale_symbol (ip->name), list);
17e90c5e
KN
114 return scm_reverse_x (list, SCM_EOL);
115}
116#undef FUNC_NAME
117
118SCM_DEFINE (scm_instruction_p, "instruction?", 1, 0, 0,
119 (SCM obj),
120 "")
121#define FUNC_NAME s_scm_instruction_p
122{
53e28ed9 123 return SCM_BOOL (scm_lookup_instruction_by_name (obj));
17e90c5e
KN
124}
125#undef FUNC_NAME
126
127SCM_DEFINE (scm_instruction_length, "instruction-length", 1, 0, 0,
128 (SCM inst),
129 "")
130#define FUNC_NAME s_scm_instruction_length
131{
53e28ed9
AW
132 struct scm_instruction *ip;
133 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip);
134 return SCM_I_MAKINUM (ip->len);
46cd9a34
KN
135}
136#undef FUNC_NAME
137
138SCM_DEFINE (scm_instruction_pops, "instruction-pops", 1, 0, 0,
139 (SCM inst),
140 "")
141#define FUNC_NAME s_scm_instruction_pops
142{
53e28ed9
AW
143 struct scm_instruction *ip;
144 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip);
145 return SCM_I_MAKINUM (ip->npop);
46cd9a34
KN
146}
147#undef FUNC_NAME
148
149SCM_DEFINE (scm_instruction_pushes, "instruction-pushes", 1, 0, 0,
150 (SCM inst),
151 "")
152#define FUNC_NAME s_scm_instruction_pushes
153{
53e28ed9
AW
154 struct scm_instruction *ip;
155 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip);
156 return SCM_I_MAKINUM (ip->npush);
17e90c5e
KN
157}
158#undef FUNC_NAME
159
160SCM_DEFINE (scm_instruction_to_opcode, "instruction->opcode", 1, 0, 0,
161 (SCM inst),
162 "")
163#define FUNC_NAME s_scm_instruction_to_opcode
164{
53e28ed9
AW
165 struct scm_instruction *ip;
166 SCM_VALIDATE_LOOKUP_INSTRUCTION (1, inst, ip);
167 return SCM_I_MAKINUM (ip->opcode);
17e90c5e
KN
168}
169#undef FUNC_NAME
170
171SCM_DEFINE (scm_opcode_to_instruction, "opcode->instruction", 1, 0, 0,
172 (SCM op),
173 "")
174#define FUNC_NAME s_scm_opcode_to_instruction
175{
53e28ed9
AW
176 struct scm_instruction *ip;
177 int opcode;
178
62082959 179 SCM_MAKE_VALIDATE (1, op, I_INUMP);
53e28ed9
AW
180 opcode = SCM_I_INUM (op);
181
182 for (ip = scm_instruction_table; ip->opcode != scm_op_last; ip++)
183 if (opcode == ip->opcode)
184 return scm_from_locale_symbol (ip->name);
185
186 scm_wrong_type_arg_msg (FUNC_NAME, 1, op, "INSTRUCTION_P");
187 return SCM_BOOL_F; /* not reached */
17e90c5e
KN
188}
189#undef FUNC_NAME
190
07e56b27
AW
191void
192scm_bootstrap_instructions (void)
193{
194}
195
17e90c5e
KN
196void
197scm_init_instructions (void)
198{
07e56b27
AW
199 scm_bootstrap_vm ();
200
17e90c5e
KN
201#ifndef SCM_MAGIC_SNARFER
202#include "instructions.x"
203#endif
204}
205
206/*
207 Local Variables:
208 c-file-style: "gnu"
209 End:
210*/