Debian Codegraph @

src/ikarus-fasl.c

raw
1 /*
2 * Ikarus Scheme -- A compiler for R6RS Scheme.
3 * Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19
20
21 #include "ikarus-data.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <strings.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <assert.h>
32 #include <sys/mman.h>
33 #include <dlfcn.h>
34
35
36 #ifndef RTLD_DEFAULT
37 #define RTLD_DEFAULT 0
38 #endif
39
40
41 typedef struct {
42 char* membase;
43 char* memp;
44 char* memq;
45 ikptr code_ap;
46 ikptr code_ep;
47 ikptr* marks;
48 int marks_size;
49 } fasl_port;
50
51 static ikptr ik_fasl_read(ikpcb* pcb, fasl_port* p);
52
53 void ik_fasl_load(ikpcb* pcb, char* fasl_file){
54 int fd = open(fasl_file, O_RDONLY);
55 if(fd == -1){
56 fprintf(stderr,
57 "ikarus: failed to open boot file \"%s\": %s\n",
58 fasl_file,
59 strerror(errno));
60 ikarus_usage_short();
61 exit(-1);
62 }
63 int filesize;
64 {
65 struct stat buf;
66 int err = fstat(fd, &buf);
67 if(err != 0){
68 fprintf(stderr,
69 "ikarus: failed to stat \"%s\": %s\n",
70 fasl_file,
71 strerror(errno));
72 exit(-1);
73 }
74 filesize = buf.st_size;
75 }
76 int mapsize = ((filesize + pagesize - 1) / pagesize) * pagesize;
77 char* mem = mmap(
78 0,
79 mapsize,
80 PROT_READ,
81 MAP_PRIVATE,
82 fd,
83 0);
84 if(mem == MAP_FAILED){
85 fprintf(stderr,
86 "ikarus: mapping failed for %s: %s\n",
87 fasl_file,
88 strerror(errno));
89 exit(-1);
90 }
91 fasl_port p;
92 p.membase = mem;
93 p.memp = mem;
94 p.memq = mem + filesize;
95 p.marks = 0;
96 p.marks_size = 0;
97 while(p.memp < p.memq){
98 p.code_ap = 0;
99 p.code_ep = 0;
100 ikptr v = ik_fasl_read(pcb, &p);
101 if(p.marks_size){
102 ik_munmap((ikptr)(long)p.marks, p.marks_size*sizeof(ikptr*));
103 p.marks = 0;
104 p.marks_size = 0;
105 }
106 if(p.memp == p.memq){
107 int err = munmap(mem, mapsize);
108 if(err != 0){
109 fprintf(stderr, "Failed to unmap fasl file: %s\n", strerror(errno));
110 exit(-1);
111 }
112 close(fd);
113 }
114 ikptr val = ik_exec_code(pcb, v, 0, 0);
115 if(val != void_object){
116 ik_print(val);
117 }
118 }
119 if(p.memp != p.memq){
120 fprintf(stderr, "fasl-read did not reach eof!\n");
121 exit(-10);
122 }
123 }
124
125 static ikptr
126 alloc_code(long int size, ikpcb* pcb, fasl_port* p){
127 long int asize = align(size);
128 ikptr ap = p->code_ap;
129 ikptr nap = ap + asize;
130 if(nap <= p->code_ep){
131 p->code_ap = nap;
132 return ap;
133 } else if (asize < pagesize){
134 ikptr mem = ik_mmap_code(pagesize, 0, pcb);
135 long int bytes_remaining = pagesize - asize;
136 long int previous_bytes =
137 ((unsigned long int)p->code_ep) - ((unsigned long int)ap);
138 if(bytes_remaining <= previous_bytes){
139 return mem;
140 } else {
141 p->code_ap = mem+asize;
142 p->code_ep = mem+pagesize;
143 return mem;
144 }
145 } else {
146 long int asize = align_to_next_page(size);
147 ikptr mem = ik_mmap_code(asize, 0, pcb);
148 return mem;
149 }
150 }
151
152
153 void
154 ik_relocate_code(ikptr code){
155 ikptr vec = ref(code, disp_code_reloc_vector);
156 ikptr size = ref(vec, off_vector_length);
157 ikptr data = code + disp_code_data;
158 ikptr p = vec + off_vector_data;
159 ikptr q = p + size;
160 while(p < q){
161 long int r = unfix(ref(p, 0));
162 if(r == 0){
163 fprintf(stderr, "unset reloc!\n");
164 exit(-1);
165 }
166 long int tag = r & 3;
167 long int code_off = r >> 2;
168 if(tag == 0){
169 /* vanilla object */
170 ref(data, code_off) = ref(p, wordsize);
171 p += (2*wordsize);
172 }
173 else if(tag == 2){
174 /* displaced object */
175 long int obj_off = unfix(ref(p, wordsize));
176 ikptr obj = ref(p, 2*wordsize);
177 ref(data, code_off) = obj + obj_off;
178 p += (3*wordsize);
179 }
180 else if(tag == 3){
181 /* jump label */
182 long int obj_off = unfix(ref(p, wordsize));
183 long int obj = ref(p, 2*wordsize);
184 long int displaced_object = obj + obj_off;
185 long int next_word = data + code_off + 4;
186 long int relative_distance = displaced_object - next_word;
187 #if 0
188 if(wordsize == 8){
189 relative_distance += 4;
190 }
191 #endif
192 *((int*)(data+code_off)) = relative_distance;
193 // ref(next_word, -wordsize) = relative_distance;
194 p += (3*wordsize);
195 }
196 else if(tag == 1){
197 /* foreign object */
198 ikptr str = ref(p, wordsize);
199 char* name;
200 if(tagof(str) == bytevector_tag){
201 name = (char*)(long) str + off_bytevector_data;
202 } else {
203 fprintf(stderr, "foreign name is not a bytevector\n");
204 exit(-1);
205 }
206 dlerror();
207 void* sym = dlsym(RTLD_DEFAULT, name);
208 char* err = dlerror();
209 if(err){
210 fprintf(stderr, "failed to find foreign name %s: %s\n", name, err);
211 exit(-1);
212 }
213 ref(data,code_off) = (ikptr)sym;
214 p += (2*wordsize);
215 }
216 else {
217 fprintf(stderr, "invalid reloc 0x%016lx (tag=%ld)\n", r, tag);
218 exit(-1);
219 }
220 }
221 }
222
223
224 static char fasl_read_byte(fasl_port* p){
225 if(p->memp < p->memq){
226 char c = *(p->memp);
227 p->memp++;
228 return c;
229 } else {
230 fprintf(stderr, "fasl_read_byte: read beyond eof\n");
231 exit(-1);
232 }
233 }
234
235 static void fasl_read_buf(fasl_port* p, void* buf, int n){
236 if((p->memp+n) <= p->memq){
237 memcpy(buf, p->memp, n);
238 p->memp += n;
239 } else {
240 fprintf(stderr, "fasl_read_buf: read beyond eof\n");
241 exit(-1);
242 }
243 }
244 typedef struct{
245 int code_size;
246 int reloc_size;
247 ikptr closure_size;
248 } code_header;
249
250
251
252 static ikptr do_read(ikpcb* pcb, fasl_port* p){
253 char c = fasl_read_byte(p);
254 int put_mark_index = 0;
255 if(c == '>'){
256 int idx = 0;
257 fasl_read_buf(p, &idx, sizeof(int));
258 put_mark_index = idx;
259 c = fasl_read_byte(p);
260 if(idx <= 0){
261 fprintf(stderr, "fasl_read: invalid index %d\n", idx);
262 exit(-1);
263 }
264 if(p->marks){
265 if(idx >= p->marks_size){
266 fprintf(stderr, "BUG: mark too big: %d\n", idx);
267 exit(-1);
268 }
269 if(idx < p->marks_size){
270 if(p->marks[idx] != 0){
271 fprintf(stderr, "mark %d already set\n", idx);
272 ik_print(p->marks[idx]);
273 exit(-1);
274 }
275 }
276 }
277 else {
278 /* allocate marks */
279 p->marks = (ikptr*)(long)ik_mmap(2*pagesize*sizeof(ikptr*));
280 bzero(p->marks, 2*pagesize*sizeof(ikptr*));
281 p->marks_size = 2*pagesize;
282 }
283 }
284 if(c == 'x'){
285 long int code_size;
286 ikptr freevars;
287 fasl_read_buf(p, &code_size, sizeof(long int));
288 fasl_read_buf(p, &freevars, sizeof(ikptr));
289 ikptr annotation = do_read(pcb, p);
290 ikptr code = alloc_code(align(code_size+disp_code_data), pcb, p);
291 ref(code, 0) = code_tag;
292 ref(code, disp_code_code_size) = fix(code_size);
293 ref(code, disp_code_freevars) = freevars;
294 ref(code, disp_code_annotation) = annotation;
295 fasl_read_buf(p, (void*)(disp_code_data+(long)code), code_size);
296 if(put_mark_index){
297 p->marks[put_mark_index] = code+vector_tag;
298 }
299 ref(code, disp_code_reloc_vector) = do_read(pcb, p);
300 ik_relocate_code(code);
301 return code+vector_tag;
302 }
303 else if(c == 'P'){
304 ikptr pair = ik_unsafe_alloc(pcb, pair_size) + pair_tag;
305 if(put_mark_index){
306 p->marks[put_mark_index] = pair;
307 }
308 ref(pair, off_car) = do_read(pcb, p);
309 ref(pair, off_cdr) = do_read(pcb, p);
310 return pair;
311 }
312 else if(c == 'M'){
313 /* symbol */
314 ikptr str = do_read(pcb, p);
315 ikptr sym = ikrt_string_to_symbol(str, pcb);
316 if(put_mark_index){
317 p->marks[put_mark_index] = sym;
318 }
319 return sym;
320 }
321 else if(c == 's'){
322 /* ascii string */
323 long int len;
324 fasl_read_buf(p, &len, sizeof(long int));
325 long int size = align(len*string_char_size + disp_string_data);
326 ikptr str = ik_unsafe_alloc(pcb, size) + string_tag;
327 ref(str, off_string_length) = fix(len);
328 fasl_read_buf(p, (char*)(long)str+off_string_data, len);
329 {
330 unsigned char* pi = (unsigned char*)(long)(str+off_string_data);
331 ikchar* pj = (ikchar*)(long)(str+off_string_data);
332 long int i = len-1;
333 for(i=len-1; i >= 0; i--){
334 pj[i] = integer_to_char(pi[i]);
335 }
336 }
337 //str[off_string_data+len] = 0;
338 if(put_mark_index){
339 p->marks[put_mark_index] = str;
340 }
341 return str;
342 }
343 else if(c == 'S'){
344 /* string */
345 long int len;
346 fasl_read_buf(p, &len, sizeof(long int));
347 long int size = align(len*string_char_size + disp_string_data);
348 ikptr str = ik_unsafe_alloc(pcb, size) + string_tag;
349 ref(str, off_string_length) = fix(len);
350 long int i;
351 for(i=0; i<len; i++){
352 ikchar c;
353 fasl_read_buf(p, &c, sizeof(ikchar));
354 string_set(str, i, integer_to_char(c));
355 }
356 //str[off_string_data+len*string_char_size] = 0;
357 if(put_mark_index){
358 p->marks[put_mark_index] = str;
359 }
360 return str;
361 }
362 else if(c == 'V'){
363 long int len;
364 fasl_read_buf(p, &len, sizeof(long int));
365 long int size = align(len * wordsize + disp_vector_data);
366 ikptr vec = ik_unsafe_alloc(pcb, size) + vector_tag;
367 if(put_mark_index){
368 p->marks[put_mark_index] = vec;
369 }
370 ref(vec, off_vector_length) = fix(len);
371 long int i;
372 for(i=0; i<len; i++){
373 ref(vec, off_vector_data + i*wordsize) = do_read(pcb, p);
374 }
375 return vec;
376 }
377 else if(c == 'I'){
378 ikptr fixn;
379 fasl_read_buf(p, &fixn, sizeof(ikptr));
380 return fixn;
381 }
382 else if(c == 'F'){
383 return false_object;
384 }
385 else if(c == 'T'){
386 return true_object;
387 }
388 else if(c == 'N'){
389 return null_object;
390 }
391 else if(c == 'c'){
392 /* FIXME: sounds broken */
393 unsigned char x = (unsigned char) fasl_read_byte(p);
394 return int_to_scheme_char(x);
395 }
396 else if(c == 'G'){
397 /* G is for gensym */
398 ikptr pretty = do_read(pcb, p);
399 ikptr unique = do_read(pcb, p);
400 ikptr sym = ikrt_strings_to_gensym(pretty, unique, pcb);
401 if(put_mark_index){
402 p->marks[put_mark_index] = sym;
403 }
404 return sym;
405 }
406 else if(c == 'R'){ /* R is for RTD */
407 ikptr name = do_read(pcb, p);
408 ikptr symb = do_read(pcb, p);
409 long int i, n;
410 fasl_read_buf(p, &n, sizeof(long int));
411 ikptr fields;
412 if(n == 0){
413 fields = null_object;
414 } else {
415 fields = ik_unsafe_alloc(pcb, n * align(pair_size)) + pair_tag;
416 ikptr ptr = fields;
417 for(i=0; i<n; i++){
418 ref(ptr, off_car) = do_read(pcb, p);
419 ref(ptr, off_cdr) = ptr + align(pair_size);
420 ptr += align(pair_size);
421 }
422 ptr -= pair_size;
423 ref(ptr, off_cdr) = null_object;
424 }
425 ikptr gensym_val = ref(symb, off_symbol_record_value);
426 ikptr rtd;
427 if(gensym_val == unbound_object){
428 rtd = ik_unsafe_alloc(pcb, align(rtd_size)) + vector_tag;
429 ikptr base_rtd = pcb->base_rtd;
430 ref(rtd, off_rtd_rtd) = base_rtd;
431 ref(rtd, off_rtd_name) = name;
432 ref(rtd, off_rtd_length) = fix(n);
433 ref(rtd, off_rtd_fields) = fields;
434 ref(rtd, off_rtd_printer) = false_object;
435 ref(rtd, off_rtd_symbol) = symb;
436 ref(symb, off_symbol_record_value) = rtd;
437 ((unsigned int*)(long)pcb->dirty_vector)[page_index(symb+off_symbol_record_value)] = -1;
438 } else {
439 rtd = gensym_val;
440 }
441 if(put_mark_index){
442 p->marks[put_mark_index] = rtd;
443 }
444 return rtd;
445 }
446 else if(c == 'Q'){ /* thunk */
447 ikptr proc = ik_unsafe_alloc(pcb, align(disp_closure_data)) + closure_tag;
448 if(put_mark_index){
449 p->marks[put_mark_index] = proc;
450 }
451 ikptr code = do_read(pcb, p);
452 ref(proc, -closure_tag) = code + off_code_data;
453 return proc;
454 }
455 else if(c == '<'){
456 int idx;
457 fasl_read_buf(p, &idx, sizeof(int));
458 if(idx <= 0){
459 fprintf(stderr, "invalid index for ref %d\n", idx);
460 exit(-1);
461 }
462 if(idx >= p->marks_size){
463 fprintf(stderr, "invalid index for ref %d\n", idx);
464 exit(-1);
465 }
466 ikptr obj = p->marks[idx];
467 if(obj){
468 return obj;
469 } else {
470 fprintf(stderr, "reference to uninitialized mark %d\n", idx);
471 exit(-1);
472 }
473 }
474 else if(c == 'v'){
475 /* bytevector */
476 long int len;
477 fasl_read_buf(p, &len, sizeof(long int));
478 long int size = align(len + disp_bytevector_data + 1);
479 ikptr x = ik_unsafe_alloc(pcb, size) + bytevector_tag;
480 ref(x, off_bytevector_length) = fix(len);
481 fasl_read_buf(p, (void*)(long)(x+off_bytevector_data), len);
482 ((char*)(long)x)[off_bytevector_data+len] = 0;
483 if(put_mark_index){
484 p->marks[put_mark_index] = x;
485 }
486 return x;
487 }
488 else if(c == 'l'){
489 int len = (unsigned char) fasl_read_byte(p);
490 ikptr pair = ik_unsafe_alloc(pcb, pair_size * (len+1)) + pair_tag;
491 if(put_mark_index){
492 p->marks[put_mark_index] = pair;
493 }
494 int i; ikptr pt = pair;
495 for(i=0; i<len; i++){
496 ref(pt, off_car) = do_read(pcb, p);
497 ref(pt, off_cdr) = pt + pair_size;
498 pt += pair_size;
499 }
500 ref(pt, off_car) = do_read(pcb, p);
501 ref(pt, off_cdr) = do_read(pcb, p);
502 return pair;
503 }
504 else if(c == 'L'){
505 long int len;
506 fasl_read_buf(p, &len, sizeof(long int));
507 if(len < 0){
508 fprintf(stderr, "invalid len=%ld\n", len);
509 exit(-1);
510 }
511 ikptr pair = ik_unsafe_alloc(pcb, pair_size * (len+1)) + pair_tag;
512 if(put_mark_index){
513 p->marks[put_mark_index] = pair;
514 }
515 long int i; ikptr pt = pair;
516 for(i=0; i<len; i++){
517 ref(pt, off_car) = do_read(pcb, p);
518 ref(pt, off_cdr) = pt + pair_size;
519 pt += pair_size;
520 }
521 ref(pt, off_car) = do_read(pcb, p);
522 ref(pt, off_cdr) = do_read(pcb, p);
523 return pair;
524 }
525 else if(c == 'f'){
526 ikptr x = ik_unsafe_alloc(pcb, flonum_size) + vector_tag;
527 ref(x, -vector_tag) = flonum_tag;
528 fasl_read_buf(p, (void*)(long)(x+disp_flonum_data-vector_tag), 8);
529 if(put_mark_index){
530 p->marks[put_mark_index] = x;
531 }
532 return x;
533 }
534 else if(c == 'C'){
535 int n;
536 fasl_read_buf(p, &n, sizeof(int));
537 return int_to_scheme_char(n);
538 }
539 else if(c == 'b'){
540 long int len;
541 long int sign = 0;
542 fasl_read_buf(p, &len, sizeof(long int));
543 if(len < 0) {
544 sign = 1;
545 len = -len;
546 }
547 if(len & 3){
548 fprintf(stderr, "Error in fasl-read: invalid bignum length %ld\n", len);
549 exit(-1);
550 }
551 unsigned long int tag = bignum_tag | (sign << bignum_sign_shift) |
552 ((len >> 2) << bignum_length_shift);
553 ikptr x = ik_unsafe_alloc(pcb, align(len + disp_bignum_data)) + vector_tag;
554 ref(x, -vector_tag) = (ikptr) tag;
555 fasl_read_buf(p, (void*)(long)(x+off_bignum_data), len);
556 if(put_mark_index){
557 p->marks[put_mark_index] = x;
558 }
559 return x;
560 }
561 else if(c == 'i'){
562 ikptr real = do_read(pcb, p);
563 ikptr imag = do_read(pcb, p);
564 ikptr x;
565 if ((tagof(real) == vector_tag)
566 && (ref(real, -vector_tag) == flonum_tag)){
567 x = ik_unsafe_alloc(pcb, cflonum_size);
568 ref(x, 0) = cflonum_tag;;
569 ref(x, disp_cflonum_real) = real;
570 ref(x, disp_cflonum_imag) = imag;
571 } else {
572 x = ik_unsafe_alloc(pcb, compnum_size);
573 ref(x, 0) = compnum_tag;
574 ref(x, disp_compnum_real) = real;
575 ref(x, disp_compnum_imag) = imag;
576 }
577 x += vector_tag;
578 if(put_mark_index){
579 p->marks[put_mark_index] = x;
580 }
581 return x;
582 }
583 else {
584 fprintf(stderr, "invalid type '%c' (0x%02x) found in fasl file\n", c, c);
585 exit(-1);
586 }
587 }
588
589
590 static ikptr ik_fasl_read(ikpcb* pcb, fasl_port* p){
591 /* first check the header */
592 char buf[IK_FASL_HEADER_LEN];
593 fasl_read_buf(p, buf, IK_FASL_HEADER_LEN);
594 if(strncmp(buf, IK_FASL_HEADER, IK_FASL_HEADER_LEN) != 0){
595 fprintf(stderr, "invalid fasl header\n");
596 exit(-1);
597 }
598 return do_read(pcb, p);
599 }