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

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

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

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

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

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

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

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

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

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

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

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

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

						--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
						--n_words;
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
						while (--n_words>=0)
John van Groningen's avatar
John van Groningen committed
403
							stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
404 405 406 407 408

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

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

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

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

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

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

							n_words=arity-1;

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

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

John van Groningen's avatar
John van Groningen committed
554
							arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
555 556 557
							heap_p+=2;
							
							--n_words;
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
							while (--n_words>=0)
John van Groningen's avatar
John van Groningen committed
560
								stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
561 562 563

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

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

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

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

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

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

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

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

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

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