Translation from Scheme to GHIL, and compilation to GLIL work.
[bpt/guile.git] / src / vm_loader.c
1 /* Copyright (C) 2001 Free Software Foundation, Inc.
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
42 /* This file is included in vm_engine.c */
43
44 VM_DEFINE_LOADER (load_integer, "load-integer")
45 {
46 size_t len;
47
48 FETCH_LENGTH (len);
49 if (len <= 4)
50 {
51 long val = 0;
52 while (len-- > 0)
53 val = (val << 8) + FETCH ();
54 PUSH (scm_long2num (val));
55 NEXT;
56 }
57 else
58 SCM_MISC_ERROR ("load-integer: not implemented yet", SCM_EOL);
59 }
60
61 VM_DEFINE_LOADER (load_number, "load-number")
62 {
63 size_t len;
64
65 FETCH_LENGTH (len);
66 PUSH (scm_string_to_number (scm_from_locale_stringn (ip, len),
67 SCM_UNSPECIFIED /* radix = 10 */));
68 /* Was: scm_istring2number (ip, len, 10)); */
69 ip += len;
70 NEXT;
71 }
72
73 VM_DEFINE_LOADER (load_string, "load-string")
74 {
75 size_t len;
76 FETCH_LENGTH (len);
77 PUSH (scm_from_locale_stringn (ip, len));
78 /* Was: scm_makfromstr (ip, len, 0) */
79 ip += len;
80 NEXT;
81 }
82
83 VM_DEFINE_LOADER (load_symbol, "load-symbol")
84 {
85 size_t len;
86 FETCH_LENGTH (len);
87 PUSH (scm_mem2symbol (ip, len));
88 ip += len;
89 NEXT;
90 }
91
92 VM_DEFINE_LOADER (load_keyword, "load-keyword")
93 {
94 SCM sym;
95 size_t len;
96 FETCH_LENGTH (len);
97 sym = scm_mem2symbol (ip, len);
98 PUSH (scm_make_keyword_from_dash_symbol (sym));
99 ip += len;
100 NEXT;
101 }
102
103 VM_DEFINE_LOADER (load_module, "load-module")
104 {
105 size_t len;
106 FETCH_LENGTH (len);
107 PUSH (scm_c_lookup_env (scm_mem2symbol (ip, len)));
108 ip += len;
109 NEXT;
110 }
111
112 VM_DEFINE_LOADER (load_program, "load-program")
113 {
114 size_t len;
115 SCM prog, x;
116 struct scm_program *p;
117
118 FETCH_LENGTH (len);
119 prog = scm_c_make_program (ip, len, program);
120 p = SCM_PROGRAM_DATA (prog);
121 ip += len;
122
123 POP (x);
124
125 /* init meta data */
126 if (SCM_CONSP (x))
127 {
128 p->meta = x;
129 POP (x);
130 }
131
132 /* init object table */
133 if (SCM_VECTORP (x))
134 {
135 p->objs = x;
136 POP (x);
137 }
138
139 /* init parameters */
140 /* NOTE: format defined in system/vm/assemble.scm */
141 if (scm_is_integer (x))
142 {
143 int i = scm_to_int (x);
144 if (-128 <= i && i <= 127)
145 {
146 /* 8-bit representation */
147 p->nargs = (i >> 6) & 0x03; /* 7-6 bits */
148 p->nrest = (i >> 5) & 0x01; /* 5 bit */
149 p->nlocs = (i >> 2) & 0x07; /* 4-2 bits */
150 p->nexts = i & 0x03; /* 1-0 bits */
151 }
152 else
153 {
154 /* 16-bit representation */
155 p->nargs = (i >> 12) & 0x07; /* 15-12 bits */
156 p->nrest = (i >> 11) & 0x01; /* 11 bit */
157 p->nlocs = (i >> 4) & 0x7f; /* 10-04 bits */
158 p->nexts = i & 0x0f; /* 03-00 bits */
159 }
160 }
161 else
162 {
163 /* Other cases */
164 sp -= 4;
165 p->nargs = scm_to_int (sp[0]);
166 p->nrest = scm_to_int (sp[1]);
167 p->nlocs = scm_to_int (sp[2]);
168 p->nexts = scm_to_int (sp[3]);
169 }
170
171 PUSH (prog);
172 NEXT;
173 }
174
175 VM_DEFINE_LOADER (link, "link")
176 {
177 SCM sym;
178 size_t len;
179
180 FETCH_LENGTH (len);
181 sym = scm_mem2symbol (ip, len);
182 ip += len;
183
184 #if 0
185 *sp = scm_c_env_vcell (*sp, sym, 1);
186 #endif
187 {
188 /* Temporary hack that supports the current module system */
189 SCM mod = scm_current_module ();
190 SCM var = scm_eval_closure_lookup (scm_standard_eval_closure (mod),
191 sym, SCM_BOOL_F);
192 if (SCM_FALSEP (var))
193 /* Create a new variable if not defined yet */
194 var = scm_eval_closure_lookup (scm_standard_eval_closure (mod),
195 sym, SCM_BOOL_T);
196 PUSH (scm_variable_ref (var));
197 /* Was: SCM_VARVCELL (var)); */
198 NEXT;
199 }
200 }
201
202 /*
203 Local Variables:
204 c-file-style: "gnu"
205 End:
206 */