copy_string_to_graph.c 17.9 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 10 11
# ifdef MACH_O64
#  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 19 20 21 22 23
# define __STRING__ _STRING__
# define __ARRAY__ _ARRAY__
#endif

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

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

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

John van Groningen's avatar
John van Groningen committed
34
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
35
{
John van Groningen's avatar
John van Groningen committed
36
	Int ***stack_p,***stack_begin,***stack_end,*heap_p,**arg_a,*root_node_p,n_free_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
37 38 39 40 41 42 43 44 45

	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
46
	n_free_words=(Int*)end_free_heap-(Int*)begin_free_heap;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
47 48 49 50
	arg_a=&root_node_p;

	for (;;){
		for (;;){
John van Groningen's avatar
John van Groningen committed
51
			Int desc;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
52 53 54 55 56
			
			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
57
				return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
58 59 60
			}

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

							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
159
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
160 161 162 163 164 165 166 167 168 169 170
							}
							
							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
171
								Int i;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
172 173 174
								
								stack_p-=array_size;
								if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
175
									Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
176 177 178 179 180

									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
181
										return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
182 183 184 185 186 187
									}
									stack_begin=stack_p;
								}
								
								i=array_size;
								while (--i>=0)
John van Groningen's avatar
John van Groningen committed
188
									stack_p[i]=(Int**)&heap_p[i];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
189 190 191

								heap_p+=array_size;
								break;
John van Groningen's avatar
John van Groningen committed
192
							} else if (elem_desc==(Int)&INT_descriptor+2
193
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
194 195 196
								|| elem_desc==(Int)&REAL+2
#endif
							){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
197 198 199
								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
200
									return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
201 202 203 204 205 206
								}

								copy (heap_p,string_p,array_size);
								string_p+=array_size;
								heap_p+=array_size;
								break;
207
#if ! ARCH_64
John van Groningen's avatar
John van Groningen committed
208
							} else if (elem_desc==(Int)&REAL+2){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
209 210 211 212 213
								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
214
									return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
215 216 217 218 219 220
								}
								
								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
221 222
#endif
							} else if (elem_desc==(Int)&BOOL+2){
223
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
224 225
								array_size=(array_size+7)>>3;
#else
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
226
								array_size=(array_size+3)>>2;
John van Groningen's avatar
John van Groningen committed
227
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
228 229 230
								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
231
									return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
232 233 234 235 236 237 238
								}
								
								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
239
								Int n_field_pointers,field_size;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
240 241

								n_field_pointers=*(unsigned short *)elem_desc;
John van Groningen's avatar
John van Groningen committed
242
								field_size=((unsigned short *)elem_desc)[-1]-(Int)256;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
243 244 245 246 247 248 249

								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
250
										return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
251 252 253 254 255 256 257
									}
																		
									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
258
									Int i;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
259 260 261 262 263
									
									array_size*=field_size;

									stack_p-=array_size;
									if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
264
										Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
265 266 267 268 269

										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
270
											return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
271 272 273 274 275 276
										}
										stack_begin=stack_p;
									}
									
									i=array_size;
									while (--i>=0)
John van Groningen's avatar
John van Groningen committed
277
										stack_p[i]=(Int**)&heap_p[i];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
278 279 280 281
									
									heap_p+=array_size;
									break;
								} else {
John van Groningen's avatar
John van Groningen committed
282
									Int n_non_field_pointers,i,***pointer_p;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
283 284 285 286 287 288
									
									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
289
										return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
290 291 292 293
									}
									
									stack_p-=array_size*n_field_pointers;
									if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
294
										Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
295 296 297 298 299

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

						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
374
							return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
375 376
						}

John van Groningen's avatar
John van Groningen committed
377 378
						arg_a=(Int**)&heap_p[1];
						heap_p[2]=(Int)&heap_p[3];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
379 380 381 382 383 384
						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
385
							Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
386 387 388 389 390

							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
391
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
392 393 394 395 396
							}
							stack_begin=stack_p;
						}

						--n_words;
John van Groningen's avatar
John van Groningen committed
397
						stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
398
						--n_words;
John van Groningen's avatar
John van Groningen committed
399
						stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
400
						while (--n_words>=0)
