SDCC Compiler User Guide

Transcription

SDCC Compiler User GuideSDCC 4.1.6 Date:: 2021-06-08 # Revision: 12437

Contents123Introduction1.1 About SDCC . . . . . . . . . . . .1.2 SDCC Suite Licenses . . . . . . . .1.3 Documentation . . . . . . . . . . .1.4 Typographic conventions . . . . . .1.5 Compatibility with previous versions1.6 System Requirements . . . . . . . .1.7 Other Resources . . . . . . . . . . .7789991112Installing SDCC2.1 Configure Options . . . . . . . . . . . . . . . . . . . . . . . . . . .2.2 Install paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.3 Search Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.4 Building SDCC . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.4.1 Building SDCC on Linux . . . . . . . . . . . . . . . . . .2.4.2 Building SDCC on Mac OS X . . . . . . . . . . . . . . . .2.4.3 Cross compiling SDCC on Linux for Windows . . . . . . .2.4.4 Building SDCC using Cygwin and Mingw32 . . . . . . . .2.4.5 Building SDCC Using Microsoft Visual C 2010 (MSVC)2.4.6 Windows Install Using a ZIP Package . . . . . . . . . . . .2.4.7 Windows Install Using the Setup Program . . . . . . . . . .2.4.8 VPATH feature . . . . . . . . . . . . . . . . . . . . . . . .2.5 Building the Documentation . . . . . . . . . . . . . . . . . . . . .2.6 Reading the Documentation . . . . . . . . . . . . . . . . . . . . .2.7 Testing the SDCC Compiler . . . . . . . . . . . . . . . . . . . . .2.8 Install Trouble-shooting . . . . . . . . . . . . . . . . . . . . . . . .2.8.1 If SDCC does not build correctly . . . . . . . . . . . . . . .2.8.2 What the ”./configure” does . . . . . . . . . . . . . . . . .2.8.3 What the ”make” does . . . . . . . . . . . . . . . . . . . .2.8.4 What the ”make install” command does. . . . . . . . . . . .2.9 Components of SDCC . . . . . . . . . . . . . . . . . . . . . . . .2.9.1 sdcc - The Compiler . . . . . . . . . . . . . . . . . . . . .2.9.2 sdcpp - The C-Preprocessor . . . . . . . . . . . . . . . . .2.9.3 sdas, sdld - The Assemblers and Linkage Editors . . . . . .2.9.4 s51, sz80, shc08, sstm8 - The Simulators . . . . . . . . . .2.9.5 sdcdb - Source Level Debugger . . . . . . . . . . . . . . 52525Using SDCC3.1 Standard-Compliance . . . . . .3.1.1 ISO C90 and ANSI C893.1.2 ISO C95 . . . . . . . .3.1.3 ISO C99 . . . . . . . .3.1.4 ISO C11 and ISO C17 .3.1.5 ISO C2X . . . . . . . .3.1.6 Embedded C . . . . . .2626262727272727.1.

fined behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.7.1 Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.7.2 Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.7.3 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.7.4 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.7.5 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.7.6 Floating point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.7.7 Arrays and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.7.8 Hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.7.9 Structures, unions, enumerations and bit-fields . . . . . . . . . . . . . . . . .3.1.7.10 Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.7.11 Preprocessing directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.7.12 Library functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.1.7.13 Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Compiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.2.1 Single Source File Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.2.2 Postprocessing the Intel Hex file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.2.3 Projects with Multiple Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.2.4 Projects with Additional Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.2.5 Using sdar to Create and Manage Libraries . . . . . . . . . . . . . . . . . . . . . . . .Command Line Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.1 Processor Selection Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.2 Preprocessor Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.3 Optimization Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.4 Other Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.5 Linker Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.6 MCS51 Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.7 DS390 / DS400 Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.8 Options common to all z80-related ports (z80, z180, r2k, r3ka, gbz80, tlcs90, ez80 z80)3.3.9 Z80 Options (apply to z80, z180, r2k, r3ka, tlcs90, ez80 z80) . . . . . . . . . . . . . .3.3.10 GBZ80 Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.11 STM8 Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.12 Intermediate Dump Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.3.13 Redirecting output on Windows Shells . . . . . . . . . . . . . . . . . . . . . . . . . . .Environment variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .SDCC Language Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.1 MCS51/DS390 intrinsic named address spaces . . . . . . . . . . . . . . . . . . . . . .3.5.1.1 data / near . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.1.2 xdata / far . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.1.3 idata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.1.4 pdata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.1.5 code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.1.6 bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.1.7 sfr / sfr16 / sfr32 / sbit . . . . . . . . . . . . . . . . . . . . . . . .3.5.1.8 Pointers to MCS51/DS390 intrinsic named address spaces . . . . . . . . . . .3.5.1.9 Notes on MCS51 memory layout . . . . . . . . . . . . . . . . . . . . . . . .3.5.2 Z80/Z180/eZ80 intrinsic named address spaces . . . . . . . . . . . . . . . . . . . . . .3.5.2.1 sfr (in/out to 8-bit addresses) . . . . . . . . . . . . . . . . . . . . . . . . .3.5.2.2 banked sfr (in/out to 16-bit addresses) . . . . . . . . . . . . . . . . . . .3.5.2.3 sfr (in0/out0 to 8 bit addresses on Z180/HD64180) . . . . . . . . . . . . .3.5.3 HC08/S08 intrinsic named address spaces . . . . . . . . . . . . . . . . . . . . . . . . .3.5.3.1 data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.3.2 xdata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.4 PDK14/PDK15 intrinsic named address spaces . . . . . . . . . . . . . . . . . . . . . .3.5.4.1 sfr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.4.2 sfr16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343434344444444

