add assembly intermediate language
[bpt/guile.git] / libguile / vm-i-loader.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
42/* This file is included in vm_engine.c */
43
3616e9e9 44VM_DEFINE_LOADER (load_integer, "load-integer")
17e90c5e
KN
45{
46 size_t len;
ea9b4b29 47
17e90c5e 48 FETCH_LENGTH (len);
ea9b4b29
KN
49 if (len <= 4)
50 {
51 long val = 0;
52 while (len-- > 0)
53 val = (val << 8) + FETCH ();
1865ad56 54 SYNC_REGISTER ();
f41cb00c 55 PUSH (scm_from_ulong (val));
ea9b4b29
KN
56 NEXT;
57 }
58 else
59 SCM_MISC_ERROR ("load-integer: not implemented yet", SCM_EOL);
17e90c5e
KN
60}
61
3616e9e9 62VM_DEFINE_LOADER (load_number, "load-number")
17e90c5e
KN
63{
64 size_t len;
f9e8c09d 65
17e90c5e 66 FETCH_LENGTH (len);
1865ad56 67 SYNC_REGISTER ();
b6368dbb 68 PUSH (scm_string_to_number (scm_from_locale_stringn ((char *)ip, len),
9cc649b8 69 SCM_UNDEFINED /* radix = 10 */));
f9e8c09d 70 /* Was: scm_istring2number (ip, len, 10)); */
17e90c5e
KN
71 ip += len;
72 NEXT;
73}
74
3616e9e9 75VM_DEFINE_LOADER (load_string, "load-string")
17e90c5e
KN
76{
77 size_t len;
78 FETCH_LENGTH (len);
1865ad56 79 SYNC_REGISTER ();
b6368dbb 80 PUSH (scm_from_locale_stringn ((char *)ip, len));
f9e8c09d 81 /* Was: scm_makfromstr (ip, len, 0) */
17e90c5e
KN
82 ip += len;
83 NEXT;
84}
85
3616e9e9 86VM_DEFINE_LOADER (load_symbol, "load-symbol")
a80be762
KN
87{
88 size_t len;
89 FETCH_LENGTH (len);
1865ad56 90 SYNC_REGISTER ();
b6368dbb 91 PUSH (scm_from_locale_symboln ((char *)ip, len));
a80be762
KN
92 ip += len;
93 NEXT;
94}
95
3616e9e9 96VM_DEFINE_LOADER (load_keyword, "load-keyword")
17e90c5e 97{
17e90c5e
KN
98 size_t len;
99 FETCH_LENGTH (len);
1865ad56 100 SYNC_REGISTER ();
a52b96a7 101 PUSH (scm_from_locale_keywordn ((char *)ip, len));
17e90c5e
KN
102 ip += len;
103 NEXT;
104}
105
3616e9e9 106VM_DEFINE_LOADER (load_program, "load-program")
17e90c5e
KN
107{
108 size_t len;
2fda0242 109 SCM prog, x, objs = SCM_BOOL_F, meta = SCM_BOOL_F;
ac99cb0c 110 struct scm_program *p;
17e90c5e 111
ac99cb0c
KN
112 POP (x);
113
114 /* init meta data */
13906f97 115 if (SCM_PROGRAM_P (x))
ac99cb0c 116 {
2fda0242 117 meta = x;
ac99cb0c
KN
118 POP (x);
119 }
120
206a0622 121 /* init object table */
238e7a11 122 if (scm_is_vector (x))
206a0622 123 {
2fda0242
AW
124 objs = x;
125 scm_c_vector_set_x (objs, 0, scm_current_module ());
126 scm_c_vector_set_x (objs, 1, meta);
ac99cb0c 127 POP (x);
206a0622
KN
128 }
129
2fda0242
AW
130 FETCH_LENGTH (len);
131 SYNC_REGISTER ();
132 prog = scm_c_make_program (ip, len, objs, program);
133 p = SCM_PROGRAM_DATA (prog);
134 ip += len;
135
206a0622 136 /* init parameters */
4bfb26f5 137 /* NOTE: format defined in system/vm/assemble.scm */
2d80426a 138 if (SCM_I_INUMP (x))
17e90c5e 139 {
5c492620
AW
140 scm_t_uint16 s = (scm_t_uint16)SCM_I_INUM (x);
141 /* 16-bit representation */
142 p->nargs = (s >> 12) & 0x0f; /* 15-12 bits */
143 p->nrest = (s >> 11) & 0x01; /* 11 bit */
144 p->nlocs = (s >> 4) & 0x7f; /* 10-04 bits */
145 p->nexts = s & 0x0f; /* 03-00 bits */
17e90c5e
KN
146 }
147 else
148 {
3d5ee0cd 149 /* Other cases */
d79d908e 150 /* x is #f, and already popped off */
11ea1aba
AW
151 POP (x); p->nexts = scm_to_unsigned_integer (x, 0, 255);
152 POP (x); p->nlocs = scm_to_unsigned_integer (x, 0, 255);
153 POP (x); p->nrest = scm_to_unsigned_integer (x, 0, 1);
154 POP (x); p->nargs = scm_to_unsigned_integer (x, 0, 255);
17e90c5e
KN
155 }
156
ac99cb0c 157 PUSH (prog);
17e90c5e
KN
158 NEXT;
159}
160
9246a486 161VM_DEFINE_INSTRUCTION (link_now, "link-now", 0, 1, 1)
17e90c5e 162{
fd358575
AW
163 SCM what;
164 POP (what);
1865ad56 165 SYNC_REGISTER ();
fd358575
AW
166 if (SCM_LIKELY (SCM_SYMBOLP (what)))
167 {
168 PUSH (scm_lookup (what)); /* might longjmp */
169 }
170 else
171 {
172 SCM mod;
173 /* compilation of @ or @@
174 `what' is a three-element list: (MODNAME SYM INTERFACE?)
175 INTERFACE? is #t if we compiled @ or #f if we compiled @@
176 */
177 mod = scm_resolve_module (SCM_CAR (what));
178 if (scm_is_true (SCM_CADDR (what)))
179 mod = scm_module_public_interface (mod);
180 if (SCM_FALSEP (mod))
181 {
182 err_args = SCM_LIST1 (SCM_CAR (what));
183 goto vm_error_no_such_module;
184 }
185 /* might longjmp */
186 PUSH (scm_module_lookup (mod, SCM_CADR (what)));
187 }
188
6297d229
AW
189 NEXT;
190}
191
cd9d95d7
AW
192VM_DEFINE_LOADER (define, "define")
193{
194 SCM sym;
195 size_t len;
196
197 FETCH_LENGTH (len);
1865ad56 198 SYNC_REGISTER ();
cd9d95d7
AW
199 sym = scm_from_locale_symboln ((char *)ip, len);
200 ip += len;
201
1865ad56 202 SYNC_REGISTER ();
cd9d95d7
AW
203 PUSH (scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_T));
204 NEXT;
fdcedea6
KN
205}
206
17e90c5e
KN
207/*
208 Local Variables:
209 c-file-style: "gnu"
210 End:
211*/