File: ikarus-pointers.c

package info (click to toggle)
ikarus 0.0.3+bzr.2010.01.26-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 39,876 kB
  • sloc: lisp: 47,954; ansic: 13,247; sh: 4,595; java: 641; asm: 366; makefile: 266; awk: 186; perl: 66
file content (453 lines) | stat: -rw-r--r-- 11,215 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453

#include "ikarus-data.h"
#include <dlfcn.h>
#include <string.h>
#include <stdlib.h>
#include <gmp.h>

ikptr
ikrt_isapointer(ikptr x, ikpcb* pcb){
  if ((tagof(x) == vector_tag) && (ref(x, -vector_tag) == pointer_tag)) {
    return true_object;
  } else {
    return false_object;
  }
}

ikptr
ikrt_pointer_to_int(ikptr x, ikpcb* pcb) {
  long int p = (long int) ref(x, wordsize-vector_tag);
  ikptr pfx = fix(p);
  if (unfix(pfx) == p) {
    return pfx;
  } else {
    ikptr bn = ik_safe_alloc(pcb, align(wordsize+disp_bignum_data));
    if (p > 0){
      ref(bn, 0) = (ikptr)(bignum_tag | (1 << bignum_length_shift)); 
      ref(bn, disp_bignum_data) = (ikptr)p;
    } else {
      ref(bn, 0) = 
        (ikptr)(bignum_tag | 
              (1 << bignum_length_shift) | 
              (1 << bignum_sign_shift));
      ref(bn, disp_bignum_data) = (ikptr)-p;
    } 
    return bn+vector_tag;
  }
}

ikptr
make_pointer(long x, ikpcb* pcb) {
  ikptr r = ik_safe_alloc(pcb, pointer_size);
  ref(r, 0) = pointer_tag;
  ref(r, wordsize) = (ikptr)x;
  return r+vector_tag;
}

#define bnfst_negative(x) \
  (((unsigned long int)(x)) & bignum_sign_mask)
static long
integer_to_long(ikptr x) {
  if (is_fixnum(x)) {
    return ((long)x) >> fx_shift;
  } else {
    if(bnfst_negative(ref(x, -vector_tag))){
      return -(long)ref(x, wordsize-vector_tag);
    } else {
      return (long)ref(x, wordsize-vector_tag);
    }
  }
}

ikptr
ikrt_fx_to_pointer(ikptr x, ikpcb* pcb) {
  return make_pointer(unfix(x), pcb);
}

ikptr
ikrt_bn_to_pointer(ikptr x, ikpcb* pcb) {
  if(bnfst_negative(ref(x, -vector_tag))){
    return make_pointer(-ref(x, wordsize-vector_tag), pcb);
  } else {
    return make_pointer(ref(x, wordsize-vector_tag), pcb);
  }
}

#if 0
ikptr
ikrt_pointer_null(ikptr x /*, ikpcb* pcb*/) {
  return ref(x, off_pointer_data) ? true_object : false_object;
}
#endif

ikptr 
ikrt_dlerror(ikpcb* pcb) {
  char* str = dlerror();
  if (str == NULL) {
    return false_object;
  }
  int len = strlen(str);
  ikptr bv = ik_safe_alloc(pcb, align(disp_bytevector_data + len + 1));
  ref(bv, 0) = fix(len);
  memcpy((void*)(bv+disp_bytevector_data), str, len+1);
  return bv+bytevector_tag;
}

#ifndef RTLD_LOCAL
#define RTLD_LOCAL 0 /* for cygwin, possibly incorrect */
#endif

ikptr
ikrt_dlopen(ikptr x, ikptr load_lazy, ikptr load_global, ikpcb* pcb) {
  int flags = 
    ((load_lazy == false_object) ? RTLD_NOW : RTLD_LAZY) |
    ((load_global == false_object) ? RTLD_LOCAL : RTLD_GLOBAL);
  char* name = 
    (x == false_object) 
      ? NULL 
      : (char*)(x + off_bytevector_data);
  void* p = dlopen(name, flags);
  if (p == NULL) {
    return false_object;
  } else {
    return make_pointer((long int) p, pcb);
  }
}