17CONTENTS3.5.5 Non-intrinsic named address spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.6 Absolute Addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.7 Preserved register specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.8 Binary constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.9 Returning void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.5.10 Omitting promotion on arguments of vararg function (does not apply to pdk13, pdk14, pdk15)Parameters and Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Overlaying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Interrupt Service Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.8.1 General Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.8.1.1 Common interrupt pitfall: variable not declared volatile . . . . . . . . . . . . .3.8.1.2 Common interrupt pitfall: non-atomic access . . . . . . . . . . . . . . . . . . .3.8.1.3 Common interrupt pitfall: stack overflow . . . . . . . . . . . . . . . . . . . . .3.8.1.4 Common interrupt pitfall: use of non-reentrant functions . . . . . . . . . . . .3.8.2 MCS51/DS390 Interrupt Service Routines . . . . . . . . . . . . . . . . . . . . . . . . . .3.8.3 HC08 Interrupt Service Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.8.4 Z80, Z180 and eZ80 Interrupt Service Routines . . . . . . . . . . . . . . . . . . . . . . .3.8.5 Rabbit 2000, 3000 and 3000A Interrupt Service Routines . . . . . . . . . . . . . . . . . .3.8.6 GBZ80 and TLCS-90 Interrupt Service Routines . . . . . . . . . . . . . . . . . . . . . .3.8.7 STM8 Interrupt Service Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Enabling and Disabling Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.9.1 Critical Functions and Critical Statements . . . . . . . . . . . . . . . . . . . . . . . . . .3.9.2 Enabling and Disabling Interrupts directly . . . . . . . . . . . . . . . . . . . . . . . . . .3.9.3 Semaphore locking (mcs51/ds390) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Functions using private register banks (mcs51/ds390) . . . . . . . . . . . . . . . . . . . . . . . .Inline Assembler Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.11.1 Inline Assembler Code Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.11.1.1 Old asm . endasm; Format . . . . . . . . . . . . . . . . . . . . . . . . .3.11.1.2 New asm (”inline assembler code”) Format . . . . . . . . . . . . . . . .3.11.2 A Step by Step Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.11.3 Naked Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.11.4 Use of Labels within Inline Assembler . . . . . . . . . . . . . . . . . . . . . . . . . . . .Support routines for integer multiplicative operators . . . . . . . . . . . . . . . . . . . . . . . . .Floating Point Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Library Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.14.1 Compiler support routines ( gptrget, mulint etc.) . . . . . . . . . . . . . . . . . . . . .3.14.2 Stdclib functions (puts, printf, strcat etc.) . . . . . . . . . . . . . . . . . . . . . . . . . .3.14.2.1 stdio.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.14.2.2 malloc.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.14.3 Math functions (sinf, powf, sqrtf etc.) . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.14.3.1 math.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.14.4 Other libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Memory Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.15.1 MCS51 Memory Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.15.1.1 Small, Medium, Large and Huge . . . . . . . . . . . . . . . . . . . . . . . . .3.15.1.2 External Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.15.2 DS390 Memory Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3.15.3 STM8 Memory Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Pragmas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Defines Created by the Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

CONTENTS4CONTENTSNotes on supported Processors4.1 MCS51 variants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.1.1 pdata access by SFR . . . . . . . . . . . . . . . . .

18.05.2021 · Introduction 1.1 About SDCC 7