compiler.ms 30 KB

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