Ravenbrook / Projects / Memory Pool System / Version 1.111 Product Sources / Design Documents

Memory Pool System Project


                 THE DESIGN OF THE MPS WRITEF FUNCTION
                           design.mps.writef
                               draft doc
                           richard 1996-10-18

INTRODUCTION

.intro: This document describes the WriteF function, which allows formatted 
output in a manner similar to ANSI C printf, but allows the MPM to operate in a 
freestanding environment (see design.mps.exec-env).

.background: The documents design.mps.exec-env and design.mps.lib describe the 
design of
the library interface and the reason that it exists.


DESIGN

.no-printf: There is no dependency on printf has been removed.  The MPM only 
depends on fputc and fputs, via the Library Interface (design.mps.lib).  This 
makes it much easier to deploy the MPS in a freestanding environment.  This is 
achieved by implementing our own internal output routines in mpm.c.

Our output requirements are few, so the code is short.  The only output 
function which should be used in the rest of the MPM is WriteF, which is 
similar to fprintf:

  Res WriteF(mps_lib_FILE *stream, ...);

WriteF expects a format string followed by zero or more items to insert into 
the output, followed by another format string, more items, etc., then a NULL 
format string, e.g.

  WriteF(stream,
         "Hello: $A\n", address,
         "Spong: $U ($S)\n", number, string,
         NULL);

This makes Describe methods much easier to do, e.g.:

  WriteF(stream,
         "Buffer $P ($U) {\n", (WriteFP)buffer, (WriteFU)buffer->serial,
         "  base $A  init $A  alloc $A  limit $A\n",
         (WriteFA)buffer->base, (WriteFA)buffer->ap.init, 
         (WriteFA)buffer->ap.alloc, (WriteFA)buffer->ap.limit,
         "  Pool $P\n",        (WriteFP)buffer->pool,
         "  Seg $P\n",         (WriteFP)buffer->seg,
         "  rank $U\n",        (WriteFU)buffer->rank,
         "  alignment $W\n",   (WriteFW)buffer->alignment,
         "  grey $B\n",        (WriteFB)buffer->grey,
         "  shieldMode $B\n",  (WriteFB)buffer->shieldMode,
         "  p $P  i $U\n",     (WriteFP)buffer->p, (WriteFU)buffer->i,
         "} Buffer $P ($U)\n", (WriteFP)buffer, (WriteFU)buffer->serial,
         NULL);

.types: For each format $X that WriteF supports, there is a type defined in 
impl.h.mpmtypes WriteFX which is the promoted version of that type.  These are 
provided both to ensure promotion and to avoid any confusion about what type 
should be used in a cast.  It is easy to check the casts against the formats to 
ensure that they correspond.  .types.future: It is possibly that this type set 
or similar may be used in future in some generalisation of varargs in the MPS.

.formats: The formats supported are as follows.

  code    name        type            example rendering

  $A      address     Addr            9EF60010
  $P      pointer     void *          9EF60100
  $F      function    void *(*)()     9EF60100 (may be plaform-specific length 
and format)
  $S      string      char *          hello
  $C      character   char            x
  $W      word        ULongest        00109AE0
  $U      decimal     ULongest        42
  $B      binary      ULongest        00000000000000001011011110010001
  $$      dollar      -               $

Note that WriteFC is an int, because that is the default promotion of a char 
(see .types).

.snazzy: We should resist the temptation to make WriteF an incredible snazzy 
output engine.  We only need it for Describe methods and assertion messages.  
At the moment it's a very simple bit of code -- let's keep it that way.

.f: The F code is used for function pointers.  They are currently printed as a 
hexadecimal string of the appropriate length for the platform, and may one day 
be extended to include function name lookup.

A. References

B. Document History

2002-06-07 RB Converted from MMInfo database design document.