copy_string_to_graph.c 18.2 KB
Newer Older
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
1 2 3

#include <stdlib.h>

John van Groningen's avatar
John van Groningen committed
4 5 6
#ifdef _WIN64
# define Int __int64
# define INT_descriptor dINT
7
# define ARCH_64 1
John van Groningen's avatar
John van Groningen committed
8
#else
John van Groningen's avatar
John van Groningen committed
9
# if defined (MACH_O64) || defined (LINUX64)
John van Groningen's avatar
John van Groningen committed
10 11
#  define Int long long
#  define INT_descriptor dINT
12
#  define ARCH_64 1
John van Groningen's avatar
John van Groningen committed
13 14 15
# else
#  define Int int
#  define INT_descriptor INT
16
#  define ARCH_64 0
John van Groningen's avatar
John van Groningen committed
17
# endif
John van Groningen's avatar
John van Groningen committed
18
# if !(defined (LINUX32) || defined (LINUX64))
John van Groningen's avatar
John van Groningen committed
19 20 21
#  define __STRING__ _STRING__
#  define __ARRAY__ _ARRAY__
# endif
John van Groningen's avatar
John van Groningen committed
22 23 24 25
#endif

extern void *INT_descriptor,*CHAR,*BOOL,*REAL,*__STRING__,*__ARRAY__;
extern Int small_integers[],static_characters[];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
26

John van Groningen's avatar
John van Groningen committed
27 28
/*inline*/
static void copy (Int *dest_p,Int *source_p,Int n_words)
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
29
{
John van Groningen's avatar
John van Groningen committed
30
	Int i;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
31 32 33 34 35

	for (i=0; i<n_words; ++i)
		dest_p[i]=source_p[i];
}