ikptr
ikrt_dlclose(ikptr x /*, ikpcb* pcb*/) {
  int r = dlclose((void*) ref(x, off_pointer_data));
  return (r == 0) ? true_object : false_object;
}


ikptr
ikrt_dlsym(ikptr handle, ikptr sym, ikpcb* pcb) {
  void* p = dlsym((void*)ref(handle, off_pointer_data), 
                  ((char*)sym) + off_bytevector_data);
  if (p == NULL) {
    return false_object;
  } else {
    return make_pointer((long int) p, pcb);
  }
}


ikptr
ikrt_malloc(ikptr len, ikpcb* pcb) {
  void* p = malloc(unfix(len));
  if (p == NULL) {
    return false_object;
  } else {
    return make_pointer((long int) p, pcb);
  }
}

ikptr
ikrt_free(ikptr x) {
  free((void*) ref(x, off_pointer_data));
  return void_object;
}

ikptr
ikrt_memcpy_to_bv(ikptr dst, ikptr dst_off, ikptr src, ikptr count
    /*, ikpcb* pcb */) {
  void *src_ptr, *dst_ptr;
  
  src_ptr = (void *)ref(src, off_pointer_data);
  dst_ptr = (void *)(dst + off_bytevector_data + unfix(dst_off));
  memcpy(dst_ptr, src_ptr, unfix(count));
  return void_object;
}

ikptr
ikrt_memcpy_from_bv(ikptr dst, ikptr src, ikptr src_off, ikptr count
    /*, ikpcb* pcb */) {
  void *src_ptr, *dst_ptr;
  
  src_ptr = (void *)(src + off_bytevector_data + unfix(src_off));
  dst_ptr = (void *)ref(dst, off_pointer_data);
  memcpy(dst_ptr, src_ptr, unfix(count));
  return void_object;
}

ikptr
ikrt_ref_char(ikptr p, ikptr off /*, ikpcb* pcb*/) {
  return fix(*((signed char*)(((long)ref(p, off_pointer_data)) + unfix(off))));
}

ikptr
ikrt_ref_uchar(ikptr p, ikptr off /*, ikpcb* pcb*/) {
  return fix(*((unsigned char*)(((long)ref(p, off_pointer_data)) + unfix(off))));
}

ikptr
ikrt_ref_short(ikptr p, ikptr off /*, ikpcb* pcb*/) {
  return fix(*((signed short*)(((long)ref(p, off_pointer_data)) + unfix(off))));
}

ikptr
ikrt_ref_ushort(ikptr p, ikptr off /*, ikpcb* pcb*/) {
  return fix(*((unsigned short*)(((long)ref(p, off_pointer_data)) + unfix(off))));
}

ikptr
ikrt_ref_pointer(ikptr p, ikptr off, ikpcb* pcb) {
  long idx = integer_to_long(off);
  void* ptr = (void*)ref(p, off_pointer_data);
  return make_pointer(ref(ptr, idx), pcb);
}

ikptr
ikrt_set_pointer(ikptr p, ikptr off, ikptr v /*, ikpcb* pcb*/) {
  long idx = integer_to_long(off);
  void* ptr = (void*)ref(p, off_pointer_data);
  ref(ptr, idx) = ref(v, off_pointer_data);
  return void_object;
}

static ikptr
double_to_flonum(double x, ikpcb* pcb){
  ikptr r = ik_safe_alloc(pcb, flonum_size) + vector_tag;
  ref(r, -vector_tag) = flonum_tag;
  flonum_data(r) = x;
  return r;
}

ikptr
ikrt_set_float(ikptr p, ikptr off, ikptr v /*, ikpcb* pcb*/) {
  long idx = integer_to_long(off);
  ikptr ptr = ref(p, off_pointer_data);
  *((float*)(ptr+idx)) = flonum_data(v);
  return void_object;
}

