Fix disassembly of strings and symbols
[bpt/guile.git] / libguile / vm-i-loader.c
CommitLineData
53e28ed9 1/* Copyright (C) 2001,2008,2009 Free Software Foundation, Inc.
17e90c5e 2 *
53e28ed9 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
53e28ed9
AW
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
17e90c5e 12 *
53e28ed9
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
53e28ed9
AW
17 */
18
782a82ee 19/* FIXME! Need to check that the fetch is within the current program */
17e90c5e
KN
20
21/* This file is included in vm_engine.c */
22
a5cfddd5 23VM_DEFINE_LOADER (80, load_unsigned_integer, "load-unsigned-integer")
b912a1cd
LC
24{
25 size_t len;
26
27 FETCH_LENGTH (len);
586cfdec 28 if (SCM_LIKELY (len <= 8))
b912a1cd 29 {
586cfdec 30 scm_t_uint64 val = 0;
b912a1cd
LC
31 while (len-- > 0)
32 val = (val << 8U) + FETCH ();
33 SYNC_REGISTER ();
586cfdec 34 PUSH (scm_from_uint64 (val));
b912a1cd
LC
35 NEXT;
36 }
37 else
38 SCM_MISC_ERROR ("load-unsigned-integer: not implemented yet", SCM_EOL);
39}
40
a5cfddd5 41VM_DEFINE_LOADER (81, load_integer, "load-integer")
17e90c5e
KN
42{
43 size_t len;
ea9b4b29 44
17e90c5e 45 FETCH_LENGTH (len);
b912a1cd 46 if (SCM_LIKELY (len <= 4))
ea9b4b29 47 {
53e28ed9 48 int val = 0;
ea9b4b29
KN
49 while (len-- > 0)
50 val = (val << 8) + FETCH ();
1865ad56 51 SYNC_REGISTER ();
53e28ed9 52 PUSH (scm_from_int (val));
ea9b4b29
KN
53 NEXT;
54 }
55 else
56 SCM_MISC_ERROR ("load-integer: not implemented yet", SCM_EOL);
17e90c5e
KN
57}
58
a5cfddd5 59VM_DEFINE_LOADER (82, load_number, "load-number")
17e90c5e
KN
60{
61 size_t len;
f9e8c09d 62
17e90c5e 63 FETCH_LENGTH (len);
1865ad56 64 SYNC_REGISTER ();
b6368dbb 65 PUSH (scm_string_to_number (scm_from_locale_stringn ((char *)ip, len),
9cc649b8 66 SCM_UNDEFINED /* radix = 10 */));
f9e8c09d 67 /* Was: scm_istring2number (ip, len, 10)); */
17e90c5e
KN
68 ip += len;
69 NEXT;
70}
71
a5cfddd5 72VM_DEFINE_LOADER (83, load_string, "load-string")
17e90c5e
KN
73{
74 size_t len;
9c44cd45
MG
75 int width;
76 SCM str;
77
17e90c5e 78 FETCH_LENGTH (len);
9c44cd45 79 FETCH_WIDTH (width);
1865ad56 80 SYNC_REGISTER ();
9c44cd45
MG
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;
17e90c5e
KN
97 NEXT;
98}
99
a5cfddd5 100VM_DEFINE_LOADER (84, load_symbol, "load-symbol")
a80be762
KN
101{
102 size_t len;
9c44cd45
MG
103 int width;
104 SCM str;
a80be762 105 FETCH_LENGTH (len);
9c44cd45 106 FETCH_WIDTH (width);
1865ad56 107 SYNC_REGISTER ();
9c44cd45
MG
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;
a80be762
KN
124 NEXT;
125}
126
a5cfddd5 127VM_DEFINE_LOADER (85, load_keyword, "load-keyword")
17e90c5e 128{
17e90c5e 129 size_t len;
9c44cd45
MG
130 int width;
131 SCM str;
17e90c5e 132 FETCH_LENGTH (len);
9c44cd45 133 FETCH_WIDTH (width);
1865ad56 134 SYNC_REGISTER ();
9c44cd45
MG
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;
17e90c5e
KN
151 NEXT;
152}
153
a5cfddd5 154VM_DEFINE_LOADER (86, load_program, "load-program")
17e90c5e 155{
53e28ed9
AW
156 scm_t_uint32 len;
157 SCM objs, objcode;
17e90c5e 158
53e28ed9
AW
159 POP (objs);
160 SYNC_REGISTER ();
ac99cb0c 161
53e28ed9
AW
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 ());
ac99cb0c 164
53e28ed9 165 objcode = scm_c_make_objcode_slice (SCM_PROGRAM_OBJCODE (fp[-1]), ip);
ac47d5f6 166 len = sizeof (struct scm_objcode) + SCM_OBJCODE_TOTAL_LEN (objcode);
206a0622 167
20d47c39 168 PUSH (scm_make_program (objcode, objs, SCM_BOOL_F));
2fda0242 169
53e28ed9 170 ip += len;
17e90c5e 171
17e90c5e
KN
172 NEXT;
173}
174
a5cfddd5 175VM_DEFINE_INSTRUCTION (87, link_now, "link-now", 0, 1, 1)
17e90c5e 176{
fd358575
AW
177 SCM what;
178 POP (what);
1865ad56 179 SYNC_REGISTER ();
b7393ea1 180 PUSH (resolve_variable (what, scm_current_module ()));
6297d229
AW
181 NEXT;
182}
183
a5cfddd5 184VM_DEFINE_LOADER (88, define, "define")
cd9d95d7 185{
9c44cd45 186 SCM str, sym;
cd9d95d7
AW
187 size_t len;
188
9c44cd45 189 int width;
cd9d95d7 190 FETCH_LENGTH (len);
9c44cd45 191 FETCH_WIDTH (width);
1865ad56 192 SYNC_REGISTER ();
9c44cd45
MG
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;
cd9d95d7 209
1865ad56 210 SYNC_REGISTER ();
cd9d95d7
AW
211 PUSH (scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_T));
212 NEXT;
fdcedea6
KN
213}
214
a5cfddd5 215VM_DEFINE_LOADER (89, load_array, "load-array")
782a82ee
AW
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
53e28ed9
AW
228/*
229(defun renumber-ops ()
230 "start from top of buffer and renumber 'VM_DEFINE_FOO (\n' sequences"
231 (interactive "")
232 (save-excursion
a5cfddd5 233 (let ((counter 79)) (goto-char (point-min))
53e28ed9
AW
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
17e90c5e
KN
240/*
241 Local Variables:
242 c-file-style: "gnu"
243 End:
244*/