John van Groningen's avatar
John van Groningen committed
36
Int *copy_string_to_graph (Int *string_p,void *begin_free_heap,void *end_free_heap,Int **last_heap_pa)
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
37
{
John van Groningen's avatar
John van Groningen committed
38
	Int ***stack_p,***stack_begin,***stack_end,*heap_p,**arg_a,*root_node_p,n_free_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
39 40 41 42 43 44 45 46 47

	string_p+=2;

	stack_end=end_free_heap;
	stack_begin=end_free_heap;
	stack_p=end_free_heap;
		
	heap_p=begin_free_heap;
	
John van Groningen's avatar
John van Groningen committed
48
	n_free_words=(Int*)end_free_heap-(Int*)begin_free_heap;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
49 50 51 52
	arg_a=&root_node_p;

	for (;;){
		for (;;){
John van Groningen's avatar
John van Groningen committed
53
			Int desc;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
54 55 56 57 58
			
			desc=*string_p;
			
			if (--n_free_words<0){
				*last_heap_pa=heap_p+1+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
59
				return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
60 61 62
			}

			if (!(desc & 1)){
John van Groningen's avatar
John van Groningen committed
63
				*string_p=(Int)heap_p;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
64
				*arg_a=heap_p;
65 66 67
#ifdef MACH_O64
				desc+=(Int)&__ARRAY__;
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
68 69
				*heap_p=desc;
				if (desc & 2){
John van Groningen's avatar
John van Groningen committed
70
					unsigned Int arity;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
71 72 73
					
					arity=((unsigned short *)desc)[-1];
					if (arity==0){
John van Groningen's avatar
John van Groningen committed
74 75
						if (desc==(Int)&INT_descriptor+2){
							Int i;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
76 77
							
							i=string_p[1];
John van Groningen's avatar
John van Groningen committed
78 79
							if ((unsigned Int)i<=(unsigned Int)32){
								Int *a;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
80 81 82 83
								
								a=&small_integers[i<<1];
								++n_free_words;
								*arg_a=a;
John van Groningen's avatar
John van Groningen committed
84
								*string_p=(Int)a;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
85 86 87
							} else {
								if (--n_free_words<0){
									*last_heap_pa=heap_p+2+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
88
									return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
89 90 91 92 93 94 95
								}
								
								heap_p[1]=i;
								heap_p+=2;
							}
							string_p+=2;
							break;
John van Groningen's avatar
John van Groningen committed
96
						} else if (desc==(Int)&CHAR+2){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
97
							unsigned char c;
John van Groningen's avatar
John van Groningen committed
98
							Int *a;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
99 100
							
							c=(unsigned char)(string_p[1]);
John van Groningen's avatar
John van Groningen committed
101
							a=&static_characters[(Int)c<<1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
102 103
							++n_free_words;
							*arg_a=a;
John van Groningen's avatar
John van Groningen committed
104
							*string_p=(Int)a;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
105 106
							string_p+=2;
							break;
John van Groningen's avatar
John van Groningen committed
107
						} else if (desc==(Int)&BOOL+2
108
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
109 110 111
							|| desc==(Int)&REAL+2
#endif
						){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
112 113
							if (--n_free_words<0){
								*last_heap_pa=heap_p+2+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
114
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
115 116 117 118 119 120
							}
							
							heap_p[1]=string_p[1];
							string_p+=2;
							heap_p+=2;
							break;
121
#if ! ARCH_64
John van Groningen's avatar
John van Groningen committed
122
						} else if (desc==(Int)&REAL+2){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
123 124 125
							n_free_words-=2;
							if (n_free_words<0){
								*last_heap_pa=heap_p+3+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
126
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
127 128 129 130 131 132 133
							}
							
							heap_p[1]=string_p[1];
							heap_p[2]=string_p[2];
							string_p+=3;
							heap_p+=3;
							break;
John van Groningen's avatar
John van Groningen committed
134 135 136
#endif
						} else if (desc==(Int)&__STRING__+2){
							unsigned Int length,n_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
137 138 139
							
							length=string_p[1];
							string_p+=2;
140
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
141 142
							n_words=(length+7)>>3;
#else
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
143
							n_words=(length+3)>>2;
John van Groningen's avatar
John van Groningen committed
144
#endif							
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
145 146 147
							n_free_words-=n_words+1;
							if (n_free_words<0){
								*last_heap_pa=heap_p+2+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
148
								return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
149 150 151 152 153 154 155 156 157
							}
													
							heap_p[1]=length;
							heap_p+=2;

							copy (heap_p,string_p,n_words);
							string_p+=n_words;
							heap_p+=n_words;
							break;
John van Groningen's avatar
John van Groningen committed
158 159
						} else if (desc==(Int)&__ARRAY__+2){
							Int array_size,elem_desc;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
160 161 162 163

							n_free_words-=2;
							if (n_free_words<0){
								*last_heap_pa=heap_p+3+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
164
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
165 166 167 168 169 170 171 172 173 174 175
							}
							
							array_size=string_p[1];
							elem_desc=string_p[2];
							string_p+=3;
		
							heap_p[1]=array_size;
							heap_p[2]=elem_desc;
							heap_p+=3;
													
							if (elem_desc==0){
John van Groningen's avatar
John van Groningen committed
176
								Int i;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
177 178 179
								
								stack_p-=array_size;
								if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
180
									Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
181 182 183 184 185

									extra_words=stack_begin-stack_p;
									n_free_words-=extra_words;
									if (n_free_words<0){
										*last_heap_pa=heap_p+array_size+(stack_end-stack_p);
John van Groningen's avatar
John van Groningen committed
186
										return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
187 188 189 190 191 192
									}
									stack_begin=stack_p;
								}
								
								i=array_size;
								while (--i>=0)
John van Groningen's avatar
John van Groningen committed
193
									stack_p[i]=(Int**)&heap_p[i];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
194 195 196

								heap_p+=array_size;
								break;
John van Groningen's avatar
John van Groningen committed
197
							} else if (elem_desc==(Int)&INT_descriptor+2
198
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
199 200 201
								|| elem_desc==(Int)&REAL+2
#endif
							){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
202 203 204
								n_free_words-=array_size;
								if (n_free_words<0){
									*last_heap_pa=heap_p+array_size+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
205
									return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
206 207 208 209 210 211
								}

								copy (heap_p,string_p,array_size);
								string_p+=array_size;
								heap_p+=array_size;
								break;
212
#if ! ARCH_64
John van Groningen's avatar
John van Groningen committed
213
							} else if (elem_desc==(Int)&REAL+2){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
214 215 216 217 218
								array_size<<=1;
							
								n_free_words-=array_size;
								if (n_free_words<0){
									*last_heap_pa=heap_p+array_size+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
219
									return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
220 221 222 223 224 225
								}
								
								copy (heap_p,string_p,array_size);
								string_p+=array_size;
								heap_p+=array_size;
								break;
John van Groningen's avatar
John van Groningen committed
226 227
#endif
							} else if (elem_desc==(Int)&BOOL+2){
228
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
229 230
								array_size=(array_size+7)>>3;
#else
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
231
								array_size=(array_size+3)>>2;
John van Groningen's avatar
John van Groningen committed
232
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
233 234 235
								n_free_words-=array_size;
								if (n_free_words<0){
									*last_heap_pa=heap_p+array_size+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
236
									return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
237 238 239 240 241 242 243
								}
								
								copy (heap_p,string_p,array_size);
								string_p+=array_size;
								heap_p+=array_size;
								break;
							} else {
John van Groningen's avatar
John van Groningen committed
244
								Int n_field_pointers,field_size;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
245 246

								n_field_pointers=*(unsigned short *)elem_desc;
John van Groningen's avatar
John van Groningen committed
247
								field_size=((unsigned short *)elem_desc)[-1]-(Int)256;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
248 249 250 251 252 253 254

								if (n_field_pointers==0){
									array_size*=field_size;
									
									n_free_words-=array_size;
									if (n_free_words<0){
										*last_heap_pa=heap_p+array_size+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
255
										return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
256 257 258 259 260 261 262
									}
																		
									copy (heap_p,string_p,array_size);
									string_p+=array_size;
									heap_p+=array_size;
									break;
								} else if (n_field_pointers==field_size){
John van Groningen's avatar
John van Groningen committed
263
									Int i;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
264 265 266 267 268
									
									array_size*=field_size;

									stack_p-=array_size;
									if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
269
										Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
270 271 272 273 274

										extra_words=stack_begin-stack_p;
										n_free_words-=extra_words;
										if (n_free_words<0){
											*last_heap_pa=heap_p+array_size+(stack_end-stack_p);
John van Groningen's avatar
John van Groningen committed
275
											return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
276 277 278 279 280 281
										}
										stack_begin=stack_p;
									}
									
									i=array_size;
									while (--i>=0)
John van Groningen's avatar
John van Groningen committed
282
										stack_p[i]=(Int**)&heap_p[i];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
283 284 285 286
									
									heap_p+=array_size;
									break;
								} else {
John van Groningen's avatar
John van Groningen committed
287
									Int n_non_field_pointers,i,***pointer_p;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
288 289 290 291 292 293
									
									n_non_field_pointers=field_size-n_field_pointers;
									
									n_free_words-=array_size*field_size;
									if (n_free_words<0){
										*last_heap_pa=heap_p+array_size*field_size+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
294
										return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
295 296 297 298
									}
									
									stack_p-=array_size*n_field_pointers;
									if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
299
										Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
300 301 302 303 304

										extra_words=stack_begin-stack_p;
										n_free_words-=extra_words;
										if (n_free_words<0){
											*last_heap_pa=heap_p+(stack_end-stack_p);
John van Groningen's avatar
John van Groningen committed
305
											return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
306 307 308 309 310 311 312
										}
										stack_begin=stack_p;
									}
									
									pointer_p=stack_p;
									
									for (i=0; i<array_size; ++i){
John van Groningen's avatar
John van Groningen committed
313
										Int n;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
314 315 316
										
										n=n_field_pointers;
										while (--n>=0)
John van Groningen's avatar
John van Groningen committed
317
											pointer_p[n]=(Int**)&heap_p[n];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331
										pointer_p+=n_field_pointers;
										heap_p+=n_field_pointers;
										
										copy (heap_p,string_p,n_non_field_pointers);
										heap_p+=n_non_field_pointers;
										string_p+=n_non_field_pointers;						
									}
									break;
								}
							}
						} else {
#ifdef OLD_DESCRIPTORS
							desc-=10;
#else
332
# if ARCH_64
John van Groningen's avatar
John van Groningen committed
333 334 335 336
							desc-=10;
# else
							desc-=6;
# endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
337 338
#endif
							++n_free_words;
John van Groningen's avatar
John van Groningen committed
339
							*arg_a=(Int*)desc;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
340 341 342 343 344 345 346
							*string_p=desc;
							++string_p;
							break;
						}
					} else if (arity==1){
						if (--n_free_words<0){
							*last_heap_pa=heap_p+2+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
347
							return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
348
						}
John van Groningen's avatar
John van Groningen committed
349
						arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
350 351 352 353 354 355 356
						++string_p;
						heap_p+=2;
						continue;
					} else if (arity==2){
						n_free_words-=2;
						if (n_free_words<0){
							*last_heap_pa=heap_p+3+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
357
							return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
358 359 360 361 362
						}
						
						if (stack_p<=stack_begin){
							if (--n_free_words<0){
								*last_heap_pa=heap_p+3+(stack_end-1-stack_begin);
John van Groningen's avatar
John van Groningen committed
363
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
364 365 366 367
							}
							--stack_begin;
						}
						
John van Groningen's avatar
John van Groningen committed
368 369
						*--stack_p=(Int**)&heap_p[2];
						arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
370 371 372 373
						++string_p;
						heap_p+=3;
						continue;
					} else if (arity<256){
John van Groningen's avatar
John van Groningen committed
374
						Int n_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
375 376 377 378

						n_free_words-=arity+1;
						if (n_free_words<0){
							*last_heap_pa=heap_p+arity+2+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
379
							return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
380 381
						}

John van Groningen's avatar
John van Groningen committed
382 383
						arg_a=(Int**)&heap_p[1];
						heap_p[2]=(Int)&heap_p[3];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
384 385 386 387 388 389
						heap_p+=3;

						n_words=arity-1;

						stack_p-=n_words;
						if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
390
							Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
391 392 393 394 395

							extra_words=stack_begin-stack_p;
							n_free_words-=extra_words;
							if (n_free_words<0){
								*last_heap_pa=heap_p+arity-1+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
396
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
397 398 399 400 401
							}
							stack_begin=stack_p;
						}

						--n_words;
John van Groningen's avatar
John van Groningen committed
402
						stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
403
						--n_words;
John van Groningen's avatar
John van Groningen committed
404
						stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
405
						while (--n_words>=0)
John van Groningen's avatar
John van Groningen committed
406
							stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
407 408 409 410 411

						heap_p+=arity-1;
						++string_p;
						continue;
					} else {
John van Groningen's avatar
John van Groningen committed
412
						Int n_pointers;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
413 414 415 416 417 418 419
						
						n_pointers=*(unsigned short*)desc;
						arity-=256;
						
						if (arity==1){
							if (--n_free_words<0){
								*last_heap_pa=heap_p+2+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
420
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
421 422 423 424 425 426 427 428
							}
							
							if (n_pointers==0){						
								heap_p[1]=string_p[1];
								string_p+=2;
								heap_p+=2;
								break;
							} else {
John van Groningen's avatar
John van Groningen committed
429
								arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
430 431 432 433 434 435 436 437
								++string_p;
								heap_p+=2;
								continue;
							}
						} else if (arity==2){
							n_free_words-=2;
							if (n_free_words<0){
								*last_heap_pa=heap_p+3+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
438
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
							}

							if (n_pointers==0){
								heap_p[1]=string_p[1];
								heap_p[2]=string_p[2];
								string_p+=3;
								heap_p+=3;
								break;
							} else {
								if (n_pointers==1){
									heap_p[2]=string_p[1];
									string_p+=2;
								} else {
									if (stack_p<=stack_begin){
										if (--n_free_words<0){
											*last_heap_pa=heap_p+3+1+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
455
											return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
456 457 458 459
										}
										--stack_begin;
									}
									++string_p;
John van Groningen's avatar
John van Groningen committed
460
									*--stack_p=(Int**)&heap_p[2];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
461
								}
John van Groningen's avatar
John van Groningen committed
462
								arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
463 464 465 466 467 468 469
								heap_p+=3;
								continue;							
							}
						} else {
							n_free_words-=arity+1;
							if (n_free_words<0){
								*last_heap_pa=heap_p+arity+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
470
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
471 472
							}
							
John van Groningen's avatar
John van Groningen committed
473
							heap_p[2]=(Int)&heap_p[3];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
474 475 476 477 478 479 480 481 482 483 484
							
							if (n_pointers==0){
								heap_p[1]=string_p[1];
								heap_p+=3;
								string_p+=2;
								--arity;
								copy (heap_p,string_p,arity);
								string_p+=arity;
								heap_p+=arity;
								break;
							} else {
John van Groningen's avatar
John van Groningen committed
485
								Int n_non_pointers;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
486

John van Groningen's avatar
John van Groningen committed
487
								arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
488 489 490 491 492 493
								heap_p+=3;

								n_non_pointers=arity-n_pointers;
								++string_p;

								if (n_non_pointers>0){
John van Groningen's avatar
John van Groningen committed
494
									Int *non_pointers_p;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
495 496 497 498 499 500 501 502 503

									non_pointers_p=&heap_p[n_pointers-1];

									copy (non_pointers_p,string_p,n_non_pointers);
									string_p+=n_non_pointers;
								}
								
								--n_pointers;
								if (n_pointers>0){
John van Groningen's avatar
John van Groningen committed
504
									Int i;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
505 506 507
									
									stack_p-=n_pointers;
									if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
508
										Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
509 510 511 512 513

										extra_words=stack_begin-stack_p;
										n_free_words-=extra_words;
										if (n_free_words<0){
											*last_heap_pa=heap_p+n_pointers+n_non_pointers+(stack_end-stack_p);
John van Groningen's avatar
John van Groningen committed
514
											return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
515 516 517 518 519 520
										}
										stack_begin=stack_p;
									}
									
									i=n_pointers;
									while (--i>=0)
John van Groningen's avatar
John van Groningen committed
521
										stack_p[i]=(Int**)&heap_p[i];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
522 523 524 525 526 527 528
								}
								heap_p+=n_pointers+n_non_pointers;
								continue;
							}
						}
					}
				} else {
John van Groningen's avatar
John van Groningen committed
529
					Int arity;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
530 531 532 533

					arity=((int*)desc)[-1];
					if (arity>1){
						if (arity<256){
John van Groningen's avatar
John van Groningen committed
534
							Int n_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
535 536 537 538
							
							n_free_words-=arity;
							if (n_free_words<0){
								*last_heap_pa=heap_p+arity+1+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
539
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
540 541 542 543 544 545
							}

							n_words=arity-1;

							stack_p-=n_words;
							if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
546
								Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
547 548 549 550 551

								extra_words=stack_begin-stack_p;
								n_free_words-=extra_words;
								if (n_free_words<0){
									*last_heap_pa=heap_p+arity+1+(stack_end-stack_p);
John van Groningen's avatar
John van Groningen committed
552
									return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
553 554 555 556
								}
								stack_begin=stack_p;
							}

John van Groningen's avatar
John van Groningen committed
557
							arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
558 559 560
							heap_p+=2;
							
							--n_words;
John van Groningen's avatar
John van Groningen committed
561
							stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
562
							while (--n_words>=0)
John van Groningen's avatar
John van Groningen committed
563
								stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
564 565 566

							++string_p;
							heap_p+=arity-1;
John van Groningen's avatar
John van Groningen committed
567
							continue;
568
						} else if (arity!=257){
John van Groningen's avatar
John van Groningen committed
569
							Int n_pointers,n_non_pointers,*non_pointers_p;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
570 571 572 573 574 575 576 577
							
							n_non_pointers=arity>>8;
							arity=arity & 255;
							n_pointers=arity - n_non_pointers;

							n_free_words-=arity;
							if (n_free_words<0){
								*last_heap_pa=heap_p+arity+1+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
578
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
579 580 581 582 583 584 585 586 587 588 589 590 591
							}

							++string_p;
							++heap_p;
							non_pointers_p=&heap_p[n_pointers];

							copy (non_pointers_p,string_p,n_non_pointers);
							string_p+=n_non_pointers;
							
							if (n_pointers==0){
								heap_p+=arity;
								break;
							} else {
John van Groningen's avatar
John van Groningen committed
592
								arg_a=(Int**)&heap_p[0];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
593 594 595 596 597 598
								++heap_p;
								if (n_pointers>1){
									--n_pointers;
									
									stack_p-=n_pointers;
									if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
599
										Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
600 601 602 603 604

										extra_words=stack_begin-stack_p;
										n_free_words-=extra_words;
										if (n_free_words<0){
											*last_heap_pa=heap_p+arity+(stack_end-stack_p);
John van Groningen's avatar
John van Groningen committed
605
											return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
606 607 608 609 610
										}
										stack_begin=stack_p;
									}
									
									--n_pointers;
John van Groningen's avatar
John van Groningen committed
611
									stack_p[n_pointers]=(Int**)&heap_p[n_pointers];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
612
									while (--n_pointers>=0)
John van Groningen's avatar
John van Groningen committed
613
										stack_p[n_pointers]=(Int**)&heap_p[n_pointers];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
614 615 616 617
								}
								heap_p+=arity-1;
								continue;
							}
618 619 620 621
						} else {
							n_free_words-=2;
							if (n_free_words<0){
								*last_heap_pa=heap_p+3+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
622
								return (Int*)((Int)&string_p[1]+1);
623 624 625 626 627 628
							}

							heap_p[1]=string_p[1];
							string_p+=2;
							heap_p+=3;
							break;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
629 630 631 632 633
						}
					} else {
						n_free_words-=2;
						if (n_free_words<0){
							*last_heap_pa=heap_p+3+(stack_end-stack_begin);
John van Groningen's avatar
John van Groningen committed
634
							return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
635 636 637 638 639 640 641
						}
					
						++string_p;
						if (arity==0){
							heap_p+=3;
							break;
						} else {
John van Groningen's avatar
John van Groningen committed
642
							arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
643 644 645 646 647 648
							heap_p+=3;
							continue;
						}
					}
				}
			} else {
John van Groningen's avatar
John van Groningen committed
649
				Int *node_p;
650 651 652

#ifdef THREAD
				if (desc & 2){
653 654 655
# ifdef MACH_O64
					node_p=(Int*)(desc-3)+(Int)&__ARRAY__;
# else
656
					node_p=(Int*)(desc-3);
657
# endif
658 659 660 661 662
					*arg_a=node_p;
					++string_p;
					break;
				}
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
663
				
John van Groningen's avatar
John van Groningen committed
664
				node_p=*(Int**)((Int)string_p+(desc-1));
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
				*arg_a=node_p;
				++string_p;
				break;
			}
		}

		if (stack_p==stack_end)
			break;
		
		arg_a=*stack_p++;
	}
	
	*last_heap_pa=heap_p;
	return root_node_p;
}

