compiler.ms 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. .TL
  2. Plan 9 C Compilers
  3. .AU
  4. Ken Thompson
  5. ken@plan9.bell-labs.com
  6. .AB
  7. .FS
  8. Originally appeared, in a different form, in
  9. .I
  10. Proceedings of the Summer 1990 UKUUG Conference,
  11. .R
  12. pp. 41-51,
  13. London, 1990.
  14. .FE
  15. This paper describes the overall structure and function of the Plan 9 C compilers.
  16. A more detailed implementation document
  17. for any one of the compilers
  18. is yet to be written.
  19. .AE
  20. .NH
  21. Introduction
  22. .LP
  23. There are many compilers in the series.
  24. Six of the compilers (MIPS 3000, SPARC, Intel 386, Power PC, DEC Alpha, and Motorola 68020)
  25. are considered active and are used to compile
  26. current versions of Plan 9.
  27. Several others (Motorola 68000, Intel 960, ARM 7500, AMD 29000) have had only limited use, such as
  28. to program peripherals or experimental devices.
  29. .NH
  30. Structure
  31. .LP
  32. The compiler is a single program that produces an
  33. object file.
  34. Combined in the compiler are the traditional
  35. roles of preprocessor, lexical analyzer, parser, code generator,
  36. local optimizer,
  37. and first half of the assembler.
  38. The object files are binary forms of assembly
  39. language,
  40. similar to what might be passed between
  41. the first and second passes of an assembler.
  42. .LP
  43. Object files and libraries
  44. are combined by a loader
  45. program to produce the executable binary.
  46. The loader combines the roles of second half
  47. of the assembler, global optimizer, and loader.
  48. The names of the compliers, loaders, and assemblers
  49. are as follows:
  50. .DS
  51. .ta 1.5i
  52. .de Ta
  53. \\$1 \f(CW\\$2\fP \f(CW\\$3\fP \f(CW\\$4\fP
  54. ..
  55. .Ta SPARC kc kl ka
  56. .Ta Power\ PC qc ql qa
  57. .Ta MIPS vc vl va
  58. .Ta Motorola\ 68000 1c 1l 1a
  59. .Ta Motorola\ 68020 2c 2l 2a
  60. .Ta ARM\ 7500 5c 5l 5a
  61. .Ta Intel\ 960 6c 6l 6a
  62. .Ta DEC\ Alpha 7c 7l 7a
  63. .Ta Intel\ 386 8c 8l 8a
  64. .Ta AMD\ 29000 9c 9l 9a
  65. .DE
  66. There is a further breakdown
  67. in the source of the compilers into
  68. object-independent and
  69. object-dependent
  70. parts.
  71. All of the object-independent parts
  72. are combined into source files in the
  73. directory
  74. .CW /sys/src/cmd/cc .
  75. The object-dependent parts are collected
  76. in a separate directory for each compiler,
  77. for example
  78. .CW /sys/src/cmd/vc .
  79. All of the code,
  80. both object-independent and
  81. object-dependent,
  82. is machine-independent
  83. and may be cross-compiled and executed on any
  84. of the architectures.
  85. .NH
  86. The Language
  87. .LP
  88. The compiler implements ANSI C with some
  89. restrictions and extensions
  90. [ANSI90].
  91. Most of the restrictions are due to
  92. personal preference, while
  93. most of the extensions were to help in
  94. the implementation of Plan 9.
  95. There are other departures from the standard,
  96. particularly in the libraries,
  97. that are beyond the scope of this
  98. paper.
  99. .NH 2
  100. Register, volatile, const
  101. .LP
  102. The keyword
  103. .CW register
  104. is recognized syntactically
  105. but is semantically ignored.
  106. Thus taking the address of a
  107. .CW register
  108. variable is not diagnosed.
  109. The keyword
  110. .CW volatile
  111. disables all optimizations, in particular registerization, of the corresponding variable.
  112. The keyword
  113. .CW const
  114. generates warnings (if warnings are enabled by the compiler's
  115. .CW -w
  116. option) of non-constant use of the variable,
  117. but does not affect the generated code.
  118. .NH 2
  119. The preprocessor
  120. .LP
  121. The C preprocessor is probably the
  122. biggest departure from the ANSI standard.
  123. .LP
  124. The preprocessor built into the Plan 9 compilers does not support
  125. .CW #if ,
  126. although it does handle
  127. .CW #ifdef
  128. and
  129. .CW #include .
  130. If it is necessary to be more standard,
  131. the source text can first be run through the separate ANSI C
  132. preprocessor,
  133. .CW cpp .
  134. .NH 2
  135. Unnamed substructures
  136. .LP
  137. The most important and most heavily used of the
  138. extensions is the declaration of an
  139. unnamed substructure or subunion.
  140. For example:
  141. .DS
  142. .CW
  143. .ta .1i .6i 1.1i 1.6i
  144. typedef
  145. struct lock
  146. {
  147. int locked;
  148. } Lock;
  149. typedef
  150. struct node
  151. {
  152. int type;
  153. union
  154. {
  155. double dval;
  156. float fval;
  157. long lval;
  158. };
  159. Lock;
  160. } Node;
  161. Lock* lock;
  162. Node* node;
  163. .DE
  164. The declaration of
  165. .CW Node
  166. has an unnamed substructure of type
  167. .CW Lock
  168. and an unnamed subunion.
  169. One use of this feature allows references to elements of the
  170. subunit to be accessed as if they were in
  171. the outer structure.
  172. Thus
  173. .CW node->dval
  174. and
  175. .CW node->locked
  176. are legitimate references.
  177. .LP
  178. When an outer structure is used
  179. in a context that is only legal for
  180. an unnamed substructure,
  181. the compiler promotes the reference to the
  182. unnamed substructure.
  183. This is true for references to structures and
  184. to references to pointers to structures.
  185. This happens in assignment statements and
  186. in argument passing where prototypes have been
  187. declared.
  188. Thus, continuing with the example,
  189. .DS
  190. .CW
  191. .ta .1i .6i 1.1i 1.6i
  192. lock = node;
  193. .DE
  194. would assign a pointer to the unnamed
  195. .CW Lock
  196. in
  197. the
  198. .CW Node
  199. to the variable
  200. .CW lock .
  201. Another example,
  202. .DS
  203. .CW
  204. .ta .1i .6i 1.1i 1.6i
  205. extern void lock(Lock*);
  206. func(...)
  207. {
  208. ...
  209. lock(node);
  210. ...
  211. }
  212. .DE
  213. will pass a pointer to the
  214. .CW Lock
  215. substructure.
  216. .LP
  217. Finally, in places where context is insufficient to identify the unnamed structure,
  218. the type name (it must be a
  219. .CW typedef )
  220. of the unnamed structure can be used as an identifier.
  221. In our example,
  222. .CW &node->Lock
  223. gives the address of the anonymous
  224. .CW Lock
  225. structure.
  226. .NH 2
  227. Structure displays
  228. .LP
  229. A structure cast followed by a list of expressions in braces is
  230. an expression with the type of the structure and elements assigned from
  231. the corresponding list.
  232. Structures are now almost first-class citizens of the language.
  233. It is common to see code like this:
  234. .DS
  235. .CW
  236. .ta .1i
  237. r = (Rectangle){point1, (Point){x,y+2}};
  238. .DE
  239. .NH 2
  240. Initialization indexes
  241. .LP
  242. In initializers of arrays,
  243. one may place a constant expression
  244. in square brackets before an initializer.
  245. This causes the next initializer to assign
  246. the indicated element.
  247. For example:
  248. .DS
  249. .CW
  250. .ta .1i .6i 1.6i
  251. enum errors
  252. {
  253. Etoobig,
  254. Ealarm,
  255. Egreg
  256. };
  257. char* errstrings[] =
  258. {
  259. [Ealarm] "Alarm call",
  260. [Egreg] "Panic: out of mbufs",
  261. [Etoobig] "Arg list too long",
  262. };
  263. .DE
  264. In the same way,
  265. individual structures members may
  266. be initialized in any order by preceding the initialization with
  267. .CW .tagname .
  268. Both forms allow an optional
  269. .CW = ,
  270. to be compatible with a proposed
  271. extension to ANSI C.
  272. .NH 2
  273. External register
  274. .LP
  275. The declaration
  276. .CW extern
  277. .CW register
  278. will dedicate a register to
  279. a variable on a global basis.
  280. It can be used only under special circumstances.
  281. External register variables must be identically
  282. declared in all modules and
  283. libraries.
  284. The feature is not intended for efficiency,
  285. although it can produce efficient code;
  286. rather it represents a unique storage class that
  287. would be hard to get any other way.
  288. On a shared-memory multi-processor,
  289. an external register is
  290. one-per-processor and neither one-per-procedure (automatic)
  291. or one-per-system (external).
  292. It is used for two variables in the Plan 9 kernel,
  293. .CW u
  294. and
  295. .CW m .
  296. .CW U
  297. is a pointer to the structure representing the currently running process
  298. and
  299. .CW m
  300. is a pointer to the per-machine data structure.
  301. .NH 2
  302. Long long
  303. .LP
  304. The compilers accept
  305. .CW long
  306. .CW long
  307. as a basic type meaning 64-bit integer.
  308. On all of the machines
  309. this type is synthesized from 32-bit instructions.
  310. .NH 2
  311. Pragma
  312. .LP
  313. The compilers accept
  314. .CW #pragma
  315. .CW lib
  316. .I libname
  317. and pass the
  318. library name string uninterpreted
  319. to the loader.
  320. The loader uses the library name to
  321. find libraries to load.
  322. If the name contains
  323. .CW %O ,
  324. it is replaced with
  325. the single character object type of the compiler
  326. (e.g.,
  327. .CW v
  328. for the MIPS).
  329. If the name contains
  330. .CW %M ,
  331. it is replaced with
  332. the architecture type for the compiler
  333. (e.g.,
  334. .CW mips
  335. for the MIPS).
  336. If the name starts with
  337. .CW /
  338. it is an absolute pathname;
  339. if it starts with
  340. .CW .
  341. then it is searched for in the loader's current directory.
  342. Otherwise, the name is searched from
  343. .CW /%M/lib .
  344. Such
  345. .CW #pragma
  346. statements in header files guarantee that the correct
  347. libraries are always linked with a program without the
  348. need to specify them explicitly at link time.
  349. .LP
  350. They also accept
  351. .CW #pragma
  352. .CW hjdicks
  353. .CW on
  354. (or
  355. .CW yes
  356. or
  357. .CW 1 )
  358. to cause subsequently declared data, until
  359. .CW #pragma
  360. .CW hjdicks
  361. .CW off
  362. (or
  363. .CW no
  364. or
  365. .CW 0 ),
  366. to be laid out in memory tightly packed in successive bytes, disregarding
  367. the usual alignment rules.
  368. Accessing such data can cause faults.
  369. .LP
  370. Two
  371. .CW #pragma
  372. statements allow type-checking of
  373. .CW print -like
  374. functions.
  375. The first, of the form
  376. .P1
  377. #pragma varargck argpos error 2
  378. .P2
  379. tells the compiler that the second argument to
  380. .CW error
  381. is a
  382. .CW print
  383. format string (see the manual page
  384. .I print (2))
  385. that specifies how to format
  386. .CW error 's
  387. subsequent arguments.
  388. The second, of the form
  389. .P1
  390. #pragma varargck type "s" char*
  391. .P2
  392. says that the
  393. .CW print
  394. format verb
  395. .CW s
  396. processes an argument of
  397. type
  398. .CW char* .
  399. If the compiler's
  400. .CW -F
  401. option is enabled, the compiler will use this information
  402. to report type violations in the arguments to
  403. .CW print ,
  404. .CW error ,
  405. and similar routines.
  406. .NH
  407. Object module conventions
  408. .LP
  409. The overall conventions of the runtime environment
  410. are important
  411. to runtime efficiency.
  412. In this section,
  413. several of these conventions are discussed.
  414. .NH 2
  415. Register saving
  416. .LP
  417. In the Plan 9 compilers,
  418. the caller of a procedure saves the registers.
  419. With caller-saves,
  420. the leaf procedures can use all the
  421. registers and never save them.
  422. If you spend a lot of time at the leaves,
  423. this seems preferable.
  424. With callee-saves,
  425. the saving of the registers is done
  426. in the single point of entry and return.
  427. If you are interested in space,
  428. this seems preferable.
  429. In both,
  430. there is a degree of uncertainty
  431. about what registers need to be saved.
  432. Callee-saved registers make it difficult to
  433. find variables in registers in debuggers.
  434. Callee-saved registers also complicate
  435. the implementation of
  436. .CW longjmp .
  437. The convincing argument is
  438. that with caller-saves,
  439. the decision to registerize a variable
  440. can include the cost of saving the register
  441. across calls.
  442. For a further discussion of caller- vs. callee-saves,
  443. see the paper by Davidson and Whalley [Dav91].
  444. .LP
  445. In the Plan 9 operating system,
  446. calls to the kernel look like normal procedure
  447. calls, which means
  448. the caller
  449. has saved the registers and the system
  450. entry does not have to.
  451. This makes system calls considerably faster.
  452. Since this is a potential security hole,
  453. and can lead to non-determinism,
  454. the system may eventually save the registers
  455. on entry,
  456. or more likely clear the registers on return.
  457. .NH 2
  458. Calling convention
  459. .LP
  460. Older C compilers maintain a frame pointer, which is at a known constant
  461. offset from the stack pointer within each function.
  462. For machines where the stack grows towards zero,
  463. the argument pointer is at a known constant offset
  464. from the frame pointer.
  465. Since the stack grows down in Plan 9,
  466. the Plan 9 compilers
  467. keep neither an
  468. explicit frame pointer nor
  469. an explicit argument pointer;
  470. instead they generate addresses relative to the stack pointer.
  471. .LP
  472. On some architectures, the first argument to a subroutine is passed in a register.
  473. .NH 2
  474. Functions returning structures
  475. .LP
  476. Structures longer than one word are awkward to implement
  477. since they do not fit in registers and must
  478. be passed around in memory.
  479. Functions that return structures
  480. are particularly clumsy.
  481. The Plan 9 compilers pass the return address of
  482. a structure as the first argument of a
  483. function that has a structure return value.
  484. Thus
  485. .DS
  486. .CW
  487. .ta .1i .6i 1.1i 1.6i
  488. x = f(...)
  489. .DE
  490. is rewritten as
  491. .DS
  492. .CW
  493. .ta .1i .6i 1.1i 1.6i
  494. f(&x, ...)\f1.
  495. .DE
  496. This saves a copy and makes the compilation
  497. much less clumsy.
  498. A disadvantage is that if you call this
  499. function without an assignment,
  500. a dummy location must be invented.
  501. .LP
  502. There is also a danger of calling a function
  503. that returns a structure without declaring
  504. it as such.
  505. With ANSI C function prototypes,
  506. this error need never occur.
  507. .NH
  508. Implementation
  509. .LP
  510. The compiler is divided internally into
  511. four machine-independent passes,
  512. four machine-dependent passes,
  513. and an output pass.
  514. The next nine sections describe each pass in order.
  515. .NH 2
  516. Parsing
  517. .LP
  518. The first pass is a YACC-based parser
  519. [Joh79].
  520. Declarations are interpreted immediately,
  521. building a block structured symbol table.
  522. Executable statements are put into a parse tree
  523. and collected,
  524. without interpretation.
  525. At the end of each procedure,
  526. the parse tree for the function is
  527. examined by the other passes of the compiler.
  528. .LP
  529. The input stream of the parser is
  530. a pushdown list of input activations.
  531. The preprocessor
  532. expansions of
  533. macros
  534. and
  535. .CW #include
  536. are implemented as pushdowns.
  537. Thus there is no separate
  538. pass for preprocessing.
  539. .NH 2
  540. Typing
  541. .LP
  542. The next pass distributes typing information
  543. to every node of the tree.
  544. Implicit operations on the tree are added,
  545. such as type promotions and taking the
  546. address of arrays and functions.
  547. .NH 2
  548. Machine-independent optimization
  549. .LP
  550. The next pass performs optimizations
  551. and transformations of the tree, such as converting
  552. .CW &*x
  553. and
  554. .CW *&x
  555. into
  556. .CW x .
  557. Constant expressions are converted to constants in this pass.
  558. .NH 2
  559. Arithmetic rewrites
  560. .LP
  561. This is another machine-independent optimization.
  562. Subtrees of add, subtract, and multiply of integers are
  563. rewritten for easier compilation.
  564. The major transformation is factoring:
  565. .CW 4+8*a+16*b+5
  566. is transformed into
  567. .CW 9+8*(a+2*b) .
  568. Such expressions arise from address
  569. manipulation and array indexing.
  570. .NH 2
  571. Addressability
  572. .LP
  573. This is the first of the machine-dependent passes.
  574. The addressability of a processor is defined as the set of
  575. expressions that is legal in the address field
  576. of a machine language instruction.
  577. The addressability of different processors varies widely.
  578. At one end of the spectrum are the 68020 and VAX,
  579. which allow a complex mix of incrementing,
  580. decrementing,
  581. indexing, and relative addressing.
  582. At the other end is the MIPS,
  583. which allows only registers and constant offsets from the
  584. contents of a register.
  585. The addressability can be different for different instructions
  586. within the same processor.
  587. .LP
  588. It is important to the code generator to know when a
  589. subtree represents an address of a particular type.
  590. This is done with a bottom-up walk of the tree.
  591. In this pass, the leaves are labeled with small integers.
  592. When an internal node is encountered,
  593. it is labeled by consulting a table indexed by the
  594. labels on the left and right subtrees.
  595. For example,
  596. on the 68020 processor,
  597. it is possible to address an
  598. offset from a named location.
  599. In C, this is represented by the expression
  600. .CW *(&name+constant) .
  601. This is marked addressable by the following table.
  602. In the table,
  603. a node represented by the left column is marked
  604. with a small integer from the right column.
  605. Marks of the form
  606. .CW A\s-2\di\u\s0
  607. are addressable while
  608. marks of the form
  609. .CW N\s-2\di\u\s0
  610. are not addressable.
  611. .DS
  612. .B
  613. .ta .1i 1.1i
  614. Node Marked
  615. .CW
  616. name A\s-2\d1\u\s0
  617. const A\s-2\d2\u\s0
  618. &A\s-2\d1\u\s0 A\s-2\d3\u\s0
  619. A\s-2\d3\u\s0+A\s-2\d1\u\s0 N\s-2\d1\u\s0 \fR(note that this is not addressable)\fP
  620. *N\s-2\d1\u\s0 A\s-2\d4\u\s0
  621. .DE
  622. Here there is a distinction between
  623. a node marked
  624. .CW A\s-2\d1\u\s0
  625. and a node marked
  626. .CW A\s-2\d4\u\s0
  627. because the address operator of an
  628. .CW A\s-2\d4\u\s0
  629. node is not addressable.
  630. So to extend the table:
  631. .DS
  632. .B
  633. .ta .1i 1.1i
  634. Node Marked
  635. .CW
  636. &A\s-2\d4\u\s0 N\s-2\d2\u\s0
  637. N\s-2\d2\u\s0+N\s-2\d1\u\s0 N\s-2\d1\u\s0
  638. .DE
  639. The full addressability of the 68020 is expressed
  640. in 18 rules like this,
  641. while the addressability of the MIPS is expressed
  642. in 11 rules.
  643. When one ports the compiler,
  644. this table is usually initialized
  645. so that leaves are labeled as addressable and nothing else.
  646. The code produced is poor,
  647. but porting is easy.
  648. The table can be extended later.
  649. .LP
  650. This pass also rewrites some complex operators
  651. into procedure calls.
  652. Examples include 64-bit multiply and divide.
  653. .LP
  654. In the same bottom-up pass of the tree,
  655. the nodes are labeled with a Sethi-Ullman complexity
  656. [Set70].
  657. This number is roughly the number of registers required
  658. to compile the tree on an ideal machine.
  659. An addressable node is marked 0.
  660. A function call is marked infinite.
  661. A unary operator is marked as the
  662. maximum of 1 and the mark of its subtree.
  663. A binary operator with equal marks on its subtrees is
  664. marked with a subtree mark plus 1.
  665. A binary operator with unequal marks on its subtrees is
  666. marked with the maximum mark of its subtrees.
  667. The actual values of the marks are not too important,
  668. but the relative values are.
  669. The goal is to compile the harder
  670. (larger mark)
  671. subtree first.
  672. .NH 2
  673. Code generation
  674. .LP
  675. Code is generated by recursive
  676. descent.
  677. The Sethi-Ullman complexity completely guides the
  678. order.
  679. The addressability defines the leaves.
  680. The only difficult part is compiling a tree
  681. that has two infinite (function call)
  682. subtrees.
  683. In this case,
  684. one subtree is compiled into the return register
  685. (usually the most convenient place for a function call)
  686. and then stored on the stack.
  687. The other subtree is compiled into the return register
  688. and then the operation is compiled with
  689. operands from the stack and the return register.
  690. .LP
  691. There is a separate boolean code generator that compiles
  692. conditional expressions.
  693. This is fundamentally different from compiling an arithmetic expression.
  694. The result of the boolean code generator is the
  695. position of the program counter and not an expression.
  696. The boolean code generator makes extensive use of De Morgan's rule.
  697. The boolean code generator is an expanded version of that described
  698. in chapter 8 of Aho, Sethi, and Ullman
  699. [Aho87].
  700. .LP
  701. There is a considerable amount of talk in the literature
  702. about automating this part of a compiler with a machine
  703. description.
  704. Since this code generator is so small
  705. (less than 500 lines of C)
  706. and easy,
  707. it hardly seems worth the effort.
  708. .NH 2
  709. Registerization
  710. .LP
  711. Up to now,
  712. the compiler has operated on syntax trees
  713. that are roughly equivalent to the original source language.
  714. The previous pass has produced machine language in an internal
  715. format.
  716. The next two passes operate on the internal machine language
  717. structures.
  718. The purpose of the next pass is to reintroduce
  719. registers for heavily used variables.
  720. .LP
  721. All of the variables that can be
  722. potentially registerized within a procedure are
  723. placed in a table.
  724. (Suitable variables are any automatic or external
  725. scalars that do not have their addresses extracted.
  726. Some constants that are hard to reference are also
  727. considered for registerization.)
  728. Four separate data flow equations are evaluated
  729. over the procedure on all of these variables.
  730. Two of the equations are the normal set-behind
  731. and used-ahead
  732. bits that define the life of a variable.
  733. The two new bits tell if a variable life
  734. crosses a function call ahead or behind.
  735. By examining a variable over its lifetime,
  736. it is possible to get a cost
  737. for registerizing.
  738. Loops are detected and the costs are multiplied
  739. by three for every level of loop nesting.
  740. Costs are sorted and the variables
  741. are replaced by available registers on a greedy basis.
  742. .LP
  743. The 68020 has two different
  744. types of registers.
  745. For the 68020,
  746. two different costs are calculated for
  747. each variable life and the register type that
  748. affords the better cost is used.
  749. Ties are broken by counting the number of available
  750. registers of each type.
  751. .LP
  752. Note that externals are registerized together with automatics.
  753. This is done by evaluating the semantics of a ``call'' instruction
  754. differently for externals and automatics.
  755. Since a call goes outside the local procedure,
  756. it is assumed that a call references all externals.
  757. Similarly,
  758. externals are assumed to be set before an ``entry'' instruction
  759. and assumed to be referenced after a ``return'' instruction.
  760. This makes sure that externals are in memory across calls.
  761. .LP
  762. The overall results are satisfactory.
  763. It would be nice to be able to do this processing in
  764. a machine-independent way,
  765. but it is impossible to get all of the costs and
  766. side effects of different choices by examining the parse tree.
  767. .LP
  768. Most of the code in the registerization pass is machine-independent.
  769. The major machine-dependency is in
  770. examining a machine instruction to ask if it sets or references
  771. a variable.
  772. .NH 2
  773. Machine code optimization
  774. .LP
  775. The next pass walks the machine code
  776. for opportunistic optimizations.
  777. For the most part,
  778. this is highly specific to a particular
  779. processor.
  780. One optimization that is performed
  781. on all of the processors is the
  782. removal of unnecessary ``move''
  783. instructions.
  784. Ironically,
  785. most of these instructions were inserted by
  786. the previous pass.
  787. There are two patterns that are repetitively
  788. matched and replaced until no more matches are
  789. found.
  790. The first tries to remove ``move'' instructions
  791. by relabeling variables.
  792. .LP
  793. When a ``move'' instruction is encountered,
  794. if the destination variable is set before the
  795. source variable is referenced,
  796. then all of the references to the destination
  797. variable can be renamed to the source and the ``move''
  798. can be deleted.
  799. This transformation uses the reverse data flow
  800. set up in the previous pass.
  801. .LP
  802. An example of this pattern is depicted in the following
  803. table.
  804. The pattern is in the left column and the
  805. replacement action is in the right column.
  806. .DS
  807. .CW
  808. .ta .1i .6i 1.6i 2.1i 2.6i
  809. MOVE a->b \fR(remove)\fP
  810. .R
  811. (sequence with no mention of \f(CWa\fP)
  812. .CW
  813. USE b USE a
  814. .R
  815. (sequence with no mention of \f(CWa\fP)
  816. .CW
  817. SET b SET b
  818. .DE
  819. .LP
  820. Experiments have shown that it is marginally
  821. worthwhile to rename uses of the destination variable
  822. with uses of the source variable up to
  823. the first use of the source variable.
  824. .LP
  825. The second transform will do relabeling
  826. without deleting instructions.
  827. When a ``move'' instruction is encountered,
  828. if the source variable has been set prior
  829. to the use of the destination variable
  830. then all of the references to the source
  831. variable are replaced by the destination and
  832. the ``move'' is inverted.
  833. Typically,
  834. this transformation will alter two ``move''
  835. instructions and allow the first transformation
  836. another chance to remove code.
  837. This transformation uses the forward data flow
  838. set up in the previous pass.
  839. .LP
  840. Again,
  841. the following is a depiction of the transformation where
  842. the pattern is in the left column and the
  843. rewrite is in the right column.
  844. .DS
  845. .CW
  846. .ta .1i .6i 1.6i 2.1i 2.6i
  847. SET a SET b
  848. .R
  849. (sequence with no use of \f(CWb\fP)
  850. .CW
  851. USE a USE b
  852. .R
  853. (sequence with no use of \f(CWb\fP)
  854. .CW
  855. MOVE a->b MOVE b->a
  856. .DE
  857. Iterating these transformations
  858. will usually get rid of all redundant ``move'' instructions.
  859. .LP
  860. A problem with this organization is that the costs
  861. of registerization calculated in the previous pass
  862. must depend on how well this pass can detect and remove
  863. redundant instructions.
  864. Often,
  865. a fine candidate for registerization is rejected
  866. because of the cost of instructions that are later
  867. removed.
  868. .NH 2
  869. Writing the object file
  870. .LP
  871. The last pass walks the internal assembly language
  872. and writes the object file.
  873. The object file is reduced in size by about a factor
  874. of three with simple compression
  875. techniques.
  876. The most important aspect of the object file
  877. format is that it is independent of the compiling machine.
  878. All integer and floating numbers in the object
  879. code are converted to known formats and byte
  880. orders.
  881. .NH
  882. The loader
  883. .LP
  884. The loader is a multiple pass program that
  885. reads object files and libraries and produces
  886. an executable binary.
  887. The loader also does some minimal
  888. optimizations and code rewriting.
  889. Many of the operations performed by the
  890. loader are machine-dependent.
  891. .LP
  892. The first pass of the loader reads the
  893. object modules into an internal data
  894. structure that looks like binary assembly language.
  895. As the instructions are read,
  896. code is reordered to remove
  897. unconditional branch instructions.
  898. Conditional branch instructions are inverted
  899. to prevent the insertion of unconditional branches.
  900. The loader will also make a copy of a few instructions
  901. to remove an unconditional branch.
  902. .LP
  903. The next pass allocates addresses for
  904. all external data.
  905. Typical of processors is the MIPS,
  906. which can reference ±32K bytes from a
  907. register.
  908. The loader allocates the register
  909. .CW R30
  910. as the static pointer.
  911. The value placed in
  912. .CW R30
  913. is the base of the data segment plus 32K.
  914. It is then cheap to reference all data in the
  915. first 64K of the data segment.
  916. External variables are allocated to
  917. the data segment
  918. with the smallest variables allocated first.
  919. If all of the data cannot fit into the first
  920. 64K of the data segment,
  921. then usually only a few large arrays
  922. need more expensive addressing modes.
  923. .LP
  924. For the MIPS processor,
  925. the loader makes a pass over the internal
  926. structures,
  927. exchanging instructions to try
  928. to fill ``delay slots'' with useful work.
  929. If a useful instruction cannot be found
  930. to fill a delay slot,
  931. the loader will insert
  932. ``noop''
  933. instructions.
  934. This pass is very expensive and does not
  935. do a good job.
  936. About 40% of all instructions are in
  937. delay slots.
  938. About 65% of these are useful instructions and
  939. 35% are ``noops.''
  940. The vendor-supplied assembler does this job
  941. more effectively,
  942. filling about 80%
  943. of the delay slots with useful instructions.
  944. .LP
  945. On the 68020 processor,
  946. branch instructions come in a variety of
  947. sizes depending on the relative distance
  948. of the branch.
  949. Thus the size of branch instructions
  950. can be mutually dependent.
  951. The loader uses a multiple pass algorithm
  952. to resolve the branch lengths
  953. [Szy78].
  954. Initially, all branches are assumed minimal length.
  955. On each subsequent pass,
  956. the branches are reassessed
  957. and expanded if necessary.
  958. When no more expansions occur,
  959. the locations of the instructions in
  960. the text segment are known.
  961. .LP
  962. On the MIPS processor,
  963. all instructions are one size.
  964. A single pass over the instructions will
  965. determine the locations of all addresses
  966. in the text segment.
  967. .LP
  968. The last pass of the loader produces the
  969. executable binary.
  970. A symbol table and other tables are
  971. produced to help the debugger to
  972. interpret the binary symbolically.
  973. .LP
  974. The loader places absolute source line numbers in the symbol table.
  975. The name and absolute line number of all
  976. .CW #include
  977. files is also placed in the
  978. symbol table so that the debuggers can
  979. associate object code to source files.
  980. .NH
  981. Performance
  982. .LP
  983. The following is a table of the source size of the MIPS
  984. compiler.
  985. .DS
  986. .ta .1i .6i
  987. lines module
  988. \0509 machine-independent headers
  989. 1070 machine-independent YACC source
  990. 6090 machine-independent C source
  991. \0545 machine-dependent headers
  992. 6532 machine-dependent C source
  993. \0298 loader headers
  994. 5215 loader C source
  995. .DE
  996. .LP
  997. The following table shows timing
  998. of a test program
  999. that plays checkers, running on a MIPS R4000.
  1000. The test program is 26 files totaling 12600 lines of C.
  1001. The execution time does not significantly
  1002. depend on library implementation.
  1003. Since no other compiler runs on Plan 9,
  1004. the Plan 9 tests were done with the Plan 9 operating system;
  1005. the other tests were done on the vendor's operating system.
  1006. The hardware was identical in both cases.
  1007. The optimizer in the vendor's compiler
  1008. is reputed to be extremely good.
  1009. .DS
  1010. .ta .1i .9i
  1011. \0\04.49s Plan 9 \f(CWvc\fP \f(CW-N\fP compile time (opposite of \f(CW-O\fP)
  1012. \0\01.72s Plan 9 \f(CWvc\fP \f(CW-N\fP load time
  1013. 148.69s Plan 9 \f(CWvc\fP \f(CW-N\fP run time
  1014. \015.07s Plan 9 \f(CWvc\fP compile time (\f(CW-O\fP implicit)
  1015. \0\01.66s Plan 9 \f(CWvc\fP load time
  1016. \089.96s Plan 9 \f(CWvc\fP run time
  1017. \014.83s vendor \f(CWcc\fP compile time
  1018. \0\00.38s vendor \f(CWcc\fP load time
  1019. 104.75s vendor \f(CWcc\fP run time
  1020. \043.59s vendor \f(CWcc\fP \f(CW-O\fP compile time
  1021. \0\00.38s vendor \f(CWcc\fP \f(CW-O\fP load time
  1022. \076.19s vendor \f(CWcc\fP \f(CW-O\fP run time
  1023. \0\08.19s vendor \f(CWcc\fP \f(CW-O3\fP compile time
  1024. \035.97s vendor \f(CWcc\fP \f(CW-O3\fP load time
  1025. \071.16s vendor \f(CWcc\fP \f(CW-O3\fP run time
  1026. .DE
  1027. .LP
  1028. To compare the Intel compiler,
  1029. a program that is about 40% bit manipulation and
  1030. about 60% single precision floating point was
  1031. run on the same 33 MHz 486, once under Windows
  1032. compiled with the Watcom compiler, version 10.0,
  1033. in 16-bit mode and once under
  1034. Plan 9 in 32-bit mode.
  1035. The Plan 9 execution time was 27 sec while the Windows
  1036. execution time was 31 sec.
  1037. .NH
  1038. Conclusions
  1039. .LP
  1040. The new compilers compile
  1041. quickly,
  1042. load slowly,
  1043. and produce
  1044. medium quality
  1045. object code.
  1046. The compilers are relatively
  1047. portable,
  1048. requiring but a couple of weeks' work to
  1049. produce a compiler for a different computer.
  1050. For Plan 9,
  1051. where we needed several compilers
  1052. with specialized features and
  1053. our own object formats,
  1054. this project was indispensable.
  1055. It is also necessary for us to
  1056. be able to freely distribute our compilers
  1057. with the Plan 9 distribution.
  1058. .LP
  1059. Two problems have come up in retrospect.
  1060. The first has to do with the
  1061. division of labor between compiler and loader.
  1062. Plan 9 runs on multi-processors and as such
  1063. compilations are often done in parallel.
  1064. Unfortunately,
  1065. all compilations must be complete before loading
  1066. can begin.
  1067. The load is then single-threaded.
  1068. With this model,
  1069. any shift of work from compile to load
  1070. results in a significant increase in real time.
  1071. The same is true of libraries that are compiled
  1072. infrequently and loaded often.
  1073. In the future,
  1074. we may try to put some of the loader work
  1075. back into the compiler.
  1076. .LP
  1077. The second problem comes from
  1078. the various optimizations performed over several
  1079. passes.
  1080. Often optimizations in different passes depend
  1081. on each other.
  1082. Iterating the passes could compromise efficiency,
  1083. or even loop.
  1084. We see no real solution to this problem.
  1085. .NH
  1086. References
  1087. .LP
  1088. [Aho87] A. V. Aho, R. Sethi, and J. D. Ullman,
  1089. .I
  1090. Compilers \- Principles, Techniques, and Tools,
  1091. .R
  1092. Addison Wesley,
  1093. Reading, MA,
  1094. 1987.
  1095. .LP
  1096. [ANSI90] \f2American National Standard for Information Systems \-
  1097. Programming Language C\f1, American National Standards Institute, Inc.,
  1098. New York, 1990.
  1099. .LP
  1100. [Dav91] J. W. Davidson and D. B. Whalley,
  1101. ``Methods for Saving and Restoring Register Values across Function Calls'',
  1102. .I
  1103. Software\-Practice and Experience,
  1104. .R
  1105. Vol 21(2), pp. 149-165, February 1991.
  1106. .LP
  1107. [Joh79] S. C. Johnson,
  1108. ``YACC \- Yet Another Compiler Compiler'',
  1109. .I
  1110. UNIX Programmer's Manual, Seventh Ed., Vol. 2A,
  1111. .R
  1112. AT&T Bell Laboratories,
  1113. Murray Hill, NJ,
  1114. 1979.
  1115. .LP
  1116. [Set70] R. Sethi and J. D. Ullman,
  1117. ``The Generation of Optimal Code for Arithmetic Expressions'',
  1118. .I
  1119. Journal of the ACM,
  1120. .R
  1121. Vol 17(4), pp. 715-728, 1970.
  1122. .LP
  1123. [Szy78] T. G. Szymanski,
  1124. ``Assembling Code for Machines with Span-dependent Instructions'',
  1125. .I
  1126. Communications of the ACM,
  1127. .R
  1128. Vol 21(4), pp. 300-308, 1978.