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

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

								heap_p+=array_size;
								break;
John van Groningen's avatar
John van Groningen committed
205
							} else if (elem_desc==(Int)&INT_descriptor+2
206
#if ARCH_64
John van Groningen's avatar
John van Groningen committed
207 208 209
								|| elem_desc==(Int)&REAL+2
#endif
							){
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
210 211 212
								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
213
									return (Int*)((Int)string_p+1);
Rinus Plasmeijer's avatar
Rinus Plasmeijer committed
214 215 216 217 218 219
								}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

							n_words=arity-1;

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

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

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

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

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

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

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

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

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

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

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

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

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

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