MPLAB C18 C COMPILER USER?S GUIDE | PDF syudy material

This document discusses the technical details of the MPLAB® C18 compiler. This document will explain all functionality of the MPLAB C18 compiler. It assumes that the programmer already:
• knows how to write C programs • knows how to use the MPLAB Integrated Development Environment (IDE) to create and debug projects • has read and understands the processor data sheet for which code is being written

DOCUMENT LAYOUT
This document layout is as follows:
• Chapter 1. Introduction – Provides an overview of the MPLAB C18 compiler and
information on invoking the compiler.
• Chapter 2. Language Specifics – Discusses how the MPLAB C18 compiler
differs from the ANSI standard.
• Chapter 3. Run-time Model – Discusses how the MPLAB C18 compiler utilizes
the resources of the PIC18 PICmicro® microcontrollers.
• Chapter 4. Optimizations – Discusses the optimizations that are performed by
the MPLAB C18 compiler.
• Chapter 5. Examples – Provides several sample applications and describes the
source code with references to the specific topics discussed in the User’s Guide.

Glossary

A


Absolute Section

A section with a fixed address
that cannot be changed by the linker.

Access Memory


Special general purpose registers on the PIC18 PICmicro
microcontrollers that allow access regardless of the setting of the Bank Select
Register (
BSR).

Address

The code that identifies where a piece of information is stored
in memory.

Anonymous Structure

An unnamed object.

ANSI

American National Standards Institute

Assembler

A language tool that translates assembly source code into
machine code.

Assembly

A symbolic language that describes the binary machine code in a
readable form.

Assigned Section

A section that has been assigned to a target memory block in the
linker command file.

Asynchronously

Multiple events that do not occur at the same time. This is
generally used to refer to interrupts that may occur at any time during
processor execution.

B

Binary


The base two numbering system that uses the digits 0-1.
The rightmost digit counts ones, the next counts multiples of 2, then 2
2
= 4, etc.

C

Central Processing Unit

The part of a device that is responsible for fetching the
correct instruction for execution, decoding that instruction, and then executing
that instruction. When necessary, it works in conjunction with the Arithmetic
Logic Unit (ALU) to complete the execution of the instruction. It controls the
program memory address bus, the data memory address bus and accesses to the
stack.

Compiler

A program that translates a source file written in a high-level
language into machine code.

Conditional Compilation

The act of compiling a program fragment only if a certain
constant expression, specified by a preprocessor directive, is true.

CPU

Central Processing Unit

E

Endianness

The ordering of bytes in a multi-byte object.

Error File

A file containing the diagnostics generated by the MPLAB C18
compiler.

Extended Mode

In Extended mode, the compiler will utilize the extended
instructions (i.e., ADDFSR,
ADDULNK
,
CALLW,
MOVSF,
MOVSS,
PUSHL,
SUBFSR
and SUBULNK)
and the indexed with literal offset addressing.

F

Fatal Error

An error that will halt compilation immediately. No further
messages will be produced.

Frame Pointer

A pointer that references the location on the stack that
separates the stack-based arguments from the stack-based local variables.

Free-standing

An implementation that accepts any strictly conforming program
that does not use complex types and in which the use of the features specified
in the library clause (ANSI ‘89 standard clause 7) is confined to the contents
of the standard headers ,
,
,
,
,

and .

H

Hexadecimal

The base 16 numbering system that uses the digits 0-9
plus the letters A-F (or a-f).The
digits A-F represent decimal values of 10
to 15. The rightmost digit counts ones, the next counts multiples of 16, then 162
= 256, etc.

High-level Language

A language for writing programs that is further removed from the
processor than assembly.

I

ICD

In-Circuit Debugger

ICE

In-Circuit Emulator

IDE

Integrated Development Environment

IEEE

Institute of Electrical and Electronics Engineers

Interrupt

A signal to the CPU that suspends the execution of a running
application and transfers control to an ISR so that the event may be processed.
Upon completion of the ISR, normal execution of the application resumes.

Interrupt Service Routine

A function that handles an interrupt.

ISO

International Organization for Standardization

