\documentstyle{article}

\title{Printf Compiler}
\author{Mark W. Eichin}

\begin{document}

printf compiler
---------------

1) scan sources (or objects?) and gather all the constant argument
strings. Assume this is what _doprint ends up with, I guess.

2) convert all the descriptions to C source; compile that with the big
switch - hash - procedure lookup table.

3) link in a _doprint (or other routines?) that could
	a) recognizes the strings on input (based on value or
location)
	b) executes the string argument, if it has been made into a
full procedure

Procedure conversion is tricky at source level (if there are macros)
though you could just filter through cpp first. It is messy at any
rate.

At the .o file level, you can find any references to printf undefined,
and find the calling sequences; if the compiler is simple enough (like
pcc, gcc may need some more trickery here) just blast in new r0 or r1
values. (somewhat crude)

If the strings are hashed (easy to do because you know *all* of them,
so you get to pick the function to be "correct") then just use a
switch statement to do the looking up. This requires no mods to the
other object files (good.) Also, switching on the actual format string
address would do the uniqueness part, maybe too well (so, we just have
multiple branches to the case.)

printf(), fprintf(), sprintf(), _doprnt(format, args, stream)
(_doprnt is a real varargs procedure...)

integer: octal, hex, dec; long, other, unsigned
char:
string:
+/- field frobbing
floating: d, e, f, g...

[printf uses putc now...]



From raeburn@ATHENA.MIT.EDU Sat Jan 16 04:44:00 1988
Date: Mon, 11 Jan 88 08:33:15 EST
From: Ken Raeburn <raeburn@ATHENA.MIT.EDU>
To: Mark W. Eichin <eichin@ATHENA.MIT.EDU>
Subject: puts & xprintf


For your xprintf(xstr+printf?) idea:

    BUGS
	 Puts appends a newline, fputs does not, all in the name of
	 backward compatibility.

Use fputs.

Also, consider putc or fputc for single character output, maybe for
double-character too.

From raeburn@ATHENA.MIT.EDU Mon Jul 18 02:51:18 1988
Date: Sun, 17 Jul 88 13:49:04 EDT
From: Ken Raeburn <raeburn@ATHENA.MIT.EDU>
To: eichin@ATHENA.MIT.EDU
Subject: [Ken Raeburn: Re: Compiler checking of printf()]


Don't know if you read comp.lang.c, but Torek seems to be one of Those
(Few) Who Know What They're Talking About... in general, and wrt PCC.

------- Forwarded Message

From: <raeburn@ATHENA.MIT.EDU>
Date: Sun, 17 Jul 88 12:07:05 EDT
To: raeburn@ATHENA.MIT.EDU

Path: bloom-beacon!husc6!uwvax!oddjob!mimsy!chris
From: chris@mimsy.UUCP (Chris Torek)
Newsgroups: comp.lang.c
Subject: Re: Compiler checking of printf()
Keywords: ANSI standard, printf()
Message-ID: <12491@mimsy.UUCP>
Date: 15 Jul 88 03:10:37 GMT
References: <524@prlhp1.prl.philips.co.uk>
Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
Lines: 38

In article <524@prlhp1.prl.philips.co.uk> yuleat@prlhp1.prl.philips.co.uk
(yuleat) writes:
>After a bit of testing, I found that not only would [the new compiler]
>issue a warning if the wrong number of arguments were passed to printf(),
>but it also checked that the types of the arguments matched the
>specifiers in the format string.

>Is this part of the standard?

Yes; or rather, the dpANS *permits* this action.  It does not *require*
it.  A good compiler will do its best to check.

>This behaviour implies to me, that the printf() command is broken
>down by the compiler to produce (at least partly) in-line code,
>is this likely/reasonable?

Possibly so.  In the worst case a full-blown printf() interpreter
is still necessary, so as to handle code such as

	char *fmt = make_up_some_format();
	...
	(void) printf(fmt, arg1, arg2, arg3);

In the best case, converting statements like

	(void) printf("\tfoo\t%s,%s\n", a, b);

to ones like

	(void) (fputs("\tfoo\t", stdout),
	 fputs(a, stdout), putchar(','), fputs(b, stdout),
	 putchar('\n'));

can gain quite a bit of speed in the resulting executable.  (Doing
it by hand made a nice difference in the latest 4BSD PCC C compiler.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

------- End Forwarded Message

 sed -n -e '/printf/s/^.*([^"]*"\([^,)]*\)[,)].*$/\1/p' *.c > /tmp/pstrs

577 lines.

sed -n -e 's/%/\\^J%/gp' pstrs | sort -u > pstrs.1
sed -n -e 's/^\(%[^DEGdoxfegcsu]*[DEGdoxfegcsu]\).*$/\1/p' pstrs.1 > pstrs.2
sort -u pstrs.2 > pstrs.3       
 wc -l pstrs.3 
      44 pstrs.3
