281
PIC18 IAR C/C++ Compiler Reference Guide for Microchip® Technology Inc’s PIC18 Microcontroller

PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

  • Upload
    others

  • View
    33

  • Download
    0

Embed Size (px)

Citation preview

Page 1: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

for Microchip® Technology Inc’sPIC18 Microcontroller

Page 2: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

CPIC18-3

COPYRIGHT NOTICE© Copyright 2000–2005 IAR Systems. All rights reserved.

No part of this document may be reproduced without the prior written consent of IAR Systems. The software described in this document is furnished under a license and may only be used or copied in accordance with the terms of such a license.

DISCLAIMERThe information in this document is subject to change without notice and does not represent a commitment on any part of IAR Systems. While the information contained herein is assumed to be accurate, IAR Systems assumes no responsibility for any errors or omissions.

In no event shall IAR Systems, its employees, its contractors, or the authors of this document be liable for special, direct, indirect, or consequential damage, losses, costs, charges, claims, demands, claim for lost profits, fees, or expenses of any nature or kind.

TRADEMARKSIAR Systems, From Idea to Target, IAR Embedded Workbench, visualSTATE, IAR MakeApp and C-SPY are trademarks owned by IAR Systems AB.

Microchip® and PICmicro are registered trademarks of Microchip® Technology Inc.

Microsoft and Windows are registered trademarks of Microsoft Corporation.

All other product names are trademarks or registered trademarks of their respective owners.

EDITION NOTICE

Third edition: April 2005

Part number: CPIC18-3

This guide applies to version 3.x of PIC18 IAR Embedded Workbench®.

Page 3: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Brief contentsTables ...................................................................................................................... xv

Preface .................................................................................................................. xvii

Part 1. Using the compiler ......................................................... 1

Getting started .................................................................................................... 3

Data storage ...................................................................................................... 11

Functions ............................................................................................................. 23

Placing code and data .................................................................................... 29

The DLIB runtime environment ............................................................... 45

The CLIB runtime environment .............................................................. 77

Assembler language interface ................................................................... 85

Using C++ ............................................................................................................ 99

Efficient coding for embedded applications ...................................... 109

Part 2. Compiler reference .................................................... 121

Data representation ...................................................................................... 123

Segment reference ......................................................................................... 131

Compiler options ........................................................................................... 147

Extended keywords ....................................................................................... 175

Pragma directives ............................................................................................ 183

The preprocessor ........................................................................................... 193

Intrinsic functions ........................................................................................... 201

Library functions ............................................................................................. 207

CPIC18-3

iii

Page 4: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

iv

Implementation-defined behavior .......................................................... 215

IAR language extensions ............................................................................. 229

Diagnostics ......................................................................................................... 239

Index ..................................................................................................................... 241

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 5: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

ContentsTables ...................................................................................................................... xv

Preface .................................................................................................................. xvii

Who should read this guide .............................................................xvii

How to use this guide ........................................................................xvii

What this guide contains ..................................................................xviii

Other documentation ..........................................................................xix

Further reading ..................................................................................xix

Document conventions ........................................................................ xx

Typographic conventions ................................................................... xx

Part 1. Using the compiler ......................................................... 1

Getting started .................................................................................................... 3

IAR language overview ........................................................................... 3

Supported PIC18 derivatives ............................................................... 4

Building applications—an overview .................................................. 4

Compiling ............................................................................................. 4

Linking ................................................................................................. 4

Basic settings for project configuration ......................................... 5

Data model ........................................................................................... 5

Code model .......................................................................................... 6

Optimization for speed and size ........................................................... 6

Runtime environment ........................................................................... 6

Special support for embedded systems .......................................... 8

Extended keywords .............................................................................. 8

Pragma directives ................................................................................. 9

Predefined symbols .............................................................................. 9

Special function types .......................................................................... 9

Header files for I/O .............................................................................. 9

Accessing low-level features ............................................................... 9

CPIC18-3

v

Page 6: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

vi

Data storage ...................................................................................................... 11

Introduction .............................................................................................. 11

Data models .............................................................................................. 12

Specifying a data model ..................................................................... 12

Memory types .......................................................................................... 12

Nonbanked memory .......................................................................... 13

Banked memory ................................................................................ 13

EEPROM .......................................................................................... 14

External memory ............................................................................... 14

Using data memory attributes ............................................................ 15

Pointers and memory types ................................................................ 16

Structures and memory types ............................................................ 17

More examples ................................................................................... 17

C++ and memory types ...................................................................... 18

Auto variables on the stack and in overlay frames .................. 19

The stack ........................................................................................... 19

Static overlay ..................................................................................... 20

Dynamic memory on the heap ........................................................ 20

Functions ............................................................................................................. 23

Code models ............................................................................................ 23

Using function memory attributes ...................................................... 24

Function directives ............................................................................. 24

Special function types .......................................................................... 24

Interrupt functions ............................................................................. 25

Monitor functions ............................................................................... 25

C++ and special function types ......................................................... 28

Placing code and data .................................................................................... 29

Segments and memory ........................................................................ 29

What is a segment? ............................................................................ 29

Placing segments in memory ............................................................ 30

Customizing the linker command file ............................................... 31

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 7: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Contents

Data segments ......................................................................................... 33

Static memory segments ................................................................... 33

The overlay system ........................................................................... 36

The stack ........................................................................................... 36

The heap ............................................................................................ 37

Located data ....................................................................................... 38

Code segments ....................................................................................... 38

Startup code ....................................................................................... 39

Normal code ....................................................................................... 39

Interrupt vectors ................................................................................ 39

C++ dynamic initialization ................................................................. 39

Efficient usage of segments and memory .................................... 39

Controlling data and function placement ........................................... 40

Creating user-defined segments ......................................................... 41

Verifying the linked result of code and data placement ........ 41

Segment too long errors and range errors .......................................... 42

Linker map file ................................................................................... 42

Managing multiple address spaces ..................................................... 43

The DLIB runtime environment ............................................................... 45

Introduction to the runtime environment .................................. 45

Runtime environment functionality ................................................... 45

Library selection ................................................................................ 46

Situations that require library building .............................................. 46

Library configurations ....................................................................... 47

Debug support in the runtime library ................................................. 47

Using a prebuilt library ........................................................................ 48

Customizing a prebuilt library without rebuilding ............................. 50

Choosing formatters for printf and scanf ..................................... 50

Choosing printf formatter ................................................................... 50

Choosing scanf formatter .................................................................. 51

Overriding library modules ................................................................ 52

Building and using a customized library ....................................... 54

Setting up a library project ................................................................. 54

CPIC18-3

vii

Page 8: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

viii

Modifying the library functionality .................................................... 54

Using a customized library ................................................................ 55

System startup and termination ...................................................... 56

System startup .................................................................................... 57

System termination ............................................................................ 57

Customizing system initialization ................................................... 58

__low_level_init ............................................................................... 58

Modifying the file cstartup.s49 ........................................................ 58

Standard streams for input and output ........................................ 59

Implementing low-level character input and output .......................... 59

Configuration symbols for printf and scanf ................................. 60

Customizing formatting capabilities .................................................. 61

File input and output ............................................................................. 62

Locale ........................................................................................................... 62

Locale support in prebuilt libraries .................................................... 63

Customizing the locale support .......................................................... 63

Changing locales at runtime ............................................................... 64

Environment interaction ..................................................................... 65

Signal and raise ........................................................................................ 65

Time ............................................................................................................. 66

Strtod ........................................................................................................... 66

Assert ........................................................................................................... 67

C-SPY Debugger runtime interface .............................................. 67

Low-level debugger runtime interface ............................................... 68

The debugger terminal I/O window ................................................... 68

Checking module consistency ........................................................... 68

Runtime model attributes .................................................................. 69

Using runtime model attributes .......................................................... 69

Predefined runtime attributes ............................................................. 70

User-defined runtime model attributes .............................................. 71

Implementation of system startup code ...................................... 72

Modules and segment parts ................................................................ 72

Added C functionality ........................................................................... 74

stdint.h ................................................................................................ 74

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 9: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Contents

stdbool.h ............................................................................................. 74

math.h ................................................................................................. 74

stdio.h ................................................................................................. 74

stdlib.h ................................................................................................ 75

printf, scanf and strtod ....................................................................... 75

The CLIB runtime environment .............................................................. 77

Runtime environment .......................................................................... 77

Input and output ..................................................................................... 78

Character-based I/O ........................................................................... 78

Formatters used by printf and sprintf ................................................. 79

Formatters used by scanf and sscanf .................................................. 80

System startup and termination ...................................................... 81

System startup .................................................................................... 81

System termination ............................................................................ 81

Overriding default library modules ................................................ 82

Customizing system initialization ................................................... 82

Implementation of cstartup .............................................................. 82

C-SPY runtime interface .................................................................... 82

The debugger terminal I/O window ................................................... 82

Termination ........................................................................................ 83

Checking module consistency ........................................................... 83

Assembler language interface ................................................................... 85

Mixing C and assembler ....................................................................... 85

Intrinsic functions .............................................................................. 85

Mixing C and assembler modules ...................................................... 86

Inline assembler ................................................................................ 87

Calling assembler routines from C ................................................. 88

Creating skeleton code ....................................................................... 88

Compiling the code ............................................................................ 89

Calling assembler routines from C++ ............................................ 90

Calling convention .................................................................................. 91

Choosing a calling convention ........................................................... 91

Function declarations ........................................................................ 92

CPIC18-3

ix

Page 10: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

x

C and C++ linkage ............................................................................. 92

Register usage ................................................................................... 93

Function entrance .............................................................................. 93

Function exit ..................................................................................... 94

Return address handling .................................................................... 95

Restrictions for special function types ............................................... 95

Call frame information ....................................................................... 96

Function directives ................................................................................. 97

Syntax ................................................................................................ 97

Parameters .......................................................................................... 97

Description ......................................................................................... 97

Using C++ ............................................................................................................ 99

Overview .................................................................................................... 99

Standard Embedded C++ ................................................................... 99

Extended Embedded C++ ................................................................ 100

Enabling C++ support ...................................................................... 100

Feature descriptions ............................................................................ 101

Classes .............................................................................................. 101

Functions .......................................................................................... 104

Templates ........................................................................................ 104

Variants of casts .............................................................................. 106

Mutable ............................................................................................ 106

Namespace ...................................................................................... 106

The STD namespace ........................................................................ 107

Pointer to member functions ............................................................ 107

Using interrupts and EC++ destructors ............................................ 107

Efficient coding for embedded applications ...................................... 109

Taking advantage of the compilation system ........................... 109

Controlling compiler optimizations ................................................. 110

Fine-tuning enabled transformations ............................................... 111

Selecting data types and placing data in memory .................. 113

Using efficient data types ................................................................ 113

Data model and data memory attributes .......................................... 113

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 11: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Contents

Using the best pointer type .............................................................. 114

Anonymous structs and unions ........................................................ 114

Writing efficient code ......................................................................... 115

Saving stack space and RAM memory ............................................ 116

Function prototypes ......................................................................... 116

Integer types and bit negation .......................................................... 117

Protecting simultaneously accessed variables .................................. 118

Accessing special function registers ................................................ 118

Non-initialized variables ................................................................. 118

Part 2. Compiler reference .................................................... 121

Data representation ...................................................................................... 123

Alignment ................................................................................................ 123

Basic data types .................................................................................... 124

Integer types ..................................................................................... 124

Floating-point types ........................................................................ 125

Pointer types .......................................................................................... 126

Casting ............................................................................................. 126

Structure types ...................................................................................... 127

General layout ................................................................................. 127

Type and object attributes ............................................................... 128

Type attributes .................................................................................. 128

Object attributes ............................................................................... 129

Declaring objects in source files ...................................................... 129

Declaring objects volatile ................................................................ 129

Data types in C++ ................................................................................ 130

Segment reference ......................................................................................... 131

Summary of segments ...................................................................... 131

Descriptions of segments .................................................................. 133

Compiler options ........................................................................................... 147

Setting command line options ........................................................ 147

Specifying parameters ..................................................................... 148

CPIC18-3

xi

Page 12: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

xii

Specifying environment variables .................................................... 149

Error return codes ............................................................................. 149

Options summary ................................................................................. 149

Descriptions of options ...................................................................... 152

Extended keywords ....................................................................................... 175

Using extended keywords ................................................................. 175

Extended keywords for data ............................................................. 175

Extended keywords for functions .................................................... 176

Summary of extended keywords ................................................... 176

Descriptions of extended keywords ............................................. 177

Pragma directives ............................................................................................ 183

Summary of pragma directives ...................................................... 183

Descriptions of pragma directives ................................................ 184

The preprocessor ........................................................................................... 193

Overview of the preprocessor ........................................................ 193

Predefined symbols .............................................................................. 193

Summary of predefined symbols ..................................................... 194

Descriptions of predefined symbols ................................................. 195

Preprocessor extensions ................................................................... 199

Intrinsic functions ........................................................................................... 201

Intrinsic functions summary ............................................................ 201

Descriptions of intrinsic functions ................................................. 202

Library functions ............................................................................................. 207

Introduction ............................................................................................ 207

Header files ...................................................................................... 207

Library object files ........................................................................... 208

Reentrancy ....................................................................................... 208

IAR DLIB Library .................................................................................. 209

C header files ................................................................................... 209

C++ header files ............................................................................... 210

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 13: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Contents

IAR CLIB Library .................................................................................. 212

Library definitions summary ............................................................ 213

Implementation-defined behavior .......................................................... 215

Descriptions of implementation-defined behavior ................ 215

Translation ....................................................................................... 215

Environment ..................................................................................... 216

Identifiers ......................................................................................... 216

Characters ......................................................................................... 216

Integers ............................................................................................. 218

Floating point ................................................................................... 218

Arrays and pointers .......................................................................... 219

Registers ........................................................................................... 219

Structures, unions, enumerations, and bitfields ............................... 219

Qualifiers .......................................................................................... 220

Declarators ....................................................................................... 220

Statements ........................................................................................ 220

Preprocessing directives ................................................................... 220

IAR DLIB Library functions ............................................................ 222

IAR CLIB Library functions ............................................................ 225

IAR language extensions ............................................................................. 229

Why should language extensions be used? ................................ 229

Descriptions of language extensions ............................................ 229

Diagnostics ......................................................................................................... 239

Message format ..................................................................................... 239

Severity levels ........................................................................................ 239

Setting the severity level .................................................................. 240

Internal error .................................................................................... 240

Index ..................................................................................................................... 241

CPIC18-3

xiii

Page 14: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

xiv

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 15: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Tables1: Typographic conventions used in this guide ......................................................... xx

2: Command line options for specifying library and dependency files ....................... 7

3: Data model characteristics .................................................................................... 12

4: Address ranges for bankN memory areas ............................................................. 13

5: Memory types and their corresponding keywords ................................................ 15

6: Code models .......................................................................................................... 23

7: Function memory attributes .................................................................................. 24

8: XLINK segment memory types ............................................................................ 30

9: Memory layout of a target system (example) ....................................................... 31

10: Memory types with corresponding segment groups ........................................... 33

11: Segment name suffixes ....................................................................................... 34

12: Library configurations ......................................................................................... 47

13: Levels of debugging support in runtime libraries ............................................... 48

14: Prebuilt DLIB libraries ........................................................................................ 49

15: Customizable items ............................................................................................. 50

16: Formatters for printf ............................................................................................ 51

17: Formatters for scanf ............................................................................................ 52

18: Descriptions of printf configuration symbols ..................................................... 61

19: Descriptions of scanf configuration symbols ...................................................... 61

20: Low-level I/O files .............................................................................................. 62

21: Functions with special meanings when linked with debug info ......................... 67

22: Example of runtime model attributes .................................................................. 69

23: Predefined runtime model attributes ................................................................... 70

24: Prebuilt CLIB libraries ........................................................................................ 78

25: Registers used for returning values ..................................................................... 94

26: Call frame information resources defined in a names block ............................... 96

27: Compiler optimization levels ............................................................................ 110

28: Integer types ...................................................................................................... 124

29: Segment summary ............................................................................................. 131

30: Environment variables ...................................................................................... 149

31: Error return codes .............................................................................................. 149

CPIC18-3

xv

Page 16: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

xvi

32: Compiler options summary ............................................................................... 149

33: Available code models ...................................................................................... 153

34: Available data models ....................................................................................... 154

35: Generating a list of dependencies (--dependencies) .......................................... 155

36: Generating a compiler list file (-l) ..................................................................... 163

37: Enabling MISRA C rules (--misrac) ................................................................. 164

38: Directing preprocessor output to file (--preprocess) ......................................... 170

39: Specifying speed optimization (-s) .................................................................... 172

40: Specifying size optimization (-z) ...................................................................... 173

41: Summary of extended keywords for functions ................................................. 176

42: Summary of extended keywords for data .......................................................... 177

43: Pragma directives summary .............................................................................. 183

44: Predefined symbols summary ........................................................................... 194

45: Intrinsic functions summary .............................................................................. 201

46: Traditional standard C header files—DLIB ...................................................... 209

47: Embedded C++ header files .............................................................................. 210

48: Additional Embedded C++ header files—DLIB ............................................... 211

49: Standard template library header files ............................................................... 211

50: New standard C header files—DLIB ................................................................ 212

51: IAR CLIB Library header files ......................................................................... 213

52: Miscellaneous IAR CLIB Library header files ................................................. 213

53: Message returned by strerror()—IAR DLIB library ......................................... 225

54: Message returned by strerror()—IAR CLIB library ......................................... 228

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 17: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

PrefaceWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed reference information that can help you to use the PIC18 IAR C/C++ Compiler to best suit your application requirements. This guide also gives you suggestions on coding techniques so that you can develop applications with maximum efficiency.

Who should read this guideYou should read this guide if you plan to develop an application using the C or C++ language for the PIC18 microcontroller and need to get detailed reference information on how to use the PIC18 IAR C/C++ Compiler. In addition, you should have a working knowledge of the following:

● The architecture and instruction set of the PIC18 microcontroller. Refer to the documentation from Microchip® Technology Inc for information about the PIC18 microcontroller

● The C or C++ programming language● Application development for embedded systems● The operating system of your host machine.

How to use this guide When you start using the PIC18 IAR C/C++ Compiler, you should read Part 1. Using the compiler in this guide.

When you are familiar with the compiler and have already configured your project, you can focus more on Part 2. Compiler reference.

If you are new to using the IAR toolkit, we recommend that you first study the IAR Embedded Workbench® IDE User Guide. This guide contains a product overview, tutorials that can help you get started, conceptual and user information about IAR Embedded Workbench and the IAR C-SPY Debugger, and corresponding reference information. The IAR Embedded Workbench® IDE User Guide also contains a glossary.

CPIC18-3

xvii

Page 18: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

xvi

What this guide contains

What this guide containsBelow is a brief outline and summary of the chapters in this guide.

Part 1. Using the compiler

● Getting started gives the information you need to get started using the PIC18 IAR C/C++ Compiler for efficiently developing your application.

● Data storage describes how data can be stored in memory, with emphasis on the different data models and data memory type attributes.

● Functions describes the different ways code can be generated, and introduces the concept of code models and function memory type attributes. Special function types, such as interrupt functions, are also covered.

● Placing code and data describes the concept of segments, introduces the linker command file, and describes how code and data are placed in memory.

● The DLIB runtime environment describes the runtime environment in which an application executes. It covers how you can modify it by setting options, overriding default library modules, or building your own library. The chapter also describes system initialization and introduces the file cstartup, as well as how to use modules for locale, and file I/O.

● The CLIB runtime environment gives an overview of the runtime libraries and how they can be customized. The chapter also describes system initialization and introduces the file cstartup.

● Assembler language interface contains information required when parts of an application are written in assembler language. This includes the calling convention.

● Using C++ gives an overview of the two levels of C++ support: The industry-standard EC++ and IAR Extended EC++.

● Efficient coding for embedded applications gives hints about how to write code that compiles to efficient code for an embedded application.

Part 2. Compiler reference

● Data representation describes the available data types, pointers, and structure types. This chapter also gives information about type and object attributes.

● Segment reference gives reference information about the compiler’s use of segments.

● Compiler options explains how to set the compiler options, gives a summary of the options, and contains detailed reference information for each compiler option.

● Extended keywords gives reference information about each of the PIC18-specific keywords that are extensions to the standard C language.

● Pragma directives gives reference information about the pragma directives.● The preprocessor gives a brief overview of the preprocessor, including reference

information about the different preprocessor directives, symbols, and other related information.

CPIC18-3

iiPIC18 IAR C/C++ CompilerReference Guide

Page 19: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Preface

● Intrinsic functions gives reference information about the functions that can be used for accessing PIC18-specific low-level features.

● Library functions gives an introduction to the C or C++ library functions, and summarizes the header files.

● Implementation-defined behavior describes how the PIC18 IAR C/C++ Compiler. handles the implementation-defined areas of the C language standard.

● IAR language extensions describes the IAR extensions to the ISO/ANSI standard for the C programming language.

● Diagnostics describes how the compiler’s diagnostic system works.

Other documentationThe complete set of IAR Systems development tools for the PIC18 microcontroller is described in a series of guides. For information about:

● Using the IAR Embedded Workbench® IDE with the IAR C-SPY™ Debugger, refer to the IAR Embedded Workbench® IDE User Guide

● Programming for the PIC18 IAR Assembler, refer to the PIC18 IAR Assembler Reference Guide

● Using the IAR XLINK Linker, the IAR XAR Library Builder, and the IAR XLIB Librarian, refer to the IAR Linker and Library Tools Reference Guide

● Using the IAR DLIB Library functions, refer to the online help system● Using the IAR CLIB Library functions, refer to the IAR C Library Functions

Reference Guide, available from the online help system.

All of these guides are delivered in hypertext PDF or HTML format on the installation media. Some of them are also delivered as printed books.

FURTHER READING

The following books may be of interest to you when using the IAR Systems development tools:

● Barr, Michael, and Andy Oram, ed. Programming Embedded Systems in C and C++ . O’Reilly & Associates.

● Harbison, Samuel P. and Guy L. Steele (contributor). C: A Reference Manual. Prentice Hall.

● Kernighan, Brian W. and Dennis M. Ritchie. The C Programming Language. Prentice Hall. [The later editions describe the ANSI C standard.]

● Labrosse, Jean J. Embedded Systems Building Blocks: Complete and Ready-To-Use Modules in C. R&D Books.

● Lippman, Stanley B. and Josée Lajoie. C++ Primer. Addison-Wesley.● Mann, Bernhard. C für Mikrocontroller. Franzis-Verlag. [Written in German.]● Stroustrup, Bjarne. The C++ Programming Language. Addison-Wesley.

CPIC18-3

xix

Page 20: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

xx

Document conventions

We recommend that you visit the following web sites:

● The Microchip® Technology Inc web site, www.microchip.com, contains information and news about the PIC18 microcontrollers and the latest version of the MPLAB® IDE.

● The IAR web site, www.iar.com, holds application notes and other product information.

● The MISRA website, www.misra.org.uk, contains information and news about the MISRA C rules.

● Finally, the Embedded C++ Technical Committee web site, www.caravan.net/ec2plus, contains information about the Embedded C++ standard.

Document conventions When, in this text, we refer to the programming language C, the text also applies to C++, unless otherwise stated.

TYPOGRAPHIC CONVENTIONS

This guide uses the following typographic conventions:

Style Used for

computer Text that you enter or that appears on the screen.

parameter A label representing the actual value you should enter as part of a command.

[option] An optional part of a command.

{option} A mandatory part of a command.

a|b|c Alternatives in a command.

bold Names of menus, menu commands, buttons, and dialog boxes that appear on the screen.

reference A cross-reference within this guide or to another guide.

… An ellipsis indicates that the previous item can be repeated an arbitrary number of times.

Identifies instructions specific to the IAR Embedded Workbench interface.

Identifies instructions specific to the command line interface.

Identifies helpful tips and programming hints.

Table 1: Typographic conventions used in this guide

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 21: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Part 1. Using the compilerThis part of the PIC18 IAR C/C++ Compiler Reference Guide includes the following chapters:

● Getting started

● Data storage

● Functions

● Placing code and data

● The DLIB runtime environment

● The CLIB runtime environment

● Assembler language interface

● Using C++

● Efficient coding for embedded applications.

CPIC18-3

1

Page 22: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

2

CPIC18-3

Page 23: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Getting startedThis chapter gives the information you need to get started using the PIC18 IAR C/C++ Compiler for efficiently developing your application.

First you will get an overview of the supported programming languages, followed by a description of the steps involved for compiling and linking an application.

Next, the compiler is introduced. You will get an overview of the basic settings needed for a project setup, including an overview of the techniques that enable applications to take full advantage of the PIC18 microcontroller. In the following chapters, these techniques will be studied in more detail.

IAR language overviewThere are two high-level programming languages available for use with the PIC18 IAR C/C++ Compiler:

● C, the most widely used high-level programming language used in the embedded systems industry. Using the PIC18 IAR C/C++ Compiler, you can build freestanding applications that follow the standard ISO 9899:1990. This standard is commonly known as ANSI C.

● C++, a modern object-oriented programming language with a full-featured library well suited for modular programming. IAR Systems supports two levels of the C++ language:

● Embedded C++ (EC++), a subset of the C++ programming standard, which is intended for embedded systems programming. It is defined by an industry consortium, the Embedded C++ Technical committee. See the chapter Using C++.

● Extended EC++, with additional features such as full template support, namespace support, the new cast operators, as well as the Standard Template Library (STL).

Each of the supported languages can be used in strict or relaxed mode, or relaxed with IAR extensions enabled. The strict mode adheres to the standard, whereas the relaxed mode allows some deviations from the standard.

It is also possible to implement parts of the application, or the whole application, in assembler language. See the PIC18 IAR Assembler Reference Guide.

CPIC18-3

Part 1. Using the compiler 3

Page 24: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

4

Supported PIC18 derivatives

For more information about the Embedded C++ language and IAR Extended Embedded EC++, see the chapter Using C++.

Supported PIC18 derivativesThe PIC18 IAR C/C++ Compiler supports all derivatives based on the standard Microchip® Technology Inc PIC18 microcontroller. In addition, external memory is also supported, up to 2 Mbytes. (Used, for example, in 18F8720 derivatives.)

Building applications—an overviewA typical application is built from a number of source files and libraries. The source files can be written in C, C++, or assembler language, and can be compiled into object files by the PIC18 IAR C/C++ Compiler or the PIC18 IAR Assembler.

A library is a collection of object files. A typical example of a library is the compiler library containing the runtime environment and the C/C++ standard library. Libraries can also be built using the IAR XAR Library Builder, the IAR XLIB Librarian, or be provided by external suppliers.

The IAR XLINK Linker is used for building the final application. XLINK normally uses a linker command file, which describes the available resources of the target system.

Below, the process for building an application on the command line is described. For information about how to build an application using the IAR Embedded Workbench IDE, see the IAR Embedded Workbench® IDE User Guide.

COMPILING

In the command line interface, the following line compiles the source file myfile.c into the object file myfile.r49 using the default settings:

iccpic18 myfile.c

In addition, you need to specify some critical options, see Basic settings for project configuration, page 5.

LINKING

The IAR XLINK Linker is used for building the final application. Normally, XLINK requires the following information as input:

● A number of object files and possibly certain libraries● The standard library containing the runtime environment and the standard language

functions

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 25: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Getting started

● A program start label● A linker command file that describes the memory layout of the target system● Information about the output format.

On the command line, the following line can be used for starting XLINK:

xlink myfile.r49 myfile2.r49 -s __program_start -f lnkpic18.xcl cl18os.r49 -o aout.a49 -FMotorola

In this example, myfile.r49 and myfile2.r49 are object files, lnkpic18.xcl is the linker command file, and cl18os.r49 is the runtime library. The option -s specifies the label where the application starts. The option -o specifies the name of the output file, and the option -F can be used for specifying the output format. (The default output format is debug.)

The IAR XLINK Linker produces output after your specifications. Choose the output format that suits your purpose. You might want to load the output to a debugger—which means that you need output with debug information. Alternatively, you might want to load the output to a PROM programmer—in which case you need output without debug information, such as Intel-hex or Motorola S-records.

Basic settings for project configuration This section gives an overview of the basic settings for the project setup that are needed to make the compiler generate the best code for the PIC18 device you are using. You can specify the options either from the command line interface or in the IAR Embedded Workbench IDE. For details about how to set options, see Setting command line options, page 147, and the IAR Embedded Workbench® IDE User Guide, respectively.

The basic settings available for the PIC18 microcontroller are:

● Data model● Code model● Optimization settings● Runtime environment.

In addition to these settings, there are many other options and settings available for fine-tuning the result even further. See the chapter Compiler options for a list of all available options.

DATA MODEL

One of the characteristics of the PIC18 microcontroller is that there is a trade-off regarding the way memory is accessed, between the range from cheap access to small memory areas, up to more expensive access methods that can access any location.

CPIC18-3

Part 1. Using the compiler 5

Page 26: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

6

Basic settings for project configuration

In the PIC18 IAR C/C++ Compiler, you can set a default memory access method by selecting a data model. However, it is possible to override the default access method for each individual variable. The following data models are supported:

● The small data model generates more compact code for small PIC18 derivatives.● The large data model provides support for access to larger code memory.

The chapter Data storage covers data models in greater detail. The chapter also covers how to fine-tune the access method for individual variables.

CODE MODEL

The PIC18 IAR C/C++ Compiler supports code models that you can set on file- or function-level to control the function calling convention. The available code models are the overlay code model and the stack code model.

For detailed information about the code models, see the chapter Functions.

OPTIMIZATION FOR SPEED AND SIZE

The PIC18 IAR C/C++ Compiler is a state-of-the-art compiler with an optimizer that performs, among other things, dead-code elimination, constant propagation, inlining, common sub-expression elimination, and precision reduction. It also performs loop optimizations, such as unrolling and induction variable elimination.

You can decide between several optimization levels and two optimization goals—size and speed. Most optimizations will make the application both smaller and faster. However, when this is not the case, the compiler uses the selected optimization goal to decide how to perform the optimization.

The optimization level and goal can be specified for the entire application, for individual files, and for individual functions. In addition, some individual optimizations, such as function inlining, can be disabled.

For details about compiler optimizations, see Controlling compiler optimizations, page 110. For more information about efficient coding techniques, see the chapter Efficient coding for embedded applications.

RUNTIME ENVIRONMENT

To create the required runtime environment you should choose a runtime library and set library options. You may also need to override certain library modules with your own customized versions.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 27: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Getting started

There are two different sets of runtime libraries provided:

● The IAR DLIB Library, which supports ISO/ANSI C and C++. This library can be configured to include different levels of support for locale, file descriptors, multibyte characters, et cetera.

● The IAR CLIB Library is a light-weight library, which is not fully compliant with ISO/ANSI C. Neither does it support Embedded C++. (This library is used by default).

The runtime library you choose can be one of the prebuilt libraries, or a library that you have customized and built yourself. The IAR Embedded Workbench IDE provides a library project template for both libraries, that you can use for building your own library version. This gives you full control of the runtime environment. If your project only contains assembler source code, there is no need to choose a runtime library.

For detailed information about the runtime environments, see the chapters The DLIB runtime environment and The CLIB runtime environment, respectively.

The way you set up a runtime environment and locate all the related files differs depending on which build interface you are using—the IAR Embedded Workbench IDE or the command line.

Choosing a runtime library in the IAR Embedded Workbench IDE

To choose a library, choose Project>Options, and click the Library Configuration tab in the General Options category. Choose the appropriate library from the Library drop-down menu.

Note that for the DLIB library there are two different configurations—Normal and Full—which include different levels of support for locale, file descriptors, multibyte characters, et cetera. See Library configurations, page 47, for more information.

Based on which library configuration you choose and your other project settings, the correct library file is used automatically. For the device-specific include files, a correct include path is set up.

Choosing a runtime library from the command line

Use the following command line options to specify the library and the dependency files:

Command line Description

-I\pic18\inc Specifies the include paths

-I\pic18\inc\{clib|dlib} Specifies the library-specific include path. Use clib or dlib depending on which library you are using.

libraryfile.r49 Specifies the library object file

Table 2: Command line options for specifying library and dependency files

CPIC18-3

Part 1. Using the compiler 7

Page 28: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

8

Special support for embedded systems

For a list of all prebuilt library object files for the IAR DLIB Library, see Table 14, Prebuilt DLIB libraries, page 49. The table also shows how the object files correspond to the dependent project options, and the corresponding configuration files. Make sure to use the object file that matches your other project options.

For a list of all prebuilt object files for the IAR CLIB Library, see Table 24, Prebuilt CLIB libraries, page 78. The table also shows how the object files correspond to the dependent project options. Make sure to use the object file that matches your other project options.

Setting library and runtime environment options

You can set certain options to reduce the library and runtime environment size:

● The formatters used by the functions printf, scanf, and their variants, see Choosing formatters for printf and scanf, page 50 (DLIB) and Input and output, page 78 (CLIB).

● The size of the stack and the heap, see The overlay system, page 36, and The heap, page 37, respectively.

Special support for embedded systemsThis section briefly describes the extensions provided by the PIC18 IAR C/C++ Compiler to support specific features of the PIC18 microcontroller.

EXTENDED KEYWORDS

The PIC18 IAR C/C++ Compiler provides a set of keywords that can be used for configuring how the code is generated. For example, there are keywords for controlling the memory type for individual variables as well as for declaring special function types.

By default, language extensions are enabled in the IAR Embedded Workbench IDE.

The command line option -e makes the extended keywords available, and reserves them so that they cannot be used as variable names. See, -e, page 159 for additional information.

For detailed descriptions of the extended keywords, see the chapter Extended keywords.

--dlib_config

C:\...\configfile.h

Specifies the library configuration file (for the IAR DLIB Library only)

Command line Description

Table 2: Command line options for specifying library and dependency files (Continued)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 29: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Getting started

PRAGMA DIRECTIVES

The pragma directives control the behavior of the compiler, for example how it allocates memory, whether it allows extended keywords, and whether it issues warning messages.

The pragma directives are always enabled in the PIC18 IAR C/C++ Compiler. They are consistent with ISO/ANSI C, and are very useful when you want to make sure that the source code is portable.

For detailed descriptions of the pragma directives, see the chapter Pragma directives.

PREDEFINED SYMBOLS

With the predefined preprocessor symbols, you can inspect your compile-time environment, for example time of compilation, and the code and data models.

For detailed descriptions of the predefined symbols, see the chapter The preprocessor.

SPECIAL FUNCTION TYPES

The special hardware features of the PIC18 microcontroller are supported by the compiler’s special function types: interrupt and monitor. You can write a complete application without having to write any of these functions in assembler language.

For detailed information, see Special function types, page 24.

HEADER FILES FOR I/O

Standard peripheral units are defined in device-specific I/O header files with the filename extension h. The product package supplies I/O files for all devices that are available at the time of the product release. You can find these files in the pic18/inc directory. Make sure to include the appropriate include file in your application source files. If you need additional I/O header files, they can easily be created using one of the provided ones as a template.

For an example, see Accessing special function registers, page 118.

ACCESSING LOW-LEVEL FEATURES

For hardware-related parts of your application, accessing low-level features is essential. The PIC18 IAR C/C++ Compiler supports several ways of doing this: intrinsic functions, mixing C and assembler modules, and inline assembler. For information about the different methods, see Mixing C and assembler, page 85.

CPIC18-3

Part 1. Using the compiler 9

Page 30: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

10

Special support for embedded systems

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 31: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Data storage This chapter gives a brief introduction to the memory layout of the PIC18 microcontroller and the fundamental ways data can be stored in memory: in static overlay memory, on the stack, in static (global) memory, or in heap memory. For efficient memory usage, PIC18 IAR C/C++ Compiler provides a set of data models and data memory attributes, allowing you to fine-tune the access methods, resulting in smaller code size. The concepts of data models and memory types are described in relation to pointers, structures, Embedded C++ class objects, and non-initialized memory. Finally, detailed information about data storage on the stack and the heap is provided.

IntroductionThe PIC18 microcontroller has two separate memory spaces: 2 Mbytes of code memory (ROM) and up to 4 Kbytes of data memory ( RAM). Data memory can be accessed efficiently, while code memory requires more code space and execution time to access. Code memory is used for code, const declared variables, string literals, and initialized data. The data memory for the PIC18 microcontroller is divided into different zones, depending on access method.

In a typical application, data can be stored in memory in four different ways:

● Static overlay memory. Static overlay is a system where local data and function parameters are stored at static locations in memory. This kind of memory is allocated once and for all; it remains valid through the entire execution of the application. Each function is associated with an overlay frame that has a fixed size and contains space for local variables, function parameters and temporary data.

● On the stack. This is memory space that can be used by a function as long as it is executing. When the function returns to its caller, the memory space is no longer valid.

● Static memory. This kind of memory is allocated once and for all; it remains valid through the entire execution of the application. Variables that are either global or declared static are placed in this type of memory. The word static in this context means that the amount of memory allocated for this type of variable does not change while the application is running.

CPIC18-3

Part 1. Using the compiler 11

Page 32: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

12

Data models

● On the heap. Once memory has been allocated on the heap, it remains valid until it is explicitly released back to the system by the application. This type of memory is useful when the number of objects is not known until the application executes. Note that there are potential risks connected with using the heap in systems with a limited amount of memory, or systems that are expected to run for a long time.

Data modelsThe PIC18 IAR C/C++ Compiler supports a number of data models that can be used for applications with different data requirements.

Technically, the data model specifies the default memory type. This means that the data model places some size restrictions on memory attributes.

The data model only specifies the default memory type. It is possible to override this for individual variables and pointers. For information about how to specify a memory type for individual objects, see Using data memory attributes, page 15.

SPECIFYING A DATA MODEL

Two data models have been implemented: small and large. The following table summarizes the different data models:

Your project can only use one data model at a time, and the same model must be used by all user modules and all library modules. If you do not specify a data model option, the compiler will use the small data model.

See the IAR Embedded Workbench® IDE User Guide for information about setting options in the IAR Embedded Workbench IDE.

Use the --data_model option to specify the data model for your project; see --data_model, page 154.

Memory typesThis section describes the concept of memory types used for accessing data by the PIC18 IAR C/C++ Compiler. It also discusses pointers in the presence of multiple memory types. For each memory type, the capabilities and limitations are discussed.

Data model Compiler option Default pointer size

Small (default) --data_model s 2 bytes

Large --data_model l 3 bytes

Table 3: Data model characteristics

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 33: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Data storage

The PIC18 IAR C/C++ Compiler uses different memory types to access data that is placed in different areas of the memory. There are different methods for reaching memory areas, and they have different costs when it comes to code space, execution speed, and register usage. The access methods range from generic but expensive methods that can access the full memory space, to cheap methods that can access limited memory areas. Each memory type corresponds to one memory access method. By mapping different memories—or part of memories—to memory types, the compiler can generate code that can access data efficiently.

For example, the memory accessible using the nonbanked memory access method is called memory of nonbanked type, or simply nonbanked memory.

By selecting a data model, you have selected a default memory type that your application will use. However, it is possible to specify—for individual variables or pointers—different memory types. This makes it possible to create an application that can contain a large amount of data, and at the same time make sure that variables that are used often are placed in memory that can be efficiently accessed.

NONBANKED MEMORY

The nonbanked memory is placed in the nonbanked access area of bank 0. This area can be accessed without the use of the BSR register, and is more efficient than the banked memory areas.

The compiler reserves up to 13 bytes of the nonbanked area for its own use.

BANKED MEMORY

There are 16 banked memory areas in the PIC18 microcontroller.

The address ranges can be seen in this table:

Bank number Address range

Bank 0 0x0–0xFF

Bank 1 0x100–0x1FF

Bank 2 0x200–0x2FF

Bank 3 0x300–0x3FF

Bank 4 0x400–0x4FF

Bank 5 0x500–0x5FF

Bank 6 0x600–0x6FF

Bank 7 0x700–0x7FF

Bank 8 0x800–0x8FF

Table 4: Address ranges for bankN memory areas

CPIC18-3

Part 1. Using the compiler 13

Page 34: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

14

Memory types

The banked memory areas can be accessed in two different ways; you can either specify the exact bank yourself or let the compiler decide which specific bank to be accessed.

Explicitly specifying the bank to be accessed

If you want to access a specific bank, you must use the __bank0, __bank1 etc. keyword. The maximum object size in a bank area is 255 bytes.

The BSR bank select register must be set up before the banked memory areas can be accessed individually. This can make this type of memory slightly less efficient than nonbanked memory.

Letting the compiler decide which bank to be accessed

A simpler, more portable way of writing applications with a minimum of target-specific keywords, is to access the banked memory using the __bank keyword. If you use this access method, data will be placed in a suitable bank at link time. This is the default memory access method for the IAR PIC18 C/C++ Compiler, with a maximum object size of 255 bytes. Because the exact placement of a variable is not known until link time, some advanced optimizations cannot be performed. Moreover, initialization of memory accessed this way requires more code space than initializing __bank0 etc. memory. This means using the __bank keyword is less efficient than using the __bank0, __bank1 etc. keyword.

EEPROM

If your PIC18 microcontroller has EEPROM memory, the eeprom access method can be used to access it. Eeprom accesses are possible regardless of which data model you are using.

EXTERNAL MEMORY

If your PIC18 microcontroller has external RAM memory, the extmem access method can be used to access it. Extmem accesses require that you use the large data model.

Bank 9 0x900–0x9FF

Bank 10 0xA00–0xAFF

Bank 11 0xB00–0xBFF

Bank 12 0xC00–0xCFF

Bank 13 0xD00–0xDFF

Bank 14 0xE00–0xEFF

Bank 15 0xF00–0xF7F

Bank number Address range

Table 4: Address ranges for bankN memory areas (Continued)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 35: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Data storage

USING DATA MEMORY ATTRIBUTES

The PIC18 IAR C/C++ Compiler provides a set of extended keywords, which can be used as data memory attributes. These keywords let you override the default memory type for individual data objects, which means that you can place data objects in other memory areas than the default memory. This also means that you can fine-tune the access method for each individual data object, which results in smaller code size.

The following table summarizes the available memory types and their corresponding keywords:

1For devices with up to 256 bytes of EEPROM memory.2For devices with more than 256 bytes of EEPROM memory.

The keywords are only available if language extensions are enabled in the PIC18 IAR C/C++ Compiler.

In the IAR Embedded Workbench IDE, language extensions are enabled by default.

Use the -e compiler option to enable language extensions. See -e, page 159 for additional information.

For reference information about each keyword, see Descriptions of extended keywords, page 177.

Syntax

The keywords follow the same syntax as the type qualifiers const and volatile. The memory attributes are type attributes and therefore they must be specified both when they are defined and in the declaration, see Type attributes, page 128.

The following declarations place the variable i and j in bank1 memory. The variables k and l behave in the same way:

__bank1 int i, j;int __bank1 k, l;

Memory type Keyword Address range Pointer size

Nonbanked __nonbanked 0x0–0x7F 1 byte

Banked __bank0, etc 0x0–0xFF 1 byte

__bank (default) 0x0–0x0FFF 2 bytes

Eeprom __eeprom 0x0–0xFF1 or0x0–0xFFFF2

1 byte1 or2 bytes2

Extmem __extmem 0x0–0x1FFFFF 3 bytes

Table 5: Memory types and their corresponding keywords

CPIC18-3

Part 1. Using the compiler 15

Page 36: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

16

Memory types

Note that the keyword affects both identifiers. If no memory type is specified, the default memory type is used.

In addition to the rules presented here—to place the keyword directly in the code—the directive #pragma type_attribute can be used for specifying the memory attributes. The advantage of using pragma directives for specifying keywords is that it offers you a method to make sure that the source code is portable. Refer to the chapter Pragma directives for details about how to use the extended keywords together with pragma directives.

Type definitions

Storage can also be specified using type definitions. The following two declarations are equivalent:

typedef char __bank1 Byte;typedef Byte *BytePtr;Byte b;BytePtr bp;

and

__bank1 char b;char __bank1 *bp;

POINTERS AND MEMORY TYPES

Pointers are used for referring to the location of data. In general, a pointer has a type. For example, a pointer that has the type int * points to an integer.

In the PIC18 IAR C/C++ Compiler, a pointer also points to some type of memory. The memory type is specified using a keyword before the asterisk. For example, a pointer that points to an integer stored in bank1 memory is declared by:

int __bank1 * p;

Note that the location of the pointer variable p is not affected by the keyword. In the following example, however, the pointer variable p2 is placed in bank2 memory. Like p, p2 points to a character in bank1 memory.

char __bank1 * __bank2 p2;

Whenever possible, pointers should be declared without memory attributes. For example, the functions in the standard library are all declared without explicit memory types.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 37: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Data storage

STRUCTURES AND MEMORY TYPES

For structures, the entire object is placed in the same memory type. It is not possible to place individual structure members in different memory types.

In the example below, the variable gamma is a structure placed in bank1 memory.

struct MyStruct{ int alpha; int beta;};__bank1 struct MyStruct gamma;

The following declaration is incorrect:

struct MySecondStruct{ int blue; __bank1 int green; /* Error! */};

MORE EXAMPLES

The following is a series of examples with descriptions. First, some integer variables are defined and then pointer variables are introduced. Finally, a function accepting a pointer to an integer in bank1 memory is declared. The function returns a pointer to an integer in bank memory. It makes no difference whether the memory attribute is placed before or after the data type. In order to read the following examples, start from the left and add one qualifier at each step

int a; A variable defined in default memory.

int __bank1 b; A variable in bank1 memory.

__bank int c; A variable in bank memory.

int * d; A pointer stored in default memory. The pointer is the generic pointer, pointing to an integer in any memory.

int __bank1 * e; A pointer stored in default memory. The pointer points to an integer in bank1 memory.

int __bank1 * __bank f; A pointer stored in bank memory pointing to an integer stored in bank1 memory.

CPIC18-3

Part 1. Using the compiler 17

Page 38: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

18

C++ and memory types

C++ and memory types A C++ class object is placed in one memory type, in the same way as for normal C structures. However, the class members that are considered to be part of the object are the non-static member variables. The static member variables can be placed individually in any kind of memory.

Remember, in C++ there is only one instance of each static member variable, regardless of the number of class objects.

Also note that for non-static member functions—unless class memory is used, see Classes, page 101—the this pointer will be of the default data pointer type. This means that it must be possible to convert a pointer to the object to the default pointer type. The restrictions that apply to the default pointer type also apply to the this pointer.

Example

In the example below, an object, named delta, of the type MyClass is defined in bank memory. The class contains a static member variable that is stored in bank0 memory.

// The class declaration (placed in a header file):class MyClass{public: int alpha; int beta;

__bank0 static int gamma;};

// Definitions needed (should be placed in a source file):__bank0 int MyClass::gamma;

// A variable definition:__bank MyClass delta;

int __bank * myFunction(

int __bank1 *);A declaration of a function that takes a parameter which is a pointer to an integer stored in bank1 memory. The function returns a pointer to an integer stored in bank memory.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 39: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Data storage

Auto variables on the stack and in overlay framesVariables that are defined inside a function—not declared static—are named auto variables by the C standard. These are placed on the stack or in static overlay.

Auto variables live as long as the function executes; when the function returns, the memory allocated on the stack or for static overlay is released.

Both the stack and static overlay frames can contain:

● Local variables and parameters● Temporary results of expressions● Processor state during interrupts● Workseg registers that should be restored before the function returns (callee-save

registers).

The stack can also hold the return value of a function.

Advantages

The main advantage of the stack and of the static overlay system is that functions in different parts of the program can use the same memory space to store their data. Unlike a heap, a stack or static overlay frame will never become fragmented or suffer from memory leaks.

Potential problems

The way the stack and the static overlay system work makes it impossible to store data that is supposed to live after the function has returned. The following function demonstrates a common programming mistake. It returns a pointer to the variable x, a variable that ceases to exist when the function returns.

int * MyFunction(){ int x; ... do something ... return &x;}

THE STACK

The stack is a fixed block of memory, divided into two parts. The first part contains allocated memory used by the function that called the current function, and the function that called it, etc. The second part contains free memory that can be allocated. The borderline between the two areas is called the top of stack and is represented by the stack pointer, which is a dedicated processor register. Memory is allocated on the stack by moving the stack pointer.

CPIC18-3

Part 1. Using the compiler 19

Page 40: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

20

Dynamic memory on the heap

A function should never refer to the memory in the area of the stack that contains free memory. The reason is that if an interrupt occurs, the called interrupt function can allocate, modify, and—of course—deallocate memory on the stack.

Specific advantages of a stack

It is possible for a function to call itself—a so-called a recursive function—and each invocation can store its own data on the stack.

Specific potential problems using a stack

You might run out of stack. This will happen when one function calls another, which in turn calls a third, etc., and the sum of the stack usage of each function is larger than the size of the stack. The risk is higher if large data objects are stored on the stack, or when recursive functions—functions that call themselves either directly or indirectly—are used.

STATIC OVERLAY

Static overlay is a system where local data and function parameters are stored at static locations in memory. Each function is associated with an overlay frame that has a fixed size and contains space for local variables, function parameters and temporary data.

Static overlay can produce very efficient code on the PIC18 microcontroller because it has good support for direct addressing. However, the amount of directly accessible memory is limited, so static overlay systems are only suitable for small applications.

The problem with the static overlay system is that it is difficult to support recursive and reentrant applications. In reentrant and recursive systems, several instances of the same function can be alive at the same time, so it is not enough to have one overlay frame for each function; instead the compiler must be able to handle multiple overlay frames for the same function. Therefore, the static overlay system is restricted and does not support recursion or reentrancy.

Dynamic memory on the heap Memory for objects allocated on the heap will live until the objects are explicitly released. This type of memory storage is very useful for applications where the amount of data is not known until runtime.

In C, memory is allocated using the standard library function malloc, or one of the related functions calloc and realloc. The memory is released again using free.

In C++, there is a special keyword, new, designed to allocate memory and run constructors. Memory allocated with new must be released using the keyword delete.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 41: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Data storage

The PIC18 IAR C/C++ Compiler can only have a heap in bank memory. For more information about this, see The heap, page 37.

Potential problems

Applications that are using heap-allocated objects must be designed very carefully, because it is easy to end up in a situation where it is not possible to allocate objects on the heap.

The heap can become exhausted because your application simply uses too much memory. It can also become full if memory that no longer is in use has not been released.

For each allocated memory block, a few bytes of data for administrative purposes is required. For applications that allocate a large number of small blocks, this administrative overhead can be substantial.

There is also the matter of fragmentation; this means a heap where small sections of free memory is separated by memory used by allocated objects. It is not possible to allocate a new object if there is no piece of free memory that is large enough for the object, even though the sum of the sizes of the free memory exceeds the size of the object.

Unfortunately, fragmentation tends to increase as memory is allocated and released. Hence, applications that are designed to run for a long time should try to avoid using memory allocated on the heap.

CPIC18-3

Part 1. Using the compiler 21

Page 42: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

22

Dynamic memory on the heap

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 43: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Functions This chapter contains information about functions. First, the different ways normal code can be generated and the concept of code models are introduced. Then, the special function types interrupt and monitor are described, including how to declare Embedded C++ member functions by using special function types. The last section describes how to use extended keywords when defining functions, and how to place functions into named segments.

Code models The PIC18 IAR C/C++ Compiler can store parameters and auto variables in one of two places: in an overlay frame or on the stack. The compiler supports this by means of code models.

The code model controls how code is generated for an application. All object files of an application must be compiled using the same code model. The supported code models are:

See the IAR Embedded Workbench® IDE User Guide for information about specifying a code model in the IAR Embedded Workbench IDE.

Use the --code_model option to specify the code model for your project; see --code_model, page 152.

Code modelDefault function type

attributeDescription

Overlay (default) __bank_func Functions use static overlay memory for parameters and auto variables. These must fit within a RAM bank, and thus cannot be larger than 256 bytes.

Stack __stack_func Functions use a simulated stack in the FSR2 register for parameters and auto variables. This may result in slower execution, but function stack frames are allowed to exceed 256 bytes. Static overlay can still be used for compiler support routines.

Table 6: Code models

CPIC18-3

Part 1. Using the compiler 23

Page 44: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

24

Special function types

USING FUNCTION MEMORY ATTRIBUTES

It is possible to specify for individual functions how to pass parameters, in other words, the assembler instructions used when calling the function. You specify this by using the appropriate function memory attribute. You can specify one function memory attribute for each function by using the #pragma type_attribute directive. These attributes must be specified both when the function is declared and when it is defined.

The following function memory attributes are available:

Pointers with function memory attributes have restrictions in implicit and explicit casts between pointers and between pointers and integer types. For details about the restrictions, see Casting, page 126.

Syntax

The extended keywords are specified before the return type, for example:

__stack_func void alpha(void);

The keywords that are type attributes must be specified both when they are defined and in the declaration.

FUNCTION DIRECTIVES

The function directives FUNCTION, ARGFRAME, LOCFRAME, and FUNCALL are generated by the PIC18 IAR C/C++ Compiler to pass information about functions and function calls to the IAR XLINK Linker. These directives can be seen if you use the compiler option Assembler file (-lA) to create an assembler list file. They are primarily intended to support static overlay.

For more information, see Function directives, page 97.

Special function types This section describes the special function types interrupt and monitor. The PIC18 IAR C/C++ Compiler allows an application to take full advantage of these PIC18 features, without forcing you to implement anything in assembler language.

Function memory attribute Pointer size Default in code model

__bank_func 4 bytes Overlay

__bankN_func 4 bytes

__stack_func 3 bytes Stack

Table 7: Function memory attributes

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 45: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Functions

INTERRUPT FUNCTIONS

In embedded systems, the use of interrupts is a method of detecting external events immediately; for example, detecting that a button has been pressed.

In general, when an interrupt occurs in the code, the microcontroller simply stops executing the code it runs, and starts executing an interrupt routine instead. It is imperative that the environment of the interrupted function is restored; this includes the values of processor registers and the processor status register. This makes it possible to continue the execution of the original code when the code that handled the interrupt has been executed.

The PIC18 microcontroller supports two interrupt sources. For each interrupt source, an interrupt routine can be written. Each interrupt routine is associated with a vector number, alternatively multiple vector numbers, which is specified in the PIC18 microcontroller documentation from the chip manufacturer. The interrupt vector is the offset into the interrupt vector table. For the PIC18 microcontroller, the interrupt vector table always starts at address 0x0.

To define an interrupt function, the __interrupt keyword and the #pragma vector directive can be used. For example:

#pragma vector=8__interrupt void my_interrupt_routine(void){ /* Do something */}

Note: An interrupt function must have the return type void, and it cannot specify any parameters.

If a vector is specified in the definition of an interrupt function, the processor interrupt vector table is populated. It is also possible to define an interrupt function without a vector. See the chip manufacturer’s PIC18 microcontroller documentation for more information about the interrupt vector table.

MONITOR FUNCTIONS

A monitor function causes interrupts to be disabled during execution of the function. At function entry, the status register is saved and interrupts are disabled. At function exit, the interrupt status that existed before the function call is also restored.

To define a monitor function, you can use the __monitor keyword. For reference information, see __monitor, page 181.

CPIC18-3

Part 1. Using the compiler 25

Page 46: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

26

Special function types

Example of implementing a semaphore in C

In the following example, a semaphore is implemented using one static variable and two monitor functions. A semaphore can be locked by one process, and is used for preventing processes from simultaneously using resources that can only be used by one process at a time, for example a printer.

/* When the_lock is non-zero, someone owns the lock. */static volatile unsigned int the_lock = 0;

/* get_lock -- Try to lock the lock. * Return 1 on success and 0 on failure. */

__monitor int get_lock(void){ if (the_lock == 0) { /* Success, we managed to lock the lock. */ the_lock = 1; return 1; } else { /* Failure, someone else has locked the lock. */ return 0; }}

/* release_lock -- Unlock the lock. */

__monitor void release_lock(void){ the_lock = 0;}

The following is an example of a program fragment that uses the semaphore:

void my_program(void){ if (get_lock()) { /* ... Do something ... */

/* When done, release the lock. */ release_lock(); }}

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 47: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Functions

The drawback using this method is that interrupts are disabled for the entire monitor function.

Example of implementing a semaphore in C++

In C++, it is common to implement small methods with the intention that they should be inlined. However, the PIC18 IAR C/C++ Compiler does not support inlining of functions and methods that are declared using the __monitor keyword.

In the following example in C++, an auto object is used for controlling the monitor block, which uses intrinsic functions instead of the __monitor keyword.

#include <intrinsics.h>

volatile long tick_count = 0;

/* Class for controlling critical blocks */class Mutex{public: Mutex () { _state = __get_interrupt_state(); __disable_interrupt(); }

~Mutex () { __set_interrupt_state(_state); }

private: __istate_t _state;};

void f(){ static long next_stop = 100; extern void do_stuff(); long tick; /* A critical block */ { Mutex m; /* Read volatile variable 'tick_count' in a safe way and put the value in a local variable */

CPIC18-3

Part 1. Using the compiler 27

Page 48: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

28

Special function types

tick = tick_count; } if (tick >= next_stop) { next_stop += 100; do_stuff(); }}

C++ AND SPECIAL FUNCTION TYPES

C++ member functions can be declared using special function types. However, there is a restriction: Interrupt member functions must be static. When calling a non-static member function, it must be applied to an object. When an interrupt occurs and the interrupt function is called, there is no such object available.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 49: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Placing code and data This chapter introduces the concept of segments, and describes the different segment groups and segment types. It also describes how they correspond to the memory and function types, and how they interact with the runtime environment. The methods for placing segments in memory, which means customizing a linker command file, are described.

The intended readers of this chapter are the system designers that are responsible for mapping the segments of the application to appropriate memory areas of the hardware system.

Segments and memoryIn an embedded system, there are many different types of physical memory. Also, it is often critical where parts of your code and data are located in the physical memory. For this reason it is important that the development tools meet these requirements.

WHAT IS A SEGMENT?

A segment is a logical entity containing a piece of data or code that should be mapped to a physical location in memory. Each segment consists of many segment parts. Normally, each function or variable with static storage duration is placed in a segment part. A segment part is the smallest linkable unit, which allows the linker to include only those units that are referred to. The segment could be placed either in RAM or in ROM. Segments that are placed in RAM do not have any content, they only occupy space.

The PIC18 IAR C/C++ Compiler has a number of predefined segments for different purposes. Each segment has a name that describes the contents of the segment, and a segment memory type that denotes the type of content. In addition to the predefined segments, you can define your own segments.

At compile time, the compiler assigns each segment its contents. The IAR XLINK Linker is responsible for placing the segments in the physical memory range, in accordance with the rules specified in the linker command file. There are supplied linker command files, but, if necessary, they can be easily modified according to the requirements of your target system and application. It is important to remember that, from the linker's point of view, all segments are equal; they are simply named parts of memory.

CPIC18-3

Part 1. Using the compiler 29

Page 50: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

30

Placing segments in memory

For detailed information about individual segments, see the chapter Segment reference in Part 2. Compiler reference.

Segment memory type

XLINK assigns a segment memory type to each of the segments. In some cases, the individual segments may have the same name as the segment memory type they belong to, for example CODE. Make sure not to confuse the individual segment names with the segment memory types in those cases.

By default, the PIC18 IAR C/C++ Compiler uses only the following XLINK segment memory types:

XLINK supports a number of other segment memory types than the ones described above. However, they exist to support other types of microcontrollers.

For more details about segments, see the chapter Segment reference.

Placing segments in memoryThe placement of segments in memory is performed by the IAR XLINK Linker. It uses a linker command file that contains command line options which specify the locations where the segments can be placed, thereby assuring that your application fits on the target chip. You can use the same source code with different derivatives just by rebuilding the code with the appropriate linker command file.

In particular, the linker command file specifies:

● The placement of segments in memory● The maximum stack size● The maximum heap size (only for the IAR DLIB runtime environment).

This section describes the methods for placing the segments in memory, which means that you have to customize the linker command file to suit the memory layout of your target system. For showing the methods, fictitious examples are used.

Segment memory type Description

CODE For executable code

CONST For data placed in ROM

DATA For data placed in bank memory

IDATA Hardware call stack

XDATA For data placed in EEPROM

Table 8: XLINK segment memory types

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 51: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Placing code and data

CUSTOMIZING THE LINKER COMMAND FILE

The only change you will normally have to make to the supplied linker command file is to customize it so it fits the target system memory map.

As an example, we assume a target system with the following memory layout:

The main purpose of customizing the linker command file is to verify that your application code and data do not cross the memory range boundaries, which would lead to application failure.

The contents of the linker command file

The config directory contains at least one ready-made linker command file. The file contains the information required by the linker, and is ready to be used. If, for example, your application uses additional external RAM, you need to add details about the external RAM memory area. Remember not to change the original file. We recommend that you make a copy in the working directory, and modify the copy instead.

Note: The supplied linker command file includes comments explaining the contents.

Among other things, the linker command file contains three different types of XLINK command line options:

● The CPU used:-cpic18

This specifies your target microcontroller.

● Definitions of constants used later in the file. These are defined using the XLINK option -D.

● The placement directives (the largest part of the linker command file). Segments can be placed using the -Z and -P options. The former will place the segment parts in the order they are found, while the latter will try to rearrange them to make better use of the memory. The -P option is useful when the memory where the segment should be placed is not continuous.

Note: In the linker command file, all numbers are specified in hexadecimal format. However, neither the prefix 0x nor the suffix h is used.

See the IAR Linker and Library Tools Reference Guide for more details.

Range Type

0x0–0x0FFF Bank memory

0x0–0xFFFF EEPROM

0x10000–0x1FFFFF ROM

Table 9: Memory layout of a target system (example)

CPIC18-3

Part 1. Using the compiler 31

Page 52: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

32

Placing segments in memory

Using the -Z command for sequential placement

Use the -Z command when you need to keep a segment in one consecutive chunk, when you need to preserve the order of segment parts in a segment, or, more unlikely, when you need to put segments in a specific order.

The following illustrates how to use the -Z command to place the segment MYSEGMENTA followed by the segment MYSEGMENTB in CONST memory (that is, ROM) in the memory range 0x20-0xCF.

-Z(CONST)MYSEGMENTA,MYSEGMENTB=20-CF

Two segments of different types can be placed in the same memory area by not specifying a range for the second segment. In the following example, the MYSEGMENTA segment is first located in memory. Then, the rest of the memory range could be used by MYCODE.

-Z(CONST)MYSEGMENTA=20-CF-Z(CODE)MYCODE

Two memory ranges may overlap. This allows segments with different placement requirements to share parts of the memory space; for example:

-Z(CONST)MYSMALLSEGMENT=20-2F-Z(CONST)MYLARGESEGMENT=20-CF

Even though it is not strictly required, make sure to always specify the end of each memory range. If you do this, the IAR XLINK Linker will alert you if your segments do not fit.

Using the -P command for packed placement

The -P command differs from -Z in that it does not necessarily place the segments (or segment parts) sequentially. With -P it is possible to put segment parts into holes left by earlier placements.

The following example illustrates how the XLINK -P option can be used for making efficient use of the memory area. The command will place the data segment MYDATA in DATA memory (that is, in bank memory) in a fictitious memory range:

-P(DATA)MYDATA=0-1F,100-1FF

If your application has an additional RAM area in the memory range 0xF0-0xFF, you just add that to the original definition:

-P(DATA)MYDATA=0-1F,F0–FF,100-1FF

Note: Copy initialization segments—BASENAME_I and BASENAME_ID—must be placed using -Z.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 53: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Placing code and data

Data segmentsThis section contains descriptions of the segments used for storing the different types of data: static, stack, heap, and located.

To get a clear understanding about how the data segments work, you must be familiar with the different memory types and the different data models available in the PIC18 IAR C/C++ Compiler. If you need to refresh these details, see the chapter Data storage.

STATIC MEMORY SEGMENTS

Static memory is memory that contains variables that are global or declared static, as described in the chapter Data storage. Declared static variables can be divided into the following categories:

● Variables that are initialized to a non-zero value● Variables that are initialized to zero● Variables that are located by use of the @ operator or the #pragma location

directive● Variables that are declared as const and therefore can be stored in ROM● Variables defined with the __no_init keyword, meaning that they should not be

initialized at all.

For the static memory segments it is important to be familiar with:

● The segment naming● How the memory types correspond to segment groups and the segments that are part

of the segment groups● Restrictions for segments holding initialized data● The placement and size limitation of the segments of each group of static memory

segments.

Segment naming

The names of the segments consist of two parts—the segment base name, or group name, and in some cases a suffix—for instance, EEPROM_Z. There is a segment group for each memory type, where each segment in the group holds different categories of declared data. The names of the segment groups are derived from the memory type and the corresponding keyword, for example EEPROM and __eeprom. The following table summarizes the memory types and the corresponding segment groups:

Memory type Segment group Memory range

Bank BANK 0x0–0xFFF

BankN BANKN 0x0–0xF7F*

Table 10: Memory types with corresponding segment groups

CPIC18-3

Part 1. Using the compiler 33

Page 54: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

34

Data segments

* The individual address ranges of the 16 bankN memory types can be seen in Table 4, page 13.

Some of the declared data is placed in non-volatile memory, for example ROM, and some of the data is placed in RAM. For this reason, it is also important to know the XLINK segment memory type of each segment. For more details about segment memory types, see Segment memory type, page 30.

The following table summarizes the different suffixes, which XLINK segment memory type they are, and which category of declared data they denote:

For a summary of all supported segments, see Summary of segments, page 131.

Examples

Assume the following examples:

Eeprom EEPROM 0x0–0xFFFF

Extmem EXTMEM 0x0–0x1FFFFF

Nonbanked BANKN 0x0–0x7F

Categories of declared data Segment memory type Suffix

Non-initialized data DATA N

Zero-initialized data DATA Z

Non-zero initialized data DATA I

Initializers for the above CONST ID

Absolute addressed data DATA A

Initializers zero initialized data CONST ZD

Table 11: Segment name suffixes

__bank1 int j;__bank1 int i = 0;

The bank1 variables that are to be initialized to zero when the system starts will be placed in the segment BANK1_Z.

__no_init __bank int j; The bank non-initialized variables will be placed in the segment BANK_N.

__bank int j = 4; The bank non-zero initialized variables will be placed in the segment BANK_I, and initializer data in segment BANK_ID.

Memory type Segment group Memory range

Table 10: Memory types with corresponding segment groups (Continued)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 55: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Placing code and data

Initialized data

When an application is started, the system startup code initializes static and global variables in two steps:

1 It clears the memory of the variables that should be initialized to zero.

2 It initializes the non-zero variables by copying a block of ROM to the location of the variables in RAM. This means that the data in the ROM segment with the suffix ID is copied to the corresponding I segment.

The ID segment can, for all segment groups, be placed anywhere in ROM, because it is not accessed using the corresponding access method.

3 Finally, global C++ objects are constructed, if any.

Data segments for static memory in the default linker command file

The default linker command file contains the following directives to place the static data segments:

/* Allocate EEPROM. */

-Z(XDATA)EEPROM_I,EEPROM_Z,EEPROM_N=[0:+_L_EEPROM_SIZE]/0100

/* Allocate External memory. */

-P(CODE)EXTMEM_I,EXTMEM_Z,EXTMEM_N=_L_EXTMEM_START: +_L_EXTMEM_SIZE

/* Allocate banked memories. */

-Z(DATA)WRKSEG,BANKN_I,BANKN_Z,BANKN_N=0-_L_BANKN_SIZE-Z(DATA)OVERLAY0,BANK0_I,BANK0_Z,BANK0_N=0:+_L_BANK0_SIZE-Z(DATA)OVERLAY1,BANK1_I,BANK1_Z,BANK1_N=100:+_L_BANK1_SIZE-Z(DATA)OVERLAY2,BANK2_I,BANK2_Z,BANK2_N=200:+_L_BANK2_SIZE-Z(DATA)OVERLAY3,BANK3_I,BANK3_Z,BANK3_N=300:+_L_BANK3_SIZE-Z(DATA)OVERLAY4,BANK4_I,BANK4_Z,BANK4_N=400:+_L_BANK4_SIZE-Z(DATA)OVERLAY5,BANK5_I,BANK5_Z,BANK5_N=500:+_L_BANK5_SIZE-Z(DATA)OVERLAY6,BANK6_I,BANK6_Z,BANK6_N=600:+_L_BANK6_SIZE-Z(DATA)OVERLAY7,BANK7_I,BANK7_Z,BANK7_N=700:+_L_BANK7_SIZE-Z(DATA)OVERLAY8,BANK8_I,BANK8_Z,BANK8_N=800:+_L_BANK8_SIZE-Z(DATA)OVERLAY9,BANK9_I,BANK9_Z,BANK9_N=900:+_L_BANK9_SIZE-Z(DATA)OVERLAY10,BANK10_I,BANK10_Z,BANK10_N=0A00:+_L_BANK10_SIZE-Z(DATA)OVERLAY11,BANK11_I,BANK11_Z,BANK11_N=0B00:+_L_BANK11_SIZE-Z(DATA)OVERLAY12,BANK12_I,BANK12_Z,BANK12_N=0C00:+_L_BANK12_SIZE-Z(DATA)OVERLAY13,BANK13_I,BANK13_Z,BANK13_N=0D00:+_L_BANK13_SIZE-Z(DATA)OVERLAY14,BANK14_I,BANK14_Z,BANK14_N=0E00:+_L_BANK14_SIZE-Z(DATA)OVERLAY15,BANK15_I,BANK15_Z,BANK15_N=0F00:+_L_BANK15_SIZE

CPIC18-3

Part 1. Using the compiler 35

Page 56: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

36

Data segments

-Z(DATA)HEAP+_L_HEAP_SIZE=_L_BANK_START-_L_BANK_END-Z(DATA)STACK+_L_STACK_SIZE=_L_BANK_START-_L_BANK_END-P(DATA)OVERLAY,BANK_I,BANK_Z,BANK_N= [_L_BANK_START-_L_BANK_END]/0100

THE OVERLAY SYSTEM

The overlay system is used by functions to store variables and other information that is used locally by functions, as described in the chapter Data storage.

The data segment used for holding the overlay frames is called OVERLAY.

Allocating a memory area for the overlay system is done differently when you use the command line interface compared to when you use the IAR Embedded Workbench IDE.

THE STACK

The stack is used by functions to store variables and other information that is used locally by functions, as described in the chapter Data storage. It is a continuous block of memory pointed to by the special function register FSR2.

The data segment used for holding the stack is called STACK. The system startup code initializes the stack pointer to the end of the stack segment.

Allocating a memory area for the stack is done differently when you use the command line interface compared to when you use the IAR Embedded Workbench IDE.

Stack size allocation in the IAR Embedded Workbench IDE

Select Project>Options. In the General Options category, click the Stack/Heap page.

Add the required stack size in the Stack size text box.

Stack size allocation from the command line

The size of the STACK segment is defined in the linker command file.

The default linker file sets up a constant representing the size of the stack, at the beginning of the linker file:

-D_STACK_SIZE=size

Specify an appropriate size for your application. Note that the size is written hexadecimally without the 0x notation.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 57: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Placing code and data

Placement of stack segment

Further down in the linker file, the actual stack segment is defined in the memory area available for the stack:

-Z(DATA)STACK+_STACK_SIZE=0-F7F

Note: This range does not specify the size of the stack; it specifies the range of the available memory.

Stack size considerations

The compiler uses the internal data stack, STACK, for a variety of user program operations, and the required stack size depends heavily on the details of these operations. If the given stack size is too large, RAM will be wasted. If the given stack size is too small, there are two things that can happen, depending on where in memory you have located your stack. Both alternatives are likely to result in application failure. Either variable storage will be overwritten, leading to undefined behavior, or the stack will fall outside of the memory area, leading to an abnormal termination of your application. Because the second alternative is easier to detect, you should consider placing your stack so that it grows towards the end of the memory.

THE HEAP

The heap contains dynamic data allocated by use of the C function malloc (or one of its relatives) or the C++ operator new.

If your application uses dynamic memory allocation, you should be familiar with the following:

● Linker segment used for the heap● Allocating the heap size, which differs depending on which build interface you are

using● Placing the heap segment in memory.

The memory allocated to the heap is placed in the segment HEAP, which is only included in the application if dynamic memory allocation is actually used.

Heap size allocation in the IAR Embedded Workbench IDE

Select Project>Options. In the General Options category, click the Stack/Heap page.

Add the required heap size in the Heap size text box.

Heap size allocation from the command line

The size of the heap segment is defined in the linker command file.

CPIC18-3

Part 1. Using the compiler 37

Page 58: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

38

Code segments

The default linker file sets up a constant, representing the size of the heap, at the beginning of the linker file:

-D_HEAP_SIZE=size

Specify the appropriate size for your application.

Placement of heap segment

The actual heap segment is allocated in the memory area available for the heap:

-Z(DATA)HEAP+_HEAP_SIZE=100-1FF

Note: This range does not specify the size of the heap; it specifies the range of the available memory.

Heap size and standard I/O

If you have excluded FILE descriptors from the DLIB runtime environment, like in the normal configuration, there are no input and output buffers at all. Otherwise, like in the full configuration, be aware that the size of the input and output buffers is set to 80 bytes in the stdio library header file. If the heap is too small, I/O will not be buffered, which is considerably slower than when I/O is buffered. If you execute the application using the simulator driver of the IAR C-SPY Debugger, you are not likely to notice the speed penalty, but it is quite noticeable when the application runs on an PIC18 microcontroller. If you use the standard I/O library, you should set the heap size to a value which accommodates the needs of the standard I/O buffer.

LOCATED DATA

A variable that has been explicitly placed at an address, for example by using the compiler @ syntax, will be placed in the BASENAME_A segment. (See Segment naming, page 33.) The individual segment part of the segment knows its location in the memory space, and it does not have to be specified in the linker command file.

If you create your own segments, these must also be defined in the linker command file using the -Z or -P segment control directives.

Code segments This section contains descriptions of the segments used for storing code, and the interrupt vector table. For a complete list of all segments, see Summary of segments, page 131.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 59: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Placing code and data

STARTUP CODE

The segment ICODE contains code used during system setup, runtime initialization, and system termination. In addition, the segments must be placed into one continuous memory space, which means the -P segment directive cannot be used.

NORMAL CODE

Code for normal functions is placed in the CODE segment. Again, this is a simple operation in the linker command file:

-Z(CODE)CODE=0-FFFF

INTERRUPT VECTORS

The interrupt vector table contains pointers to interrupt routines, including the reset routine. The table is placed in the segment INTVEC. For the PIC18 microcontroller, you must place this segment at the address 0x0. The linker directive would then look like this:

-Z(CODE)INTVEC=0000-0027

C++ dynamic initialization In C++, all global objects will be created before the main function is called. The creation of objects can involve the execution of a constructor.

The DIFUNCT segment contains a vector of addresses that point to initialization code. All entries in the vector will be called when the system is initialized.

For example:

-Z(CONST)DIFUNCT=0004-FFFF

For additional information, see DIFUNCT, page 139.

Efficient usage of segments and memoryThis section lists several features and methods to help you manage memory and segments.

CPIC18-3

Part 1. Using the compiler 39

Page 60: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

40

Efficient usage of segments and memory

CONTROLLING DATA AND FUNCTION PLACEMENT

The @ operator, alternatively the #pragma location directive, can be used for placing global and static variables at absolute addresses. The syntax can also be used for placing variables or functions in named segments. The variables must be declared either __no_init or const. If declared const, it is legal for them to have initializers. The named segment can either be a predefined segment, or a user-defined segment.

Note: Take care when explicitly placing a variable or function in a predefined segment other than the one used by default. This is possible and useful in some situations, but incorrect placement can result in anything from error messages during compilation and linking to a malfunctioning application. Carefully consider the circumstances; there might be strict requirements on the declaration and use of the function or variable.

C++ static member variables can be placed at an absolute address or in named segments, just like any other static variable.

Data placement at an absolute location

To place a variable at an absolute address, the argument to the operator @ and the #pragma location directive should be a literal number, representing the actual address.

Example

__no_init char alpha @ 0x10; /* OK */

#pragma location=0x12const int beta; /* OK */

const int gamma @ 0x14 = 3; /* OK */

int delta @ 0x16; /* Error, neither */ /* "__no_init" nor "const".*/

Note: A variable placed in an absolute location should be defined in an include file, to be included in every module that uses the variable. An unused definition in a module will be ignored. A normal extern declaration—one that does not use an absolute placement directive—can refer to a variable at an absolute address; however, optimizations based on the knowledge of the absolute address cannot be performed.

Data placement into named segments

It is possible to place variables into named segments using either the @ operator or the #pragma location directive. A string should be used for specifying the segment name.

For information about segments, see the chapter Placing code and data.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 61: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Placing code and data

Example

__no_init int alpha @ "MYSEGMENT"; /* OK */

#pragma location="MYSEGMENT"const int beta; /* OK */

const int gamma @ "MYSEGMENT" = 3; /* OK */

int delta @ "MYSEGMENT"; /* Error, neither *//* "__no_init" nor "const" */

Function placement into named segments

It is possible to place functions into named segments using either the @ operator or the #pragma location directive. When placing functions into segments, the segment is specified as a string literal.

Example

void f(void) @ "MYSEGMENT";void g(void) @ "MYSEGMENT"{}

#pragma location="MYSEGMENT"void h(void);

CREATING USER-DEFINED SEGMENTS

In addition to the predefined segments, you can create your own segments. This is useful if you need to have precise control of placement of individual variables or functions.

A typical situation where this can be useful is if you need to optimize accesses to code and data that is frequently used, and place it in a different physical memory.

To create your own segments, use the #pragma location directive.

Verifying the linked result of code and data placementThe linker has several features that help you to manage code and data placement, for example, messages at link time and the linker map file.

CPIC18-3

Part 1. Using the compiler 41

Page 62: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

42

Verifying the linked result of code and data placement

SEGMENT TOO LONG ERRORS AND RANGE ERRORS

All code and data that is placed in relocatable segments will have its absolute addresses resolved at link time. It is also at link time it is known whether all segments will fit in the reserved memory ranges. If the contents of a segment do not fit in the address range defined in the linker command file, XLINK will issue a segment too long error.

Some instructions do not work unless a certain condition holds after linking, for example that a branch must be within a certain distance or that an address must be even. XLINK verifies that the conditions hold when the files are linked. If a condition is not satisfied, XLINK generates a range error or warning and prints a description of the error.

For further information about these types of errors, see the IAR Linker and Library Tools Reference Guide.

LINKER MAP FILE

XLINK can produce an extensive cross-reference listing, which can optionally contain the following information:

● A segment map which lists all segments in dump order● A module map which lists all segments, local symbols, and entries (public symbols)

for every module in the program. All symbols not included in the output can also be listed

● Module summary which lists the contribution (in bytes) from each module● A symbol list which contains every entry (global symbol) in every module.

Use the option Generate linker listing in the Embedded Workbench IDE, or the option -X on the command line, and one of their suboptions to generate a linker listing.

Normally, XLINK will not generate an output file if there are any errors, such as range errors, during the linking process. Use the option Range checks disabled in the Embedded Workbench IDE, or the option -R on the command line, to generate an output file even if a range error was encountered.

For further information about the listing options and the linker listing, see the IAR Linker and Library Tools Reference Guide, and the IAR Embedded Workbench® IDE User Guide.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 63: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Placing code and data

MANAGING MULTIPLE ADDRESS SPACES

Output formats that do not support more than one memory space—like MOTOROLA and INTEL-HEX—may require up to one output file per memory space. This causes no problems if you are only producing output to one memory space (FLASH), but if you also are placing objects in EEPROM or an external ROM in DATA space, the output format cannot represent this, and the linker issues the following error message:

Error[e133]: The output format Format cannot handle multiple address spaces. Use format variants (-y -O) to specify which address space is wanted.

To limit the output to FLASH, make a copy of the linker command file for the derivative and memory type you are using, and put it in the project directory. Add the following line at the end of the file:

-y(CODE)

To produce output for the other memory space(s), you must generate one output file per memory space (because the output format you have chosen does not support more than one memory space). Use the XLINK option -O for this purpose.

For each additional output file, you have to specify format, XLINK segment type, and file name. For example:

-Omotorola,(DATA)=external_rom.a49-Omotorola,(EEPROM)=eeprom.a49

Note: As a general rule, an output file is only necessary if you use non-volatile memory. In other words, output from the data space is only necessary if the data space contains external ROM.

CPIC18-3

Part 1. Using the compiler 43

Page 64: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

44

Verifying the linked result of code and data placement

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 65: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environmentThis chapter describes the runtime environment in which an application executes. In particular, the chapter covers the DLIB runtime library and how you can modify it—setting options, overriding default library modules, or building your own library—to optimize it for your application.

The chapter also covers system initialization and termination; how an application can control what happens before the function main is called, and how you can customize the initialization.

The chapter then describes how to configure functionality like locale and file I/O, how to get C-SPY runtime support, and how to prevent incompatible modules from being linked together.

For information about the CLIB runtime environment, see the chapter The CLIB runtime environment.

Introduction to the runtime environmentThe runtime environment is the environment in which your application executes. The runtime environment depends on the target hardware, the software environment, and the application code. The IAR DLIB runtime environment can be used as is together with the IAR C-SPY Debugger. However, to be able to run the application on hardware, you must adapt the runtime environment.

This section gives an overview of:

● The runtime environment and its components● Library selection.

RUNTIME ENVIRONMENT FUNCTIONALITY

The runtime environment (RTE) supports ISO/ANSI C and C++ including the standard template library. The runtime environment consists of the runtime library, which contains the functions defined by these standards, and include files that define the library interface.

CPIC18-3

Part 1. Using the compiler 45

Page 66: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

46

Introduction to the runtime environment

The runtime library is delivered both as prebuilt libraries and as source files, and you can find them in the product subdirectories pic18\lib and pic18\src, respectively.

The runtime environment also consists of a part with specific support for the target system, which includes:

● Support for hardware features:● Direct access to low-level processor operations by means of intrinsic functions,

such as functions for register handling● Peripheral unit registers and interrupt definitions in include files

● Runtime environment support, that is, startup and exit code and low-level interface to some library functions.

● Special compiler support for some functions, for instance functions for floating-point arithmetics

Some parts, like the startup and exit code and the size of the heap, must be tailored for the specific hardware and application requirements.

For further information about the library, see the chapter Library functions.

LIBRARY SELECTION

To configure the most code-efficient runtime environment, you must determine your application and hardware requirements. The more functionality you need, the larger your code will get.

IAR Embedded Workbench comes with a set of prebuilt runtime libraries. To get the required runtime environment, you can customize it by:

● Setting library options, for example, for choosing scanf input and printf output formatters, and for specifying the size of the stack and the heap

● Overriding certain library functions, for example cstartup.s49, with your own customized versions

● Choosing the level of support for certain standard library functionality, for example, locale, file descriptors, and multibytes, by choosing a library configuration: normal or full.

In addition, you can also make your own library configuration, but that requires that you rebuild the library. This gives you full control of the runtime environment.

Note: Your application project must be able to locate the library, include files, and the library configuration file.

SITUATIONS THAT REQUIRE LIBRARY BUILDING

Building a customized library is complex. You should therefore carefully consider whether it is really necessary.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 67: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

You must build your own library when:

● There is no prebuilt library for the required combination of compiler options or hardware support

● You want to define your own library configuration with support for locale, file descriptors, multibyte characters, et cetera.

For information about how to build a customized library, see Building and using a customized library, page 54.

LIBRARY CONFIGURATIONS

It is possible to configure the level of support for, for example, locale, file descriptors, multibytes. The runtime library configuration is defined in the library configuration file. It contains information about what functionality is part of the runtime environment. The configuration file is used for tailoring a build of a runtime library, as well as tailoring the system header files used when compiling your application. The less functionality you need in the runtime environment, the smaller it is.

The following DLIB library configurations are available:

In addition to these configurations, you can define your own configurations, which means that you must modify the configuration file. Note that the library configuration file describes how a library was built and thus cannot be changed unless you rebuild the library. For further information, see Building and using a customized library, page 54.

The prebuilt libraries are based on the default configurations, see Table 14, Prebuilt DLIB libraries, page 49. There is also a ready-made library project template that you can use if you want to rebuild the runtime library.

DEBUG SUPPORT IN THE RUNTIME LIBRARY

You can make the library provide different levels of debugging support—basic, runtime, and I/O debugging.

Library configuration Description

Normal DLIB No locale interface, C locale, no file descriptor support, no multibyte characters in printf and scanf, and no hex floats in strtod.

Full DLIB Full locale interface, C locale, file descriptor support, multibyte characters in printf and scanf, and hex floats in strtod.

Table 12: Library configurations

CPIC18-3

Part 1. Using the compiler 47

Page 68: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

48

Using a prebuilt library

The following table describes the different levels of debugging support:

If you build your application project with the XLINK options With runtime control modules or With I/O emulation modules, certain functions in the library will be replaced by functions that communicate with the IAR C-SPY Debugger. For further information, see C-SPY Debugger runtime interface, page 67.

To set linker options for debug support in the IAR Embedded Workbench IDE, choose Project>Options and select the Linker category. On the Output page, select the appropriate Format option.

Using a prebuilt libraryThe prebuilt runtime libraries are configured for different combinations of the following features:

● Code model● Data model● Library configuration—Normal or Full.

Debugging

support

Linker option in

IDE

Linker command

line optionDescription

Basic debugging Debug information for C-SPY

-Fubrof Debug support for C-SPY without any runtime support

Runtime debugging With runtime control modules

-r The same as -Fubrof, but also includes debugger support for handling program abort, exit, and assertions.

I/O debugging With I/O emulation modules

-rt The same as -r, but also includes debugger support for I/O handling, which means that stdin and stdout are redirected to the C-SPY Terminal I/O window, and that it is possible to access files on the host computer during debugging.

Table 13: Levels of debugging support in runtime libraries

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 69: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

For the PIC18 IAR C/C++ Compiler, this means there is a prebuilt runtime library for each combination of these options. The following table shows the mapping of the library file, code model, data model, and library configuration:

The names of the libraries are constructed in the following way:

<type><cpu_variant><code_model><data_model><library_config>.r49

where

● <type> is dl for the IAR DLIB runtime environment● <cpu_variant> is 18● <code_model> is one of o or s for overlay or stack, respectively● <data_model> is one of s or l for small or large, respectively● <library_config> is one of n or f for normal and full, respectively.

Note: The library configuration file has the same base name as the library.

the IAR Embedded Workbench IDE will include the correct library object file and library configuration file based on the options you select. See the IAR Embedded Workbench® IDE User Guide for additional information.

On the command line, you must specify the following items:

● Specify which library object file to use on the XLINK command line, for instance:dl18s1n.r49

● Specify the include paths for the compiler and assembler:-I pic18\inc

● Specify the library configuration file for the compiler:--dlib_config C:\...\dl18s1n.h

You can find the library object files and the library configuration files in the subdirectory pic18\lib.

Library Code model Data model Library configuration

dl18osn.r49 Overlay Small Normal

dl18osf.r49 Overlay Small Full

dl18oln.r49 Overlay Large Normal

dl18olf.r49 Overlay Large Full

dl18ssn.r49 Stack Small Normal

dl18ssf.r49 Stack Small Full

dl18sln.r49 Stack Large Normal

dl18slf.r49 Stack Large Full

Table 14: Prebuilt DLIB libraries

CPIC18-3

Part 1. Using the compiler 49

Page 70: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

50

Choosing formatters for printf and scanf

CUSTOMIZING A PREBUILT LIBRARY WITHOUT REBUILDING

The prebuilt libraries delivered with the PIC18 IAR C/C++ Compiler can be used as is. However, it is possible to customize parts of a library without rebuilding it. There are two different methods:

● Setting options for:● Formatters used by printf and scanf● The sizes of the heap and the stack

● Overriding library modules with your own customized versions.

The following items can be customized:

For a description about how to override library modules, see Overriding library modules, page 52.

Choosing formatters for printf and scanfTo override the default formatter for all the printf- and scanf-related functions, except for wprintf and wscanf variants, you simply set the appropriate library options. This section describes the different options available.

Note: If you rebuild the library, it is possible to optimize these functions even further, see Configuration symbols for printf and scanf, page 60.

CHOOSING PRINTF FORMATTER

The printf function uses a formatter called _Printf. The default version is quite large, and provides facilities not required in many embedded applications. To reduce the memory consumption, three smaller, alternative versions are also provided in the standard C/EC++ library.

Items that can be customized Described on page

Formatters for printf and scanf Choosing formatters for printf and scanf, page 50

Startup and termination code System startup and termination, page 56

Low-level input and output Standard streams for input and output, page 59

File input and output File input and output, page 62

Low-level environment functions Environment interaction, page 65

Low-level signal functions Signal and raise, page 65

Low-level time functions Time, page 66

Size of heap, stacks, and segments Placing code and data, page 29

Table 15: Customizable items

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 71: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

The following table summarizes the capabilities of the different formatters:

* Depends on which library configuration is used.

For information about how to fine-tune the formatting capabilities even further, see Configuration symbols for printf and scanf, page 60.

Specifying the print formatter in the IAR Embedded Workbench IDE

To specify the printf formatter in the IAR Embedded Workbench IDE, choose Project>Options and select the General Options category. Select the appropriate option on the Library options page.

Specifying printf formatter from the command line

To use any other variant than the default (_PrintfFull), add one of the following lines in the linker command file you are using:

-e_PrintfLarge=_Printf-e_PrintfSmall=_Printf-e_PrintfTiny=_Printf

CHOOSING SCANF FORMATTER

In a similar way to the printf function, scanf uses a common formatter, called _Scanf. The default version is very large, and provides facilities that are not required in many embedded applications. To reduce the memory consumption, two smaller, alternative versions are also provided in the standard C/C++ library.

Formatting capabilities_PrintfFull

(default)_PrintfLarge _PrintfSmall _PrintfTiny

Basic specifiers c, d, i, o, p, s, u, X, x, and %

Yes Yes Yes Yes

Multibyte support * * * No

Floating-point specifiers a, and A Yes No No No

Floating-point specifiers e, E, f, F, g, and G

Yes Yes No No

Conversion specifier n Yes Yes No No

Format flag space, +, -, #, and 0 Yes Yes Yes No

Length modifiers h, l, L, s, t, and Z Yes Yes Yes No

Field width and precision, including * Yes Yes Yes No

long long support Yes Yes No No

Table 16: Formatters for printf

CPIC18-3

Part 1. Using the compiler 51

Page 72: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

52

Overriding library modules

The following table summarizes the capabilities of the different formatters:

* Depends on which library configuration that is used.

For information about how to fine-tune the formatting capabilities even further, see Configuration symbols for printf and scanf, page 60.

Specifying scanf formatter in the IAR Embedded Workbench IDE

To specify the scanf formatter in the IAR Embedded Workbench IDE, choose Project>Options and select the General Options category. Select the appropriate option on the Library options page.

Specifying scanf formatter from the command line

To use any other variant than the default (_ScanfFull), add one of the following lines in the linker command file you are using:

-e_ScanfLarge=_Scanf-e_ScanfSmall=_Scanf

Overriding library modulesThe library contains modules which you probably need to override with your own customized modules, for example functions for character-based I/O and cstartup. This can be done without rebuilding the entire library. This section describes the procedure for including your version of the module in the application project build process. The library files that you can override with your own versions are located in the pic18\src\lib directory.

Formatting capabilities _ScanfFull (default) _ScanfLarge _ScanfSmall

Basic specifiers c, d, i, o, p, s, u, X, x, and %

Yes Yes Yes

Multibyte support * * *

Floating-point specifiers a, and A Yes No No

Floating-point specifiers e, E, f, F, g, and G

Yes No No

Conversion specifier n Yes No No

Scan set [ and ] Yes Yes No

Assignment suppressing * Yes Yes No

long long support Yes No No

Table 17: Formatters for scanf

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 73: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

Note: If you override a default I/O library module with your own module, C-SPY support for the module is turned off. For example, if you replace the module __write with your own version, the C-SPY Terminal I/O window will not be supported.

Overriding library modules using the IAR Embedded Workbench IDE

This procedure is applicable to any source file in the library, which means library_module.c in this example can be any module in the library.

1 Copy the appropriate library_module.c file to your project directory.

2 Make the required additions to the file (or create your own routine, using the default file as a model), and make sure that it has the same module name as the original module. The easiest way to achieve this is to save the new file under the same name as the original file.

3 Add the customized file to your project.

4 Rebuild your project.

Overriding library modules from the command line

This procedure is applicable to any source file in the library, which means library_module.c in this example can be any module in the library.

1 Copy the appropriate library_module.c to your project directory.

2 Make the required additions to the file (or create your own routine, using the default file as a model), and make sure that it has the same module name as the original module. The easiest way to achieve this is to save the new file under the same name as the original file.

3 Compile the modified file using the same options as for the rest of the project:

iccpic18 library_module

This creates a replacement object module file named library_module.r49.

Note: The code model, the data model, include paths, and the library configuration file must be the same for library_module as for the rest of your code.

4 Add library_module.r49 to the XLINK command line, either directly or by using an extended linker command file, for example:

xlink library_module dlpic18s0n.r49

Make sure that library_module is located before the library on the command line. This ensures that your module is used instead of the one in the library.

Run XLINK to rebuild your application.

CPIC18-3

Part 1. Using the compiler 53

Page 74: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

54

Building and using a customized library

This will use your version of library_module.r49, instead of the one in the library. For information about the XLINK options, see the IAR Linker and Library Tools Reference Guide.

Building and using a customized libraryIn some situations, see Situations that require library building, page 46, it is necessary to rebuild the library. In those cases you need to:

● Set up a library project● Make the required library modifications● Build your customized library● Finally, make sure your application project will use the customized library.

Information about the build process is described in IAR Embedded Workbench® IDE User Guide.

Note: It is possible to build IAR Embedded Workbench projects from the command line by using the IAR Command Line Build Utility (iarbuild.exe). However, no make or batch files for building the library from the command line are provided.

SETTING UP A LIBRARY PROJECT

IAR Embedded Workbench provides a library project template which can be used for customizing the runtime environment configuration. This library template has full library configuration, see Table 12, Library configurations, page 47.

In the IAR Embedded Workbench IDE, modify the generic options in the created library project to suit your application, see Basic settings for project configuration, page 5.

Note: There is one important restriction on setting options. If you set an option on file level (file level override), no options on higher levels that operate on files will affect that file.

MODIFYING THE LIBRARY FUNCTIONALITY

You must modify the library configuration file and build your own library to modify support for, for example, locale, file descriptors, and multibytes. This will include or exclude certain parts of the runtime environment.

The library functionality is determined by a set of configuration symbols. The default values of these symbols are defined in the file Dlib_defaults.h. This read-only file describes the configuration possibilities. In addition, your library has its own library configuration file dlpic18Custom.h, which sets up that specific library with full library configuration. For more information, see Table 15, Customizable items, page 50.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 75: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

The library configuration file is used for tailoring a build of the runtime library, as well as tailoring the system header files.

Modifying the library configuration file

In your library project, open the file dl18Custom.h and customize it by setting the values of the configuration symbols according to the application requirements.

When you are finished, build your library project with the appropriate project options.

USING A CUSTOMIZED LIBRARY

After you have built your library, you must make sure to use it in your application project.

In the IAR Embedded Workbench IDE you must perform the following steps:

1 Choose Project>Options and click the Library Configuration tab in the General Options category.

2 Choose Custom DLIB from the Library drop-down menu.

3 In the Library file text box, locate your library file.

4 In the Configuration file text box, locate your library configuration file.

CPIC18-3

Part 1. Using the compiler 55

Page 76: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

56

System startup and termination

System startup and terminationThis section describes the runtime environment actions performs during startup and termination of applications. The following figure gives a graphical overview of the startup and exit sequences:

Figure 1: Startup and exit sequences

The code for handling startup and termination is located in the source files cstartup.s49, dlib.sup.s49, and low_level_init.c located in the pic18\src\lib directory.

Reset

__low_level_init

Hardware setup

Static initialization

Dynamic C++ initialization

exit

abort

_Exit

__exit

Application

main

Program entry label

cstartup

Return from main and call exit

System terminated

_exit

Dynamic C++ destruction and atexit execution

cexit

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 77: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

SYSTEM STARTUP

When an application is initialized, a number of steps are performed:

● The stack pointer is initialized to the end of STACK ● When the cpu is reset it will jump to the program entry label __program_start in

the system startup code.● The function __low_level_init is called, giving the application a chance to

perform early initializations● Static variables are initialized; this includes clearing zero-initialized memory and

copying the ROM image of the RAM memory of the rest of the initialized variables depending on the return value of __low_level_init

● Static C++ objects are constructed● The main function is called, which starts the application.

SYSTEM TERMINATION

An application can terminate normally in two different ways:

● Return from the main function● Call the exit function.

Because the ISO/ANSI C standard states that the two methods should be equivalent, the system startup code calls the exit function if main returns. The parameter passed to the exit function is the return value of main.

The default exit function is written in C. It calls a small function _exit, written in assembler, that will perform the following operations:

● Call functions registered to be executed when the application ends. This includes C++ destructors for static and global variables, and functions registered with the standard C function atexit

● Close all open files● Call __exit● When __exit is reached, stop the system.

An application can also exit by calling the abort or the _Exit function. The abort function just calls __exit to halt the system, and does not perform any type of cleanup. The _Exit function is equivalent to the abort function, except for the fact that _Exit takes an argument for passing exit status information.

If you want your application to perform anything extra at exit, for example resetting the system, you can write your own implementation of the __exit(int) function.

CPIC18-3

Part 1. Using the compiler 57

Page 78: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

58

Customizing system initialization

C-SPY interface to system termination

If your project is linked with the XLINK options With runtime control modules or With I/O emulation modules, the normal __exit and abort functions are replaced with special ones. C-SPY will then recognize when those functions are called and can take appropriate actions to simulate program termination. For more information, see C-SPY Debugger runtime interface, page 67.

Customizing system initializationIt is likely that you need to customize the code for system initialization. For example, your application might need to initialize memory-mapped special function registers (SFRs), or omit the default initialization of data segments performed by cstartup.

You can do this by providing a customized version of the routine __low_level_init, which is called from cstartup before the data segments are initialized. Modifying the file cstartup directly should be avoided.

The code for handling system startup is located in the source files cstartup.s49 and low_level_init.c, located in the pic18\src directory.

Note: Normally, there is no need for customizing either of the files cstartup.s49 or dlib_sup.s49.

If you intend to rebuild the library, the source files are available in the template library project, see Building and using a customized library, page 54.

Note: Regardless of whether you modify the routine __low_level_init or the file cstartup.s49, you do not have to rebuild the library.

__LOW_LEVEL_INIT

A skeleton low-level initialization file is supplied with the product, low_level_init.c. The value returned by __low_level_init determines whether or not data segments should be initialized by the system startup code. If the function returns 0, the data segments will not be initialized.

MODIFYING THE FILE CSTARTUP.S49

As noted earlier, you should not modify the file cstartup.s49 if a customized version of __low_level_init is enough for your needs. However, if you do need to modify the file cstartup.s49, we recommend that you follow the general procedure for creating a modified copy of the file and adding it to your project, see Overriding library modules, page 52.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 79: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

Standard streams for input and outputThere are three standard communication channels (streams)—stdin, stdout, and stderr—which are defined in stdio.h. If any of these streams are used by your application, for example by the functions printf and scanf, you need to customize the low-level functionality to suit your hardware.

There are primitive I/O functions, which are the fundamental functions through which C and C++ performs all character-based I/O. For any character-based I/O to be available, you must provide definitions for these functions using whatever facilities the hardware environment provides.

IMPLEMENTING LOW-LEVEL CHARACTER INPUT AND OUTPUT

To implement low-level functionality of the stdin and stdout streams, you must write the functions __read and __write, respectively. You can find template source code for these functions in the pic18\src directory.

If you intend to rebuild the library, the source files are available in the template library project, see Building and using a customized library, page 54. Note that customizing the low-level routines for input and output does not require you to rebuild the library.

Note: If you write your own variants of __read or __write, special considerations for the C-SPY runtime interface are needed, see C-SPY Debugger runtime interface, page 67.

Example of using __write and __read

The code in the following examples use memory-mapped I/O to write to an LCD display:

__no_init volatile unsigned char LCD_IO @ address;

size_t __write(int Handle, const unsigned char * Buf, size_t Bufsize){ int nChars = 0; /* Check for stdout and stderr (only necessary if file descriptors are enabled.) */ if (Handle != 1 && Handle != 2) { return -1; } for (/*Empty */; Bufsize > 0; --Bufsize) { LCD_IO = * Buf++;

CPIC18-3

Part 1. Using the compiler 59

Page 80: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

60

Configuration symbols for printf and scanf

++nChars; } return nChars;}

The code in the following example uses memory-mapped I/O to read from a keyboard:

__no_init volatile unsigned char KB_IO @ 0xD2;

size_t __read(int Handle, unsigned char *Buf, size_t BufSize){ int nChars = 0; /* Check for stdin (only necessary if FILE descriptors are enabled) */ if (Handle != 0) { return -1; } for (/*Empty*/; BufSize > 0; --BufSize) { int c = KB_IO; if (c < 0) break; *Buf++ = c; ++nChars; } return nChars;}

For information about the @ operator, see Controlling data and function placement, page 40.

Configuration symbols for printf and scanfWhen you set up your application project, you typically need to consider what printf and scanf formatting capabilities your application requires, see Choosing formatters for printf and scanf, page 50.

If the provided formatters do not meet your requirements, you can customize the full formatters. However, that means you need to rebuild the runtime library.

The default behavior of the printf and scanf formatters are defined by configuration symbols in the file DLIB_Defaults.h.

The following configuration symbols determine what capabilities the function printf should have:

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 81: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

When you build a library, the following configurations determine what capabilities the function scanf should have:

CUSTOMIZING FORMATTING CAPABILITIES

To customize the formatting capabilities, you need to set up a library project, see Building and using a customized library, page 54. Define the configuration symbols according to your application requirements.

Printf configuration symbols Includes support for

_DLIB_PRINTF_MULTIBYTE Multibyte characters

_DLIB_PRINTF_LONG_LONG Long long (ll qualifier)

_DLIB_PRINTF_SPECIFIER_FLOAT Floating-point numbers

_DLIB_PRINTF_SPECIFIER_A Hexadecimal floats

_DLIB_PRINTF_SPECIFIER_N Output count (%n)

_DLIB_PRINTF_QUALIFIERS Qualifiers h, l, L, v, t, and z

_DLIB_PRINTF_FLAGS Flags -, +, #, and 0

_DLIB_PRINTF_WIDTH_AND_PRECISION Width and precision

_DLIB_PRINTF_CHAR_BY_CHAR Output char by char or buffered

Table 18: Descriptions of printf configuration symbols

Scanf configuration symbols Includes support for

_DLIB_SCANF_MULTIBYTE Multibyte characters

_DLIB_SCANF_LONG_LONG Long long (ll qualifier)

_DLIB_SCANF_SPECIFIER_FLOAT Floating-point numbers

_DLIB_SCANF_SPECIFIER_N Output count (%n)

_DLIB_SCANF_QUALIFIERS Qualifiers h, j, l, t, z, and L

_DLIB_SCANF_SCANSET Scanset ([*])

_DLIB_SCANF_WIDTH Width

_DLIB_SCANF_ASSIGNMENT_SUPPRESSING Assignment suppressing ([*])

Table 19: Descriptions of scanf configuration symbols

CPIC18-3

Part 1. Using the compiler 61

Page 82: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

62

File input and output

File input and outputThe library contains a large number of powerful functions for file I/O operations. If you use any of these functions you need to customize them to suit your hardware. In order to simplify adaptation to specific hardware, all I/O functions call a small set of primitive functions, each designed to accomplish one particular task; for example, __open opens a file, and __write outputs a number of characters.

Note that file I/O capability in the library is only supported by libraries with full library configuration, see Library configurations, page 47. In other words, file I/O is supported when the configuration symbol __DLIB_FILE_DESCRIPTOR is enabled. If not enabled, functions taking a FILE * argument cannot be used.

Template code for the following I/O files are included in the product:

The primitive functions identify I/O streams, such as an open file, with a file descriptor that is a unique integer. The I/O streams normally associated with stdin, stdout, and stderr have the file descriptors 0, 1, and 2, respectively.

Note: If you link your library with I/O debugging support, C-SPY variants of the low-level I/O functions will be linked for interaction with C-SPY. For more information, see Debug support in the runtime library, page 47.

LocaleLocale is a part of the C language that allows language- and country-specific settings for a number of areas, such as currency symbols, date and time, and multibyte encoding.

Depending on what runtime library you are using you get different level of locale support. However, the more locale support, the larger your code will get. It is therefore necessary to consider what level of support your application needs.

I/O function File Description

__close close.c Closes a file.

__lseek lseek.c Sets the file position indicator.

__open open.c Opens a file.

__read read.c Reads a character buffer.

__write write.c Writes a character buffer.

remove remove.c Removes a file.

rename rename.c Renames a file.

Table 20: Low-level I/O files

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 83: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

The DLIB library can be used in two major modes:

● With locale interface, which makes it possible to switch between different locales during runtime

● Without locale interface, where one selected locale is hardwired into the application.

LOCALE SUPPORT IN PREBUILT LIBRARIES

The level of locale support in the prebuilt libraries depends on the library configuration.

● All prebuilt libraries supports the C locale only● All libraries with full library configuration have support for the locale interface. For

prebuilt libraries with locale interface, it is by default only supported to switch multibyte encoding during runtime.

● Libraries with normal library configuration do not have support for the locale interface.

If your application requires a different locale support, you need to rebuild the library.

CUSTOMIZING THE LOCALE SUPPORT

If you decide to rebuild the library, you can choose between the following locales:

● The standard C locale● The POSIX locale● A wide range of international locales.

Locale configuration symbols

The configuration symbol _DLIB_FULL_LOCALE_SUPPORT, which is defined in the library configuration file, determines whether a library has support for a locale interface or not. The locale configuration symbols _LOCALE_USE_LANG_REGION and _ENCODING_USE_ENCODING define all the supported locales and encodings.

If you want to customize the locale support, you simply define the locale configuration symbols required by your application. For more information, see Building and using a customized library, page 54.

Note: If you use multibyte characters in your C or assembler source code, make sure that you select the correct locale symbol (the local host locale).

CPIC18-3

Part 1. Using the compiler 63

Page 84: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

64

Locale

Building a library without support for locale interface

The locale interface is not included if the configuration symbol _DLIB_FULL_LOCALE_SUPPORT is set to 0 (zero). This means that a hardwired locale is used—by default the standard C locale—but you can choose one of the supported locale configuration symbols. The setlocale function is not available and can therefore not be used for changing locales at runtime.

Building a library with support for locale interface

Support for the locale interface is obtained if the configuration symbol _DLIB_FULL_LOCALE_SUPPORT is set to 1. By default, the standard C locale is used, but you can define as many configuration symbols as required. Because the setlocale function will be available in your application, it will be possible to switch locales at runtime.

CHANGING LOCALES AT RUNTIME

The standard library function setlocale is used for selecting the appropriate portion of the application’s locale when the application is running.

The setlocale function takes two arguments. The first one is a locale category that is constructed after the pattern LC_CATEGORY. The second argument is a string that describes the locale. It can either be a string previously returned by setlocale, or it can be a string constructed after the pattern:

lang_REGION

or

lang_REGION.encoding

The lang part specifies the language code, and the REGION part specifies a region qualifier, and encoding specifies the multibyte encoding that should be used.

The lang_REGION part matches the _LOCALE_USE_LANG_REGION preprocessor symbols that can be specified in the library configuration file.

Example

This example sets the locale configuration symbols to Swedish to be used in Finland and UTF8 multibyte encoding:

setlocale (LC_ALL, "sv_FI.Utf8");

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 85: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

Environment interactionAccording to the C standard, your application can interact with the environment using the functions getenv and system.

Note: The putenv function is not required by the standard, and the library does not provide an implementation of it.

The getenv function searches the string, pointed to by the global variable __environ, for the key that was passed as argument. If the key is found, the value of it is returned, otherwise 0 (zero) is returned. By default, the string is empty.

To create or edit keys in the string, you must create a sequence of null terminated strings where each string has the format:

key=value\0

The last string must be empty. Assign the created sequence of strings to the __environ variable.

For example:

const char MyEnv[] = ”Key=Value\0Key2=Value2\0”;__environ = MyEnv;

If you need a more sophisticated environment variable handling, you should implement your own getenv, and possibly putenv function. This does not require that you rebuild the library. You can find source templates in the files getenv.c and environ.c in the pic18\src\lib directory. For information about overriding default library modules, see Overriding library modules, page 52.

If you need to use the system function, you need to implement it yourself. The system function available in the library simply returns -1.

If you decide to rebuild the library, you can find source templates in the library project template. For further information, see Building and using a customized library, page 54.

Note: If you link your application with support for I/O debugging, the functions getenv and system will be replaced by C-SPY variants. For further information, see Debug support in the runtime library, page 47.

Signal and raiseThere are default implementations of the functions signal and raise available. If these functions do not provide the functionality that you need, you can implement your own versions.

CPIC18-3

Part 1. Using the compiler 65

Page 86: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

66

Time

This does not require that you rebuild the library. You can find source templates in the files Signal.c and Raise.c in the pic18\src\lib directory. For information about overriding default library modules, see Overriding library modules, page 52.

If you decide to rebuild the library, you can find source templates in the library project template. For further information, see Building and using a customized library, page 54.

TimeTo make the time and date functions work, you must implement the three functions clock, time, and __getzone.

This does not require that you rebuild the library. You can find source templates in the files Clock.c and Time.c, and Getzone.c in the pic18\src\lib directory. For information about overriding default library modules, see Overriding library modules, page 52.

If you decide to rebuild the library, you can find source templates in the library project template. For further information, see Building and using a customized library, page 54.

The default implementation of __getzone specifies UTC as the time-zone.

Note: If you link your application with support for I/O debugging, the functions clock and time will be replaced by C-SPY variants that return the host clock and time respectively. For further information, see C-SPY Debugger runtime interface, page 67.

StrtodThe function strtod does not accept hexadecimal floating-point strings in libraries with the normal library configuration. To make a library do so, you need to rebuild the library, see Building and using a customized library, page 54. Enable the configuration symbol _DLIB_STRTOD_HEX_FLOAT in the library configuration file.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 87: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

AssertIf you have linked your application with support for runtime debugging, C-SPY will be notified about failed asserts. If this is not the behavior you require, you must add the source file xReportAssert.c to your application project. Alternatively, you can rebuild the library. The __ReportAssert function generates the assert notification.You can find template code in the pic18\src directory. For further information, see Building and using a customized library, page 54. To turn off assertions, you must define the symbol NDEBUG.

In the IAR Embedded Workbench IDE, this symbol NDEBUG is by default defined in a Release project and not defined in a Debug project. If you build from the command line, you must explicitly define the symbol according to your needs.

C-SPY Debugger runtime interface To include support for runtime and I/O debugging, you must link your application with the XLINK options With runtime control modules or With I/O emulation modules, see Debug support in the runtime library, page 47. In this case, C-SPY variants of the following library functions will be linked to the application:

* The linker option With I/O emulation modules is not required for these functions.

Function Description

abort C-SPY notifies that the application has called abort *

__exit C-SPY notifies that the end of the application has been reached *

__read stdin, stdout, and stderr will be directed to the Terminal I/O window; all other files will read the associated host file

__write stdin, stdout, and stderr will be directed to the Terminal I/O window, all other files will write to the associated host file

__open Opens a file on the host computer

__close Closes the associated host file on the host computer

__seek Seeks in the associated host file on the host computer

remove Writes a message to the Debug Log window and returns -1

rename Writes a message to the Debug Log window and returns -1

time Returns the time on the host computer

clock Returns the clock on the host computer

system Writes a message to the Debug Log window and returns -1

_ReportAssert Handles failed asserts *

Table 21: Functions with special meanings when linked with debug info

CPIC18-3

Part 1. Using the compiler 67

Page 88: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

68

Checking module consistency

LOW-LEVEL DEBUGGER RUNTIME INTERFACE

The low-level debugger runtime interface is used for communication between the application being debugged and the debugger itself. The debugger provides runtime services to the application via this interface; services that allow capabilities like file and terminal I/O to be performed on the host computer.

These capabilities can be valuable during the early development of an application, for example in an application using file I/O before any flash file system I/O drivers have been implemented. Or, if you need to debug constructions in your application that use stdin and stdout without the actual hardware device for input and output being available. Another debugging purpose can be to produce debug trace printouts.

The mechanism used for implementing this feature works as follows. The debugger will detect the presence of the function __DebugBreak, which will be part of the application if you have linked it with the XLINK options for C-SPY runtime interface. In this case, the debugger will automatically set a breakpoint at the __DebugBreak function. When the application calls, for example open, the __DebugBreak function is called, which will cause the application to break and perform the necessary services. The execution will then resume.

THE DEBUGGER TERMINAL I/O WINDOW

To make the Terminal I/O window available, the application must be linked with support for I/O debugging, see Debug support in the runtime library, page 47. This means that when the functions __read or __write are called to perform I/O operations on the streams stdin, stdout, or stderr, data will be sent to or read from the C-SPY Terminal I/O window.

Note: The Terminal I/O window is not opened automatically just because __read or __write is called; you must open it manually.

See the IAR Embedded Workbench® IDE User Guide for more information about the Terminal I/O window.

Checking module consistencyThis section introduces the concept of runtime model attributes, a mechanism used by the IAR compiler, assembler, and linker to ensure module consistency.

When developing an application, it is important to ensure that incompatible modules are not used together. For example, in the PIC18 IAR C/C++ Compiler, it is possible to specify the size of the __eeprom pointer. If you write a routine that only works for 1-byte __eeprom pointers, it is possible to check that the routine is not used in an application built using 2-byte __eeprom pointers.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 89: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

The tools provided by IAR use a set of predefined runtime model attributes. You can use these predefined attributes or define your own to perform any type of consistency check.

RUNTIME MODEL ATTRIBUTES

A runtime attribute is a pair constituted of a named key and its corresponding value. Two modules can only be linked together if they have the same value for each key that they both define.

There is one exception: if the value of an attribute is *, then that attribute matches any value. The reason for this is that you can specify this in a module to show that you have considered a consistency property, and this ensures that the module does not rely on that property.

Example

In the following table, the object files could (but do not have to) define the two runtime attributes color and taste. In this case, file1 cannot be linked with any of the other files, since the runtime attribute color does not match. Also, file4 and file5 cannot be linked together, because the taste runtime attribute does not match.

On the other hand, file2 and file3 can be linked with each other, and with either file4 or file5, but not with both.

USING RUNTIME MODEL ATTRIBUTES

Runtime model attributes can be specified in your C/C++ source code to ensure module consistency with other object files by using the #pragma rtmodel directive. For example:

#pragma rtmodel="__rt_version", "1"

For detailed syntax information, see #pragma rtmodel, page 190.

Runtime model attributes can also be specified in your assembler source code by using the RTMODEL assembler directive. For example:

RTMODEL "color", "red"

Object file Color Taste

file1 blue not defined

file2 red not defined

file3 red *

file4 red spicy

file5 red lean

Table 22: Example of runtime model attributes

CPIC18-3

Part 1. Using the compiler 69

Page 90: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

70

Checking module consistency

For detailed syntax information, see the PIC18 IAR Assembler Reference Guide.

Note: The predefined runtime attributes all start with two underscores. Any attribute names you specify yourself should not contain two initial underscores in the name, to eliminate any risk that they will conflict with future IAR runtime attribute names.

At link time, the IAR XLINK Linker checks module consistency by ensuring that modules with conflicting runtime attributes will not be used together. If conflicts are detected, an error is issued.

PREDEFINED RUNTIME ATTRIBUTES

The table below shows the predefined runtime model attributes that are available for the PIC18 IAR C/C++ Compiler. These can be included in assembler code or in mixed C or C++ and assembler code.

The easiest way to find the proper settings of the RTMODEL directive is to compile a C or C++ module to generate an assembler file, and then examine the file.

If you are using assembler routines in the C or C++ code, refer to the chapter Assembler directives in the PIC18 IAR Assembler Reference Guide.

Examples

For an example of using the runtime model attribute __rt_version for checking module consistency on used calling convention, see Hints for using the stack calling convention, page 92.

Runtime model attribute Value Description

__code_model stack or overlay Reflects the code model option

__cpu string Hex identifier used by the COFF/COD output formats

__data_model small or large

Corresponds to the data model used in the project.

__eeprom 1 or 2 Reflects the size of the __eeprom pointer

__rt_version n This runtime key is always present in all modules generated by the PIC18 IAR C/C++ Compiler. If a major change in the runtime characteristics occurs, the value of this key changes.

Table 23: Predefined runtime model attributes

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 91: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

The following—fictitious—assembler source code provides a function, part2, that counts the number of times it has been called by increasing a register R4. The routine assumes that the application does not use R4 for anything else, that is, the register has been locked for usage. To ensure this, a runtime module attribute, __reg_r4, has been defined with a value counter. This definition will ensure that this specific module can only be linked with either other modules containing the same definition, or with modules that do not set this attribute. Note that the compiler sets this attribute to free, unless the register is locked.

RTMODEL "RTMODEL "__reg_r4", "counter"MODULE myCounterPUBLIC myCounterRSEG CODE:CODE:NOROOT(1)

myCounter: INC R4RETENDMODEND

If this module is used in an application that contains modules where the register R4 has not been locked, an error is issued by the linker:

Error[e117]: Incompatible runtime models. Module myCounter specifies that '__reg_r4' must be 'counter', but module part1 has the value 'free'

USER-DEFINED RUNTIME MODEL ATTRIBUTES

In cases where the predefined runtime model attributes are not sufficient, you can define your own attributes by using the RTMODEL assembler directive. For each property, select a key and a set of values that describe the states of the property that are incompatible. Note that key names that start with two underscores are reserved by IAR Systems.

For example, if you have a UART that can run in two modes, you can specify a runtime model attribute, for example uart. For each mode, specify a value, for example mode1 and mode2. You should declare this in each module that assumes that the UART is in a particular mode. This is how it could look like in one of the modules:

#pragma rtmodel="UART", "mode1"

CPIC18-3

Part 1. Using the compiler 71

Page 92: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

72

Implementation of system startup code

Implementation of system startup codeThis section presents some general techniques used in the system startup code, including background information that might be useful if you need to modify it.

Note:

● Do not modify the file cstartup.s49 unless required by your application. Your first option should be to use a customized version of __low_level_init for initialization code.

● Normally, there is no need for customizing either of the files cmain.s49 or cexit.s49.

The source files are well commented and are not described in detail in this guide.

For information about assembler source files, see the PIC18 IAR Assembler Reference Guide.

MODULES AND SEGMENT PARTS

To understand how the startup code is designed, you must have a clear understanding of modules and segment parts, and how the IAR XLINK Linker treats them.

An assembler module starts with a MODULE directive and ends with an ENDMOD directive. Each module is logically divided into segment parts, which are the smallest linkable units. There will be segment parts for constants, code bytes, and for reserved space for data. Each segment part begins with an RSEG directive.

When XLINK builds an application, it starts with a small number of modules that have either been declared using the __root keyword or have the program entry label __program_start. The linker then continues to include all modules that are referred from the already included modules. XLINK then discards unused segment parts.

Segment parts, REQUIRE, and the falling-through trick

The system startup code has been designed to use segment parts so that as little as possible of unused code will be included in the linked application.

For example, every piece of code used for initializing one type of memory is stored in a segment part of its own. If a variable is stored in a certain memory type, the corresponding initialization code will be referenced by the code generated by the compiler, and included in your application. Should no variables of a certain type exist, the code is simply discarded.

A piece of code or data is not included if it is not used or referred to. To make the linker always include a piece of code or data, the assembler directive REQUIRE can be used.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 93: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

The segment parts defined in the system startup code are guaranteed to be placed immediately after each other. The reason for this is that XLINK will not change the order of the segment parts or modules, because the segments holding the system startup code are placed using the -Z option.

This lets the system startup code specify code in subsequent segment parts and modules that are designed so that some of the parts may not be included by XLINK. The code simply falls through to the next piece of code not discarded by the linker. The following example shows this technique:

MODULE doSomething

RSEG MYSEG:CODE:NOROOT(1) // First segment part. PUBLIC ?do_something EXTERN ?end_of_test REQUIRE ?end_of_test

?do_something: // This will be included if someone refers to ... // ?do_something. If this is included then // the REQUIRE directive above ensures that // the RETURN instruction below is included.

RSEG MYSEG:CODE:NOROOT(1) // Second segment part. PUBLIC ?do_something_else

?do_something_else: ... // This will only be included in the linked // application if someone outside this function // refers to or requires ?do_something_else

RSEG MYSEG:CODE:NOROOT(1) // Third segment part. PUBLIC ?end_of_test

?end_of_test: RETURN // This is included if ?do_something above // is included. ENDMOD

CPIC18-3

Part 1. Using the compiler 73

Page 94: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

74

Added C functionality

Added C functionalityThe IAR DLIB Library includes some added C functionality, partly taken from the C99 standard.

The following include files provide these features:

● stdint.h

● stdbool.h● math.h● stdio.h● stdlib.h

STDINT.H

This include file provides integer characteristics.

STDBOOL.H

This include file makes the bool type available if the Allow IAR extensions (-e) option is used.

MATH.H

In math.h all functions exist in a float variant and a long double variant, suffixed by f and l respectively. For example, sinf and sinl.

STDIO.H

In stdio.h, the following functions have been added from the C99 standard:

The following functions have been added to provide I/O functionality for libraries built without FILE support:

vscanf,

vfscanf,

vsscanf,

vsnprintf

Variants that have a va_list as argument.

snprintf Same as sprintf, but writes to a size limited array.

__write_array Corresponds to fwrite on stdout.

__ungetchar Corresponds to ungetc on stdout.

__gets Corresponds to fgets on stdin.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 95: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The DLIB runtime environment

STDLIB.H

In stdlib.h, the following functions have been added:

PRINTF, SCANF AND STRTOD

The functions printf, scanf and strtod have added functionality from the C99 standard. For reference information about these functions, see the library reference available from the Help menu.

_exit Exits without closing files et cetera.

__qsortbbl A qsort function that uses the bubble sort algorithm. Useful for applications that have limited stack or static overlay.

CPIC18-3

Part 1. Using the compiler 75

Page 96: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

76

Added C functionality

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 97: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The CLIB runtime environment This chapter describes the runtime environment in which an application executes. In particular, it covers the CLIB runtime library and how you can optimize it for your application.

The standard library uses a small set of low-level input and output routines for character-based I/O. This chapter describes how the low-level routines can be replaced by your own version. The chapter also describes how you can choose printf and scanf formatters.

The chapter then describes system initialization and termination. It presents how an application can control what happens before the start function main is called, and the method for how you can customize the initialization. Finally, the C-SPY runtime interface is covered.

Note that the legacy CLIB runtime environment is mainly provided for backward compatibility and should not be used for new application projects.

Runtime environmentThe CLIB runtime environment includes the C standard library. The linker will include only those routines that are required—directly or indirectly—by your application. For detailed reference information about the runtime libraries, see the chapter Library functions.

PIC18 IAR Embedded Workbench comes with a set of prebuilt runtime libraries, which are configured for different combinations of code model and data model.

CPIC18-3

Part 1. Using the compiler 77

Page 98: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

78

Input and output

For the PIC18 IAR C/C++ Compiler, this means there is a prebuilt runtime library for each combination of these options. The following table shows the mapping of the library file, code model, and data model:

The runtime library names are constructed in the following way:

<type><cpu_variant><code_model><data_model>.r49

where

● <type> cl for the IAR CLIB Library● <cpu_variant> is 18● <code_model> is one of o or s for overlay or stack, respectively● <data_model> is one of s or l for small or large, respectively.

The IAR Embedded Workbench IDE includes the correct runtime library based on the options you select. See the IAR Embedded Workbench® IDE User Guide for additional information.

Specify which runtime library object file to use on the XLINK command line, for instance:

cl18s1.r49

Input and outputYou can customize:

● The functions related to character-based I/O● The formatters used by printf/sprintf and scanf/sscanf.

CHARACTER-BASED I/O

The functions putchar and getchar are the fundamental functions through which C performs all character-based I/O. For any character-based I/O to be available, you must provide definitions for these two functions, using whatever facilities the hardware environment provides.

Library Code model Data model

cl18os.r49 Overlay Small

cl18ol.r49 Overlay Large

cl18ss.r49 Stack Small

cl18sl.r49 Stack Large

Table 24: Prebuilt CLIB libraries

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 99: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The CLIB runtime environment

The creation of new I/O routines is based on the following files:

● putchar.c, which serves as the low-level part of functions such as printf ● getchar.c, which serves as the low-level part of functions such as scanf.

The code example below shows how memory-mapped I/O could be used to write to a memory-mapped I/O device:

__no_init volatile unsigned char DEV_IO @ address;

int putchar(int outchar) { DEV_IO = outchar; return outchar; }

The exact address is a design decision. For example, it can depend on the selected processor variant.

For information about how to include your own modified version of putchar and getchar in your project build process, see Overriding library modules, page 52.

FORMATTERS USED BY PRINTF AND SPRINTF

The printf and sprintf functions use a common formatter, called _formatted_write. The full version of _formatted_write is very large, and provides facilities not required in many embedded applications. To reduce the memory consumption, two smaller, alternative versions are also provided in the standard C library.

_medium_write

The _medium_write formatter has the same functions as _formatted_write, except that floating-point numbers are not supported. Any attempt to use a %f, %g, %G, %e, or %E specifier will produce a runtime error:

FLOATS? wrong formatter installed!

_medium_write is considerably smaller than _formatted_write.

_small_write

The _small_write formatter works in the same way as _medium_write, except that it supports only the %%, %d, %o, %c, %s, and %x specifiers for integer objects, and does not support field width or precision arguments. The size of _small_write is 10–15% that of _formatted_write.

CPIC18-3

Part 1. Using the compiler 79

Page 100: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

80

Input and output

Specifying the printf formatter in the IAR Embedded Workbench IDE

1 Choose Project>Options and select the General Options category. Click the Library options tab.

2 Select the appropriate Printf formatter option, which can be either Small, Medium, or Large.

Specifying the printf formatter from the command line

To use the _small_write or _medium_write formatter, add the corresponding line in the linker command file:

-e_small_write=_formatted_write

or

-e_medium_write=_formatted_write

To use the full version, remove the line.

Customizing printf

For many embedded applications, sprintf is not required, and even printf with _small_write provides more facilities than are justified, considering the amount of memory it consumes. Alternatively, a custom output routine may be required to support particular formatting needs or non-standard output devices.

For such applications, a much reduced version of the printf function (without sprintf) is supplied in source form in the file intwri.c. This file can be modified to meet your requirements, and the compiled module inserted into the library in place of the original file; see Overriding library modules, page 52.

FORMATTERS USED BY SCANF AND SSCANF

Similar to the printf and sprintf functions, scanf and sscanf use a common formatter, called _formatted_read. The full version of _formatted_read is very large, and provides facilities that are not required in many embedded applications. To reduce the memory consumption, an alternative smaller version is also provided.

_medium_read

The _medium_read formatter has the same functions as the full version, except that floating-point numbers are not supported. _medium_read is considerably smaller than the full version.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 101: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The CLIB runtime environment

Specifying the scanf formatter in the IAR Embedded Workbench IDE

1 Choose Project>Options and select the General Options category. Click the Library options tab.

2 Select the appropriate Scanf formatter option, which can be either Medium or Large.

Specifying the read formatter from the command line

To use the _medium_read formatter, add the following line in the linker command file:

-e_medium_read=_formatted_read

To use the full version, remove the line.

System startup and terminationThis section describes the actions the runtime environment performs during startup and termination of applications.

Note: The code for handling startup and termination is located in the source files cstartup.s49, dlib_sup.s49, and low_level_init.c located in the pic18/src/lib directory.Normally, there is no need for customizing either of the files cstartup.s49 or dlib_sup.s49.

SYSTEM STARTUP

When an application is initialized, a number of steps are performed:

● The custom function __low_level_init is called, giving the application a chance to perform early initializations

● Static variables are initialized; this includes clearing zero-initialized memory and copying the ROM image of the RAM memory of the remaining initialized variables

● The main function is called, which starts the application.

Note that the system startup code contains code for more steps than described here. The other steps are applicable to the DLIB runtime environment.

SYSTEM TERMINATION

An application can terminate normally in two different ways:

● Return from the main function● Call the exit function.

CPIC18-3

Part 1. Using the compiler 81

Page 102: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

82

Overriding default library modules

Because the ISO/ANSI C standard states that the two methods should be equivalent, the cstartup code calls the exit function if main returns. The parameter passed to the exit function is the return value of main. The default exit function is written in assembler.

When the application is built in debug mode, C-SPY stops when it reaches the special code label ?C_EXIT.

An application can also exit by calling the abort function. The default function just calls __exit in order to halt the system, without performing any type of cleanup.

Overriding default library modulesThe IAR CLIB Library contains modules which you probably need to override with your own customized modules, for example for character-based I/O, without rebuilding the entire library. For information about how to override default library modules, see Overriding library modules, page 52 in the chapter The DLIB runtime environment.

Customizing system initializationFor information about how to customize system initialization, see Customizing system initialization, page 58.

Implementation of cstartup For information about cstartup implementation, see Implementation of system startup code, page 72 in the chapter The DLIB runtime environment.

C-SPY runtime interface The low-level debugger interface is used for communication between the application being debugged and the debugger itself. The interface is simple: C-SPY will place breakpoints on certain assembler labels in the application. When code located at the special labels is about to be executed, C-SPY will be notified and can perform an action.

THE DEBUGGER TERMINAL I/O WINDOW

When code at the labels ?C_PUTCHAR and ?C_GETCHAR is executed, data will be sent to or read from the debugger window.

For the ?C_PUTCHAR routine, one character is taken from the output stream and written. If everything goes well, the character itself is returned, otherwise -1 is returned.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 103: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The CLIB runtime environment

When the label ?C_GETCHAR is reached, C-SPY returns the next character in the input field. If no input is given, C-SPY waits until the user has typed some input and pressed the Return key.

To make the Terminal I/O window available, the application must be linked with the XLINK option With I/O emulation modules selected. See the IAR Embedded Workbench® IDE User Guide.

TERMINATION

The debugger stops executing when it reaches the special label ?C_EXIT.

Checking module consistencyFor information about how to check module consistency, see Checking module consistency, page 68 in the chapter The DLIB runtime environment.

CPIC18-3

Part 1. Using the compiler 83

Page 104: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

84

Checking module consistency

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 105: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Assembler language interface When you develop an application for an embedded system, there may be situations where you will find it necessary to write parts of the code in assembler, for example, when using mechanisms in the PIC18 microcontroller that require precise timing and special instruction sequences.

This chapter describes the available methods for this, as well as some C alternatives, with their pros and cons. It also describes how to write functions in assembler language that work together with an application written in C or C++.

Finally, the chapter covers how functions are called in the different code models, the different memory access methods corresponding to the supported memory types, and how you can implement support for call frame information in your assembler routines for use in the C-SPY Call Stack window.

Mixing C and assemblerThe PIC18 IAR C/C++ Compiler provides several ways to mix C or C++ and assembler:

● Modules written entirely in assembler● Intrinsic functions (the C alternative)● Inline assembler.

It might be tempting to use simple inline assembler. However, you should carefully choose which method to use.

INTRINSIC FUNCTIONS

The compiler provides a small number of predefined functions that allow direct access to low-level processor operations without having to use the assembler language. These functions are known as intrinsic functions. They can be very useful in, for example, time-critical routines.

CPIC18-3

Part 1. Using the compiler 85

Page 106: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

86

Mixing C and assembler

An intrinsic function looks like a normal function call, but it is really a built-in function that the compiler recognizes. The intrinsic functions compile into inline code, either as a single instruction, or as a short sequence of instructions.

The advantage of an intrinsic function compared to using inline assembler is that the compiler has all necessary information to interface the sequence properly with register allocation and variables. The compiler also knows how to optimize functions with such sequences; something the compiler is unable to do with inline assembler sequences. The result is, that you get the desired sequence properly integrated in your code, and that the compiler can optimize the result.

For detailed information about the available intrinsic functions, see the chapter Intrinsic functions.

MIXING C AND ASSEMBLER MODULES

When an application is written partly in assembler language and partly in C or C++, you are faced with a number of questions:

● How should the assembler code be written so that it can be called from C?● Where does the assembler code find its parameters, and how is the return value

passed back to the caller?● How should assembler code call functions written in C?● How are global C variables accessed from code written in assembler language?● Why does not the debugger display the call stack when assembler code is being

debugged?

The first issue is discussed in this section. The following two are covered in the section Calling convention, page 91.

The answer to the final question is that the call stack can be displayed when you run assembler code in the debugger. However, the debugger requires information about the call frame, which must be supplied as annotations in the assembler source file. For more information, see Call frame information, page 96.

It is possible to write parts of your application in assembler and mix them with your C or C++ modules. There are several benefits with this:

● The function call mechanism is well-defined● The code will be easy to read● The optimizer can work with the C or C++ functions.

There will be some overhead in the form of a function call and return instruction sequences, and the compiler will regard some registers as scratch registers. However, the compiler will also assume that all registers are destroyed by an inline assembler instruction. In many cases, the overhead of the extra instructions is compensated by the work of the optimizer.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 107: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Assembler language interface

The recommended method for mixing C or C++ and assembler modules is described in Calling assembler routines from C, page 88, and Calling assembler routines from C++, page 90, respectively.

INLINE ASSEMBLER

It is possible to insert assembler code directly into a C or C++ function. The asm keyword assembles and inserts the supplied assembler statement in-line. The following example shows how to use inline assembler to insert assembler instructions directly in the C source code. This example also shows the risks of using inline assembler.

bool flag;

void foo(){ while (!flag) { asm("MOVFF IOPORT,flag"); }}

In this example, the assignment of flag is not noticed by the compiler, which means the surrounding code cannot be expected to rely on the inline assembler statement.

The inline assembler instruction will simply be inserted at the given location in the program flow. The consequences or side-effects the insertion may have on the surrounding code have not been taken into consideration. If, for example, registers or memory locations are altered, they may have to be restored within the sequence of inline assembler instructions for the rest of the code to work properly.

Inline assembler sequences have no well-defined interface with the surrounding code generated from your C or C++ code. This makes the inline assembler code fragile, and will possibly also become a maintenance problem if you upgrade the compiler in the future. In addition, there are several limitations to using inline assembler:

● The compiler’s various optimizations will disregard any effects of the inline sequences, which will not be optimized at all

● In general, assembler directives will cause errors or have no meaning. Data definition directives will work as expected

● Alignment cannot be controlled; this means, for example, that DC32 directives may be misaligned

● Auto variables cannot be accessed● Labels cannot be declared.

CPIC18-3

Part 1. Using the compiler 87

Page 108: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

88

Calling assembler routines from C

Inline assembler is therefore often best avoided. If there is no suitable intrinsic function available, we recommend the use of modules written in assembler language instead of inline assembler, because the function call to an assembler routine normally causes less performance reduction.

Calling assembler routines from CAn assembler routine that is to be called from C must:

● Conform to the calling convention● Have a PUBLIC entry-point label● Be declared as external before any call, to allow type checking and optional

promotion of parameters, as in the following examples:

extern int foo(void);

or

extern int foo(int i, int j);

One way of fulfilling these requirements is to create skeleton code in C, compile it, and study the assembler list file.

CREATING SKELETON CODE

The recommended way to create an assembler language routine with the correct interface is to start with an assembler language source file created by the C compiler. Note that you must create skeleton code for each function prototype.

The following example shows how to create skeleton code to which you can easily add the functional body of the routine. The skeleton source code only needs to declare the variables required and perform simple accesses to them. In this example, the assembler routine takes an int, char, and long, and then returns a char:

char globChar;int globInt;long globLong;

char func(int arg1, char arg2, long arg3){ char locChar = arg2; /* set local */ globInt = arg1; /* use globInt/arg1 */ globChar = arg2; /* use globChar/arg2 */ globLong = arg3; /* use globLong/arg3 */ return locChar; /* set return value */}

void main(void)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 109: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Assembler language interface

{ long locLong = globLong; globChar = func(globInt, globChar, locLong);}

Note: In this example we use a low optimization level when compiling the code to show local and global variable access. If a higher level of optimization is used, the required references to local variables could be removed during the optimization. The actual function declaration is not changed by the optimization level.

COMPILING THE CODE

In the IAR Embedded Workbench IDE, specify list options on file level. Select the file in the workspace window. Then choose Project>Options. In the C/C++ Compiler category, select Override inherited settings. On the List page, deselect Output list file, and instead select the Output assembler file option and its suboption Include source. Also, be sure to specify a low level of optimization.

Use the following options to compile the skeleton code:

iccpic18 skeleton -lA .

The -lA option creates an assembler language output file including C or C++ source lines as assembler comments. The . (period) specifies that the assembler file should be named in the same way as the C or C++ module (skeleton), but with the filename extension s49. Also remember to specify the code model, and data model you are using as well as a low level of optimization.

The result is the assembler source output file skeleton.s49.

Note: The -lA option creates a list file containing call frame information (CFI) directives, which can be useful if you intend to study these directives and how they are used. If you only want to study the calling convention, you can exclude the CFI directives from the list file. In the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>List and deselect the suboption Include compiler runtime information. On the command line, use the option -lB instead of -lA. Note that CFI information must be included in the source code to make the C-SPY Call Stack window work.

The output file

The output file contains the following important information:

● The calling convention● The return values● The global variables● The function parameters● How to create space on the stack (auto variables)

CPIC18-3

Part 1. Using the compiler 89

Page 110: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

90

Calling assembler routines from C++

● Call frame information (CFI).

The CFI directives describe the call frame information needed by the Call Stack window in the IAR C-SPY™ Debugger.

Calling assembler routines from C++The C calling convention does not apply to C++ functions. Most importantly, a function name is not sufficient to identify a C++ function. The scope and the type of the function are also required to guarantee type-safe linkage, and to resolve overloading.

Another difference is that non-static member functions get an extra, hidden argument, the this pointer.

However, when using C linkage, the calling convention conforms to the C calling convention. An assembler routine may therefore be called from C++ when declared in the following manner:

extern "C" { int my_routine(int x);}

Memory access layout of non-PODs ("plain old data structures") is not defined, and may change between compiler versions. Therefore, we do not recommend that you access non-PODs from assembler routines.

To achieve the equivalent to a non-static member function, the implicit this pointer has to be made explicit:

class X;

extern "C" { void doit(X *ptr, int arg);}

It is possible to “wrap” the call to the assembler routine in a member function. Using an inline member function removes the overhead of the extra call—provided that function inlining is enabled:

class X {public: inline void doit(int arg) { ::doit(this, arg); }};

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 111: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Assembler language interface

Note: Support for C++ names from assembler code is extremely limited. This means that:

● Assembler list files resulting from compiling C++ files cannot, in general, be passed through the assembler.

● It is not possible to refer to or define C++ functions that do not have C linkage in assembler.

Calling conventionA calling convention is the way a function in a program calls another function. The compiler handles this automatically, but, if a function is written in assembler language, you must know where and how its parameters can be found, how to return to the program location from where it was called, and how to return the resulting value.

It is also important to know which registers an assembler-level routine must preserve. If the program preserves too many registers, the program might be ineffective. If it preserves too few registers, the result would be an incorrect program.

This section describes the calling conventions used by the PIC18 IAR C/C++ Compiler. The PIC18 IAR C/C++ Compiler provides two calling conventions—one for the static overlay code model and one for the stack code model. The following items are looked upon:

● Choosing a calling convention● Function declarations● C and C++ linkage● Preserved versus scratch registers● Function entrance● Function exit● Return address handling.

At the end of the section, some examples are shown to describe the calling convention in practice.

CHOOSING A CALLING CONVENTION

Each of the code models, static overlay and stack, has a corresponding calling convention.

The calling convention that corresponds to the code model is used by default. You can choose a different calling convention by declaring individual functions using a function type attribute, for example:

extern__stack_func void my_function(int arg);

CPIC18-3

Part 1. Using the compiler 91

Page 112: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

92

Calling convention

See also the Extended keywords chapter.

Hints for using the stack calling convention

If you intend to use the stack calling convention, you should also specify a value to the runtime model attribute __rt_version using the RTMODEL assembler directive:

RTMODEL "__rt_version"="value"

The parameter value should have the same value as used internally by the compiler. For information about what value to use, see the generated list file. If the calling convention changes in future compiler versions, the runtime model value used internally by the compiler will also change. Using this method gives a module consistency check as the linker will produce an error if there is a mismatch between the values.

For more information about checking module consistency, see Checking module consistency, page 68.

FUNCTION DECLARATIONS

In C, a function must be declared in order for the compiler to know how to call it. A declaration could look as follows:

int a_function(int first, char * second);

This means that the function takes two parameters: an integer and a pointer to a character. The function returns a value, an integer.

In the general case, this is the only knowledge that the compiler has about a function. Therefore, it must be able to deduce the calling convention from this information.

C AND C++ LINKAGE

In C++, a function can have either C or C++ linkage. Only functions with C linkage can be implemented in assembler.

The following is an example of a declaration of a function with C linkage:

extern "C"{ int f(int);}

It is often practical to share header files between C and C++. The following is an example of a declaration that declares a function with C linkage in both C and C++:

#ifdef __cplusplusextern "C" {#endif

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 113: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Assembler language interface

int f(int);

#ifdef __cplusplus}#endif

REGISTER USAGE

The PIC18 IAR C/C++ Compiler uses internal memory-mapped registers located in the access area. These are named ?A0, ?A1, ?A2, ?A3, ?B0–?B3, ?C0–?C3, and ?E0—a total of thirteen bytes. If a register is not referred to, it is not allocated and the space is free to use by the C program.

No registers are saved by a function. After a call to a C function, all the registers above plus FSR0L, FSR0H, FSR1L, FSR1H, WREG, PRODH, PRODL, TABLAT, TBLPTRU, TBLPTRH, TBLPTRL, STATUS, and BSR are assumed to be destroyed.

Because of this, you should not call any C functions from within an interrupt routine, since the save and restore sequences will be very large. Assembler support routines that are used by the compiler only save the destroyed registers, since these are known at compile time.

FUNCTION ENTRANCE

Parameters can be passed to a function using one of two basic methods: in an overlay frame or on the stack. In the static overlay code model, the parameters are placed in the called function’s parameter area. For the stack code model, the parameters are pushed on the stack using the POSTINC2 register.

Hidden parameters

In addition to the parameters visible in a function declaration and definition, there can be hidden parameters. If the function returns a structure, the memory location where the structure is to be stored is passed as a hidden parameter.

CPIC18-3

Part 1. Using the compiler 93

Page 114: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

94

Calling convention

Stack frames

A function call creates a stack frame as follows:

Figure 2: Storing stack parameters in memory

FUNCTION EXIT

A function can return a value to the function or program that called it, or it can be of the type void.

The return value of a function, if any, can be scalar (such as integers and pointers), floating-point, or a structure.

Registers used for returning values

The registers available for returning values are W and A0–A3.

Return values Passed in registers

8-bit values W

16-bit values A0–A1

24-bit values A0–A2

32-bit values A0–A3

Table 25: Registers used for returning values

Stack parameters

Parameters

Saved registers

Interrupt functions

Auto variables

Area for local variables.

Temporary storage

Any temporary values.

High address

Low address

Stack pointer

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 115: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Assembler language interface

Stack layout

Called stack functions are responsible for cleaning up the stack parameters that were pushed, except for vararg functions, where the caller cleans up the stack.

RETURN ADDRESS HANDLING

A function written in assembler language should, when finished, return to the caller. Typically, a function returns by using the RETURN instruction.

RESTRICTIONS FOR SPECIAL FUNCTION TYPES

Monitor functions

A monitor function causes interrupts to be disabled during execution of the function. At function entry the status register INTCON is saved and global interrupts are disabled. At function exit the global interrupt-enabled bit (GIE) is restored to the setting it had before entering the monitor function.

For additional information, see __monitor, page 181.

Interrupt functions

The calling convention for ordinary functions cannot be used for interrupt functions since the interrupt can occur any time during program execution. Hence the requirements for an interrupt routine are different from those of a normal function.

Interrupt functions may call reentrant functions (static overlay functions are not reentrant).

For information about which registers are saved, see Register usage, page 93.

Notice than interrupts should not be enabled within an interrupt routine.

Defining interrupt vectors

When you have an assembler-written interrupt function, you must install it in the interrupt vector table. See the PIC18 IAR Assembler Reference Guide for a description.

The interrupt vectors are located in the INTVEC segment. See INTVEC, page 144, for additional information.

CPIC18-3

Part 1. Using the compiler 95

Page 116: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

96

Call frame information

Call frame information When debugging an application using C-SPY, it is possible to view the call stack, that is, the functions that have called the current function. The compiler makes this possible by supplying debug information that describes the layout of the call frame, in particular information about where the return address is stored.

If you want the call stack to be available when debugging a routine written in assembler language, you must supply equivalent debug information in your assembler source using the assembler directive CFI. This directive is described in detail in the PIC18 IAR Assembler Reference Guide.

The CFI directives will provide C-SPY with information about the state of the calling function(s). Most important of this is the return address, and the value of the stack pointer at the entry of the function or assembler routine. Given this information, C-SPY can reconstruct the state for the calling function, and thereby unwind the stack.

A full description about the calling convention may require extensive call frame information. In many cases, a more limited approach will suffice.

When describing the call frame information, the following three components must be present:

● A names block describing the available resources to be tracked● A common block corresponding to the calling convention● A data block describing the changes that are performed on the call frame. This

typically includes information about when the stack pointer is changed, and when permanent registers are stored or restored on the stack.

The following table lists all the resources defined in the names block used by the compiler:

Resource Description

CFA_?SP, CFA_STKPTR The call frames of the FSR2 stack and of the call stack, respectively

W Normal register

STKPTR The call stack pointer

PCLATH, PCLATU, PRODH, PRODL, TABLAT

Special processor registers

?RET The return address

?A0–?A3, ?B0–?B3, ?C0–?C3, ?E0 WRKSEG registers

Table 26: Call frame information resources defined in a names block

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 117: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Assembler language interface

Function directivesThe function directives are generated by the PIC18 IAR C/C++ Compiler to pass information about functions and function calls to the IAR XLINK Linker. These directives can be seen if you create an assembler list file by using the compiler option Assembler file (-lA).

SYNTAX

FUNCTION label,valueARGFRAME segment, size, typeLOCFRAME segment, size, typeFUNCALL caller, callee

PARAMETERS

DESCRIPTION

FUNCTION declares the label name to be a function. value encodes extra information about the function.

FUNCALL declares that the function caller calls the function callee. callee can be omitted to indicate an indirect function call.

ARGFRAME and LOCFRAME declare how much space the frame of the function uses in different memories. ARGFRAME declares the space used for the arguments to the function, LOCFRAME the space for locals. segment is the segment in which the space resides. size is the number of bytes used. type is either STACK or STATIC, for stack-based allocation and static overlay allocation, respectively.

ARGFRAME and LOCFRAME always occur immediately after a FUNCTION or FUNCALL directive.

label Label to be declared as function.

value Function information.

segment Segment in which argument frame or local frame is to be stored.

size Size of argument frame or local frame.

type Type of argument or local frame; either STACK or STATIC.

caller Caller to a function.

callee Called function.

CPIC18-3

Part 1. Using the compiler 97

Page 118: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

98

Function directives

After a FUNCTION directive for an external function, there can only be ARGFRAME directives, which indicate the maximum argument frame usage of any call to that function. After a FUNCTION directive for a defined function, there can be both ARGFRAME and LOCFRAME directives.

After a FUNCALL directive, there will first be LOCFRAME directives declaring frame usage in the calling function at the point of call, and then ARGFRAME directives declaring argument frame usage of the called function.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 119: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Using C++IAR Systems supports two levels of the C++ language: The industry-standard Embedded C++ and IAR Extended Embedded C++. They are described in this chapter.

OverviewEmbedded C++ is a subset of the C++ programming language which is intended for embedded systems programming. It was defined by an industry consortium, the Embedded C++ Technical Committee. Performance and portability are particularly important in embedded systems development, which was considered when defining the language.

STANDARD EMBEDDED C++

The following C++ features are supported:

● Classes, which are user-defined types that incorporate both data structure and behavior; the essential feature of inheritance allows data structure and behavior to be shared among classes

● Polymorphism, which means that an operation can behave differently on different classes, is provided by virtual functions

● Overloading of operators and function names, which allows several operators or functions with the same name, provided that there is a sufficient difference in their argument lists

● Type-safe memory management using operators new and delete● Inline functions, which are indicated as particularly suitable for inline expansion.

C++ features which have been excluded are those that introduce overhead in execution time or code size that are beyond the control of the programmer. Also excluded are recent additions to the ISO/ANSI C++ standard. This is because they represent potential portability problems, due to the fact that few development tools support the standard. Embedded C++ thus offers a subset of C++ which is efficient and fully supported by existing development tools.

Standard Embedded C++ lacks the following features of C++:

● Templates ● Multiple and virtual inheritance ● Exception handling ● Runtime type information

CPIC18-3

Part 1. Using the compiler 99

Page 120: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

100

Overview

● New cast syntax (the operators dynamic_cast, static_cast, reinterpret_cast, and const_cast)

● Namespaces● Mutable attribute.

The exclusion of these language features makes the runtime library significantly more efficient. The Embedded C++ library furthermore differs from the full C++ library in that:

● The standard template library (STL) is excluded● Streams, strings, and complex numbers are supported without the use of templates● Library features which relate to exception handling and runtime type information

(the headers except, stdexcept, and typeinfo) are excluded.

Note: The library is not in the std namespace, because Embedded C++ does not support namespaces.

EXTENDED EMBEDDED C++

IAR Extended EC++ is a slightly larger subset of C++ which adds the following features to the standard EC++:

● Full template support● Namespace support● Mutable attribute ● The cast operators static_cast, const_cast, and reinterpret_cast.

All these added features conform to the C++ standard.

To support Extended EC++, this product includes a version of the standard template library (STL), in other words, the C++ standard chapters utilities, containers, iterators, algorithms, and some numerics. This STL has been tailored for use with the Extended EC++ language, which means that there are no exceptions, no multiple inheritance, and no support for runtime type information (rtti). Moreover, the library is not in the std namespace.

Note: A module compiled with Extended EC++ enabled is fully link-compatible with a module compiled without Extended EC++ enabled.

ENABLING C++ SUPPORT

In the PIC18 IAR C/C++ Compiler, the default language is C. To be able to compile files written in Embedded C++, you must use the --ec++ compiler option. See --ec++, page 159. You must also use the IAR DLIB runtime library.

To take advantage of Extended Embedded C++ features in your source code, you must use the --eec++ compiler option. See --eec++, page 159.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 121: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Using C++

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Language.

Feature descriptionsWhen writing C++ source code for the IAR C/C++ Compiler, there are some benefits and some possible quirks that you need to be aware of when mixing C++ features—such as classes, and class members—with IAR language extensions, such as IAR-specific attributes.

CLASSES

A class type class and struct in C++ can have static and non-static data members, and static and non-static function members. The non-static function members can be further divided into virtual function members, non-virtual function members, constructors, and destructors. For the static data members, static function members, and non-static non-virtual function members the same rules apply as for statically linked symbols outside of a class. In other words, they can have any applicable IAR-specific type, memory, and object attribute.

The non-static virtual function members can have any applicable IAR-specific type, memory, and object attribute as long as a pointer to the member function is implicitly castable to the default function pointer type. The constructors, destructors, and non-static data members cannot have any IAR attributes.

For further information about attributes, see Type and object attributes, page 128.

Example

class A { public: static __bank1 int i @ 60; //Located in bank at address 60 static __bank_func void f(); //Located in bank memory __bank_func void g(); //Located in bank memory virtual __bank_func void h();//Located in bank memory};

The this pointer used for referring to a class object will by default have the data memory attribute for the default data pointer type. This means that such a class object can only be defined to reside in memory from which pointers can be implicitly casted to a default data pointer.

Example

class B { public:

CPIC18-3

Part 1. Using the compiler 101

Page 122: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

102

Feature descriptions

void f(); int i;};

Class memory

To compensate for this limitation, a class can be associated with a class memory type. The class memory type changes:

● the this pointer type in all member functions, constructors, and destructors into a pointer to class memory

● the default memory for static storage duration variables—that is, not auto variables—of the class type, into the specified class memory

● the pointer type used for pointing to objects of the class type, into a pointer to class memory.

Example

class __bank C { public: void f(); // Has a this pointer of type C __bank * void f() const; // Has a this pointer of type // C __bank const * C(); // Has a this pointer pointing into bank // memory C(C const &); // Takes a parameter of type C __bank const & // (also true of generated copy constructor) int i;};C Ca; // Resides in bank memory instead of the // default memoryC __bank2 Cb; // Resides in bank2 memory, the 'this' // pointer still points into bank memoryC __eeprom Cc; // Not allowed, __eeprom pointer can't be // implicitly casted into a __bank pointervoid h(){ C Cd; // Resides on the stack}C * Cp; // Creates a pointer to bank memoryC __bank2 * Cp; // Creates a pointer to bank2 memory

Note: Whenever a class type associated with a class memory type, like C, must be declared, the class memory type must be mentioned as well:

class __bank2 C;

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 123: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Using C++

Also note that class types associated with different class memories are not compatible types.

There is a built-in operator that returns the class memory type associated with a class, __memory_of(class). For instance, __memory_of(C) returns __bank.

When inheriting, the rule is that it must be possible to convert implicitly a pointer to a subclass into a pointer to its base class. This means that a subclass can have a more restrictive class memory than its base class, but not a less restrictive class memory.

class __bank D : public C { // OK, same class memory public: void g(); int j;};

class __bank2 E : public C { // OK, bank2 memory is inside bank public: void g() // Has a this pointer pointing into bank2 memory { f(); // Gets a this pointer into bank memory } int j;};

class __eeprom F : public C { // Not OK, eeprom memory isn’t // inside bank memory public: void g(); int j;};

class G : public C { // OK, will be associated with same class // memory as C public: void g(); int j;};

A new expression on the class will allocate memory in the heap residing in the class memory. A delete expression will naturally deallocate the memory back to the same heap. To override the default new and delete operator for a class, declare

void *operator new(size_t);void operator delete(void *);

as member functions, just like in ordinary C++.

CPIC18-3

Part 1. Using the compiler 103

Page 124: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

104

Feature descriptions

If a pointer to class memory cannot be implicitly casted into a default pointer type, no temporaries can be created for that class.

For more information about memory types, see Memory types, page 12.

FUNCTIONS

A function with extern "C" linkage is compatible with a function that has C++ linkage.

Example

extern "C" { typedef void (*fpC)(void); // A C function typedef};void (*fpCpp)(void); // A C++ function typedef

fpC f1;fpCpp f2;void f(fpC);

f(f1); // Always worksf(f2); // fpCpp is compatible with fpC

TEMPLATES

Extended EC++ supports templates according to the C++ standard, except for the support of the export keyword. The implementation uses a two-phase lookup which means that the keyword typename has to be inserted wherever needed. Furthermore, at each use of a template, the definitions of all possible templates must be visible. This means that the definitions of all templates have to be in include files or in the actual source file.

Templates and data memory attributes

For data memory attributes to work as expected in templates, two elements of the standard C++ template handling have been changed—class template partial specialization matching and function template parameter deduction.

In Extended Embedded C++, the class template partial specialization matching algorithm works like this:

When a pointer or reference type is matched against a pointer or reference to a template parameter type, the template parameter type will be the type pointed to, stripped of any data memory attributes, if the resulting pointer or reference type is the same.

Example

// We assume that far is the memory type of the default pointer.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 125: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Using C++

template<typename> class Z;template<typename T> class Z<T *>;

Z<int __bank2 *> zn; // T = int __bank2Z<int __bank *> zf; // T = intZ<int *> zd; // T = intZ<int __eeprom *> zh; // T = int __eeprom

In Extended Embedded C++, the function template parameter deduction algorithm works like this:

When function template matching is performed and an argument is used for the deduction; if that argument is a pointer to a memory that can be implicitly converted to a default pointer, do the parameter deduction as if it was a default pointer.

When an argument is matched against a reference, do the deduction as if the argument and the parameter both were pointers.

Example

template<typename T> void fun(T *);

fun((int __bank2 *) 0); // T = intfun((int *) 0); // T = intfun((int __bank *) 0); // T = intfun((int __eeprom *) 0); // T = int __eeprom

Note that line 3 above gets a different result than the analogous situation with class template specializations.

For templates that are matched using this modified algorithm, it is impossible to get automatic generation of special code for pointers to small memory types. For large and “other” memory types (memory that cannot be pointed to by a default pointer) it is possible. In order to make it possible to write templates that are fully memory-aware—in the rare cases where this is useful—use the #pragma basic_template_matching directive in front of the template function declaration. That template function will then match without the modifications described above.

Example

#pragma basic_template_matchingtemplate<typename T> void fun(T *);

fun((int __bank2 *) 0); // T = int __bank2

CPIC18-3

Part 1. Using the compiler 105

Page 126: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

106

Feature descriptions

Non-type template parameters

It is allowed to have a reference to a memory type as a template parameter, even if pointers to that memory type are not allowed.

Example

extern int __bank1 x;

template<__bank1 int &y>void foo() { y = 17; }

void bar() { foo<x>();}

The standard template library

The STL (standard template library) delivered with the product is tailored for Extended EC++, as described in Extended Embedded C++, page 100.

STL and the IAR C-SPY Debugger

C-SPY has built-in display support for the STL containers.

VARIANTS OF CASTS

In Extended EC++ the following additional C++ cast variants can be used:

const_cast<t2>(t), static_cast<t2>(t), reinterpret_cast<t2>(t).

MUTABLE

The mutable attribute is supported in Extended EC++. A mutable symbol can be changed even though the whole class object is const.

NAMESPACE

The namespace feature is only supported in Extended EC++. This means that you can use namespaces to partition your code. Note, however, that the library itself is not placed in the std namespace.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 127: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Using C++

THE STD NAMESPACE

The std namespace is not used in either standard EC++ or in Extended EC++. If you have code that refers to symbols in the std namespace, simply define std as nothing; for example:

#define std // Nothing here

POINTER TO MEMBER FUNCTIONS

A pointer to a member function can only contain a default function pointer, or a function pointer that can implicitly be casted to a default function pointer. To use a pointer to a member function, make sure that all functions that should be pointed to reside in the default memory or a memory contained in the default memory.

Example

class X{public: __bank1_func void f();};void (__bank_func X::*pmf)() = &X::f;

USING INTERRUPTS AND EC++ DESTRUCTORS

If interrupts are enabled and the interrupt functions use class objects that have destructors, there may be problems if the program exits either by using exit or by returning from main. If an interrupt occurs after an object has been destroyed, there is no guarantee that the program will work properly.

To avoid this, you must override the function exit(int).

The standard implementation of this function (located in the file exit.c) looks like this:

extern void _exit(int arg);void exit(int arg){ _exit(arg);}

_exit(int) is responsible for calling the destructors of global class objects before ending the program.

To avoid interrupts, place a call to the intrinsic function __disable_interrupt before the call to _exit.

CPIC18-3

Part 1. Using the compiler 107

Page 128: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

108

Feature descriptions

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 129: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Efficient coding for embedded applicationsFor embedded systems, the size of the generated code and data is very important, because using smaller external memory or on-chip memory can significantly decrease the cost and power consumption of a system.

This chapter gives an overview about how to write code that compiles to efficient code for an embedded application. The issues discussed are:

● Taking advantage of the compilation system

● Selecting data types and placing data in memory

● Writing efficient code.

As a part of this, the chapter also demonstrates some of the more common mistakes and how to avoid them, and gives a catalog of good coding techniques.

Taking advantage of the compilation systemLargely, the compiler determines what size the executable code for the application will be. The compiler performs many transformations on a program in order to generate the best possible code. Examples of such transformations are removing superfluous code, reordering computations in a more efficient order, and replacing arithmetic operations by cheaper operations.

The linker should also be considered an integral part of the compilation system, since there are some optimizations that are performed by the linker. For instance, all unused functions and variables are removed and not included in the final object file. It is also as input to the linker you specify the memory layout. For detailed information about how to design the linker command file to suit the memory layout of your target system, see the chapter Placing code and data.

CPIC18-3

Part 1. Using the compiler 109

Page 130: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

110

Taking advantage of the compilation system

CONTROLLING COMPILER OPTIMIZATIONS

The PIC18 IAR C/C++ Compiler allows you to specify whether generated code should be optimized for size or for speed, at a selectable optimization level. The purpose of optimization is to reduce the code size and to improve the execution speed. When only one of these two goals can be reached, the compiler prioritizes according to the settings you specify. Note that one optimization sometimes enables other optimizations to be performed, and an application may become smaller even when optimizing for speed rather than size.

The following table describes the optimization levels:

By default, the same optimization level for an entire project or file is used, but you should consider using different optimization settings for different files in a project. For example, put code that must execute very quickly into a separate file and compile it for minimal execution time (maximum speed), and the rest of the code for minimal code size. This will give a small program, which is still fast enough where it matters. The #pragma optimize directive allows you to fine-tune the optimization for specific functions, such as time-critical functions.

A high level of optimization will result in increased compile time, and may also make debugging more difficult, since it will be less clear how the generated code relates to the source code. At any time, if you experience difficulties when debugging your code, try lowering the optimization level.

Optimization level Description

None (Best debug support) Variables live through their entire scope

Low Dead code eliminationRedundant label eliminationRedundant branch elimination

Medium Live-dead analysis and optimizationCode hoistingRegister content analysis and optimizationCommon subexpression elimination

High (Maximum optimization) Peephole optimizationLoop unrollingFunction inliningCode motionType-based alias analysis

Table 27: Compiler optimization levels

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 131: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Efficient coding for embedded applications

Both compiler options and pragma directives are available for specifying the preferred type and level of optimization. The chapter Compiler options contains reference information about the command line options used for specifying optimization type and level. Refer to the IAR Embedded Workbench® IDE User Guide for information about the compiler options available in the IAR Embedded Workbench IDE. Refer to #pragma optimize, page 189, for information about the pragma directives that can be used for specifying optimization type and level.

FINE-TUNING ENABLED TRANSFORMATIONS

At each optimization level you can disable some of the transformations individually. To disable a transformation, use either the appropriate option, for instance the command line option --no_inline, alternatively its equivalent in the IAR Embedded Workbench IDE Function inlining, or the #pragma optimize directive. The following transformations can be disabled:

● Common subexpression elimination● Loop unrolling● Function inlining● Code motion● Type-based alias analysis.

Common subexpression elimination

Redundant re-evaluation of common subexpressions is by default eliminated at optimization levels Medium and High. This optimization normally reduces both code size and execution time. However, the resulting code might be difficult to debug.

Note: This option has no effect at optimization levels Low and None.

To read more about the command line option, see --no_cse, page 166.

Loop unrolling

It is possible to duplicate the loop body of a small loop, whose number of iterations can be determined at compile time, to reduce the loop overhead.

This optimization, which can be performed at optimization level High, normally reduces execution time, but increases code size. The resulting code might also be difficult to debug.

The compiler heuristically decides which loops to unroll. Different heuristics are used when optimizing for speed and size.

Note: This option has no effect at optimization levels None, Low, and Medium.

To read more about the command line option, see --no_unroll, page 168.

CPIC18-3

Part 1. Using the compiler 111

Page 132: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

112

Taking advantage of the compilation system

Function inlining

Function inlining means that a simple function, whose definition is known at compile time, is integrated into the body of its caller to eliminate the overhead of the call. This optimization, which is performed at optimization level High, normally reduces execution time, but increases code size. The resulting code might also be difficult to debug.

The compiler decides which functions to inline. Different heuristics are used when optimizing for speed and size.

Note: This option has no effect at optimization levels None, Low, and Medium.

To read more about the command line option, see --no_inline, page 167.

Code motion

Evaluation of loop-invariant expressions and common subexpressions are moved to avoid redundant re-evaluation. This optimization, which is performed at optimization level High, normally reduces code size and execution time. The resulting code might however be difficult to debug.

Note: This option has no effect at optimization levels None, and Low.

Type-based alias analysis

When two or more pointers reference the same memory location, these pointers are said to be aliases for each other. The existence of aliases makes optimization more difficult because it is not necessarily known at compile time whether a particular value is being changed.

Type-based alias analysis optimization assumes that all accesses to an object will take place using its declared type or as a char type. This assumption lets the compiler detect whether pointers may reference the same memory location or not.

Type-based alias analysis is performed at optimization level High. For ISO/ANSI standard-conforming C or C++ application code, this optimization can reduce code size and execution time. However, non-standard-conforming C or C++ code might result in the compiler producing code that leads to unexpected behavior. Therefore, it is possible to turn this optimization off.

Note: This option has no effect at optimization levels None, Low, and Medium.

To read more about the command line option, see --no_tbaa, page 167.

Example

short f(short * p1, long * p2){

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 133: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Efficient coding for embedded applications

*p2 = 0; *p1 = 1; return *p2;}

With type-based alias analysis, it is assumed that a write access to the short pointed to by p1 cannot affect the long value that p2 points to. Thus, it is known at compile time that this function returns 0. However, in non-standard-conforming C or C++ code these pointers could overlap each other by being part of the same union. By using explicit casts, you can also force pointers of different pointer types to point to the same memory location.

Selecting data types and placing data in memoryFor efficient treatment of data, you should consider the data types used and the most efficient placement of the variables.

USING EFFICIENT DATA TYPES

The data types you use should be considered carefully, because this can have a large impact on code size and code speed.

● Use small and unsigned data types, (unsigned char and unsigned short) unless your application really requires signed values.

● Non-char bitfields with sizes other than 1 bit should be avoided because they will result in inefficient code compared to bit operations.

● Using floating-point types on a microprocessor without a math co-processor is very inefficient, both in terms of code size and execution speed.

● Declaring a pointer to const data tells the calling function that the data pointed to will not change, which opens up for better optimizations.

For details about representation of supported data types, pointers, and structures types, see the chapter Data representation.

DATA MODEL AND DATA MEMORY ATTRIBUTES

For most applications it is sufficient to use the data model feature to specify the default memory for the data objects. However, for individual objects it might be necessary to specify other memory attributes in certain cases, for example:

● An application where all data, with the exception of one large chunk of data, fits into the region of one of the smaller memory types

● Data that must be placed at a specific memory location.

CPIC18-3

Part 1. Using the compiler 113

Page 134: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

114

Selecting data types and placing data in memory

The PIC18 IAR C/C++ Compiler provides five different data memory type attributes—__bank, __bankN, __eeprom, __extmem and __nonbanked. Efficient usage of memory type attributes can significantly reduce the application size.

For details about the memory types, see Memory types, page 12.

USING THE BEST POINTER TYPE

The generic pointers can point to all memory spaces, which makes them simple and also tempting to use. However, they carry a cost in that special code is needed before each pointer access to check which memory a pointer points to and performing appropriate actions. Use the smallest pointer type you can, and avoid any generic pointers unless necessary. Specifically, note that pointers to bank0, bank1 etc. memory are smaller than pointers to bank memory and pointers to bank memory are smaller than dptr pointers.

ANONYMOUS STRUCTS AND UNIONS

When declaring a structure or union without a name, it becomes anonymous. The effect is that its members will only be seen in the surrounding scope.

Anonymous structures are part of the C++ language; however, they are not part of the C standard. In the PIC18 IAR C/C++ Compiler they can be used in C if language extensions are enabled.

In the IAR Embedded Workbench IDE, language extensions are enabled by default.

Use the -e compiler option to enable language extensions. See -e, page 159, for additional information.

Example

In the following example, the members in the anonymous union can be accessed, in function f, without explicitly specifying the union name:

struct s{

char tag;union{

long l;float f;

};} st;

void f(void){

st.l = 5;}

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 135: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Efficient coding for embedded applications

The member names must be unique in the surrounding scope. Having an anonymous struct or union at file scope, as a global, external, or static variable is also allowed. This could for instance be used for declaring I/O registers, as in the following example:

__no_init volatileunion{

unsigned char IOPORT;struct{

unsigned char way: 1;unsigned char out: 1;

};} @ 0x123;

This declares an I/O register byte IOPORT at the address 0x123. The I/O register has 2 bits declared, way and out. Note that both the inner structure and the outer union are anonymous.

The following example illustrates how variables declared this way can be used:

void test(void){

IOPORT = FE0;way = 1;out = 1;

}

Anonymous structures and unions are implemented in terms of objects named after the first field, with a prefix _A to place the name in the implementation part of the namespace. In this example, the anonymous union will be implemented through an object named _A_IOPORT.

Writing efficient codeThis section contains general programming hints on how to implement functions to make your applications robust, but at the same time facilitate compiler optimizations.

The following is a list of programming techniques that will, when followed, enable the compiler to better optimize the application.

● Local variables—auto variables and parameters—are preferred over static or global variables. The reason is that the optimizer must assume, for example, that called functions may modify non-local variables. When the life spans for local variables end, the previously occupied memory can then be reused. Globally declared variables will occupy data memory during the whole program execution.

CPIC18-3

Part 1. Using the compiler 115

Page 136: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

116

Writing efficient code

● Avoid taking the address of local variables using the & operator. This is inefficient because it means that the optimizer can no longer assume that the local variable is unaffected over function calls.

● Module-local variables—variables that are declared static—are preferred over global variables. Also avoid taking the address of frequently accessed static variables.

● The compiler is capable of inlining functions. This means that instead of calling a function, the compiler inserts the content of the function at the location where the function was called. The result is a faster, but often larger, application. Also, inlining may enable further optimizations. The compiler often inlines small functions declared static. The use of the #pragma inline directive and the C++ keyword inline gives you fine-grained control, and it is the preferred method compared to the traditional way of using preprocessor macros. This feature can be disabled using the --no_inline command line option; see --no_inline, page 167.

● Avoid using inline assembler. Instead, try writing the code in C or C++, use intrinsic functions, or write a separate module in assembler language. For more details, see Mixing C and assembler, page 85.

SAVING STACK SPACE AND RAM MEMORY

The following is a list of programming techniques that will, when followed, save memory and stack space:

● If stack space is limited, avoid long call chains and recursive functions.● Avoid using large non-scalar types, such as structures, as parameters or return type;

in order to save stack space, you should instead pass them as pointers or, in C++, as references.

FUNCTION PROTOTYPES

It is possible to declare and define functions using one of two different styles:

● Prototyped● Kernighan & Ritchie C (K&R C)

Both styles are included in the C standard; however, it is recommended to use the prototyped style, since it makes it easier for the compiler to find problems in the code. In addition, using the prototyped style will make it possible to generate more efficient code, since type promotion (implicit casting) is not needed. The K&R style is only supported for compatibility reasons.

To make the compiler verify that all functions have proper prototypes, use the compiler option Require prototypes (--require_prototypes).

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 137: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Efficient coding for embedded applications

Prototyped style

In prototyped function declarations, the type for each parameter must be specified.

int test(char, int); /* declaration */int test(char a, int b) /* definition */{ .....}

Kernighan & Ritchie style

In K&R style—traditional pre-ISO/ANSI C—it is not possible to declare a function prototyped. Instead, an empty parameter list is used in the function declaration. Also, the definition looks different.

int test(); /* old declaration */int test(a,b) /* old definition */char a;int b;{ .....}

INTEGER TYPES AND BIT NEGATION

There are situations when the rules for integer types and their conversion lead to possibly confusing behavior. Things to look out for are assignments or conditionals (test expressions) involving types with different size and logical operations, especially bit negation. Here, types also include types of constants.

In some cases there may be warnings (for example, constant conditional or pointless comparison), in others just a different result than what is expected. Under certain circumstances the compiler may warn only at higher optimizations, for example, if the compiler relies on optimizations to identify some instances of constant conditionals. In the following example an 8-bit character, a 16-bit integer, and two’s complement is assumed:

void f1(unsigned char c1){ if (c1 == ~0x80) ;}

Here, the test is always false. On the right hand side, 0x80 is 0x0080, and ~0x0080 becomes 0xFF7F. On the left hand side, c1 is an 8-bit unsigned character, and, thus, cannot be larger than 255. It also cannot be negative, thus the integral promoted value can never have the top 8 bits set.

CPIC18-3

Part 1. Using the compiler 117

Page 138: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

118

Writing efficient code

PROTECTING SIMULTANEOUSLY ACCESSED VARIABLES

Variables that are accessed from multiple threads, for example from main or an interrupt, must be properly marked and have adequate protection, the only exception to this is a variable that is always read-only.

To mark a variable properly, use the volatile keyword. This informs the compiler, among other things, that the variable can be changed from other threads. The compiler will then avoid optimizing on the variable, will not delay writes to it, and be careful accessing the variable only the number of times given in the source code.

A sequence that accesses a volatile declared variable must also not be interrupted. This can be achieved using the __monitor keyword in interruptible code. This must be done for both write and read sequences, otherwise you might end up reading a partially updated variable. This is true for all variables of all sizes. Accessing a small-sized variable can be an atomic operation, but this is not guaranteed and you should not rely on it unless you continuously study the compiler output. It is safer to ensure that the sequence is an atomic operation using the __monitor keyword.

ACCESSING SPECIAL FUNCTION REGISTERS

Specific header files for a number of PIC18 derivatives are included in the PIC18 IAR C/C++ Compiler delivery. The header files are named iochip.h and define the processor-specific special function registers (SFRs).

SFRs with bitfields are declared in the header file. The following example is from io18c242.h:

__SFR_BITS(__nonbanked,INTCON,0xFF2,RBIF,INT0IF,TMR0IF,RBIE, INT0IE,TMR0IE,PEIE,GIE)

By including the appropriate include file into the user code it is possible to access either the whole register or any individual bit (or bitfields) from C code as follows:

/* whole register access */INTCON = 0x1234;

/* Bitfield accesses */GIE = 1;PEIE = 1;

You can also use the header files as templates when you create new header files for other PIC18 derivatives. For details about the @ operator, see Located data, page 38.

NON-INITIALIZED VARIABLES

Normally, the runtime environment will initialize all global and static variables when the application is started.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 139: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Efficient coding for embedded applications

The compiler supports the declaration of variables that will not be initialized, using the __no_init type modifier. They can be specified either as a keyword or using the #pragma object_attribute directive. The compiler places such variables in separate segments, according to the specified memory keyword. See the chapter Placing code and data for more information.

For __no_init, the const keyword implies that an object is read-only, rather than that the object is stored in read-only memory. It is not possible to give a __no_init object an initial value.

Variables declared using the __no_init keyword could, for example, be large input buffers or mapped to special RAM that keeps its content even when the application is turned off.

For information about the __no_init keyword, see page 181. Note that to use this keyword, language extensions must be enabled; see -e, page 159. For information about the #pragma object_attribute, see page 188.

CPIC18-3

Part 1. Using the compiler 119

Page 140: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

120

Writing efficient code

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 141: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Part 2. Compiler referenceThis part of the PIC18 IAR C/C++ Compiler Reference Guide contains the following chapters:

● Data representation

● Segment reference

● Compiler options

● Extended keywords

● Pragma directives

● The preprocessor

● Intrinsic functions

● Library functions

● Implementation-defined behavior

● IAR language extensions

● Diagnostics.

CPIC18-3

121

Page 142: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

122

CPIC18-3

Page 143: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Data representationThis chapter describes the data types, pointers, and structure types supported by the PIC18 IAR C/C++ Compiler.

See the chapter Efficient coding for embedded applications for information about which data types and pointers provide the most efficient code for your application.

AlignmentEvery C data object has an alignment that controls how the object can be stored in memory. Should an object have an alignment of, for example, four, it must be stored on an address that is divisible by four.

The reason for the concept of alignment is that some processors have hardware limitations for how the memory can be accessed.

Assume that a processor can read 4 bytes of memory using one instruction, but only when the memory read is placed on an address divisible by 4. Then, 4-byte objects, such as long integers, will have alignment 4.

Another processor might only be able to read 2 bytes at a time; in that environment, the alignment for a 4-byte long integer might be 2.

A structure type will inherit the alignment from its components.

All objects must have a size that is a multiple of the alignment. Otherwise, only the first element of an array would be placed in accordance with the alignment requirements.

In the following example, the alignment of the structure is 4, under the assumption that long has alignment 4. Its size is 8, even though only 5 bytes are effectively used.

struct str { long a; char b;};

In standard C, the size of an object can be accessed using the sizeof operator.

CPIC18-3

Part 2. Compiler reference 123

Page 144: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

124

Basic data types

Basic data types

The compiler supports both all ISO/ANSI C basic data types and some additional types.

INTEGER TYPES

The following table gives the size and range of each integer data type:

Signed variables are represented using the two’s complement form.

Bool

The bool data type is supported by default in the C++ language. If you have enabled language extensions, the bool type can also be used in C source code if you include the file stdbool.h. This will also enable the boolean values false and true.

The enum type

ISO/ANSI C specifies that constants defined using the enum construction should be representable using the type int. The compiler will use the shortest signed or unsigned type required to contain the values.

When IAR Systems language extensions are enabled, and in C++, the enum constants and types can also be of the type long or unsigned long.

The char type

The char type is by default unsigned in the compiler, but the --char_is_signed compiler option allows you to make it signed. Note, however, that the library is compiled with the char type as unsigned.

Data type Size Range

bool 8 bits 0 to 1

char 8 bits 0 to 255

signed char 8 bits -128 to 127

unsigned char 8 bits 0 to 255

signed short 16 bits -32768 to 32767

unsigned short 16 bits 0 to 65535

signed int 16 bits -32768 to 32767

unsigned int 16 bits 0 to 65535

signed long 32 bits -231 to 231-1

unsigned long 32 bits 0 to 232-1

Table 28: Integer types

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 145: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Data representation

The wchar_t type

The wchar_t data type is an integer type whose range of values can represent distinct codes for all members of the largest extended character set specified among the supported locals.

The wchar_t data type is supported by default in the C++ language. To use the wchar_t type also in C source code, you must include the file stddef.h from the runtime library.

Note: The IAR CLIB Library has only rudimentary support for wchar_t.

Bitfields

In ISO/ANSI C, int and unsigned int can be used as the base type for integer bitfields. In the PIC18 IAR C/C++ Compiler, any integer type can be used as the base type when language extensions are enabled.

Bitfields in expressions will have the same data type as the integer base type.

By default, the compiler places bitfield members from the least significant to the most significant bit in the container type.

By using the directive #pragma bitfields=reversed, the bitfield members are placed from the most significant to the least significant bit.

FLOATING-POINT TYPES

In the PIC18 IAR C/C++ Compiler, floating-point values are represented in standard IEEE format. The compiler does not support subnormal numbers. All operations that should produce subnormal numbers will instead generate zero.

The ranges and sizes for the floating-point types, float, double, and long double, are:

● Size: 32 bits● Range (+/-): ±1.18E-38 to ±3.39E+38● Exponent: 8 bits● Mantissa: 23 bits

The representation of a floating-point number as an integer is:

The value of the number is:

(-1)S * 2(Exponent-127) * 1.Mantissa

The precision of the float operators (+, -, *, and /) is approximately 7 decimal digits.

S

31 30 23 22 0

Exponent Mantissa

CPIC18-3

Part 2. Compiler reference 125

Page 146: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

126

Pointer types

Special cases

The following applies to the floating-point format:

● Zero is represented by zero mantissa and exponent. The sign bit signifies positive or negative zero.

● Infinity is represented by setting the exponent to the highest value and the mantissa to zero. The sign bit signifies positive or negative infinity.

Note: The runtime libraries do not fully support the special cases of floating-point numbers, such as NaN.

Pointer typesThe PIC18 IAR C/C++ Compiler has two basic types of pointers: function pointers and data pointers.

CASTING

Casts between pointers have the following characteristics:

● The __nonbanked and __bankN pointers can be cast to a __bank pointer● The __bank, __constptr, __eeprom, and __extmem pointers can be cast to a

__dptr pointer● The __bankN_func pointer can be cast to a __bank_func pointer● Casting a value of an integer type to a pointer of a smaller type is performed by

truncation● Casting a value of an integer type to a pointer of a larger type is performed by zero

extension● Casting a pointer type to a smaller integer type is performed by truncation● Casting a pointer type to a larger integer type, the pointer is first cast to the largest

possible pointer that fits in the integer● Casting a data pointer to a function pointer and vice versa is illegal● Casting a function pointer to an integer type gives an undefined result.

size_t

size_t is the unsigned integer type required to hold the maximum size of an object. In the large data model, the size of size_t is 32 bits. In the small data model, the size is 16 bits.

ptrdiff_t

ptrdiff_t is the type of the signed integer required to hold the difference between two pointers to elements of the same array. In the large data model, the size of ptrdiff_t is 32 bits. In the small data model, the size is 16 bits.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 147: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Data representation

Note: Subtracting the start address of an object from the end address can yield a negative value, because the object can be larger than what the ptrdiff_t can represent. See this example:

char buff[60000]; /* Assuming ptrdiff_t is a 16-bit */char *p1 = buff; /* signed integer type. */char *p2 = buff + 60000;ptrdiff_t diff = p2 - p1;

intptr_t

intptr_t is a signed integer type large enough to contain a void *. In the large data model, the size of intptr_t is 32 bits. In the small data model, the size is 16 bits.

uintptr_t

uintptr_t is equivalent to intptr_t, with the exception that it is unsigned.

Structure types The members of a struct are stored sequentially in the order in which they are declared: the first member has the lowest memory address.

GENERAL LAYOUT

Members of a struct (fields) are always allocated in the order given in the declaration. The members are placed in memory according to the given alignment (offsets).

Example

struct {short s; /* stored in byte 0 and 1 */char c; /* stored in byte 2 */long l; /* stored in byte 3, 4, 5, and 6 */char c2; /* stored in byte 7 */

} s;

The following diagram shows the layout in memory:

s.s s.c s.l s.c2

1 byte 4 bytes 1 byte2 bytes

CPIC18-3

Part 2. Compiler reference 127

Page 148: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

128

Type and object attributes

Type and object attributesThe PIC18 IAR C/C++ Compiler provides a set of attributes that support specific features of the PIC18 microcontroller. There are two basic types of attributes—type attributes and object attributes.

Type attributes affect the external functionality of the data object or function. For instance, how an object is placed in memory, or in other words, how it is accessed.

Object attributes affect the internal functionality of the data object or function.

To understand the syntax rules for the different attributes, it is important to be familiar with the concepts of the type attributes and the object attributes.

For information about how to use attributes to modify data, see the chapter Data storage. For information about how to use attributes to modify functions, see the chapter Functions. For detailed information about each attribute, see Descriptions of extended keywords, page 177.

TYPE ATTRIBUTES

Type attributes define how a function is called, or how a data object is accessed. This means that type attributes must be specified both when they are defined and in the declaration.

You can either place the type attributes directly in your source code, or use the pragma directive #pragma type_attribute.

Type attributes can be further divided into memory attributes and general type attributes.

Memory attributes

A memory attribute corresponds to a certain logical or physical memory in the microcontroller.

● Available function memory attributes: __bank_func, __bankN_func, and __stack_func

● Available data memory attributes: __bank, __bankN, __eeprom, __extmem, and __nonbanked

For each level of indirection, you can only specify one memory attribute.

Pointer attributes

Available pointer attributes are all memory attributes, __constptr, and __dptr.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 149: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Data representation

General type attributes

The following general type attributes are available:

● Function type attributes change the calling convention of a function: __interrupt and __monitor

● Data type attributes: const and volatile

For each level of indirection, you can specify as many type attributes as required.

To read more about volatile, see Declaring objects volatile, page 129.

OBJECT ATTRIBUTES

Object attributes affect functions and data objects, but not how the function is called or how the data is accessed. This means that an object attribute does not need to be present in the declaration of an object.

The following object attributes are available:

● Object attributes that can be used for variables: __no_init● Object attributes that can be used for functions and variables: location, @, and

__root

● Object attributes that can be used for functions: __intrinsic, __noreturn, and vector.

Note: The __intrinsic attribute is reserved for compiler internal use only.

You can specify as many object attributes as required.

DECLARING OBJECTS IN SOURCE FILES

When declaring objects, note that he IAR-specific attributes work exactly like const. One exception to this is attributes that are declared in front of the type specifier apply to all declared objects.

See More examples, page 17.

DECLARING OBJECTS VOLATILE

There are three main reasons for declaring an object volatile:

● Shared access; the object is shared between several tasks in a multitasking environment

● Trigger access; as for a memory-mapped SFR where the fact that an access occurs has an effect

● Modified access; where the contents of the object can change in ways not known to the compiler.

CPIC18-3

Part 2. Compiler reference 129

Page 150: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

130

Data types in C++

Definition of access to volatile objects

The ISO/ANSI standard defines an abstract machine, which governs the behavior of accesses to volatile declared objects. The PIC18 IAR C/C++ Compiler considers each read and write access to an object that has been declared volatile as an access. The unit for the access is either the entire object or, for accesses to an element in a composite object—such as an array, struct, class, or union—the element. For example:

char volatile a;a = 5; /* A write access */a += 6; /* First a read then a write access */

An access to a bitfield is treated as an access to the underlaying type.

Rules for accesses

Accesses to volatile declared objects are subject to the following rules:

● All accesses are preserved● All accesses are complete, that is, the whole object is accessed● All accesses are performed in the same order as given in the abstract machine.

The PIC18 IAR C/C++ Compiler only adheres to these rules for 8-bit accesses of volatile declared objects in bank0–bank15 memory (RAM). For all other combinations of object types, only rule number one applies.

Data types in C++ In C++, all plain C data types are represented in the same way as described earlier in this chapter. However, if any Embedded C++ features are used for a type, no assumptions can be made concerning the data representation. This means, for example, that it is not legal to write assembler code that accesses class members.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 151: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Segment referenceThe PIC18 IAR C/C++ Compiler places code and data into named segments which are referred to by the IAR XLINK Linker. Details about the segments are required for programming assembler language modules, and are also useful when interpreting the assembler language output from the compiler.

For information about how to define segments in the linker command file, see Customizing the linker command file, page 31.

Summary of segments The table below lists the segments that are available in the PIC18 IAR C/C++ Compiler. Note that located denotes absolute location using the @ operator or the #pragma location directive. The XLINK segment memory type CODE, CONST, DATA, IDATA, or XDATA indicates in which memory area the segment should be placed; see Table 8, XLINK segment memory types, page 30.

Segment Contents

BANK0_A–BANK15_A Variables placed with the @ operator or #pragma location directive

BANK0_I–BANK15_I Initialized variables

BANK0_ID–BANK15_ID Data to initialize the matching _I segment

BANK0_N–BANK15_N Variables declared with the keyword __no_init

BANK0_Z–BANK15_Z Zero-initialized variables

BANK_A Holds __bank declared located data

BANK_I Initialized __bank declared variables

BANK_ID Data to initialize the matching _I segment

BANK_N __bank declared variables declared with the keyword __no_init

BANK_Z Zero-initialized __bank declared variables

BANK_ZD Initializer __bank declared data used by the BANK_Z segment

BANKN_A Holds __nonbanked declared located data.

BANKN_I Holds initialized __nonbanked declared data.

Table 29: Segment summary

CPIC18-3

Part 2. Compiler reference 131

Page 152: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

132

Summary of segments

BANKN_ID Holds __nonbanked declared data that is copied to BANKN_I by cstartup.

BANKN_N Holds uninitialized __nonbanked declared data.

BANKN_Z Holds zero-initialized __nonbanked declared data.

CODE Program and library code

CONST const-declared variables

CSTACK Hardware return address stack

DIFUNCT Holds pointers to code, typically C++ constructors, which should be executed by the system startup code before main is called.

EEPROM_A __eeprom declared variables placed with the @ operator or #pragma location directive

EEPROM_I Initialized __eeprom declared variables

EEPROM_ID Data to initialize the matching _I segment

EEPROM_N __eeprom declared variables declared with the keyword __no_init

EEPROM_Z Zero-initialized __eeprom declared variables

EXTMEM_A Variables placed with the @ operator or #pragma location directive and declared __extmem

EXTMEM_I Initialized variables declared __extmem

EXTMEM_ID Data to initialize the matching _I segment

EXTMEM_N Variables declared with the keywords __no_init and __extmem

EXTMEM_Z Zero-initialized variables declared __extmem

HEAP Holds the heap data used by malloc and free.

ICODE CSTARTUP and initializer code

INTVEC Reset and interrupt vectors

OVERLAY Auto variables using the overlay code model

OVERLAY0–OVERLAY15 Auto variables using the overlay code model

STACK Auto variables using the stack code model

WRKSEG Holds the workseg registers

Segment Contents

Table 29: Segment summary (Continued)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 153: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Segment reference

Descriptions of segmentsThe following section gives reference information about each segment. For detailed information about the extended keywords mentioned here, see the chapter Extended keywords.

BANK0_A–BANK15_A Holds variables declared __bank0–__bank15 with an absolute location.

Type

Read/write.

Memory area

Data. The address range is 0x0–0x0FFF.

Description

The BANK0_A–BANK15_A segments hold located variables, that is variables that have been assigned an absolute location by use of the @ operator or the #pragma location directive.

BANK0_I–BANK15_I Initialized data declared __bank0–__bank15.

Type

Read/write.

Memory area

Data. The address range is 0x0–0xFFF.

Description

The BANK0_I–BANK15_I segments hold static and global variables that have been declared with non-zero initial values.

BANK0_ID–BANK15_ID Initializer data.

Type

Read-only.

CPIC18-3

Part 2. Compiler reference 133

Page 154: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

134

Descriptions of segments

Memory area

Code. The address range is 0x0–0x1FFFFF.

Description

The BANK0_ID–BANK15_ID segments hold initial values for the variables located in the corresponding BANK0_I–BANK15_I segment. These values are copied from BANKN_ID to BANKN_I during system initialization.

BANK0_N–BANK15_N Uninitialized data declared __bank0–__bank15.

Type

Read/write.

Memory area

Data. The address range is 0x0–0xFFF.

Description

The BANK0_N–BANK15_N segments hold static and global variables that will not be initialized at system startup, for example variables that are to be placed in non-volatile memory. These variables have been declared __no_init, created __no_init by use of the #pragma memory directive, or allocated by the compiler.

BANK0_Z–BANK15_Z Zero-initialized data declared __bank0–__bank15.

Type

Read/write.

Memory area

Data. The address range is 0x0–0xFFF.

Description

The BANK0_Z–BANK15_Z segments hold zero-initialized variables.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 155: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Segment reference

BANK_A Variables declared __bank with an absolute location.

Type

Read/write.

Memory area

Data. The address range is 0x0–0xFFF.

Description

The BANK_A segment holds located variables, that is variables that have been assigned an absolute location by use of the @ operator or the #pragma location directive.

BANK_I Initialized data declared __bank.

Type

Read/write.

Memory area

Data. The address range is 0x0–0xFFF.

Description

The BANK_I segment holds static and global variables that have been declared with non-zero initial values.

BANK_ID Initializer data.

Type

Read-only.

Memory area

Code. The address range is 0x0–0x1FFFFF.

Description

The BANK_ID segment holds initial values for the variables located in the BANK_I segment. These values are copied from BANK_ID to BANK_I during system initialization.

CPIC18-3

Part 2. Compiler reference 135

Page 156: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

136

Descriptions of segments

BANK_N Uninitialized data declared __bank.

Type

Read/write.

Memory area

Data. The address range is 0x0–0xFFF.

Description

The BANK_N segment holds static and global variables that will not be initialized at system startup, for example variables that are to be placed in non-volatile memory. These variables have been declared __no_init, created __no_init by use of the #pragma memory directive, or allocated by the compiler.

BANK_Z Zero-initialized data declared __bank.

Type

Read/write.

Memory area

Data. The address range is 0x0–0xFFF.

Description

The BANK_Z segment holds zero-initialized variables.

BANK_ZD Uninitialized data declared __bank.

Type

Read/write.

Memory area

Data. The address range is 0x0–0x1FFFFF.

Description

The BANK_ZD segment holds uninitialized data to be used by BANK_Z.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 157: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Segment reference

BANKN_A Variables declared __nonbanked with an absolute location.

Type

Read/write.

Memory area

Data. The address range is 0x0–0xFFF.

Description

The BANKN_A segment holds located variables, that is variables that have been assigned an absolute location by use of the @ operator or the #pragma location directive.

BANKN_I Initialized data declared __nonbanked.

Type

Read/write.

Memory area

Data. The address range is 0x0–0xFFF.

Description

The BANKN_I segment holds static and global variables that have been declared with non-zero initial values.

BANKN_ID Initializer data.

Type

Read-only.

Memory area

Code. The address range is 0x0–0x1FFFFF.

Description

The BANKN_ID segment holds initial values for the variables located in the BANKN_I segment. These values are copied from BANKN_ID to BANKN_I during system initialization.

CPIC18-3

Part 2. Compiler reference 137

Page 158: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

138

Descriptions of segments

BANKN_N Uninitialized data declared __nonbanked.

Type

Read/write.

Memory area

Data. The address range is 0x0–0xFFF.

Description

The BANKN_N segment holds static and global variables that will not be initialized at system startup, for example variables that are to be placed in non-volatile memory. These variables have been declared __no_init, created __no_init by use of the #pragma memory directive, or allocated by the compiler.

BANKN_Z Zero-initialized data declared __nonbanked.

Type

Read/write.

Memory area

Data. The address range is 0x0–0xFFF.

Description

The BANKN_Z segment holds zero-initialized variables.

CODE User program and library code.

Type

Read-only.

Memory area

Code. The address range is 0x0–0x01FFFFF.

Description

The CODE segment holds user program code and various library routines.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 159: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Segment reference

Notice that any assembler language routines called from C or C++ must meet the calling convention in use. For more information, see Calling convention, page 91.

CONST const-declared variables.

Type

Read/write.

Memory area

Code. The address range is 0x0–0x1FFFFF.

Description

The CONST segment holds variables declared as const. These are placed as a series of byte instructions.

CSTACK Pseudo stack segment.

Type

Not user-accessible.

Memory area

Hardware stack.

Description

CSTACK is the hardware return address stack.

DIFUNCT Holds the dynamic initialization vector used by C++.

Type

Read-only.

Memory area

Code. The address range is 0x0–0x1FFFFF.

CPIC18-3

Part 2. Compiler reference 139

Page 160: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

140

Descriptions of segments

EEPROM_A __eeprom-declared variables with an absolute location.

Type

Read/write.

Memory area

EEPROM.

Description

The EEPROM_A segment holds located variables, that is variables that have been assigned an absolute location by use of the @ operator or the #pragma location directive.

EEPROM_I Initialized __eeprom-declared variables.

Type

Read/write.

Memory area

EEPROM.

Description

The EEPROM_I segment holds static and global variables that have been declared with non-zero initial values.

EEPROM_ID Initializer EEPROM data.

Type

Read-only.

Memory area

Code. The address range is 0x0–0x1FFFFF.

Description

The EEPROM_ID segment holds initial values for the variables located in the EEPROM_I segment. These values are copied from EEPROM_ID to EEPROM_I during system initialization.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 161: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Segment reference

EEPROM_N Uninitialized __eeprom-declared variables.

Type

Read/write.

Memory area

EEPROM.

Description

The EEPROM_N segment holds static and global variables that will not be initialized at system startup, for example variables that are to be placed in non-volatile memory. These variables have been declared __no_init, created __no_init by use of the #pragma memory directive, or allocated by the compiler.

EEPROM_Z Zero-initialized __eeprom-declared variables.

Type

Read/write.

Memory area

EEPROM.

Description

The EEPROM_Z segment holds zero-initialized variables.

EXTMEM_A __extmem-declared variables with an absolute location.

Type

Read/write.

Memory area

Code.

CPIC18-3

Part 2. Compiler reference 141

Page 162: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

142

Descriptions of segments

Description

The EXTMEM_A segment holds located __extmem-declared variables, that is variables that have been assigned an absolute location by use of the @ operator or the #pragma location directive.

EXTMEM_I Initialized __extmem-declared variables.

Type

Read/write.

Memory area

Code.

Description

The EXTMEM_I segment holds __extmem-declared variables that have been declared with non-zero initial values. If the external memory is large, the EXTMEM_I segment will also be large. If you want to conserve program memory when using a large external memory, you should therefore declare __extmem-variables __non_init or zero-initialize them.

EXTMEM_ID Initializer EXTMEM data.

Type

Read-only.

Memory area

Code. The address range is 0x0–0x1FFFFF.

Description

The EXTMEM_ID segment holds initial values for the variables located in the EXTMEM_I segment. These values are copied from EXTMEM_ID to EXTMEM_I during system initialization.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 163: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Segment reference

EXTMEM_N Uninitialized __extmem-declared variables.

Type

Read/write.

Memory area

Code.

Description

The EXTMEM_N segment holds __extmem-declared variables that will not be initialized at system startup, for example variables that are to be placed in non-volatile memory. These variables have been declared __no_init, created __no_init by use of the #pragma memory directive, or allocated by the compiler.

EXTMEM_Z Zero-initialized __extmem-declared variables.

Type

Read/write.

Memory area

Code.

Description

The EXTMEM_Z segment holds zero-initialized __extmem-declared variables.

HEAP Dynamically allocated data.

Type

Read/write.

Memory area

Data. This segment must be placed in bank memory.

Description

Holds dynamically allocated data, in other words data used by malloc and free, and in C++, new and delete.

CPIC18-3

Part 2. Compiler reference 143

Page 164: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

144

Descriptions of segments

This segment is normally defined in the linker command file by the command:

-Z(DATA)HEAP+_HEAP_SIZE=start

where start is the location. See The heap, page 37 for more information.

ICODE CSTARTUP and initializer code.

Type

Read-only.

Memory area

Code. The address range is 0x0–0x1FFFFF.

Description

The ICODE segment holds the CSTARTUP and initializer code.

INTVEC Interrupt vector table.

Type

Read-only.

Memory area

Code.

Description

The INTVEC segment holds the interrupt vector table generated by the use of the __interrupt extended keyword.

Note: This segment must be placed at address 0 and forwards.

Example

See Interrupt vectors, page 39, for an example.

OVERLAY Overlay segment for the BANK segment.

Type

Read/write.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 165: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Segment reference

Memory area

Data. The address range is 0x0–0xFF.

Description

The OVERLAY segment holds parameters and auto objects. The static overlay scheme is invoked by a function declared __bank_func.

OVERLAY0–OVERLAY15 Overlay segments for the BANK0–BANK15 segments.

Type

Read/write.

Memory area

Data. The address range is 0x0–0xFFF in each bank.

Description

The OVERLAY0–OVERLAY15 segments hold parameters and auto variables. The static overlay scheme is invoked by a function declared __bankN_func.

STACK Auto variables using the stack code model.

Type

Read/write.

Memory area

Data. The address range 0x0–0xFFF.

Description

The STACK segment holds parameters and auto variables. It is invoked by a function declared __stack_func.

WRKSEG Holds the workseg registers.

Type

Read/write.

CPIC18-3

Part 2. Compiler reference 145

Page 166: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

146

Descriptions of segments

Memory area

Nonbanked memory.

Description

The WRKSEG register holds the working registers ?A0–?E0.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 167: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler optionsThis chapter explains how to set the compiler options from the command line, and gives detailed reference information about each option.

Refer to the IAR Embedded Workbench® IDE User Guide for information about the compiler options available in the IAR Embedded Workbench IDE and how to set them.

Setting command line optionsTo set compiler options from the command line, include them on the command line after the iccpic18 command, either before or after the source filename. For example, when compiling the source prog.c, use the following command to generate an object file with debug information:

iccpic18 prog --debug

Some options accept a filename, included after the option letter with a separating space. For example, to generate a listing to the file list.lst:

iccpic18 prog -l list.lst

Some other options accept a string that is not a filename. The string is included after the option letter, but without a space. For example, to define a symbol:

iccpic18 prog -DDEBUG=1

Generally, the order of options on the command line, both relative to each other and to the source filename, is not significant. There is, however, one exception: when you use the -I option, the directories are searched in the same order as they are specified on the command line.

Note that a command line option has a short name and/or a long name:

● A short option name consists of one character, with or without parameters. You specify it with a single dash, for example -e

● A long name consists of one or several words joined by underscores, and it may have parameters. You specify it with double dashes, for example --char_is_signed.

CPIC18-3

Part 2. Compiler reference 147

Page 168: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

148

Setting command line options

SPECIFYING PARAMETERS

When a parameter is needed for an option with a short name, it can be specified either immediately following the option or as the next command line argument.

For instance, an include file path of \usr\include can be specified either as:

-I\usr\include

or as:

-I \usr\include

Note: / can be used instead of \ as the directory delimiter.

Additionally, output file options can take a parameter that is a directory name. The output file will then receive a default name and extension.

When a parameter is needed for an option with a long name, it can be specified either immediately after the equal sign (=) or as the next command line argument, for example:

--diag_suppress=Pe0001

or

--diag_suppress Pe0001

The option --preprocess, however, is an exception, as the filename must be preceded by a space. In the following example, comments are included in the preprocessor output:

--preprocess=c prog

Options that accept multiple values may be repeated, and may also have comma-separated values (without a space), for example:

--diag_warning=Be0001,Be0002

The current directory is specified with a period (.), for example:

iccpic18 prog -l .

A file parameter specified by '-' represents standard input or output, whichever is appropriate.

Note: When an option takes a parameter, the parameter cannot start with a dash (-) followed by another character. Instead, you can prefix the parameter with two dashes; the following example will create a list file called -r:

iccpic18 prog -l ---r

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 169: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

SPECIFYING ENVIRONMENT VARIABLES

Compiler options can also be specified in the QCCPIC18 environment variable. The compiler automatically appends the value of this variable to every command line, so it provides a convenient method of specifying options that are required for every compilation.

The following environment variables can be used with the PIC18 IAR C/C++ Compiler:

ERROR RETURN CODES

The PIC18 IAR C/C++ Compiler returns status information to the operating system which can be tested in a batch file.

The following command line error codes are supported:

Options summaryThe following table summarizes the compiler command line options:

Environment variable Description

C_INCLUDE Specifies directories to search for include files; for example: C_INCLUDE=c:\program files\iar systems\

embedded workbench 4.n\pic18\inc;c:\headers

QCCPIC18 Specifies command line options; for example:QCCPIC18=-lA asm.lst -z9

Table 30: Environment variables

Code Description

0 Compilation successful, but there may have been warnings.

1 There were warnings, provided that the option --warnings_affect_exit_code was used.

2 There were non-fatal errors or fatal compilation errors making the compiler abort.

3 There were fatal errors.

Table 31: Error return codes

Command line option Description

--char_is_signed Treats char as signed

--code_model {o|s} Specifies the code model

-Dsymbol[=value] Defines preprocessor symbols

--data_model {s|l} Specifies the data model

Table 32: Compiler options summary

CPIC18-3

Part 2. Compiler reference 149

Page 170: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

150

Options summary

--debug Generates debug information

--dependencies[=[i][m]] {filename|directory} Lists file dependencies

--diag_error=tag,tag,... Treats these as errors

--diag_remark=tag,tag,... Treats these as remarks

--diag_suppress=tag,tag,... Suppresses these diagnostics

--diag_warning=tag,tag,... Treats these as warnings

--diagnostics_tables {filename|directory} Lists all diagnostic messages

--disable_fast_return Disables fast returns for high priority interrupts

--disable_lfsr Disables generation of LFSR instructions

-dlib_config filename Determines the library configuration file

-e Enables language extensions

--ec++ Enables Embedded C++ syntax

--eec++ Enables Extended Embedded C++ syntax

--eeprom_size=N Specifies the available EEPROM size

--enable_multibytes Enables support for multibyte characters

--error_limit=n Specifies the allowed number of errors before compilation stops

--extmem Enables access to external RAM memory

-f filename Extends the command line

--header_context Lists all referred source files

-Ipath Specifies include file path

-l[a|A|b|B|c|C|D][N][H] {filename|directory} Creates a list file

--library Disables certain memory range checks

--library_module Creates a library module

Command line option Description

Table 32: Compiler options summary (Continued)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 171: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

--migration_preprocessor_extensions Extends the preprocessor

--misrac Enables MISRA C-specific error messages

--misrac_verbose Enables verbose logging of MISRA C checking

--module_name=name Sets object module name

--no_code_motion Disables code motion optimization

--no_cse Disables common subexpression elimination

--no_inline Disables function inlining

--no_tbaa Disables type-based alias analysis

--no_typedefs_in_diagnostics Disables the use of typedef names in diagnostics

--no_unroll Disables loop unrolling

--no_warnings Disables all warnings

--no_wrap_diagnostics Disables wrapping of diagnostic messages

-o {filename|directory} Sets object filename

--omit_types Excludes type information

--only_stdout Uses standard output only

--preinclude includefile Includes an include file before reading the source file

--preprocess[=[c][n][l]] {filename|directory}Generates preprocessor output

--public_equ symbol[=value] Defines a global named assembler label

-r Generates debug information

--remarks Enables remarks

--require_prototypes Verifies that prototypes are proper

-s[2|3|6|9] Optimizes for speed

--silent Sets silent operation

Command line option Description

Table 32: Compiler options summary (Continued)

CPIC18-3

Part 2. Compiler reference 151

Page 172: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

152

Descriptions of options

Descriptions of optionsThe following section gives detailed reference information about each compiler option.

--char_is_signed --char_is_signed

By default, the compiler interprets the char type as unsigned. The --char_is_signed option causes the compiler to interpret the char type as signed instead. This can be useful when you, for example, want to maintain compatibility with another compiler.

Note: The runtime library is compiled without the --char_is_signed option. If you use this option, you may get type mismatch warnings from the IAR XLINK Linker, because the library uses unsigned chars.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Language.

--code_model --code_model={o|s}

The PIC18 microcontroller can store auto variables and parameters in one of two places: in static overlay or on the stack. The PIC18 IAR C/C++ Compiler supports these methods by means of code models.

--strict_ansi Checks for strict compliance with ISO/ANSI C

--warnings_affect_exit_code Warnings affects exit code

--warnings_are_errors Warnings are treated as errors

--workaround_pic18fx5x Creates workarounds for some hardware problems

-z[2|3|6|9] Optimizes for size

Command line option Description

Table 32: Compiler options summary (Continued)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 173: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

Use this option to select the code model. The following code models are available:

If you do not include any of the code model options, the compiler uses the overlay code model as default.

Note that all modules of your application must use the same code model.

Example

For example, use the following command to specify the stack code model:

--code_model=s

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>General Options>Target.

-D -Dsymbol[=value]

-D symbol[=value]

Use this option to define a preprocessor symbol with the name symbol and the value value. If no value is specified, 1 is used.

The option -D has the same effect as a #define statement at the top of the source file:

-Dsymbol

is equivalent to:

#define symbol 1

In order to get the equivalence of:

#define FOO

Code modelDefault function type

attributeDescription

o (default) __bank_func Functions use static overlay memory for parameters and auto variables. These must fit within a RAM bank, and thus cannot be larger than 256 bytes.

s __stack_func Functions use a simulated stack in the FSR2 register. This may result in slower execution, but function stack frames may exceed 256 bytes. Static overlay can still be used for compiler support routines.

Table 33: Available code models

CPIC18-3

Part 2. Compiler reference 153

Page 174: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

154

Descriptions of options

specify the = sign but nothing after, for example:

-DFOO=

This option can be used one or more times on the command line.

Example

You may want to arrange your source to produce either the test or production version of your program, depending on whether the symbol TESTVER was defined. To do this, you would use include sections such as:

#ifdef TESTVER... additional code lines for test version only

#endif

Then, you would select the version required on the command line as follows:

Production version: iccpic18 prog

Test version: iccpic18 prog -DTESTVER

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Preprocessor.

--data_model --data_model {s|l}

Use this option to select the data model for which the code is to be generated:

If you do not include any of the data model options, the compiler uses the small data model as default.

Note that all modules of your application must use the same data model.

Example

For example, use the following command to specify the large data model:

--data_model=l

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>General Options>Target.

Data model Default pointer size

s (default) 2 bytes

l 3 bytes

Table 34: Available data models

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 175: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

--debug, -r --debug

-r

Use the --debug or -r option to make the compiler include information required by the IAR C-SPY™ Debugger and other symbolic debuggers in the object modules.

Note: Including debug information will make the object files larger than otherwise.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Output.

--dependencies --dependencies=[i][m] {filename|directory}

Use this option to make the compiler write information to a file about each source code file opened by the compiler. The following modifiers are available:

If a filename is specified, the compiler stores the output in that file.

If a directory is specified, the compiler stores the output in that directory, in a file with the extension i. The filename will be the same as the name of the compiled source file, unless a different name has been specified with the option -o, in which case that name will be used.

To specify the working directory, replace directory with a period (.).

If --dependencies or --dependencies=i is used, the name of each opened source file, including the full path, if available, is output on a separate line. For example:

c:\iar\product\include\stdio.h d:\myproject\include\foo.h

If --dependencies=m is used, the output uses makefile style. For each source file, one line containing a makefile dependency rule is produced. Each line consists of the name of the object file, a colon, a space, and the name of a source file. For example:

foo.r49: c:\iar\product\include\stdio.h foo.r49: d:\myproject\include\foo.h

Example 1

To generate a listing of file dependencies to the file listing.i, use:

iccpic18 prog --dependencies=i listing

Option modifier Description

i Lists only the names of files (default)

m Lists in makefile style

Table 35: Generating a list of dependencies (--dependencies)

CPIC18-3

Part 2. Compiler reference 155

Page 176: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

156

Descriptions of options

Example 2

To generate a listing of file dependencies to a file called listing.i in the mypath directory, you would use:

iccpic18 prog --dependencies mypath\listing

Note: Both \ and / can be used as directory delimiters.

Example 3

An example of using --dependencies with a popular make utility, such as gmake (GNU make):

1 Set up the rule for compiling files to be something like:

%.r49 : %.c $(ICC) $(ICCFLAGS) $< --dependencies=m $*.d

That is, besides producing an object file, the command also produces a dependency file in makefile style (in this example, using the extension .d).

2 Include all the dependency files in the makefile using, for example:

-include $(sources:.c=.d)

Because of the dash (-) it works the first time, when the .d files do not yet exist.

--diag_error --diag_error=tag,tag,...

Use this option to classify diagnostic messages as errors. An error indicates a violation of the C or C++ language rules, of such severity that object code will not be generated, and the exit code will be non-zero.

Example

The following example classifies warning Pe117 as an error:

--diag_error=Pe117

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Diagnostics.

--diag_remark --diag_remark=tag,tag,...

Use this option to classify diagnostic messages as remarks. A remark is the least severe type of diagnostic message and indicates a source code construct that may cause strange behavior in the generated code.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 177: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

Example

The following example classifies the warning Pe177 as a remark:

--diag_remark=Pe177

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Diagnostics.

--diag_suppress --diag_suppress=tag,tag,...

Use this option to suppress diagnostic messages.

Example

The following example suppresses the warnings Pe117 and Pe177:

--diag_suppress=Pe117,Pe177

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Diagnostics.

--diag_warning --diag_warning=tag,tag,...

Use this option to classify diagnostic messages as warnings. A warning indicates an error or omission that is of concern, but which will not cause the compiler to stop before compilation is completed.

Example

The following example classifies the remark Pe826 as a warning:

--diag_warning=Pe826

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Diagnostics.

--diagnostics_tables --diagnostics_tables {filename|directory}

Use this option to list all possible diagnostic messages in a named file. This can be very convenient, for example, if you have used a pragma directive to suppress or change the severity level of any diagnostic messages, but forgot to document why.

This option cannot be given together with other options.

If a filename is specified, the compiler stores the output in that file.

CPIC18-3

Part 2. Compiler reference 157

Page 178: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

158

Descriptions of options

If a directory is specified, the compiler stores the output in that directory, in a file with the name diagnostics_tables.txt. To specify the working directory, replace directory with a period (.).

Example 1

To output a list of all possible diagnostic messages to the file diag.txt, use:

--diagnostics_tables diag

Example 2

If you want to generate a table to a file diagnostics_tables.txt in the working directory, you could use:

--diagnostics_tables .

Both \ and / can be used as directory delimiters.

--disable_fast_return --disable_fast_return

Fast returns from high priority interrupts are generally desirable, but not from assembler code. Use this option to stop the compiler from generating any fast returns from high priority interrupts.

To set the related option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Optimizations.

--disable_lfsr --disable_lfsr

Use this option to stop the compiler from generating any LFSR instructions.

In some early PIC18 derivatives the LFSR instruction would cause unexpected behavior (corrupting RAM locations). Affected derivatives include 18C242, 18C252, 18C442, and 18C452 (Rev B). See Microchip’s documentation #DS80058A.

--dlib_config --dlib_config filename

Each runtime library has a corresponding library configuration file. Use the --dlib_config option to specify the library configuration file for the compiler. Make sure that you specify a configuration file that corresponds to the library you are using.

All prebuilt runtime libraries are delivered with corresponding configuration files. You can find the library object files and the library configuration files in the directory pic18\lib. For examples and a list of prebuilt runtime libraries, see Using a prebuilt library, page 48.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 179: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

If you build your own customized runtime library, you should also create a corresponding customized library configuration file, which must be specified to the compiler. For more information, see Building and using a customized library, page 54.

Note: This option only applies to the IAR DLIB runtime environment.

To set the related option in the IAR Embedded Workbench IDE, select Project>Options>General Options>Library Configuration.

-e -e

In the command line version of the PIC18 IAR C/C++ Compiler, language extensions are disabled by default. If you use language extensions such as PIC18-specific keywords and anonymous structs and unions in your source code, you must enable them by using this option.

Note: The -e option and the --strict_ansi option cannot be used at the same time.

For additional information, see Special support for embedded systems, page 8.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Language.

--ec++ --ec++

In the PIC18 IAR C/C++ Compiler, the default language is C. If you use Embedded C++, you must use this option to set the language the compiler uses to Embedded C++.

To set the equivalent option in the IAR Embedded Workbench, select Project>Options>C/C++ Compiler>Language.

--eec++ --eec++

In the PIC18 IAR C/C++ Compiler, the default language is C. If you take advantage of Extended Embedded C++ features like namespaces or the standard template library in your source code, you must use this option to set the language the compiler uses to Extended Embedded C++. See Extended Embedded C++, page 100.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Language.

CPIC18-3

Part 2. Compiler reference 159

Page 180: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

160

Descriptions of options

--eeprom_size --eeprom_size=N

Use this option to enable the extended keyword __eeprom by specifying the size of the EEPROM in bytes. The value N can be 0–65536.

Setting the size to more than 256 bytes will cause the compiler to use 2-byte __eeprom pointers whereas a size of 256 bytes or less will allow the more efficient 1-byte pointers.

Note: To use the __eeprom extended keyword, language extensions must be enabled. For additional information, see -e, page 159 and #pragma language, page 188. Also, this option is only useful for derivatives with EEPROM, for instance PIC18FXXX.

To set the related option in the IAR Embedded Workbench IDE, select Project>Options>General Options>Target.

--extmem --extmem

When this option is selected, you can use the __extmem extended keyword to access external RAM memory. See __extmem, page 180.

Use this option if your PIC18 derivative has external RAM memory. It can only be used together with the large data model.

To set the related option in the IAR Embedded Workbench IDE, select Project>Options>General Options>Target.

--enable_multibytes --enable_multibytes

By default, multibyte characters cannot be used in C or C++ source code. If you use this option, multibyte characters in the source code are interpreted according to the host computer’s default setting for multibyte support.

Multibyte characters are allowed in C and C++ style comments, in string literals, and in character constants. They are transferred untouched to the generated code.

To set the equivalent option in the IAR Embedded Workbench IDE, choose Project>Options>C/C++ Compiler>Language.

--error_limit --error_limit=n

Use the --error_limit option to specify the number of errors allowed before the compiler stops the compilation. By default, 100 errors are allowed. n must be a positive number; 0 indicates no limit.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 181: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

-f -f filename

Reads command line options from the named file, with the default extension xcl.

By default, the compiler accepts command parameters only from the command line itself and the QCCPIC18 environment variable. To make long command lines more manageable, and to avoid any operating system command line length limit, you can use the -f option to specify a command file, from which the compiler reads command line items as if they had been entered at the position of the option.

In the command file, you format the items exactly as if they were on the command line itself, except that you may use multiple lines, because the newline character acts just as a space or tab character.

Both C and C++ style comments are allowed in the file. Double quotes behave as in the Microsoft Windows command line environment.

Example

For example, you could replace the command line:

iccpic18 prog -r "-DUsername=John Smith" -DUserid=463760

with

iccpic18 prog -r -f userinfo

if the file userinfo.xcl contains:

"-DUsername=John Smith"-DUserid=463760

--header_context --header_context

Occasionally, to find the cause of a problem it is necessary to know which header file was included from which source line. Use this option to list, for each diagnostic message, not only the source position of the problem, but also the entire include stack at that point.

-I -Ipath

-I path

Use this option to specify the search path for #include files. This option may be used more than once on a single command line.

Following is the full description of the compiler’s #include file search procedure:

● If the name of the #include file is an absolute path, that file is opened.

CPIC18-3

Part 2. Compiler reference 161

Page 182: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

162

Descriptions of options

● If the compiler encounters the name of an #include file in angle brackets, such as:

#include <stdio.h>

it searches the following directories for the file to include:

1 The directories specified with the -I option, in the order that they were specified.

2 The directories specified using the C_INCLUDE environment variable, if any.

● If the compiler encounters the name of an #include file in double quotes, for example:

#include "vars.h"

it searches the directory of the source file in which the #include statement occurs, and then performs the same sequence as for angle-bracketed filenames.

If there are nested #include files, the compiler starts searching the directory of the file that was last included, iterating upwards for each included file, searching the source file directory last. For example:

src.c in directory dir\src#include "src.h"...

src.h in directory dir\include#include "config.h"...

When dir\exe is the current directory, use the following command for compilation:

iccpic18 ..\src\src.c -I..\include -I..\debugconfig

Then the following directories are searched in the order listed below for the file config.h, which in this example is located in the dir\debugconfig directory:

dir\include Current file.

dir\src File including current file.

dir\include As specified with the first -I option.

dir\debugconfig As specified with the second -I option.

Use angle brackets for standard header files, like stdio.h, and double quotes for files that are part of your application.

Note: Both \ and / can be used as directory delimiters.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Preprocessor.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 183: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

-l -l[a|A|b|B|c|C|D][N][H] {filename|directory}

By default, the compiler does not generate a listing. Use this option to generate a listing to a file.

The following modifiers are available:

* This makes the list file less useful as input to the assembler, but more useful for reading by a human.

If a filename is specified, the compiler stores the output in that file.

If a directory is specified, the compiler stores the output in that directory, in a file with the extension lst. The filename will be the same as the name of the compiled source file, unless a different name has been specified with the option -o, in which case that name will be used.

To specify the working directory, replace directory with a period (.).

Example 1

To generate a listing to the file list.lst, use:

iccpic18 prog -l list

Option modifier Description

a Assembler list file

A Assembler file with C or C++ source as comments

b Basic assembler list file. This file has the same contents as a list file produced with -la, except that none of the extra compiler generated information (runtime model attributes, call frame information, frame size information) is included *

B Basic assembler list file. This file has the same contents as a list file produced with -lA, except that none of the extra compiler generated information (runtime model attributes, call frame information, frame size information) is included *

c C or C++ list file

C (default) C or C++ list file with assembler source as comments

D C or C++ list file with assembler source as comments, but without instruction offsets and hexadecimal byte values

N No diagnostics in file

H Include source lines from header files in output. Without this option, only source lines from the primary source file are included

Table 36: Generating a compiler list file (-l)

CPIC18-3

Part 2. Compiler reference 163

Page 184: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

164

Descriptions of options

Example 2

If you compile the file mysource.c and want to generate a listing to a file mysource.lst in the working directory, you could use:

iccpic18 mysource -l .

Note: Both \ and / can be used as directory delimiters.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>List.

--library --library

Use this option to disable out of memory errors when building libraries.

--library_module --library_module

Use this option to make the compiler generate a library module rather than a program module. A program module is always included during linking. A library module will only be included if it is referenced in your program.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Output.

--misrac --misrac[={tag1,tag2-tag3,…|all|required}]

Use this option to enable the compiler to check for deviations from the rules described in the MISRA Guidelines for the Use of the C Language in Vehicle Based Software. By using one or more arguments with the option, you can restrict the checking to a specific subset of the MISRA C rules. The possible arguments are described in this table:

Command line option Description

--misrac Enables checking for all MISRA C rules

--misrac=n Enables checking for the MISRA C rule with number n

--misrac=m,n Enables checking for the MISRA C rules with numbers m and n

--misrac=k-n Enables checking for all MISRA C rules with numbers from k to n

--misrac=k,m,r-t Enables checking for MISRA C rules with numbers k, m, and from r to t

--misrac=all Enables checking for all MISRA C rules

Table 37: Enabling MISRA C rules (--misrac)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 185: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

If the compiler is unable to check for a rule, specifying the option for that rule has no effect. For instance, MISRA C rule 15 is a documentation issue, and the rule is not checked by the compiler. As a consequence, specifying --misrac=15 has no effect.

Note: MISRA C is not supported by all IAR Systems products. If MISRA C checking is not supported by the compiler, using this option will generate an error.

To set the equivalent option in the IAR Embedded Workbench, select Project>Options>General Options>MISRA C or Project>Options>C/C++ Compiler>MISRA C.

--misrac_verbose --misrac_verbose

Use this option to generate a MISRA C log during compilation and linking. This is a list of the rules that are enabled—but not necessarily checked—and a list of rules that are actually checked.

If this option is enabled, the compiler displays a text at sign-on that shows both enabled and checked MISRA C rules.

To set the equivalent option in the IAR Embedded Workbench, select Project>Options>General Options>MISRA C.

--module_name --module_name=name

Normally, the internal name of the object module is the name of the source file, without a directory name or extension. Use this option to specify an object module name.

To set the object module name explicitly, use the option --module_name=name, for example:

iccpic18 prog --module_name=main

This option is useful when several modules have the same filename, because the resulting duplicate module name would normally cause a linker error; for example, when the source file is a temporary file generated by a preprocessor.

--misrac=required Enables checking for all MISRA C rules categorized as required

Command line option Description

Table 37: Enabling MISRA C rules (--misrac) (Continued)

CPIC18-3

Part 2. Compiler reference 165

Page 186: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

166

Descriptions of options

Example

The following example—in which %1 is an operating system variable containing the name of the source file—will give duplicate name errors from the linker:

preproc %1.c temp.c ; preprocess source, ; generating temp.c

iccpic18 temp.c ; module name is ; always 'temp'

To avoid this, use --module_name=name to retain the original name:

preproc %1.c temp.c ; preprocess source, ; generating temp.c

iccpic18 temp.c --module_name=%1 ; use original source ; name as module name

Note: In this example, preproc is an external utility.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Output.

--no_code_motion --no_code_motion

Use this option to disable optimizations that move code. These optimizations, which are performed at optimization levels 6 and 9, normally reduce code size and execution time. However, the resulting code may be difficult to debug.

Note: This option has no effect at optimization levels below 6.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Optimizations.

--no_cse --no_cse

Use --no_cse to disable common subexpression elimination.

At optimization levels 6 and 9, the compiler avoids calculating the same expression more than once. This optimization normally reduces both code size and execution time. However, the resulting code may be difficult to debug.

Note: This option has no effect at optimization levels below 6.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Optimizations.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 187: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

--no_inline --no_inline

Use --no_inline to disable function inlining.

Function inlining means that a simple function, whose definition is known at compile time, is integrated into the body of its caller to eliminate the overhead of the call.

This optimization, which is performed at optimization level 9, normally reduces execution time and increases code size. The resulting code may also be difficult to debug.

The compiler heuristically decides which functions to inline. Different heuristics are used when optimizing for speed.

Note: This option has no effect at optimization levels below 9.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Optimizations.

--no_tbaa --no_tbaa

Use --no_tbaa to disable type-based alias analysis. When this options is not used, the compiler is free to assume that objects are only accessed through the declared type or through unsigned char. See Type-based alias analysis, page 112 for more information.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Optimizations.

--no_typedefs_in_diagnostics --no_typedefs_in_diagnostics

Normally, when a type is mentioned in a message from the compiler, most commonly in a diagnostic message of some kind, the typedef names that were used in the original declaration are used whenever they make the resulting text shorter. For example,

typedef int (*MyPtr)(char const *);MyPtr p = "foo";

will give an error message like the following:

Error[Pe144]: a value of type "char *" cannot be used to initialize an entity of type "MyPtr"

If the --no_typedefs_in_diagnostics option is specified, the error message will be like this:

Error[Pe144]: a value of type "char *" cannot be used to initialize an entity of type "int (*)(char const *)"

CPIC18-3

Part 2. Compiler reference 167

Page 188: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

168

Descriptions of options

--no_unroll --no_unroll

Use this option to disable loop unrolling.

The code body of a small loop, whose number of iterations can be determined at compile time, is duplicated to reduce the loop overhead.

For small loops, the overhead required to perform the looping can be large compared with the work performed in the loop body.

The loop unrolling optimization duplicates the body several times, reducing the loop overhead. The unrolled body also opens up for other optimization opportunities, for example the instruction scheduler.

This optimization, which is performed at optimization level 9, normally reduces execution time, but increases code size. The resulting code may also be difficult to debug.

The compiler heuristically decides which loops to unroll. Different heuristics are used when optimizing for speed and size.

Note: This option has no effect at optimization levels below 9.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Optimizations.

--no_warnings --no_warnings

By default, the compiler issues warning messages. Use this option to disable all warning messages. To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Diagnostics.

--no_wrap_diagnostics --no_wrap_diagnostics

By default, long lines in compiler diagnostic messages are broken into several lines to make the message easier to read. Use this option to disable line wrapping of diagnostic messages.

-o -o {filename|directory}

Use the -o option to specify an output file for object code.

If a filename is specified, the compiler stores the object code in that file.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 189: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

If a directory is specified, the compiler stores the object code in that directory, in a file with the same name as the name of the compiled source file, but with the extension r49. To specify the working directory, replace directory with a period (.).

Example 1

To store the compiler output in a file called obj.r49 in the mypath directory, you would use:

iccpic18 mysource -o mypath\obj

Example 2

If you compile the file mysource.c and want to store the compiler output in a file mysource.r49 in the working directory, you could use:

iccpic18 mysource -o .

Note: Both \ and / can be used as directory delimiters.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>General Options>Output.

--omit_types --omit_types

By default, the compiler includes type information about variables and functions in the object output.

Use this option if you do not want the compiler to include this type information in the output. The object file will then only contain type information that is a part of a symbol’s name. This means that the linker cannot check symbol references for type correctness, which is useful when you build a library that should not contain type information.

--only_stdout --only_stdout

Use this option to make the compiler use the standard output stream (stdout) also for messages that are normally directed to the error output stream (stderr).

--preinclude --preinclude includefile

Use this option to make the compiler include the specified include file before it starts to read the source file. This is useful if you want to change something in the source code for the entire application, for instance if you want to define a new symbol.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Preprocessor.

CPIC18-3

Part 2. Compiler reference 169

Page 190: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

170

Descriptions of options

--preprocess --preprocess[=[c][n][l]] {filename|directory}

Use this option to direct preprocessor output to a named file.

The following table shows the mapping of the available preprocessor modifiers:

If a filename is specified, the compiler stores the output in that file.

If a directory is specified, the compiler stores the output in that directory, in a file with the extension i. The filename will be the same as the name of the compiled source file, unless a different name has been specified with the option -o, in which case that name will be used.

To specify the working directory, replace directory with a period (.).

Example 1

To store the compiler output with preserved comments to the file output.i, use:

iccpic18 prog --preprocess=c output

Example 2

If you compile the file mysource.c and want to store the compiler output with #line directives to a file mysource.i in the working directory, you could use:

iccpic18 mysource --preprocess=l .

Note: Both \ and / can be used as directory delimiters.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Preprocessor.

--public_equ --public_equ symbol[=value]

This option is equivalent to defining a label in assembler language by using the EQU directive and exporting it using the PUBLIC directive.

Command line option Description

--preprocess=c Preserve comments

--preprocess=n Preprocess only

--preprocess=l Generate #line directives

Table 38: Directing preprocessor output to file (--preprocess)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 191: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

-r, --debug -r

--debug

Use the -r or the --debug option to make the compiler include information required by the IAR C-SPY Debugger and other symbolic debuggers in the object modules.

Note: Including debug information will make the object files larger than otherwise.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Output.

--remarks --remarks

The least severe diagnostic messages are called remarks (see Severity levels, page 239). A remark indicates a source code construct that may cause strange behavior in the generated code.

By default, the compiler does not generate remarks. Use this option to make the compiler generate remarks.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Diagnostics.

--require_prototypes --require_prototypes

This option forces the compiler to verify that all functions have proper prototypes. Using this option means that code containing any of the following will generate an error:

● A function call of a function with no declaration, or with a Kernighan & Ritchie C declaration

● A function definition of a public function with no previous prototype declaration● An indirect function call through a function pointer with a type that does not include

a prototype.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Language.

-s -s[2|3|6|9]

Use this option to make the compiler optimize the code for maximum execution speed.

If no optimization option is specified, the compiler will use the size optimization -z2 by default. If the -s option is used without specifying the optimization level, speed optimization at level 2 is used by default.

CPIC18-3

Part 2. Compiler reference 171

Page 192: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

172

Descriptions of options

The following table shows how the optimization levels are mapped:

*The most important difference between -s2 and -s3 is that at level 2, all non-static variables will live during their entire scope.

A low level of optimization makes it relatively easy to follow the program flow in the debugger, and, conversely, a high level of optimization makes it relatively hard.

Note: The -s and -z options cannot be used at the same time.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Optimizations.

--silent --silent

By default, the compiler issues introductory messages and a final statistics report. Use --silent to make the compiler operate without sending these messages to the standard output stream (normally the screen).

This option does not affect the display of error and warning messages.

--strict_ansi --strict_ansi

By default, the compiler accepts a relaxed superset of ISO/ANSI C (see the chapter IAR language extensions). Use --strict_ansi to ensure that the program conforms to the ISO/ANSI C standard.

Note: The -e option and the --strict_ansi option cannot be used at the same time.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Language.

--warnings_affect_exit_code --warnings_affect_exit_code

By default, the exit code is not affected by warnings, as only errors produce a non-zero exit code. With this option, warnings will generate a non-zero exit code.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Diagnostics.

Option modifier Optimization level

2 None* (Best debug support)

3 Low*

6 Medium

9 High (Maximum optimization)

Table 39: Specifying speed optimization (-s)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 193: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Compiler options

--warnings_are_errors --warnings_are_errors

Use this option to make the compiler treat all warnings as errors. If the compiler encounters an error, no object code is generated. Warnings that have been changed into remarks are not treated as errors.

Note: Any diagnostic messages that have been reclassified as warnings by the compiler option --diag_warning or the #pragma diag_warning directive will also be treated as errors when --warnings_are_errors is used.

For additional information, see --diag_warning, page 157 and #pragma diag_warning, page 187.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Diagnostics.

--workaround_pic18fx5x --workaround_pic18fx5x

Use this option to work around some hardware problems with the PIC18Fx52 and PIC18Fx58 devices.

-z -z[2|3|6|9]

Use this option to make the compiler optimize the code for minimum size. If no optimization option is specified, -z2 is used by default.

The following table shows how the optimization levels are mapped:

*The most important difference between -z2 and -z3 is that at level 2, all non-static variables will live during their entire scope.

A low level of optimization makes it relatively easy to follow the program flow in the debugger, and, conversely, a high level of optimization makes it relatively hard.

Note: The -s and -z options cannot be used at the same time.

To set the equivalent option in the IAR Embedded Workbench IDE, select Project>Options>C/C++ Compiler>Optimizations.

Option modifier Optimization level

2 None* (Best debug support)

3 Low*

6 Medium

9 High (Maximum optimization)

Table 40: Specifying size optimization (-z)

CPIC18-3

Part 2. Compiler reference 173

Page 194: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

174

Descriptions of options

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 195: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Extended keywordsThis chapter describes the extended keywords that support specific features of the PIC18 microcontroller, the general syntax rules for the keywords, and a detailed description of each keyword.

For information about the address ranges of the different memory areas, see the chapter Segment reference.

Using extended keywordsThis section covers how extended keywords can be used when declaring and defining data and functions. The syntax rules for extended keywords are also described.

In addition to the rules presented here—to place the keyword directly in the code—the directives #pragma type_attribute and #pragma object_attribute can be used for specifying the keywords. Refer to the chapter Pragma directives for details about how to use the extended keywords together with pragma directives.

The keywords and the @ operator are only available when language extensions are enabled in the PIC18 IAR C/C++ Compiler.

In the IAR Embedded Workbench IDE, language extensions are enabled by default.

Use the -e compiler option to enable language extensions. See -e, page 159 for additional information.

EXTENDED KEYWORDS FOR DATA

The extended keywords that can be used for data can be divided into two groups that control the following:

● The memory type of objects and pointers: __bank, __bankN, __constptr, __dptr, __eeprom, __extmem, and __nonbanked

● Other characteristics of objects: __root and __no_init.

See the chapter Data storage in Part 1. Using the compiler for more information about how to use data memory types.

CPIC18-3

Part 2. Compiler reference 175

Page 196: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

176

Summary of extended keywords

EXTENDED KEYWORDS FOR FUNCTIONS

The extended keywords that can be used when functions are declared can be divided into two groups:

● Keywords that control the type of the functions. Keywords of this group must be specified both when the function is declared and when it is defined: __bank_func, __bankN_func, __interrupt, __monitor, and __stack_func.

● Keywords that control only the defined function: __noreturn, __root.

See the chapter Functions in Part 1. Using the compiler for more information about how to use function memory types.

Summary of extended keywordsSome extended keywords are used on data, some on functions, and some can be used on both data and functions.

The following table summarizes the extended keywords that can be used on functions:

Extended keywords for

functionsDescription

@ Controls the storage of variables and functions

asm, __asm Inserts an assembler instruction

__bank_func Causes a function to use a static overlay scheme for parameters and auto variables

__bankN_func Causes a function to use a static overlay scheme for parameters and auto variables and specifies which bank to use

__interrupt Supports interrupt functions

__intrinsic Reserved for compiler internal use only

__monitor Supports atomic execution of a function

__noreturn Informs the compiler that the declared function will not return

__root Ensures that a function or variable is included in the object code even if unused

__stack_func Causes a function to use a pseudo stack for parameters and auto variables

Table 41: Summary of extended keywords for functions

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 197: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Extended keywords

The following table summarizes the extended keywords that can be used on data:

Note: Some of the keywords can be used on both data and functions.

Descriptions of extended keywordsThe following sections give detailed information about each extended keyword.

@ The @ operator can be used for placing global and static variables at absolute addresses. The syntax can also be used for placing variables and functions in named segments.

For more information about the @ operator, see Efficient usage of segments and memory, page 42.

asm, __asm The asm and __asm extended keywords both insert an assembler instruction. However, when compiling C source code, the asm keyword is not available when the option --strict_ansi is used. The __asm keyword is always available.

Note: Not all assembler directives or operators can be inserted using this keyword.

Syntax

asm ("string");

Extended keywords for

dataDescription

@ Controls the storage of variables and functions

__bank Controls the storage of variables

__bankN Controls the storage of variables

__constptr const pointer attribute

__dptr Default pointer attribute

__eeprom Controls the storage of variables

__extmem Controls the storage of variables

__no_init Supports non-volatile memory

__nonbanked Controls the storage of variables

__root Ensures that a function or variable is included in the object code even if unused

Table 42: Summary of extended keywords for data

CPIC18-3

Part 2. Compiler reference 177

Page 198: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

178

Descriptions of extended keywords

The string can be a valid assembler instruction or an assembler directive, but not a comment. You can write several consecutive inline assembler instructions, for example:

asm ("Label: nop\n" " bra Label");

where \n (new line) separates each new assembler instruction. Note that you can define and use local labels in inline assembler instructions.

For more information about inline assembler, see Mixing C and assembler, page 85.

__bank The __bank extended keyword places variables in banks 0–15.

Max. object size

255 bytes

Pointer size

2 bytes

Address range

0x0–0x0FFF

__bank_func The __bank_func extended keyword causes the function to use a static overlay scheme for parameters and auto variables. These are placed in the OVERLAY segment; see OVERLAY, page 144 for additional information.

__bankN The __bankN extended keyword places variables in banks 0–15.

Data objects declared __bankN should be linked at an address such that the object does not cross a 256-byte boundary.

Max. object size

255 bytes

Pointer size

1 byte

Address range

0x0–0xFF

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 199: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Extended keywords

__bankN_func The __bankN_func extended keyword causes the function to use a static overlay scheme for parameters and auto variables. These are placed in the segments OVERLAY0–OVERLAY15, see OVERLAY0–OVERLAY15, page 145 for additional information.

__constptr The __constptr extended keyword is the pointer to const-declared objects. The syntax requires that const must be used also in the declaration, for example:

__constptr const int i=0x1234;int const __constptr * ip=&i;

Pointer size

2 or 3 bytes, depending on the data model being used

Address range

0x0–0x1FFFFF

__dptr The __dptr extended keyword is the default pointer attribute. It can point to memory declared __bank or __eeprom.

Pointer size

2 or 3 bytes, depending on the data model being used

__eeprom The __eeprom extended keyword allows you to place variables in the built-in EEPROM of the PIC18 microcontroller. Data objects declared __eeprom should be linked so that the object does not cross a 256-byte boundary.

Note: __eeprom variables must be declared __no_init.

Maximum object size

256 bytes

Pointer size

1 or 2 bytes, depending on the size of the EEPROM memory. See --eeprom_size, page 160.

Note: This access method disables interrupts when reading and writing, to make sure that EEPROM timing constraints are observed.

CPIC18-3

Part 2. Compiler reference 179

Page 200: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

180

Descriptions of extended keywords

__extmem The __extmem extended keyword places data objects in one of the EXTMEM segments. Use the keyword for variables that are to be located to the RAM areas of the external memory bus. Do not use it for manipulating internal program memory.

This keyword cannot be used in the small data model.

Pointer size

3 bytes

Address range

0x0–0x1FFFFF

__interrupt The __interrupt keyword specifies interrupt functions. The #pragma vector directive can be used for specifying the interrupt vector(s), see #pragma vector, page 192. An interrupt function must have a void return type and cannot have any parameters.

The following example declares an interrupt function with an interrupt vector with offset 0x8 in the INTVEC segment:

#pragma vector=0x8__interrupt void my_interrupt_handler(void);

An interrupt function cannot be called directly from a C program. It can only be executed as a response to an interrupt request.

It is possible to define an interrupt function without a vector, but then the compiler will not generate an entry in the interrupt vector table. For additional information, see INTVEC, page 144.

The header file ioderivative.h, where derivative corresponds to the selected derivative, contains predefined names for the existing interrupt vectors. There are two available interrupt vectors: 0x08 for high-priority interrupts and 0x18 for low-priority interrupts.

For additional information, see Interrupt functions, page 25.

__intrinsic The __intrinsic keyword is reserved for compiler internal use only.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 201: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Extended keywords

__monitor The __monitor keyword causes interrupts to be disabled during execution of the function. This allows atomic operations to be performed, such as operations on semaphores that control access to resources by multiple processes. A function declared with the __monitor keyword is equivalent to any other function in all other respects. This keyword can be specified using the #pragma object_attribute directive.

Avoid using the __monitor keyword on large functions, since the interrupt will otherwise be turned off for too long.

For additional information, see the intrinsic functions __disable_interrupt, page 202, and __enable_interrupt, page 202.

Read more about monitor functions in Monitor functions, page 25.

__no_init The __no_init keyword is used for suppressing initialization of a variable at system startup.

The __no_init keyword is placed in front of the type. In this example, myarray is placed in a non-initialized segment:

__no_init int myarray[10];

The #pragma object_attribute directive can also be used. The following declaration is equivalent to the previous one:

#pragma object_attribute=__no_initint myarray[10];

Note: The __no_init keyword cannot be used in combination with the typedef keyword.

__nonbanked The __nonbanked extended keyword places variables and constants in bank 0 of the access bank area.

Max. object size

128 bytes, or 96 bytes on devices with the access bank border at address 0x60.

Pointer size

1 byte

Address range

0x0–0x7F

CPIC18-3

Part 2. Compiler reference 181

Page 202: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

182

Descriptions of extended keywords

__noreturn The __noreturn keyword can be used on a function to inform the compiler that the function will not return. If you use this keyword on such functions, the compiler can optimize more efficiently. Examples of functions that do not return are abort and exit.

The __noreturn keyword is an object attribute, which means the #pragma object_attribute directive can be used for specifying it. For more information about object attributes, see Object attributes, page 129.

__root The __root attribute can be used on a function or a variable to ensure that, when the module containing the function or variable is linked, the function or variable is also included, whether or not it is referenced by the rest of the program.

By default, only the part of the runtime library calling main and any interrupt vectors are root. All other functions and variables are included in the linked output only if they are referenced by the rest of the program.

The __root keyword is placed in front of the type, for example to place myarray in non-volatile memory:

__root int myarray[10];

The #pragma object_attribute directive can also be used. The following declaration is equivalent to the previous one:

#pragma object_attribute=__rootint myarray[10];

Note: The __root keyword cannot be used in combination with the typedef keyword.

__stack_func The __stack_func keyword causes the function to use a pseudo stack implemented using the FSR2/INDF2 register set for parameters and auto variables. It should only be used on recursive or reentrant functions since it is slower than static overlay, especially if the frame size exceeds 128 bytes.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 203: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Pragma directivesThis chapter describes the pragma directives of the PIC18 IAR C/C++ Compiler.

The pragma directives control the behavior of the compiler, for example how it allocates memory, whether it allows extended keywords, and whether it outputs warning messages. The pragma directives are preprocessed, which means that macros are substituted in a pragma directive.

The pragma directives are always enabled in the compiler. They are consistent with ISO/ANSI C and are very useful when you want to make sure that the source code is portable.

Summary of pragma directivesThe following table shows the pragma directives of the compiler:

Pragma directive Description

#pragma

basic_template_matching

Makes a template function fully memory-aware

#pragma bitfields Controls the order of bitfield members

#pragma constseg Places constant variables in a named segment

#pragma data_alignment Gives a variable a higher (more strict) alignment

#pragma dataseg Places variables in a named segment

#pragma diag_default Changes the severity level of diagnostic messages

#pragma diag_error Changes the severity level of diagnostic messages

#pragma diag_remark Changes the severity level of diagnostic messages

#pragma diag_suppress Suppresses diagnostic messages

#pragma diag_warning Changes the severity level of diagnostic messages

#pragma include_alias Specifies an alias for an include file

#pragma inline Inlines a function

#pragma language Controls the IAR language extensions

#pragma location Specifies the absolute address of a variable

Table 43: Pragma directives summary

CPIC18-3

Part 2. Compiler reference 183

Page 204: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

184

Descriptions of pragma directives

Note: For portability reasons, some old-style pragma directives are recognized but will give a diagnostic message. It is important to be aware of this if you need to port existing code that contains any of those pragma directives.

Descriptions of pragma directivesThis section gives detailed information about each pragma directive.

All pragma directives using = for value assignment should be entered like:

#pragma pragmaname=pragmavalue

or

#pragma pragmaname = pragmavalue

#pragma basic_template_matching #pragma basic_template_matching

Use this pragma directive in front of a template function declaration to make the function fully memory-aware, in the rare cases where this is useful. That template function will then match the template without the modifications described in Templates and data memory attributes, page 104.

Example

#pragma basic_template_matchingtemplate<typename T> void fun(T *);

fun((int __bank *) 0); // T = int __bank

#pragma message Prints a message

#pragma object_attribute Changes the definition of a variable or a function

#pragma optimize Specifies type and level of optimization

#pragma required Ensures that a symbol which is needed by another symbol is present in the linked output

#pragma rtmodel Adds a runtime model attribute to the module

#pragma segment Declares a segment name to be used by intrinsic functions

#pragma type_attribute Changes the declaration and definitions of a variable or function

#pragma vector Specifies the vector of an interrupt function

Pragma directive Description

Table 43: Pragma directives summary (Continued)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 205: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Pragma directives

#pragma bitfields #pragma bitfields={reversed|default}

The #pragma bitfields directive controls the order of bitfield members.

By default, the PIC18 IAR C/C++ Compiler places bitfield members from the least significant bit to the most significant bit in the container type. Use the #pragma bitfields=reversed directive to place the bitfield members from the most significant to the least significant bit. This setting remains active until you turn it off again with the #pragma bitfields=default directive.

#pragma constseg The #pragma constseg directive places constant variables in a named segment. Use the following syntax:

#pragma constseg=MY_CONSTANTSconst int factorySettings[] = {42, 15, -128, 0};#pragma constseg=default

The segment name cannot be a predefined segment; see the chapter Segment reference for more information.

The memory in which the segment resides is optionally specified using the following syntax:

#pragma constseg=__bank MyOtherSeg

All constants defined following this directive will be placed in the segment MyOtherSeg and accessed using bank addressing.

#pragma data_alignment #pragma data_alignment=expression

Use this pragma directive to give a variable a higher (more strict) alignment than it would otherwise have. It can be used on variables with static and automatic storage duration.

The value of the constant expression must be a power of two (1, 2, 4, etc.).

When you use #pragma data_alignment on variables with automatic storage duration, there is an upper limit on the allowed alignment for each function, determined by the calling convention used.

#pragma dataseg The #pragma dataseg directive places variables in a named segment. Use the following syntax:

#pragma dataseg=MY_SEGMENT __no_init char myBuffer[1000];#pragma dataseg=default

CPIC18-3

Part 2. Compiler reference 185

Page 206: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

186

Descriptions of pragma directives

The segment name cannot be a predefined segment, see the chapter Segment reference for more information. The variable myBuffer will not be initialized at startup, and can for this reason not have any initializer.

The memory in which the segment resides is optionally specified using the following syntax:

#pragma dataseg=__bank0 MyOtherSeg

All variables in MyOtherSeg will be accessed using bank0 addressing.

Use the same declaration for several blocks of code.

#pragma diag_default #pragma diag_default=tag,tag,...

Changes the severity level back to default, or as defined on the command line for the diagnostic messages with the specified tags. For example:

#pragma diag_default=Pe117

See the chapter Diagnostics for more information about diagnostic messages.

#pragma diag_error #pragma diag_error=tag,tag,...

Changes the severity level to error for the specified diagnostics. For example:

#pragma diag_error=Pe117

See the chapter Diagnostics for more information about diagnostic messages.

#pragma diag_remark #pragma diag_remark=tag,tag,...

Changes the severity level to remark for the specified diagnostics. For example:

#pragma diag_remark=Pe177

See the chapter Diagnostics for more information about diagnostic messages.

#pragma diag_suppress #pragma diag_suppress=tag,tag,...

Suppresses the diagnostic messages with the specified tags. For example:

#pragma diag_suppress=Pe117,Pe177

See the chapter Diagnostics for more information about diagnostic messages.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 207: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Pragma directives

#pragma diag_warning #pragma diag_warning=tag,tag,...

Changes the severity level to warning for the specified diagnostics. For example:

#pragma diag_warning=Pe826

See the chapter Diagnostics for more information about diagnostic messages.

#pragma include_alias #pragma include_alias "orig_header" "subst_header"

#pragma include_alias <orig_header> <subst_header>

The #pragma include_alias directive makes it possible to provide an alias for a header file. This is useful for substituting one header file with another, and for specifying an absolute path to a relative file.

The parameter subst_header is used for specifying an alias for orig_header. This pragma directive must appear before the corresponding #include directives and subst_header must match its corresponding #include directive exactly.

Example

#pragma include_alias <stdio.h> <C:\MyHeaders\stdio.h>#include <stdio.h>

This example will substitute the relative file stdio.h with a counterpart located according to the specified path.

#pragma inline #pragma inline[=forced]

The #pragma inline directive advises the compiler that the function whose declaration follows immediately after the directive should be inlined—that is, expanded into the body of the calling function. Whether the inlining actually takes place is subject to the compiler’s heuristics.

This is similar to the C++ keyword inline, but has the advantage of being available in C code.

Specifying #pragma inline=forced disables the compiler’s heuristics and forces the inlining. If the inlining fails for some reason, for example if it cannot be used with the function type in question (like printf), an error message is emitted.

CPIC18-3

Part 2. Compiler reference 187

Page 208: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

188

Descriptions of pragma directives

#pragma language #pragma language={extended|default}

The #pragma language directive is used for turning on the IAR language extensions or for using the language settings specified on the command line:

#pragma location #pragma location=address

The #pragma location directive specifies the location—the absolute address—of the variable whose declaration follows the pragma directive. For example:

#pragma location=0x10char PIND; /* PIND is located at address 0x10 */

The directive can also take a string specifying the segment placement for either a variable or a function, for example:

#pragma location="foo"

For additional information and examples, see Located data, page 38.

#pragma message #pragma message(message)

Makes the compiler print a message on stdout when the file is compiled. For example:

#ifdef TESTING#pragma message("Testing")#endif

#pragma object_attribute #pragma object_attribute=keyword

The #pragma object_attribute directive affects the definition of the identifier that follows immediately after the directive. The object is modified, not its type.

The following keyword can be used with #pragma object_attribute for a variable:

extended Turns on the IAR language extensions and turns off the --strict_ansi command line option.

default Uses the settings specified on the command line.

__no_init Suppresses initialization of a variable at startup.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 209: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Pragma directives

The following keyword can be used with #pragma object_attribute for a function or variable:

Example

In the following example, the variable bar is placed in the non-initialized segment:

#pragma object_attribute=__no_init char bar;

Unlike the directive #pragma type_attribute that specifies the storing and accessing of a variable, it is not necessary to specify an object attribute in declarations. The following example declares bar without a #pragma object_attribute:

__no_init char bar;

#pragma optimize #pragma optimize=token_1 token_2 token_3

where token_n is one of the following:

The #pragma optimize directive is used for decreasing the optimization level, or for turning off some specific optimizations. This pragma directive only affects the function that follows immediately after the directive.

Note that it is not possible to optimize for speed and size at the same time. Only one of the s and z tokens can be used. It is also not possible to use macros embedded in this pragma directive. Any such macro will not be expanded by the preprocessor.

__root Ensures that a function or data object is included in the linked application, even if it is not referenced.

__noreturn Informs the compiler that the function will not return.

s Optimizes for speed

z Optimizes for size

2|none|3|low|6|medium|9|high Specifies the level of optimization

no_code_motion Turns off code motion

no_cse Turns off common subexpression elimination

no_inline Turns off function inlining

no_tbaa Turns off type-based alias analysis

no_unroll Turns off loop unrolling

CPIC18-3

Part 2. Compiler reference 189

Page 210: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

190

Descriptions of pragma directives

Note: If you use the #pragma optimize directive to specify an optimization level that is higher than the optimization level you specify using a compiler option, the pragma directive is ignored.

Example

#pragma optimize=s 9int small_and_used_often(){ ...}

#pragma optimize=z 9int big_and_seldom_used(){ ...}

#pragma required #pragma required=symbol

Use the #pragma required directive to ensure that a symbol which is needed by another symbol is present in the linked output. The symbol can be any statically linked function or variable, and the pragma directive must be placed immediately before a symbol definition.

Use the directive if the requirement for a symbol is not otherwise visible in the application, for example, if a variable is only referenced indirectly through the segment it resides in.

Example

void * const myvar_entry @ "MYSEG" = &myvar;...#pragma required=myvar_entrylong myvar;

#pragma rtmodel #pragma rtmodel="key","value"

Use the #pragma rtmodel directive to add a runtime model attribute to a module. Use a text string to specify key and value.

This pragma directive is useful to enforce consistency between modules. All modules that are linked together and define the same runtime attribute key must have the same value for the corresponding key value, or the special value *. Using the special value * is equivalent to not defining the attribute at all. It can, however, be useful to state explicitly that the module can handle any runtime model.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 211: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Pragma directives

A module can have several runtime model definitions.

Note: The predefined compiler runtime model attributes start with a double underscore. In order to avoid confusion, this style must not be used in the user-defined attributes.

Example

#pragma rtmodel="I2C","ENABLED"

The linker will generate an error if a module that contains this definition is linked with a module that does not have the corresponding runtime model attributes defined.

For more information about runtime model attributes and module consistency, see Checking module consistency, page 68.

#pragma segment #pragma segment="segment" [memattr] [align]

The #pragma segment directive declares a segment name that can be used by the intrinsic functions __segment_begin and __segment_end. All segment declarations for a specific segment must have the same memory type attribute and alignment.

The optional memory attribute memattr will be used in the return type of the intrinsic function. The optional parameter align can be specified to align the segment part. The value must be a constant integer expression to the power of two.

Example

#pragma segment="MYSEG" __bank 1

See also __segment_begin, page 203.

For more information about segments and segment parts, see the chapter Placing code and data.

#pragma type_attribute #pragma type_attribute=keyword

The #pragma type_attribute directive can be used for specifying IAR-specific type attributes, which are not part of the ISO/ANSI C language standard. Note however, that a given type attribute may not be applicable to all kind of objects. For a list of all supported type attributes, see Type and object attributes, page 128.

The #pragma type_attribute directive affects the declaration of the identifier, the next variable, or the next function that follows immediately after the pragma directive.

CPIC18-3

Part 2. Compiler reference 191

Page 212: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

192

Descriptions of pragma directives

Example

In the following example, even though IAR-specific type attributes are used, the application can still be compiled by a different compiler. First, a typedef is declared; a char object with the memory attribute __bank is defined as MyCharInBank. Then a pointer is declared; the pointer is located in nonbanked memory and it points to a char object that is located in bank memory.

#pragma type_attribute=__banktypedef char MyCharInBank;#pragma type_attribute=__nonbankedMyCharInBank * ptr;

The following declarations, which use extended keywords, are equivalent. See the chapter Extended keywords for more details.

char __bank * __nonbanked ptr;

#pragma vector #pragma vector=vector1[, vector2, vector3, ...]

The #pragma vector directive specifies the vector(s) of an interrupt function whose declaration follows the pragma directive.

Example

#pragma vector=0x08__interrupt void my_handler(void);

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 213: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The preprocessorThis chapter gives a brief overview of the preprocessor, including reference information about the different preprocessor directives, symbols, and other related information.

Overview of the preprocessorThe preprocessor of the PIC18 IAR C/C++ Compiler adheres to the ISO/ANSI standard. The compiler also makes the following preprocessor-related features available to you:

● Predefined preprocessor symbols. These symbols allow you to inspect the compile-time environment, for example the time and date of compilation. Some of the symbols take arguments and perform more advanced operations than just inspecting the compile-time environment. For details, see Predefined symbols, page 193.

● User-defined preprocessor symbols. Use the option -D to define your own preprocessor symbols, see -D, page 153.

● Preprocessor extensions. There are several preprocessor extensions, for example many pragma directives; for more information, see the chapter Pragma directives in this guide. For information about other extensions, see Preprocessor extensions, page 199.

● Preprocessor output. Use the option --preprocess to direct preprocessor output to a named file, see --preprocess, page 170.

● Implementation-defined behavior. Some parts listed by the ISO/ANSI standard are implementation-defined, for example the character set used in the preprocessor directives and inclusion of bracketed and quoted filenames. To read more about this, see Preprocessing directives, page 220.

Predefined symbolsThis section first summarizes all predefined symbols and then provides detailed information about each symbol.

CPIC18-3

Part 2. Compiler reference 193

Page 214: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

194

Predefined symbols

SUMMARY OF PREDEFINED SYMBOLS

The following table summarizes the predefined symbols:

* This symbol is required by the ISO/ANSI standard.

Predefined symbol Identifies

__ALIGNOF__() Accesses the alignment of an object

__BASE_FILE__ Identifies the name of the file being compiled. If the file is a header file, the name of the file that includes the header file is identified.

__CODE_MODEL__ Identifies the code model in use

__cplusplus Determines whether the compiler runs in C++ mode*

__DATA_MODEL__ Determines the data model in use

__DATE__ Determines the date of compilation*

__embedded_cplusplus Determines whether the compiler runs in C++ mode*

__FILE__ Identifies the name of the file being compiled*

__func__ Expands into a string with the function name as context

__FUNCTION__ Expands into a string with the function name as context

__IAR_SYSTEMS_ICC__ Identifies the IAR compiler platform

__ICCPIC18__ Identifies the PIC18 IAR C/C++ Compiler

__LINE__ Determines the current source line number*

_Pragma() Can be used in preprocessor defines and has the equivalent effect as the pragma directive

__PRETTY_FUNCTION__ Expands into a string with the function name, including parameter types and return type, as context

__STDC__ Identifies ISO/ANSI Standard C*

__STDC_VERSION__ Identifies the version of ISO/ANSI Standard C in use*

__TID__ Identifies the target processor of the IAR compiler in use

__TIME__ Determines the time of compilation*

__VER__ Identifies the version number of the IAR compiler in use

Table 44: Predefined symbols summary

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 215: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The preprocessor

DESCRIPTIONS OF PREDEFINED SYMBOLS

The following section gives reference information about each predefined symbol.

__ALIGNOF__() The __ALIGNOF__ operator is used to access the alignment of an object. It takes one of two forms:

● __ALIGNOF__ (type)● __ALIGNOF__ (expression)

In the second form, the expression is not evaluated.

__BASE_FILE__ Use this symbol to identify which file is currently being compiled. This symbol expands to the name of that file, unless the file is a header file. In that case, the name of the file that includes the header file is identified.

See also, __FILE__, page 196.

__CODE_MODEL__ Use this symbol to identify the used code model.

The value of this symbol is __CODE_MODEL_OVERLAY__ or __CODE_MODEL_STACK__ for the normal and short code models, respectively.

Example

#if __CODE_MODEL__==__CODE_MODEL_STACK__int my_array[10];#elseint my_array[20];#endif

__cplusplus This predefined symbol expands to the number 199711L when the compiler runs in any of the C++ modes. When the compiler runs in ISO/ANSI C mode, the symbol is undefined.

This symbol can be used with #ifdef to detect whether the compiler accepts C++ code. It is particularly useful when creating header files that are to be shared by C and C++ code.

__DATA_MODEL__ Use this symbol to identify the used data model.

The value of this symbol is __DATA_MODEL_SMALL__ or __DATA_MODEL_LARGE__ for the small and large data models, respectively.

CPIC18-3

Part 2. Compiler reference 195

Page 216: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

196

Predefined symbols

Example

#if __DATA_MODEL__==__DATA_MODEL_LARGE__... code used in large data model only#endif

__DATE__ Use this symbol to identify when the file was compiled. This symbol expands to the date of compilation, which is returned in the form "Mmm dd yyyy", for example "Jan 30 2002".

__embedded_cplusplus This predefined symbol expands to the number 1 when the compiler runs in any of the C++ modes. When the compiler runs in ISO/ANSI C mode, the symbol is undefined.

__FILE__ Use this symbol to identify which file is currently being compiled. This symbol expands to the name of that file.

See also, __BASE_FILE__, page 195.

__func__, __FUNCTION__ Use one of these symbols inside a function body to make it expand into a string with the function name as context. This is useful for assertions and other trace utilities. These symbols require that language extensions are enabled, see -e, page 159.

See also, __PRETTY_FUNCTION__, page 197.

__IAR_SYSTEMS_ICC__ This predefined symbol expands to a number that identifies the IAR compiler platform. The current identifier is 6. Note that the number could be higher in a future version of the product.

This symbol can be tested with #ifdef to detect whether the code was compiled by a compiler from IAR Systems.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 217: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The preprocessor

__ICCPIC18__ This predefined symbol expands to the number 1 when the code is compiled with the PIC18 IAR C/C++ Compiler.

__LINE__ This predefined symbol expands to the current line number of the file currently being compiled.

_Pragma() The preprocessor operator _Pragma can be used in defines and has the equivalent effect of the #pragma directive. The syntax is:

_Pragma("string")

where string follows the syntax for the corresponding pragma directive. For example:

#if NO_OPTIMIZE #define NOOPT _Pragma("optimize=2")#else #define NOOPT#endif

See the chapter Pragma directives.

Note: The -e option—enable language extensions—is not required.

__PRETTY_FUNCTION__ Use this symbol inside a function body to make it expand into a string, with the function name including parameter types and return type as context. The result might, for example, look like this:

"void func(char)"

This symbol is useful for assertions and other trace utilities. These symbols require that language extensions are enabled, see -e, page 159.

See also, __func__, __FUNCTION__, page 196.

__STDC__ This predefined symbol expands to the number 1. This symbol can be tested with #ifdef to detect whether the compiler in use adheres to ISO/ANSI C.

__STDC_VERSION__ ISO/ANSI C and version identifier.

This predefined symbol expands to 199409L.

Note: This predefined symbol does not apply in EC++ mode.

CPIC18-3

Part 2. Compiler reference 197

Page 218: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

198

Predefined symbols

__TID__ Target identifier for the PIC18 IAR C/C++ Compiler.

Expands to the target identifier which contains the following parts:

● A target-identifier (t) unique for each IAR compiler. For the PIC18 microcontroller, the target identifier is 0x31

● A value (c) reserved for specifying different CPU cores. For the PIC18 microcontroller, the value is zero

● A value (m) reserved for data model. For the PIC18 microcontroller, the value is zero.

The __TID__value is constructed as:

((t << 8) | (c << 4) | m)

You can extract the values as follows:

To find the value of the target identifier for the current compiler, execute:

printf("%ld",(__TID__ >> 8) & 0x7F)

Note: The use of __TID__ is not recommended. We recommend that you use the symbol __ICCPIC18__.

__TIME__ Current time.

Expands to the time of compilation in the form hh:mm:ss.

__VER__ Compiler version number.

Expands to an integer representing the version number of the compiler. The value of the number is calculated in the following way:

(100 * the major version number + the minor version number)

Example

The example below prints a message for version 3.34.

#if __VER__ == 334#pragma message("Compiler version 3.34")#endif

In this example, 3 is the major version number and 34 is the minor version number.

t = (__TID__ >> 8) & 0x7F; /* target identifier */

c = (__TID__ >> 4) & 0x0F; /* cpu core */

m = __TID__ & 0x0F; /* data model */

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 219: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

The preprocessor

Preprocessor extensionsThe following section gives reference information about the extensions that are available in addition to the pragma directives and ISO/ANSI directives.

#warning message Use this preprocessor directive to produce messages. Typically this is useful for assertions and other trace utilities, similar to the way the ISO/ANSI standard #error directive is used. The syntax is:

#warning message

where message can be any string.

__VA_ARGS__ Variadic macros are the preprocessor macro equivalents of printf style functions.

Syntax

#define P(...) __VA_ARGS__#define P(x,y,...) x + y + __VA_ARGS__

Here, __VA_ARGS__ will contain all variadic arguments concatenated, including the separating commas.

Example

#if DEBUG #define DEBUG_TRACE(...) printf(S,__VA_ARGS__)#else #define DEBUG_TRACE(...)#endif...DEBUG_TRACE("The value is:%d\n",value);

will result in:

printf("The value is:%d\n",value);

CPIC18-3

Part 2. Compiler reference 199

Page 220: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

200

Preprocessor extensions

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 221: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Intrinsic functionsThis chapter gives reference information about the intrinsic functions.

The intrinsic functions provide direct access to low-level processor operations and can be very useful in, for example, time-critical routines. The intrinsic functions compile into inline code, either as a single instruction or as a short sequence of instructions.

Intrinsic functions summaryThe following table summarizes the intrinsic functions:

Intrinsic function Description

__clear_watchdog_timer Inserts a CLRWTD instruction

__disable_interrupt Disables global interrupts

__disable_high_priority_interruptDisables global interrupts

__disable_low_priority_interrupt Disables peripheral interrupts

__disable_peripheral_interrupt Disables peripheral interrupts

__enable_interrupt Enables global interrupts

__enable_high_priority_interrupt Enables global interrupts

__enable_low_priority_interrupt Enables peripheral interrupts

__enable_peripheral_interrupt Enables peripheral interrupts

__get_interrupt_state Retrieves the state of the interrupt flag

__no_operation Inserts a NOP instruction

__reset Inserts a RESET instruction

__segment_begin Returns the start address of a segment

__segment_end Returns the end address of a segment

__set_configuration_bits Sets the controller configuration bits

__set_interrupt_state Sets the interrupt flag

__sleep Inserts a SLEEP instruction

__write_table_char Writes a char to the CODE area

__write_table_dptr Writes a __dptr to the CODE area

__write_table_long Writes a long to the CODE area

Table 45: Intrinsic functions summary

CPIC18-3

Part 2. Compiler reference 201

Page 222: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

202

Descriptions of intrinsic functions

To use intrinsic functions in an application, include the header file Intrinsics.h.

Note that the intrinsic function names start with double underscores, for example:

__segment_begin

Descriptions of intrinsic functionsThe following section gives reference information about each intrinsic function.

__clear_watchdog_timer void __clear_watchdog_timer(void);

Inserts a CLRWTD instruction.

__disable_interrupt void __disable_interrupt(void);

Disables global interrupts.

__disable_high_priority_

interrupt

void __disable_high_priority_interrupt(void);

Alias for __disable_interrupt.

__disable_low_priority_

interrupt

void __disable_low_priority_interrupt(void);

Alias for __disable_peripheral_interrupt.

__disable_peripheral_interrupt void __disable_peripheral_interrupt(void);

Disables peripheral interrupts.

__enable_interrupt void __enable_interrupt(void);

Enables global interrupts.

__enable_high_priority_

interrupt

void __enable_high_priority_interrupt(void);

Alias for __enable_interrupt.

__write_table_short Writes a short to the CODE area

Intrinsic function Description

Table 45: Intrinsic functions summary (Continued)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 223: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Intrinsic functions

__enable_low_priority_

interrupt

void __enable_low_priority_interrupt(void);

Alias for __enable_peripheral_interrupt.

__enable_peripheral_interrupt void __enable_peripheral_interrupt(void);

Enables peripheral interrupts.

__get_interrupt_state __istate_t __get_interrupt_state(void);

This intrinsic function will save the state of the interrupt flag in the byte returned. This value can then be used for restoring the state of the interrupt flag with the __set_interrupt_state intrinsic function.

Example

__istate_t oldState;

oldState = __get_interrupt_state();__disable_interrupt();

/* Critical section goes here */

__set_interrupt_state(oldState);

__no_operation void __no_operation(void);

Inserts a NOP instruction.

__reset void __reset(void);

Inserts a RESET instruction.

__segment_begin void * __segment_begin(segment);

Returns the address of the first byte of the named segment. The named segment must be a string literal that has been declared earlier with the #pragma segment directive. See #pragma segment, page 191.

If the segment was declared with a memory attribute memattr, the type of the __segment_begin function is pointer to memattr void. Otherwise, the type is a default pointer to void.

CPIC18-3

Part 2. Compiler reference 203

Page 224: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

204

Descriptions of intrinsic functions

Example

#pragma segment="MYSEG" __bank...segment_start_address = __segment_begin("MYSEG");

Here, the type of the __segment_begin intrinsic function is void __bank *.

Note: You must have enabled language extensions to use this intrinsic function.

__segment_end void * __segment_end(segment);

Returns the address of the first byte after the named segment. The named segment must be a string literal that has been declared earlier with the #pragma segment directive. See #pragma segment, page 191.

If the segment was declared with a memory attribute memattr, the type of the __segment_end function is pointer to memattr void. Otherwise, the type is a default pointer to void.

Example

#pragma segment="MYSEG" __bank...segment_end_address = __segment_end("MYSEG");

Here, the type of the __segment_end intrinsic function is void __bank *.

Note: You must have enabled language extensions to use this intrinsic function.

__set_configuration_bits __intrinsic void __set_configuration_bits(unsigned long address,

unsigned char value);

Sets the controller configuration bits by inserting a define byte directive at the specified address.

The file Io18xxxx.h, where xxxx is the derivative, contains predefined values which are to be ANDed together.

Example

__set_configuration_bits(CONFIG3H, CCP2M X_OFF_5);

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 225: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Intrinsic functions

__set_interrupt_state void __set_interrupt_state(__istate_t oldState);

This intrinsic function will restore the interrupt flag to the specified state.

Note: The value of oldState must be the result of a call to the __get_interrupt_state intrinsic function.

__sleep void __sleep(void);

Inserts a SLEEP instruction.

__write_table_char __intrinsic void __write_table_char( void pointer *, char );

Writes a char to the address pointer in the CODE area, and calls an empty function that you should modify according to your requirements by using the L06.s49 file located in the src\lib directory.

When you are using RAM in external memory, the preferred way of writing to the CODE area is to use the extended keyword __extmem.

__write_table_dptr __intrinsic void __write_table_char( void pointer *, __dptr );

Writes a __dptr to the address pointer in the CODE area, and calls an empty function that you should modify according to your requirements by using the L06.s49 file located in the src\lib directory.

When you are using RAM in external memory, the preferred way of writing to the CODE area is to use the extended keyword __extmem.

__write_table_long __intrinsic void __write_table_long( void pointer *, long );

Writes a long to the address pointer in the CODE area, and calls an empty function that you should modify according to your requirements by using the L06.s49 file located in the src\lib directory.

When you are using RAM in external memory, the preferred way of writing to the CODE area is to use the extended keyword __extmem.

__write_table_short __intrinsic void __write_table_short( void pointer *, short );

Writes a short to the address pointer in the CODE area, and calls an empty function that you should modify according to your requirements by using the L06.s49 file located in the src\lib directory.

CPIC18-3

Part 2. Compiler reference 205

Page 226: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

206

Descriptions of intrinsic functions

When you are using RAM in external memory, the preferred way of writing to the CODE area is to use the extended keyword __extmem.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 227: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Library functionsThis chapter gives an introduction to the C and C++ library functions. It also lists the header files used for accessing library definitions.

IntroductionThe PIC18 IAR C/C++ Compiler provides two different libraries:

● IAR DLIB Library is a complete ISO/ANSI C and C++ library. This library also supports floating-point numbers in IEEE 754 format and it can be configured to include different levels of support for locale, file descriptors, multibytes, et cetera.

● IAR CLIB Library is a light-weight library, which is not fully compliant with ISO/ANSI C. Neither does it fully support floating-point numbers in IEEE 754 format or does it support Embedded C++.

Note that different customization methods are normally needed for these two libraries. For additional information, see the chapter The DLIB runtime environment and The CLIB runtime environment, respectively.

For detailed information about the library functions, see the online documentation supplied with the product. There is also keyword reference information for the DLIB library functions. To obtain reference information for a function, select the function name in the editor window and press F1.

For additional information about library functions, see the chapter Implementation-defined behaviour in this guide.

HEADER FILES

Your application program gains access to library definitions through header files, which it incorporates using the #include directive. The definitions are divided into a number of different header files, each covering a particular functional area, letting you include just those that are required.

It is essential to include the appropriate header file before making any reference to its definitions. Failure to do so can cause the call to fail during execution, or generate error or warning messages at compile time or link time.

CPIC18-3

Part 2. Compiler reference 207

Page 228: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

208

Introduction

LIBRARY OBJECT FILES

Most of the library definitions can be used without modification, that is, directly from the library object files that are supplied with the product. For information about how to choose a runtime library, see Basic settings for project configuration, page 5. The linker will include only those routines that are required—directly or indirectly—by your application.

REENTRANCY

A function that can be simultaneously invoked in the main application and in any number of interrupts is reentrant. A library function that uses statically allocated data is therefore not reentrant. Most parts of the DLIB library are reentrant, but the following functions and parts are not reentrant:

In addition, some functions share the same storage for errno. These functions are not reentrant, since an errno value resulting from one of these functions can be destroyed by a subsequent use of the function before it has been read. Among these functions are:

exp, exp10, ldexp, log, log10, pow, sqrt, acos, asin, atan2, cosh, sinh, strtod, strtol, strtoul

Remedies for this are:

● Do not use non-reentrant functions in interrupt service routines● Guard calls to a non-reentrant function by a mutex, or a secure region, etc.

Note: Reentrancy is not supported when you use the static overlay system. See Static overlay, page 20.

atexit Needs static data

heap functions Need static data for memory allocation tables

strerror Needs static data

strtok Designed by ISO/ANSI standard to need static data

I/O Every function that uses files in some way. This includes printf, scanf, getchar, and putchar. The functions sprintf and sscanf are not included.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 229: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Library functions

IAR DLIB LibraryThe IAR DLIB Library provides most of the important C and C++ library definitions that apply to embedded systems. These are of the following types:

● Adherence to a free-standing implementation of the ISO/ANSI standard for the programming language C. For additional information, see the chapter Implementation-defined behavior in this guide.

● Standard C library definitions, for user programs.● Embedded C++ library definitions, for user programs.● CSTARTUP, the module containing the start-up code. It is described in the The DLIB

runtime environment chapter in this guide.● Runtime support libraries; for example, low-level floating-point routines.● Intrinsic functions, allowing low-level use of PIC18 features. See the chapter

Intrinsic functions for more information.

C HEADER FILES

This section lists the header files specific to the DLIB library C definitions. Header files may additionally contain target-specific definitions; these are documented in the chapter IAR language extensions.

The following table lists the C header files:

Header file Usage

assert.h Enforcing assertions when functions execute

ctype.h Classifying characters

errno.h Testing error codes reported by library functions

float.h Testing floating-point type properties

iso646.h Using Amendment 1—iso646.h standard header

limits.h Testing integer type properties

locale.h Adapting to different cultural conventions

math.h Computing common mathematical functions

setjmp.h Executing non-local goto statements

signal.h Controlling various exceptional conditions

stdarg.h Accessing a varying number of arguments

stdbool.h Adds support for the bool data type in C.

stddef.h Defining several useful types and macros

stdio.h Performing input and output

stdlib.h Performing a variety of operations

Table 46: Traditional standard C header files—DLIB

CPIC18-3

Part 2. Compiler reference 209

Page 230: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

210

IAR DLIB Library

C++ HEADER FILES

This section lists the C++ header files.

Embedded C++

The following table lists the Embedded C++ header files:

string.h Manipulating several kinds of strings

time.h Converting between various time and date formats

wchar.h Support for wide characters

wctype.h Classifying wide characters

Header file Usage

complex Defining a class that supports complex arithmetic

exception Defining several functions that control exception handling

fstream Defining several I/O streams classes that manipulate external files

iomanip Declaring several I/O streams manipulators that take an argument

ios Defining the class that serves as the base for many I/O streams classes

iosfwd Declaring several I/O streams classes before they are necessarily defined

iostream Declaring the I/O streams objects that manipulate the standard streams

istream Defining the class that performs extractions

new Declaring several functions that allocate and free storage

ostream Defining the class that performs insertions

sstream Defining several I/O streams classes that manipulate string containers

stdexcept Defining several classes useful for reporting exceptions

streambuf Defining classes that buffer I/O streams operations

string Defining a class that implements a string container

strstream Defining several I/O streams classes that manipulate in-memory character sequences

Table 47: Embedded C++ header files

Header file Usage

Table 46: Traditional standard C header files—DLIB (Continued)

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 231: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Library functions

The following table lists additional C++ header files:

Extended Embedded C++ standard template library

The following table lists the Extended Embedded C++ standard template library (STL) header files:

Using standard C libraries in C++

The C++ library works in conjunction with 15 of the header files from the standard C library, sometimes with small alterations. The header files come in two forms—new and traditional—for example, cassert and assert.h.

Header file Usage

fstream.h Defining several I/O stream classes that manipulate external files

iomanip.h Declaring several I/O streams manipulators that take an argument

iostream.h Declaring the I/O streams objects that manipulate the standard streams

new.h Declaring several functions that allocate and free storage

Table 48: Additional Embedded C++ header files—DLIB

Header file Description

algorithm Defines several common operations on sequences

deque A deque sequence container

functional Defines several function objects

hash_map A map associative container, based on a hash algorithm

hash_set A set associative container, based on a hash algorithm

iterator Defines common iterators, and operations on iterators

list A doubly-linked list sequence container

map A map associative container

memory Defines facilities for managing memory

numeric Performs generalized numeric operations on sequences

queue A queue sequence container

set A set associative container

slist A singly-linked list sequence container

stack A stack sequence container

utility Defines several utility components

vector A vector sequence container

Table 49: Standard template library header files

CPIC18-3

Part 2. Compiler reference 211

Page 232: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

212

IAR CLIB Library

The following table shows the new header files:

IAR CLIB LibraryThe IAR CLIB Library provides most of the important C library definitions that apply to embedded systems. These are of the following types:

● Standard C library definitions available for user programs. These are documented in this chapter.

● The system startup code. It is described in the The CLIB runtime environment chapter in this guide.

● Runtime support libraries; for example, low-level floating-point routines.● Intrinsic functions, allowing low-level use of PIC18 features. See the chapter

Intrinsic functions for more information.

Header file Usage

cassert Enforcing assertions when functions execute

cctype Classifying characters

cerrno Testing error codes reported by library functions

cfloat Testing floating-point type properties

climits Testing integer type properties

clocale Adapting to different cultural conventions

cmath Computing common mathematical functions

csetjmp Executing non-local goto statements

csignal Controlling various exceptional conditions

cstdarg Accessing a varying number of arguments

cstddef Defining several useful types and macros

cstdio Performing input and output

cstdlib Performing a variety of operations

cstring Manipulating several kinds of strings

ctime Converting between various time and date formats

Table 50: New standard C header files—DLIB

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 233: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Library functions

LIBRARY DEFINITIONS SUMMARY

This section lists the header files. Header files may additionally contain target-specific definitions.

* The functions isxxx, toupper, and tolower declared in the header file ctype.h evaluate their argument more than once. This is not according to the ISO/ANSI standard.

The following table shows header files that do not contain any functions, but specify various definitions and data types:

Header file Description

assert.h Assertions

ctype.h* Character handling

iccbutl.h Low-level routines

math.h Mathematics

setjmp.h Non-local jumps

stdarg.h Variable arguments

stdio.h Input/output

stdlib.h General utilities

string.h String handling

Table 51: IAR CLIB Library header files

Header file Description

errno.h Error return values

float.h Limits and sizes of floating-point types

limits.h Limits and sizes of integral types

stdbool.h Adds support for the bool data type in C

stddef.h Common definitions including size_t, NULL, ptrdiff_t, and offsetof

Table 52: Miscellaneous IAR CLIB Library header files

CPIC18-3

Part 2. Compiler reference 213

Page 234: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

214

IAR CLIB Library

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 235: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Implementation-defined behaviorThis chapter describes how the PIC18 IAR C/C++ Compiler handles the implementation-defined areas of the C language.

ISO 9899:1990, the International Organization for Standardization standard - Programming Languages - C (revision and redesign of ANSI X3.159-1989, American National Standard), changed by the ISO Amendment 1:1994, Technical Corrigendum 1, and Technical Corrigendum 2, contains an appendix called Portability Issues. The ISO appendix lists areas of the C language that ISO leaves open to each particular implementation.

Note: The PIC18 IAR C/C++ Compiler adheres to a freestanding implementation of the ISO standard for the C programming language. This means that parts of a standard library can be excluded in the implementation.

Descriptions of implementation-defined behaviorThis section follows the same order as the ISO appendix. Each item covered includes references to the ISO chapter and section (in parenthesis) that explains the implementation-defined behavior.

TRANSLATION

Diagnostics (5.1.1.3)

Diagnostics are produced in the form:

level[tag]: filename linenumber: message

where filename is the name of the source file in which the error was encountered, linenumber is the line number at which the compiler detected the error, level is the level of seriousness of the message (remark, warning, error, or fatal error), tag is a unique tag that identifies the message, and message is an explanatory message, possibly several lines.

CPIC18-3

Part 2. Compiler reference 215

Page 236: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

216

Descriptions of implementation-defined behavior

ENVIRONMENT

Arguments to main (5.1.2.2.2.1)

The function called at program startup is called main. There is no prototype declared for main, and the only definition supported for main is:

int main(void)

To change this behavior for the IAR CLIB runtime environment, see Implementation of cstartup, page 82. To change this behavior for the IAR DLIB runtime environment, see Implementation of system startup code, page 72.

Interactive devices (5.1.2.3)

The streams stdin and stdout are treated as interactive devices.

IDENTIFIERS

Significant characters without external linkage (6.1.2)

The number of significant initial characters in an identifier without external linkage is 200.

Significant characters with external linkage (6.1.2)

The number of significant initial characters in an identifier with external linkage is 200.

Case distinctions are significant (6.1.2)

Identifiers with external linkage are treated as case-sensitive.

CHARACTERS

Source and execution character sets (5.2.1)

The source character set is the set of legal characters that can appear in source files. The default source character set is the standard ASCII character set. However, if you use the command line option --enable_multibytes, the source character set will be the host computer’s default character set.

The execution character set is the set of legal characters that can appear in the execution environment. The default execution character set is the standard ASCII character set. However, if you use the command line option --enable_multibytes, the execution character set will be the host computer’s default character set. The IAR DLIB Library needs a multibyte character scanner to support a multibyte execution character set. The IAR CLIB Library does not support multibyte characters.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 237: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Implementation-defined behavior

See Locale, page 62.

Bits per character in execution character set (5.2.4.2.1)

The number of bits in a character is represented by the manifest constant CHAR_BIT. The standard include file limits.h defines CHAR_BIT as 8.

Mapping of characters (6.1.3.4)

The mapping of members of the source character set (in character and string literals) to members of the execution character set is made in a one-to-one way. In other words, the same representation value is used for each member in the character sets except for the escape sequences listed in the ISO standard.

Unrepresented character constants (6.1.3.4)

The value of an integer character constant that contains a character or escape sequence not represented in the basic execution character set or in the extended character set for a wide character constant generates a diagnostic message, and will be truncated to fit the execution character set.

Character constant with more than one character (6.1.3.4)

An integer character constant that contains more than one character will be treated as an integer constant. The value will be calculated by treating the leftmost character as the most significant character, and the rightmost character as the least significant character, in an integer constant. A diagnostic message will be issued if the value cannot be represented in an integer constant.

A wide character constant that contains more than one multibyte character generates a diagnostic message.

Converting multibyte characters (6.1.3.4)

The only locale supported—that is, the only locale supplied with the IAR C/C++ Compiler—is the ‘C’ locale. If you use the command line option --enable_multibytes, the IAR DLIB Library will support multibyte characters if you add a locale with multibyte support or a multibyte character scanner to the library. The IAR CLIB Library does not support multibyte characters.

See Locale, page 62.

Range of 'plain' char (6.2.1.1)

A ‘plain’ char has the same range as an unsigned char.

CPIC18-3

Part 2. Compiler reference 217

Page 238: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

218

Descriptions of implementation-defined behavior

INTEGERS

Range of integer values (6.1.2.5)

The representation of integer values are in the two's complement form. The most significant bit holds the sign; 1 for negative, 0 for positive and zero.

See Basic data types, page 124, for information about the ranges for the different integer types.

Demotion of integers (6.2.1.2)

Converting an integer to a shorter signed integer is made by truncation. If the value cannot be represented when converting an unsigned integer to a signed integer of equal length, the bit-pattern remains the same. In other words, a large enough value will be converted into a negative value.

Signed bitwise operations (6.3)

Bitwise operations on signed integers work the same way as bitwise operations on unsigned integers; in other words, the sign-bit will be treated as any other bit.

Sign of the remainder on integer division (6.3.5)

The sign of the remainder on integer division is the same as the sign of the dividend.

Negative valued signed right shifts (6.3.7)

The result of a right-shift of a negative-valued signed integral type preserves the sign-bit. For example, shifting 0xFF00 down one step yields 0xFF80.

FLOATING POINT

Representation of floating-point values (6.1.2.5)

The representation and sets of the various floating-point numbers adheres to IEEE 854–1987. A typical floating-point number is built up of a sign-bit (s), a biased exponent (e), and a mantissa (m).

See Floating-point types, page 125, for information about the ranges and sizes for the different floating-point types: float and double.

Converting integer values to floating-point values (6.2.1.3)

When an integral number is cast to a floating-point value that cannot exactly represent the value, the value is rounded (up or down) to the nearest suitable value.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 239: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Implementation-defined behavior

Demoting floating-point values (6.2.1.4)

When a floating-point value is converted to a floating-point value of narrower type that cannot exactly represent the value, the value is rounded (up or down) to the nearest suitable value.

ARRAYS AND POINTERS

size_t (6.3.3.4, 7.1.1)

See size_t, page 126, for information about size_t.

Conversion from/to pointers (6.3.4)

See Casting, page 126, for information about casting of data pointers and function pointers.

ptrdiff_t (6.3.6, 7.1.1)

See ptrdiff_t, page 126, for information about the ptrdiff_t.

REGISTERS

Honoring the register keyword (6.5.1)

User requests for register variables are not honored.

STRUCTURES, UNIONS, ENUMERATIONS, AND BITFIELDS

Improper access to a union (6.3.2.3)

If a union gets its value stored through a member and is then accessed using a member of a different type, the result is solely dependent on the internal storage of the first member.

Padding and alignment of structure members (6.5.2.1)

See the section Basic data types, page 124, for information about the alignment requirement for data objects.

Sign of 'plain' bitfields (6.5.2.1)

A 'plain' int bitfield is treated as a signed int bitfield. All integer types are allowed as bitfields.

CPIC18-3

Part 2. Compiler reference 219

Page 240: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

220

Descriptions of implementation-defined behavior

Allocation order of bitfields within a unit (6.5.2.1)

Bitfields are allocated within an integer from least-significant to most-significant bit.

Can bitfields straddle a storage-unit boundary (6.5.2.1)

Bitfields cannot straddle a storage-unit boundary for the chosen bitfield integer type.

Integer type chosen to represent enumeration types (6.5.2.2)

The chosen integer type for a specific enumeration type depends on the enumeration constants defined for the enumeration type. The chosen integer type is the smallest possible.

QUALIFIERS

Access to volatile objects (6.5.3)

Any reference to an object with volatile qualified type is an access.

DECLARATORS

Maximum numbers of declarators (6.5.4)

The number of declarators is not limited. The number is limited only by the available memory.

STATEMENTS

Maximum number of case statements (6.6.4.2)

The number of case statements (case values) in a switch statement is not limited. The number is limited only by the available memory.

PREPROCESSING DIRECTIVES

Character constants and conditional inclusion (6.8.1)

The character set used in the preprocessor directives is the same as the execution character set. The preprocessor recognizes negative character values if a 'plain' character is treated as a signed character.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 241: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Implementation-defined behavior

Including bracketed filenames (6.8.2)

For file specifications enclosed in angle brackets, the preprocessor does not search directories of the parent files. A parent file is the file that contains the #include directive. Instead, it begins by searching for the file in the directories specified on the compiler command line.

Including quoted filenames (6.8.2)

For file specifications enclosed in quotes, the preprocessor directory search begins with the directories of the parent file, then proceeds through the directories of any grandparent files. Thus, searching begins relative to the directory containing the source file currently being processed. If there is no grandparent file and the file has not been found, the search continues as if the filename was enclosed in angle brackets.

Character sequences (6.8.2)

Preprocessor directives use the source character set, with the exception of escape sequences. Thus, to specify a path for an include file, use only one backslash:

#include "mydirectory\myfile"

Within source code, two backslashes are necessary:

file = fopen("mydirectory\\myfile","rt");

Recognized pragma directives (6.8.6)

The following pragma directives are recognized:

alignmentARGSUSEDbaseaddrbitfieldscan_instantiatecodesegconstsegcspy_supportdata_alignmentdatasegdefine_type_infodiag_defaultdiag_errordiag_remarkdiag_suppressdiag_warningdo_not_instantiatefunctionhdrstop

CPIC18-3

Part 2. Compiler reference 221

Page 242: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

222

Descriptions of implementation-defined behavior

include_aliasinlineinstantiatelanguagelocationmemorymessagemodule_namenoneno_pchNOTREACHEDobject_attribute

once optimizepack__printf_argspublic_equrequiredrtmodel__scanf_argssegmentsystem_includetype_attributeVARARGSvectorwarnings

For a description of the pragma directives, see the chapter Pragma directives.

Default __DATE__ and __TIME__ (6.8.8)

The definitions for __TIME__ and __DATE__ are always available.

IAR DLIB LIBRARY FUNCTIONS

The information in this section is valid only if the runtime library configuration you have chosen supports file descriptors. See the chapter The DLIB runtime environment for more information about runtime library configurations.

NULL macro (7.1.6)

The NULL macro is defined to 0.

Diagnostic printed by the assert function (7.2)

The assert() function prints:

filename:linenr expression -- assertion failed

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 243: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Implementation-defined behavior

when the parameter evaluates to zero.

Domain errors (7.5.1)

NaN (Not a Number) will be returned by the mathematic functions on domain errors.

Underflow of floating-point values sets errno to ERANGE (7.5.1)

The mathematics functions set the integer expression errno to ERANGE (a macro in errno.h) on underflow range errors.

fmod() functionality (7.5.6.4)

If the second argument to fmod() is zero, the function returns NaN; errno is set to EDOM.

signal() (7.7.1.1)

The signal part of the library is not supported.

Note: Low-level interface functions exist in the library, but will not perform anything. Use the template source code to implement application-specific signal handling. See Signal and raise, page 65.

Terminating newline character (7.9.2)

stdout stream functions recognize either newline or end of file (EOF) as the terminating character for a line.

Blank lines (7.9.2)

Space characters written to the stdout stream immediately before a newline character are preserved. There is no way to read the line through the stdin stream that was written through the stdout stream.

Null characters appended to data written to binary streams (7.9.2)

No null characters are appended to data written to binary streams.

Files (7.9.3)

Whether a write operation on a text stream causes the associated file to be truncated beyond that point, depends on the application-specific implementation of the low-level file routines. See File input and output, page 62.

CPIC18-3

Part 2. Compiler reference 223

Page 244: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

224

Descriptions of implementation-defined behavior

remove() (7.9.4.1)

The effect of a remove operation on an open file depends on the application-specific implementation of the low-level file routines. See File input and output, page 62.

rename() (7.9.4.2)

The effect of renaming a file to an already existing filename depends on the application-specific implementation of the low-level file routines. See File input and output, page 62.

%p in printf() (7.9.6.1)

The argument to a %p conversion specifier, print pointer, to printf() is treated as having the type void *. The value will be printed as a hexadecimal number, similar to using the %x conversion specifier.

%p in scanf() (7.9.6.2)

The %p conversion specifier, scan pointer, to scanf() reads a hexadecimal number and converts it into a value with the type void *.

Reading ranges in scanf() (7.9.6.2)

A - (dash) character is always treated as a range symbol.

File position errors (7.9.9.1, 7.9.9.4)

On file position errors, the functions fgetpos and ftell store EFPOS in errno.

Message generated by perror() (7.9.10.4)

The generated message is:

usersuppliedprefix:errormessage

Allocating zero bytes of memory (7.10.3)

The calloc(), malloc(), and realloc() functions accept zero as an argument. Memory will be allocated, a valid pointer to that memory is returned, and the memory block can be modified later by realloc.

Behavior of abort() (7.10.4.1)

The abort() function does not flush stream buffers, and it does not handle files, because this is an unsupported feature.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 245: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Implementation-defined behavior

Behavior of exit() (7.10.4.3)

The argument passed to the exit function will be the return value returned by the main function to cstartup.

Environment (7.10.4.4)

The set of available environment names and the method for altering the environment list is described in Environment interaction, page 65.

system() (7.10.4.5)

How the command processor works depends on how you have implemented the system function. See Environment interaction, page 65.

Message returned by strerror() (7.11.6.2)

The messages returned by strerror() depending on the argument is:

The time zone (7.12.1)

The local time zone and daylight savings time implementation is described in Time, page 66.

clock() (7.12.2.1)

From where the system clock starts counting depends on how you have implemented the clock function. See Time, page 66.

IAR CLIB LIBRARY FUNCTIONS

NULL macro (7.1.6)

The NULL macro is defined to (void *) 0.

Argument Message

EZERO no error

EDOM domain error

ERANGE range error

EFPOS file positioning error

EILSEQ multi-byte encoding error

<0 || >99 unknown error

all others error nnn

Table 53: Message returned by strerror()—IAR DLIB library

CPIC18-3

Part 2. Compiler reference 225

Page 246: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

226

Descriptions of implementation-defined behavior

Diagnostic printed by the assert function (7.2)

The assert() function prints:

Assertion failed: expression, file Filename, line linenumber

when the parameter evaluates to zero.

Domain errors (7.5.1)

HUGE_VAL, the largest representable value in a double floating-point type, will be returned by the mathematic functions on domain errors.

Underflow of floating-point values sets errno to ERANGE (7.5.1)

The mathematics functions set the integer expression errno to ERANGE (a macro in errno.h) on underflow range errors.

fmod() functionality (7.5.6.4)

If the second argument to fmod() is zero, the function returns zero (it does not change the integer expression errno).

signal() (7.7.1.1)

The signal part of the library is not supported.

Terminating newline character (7.9.2)

stdout stream functions recognize either newline or end of file (EOF) as the terminating character for a line.

Blank lines (7.9.2)

Space characters written to the stdout stream immediately before a newline character are preserved. There is no way to read the line through the stdin stream that was written through the stdout stream.

Null characters appended to data written to binary streams (7.9.2)

There are no binary streams implemented.

Files (7.9.3)

There are no other streams than stdin and stdout. This means that a file system is not implemented.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 247: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Implementation-defined behavior

remove() (7.9.4.1)

There are no other streams than stdin and stdout. This means that a file system is not implemented.

rename() (7.9.4.2)

There are no other streams than stdin and stdout. This means that a file system is not implemented.

%p in printf() (7.9.6.1)

The argument to a %p conversion specifier, print pointer, to printf() is treated as having the type 'char *'. The value will be printed as a hexadecimal number, similar to using the %x conversion specifier.

%p in scanf() (7.9.6.2)

The %p conversion specifier, scan pointer, to scanf() reads a hexadecimal number and converts it into a value with the type 'void *'.

Reading ranges in scanf() (7.9.6.2)

A - (dash) character is always treated explicitly as a - character.

File position errors (7.9.9.1, 7.9.9.4)

There are no other streams than stdin and stdout. This means that a file system is not implemented.

Message generated by perror() (7.9.10.4)

perror() is not supported.

Allocating zero bytes of memory (7.10.3)

The calloc(), malloc(), and realloc() functions accept zero as an argument. Memory will be allocated, a valid pointer to that memory is returned, and the memory block can be modified later by realloc.

Behavior of abort() (7.10.4.1)

The abort() function does not flush stream buffers, and it does not handle files, because this is an unsupported feature.

Behavior of exit() (7.10.4.3)

The exit() function does not return.

CPIC18-3

Part 2. Compiler reference 227

Page 248: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

228

Descriptions of implementation-defined behavior

Environment (7.10.4.4)

Environments are not supported.

system() (7.10.4.5)

The system() function is not supported.

Message returned by strerror() (7.11.6.2)

The messages returned by strerror() depending on the argument are:

The time zone (7.12.1)

The time zone function is not supported.

clock() (7.12.2.1)

The clock() function is not supported.

Argument Message

EZERO no error

EDOM domain error

ERANGE range error

<0 || >99 unknown error

all others error No.xx

Table 54: Message returned by strerror()—IAR CLIB library

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 249: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

IAR language extensionsThis chapter describes IAR language extensions to the ISO/ANSI standard for the C programming language. All extensions can also be used for the C++ programming language.

In the IAR Embedded Workbench® IDE, language extensions are enabled by default.

See the compiler options -e on page 159 and --strict_ansi on page 172 for information about how to enable and disable language extensions from the command line.

Why should language extensions be used?By using language extensions, you gain full control over the resources and features of the target microcontroller, and can thereby fine-tune your application.

If you want to use the source code with different compilers, note that language extensions may require minor modifications before the code can be compiled. A compiler typically supports microcontroller-specific language extensions as well as vendor-specific ones.

Descriptions of language extensionsThis section gives an overview of available language extensions.

Memory, type, and object attributes

Entities such as variables and functions may be declared with memory, type, and object attributes. The syntax follows the syntax for qualifiers—such as const—but the semantics is different.

● A memory attribute controls the placement of the entity. There can be only one memory attribute.

● A type attribute controls aspects of the object visible to the surrounding context. There can be many different type attributes, and they must be included when the object is declared.

● An object attribute only has to be specified at the definition, but not at the declaration, of an object. The object attribute does not affect the object interface.

CPIC18-3

Part 2. Compiler reference 229

Page 250: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

230

Descriptions of language extensions

See the chapter Extended keywords for a complete list of attributes.

Placement at an absolute address or in a named segment

The operator @ or the directive #pragma location can be used for placing a variable at an absolute address, or placing a variable or function in a named segment. The named segment can either be a predefined segment, or a user-defined segment.

Note: Placing variables and functions into named segments can also be done using the pragma directives #pragma codeseg, #pragma constseg, and #pragma dataseg.

Example 1

__no_init int x @ 0x1000;

An absolute declared variable cannot have an initializer, which means the variable must also be __no_init or const declared.

Example 2

void test(void) @ "MYOWNSEGMENT"{ ...

Note that all segments, both user-defined and predefined, must be assigned a location, which is done in the linker command file.

_Pragma

For information about the preprocessor operator _Pragma, see _Pragma(), page 197.

Variadic macros

Variadic macros are the preprocessor macro equivalents of printf style functions. For more information, see __VA_ARGS__, page 199.

Inline functions

The inline keyword can be used on functions. It works just like the C++ keyword inline and the #pragma inline directive.

Mixing declarations and statements

It is possible to mix declarations and statements within the same scope.

Declaration in for loops

It is possible to have a declaration in the initialization expression of a for loop.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 251: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

IAR language extensions

Inline assembler

Inline assembler can be used for inserting assembler instructions in the generated function.

The syntax for inline assembler is:

asm("MOVLW 0x23");

In strict ISO/ANSI mode, the use of inline assembler is disabled.

For more details about inline assembler, see Mixing C and assembler, page 85.

C++ style comments

C++ style comments are accepted. A C++ style comment starts with the character sequence // and continues to the end of the line. For example:

// The length of the bar, in centimeters.int length;

__ALIGNOF__

For information about alignment, see Alignment, page 123, and __ALIGNOF__(), page 195.

Compound literals

To create compound literals you can use the following syntax:

/* Create a pointer to an anonymous array */int *p = (int []) {1,2,3};

/* Create a pointer to an anonymous structX */structX *px = &(structX) {5,6,7};

Note:

● A compound literal can be modified unless it is declared const● Compound literals are not supported in Embedded C++.

Anonymous structs and unions

C++ includes a feature named anonymous unions. The PIC18 IAR C/C++ Compiler allow a similar feature for both structs and unions.

An anonymous structure type (that is, one without a name) defines an unnamed object (and not a type) whose members are promoted to the surrounding scope. External anonymous structure types are allowed.

CPIC18-3

Part 2. Compiler reference 231

Page 252: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

232

Descriptions of language extensions

For example, the structure str in the following example contains an anonymous union. The members of the union are accessed using the names b and c, for example obj.b.

Without anonymous structure types, the union would have to be named—for example u—and the member elements accessed using the syntax obj.u.b.

struct str { int a; union { int b; int c; };};

struct str obj;

Bitfields and non-standard types

In ISO/ANSI C, a bitfield must be of the type int or unsigned int. Using IAR language extensions, any integer types and enums may be used.

For example, in the following structure an unsigned char is used for holding three bits. The advantage is that the struct will be smaller.

struct str { unsigned char bitOne : 1; unsigned char bitTwo : 1; unsigned char bitThree : 1;};

This matches G.5.8 in the appendix of the ISO standard, ISO Portability Issues.

Incomplete arrays at end of structs

The last element of a struct may be an incomplete array. This is useful because one chunk of memory can be allocated for the struct itself and for the array, regardless of the size of the array.

Note: The array may not be the only member of the struct. If that was the case, then the size of the struct would be zero, which is not allowed in ISO/ANSI C.

Example

struct str { char a;

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 253: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

IAR language extensions

unsigned long b[];};

struct str * GetAStr(int size){ return malloc(sizeof(struct str) + sizeof(unsigned long) * size);}

void UseStr(struct str * s){ s->b[10] = 0;}

Arrays of incomplete types

An array may have an incomplete struct, union, or enum type as its element type. The types must be completed before the array is used (if it is), or by the end of the compilation unit (if it is not).

Empty translation units

A translation unit (source file) is allowed to be empty, that is, it does not have to contain any declarations.

In strict ISO/ANSI mode, a warning is issued if the translation unit is empty.

Example

The following source file is only used in a debug build. (In a debug build, the NDEBUG preprocessor flag is undefined.) Since the entire contents of the file is conditionally compiled using the preprocessor, the translation unit will be empty when the application is compiled in release mode. Without this extension, this would be considered an error.

#ifndef NDEBUG

void PrintStatusToTerminal(){ /* Do something */}

#endif

CPIC18-3

Part 2. Compiler reference 233

Page 254: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

234

Descriptions of language extensions

Comments at the end of preprocessor directives

This extension, which makes it legal to place text after preprocessor directives, is enabled, unless strict ISO/ANSI mode is used. This language extension exists to support compilation of old legacy code; we do not recommend that you write new code in this fashion.

Example

#ifdef FOO

... something ...

#endif FOO /* This is allowed but not recommended. */

Forward declaration of enums

The IAR Systems language extensions allow that you first declare the name of an enum and later resolve it by specifying the brace-enclosed list.

Extra comma at end of enum list

It is allowed to place an extra comma at the end of an enum list. In strict ISO/ANSI mode, a warning is issued.

Note: ISO/ANSI C allows extra commas in similar situations, for example after the last element of the initializers to an array. The reason is, that it is easy to get the commas wrong if parts of the list are moved using a normal cut-and-paste operation.

Example

enum { kOne, kTwo, /* This is now allowed. */};

Missing semicolon at end of struct or union specifier

A warning is issued if the semicolon at the end of a struct or union specifier is missing.

NULL and void

In operations on pointers, a pointer to void is always implicitly converted to another type if necessary, and a null pointer constant is always implicitly converted to a null pointer of the right type if necessary. In ISO/ANSI C, some operators allow such things, while others do not allow them.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 255: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

IAR language extensions

A label preceding a "}"

In ISO/ANSI C, a label must be followed by at least one statement. Therefore, it is illegal to place the label at the end of a block. In the PIC18 IAR C/C++ Compiler, a warning is issued.

To create a standard-compliant C program (so that you will not have to see the warning), you can place an empty statement after the label. An empty statement is a single ; (semi-colon).

Example

void test(){ if (...) goto end;

/* Do something */

end: /* Illegal at the end of block. */}

Note: This also applies to the labels of switch statements.

The following piece of code will generate a warning:

switch (x){case 1: ...; break;

default:}

A good way to convert this into a standard-compliant C program is to place a break; statement after the default: label.

Empty declarations

An empty declaration (a semicolon by itself) is allowed, but a remark is issued (provided that remarks are enabled).

This is useful when preprocessor macros are used that could expand to nothing. Consider the following example. In a debug build, the macros DEBUG_ENTER and DEBUG_LEAVE could be defined to something useful. However, in a release build they could expand into nothing, leaving the ; character in the code:

void test(){

CPIC18-3

Part 2. Compiler reference 235

Page 256: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

236

Descriptions of language extensions

DEBUG_ENTER();

do_something(); DEBUG_LEAVE();}

Single value initialization

ISO/ANSI C requires that all initializer expressions of static arrays, structs, and unions are enclosed in braces.

Single-value initializers are allowed to appear without braces, but a warning is issued.

Example

In the PIC18 IAR C/C++ Compiler, the following expression is allowed:

struct str{ int a;} x = 10;

Casting pointers to integers in static initializers

In an initializer, a pointer constant value may be cast to an integral type if the integral type is large enough to contain it.

In the following example, it is assumed that pointers to __bank0 and __bank are 8 and 16 bits, respectively. The first initialization is correct, because it is possible to cast the 8-bit address to a 8-bit unsigned char variable. However, it is illegal to use the 16-bit address of b as an initializer for a 8-bit value.

__bank0 int a;__bank int b;

unsigned char ap = (unsigned char)&a; /* Correct */unsigned char bp = (unsigned char)&b; /* Error */

Hexadecimal floating-point constants

Floating-point constants can be given in hexadecimal style. The syntax is 0xMANTp{+|-}EXP, where MANT is the mantissa in hexadecimal digits, including an optional . (decimal point), and EXP is the exponent with decimal digits, representing an exponent of 2.

Examples

0x1p0 is 1

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 257: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

IAR language extensions

0xA.8p2 is 10.5*2^2

Using the bool data type in C

To use the bool type in C source code, you must include the file stdbool.h. (The bool data type is supported by default in C++.)

Taking the address of a register variable

In ISO/ANSI C, it is illegal to take the address of a variable specified as a register variable.

The PIC18 IAR C/C++ Compiler allows this, but a warning is issued.

Duplicated size and sign specifiers

Should the size or sign specifiers be duplicated (for example, short short or unsigned unsigned), an error is issued.

"long float" means "double"

long float is accepted as a synonym for double.

Repeated typedefs

Redeclarations of typedef that occur in the same scope are allowed, but a warning is issued.

Mixing pointer types

Assignment and pointer difference is allowed between pointers to types that are interchangeable but not identical; for example, unsigned char * and char *. This includes pointers to integral types of the same size. A warning is issued.

Assignment of a string constant to a pointer to any kind of character is allowed, and no warning will be issued.

Non-top level const

Assignment of pointers is allowed in cases where the destination type has added type qualifiers that are not at the top level (for example, int ** to int const **). It is also allowed to compare and take the difference of such pointers.

Declarations in other scopes

External and static declarations in other scopes are visible. In the following example, the variable y can be used at the end of the function, even though it should only be visible in the body of the if statement. A warning is issued.

CPIC18-3

Part 2. Compiler reference 237

Page 258: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

238

Descriptions of language extensions

int test(int x){ if (x) { extern int y; y = 1; }

return y;}

Non-lvalue arrays

A non-lvalue array expression is converted to a pointer to the first element of the array when it is used.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 259: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

DiagnosticsThis chapter describes the format of the diagnostic messages and explains how diagnostic messages are divided into different levels of severity.

Message formatAll diagnostic messages are issued as complete, self-explanatory messages. A typical diagnostic message from the compiler is produced in the form:

level[tag]: filename linenumber: message

where filename is the name of the source file in which the error was encountered, linenumber is the line number at which the compiler detected the error, level is the level of seriousness of the diagnostic, tag is a unique tag that identifies the diagnostic message, and message is a self-explanatory message, possibly several lines long.

Diagnostic messages are displayed on the screen, as well as printed in the optional list file.

Use the option --diagnostics_tables to list all possible compiler diagnostic messages in a named file. In addition, you can find all messages specific to the PIC18 IAR C/C++ Compiler in the readme file ICCPIC18_msg.htm.

Severity levelsThe diagnostics are divided into different levels of severity:

Remark

A diagnostic message that is produced when the compiler finds a source code construct that can possibly lead to erroneous behavior in the generated code. Remarks are by default not issued, but can be enabled, see --remarks, page 171.

Warning

A diagnostic that is produced when the compiler finds a programming error or omission which is of concern, but not so severe as to prevent the completion of compilation. Warnings can be disabled by use of the command-line option --no_warnings, see page 168.

CPIC18-3

Part 2. Compiler reference 239

Page 260: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

240

Severity levels

Error

A diagnostic that is produced when the compiler has found a construct which clearly violates the C or C++ language rules, such that code cannot be produced. An error will produce a non-zero exit code.

Fatal error

A diagnostic that is produced when the compiler has found a condition that not only prevents code generation, but which makes further processing of the source code pointless. After the diagnostic has been issued, compilation terminates. A fatal error will produce a non-zero exit code.

SETTING THE SEVERITY LEVEL

The diagnostic can be suppressed or the severity level can be changed for all diagnostics, except for fatal errors and some of the regular errors.

See Options summary, page 149, for a description of the compiler options that are available for setting severity levels.

See the chapter Pragma directives, for a description of the pragma directives that are available for setting severity levels.

INTERNAL ERROR

An internal error is a diagnostic message that signals that there has been a serious and unexpected failure due to a fault in the compiler. It is produced using the following form:

Internal error: message

where message is an explanatory message. If internal errors occur, they should be reported to your software distributor or IAR Technical Support. Include information enough to reproduce the problem, typically:

● The product name● The version number of the compiler, which can be seen in the header of the list files

generated by the compiler● Your license number● The exact internal error message text● The source file of the program that generated the internal error● A list of the options that were used when the internal error occurred.

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 261: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Index

Index

Aabort (exit function) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57absolute location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

#pragma location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188absolute placement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230address spaces, multiple . . . . . . . . . . . . . . . . . . . . . . . . . . . 43addressing. See memory typesalgorithm (STL header file) . . . . . . . . . . . . . . . . . . . . . . . 211alias, for header files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

forcing stricter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185__ALIGNOF__ (predefined symbol) . . . . . . . . . . . . . . . . 195& (operator), avoid for auto variables . . . . . . . . . . . . . . . . 116anonymous structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114anonymous symbols, creating . . . . . . . . . . . . . . . . . . . . . . 231applications

building . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4initializing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57, 81terminating. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57, 81

architecture, PIC18 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiARGFRAME (compiler function directive) . . . . . . . . . 24, 97arrays

implementation-defined behavior. . . . . . . . . . . . . . . . . 219__asm (extended keyword) . . . . . . . . . . . . . . . . . . . . . . . . 177asm (extended keyword) . . . . . . . . . . . . . . . . . . . . . . . . . . 177assembler

inline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87, 116, 231mixing with C/C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

assembler directivesARGFRAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97CFI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96DC32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87ENDMOD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72EQU. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170FUNCALL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97FUNCTION. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97LOCFRAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97MODULE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

PUBLIC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170REQUIRE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72RSEG. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72RTMODEL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

assembler instructionsCLRWTD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202NOP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203RESET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203SLEEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

assembler labels?C_EXIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83?C_GETCHAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82?C_PUTCHAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

assembler language interface . . . . . . . . . . . . . . . . . . . . . . . 85creating skeleton code . . . . . . . . . . . . . . . . . . . . . . . . . . 88

assembler list file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97assembler routines, calling from C . . . . . . . . . . . . . . . . . . . 88assert. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67assert.h (library header file) . . . . . . . . . . . . . . . . . . . 209, 213assumptions (programming experience) . . . . . . . . . . . . . . xvii@ (operator) . . . . . . . . . . . . . . . . . . . . . . . . . . . 133, 135, 137atomic operations, performing . . . . . . . . . . . . . . . . . . . . . 181attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128auto variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Bbank select register (BSR). . . . . . . . . . . . . . . . . . . . . . . 13–14__bank (extended keyword) . . . . . . . . . . . . . . . . . . . . . . . 178banked memory, overview . . . . . . . . . . . . . . . . . . . . . . . . . 13__bankN (extended keyword) . . . . . . . . . . . . . . . . . . . . . . 178BANKN_A (segment). . . . . . . . . . . . . . . . . . . . . . . . . . . . 137BANKn_A (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133__bankN_func (extended keyword) . . . . . . . . . . . . . . . . . 179BANKN_I (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137BANKn_I (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133BANKN_ID (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . 137BANKn_ID (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . 133BANKN_N (segment). . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

CPIC18-3

241

Page 262: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

242

BANKn_N (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134BANKN_Z (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138BANKn_Z (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134bank, specifying accesses . . . . . . . . . . . . . . . . . . . . . . . . . . 14BANK_A (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135__bank_func (extended keyword) . . . . . . . . . . . . . . . . . . . 178BANK_I (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135BANK_ID (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135BANK_N (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136BANK_Z (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136BANK_ZD (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136__BASE_FILE__ (predefined symbol) . . . . . . . . . . . . . . . 195basic_template_matching (pragma directive) . . . . . . . . . . 184

using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105bit negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117bitfields

data representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113implementation-defined behavior. . . . . . . . . . . . . . . . . 219

bitfields (pragma directive) . . . . . . . . . . . . . . . . . . . . 125, 185bool (data type) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

adding support for in CLIB . . . . . . . . . . . . . . . . . . . . . 213adding support for in DLIB . . . . . . . . . . . . . . . . . . . . . 209making available in C . . . . . . . . . . . . . . . . . . . . . . . . . . 74supported in C code . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

BSR (register) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13–14bubble sort algorithm, adding support for . . . . . . . . . . . . . . 75

C__CODE_MODEL__ (predefined symbol). . . . . . . . . . . . 195C and C++ linkage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92C calling convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91C header files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209call chains, avoiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116call frame information . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96call stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96callee-save registers, stored on stack. . . . . . . . . . . . . . . . . . 19

calling conventionC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

calloc (standard library function) . . . . . . . . . . . . . . . . . . . . 20calls, eliminating overhead of . . . . . . . . . . . . . . . . . . . . . . 112cassert (library header file) . . . . . . . . . . . . . . . . . . . . . . . . 212cast operators

in Extended EC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100missing from Embedded C++ . . . . . . . . . . . . . . . . . . . 100

castsof pointers and integers . . . . . . . . . . . . . . . . . . . . . . . . 126variants in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

cctype (library header file) . . . . . . . . . . . . . . . . . . . . . . . . 212cerrno (library header file) . . . . . . . . . . . . . . . . . . . . . . . . 212CFI (assembler directive) . . . . . . . . . . . . . . . . . . . . . . . . . . 96cfloat (library header file) . . . . . . . . . . . . . . . . . . . . . . . . . 212char (data type) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

signed and unsigned. . . . . . . . . . . . . . . . . . . . . . . 124, 152char (data type), signed and unsigned . . . . . . . . . . . . . . . . 164characters, implementation-defined behavior . . . . . . . . . . 216character-based I/O

customizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52providing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

--char_is_signed (compiler option) . . . . . . . . . . . . . . . . . . 152chip extensions, support for . . . . . . . . . . . . . . . . . . . . . . . . . 4class memory (extended EC++) . . . . . . . . . . . . . . . . . . . . 102class template partial specialization matching(extended EC++). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101__clear_watchdog_timer (intrinsic function) . . . . . . . . . . 202CLIB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

climits (library header file) . . . . . . . . . . . . . . . . . . . . . . . . 212clocale (library header file) . . . . . . . . . . . . . . . . . . . . . . . . 212__close (DLIB library function) . . . . . . . . . . . . . . . . . . . . . 62CLRWTD (assembler instruction) . . . . . . . . . . . . . . . . . . 202cmath (library header file). . . . . . . . . . . . . . . . . . . . . . . . . 212code

excluding when linking . . . . . . . . . . . . . . . . . . . . . . . . . 72

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 263: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Index

execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6placement of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131placement of startup code . . . . . . . . . . . . . . . . . . . . . . . 39

code modelsconfiguration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6overlay model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23specifying on command line . . . . . . . . . . . . . . . . . . . . 152stack model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Code motion (compiler option). . . . . . . . . . . . . . . . . . . . . 112code motion, disabling . . . . . . . . . . . . . . . . . . . . . . . . . . . 166CODE (memory area)

writing to when using external RAM . . . . . . . . . . 205–206CODE (segment memory type). . . . . . . . . . . . . . . . . . . . . . 30CODE (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39, 138--code_model (compiler option) . . . . . . . . . . . . . . . . . . . . 152__code_model (runtime model attribute) . . . . . . . . . . . . . . 70COFF/COD (output formats) . . . . . . . . . . . . . . . . . . . . . . . 70Command Line Build Utility . . . . . . . . . . . . . . . . . . . . . . . 54Common subexpr elimination (compiler option). . . . . . . . 111common sub-expression elimination, disabling . . . . . . . . 166compact code, data model for . . . . . . . . . . . . . . . . . . . . . . . . 6compiler environment variables . . . . . . . . . . . . . . . . . . . . 149compiler error return codes . . . . . . . . . . . . . . . . . . . . . . . . 149compiler listing, generating. . . . . . . . . . . . . . . . . . . . . . . . 163compiler object file

including debug information . . . . . . . . . . . . . . . . 155, 171specifying filename . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

compiler optionssetting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147specifying parameters . . . . . . . . . . . . . . . . . . . . . . . . . 148summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149typographic convention . . . . . . . . . . . . . . . . . . . . . . . . . xxCode motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112Common subexpr elimination . . . . . . . . . . . . . . . . . . . 111Function inlining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112Loop unrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Type-based alias analysis . . . . . . . . . . . . . . . . . . . . . . . 112-D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

-e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159-f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161-I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161-l . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89, 163-o . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168-r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171-s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171-z . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173--char_is_signed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152--code-model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152--data_model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154--debug. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155--dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155--diagnostics_tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 157--diag_error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156--diag_remark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156--diag_suppress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157--diag_warning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157--disable_fast_return . . . . . . . . . . . . . . . . . . . . . . . . . . 158--disable_lfsr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158--dlib_config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158--ec++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159--eec++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159--eeprom_size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160--enable_multibytes . . . . . . . . . . . . . . . . . . . . . . . . . . . 160--error_limit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160--extmem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160--header_context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161--library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164--library_module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164--misrac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164--misrac_verbose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165--module_name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165--no_code_motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166--no_cse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166--no_inline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167--no_tbaa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167--no_typedefs_in_diagnostics . . . . . . . . . . . . . . . . . . . 167--no_unroll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

CPIC18-3

243

Page 264: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

244

--no_warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168--no_wrap_diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . 168--omit_types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169--only_stdout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169--preinclude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169--preprocess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170--public_equ. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170--remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171--require_prototypes. . . . . . . . . . . . . . . . . . . . . . . . . . . 171--silent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172--strict_ansi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172--warnings_affect_exit_code . . . . . . . . . . . . . . . . 149, 172--warnings_are_errors . . . . . . . . . . . . . . . . . . . . . . . . . 173--workaround_pic18fx5x . . . . . . . . . . . . . . . . . . . . . . . 173

compiler version number . . . . . . . . . . . . . . . . . . . . . . . . . 198compiling, from the command line . . . . . . . . . . . . . . . . . . . . 4complex numbers, supported in Embedded C++. . . . . . . . 100complex (library header file). . . . . . . . . . . . . . . . . . . . . . . 210compound literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231computer style, typographic convention . . . . . . . . . . . . . . . xxconfiguration

basic project settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5__low_level_init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

configuration symbols, in library configuration files. . . . . . 54consistency

enforcing using pragma directive . . . . . . . . . . . . . . . . . 190of modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

CONST (segment memory type). . . . . . . . . . . . . . . . . . . . . 30CONST (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139constants, in named segments . . . . . . . . . . . . . . . . . . . . . . 185__constptr (extended keyword) . . . . . . . . . . . . . . . . . . . . . 179constseg (pragma directive) . . . . . . . . . . . . . . . . . . . . . . . 185const_cast (cast operator) . . . . . . . . . . . . . . . . . . . . . . . . . 100conventions, typographic . . . . . . . . . . . . . . . . . . . . . . . . . . xxcopyright notice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii__cplusplus (predefined symbol) . . . . . . . . . . . . . . . . . . . 195__cpu (runtime model attribute) . . . . . . . . . . . . . . . . . . . . . 70CPU, specifying in linker command file . . . . . . . . . . . . . . . 31csetjmp (library header file) . . . . . . . . . . . . . . . . . . . . . . . 212

csignal (library header file) . . . . . . . . . . . . . . . . . . . . . . . . 212CSTACK (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

See also stackCSTART (segment). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39cstartup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

customizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52, 58implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

cstdarg (library header file) . . . . . . . . . . . . . . . . . . . . . . . . 212cstddef (library header file). . . . . . . . . . . . . . . . . . . . . . . . 212cstdio (library header file). . . . . . . . . . . . . . . . . . . . . . . . . 212cstdlib (library header file) . . . . . . . . . . . . . . . . . . . . . . . . 212cstring (library header file) . . . . . . . . . . . . . . . . . . . . . . . . 212ctime (library header file) . . . . . . . . . . . . . . . . . . . . . . . . . 212ctype.h (library header file). . . . . . . . . . . . . . . . . . . . 209, 213C++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

See also Embedded C++ and Extended Embedded C++absolute location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40calling convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130dynamic initialization in . . . . . . . . . . . . . . . . . . . . . . . . 39features excluded from EC++ . . . . . . . . . . . . . . . . . . . . 99function linkage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99special function types. . . . . . . . . . . . . . . . . . . . . . . . . . . 28static member variables . . . . . . . . . . . . . . . . . . . . . . . . . 40terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx

C++ header files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210–211C++ objects, placing in memory type . . . . . . . . . . . . . . . . . 18C-SPY

low-level interface . . . . . . . . . . . . . . . . . . . . . . . . . . 67, 82STL container support . . . . . . . . . . . . . . . . . . . . . . . . . 106

C/C++, mixing with assembler . . . . . . . . . . . . . . . . . . . . . . 86?C_EXIT (assembler label). . . . . . . . . . . . . . . . . . . . . . . . . 83?C_GETCHAR (assembler label) . . . . . . . . . . . . . . . . . . . . 82C_INCLUDE (environment variable) . . . . . . . . . . . . 149, 162?C_PUTCHAR (assembler label) . . . . . . . . . . . . . . . . . . . . 82C99 standard, added functionality from . . . . . . . . . . . . . . . 74

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 265: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Index

D-D (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153__DATA_MODEL__ (predefined symbol) . . . . . . . . . . . . 195-D (XLINK option). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31data

alignment of. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123excluding when linking . . . . . . . . . . . . . . . . . . . . . . . . . 72located, placement of . . . . . . . . . . . . . . . . . . . . . . . . . . . 38placement of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

data memory attributes, using . . . . . . . . . . . . . . . . . . . . . . . 15data models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5data representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123data storage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

extended keywords. . . . . . . . . . . . . . . . . . . . . . . . . . . . 175using type definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124floating point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124using efficient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

DATA (segment memory type) . . . . . . . . . . . . . . . . . . . . . . 30dataseg (pragma directive) . . . . . . . . . . . . . . . . . . . . . . . . 185data_alignment (pragma directive) . . . . . . . . . . . . . . . . . . 185--data_model (compiler option) . . . . . . . . . . . . . . . . . . . . 154__data_model (runtime model attribute) . . . . . . . . . . . . . . . 70__DATE__ (predefined symbol) . . . . . . . . . . . . . . . . . . . . 196date (DLIB library function), configuring support for. . . . . 66DC32 (assembler directive). . . . . . . . . . . . . . . . . . . . . . . . . 87--debug (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . 155debug information, including in object file . . . . . . . . 155, 171declarations

in for loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230mixing with statements . . . . . . . . . . . . . . . . . . . . . . . . 230of functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

declarators, implementation-defined behavior . . . . . . . . . 220delete (keyword) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20--dependencies (compiler option) . . . . . . . . . . . . . . . . . . . 155

deque (STL header file) . . . . . . . . . . . . . . . . . . . . . . . . . . 211destructors and interrupts, using . . . . . . . . . . . . . . . . . . . . 107diagnostic messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

classifying as errors . . . . . . . . . . . . . . . . . . . . . . . . . . . 156classifying as remarks . . . . . . . . . . . . . . . . . . . . . . . . . 156classifying as warnings . . . . . . . . . . . . . . . . . . . . . . . . 157disabling warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168disabling wrapping of . . . . . . . . . . . . . . . . . . . . . . . . . 168enabling remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171listing all used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157suppressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

--diagnostics_tables (compiler option) . . . . . . . . . . . . . . . 157diag_default (pragma directive) . . . . . . . . . . . . . . . . . . . . 186--diag_error (compiler option) . . . . . . . . . . . . . . . . . . . . . 156diag_error (pragma directive) . . . . . . . . . . . . . . . . . . . . . . 186--diag_remark (compiler option). . . . . . . . . . . . . . . . . . . . 156diag_remark (pragma directive) . . . . . . . . . . . . . . . . . . . . 186--diag_suppress (compiler option) . . . . . . . . . . . . . . . . . . 157diag_suppress (pragma directive) . . . . . . . . . . . . . . . . . . . 186--diag_warning (compiler option) . . . . . . . . . . . . . . . . . . . 157diag_warning (pragma directive) . . . . . . . . . . . . . . . . . . . 187DIFUNCT (segment) . . . . . . . . . . . . . . . . . . . . . . . . . 39, 139direct addressing, support for . . . . . . . . . . . . . . . . . . . . . . . 20directives

function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24, 97pragma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9, 183

--disable_fast_return (compiler option). . . . . . . . . . . . . . . 158__disable_high_priority_interrupt (intrinsic function) . . . 202__disable_interrupt (intrinsic function). . . . . . . . . . . . . . . 202--disable_lfsr (compiler option) . . . . . . . . . . . . . . . . . . . . 158__disable_low_priority_interrupt (intrinsic function) . . . . 202__disable_peripheral_interrupt (intrinsic function) . . . . . . 202disclaimer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iiDLIB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7prebuilt libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

--dlib_config (compiler option). . . . . . . . . . . . . . . . . . . . . 158Dlib_defaults.h (library configuration defaults) . . . . . . . . . 54

CPIC18-3

245

Page 266: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

246

dlpic18Custom.h (library configuration file). . . . . . . . . 54–55document conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxdocumentation, library . . . . . . . . . . . . . . . . . . . . . . . . . . . 207double (data type) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125__dptr (extended keyword) . . . . . . . . . . . . . . . . . . . . . . . . 179dynamic initialization . . . . . . . . . . . . . . . . . . . . . . . . . . 56, 81

in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39dynamic memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

E-e (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159--ec++ (compiler option). . . . . . . . . . . . . . . . . . . . . . . . . . 159EC++ header files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210--eec++ (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . 159__eeprom (extended keyword) . . . . . . . . . . . . . . . . . . . . . 179__eeprom (runtime model attribute) . . . . . . . . . . . . . . . . . . 70EEPROM memory

overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14specifying size of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

EEPROM_A (segment). . . . . . . . . . . . . . . . . . . . . . . . . . . 140EEPROM_I (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . 140EEPROM_ID (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . 140EEPROM_N (segment). . . . . . . . . . . . . . . . . . . . . . . . . . . 141--eeprom_size (compiler option). . . . . . . . . . . . . . . . . . . . 160EEPROM_Z (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . 141Embedded C++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

See also C++differences from C++. . . . . . . . . . . . . . . . . . . . . . . . . . . 99enabling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159language extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

__embedded_cplusplus (predefined symbol) . . . . . . . . . . 196__enable_high_priority_ . . . . . . . . . . . . . . . . . . . . . . . . . . 202__enable_interrupt (intrinsic function) . . . . . . . . . . . . . . . 202__enable_low_priority_interrupt (intrinsic function) . . . . 203--enable_multibytes (compiler option) . . . . . . . . . . . . . . . 160__enable_peripheral_interrupt (intrinsic function) . . . . . . 203ENDMOD (assembler directive). . . . . . . . . . . . . . . . . . . . . 72enumerations, implementation-defined behavior. . . . . . . . 219

enum, data representation . . . . . . . . . . . . . . . . . . . . . . . . . 124environment

implementation-defined behavior. . . . . . . . . . . . . . . . . 216runtime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

environment variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149C_INCLUDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149, 162QCCPIC18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

EQU (assembler directive) . . . . . . . . . . . . . . . . . . . . . . . . 170errno.h (library header file) . . . . . . . . . . . . . . . . . . . . 209, 213error messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240

classifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156error return codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149--error_limit (compiler option) . . . . . . . . . . . . . . . . . . . . . 160exception handling, missing from Embedded C++ . . . . . . . 99exception vectors. See interrupt vectorsexception (library header file) . . . . . . . . . . . . . . . . . . . . . . 210exit sequence, overview . . . . . . . . . . . . . . . . . . . . . . . . . . . 56_Exit (exit function) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57exit (exit function) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107_exit (exit function) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57exiting, application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57experience, programming . . . . . . . . . . . . . . . . . . . . . . . . . xviiexport (keyword), missing from Extended EC++ . . . . . . . 104Extended Embedded C++ . . . . . . . . . . . . . . . . . . . . . . . . . 100

See also C++enabling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159standard template library (STL) . . . . . . . . . . . . . . . . . . 211

extended keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175data storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175enabling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15, 24using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175@ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177asm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177__asm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177__bank . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 267: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Index

__bankN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178__bankN_func . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179__bank_func . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178__constptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179__dptr. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179__eeprom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179__extmem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180__interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25, 180

See also INTVEC (segment)using in pragma directives. . . . . . . . . . . . . . . . . . . . 192

__intrinsic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180__monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181__nonbanked . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181__noreturn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

using in pragma directives. . . . . . . . . . . . . . . . . . . . 189__no_init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119, 181

using in pragma directives. . . . . . . . . . . . . . . . . . . . 188__root. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

using in pragma directives. . . . . . . . . . . . . . . . . . . . 189__stack_func . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

extern "C" . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104external memory

enabling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14support for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

--extmem (compiler option) . . . . . . . . . . . . . . . . . . . . . . . 160extmem memory. See external memory__extmem (extended keyword) . . . . . . . . . . . . . . . . . . . . . 180EXTMEM_A (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . 141EXTMEM_I (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . 142EXTMEM_ID (segment) . . . . . . . . . . . . . . . . . . . . . . . . . 142EXTMEM_N (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . 143EXTMEM_Z (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . 143

F-f (compiler option). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161fast returns, disabling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158fatal error messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240

__FILE__ (predefined symbol). . . . . . . . . . . . . . . . . . . . . 196file dependencies, tracking . . . . . . . . . . . . . . . . . . . . . . . . 155file paths, specifying for #include files . . . . . . . . . . . . . . . 161filename, of object file . . . . . . . . . . . . . . . . . . . . . . . . . . . 168float (data type). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125floating-point constants, hexadecimal notation . . . . . . . . . 236floating-point format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113implementation-defined behavior. . . . . . . . . . . . . . . . . 218special cases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

float.h (library header file) . . . . . . . . . . . . . . . . . . . . 209, 213for loops, declarations in. . . . . . . . . . . . . . . . . . . . . . . . . . 230formats

floating-point values . . . . . . . . . . . . . . . . . . . . . . . . . . 125standard IEEE (floating point) . . . . . . . . . . . . . . . . . . . 125

_formatted_write (CLIB library function). . . . . . . . . . . . . . 79fragmentation, of heap memory . . . . . . . . . . . . . . . . . . . . . 21free (standard library function) . . . . . . . . . . . . . . . . . . . . . . 20FSR2 (special function register) . . . . . . . . . . . . . . . . . 36, 182fstream (library header file) . . . . . . . . . . . . . . . . . . . . . . . 210fstream.h (library header file) . . . . . . . . . . . . . . . . . . . . . . 211__func__ (predefined symbol) . . . . . . . . . . . . . . . . . . . . . 196FUNCALL (compiler function directive) . . . . . . . . . . . 24, 97__FUNCTION__ (predefined symbol) . . . . . . . . . . . . . . . 196function directives. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24, 97Function inlining (compiler option) . . . . . . . . . . . . . . . . . 112function inlining, disabling . . . . . . . . . . . . . . . . . . . . . . . . 167function prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116function template parameter deduction (extended EC++) . 105function type information, omitting in object output. . . . . 169FUNCTION (compiler function directive) . . . . . . . . . . 24, 97functional (STL header file) . . . . . . . . . . . . . . . . . . . . . . . 211functions

C++ and special function types . . . . . . . . . . . . . . . . . . . 28declaring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92executing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11extended keywords. . . . . . . . . . . . . . . . . . . . . . . . . . . . 176inline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

CPIC18-3

247

Page 268: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

248

support for. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25intrinsic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25omitting type info . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93placing in segments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41recursive

avoiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116storing data on stack . . . . . . . . . . . . . . . . . . . . . . . . . 20

reentrancy (DLIB) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208return values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

stored on stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19special function types. . . . . . . . . . . . . . . . . . . . . . . . . . . 24template functions, memory-aware . . . . . . . . . . . . . . . 184

Ggetchar (CLIB library function) . . . . . . . . . . . . . . . . . . . . . 79getenv (DLIB library function), configuring support for. . . 65getzone (DLIB library function), configuring support for . . 66__get_interrupt_state (intrinsic function) . . . . . . . . . . . . . 203global variables

initializing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35glossary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiguidelines, reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii

Hhardware call stack, memory type of. . . . . . . . . . . . . . . . . . 30hardware extensions, support for . . . . . . . . . . . . . . . . . . . . . 4hash_map (STL header file) . . . . . . . . . . . . . . . . . . . . . . . 211hash_set (STL header file) . . . . . . . . . . . . . . . . . . . . . . . . 211header files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

alias for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187assert.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

ctype.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210–211EC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210errno.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213float.h. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213iccbutl.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213limits.h. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213math.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213setjmp.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213special function registers . . . . . . . . . . . . . . . . . . . . . . . 118stdarg.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213stdbool.h . . . . . . . . . . . . . . . . . . . . . . . . . . . 124, 209, 213stddef.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125, 213stdio.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213stdlib.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211string.h. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213using as templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

--header_context (compiler option). . . . . . . . . . . . . . . . . . 161heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

changing default size (command line) . . . . . . . . . . . . . . 37changing default size (IDE) . . . . . . . . . . . . . . . . . . . . . . 37size. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36–37size and standard I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . 38storing data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

HEAP (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37, 143placement of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

hidden parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93High (optimization level) . . . . . . . . . . . . . . . . . . . . . . . . . 110

I-I (compiler option). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161IAR Technical Support . . . . . . . . . . . . . . . . . . . . . . . . . . . 240iarbuild.exe (Command Line Build Utility) . . . . . . . . . . . . 54__IAR_SYSTEMS_ICC__ (predefined symbol) . . . . . . . 196iccbutl.h (library header file). . . . . . . . . . . . . . . . . . . . . . . 213__ICCPIC18__ (predefined symbol). . . . . . . . . . . . . . . . . 197ICODE (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 269: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Index

IDATA (segment memory type) . . . . . . . . . . . . . . . . . . . . . 30identifiers, implementation-defined behavior . . . . . . . . . . 216IEEE format, floating-point values . . . . . . . . . . . . . . . . . . 125implementation, system startup. . . . . . . . . . . . . . . . . . . . . . 72implementation-defined behavior . . . . . . . . . . . . . . . . . . . 215#include files, specifying . . . . . . . . . . . . . . . . . . . . . . . . . 161include_alias (pragma directive) . . . . . . . . . . . . . . . . . . . . 187INDF2 (special function register) . . . . . . . . . . . . . . . . . . . 182inheritance, in Embedded C++ . . . . . . . . . . . . . . . . . . . . . . 99initialization

dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56, 81variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

inline assembler . . . . . . . . . . . . . . . . . . . . . . . . . 87, 116, 231See also assembler language interface

inline functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116support for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

inline (pragma directive) . . . . . . . . . . . . . . . . . . . . . . . . . . 187inlining of functions, in compiler . . . . . . . . . . . . . . . . . . . 112instruction set, PIC18 . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiinteger characteristics, adding . . . . . . . . . . . . . . . . . . . . . . . 74integer types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126implementation-defined behavior. . . . . . . . . . . . . . . . . 218intptr_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127ptrdiff_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126size_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126uintptr_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

internal error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240__interrupt (extended keyword) . . . . . . . . . . . . . . . . . 25, 180

using in pragma directives . . . . . . . . . . . . . . . . . . . . . . 192interrupt functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

calling convention restrictions . . . . . . . . . . . . . . . . . . . . 95placement in memory. . . . . . . . . . . . . . . . . . . . . . . . . . . 39return type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

interrupt sources, supported . . . . . . . . . . . . . . . . . . . . . . . . 25interrupt vector table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

start address of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

interrupt vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39assembler-written. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95available. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180in linker command file . . . . . . . . . . . . . . . . . . . . . . . . . . 39specifying with pragma directive . . . . . . . . . . . . . . . . . 192

interruptsdisabling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

during function execution . . . . . . . . . . . . . . . . . . . . . 25when accessing EEPROM. . . . . . . . . . . . . . . . . . . . 179

high-priority. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180INTVEC segment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144low-priority . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180processor state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

interrupts and EC++ destructors, using . . . . . . . . . . . . . . . 107intptr_t (integer type) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127__intrinsic (extended keyword). . . . . . . . . . . . . . . . . . . . . 180intrinsic functions

hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201__clear_watchdog_timer . . . . . . . . . . . . . . . . . . . . . . . 202__disable_high_priority_interrupt . . . . . . . . . . . . . . . . 202__disable_interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . 202__disable_low_priority_interrupt. . . . . . . . . . . . . . . . . 202__disable_peripheral_interrupt . . . . . . . . . . . . . . . . . . 202__enable_high_priority_interrupt. . . . . . . . . . . . . . . . . 202__enable_interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202__enable_low_priority_interrupt . . . . . . . . . . . . . . . . . 203__enable_peripheral_interrupt . . . . . . . . . . . . . . . . . . . 203__get_interrupt_state . . . . . . . . . . . . . . . . . . . . . . . . . . 203__no_operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203__reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203__segment_begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203__segment_end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204__set_configuration_bits . . . . . . . . . . . . . . . . . . . . . . . 204__set_interrupt_state . . . . . . . . . . . . . . . . . . . . . . . . . . 205__sleep. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205__write_table_char. . . . . . . . . . . . . . . . . . . . . . . . . . . . 205__write_table_dptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

CPIC18-3

249

Page 270: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

250

__write_table_long . . . . . . . . . . . . . . . . . . . . . . . . . . . 205__write_table_short . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

Intrinsics.h (header file) . . . . . . . . . . . . . . . . . . . . . . . . . . 202INTVEC (segment). . . . . . . . . . . . . . . . . . . . . . . . . . . 39, 144iomanip (library header file) . . . . . . . . . . . . . . . . . . . . . . . 210iomanip.h (library header file) . . . . . . . . . . . . . . . . . . . . . 211ios (library header file) . . . . . . . . . . . . . . . . . . . . . . . . . . . 210iosfwd (library header file) . . . . . . . . . . . . . . . . . . . . . . . . 210iostream (library header file). . . . . . . . . . . . . . . . . . . . . . . 210iostream.h (library header file) . . . . . . . . . . . . . . . . . . . . . 211Io18xxxx.h (header file) . . . . . . . . . . . . . . . . . . . . . . . . . . 204ISO/ANSI C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7, 207

C++ features excluded from EC++ . . . . . . . . . . . . . . . . 99language extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . 229specifying strict usage . . . . . . . . . . . . . . . . . . . . . . . . . 172

iso646.h (library header file). . . . . . . . . . . . . . . . . . . . . . . 209istream (library header file). . . . . . . . . . . . . . . . . . . . . . . . 210iterator (STL header file) . . . . . . . . . . . . . . . . . . . . . . . . . 211I/O

customizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59low-level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

I/O library module, overriding . . . . . . . . . . . . . . . . . . . . . . 53

Kkeywords, extended. . . . . . . . . . . . . . . . . . . . . . . . . . . . 8, 176

L-l (compiler option). . . . . . . . . . . . . . . . . . . . . . . . . . . 89, 163language extensions

descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229Embedded C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99enabling using compiler option . . . . . . . . . . . . . . . . . . 159enabling using pragma directive. . . . . . . . . . . . . . . . . . 188

language (pragma directive) . . . . . . . . . . . . . . . . . . . . . . . 188LFSR instruction, disabling . . . . . . . . . . . . . . . . . . . . . . . 158

libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4customized. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55runtime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48, 77standard template library . . . . . . . . . . . . . . . . . . . . . . . 211

--library (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . 164library configuration file

modifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54–55option for specifying . . . . . . . . . . . . . . . . . . . . . . . . . . 158

library configurationsintroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

library documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207library features, missing from Embedded C++ . . . . . . . . . 100library functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

reference information. . . . . . . . . . . . . . . . . . . . . . . . . . xixCLIB

summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213getchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79putchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80sprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79sscanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80_formatted_write. . . . . . . . . . . . . . . . . . . . . . . . . . . . 79_medium_write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79_small_write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

DLIBsummary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209choosing printf formatter . . . . . . . . . . . . . . . . . . . . . 50choosing scanf formatter . . . . . . . . . . . . . . . . . . . . . . 51choosing sprintf formatter . . . . . . . . . . . . . . . . . . . . . 50choosing sscanf formatter . . . . . . . . . . . . . . . . . . . . . 51printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50remove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62rename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62sprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50__close . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62__lseek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62__open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 271: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Index

__read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62__write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

library modulescreating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164overriding with customized . . . . . . . . . . . . . . . . . . . . . . 52

library object files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208including (command line) . . . . . . . . . . . . . . . . . . . . . . . 49including (IDE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

library options, setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8library project template . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54--library_module (compiler option) . . . . . . . . . . . . . . . . . 164limits.h (library header file) . . . . . . . . . . . . . . . . . . . 209, 213__LINE__ (predefined symbol) . . . . . . . . . . . . . . . . . . . . 197linkage, C and C++. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92linker command files

contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30customizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31, 35, 37

code segments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38data segments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33interrupt vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39overlay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31using the -P command . . . . . . . . . . . . . . . . . . . . . . . . . . 32using the -Z command . . . . . . . . . . . . . . . . . . . . . . . . . . 32viewing default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

linker output, specifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5linking, from the command line . . . . . . . . . . . . . . . . . . . . . . 5list (STL header file). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211listing, generating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163literals, compound. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231literature, recommended . . . . . . . . . . . . . . . . . . . . . . . . . . xixlocale.h (library header file) . . . . . . . . . . . . . . . . . . . . . . . 209located data, placement of. . . . . . . . . . . . . . . . . . . . . . . . . . 38location (pragma directive) . . . . . . . . . . . . . . . . . . 40–41, 188LOCFRAME (compiler function directive) . . . . . . . . . 24, 97Loop unrolling (compiler option) . . . . . . . . . . . . . . . . . . . 111loop unrolling, disabling . . . . . . . . . . . . . . . . . . . . . . . . . . 168

loop-invariant expressions. . . . . . . . . . . . . . . . . . . . . . . . . 112Low (optimization level). . . . . . . . . . . . . . . . . . . . . . . . . . 110low-level features, accessing. . . . . . . . . . . . . . . . . . . . . . . . . 9low-level I/O, implementing . . . . . . . . . . . . . . . . . . . . . . . . 59low-level processor operations . . . . . . . . . . . . . . . . . . . . . 201__low_level_init, customizing . . . . . . . . . . . . . . . . . . . . . . 58__lseek (DLIB library function) . . . . . . . . . . . . . . . . . . . . . 62

Mmacros, variadic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199, 230malloc (standard library function). . . . . . . . . . . . . . . . . . . . 20map (STL header file) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211math.h (library header file) . . . . . . . . . . . . . . . . . 74, 209, 213Medium (optimization level). . . . . . . . . . . . . . . . . . . . . . . 110_medium_write (CLIB library function) . . . . . . . . . . . . . . . 79memory

accessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5allocating in Embedded C++ . . . . . . . . . . . . . . . . . . . . . 20banks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

specifying accesses . . . . . . . . . . . . . . . . . . . . . . . . . . 14dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20external, enabling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20non-initialized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118non-volatile . . . . . . . . . . . . . . . . . . . . . . . . . 134, 136, 138RAM, saving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116releasing in Embedded C++. . . . . . . . . . . . . . . . . . . . . . 20stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

saving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11static overlay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11used by executing functions . . . . . . . . . . . . . . . . . . . . . . 11used by global or static variables . . . . . . . . . . . . . . . . . . 11

memory attributes, hints using . . . . . . . . . . . . . . . . . . . . . 114memory management, type-safe . . . . . . . . . . . . . . . . . . . . . 99memory types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

CPIC18-3

251

Page 272: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

252

placing variables in . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16specifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

memory (STL header file). . . . . . . . . . . . . . . . . . . . . . . . . 211message (pragma directive). . . . . . . . . . . . . . . . . . . . . . . . 188MISRA C rules, enabling . . . . . . . . . . . . . . . . . . . . . . . . . 164--misrac (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . 164--misrac_verbose (compiler option) . . . . . . . . . . . . . . . . . 165module consistency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

rtmodel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190module name, specifying . . . . . . . . . . . . . . . . . . . . . . . . . 165MODULE (assembler directive) . . . . . . . . . . . . . . . . . . . . . 72modules

assembler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

--module_name (compiler option) . . . . . . . . . . . . . . . . . . 165__monitor (extended keyword) . . . . . . . . . . . . . . . . . . . . . 181monitor functions . . . . . . . . . . . . . . . . . . . . . . . . . 25, 95, 181multibyte character support. . . . . . . . . . . . . . . . . . . . . . . . 160multiple address spaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . 43multiple inheritance

missing from Embedded C++ . . . . . . . . . . . . . . . . . . . . 99multiple output files, from XLINK . . . . . . . . . . . . . . . . . . . 43mutable (attribute)

in Extended EC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106support for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

Nnamespace support

in Extended EC++ . . . . . . . . . . . . . . . . . . . . . . . . 100, 106missing from Embedded C++ . . . . . . . . . . . . . . . . . . . 100

name, specifying for object file. . . . . . . . . . . . . . . . . . . . . 168NDEBUG (preprocessor symbol) . . . . . . . . . . . . . . . . . . . . 67new (keyword) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20new (library header file) . . . . . . . . . . . . . . . . . . . . . . . . . . 210new.h (library header file) . . . . . . . . . . . . . . . . . . . . . . . . . 211

__nonbanked (extended keyword) . . . . . . . . . . . . . . . . . . 181nonbanked memory, overview . . . . . . . . . . . . . . . . . . . . . . 13None (optimization level) . . . . . . . . . . . . . . . . . . . . . . . . . 110non-initialized variables . . . . . . . . . . . . . . . . . . . . . . . . . . 119non-scalar parameters, avoiding . . . . . . . . . . . . . . . . . . . . 116non-volatile memory. . . . . . . . . . . . . . . . . . . . . 134, 136, 138NOP (assembler instruction) . . . . . . . . . . . . . . . . . . . . . . . 203__noreturn (extended keyword) . . . . . . . . . . . . . . . . . . . . 182

using in pragma directives . . . . . . . . . . . . . . . . . . . . . . 189--no_code_motion (compiler option) . . . . . . . . . . . . . . . . 166--no_cse (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . 166__no_init (extended keyword) . . . . . . . . . . . . . . . . . 119, 181

using in pragma directives . . . . . . . . . . . . . . . . . . . . . . 188--no_inline (compiler option) . . . . . . . . . . . . . . . . . . . . . . 167__no_operation (intrinsic function). . . . . . . . . . . . . . . . . . 203--no_tbaa (compiler option) . . . . . . . . . . . . . . . . . . . . . . . 167--no_typedefs_in_diagnostics (compiler option). . . . . . . . 167--no_unroll (compiler option) . . . . . . . . . . . . . . . . . . . . . . 168--no_warnings (compiler option) . . . . . . . . . . . . . . . . . . . 168--no_wrap_diagnostics (compiler option) . . . . . . . . . . . . . 168NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213numeric (STL header file). . . . . . . . . . . . . . . . . . . . . . . . . 211

O-o (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168-O (XLINK option). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43object filename, specifying . . . . . . . . . . . . . . . . . . . . . . . . 168object module name, specifying . . . . . . . . . . . . . . . . . . . . 165object_attribute (pragma directive) . . . . . . . . . . . . . . 119, 188offsetof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213--omit_types (compiler option) . . . . . . . . . . . . . . . . . . . . . 169--only_stdout (compiler option) . . . . . . . . . . . . . . . . . . . . 169__open (DLIB library function) . . . . . . . . . . . . . . . . . . . . . 62operators

& . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116@ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40–41__memory_of. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 273: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Index

optimizationcode motion, disabling . . . . . . . . . . . . . . . . . . . . . . . . . 166common sub-expression elimination, disabling . . . . . . 166configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6decreasing using pragma directive . . . . . . . . . . . . . . . . 189function inlining, disabling . . . . . . . . . . . . . . . . . . . . . 167hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115loop unrolling, disabling . . . . . . . . . . . . . . . . . . . . . . . 168size, specifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173speed, specifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111types and levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110type-based alias analysis . . . . . . . . . . . . . . . . . . . . . . . 112

optimize (pragma directive) . . . . . . . . . . . . . . . . . . . . . . . 189options summary, compiler . . . . . . . . . . . . . . . . . . . . . . . . 149ostream (library header file) . . . . . . . . . . . . . . . . . . . . . . . 210output

preprocessor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170specifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

output files, from XLINK . . . . . . . . . . . . . . . . . . . . . . . . . . . 5multiple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

output formats, COFF/COD . . . . . . . . . . . . . . . . . . . . . . . . 70overhead, call

eliminating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112overlay frames, contents of . . . . . . . . . . . . . . . . . . . . . . . . . 19overlay (code model) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23OVERLAY (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36OVERLAYn (segment) . . . . . . . . . . . . . . . . . . . . . . . . . . . 145overlay. See static overlay

P-P (XLINK option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32parameters

function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93hidden . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93non-scalar, avoiding . . . . . . . . . . . . . . . . . . . . . . . . . . . 116overlay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

specifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93–94typographic convention . . . . . . . . . . . . . . . . . . . . . . . . . xx

PIC18architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiinstruction set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviimemory access. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

placement of code and data. . . . . . . . . . . . . . . . . . . . . . . . 131pointers

casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114, 116implementation-defined behavior. . . . . . . . . . . . . . . . . 219stack, initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

polymorphism, in Embedded C++ . . . . . . . . . . . . . . . . . . . 99porting, code containing pragma directives. . . . . . . . . . . . 184_Pragma (predefined symbol) . . . . . . . . . . . . . . . . . . . . . . 197pragma directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184basic_template_matching. . . . . . . . . . . . . . . . . . . . . . . 184

using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105bitfields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125, 185constseg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185dataseg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185data_alignment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185diag_default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186diag_error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186diag_remark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186diag_suppress. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186diag_warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187include_alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187inline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40–41, 188message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188object_attribute . . . . . . . . . . . . . . . . . . . . . . . . . . 119, 188optimize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190rtmodel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

CPIC18-3

253

Page 274: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

254

segment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191type_attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25, 192

predefined symbolsoverview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194_Pragma. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197__ALIGNOF__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195__BASE_FILE__. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195__CODE_MODEL__ . . . . . . . . . . . . . . . . . . . . . . . . . 195__cplusplus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195__DATA_MODEL__ . . . . . . . . . . . . . . . . . . . . . . . . . . 195__DATE__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196__embedded_cplusplus . . . . . . . . . . . . . . . . . . . . . . . . 196__FILE__. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196__FUNCTION__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196__func__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196__IAR_SYSTEMS_ICC__ . . . . . . . . . . . . . . . . . . . . . 196__ICCPIC18__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197__LINE__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197__PRETTY_FUNCTION__ . . . . . . . . . . . . . . . . . . . . 197__STDC_VERSION__ . . . . . . . . . . . . . . . . . . . . . . . . 197__STDC__. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197__TID__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198__TIME__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198__VER__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

--preinclude (compiler option) . . . . . . . . . . . . . . . . . . . . . 169--preprocess (compiler option) . . . . . . . . . . . . . . . . . . . . . 170preprocessing directives, implementation-defined behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220preprocessor extensions

#warning message . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199__VA_ARGS__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199

preprocessor output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170preprocessor symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

defining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153prerequisites (programming experience) . . . . . . . . . . . . . . xvii__PRETTY_FUNCTION__ (predefined symbol). . . . . . . 197print formatter, selecting . . . . . . . . . . . . . . . . . . . . . . . . . . . 51printf (CLIB library function) . . . . . . . . . . . . . . . . . . . . . . . 79

printf (DLIB library function). . . . . . . . . . . . . . . . . . . . . . . 50choosing formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

processor operations, low-level . . . . . . . . . . . . . . . . . . . . . 201programming experience, required . . . . . . . . . . . . . . . . . . xviiprogramming hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115programming languages, overview . . . . . . . . . . . . . . . . . . . . 3prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116ptrdiff_t (integer type). . . . . . . . . . . . . . . . . . . . . . . . 126, 213PUBLIC (assembler directive) . . . . . . . . . . . . . . . . . . . . . 170--public_equ (compiler option) . . . . . . . . . . . . . . . . . . . . . 170putchar (CLIB library function) . . . . . . . . . . . . . . . . . . . . . 79

QQCCPIC18 (environment variable) . . . . . . . . . . . . . . . . . . 149qualifiers, implementation-defined behavior . . . . . . . . . . . 220queue (STL header file) . . . . . . . . . . . . . . . . . . . . . . . . . . 211

R-r (compiler option). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171raise (DLIB library function), configuring support for . . . . 65RAM memory

external, writing to. . . . . . . . . . . . . . . . . . . . . . . . . . . . 180saving. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

read formatter, selecting . . . . . . . . . . . . . . . . . . . . . . . . 52, 81__read (DLIB I/O function) . . . . . . . . . . . . . . . . . . . . . . . . 62

using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59reading guidelines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviireading, recommended . . . . . . . . . . . . . . . . . . . . . . . . . . . xixrealloc (standard library function). . . . . . . . . . . . . . . . . . . . 20recursive functions

avoiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116not supported by static overlay. . . . . . . . . . . . . . . . . . . . 20storing data on stack . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

reentrancylibrary support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208not supported by static overlay. . . . . . . . . . . . . . . . . . . . 20

reference information, typographic convention. . . . . . . . . . xx

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 275: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Index

registered trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iiregisters

callee-save, stored on stack . . . . . . . . . . . . . . . . . . . . . . 19implementation-defined behavior. . . . . . . . . . . . . . . . . 219usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

reinterpret_cast (cast operator) . . . . . . . . . . . . . . . . . . . . . 100remark (diagnostic message)

classifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156enabling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

--remarks (compiler option) . . . . . . . . . . . . . . . . . . . . . . . 171remarks (diagnostic message) . . . . . . . . . . . . . . . . . . . . . . 239remove (DLIB library function) . . . . . . . . . . . . . . . . . . . . . 62rename (DLIB library function) . . . . . . . . . . . . . . . . . . . . . 62REQUIRE (assembler directive) . . . . . . . . . . . . . . . . . . . . . 72required (pragma directive). . . . . . . . . . . . . . . . . . . . . . . . 190--require_prototypes (compiler option) . . . . . . . . . . . . . . . 171__reset (intrinsic function) . . . . . . . . . . . . . . . . . . . . . . . . 203RESET (assembler instruction). . . . . . . . . . . . . . . . . . . . . 203return address handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95return values, from functions . . . . . . . . . . . . . . . . . . . . . . . 94__root (extended keyword) . . . . . . . . . . . . . . . . . . . . . . . . 182

using in pragma directives . . . . . . . . . . . . . . . . . . . . . . 189routines, time-critical . . . . . . . . . . . . . . . . . . . . . . . . . 85, 201RSEG (assembler directive) . . . . . . . . . . . . . . . . . . . . . . . . 72RTMODEL (assembler directive) . . . . . . . . . . . . . . . . . . . . 69rtmodel (pragma directive) . . . . . . . . . . . . . . . . . . . . . . . . 190rtti support, missing from STL . . . . . . . . . . . . . . . . . . . . . 100__rt_version (runtime model attribute) . . . . . . . . . . . . . . . . 70runtime environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

customizing using template . . . . . . . . . . . . . . . . . . . . . . 54DLIB

overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45runtime environment options, setting . . . . . . . . . . . . . . . . . . 8runtime libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48, 77

choosing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7DLIB

debug support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

naming conventionCLIB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78DLIB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

summaryCLIB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78DLIB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

runtime model attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . 69__code_model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70__cpu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70__data_model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70__eeprom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70__rt_version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

runtime type information, missing from Embedded C++ . . 99

S-s (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171scanf (CLIB library function) . . . . . . . . . . . . . . . . . . . . . . . 80scanf (DLIB library function)

choosing formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51segment memory types, in XLINK . . . . . . . . . . . . . . . . . . . 30segment parts

specifying in linker command file . . . . . . . . . . . . . . . . . 31unused . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

segment (pragma directive). . . . . . . . . . . . . . . . . . . . . . . . 191segments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29naming convention. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33packed placement of . . . . . . . . . . . . . . . . . . . . . . . . . . . 32placing in memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30sequential placement of . . . . . . . . . . . . . . . . . . . . . . . . . 32static memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131BANKN_A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137BANKn_A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133BANKN_I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137BANKn_I. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133BANKN_ID. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137BANKn_ID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

CPIC18-3

255

Page 276: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

256

BANKN_N . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138BANKn_N . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134BANKN_Z. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138BANKn_Z . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134BANK_A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135BANK_I. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135BANK_ID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135BANK_N . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136BANK_Z . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136BANK_ZD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136CODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39, 138CONST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139CSTACK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139CSTART . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39DIFUNCT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39, 139EEPROM_A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140EEPROM_I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140EEPROM_ID. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140EEPROM_N . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141EEPROM_Z. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141EXTMEM_A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141EXTMEM_I. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142EXTMEM_ID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142EXTMEM_N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143EXTMEM_Z . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143HEAP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37, 143ICODE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144INTVEC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39, 144OVERLAY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36OVERLAYn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145STACK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36WRKSEG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

__segment_begin (intrinsic function) . . . . . . . . . . . . . . . . 203__segment_end (intrinsic function). . . . . . . . . . . . . . . . . . 204semaphores

implementing in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26implementing in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

operations on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181set (STL header file) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211setjmp.h (library header file). . . . . . . . . . . . . . . . . . . 209, 213settings, basic for project configuration . . . . . . . . . . . . . . . . 5__set_configuration_bits (intrinsic function) . . . . . . . . . . 204__set_interrupt_state (intrinsic function) . . . . . . . . . . . . . 205severity level, of diagnostic messages . . . . . . . . . . . . . . . . 239

specifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240SFR (special function registers) . . . . . . . . . . . . . . . . . . . . 118signal (DLIB library function), configuring support for . . . 65signal.h (library header file) . . . . . . . . . . . . . . . . . . . . . . . 209signed char (data type) . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

specifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152signed int (data type). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124signed long (data type) . . . . . . . . . . . . . . . . . . . . . . . . . . . 124signed short (data type). . . . . . . . . . . . . . . . . . . . . . . . . . . 124--silent (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . 172silent operation, specifying . . . . . . . . . . . . . . . . . . . . . . . . 172size optimization, specifying. . . . . . . . . . . . . . . . . . . . . . . 173size_t (integer type) . . . . . . . . . . . . . . . . . . . . . . . . . 126, 213skeleton code, creating for assembler language interface . . 88__sleep (intrinsic function) . . . . . . . . . . . . . . . . . . . . . . . . 205SLEEP (assembler instruction) . . . . . . . . . . . . . . . . . . . . . 205slist (STL header file) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211_small_write (CLIB library function) . . . . . . . . . . . . . . . . . 79source files, list all referred. . . . . . . . . . . . . . . . . . . . . . . . 161special function registers (SFR) . . . . . . . . . . . . . . . . . . . . 118special function types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9speed optimization, specifying . . . . . . . . . . . . . . . . . . . . . 171sprintf (CLIB library function) . . . . . . . . . . . . . . . . . . . . . . 79sprintf (DLIB library function) . . . . . . . . . . . . . . . . . . . . . . 50

choosing formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50sscanf (CLIB library function) . . . . . . . . . . . . . . . . . . . . . . 80sscanf (DLIB library function), choosing formatter . . . . . . 51sstream (library header file) . . . . . . . . . . . . . . . . . . . . . . . 210stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

advantages and problems using . . . . . . . . . . . . . . . . . . . 19changing default size (in IDE) . . . . . . . . . . . . . . . . . . . . 36

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 277: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Index

changing default size (on command line). . . . . . . . . . . . 36contents of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19function usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11internal data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93saving space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116size. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

stack calling conventionhints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

stack parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94stack pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

initialization of. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57stack (code model) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23STACK (segment). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36placing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

stack (STL header file) . . . . . . . . . . . . . . . . . . . . . . . . . . . 211__stack_func (extended keyword). . . . . . . . . . . . . . . . . . . 182standard error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169standard output, specifying . . . . . . . . . . . . . . . . . . . . . . . . 169standard template library (STL)

in Extended EC++ . . . . . . . . . . . . . . . . . . . . 100, 106, 211missing from Embedded C++ . . . . . . . . . . . . . . . . . . . 100

startup codeplacement of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39See also CSTART

startup sequence, overview . . . . . . . . . . . . . . . . . . . . . . . . . 56startup, system

in CLIB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81in DLIB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

statements, implementation-defined behavior . . . . . . . . . . 220static memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

segments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33static overlay

advantages and problems using . . . . . . . . . . . . . . . . . . . 19auto variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20function directives . . . . . . . . . . . . . . . . . . . . . . . . . . 24, 97

memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

static variablesinitializing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

static_cast (cast operator) . . . . . . . . . . . . . . . . . . . . . . . . . 100std namespace, missing from EC++and Extended EC++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107stdarg.h (library header file) . . . . . . . . . . . . . . . . . . . 209, 213stdbool.h (library header file) . . . . . . . . . . . 74, 124, 209, 213__STDC__ (predefined symbol) . . . . . . . . . . . . . . . . . . . . 197__STDC_VERSION__ (predefined symbol) . . . . . . . . . . 197stddef.h (library header file) . . . . . . . . . . . . . . . 125, 209, 213stderr. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62, 169

customizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59stdexcept (library header file) . . . . . . . . . . . . . . . . . . . . . . 210stdin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

customizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59stdint.h (library header file). . . . . . . . . . . . . . . . . . . . . . . . . 74stdio.h (library header file) . . . . . . . . . . . . . . . . . 74, 209, 213stdlib.h (library header file). . . . . . . . . . . . . . . . . 75, 209, 213stdout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62, 169

customizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59STL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106streambuf (library header file). . . . . . . . . . . . . . . . . . . . . . 210streams, supported in Embedded C++. . . . . . . . . . . . . . . . 100--strict_ansi (compiler option). . . . . . . . . . . . . . . . . . . . . . 172string (library header file) . . . . . . . . . . . . . . . . . . . . . . . . . 210strings, supported in Embedded C++ . . . . . . . . . . . . . . . . 100string.h (library header file) . . . . . . . . . . . . . . . . . . . 210, 213strstream (library header file) . . . . . . . . . . . . . . . . . . . . . . 210strtod (DLIB library function), configuring support for . . . 66structure types, layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127structures

anonymous. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114implementation-defined behavior. . . . . . . . . . . . . . . . . 219placing in memory type . . . . . . . . . . . . . . . . . . . . . . . . . 17

support, technical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240symbols

anonymous, creating . . . . . . . . . . . . . . . . . . . . . . . . . . 231overview of predefined. . . . . . . . . . . . . . . . . . . . . . . . . . . 9

CPIC18-3

257

Page 278: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

258

preprocessor, defining . . . . . . . . . . . . . . . . . . . . . . . . . 153syntax

compiler options . . . . . . . . . . . . . . . . . . . . . . . . . 147–148compound literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231diagnostic messages . . . . . . . . . . . . . . . . . . . . . . . 215, 239extended keywords. . . . . . . . . . . . . . . . . . . . . . . . . . 15, 24function directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97inline assembler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231pragma directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

system startupCLIB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81DLIB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72system termination

CLIB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81DLIB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56system (DLIB library function), configuring support for . . 65

Ttechnical support, IAR . . . . . . . . . . . . . . . . . . . . . . . . . . . 240template functions

memory-aware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184template support

in Extended EC++ . . . . . . . . . . . . . . . . . . . . . . . . 100, 104missing from Embedded C++ . . . . . . . . . . . . . . . . . . . . 99

Terminal I/O window, in C-SPY . . . . . . . . . . . . . . . . . . . . . 82termination, system

in CLIB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81in DLIB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56terminology. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii, xx32-bits (floating-point format) . . . . . . . . . . . . . . . . . . . . . 125this (pointer) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

referring to a class object (extended EC++) . . . . . . . . . 101__TID__ (predefined symbol). . . . . . . . . . . . . . . . . . . . . . 198__TIME__ (predefined symbol) . . . . . . . . . . . . . . . . . . . . 198time (DLIB library function), configuring support for . . . . 66

time-critical routines . . . . . . . . . . . . . . . . . . . . . . . . . . 85, 201time.h (library header file) . . . . . . . . . . . . . . . . . . . . . . . . 210trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iitransformations, optimization . . . . . . . . . . . . . . . . . . . . . . 111translation, implementation-defined behavior . . . . . . . . . . 215type information, omitting . . . . . . . . . . . . . . . . . . . . . . . . 169Type-based alias analysis (compiler option) . . . . . . . . . . . 112type-safe memory management . . . . . . . . . . . . . . . . . . . . . 99type_attribute (pragma directive) . . . . . . . . . . . . . . . . . . . 191typographic conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . xx

Uuintptr_t (integer type) . . . . . . . . . . . . . . . . . . . . . . . . . . . 127unions

anonymous. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114implementation-defined behavior. . . . . . . . . . . . . . . . . 219

unsigned char (data type) . . . . . . . . . . . . . . . . . . . . . . . . . 124changing to signed char . . . . . . . . . . . . . . . . . . . . . . . . 152

unsigned int (data type). . . . . . . . . . . . . . . . . . . . . . . . . . . 124unsigned long (data type) . . . . . . . . . . . . . . . . . . . . . . . . . 124unsigned short (data type) . . . . . . . . . . . . . . . . . . . . . . . . . 124utility (STL header file) . . . . . . . . . . . . . . . . . . . . . . . . . . 211

Vvariable type information, omitting in object output . . . . . 169variables

auto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19preferring over global . . . . . . . . . . . . . . . . . . . . . . . 115

constants, in named segments . . . . . . . . . . . . . . . . . . . 185defined inside a function . . . . . . . . . . . . . . . . . . . . . . . . 19global, placement in memory. . . . . . . . . . . . . . . . . . . . . 11in named segments. . . . . . . . . . . . . . . . . . . . . . . . . . . . 185initializing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35local. See auto variablesnon-initialized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119omitting type info . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169placing at absolute addresses . . . . . . . . . . . . . . . . . . . . . 40

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 279: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Index

placing in named segments . . . . . . . . . . . . . . . . . . . . . . 40placing in segments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40simultaneously accessed . . . . . . . . . . . . . . . . . . . . . . . 118static, placement in memory . . . . . . . . . . . . . . . . . . . . . 11

variadic macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230vector (pragma directive) . . . . . . . . . . . . . . . . . . . . . . 25, 192vector (STL header file) . . . . . . . . . . . . . . . . . . . . . . . . . . 211vectors, interrupt. See interrupt vectors__VER__ (predefined symbol) . . . . . . . . . . . . . . . . . . . . . 198version, of compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198volatile (keyword), using . . . . . . . . . . . . . . . . . . . . . . . . . 118volatile, declaring objects . . . . . . . . . . . . . . . . . . . . . . . . . 129

W#warning message (preprocessor extension) . . . . . . . . . . . 199warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

classifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157disabling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168exit code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

--warnings_affect_exit_code (compiler option) . . . . . . . . 172--warnings_are_errors (compiler option) . . . . . . . . . . . . . 173wchar.h (library header file) . . . . . . . . . . . . . . . . . . . . . . . 210wchar_t (data type), adding support for in C. . . . . . . . . . . 125wctype.h (library header file) . . . . . . . . . . . . . . . . . . . . . . 210web sites, recommended . . . . . . . . . . . . . . . . . . . . . . . . . . . xx--workaround_pic18fx5x (compiler option) . . . . . . . . . . . 173__write (DLIB I/O function). . . . . . . . . . . . . . . . . . . . . . . . 62write formatter, selecting . . . . . . . . . . . . . . . . . . . . . . . . . . 80__write (DLIB I/O function)

using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59__write_table_char (intrinsic function) . . . . . . . . . . . . . . . 205__write_table_dptr (intrinsic function) . . . . . . . . . . . . . . . 205__write_table_long (intrinsic function). . . . . . . . . . . . . . . 205__write_table_short (intrinsic function) . . . . . . . . . . . . . . 205WRKSEG (segment). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

XXDATA (segment memory type). . . . . . . . . . . . . . . . . . . . . 30XLINK options

-D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-O. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43-P . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43-Z . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

XLINK output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5XLINK segment memory types . . . . . . . . . . . . . . . . . . . . . 30

Y-y (XLINK option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

Z-z (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173-Z (XLINK option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

Symbols#include files, specifying . . . . . . . . . . . . . . . . . . . . . . . . . 161#warning message (preprocessor extension) . . . . . . . . . . . 199& (operator) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116-D (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153-D (XLINK option). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31-e (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159-f (compiler option). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161-I (compiler option). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161-l (compiler option). . . . . . . . . . . . . . . . . . . . . . . . . . . 89, 163-o (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168-O (XLINK option). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43-P (XLINK option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32-r (compiler option). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171-s (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171-y (XLINK option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43-z (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

CPIC18-3

259

Page 280: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

260

-Z (XLINK option) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32--char_is_signed (compiler option) . . . . . . . . . . . . . . . . . . 152--code_model (compiler option) . . . . . . . . . . . . . . . . . . . . 152--data_model (compiler option) . . . . . . . . . . . . . . . . . . . . 154--debug (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . 155--dependencies (compiler option) . . . . . . . . . . . . . . . . . . . 155--diagnostics_tables (compiler option) . . . . . . . . . . . . . . . 157--diag_error (compiler option) . . . . . . . . . . . . . . . . . . . . . 156--diag_remark (compiler option). . . . . . . . . . . . . . . . . . . . 156--diag_suppress (compiler option) . . . . . . . . . . . . . . . . . . 157--diag_warning (compiler option) . . . . . . . . . . . . . . . . . . . 157--disable_fast_return (compiler option). . . . . . . . . . . . . . . 158--disable_lfsr (compiler option) . . . . . . . . . . . . . . . . . . . . 158--dlib_config (compiler option). . . . . . . . . . . . . . . . . . . . . 158--ec++ (compiler option). . . . . . . . . . . . . . . . . . . . . . . . . . 159--eec++ (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . 159--eeprom_size (compiler option). . . . . . . . . . . . . . . . . . . . 160--enable_multibytes (compiler option) . . . . . . . . . . . . . . . 160--error_limit (compiler option) . . . . . . . . . . . . . . . . . . . . . 160--extmem (compiler option) . . . . . . . . . . . . . . . . . . . . . . . 160--header_context (compiler option). . . . . . . . . . . . . . . . . . 161--library (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . 164--library_module (compiler option) . . . . . . . . . . . . . . . . . 164--misrac (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . 164--misrac_verbose (compiler option) . . . . . . . . . . . . . . . . . 165--module_name (compiler option) . . . . . . . . . . . . . . . . . . 165--no_code_motion (compiler option) . . . . . . . . . . . . . . . . 166--no_cse (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . 166--no_inline (compiler option) . . . . . . . . . . . . . . . . . . . . . . 167--no_tbaa (compiler option) . . . . . . . . . . . . . . . . . . . . . . . 167--no_typedefs_in_diagnostics (compiler option). . . . . . . . 167--no_unroll (compiler option) . . . . . . . . . . . . . . . . . . . . . . 168--no_warnings (compiler option) . . . . . . . . . . . . . . . . . . . 168--no_wrap_diagnostics (compiler option) . . . . . . . . . . . . . 168--omit_types (compiler option) . . . . . . . . . . . . . . . . . . . . . 169--only_stdout (compiler option) . . . . . . . . . . . . . . . . . . . . 169--preinclude (compiler option) . . . . . . . . . . . . . . . . . . . . . 169--preprocess (compiler option) . . . . . . . . . . . . . . . . . . . . . 170--remarks (compiler option) . . . . . . . . . . . . . . . . . . . . . . . 171

--require_prototypes (compiler option) . . . . . . . . . . . . . . . 171--silent (compiler option) . . . . . . . . . . . . . . . . . . . . . . . . . 172--strict_ansi (compiler option). . . . . . . . . . . . . . . . . . . . . . 172--warnings_affect_exit_code (compiler option) . . . . 149, 172--warnings_are_errors (compiler option) . . . . . . . . . . . . . 173--workaround_workaround_pic18fx5x (compiler option) . 173?C_EXIT (assembler label). . . . . . . . . . . . . . . . . . . . . . . . . 83?C_GETCHAR (assembler label) . . . . . . . . . . . . . . . . . . . . 82?C_PUTCHAR (assembler label) . . . . . . . . . . . . . . . . . . . . 82@ (extended keyword) . . . . . . . . . . . . . . . . . . . . . . . . . . . 177@ (operator) . . . . . . . . . . . . . . . . . . . . . 40–41, 133, 135, 137_Exit (exit function) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57_exit (exit function) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57_formatted_write (CLIB library function). . . . . . . . . . . . . . 79_medium_write (CLIB library function) . . . . . . . . . . . . . . . 79_Pragma (predefined symbol) . . . . . . . . . . . . . . . . . . . . . . 197_small_write (CLIB library function) . . . . . . . . . . . . . . . . . 79__ALIGNOF__ (predefined symbol) . . . . . . . . . . . . . . . . 195__asm (extended keyword) . . . . . . . . . . . . . . . . . . . . . . . . 177__bank (extended keyword) . . . . . . . . . . . . . . . . . . . . . . . 178__bankN (extended keyword) . . . . . . . . . . . . . . . . . . . . . . 178__bankN_func (extended keyword) . . . . . . . . . . . . . . . . . 179__bank_func (extended keyword) . . . . . . . . . . . . . . . . . . . 178__BASE_FILE__ (predefined symbol) . . . . . . . . . . . . . . . 195__clear_watchdog_timer (intrinsic function) . . . . . . . . . . 202__close (DLIB library function) . . . . . . . . . . . . . . . . . . . . . 62__code_model (runtime model attribute) . . . . . . . . . . . . . . 70__CODE_MODEL__ (predefined symbol). . . . . . . . . . . . 195__constptr (extended keyword) . . . . . . . . . . . . . . . . . . . . . 179__cplusplus (predefined symbol) . . . . . . . . . . . . . . . . . . . 195__cpu (runtime model attribute) . . . . . . . . . . . . . . . . . . . . . 70__data_model (runtime model attribute) . . . . . . . . . . . . . . . 70__DATA_MODEL__ (predefined symbol) . . . . . . . . . . . . 195__DATE__ (predefined symbol) . . . . . . . . . . . . . . . . . . . . 196__disable_high_priority_interrupt (intrinsic function) . . . 202__disable_interrupt (intrinsic function). . . . . . . . . . . . . . . 202__disable_low_priority_interrupt (intrinsic function) . . . . 202__disable_peripheral_interrupt (intrinsic function) . . . . . . 202__dptr (extended keyword) . . . . . . . . . . . . . . . . . . . . . . . . 179

CPIC18-3

PIC18 IAR C/C++ CompilerReference Guide

Page 281: PIC18 IAR C/C++ Compilerguppy.mpe.nus.edu.sg/me3/IARpic18CRefGuide.pdfWelcome to the PIC18 IAR C/C++ Compiler Reference Guide. The purpose of this guide is to provide you with detailed

Index

__eeprom (extended keyword) . . . . . . . . . . . . . . . . . . . . . 179__eeprom (runtime model attribute) . . . . . . . . . . . . . . . . . . 70__embedded_cplusplus (predefined symbol) . . . . . . . . . . 196__enable_high_priority_interrupt (intrinsic function) . . . . 202__enable_interrupt (intrinsic function) . . . . . . . . . . . . . . . 202__enable_low_priority_interrupt (intrinsic function) . . . . 203__enable_peripheral_interrupt (intrinsic function) . . . . . . 203__extmem (extended keyword) . . . . . . . . . . . . . . . . . . . . . 180__FILE__ (predefined symbol). . . . . . . . . . . . . . . . . . . . . 196__FUNCTION__ (predefined symbol) . . . . . . . . . . . . . . . 196__func__ (predefined symbol) . . . . . . . . . . . . . . . . . . . . . 196__get_interrupt_state (intrinsic function) . . . . . . . . . . . . . 203__IAR_SYSTEMS_ICC__ (predefined symbol) . . . . . . . 196__ICCPIC18__ (predefined symbol). . . . . . . . . . . . . . . . . 197__interrupt (extended keyword) . . . . . . . . . . . . . . . . . 25, 180

using in pragma directives . . . . . . . . . . . . . . . . . . . . . . 192__intrinsic (extended keyword). . . . . . . . . . . . . . . . . . . . . 180__LINE__ (predefined symbol) . . . . . . . . . . . . . . . . . . . . 197__low_level_init, customizing . . . . . . . . . . . . . . . . . . . . . . 58__lseek (DLIB library function) . . . . . . . . . . . . . . . . . . . . . 62__memory_of, operator. . . . . . . . . . . . . . . . . . . . . . . . . . . 103__monitor (extended keyword) . . . . . . . . . . . . . . . . . . . . . 181__nonbanked (extended keyword) . . . . . . . . . . . . . . . . . . 181__noreturn (extended keyword) . . . . . . . . . . . . . . . . . . . . 182

using in pragma directives . . . . . . . . . . . . . . . . . . . . . . 189__no_init (extended keyword) . . . . . . . . . . . . . . . . . 119, 181

using in pragma directives . . . . . . . . . . . . . . . . . . . . . . 188__no_operation (intrinsic function). . . . . . . . . . . . . . . . . . 203__open (DLIB library function) . . . . . . . . . . . . . . . . . . . . . 62__PRETTY_FUNCTION__ (predefined symbol). . . . . . . 197__read (DLIB I/O function) . . . . . . . . . . . . . . . . . . . . . . . . 62

using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59__reset (intrinsic function) . . . . . . . . . . . . . . . . . . . . . . . . 203__root (extended keyword) . . . . . . . . . . . . . . . . . . . . . . . . 182

using in pragma directives . . . . . . . . . . . . . . . . . . . . . . 189__rt_version (runtime model attribute) . . . . . . . . . . . . . . . . 70__segment_begin (intrinsic function) . . . . . . . . . . . . . . . . 203__segment_end (intrinsic function). . . . . . . . . . . . . . . . . . 204__set_configuration_bits (intrinsic function) . . . . . . . . . . 204

__set_interrupt_state (intrinsic function) . . . . . . . . . . . . . 205__sleep (intrinsic function) . . . . . . . . . . . . . . . . . . . . . . . . 205__stack_func (extended keyword). . . . . . . . . . . . . . . . . . . 182__STDC_VERSION__ (predefined symbol) . . . . . . . . . . 197__STDC__ (predefined symbol) . . . . . . . . . . . . . . . . . . . . 197__TID__ (predefined symbol). . . . . . . . . . . . . . . . . . . . . . 198__TIME__ (predefined symbol) . . . . . . . . . . . . . . . . . . . . 198__VA_ARGS__ (preprocessor extension). . . . . . . . . . . . . 199__VER__ (predefined symbol) . . . . . . . . . . . . . . . . . . . . . 198__write (DLIB I/O function). . . . . . . . . . . . . . . . . . . . . . . . 62

using . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59__write_table_char (intrinsic function) . . . . . . . . . . . . . . . 205__write_table_dptr (intrinsic function) . . . . . . . . . . . . . . . 205__write_table_long (intrinsic function). . . . . . . . . . . . . . . 205__write_table_short (intrinsic function) . . . . . . . . . . . . . . 205

Numerics32-bits (floating-point format) . . . . . . . . . . . . . . . . . . . . . 125

CPIC18-3

261