compiler.ms 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. Similarly,
  371. .CW #pragma
  372. .CW profile
  373. .CW off
  374. (or
  375. .CW no
  376. or
  377. .CW 0 )
  378. causes subsequently declared functions, until
  379. .CW #pragma
  380. .CW profile
  381. .CW on
  382. (or
  383. .CW yes
  384. or
  385. .CW 1 ),
  386. to be marked as unprofiled.
  387. Such functions will not be profiled when
  388. profiling is enabled for the rest of the program.
  389. .LP
  390. Two
  391. .CW #pragma
  392. statements allow type-checking of
  393. .CW print -like
  394. functions.
  395. The first, of the form
  396. .P1
  397. #pragma varargck argpos error 2
  398. .P2
  399. tells the compiler that the second argument to
  400. .CW error
  401. is a
  402. .CW print
  403. format string (see the manual page
  404. .I print (2))
  405. that specifies how to format
  406. .CW error 's
  407. subsequent arguments.
  408. The second, of the form
  409. .P1
  410. #pragma varargck type "s" char*
  411. .P2
  412. says that the
  413. .CW print
  414. format verb
  415. .CW s
  416. processes an argument of
  417. type
  418. .CW char* .
  419. If the compiler's
  420. .CW -F
  421. option is enabled, the compiler will use this information
  422. to report type violations in the arguments to
  423. .CW print ,
  424. .CW error ,
  425. and similar routines.
  426. .NH
  427. Object module conventions
  428. .LP
  429. The overall conventions of the runtime environment
  430. are important
  431. to runtime efficiency.
  432. In this section,
  433. several of these conventions are discussed.
  434. .NH 2
  435. Register saving
  436. .LP
  437. In the Plan 9 compilers,
  438. the caller of a procedure saves the registers.
  439. With caller-saves,
  440. the leaf procedures can use all the
  441. registers and never save them.
  442. If you spend a lot of time at the leaves,
  443. this seems preferable.
  444. With callee-saves,
  445. the saving of the registers is done
  446. in the single point of entry and return.
  447. If you are interested in space,
  448. this seems preferable.
  449. In both,
  450. there is a degree of uncertainty
  451. about what registers need to be saved.
  452. Callee-saved registers make it difficult to
  453. find variables in registers in debuggers.
  454. Callee-saved registers also complicate
  455. the implementation of
  456. .CW longjmp .
  457. The convincing argument is
  458. that with caller-saves,
  459. the decision to registerize a variable
  460. can include the cost of saving the register
  461. across calls.
  462. For a further discussion of caller- vs. callee-saves,
  463. see the paper by Davidson and Whalley [Dav91].
  464. .LP
  465. In the Plan 9 operating system,
  466. calls to the kernel look like normal procedure
  467. calls, which means
  468. the caller
  469. has saved the registers and the system
  470. entry does not have to.
  471. This makes system calls considerably faster.
  472. Since this is a potential security hole,
  473. and can lead to non-determinism,
  474. the system may eventually save the registers
  475. on entry,
  476. or more likely clear the registers on return.
  477. .NH 2
  478. Calling convention
  479. .LP
  480. Older C compilers maintain a frame pointer, which is at a known constant
  481. offset from the stack pointer within each function.
  482. For machines where the stack grows towards zero,
  483. the argument pointer is at a known constant offset
  484. from the frame pointer.
  485. Since the stack grows down in Plan 9,
  486. the Plan 9 compilers
  487. keep neither an
  488. explicit frame pointer nor
  489. an explicit argument pointer;
  490. instead they generate addresses relative to the stack pointer.
  491. .LP
  492. On some architectures, the first argument to a subroutine is passed in a register.
  493. .NH 2
  494. Functions returning structures
  495. .LP
  496. Structures longer than one word are awkward to implement
  497. since they do not fit in registers and must
  498. be passed around in memory.
  499. Functions that return structures
  500. are particularly clumsy.
  501. The Plan 9 compilers pass the return address of
  502. a structure as the first argument of a
  503. function that has a structure return value.
  504. Thus
  505. .DS
  506. .CW
  507. .ta .1i .6i 1.1i 1.6i
  508. x = f(...)
  509. .DE
  510. is rewritten as
  511. .DS
  512. .CW
  513. .ta .1i .6i 1.1i 1.6i
  514. f(&x, ...)\f1.
  515. .DE
  516. This saves a copy and makes the compilation
  517. much less clumsy.
  518. A disadvantage is that if you call this
  519. function without an assignment,
  520. a dummy location must be invented.
  521. .LP
  522. There is also a danger of calling a function
  523. that returns a structure without declaring
  524. it as such.
  525. With ANSI C function prototypes,
  526. this error need never occur.
  527. .NH
  528. Implementation
  529. .LP
  530. The compiler is divided internally into
  531. four machine-independent passes,
  532. four machine-dependent passes,
  533. and an output pass.
  534. The next nine sections describe each pass in order.
  535. .NH 2
  536. Parsing
  537. .LP
  538. The first pass is a YACC-based parser
  539. [Joh79].
  540. Declarations are interpreted immediately,
  541. building a block structured symbol table.
  542. Executable statements are put into a parse tree
  543. and collected,
  544. without interpretation.
  545. At the end of each procedure,
  546. the parse tree for the function is
  547. examined by the other passes of the compiler.
  548. .LP
  549. The input stream of the parser is
  550. a pushdown list of input activations.
  551. The preprocessor
  552. expansions of
  553. macros
  554. and
  555. .CW #include
  556. are implemented as pushdowns.
  557. Thus there is no separate
  558. pass for preprocessing.
  559. .NH 2
  560. Typing
  561. .LP
  562. The next pass distributes typing information
  563. to every node of the tree.
  564. Implicit operations on the tree are added,
  565. such as type promotions and taking the
  566. address of arrays and functions.
  567. .NH 2
  568. Machine-independent optimization
  569. .LP
  570. The next pass performs optimizations
  571. and transformations of the tree, such as converting
  572. .CW &*x
  573. and
  574. .CW *&x
  575. into
  576. .CW x .
  577. Constant expressions are converted to constants in this pass.
  578. .NH 2
  579. Arithmetic rewrites
  580. .LP
  581. This is another machine-independent optimization.
  582. Subtrees of add, subtract, and multiply of integers are
  583. rewritten for easier compilation.
  584. The major transformation is factoring:
  585. .CW 4+8*a+16*b+5
  586. is transformed into
  587. .CW 9+8*(a+2*b) .
  588. Such expressions arise from address
  589. manipulation and array indexing.
  590. .NH 2
  591. Addressability
  592. .LP
  593. This is the first of the machine-dependent passes.
  594. The addressability of a processor is defined as the set of
  595. expressions that is legal in the address field
  596. of a machine language instruction.
  597. The addressability of different processors varies widely.
  598. At one end of the spectrum are the 68020 and VAX,
  599. which allow a complex mix of incrementing,
  600. decrementing,
  601. indexing, and relative addressing.
  602. At the other end is the MIPS,
  603. which allows only registers and constant offsets from the
  604. contents of a register.
  605. The addressability can be different for different instructions
  606. within the same processor.
  607. .LP
  608. It is important to the code generator to know when a
  609. subtree represents an address of a particular type.
  610. This is done with a bottom-up walk of the tree.
  611. In this pass, the leaves are labeled with small integers.
  612. When an internal node is encountered,
  613. it is labeled by consulting a table indexed by the
  614. labels on the left and right subtrees.
  615. For example,
  616. on the 68020 processor,
  617. it is possible to address an
  618. offset from a named location.
  619. In C, this is represented by the expression
  620. .CW *(&name+constant) .
  621. This is marked addressable by the following table.
  622. In the table,
  623. a node represented by the left column is marked
  624. with a small integer from the right column.
  625. Marks of the form
  626. .CW A\s-2\di\u\s0
  627. are addressable while
  628. marks of the form
  629. .CW N\s-2\di\u\s0
  630. are not addressable.
  631. .DS
  632. .B
  633. .ta .1i 1.1i
  634. Node Marked
  635. .CW
  636. name A\s-2\d1\u\s0
  637. const A\s-2\d2\u\s0
  638. &A\s-2\d1\u\s0 A\s-2\d3\u\s0
  639. 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
  640. *N\s-2\d1\u\s0 A\s-2\d4\u\s0
  641. .DE
  642. Here there is a distinction between
  643. a node marked
  644. .CW A\s-2\d1\u\s0
  645. and a node marked
  646. .CW A\s-2\d4\u\s0
  647. because the address operator of an
  648. .CW A\s-2\d4\u\s0
  649. node is not addressable.
  650. So to extend the table:
  651. .DS
  652. .B
  653. .ta .1i 1.1i
  654. Node Marked
  655. .CW
  656. &A\s-2\d4\u\s0 N\s-2\d2\u\s0
  657. N\s-2\d2\u\s0+N\s-2\d1\u\s0 N\s-2\d1\u\s0
  658. .DE
  659. The full addressability of the 68020 is expressed
  660. in 18 rules like this,
  661. while the addressability of the MIPS is expressed
  662. in 11 rules.
  663. When one ports the compiler,
  664. this table is usually initialized
  665. so that leaves are labeled as addressable and nothing else.
  666. The code produced is poor,
  667. but porting is easy.
  668. The table can be extended later.
  669. .LP
  670. This pass also rewrites some complex operators
  671. into procedure calls.
  672. Examples include 64-bit multiply and divide.
  673. .LP
  674. In the same bottom-up pass of the tree,
  675. the nodes are labeled with a Sethi-Ullman complexity
  676. [Set70].
  677. This number is roughly the number of registers required
  678. to compile the tree on an ideal machine.
  679. An addressable node is marked 0.
  680. A function call is marked infinite.
  681. A unary operator is marked as the
  682. maximum of 1 and the mark of its subtree.
  683. A binary operator with equal marks on its subtrees is
  684. marked with a subtree mark plus 1.
  685. A binary operator with unequal marks on its subtrees is
  686. marked with the maximum mark of its subtrees.
  687. The actual values of the marks are not too important,
  688. but the relative values are.
  689. The goal is to compile the harder
  690. (larger mark)
  691. subtree first.
  692. .NH 2
  693. Code generation
  694. .LP
  695. Code is generated by recursive
  696. descent.
  697. The Sethi-Ullman complexity completely guides the
  698. order.
  699. The addressability defines the leaves.
  700. The only difficult part is compiling a tree
  701. that has two infinite (function call)
  702. subtrees.
  703. In this case,
  704. one subtree is compiled into the return register
  705. (usually the most convenient place for a function call)
  706. and then stored on the stack.
  707. The other subtree is compiled into the return register
  708. and then the operation is compiled with
  709. operands from the stack and the return register.
  710. .LP
  711. There is a separate boolean code generator that compiles
  712. conditional expressions.
  713. This is fundamentally different from compiling an arithmetic expression.
  714. The result of the boolean code generator is the
  715. position of the program counter and not an expression.
  716. The boolean code generator makes extensive use of De Morgan's rule.
  717. The boolean code generator is an expanded version of that described
  718. in chapter 8 of Aho, Sethi, and Ullman
  719. [Aho87].
  720. .LP
  721. There is a considerable amount of talk in the literature
  722. about automating this part of a compiler with a machine
  723. description.
  724. Since this code generator is so small
  725. (less than 500 lines of C)
  726. and easy,
  727. it hardly seems worth the effort.
  728. .NH 2
  729. Registerization
  730. .LP
  731. Up to now,
  732. the compiler has operated on syntax trees
  733. that are roughly equivalent to the original source language.
  734. The previous pass has produced machine language in an internal
  735. format.
  736. The next two passes operate on the internal machine language
  737. structures.
  738. The purpose of the next pass is to reintroduce
  739. registers for heavily used variables.
  740. .LP
  741. All of the variables that can be
  742. potentially registerized within a procedure are
  743. placed in a table.
  744. (Suitable variables are any automatic or external
  745. scalars that do not have their addresses extracted.
  746. Some constants that are hard to reference are also
  747. considered for registerization.)
  748. Four separate data flow equations are evaluated
  749. over the procedure on all of these variables.
  750. Two of the equations are the normal set-behind
  751. and used-ahead
  752. bits that define the life of a variable.
  753. The two new bits tell if a variable life
  754. crosses a function call ahead or behind.
  755. By examining a variable over its lifetime,
  756. it is possible to get a cost
  757. for registerizing.
  758. Loops are detected and the costs are multiplied
  759. by three for every level of loop nesting.
  760. Costs are sorted and the variables
  761. are replaced by available registers on a greedy basis.
  762. .LP
  763. The 68020 has two different
  764. types of registers.
  765. For the 68020,
  766. two different costs are calculated for
  767. each variable life and the register type that
  768. affords the better cost is used.
  769. Ties are broken by counting the number of available
  770. registers of each type.
  771. .LP
  772. Note that externals are registerized together with automatics.
  773. This is done by evaluating the semantics of a ``call'' instruction
  774. differently for externals and automatics.
  775. Since a call goes outside the local procedure,
  776. it is assumed that a call references all externals.
  777. Similarly,
  778. externals are assumed to be set before an ``entry'' instruction
  779. and assumed to be referenced after a ``return'' instruction.
  780. This makes sure that externals are in memory across calls.
  781. .LP
  782. The overall results are satisfactory.
  783. It would be nice to be able to do this processing in
  784. a machine-independent way,
  785. but it is impossible to get all of the costs and
  786. side effects of different choices by examining the parse tree.
  787. .LP
  788. Most of the code in the registerization pass is machine-independent.
  789. The major machine-dependency is in
  790. examining a machine instruction to ask if it sets or references
  791. a variable.
  792. .NH 2
  793. Machine code optimization
  794. .LP
  795. The next pass walks the machine code
  796. for opportunistic optimizations.
  797. For the most part,
  798. this is highly specific to a particular
  799. processor.
  800. One optimization that is performed
  801. on all of the processors is the
  802. removal of unnecessary ``move''
  803. instructions.
  804. Ironically,
  805. most of these instructions were inserted by
  806. the previous pass.
  807. There are two patterns that are repetitively
  808. matched and replaced until no more matches are
  809. found.
  810. The first tries to remove ``move'' instructions
  811. by relabeling variables.
  812. .LP
  813. When a ``move'' instruction is encountered,
  814. if the destination variable is set before the
  815. source variable is referenced,
  816. then all of the references to the destination
  817. variable can be renamed to the source and the ``move''
  818. can be deleted.
  819. This transformation uses the reverse data flow
  820. set up in the previous pass.
  821. .LP
  822. An example of this pattern is depicted in the following
  823. table.
  824. The pattern is in the left column and the
  825. replacement action is in the right column.
  826. .DS
  827. .CW
  828. .ta .1i .6i 1.6i 2.1i 2.6i
  829. MOVE a->b \fR(remove)\fP
  830. .R
  831. (sequence with no mention of \f(CWa\fP)
  832. .CW
  833. USE b USE a
  834. .R
  835. (sequence with no mention of \f(CWa\fP)
  836. .CW
  837. SET b SET b
  838. .DE
  839. .LP
  840. Experiments have shown that it is marginally
  841. worthwhile to rename uses of the destination variable
  842. with uses of the source variable up to
  843. the first use of the source variable.
  844. .LP
  845. The second transform will do relabeling
  846. without deleting instructions.
  847. When a ``move'' instruction is encountered,
  848. if the source variable has been set prior
  849. to the use of the destination variable
  850. then all of the references to the source
  851. variable are replaced by the destination and
  852. the ``move'' is inverted.
  853. Typically,
  854. this transformation will alter two ``move''
  855. instructions and allow the first transformation
  856. another chance to remove code.
  857. This transformation uses the forward data flow
  858. set up in the previous pass.
  859. .LP
  860. Again,
  861. the following is a depiction of the transformation where
  862. the pattern is in the left column and the
  863. rewrite is in the right column.
  864. .DS
  865. .CW
  866. .ta .1i .6i 1.6i 2.1i 2.6i
  867. SET a SET b
  868. .R
  869. (sequence with no use of \f(CWb\fP)
  870. .CW
  871. USE a USE b
  872. .R
  873. (sequence with no use of \f(CWb\fP)
  874. .CW
  875. MOVE a->b MOVE b->a
  876. .DE
  877. Iterating these transformations
  878. will usually get rid of all redundant ``move'' instructions.
  879. .LP
  880. A problem with this organization is that the costs
  881. of registerization calculated in the previous pass
  882. must depend on how well this pass can detect and remove
  883. redundant instructions.
  884. Often,
  885. a fine candidate for registerization is rejected
  886. because of the cost of instructions that are later
  887. removed.
  888. .NH 2
  889. Writing the object file
  890. .LP
  891. The last pass walks the internal assembly language
  892. and writes the object file.
  893. The object file is reduced in size by about a factor
  894. of three with simple compression
  895. techniques.
  896. The most important aspect of the object file
  897. format is that it is independent of the compiling machine.
  898. All integer and floating numbers in the object
  899. code are converted to known formats and byte
  900. orders.
  901. .NH
  902. The loader
  903. .LP
  904. The loader is a multiple pass program that
  905. reads object files and libraries and produces
  906. an executable binary.
  907. The loader also does some minimal
  908. optimizations and code rewriting.
  909. Many of the operations performed by the
  910. loader are machine-dependent.
  911. .LP
  912. The first pass of the loader reads the
  913. object modules into an internal data
  914. structure that looks like binary assembly language.
  915. As the instructions are read,
  916. code is reordered to remove
  917. unconditional branch instructions.
  918. Conditional branch instructions are inverted
  919. to prevent the insertion of unconditional branches.
  920. The loader will also make a copy of a few instructions
  921. to remove an unconditional branch.
  922. .LP
  923. The next pass allocates addresses for
  924. all external data.
  925. Typical of processors is the MIPS,
  926. which can reference ±32K bytes from a
  927. register.
  928. The loader allocates the register
  929. .CW R30
  930. as the static pointer.
  931. The value placed in
  932. .CW R30
  933. is the base of the data segment plus 32K.
  934. It is then cheap to reference all data in the
  935. first 64K of the data segment.
  936. External variables are allocated to
  937. the data segment
  938. with the smallest variables allocated first.
  939. If all of the data cannot fit into the first
  940. 64K of the data segment,
  941. then usually only a few large arrays
  942. need more expensive addressing modes.
  943. .LP
  944. For the MIPS processor,
  945. the loader makes a pass over the internal
  946. structures,
  947. exchanging instructions to try
  948. to fill ``delay slots'' with useful work.
  949. If a useful instruction cannot be found
  950. to fill a delay slot,
  951. the loader will insert
  952. ``noop''
  953. instructions.
  954. This pass is very expensive and does not
  955. do a good job.
  956. About 40% of all instructions are in
  957. delay slots.
  958. About 65% of these are useful instructions and
  959. 35% are ``noops.''
  960. The vendor-supplied assembler does this job
  961. more effectively,
  962. filling about 80%
  963. of the delay slots with useful instructions.
  964. .LP
  965. On the 68020 processor,
  966. branch instructions come in a variety of
  967. sizes depending on the relative distance
  968. of the branch.
  969. Thus the size of branch instructions
  970. can be mutually dependent.
  971. The loader uses a multiple pass algorithm
  972. to resolve the branch lengths
  973. [Szy78].
  974. Initially, all branches are assumed minimal length.
  975. On each subsequent pass,
  976. the branches are reassessed
  977. and expanded if necessary.
  978. When no more expansions occur,
  979. the locations of the instructions in
  980. the text segment are known.
  981. .LP
  982. On the MIPS processor,
  983. all instructions are one size.
  984. A single pass over the instructions will
  985. determine the locations of all addresses
  986. in the text segment.
  987. .LP
  988. The last pass of the loader produces the
  989. executable binary.
  990. A symbol table and other tables are
  991. produced to help the debugger to
  992. interpret the binary symbolically.
  993. .LP
  994. The loader places absolute source line numbers in the symbol table.
  995. The name and absolute line number of all
  996. .CW #include
  997. files is also placed in the
  998. symbol table so that the debuggers can
  999. associate object code to source files.
  1000. .NH
  1001. Performance
  1002. .LP
  1003. The following is a table of the source size of the MIPS
  1004. compiler.
  1005. .DS
  1006. .ta .1i .6i
  1007. lines module
  1008. \0509 machine-independent headers
  1009. 1070 machine-independent YACC source
  1010. 6090 machine-independent C source
  1011. \0545 machine-dependent headers
  1012. 6532 machine-dependent C source
  1013. \0298 loader headers
  1014. 5215 loader C source
  1015. .DE
  1016. .LP
  1017. The following table shows timing
  1018. of a test program
  1019. that plays checkers, running on a MIPS R4000.
  1020. The test program is 26 files totaling 12600 lines of C.
  1021. The execution time does not significantly
  1022. depend on library implementation.
  1023. Since no other compiler runs on Plan 9,
  1024. the Plan 9 tests were done with the Plan 9 operating system;
  1025. the other tests were done on the vendor's operating system.
  1026. The hardware was identical in both cases.
  1027. The optimizer in the vendor's compiler
  1028. is reputed to be extremely good.
  1029. .DS
  1030. .ta .1i .9i
  1031. \0\04.49s Plan 9 \f(CWvc\fP \f(CW-N\fP compile time (opposite of \f(CW-O\fP)
  1032. \0\01.72s Plan 9 \f(CWvc\fP \f(CW-N\fP load time
  1033. 148.69s Plan 9 \f(CWvc\fP \f(CW-N\fP run time
  1034. \015.07s Plan 9 \f(CWvc\fP compile time (\f(CW-O\fP implicit)
  1035. \0\01.66s Plan 9 \f(CWvc\fP load time
  1036. \089.96s Plan 9 \f(CWvc\fP run time
  1037. \014.83s vendor \f(CWcc\fP compile time
  1038. \0\00.38s vendor \f(CWcc\fP load time
  1039. 104.75s vendor \f(CWcc\fP run time
  1040. \043.59s vendor \f(CWcc\fP \f(CW-O\fP compile time
  1041. \0\00.38s vendor \f(CWcc\fP \f(CW-O\fP load time
  1042. \076.19s vendor \f(CWcc\fP \f(CW-O\fP run time
  1043. \0\08.19s vendor \f(CWcc\fP \f(CW-O3\fP compile time
  1044. \035.97s vendor \f(CWcc\fP \f(CW-O3\fP load time
  1045. \071.16s vendor \f(CWcc\fP \f(CW-O3\fP run time
  1046. .DE
  1047. .LP
  1048. To compare the Intel compiler,
  1049. a program that is about 40% bit manipulation and
  1050. about 60% single precision floating point was
  1051. run on the same 33 MHz 486, once under Windows
  1052. compiled with the Watcom compiler, version 10.0,
  1053. in 16-bit mode and once under
  1054. Plan 9 in 32-bit mode.
  1055. The Plan 9 execution time was 27 sec while the Windows
  1056. execution time was 31 sec.
  1057. .NH
  1058. Conclusions
  1059. .LP
  1060. The new compilers compile
  1061. quickly,
  1062. load slowly,
  1063. and produce
  1064. medium quality
  1065. object code.
  1066. The compilers are relatively
  1067. portable,
  1068. requiring but a couple of weeks' work to
  1069. produce a compiler for a different computer.
  1070. For Plan 9,
  1071. where we needed several compilers
  1072. with specialized features and
  1073. our own object formats,
  1074. this project was indispensable.
  1075. It is also necessary for us to
  1076. be able to freely distribute our compilers
  1077. with the Plan 9 distribution.
  1078. .LP
  1079. Two problems have come up in retrospect.
  1080. The first has to do with the
  1081. division of labor between compiler and loader.
  1082. Plan 9 runs on multi-processors and as such
  1083. compilations are often done in parallel.
  1084. Unfortunately,
  1085. all compilations must be complete before loading
  1086. can begin.
  1087. The load is then single-threaded.
  1088. With this model,
  1089. any shift of work from compile to load
  1090. results in a significant increase in real time.
  1091. The same is true of libraries that are compiled
  1092. infrequently and loaded often.
  1093. In the future,
  1094. we may try to put some of the loader work
  1095. back into the compiler.
  1096. .LP
  1097. The second problem comes from
  1098. the various optimizations performed over several
  1099. passes.
  1100. Often optimizations in different passes depend
  1101. on each other.
  1102. Iterating the passes could compromise efficiency,
  1103. or even loop.
  1104. We see no real solution to this problem.
  1105. .NH
  1106. References
  1107. .LP
  1108. [Aho87] A. V. Aho, R. Sethi, and J. D. Ullman,
  1109. .I
  1110. Compilers \- Principles, Techniques, and Tools,
  1111. .R
  1112. Addison Wesley,
  1113. Reading, MA,
  1114. 1987.
  1115. .LP
  1116. [ANSI90] \f2American National Standard for Information Systems \-
  1117. Programming Language C\f1, American National Standards Institute, Inc.,
  1118. New York, 1990.
  1119. .LP
  1120. [Dav91] J. W. Davidson and D. B. Whalley,
  1121. ``Methods for Saving and Restoring Register Values across Function Calls'',
  1122. .I
  1123. Software\-Practice and Experience,
  1124. .R
  1125. Vol 21(2), pp. 149-165, February 1991.
  1126. .LP
  1127. [Joh79] S. C. Johnson,
  1128. ``YACC \- Yet Another Compiler Compiler'',
  1129. .I
  1130. UNIX Programmer's Manual, Seventh Ed., Vol. 2A,
  1131. .R
  1132. AT&T Bell Laboratories,
  1133. Murray Hill, NJ,
  1134. 1979.
  1135. .LP
  1136. [Set70] R. Sethi and J. D. Ullman,
  1137. ``The Generation of Optimal Code for Arithmetic Expressions'',
  1138. .I
  1139. Journal of the ACM,
  1140. .R
  1141. Vol 17(4), pp. 715-728, 1970.
  1142. .LP
  1143. [Szy78] T. G. Szymanski,
  1144. ``Assembling Code for Machines with Span-dependent Instructions'',
  1145. .I
  1146. Communications of the ACM,
  1147. .R
  1148. Vol 21(4), pp. 300-308, 1978.