ikptr
ikrt_ref_float(ikptr p, ikptr off, ikpcb* pcb) {
  long idx = integer_to_long(off);
  ikptr ptr = ref(p, off_pointer_data);
  double v = *((float*)(ptr+idx));
  return double_to_flonum(v, pcb);
}


ikptr
ikrt_set_double(ikptr p, ikptr off, ikptr v /*, ikpcb* pcb*/) {
  long idx = integer_to_long(off);
  ikptr ptr = ref(p, off_pointer_data);
  *((double*)(ptr+idx)) = flonum_data(v);
  return void_object;
}

ikptr
ikrt_ref_double(ikptr p, ikptr off, ikpcb* pcb) {
  long idx = integer_to_long(off);
  ikptr ptr = ref(p, off_pointer_data);
  double v = *((double*)(ptr+idx));
  return double_to_flonum(v, pcb);
}






ikptr
s_to_number(signed long n, ikpcb* pcb) {
  ikptr fx = fix(n);
  if (unfix(fx) == n) {
    return fx;
  }
  ikptr bn = ik_safe_alloc(pcb, align(wordsize+disp_bignum_data));
  if (n > 0){
    ref(bn, 0) = (ikptr)(bignum_tag | (1 << bignum_length_shift)); 
    ref(bn, disp_bignum_data) = (ikptr)n;
  } else {
    ref(bn, 0) = 
      (ikptr)(bignum_tag | 
            (1 << bignum_length_shift) | 
            (1 << bignum_sign_shift));
    ref(bn, disp_bignum_data) = (ikptr)-n;
  }
  return bn+vector_tag;
}
  
ikptr
sll_to_number(signed long long n, ikpcb* pcb) {
  if (((signed long long)(signed long) n) == n) {
    return s_to_number(n, pcb);
  }
  int len = sizeof(long long) / sizeof(mp_limb_t);
  ikptr bn = ik_safe_alloc(pcb, align(sizeof(long long)+disp_bignum_data));
  if (n > 0){
    ref(bn, 0) = (ikptr)(bignum_tag | (len << bignum_length_shift)); 
    *((long long*)(bn+disp_bignum_data)) = n;
  } else {
    ref(bn, 0) = 
      (ikptr)(bignum_tag | 
            (len << bignum_length_shift) | 
            (1 << bignum_sign_shift));
    *((long long*)(bn+disp_bignum_data)) = -n;
  }
  return bn+vector_tag;
}


ikptr
u_to_number(unsigned long n, ikpcb* pcb) {
  unsigned long mxn = ((unsigned long)-1)>>(fx_shift+1);
  if (n <= mxn) {
    return fix(n);
  }
  ikptr bn = ik_safe_alloc(pcb, align(wordsize+disp_bignum_data));
  ref(bn, 0) = (ikptr)(bignum_tag | (1 << bignum_length_shift)); 
  ref(bn, disp_bignum_data) = (ikptr)n;
  return bn+vector_tag;
}


ikptr
ull_to_number(unsigned long long n, ikpcb* pcb) {
  if (((unsigned long long)(unsigned long) n) == n) {
    return u_to_number(n, pcb);
  }
  ikptr bn = ik_safe_alloc(pcb, align(disp_bignum_data+sizeof(long long)));
  bcopy((char*)(&n), (char*)(bn+disp_bignum_data), sizeof(long long));
  return normalize_bignum(sizeof(long long)/sizeof(mp_limb_t), 0, bn);
}

ikptr
d_to_number(double n, ikpcb* pcb) {
  ikptr x = ik_safe_alloc(pcb, flonum_size) + vector_tag;
  ref(x, -vector_tag) = flonum_tag;
  flonum_data(x) = n;
  return x;
}

ikptr
ikrt_ref_int(ikptr p, ikptr off , ikpcb* pcb) {
  signed int r = 
    *((signed int*)(((long)ref(p, off_pointer_data)) + unfix(off)));
  if (wordsize == 8) {
    return fix(r);
  } else {
    return s_to_number(r, pcb);
  }
}

