Charles.Forsyth20060303-partialeea662d19 years agoHistory
1174 lines · 29.8 KB · Text
RawDownload
.TLPlan 9 C Compilers \(dg.AU.I "Ken Thompson".AIken@plan9.bell-labs.com.AB.FS\l'1i'.br\(dg Originally appeared, in a different form, in.IProceedings of the Summer 1990 UKUUG Conference,.Rpp. 41-51,London, 1990.This version first appeared in.I "Plan 9 Programmer's Manual, Volume 2 (Second Edition)" .The Plan 9 compiler suite forms the basis for the portable Inferno compiler suite,making this paper still relevant..FEThis paper describes the overall structure and function of the Plan 9 C compilers.A more detailed implementation documentfor any one of the compilersis yet to be written..AE.NHIntroduction.LPThere are many compilers in the series.Eight of the compilers (MIPS 3000, SPARC, Intel 386, AMD64, Power PC, ARM, DEC Alpha, and Motorola 68020)are considered active and are used to compilecurrent versions of Plan 9 or Inferno.Several others (Motorola 68000, Intel 960, AMD 29000) have had only limited use, such asto program peripherals or experimental devices..NHStructure.LPThe compiler is a single program that produces anobject file.Combined in the compiler are the traditionalroles of preprocessor, lexical analyzer, parser, code generator,local optimizer,and first half of the assembler.The object files are binary forms of assemblylanguage,similar to what might be passed betweenthe first and second passes of an assembler..LPObject files and librariesare combined by a loaderprogram to produce the executable binary.The loader combines the roles of second halfof the assembler, global optimizer, and loader.The names of the compliers, loaders, and assemblersare as follows:.DS.ta 1.5i.de Ta\\$1	\f(CW\\$2\fP  \f(CW\\$3\fP  \f(CW\\$4\fP...Ta SPARC kc kl ka.Ta PowerPC qc ql qa.Ta MIPS vc vl va.Ta Motorola\ 68000 1c 1l 1a.Ta Motorola\ 68020 2c 2l 2a.Ta ARM 5c 5l 5a.Ta AMD64 6c 6l 6a.Ta DEC\ Alpha 7c 7l 7a.Ta Intel\ 386 8c 8l 8a.Ta AMD\ 29000 9c 9l 9a.DEThere is a further breakdownin the source of the compilers intoobject-independent andobject-dependentparts.All of the object-independent partsare combined into source files in thedirectory.CW /sys/src/cmd/cc .The object-dependent parts are collectedin a separate directory for each compiler,for example.CW /sys/src/cmd/vc .All of the code,both object-independent andobject-dependent,is machine-independentand may be cross-compiled and executed on anyof the architectures..NHThe Language.LPThe compiler implements ANSI C with somerestrictions and extensions[ANSI90].Most of the restrictions are due topersonal preference, whilemost of the extensions were to help inthe implementation of Plan 9.There are other departures from the standard,particularly in the libraries,that are beyond the scope of thispaper..NH 2Register, volatile, const.LPThe keyword.CW registeris recognized syntacticallybut is semantically ignored.Thus taking the address of a.CW registervariable is not diagnosed.The keyword.CW volatiledisables all optimizations, in particular registerization, of the corresponding variable.The keyword.CW constgenerates warnings (if warnings are enabled by the compiler's.CW -woption) of non-constant use of the variable,but does not affect the generated code..NH 2The preprocessor.LPThe C preprocessor is probably thebiggest departure from the ANSI standard..LPThe preprocessor built into the Plan 9 compilers does not support.CW #if ,although it does handle.CW #ifdefand.CW #include .If it is necessary to be more standard,the source text can first be run through the separate ANSI Cpreprocessor,.CW cpp ..NH 2Unnamed substructures.LPThe most important and most heavily used of theextensions is the declaration of anunnamed substructure or subunion.For example:.DS.CW.ta .1i .6i 1.1i 1.6i	typedef	struct	lock	{		int    locked;	} Lock; 	typedef	struct	node	{		int	type;		union		{			double dval;			float  fval;			long   lval;		};		Lock;	} Node; 	Lock*	lock;	Node*	node;.R.DEThe declaration of.CW Nodehas an unnamed substructure of type.CW Lockand an unnamed subunion.One use of this feature allows references to elements of thesubunit to be accessed as if they were inthe outer structure.Thus.CW node->dvaland.CW node->lockedare legitimate references..LPWhen an outer structure is usedin a context that is only legal foran unnamed substructure,the compiler promotes the reference to theunnamed substructure.This is true for references to structures andto references to pointers to structures.This happens in assignment statements andin argument passing where prototypes have beendeclared.Thus, continuing with the example,.DS.CW.ta .1i .6i 1.1i 1.6i	lock = node;.R.DEwould assign a pointer to the unnamed.CW Lockinthe.CW Nodeto the variable.CW lock .Another example,.DS.CW.ta .1i .6i 1.1i 1.6i	extern void lock(Lock*);	func(...)	{		...		lock(node);		...	}.R.DEwill pass a pointer to the.CW Locksubstructure..LPFinally, in places where context is insufficient to identify the unnamed structure,the type name (it must be a.CW typedef )of the unnamed structure can be used as an identifier.In our example,.CW &node->Lockgives the address of the anonymous.CW Lockstructure..NH 2Structure displays.LPA structure cast followed by a list of expressions in braces isan expression with the type of the structure and elements assigned fromthe corresponding list.Structures are now almost first-class citizens of the language.It is common to see code like this:.DS.CW.ta .1i	r = (Rectangle){point1, (Point){x,y+2}};.R.DE.NH 2Initialization indexes.LPIn initializers of arrays,one may place a constant expressionin square brackets before an initializer.This causes the next initializer to assignthe indicated element.For example:.DS.CW.ta .1i .6i 1.6i	enum	errors	{		Etoobig,		Ealarm,		Egreg	};	char* errstrings[] =	{		[Ealarm]	"Alarm call",		[Egreg]	"Panic: out of mbufs",		[Etoobig]	"Arg list too long",	};.R.DEIn the same way,individual structures members maybe initialized in any order by preceding the initialization with.CW .tagname .Both forms allow an optional.CW = ,to be compatible with a proposedextension to ANSI C..NH 2External register.LPThe declaration.CW extern.CW registerwill dedicate a register toa variable on a global basis.It can be used only under special circumstances.External register variables must be identicallydeclared in all modules andlibraries.The feature is not intended for efficiency,although it can produce efficient code;rather it represents a unique storage class thatwould be hard to get any other way.On a shared-memory multi-processor,an external register isone-per-processor and neither one-per-procedure (automatic)or one-per-system (external).It is used for two variables in the Plan 9 kernel,.CW uand.CW m ..CW Uis a pointer to the structure representing the currently running processand.CW mis a pointer to the per-machine data structure..NH 2Long long.LPThe compilers accept.CW long.CW longas a basic type meaning 64-bit integer.On all of the machinesthis type is synthesized from 32-bit instructions..NH 2Pragma.LPThe compilers accept.CW #pragma.CW lib.I libnameand pass thelibrary name string uninterpretedto the loader.The loader uses the library name tofind libraries to load.If the name contains.CW $O ,it is replaced withthe single character object type of the compiler(e.g.,.CW vfor the MIPS).If the name contains.CW $M ,it is replaced withthe architecture type for the compiler(e.g.,.CW mipsfor the MIPS).If the name starts with.CW /it is an absolute pathname;if it starts with.CW .then it is searched for in the loader's current directory.Otherwise, the name is searched from.CW /$M/lib .Such.CW #pragmastatements in header files guarantee that the correctlibraries are always linked with a program without theneed to specify them explicitly at link time..LPThey also accept.CW #pragma.CW packed.CW on(or.CW yesor.CW 1 )to cause subsequently declared data, until.CW #pragma.CW packed.CW off(or.CW noor.CW 0 ),to be laid out in memory tightly packed in successive bytes, disregardingthe usual alignment rules.Accessing such data can cause faults..LPSimilarly, .CW #pragma.CW profile.CW off(or.CW noor.CW 0 )causes subsequently declared functions, until.CW #pragma.CW profile.CW on(or.CW yesor.CW 1 ),to be marked as unprofiled.Such functions will not be profiled when profiling is enabled for the rest of the program..LPTwo.CW #pragmastatements allow type-checking of.CW print -likefunctions.The first, of the form.P1#pragma varargck argpos error 2.P2tells the compiler that the second argument to.CW erroris a.CW printformat string (see the manual page.I print (2))that specifies how to format.CW error 'ssubsequent arguments.The second, of the form.P1#pragma varargck type "s" char*.P2says that the.CW printformat verb.CW sprocesses an argument oftype.CW char* .If the compiler's.CW -Foption is enabled, the compiler will use this informationto report type violations in the arguments to.CW print ,.CW error ,and similar routines..NHObject module conventions.LPThe overall conventions of the runtime environmentare importantto runtime efficiency.In this section,several of these conventions are discussed..NH 2Register saving.LPIn the Plan 9 compilers,the caller of a procedure saves the registers.With caller-saves,the leaf procedures can use all theregisters and never save them.If you spend a lot of time at the leaves,this seems preferable.With callee-saves,the saving of the registers is donein the single point of entry and return.If you are interested in space,this seems preferable.In both,there is a degree of uncertaintyabout what registers need to be saved.Callee-saved registers make it difficult tofind variables in registers in debuggers.Callee-saved registers also complicatethe implementation of.CW longjmp .The convincing argument isthat with caller-saves,the decision to registerize a variablecan include the cost of saving the registeracross calls.For a further discussion of caller- vs. callee-saves,see the paper by Davidson and Whalley [Dav91]..LPIn the Plan 9 operating system,calls to the kernel look like normal procedurecalls, which meansthe callerhas saved the registers and the systementry does not have to.This makes system calls considerably faster.Since this is a potential security hole,and can lead to non-determinism,the system may eventually save the registerson entry,or more likely clear the registers on return..NH 2Calling convention.LPOlder C compilers maintain a frame pointer, which is at a known constantoffset from the stack pointer within each function.For machines where the stack grows towards zero,the argument pointer is at a known constant offsetfrom the frame pointer.Since the stack grows down in Plan 9,the Plan 9 compilers

Lines 1–500 of 1174.