ISR

Interrupt Service Routine

L

Latency

The time between when an event occurs and the response to it.

Librarian

A program that creates and manipulates libraries.

Library

A collection of relocatable object modules.

Linker

A program that combines object files and libraries to create
executable code.

Little Endian

Within a given object, the least significant byte is stored at
lower addresses.

M

Memory Model

A description that specifies the size of pointers that point to
program memory.

Microcontroller

A highly integrated chip that contains a CPU, RAM, some form of
ROM, I/O ports and timers.

MPASM Assembler

Microchip Technology’s relocatable macro assembler for PICmicro
microcontroller families.

MPLIB Object Librarian

Microchip Technology’s librarian for PICmicro microcontroller
families.

MPLINK Object Linker

Microchip Technology’s linker for PICmicro microcontroller
families.

N

Non-Extended Mode

In Non-Extended mode, the compiler will not utilize the extended
instructions nor the indexed with literal offset addressing.

O

Object File

A file containing object code. It may be immediately executable
or it may require linking with other object code files, (e.g. libraries), to
produce a complete executable program.

Object Code

The machine code generated by an assembler or compiler.

Octal

The base 8 number system that only uses the digits 0-7.
The rightmost digit counts ones, the next digit counts multiples of 8, then 82
= 64, etc.

P

Pragma

A directive that has meaning to a specific compiler.

R

RAM

Random Access Memory

Random Access Memory

A memory device in which information can be accessed in any
order.

Read Only Memory

Memory hardware that allows fast access to permanently stored
data but prevents addition to or modification of the data.

ROM

Read Only Memory

Recursive

Self-referential (e.g., a function that calls itself).

Reentrant

A function that may have multiple, simultaneously active
instances. This may happen due to either direct or indirect recursion or through
execution during interrupt processing.

Relocatable

An object whose address has not been assigned to a fixed memory
location.

Run-time Model

Set of assumptions under which the compiler operates.

S

Section

A portion of an application located at a specific address of
memory.

Section Attribute

A characteristic ascribed to a section (e.g., an
access section).

Special Function Register

Registers that control I/O processor functions, I/O status,
timers or other modes or peripherals.

Storage Class

Determines the lifetime of the memory associated with the
identified object.

Storage Qualifier

Indicates special properties of the objects being declared
(e.g., const).

V

Vector

The memory locations that an application will jump to when
either a reset or interrupt occurs.

Table of Contents

Preface .. 1

Chapter 1. Introduction


1.1 Overview.. 7

1.2 Invoking the Compiler ………………… 7

1.2.1 Creating Output Files …………………… 8

1.2.2 Displaying Diagnostics………………….
8

1.2.3 Defining Macros ………………………….. 9

1.2.4 Selecting the Processor ……………….. 9

1.2.5 Selecting the Mode ……………………… 9

Chapter 2. Language Specifics


2.1 Data Types and Limits ……………… 11

2.1.1 Integer Types ……………………………. 11

2.1.2 Floating-point Types ………………….. 11

2.2 Data Type Storage – Endianness . 12

2.3 Storage Classes………………………. 12

2.3.1 Overlay….. 12

2.3.2
static
Function Arguments
………. 13

2.4 Storage Qualifiers ……………………. 14

2.4.1
near/far
Data Memory Objects
… 14

2.4.2
near/far
Program Memory
Objects………………. 14

2.4.3
ram/rom

Qualifiers……………………. 14

2.5 Include File Search Paths …………. 15

2.5.1 System Header Files………………….. 15

2.5.2 User Header Files ……………………… 15

2.6 Predefined Macro Names …………. 15

2.7 ISO Divergences……………………… 15

2.7.1 Integer Promotions…………………….. 15

2.7.2 Numeric Constants ……………………. 16

2.7.3 String Constants
……………………….. 16

2.7.4
stdio.h
Functions
…………………… 18

2.8 Language Extensions ………………. 18

2.8.1 Anonymous Structures……………….. 18

2.8.2 Inline Assembly…………………………. 19

2.9 Pragmas  20

