compiler.ms 30 KB

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