comp.ms 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. .HTML "How to Use the Plan 9 C Compiler
  2. .TL
  3. How to Use the Plan 9 C Compiler
  4. .AU
  5. Rob Pike
  6. rob@plan9.bell-labs.com
  7. .SH
  8. Introduction
  9. .PP
  10. The C compiler on Plan 9 is a wholly new program; in fact
  11. it was the first piece of software written for what would
  12. eventually become Plan 9 from Bell Labs.
  13. Programmers familiar with existing C compilers will find
  14. a number of differences in both the language the Plan 9 compiler
  15. accepts and in how the compiler is used.
  16. .PP
  17. The compiler is really a set of compilers, one for each
  18. architecture \(em MIPS, SPARC, Motorola 68020, Intel 386, etc. \(em
  19. that accept a dialect of ANSI C and efficiently produce
  20. fairly good code for the target machine.
  21. There is a packaging of the compiler that accepts strict ANSI C for
  22. a POSIX environment, but this document focuses on the
  23. native Plan 9 environment, that in which all the system source and
  24. almost all the utilities are written.
  25. .SH
  26. Source
  27. .PP
  28. The language accepted by the compilers is the core ANSI C language
  29. with some modest extensions,
  30. a greatly simplified preprocessor,
  31. a smaller library that includes system calls and related facilities,
  32. and a completely different structure for include files.
  33. .PP
  34. Official ANSI C accepts the old (K&R) style of declarations for
  35. functions; the Plan 9 compilers
  36. are more demanding.
  37. Without an explicit run-time flag
  38. .CW -B ) (
  39. whose use is discouraged, the compilers insist
  40. on new-style function declarations, that is, prototypes for
  41. function arguments.
  42. The function declarations in the libraries' include files are
  43. all in the new style so the interfaces are checked at compile time.
  44. For C programmers who have not yet switched to function prototypes
  45. the clumsy syntax may seem repellent but the payoff in stronger typing
  46. is substantial.
  47. Those who wish to import existing software to Plan 9 are urged
  48. to use the opportunity to update their code.
  49. .PP
  50. The compilers include an integrated preprocessor that accepts the familiar
  51. .CW #include ,
  52. .CW #define
  53. for macros both with and without arguments,
  54. .CW #undef ,
  55. .CW #line ,
  56. .CW #ifdef ,
  57. .CW #ifndef ,
  58. and
  59. .CW #endif .
  60. It
  61. supports neither
  62. .CW #if
  63. nor
  64. .CW ## ,
  65. although it does
  66. honor a few
  67. .CW #pragmas .
  68. The
  69. .CW #if
  70. directive was omitted because it greatly complicates the
  71. preprocessor, is never necessary, and is usually abused.
  72. Conditional compilation in general makes code hard to understand;
  73. the Plan 9 source uses it sparingly.
  74. Also, because the compilers remove dead code, regular
  75. .CW if
  76. statements with constant conditions are more readable equivalents to many
  77. .CW #ifs .
  78. To compile imported code ineluctably fouled by
  79. .CW #if
  80. there is a separate command,
  81. .CW /bin/cpp ,
  82. that implements the complete ANSI C preprocessor specification.
  83. .PP
  84. Include files fall into two groups: machine-dependent and machine-independent.
  85. The machine-independent files occupy the directory
  86. .CW /sys/include ;
  87. the others are placed in a directory appropriate to the machine, such as
  88. .CW /mips/include .
  89. The compiler searches for include files
  90. first in the machine-dependent directory and then
  91. in the machine-independent directory.
  92. At the time of writing there are thirty-one machine-independent include
  93. files and two (per machine) machine-dependent ones:
  94. .CW <ureg.h>
  95. and
  96. .CW <u.h> .
  97. The first describes the layout of registers on the system stack,
  98. for use by the debugger.
  99. The second defines some
  100. architecture-dependent types such as
  101. .CW jmp_buf
  102. for
  103. .CW setjmp
  104. and the
  105. .CW va_arg
  106. and
  107. .CW va_list
  108. macros for handling arguments to variadic functions,
  109. as well as a set of
  110. .CW typedef
  111. abbreviations for
  112. .CW unsigned
  113. .CW short
  114. and so on.
  115. .PP
  116. Here is an excerpt from
  117. .CW /68020/include/u.h :
  118. .P1
  119. #define nil ((void*)0)
  120. typedef unsigned short ushort;
  121. typedef unsigned char uchar;
  122. typedef unsigned long ulong;
  123. typedef unsigned int uint;
  124. typedef signed char schar;
  125. typedef long long vlong;
  126. typedef long jmp_buf[2];
  127. #define JMPBUFSP 0
  128. #define JMPBUFPC 1
  129. #define JMPBUFDPC 0
  130. .P2
  131. Plan 9 programs use
  132. .CW nil
  133. for the name of the zero-valued pointer.
  134. The type
  135. .CW vlong
  136. is the largest integer type available; on most architectures it
  137. is a 64-bit value.
  138. A couple of other types in
  139. .CW <u.h>
  140. are
  141. .CW u32int ,
  142. which is guaranteed to have exactly 32 bits (a possibility on all the supported architectures) and
  143. .CW mpdigit ,
  144. which is used by the multiprecision math package
  145. .CW <mp.h> .
  146. The
  147. .CW #define
  148. constants permit an architecture-independent (but compiler-dependent)
  149. implementation of stack-switching using
  150. .CW setjmp
  151. and
  152. .CW longjmp .
  153. .PP
  154. Every Plan 9 C program begins
  155. .P1
  156. #include <u.h>
  157. .P2
  158. because all the other installed header files use the
  159. .CW typedefs
  160. declared in
  161. .CW <u.h> .
  162. .PP
  163. In strict ANSI C, include files are grouped to collect related functions
  164. in a single file: one for string functions, one for memory functions,
  165. one for I/O, and none for system calls.
  166. Each include file is protected by an
  167. .CW #ifdef
  168. to guarantee its contents are seen by the compiler only once.
  169. Plan 9 takes a different approach. Other than a few include
  170. files that define external formats such as archives, the files in
  171. .CW /sys/include
  172. correspond to
  173. .I libraries.
  174. If a program is using a library, it includes the corresponding header.
  175. The default C library comprises string functions, memory functions, and
  176. so on, largely as in ANSI C, some formatted I/O routines,
  177. plus all the system calls and related functions.
  178. To use these functions, one must
  179. .CW #include
  180. the file
  181. .CW <libc.h> ,
  182. which in turn must follow
  183. .CW <u.h> ,
  184. to define their prototypes for the compiler.
  185. Here is the complete source to the traditional first C program:
  186. .P1
  187. #include <u.h>
  188. #include <libc.h>
  189. void
  190. main(void)
  191. {
  192. print("hello world\en");
  193. exits(0);
  194. }
  195. .P2
  196. The
  197. .CW print
  198. routine and its relatives
  199. .CW fprint
  200. and
  201. .CW sprint
  202. resemble the similarly-named functions in Standard I/O but are not
  203. attached to a specific I/O library.
  204. In Plan 9
  205. .CW main
  206. is not integer-valued; it should call
  207. .CW exits ,
  208. which takes a string argument (or null; here ANSI C promotes the 0 to a
  209. .CW char* ).
  210. All these functions are, of course, documented in the Programmer's Manual.
  211. .PP
  212. To use
  213. .CW printf ,
  214. .CW <stdio.h>
  215. must be included to define the function prototype for
  216. .CW printf :
  217. .P1
  218. #include <u.h>
  219. #include <libc.h>
  220. #include <stdio.h>
  221. void
  222. main(int argc, char *argv[])
  223. {
  224. printf("%s: hello world; argc = %d\en", argv[0], argc);
  225. exits(0);
  226. }
  227. .P2
  228. In practice, Standard I/O is not used much in Plan 9. I/O libraries are
  229. discussed in a later section of this document.
  230. .PP
  231. There are libraries for handling regular expressions, raster graphics,
  232. windows, and so on, and each has an associated include file.
  233. The manual for each library states which include files are needed.
  234. The files are not protected against multiple inclusion and themselves
  235. contain no nested
  236. .CW #includes .
  237. Instead the
  238. programmer is expected to sort out the requirements
  239. and to
  240. .CW #include
  241. the necessary files once at the top of each source file. In practice this is
  242. trivial: this way of handling include files is so straightforward
  243. that it is rare for a source file to contain more than half a dozen
  244. .CW #includes .
  245. .PP
  246. The compilers do their own register allocation so the
  247. .CW register
  248. keyword is ignored.
  249. For different reasons,
  250. .CW volatile
  251. and
  252. .CW const
  253. are also ignored.
  254. .PP
  255. To make it easier to share code with other systems, Plan 9 has a version
  256. of the compiler,
  257. .CW pcc ,
  258. that provides the standard ANSI C preprocessor, headers, and libraries
  259. with POSIX extensions.
  260. .CW Pcc
  261. is recommended only
  262. when broad external portability is mandated. It compiles slower,
  263. produces slower code (it takes extra work to simulate POSIX on Plan 9),
  264. eliminates those parts of the Plan 9 interface
  265. not related to POSIX, and illustrates the clumsiness of an environment
  266. designed by committee.
  267. .CW Pcc
  268. is described in more detail in
  269. .I
  270. APE\(emThe ANSI/POSIX Environment,
  271. .R
  272. by Howard Trickey.
  273. .SH
  274. Process
  275. .PP
  276. Each CPU architecture supported by Plan 9 is identified by a single,
  277. arbitrary, alphanumeric character:
  278. .CW k
  279. for SPARC,
  280. .CW q
  281. for Motorola Power PC 630 and 640,
  282. .CW v
  283. for MIPS,
  284. .CW 1
  285. for Motorola 68000,
  286. .CW 2
  287. for Motorola 68020 and 68040,
  288. .CW 5
  289. for Acorn ARM 7500,
  290. .CW 6
  291. for Intel 960,
  292. .CW 7
  293. for DEC Alpha,
  294. .CW 8
  295. for Intel 386, and
  296. .CW 9
  297. for AMD 29000.
  298. The character labels the support tools and files for that architecture.
  299. For instance, for the 68020 the compiler is
  300. .CW 2c ,
  301. the assembler is
  302. .CW 2a ,
  303. the link editor/loader is
  304. .CW 2l ,
  305. the object files are suffixed
  306. .CW \&.2 ,
  307. and the default name for an executable file is
  308. .CW 2.out .
  309. Before we can use the compiler we therefore need to know which
  310. machine we are compiling for.
  311. The next section explains how this decision is made; for the moment
  312. assume we are building 68020 binaries and make the mental substitution for
  313. .CW 2
  314. appropriate to the machine you are actually using.
  315. .PP
  316. To convert source to an executable binary is a two-step process.
  317. First run the compiler,
  318. .CW 2c ,
  319. on the source, say
  320. .CW file.c ,
  321. to generate an object file
  322. .CW file.2 .
  323. Then run the loader,
  324. .CW 2l ,
  325. to generate an executable
  326. .CW 2.out
  327. that may be run (on a 680X0 machine):
  328. .P1
  329. 2c file.c
  330. 2l file.2
  331. 2.out
  332. .P2
  333. The loader automatically links with whatever libraries the program
  334. needs, usually including the standard C library as defined by
  335. .CW <libc.h> .
  336. Of course the compiler and loader have lots of options, both familiar and new;
  337. see the manual for details.
  338. The compiler does not generate an executable automatically;
  339. the output of the compiler must be given to the loader.
  340. Since most compilation is done under the control of
  341. .CW mk
  342. (see below), this is rarely an inconvenience.
  343. .PP
  344. The distribution of work between the compiler and loader is unusual.
  345. The compiler integrates preprocessing, parsing, register allocation,
  346. code generation and some assembly.
  347. Combining these tasks in a single program is part of the reason for
  348. the compiler's efficiency.
  349. The loader does instruction selection, branch folding,
  350. instruction scheduling,
  351. and writes the final executable.
  352. There is no separate C preprocessor and no assembler in the usual pipeline.
  353. Instead the intermediate object file
  354. (here a
  355. .CW \&.2
  356. file) is a type of binary assembly language.
  357. The instructions in the intermediate format are not exactly those in
  358. the machine. For example, on the 68020 the object file may specify
  359. a MOVE instruction but the loader will decide just which variant of
  360. the MOVE instruction \(em MOVE immediate, MOVE quick, MOVE address,
  361. etc. \(em is most efficient.
  362. .PP
  363. The assembler,
  364. .CW 2a ,
  365. is just a translator between the textual and binary
  366. representations of the object file format.
  367. It is not an assembler in the traditional sense. It has limited
  368. macro capabilities (the same as the integral C preprocessor in the compiler),
  369. clumsy syntax, and minimal error checking. For instance, the assembler
  370. will accept an instruction (such as memory-to-memory MOVE on the MIPS) that the
  371. machine does not actually support; only when the output of the assembler
  372. is passed to the loader will the error be discovered.
  373. The assembler is intended only for writing things that need access to instructions
  374. invisible from C,
  375. such as the machine-dependent
  376. part of an operating system;
  377. very little code in Plan 9 is in assembly language.
  378. .PP
  379. The compilers take an option
  380. .CW -S
  381. that causes them to print on their standard output the generated code
  382. in a format acceptable as input to the assemblers.
  383. This is of course merely a formatting of the
  384. data in the object file; therefore the assembler is just
  385. an
  386. ASCII-to-binary converter for this format.
  387. Other than the specific instructions, the input to the assemblers
  388. is largely architecture-independent; see
  389. ``A Manual for the Plan 9 Assembler'',
  390. by Rob Pike,
  391. for more information.
  392. .PP
  393. The loader is an integral part of the compilation process.
  394. Each library header file contains a
  395. .CW #pragma
  396. that tells the loader the name of the associated archive; it is
  397. not necessary to tell the loader which libraries a program uses.
  398. The C run-time startup is found, by default, in the C library.
  399. The loader starts with an undefined
  400. symbol,
  401. .CW _main ,
  402. that is resolved by pulling in the run-time startup code from the library.
  403. (The loader undefines
  404. .CW _mainp
  405. when profiling is enabled, to force loading of the profiling start-up
  406. instead.)
  407. .PP
  408. Unlike its counterpart on other systems, the Plan 9 loader rearranges
  409. data to optimize access. This means the order of variables in the
  410. loaded program is unrelated to its order in the source.
  411. Most programs don't care, but some assume that, for example, the
  412. variables declared by
  413. .P1
  414. int a;
  415. int b;
  416. .P2
  417. will appear at adjacent addresses in memory. On Plan 9, they won't.
  418. .SH
  419. Heterogeneity
  420. .PP
  421. When the system starts or a user logs in the environment is configured
  422. so the appropriate binaries are available in
  423. .CW /bin .
  424. The configuration process is controlled by an environment variable,
  425. .CW $cputype ,
  426. with value such as
  427. .CW mips ,
  428. .CW 68020 ,
  429. .CW 386 ,
  430. or
  431. .CW sparc .
  432. For each architecture there is a directory in the root,
  433. with the appropriate name,
  434. that holds the binary and library files for that architecture.
  435. Thus
  436. .CW /mips/lib
  437. contains the object code libraries for MIPS programs,
  438. .CW /mips/include
  439. holds MIPS-specific include files, and
  440. .CW /mips/bin
  441. has the MIPS binaries.
  442. These binaries are attached to
  443. .CW /bin
  444. at boot time by binding
  445. .CW /$cputype/bin
  446. to
  447. .CW /bin ,
  448. so
  449. .CW /bin
  450. always contains the correct files.
  451. .PP
  452. The MIPS compiler,
  453. .CW vc ,
  454. by definition
  455. produces object files for the MIPS architecture,
  456. regardless of the architecture of the machine on which the compiler is running.
  457. There is a version of
  458. .CW vc
  459. compiled for each architecture:
  460. .CW /mips/bin/vc ,
  461. .CW /68020/bin/vc ,
  462. .CW /sparc/bin/vc ,
  463. and so on,
  464. each capable of producing MIPS object files regardless of the native
  465. instruction set.
  466. If one is running on a SPARC,
  467. .CW /sparc/bin/vc
  468. will compile programs for the MIPS;
  469. if one is running on machine
  470. .CW $cputype ,
  471. .CW /$cputype/bin/vc
  472. will compile programs for the MIPS.
  473. .PP
  474. Because of the bindings that assemble
  475. .CW /bin ,
  476. the shell always looks for a command, say
  477. .CW date ,
  478. in
  479. .CW /bin
  480. and automatically finds the file
  481. .CW /$cputype/bin/date .
  482. Therefore the MIPS compiler is known as just
  483. .CW vc ;
  484. the shell will invoke
  485. .CW /bin/vc
  486. and that is guaranteed to be the version of the MIPS compiler
  487. appropriate for the machine running the command.
  488. Regardless of the architecture of the compiling machine,
  489. .CW /bin/vc
  490. is
  491. .I always
  492. the MIPS compiler.
  493. .PP
  494. Also, the output of
  495. .CW vc
  496. and
  497. .CW vl
  498. is completely independent of the machine type on which they are executed:
  499. .CW \&.v
  500. files compiled (with
  501. .CW vc )
  502. on a SPARC may be linked (with
  503. .CW vl )
  504. on a 386.
  505. (The resulting
  506. .CW v.out
  507. will run, of course, only on a MIPS.)
  508. Similarly, the MIPS libraries in
  509. .CW /mips/lib
  510. are suitable for loading with
  511. .CW vl
  512. on any machine; there is only one set of MIPS libraries, not one
  513. set for each architecture that supports the MIPS compiler.
  514. .SH
  515. Heterogeneity and \f(CWmk\fP
  516. .PP
  517. Most software on Plan 9 is compiled under the control of
  518. .CW mk ,
  519. a descendant of
  520. .CW make
  521. that is documented in the Programmer's Manual.
  522. A convention used throughout the
  523. .CW mkfiles
  524. makes it easy to compile the source into binary suitable for any architecture.
  525. .PP
  526. The variable
  527. .CW $cputype
  528. is advisory: it reports the architecture of the current environment, and should
  529. not be modified. A second variable,
  530. .CW $objtype ,
  531. is used to set which architecture is being
  532. .I compiled
  533. for.
  534. The value of
  535. .CW $objtype
  536. can be used by a
  537. .CW mkfile
  538. to configure the compilation environment.
  539. .PP
  540. In each machine's root directory there is a short
  541. .CW mkfile
  542. that defines a set of macros for the compiler, loader, etc.
  543. Here is
  544. .CW /mips/mkfile :
  545. .P1
  546. </sys/src/mkfile.proto
  547. CC=vc
  548. LD=vl
  549. O=v
  550. AS=va
  551. .P2
  552. The line
  553. .P1
  554. </sys/src/mkfile.proto
  555. .P2
  556. causes
  557. .CW mk
  558. to include the file
  559. .CW /sys/src/mkfile.proto ,
  560. which contains general definitions:
  561. .P1
  562. #
  563. # common mkfile parameters shared by all architectures
  564. #
  565. OS=v486xq7
  566. CPUS=mips 386 power alpha
  567. CFLAGS=-FVw
  568. LEX=lex
  569. YACC=yacc
  570. MK=/bin/mk
  571. .P2
  572. .CW CC
  573. is obviously the compiler,
  574. .CW AS
  575. the assembler, and
  576. .CW LD
  577. the loader.
  578. .CW O
  579. is the suffix for the object files and
  580. .CW CPUS
  581. and
  582. .CW OS
  583. are used in special rules described below.
  584. .PP
  585. Here is a
  586. .CW mkfile
  587. to build the installed source for
  588. .CW sam :
  589. .P1
  590. </$objtype/mkfile
  591. OBJ=sam.$O address.$O buffer.$O cmd.$O disc.$O error.$O \e
  592. file.$O io.$O list.$O mesg.$O moveto.$O multi.$O \e
  593. plan9.$O rasp.$O regexp.$O string.$O sys.$O xec.$O
  594. $O.out: $OBJ
  595. $LD $OBJ
  596. install: $O.out
  597. cp $O.out /$objtype/bin/sam
  598. installall:
  599. for(objtype in $CPUS) mk install
  600. %.$O: %.c
  601. $CC $CFLAGS $stem.c
  602. $OBJ: sam.h errors.h mesg.h
  603. address.$O cmd.$O parse.$O xec.$O unix.$O: parse.h
  604. clean:V:
  605. rm -f [$OS].out *.[$OS] y.tab.?
  606. .P2
  607. (The actual
  608. .CW mkfile
  609. imports most of its rules from other secondary files, but
  610. this example works and is not misleading.)
  611. The first line causes
  612. .CW mk
  613. to include the contents of
  614. .CW /$objtype/mkfile
  615. in the current
  616. .CW mkfile .
  617. If
  618. .CW $objtype
  619. is
  620. .CW mips ,
  621. this inserts the MIPS macro definitions into the
  622. .CW mkfile .
  623. In this case the rule for
  624. .CW $O.out
  625. uses the MIPS tools to build
  626. .CW v.out .
  627. The
  628. .CW %.$O
  629. rule in the file uses
  630. .CW mk 's
  631. pattern matching facilities to convert the source files to the object
  632. files through the compiler.
  633. (The text of the rules is passed directly to the shell,
  634. .CW rc ,
  635. without further translation.
  636. See the
  637. .CW mk
  638. manual if any of this is unfamiliar.)
  639. Because the default rule builds
  640. .CW $O.out
  641. rather than
  642. .CW sam ,
  643. it is possible to maintain binaries for multiple machines in the
  644. same source directory without conflict.
  645. This is also, of course, why the output files from the various
  646. compilers and loaders
  647. have distinct names.
  648. .PP
  649. The rest of the
  650. .CW mkfile
  651. should be easy to follow; notice how the rules for
  652. .CW clean
  653. and
  654. .CW installall
  655. (that is, install versions for all architectures) use other macros
  656. defined in
  657. .CW /$objtype/mkfile .
  658. In Plan 9,
  659. .CW mkfiles
  660. for commands conventionally contain rules to
  661. .CW install
  662. (compile and install the version for
  663. .CW $objtype ),
  664. .CW installall
  665. (compile and install for all
  666. .CW $objtypes ),
  667. and
  668. .CW clean
  669. (remove all object files, binaries, etc.).
  670. .PP
  671. The
  672. .CW mkfile
  673. is easy to use. To build a MIPS binary,
  674. .CW v.out :
  675. .P1
  676. % objtype=mips
  677. % mk
  678. .P2
  679. To build and install a MIPS binary:
  680. .P1
  681. % objtype=mips
  682. % mk install
  683. .P2
  684. To build and install all versions:
  685. .P1
  686. % mk installall
  687. .P2
  688. These conventions make cross-compilation as easy to manage
  689. as traditional native compilation.
  690. Plan 9 programs compile and run without change on machines from
  691. large multiprocessors to laptops. For more information about this process, see
  692. ``Plan 9 Mkfiles'',
  693. by Bob Flandrena.
  694. .SH
  695. Portability
  696. .PP
  697. Within Plan 9, it is painless to write portable programs, programs whose
  698. source is independent of the machine on which they execute.
  699. The operating system is fixed and the compiler, headers and libraries
  700. are constant so most of the stumbling blocks to portability are removed.
  701. Attention to a few details can avoid those that remain.
  702. .PP
  703. Plan 9 is a heterogeneous environment, so programs must
  704. .I expect
  705. that external files will be written by programs on machines of different
  706. architectures.
  707. The compilers, for instance, must handle without confusion
  708. object files written by other machines.
  709. The traditional approach to this problem is to pepper the source with
  710. .CW #ifdefs
  711. to turn byte-swapping on and off.
  712. Plan 9 takes a different approach: of the handful of machine-dependent
  713. .CW #ifdefs
  714. in all the source, almost all are deep in the libraries.
  715. Instead programs read and write files in a defined format,
  716. either (for low volume applications) as formatted text, or
  717. (for high volume applications) as binary in a known byte order.
  718. If the external data were written with the most significant
  719. byte first, the following code reads a 4-byte integer correctly
  720. regardless of the architecture of the executing machine (assuming
  721. an unsigned long holds 4 bytes):
  722. .P1
  723. ulong
  724. getlong(void)
  725. {
  726. ulong l;
  727. l = (getchar()&0xFF)<<24;
  728. l |= (getchar()&0xFF)<<16;
  729. l |= (getchar()&0xFF)<<8;
  730. l |= (getchar()&0xFF)<<0;
  731. return l;
  732. }
  733. .P2
  734. Note that this code does not `swap' the bytes; instead it just reads
  735. them in the correct order.
  736. Variations of this code will handle any binary format
  737. and also avoid problems
  738. involving how structures are padded, how words are aligned,
  739. and other impediments to portability.
  740. Be aware, though, that extra care is needed to handle floating point data.
  741. .PP
  742. Efficiency hounds will argue that this method is unnecessarily slow and clumsy
  743. when the executing machine has the same byte order (and padding and alignment)
  744. as the data.
  745. The CPU cost of I/O processing
  746. is rarely the bottleneck for an application, however,
  747. and the gain in simplicity of porting and maintaining the code greatly outweighs
  748. the minor speed loss from handling data in this general way.
  749. This method is how the Plan 9 compilers, the window system, and even the file
  750. servers transmit data between programs.
  751. .PP
  752. To port programs beyond Plan 9, where the system interface is more variable,
  753. it is probably necessary to use
  754. .CW pcc
  755. and hope that the target machine supports ANSI C and POSIX.
  756. .SH
  757. I/O
  758. .PP
  759. The default C library, defined by the include file
  760. .CW <libc.h> ,
  761. contains no buffered I/O package.
  762. It does have several entry points for printing formatted text:
  763. .CW print
  764. outputs text to the standard output,
  765. .CW fprint
  766. outputs text to a specified integer file descriptor, and
  767. .CW sprint
  768. places text in a character array.
  769. To access library routines for buffered I/O, a program must
  770. explicitly include the header file associated with an appropriate library.
  771. .PP
  772. The recommended I/O library, used by most Plan 9 utilities, is
  773. .CW bio
  774. (buffered I/O), defined by
  775. .CW <bio.h> .
  776. There also exists an implementation of ANSI Standard I/O,
  777. .CW stdio .
  778. .PP
  779. .CW Bio
  780. is small and efficient, particularly for buffer-at-a-time or
  781. line-at-a-time I/O.
  782. Even for character-at-a-time I/O, however, it is significantly faster than
  783. the Standard I/O library,
  784. .CW stdio .
  785. Its interface is compact and regular, although it lacks a few conveniences.
  786. The most noticeable is that one must explicitly define buffers for standard
  787. input and output;
  788. .CW bio
  789. does not predefine them. Here is a program to copy input to output a byte
  790. at a time using
  791. .CW bio :
  792. .P1
  793. #include <u.h>
  794. #include <libc.h>
  795. #include <bio.h>
  796. Biobuf bin;
  797. Biobuf bout;
  798. main(void)
  799. {
  800. int c;
  801. Binit(&bin, 0, OREAD);
  802. Binit(&bout, 1, OWRITE);
  803. while((c=Bgetc(&bin)) != Beof)
  804. Bputc(&bout, c);
  805. exits(0);
  806. }
  807. .P2
  808. For peak performance, we could replace
  809. .CW Bgetc
  810. and
  811. .CW Bputc
  812. by their equivalent in-line macros
  813. .CW BGETC
  814. and
  815. .CW BPUTC
  816. but
  817. the performance gain would be modest.
  818. For more information on
  819. .CW bio ,
  820. see the Programmer's Manual.
  821. .PP
  822. Perhaps the most dramatic difference in the I/O interface of Plan 9 from other
  823. systems' is that text is not ASCII.
  824. The format for
  825. text in Plan 9 is a byte-stream encoding of 16-bit characters.
  826. The character set is based on the Unicode Standard and is backward compatible with
  827. ASCII:
  828. characters with value 0 through 127 are the same in both sets.
  829. The 16-bit characters, called
  830. .I runes
  831. in Plan 9, are encoded using a representation called
  832. UTF,
  833. an encoding that is becoming accepted as a standard.
  834. (ISO calls it UTF-8;
  835. throughout Plan 9 it's just called
  836. UTF.)
  837. UTF
  838. defines multibyte sequences to
  839. represent character values from 0 to 65535.
  840. In
  841. UTF,
  842. character values up to 127 decimal, 7F hexadecimal, represent themselves,
  843. so straight
  844. ASCII
  845. files are also valid
  846. UTF.
  847. Also,
  848. UTF
  849. guarantees that bytes with values 0 to 127 (NUL to DEL, inclusive)
  850. will appear only when they represent themselves, so programs that read bytes
  851. looking for plain ASCII characters will continue to work.
  852. Any program that expects a one-to-one correspondence between bytes and
  853. characters will, however, need to be modified.
  854. An example is parsing file names.
  855. File names, like all text, are in
  856. UTF,
  857. so it is incorrect to search for a character in a string by
  858. .CW strchr(filename,
  859. .CW c)
  860. because the character might have a multi-byte encoding.
  861. The correct method is to call
  862. .CW utfrune(filename,
  863. .CW c) ,
  864. defined in
  865. .I rune (2),
  866. which interprets the file name as a sequence of encoded characters
  867. rather than bytes.
  868. In fact, even when you know the character is a single byte
  869. that can represent only itself,
  870. it is safer to use
  871. .CW utfrune
  872. because that assumes nothing about the character set
  873. and its representation.
  874. .PP
  875. The library defines several symbols relevant to the representation of characters.
  876. Any byte with unsigned value less than
  877. .CW Runesync
  878. will not appear in any multi-byte encoding of a character.
  879. .CW Utfrune
  880. compares the character being searched against
  881. .CW Runesync
  882. to see if it is sufficient to call
  883. .CW strchr
  884. or if the byte stream must be interpreted.
  885. Any byte with unsigned value less than
  886. .CW Runeself
  887. is represented by a single byte with the same value.
  888. Finally, when errors are encountered converting
  889. to runes from a byte stream, the library returns the rune value
  890. .CW Runeerror
  891. and advances a single byte. This permits programs to find runes
  892. embedded in binary data.
  893. .PP
  894. .CW Bio
  895. includes routines
  896. .CW Bgetrune
  897. and
  898. .CW Bputrune
  899. to transform the external byte stream
  900. UTF
  901. format to and from
  902. internal 16-bit runes.
  903. Also, the
  904. .CW %s
  905. format to
  906. .CW print
  907. accepts
  908. UTF;
  909. .CW %c
  910. prints a character after narrowing it to 8 bits.
  911. The
  912. .CW %S
  913. format prints a null-terminated sequence of runes;
  914. .CW %C
  915. prints a character after narrowing it to 16 bits.
  916. For more information, see the Programmer's Manual, in particular
  917. .I utf (6)
  918. and
  919. .I rune (2),
  920. and the paper,
  921. ``Hello world, or
  922. Καλημέρα κόσμε, or\
  923. \f(Jpこんにちは 世界\f1'',
  924. by Rob Pike and
  925. Ken Thompson;
  926. there is not room for the full story here.
  927. .PP
  928. These issues affect the compiler in several ways.
  929. First, the C source is in
  930. UTF.
  931. ANSI says C variables are formed from
  932. ASCII
  933. alphanumerics, but comments and literal strings may contain any characters
  934. encoded in the native encoding, here
  935. UTF.
  936. The declaration
  937. .P1
  938. char *cp = "abcÿ";
  939. .P2
  940. initializes the variable
  941. .CW cp
  942. to point to an array of bytes holding the
  943. UTF
  944. representation of the characters
  945. .CW abcÿ.
  946. The type
  947. .CW Rune
  948. is defined in
  949. .CW <u.h>
  950. to be
  951. .CW ushort ,
  952. which is also the `wide character' type in the compiler.
  953. Therefore the declaration
  954. .P1
  955. Rune *rp = L"abcÿ";
  956. .P2
  957. initializes the variable
  958. .CW rp
  959. to point to an array of unsigned short integers holding the 16-bit
  960. values of the characters
  961. .CW abcÿ .
  962. Note that in both these declarations the characters in the source
  963. that represent
  964. .CW "abcÿ"
  965. are the same; what changes is how those characters are represented
  966. in memory in the program.
  967. The following two lines:
  968. .P1
  969. print("%s\en", "abcÿ");
  970. print("%S\en", L"abcÿ");
  971. .P2
  972. produce the same
  973. UTF
  974. string on their output, the first by copying the bytes, the second
  975. by converting from runes to bytes.
  976. .PP
  977. In C, character constants are integers but narrowed through the
  978. .CW char
  979. type.
  980. The Unicode character
  981. .CW ÿ
  982. has value 255, so if the
  983. .CW char
  984. type is signed,
  985. the constant
  986. .CW 'ÿ'
  987. has value \-1 (which is equal to EOF).
  988. On the other hand,
  989. .CW L'ÿ'
  990. narrows through the wide character type,
  991. .CW ushort ,
  992. and therefore has value 255.
  993. .PP
  994. Finally, although it's not ANSI C, the Plan 9 C compilers
  995. assume any character with value above
  996. .CW Runeself
  997. is an alphanumeric,
  998. so α is a legal, if non-portable, variable name.
  999. .SH
  1000. Arguments
  1001. .PP
  1002. Some macros are defined
  1003. in
  1004. .CW <libc.h>
  1005. for parsing the arguments to
  1006. .CW main() .
  1007. They are described in
  1008. .I ARG (2)
  1009. but are fairly self-explanatory.
  1010. There are four macros:
  1011. .CW ARGBEGIN
  1012. and
  1013. .CW ARGEND
  1014. are used to bracket a hidden
  1015. .CW switch
  1016. statement within which
  1017. .CW ARGC
  1018. returns the current option character (rune) being processed and
  1019. .CW ARGF
  1020. returns the argument to the option, as in the loader option
  1021. .CW -o
  1022. .CW file .
  1023. Here, for example, is the code at the beginning of
  1024. .CW main()
  1025. in
  1026. .CW ramfs.c
  1027. (see
  1028. .I ramfs (1))
  1029. that cracks its arguments:
  1030. .P1
  1031. void
  1032. main(int argc, char *argv[])
  1033. {
  1034. char *defmnt;
  1035. int p[2];
  1036. int mfd[2];
  1037. int stdio = 0;
  1038. defmnt = "/tmp";
  1039. ARGBEGIN{
  1040. case 'i':
  1041. defmnt = 0;
  1042. stdio = 1;
  1043. mfd[0] = 0;
  1044. mfd[1] = 1;
  1045. break;
  1046. case 's':
  1047. defmnt = 0;
  1048. break;
  1049. case 'm':
  1050. defmnt = ARGF();
  1051. break;
  1052. default:
  1053. usage();
  1054. }ARGEND
  1055. .P2
  1056. .SH
  1057. Extensions
  1058. .PP
  1059. The compiler has several extensions to ANSI C, all of which are used
  1060. extensively in the system source.
  1061. First,
  1062. .I structure
  1063. .I displays
  1064. permit
  1065. .CW struct
  1066. expressions to be formed dynamically.
  1067. Given these declarations:
  1068. .P1
  1069. typedef struct Point Point;
  1070. typedef struct Rectangle Rectangle;
  1071. struct Point
  1072. {
  1073. int x, y;
  1074. };
  1075. struct Rectangle
  1076. {
  1077. Point min, max;
  1078. };
  1079. Point p, q, add(Point, Point);
  1080. Rectangle r;
  1081. int x, y;
  1082. .P2
  1083. this assignment may appear anywhere an assignment is legal:
  1084. .P1
  1085. r = (Rectangle){add(p, q), (Point){x, y+3}};
  1086. .P2
  1087. The syntax is the same as for initializing a structure but with
  1088. a leading cast.
  1089. .PP
  1090. If an
  1091. .I anonymous
  1092. .I structure
  1093. or
  1094. .I union
  1095. is declared within another structure or union, the members of the internal
  1096. structure or union are addressable without prefix in the outer structure.
  1097. This feature eliminates the clumsy naming of nested structures and,
  1098. particularly, unions.
  1099. For example, after these declarations,
  1100. .P1
  1101. struct Lock
  1102. {
  1103. int locked;
  1104. };
  1105. struct Node
  1106. {
  1107. int type;
  1108. union{
  1109. double dval;
  1110. double fval;
  1111. long lval;
  1112. }; /* anonymous union */
  1113. struct Lock; /* anonymous structure */
  1114. } *node;
  1115. void lock(struct Lock*);
  1116. .P2
  1117. one may refer to
  1118. .CW node->type ,
  1119. .CW node->dval ,
  1120. .CW node->fval ,
  1121. .CW node->lval ,
  1122. and
  1123. .CW node->locked .
  1124. Moreover, the address of a
  1125. .CW struct
  1126. .CW Node
  1127. may be used without a cast anywhere that the address of a
  1128. .CW struct
  1129. .CW Lock
  1130. is used, such as in argument lists.
  1131. The compiler automatically promotes the type and adjusts the address.
  1132. Thus one may invoke
  1133. .CW lock(node) .
  1134. .PP
  1135. Anonymous structures and unions may be accessed by type name
  1136. if (and only if) they are declared using a
  1137. .CW typedef
  1138. name.
  1139. For example, using the above declaration for
  1140. .CW Point ,
  1141. one may declare
  1142. .P1
  1143. struct
  1144. {
  1145. int type;
  1146. Point;
  1147. } p;
  1148. .P2
  1149. and refer to
  1150. .CW p.Point .
  1151. .PP
  1152. In the initialization of arrays, a number in square brackets before an
  1153. element sets the index for the initialization. For example, to initialize
  1154. some elements in
  1155. a table of function pointers indexed by
  1156. ASCII
  1157. character,
  1158. .P1
  1159. void percent(void), slash(void);
  1160. void (*func[128])(void) =
  1161. {
  1162. ['%'] percent,
  1163. ['/'] slash,
  1164. };
  1165. .P2
  1166. .LP
  1167. A similar syntax allows one to initialize structure elements:
  1168. .P1
  1169. Point p =
  1170. {
  1171. .y 100,
  1172. .x 200
  1173. };
  1174. .P2
  1175. These initialization syntaxes were later added to ANSI C, with the addition of an
  1176. equals sign between the index or tag and the value.
  1177. The Plan 9 compiler accepts either form.
  1178. .PP
  1179. Finally, the declaration
  1180. .P1
  1181. extern register reg;
  1182. .P2
  1183. .I this "" (
  1184. appearance of the register keyword is not ignored)
  1185. allocates a global register to hold the variable
  1186. .CW reg .
  1187. External registers must be used carefully: they need to be declared in
  1188. .I all
  1189. source files and libraries in the program to guarantee the register
  1190. is not allocated temporarily for other purposes.
  1191. Especially on machines with few registers, such as the i386,
  1192. it is easy to link accidentally with code that has already usurped
  1193. the global registers and there is no diagnostic when this happens.
  1194. Used wisely, though, external registers are powerful.
  1195. The Plan 9 operating system uses them to access per-process and
  1196. per-machine data structures on a multiprocessor. The storage class they provide
  1197. is hard to create in other ways.
  1198. .SH
  1199. The compile-time environment
  1200. .PP
  1201. The code generated by the compilers is `optimized' by default:
  1202. variables are placed in registers and peephole optimizations are
  1203. performed.
  1204. The compiler flag
  1205. .CW -N
  1206. disables these optimizations.
  1207. Registerization is done locally rather than throughout a function:
  1208. whether a variable occupies a register or
  1209. the memory location identified in the symbol
  1210. table depends on the activity of the variable and may change
  1211. throughout the life of the variable.
  1212. The
  1213. .CW -N
  1214. flag is rarely needed;
  1215. its main use is to simplify debugging.
  1216. There is no information in the symbol table to identify the
  1217. registerization of a variable, so
  1218. .CW -N
  1219. guarantees the variable is always where the symbol table says it is.
  1220. .PP
  1221. Another flag,
  1222. .CW -w ,
  1223. turns
  1224. .I on
  1225. warnings about portability and problems detected in flow analysis.
  1226. Most code in Plan 9 is compiled with warnings enabled;
  1227. these warnings plus the type checking offered by function prototypes
  1228. provide most of the support of the Unix tool
  1229. .CW lint
  1230. more accurately and with less chatter.
  1231. Two of the warnings,
  1232. `used and not set' and `set and not used', are almost always accurate but
  1233. may be triggered spuriously by code with invisible control flow,
  1234. such as in routines that call
  1235. .CW longjmp .
  1236. The compiler statements
  1237. .P1
  1238. SET(v1);
  1239. USED(v2);
  1240. .P2
  1241. decorate the flow graph to silence the compiler.
  1242. Either statement accepts a comma-separated list of variables.
  1243. Use them carefully: they may silence real errors.
  1244. For the common case of unused parameters to a function,
  1245. leaving the name off the declaration silences the warnings.
  1246. That is, listing the type of a parameter but giving it no
  1247. associated variable name does the trick.
  1248. .SH
  1249. Debugging
  1250. .PP
  1251. There are two debuggers available on Plan 9.
  1252. The first, and older, is
  1253. .CW db ,
  1254. a revision of Unix
  1255. .CW adb .
  1256. The other,
  1257. .CW acid ,
  1258. is a source-level debugger whose commands are statements in
  1259. a true programming language.
  1260. .CW Acid
  1261. is the preferred debugger, but since it
  1262. borrows some elements of
  1263. .CW db ,
  1264. notably the formats for displaying values, it is worth knowing a little bit about
  1265. .CW db .
  1266. .PP
  1267. Both debuggers support multiple architectures in a single program; that is,
  1268. the programs are
  1269. .CW db
  1270. and
  1271. .CW acid ,
  1272. not for example
  1273. .CW vdb
  1274. and
  1275. .CW vacid .
  1276. They also support cross-architecture debugging comfortably:
  1277. one may debug a 68020 binary on a MIPS.
  1278. .PP
  1279. Imagine a program has crashed mysteriously:
  1280. .P1
  1281. % X11/X
  1282. Fatal server bug!
  1283. failed to create default stipple
  1284. X 106: suicide: sys: trap: fault read addr=0x0 pc=0x00105fb8
  1285. %
  1286. .P2
  1287. When a process dies on Plan 9 it hangs in the `broken' state
  1288. for debugging.
  1289. Attach a debugger to the process by naming its process id:
  1290. .P1
  1291. % acid 106
  1292. /proc/106/text:mips plan 9 executable
  1293. /sys/lib/acid/port
  1294. /sys/lib/acid/mips
  1295. acid:
  1296. .P2
  1297. The
  1298. .CW acid
  1299. function
  1300. .CW stk()
  1301. reports the stack traceback:
  1302. .P1
  1303. acid: stk()
  1304. At pc:0x105fb8:abort+0x24 /sys/src/ape/lib/ap/stdio/abort.c:6
  1305. abort() /sys/src/ape/lib/ap/stdio/abort.c:4
  1306. called from FatalError+#4e
  1307. /sys/src/X/mit/server/dix/misc.c:421
  1308. FatalError(s9=#e02, s8=#4901d200, s7=#2, s6=#72701, s5=#1,
  1309. s4=#7270d, s3=#6, s2=#12, s1=#ff37f1c, s0=#6, f=#7270f)
  1310. /sys/src/X/mit/server/dix/misc.c:416
  1311. called from gnotscreeninit+#4ce
  1312. /sys/src/X/mit/server/ddx/gnot/gnot.c:792
  1313. gnotscreeninit(snum=#0, sc=#80db0)
  1314. /sys/src/X/mit/server/ddx/gnot/gnot.c:766
  1315. called from AddScreen+#16e
  1316. /n/bootes/sys/src/X/mit/server/dix/main.c:610
  1317. AddScreen(pfnInit=0x0000129c,argc=0x00000001,argv=0x7fffffe4)
  1318. /sys/src/X/mit/server/dix/main.c:530
  1319. called from InitOutput+0x80
  1320. /sys/src/X/mit/server/ddx/brazil/brddx.c:522
  1321. InitOutput(argc=0x00000001,argv=0x7fffffe4)
  1322. /sys/src/X/mit/server/ddx/brazil/brddx.c:511
  1323. called from main+0x294
  1324. /sys/src/X/mit/server/dix/main.c:225
  1325. main(argc=0x00000001,argv=0x7fffffe4)
  1326. /sys/src/X/mit/server/dix/main.c:136
  1327. called from _main+0x24
  1328. /sys/src/ape/lib/ap/mips/main9.s:8
  1329. .P2
  1330. The function
  1331. .CW lstk()
  1332. is similar but
  1333. also reports the values of local variables.
  1334. Note that the traceback includes full file names; this is a boon to debugging,
  1335. although it makes the output much noisier.
  1336. .PP
  1337. To use
  1338. .CW acid
  1339. well you will need to learn its input language; see the
  1340. ``Acid Manual'',
  1341. by Phil Winterbottom,
  1342. for details. For simple debugging, however, the information in the manual page is
  1343. sufficient. In particular, it describes the most useful functions
  1344. for examining a process.
  1345. .PP
  1346. The compiler does not place
  1347. information describing the types of variables in the executable,
  1348. but a compile-time flag provides crude support for symbolic debugging.
  1349. The
  1350. .CW -a
  1351. flag to the compiler suppresses code generation
  1352. and instead emits source text in the
  1353. .CW acid
  1354. language to format and display data structure types defined in the program.
  1355. The easiest way to use this feature is to put a rule in the
  1356. .CW mkfile :
  1357. .P1
  1358. syms: main.$O
  1359. $CC -a main.c > syms
  1360. .P2
  1361. Then from within
  1362. .CW acid ,
  1363. .P1
  1364. acid: include("sourcedirectory/syms")
  1365. .P2
  1366. to read in the relevant definitions.
  1367. (For multi-file source, you need to be a little fancier;
  1368. see
  1369. .I 2c (1)).
  1370. This text includes, for each defined compound
  1371. type, a function with that name that may be called with the address of a structure
  1372. of that type to display its contents.
  1373. For example, if
  1374. .CW rect
  1375. is a global variable of type
  1376. .CW Rectangle ,
  1377. one may execute
  1378. .P1
  1379. Rectangle(*rect)
  1380. .P2
  1381. to display it.
  1382. The
  1383. .CW *
  1384. (indirection) operator is necessary because
  1385. of the way
  1386. .CW acid
  1387. works: each global symbol in the program is defined as a variable by
  1388. .CW acid ,
  1389. with value equal to the
  1390. .I address
  1391. of the symbol.
  1392. .PP
  1393. Another common technique is to write by hand special
  1394. .CW acid
  1395. code to define functions to aid debugging, initialize the debugger, and so on.
  1396. Conventionally, this is placed in a file called
  1397. .CW acid
  1398. in the source directory; it has a line
  1399. .P1
  1400. include("sourcedirectory/syms");
  1401. .P2
  1402. to load the compiler-produced symbols. One may edit the compiler output directly but
  1403. it is wiser to keep the hand-generated
  1404. .CW acid
  1405. separate from the machine-generated.
  1406. .PP
  1407. To make things simple, the default rules in the system
  1408. .CW mkfiles
  1409. include entries to make
  1410. .CW foo.acid
  1411. from
  1412. .CW foo.c ,
  1413. so one may use
  1414. .CW mk
  1415. to automate the production of
  1416. .CW acid
  1417. definitions for a given C source file.
  1418. .PP
  1419. There is much more to say here. See
  1420. .CW acid
  1421. manual page, the reference manual, or the paper
  1422. ``Acid: A Debugger Built From A Language'',
  1423. also by Phil Winterbottom.