compiler.ms 30 KB

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