copy_string_to_graph.c 18.5 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
#endif

24 25 26 27 28
#if defined (MACH_O64) || defined (PIC)
// Use positions relative to _ARRAY_ for address space layout randomization systems.
#  define USE_DESC_RELATIVE_TO_ARRAY 1
#endif

John van Groningen's avatar
John van Groningen committed
29 30
extern void *INT_descriptor,*CHAR,*BOOL,*REAL,*__STRING__,*__ARRAY__;
extern Int small_integers[],static_characters[];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
31

John van Groningen's avatar
John van Groningen committed
32 33
/*inline*/
static void copy (Int *dest_p,Int *source_p,Int n_words)
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
34
{
John van Groningen's avatar
John van Groningen committed
35
	Int i;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
36 37 38 39 40

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

John van Groningen's avatar
John van Groningen committed
41
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
42
{
John van Groningen's avatar
John van Groningen committed
43
	Int ***stack_p,***stack_begin,***stack_end,*heap_p,**arg_a,*root_node_p,n_free_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
44 45 46 47 48 49 50 51 52

	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
53
	n_free_words=(Int*)end_free_heap-(Int*)begin_free_heap;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
54 55 56 57
	arg_a=&root_node_p;

	for (;;){
		for (;;){
John van Groningen's avatar
John van Groningen committed
58
			Int desc;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
59 60 61 62 63
			
			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
64
				return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
65 66 67
			}

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

							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
169
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
170 171 172 173 174
							}
							
							array_size=string_p[1];
							elem_desc=string_p[2];
							string_p+=3;
175 176

#if defined (USE_DESC_RELATIVE_TO_ARRAY)
177 178 179
							if (elem_desc!=0)
								elem_desc+=(Int)&__ARRAY__;
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
180 181 182 183 184
							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
185
								Int i;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
186 187 188
								
								stack_p-=array_size;
								if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
189
									Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
190 191 192 193 194

									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
195
										return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
196 197 198 199 200 201
									}
									stack_begin=stack_p;
								}
								
								i=array_size;
								while (--i>=0)
John van Groningen's avatar
John van Groningen committed
202
									stack_p[i]=(Int**)&heap_p[i];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
203 204 205

								heap_p+=array_size;
								break;
John van Groningen's avatar
John van Groningen committed
206
							} else if (elem_desc==(Int)&INT_descriptor+2
207
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
208 209 210
								|| elem_desc==(Int)&REAL+2
#endif
							){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
211 212 213
								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;
221
#if ! ARCH_64
John van Groningen's avatar
John van Groningen committed
222
							} else if (elem_desc==(Int)&REAL+2){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
223 224 225 226 227
								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
228
									return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
229 230 231 232 233 234
								}
								
								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
235 236
#endif
							} else if (elem_desc==(Int)&BOOL+2){
237
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
238 239
								array_size=(array_size+7)>>3;
#else
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
240
								array_size=(array_size+3)>>2;
John van Groningen's avatar
John van Groningen committed
241
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
242 243 244
								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
245
									return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
246 247 248 249 250 251 252
								}
								
								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
253
								Int n_field_pointers,field_size;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
254 255

								n_field_pointers=*(unsigned short *)elem_desc;
John van Groningen's avatar
John van Groningen committed
256
								field_size=((unsigned short *)elem_desc)[-1]-(Int)256;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
257 258 259 260 261 262 263

								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
264
										return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
265 266 267 268 269 270 271
									}
																		
									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
272
									Int i;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
273 274 275 276 277
									
									array_size*=field_size;

									stack_p-=array_size;
									if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
278
										Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
279 280 281 282 283

										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
284
											return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
285 286 287 288 289 290
										}
										stack_begin=stack_p;
									}
									
									i=array_size;
									while (--i>=0)
John van Groningen's avatar
John van Groningen committed
291
										stack_p[i]=(Int**)&heap_p[i];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