John van Groningen's avatar
John van Groningen committed
681
void remove_forwarding_pointers_from_string (Int *string_p,Int *end_forwarding_pointers)
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
682 683 684 685
{
	string_p+=2;

	while (string_p<end_forwarding_pointers){
John van Groningen's avatar
John van Groningen committed
686
		Int forwarding_pointer;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
687 688 689
			
		forwarding_pointer=*string_p;
		if (!(forwarding_pointer & 1)){
John van Groningen's avatar
John van Groningen committed
690
			Int desc;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
691
			
John van Groningen's avatar
John van Groningen committed
692
			desc=*(Int*)forwarding_pointer;
693 694 695
#ifdef MACH_O64
			*string_p=desc-(Int)&__ARRAY__;
#else
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
696
			*string_p=desc;
697
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
698
			if (desc & 2){
John van Groningen's avatar
John van Groningen committed
699
				unsigned Int arity;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
700 701 702
				
				arity=((unsigned short *)desc)[-1];
				if (arity==0){
John van Groningen's avatar
John van Groningen committed
703
					if (desc==(Int)&INT_descriptor+2 || desc==(Int)&CHAR+2 || desc==(Int)&BOOL+2
704
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
705 706 707
						|| desc==(Int)&REAL+2
#endif
					){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
708
						string_p+=2;
709
#if ! ARCH_64
John van Groningen's avatar
John van Groningen committed
710
					} else if (desc==(Int)&REAL+2){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
711
						string_p+=3;
John van Groningen's avatar
John van Groningen committed
712 713 714
#endif
					} else if (desc==(Int)&__STRING__+2){
						unsigned Int length,n_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
715 716 717
							
						length=string_p[1];
						string_p+=2;
718
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
719 720
						n_words=(length+7)>>3;
#else
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
721
						n_words=(length+3)>>2;
John van Groningen's avatar
John van Groningen committed
722
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
723
						string_p+=n_words;
John van Groningen's avatar
John van Groningen committed
724 725
					} else if (desc==(Int)&__ARRAY__+2){
						Int array_size,elem_desc;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
726 727 728 729 730 731

						array_size=string_p[1];
						elem_desc=string_p[2];
						string_p+=3;
													
						if (elem_desc==0){
John van Groningen's avatar
John van Groningen committed
732
						} else if (elem_desc==(Int)&INT_descriptor+2
733
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
734 735 736
							|| elem_desc==(Int)&REAL+2
#endif
						){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
737
							string_p+=array_size;
738
#if ! ARCH_64
John van Groningen's avatar
John van Groningen committed
739
						} else if (elem_desc==(Int)&REAL+2){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
740 741
							array_size<<=1;
							string_p+=array_size;
John van Groningen's avatar
John van Groningen committed
742 743
#endif
						} else if (elem_desc==(Int)&BOOL+2){
744
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
745 746
							array_size=(array_size+7)>>3;
#else
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
747
							array_size=(array_size+3)>>2;
John van Groningen's avatar
John van Groningen committed
748
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
749 750
							string_p+=array_size;
						} else {
John van Groningen's avatar
John van Groningen committed
751
							Int n_field_pointers,n_non_field_pointers,field_size;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
752 753

							n_field_pointers=*(unsigned short *)elem_desc;
John van Groningen's avatar
John van Groningen committed
754
							field_size=((unsigned short *)elem_desc)[-1]-(Int)256;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
755 756 757 758 759 760 761 762 763 764
							n_non_field_pointers=field_size-n_field_pointers;
						
							string_p+=n_non_field_pointers*array_size;
						}
					} else {
						++string_p;
					}
				} else {
					++string_p;
					if (arity>=256){
John van Groningen's avatar
John van Groningen committed
765
						Int n_pointers,n_non_pointers;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
766 767 768 769 770 771 772 773

						n_pointers=*(unsigned short*)desc;
						arity-=256;
						n_non_pointers=arity-n_pointers;
						string_p+=n_non_pointers;
					}
				}
			} else {
John van Groningen's avatar
John van Groningen committed
774
				Int arity;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
775 776 777 778

				arity=((int*)desc)[-1];
				++string_p;
				if (arity>=256){
John van Groningen's avatar
John van Groningen committed
779
					Int n_non_pointers;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
780 781 782 783 784 785 786 787 788 789
					
					n_non_pointers=arity>>8;
					string_p+=n_non_pointers;
				}
			}
		} else {
			++string_p;
		}
	}
}