| 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 |
#ifndef IKARUS_H |
| 21 |
#define IKARUS_H |
| 22 |
#include "ikarus-getaddrinfo.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <sys/resource.h> |
| 26 |
|
| 27 |
extern int total_allocated_pages; |
| 28 |
extern int total_malloced; |
| 29 |
extern int hash_table_count; |
| 30 |
|
| 31 |
#define cardsize 512 |
| 32 |
#define cards_per_page 8 |
| 33 |
|
| 34 |
#define most_bytes_in_minor 0x10000000 |
| 35 |
|
| 36 |
#define old_gen_mask 0x00000007 |
| 37 |
#define new_gen_mask 0x00000008 |
| 38 |
#define gen_mask 0x0000000F |
| 39 |
#define new_gen_tag 0x00000008 |
| 40 |
#define meta_dirty_mask 0x000000F0 |
| 41 |
#define type_mask 0x00000F00 |
| 42 |
#define scannable_mask 0x0000F000 |
| 43 |
#define dealloc_mask 0x000F0000 |
| 44 |
#define large_object_mask 0x00100000 |
| 45 |
#define meta_dirty_shift 4 |
| 46 |
|
| 47 |
#define hole_type 0x00000000 |
| 48 |
#define mainheap_type 0x00000100 |
| 49 |
#define mainstack_type 0x00000200 |
| 50 |
#define pointers_type 0x00000300 |
| 51 |
#define dat_type 0x00000400 |
| 52 |
#define code_type 0x00000500 |
| 53 |
#define weak_pairs_type 0x00000600 |
| 54 |
#define symbols_type 0x00000700 |
| 55 |
|
| 56 |
#define scannable_tag 0x00001000 |
| 57 |
#define unscannable_tag 0x00000000 |
| 58 |
|
| 59 |
#define dealloc_tag_un 0x00010000 |
| 60 |
#define dealloc_tag_at 0x00020000 |
| 61 |
#define retain_tag 0x00000000 |
| 62 |
|
| 63 |
#define large_object_tag 0x00100000 |
| 64 |
|
| 65 |
#define hole_mt (hole_type | unscannable_tag | retain_tag) |
| 66 |
#define mainheap_mt (mainheap_type | unscannable_tag | retain_tag) |
| 67 |
#define mainstack_mt (mainstack_type | unscannable_tag | retain_tag) |
| 68 |
#define pointers_mt (pointers_type | scannable_tag | dealloc_tag_un) |
| 69 |
#define symbols_mt (symbols_type | scannable_tag | dealloc_tag_un) |
| 70 |
#define data_mt (dat_type | unscannable_tag | dealloc_tag_un) |
| 71 |
#define code_mt (code_type | scannable_tag | dealloc_tag_un) |
| 72 |
#define weak_pairs_mt (weak_pairs_type | scannable_tag | dealloc_tag_un) |
| 73 |
|
| 74 |
|
| 75 |
#define pagesize 4096 |
| 76 |
#define generation_count 5 /* generations 0 (nursery), 1, 2, 3, 4 */ |
| 77 |
|
| 78 |
typedef unsigned long int ikptr; |
| 79 |
|
| 80 |
typedef int ikchar; |
| 81 |
|
| 82 |
void ik_error(ikptr args); |
| 83 |
|
| 84 |
typedef struct ikpage{ |
| 85 |
ikptr base; |
| 86 |
struct ikpage* next; |
| 87 |
} ikpage; |
| 88 |
|
| 89 |
typedef struct ikpages{ |
| 90 |
ikptr base; |
| 91 |
int size; |
| 92 |
struct ikpages* next; |
| 93 |
} ikpages; |
| 94 |
|
| 95 |
typedef struct ikdl{ /* double-link */ |
| 96 |
struct ikdl* prev; |
| 97 |
struct ikdl* next; |
| 98 |
} ikdl; |
| 99 |
|
| 100 |
|
| 101 |
#define ik_ptr_page_size \ |
| 102 |
((pagesize - sizeof(long int) - sizeof(struct ik_ptr_page*))/sizeof(ikptr)) |
| 103 |
|
| 104 |
typedef struct ik_ptr_page{ |
| 105 |
long int count; |
| 106 |
struct ik_ptr_page* next; |
| 107 |
ikptr ptr[ik_ptr_page_size]; |
| 108 |
} ik_ptr_page; |
| 109 |
|
| 110 |
typedef struct callback_locative{ |
| 111 |
ikptr data; |
| 112 |
struct callback_locative* next; |
| 113 |
} callback_locative; |
| 114 |
|
| 115 |
typedef struct ikpcb{ |
| 116 |
/* the first locations may be accessed by some */ |
| 117 |
/* compiled code to perform overflow/underflow ops */ |
| 118 |
ikptr allocation_pointer; /* offset = 0 */ |
| 119 |
ikptr allocation_redline; /* offset = 4 */ |
| 120 |
ikptr frame_pointer; /* offset = 8 */ |
| 121 |
ikptr frame_base; /* offset = 12 */ |
| 122 |
ikptr frame_redline; /* offset = 16 */ |
| 123 |
ikptr next_k; /* offset = 20 */ |
| 124 |
ikptr system_stack; /* offset = 24 */ |
| 125 |
ikptr dirty_vector; /* offset = 28 */ |
| 126 |
ikptr arg_list; /* offset = 32 */ |
| 127 |
ikptr engine_counter; /* offset = 36 */ |
| 128 |
ikptr interrupted; /* offset = 40 */ |
| 129 |
ikptr base_rtd; /* offset = 44 */ |
| 130 |
ikptr collect_key; /* offset = 48 */ |
| 131 |
/* the rest are not used by any scheme code */ |
| 132 |
/* they only support the runtime system (gc, etc.) */ |
| 133 |
callback_locative* callbacks; |
| 134 |
ikptr* root0; |
| 135 |
ikptr* root1; |
| 136 |
unsigned int* segment_vector; |
| 137 |
ikptr weak_pairs_ap; |
| 138 |
ikptr weak_pairs_ep; |
| 139 |
ikptr heap_base; |
| 140 |
unsigned long int heap_size; |
| 141 |
ikpages* heap_pages; |
| 142 |
ikpage* cached_pages; /* pages cached so that we don't map/unmap */ |
| 143 |
ikpage* uncached_pages; /* ikpages cached so that we don't malloc/free */ |
| 144 |
ikptr cached_pages_base; |
| 145 |
int cached_pages_size; |
| 146 |
ikptr stack_base; |
| 147 |
unsigned long int stack_size; |
| 148 |
ikptr symbol_table; |
| 149 |
ikptr gensym_table; |
| 150 |
ik_ptr_page* protected_list[generation_count]; |
| 151 |
unsigned int* dirty_vector_base; |
| 152 |
unsigned int* segment_vector_base; |
| 153 |
ikptr memory_base; |
| 154 |
ikptr memory_end; |
| 155 |
int collection_id; |
| 156 |
int allocation_count_minor; |
| 157 |
int allocation_count_major; |
| 158 |
struct timeval collect_utime; |
| 159 |
struct timeval collect_stime; |
| 160 |
struct timeval collect_rtime; |
| 161 |
int last_errno; |
| 162 |
} ikpcb; |
| 163 |
|
| 164 |
typedef struct { |
| 165 |
ikptr tag; |
| 166 |
ikptr top; |
| 167 |
long int size; |
| 168 |
ikptr next; |
| 169 |
} cont; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
ikpcb* ik_collect(unsigned long int, ikpcb*); |
| 175 |
void ikarus_usage_short(void); |
| 176 |
|
| 177 |
void* ik_malloc(int); |
| 178 |
void ik_free(void*, int); |
| 179 |
|
| 180 |
ikptr ik_mmap(unsigned long int); |
| 181 |
ikptr ik_mmap_typed(unsigned long int size, unsigned int type, ikpcb*); |
| 182 |
ikptr ik_mmap_ptr(unsigned long int size, int gen, ikpcb*); |
| 183 |
ikptr ik_mmap_data(unsigned long int size, int gen, ikpcb*); |
| 184 |
ikptr ik_mmap_code(unsigned long int size, int gen, ikpcb*); |
| 185 |
ikptr ik_mmap_mixed(unsigned long int size, ikpcb*); |
| 186 |
void ik_munmap(ikptr, unsigned long int); |
| 187 |
void ik_munmap_from_segment(ikptr, unsigned long int, ikpcb*); |
| 188 |
ikpcb* ik_make_pcb(); |
| 189 |
void ik_delete_pcb(ikpcb*); |
| 190 |
void ik_free_symbol_table(ikpcb* pcb); |
| 191 |
|
| 192 |
void ik_fasl_load(ikpcb* pcb, char* filename); |
| 193 |
void ik_relocate_code(ikptr); |
| 194 |
|
| 195 |
ikptr ik_exec_code(ikpcb* pcb, ikptr code_ptr, ikptr argcount, ikptr cp); |
| 196 |
void ik_print(ikptr x); |
| 197 |
void ik_fprint(FILE*, ikptr x); |
| 198 |
|
| 199 |
ikptr ikrt_string_to_symbol(ikptr, ikpcb*); |
| 200 |
ikptr ikrt_strings_to_gensym(ikptr, ikptr, ikpcb*); |
| 201 |
|
| 202 |
ikptr ik_cstring_to_symbol(char*, ikpcb*); |
| 203 |
|
| 204 |
ikptr ik_asm_enter(ikpcb*, ikptr code_object, ikptr arg, ikptr cp); |
| 205 |
ikptr ik_asm_reenter(ikpcb*, ikptr code_object, ikptr val); |
| 206 |
ikptr ik_underflow_handler(ikpcb*); |
| 207 |
ikptr ik_unsafe_alloc(ikpcb* pcb, int size); |
| 208 |
ikptr ik_safe_alloc(ikpcb* pcb, int size); |
| 209 |
|
| 210 |
ikptr u_to_number(unsigned long, ikpcb*); |
| 211 |
ikptr ull_to_number(unsigned long long, ikpcb*); |
| 212 |
ikptr normalize_bignum(long int limbs, int sign, ikptr r); |
| 213 |
ikptr s_to_number(signed long x, ikpcb* pcb); |
| 214 |
ikptr d_to_number(double x, ikpcb* pcb); |
| 215 |
ikptr make_pointer(long x, ikpcb* pcb); |
| 216 |
long long extract_num_longlong(ikptr x); |
| 217 |
|
| 218 |
#define IK_HEAP_EXT_SIZE (32 * 4096) |
| 219 |
#define IK_HEAPSIZE (1024 * ((wordsize==4)?1:2) * 4096) /* 4/8 MB */ |
| 220 |
|
| 221 |
#define wordsize ((int)(sizeof(ikptr))) |
| 222 |
#define wordshift ((wordsize == 4)?2:3) |
| 223 |
#define align_shift (wordshift + 1) |
| 224 |
#define align_size (2 * wordsize) |
| 225 |
#define fx_shift wordshift |
| 226 |
#define fx_mask (wordsize - 1) |
| 227 |
|
| 228 |
#define align(n) \ |
| 229 |
((((n) + align_size - 1) >> align_shift) << align_shift) |
| 230 |
|
| 231 |
#define IK_FASL_HEADER \ |
| 232 |
((sizeof(ikptr) == 4) ? "#@IK01" : "#@IK02") |
| 233 |
#define IK_FASL_HEADER_LEN (strlen(IK_FASL_HEADER)) |
| 234 |
|
| 235 |
#define code_pri_tag vector_tag |
| 236 |
#define code_tag ((ikptr)0x2F) |
| 237 |
#define disp_code_code_size (1 * wordsize) |
| 238 |
#define disp_code_reloc_vector (2 * wordsize) |
| 239 |
#define disp_code_freevars (3 * wordsize) |
| 240 |
#define disp_code_annotation (4 * wordsize) |
| 241 |
#define disp_code_unused (5 * wordsize) |
| 242 |
#define disp_code_data (6 * wordsize) |
| 243 |
#define off_code_annotation (disp_code_annotation - code_pri_tag) |
| 244 |
#define off_code_data (disp_code_data - code_pri_tag) |
| 245 |
#define off_code_reloc_vector (disp_code_reloc_vector - code_pri_tag) |
| 246 |
|
| 247 |
#define unfix(x) (((long int)(x)) >> fx_shift) |
| 248 |
#define fix(x) ((ikptr)(((long int)(x)) << fx_shift)) |
| 249 |
#define is_fixnum(x) ((((unsigned long)(x)) & fx_mask) == 0) |
| 250 |
|
| 251 |
#define ref(x,n) \ |
| 252 |
(((ikptr*)(((long int)(x)) + ((long int)(n))))[0]) |
| 253 |
|
| 254 |
#define tagof(x) (((int)(x)) & 7) |
| 255 |
|
| 256 |
#define immediate_tag 7 |
| 257 |
|
| 258 |
#define false_object ((ikptr)0x2F) |
| 259 |
#define true_object ((ikptr)0x3F) |
| 260 |
#define null_object ((ikptr)0x4F) |
| 261 |
#define void_object ((ikptr)0x7F) |
| 262 |
#define bwp_object ((ikptr)0x8F) |
| 263 |
|
| 264 |
#define unbound_object ((ikptr)0x6F) |
| 265 |
#define char_tag 0x0F |
| 266 |
#define char_mask 0xFF |
| 267 |
#define char_shift 8 |
| 268 |
#define int_to_scheme_char(x) \ |
| 269 |
((ikptr)((((unsigned long int)(x)) << char_shift) | char_tag)) |
| 270 |
#define pair_size (2 * wordsize) |
| 271 |
#define pair_mask 7 |
| 272 |
#define pair_tag 1 |
| 273 |
#define disp_car 0 |
| 274 |
#define disp_cdr wordsize |
| 275 |
#define off_car (disp_car - pair_tag) |
| 276 |
#define off_cdr (disp_cdr - pair_tag) |
| 277 |
#define is_char(x) ((char_mask & (int)(x)) == char_tag) |
| 278 |
#define string_tag 6 |
| 279 |
#define disp_string_length 0 |
| 280 |
#define disp_string_data wordsize |
| 281 |
#define off_string_length (disp_string_length - string_tag) |
| 282 |
#define off_string_data (disp_string_data - string_tag) |
| 283 |
|
| 284 |
#define string_set(x,i,c) \ |
| 285 |
(((ikchar*)(((long)(x)) + off_string_data))[i] = ((ikchar)(c))) |
| 286 |
#define integer_to_char(x) \ |
| 287 |
((ikchar)((((int)(x)) << char_shift) + char_tag)) |
| 288 |
#define string_char_size 4 |
| 289 |
|
| 290 |
#define vector_tag 5 |
| 291 |
#define disp_vector_length 0 |
| 292 |
#define disp_vector_data wordsize |
| 293 |
#define off_vector_data (disp_vector_data - vector_tag) |
| 294 |
#define off_vector_length (disp_vector_length - vector_tag) |
| 295 |
|
| 296 |
#define bytevector_tag 2 |
| 297 |
#define disp_bytevector_length 0 |
| 298 |
#define disp_bytevector_data 8 /* not f(wordsize) */ |
| 299 |
#define off_bytevector_length (disp_bytevector_length - bytevector_tag) |
| 300 |
#define off_bytevector_data (disp_bytevector_data - bytevector_tag) |
| 301 |
|
| 302 |
#define symbol_record_tag ((ikptr) 0x5F) |
| 303 |
#define disp_symbol_record_string (1 * wordsize) |
| 304 |
#define disp_symbol_record_ustring (2 * wordsize) |
| 305 |
#define disp_symbol_record_value (3 * wordsize) |
| 306 |
#define disp_symbol_record_proc (4 * wordsize) |
| 307 |
#define disp_symbol_record_plist (5 * wordsize) |
| 308 |
#define symbol_record_size (6 * wordsize) |
| 309 |
#define off_symbol_record_string (disp_symbol_record_string - record_tag) |
| 310 |
#define off_symbol_record_ustring (disp_symbol_record_ustring - record_tag) |
| 311 |
#define off_symbol_record_value (disp_symbol_record_value - record_tag) |
| 312 |
#define off_symbol_record_proc (disp_symbol_record_proc - record_tag) |
| 313 |
#define off_symbol_record_plist (disp_symbol_record_plist - record_tag) |
| 314 |
|
| 315 |
#define closure_tag 3 |
| 316 |
#define closure_mask 7 |
| 317 |
#define disp_closure_code 0 |
| 318 |
#define disp_closure_data wordsize |
| 319 |
#define off_closure_code (disp_closure_code - closure_tag) |
| 320 |
#define off_closure_data (disp_closure_data - closure_tag) |
| 321 |
|
| 322 |
#define is_closure(x) ((((long int)(x)) & closure_mask) == closure_tag) |
| 323 |
#define is_pair(x) ((((long int)(x)) & pair_mask) == pair_tag) |
| 324 |
|
| 325 |
#define record_tag vector_tag |
| 326 |
#define disp_record_rtd 0 |
| 327 |
#define disp_record_data wordsize |
| 328 |
#define off_record_rtd (disp_record_rtd - record_tag) |
| 329 |
#define off_record_data (disp_record_data - record_tag) |
| 330 |
|
| 331 |
#define rtd_tag record_tag |
| 332 |
#define disp_rtd_rtd 0 |
| 333 |
#define disp_rtd_name (1 * wordsize) |
| 334 |
#define disp_rtd_length (2 * wordsize) |
| 335 |
#define disp_rtd_fields (3 * wordsize) |
| 336 |
#define disp_rtd_printer (4 * wordsize) |
| 337 |
#define disp_rtd_symbol (5 * wordsize) |
| 338 |
#define rtd_size (6 * wordsize) |
| 339 |
|
| 340 |
#define off_rtd_rtd (disp_rtd_rtd - rtd_tag) |
| 341 |
#define off_rtd_name (disp_rtd_name - rtd_tag) |
| 342 |
#define off_rtd_length (disp_rtd_length - rtd_tag) |
| 343 |
#define off_rtd_fields (disp_rtd_fields - rtd_tag) |
| 344 |
#define off_rtd_printer (disp_rtd_printer - rtd_tag) |
| 345 |
#define off_rtd_symbol (disp_rtd_symbol - rtd_tag) |
| 346 |
|
| 347 |
#define continuation_tag ((ikptr)0x1F) |
| 348 |
#define disp_continuation_top (1 * wordsize) |
| 349 |
#define disp_continuation_size (2 * wordsize) |
| 350 |
#define disp_continuation_next (3 * wordsize) |
| 351 |
#define continuation_size (4 * wordsize) |
| 352 |
|
| 353 |
#define system_continuation_tag ((ikptr) 0x11F) |
| 354 |
#define disp_system_continuation_top (1 * wordsize) |
| 355 |
#define disp_system_continuation_next (2 * wordsize) |
| 356 |
#define disp_system_continuation_unused (3 * wordsize) |
| 357 |
#define system_continuation_size (4 * wordsize) |
| 358 |
|
| 359 |
#define off_continuation_top (disp_continuation_top - vector_tag) |
| 360 |
#define off_continuation_size (disp_continuation_size - vector_tag) |
| 361 |
#define off_continuation_next (disp_continuation_next - vector_tag) |
| 362 |
|
| 363 |
#define pagesize 4096 |
| 364 |
#define pageshift 12 |
| 365 |
#define align_to_next_page(x) \ |
| 366 |
(((pagesize - 1 + (unsigned long int)(x)) >> pageshift) << pageshift) |
| 367 |
#define align_to_prev_page(x) \ |
| 368 |
((((unsigned long int)(x)) >> pageshift) << pageshift) |
| 369 |
|
| 370 |
#define call_instruction_size \ |
| 371 |
((wordsize == 4) ? 5 : 10) |
| 372 |
#define disp_frame_size (- (call_instruction_size + 3 * wordsize)) |
| 373 |
#define disp_frame_offset (- (call_instruction_size + 2 * wordsize)) |
| 374 |
#define disp_multivale_rp (- (call_instruction_size + 1 * wordsize)) |
| 375 |
|
| 376 |
#define port_tag 0x3F |
| 377 |
#define port_mask 0x3F |
| 378 |
#define port_size (14 * wordsize) |
| 379 |
|
| 380 |
#define disp_tcbucket_tconc (0 * wordsize) |
| 381 |
#define disp_tcbucket_key (1 * wordsize) |
| 382 |
#define disp_tcbucket_val (2 * wordsize) |
| 383 |
#define disp_tcbucket_next (3 * wordsize) |
| 384 |
#define tcbucket_size (4 * wordsize) |
| 385 |
#define off_tcbucket_tconc (disp_tcbucket_tconc - vector_tag) |
| 386 |
#define off_tcbucket_key (disp_tcbucket_key - vector_tag) |
| 387 |
#define off_tcbucket_val (disp_tcbucket_val - vector_tag) |
| 388 |
#define off_tcbucket_next (disp_tcbucket_next - vector_tag) |
| 389 |
|
| 390 |
#define bignum_mask 0x7 |
| 391 |
#define bignum_tag 0x3 |
| 392 |
#define bignum_sign_mask 0x8 |
| 393 |
#define bignum_sign_shift 3 |
| 394 |
#define bignum_length_shift 4 |
| 395 |
#define disp_bignum_data wordsize |
| 396 |
#define off_bignum_data (disp_bignum_data - vector_tag) |
| 397 |
|
| 398 |
#define flonum_tag ((ikptr)0x17) |
| 399 |
#define flonum_size 16 |
| 400 |
#define disp_flonum_data 8 /* not f(wordsize) */ |
| 401 |
#define off_flonum_data (disp_flonum_data - vector_tag) |
| 402 |
#define flonum_data(x) (*((double*)(((char*)(long)(x))+off_flonum_data))) |
| 403 |
|
| 404 |
#define ratnum_tag ((ikptr) 0x27) |
| 405 |
#define disp_ratnum_num (1 * wordsize) |
| 406 |
#define disp_ratnum_den (2 * wordsize) |
| 407 |
#define disp_ratnum_unused (3 * wordsize) |
| 408 |
#define ratnum_size (4 * wordsize) |
| 409 |
|
| 410 |
#define compnum_tag ((ikptr) 0x37) |
| 411 |
#define disp_compnum_real (1 * wordsize) |
| 412 |
#define disp_compnum_imag (2 * wordsize) |
| 413 |
#define disp_compnum_unused (3 * wordsize) |
| 414 |
#define compnum_size (4 * wordsize) |
| 415 |
|
| 416 |
#define cflonum_tag ((ikptr) 0x47) |
| 417 |
#define disp_cflonum_real (1 * wordsize) |
| 418 |
#define disp_cflonum_imag (2 * wordsize) |
| 419 |
#define disp_cflonum_unused (3 * wordsize) |
| 420 |
#define cflonum_size (4 * wordsize) |
| 421 |
|
| 422 |
#define ik_eof_p(x) ((x) == ik_eof_object) |
| 423 |
#define page_index(x) (((unsigned long int)(x)) >> pageshift) |
| 424 |
|
| 425 |
#define pointer_tag ((ikptr) 0x107) |
| 426 |
#define disp_pointer_data (1 * wordsize) |
| 427 |
#define pointer_size (2 * wordsize) |
| 428 |
#define off_pointer_data (disp_pointer_data - vector_tag) |
| 429 |
|
| 430 |
#endif |