292 293 294 295
									
									heap_p+=array_size;
									break;
								} else {
John van Groningen's avatar
John van Groningen committed
296
									Int n_non_field_pointers,i,***pointer_p;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
297 298 299 300 301 302
									
									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
303
										return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
304 305 306 307
									}
									
									stack_p-=array_size*n_field_pointers;
									if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
308
										Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
309 310 311 312 313

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

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

John van Groningen's avatar
John van Groningen committed
391 392
						arg_a=(Int**)&heap_p[1];
						heap_p[2]=(Int)&heap_p[3];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
393 394 395 396 397 398
						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
399
							Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
400 401 402 403 404

							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
405
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
406 407 408 409 410
							}
							stack_begin=stack_p;
						}

						--n_words;
John van Groningen's avatar
John van Groningen committed
411
						stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
412
						--n_words;
John van Groningen's avatar
John van Groningen committed
413
						stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
414
						while (--n_words>=0)
John van Groningen's avatar
John van Groningen committed
415
							stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
416 417 418 419 420

						heap_p+=arity-1;
						++string_p;
						continue;
					} else {
John van Groningen's avatar
John van Groningen committed
421
						Int n_pointers;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
422 423 424 425 426 427 428
						
						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
429
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
430 431 432 433 434 435 436 437
							}
							
							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
438
								arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
439 440 441 442 443 444 445 446
								++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
447
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
							}

							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
464
											return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
465 466 467 468
										}
										--stack_begin;
									}
									++string_p;
John van Groningen's avatar
John van Groningen committed
469
									*--stack_p=(Int**)&heap_p[2];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
470
								}
John van Groningen's avatar
John van Groningen committed
471
								arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
472 473 474 475 476 477 478
								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
479
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
480 481
							}
							
John van Groningen's avatar
John van Groningen committed
482
							heap_p[2]=(Int)&heap_p[3];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
483 484 485 486 487 488 489 490 491 492 493
							
							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
494
								Int n_non_pointers;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
495

John van Groningen's avatar
John van Groningen committed
496
								arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
497 498 499 500 501 502
								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
503
									Int *non_pointers_p;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
504 505 506 507 508 509 510 511 512

									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
513
									Int i;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
514 515 516
									
									stack_p-=n_pointers;
									if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
517
										Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
518 519 520 521 522

										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
523
											return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
524 525 526 527 528 529
										}
										stack_begin=stack_p;
									}
									
									i=n_pointers;
									while (--i>=0)
John van Groningen's avatar
John van Groningen committed
530
										stack_p[i]=(Int**)&heap_p[i];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
531 532 533 534 535 536 537
								}
								heap_p+=n_pointers+n_non_pointers;
								continue;
							}
						}
					}
				} else {
John van Groningen's avatar
John van Groningen committed
538
					Int arity;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
539 540 541 542

					arity=((int*)desc)[-1];
					if (arity>1){
						if (arity<256){
John van Groningen's avatar
John van Groningen committed
543
							Int n_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
544 545 546 547
							
							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
548
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
549 550 551 552 553 554
							}

							n_words=arity-1;

							stack_p-=n_words;
							if (stack_p<stack_begin){
John van Groningen's avatar
John van Groningen committed
555
								Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
556 557 558 559 560

								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
561
									return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
562 563 564 565
								}
								stack_begin=stack_p;
							}

John van Groningen's avatar
John van Groningen committed
566
							arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
567 568 569
							heap_p+=2;
							
							--n_words;
John van Groningen's avatar
John van Groningen committed
570
							stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
571
							while (--n_words>=0)
John van Groningen's avatar
John van Groningen committed
572
								stack_p[n_words]=(Int**)&heap_p[n_words];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
573 574 575

							++string_p;
							heap_p+=arity-1;
John van Groningen's avatar
John van Groningen committed
576
							continue;
577
						} else if (arity!=257){
John van Groningen's avatar
John van Groningen committed
578
							Int n_pointers,n_non_pointers,*non_pointers_p;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
579 580 581 582 583 584 585 586
							
							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
587
								return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
588 589 590 591 592 593 594 595 596 597 598 599 600
							}

							++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