2.9.1 #pragma
sectiontype……………..20


2.9.2 #pragma
interruptlow fname
/

#pragma interrupt
fnam
e……..27


2.9.3 #pragma
tmpdata [section-name
]
……………31

2.9.4 #pragma
varlocate
bank
variable-name

#pragma varlocate
section-namevariable-name
………………….33

2.9.5 #pragma
config

……………………..34

2.10 Processor-Specific Header Files .. 36

2.11 Processor-Specific Register Definitions Files ….. 38

Chapter 3. Run-time Model


3.1 Memory Models ………………………. 39

3.2 Calling Conventions…………………. 40

3.2.1 Non-Extended Mode Convention ….40

3.2.2 Extended Mode Convention …………41

3.2.3 Return Values…………………………….42

3.2.4 Managing the Software Stack……….43

3.2.5 Mixing C and Assembly ……………….43

3.3 Start-up Code …………………………. 48

3.3.1 Default Behavior …………………………48

3.3.2 Customization…………………………….49

3.4 Compiler-Managed Resources ….. 50

Chapter 4. Optimizations


4.1 Duplicate String Merging ………….. 51

4.2 Branches 52

4.3 Banking.. 52

4.4 WREG Content Tracking ………….. 53

4.5 Code Straightening ………………….. 53

4.6 Tail Merging ……………………………. 54

4.7 Unreachable Code Removal……… 55

4.8 Copy Propagation ……………………. 55

4.9 Redundant Store Removal ……….. 56

4.10 Dead Code Removal ……………….. 57

4.11 Procedural Abstraction …………….. 57

Chapter 5. Examples


5.1 Application: Embedded “Hello, World!” Using LEDs and
Interrupts……….. 59

5.2 Application: Creating Large Data Objects and the USART……………………
62

5.3 Application: Using EEDATA and Multiple Interrupt
Sources…………………. 67

Appendix A. COFF File
Format


A.1
struct filehdr
– File Header
. 73

A.1.1
unsigned short f_magic

…….. 73

A.1.2
unsigned short f_nscns

…….. 73

A.1.3
unsigned long f_timdat

…….. 73

A.1.4
unsigned long f_symptr

…….. 73

A.1.5
unsigned long f_nsyms

………. 73

A.1.6
unsigned short f_opthdr…… 73


A.1.7
unsigned short f_flags

…….. 74

A.2
struct opthdr
– Optional
File Header……….. 74

A.2.1
unsigned short magi
c………….
74

A.2.2
unsigned long vstamp…………. 74


A.2.3
unsigned long proc_type…… 75


A.2.4
unsigned long rom_width_bits

…………….. 76

A.2.5
unsigned long ram_width_bits

…………….. 76

A.3
struct scnhdr
– Section
Header ………………. 77

A.3.1
union _s

……………………………….. 78

A.3.2
unsigned long s_size…………. 78


A.3.3
unsigned long s_scnptr

…….. 78

A.3.4
unsigned long s_relptr

…….. 78

A.3.5
unsigned long s_lnnoptr…… 78


A.3.6
unsigned short s_nreloc…… 78


A.3.7
unsigned short s_nlnno

…….. 78

A.3.8
unsigned long s_flags

………. 79

A.4
struct reloc
– Relocation
Entry……………….. 79

A.4.1
unsigned long r_vaddr

………. 79

A.4.2
unsigned long r_symndx

…….. 79

A.4.3
short r_offset

……………………. 79

A.4.4
unsigned short r_type

………. 80

A.5
struct syment
– Symbol Table
Entry…………. 81

A.5.1
union _n

……………………………….. 81

A.5.2
unsigned long n_value…………. 81


A.5.3
short n_scnum……………………….. 82


A.5.4
unsigned long n_type…………… 82


A.5.5
char n_sclass……………………….. 83


A.5.6
unsigned char n_numaux

……… 83

A.6
struct coff_lineno
– Line
Number Entry … 84

A.6.1
unsigned long l_srcndx……….. 84


A.6.2
unsigned short l_lnno…………. 84


A.6.3
unsigned long l_paddr…………. 84


