.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 compilersLines 1–500 of 1174. Load more
.TL Plan 9 C Compilers \(dg .AU .I "Ken Thompson" .AI ken@plan9.bell-labs.com .AB .FS \l'1i' .br \(dg Originally appeared, in a different form, in .I Proceedings of the Summer 1990 UKUUG Conference, .R pp. 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. .FE This paper describes the overall structure and function of the Plan 9 C compilers. A more detailed implementation document for any one of the compilers is yet to be written. .AE .NH Introduction .LP There 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 compile current versions of Plan 9 or Inferno. Several others (Motorola 68000, Intel 960, AMD 29000) have had only limited use, such as to program peripherals or experimental devices. .NH Structure .LP The compiler is a single program that produces an object file. Combined in the compiler are the traditional roles of preprocessor, lexical analyzer, parser, code generator, local optimizer, and first half of the assembler. The object files are binary forms of assembly language, similar to what might be passed between the first and second passes of an assembler. .LP Object files and libraries are combined by a loader program to produce the executable binary. The loader combines the roles of second half of the assembler, global optimizer, and loader. The names of the compliers, loaders, and assemblers are 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 .DE There is a further breakdown in the source of the compilers into object-independent and object-dependent parts. All of the object-independent parts are combined into source files in the directory .CW /sys/src/cmd/cc . The object-dependent parts are collected in a separate directory for each compiler, for example .CW /sys/src/cmd/vc . All of the code, both object-independent and object-dependent, is machine-independent and may be cross-compiled and executed on any of the architectures. .NH The Language .LP The compiler implements ANSI C with some restrictions and extensions [ANSI90]. Most of the restrictions are due to personal preference, while most of the extensions were to help in the implementation of Plan 9. There are other departures from the standard, particularly in the libraries, that are beyond the scope of this paper. .NH 2 Register, volatile, const .LP The keyword .CW register is recognized syntactically but is semantically ignored. Thus taking the address of a .CW register variable is not diagnosed. The keyword .CW volatile disables all optimizations, in particular registerization, of the corresponding variable. The keyword .CW const generates warnings (if warnings are enabled by the compiler's .CW -w option) of non-constant use of the variable, but does not affect the generated code. .NH 2 The preprocessor .LP The C preprocessor is probably the biggest departure from the ANSI standard. .LP The preprocessor built into the Plan 9 compilers does not support .CW #if , although it does handle .CW #ifdef and .CW #include . If it is necessary to be more standard, the source text can first be run through the separate ANSI C preprocessor, .CW cpp . .NH 2 Unnamed substructures .LP The most important and most heavily used of the extensions is the declaration of an unnamed 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 .DE The declaration of .CW Node has an unnamed substructure of type .CW Lock and an unnamed subunion. One use of this feature allows references to elements of the subunit to be accessed as if they were in the outer structure. Thus .CW node->dval and .CW node->locked are legitimate references. .LP When an outer structure is used in a context that is only legal for an unnamed substructure, the compiler promotes the reference to the unnamed substructure. This is true for references to structures and to references to pointers to structures. This happens in assignment statements and in argument passing where prototypes have been declared. Thus, continuing with the example, .DS .CW .ta .1i .6i 1.1i 1.6i lock = node; .R .DE would assign a pointer to the unnamed .CW Lock in the .CW Node to the variable .CW lock . Another example, .DS .CW .ta .1i .6i 1.1i 1.6i extern void lock(Lock*); func(...) { ... lock(node); ... } .R .DE will pass a pointer to the .CW Lock substructure. .LP Finally, 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->Lock gives the address of the anonymous .CW Lock structure. .NH 2 Structure displays .LP A structure cast followed by a list of expressions in braces is an expression with the type of the structure and elements assigned from the 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 2 Initialization indexes .LP In initializers of arrays, one may place a constant expression in square brackets before an initializer. This causes the next initializer to assign the 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 .DE In the same way, individual structures members may be initialized in any order by preceding the initialization with .CW .tagname . Both forms allow an optional .CW = , to be compatible with a proposed extension to ANSI C. .NH 2 External register .LP The declaration .CW extern .CW register will dedicate a register to a variable on a global basis. It can be used only under special circumstances. External register variables must be identically declared in all modules and libraries. The feature is not intended for efficiency, although it can produce efficient code; rather it represents a unique storage class that would be hard to get any other way. On a shared-memory multi-processor, an external register is one-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 u and .CW m . .CW U is a pointer to the structure representing the currently running process and .CW m is a pointer to the per-machine data structure. .NH 2 Long long .LP The compilers accept .CW long .CW long as a basic type meaning 64-bit integer. On all of the machines this type is synthesized from 32-bit instructions. .NH 2 Pragma .LP The compilers accept .CW #pragma .CW lib .I libname and pass the library name string uninterpreted to the loader. The loader uses the library name to find libraries to load. If the name contains .CW $O , it is replaced with the single character object type of the compiler (e.g., .CW v for the MIPS). If the name contains .CW $M , it is replaced with the architecture type for the compiler (e.g., .CW mips for 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 #pragma statements in header files guarantee that the correct libraries are always linked with a program without the need to specify them explicitly at link time. .LP They also accept .CW #pragma .CW packed .CW on (or .CW yes or .CW 1 ) to cause subsequently declared data, until .CW #pragma .CW packed .CW off (or .CW no or .CW 0 ), to be laid out in memory tightly packed in successive bytes, disregarding the usual alignment rules. Accessing such data can cause faults. .LP Similarly, .CW #pragma .CW profile .CW off (or .CW no or .CW 0 ) causes subsequently declared functions, until .CW #pragma .CW profile .CW on (or .CW yes or .CW 1 ), to be marked as unprofiled. Such functions will not be profiled when profiling is enabled for the rest of the program. .LP Two .CW #pragma statements allow type-checking of .CW print -like functions. The first, of the form .P1 #pragma varargck argpos error 2 .P2 tells the compiler that the second argument to .CW error is a .CW print format string (see the manual page .I print (2)) that specifies how to format .CW error 's subsequent arguments. The second, of the form .P1 #pragma varargck type "s" char* .P2 says that the .CW print format verb .CW s processes an argument of type .CW char* . If the compiler's .CW -F option is enabled, the compiler will use this information to report type violations in the arguments to .CW print , .CW error , and similar routines. .NH Object module conventions .LP The overall conventions of the runtime environment are important to runtime efficiency. In this section, several of these conventions are discussed. .NH 2 Register saving .LP In the Plan 9 compilers, the caller of a procedure saves the registers. With caller-saves, the leaf procedures can use all the registers 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 done in the single point of entry and return. If you are interested in space, this seems preferable. In both, there is a degree of uncertainty about what registers need to be saved. Callee-saved registers make it difficult to find variables in registers in debuggers. Callee-saved registers also complicate the implementation of .CW longjmp . The convincing argument is that with caller-saves, the decision to registerize a variable can include the cost of saving the register across calls. For a further discussion of caller- vs. callee-saves, see the paper by Davidson and Whalley [Dav91]. .LP In the Plan 9 operating system, calls to the kernel look like normal procedure calls, which means the caller has saved the registers and the system entry 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 registers on entry, or more likely clear the registers on return. .NH 2 Calling convention .LP Older C compilers maintain a frame pointer, which is at a known constant offset from the stack pointer within each function. For machines where the stack grows towards zero, the argument pointer is at a known constant offset from the frame pointer. Since the stack grows down in Plan 9, the Plan 9 compilers