John van Groningen's avatar
John van Groningen committed
401
							stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
402 403 404 405 406

						heap_p+=arity-1;
						++string_p;
						continue;
					} else {
John van Groningen's avatar
John van Groningen committed
407
						Int n_pointers;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
408 409 410 411 412 413 414
						
						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
415
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
416 417 418 419 420 421 422 423
							}
							
							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
424
								arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
425 426 427 428 429 430 431 432
								++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
433
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
							}

							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
450
											return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
451 452 453 454
										}
										--stack_begin;
									}
									++string_p;
John van Groningen's avatar
John van Groningen committed
455
									*--stack_p=(Int**)&heap_p[2];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
456
								}
John van Groningen's avatar
John van Groningen committed
457
								arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
458 459 460 461 462 463 464
								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
465
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
466 467
							}
							
John van Groningen's avatar
John van Groningen committed
468
							heap_p[2]=(Int)&heap_p[3];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
469 470 471 472 473 474 475 476 477 478 479
							
							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
480
								Int n_non_pointers;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
481

John van Groningen's avatar
John van Groningen committed
482
								arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
483 484 485 486 487 488
								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
489
									Int *non_pointers_p;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
490 491 492 493 494 495 496 497 498

									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
499
									Int i;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
500 501 502
									
									stack_p-=n_pointers;
									if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
503
										Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
504 505 506 507 508

										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
509
											return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
510 511 512 513 514 515
										}
										stack_begin=stack_p;
									}
									
									i=n_pointers;
									while (--i>=0)
John van Groningen's avatar
John van Groningen committed
516
										stack_p[i]=(Int**)&heap_p[i];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
517 518 519 520 521 522 523
								}
								heap_p+=n_pointers+n_non_pointers;
								continue;
							}
						}
					}
				} else {
John van Groningen's avatar
John van Groningen committed
524
					Int arity;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
525 526 527 528

					arity=((int*)desc)[-1];
					if (arity>1){
						if (arity<256){
John van Groningen's avatar
John van Groningen committed
529
							Int n_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
530 531 532 533
							
							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
534
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
535 536 537 538 539 540
							}

							n_words=arity-1;

							stack_p-=n_words;
							if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
541
								Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
542 543 544 545 546

								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
547
									return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
548 549 550 551
								}
								stack_begin=stack_p;
							}

John van Groningen's avatar
John van Groningen committed
552
							arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
553 554 555
							heap_p+=2;
							
							--n_words;
John van Groningen's avatar
John van Groningen committed
556
							stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
557
							while (--n_words>=0)
John van Groningen's avatar
John van Groningen committed
558
								stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
559 560 561

							++string_p;
							heap_p+=arity-1;
John van Groningen's avatar
John van Groningen committed
562
							continue;
563
						} else if (arity!=257){
John van Groningen's avatar
John van Groningen committed
564
							Int n_pointers,n_non_pointers,*non_pointers_p;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
565 566 567 568 569 570 571 572
							
							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
573
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
574 575 576 577 578 579 580 581 582 583 584 585 586
							}

							++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
587
								arg_a=(Int**)&heap_p[0];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
588 589 590 591 592 593
								++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
594
										Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
595 596 597 598 599

										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
600
											return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
601 602 603 604 605
										}
										stack_begin=stack_p;
									}
									
									--n_pointers;
John van Groningen's avatar
John van Groningen committed
606
									stack_p[n_pointers]=(Int**)&heap_p[n_pointers];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
607
									while (--n_pointers>=0)
John van Groningen's avatar
John van Groningen committed
608
										stack_p[n_pointers]=(Int**)&heap_p[n_pointers];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
609 610 611 612
								}
								heap_p+=arity-1;
								continue;
							}
613 614 615 616
						} 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
617
								return (Int*)((Int)&string_p[1]+1);
618 619 620 621 622 623
							}

							heap_p[1]=string_p[1];
							string_p+=2;
							heap_p+=3;
							break;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
624 625 626 627 628
						}
					} 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
629
							return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