A.6.4
unsigned short l_flags……….. 84


A.6.5
unsigned long l_fcnndx……….. 84


A.7
struct aux_file
– Auxiliary
Symbol Table Entry for a Source File ….. 84

A.7.1
unsigned long x_offset

……… 84

A.7.2
unsigned long x_incline

…….. 84

A.7.3 unsigned char
x_flags…………. 85


A.8
struct aux_scn
– Auxiliary
Symbol Table Entry for a Section …………. 85

A.8.1
unsigned long x_scnlen………..85


A.8.2
unsigned short x_nreloc………85


A.8.3
unsigned short x_nlinno………85


A.9
struct aux_tag
– Auxiliary
Symbol Table Entry for a

struct/union/enum Tagname. 85

A.9.1
unsigned short x_size

………….85

A.9.2
unsigned long x_endndx………..85


A.10
struct aux_eos
– Auxiliary
Symbol Table Entry for an End of

struct/union/enum …………….. 86

A.10.1
unsigned long x_tagndx………86


A.10.2
unsigned short x_size

………..86

A.11
struct aux_fcn
– Auxiliary
Symbol Table Entry for a

Function Name ……………………….. 86

A.11.1
unsigned long x_tagndx………86


A.11.2
unsigned long x_lnnoptr…….86


A.11.3
unsigned long x_endndx………86


A.11.4
short x_actscnum…………………86


A.12
struct aux_fcn_calls

Auxiliary Symbol Table Entry for

Function Call References …………. 87

A.12.1
unsigned long x_calleendx…87


A.12.2
unsigned long x_is_interrupt………………87


A.13
struct aux_arr
– Auxiliary
Symbol Table Entry for an Array…………… 87

A.13.1
unsigned long x_tagndx………87


A.13.2
unsigned short x_size

………..87

A.13.3
unsigned short x_dimen[X_DIMNUM]

………87

A.14
struct aux_eobf
– Auxiliary
Symbol Table Entry for the End of a

Block or Function …………………….. 88

A.14.1
unsigned short x_lnno

………..88

A.15
struct aux_bobf
– Auxiliary
Symbol Table Entry for the

Beginning of a Block or Function .. 88

A.15.1
unsigned short x_lnno

………..88

A.15.2
unsigned long x_endndx………88


A.16
struct aux_var
– Auxiliary
Symbol Table Entry for a

Variable of Type
struct/union/enum………… 88


A.16.1
unsigned long x_tagndx………88


A.16.2
unsigned short x_size

………..88

A.17
struct aux_field
– Auxiliary
Entry for a bit field…………………………… 89

A.17.1
unsigned short x_size

………..89

Appendix B. ANSI Implementation-defined Behavior


B.1 Introduction …………………………….. 91

B.2 Identifiers ……………………………….. 91

B.3 Characters ……………………………… 91

B.4 Integers.. 92

B.5 Floating-point ………………………….. 92

B.6 Arrays and Pointers …………………. 93

B.7 Registers 93

B.8 Structures and Unions ……………… 93

B.9 Bit fields . 93

B.10 Enumerations………………………….. 94

B.11 Switch Statement …………………….. 94

B.12 Preprocessing Directives ………….. 94

Appendix C. Command-line Summary……… 95

Appendix D. MPLAB C18 Diagnostics


D.1 Errors ….. 97

D.2 Warnings………………………………. 109

D.3 Messages……………………………… 112

Appendix E. Extended Mode


E.1 Source Code Compatibility ……… 113

E.1.1 Stack Frame Size ……………………. 113

E.1.2
static
Parameters
………………… 113

E.1.3
overlay

Keyword…………………… 113

E.1.4 Inline Assembly ………………………. 114

E.1.5 Predefined Macros ………………….. 114

E.2 Command-line Option Differences……………….. 115

E.3 COFF File Differences ……………. 115

E.3.1 Generic Processor …………………… 115

E.3.2 File Header’s
f_flags
Field…….. 115

 

Read this MPLAB C18 C COMPILER PDF study material

View Source

Related Tutorials

Comments are closed.