[TUHS] Memory management in Dennis Ritchie's C Compiler

Norman Wilson norman at oclsc.org
Tue Aug 18 08:32:29 AEST 2020

Dan Cross:

  I'll confess I haven't looked _that_ closely, but I rather imagine that the
  V10 compiler is a descendant of PCC rather than Dennis's V6/V7 PDP-11


Correct.  From 8/e to the end of official Research UNIX,
cc was pcc2 with a few research-specific hacks.

As Dan says, lcc was there too, but not used a lot.
I'm not sure which version of lcc it was; probably it
was already out-of-date.

In my private half-backed 10/e descendant system, which
runs only on MicroVAXes in my basement, cc is an lcc
descendant instead.  I took the lcc on which the book
was based and re-ported it to the VAX to get an ISO-
compliant C compiler, and made small changes to libc
and /usr/include to afford ISO-C compliance there too.

The hardest but most-interesting part was optimizing.
lcc does a lot of optimization work by itself, and
initially I'd hoped to dispense with a separate c2
pass entirely, but that turns out not to be feasible
on machines like the VAX or the PDP-11: internally
lcc separates something like
	c = *p++;
into two operations
	c = *p;
and makes two distinct calls to the code generator.
To sew them back together from
	cvtbl	(p),c
	incl	p
	cvtbl	(p)+,c
requires external help; lcc just can't see that
what it thinks of as two distinct expressions
can be combined.

It's more than 15 years since I last looked at any
of this stuff, but I vaguely remember that lcc has
its own interesting (but ISO/POSIX-compatible)
memory-allocation setup.  It allows several nested
contexts' worth of allocation, freeing an inner
context when there's no longer any need for it.
For example, once the compiler has finished with
a function and has no further need for its local
symbols, it frees the associated memory.

See the lcc book for details.  Read the book anyway;
it's the one case I know of in which the authors
followed strict Literate Programming rules and made
a big success of it.  Not only is the compiler well-
documented, but the result is a wonderful tour
through the construction and design decisions of a
large program that does real work.

Norman Wilson
Toronto ON

More information about the TUHS mailing list