630 631 632 633 634 635 636
						}
					
						++string_p;
						if (arity==0){
							heap_p+=3;
							break;
						} else {
John van Groningen's avatar
John van Groningen committed
637
							arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
638 639 640 641 642 643
							heap_p+=3;
							continue;
						}
					}
				}
			} else {
John van Groningen's avatar
John van Groningen committed
644
				Int *node_p;
645 646 647 648 649 650 651 652 653

#ifdef THREAD
				if (desc & 2){
					node_p=(Int*)(desc-3);
					*arg_a=node_p;
					++string_p;
					break;
				}
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
654
				
John van Groningen's avatar
John van Groningen committed
655
				node_p=*(Int**)((Int)string_p+(desc-1));
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
				*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
672
void remove_forwarding_pointers_from_string (Int *string_p,Int *end_forwarding_pointers)
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
673 674 675 676
{
	string_p+=2;

	while (string_p<end_forwarding_pointers){
John van Groningen's avatar
John van Groningen committed
677
		Int forwarding_pointer;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
678 679 680
			
		forwarding_pointer=*string_p;
		if (!(forwarding_pointer & 1)){
John van Groningen's avatar
John van Groningen committed
681
			Int desc;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
682
			
John van Groningen's avatar
John van Groningen committed
683
			desc=*(Int*)forwarding_pointer;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
684 685
			*string_p=desc;
			if (desc & 2){
John van Groningen's avatar
John van Groningen committed
686
				unsigned Int arity;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
687 688 689
				
				arity=((unsigned short *)desc)[-1];
				if (arity==0){
John van Groningen's avatar
John van Groningen committed
690
					if (desc==(Int)&INT_descriptor+2 || desc==(Int)&CHAR+2 || desc==(Int)&BOOL+2
691
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
692 693 694
						|| desc==(Int)&REAL+2
#endif
					){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
695
						string_p+=2;
696
#if ! ARCH_64
John van Groningen's avatar
John van Groningen committed
697
					} else if (desc==(Int)&REAL+2){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
698
						string_p+=3;
John van Groningen's avatar
John van Groningen committed
699 700 701
#endif
					} else if (desc==(Int)&__STRING__+2){
						unsigned Int length,n_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
702 703 704
							
						length=string_p[1];
						string_p+=2;
705
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
706 707
						n_words=(length+7)>>3;
#else
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
708
						n_words=(length+3)>>2;
John van Groningen's avatar
John van Groningen committed
709
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
710
						string_p+=n_words;
John van Groningen's avatar
John van Groningen committed
711 712
					} else if (desc==(Int)&__ARRAY__+2){
						Int array_size,elem_desc;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
713 714 715 716 717 718

						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
719
						} else if (elem_desc==(Int)&INT_descriptor+2
720
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
721 722 723
							|| elem_desc==(Int)&REAL+2
#endif
						){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
724
							string_p+=array_size;
725
#if ! ARCH_64
John van Groningen's avatar
John van Groningen committed
726
						} else if (elem_desc==(Int)&REAL+2){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
727 728
							array_size<<=1;
							string_p+=array_size;
John van Groningen's avatar
John van Groningen committed
729 730
#endif
						} else if (elem_desc==(Int)&BOOL+2){
731
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
732 733
							array_size=(array_size+7)>>3;
#else
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
734
							array_size=(array_size+3)>>2;
John van Groningen's avatar
John van Groningen committed
735
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
736 737
							string_p+=array_size;
						} else {
John van Groningen's avatar
John van Groningen committed
738
							Int n_field_pointers,n_non_field_pointers,field_size;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
739 740

							n_field_pointers=*(unsigned short *)elem_desc;
John van Groningen's avatar
John van Groningen committed
741
							field_size=((unsigned short *)elem_desc)[-1]-(Int)256;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
742 743 744 745 746 747 748 749 750 751
							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
752
						Int n_pointers,n_non_pointers;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
753 754 755 756 757 758 759 760

						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
761
				Int arity;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
762 763 764 765

				arity=((int*)desc)[-1];
				++string_p;
				if (arity>=256){
John van Groningen's avatar
John van Groningen committed
766
					Int n_non_pointers;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
767 768 769 770 771 772 773 774 775 776
					
					n_non_pointers=arity>>8;
					string_p+=n_non_pointers;
				}
			}
		} else {
			++string_p;
		}
	}
}