601
								arg_a=(Int**)&heap_p[0];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
602 603 604 605 606 607
								++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
608
										Int extra_words;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
609 610 611 612 613

										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
614
											return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
615 616 617 618 619
										}
										stack_begin=stack_p;
									}
									
									--n_pointers;
John van Groningen's avatar
John van Groningen committed
620
									stack_p[n_pointers]=(Int**)&heap_p[n_pointers];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
621
									while (--n_pointers>=0)
John van Groningen's avatar
John van Groningen committed
622
										stack_p[n_pointers]=(Int**)&heap_p[n_pointers];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
623 624 625 626
								}
								heap_p+=arity-1;
								continue;
							}
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);
632 633 634 635 636 637
							}

							heap_p[1]=string_p[1];
							string_p+=2;
							heap_p+=3;
							break;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
638 639 640 641 642
						}
					} 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
643
							return (Int*)((Int)&string_p[1]+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
644 645 646 647 648 649 650
						}
					
						++string_p;
						if (arity==0){
							heap_p+=3;
							break;
						} else {
John van Groningen's avatar
John van Groningen committed
651
							arg_a=(Int**)&heap_p[1];
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
652 653 654 655 656 657
							heap_p+=3;
							continue;
						}
					}
				}
			} else {
John van Groningen's avatar
John van Groningen committed
658
				Int *node_p;
659 660 661

#ifdef THREAD
				if (desc & 2){
662
# ifdef USE_DESC_RELATIVE_TO_ARRAY
663 664
					node_p=(Int*)(desc-3)+(Int)&__ARRAY__;
# else
665
					node_p=(Int*)(desc-3);
666
# endif
667 668 669 670 671
					*arg_a=node_p;
					++string_p;
					break;
				}
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
672
				
John van Groningen's avatar
John van Groningen committed
673
				node_p=*(Int**)((Int)string_p+(desc-1));
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
				*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
690
void remove_forwarding_pointers_from_string (Int *string_p,Int *end_forwarding_pointers)
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
691 692 693 694
{
	string_p+=2;

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

						array_size=string_p[1];
						elem_desc=string_p[2];
						string_p+=3;
739

Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
740
						if (elem_desc==0){
John van Groningen's avatar
John van Groningen committed
741
						} else if (elem_desc==(Int)&INT_descriptor+2
742
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
743 744 745
							|| elem_desc==(Int)&REAL+2
#endif
						){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
746
							string_p+=array_size;
747
#if ! ARCH_64
John van Groningen's avatar
John van Groningen committed
748
						} else if (elem_desc==(Int)&REAL+2){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
749 750
							array_size<<=1;
							string_p+=array_size;
John van Groningen's avatar
John van Groningen committed
751 752
#endif
						} else if (elem_desc==(Int)&BOOL+2){
753
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
754 755
							array_size=(array_size+7)>>3;
#else
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
756
							array_size=(array_size+3)>>2;
John van Groningen's avatar
John van Groningen committed
757
#endif
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
758 759
							string_p+=array_size;
						} else {
John van Groningen's avatar
John van Groningen committed
760
							Int n_field_pointers,n_non_field_pointers,field_size;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
761 762

							n_field_pointers=*(unsigned short *)elem_desc;
John van Groningen's avatar
John van Groningen committed
763
							field_size=((unsigned short *)elem_desc)[-1]-(Int)256;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
764 765 766 767 768 769 770 771 772 773
							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
774
						Int n_pointers,n_non_pointers;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
775 776 777 778 779 780 781 782

						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
783
				Int arity;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
784 785 786 787

				arity=((int*)desc)[-1];
				++string_p;
				if (arity>=256){
John van Groningen's avatar
John van Groningen committed
788
					Int n_non_pointers;
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
789 790 791 792 793 794 795 796 797 798
					
					n_non_pointers=arity>>8;
					string_p+=n_non_pointers;
				}
			}
		} else {
			++string_p;
		}
	}
}