Add Unicode strings and symbols
[bpt/guile.git] / libguile / vm-i-loader.c
1 /* Copyright (C) 2001,2008,2009 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
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.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19 /* FIXME! Need to check that the fetch is within the current program */
20
21 /* This file is included in vm_engine.c */
22
23 VM_DEFINE_LOADER (80, load_unsigned_integer, "load-unsigned-integer")
24 {
25 size_t len;
26
27 FETCH_LENGTH (len);
28 if (SCM_LIKELY (len <= 8))
29 {
30 scm_t_uint64 val = 0;
31 while (len-- > 0)
32 val = (val << 8U) + FETCH ();
33 SYNC_REGISTER ();
34 PUSH (scm_from_uint64 (val));
35 NEXT;
36 }
37 else
38 SCM_MISC_ERROR ("load-unsigned-integer: not implemented yet", SCM_EOL);
39 }
40
41 VM_DEFINE_LOADER (81, load_integer, "load-integer")
42 {
43 size_t len;
44
45 FETCH_LENGTH (len);
46 if (SCM_LIKELY (len <= 4))
47 {
48 int val = 0;
49 while (len-- > 0)
50 val = (val << 8) + FETCH ();
51 SYNC_REGISTER ();
52 PUSH (scm_from_int (val));
53 NEXT;
54 }
55 else
56 SCM_MISC_ERROR ("load-integer: not implemented yet", SCM_EOL);
57 }
58
59 VM_DEFINE_LOADER (82, load_number, "load-number")
60 {
61 size_t len;
62
63 FETCH_LENGTH (len);
64 SYNC_REGISTER ();
65 PUSH (scm_string_to_number (scm_from_locale_stringn ((char *)ip, len),
66 SCM_UNDEFINED /* radix = 10 */));
67 /* Was: scm_istring2number (ip, len, 10)); */
68 ip += len;
69 NEXT;
70 }
71
72 VM_DEFINE_LOADER (83, load_string, "load-string")
73 {
74 size_t len;
75 int width;
76 SCM str;
77
78 FETCH_LENGTH (len);
79 FETCH_WIDTH (width);
80 SYNC_REGISTER ();
81 if (width == 1)
82 {
83 char *buf;
84 str = scm_i_make_string (len, &buf);
85 memcpy (buf, (char *) ip, len);
86 }
87 else if (width == 4)
88 {
89 scm_t_wchar *wbuf;
90 str = scm_i_make_wide_string (len, &wbuf);
91 memcpy ((char *) wbuf, (char *) ip, len * width);
92 }
93 else
94 SCM_MISC_ERROR ("load-string: invalid character width", SCM_EOL);
95 PUSH (str);
96 ip += len * width;
97 NEXT;
98 }
99
100 VM_DEFINE_LOADER (84, load_symbol, "load-symbol")
101 {
102 size_t len;
103 int width;
104 SCM str;
105 FETCH_LENGTH (len);
106 FETCH_WIDTH (width);
107 SYNC_REGISTER ();
108 if (width == 1)
109 {
110 char *buf;
111 str = scm_i_make_string (len, &buf);
112 memcpy (buf, (char *) ip, len);
113 }
114 else if (width == 4)
115 {
116 scm_t_wchar *wbuf;
117 str = scm_i_make_wide_string (len, &wbuf);
118 memcpy ((char *) wbuf, (char *) ip, len * width);
119 }
120 else
121 SCM_MISC_ERROR ("load-symbol: invalid character width", SCM_EOL);
122 PUSH (scm_string_to_symbol (str));
123 ip += len * width;
124 NEXT;
125 }
126
127 VM_DEFINE_LOADER (85, load_keyword, "load-keyword")
128 {
129 size_t len;
130 int width;
131 SCM str;
132 FETCH_LENGTH (len);
133 FETCH_WIDTH (width);
134 SYNC_REGISTER ();
135 if (width == 1)
136 {
137 char *buf;
138 str = scm_i_make_string (len, &buf);
139 memcpy (buf, (char *) ip, len);
140 }
141 else if (width == 4)
142 {
143 scm_t_wchar *wbuf;
144 str = scm_i_make_wide_string (len, &wbuf);
145 memcpy ((char *) wbuf, (char *) ip, len * width);
146 }
147 else
148 SCM_MISC_ERROR ("load-keyword: invalid character width", SCM_EOL);
149 PUSH (scm_symbol_to_keyword (scm_string_to_symbol (str)));
150 ip += len * width;
151 NEXT;
152 }
153
154 VM_DEFINE_LOADER (86, load_program, "load-program")
155 {
156 scm_t_uint32 len;
157 SCM objs, objcode;
158
159 POP (objs);
160 SYNC_REGISTER ();
161
162 if (scm_is_vector (objs) && scm_is_false (scm_c_vector_ref (objs, 0)))
163 scm_c_vector_set_x (objs, 0, scm_current_module ());
164
165 objcode = scm_c_make_objcode_slice (SCM_PROGRAM_OBJCODE (fp[-1]), ip);
166 len = sizeof (struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode);
167
168 PUSH (scm_make_program (objcode, objs, SCM_BOOL_F));
169
170 ip += len;
171
172 NEXT;
173 }
174
175 VM_DEFINE_INSTRUCTION (87, link_now, "link-now", 0, 1, 1)
176 {
177 SCM what;
178 POP (what);
179 SYNC_REGISTER ();
180 PUSH (resolve_variable (what, scm_current_module ()));
181 NEXT;
182 }
183
184 VM_DEFINE_LOADER (88, define, "define")
185 {
186 SCM str, sym;
187 size_t len;
188
189 int width;
190 FETCH_LENGTH (len);
191 FETCH_WIDTH (width);
192 SYNC_REGISTER ();
193 if (width == 1)
194 {
195 char *buf;
196 str = scm_i_make_string (len, &buf);
197 memcpy (buf, (char *) ip, len);
198 }
199 else if (width == 4)
200 {
201 scm_t_wchar *wbuf;
202 str = scm_i_make_wide_string (len, &wbuf);
203 memcpy ((char *) wbuf, (char *) ip, len * width);
204 }
205 else
206 SCM_MISC_ERROR ("load define: invalid character width", SCM_EOL);
207 sym = scm_string_to_symbol (str);
208 ip += len * width;
209
210 SYNC_REGISTER ();
211 PUSH (scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_T));
212 NEXT;
213 }
214
215 VM_DEFINE_LOADER (89, load_array, "load-array")
216 {
217 SCM type, shape;
218 size_t len;
219 FETCH_LENGTH (len);
220 POP (shape);
221 POP (type);
222 SYNC_REGISTER ();
223 PUSH (scm_from_contiguous_typed_array (type, shape, ip, len));
224 ip += len;
225 NEXT;
226 }
227
228 /*
229 (defun renumber-ops ()
230 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
231 (interactive "")
232 (save-excursion
233 (let ((counter 79)) (goto-char (point-min))
234 (while (re-search-forward "^VM_DEFINE_[^ ]+ (\\([^,]+\\)," (point-max) t)
235 (replace-match
236 (number-to-string (setq counter (1+ counter)))
237 t t nil 1)))))
238 */
239
240 /*
241 Local Variables:
242 c-file-style: "gnu"
243 End:
244 */