home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 156_01 / cc.doc < prev    next >
Text File  |  1985-08-21  |  14KB  |  468 lines

  1.             C Compiler Documentation
  2.  
  3. INTRODUCTION 
  4.  
  5.     This compiler is the small C compiler written by Ron
  6. Cain and published in Dr. Dobb's #45 (May '80). The compiler
  7. was modified to include floating point by James R. Van Zandt.
  8. The floating point routines themselves were written by Niel
  9. Colvin.  The companion assembler ZMAC and linker ZLINK were
  10. written by Bruce Mallett.
  11.  
  12.     This compiler accepts a subset of standard C. It
  13. requires a Z-80 processor. It reads C source code and produces
  14. Zilog- mnemonic assembly language output, with syntax matching
  15. the assembler ZMAC supplied with it.  ZMAC produces a
  16. relocatable file with the extension .OBJ. One or more such
  17. relocatable files can be linkage edited by the companion
  18. program ZLINK. The compiler (with source in C), the assembler,
  19. and the linker (with sources in assembly language) are hereby
  20. placed in the public domain.
  21.  
  22.     There are several sample source code files on this
  23. disk. For more about the C programming language, see the above
  24. issue of Dr. Dobb's or "The C Programming Language" by B. W.
  25. Kernighan and D. M. Ritchie, Englewood Cliffs, N.J.:
  26. Prentice-Hall, 1978.
  27.  
  28.  
  29. DATA TYPES
  30.  
  31. The data types are...
  32.  
  33. char c        character 
  34. char *c        pointer to character 
  35. char c[3];    character array 
  36. int i;        16 bit integer 
  37. int *i;        pointer to integer 
  38. int i();    function returning integer 
  39. int i[4];    integer array 
  40. double d;    48 bit floating point 
  41. double *d;    pointer to double 
  42. double d();    function returning double 
  43. double d[5];    array of doubles 
  44.  
  45. Storage classes, structures, multidimensional arrays, unions,
  46. and more complex types like "int **i" are not included.
  47.  
  48.  
  49. PRIMARIES
  50.  
  51.     array[expression] 
  52.     function(arg1,arg2,...,argn) 
  53.     constant 
  54.         decimal integer 
  55.         decimal floating point (1.0, 2., .3, 340.2e-8)
  56.         quoted string    ("sample string") 
  57.         primed character ('a'  or  'Z') 
  58.     local variable 
  59.     global variable 
  60.  
  61. Each variable must be declared before it is used.
  62.  
  63.   
  64. UNARY INTEGER OPERATORS
  65.  
  66.     -    minus 
  67.     *    indirection 
  68.     &    address of 
  69.     ++    increment, either prefix or suffix 
  70.     --    decrement, either prefix or suffix 
  71.  
  72.  
  73. BINARY INTEGER OPERATORS
  74.  
  75.     +    addition 
  76.     -    subtraction 
  77.     *    multiplication 
  78.     /    division 
  79.     %    mod, remainder from division 
  80.     |    inclusive or 
  81.     ^    exclusive or 
  82.     &    logical and 
  83.     ==    equal 
  84.     !=    not equal 
  85.     <    less than 
  86.     <=    less than or equal 
  87.     >    greater than 
  88.     >=    greater than or equal 
  89.     <<    left shift 
  90.     >>    arithmetic right shift 
  91.     =    assignment 
  92.  
  93.  
  94. UNARY DOUBLE OPERATORS
  95.  
  96.     -    minus 
  97.  
  98.  
  99. BINARY DOUBLE OPERATORS
  100.  
  101.     +    add 
  102.     -    subtract 
  103.     *    multiply 
  104.     /    divide 
  105.     =    assignment 
  106.  
  107. Assignment operators are not included. 
  108.  
  109. Conversion between floating point and integer is automatic for
  110. assignment and for the expression returned by a function.
  111. Conversion from integer to floating point is automatic for the
  112. arguments of any of the floating point operators. Otherwise,
  113. the routines "float(jj)" and "ifix(yy)" (as in FORTRAN) may be
  114. used.  The arguments of integer-only operators are checked to
  115. be sure they are integers.  There is no type checking for the
  116. actual parameters of function calls.
  117.  
  118.  
  119. PROGRAM CONTROL
  120.  
  121.     if(expression) statement; 
  122.     if(expression) statement; else statement; 
  123.     while(expression) statement; 
  124.     break; 
  125.     continue; 
  126.     return;
  127.     return expression; 
  128.     ;                 /* null statement */
  129.     {statement;statement;   ...   statement;}
  130.                  /* compound statement */ 
  131.  
  132. not included: for, do while, switch, case, default, goto
  133.  
  134.  
  135. COMMAND LINES
  136.  
  137. When the compiler is run, it reads one or more C source files
  138. and produces one assembly language file. The assembly language
  139. files are separately assembled by ZMAC, then are combined into
  140. a single executable file by ZLINK. The format of the compiler
  141. command line is:
  142.  
  143.     cc [options] file [file file...]
  144.  
  145. Each option is a minus sign followed by a letter:
  146.  
  147.     -C    to include the C source code as comments in
  148.         the compiler-generated assembly code.
  149.  
  150.     -E    pause after an error is encountered.
  151.  
  152.     -G    don't reserve space for any global variables.
  153.  
  154.     -M    none of these files contains main().
  155.  
  156.     -P    produce profile of function calls, and enable
  157.         walkback trace on calls to err().
  158.  
  159. Use the -G option if all global variables are being declared in
  160. one compilation, but will be used by programs being compiled
  161. and assembled separately. (This is a rudimentary form of
  162. "extern".) If functions return doubles, they must be declared
  163. before use in each compilation. Otherwise, functions are
  164. automatically imported and exported. Names of functions and
  165. global variables i.e., those declared outside function
  166. definitions) are always global as far as the linker are
  167. concerned, and may not overlap.
  168.  
  169. The -M option keeps the compiler from producing its standard
  170. header (initializing the stack pointer, for example). The
  171. header does not include an ORG 100H directive, since ZLINK
  172. automatically starts programs at 100H. As a result, forgetting
  173. the -M option will lengthen your program by a few bytes but
  174. cause no other harm. 
  175.  
  176. The profile printout will include only the functions in the
  177. first file the linker sees. That's the one that should be
  178. compiled with the -P option. If there is a call to err() in
  179. IOLIB, a "walkback trace" is printed, which lists all the
  180. functions that have been called but which have not yet returned
  181. (recursive calls lead to multiple listings). The walkback trace
  182. will include all functions compiled with the -P option, whether
  183. or not they were in the first .OBJ file.
  184.  
  185. Options and files are separated by spaces, and can be mentioned
  186. in any order (even intermixed). Only the file name (optionally
  187. preceded by a disk name) should be given. The compiler
  188. automatically adds the extension ".C". The output file is given
  189. the same name (and is put onto the same disk) as the first
  190. input file, but with the extension ".ASM".
  191.  
  192. Each assembly language file is assembled as follows:
  193.  
  194.     zmac alpha=alpha
  195.  
  196. If extensions are not specified, as here, ZMAC uses ".ASM" for
  197. its input file and ".OBJ" for its output file.
  198.  
  199. The object files are linked as follows:
  200.  
  201.     zlink alpha,alpha=alpha,beta,iolib,float,printf
  202.  
  203. The first name is for the output file. By default, it is given
  204. the extension ".COM". The second name is for the map file
  205. (default extension ".MAP) which gives the values of all the
  206. global symbols. ZLINK will always tell you how many global
  207. symbols were undefined, but won't tell you what the undefined
  208. symbols were unless you ask for a map file. All the names to
  209. the right of the '=' are input files, with the default
  210. extension ".OBJ". The first input file must have been compiled
  211. WITHOUT the -M option. Ordinarily, it will be the one with
  212. main(). The other files can be mentioned in any order.
  213.  
  214.  
  215. EMBEDDED COMPILER COMMANDS
  216.  
  217.     #define name string
  218.  
  219. "name" is replaced by "string" hereafter. 
  220.  
  221.     #include filename 
  222.  
  223. compiler gets source code from another file (can't be nested) 
  224.  
  225.     #asm 
  226.     ... 
  227.     ... 
  228.     #endasm 
  229.  
  230. code between these is passed directly to the assembler.  
  231.  
  232.  
  233. LIBRARY FILES 
  234.  
  235. Seven library files are included: 
  236.  
  237. IOLIB.C        Basic input/output and integer math routines
  238. IOLIB.ASM    (required by ALL programs). 
  239. IOLIB.H     / 
  240.  
  241. FLOAT.C        Floating point math routines. 
  242. FLOAT.OBJ      / 
  243. FLOAT.H        / 
  244.  
  245. TRANSCEN.OBJ    Trancendental routines. 
  246. TRANSCEN.H    / 
  247.  
  248. PRINTF1.C    Output routine printf(), integer only. 
  249. PRINTF1.OBJ      / 
  250. PRINTF1.H     / 
  251.  
  252. PRINTF2.C    Output routine printf(), 
  253. PRINTF2.OBJ     integer & floating point 
  254. PRINTF2.H    / 
  255.  
  256. PROFILE.ASM    Prints profile (number of calls to each
  257. PROFILE.OBJ    function) after program finishes, and enables
  258. PROFILE.H    walkback trace in case of error (calls to 
  259.         err(), in IOLIB).
  260.  
  261. ARGS.C        I/O redirection and command line parsing.
  262. ARGS.OBJ     / 
  263. ARGS.H        / 
  264.  
  265. These are related as follows: 
  266.  
  267.         IOLIB
  268.            / | \  \
  269.     PRINTF1     |   \  ARGS
  270.               PROFILE  \
  271.                       FLOAT
  272.                      /     \
  273.               PRINTF2       TRANSCEN
  274.  
  275. That is, PRINTF2 requires both FLOAT and IOLIB. PRINTF1 or
  276. PROFILE require only IOLIB. TRANSCEN requires both FLOAT and
  277. IOLIB. 
  278.  
  279. In each case, the .C file was compiled and assembled to give
  280. the corresponding .OBJ file, and the .H file declares (for the
  281. assembler, and if necessary for the compiler as well) the
  282. exported symbols.
  283.  
  284. Each library to be used must be mentioned twice: once to the
  285. compiler (either in an #include statement in the source code or
  286. in a reply to the "input file" question), and again to the
  287. linker (either in the command line or in a reply to the linker
  288. prompt). For example, if floating point operations are needed,
  289. the source file should contain:
  290.  
  291.     #include iolib.h 
  292.     #include float.h 
  293.     double x; 
  294.     ...            (rest of source code) 
  295.  
  296. Compilation, assembly, and linking would consist of:
  297.  
  298.     A>cc alpha
  299.     A>zmac alpha=alpha 
  300.     A>zlink alpha=alpha,iolib,float 
  301.  
  302. For details on these libraries, see IOLIB.DOC, FLOAT.DOC,
  303. TRANSCEN.DOC, PRINTF.DOC, PROFILE.DOC, and ARGS.DOC.
  304.  
  305.  
  306. SAMPLE COMPILATION
  307.  
  308.     C>cc test
  309.     
  310.     * * *  Small-C  V1.2  * * *
  311.     
  312.            By Ron Cain  and  James Van Zandt
  313.     
  314.          29 July 1984
  315.     
  316.     
  317.     TEST.C                <file names echoed>
  318.     
  319.     #include iolib.h        <included files echoed>
  320.     #end include
  321.     #include float.h
  322.     #end include
  323.     #include printf2.h
  324.     #end include
  325.     #include args.h
  326.     #end include
  327.     #include transcen.h
  328.     #end include
  329.     ====== main()            <function names echoed>
  330.     ====== out()
  331.     ====== alpha()
  332.     ====== beta()
  333.     ====== gamma()
  334.     ====== putnum()
  335.     ====== outf()
  336.     
  337.     There were 0 errors in compilation.
  338.     
  339.     C>zmac test=test
  340.     SSD RELOCATING   (AND EVENTUALLY MACRO)  Z80 ASSEMBLER
  341. VER 1.07
  342.     
  343.         0  ERRORS
  344.     C>zlink
  345. test,test=test,a:args,iolib,float,printf2,a:transcen
  346.     SSD LINKAGE EDITOR VERSION 1.4
  347.           0 UNDEFINED SYMBOL(S).
  348.     C> 
  349.  
  350.  
  351. SAMPLE COMPILER COMPILATION 
  352.  
  353.     C>cc c80v 
  354.     C>zmac c80v3=c80v        <pick a different name>
  355.     C>zlink c80v3,c80v3=c80v3,iolib,float           <link it>
  356.   
  357. The assembly files total about 190K. The object files total
  358. about 65K, and the temporary file produced by the linker is
  359. about 80K.  Compilation requires about 43K of RAM, divided as
  360. follows: 34K code & data, 8K heap (mostly symbol table), and 1K
  361. stack.
  362.  
  363.  
  364. PERFORMANCE
  365.  
  366. The program test.c on this disk (with 142 lines and 3200 bytes)
  367. was compiled in 47.8 sec on a 4 MHz Z-80, for a compilation
  368. speed of approximately 3 lines/sec, or 67 bytes/sec. Of this
  369. time, approximately 7 seconds was spent reading in the 43136
  370. bytes of compiler code.
  371.  
  372. The following floating point benchmark (from Dr. Dobb's
  373. Journal, Mar 84) finished in 489 seconds on a 4 MHz Z-80, with
  374. the result 2500.00469:
  375.  
  376.     #include iolib.h
  377.     #include float.h
  378.     #include transcen.h
  379.     #include printf2.h
  380.     int i;
  381.     double a;
  382.     main()
  383.     {    a=1.; i=1;
  384.         printf("starting\n");
  385.         while(i++<2500)
  386.             {a=tan(atan(exp(ln(sqrt(a*a)))))+1.;}
  387.         printf("Result is %20.12f ",a);
  388.     }
  389.  
  390.  
  391. INTERNAL DOCUMENTATION 
  392.  
  393. This is a recursive descent, one pass compiler producing
  394. assembly language.  The two major changes that have been made
  395. to speed it up relative to Ron Cain's original compiler are a
  396. hash coded symbol table and 1K disk buffers.
  397.  
  398. Global symbols defined in a C program are prefixed by 'Q' so
  399. they do not conflict with any global symbols in libraries.
  400.  
  401. A compiled program has the following layout...
  402.  
  403.     100H to _end-1        program & data
  404.     _end to *heaptop    heap
  405.     *heaptop to *SP-1    unused
  406.     *SP to *(0006)        stack
  407.  
  408. The symbol _end is defined by ZLINK at link time, and points to
  409. the first byte above program and data.  The variable heaptop
  410. (initialized to _end) always points to the first byte above the
  411. heap.  The stack pointer register SP is initialized to the
  412. first location above user memory (pointed to by the word at
  413. 0006). The variable names _end and heaptop are visible only to
  414. assembly language programs, since they do not begin with Qs.
  415.  
  416. Assembly language programs can be called by C programs.  The C
  417. program will evaluate each of its arguments (left to right),
  418. push it onto the stack, then call the named program. Therefore,
  419. the assembly language program should expect that the top word
  420. on the stack is the return address, the next word is the last
  421. argument, etc. If the function is to return an integer or
  422. character value, it should be left in the HL register. Double
  423. values should be left in the 6 byte area starting at FA.
  424.  
  425.  
  426. WARNINGS 
  427.  
  428. In addition to the limitations mentioned above, the user should
  429. be aware of the following... 
  430.  
  431. In declaring a function returning a double, you have to use two
  432. lines... 
  433.  
  434.     double frodo(); 
  435.     frodo(x,y,z) int x; double y,z; 
  436.  
  437. You can't combine them, as in standard C... 
  438.  
  439.     double frodo(x,y,z) ...        /* not accepted */ 
  440.  
  441. It addition, the declaration "double frodo();" must appear
  442. before any use of the function, or the compiler will assume
  443. while generating the calling code that the function returns an
  444. integer. 
  445.  
  446. The floating point routines in the FLOAT library (though none
  447. of the code produced by the compiler) use several of the
  448. undocumented Z-80 op codes, so they may not work on some
  449. processors. FLOAT also uses the alternate register set. 
  450.  
  451. The floating point routines do not meet the proposed IEEE
  452. standard. 
  453.  
  454.  
  455. POTENTIAL IMPROVEMENTS 
  456.  
  457. Adding the features noted above as "not included". 
  458.  
  459. The compiler could probably be made faster by tokenizing the
  460. input stream, so parsing would not involve string comparisons.
  461. It could be made smaller by moving the error messages to a disk
  462. file, to be consulted at need.  Implementing storage class
  463. specifiers could make compiled programs (and the compiler
  464. itself) smaller and faster, since variables local to a function
  465. that is not called recursively could be declared "static".
  466. Static variables can be accessed much more readily than "auto"
  467. variables (those on the stack).
  468.