ikptr
ikrt_ref_uint(ikptr p, ikptr off , ikpcb* pcb) {
  unsigned int r = 
    *((unsigned int*)(((long)ref(p, off_pointer_data)) + unfix(off)));
  if (wordsize == 8) {
    return fix(r);
  } else {
    return u_to_number(r, pcb);
  }
}

ikptr
ikrt_ref_long(ikptr p, ikptr off , ikpcb* pcb) {
  signed long r = 
    *((signed long*)(((long)ref(p, off_pointer_data)) + unfix(off)));
  return s_to_number(r, pcb);
}

ikptr
ikrt_ref_ulong(ikptr p, ikptr off , ikpcb* pcb) {
  unsigned long r = 
    *((unsigned long*)(((long)ref(p, off_pointer_data)) + unfix(off)));
  return u_to_number(r, pcb);
}

ikptr
ikrt_ref_longlong(ikptr p, ikptr off , ikpcb* pcb) {
  signed long long r = 
    *((signed long long*)(((long)ref(p, off_pointer_data)) + unfix(off)));
  return sll_to_number(r, pcb);
}

ikptr
ikrt_ref_ulonglong(ikptr p, ikptr off , ikpcb* pcb) {
  unsigned long long r = 
    *((unsigned long long*)(((long)ref(p, off_pointer_data)) + unfix(off)));
  return ull_to_number(r, pcb);
}

long
extract_num(ikptr x) {
  if (is_fixnum(x)) {
    return unfix(x);
  } else if (x == void_object) {
    return 0;
  } else {
    if (bnfst_negative(ref(x, -vector_tag))){
      return (long)(-ref(x, wordsize-vector_tag));
    } else {
      return (long)(ref(x, wordsize-vector_tag));
    }
  }  
}

long long
extract_num_longlong(ikptr x) {
  if (is_fixnum(x)) {
    return unfix(x);
  } else if (x == void_object) {
    return 0;
  } else {
    ikptr fst = ref(x, -vector_tag);
    ikptr pos_one_limb_tag = 
        (ikptr)(bignum_tag | (1 << bignum_length_shift));
    ikptr neg_one_limb_tag = 
        (ikptr)(pos_one_limb_tag | (1 << bignum_sign_shift));
    if (fst == pos_one_limb_tag) {
      return (unsigned long)ref(x, wordsize-vector_tag);
    } else if (fst == neg_one_limb_tag) {
      return -(signed long)ref(x, wordsize-vector_tag);
    } else if (bnfst_negative(fst)) {
      return -(*((long long*)(x+wordsize-vector_tag)));
    } else {
      return *((long long*)(x+wordsize-vector_tag));
    }
  }  
}



ikptr
ikrt_set_char(ikptr p, ikptr off, ikptr v/*, ikpcb* pcb*/) {
  *((char*)(((long)ref(p, off_pointer_data)) + unfix(off))) = 
    extract_num(v);
  return void_object;
}

ikptr
ikrt_set_short(ikptr p, ikptr off, ikptr v/*, ikpcb* pcb*/) {
  *((short*)(((long)ref(p, off_pointer_data)) + unfix(off))) = 
    extract_num(v);
  return void_object;
}

ikptr
ikrt_set_int(ikptr p, ikptr off, ikptr v/*, ikpcb* pcb*/) {
  *((int*)(((long)ref(p, off_pointer_data)) + unfix(off))) = 
    extract_num(v);
  return void_object;
}

ikptr
ikrt_set_long(ikptr p, ikptr off, ikptr v/*, ikpcb* pcb*/) {
  *((long*)(((long)ref(p, off_pointer_data)) + unfix(off))) = 
    extract_num(v);
  return void_object;
}

ikptr
ikrt_set_longlong(ikptr p, ikptr off, ikptr v/*, ikpcb* pcb*/) {
  *((long long*)(((long)ref(p, off_pointer_data)) + unfix(off))) = 
    extract_num_longlong(v);
  return void_object;
}