1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
|
/*-----------------------------------------------------------------*-C-*---
* File: handc/heapi/savewrit.c
*
* Copyright (C)1997 Donovan Kolbly <d.kolbly@rscheme.org>
* as part of the RScheme project, licensed for free use.
* See <http://www.rscheme.org/> for the latest information.
*
* File version: 1.6
* File mod date: 1997.12.18 22:16:14
* System build: v0.7.2, 97.12.21
*
* Purpose: Output-file portion of 0.7 image saver
*------------------------------------------------------------------------*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <rscheme/smemory.h>
#include <rscheme/hashmain.h>
#include <rscheme/hashfn.h>
#include <rscheme/linktype.h>
#include "saveimg.h"
#define DEBUG_SAVE 0 /* define to 1 for some trace info */
static obj *ref_str_tbl;
static struct file_header hdr;
static UINT_32 num_objects;
static UINT_32 known_num_objects;
static UINT_32 ref_str_tbl_size;
/************************************************************************
**
** Output buffer management (inlined)
**
************************************************************************/
static char *output_ptr;
static char *output_buffer;
static char *output_limit;
static FILE *output_file;
static UINT_32 output_bytes_written;
static UINT_32 output_posn( void )
{
return output_bytes_written + (output_ptr - output_buffer);
}
static void init_output( FILE *f, UINT_32 len )
{
output_buffer = output_ptr = (char *)malloc( len );
output_limit = output_buffer + len;
output_file = f;
output_bytes_written = 0;
}
static void output_flush( UINT_32 len )
{
UINT_32 flsh = output_ptr - output_buffer;
/*printf( "** flushing %u bytes\n", flsh );*/
if (flsh && (fwrite( output_buffer, 1, flsh, output_file ) != flsh))
{
perror( "save-image" );
}
output_bytes_written += flsh;
output_ptr = output_buffer;
if (output_ptr + len > output_limit)
{
UINT_32 elbow_room = len + 1024;
free( output_buffer ); /* free+malloc saves the copy in realloc */
output_buffer = (char *)malloc( elbow_room );
output_ptr = output_buffer;
output_limit = output_ptr + elbow_room;
}
output_ptr = output_buffer + len;
}
static void output_close( void )
{
output_flush(0);
free( output_buffer );
}
static _rs_inline void *output_alloc( UINT_32 len )
{
if (output_ptr + len <= output_limit)
{
char *p = output_ptr;
output_ptr += len;
return (void *)p;
}
else
{
output_flush(len);
return (void *)output_buffer;
}
}
/************************************************************************
**
** Output Reference Transforming
**
************************************************************************/
static _rs_inline obj unswizzle( obj item )
{
if (OBJ_ISA_PTR(item))
{
obj swz = PTR_TO_HDRPTR(replace_ptr(item))->pob_class;
assert( OBJ_ISA_PTR(swz)
&& ((VAL(swz)>>PRIMARY_TAG_SIZE) < known_num_objects) );
return swz;
}
else
{
return item;
}
}
/************************************************************************
**
** Output Processing
**
************************************************************************/
static void write_part_ref( struct part_descr *pd );
static void *alloc_output_frame( UINT_32 size_in_bytes,
obj preamble,
enum load1_mode mode )
{
UINT_32 *p;
p = output_alloc( (size_in_bytes + 3 + 12) & ~3 );
*p++ = HOST_TO_BIG_ENDIAN_32( size_in_bytes );
*p++ = HOST_TO_BIG_ENDIAN_32( mode );
*p++ = VAL(HOST_TO_BIG_ENDIAN_OBJ( unswizzle(preamble) ));
return p;
}
static void write_gvec( obj gvec, obj orig_class )
{
obj *d, *s, *limit;
UINT_32 len;
len = SIZEOF_PTR( gvec );
s = (obj *)PTR_TO_DATAPTR( gvec );
limit = (obj *)(((char *)s) + len);
d = (obj *)alloc_output_frame( len, orig_class, LOAD1_ARRAY32 );
while (s < limit)
*d++ = HOST_TO_BIG_ENDIAN_OBJ( unswizzle( *s++ ) );
}
static void write_mixvec( obj mixvec, obj orig_class, UINT_32 nslots )
{
obj *d, *s;
UINT_32 i, len, *s_word, *d_word, *limit_word;
len = SIZEOF_PTR( mixvec );
s = (obj *)PTR_TO_DATAPTR( mixvec );
d = (obj *)alloc_output_frame( len, orig_class, LOAD1_ARRAY32 );
for (i=0; i<nslots; i++)
{
*d++ = HOST_TO_BIG_ENDIAN_OBJ( unswizzle( *s++ ) );
}
/* write the rest as an array of 32bits */
limit_word = (UINT_32 *)(((char *)s) + SIZEOF_PTR(mixvec));
s_word = (UINT_32 *)s;
d_word = (UINT_32 *)d;
while (s_word < limit_word)
*d_word++ = HOST_TO_BIG_ENDIAN_32( *s_word++ );
}
static void write_alloc_area( obj item, obj orig_class )
{
write_mixvec( item, orig_class, 2 );
}
void write_template( obj tmpl, obj orig_class )
{
obj link2;
struct function_descr *fn;
obj *d, *s, *limit;
UINT_32 len;
struct function_descr **pfns;
unsigned i;
link2 = gvec_read(tmpl,TEMPLATE_LINKAGE);
if (OBJ_ISA_PTR(link2))
{
#ifdef DEBUG_0
printf( "template %#x is already unswizzled\n", VAL(tmpl) );
#endif /* DEBUG_0 */
/* this template is already unswizzled, so just
write it out as a gvec */
write_gvec( tmpl, orig_class );
}
else
{
#ifdef DEBUG_0
printf( "template %#x is being unswizzled\n", VAL(tmpl) );
#endif /* DEBUG_0 */
fn = (struct function_descr *)OBJ_TO_RAW_PTR(link2);
assert( fn );
/* it can't be a stub; it was unstubbed during traversal */
assert( fn->in_part->tag < STUB_PART_TAG );
/* and, it's had a name assigned */
assert( fn->in_part->unswizzled_as );
/* find the function's index in the part */
pfns = fn->in_part->functions;
for (i=0; pfns[i]; i++)
{
if (pfns[i] == fn)
break;
}
#ifdef DEBUG_0
if (pfns[i])
printf( "\t%s is %s.%d\n", fn->name, fn->in_part->name, i );
#endif /* DEBUG_0 */
if (!pfns[i])
{
const char *s;
fprintf( stderr, "Warning: (function_descr %p) `", fn );
for (s = fn->name; *s; s++)
if (isprint(*s))
fputc( *s, stderr );
else
fprintf( stderr, "\\%03o", *(UINT_8 *)s );
fprintf( stderr, "'\nis not found in (part %p) `", fn->in_part );
for (s = fn->in_part->name; *s; s++)
if (isprint(*s))
fputc( *s, stderr );
else
fprintf( stderr, "\\%03o", *(UINT_8 *)s );
fprintf( stderr, "'\n" );
}
len = SIZEOF_PTR( tmpl );
s = (obj *)PTR_TO_DATAPTR( tmpl );
limit = (obj *)(((char *)s) + len);
d = (obj *)alloc_output_frame( len, orig_class, LOAD1_ARRAY32 );
*d++ = HOST_TO_BIG_ENDIAN_OBJ( int2fx(i) );
*d++ = HOST_TO_BIG_ENDIAN_OBJ( OBJ(fn->in_part->unswizzled_as) );
s += 2;
while (s < limit)
*d++ = HOST_TO_BIG_ENDIAN_OBJ( unswizzle( *s++ ) );
}
}
void write_array_32( obj item, obj orig_class )
{
UINT_32 *s, *limit, *d, len;
len = SIZEOF_PTR( item );
s = (UINT_32 *)PTR_TO_DATAPTR( item );
limit = (UINT_32 *)(((char *)s) + len);
d = (UINT_32 *)alloc_output_frame( len, orig_class, LOAD1_ARRAY32 );
while (s < limit)
*d++ = HOST_TO_BIG_ENDIAN_32( *s++ );
}
void write_array_64( obj item, obj orig_class )
{
UINT_32 len;
IEEE_64 *s, *limit, *d;
len = SIZEOF_PTR( item );
s = (IEEE_64 *)PTR_TO_DATAPTR( item );
limit = (IEEE_64 *)(((char *)s) + len);
d = (IEEE_64 *)alloc_output_frame( len, orig_class, LOAD1_ARRAY64 );
while (s < limit)
*d++ = HOST_TO_BIG_ENDIAN_IEEE_64( *s++ );
}
/* Writes a frame of the form
+---------------+
| size |
+---------------+
| mode |
+---------------+
| preamble |
+---------------+
| |
: text :
| .... |
+---------------+
which is the format used by LOAD1_REF, LOAD1_SYMBOL,
LOAD1_PART, and LOAD1_ARRAY8
(where the content of the `preamble' word depends on the mode:
mode use of preamble word
--------------- ---------------------------------------------------
LOAD1_REF => string hash value
LOAD1_SYMBOL => string hash value (NOT same as symbol's hash value)
LOAD1_PART => tag of part
LOAD1_ARRAY8 => bytevector class, e.g. reference to <string>
)
Note that the string ('text') had better be ZERO padded to a
full word length!
*/
static void write_string_frame( enum load1_mode mode, obj preamble, obj text )
{
void *d;
UINT_32 len;
len = SIZEOF_PTR( text );
d = alloc_output_frame( len, preamble, mode );
memcpy( d, PTR_TO_DATAPTR(text), (len + 3) & ~3 );
}
static void write_array_8( obj item, obj orig_class )
{
write_string_frame( LOAD1_ARRAY8, orig_class, item );
}
static void write_ref( obj item, obj orig_class )
{
if (EQ(orig_class,FALSE_OBJ))
{
/* it's a part descriptor */
struct part_descr *p = (struct part_descr *)OBJ_TO_RAW_PTR(item);
/*printf( "ref: PART %s %d\n", p->in_module->name, p->tag );*/
write_part_ref( (struct part_descr *)OBJ_TO_RAW_PTR(item) );
}
else
{
/* it's a normal reference -- write out the key string */
obj str, ext_name = PTR_TO_HDRPTR(item)->pob_class;
obj hash;
assert( VAL(ext_name)/SLOT(1) < ref_str_tbl_size );
str = ref_str_tbl[ VAL(ext_name) / SLOT(1) ];
hash = raw_bytes_hash( PTR_TO_DATAPTR(str), SIZEOF_PTR(str)-1 );
/*printf( "ref: %s\n", PTR_TO_DATAPTR(str) );*/
write_string_frame( LOAD1_REF, hash, str );
}
}
static void write_symbol( obj item, obj orig_class )
{
write_string_frame( LOAD1_SYMBOL,
ZERO,
gvec_read( item, SYMBOL_STR ) );
}
/*
Here, we are writing out a <PartDescr>. At load time,
it will be swizzled to point directly to the part_descr
*/
static void write_part( obj item, obj orig_class )
{
write_string_frame( LOAD1_PART,
gvec_read( item, PART_DESCR_PART_TAG ),
gvec_read( item, PART_DESCR_MODULE_NAME ) );
}
static void write_part_ref( struct part_descr *pd )
{
char *d;
UINT_32 len;
#ifdef DEBUG_0
printf( "Writing part %#x", (UINT_32)pd );
fflush(stdout);
printf( " (%s) as %#x\n", pd->name, pd->unswizzled_as );
#endif /* DEBUG_0 */
if (pd->in_module->loaded_from)
{
len = strlen( pd->in_module->name )
+ 1 + strlen(pd->in_module->loaded_from);
}
else
{
len = strlen( pd->in_module->name );
}
d = (char *)alloc_output_frame( len+1, int2fx(pd->tag), LOAD1_PART );
if (pd->in_module->loaded_from)
{
sprintf( d, "%s|%s", pd->in_module->name, pd->in_module->loaded_from );
}
else
{
memcpy( d, pd->in_module->name, len );
}
memset( d + len, 0, 4 - (len & 3) );
}
static void queue_write( SaveQueue *q,
void (*proc)( obj item, obj orig_class ) )
{
UINT_32 i, n = q->count;
struct queue_entry *p;
num_objects += n;
for (i=0, p=q->contents; i<n; i++, p++)
proc( p->thing, p->orig_class );
}
static obj alloc_hash_table( void )
{
return make4( vector_class,
make_empty_vector(8),
int2fx(3),
ZERO,
vector_class );
}
/************************************************************************
**
** Top-level Interface
**
************************************************************************/
void hi_init_output( FILE *f,
obj refs_vec, obj ref_names, obj root,
SaveQueue *used_refs,
UINT_32 n_obj )
{
unsigned i;
/*
* for those reference heap objects that have been used
* (as we can tell, because their pob_class is non-ONE)
* create an entry in the ref_str_table for their reference
* string.
*/
ref_str_tbl_size = used_refs->count;
ref_str_tbl = (obj *)malloc( used_refs->count * sizeof(obj) );
for (i=0; i<SIZEOF_PTR(refs_vec); i+=SLOT(1))
{
obj key, val, ext_name;
key = gvec_ref( refs_vec, i );
val = gvec_ref( ref_names, i );
ext_name = PTR_TO_HDRPTR(key)->pob_class;
if (!EQ(ext_name,int2fx(1)))
{
assert( OBJ_ISA_PTR(ext_name) );
assert( VAL(ext_name)/SLOT(1) < used_refs->count );
ref_str_tbl[ VAL(ext_name)/SLOT(1) ] = val;
}
}
memset( &hdr, 0, sizeof hdr );
init_output( f, 1280000 );
num_objects = 0; /* we're going to count ourselves, to verify */
known_num_objects = n_obj;
hdr.entry_object = HOST_TO_BIG_ENDIAN_OBJ(unswizzle( root ));
memset( output_alloc( FILE_HDR_OFFSET ),
' ',
FILE_HDR_OFFSET );
memset( output_alloc( sizeof(struct file_header) ),
0,
sizeof(struct file_header) );
assert( output_posn() == FILE_DATA_OFFSET );
}
void hi_done_output( void )
{
output_close();
hdr.num_objects = HOST_TO_BIG_ENDIAN_32( num_objects );
hdr.magic = HOST_TO_BIG_ENDIAN_32( IMAGE_MAGIC_NUMBER );
hdr.version = HOST_TO_BIG_ENDIAN_32( IMAGE_VERSION_NUMBER );
fseek( output_file, FILE_HDR_OFFSET, SEEK_SET );
fwrite( &hdr, sizeof(struct file_header), 1, output_file );
fclose( output_file );
free( ref_str_tbl );
#if DEBUG_SAVE
printf( "wrote out %u objects in %u bytes\n",
num_objects, output_bytes_written );
#endif
}
void hi_output_mode2( enum load2_mode mode2,
SaveQueue **queues, unsigned num_queues )
{
UINT_32 i, cnt = 0;
unsigned k;
obj *d;
if (!hdr.data_area_length)
{
UINT_32 dalen = output_posn() - FILE_DATA_OFFSET;
hdr.data_area_length = HOST_TO_BIG_ENDIAN_32( dalen );
}
for (k=0; k<num_queues; k++)
cnt += queues[k]->count;
hdr.load2_offset[mode2] = HOST_TO_BIG_ENDIAN_32( output_posn() );
hdr.load2_count[mode2] = HOST_TO_BIG_ENDIAN_32( cnt );
d = (obj *)output_alloc( cnt * sizeof(obj) );
for (k=0; k<num_queues; k++)
{
UINT_32 n = queues[k]->count;
struct queue_entry *p = queues[k]->contents;
for (i=0; i<n; i++, p++)
{
obj save_name = PTR_TO_HDRPTR(p->thing)->pob_class;
*d++ = HOST_TO_BIG_ENDIAN_OBJ(save_name);
}
}
}
void hi_output_refs( SaveQueue *q )
{
queue_write( q, write_ref );
}
/************************************************************************
**
** Exported Function Structure
**
************************************************************************/
#define QWRITE(n,one) static void n( SaveQueue *q ) { queue_write( q, one ); }
QWRITE(writeq_gvec, write_gvec)
QWRITE(writeq_array_8, write_array_8)
QWRITE(writeq_symbol, write_symbol)
QWRITE(writeq_part, write_part)
QWRITE(writeq_template, write_template)
QWRITE(writeq_array_64, write_array_64)
QWRITE(writeq_array_32, write_array_32)
QWRITE(writeq_alloc_area, write_alloc_area)
struct writer_info hi_writers[NUM_CLASS_MODES] = {
{ /* 0: gvec */ writeq_gvec, LOAD2_GVEC },
{ /* 1: bvec */ writeq_array_8, LOAD2_CLASS_ONLY },
{ /* 2: symbol */ writeq_symbol, LOAD2_NOP },
{ /* 3: part */ writeq_part, LOAD2_NOP },
{ /* 4: template */ writeq_template, LOAD2_TEMPLATE },
{ /* 5: part-cont */ writeq_gvec, LOAD2_PARTCONT },
{ /* 6: longfloat */ writeq_array_64, LOAD2_CLASS_ONLY },
{ /* 7: uint32*n */ writeq_array_32, LOAD2_CLASS_ONLY },
{ /* 8: alloc-area */ writeq_alloc_area, LOAD2_MIXVEC_2 } };
|