This chapter contains the following sections:
Introduction to 8051 C Cross-Compiler
Product Definition
General Implementation
Compiler Phases
Frontend Optimizations
Compiler Structure
Environment Variables
Sample Session
Using EDE
Using the Makefile
This manual provides a functional description of the TASKING 8051 C Cross-Compiler. This manual uses cc51 (the name of the binary) as a shorthand notation for "TASKING 8051 C Compiler".
TASKING offers a complete toolchain for the 8051 family of microcontroller units and their derivatives. '8051' is used as a shorthand notation for this microcontroller. The toolchain contains a C compiler, a macro preprocessor, an assembler, a linker/locator, a library manager and a debugger.
Unlike other C-8051 compilers, cc51 is not a general C compiler, adapted for the 8051, but it is dedicated to the microcontroller architecture of the 8051. This means that you can access all special features of the 8051 in C: multiple address spaces (with full pointer support), bit memory, special function registers (I/O ports), interrupt functions using bank switching and a number of built-in (inline) functions to access special 8051 instructions. And yet no compromise is made to the ANSI standard. It is a fast, single pass, optimizing compiler that generates extremely fast and compact code.
cc51 generates assembly source code using the Intel assembly language specification, which can be assembled with the TASKING 8051 Cross-Assembler (In this document we use asm51 as a shorthand notation for 'TASKING 8051 Cross-Assembler').
You can link the generated object with other objects and libraries by using the TASKING link51 linker/locator (In this document we use link51 as a shorthand notation for 'TASKING link51 linker/locator'). The software written in C can be debugged using a TASKING high-level language debugger. A list of supported platforms and emulators is available from TASKING.
TASKING C 8051 Family Cross-Compiler (cc51)
TK008-002
TASKING 8051 Cross-Assembler (included with TK008-002)
TASKING 8051 CrossView Debugger
ROM Monitor version (TK008-041)
Simulator version (included with TK008-002)
All 8051 derivatives. Special function registers can be accessed by means of user-definable 'sfrfile'.
This section describes the different phases of the compiler and the target independent optimizations.
During the compilation of a C program, a number of phases can be identified. These phases are divided into two groups, referred to as frontend and backend.
The preprocessor phase:
The scanner phase:
The parser phase:
The frontend optimization phase:
The backend optimization phase:
The code generator phase:
The peephole optimizer phase:
All phases (of both frontend and backend) are combined into one program: cc51. The compiler does not use any intermediate file for communication between the different phases of compilation. The backend part is not called for each C statement, but is started after a complete C function has been processed by the frontend (in memory), thus allowing more optimization. The compiler only requires one pass over the input file, resulting in relatively fast compilation.
The compiler performs the following optimizations on the intermediate code. They are independent of the target processor and the code generation strategy:
Expressions only involving constants are replaced by their result.
Expressions are rearranged to allow more constant folding. E.g. 1+ (x-3) is transformed into x + (1-3), which can be folded.
Multiplication by 0 or 1 and additions or subtractions of 0 are removed. Such useless expressions may be introduced by macros in C (#define), or by the compiler itself.
Expressions involving '&&', '||' and '!' are interpreted and translated into a series of conditional jumps.
With for and while loops, the expression is evaluated once at the 'top' and then at the 'bottom' of the loop. This optimization does not save code, but speeds up execution.
A number of optimizations of a switch statement are performed, such as the deletion of redundant case labels or even the deletion of the switch.
By reversing jump conditions and moving code, the number of jump instructions is minimized. This reduces both the code size and the execution time.
A conditional or unconditional jump to a label which is immediately followed by an unconditional jump may be replaced by a jump to the destination label of the second jump. This optimization does not save code, but speeds up execution.
An unconditional jump to a label directly following the jump is removed. A conditional jump to such a label is replaced by an evaluation of the jump condition. The evaluation is necessary because it may have side effects.
A conditional jump over an unconditional jump is transformed into one conditional jump with the jump condition reversed. This reduces both the code size and the execution time.
A reference to a variable with known contents is replaced by those contents.
The compiler has the ability to detect repeated uses of the same (sub-) expression. Such a "common" expression may be temporarily saved to avoid recomputation. This method is called common subexpression elimination, abbreviated CSE.
Unreachable code can be removed from the intermediate code without affecting the program. However, the compiler generates a warning message, because the unreachable code may be the result of a coding error.
Invariant expressions may be moved out of a loop and expressions involving an index variable may be reduced in strength.
Eliminate short loops by replacing them with a number of copies.
String literals and floating point constants are put in ROM memory. The compiler overlays identical strings (within the same module) and let them share the same space, thus saving ROM space. Likewise identical floating point constants are overlaid and allocated only once.
If you want to build a C-51 application you need to invoke the following programs directly, or via the control program:
The next figure explains the relationship between the different parts of the TASKING 8051 toolchain:
Figure 2-1: 8051 development flow
The ihex51 program formats the a.out file into an Intel Hex format file. You can load this output file into an EPROM programmer.
The omf51 program formats the a.out file into a (full symbol) absolute Intel OMF51 format file. This output file can be loaded into a debugger. See the appendices for examples.
The ar51 program is a librarian facility. You can use this program to create and maintain object libraries.
The programs ieee51, ihex51, omf51 and ar51 are delivered with the asm51/link51 package.
For a full description of all available formatter programs and other utilities, we refer to the 8051 Cross-Assembler User's Guide.
The name of the 8051 CrossView Pro Debugger is xfw51. For more information check the 8051 CrossView Pro Debugger User's Guide.
This section contains an overview of the environment variables used by the 8051 toolchain.
Environment Variable | Description |
ASMDIR | Specifies an alternative path for include files for the macro preprocessor mpp51. |
CC51INC | Specifies an alternative path for #include files for the C compiler cc51. |
CC51LIB | Specifies an alternative path for library files for the linker link51. |
LM_LICENSE_FILE | Identifies the location of the license data file. Only needed for hosts that need the FLEXlm license manager. |
PATH | Specifies the search path for your executables. |
TMPDIR | Specifies an alternative directory where programs can create temporary files. |
Table 2-1: Environment variables
The subdirectory message in the examples subdirectory contains a demo program for the 8051 toolchain.
In order to debug your programs, you will have to compile, assemble, link and locate them for debugging using the TASKING 8051 tools. You can do this with EDE, the Embedded Development Environment (which uses a project file and a makefile) or you can call the makefile from the command line.
EDE stands for "Embedded Development Environment" and is the Windows oriented Integrated Development Environment you can use with your TASKING toolchain to design and develop your application.
To use EDE on the demo program in the subdirectory message in the examples subdirectory of the 8051 product tree follow the steps below.
A detailed description of the process using the sample program message.c is described below. This procedure is outlined as a guide for you to build your own executables for debugging.
The dialog boxes shown in this manual serve as an example. They may slightly differ from the ones in your product.
You can launch EDE by double-clicking on the EDE shortcut on your desktop.
The EDE screen provides you with a menu bar, a toolbar (command buttons) and one or more windows (for example, for source files), a status bar and numerous dialog boxes.
EDE supports all the TASKING toolchains. When you first start EDE, the toolchain of the product you purchased is selected and displayed in the title of the EDE desktop window.
If you have more than one TASKING product installed and you want to change toolchains, do the following::
1. From the Project menu, select Select Toolchain...
The Select Toolchain dialog appears.
2. Select the toolchain you want. You can do this by clicking on a toolchain in the Toolchains list box and click OK.
If no toolchains are present, use the Browse... or Scan Disk... button to search for a toolchain directory. Use the Browse... button if you know the installation directory of another TASKING product. Use the Scan Disk... button to search for all TASKING products present on a specific drive. Then return to step 2.
Follow these steps to open an existing project:
1. From the Project menu, select Set Current ->.
2. Select the project file to open. For the demo program select the file message.pjt in the subdirectory message in the examples subdirectory of the 8051 product tree. If you have used the defaults, the file message.pjt is in the directory c:\cc51\examples\message.
The next two steps are not needed for the demo program because the files message.c and makefile are already open. To load the file you want to look at.
1. From the Project menu, select Load Files...
The Choose Project Files to Edit dialog appears.
2. Choose the file(s) you want to open by clicking on it. You can select multiple files by pressing the <Ctrl> or <Shift> key while you click on a file. With the <Ctrl> key you can make single selections and with the <Shift> key you can select everything from the first selected file to the file you click on. Then click OK.
This launches the file(s) so you can edit it (them).
1. From the Project menu, select Directories....
The Directories dialog appears.
2. Check the directory paths for programs, include files and libraries. You can add your own directories here, separated by semicolons.
3. Click OK.
The next step is to compile the file(s) together with its dependent files so you can debug the application.
Steps 1 and 2 are optional. Follow these steps if you want to specify additional build options such as to stop the build process on errors and to keep temporary files that are generated during a build.
1. From the Build menu, select Options...
The Build Options dialog appears.
2. Make your changes and press the OK button.
3. From the Build menu, select Scan All Dependencies.
4. Click on the Execute 'Make' command button. The following button is the execute Make button which is located in the toolbar.
If there are any unsaved files, EDE will ask you in a separate dialog if you want to save them before starting the build.
Once the files have been processed you can inspect the generated messages in the Build tab:
TASKING program builder vx.yrz Build nnn SN 00000000 Compiling demo.c Assembling demo.src Assembling addone.src Macro preprocessing sim_cstart.asm Assembling sim_cstart.src Linking to sim.out Creating IEEE-695 absolute file sim.abs
Once the files have been compiled, assembled, linked, located and formatted they can be executed by CrossView Pro.
To execute CrossView Pro:
1. Click on the Debug application button. The following button is the Debug application button which is located in the toolbar.
CrossView Pro is launched. CrossView Pro will automatically download the compiled file for debugging.
When you use CrossView Pro ROM for the first time, you must setup the communication parameters.
To set the communication parameters:
1. From the File menu, select Communication Setup....
The Communication Setup dialog appears.
2. In this dialog you need to identify the COM port (probably COM1: or COM2:) and the baud rate (9600 for RISM).
3. Click OK to close the dialog.
You must tell CrossView Pro which program you want to debug. To do this:
1. From the File menu, select Load Symbolic Debug Info....
The Load Symbolic Debug Info dialog box appears.
2. Click Load.
To view your source while debugging, the Source Window must be open. To open this window,
1. From the View menu, select Source->Source lines.
Before starting execution you have to reset the target system to its initial state. The program counter, stack pointer and any other registers must be set to their initial value. The easiest way to do this is:
2. From the Run menu, select Reset Target System.
To run your application step-by-step:
3. From the Run menu, select Animate.
The program message.abs is now stepping through the high level language statements. Using the Accelerator bar or the menu bar you can set breakpoints, monitor data, display registers, simulate I/O and much more. See the CrossView Pro Debugger User's Guide for more information.
When you first use EDE you need to setup a project space and add a new project:
1. From the File menu, select New Project Space...
The Create a New Project Space dialog appears.
2. Give your project space a name and then click OK.
The Project Properties dialog box appears.
3. Click on the Add new project to project space button.
The Add New Project to Project Space dialog appears.
4. Give your project a name and then click OK.
The Project Properties dialog box then appears for you to identify the files to be added.
5. Add all the files you want to be part of your project. Then press the OK button. To add files, use one of the 3 methods described below.
The new project is now open.
6. From the Project menu, select Load Files... to open the files you want on your EDE desktop.
EDE automatically creates a makefile for the project. EDE updates the makefile every time you modify your project.
The subdirectories in the examples directory each contain a makefile which can be processed by mk51. Also each subdirectory contains a readme.txt file with a description of how to build the example.
To build the mesage demo example follow the steps below. This procedure is outlined as a guide for you to build your own executables for debugging.
1. Make the subdirectory message of the examples directory the current working directory.
This directory contains a makefile for building the demo example. It uses the default mk51 rules.
2. Be sure that the directory of the binaries is present in the PATH environment variable.
3. Compile, assemble, link and locate the modules using one call to the program builder mk51:
mk51
This command will build the example using the file makefile.
To see which commands are invoked by mk51 without actually executing them, type:
mk51 -n
To remove all generated files type:
mk51 clean