Spnu 151 W
Spnu 151 W
v20.2.0.LTS
User’s Guide
Table of Contents
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 3
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Table of Contents www.ti.com
4 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Table of Contents
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 5
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Table of Contents www.ti.com
6 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Table of Contents
List of Figures
Figure 1-1. ARM Software Development Flow.......................................................................................................................... 14
Figure 6-1. Char and Short Data Storage Format................................................................................................................... 141
Figure 6-2. 32-Bit Data Storage Format.................................................................................................................................. 142
Figure 6-3. Double-Precision Floating-Point Data Storage Format......................................................................................... 143
Figure 6-4. Bit-Field Packing in Big-Endian and Little-Endian Formats...................................................................................145
Figure 6-5. Use of the Stack During a Function Call............................................................................................................... 150
Figure 6-6. Autoinitialization at Run Time................................................................................................................................ 162
Figure 6-7. Initialization at Load Time......................................................................................................................................166
Figure 6-8. Constructor Table.................................................................................................................................................. 166
Figure 6-9. Format of Initialization Records in the .cinit Section..............................................................................................167
Figure 6-10. Format of Initialization Records in the .pinit Section........................................................................................... 168
List of Tables
Table 2-1. Processor Options.................................................................................................................................................... 19
Table 2-2. Optimization Options(1) ............................................................................................................................................ 19
Table 2-3. Advanced Optimization Options(1) ........................................................................................................................... 20
Table 2-4. Debug Options.......................................................................................................................................................... 20
Table 2-5. Include Options......................................................................................................................................................... 20
Table 2-6. ULP Advisor Options.................................................................................................................................................20
Table 2-7. Control Options......................................................................................................................................................... 20
Table 2-8. Language Options.....................................................................................................................................................21
Table 2-9. Parser Preprocessing Options.................................................................................................................................. 21
Table 2-10. Predefined Macro Options...................................................................................................................................... 22
Table 2-11. Diagnostic Message Options...................................................................................................................................22
Table 2-12. Supplemental Information Options..........................................................................................................................22
Table 2-13. Run-Time Model Options........................................................................................................................................ 22
Table 2-14. Entry/Exit Hook Options..........................................................................................................................................23
Table 2-15. Feedback Options...................................................................................................................................................23
Table 2-16. Assembler Options..................................................................................................................................................23
Table 2-17. File Type Specifier Options..................................................................................................................................... 23
Table 2-18. Directory Specifier Options..................................................................................................................................... 24
Table 2-19. Default File Extensions Options..............................................................................................................................24
Table 2-20. Command Files Options......................................................................................................................................... 24
Table 2-21. MISRA-C 2004 Options.......................................................................................................................................... 24
Table 2-22. Linker Basic Options............................................................................................................................................... 25
Table 2-23. File Search Path Options........................................................................................................................................ 25
Table 2-24. Command File Preprocessing Options................................................................................................................... 25
Table 2-25. Diagnostic Message Options.................................................................................................................................. 25
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 7
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Table of Contents www.ti.com
8 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Read This First
Preface
Read This First
• In syntax descriptions, instructions, commands, and directives are in a bold typeface and parameters are in
an italic typeface. Portions of a syntax that are in bold should be entered as shown; portions of a syntax that
are in italics describe the type of information that should be entered.
• Square brackets ( [ and ] ) identify an optional parameter. If you use an optional parameter, you specify
the information within the brackets. Unless the square brackets are in the bold typeface, do not enter the
brackets themselves. The following is an example of a command that has an optional parameter:
armcl [options] [filenames] [--run_linker [link_options] [object files]]
• Braces ( { and } ) indicate that you must choose one of the parameters within the braces; you do not enter the
braces themselves. This is an example of a command with braces that are not included in the actual syntax
but indicate that you must specify either the --rom_model or --ram_model option:
armcl --run_linker {--rom_model | --ram_model} filenames [--output_file= name.out]
--library= libraryname
• In assembler syntax statements, the leftmost column is reserved for the first character of a label or symbol. If
the label or symbol is optional, it is usually not shown. If a label or symbol is a required parameter, it is shown
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 9
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Read This First www.ti.com
starting against the left margin of the box, as in the example below. No instruction, command, directive, or
parameter, other than a symbol or label, can begin in the leftmost column.
symbol .usect "section name", size in bytes[, alignment]
• Some directives can have a varying number of parameters. For example, the .byte directive. This syntax is
shown as [, ..., parameter].
• The ARM® 16-bit instruction set is referred to as 16-BIS.
• The ARM 32-bit instruction set is referred to as 32-BIS.
Related Documentation
You can use the following books to supplement this user's guide:
ANSI X3.159-1989, Programming Language - C (Alternate version of the 1989 C Standard), American
National Standards Institute
ISO/IEC 9899:1989, International Standard - Programming Languages - C (The 1989 C Standard),
International Organization for Standardization
ISO/IEC 9899:1999, International Standard - Programming Languages - C (The 1999 C Standard),
International Organization for Standardization
ISO/IEC 9899:2011, International Standard - Programming Languages - C (The 2011 C Standard),
International Organization for Standardization
ISO/IEC 14882-2014, International Standard - Programming Languages - C++ (The 2014 C++ Standard),
International Organization for Standardization
The C Programming Language (second edition), by Brian W. Kernighan and Dennis M. Ritchie, published by
Prentice-Hall, Englewood Cliffs, New Jersey, 1988
The Annotated C++ Reference Manual, Margaret A. Ellis and Bjarne Stroustrup, published by Addison-Wesley
Publishing Company, Reading, Massachusetts, 1990
C: A Reference Manual (fourth edition), by Samuel P. Harbison, and Guy L. Steele Jr., published by Prentice
Hall, Englewood Cliffs, New Jersey
Programming Embedded Systems in C and C++, by Michael Barr, Andy Oram (Editor), published by O'Reilly
& Associates; ISBN: 1565923545, February 1999
Programming in C, Steve G. Kochan, Hayden Book Company
The C++ Programming Language (second edition), Bjarne Stroustrup, published by Addison-Wesley
Publishing Company, Reading, Massachusetts, 1990
Tool Interface Standards (TIS) DWARF Debugging Information Format Specification Version 2.0, TIS
Committee, 1995
DWARF Debugging Information Format Version 3, DWARF Debugging Information Format Workgroup, Free
Standards Group, 2005 (http://dwarfstd.org)
DWARF Debugging Information Format Version 4, DWARF Debugging Information Format Workgroup, Free
Standards Group, 2010 (http://dwarfstd.org)
System V ABI specification (http://www.sco.com/developers/gabi/)
ARM C Language Extensions (ACLE) specification (ACLE Version ACLE Q2 2017)
Related Documentation From Texas Instruments
See the following resources for further information about the TI Code Generation Tools:
• Code Composer Studio Documentation Overview
• Texas Instruments E2E Software Tools Forum
10 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Read This First
You can use the following documents to supplement this user's guide:
SPNU118 ARM Assembly Language Tools User's Guide. Describes the assembly language tools
(assembler, linker, and other tools used to develop assembly language code), assembler directives,
macros, common object file format, and symbolic debugging directives for the ARM devices.
SPRAAB5 The Impact of DWARF on TI Object Files. Describes the Texas Instruments extensions to the
DWARF specification.
SPRUEX3 TI SYS/BIOS Real-time Operating System User's Guide. SYS/BIOS gives application developers
the ability to develop embedded real-time software. SYS/BIOS is a scalable real-time kernel. It
is designed to be used by applications that require real-time scheduling and synchronization or
real-time instrumentation. SYS/BIOS provides preemptive multithreading, hardware abstraction,
real-time analysis, and configuration tools.
Trademarks
Code Composer Studio™ is a trademark of Texas Instruments.
ARM® is a registered trademark of ARM Limited.
All trademarks are the property of their respective owners.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 11
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Read This First www.ti.com
12 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Introduction to the Software Development Tools
Chapter 1
Introduction to the Software Development Tools
The ARM® is supported by a set of software development tools, which includes an optimizing C/C++ compiler,
an assembler, a linker, and assorted utilities.
This chapter provides an overview of these tools and introduces the features of the optimizing C/C++ compiler.
The assembler and linker are discussed in detail in the ARM Assembly Language Tools User's Guide.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 13
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Introduction to the Software Development Tools www.ti.com
C/C++
source
files
Macro
source C/C++
files compiler
C/C++ name
Assembler
Archiver demangling
source
utility
Macro
library Assembler
Library-build Debugging
Object
Archiver utility tools
files
Run-time-
Library of support
object library
files Linker
Executable
object file
Hex-conversion
utility
The following list describes the tools that are shown in Figure 1-1:
• The compiler accepts C/C++ source code and produces ARM assembly language source code. See Chapter
2.
• The assembler translates assembly language source files into machine language relocatable object files.
See the ARM Assembly Language Tools User's Guide.
• The linker combines relocatable object files into a single absolute executable object file. As it creates the
executable file, it performs relocation and resolves external references. The linker accepts relocatable object
14 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Introduction to the Software Development Tools
files and object libraries as input. See Chapter 4 for an overview of the linker. See the ARM Assembly
Language Tools User's Guide for details.
• The archiver allows you to collect a group of files into a single archive file, called a library. The archiver
allows you to modify such libraries by deleting, replacing, extracting, or adding members. One of the most
useful applications of the archiver is building a library of object files. See the ARM Assembly Language Tools
User's Guide.
• The run-time-support libraries contain the standard ISO C and C++ library functions, compiler-utility
functions, floating-point arithmetic functions, and C I/O functions that are supported by the compiler. See
Chapter 7.
The library-build utility automatically builds the run-time-support library if compiler and linker options require
a custom version of the library. See Section 7.4. Source code for the standard run-time-support library
functions for C and C++ is provided in the lib\src subdirectory of the directory where the compiler is installed.
• The hex conversion utility converts an object file into other object formats. You can download the converted
file to an EPROM programmer. See the ARM Assembly Language Tools User's Guide.
• The absolute lister accepts linked object files as input and creates .abs files as output. You can assemble
these .abs files to produce a listing that contains absolute, rather than relative, addresses. Without the
absolute lister, producing such a listing would be tedious and would require many manual operations. See the
ARM Assembly Language Tools User's Guide.
• The cross-reference lister uses object files to produce a cross-reference listing showing symbols, their
definitions, and their references in the linked source files. See the ARM Assembly Language Tools User's
Guide.
• The C++ name demangler is a debugging aid that converts names mangled by the compiler back to their
original names as declared in the C++ source code. As shown in Figure 1-1, you can use the C++ name
demangler on the assembly file that is output by the compiler; you can also use this utility on the assembler
listing file and the linker map file. See Chapter 8.
• The disassembler decodes object files to show the assembly instructions that they represent. See the ARM
Assembly Language Tools User's Guide.
• The main product of this development process is an executable object file that can be executed on a ARM
device.
1.2 Compiler Interface
The compiler is a command-line program named armcl. This program can compile, optimize, assemble, and
link programs in a single step. Within Code Composer Studio™, the compiler is run automatically to perform the
steps needed to build a project.
For more information about compiling a program, see Section 2.1.
The compiler has straightforward calling conventions, so you can write assembly and C functions that call each
other. For more information about calling conventions, see Chapter 6.
1.3 ANSI/ISO Standard
The compiler supports the 1989, 1999, and 2011 versions of the C language and the 2014 version of the C++
language. The C and C++ language features in the compiler are implemented in conformance with the following
ISO standards:
• ISO-standard C: The C compiler supports the 1989, 1999, and 2011 versions of the C language.
– C89. Compiling with the --c89 option causes the compiler to conform to the ISO/IEC 9899:1990 C
standard, which was previously ratified as ANSI X3.159-1989. The names "C89" and "C90" refer to the
same programming language. "C89" is used in this document.
– C99. Compiling with the --c99 option causes the compiler to conform to the ISO/IEC 9899:1999 C
standard.
– C11. Compiling with the --c11 option causes the compiler to conform to the ISO/IEC 9899:2011 C
standard.
The C language is also described in the second edition of Kernighan and Ritchie's The C Programming
Language (K&R).
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 15
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Introduction to the Software Development Tools www.ti.com
• ISO-standard C++: The compiler uses the C++14 version of the C++ standard. Previously, C++03 was used.
See the C++ Standard ISO/IEC 14882:2014. For a description of unsupported C++ features, see Section 5.2.
• ISO-standard run-time support: The compiler tools come with an extensive run-time library. Library
functions conform to the ISO C/C++ library standard unless otherwise stated. The library includes functions
for standard input and output, string manipulation, dynamic memory allocation, data conversion, timekeeping,
trigonometry, and exponential and hyperbolic functions. Functions for signal handling are not included,
because these are target-system specific. For more information, see Chapter 7.
See Section 5.16 for command line options to select the C or C++ standard your code uses.
1.4 Output Files
The following types of output files are created by the compiler:
• ELF object files. Executable and Linking Format (ELF) enables supporting modern language features like
early template instantiation and exporting inline functions. The ELF format for ARM is part of the Application
Binary Interface (ABI) specification, which is documented in the ARM Infocenter.
COFF object files and the legacy TIABI and TI ARM9 ABI modes are not supported in v15.6.0.STS and later
versions of the TI Code Generation Tools. If you would like to produce COFF output files, please use v5.2 of the
ARM Code Generation Tools and refer to SPNU151J for documentation.
1.5 Utilities
These features are compiler utilities:
• Library-build utility
The library-build utility lets you custom-build object libraries from source for any combination of run-time
models. For more information, see Section 7.4.
• C++ name demangler
The C++ name demangler (armdem ) is a debugging aid that translates each mangled name it detects
in compiler-generated assembly code, disassembly output, or compiler diagnostic messages to its original
name found in the C++ source code. For more information, see Chapter 8.
• Hex conversion utility
For stand-alone embedded applications, the compiler has the ability to place all code and initialization data
into ROM, allowing C/C++ code to run from reset. The ELF files output by the compiler can be converted
to EPROM programmer data files by using the hex conversion utility, as described in the ARM Assembly
Language Tools User's Guide.
16 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
Chapter 2
Using the C/C++ Compiler
The compiler translates your source program into machine language object code that the ARM can execute.
Source code must be compiled, assembled, and linked to create an executable file. All of these steps are
executed at once by using the compiler.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 17
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
Note
Invoking the Linker
By default, the compiler does not invoke the linker. You can invoke the linker by using the --run_linker
(-z) compiler option. See Section 4.1.1 for details.
For a complete description of the assembler and the linker, see the ARM Assembly Language Tools User's
Guide.
2.2 Invoking the C/C++ Compiler
To invoke the compiler, enter:
armcl [options] [filenames] [--run_linker [link_options] object files]]
18 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
(1) Note: Machine-specific options (see Table 2-13) can also affect optimization.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 19
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
(1) Note: Machine-specific options (see Table 2-13) can also affect optimization.
20 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 21
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
22 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 23
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
24 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 25
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
26 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
--gen_func_info_listing Generates a user information file with a .aux file extension. The file contains linker call graph information on
a per-file level.
--help Displays the syntax for invoking the compiler and lists available options. If the --help option is followed by
another option or phrase, detailed information about the option or phrase is displayed. For example, to see
information about debugging options use --help debug.
--include_path=directory Adds directory to the list of directories that the compiler searches for #include files. The --include_path
option's short form is -I. You can use this option several times to define several directories; be sure to
separate the --include_path options with spaces. If you do not specify a directory name, the preprocessor
ignores the --include_path option. See Section 2.5.2.1.
--keep_asm Retains the assembly language output from the compiler or assembly optimizer. Normally, the compiler
deletes the output assembly language file after assembly is complete. This option's short form is -k.
--quiet Suppresses banners and progress information from all the tools. Only source filenames and error messages
are output. The --quiet option's short form is -q.
--run_linker Runs the linker on the specified object files. The --run_linker option and its parameters follow all other
options on the command line. All arguments that follow --run_linker are passed to the linker. The --run_linker
option's short form is -z. See Section 4.1.
--skip_assembler Compiles only. The specified source files are compiled but not assembled or linked. This option's short form
is -n. This option overrides --run_linker. The output is assembly language output from the compiler.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 27
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
--src_interlist Invokes the interlist feature, which interweaves optimizer comments or C/C++ source with assembly source.
If the optimizer is invoked (--opt_level=n option), optimizer comments are interlisted with the assembly
language output of the compiler, which may rearrange code significantly. If the optimizer is not invoked,
C/C++ source statements are interlisted with the assembly language output of the compiler, which allows you
to inspect the code generated for each C/C++ statement. The --src_interlist option implies the --keep_asm
option. The --src_interlist option's short form is -s.
--tool_version Prints the version number for each tool in the compiler. No compiling occurs.
--undefine=name Undefines the predefined constant name. This option overrides any --define options for the specified
constant. The --undefine option's short form is -U.
--verbose Displays progress information and toolset version while compiling. Resets the --quiet option.
28 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
--preinclude=filename Includes the source code of filename at the beginning of the compilation. This can be used to establish
standard macro definitions. The filename is searched for in the directories on the include search list.
The files are processed in the order in which they were specified.
--printf_support={full| Enables support for smaller, limited versions of the printf function family (sprintf, fprintf, etc.) and the
nofloat|minimal} scanf function family (sscanf, fscanf, etc.) run-time-support functions. The valid values are:
• full: Supports all format specifiers. This is the default.
• nofloat: Excludes support for printing and scanning floating-point values. Supports all format
specifiers except %a, %A, %f, %F, %g, %G, %e, and %E.
• minimal: Supports the printing and scanning of integer, char, or string values without width or
precision flags. Specifically, only the %%, %d, %o, %c, %s, and %x format specifiers are supported.
There is no run-time error checking to detect if a format specifier is used for which support is
not included. The --printf_support option precedes the --run_linker option, and must be used when
performing the final link.
--sat_reassoc={on|off} Enables or disables the reassociation of saturating arithmetic.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 29
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
--neon The compiler can generate code using the SIMD instructions available in the Neon extension
to the version 7 ARM architecture. The optimizer attempts to vectorize source code in order
to take advantage of these SIMD instructions. In order to generate vectorized SIMD Neon
code, select the version 7 architecture with the -mv=7A8 option and enable Neon instruction
support with the --neon option.
The optimizer is used to vectorize the source code. At least level 2 optimization (--
opt_level=2 or O2) is required, although level 3 (--opt_level=3) is recommended along with
the --opt_for_speed option.
--pending_instantiations=# Specify the number of template instantiations that may be in progress at any given time. Use
0 to specify an unlimited number.
--plain_char={signed|unsigned} Specifies how to treat C/C++ plain char variables. Default is unsigned.
--ramfunc={on|off} If set to on, specifies that all functions should be placed in the .TI.ramfunc section, which is
placed in RAM. If set to off, only functions with the ramfunc function attribute are treated this
way. See Section 5.17.2.
Newer TI linker command files support the --ramfunc option automatically by placing
functions in the .TI.ramfunc section. If you have a linker command file that does not include
a section specification for the .TI.ramfunc section, you can modify the linker command file to
place this section in RAM. See the ARM Assembly Language Tools User's Guide for details
on section placement.
--silicon_version Selects the instruction set version. The options are:
• 4 = ARM V4 (ARM7) This is the default.
• 5e = ARM V5e (ARM9E)
• 6 = ARM V6 (ARM11)
• 6M0 = ARM V6M0 (Cortex-M0)
• 7A8 = ARM V7A8 (Cortex-A8)
• 7M3 = ARM V7M3 (Cortex-M3)
• 7M4 = ARM V7M4 (Cortex-M4)
• 7R4 = ARM V7R4 (Cortex-R4),
• 7R5 = ARM V7R5 (Cortex-R5)
30 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
Note
Case Sensitivity in Filename Extensions: Case sensitivity in filename extensions is determined
by your operating system. If your operating system is not case sensitive, a file with a .C extension
is interpreted as a C file. If your operating system is case sensitive, a file with a .C extension is
interpreted as a C++ file.
For information about how you can alter the way that the compiler interprets individual filenames, see Section
2.3.7. For information about how you can alter the way that the compiler interprets and names the extensions of
assembly source and object files, see Section 2.3.10.
You can use wildcard characters to compile or assemble multiple files. Wildcard specifications vary by system;
use the appropriate form listed in your operating system manual. For example, to compile all of the files in a
directory with the extension .cpp, enter the following:
armcl *.cpp
Note
No Default Extension for Source Files is Assumed: If you list a filename called example on the
command line, the compiler assumes that the entire filename is example not example.c. No default
extensions are added onto files that do not contain an extension.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 31
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
For example, if you have a C source file called file.s and an assembly language source file called assy, use the
--asm_file and --c_file options to force the correct interpretation:
armcl --c_file=file.s --asm_file=assy
Note
The default file extensions for object files created by the compiler have been changed in order
to prevent conflicts when C and C++ files have the same names. Object files generated from C
source files have the .c.obj extension. Object files generated from C++ source files have the .cpp.obj
extension.
The following example assembles the file fit.rrr and creates an object file named fit.o:
armcl --asm_extension=.rrr --obj_extension=.o fit.rrr
The period (.) in the extension is optional. You can also write the example above as:
armcl --asm_extension=rrr --obj_extension=o fit.rrr
32 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
--asm_dependency Performs preprocessing for assembly files, but instead of writing preprocessed output,
writes a list of dependency lines suitable for input to a standard make utility. The list is
written to a file with the same name as the source file but with a .ppa extension.
--asm_includes Performs preprocessing for assembly files, but instead of writing preprocessed output,
writes a list of files included with the #include directive. The list is written to a file with the
same name as the source file but with a .ppa extension.
--asm_listing Produces an assembly listing file.
--asm_undefine=name Undefines the predefined constant name. This option overrides any --asm_define options
for the specified name.
--code_state={16|32} Generates 16-bit Thumb code. By default, 32-bit code is generated. When Cortex-R4,
Cortex-M0, Cortex-M3, or Cortex-A8 architecture support is chosen, the --code_state
option generates Thumb-2 code. For details on indirect calls in 16-bit versus 32-bit code,
see Section 6.11.2.2.
--asm_cross_reference_listing Produces a symbolic cross-reference in the listing file.
--include_file=filename Includes the specified file for the assembly module; acts like an .include directive. The file
is included before source file statements. The included file does not appear in the assembly
listing files.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 33
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
Note
C_OPTION and C_DIR -- The C_OPTION and C_DIR environment variables are deprecated. Use
device-specific environment variables instead.
Environment variable options are specified in the same way and have the same meaning as they do
on the command line. For example, if you want to always run quietly (the --quiet option), enable C/C+
+ source interlisting (the --src_interlist option), and link (the --run_linker option) for Windows, set up the
TI_ARM_C_OPTION environment variable as follows:
set TI_ARM_C_OPTION=--quiet --src_interlist --run_linker
Note
The TI_ARM_C_OPTION environment variable takes precedence over the older TMS470_C_OPTION
environment variable if both are defined. If only TMS470_C_OPTION is set, it will continue to be used.
Any options following --run_linker on the command line or in TI_ARM_C_OPTION are passed to the linker.
Thus, you can use the TI_ARM_C_OPTION environment variable to specify default compiler and linker options
and then specify additional compiler and linker options on the command line. If you have set --run_linker in
the environment variable and want to compile only, use the compiler --compile_only option. These additional
examples assume TI_ARM_C_OPTION is set as shown above:
armcl *c ; compiles and links
armcl --compile_only *.c ; only compiles
armcl *.c --run_linker lnk.cmd ; compiles and links using a command file
armcl --compile_only *.c --run_linker lnk.cmd
; only compiles (--compile_only overrides --run_linker)
For details on compiler options, see Section 2.3. For details on linker options, see the Linker Description chapter
in the ARM Assembly Language Tools User's Guide.
34 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
The pathnames are directories that contain input files. The pathnames must follow these constraints:
• Pathnames must be separated with a semicolon.
• Spaces or tabs at the beginning or end of a path are ignored. For example, the space before and after the
semicolon in the following is ignored:
set TI_ARM_C_DIR=c:\path\one\to\tools ; c:\path\two\to\tools
• Spaces and tabs are allowed within paths to accommodate Windows directories that contain spaces. For
example, the pathnames in the following are valid:
set TI_ARM_C_DIR=c:\first path\to\tools;d:\second path\to\tools
The environment variable remains set until you reboot the system or reset the variable by entering:
Operating System Enter
UNIX (Bourne shell) unset TI_ARM_C_DIR
Windows set TI_ARM_C_DIR=
Note
The TI_ARM_C_DIR environment variable takes precedence over the older TMS470_C_DIR
environment variable if both are defined. If only TMS470_C_DIR is set, it will continue to be used.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 35
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
36 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
Note
Macros with names that contain __TI_ARM are duplicates of the older __TI_TMS470 macros. For
example, __TI_ARM_V7__ is the newer name for the __TI_TMS470_V7__ macro. The old macro
names still exist and can continue to be used.
You can use the names listed in Table 2-30 in the same manner as any other defined name. For example,
printf ( "%s %s" , __TIME__ , __DATE__);
In addition, the ARM C Language Extensions (ACLE) v2.0 specification describes macros that identify features
of the ARM architecture and how the C/C++ implementation uses the architecture. All ACLE predefined macros
begin with the prefix __ARM. Table 2-31 lists the macros mentioned in the ACLE specification and the section
of the specification that provides more information. Some macros are undefined because they are not applicable
for any Cortex-M or Cortex-R processor variant.
Table 2-31. ACLE Pre-Defined Macros
Macro Name Description Section in ACLE
Specification
__ARM_32BIT_STATE Defined as 1 if the compiler is generating code for an ARM (Section 5.4.1)
32-bit processor variant (-mv6m0, -mv7m3, -mv7m4, -mv7a8,
-mv7r4, and -mv7r5); undefined otherwise.
__ARM_64BIT_STATE Undefined (Section 5.4.1)
__ARM_ACLE Defined as 200 for all Cortex-M and Cortex-R processor variants (Sections 3.4, 5.2)
(-mv6m0, -mv7m3, -mv7m4, -mv7r4, and -mv7r5).
__ARM_ALIGN_MAX_PWR Not supported (Section 6.5.2)
__ARM_ALIGN_MAX_STACK_PWR Not supported (Section 6.5.3)
__ARM_ARCH Identifies the version of ARM architecture selected on the (Section 5.1)
compiler command line.
• 4 indicates -mv4
• 5 indicates -mv5e
• 6 indicates -mv6 or -mv6m0
• 7 indicates -mv7a8, -mv7m3, -mv7m4, -mv7r4, or -mv7r5
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 37
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
38 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 39
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
The table below shows how to invoke the compiler. Select the command for your operating system:
Operating System Enter
UNIX armcl --include_path=/tools/files source.c
Windows armcl --include_path=c:\tools\files source.c
Note
Specifying Path Information in Angle Brackets: If you specify the path information in angle
brackets, the compiler applies that information relative to the path information specified with --
include_path options and the TI_ARM_C_DIR environment variable.
For example, if you set up TI_ARM_C_DIR with the following command:
TI_ARM_C_DIR "/usr/include;/usr/ucb"; export TI_ARM_C_DIR
40 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 41
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
The loadProg syntax is as follows, where file is an executable file and args is an object array of arguments. Use
JavaScript to declare the array of arguments before using this command.
loadProg(file, args)
The .args section is loaded with the following data for non-SYS/BIOS-based executables, where each element in
the argv[] array contains a string corresponding to that argument:
Int argc;
Char * argv[0];
Char * argv[1];
...
Char * argv[n];
For SYS/BIOS-based executables, the elements in the .args section are as follows:
Int argc;
Char ** argv; /* points to argv[0] */
Char * envp; /* ignored by loadProg command */
Char * argv[0];
Char * argv[1];
...
Char * argv[n];
42 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
Diagnostic messages are written to standard error with a form like the following example:
"test.c", line 5: error: a break statement may only be used within a loop or switch
break;
^
By default, the source code line is not printed. Use the --verbose_diagnostics compiler option to display the
source line and the error position. The above example makes use of this option.
The message identifies the file and line involved in the diagnostic, and the source line itself (with the position
indicated by the ^ character) follows the message. If several diagnostic messages apply to one source line, each
diagnostic has the form shown; the text of the source line is displayed several times, with an appropriate position
indicated each time.
Long messages are wrapped to additional lines, when necessary.
You can use the --display_error_number command-line option to request that the diagnostic's numeric identifier
be included in the diagnostic message. When displayed, the diagnostic identifier also indicates whether the
diagnostic can have its severity overridden on the command line. If the severity can be overridden, the
diagnostic identifier includes the suffix -D (for discretionary); otherwise, no suffix is present. For example:
"Test_name.c", line 7: error #64-D: declaration does not declare anything
struct {};
^
"Test_name.c", line 9: error #77: this declaration has no storage class or type specifier
xxxxx;
^
Because errors are determined to be discretionary based on the severity in a specific context, an error can be
discretionary in some cases and not in others. All warnings and remarks are discretionary.
For some messages, a list of entities (functions, local variables, source files, etc.) is useful; the entities are listed
following the initial error message:
"test.c", line 4: error: more than one instance of overloaded function "f"
matches the argument list:
function "f(int)"
function "f(float)"
argument types are: (double)
f(1.5);
^
In some cases, additional context information is provided. Specifically, the context information is useful when the
front end issues a diagnostic while doing a template instantiation or while generating a constructor, destructor, or
assignment operator function. For example:
"test.c", line 7: error: "A::A()" is inaccessible
B x;
^
detected during implicit generation of "B::B()" at line 7
Without the context information, it is difficult to determine to what the error refers.
2.7.1 Controlling Diagnostic Messages
The C/C++ compiler provides diagnostic options to control compiler- and linker-generated diagnostic messages.
The diagnostic options must be specified before the --run_linker option.
--diag_error=num Categorizes the diagnostic identified by num as an error. To determine the numeric identifier of a
diagnostic message, use the --display_error_number option first in a separate compile. Then use
--diag_error=num to recategorize the diagnostic as an error. You can only alter the severity of
discretionary diagnostic messages.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 43
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
--diag_remark=num Categorizes the diagnostic identified by num as a remark. To determine the numeric identifier of a
diagnostic message, use the --display_error_number option first in a separate compile. Then use
--diag_remark=num to recategorize the diagnostic as a remark. You can only alter the severity of
discretionary diagnostic messages.
--diag_suppress=num Suppresses the diagnostic identified by num. To determine the numeric identifier of a diagnostic
message, use the --display_error_number option first in a separate compile. Then use --
diag_suppress=num to suppress the diagnostic. You can only suppress discretionary diagnostic
messages.
--diag_warning=num Categorizes the diagnostic identified by num as a warning. To determine the numeric identifier of
a diagnostic message, use the --display_error_number option first in a separate compile. Then use
--diag_warning=num to recategorize the diagnostic as a warning. You can only alter the severity of
discretionary diagnostic messages.
--display_error_number Displays a diagnostic's numeric identifier along with its text. Use this option in determining which
arguments you need to supply to the diagnostic suppression options (--diag_suppress, --diag_error,
--diag_remark, and --diag_warning). This option also indicates whether a diagnostic is discretionary. A
discretionary diagnostic is one whose severity can be overridden. A discretionary diagnostic includes the
suffix -D; otherwise, no suffix is present. See Section 2.7.
--emit_warnings_as_ Treats all warnings as errors. This option cannot be used with the --no_warnings option. The
errors --diag_remark option takes precedence over this option. This option takes precedence over the --
diag_warning option.
--issue_remarks Issues remarks (non-serious warnings), which are suppressed by default.
--no_warnings Suppresses diagnostic warnings (errors are still issued).
--section_sizes={on|off} Generates section size information, including sizes for sections containing executable code and
constants, constant or initialized data (global and static variables), and uninitialized data. Section size
information is output during both the assembly and linking phases. This option should be placed on the
command line with the compiler options (that is, before the --run_linker or --z option).
--set_error_limit=num Sets the error limit to num, which can be any decimal value. The compiler abandons compiling after this
number of errors. (The default is 100.)
--verbose_diagnostics Provides verbose diagnostic messages that display the original source with line-wrap and indicate the
position of the error in the source line. Note that this command-line option cannot be used within the
Code Composer Studio IDE.
--write_diagnostics_file Produces a diagnostic message information file with the same source file name with an .err extension.
(The --write_diagnostics_file option is not supported by the linker.) Note that this command-line option
cannot be used within the Code Composer Studio IDE.
If you invoke the compiler with the --quiet option, this is the result:
"err.c", line 9: warning: statement is unreachable
"err.c", line 12: warning: statement is unreachable
44 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
Because it is standard programming practice to include break statements at the end of each case arm to avoid
the fall-through condition, these warnings can be ignored. Using the --display_error_number option, you can find
out the diagnostic identifier for these warnings. Here is the result:
[err.c]
"err.c", line 9: warning #111-D: statement is unreachable
"err.c", line 12: warning #111-D: statement is unreachable
Next, you can use the diagnostic identifier of 111 as the argument to the --diag_remark option to treat this
warning as a remark. This compilation produces no diagnostic messages (because remarks are disabled by
default).
Note
You can suppress any non-fatal errors, but be careful to make sure you only suppress diagnostic
messages that you understand and are known not to affect the correctness of your program.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 45
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
The --gen_preprocessor_listing option also includes diagnostic identifiers as defined in Table 2-33.
Table 2-33. Raw Listing File Diagnostic Identifiers
Diagnostic Identifier Definition
E Error
F Fatal
R Remark
W Warning
S One of the identifiers in Table 2-33 that indicates the severity of the diagnostic
filename The source file
line number The line number in the source file
column number The column number in the source file
diagnostic The message text for the diagnostic
Diagnostic messages after the end of file are indicated as the last line of the file with a column number of 0. When diagnostic message text
requires more than one line, each subsequent line contains the same file, line, and column information but uses a lowercase version of the
diagnostic identifier. For more information about diagnostic messages, see Section 2.7.
46 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
Note
Excessive Inlining Can Degrade Performance: Excessive inlining can make the compiler
dramatically slower and degrade the performance of generated code.
Note
Function Inlining Can Greatly Increase Code Size: Function inlining increases code size, especially
inlining a function that is called in a number of places. Function inlining is optimal for functions that are
called only from a small number of places and for small functions.
The semantics of the inline keyword in C code follow the C99 standard. The semantics of the inline
keyword in C++ code follow the C++ standard.
The inline keyword is supported in all C++ modes, in relaxed ANSI mode for all C standards, and in strict
ANSI mode for C99 and C11. It is disabled in strict ANSI mode for C89, because it is a language extension that
could conflict with a strictly conforming program. If you want to define inline functions while in strict ANSI C89
mode, use the alternate keyword __inline.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 47
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
48 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
The --c_src_interlist option prevents the compiler from deleting the interlisted assembly language output file. The
output assembly file, function.asm, is assembled normally.
When you invoke the interlist feature without the optimizer, the interlist runs as a separate pass between the
code generator and the assembler. It reads both the assembly and C/C++ source files, merges them, and writes
the C/C++ statements into the assembly file as comments.
For information about using the interlist feature with the optimizer, see Section 3.11. Using the --c_src_interlist
option can cause performance and/or code size degradation.
The following example shows a typical interlisted assembly file.
_main:
STMFD SP!, {LR}
;------------------------------------------------------------------------------
; 5 | printf("Hello, world\n");
;------------------------------------------------------------------------------
ADR A1, SL1
BL _printf
;------------------------------------------------------------------------------
; 6 | return 0;
;------------------------------------------------------------------------------
MOV A1, #0
LDMFD SP!, {PC}
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 49
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
none Disables hardware floating point support. Specifies that the compiler implements floating point
operations in software.
Using the --silicon_version=7M4 command-line option automatically sets the --float_support=fpv4spd16 option.
To disable hardware floating point support, use the --float_support=none option.
This is the current support for VFP:
• You must link any VFP compiled code with a separate version of the run-time support library. See Section
7.1.9 for information on library-naming conventions.
• The compiler follows the VFP argument passing and returning calling convention for qualified VFP
arguments.
• Object files that do not contain any functions with floating point arguments or return values can be linked with
both VFP and non-VFP files.
• Object files that do contain functions with floating point arguments or return values can only be linked with
objects that were compiled with matching VFP support.
• All hand-coded VFP assembly must follow VFP calling conventions and EABI conventions to correctly
compile and link. In addition to these, the appropriate VFP build attributes for EABI must be correctly set.
• The compile-time predefined macro __TI_VFP_SUPPORT__ can be used for conditionally compiling/
assembling user code. VFP-specific user code can use this macro to ensure that the conditionally included
code is compiled only when VFP is enabled.
Refer to the ARM architecture manual for more details on the VFPv3 and VFPv3D16 architectures and ISAs.
Refer to the ARM AAPCS and EABI documents for more details on VFP calling conventions and build attributes.
50 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using the C/C++ Compiler
The presence of the hook options creates an implicit declaration of the hook function with the given signature. If
a declaration or definition of the hook function appears in the compilation unit compiled with the options, it must
agree with the signatures listed above.
In C++, the hooks are declared extern "C". Thus you can define them in C (or assembly) without being
concerned with name mangling.
Hooks can be declared inline, in which case the compiler tries to inline them using the same criteria as other
inline functions.
Entry hooks and exit hooks are independent. You can enable one but not the other, or both. The same function
can be used as both the entry and exit hook.
You must take care to avoid recursive calls to hook functions. The hook function should not call any function
which itself has hook calls inserted. To help prevent this, hooks are not generated for inline functions, or for the
hook functions themselves.
You can use the --remove_hooks_when_inlining option to remove entry/exit hooks for functions that are auto-
inlined by the optimizer.
See Section 5.11.21 for information about the NO_HOOKS pragma.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 51
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using the C/C++ Compiler www.ti.com
52 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Optimizing Your Code
Chapter 3
Optimizing Your Code
The compiler tools can perform many optimizations to improve the execution speed and reduce the size of C
and C++ programs by simplifying loops, rearranging statements and expressions, and allocating variables into
registers.
This chapter describes how to invoke different levels of optimization and describes which optimizations are
performed at each level. This chapter also describes how you can use the Interlist feature when performing
optimization and how you can profile or debug optimized code.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 53
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Optimizing Your Code www.ti.com
54 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Optimizing Your Code
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 55
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Optimizing Your Code www.ti.com
Note
Compiling Files With the --program_level_compile and --keep_asm Options
If you compile all files with the --program_level_compile and --keep_asm options, the compiler
produces only one .asm file, not one for each corresponding source file.
In certain circumstances, the compiler reverts to a different --call_assumptions level from the one you specified,
or it might disable program-level optimization altogether. Table 3-4 lists the combinations of --call_assumptions
levels and conditions that cause the compiler to revert to other --call_assumptions levels.
56 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Optimizing Your Code
In some situations when you use --program_level_compile and --opt_level=3, you must use a --call_assumptions
option or the FUNC_EXT_CALLED pragma. See Section 3.4.2 for information about these situations.
3.4.2 Optimization Considerations When Mixing C/C++ and Assembly
If you have any assembly functions in your program, you need to exercise caution when using the --
program_level_compile option. The compiler recognizes only the C/C++ source code and not any assembly
code that might be present. Because the compiler does not recognize the assembly code calls and variable
modifications to C/C++ functions, the --program_level_compile option optimizes out those C/C++ functions. To
keep these functions, place the FUNC_EXT_CALLED pragma (see Section 5.11.14) before any declaration or
reference to a function that you want to keep.
Another approach you can take when you use assembly functions in your program is to use the --
call_assumptions=n option with the --program_level_compile and --opt_level=3 options. See Section 3.4.1 for
information about the --call_assumptions=n option.
In general, you achieve the best results through judicious use of the FUNC_EXT_CALLED pragma in
combination with --program_level_compile --opt_level=3 and --call_assumptions=1 or --call_assumptions=2.
If any of the following situations apply to your application, use the suggested solution:
• Situation: Your application consists of C/C++ source code that calls assembly functions. Those assembly
functions do not call any C/C++ functions or modify any C/C++ variables.
Solution: Compile with --program_level_compile --opt_level=3 --call_assumptions=2 to tell the compiler that
outside functions do not call C/C++ functions or modify C/C++ variables.
If you compile with the --program_level_compile --opt_level=3 options only, the compiler reverts from
the default optimization level (--call_assumptions=2) to --call_assumptions=0. The compiler uses --
call_assumptions=0, because it presumes that the calls to the assembly language functions that have a
definition in C/C++ may call other C/C++ functions or modify C/C++ variables.
• Situation: Your application consists of C/C++ source code that calls assembly functions. The assembly
language functions do not call C/C++ functions, but they modify C/C++ variables.
Solution: Try both of these solutions and choose the one that works best with your code:
– Compile with --program_level_compile --opt_level=3 --call_assumptions=1.
– Add the volatile keyword to those variables that may be modified by the assembly functions and compile
with --program_level_compile --opt_level=3 --call_assumptions=2.
• Situation: Your application consists of C/C++ source code and assembly source code. The assembly
functions are interrupt service routines that call C/C++ functions; the C/C++ functions that the assembly
functions call are never called from C/C++. These C/C++ functions act like main: they function as entry points
into C/C++.
Solution: Add the volatile keyword to the C/C++ variables that may be modified by the interrupts. Then, you
can optimize your code in one of these ways:
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 57
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Optimizing Your Code www.ti.com
– You achieve the best optimization by applying the FUNC_EXT_CALLED pragma to all of the entry-point
functions called from the assembly language interrupts, and then compiling with --program_level_compile
--opt_level=3 --call_assumptions=2. Be sure that you use the pragma with all of the entry-point functions.
If you do not, the compiler might remove the entry-point functions that are not preceded by the
FUNC_EXT_CALLED pragma.
– Compile with --program_level_compile --opt_level=3 --call_assumptions=3. Because you do not use the
FUNC_EXT_CALLED pragma, you must use the --call_assumptions=3 option, which is less aggressive
than the --call_assumptions=2 option, and your optimization may not be as effective.
Keep in mind that if you use --program_level_compile --opt_level=3 without additional options, the compiler
removes the C functions that the assembly functions call. Use the FUNC_EXT_CALLED pragma to keep
these functions.
3.5 Automatic Inline Expansion (--auto_inline Option)
When optimizing with the --opt_level=3 option (aliased as -O3), the compiler automatically inlines small
functions. A command-line option, --auto_inline=size, specifies the size threshold for automatic inlining. This
option controls only the inlining of functions that are not explicitly declared as inline.
When the --auto_inline option is not used, the compiler sets the size limit based on the optimization level and the
optimization goal (performance versus code size). If the -auto_inline size parameter is set to 0, automatic inline
expansion is disabled. If the --auto_inline size parameter is set to a non-zero integer, the compiler automatically
inlines any function smaller than size. (This is a change from previous releases, which inlined functions for which
the product of the function size and the number of calls to it was less than size. The new scheme is simpler, but
will usually lead to more inlining for a given value of size.)
The compiler measures the size of a function in arbitrary units; however the optimizer information file (created
with the --gen_opt_info=1 or --gen_opt_info=2 option) reports the size of each function in the same units that
the --auto_inline option uses. When --auto_inline is used, the compiler does not attempt to prevent inlining that
causes excessive growth in compile time or size; use with care.
When --auto_inline option is not used, the decision to inline a function at a particular call-site is based on an
algorithm that attempts to optimize benefit and cost. The compiler inlines eligible functions at call-sites until a
limit on size or compilation time is reached.
Inlining behavior varies, depending on which compile-time options are specified:
• The code size limit is smaller when compiling for code size rather than performance. The --auto_inline option
overrides this size limit.
• At --opt_level=3, the compiler automatically inlines small functions.
• At --opt_level=4, the compiler auto-inlines aggressively if compiling for performance.
For information about interactions between command-line options, pragmas, and keywords that affect inlining,
see Section 2.11.
Note
Some Functions Cannot Be Inlined: For a call-site to be considered for inlining, it must be legal
to inline the function and inlining must not be disabled in some way. See the inlining restrictions in
Section 2.11.2.
Note
Optimization Level 3 and Inlining: In order to turn on automatic inlining, you must use the
--opt_level=3 option. If you desire the --opt_level=3 optimizations, but not automatic inlining, use
--auto_inline=0 with the --opt_level=3 option.
58 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Optimizing Your Code
Note
Inlining and Code Size: Expanding functions inline increases code size, especially inlining a function
that is called in a number of places. Function inlining is optimal for functions that are called only from a
small number of places and for small functions. To prevent increases in code size because of inlining,
use the --auto_inline=0 option. This option causes the compiler to inline intrinsics only.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 59
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Optimizing Your Code www.ti.com
60 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Optimizing Your Code
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 61
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Optimizing Your Code www.ti.com
-a Computes the average of the data values in the data sets instead of accumulating data values
-e exec.out Specifies exec.out is the name of the application executable.
-o application.prf Specifies application.prf is the formatted profile feedback file that is used as the argument to --
use_profile_info during recompilation. If no output file is specified, the default output filename is
pprofout.prf.
filename .pdat Is the name of the profile data file generated by the run-time-support function. This is the default name and
it can be overridden by using the host environment variable TI_PROFDATA.
The run-time-support function and armpdd append to their respective output files and do not overwrite them.
This enables collection of data sets from multiple runs of the application.
Note
Profile Data Decoder Requirements: Compile applications with at least DWARF debug support
to enable feedback-directed optimization. When compiling for feedback-directed optimization, the
armpdd tool relies on basic debug information about each function to generate the formatted .prf file.
The pprofout.pdat file generated by the run-time support is a raw data file of a fixed format understood
only by armpdd. You should not modify this file in any way.
62 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Optimizing Your Code
API
_TI_start_pprof_collection() Clears the profile counters to file
_TI_stop_pprof_collection() Writes out all profile counters to file
PPHDNL Device driver handle for low-level C I/O based driver for writing out profile data from a target program.
Files Created
*.pdat Profile data file, which is created by executing an instrumented program and used as input to the
profile data decoder
*.prf Profiling feedback file, which is created by the profile data decoder and used as input to the re-
compilation step
You can specify two environment variables to control the destination of the code-coverage information file.
• The TI_COVDIR environment variable specifies the directory where the code-coverage file should be
generated. The default is the directory where the compiler is invoked.
• The TI_COVDATA environment variable specifies the name of the code-coverage data file generated by
the compiler. the default is filename.csv where filename is the base-name of the file being compiled. For
example, if foo.c is being compiled, the default code-coverage data file name is foo.csv.
If the code-coverage data file already exists, the compiler appends the new dataset at the end of the file.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 63
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Optimizing Your Code www.ti.com
Code-coverage data is a comma-separated list of data items that can be conveniently handled by data-
processing tools and scripting languages. The following is the format of code-coverage data:
"filename-with-full-path","funcname",line#,column#,exec-frequency,"comments"
"filename-with-full-path" Full pathname of the file corresponding to the entry
"funcname" Name of the function
line# Line number of the source line corresponding to frequency data
column# Column number of the source line
exec-frequency Execution frequency of the line
"comments" Intermediate-level representation of the source-code generated by the parser
The full filename, function name, and comments appear within quotation marks ("). For example:
"/some_dir/zlib/arm/deflate.c","_deflateInit2_",216,5,1,"( strm->zalloc )"
Other tools, such as a spreadsheet program, can be used to format and view the code coverage data.
For further information about profile-based optimization and a more detailed description of the profiling
infrastructure, see Section 3.7.
3.8.2.2 Analysis Options
The path profiling utility, armpprof, appends code coverage information to existing CSV (comma separated
values) files that contain the same type of analysis information.
The utility checks to make sure that an existing CSV file contains analysis information that is consistent with the
type of analysis information it is being asked to generate . Attempts to mix code coverage and other analysis
information in the same output CSV file will be detected, and armpprof will emit a fatal error and abort.
--analyze=codecov Instructs the compiler to generate code coverage analysis information. This option replaces the
previous --codecov option.
--analyze_only Halts compilation after generation of analysis information is completed.
64 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Optimizing Your Code
Note
Impact on Performance and Code Size: The --c_src_interlist option can have a negative effect on
performance and code size.
When you use the --c_src_interlist and --optimizer_interlist options with optimization, the compiler inserts its
comments and the interlist feature runs before the assembler, merging the original C/C++ source into the
assembly file.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 65
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Optimizing Your Code www.ti.com
For example, suppose the following C code is compiled with optimization (--opt_level=2) and --optimizer_interlist
options:
int copy (char *str, const char *s, int n)
{
int i;
for (i = 0; i < n; i ++)
*str++ = *s++;
}
The assembly file contains compiler comments interlisted with assembly code.
_main:
STMFD SP!, {LR}
;** 5----------------------- printf("Hello, world\n");
ADR A1, SL1
BL _printf
;** 6----------------------- return 0;
MOV A1, #0
LDMFD SP!, {PC}
If you add the --c_src_interlist option (compile with --opt_level=2, --c_src_interlist, and --optimizer_interlist), the
assembly file contains compiler comments and C source interlisted with assembly code.
_main:
STMFD SP!, {LR}
;** 5----------------------- printf("Hello, world\n");
;------------------------------------------------------------------------------
; 5 | printf("Hello, world\n");
;------------------------------------------------------------------------------
ADR A1, SL1
BL _printf
;** 6----------------------- return 0;
;------------------------------------------------------------------------------
; 6 | return 0;
;------------------------------------------------------------------------------
MOV A1, #0
LDMFD SP!, {PC}
66 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Optimizing Your Code
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 67
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Optimizing Your Code www.ti.com
When the value of a condition is determined at compile time (through copy propagation or other data flow
analysis), the compiler can delete a conditional branch. Switch case lists are analyzed in the same way as
conditional branches and are sometimes eliminated entirely. Some simple control flow constructs are reduced to
conditional instructions, totally eliminating the need for branches.
This type of optimization is enabled by the --opt_level=0 and higher optimization settings.
3.13.4 Data Flow Optimizations
Collectively, the following data flow optimizations replace expressions with less costly ones, detect and remove
unnecessary assignments, and avoid operations that produce values that are already computed. The compiler
with optimization enabled performs these data flow optimizations both locally (within basic blocks) and globally
(across entire functions).
• Copy propagation. Following an assignment to a variable, the compiler replaces references to the variable
with its value. The value can be another variable, a constant, or a common subexpression. This can result in
increased opportunities for constant folding, common subexpression elimination, or even total elimination of
the variable. This type of optimization is enabled by the --opt_level=1 and higher optimization settings.
• Common subexpression elimination. When two or more expressions produce the same value, the
compiler computes the value once, saves it, and reuses it. This type of optimization is enabled by the
--opt_level=2 and higher optimization settings.
• Redundant assignment elimination. Often, copy propagation and common subexpression elimination
optimizations result in unnecessary assignments to variables (variables with no subsequent reference before
another assignment or before the end of the function). The compiler removes these dead assignments.
This type of optimization is enabled by the --opt_level=1 for local assignments and --opt_level=2 for global
assignments.
3.13.5 Expression Simplification
For optimal evaluation, the compiler simplifies expressions into equivalent forms, requiring fewer instructions or
registers. Operations between constants are folded into single constants. For example, a = (b + 4) - (c + 1)
becomes a = b - c + 3.
This type of optimization is enabled by the --opt_level=0 and higher optimization settings.
3.13.6 Inline Expansion of Functions
The compiler replaces calls to small functions with inline code, saving the overhead associated with a function
call as well as providing increased opportunities to apply other optimizations. For information about interactions
between command-line options, pragmas, and keywords that affect inlining, see Section 2.11.
This type of optimization is enabled by the --opt_level=0 and higher optimization settings.
3.13.7 Function Symbol Aliasing
The compiler recognizes a function whose definition contains only a call to another function. If the two functions
have the same signature (same return value and same number of parameters with the same type, in the same
order), then the compiler can make the calling function an alias of the called function.
For example, consider the following:
int bbb(int arg1, char *arg2);
int aaa(int n, char *str)
{
return bbb(n, str);
}
For this example, the compiler makes aaa an alias of bbb, so that at link time all calls to function aaa should be
redirected to bbb. If the linker can successfully redirect all references to aaa, then the body of function aaa can
be removed and the symbol aaa is defined at the same address as bbb.
For information about using the GCC function attribute syntax to declare function aliases, see Section 5.17.2
68 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Optimizing Your Code
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 69
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Optimizing Your Code www.ti.com
int main(int a)
{
if (a < 0)
a = a-3;
else
a = a*3;
return ++a;
}
;*********************************************************
;* FUNCTION DEF: _main *
;*********************************************************
_main:
CMP A1, #0
ADDPL A1, A1, A1, LSL #1
SUBMI A1, A1, #3
ADD A1, A1, #1
BX LR
70 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Optimizing Your Code
If L10 is far away from LAB1 (large offset), the assembler converts BR into a sequence of branch around and
unconditional branches, resulting in a sequence of two instructions that are either four or six bytes long. Instead,
if the branch at LAB1 can jump to LAB2, and LAB2 is close enough that BR can be replaced by a single short
branch instruction, the resulting code is smaller as the BR in LAB1 would be converted into one instruction that is
two bytes long. LAB2 can in turn jump to another branch if L10 is too far away from LAB2. Thus, branch chaining
can be extended to arbitrary depths.
When you compile in thumb mode (--code_state=16) and for code size (--opt_for_speed is not used), the
compiler generates two psuedo instructions:
• BTcc instead of BRcc. The format is BRcc target, #[depth].
The #depth is an optional argument. If depth is not specified, it is set to the default branch chaining depth. If
specified, the chaining depth for this branch instruction is set to #depth. The assembler issues a warning if
#depth is less than zero and sets the branch chaining depth for this instruction to zero.
• BQcc instead of Bcc. The format is BQcc target , #[depth].
The #depth is the same as for the BTcc psuedo instruction.
The BT pseudo instruction replaces the BR (pseudo branch) instruction. Similarly, BQ replaces B. The
assembler performs branch chain optimizations for these instructions, if branch chaining is enabled. The
assembler replaces the BT and BQ jump targets with the offset to the branch to which these instructions jump.
The default branch chaining depth is 10. This limit is designed to prevent longer branch chains from impeding
performance.
You can the BT and BQ instructions in assembly language programs to enable the assembler to perform branch
chaining. You can control the branch chaining depth for each instruction by specifying the (optional) #depth
argument. You must use the BR and B instructions to prevent branch chaining for any BT or BQ branches.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 71
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Optimizing Your Code www.ti.com
72 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Linking C/C++ Code
Chapter 4
Linking C/C++ Code
The C/C++ Code Generation Tools provide two methods for linking your programs:
• You can compile individual modules and link them together. This method is especially useful when you have
multiple source files.
• You can compile and link in one step. This method is useful when you have a single source module.
This chapter describes how to invoke the linker with each method. It also discusses special requirements of
linking C/C++ code, including the run-time-support libraries, specifying the type of initialization, and allocating the
program into memory. For a complete description of the linker, see the ARM Assembly Language Tools User's
Guide.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 73
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Linking C/C++ Code www.ti.com
Note
The default file extensions for object files created by the compiler have been changed. Object files
generated from C source files have the .c.obj extension. Object files generated from C++ source files
have the .cpp.obj extension.
When you specify a library as linker input, the linker includes and links only those library members that resolve
undefined references. The linker uses a default allocation algorithm to allocate your program into memory. You
can use the MEMORY and SECTIONS directives in the linker command file to customize the allocation process.
For information, see the ARM Assembly Language Tools User's Guide.
You can link a C/C++ program consisting of object files prog1.c.obj, prog2.c.obj, and prog3.cpp.obj, with an
executable object file filename of prog.out with the command:
armcl --run_linker --rom_model prog1 prog2 prog3 --output_file=prog.out
--library=rtsv4_A_be_eabi.lib
74 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Linking C/C++ Code
The --run_linker option divides the command line into the compiler options (the options before --run_linker) and
the linker options (the options following --run_linker). The --run_linker option must follow all source files and
compiler options on the command line.
All arguments that follow --run_linker on the command line are passed to the linker. These arguments can
be linker command files, additional object files, linker options, or libraries. These arguments are the same as
described in Section 4.1.1.
All arguments that precede --run_linker on the command line are compiler arguments. These arguments can be
C/C++ source files, assembly files, or compiler options. These arguments are described in Section 2.2.
You can compile and link a C/C++ program consisting of object files prog1.c, prog2.c, and prog3.c, with an
executable object file filename of prog.out with the command:
armcl prog1.c prog2.c prog3.c --run_linker --rom_model --output_file=prog.out
--library=rtsv4_A_be_eabi.lib
When you use armcl --run_linker after listing at least one C/C++ file to be compiled on the same command line,
by default the --rom_model is used for automatic variable initialization at run time. See Section 4.3.5 for details
about using the --rom_model and --ram_model options.
Note
Order of Processing Arguments in the Linker: The order in which the linker processes arguments
is important. The compiler passes arguments to the linker in the following order:
1. Object filenames from the command line
2. Arguments following the --run_linker option on the command line
3. Arguments following the --run_linker option from the TI_ARM_C_OPTION environment variable
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 75
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Linking C/C++ Code www.ti.com
2. During the linker, use the --generate_dead_funcs_list option to generate the feedback file based on the
generated object files. For example:
armcl --run_linker file1.c.obj file2.c.obj --generate_dead_funcs_list=feedback.txt
Alternatively, you can combine steps 1 and 2 into one step. When you do this, you are not required to specify
--gen_func_subsections when compiling the source files as this is done for you automatically. For example:
armcl file1.c file2.c --run_linker --generate_dead_funcs_list=feedback.txt
3. Once you have the feedback file, rebuild the source. Give the feedback file to the compiler using the
--use_dead_funcs_list option. This option forces each dead function listed in the file into its own subsection.
For example:
armcl file1.c file2.c --use_dead_funcs_list=feedback.txt
4. Invoke the linker with the newly built object files. The linker removes the subsections. For example:
armcl --run_linker file1.c.obj file2.c.obj
Alternatively, you can combine steps 3 and 4 into one step. For example:
armcl file1.c file2.c --use_dead_funcs_list=feedback.txt --run_linker
Note
Dead Functions Feedback: The format of the feedback file generated with --
generate_dead_funcs_list is tightly controlled. It must be generated by the linker in order to be
processed correctly by the compiler. The format of this file may change over time, so the file contains
a version format number to allow backward compatibility.
76 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Linking C/C++ Code
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 77
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Linking C/C++ Code www.ti.com
specify a library as linker input, the linker includes and links only those library members that resolve undefined
references.
By default, if a library introduces an unresolved reference and multiple libraries have a definition for it, then the
definition from the same library that introduced the unresolved reference is used. Use the --priority option if you
want the linker to use the definition from the first library on the command line that contains the definition.
4.3.2 Run-Time Initialization
You must link all C/C++ programs with code to initialize and execute the program called a bootstrap routine. The
bootstrap routine is responsible for the following tasks:
1. Switch to user mode and sets up the user mode stack
2. Set up status and configuration registers
3. Set up the stack
4. Process special binit copy table, if present.
5. Process the run-time initialization table to autoinitialize global variables (when using the --rom_model option)
6. Call all global constructors
7. Call the main() function
8. Call exit() when main() returns
Note
The _c_int00 Symbol: If you use the --ram_model or --rom_model link option, _c_int00 is
automatically defined as the entry point for the program. If your command line does not list any C/C++
files to compile and does not specify either the --ram_model or --rom_model link option, the linker
does not know whether or not to use the C/C++ conventions, and you will receive a linker warning that
says "warning: no suitable entry-point found; setting to 0". See Section 4.3.5 for details about using
the --rom_model and --ram_model options.
78 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Linking C/C++ Code
whether to select initialization at run time or load time. If your command line fails to include one of these options
when it is required, you will see "warning: no suitable entry-point found; setting to 0".
If you use a single command line to both compile and link, the --rom_model option is the default. If used, the
--rom_model or --ram_model option must follow the --run_linker option (see Section 4.1).
For details on linking conventions for EABI with --rom_model and --ram_model, see Section 6.10.3.3 and
Section 6.10.3.5, respectively.
Note
Boot Loader: A loader is not included as part of the C/C++ compiler tools. You can use the ARM
simulator or emulator with the source debugger as a loader. See the "Program Loading and Running"
chapter of the ARM Assembly Language Tools User's Guide for more about boot loading.
When you link your program, you must specify where to allocate the sections in memory. In general, initialized
sections are linked into ROM or RAM; uninitialized sections are linked into RAM.
The linker provides MEMORY and SECTIONS directives for allocating sections. For more information about
allocating sections into memory, see the ARM Assembly Language Tools User's Guide.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 79
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Linking C/C++ Code www.ti.com
80 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
Chapter 5
C/C++ Language Implementation
The C language supported by the ARM was developed by a committee of the American National Standards
Institute (ANSI) and subsequently adopted by the International Standards Organization (ISO).
The C++ language supported by the ARM is defined by the ANSI/ISO/IEC 14882:2014 standard with certain
exceptions.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 81
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
82 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 83
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
\a BEL (bell) 7
\b BS (backspace) 8
\f FF (form feed) 12
\n LF (line feed) 10
\r CR (carriage 13
return)
\t HT (horizontal tab) 9
\v VT (vertical tab) 11
• The value of a char object into which any character other than a member of the basic execution character set
has been stored is the ASCII value of that character. (6.2.5)
• Plain char is identical to unsigned char, but can be changed to signed char with the --plain_char=signed
option. (6.2.5, 6.3.1.1)
• The source character set and execution character set are both plain ASCII, so the mapping between them
is one-to-one. The compiler accepts multibyte characters in comments. See Section 5.6 for details. (6.4.4.4,
5.1.1.2)
• The compiler currently supports only one locale, "C". (6.4.4.4)
• The compiler currently supports only one locale, "C". (6.4.5)
J.3.5 Integers
• No extended integer types are provided. (6.2.5)
• Negative values for signed integer types are represented as two's complement, and there are no trap
representations. (6.2.6.2)
• No extended integer types are provided, so there is no change to the integer ranks. (6.3.1.1)
• When an integer is converted to a signed integer type which cannot represent the value, the value is
truncated (without raising a signal) by discarding the bits which cannot be stored in the destination type; the
lowest bits are not modified. (6.3.1.3)
• Right shift of a signed integer value performs an arithmetic (signed) shift. The bitwise operations other than
right shift operate on the bits in exactly the same way as on an unsigned value. That is, after the usual
arithmetic conversions, the bitwise operation is performed without regard to the format of the integer type, in
particular the sign bit. (6.5)
J.3.6 Floating point
• The accuracy of floating-point operations (+ - * /) is bit-exact. The accuracy of library functions that return
floating-point results is not specified. (5.2.4.2.2)
• The compiler does not provide non-standard values for FLT_ROUNDS. (5.2.4.2.2)
• The compiler does not provide non-standard negative values of FLT_EVAL_METHOD. (5.2.4.2.2)
• The rounding direction when an integer is converted to a floating-point number is IEEE-754 "round to even".
(6.3.1.4)
• The rounding direction when a floating-point number is converted to a narrower floating-point number is
IEEE-754 "round to even". (6.3.1.5)
• For floating-point constants that are not exactly representable, the implementation uses the nearest
representable value. (6.4.4.2)
• The compiler does not contract float expressions. (6.5)
• The default state for the FENV_ACCESS pragma is off. (7.6.1)
• The TI compiler does not define any additional float exceptions. (7.6, 7.12)
• The default state for the FP_CONTRACT pragma is off. (7.12.2)
• The "inexact" floating-point exception cannot be raised if the rounded result equals the mathematical result.
(F.9)
84 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
• The "underflow" and "inexact" floating-point exceptions cannot be raised if the result is tiny but not inexact.
(F.9)
J.3.7 Arrays and pointers
• When converting a pointer to an integer or vice versa, the pointer is considered an unsigned integer of the
same size, and the normal integer conversion rules apply.
• When converting a pointer to an integer or vice versa, if the bitwise representation of the destination can hold
all of the bits in the bitwise representation of the source, the bits are copied exactly. (6.3.2.3)
• The size of the result of subtracting two pointers to elements of the same array is the size of ptrdiff_t, which is
defined in Section 5.5. (6.5.6)
J.3.8 Hints
• When the optimizer is used, the register storage-class specifier is ignored. When the optimizer is not used,
the compiler will preferentially place register storage class objects into registers to the extent possible. The
compiler reserves the right to place any register storage class object somewhere other than a register. (6.7.1)
• The inline function specifier is ignored unless the optimizer is used. For other restrictions on inlining, see
Section 2.11.2. (6.7.4)
J.3.9 Structures, unions, enumerations, and bit-fields
• A "plain" int bit-field is treated as a signed int bit-field. (6.7.2, 6.7.2.1)
• In addition to _Bool, signed int, and unsigned int, the compiler allows char, signed char, unsigned char,
signed short, unsigned shot, signed long, unsigned long, signed long long, unsigned long long, and enum
types as bit-field types. (6.7.2.1)
• Bit-fields may not straddle a storage-unit boundary. (6.7.2.1)
• Bit-fields are allocated in endianness order within a unit. See Section 6.2.2. (6.7.2.1)
• Non-bit-field members of structures are aligned as specified in Section 6.2.1. (6.7.2.1)
• The integer type underlying each enumerated type is described in Section 5.5.1. (6.7.2.2)
J.3.10 Qualifiers
• The TI compiler does not shrink or grow volatile accesses. It is the user's responsibility to make sure the
access size is appropriate for devices that only tolerate accesses of certain widths. The TI compiler does
not change the number of accesses to a volatile variable unless absolutely necessary. This is significant
for read-modify-write expressions such as += ; for an architecture which does not have a corresponding
read-modify-write instruction, the compiler will be forced to use two accesses, one for the read and one for
the write. Even for architectures with such instructions, it is not guaranteed that the compiler will be able to
map such expressions to an instruction with a single memory operand. It is not guaranteed that the memory
system will lock that memory location for the duration of the instruction. In a multi-core system, some other
core may write the location after a RMW instruction reads it, but before it writes the result. The TI compiler
will not reorder two volatile accesses, but it may reorder a volatile and a non-volatile access, so volatile
cannot be used to create a critical section. Use some sort of lock if you need to create a critical section.
(6.7.3)
J.3.11 Preprocessing directives
• Include directives may have one of two forms, " " or < >. For both forms, the compiler will look for a real file
on-disk by that name using the include file search path. See Section 2.5.2. (6.4.7)
• The value of a character constant in a constant expression that controls conditional inclusion matches the
value of the same character constant in the execution character set (both are ASCII). (6.10.1)
• The compiler uses the file search path to search for an included < > delimited header file. See Section 2.5.2.
(6.10.2)
• The compiler uses the file search path to search for an included " " delimited header file. See Section 2.5.2.
(6.10.2)
• There is no arbitrary nesting limit for #include processing. (6.10.2)
• See Section 5.11 for a description of the recognized non-standard pragmas. (6.10.6)
• The date and time of translation are always available from the host. (6.10.8)
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 85
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
86 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 87
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
88 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
The rulespec parameter is a comma-separated list of rule numbers to enable. For example, --
advice:power=1.1,7.2,7.3,7.4 enables rules 1.1, 7.2, 7.3, and 7.4.
The --advice:power_severity option sets the diagnostic severity for ULP Advisor rules. The syntax is:
--advice:power_severity={error|warning|remark|suppress}
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 89
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
(1) "Plain" char has the same representation as either signed char or unsigned char. The --plain_char option specifies whether "plain" char
is signed or unsigned. The default is unsigned.
(2) This is the default type for wchar_t. You can use the --wchar_t option to change the wchar_t type to a 32-bit unsigned int type.
(3) 64-bit data is aligned on a 64-bit boundary.
(4) For details about the size of an enum type, see Section 5.5.1. Also see Table 5-2 for sizes.
(5) Figures are minimum precision.
Negative values for signed types are represented using two's complement.
The type of the storage container for an enumerated type is the smallest integer type that contains all the
enumerated values. The container types for enumerators are shown in Table 5-2.
Table 5-2. Enumerator Types
Lower Bound Range Upper Bound Range Enumerator Type
0 to 255 0 to 255 unsigned char
-128 to 1 -128 to 127 signed char
0 to 65 535 256 to 65 535 unsigned short
-128 to 1 128 to 32 767 short, signed short
-32 768 to -129 -32 768 to 32 767
0 to 4 294 967 295 2 147 483 648 to 4 294 967 295 unsigned int
-32 768 to -1 32 767 to 2 147 483 647 int, signed int
-2 147 483 648 to -32 769 -2 147 483 648 to 2 147 483 647
0 to 2 147 483 647 65 536 to 2 147 483 647
90 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
The compiler determines the type based on the range of the lowest and highest elements of the enumerator. For
example, the following code results in an enumerator type of int:
enum COLORS
{ green = -200,
blue = 1,
yellow = 2,
red = 60000
};
Each enumerated type is assigned an integer type that can hold all of the enumeration constants. This integer
type is the "underlying type." The type of each enumeration constant is also an integer type, and in C might not
be the same type. Be careful to note the difference between the underlying type of an enumerated type and the
type of an enumeration constant.
The size and signedness chosen for the enumerated type and each enumeration constant depend on the values
of the enumeration constants and whether you are compiling for C or C++. C++11 allows you to specify a
specific type for an enumeration type; if such a type is provided, it will be used and the rest of this section does
not apply.
In C++ mode, the compiler allows enumeration constants up to the largest integral type (64 bits). The C standard
says that all enumeration constants in strictly conforming C code (C89/C99/C11) must have a value that fits into
the type "int;" however, as an extension, you may use enumeration constants larger than "int" even in C mode.
You may control the strategy for picking enumerated types by using either the --enum_type command line
option, or by using an attribute, or both. If you use the --enum_type=packed option (the default), the compiler
uses the smallest type it can for the enumerated type. If you use the --enum_type=int option, the underlying type
will be int. An enumeration constant with a value outside the int range generates an error.
For the enumerated type if --enum_type=packed, the compiler selects the first type in this list that is big enough
and of the correct sign to represent all of the values of the enumeration constants:
• unsigned char
• signed char
• unsigned short
• signed short
• unsigned int
• signed int
• unsigned long long
• signed long long
The "long" type is skipped because it is the same size as "int."
For example, this enumerated type will have "unsigned char" as its underlying type:
enum uc { a, b, c };
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 91
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
But this one will have "signed char" as its underlying type:
enum sc { a, b, c, d = -1 };
And this one will have "signed short" as its underlying type:
enum ss { a, b, c, d = -1, e = UCHAR_MAX };
For C++, the enumeration constants are all of the same type as the enumerated type.
For C, the enumeration constants are assigned types depending on their value. All enumeration constants with
values that can fit into "int" are given type "int," even if the underlying type of the enumerated type is smaller than
"int." All enumeration constants that do not fit in an "int" are given the same type as the underlying type of the
enumerated type. This means that some enumeration constants may have a different size and signedness than
the enumeration type.
5.6 File Encodings and Character Sets
The compiler accepts source files with one of two distinct encodings:
• UTF-8 with Byte Order Mark (BOM). These files may contain extended (multibyte) characters in C/C++
comments. In all other contexts—including string constants, identifiers, assembly files, and linker command
files—only 7-bit ASCII characters are supported.
• Plain ASCII files. These files must contain only 7-bit ASCII characters.
To choose the UTF-8 encoding in Code Composer Studio, open the Preferences dialog, select General >
Workspace, and set the Text File Encoding to UTF-8.
If you use an editor that does not have a "plain ASCII" encoding mode, you can use Windows-1252 (also called
CP-1252) or ISO-8859-1 (also called Latin 1), both of which accept all 7-bit ASCII characters. However, the
compiler may not accept extended characters in these encodings, so you should not use extended characters,
even in comments.
Wide character (wchar_t) types and operations are supported by the compiler. However, wide character strings
may not contain characters beyond 7-bit ASCII. The encoding of wide characters is 7-bit ASCII, 0 extended to
the width of the wchar_t type.
5.7 Keywords
The ARM C/C++ compiler supports all of the standard C89 keywords, including const, volatile, and register.
It supports all of the standard C99 keywords, including inline and restrict. It supports all of the standard C11
keywords. It also supports TI extension keywords __interrupt,and __asm. Some keywords are not available in
strict ANSI mode.
The following keywords may appear in other target documentation and require the same treatment as the
interrupt and restrict keywords:
• trap
• reentrant
• cregister
5.7.1 The const Keyword
The C/C++ compiler supports the ANSI/ISO standard keyword const in all modes. This keyword gives you
greater optimization and control over allocation for certain data objects. You can apply the const qualifier to the
definition of any variable or array to ensure that its value is not altered.
Global objects qualified as const are placed in the .const section. The linker allocates the .const section from
ROM or FLASH, which are typically more plentiful than RAM. The const data storage allocation rule has the
following exceptions:
92 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
• If volatile is also specified in the object definition. For example, volatile const int x. Volatile keywords
are assumed to be allocated to RAM. (The program is not allowed to modify a const volatile object, but
something external to the program might.)
• If the object has automatic storage ( function scope).
• If the object is a C++ object with a "mutable" member.
• If the object is initialized with a value that is not known at compile time (such as the value of another
variable).
In these cases, the storage for the object is the same as if the const keyword were not used.
The placement of the const keyword is important. For example, the first statement below defines a constant
pointer p to a modifiable int. The second statement defines a modifiable pointer q to a constant int:
int * const p = &x;
const int * q = &x;
Using the const keyword, you can define large constant tables and allocate them into system ROM. For
example, to allocate a ROM table, you could use the following definition:
const int digits[] = {0,1,2,3,4,5,6,7,8,9};
The name c_int00 is the C/C++ entry point. This name is reserved for the system reset interrupt. This special
interrupt routine initializes the system and calls the main() function. Because it has no caller, c_int00 does not
save any registers.
Note
Hwi Objects and the __interrupt Keyword: The __interrupt keyword must not be used when SYS/
BIOS Hwi objects are used in conjunction with C functions. The Hwi_enter/Hwi_exit macros and the
Hwi dispatcher already contain this functionality; use of the C modifier can cause unwanted conflicts.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 93
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
However, in this example, *ctrl is a loop-invariant expression, so the loop is optimized down to a single-memory
read. To get the desired result, define ctrl as:
volatile unsigned int *ctrl;
Here the *ctrl pointer is intended to reference a hardware location, such as an interrupt flag.
The volatile keyword must also be used when accessing memory locations that represent memory-mapped
peripheral devices. Such memory locations might change value in ways that the compiler cannot predict. These
locations might change if accessed, or when some other memory location is accessed, or when some signal
occurs.
Volatile must also be used for local variables in a function which calls setjmp, if the value of the local variables
needs to remain valid if a longjmp occurs.
#include <stdlib.h>
jmp_buf context;
void function()
{
volatile int x = 3;
switch(setjmp(context))
{
case 0: setup(); break;
default:
{
/* We only reach here if longjmp occurs. Because x's lifetime begins before setjmp
and lasts through longjmp, the C standard requires x be declared "volatile". */
printf("x == %d\n", x);
break;
}
}
}
94 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 95
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
The regid parameter can be __R5, __R6, or __R9. The identifiers _ _R5, _ _R6, and _ _R9 are each bound to
their corresponding register R5, R6 and R9, respectively.
When you use this declaration at the file level, the register is permanently reserved from any other use by the
optimizer and code generator for that file. You cannot assign an initial value to the register. You can use a
#define directive to assign a meaningful name to the register; for example:
register struct data_struct *__R5
#define data_pointer __R5
data_pointer->element;
data_pointer++;
There are two reasons that you would be likely to use a global register variable:
• You are using a global variable throughout your program, and it would significantly reduce code size and
execution speed to assign this variable to a register permanently.
• You are using an interrupt service routine that is called so frequently that it would significantly reduce
execution speed if the routine did not have to save and restore the register(s) it uses every time it is called.
You need to consider very carefully the implications of reserving a global register variable. Registers are a
precious resource to the compiler, and using this feature indiscriminately may result in poorer code.
You also need to consider carefully how code with a globally declared register variable interacts with other code,
including library functions, that does not recognize the restriction placed on the register.
Because the registers that can be global register variables are save-on-entry registers, a normal function call
and return does not affect the value in the register and neither does a normal interrupt. However, when you mix
code that has a globally declared register variable with code that does not have the register reserved, it is still
possible for the value in the register to become corrupted. To avoid the possibility of corruption, you must follow
these rules:
• Functions that alter global register variables cannot be called by functions that are not aware of the global
register. Use the -r shell option to reserve the register in code that is not aware of the global register
declaration. You must be careful if you pass a pointer to a function as an argument. If the passed function
alters the global register variable and the called function saves the register, the value in the register will be
corrupted.
• You cannot access a global register variable in an interrupt service routine unless you recompile all code,
including all libraries, to reserve the register. This is because the interrupt routine can be called from any
point in the program.
• The longjmp ( ) function restores global register variables to the values they had at the setjmp ( ) location. If
this presents a problem in your code, you must alter the code for the function and recompile rts.src.
The -r register compiler command-line option allows you to prevent the compiler from using the named
register. This lets you reserve the named register in modules that do not have the global register variable
declaration, such as the run-time-support libraries, if you need to compile the modules to prevent some of the
above occurrences.
96 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
The compiler copies the argument string directly into your output file. The assembler text must be enclosed in
double quotes. All the usual character string escape codes retain their definitions. For example, you can insert
a .byte directive that contains quotes as follows:
__asm("STR: .byte \"abc\"");
The naked function attribute can be used to identify functions that are written as embedded assembly functions
using __asm statements. See Section 5.17.2.
The inserted code must be a legal assembly language statement. Like all assembly language statements, the
line of code inside the quotes must begin with a label, a blank, a tab, or a comment (asterisk or semicolon). The
compiler performs no checking on the string; if there is an error, the assembler detects it. For more information
about the assembly language statements, see the ARM Assembly Language Tools User's Guide.
The __asm statements do not follow the syntactic restrictions of normal C/C++ statements. Each can appear as
a statement or a declaration, even outside of blocks. This is useful for inserting directives at the very beginning
of a compiled module.
The __asm statement does not provide any way to refer to local variables. If your assembly code needs to refer
to local variables, you will need to write the entire function in assembly code.
For more information, refer to Section 6.6.5.
Note
Avoid Disrupting the C/C++ Environment With asm Statements
Be careful not to disrupt the C/C++ environment with __asm statements. The compiler does not check
the inserted instructions. Inserting jumps and labels into C/C++ code can cause unpredictable results
in variables manipulated in or around the inserted code. Directives that change sections or otherwise
affect the assembly environment can also be troublesome.
Be especially careful when you use optimization with __asm statements. Although the compiler
cannot remove __asm statements, it can significantly rearrange the code order near them and cause
undesired results.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 97
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
98 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
Note that in C++, the arguments to the CALLS pragma must be the full mangled names for the functions that can
be indirectly called from the calling function.
The GCC-style "calls" function attribute (see Section 5.17.2), which has the same effect as the CALLS pragma,
has the following syntax:
__attribute__((calls("function_1","function_2",..., "function_n")))
The rulespec parameter is a comma-separated list of rule numbers. See Section 5.3 for details.
The RESET_MISRA pragma can be used to reset any CHECK_MISRA pragmas; see Section 5.11.25.
5.11.3 The CHECK_ULP Pragma
The CHECK_ULP pragma enables/disables ULP Advisor rules at the source level. This pragma has the same
effect as using the --advice:power option.
The syntax of the pragma in C is:
#pragma CHECK_ULP (" {all|none|rulespec} ")
The rulespec parameter is a comma-separated list of rule numbers. See Section 5.4 for the syntax. See
www.ti.com/ulpadvisor for a list of rules.
The RESET_ULP pragma can be used to reset any CHECK_ULP pragmas; see Section 5.11.26.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 99
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
100 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
Example 5-1 through Example 5-3 demonstrate the use of the DATA_SECTION pragma.
Example 5-1. Using the DATA_SECTION Pragma C Source File
char bufferA[512];
#pragma DATA_SECTION("my_sect")
char bufferB[512];
.global _bufferA
.bss _bufferA,512,4
.global _bufferB
_bufferB: .usect "my_sect",512,4
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 101
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
The syntax of the diag_suppress, diag_remark, diag_warning, and diag_error pragmas in C is:
#pragma diag_ xxx [=]num[, num2, num3...]
102 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
For example, in the following example, the mytest() and getname() functions are inlined, but the error() function
is not.
#pragma FORCEINLINE
if (!mytest(getname(myvar))) {
error();
}
Placing the FORCEINLINE pragma before the call to error() would inline that function but not the others.
For information about interactions between command-line options, pragmas, and keywords that affect inlining,
see Section 2.11.
Notice that the FORCEINLINE, FORCEINLINE_RECURSIVE, and NOINLINE pragmas affect only the C/C++
statement that follows the pragma. The FUNC_ALWAYS_INLINE and FUNC_CANNOT_INLINE pragmas affect
an entire function.
5.11.11 The FORCEINLINE_RECURSIVE Pragma
The FORCEINLINE_RECURSIVE can be placed before a statement to force any function calls made in that
statement to be inlined along with any calls made from those functions. That is, calls that are not visible in the
statement but are called as a result of the statement will be inlined.
The syntax of the pragma in C/C++ is:
#pragma FORCEINLINE_RECURSIVE
For information about interactions between command-line options, pragmas, and keywords that affect inlining,
see Section 2.11.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 103
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
Note
Use Caution with the FUNC_ALWAYS_INLINE Pragma
The FUNC_ALWAYS_INLINE pragma overrides the compiler's inlining decisions. Overuse of this
pragma could result in increased compilation times or memory usage, potentially enough to consume
all available memory and result in compilation tool failures.
104 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
Except for _c_int00, which is the name reserved for the system reset interrupt for C/C++programs, the name of
the interrupt (the func argument) does not need to conform to a naming convention.
When you use program-level optimization, you may need to use the FUNC_EXT_CALLED pragma with certain
options. See Section 3.4.2.
5.11.15 The FUNCTION_OPTIONS Pragma
The FUNCTION_OPTIONS pragma allows you to compile a specific function in a C or C++ file with additional
command-line compiler options. The affected function will be compiled as if the specified list of options appeared
on the command line after all other compiler options. In C, the pragma is applied to the function specified. In
C++, the pragma is applied to the next function.
The syntax of the pragma in C is:
#pragma FUNCTION_OPTIONS ( func , " additional options " )
Supported options for this pragma are --opt_level, --auto_inline, --code_state, and --opt_for_speed.
In order to use --opt_level and --auto_inline with the FUNCTION_OPTIONS pragma, the compiler must be
invoked with some optimization level (that is, at least --opt_level=0). The FUNCTION_OPTIONS pragma
is ignored if --opt_level=off. The FUNCTION_OPTIONS pragma cannot be used to completely disable the
optimizer for the compilation of a function; the lowest optimization level that can be specified is --opt_level=0.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 105
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
The GCC interrupt attribute syntax, which has the same effects as the INTERRUPT pragma, is as follows. Note
that the interrupt attribute can precede either the function's definition or its declaration.
__attribute__((interrupt [("interrupt_type")] )) void func ( void )
In C, the argument func is the name of a function. In C++, the pragma applies to the next function declared.
The optional argument interrupt_type specifies an interrupt type. The registers that are saved and the return
sequence depend upon the interrupt type. If the interrupt type is omitted from the interrupt pragma, the interrupt
type IRQ is assumed. These are the valid interrupt types:
Interrupt Type Description
DABT Data abort
FIQ Fast interrupt request
IRQ Interrupt request
PABT Prefetch abort
RESET System reset
SWI Software interrupt
UDEF Undefined instruction
Except for _c_int00, which is the name reserved for the system reset interrupt for C programs, the name of the
interrupt (the func argument) does not need to conform to a naming convention.
For the Cortex-M architectures, the interrupt_type can be nothing (default) or SWI. The hardware performs the
necessary saving and restoring of context for interrupts. Therefore, the compiler does not distinguish between
the different interrupt types. The only exception is for software interrupts (SWIs) which are allowed to have
arguments (for Cortex-M architectures, C SWI handlers cannot return values).
#pragma INTERRUPT ( func , {HPI|LPI} )
Note
Hwi Objects and the INTERRUPT Pragma: The INTERRUPT pragma must not be used when SYS/
BIOS Hwi objects are used in conjunction with C functions. The Hwi_enter/Hwi_exit macros and the
Hwi dispatcher contain this functionality, and the use of the C modifier can cause negative results.
106 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
The NOINIT pragma may be used in conjunction with the LOCATION pragma to map variables to special
memory locations; see Section 5.11.19.
5.11.18 The MUST_ITERATE Pragma
The MUST_ITERATE pragma specifies to the compiler certain properties of a loop. When you use this pragma,
you are guaranteeing to the compiler that a loop executes a specific number of times or a number of times within
a specified range.
Any time the UNROLL pragma is applied to a loop, MUST_ITERATE should be applied to the same loop.
For loops the MUST_ITERATE pragma's third argument, multiple, is the most important and should always be
specified.
Furthermore, the MUST_ITERATE pragma should be applied to any other loops as often as possible. This
is because the information provided via the pragma (especially the minimum number of iterations) aids the
compiler in choosing the best loops and loop transformations (that is, nested loop transformations). It also helps
the compiler reduce code size.
No statements are allowed between the MUST_ITERATE pragma and the for, while, or do-while loop to which it
applies. However, other pragmas, such as UNROLL, can appear between the MUST_ITERATE pragma and the
loop.
5.11.18.1 The MUST_ITERATE Pragma Syntax
The syntax of the pragma for C and C++ is:
#pragma MUST_ITERATE ( min, max, multiple )
The C++ syntax for the corresponding attribute is as follows. No C attribute syntax is available.
[[TI::must_iterate( min, max, multiple )]]
The arguments min and max are programmer-guaranteed minimum and maximum trip counts. The trip count is
the number of times a loop iterates. The trip count of the loop must be evenly divisible by multiple. All arguments
are optional. For example, if the trip count could be 5 or greater, you can specify the argument list as follows:
#pragma MUST_ITERATE(5)
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 107
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
However, if the trip count could be any nonzero multiple of 5, the pragma would look like this:
#pragma MUST_ITERATE(5, , 5) /* Note the blank field for max */
It is sometimes necessary for you to provide min and multiple in order for the compiler to perform unrolling. This
is especially the case when the compiler cannot easily determine how many iterations the loop will perform (that
is, the loop has a complex exit condition).
When specifying a multiple via the MUST_ITERATE pragma, results of the program are undefined if the trip
count is not evenly divisible by multiple. Also, results of the program are undefined if the trip count is less than
the minimum or greater than the maximum specified.
If no min is specified, zero is used. If no max is specified, the largest possible number is used. If multiple
MUST_ITERATE pragmas are specified for the same loop, the smallest max and largest min are used.
The following example uses the must_iterate C++ attribute syntax:
void myFunc (int *a, int *b, int * restrict c, int n)
{
...
[[TI::must_iterate(32, 1024, 16)]]
for (int i = 0; i < n; i++)
{
c[i] = a[i] + b[i];
}
...
}
In this example, the compiler attempts to generate a loop even without the pragma. However, if MUST_ITERATE
is not specified for a loop such as this, the compiler generates code to bypass the loop, to account for the
possibility of 0 iterations. With the pragma specification, the compiler knows that the loop iterates at least once
and can eliminate the loop-bypassing code.
MUST_ITERATE can specify a range for the trip count as well as a factor of the trip count. The following
example tells the compiler that the loop executes between 8 and 48 times and the trip_count variable is a
multiple of 8 (8, 16, 24, 32, 40, 48). The multiple argument allows the compiler to unroll the loop.
#pragma MUST_ITERATE(8, 48, 8)
for(i = 0; i < trip_count; i++) { ...
You should consider using MUST_ITERATE for loops with complicated bounds. In the following example, the
compiler would have to generate a divide function call to determine, at run time, the number of iterations
performed.
for(i2 = ipos[2]; i2 < 40; i2 += 5) { ...
The compiler will not do the above. In this case, using MUST_ITERATE to specify that the loop always executes
eight times allows the compiler to attempt to generate a loop:
#pragma MUST_ITERATE(8, 8)
for(i2 = ipos[2]; i2 < 40; i2 += 5) { ...
108 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
Note
When using these pragmas in non-volatile FRAM memory, the memory region could be protected
against unintended writes through the device's Memory Protection Unit. Some devices have memory
protection enabled by default. Please see the information about memory protection in the datasheet
for your device. If the Memory Protection Unit is enabled, it first needs to be disabled before modifying
the variables.
If you are using non-volatile RAM, you can define a persistent variable with an initial value of zero loaded into
RAM. The program can increment that variable over time as a counter, and that count will not disappear if the
device loses power and restarts, because the memory is non-volatile and the boot routines do not initialize it
back to zero. For example:
#pragma PERSISTENT(x)
#pragma location = 0xC200 // memory address in RAM
int x = 0;
void main() {
run_init();
while (1) {
run_actions(x);
__delay_cycles(1000000);
x++;
}
}
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 109
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
For information about interactions between command-line options, pragmas, and keywords that affect inlining,
see Section 2.11.
5.11.21 The NO_HOOKS Pragma
The NO_HOOKS pragma prevents entry and exit hook calls from being generated for a function.
The syntax of the pragma in C is:
#pragma NO_HOOKS ( func )
110 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
This pragma is not part of the C or C++ standard, but it is a widely-supported preprocessor directive. Note that
this pragma does not protect against the inclusion of a header file with the same contents that has been copied
to another directory.
5.11.23 The pack Pragma
The pack pragma can be used to control the alignment of fields within a class, struct, or union type. The syntax
of the pragma in C/C++ can be any of the following.
#pragma pack ( n )
The above form of the pack pragma affects all class, struct, or union type declarations that follow this pragma in
a file. It forces the maximum alignment of each field to be the value specified by n. Valid values for n are 1, 2, 4,
8, and 16 bytes.
#pragma pack ( push, n )
#pragma pack ( pop )
The above form of the pack pragma affects only class, struct, and union type declarations between push and
pop directives. (A pop directive with no prior push results in a warning diagnostic from the compiler.) The
maximum alignment of all fields declared is n. Valid values for n are 1, 2, 4, 8, and 16 bytes.
#pragma pack ( show )
The above form of the pack pragma sends a warning diagnostic to stderr to record the current state of the pack
pragma stack. You can use this form while debugging.
For more about packed fields, see Section 5.17.5.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 111
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
The C++ syntax for the corresponding attribute is as follows. No C attribute syntax is available. See Section
5.11.18.1 for an example that uses similar syntax.
[[TI::prob_iterate( min, max )]]
Where min and max are the minimum and maximum trip counts of the loop in the common case. The trip count
is the number of times a loop iterates. Both arguments are optional.
For example, PROB_ITERATE could be applied to a loop that executes for eight iterations in the majority of
cases (but sometimes may execute more or less than eight iterations):
#pragma PROB_ITERATE(8, 8)
If only the minimum expected trip count is known (say it is 5), the pragma would look like this:
#pragma PROB_ITERATE(5)
If only the maximum expected trip count is known (say it is 10), the pragma would look like this:
#pragma PROB_ITERATE(, 10) /* Note the blank field for min */
The rulespec parameter is a comma-separated list of rule numbers. See Section 5.3 for details.
112 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
The rulespec parameter is a comma-separated list of rule numbers. See Section 5.4 for details. See www.ti.com/
ulpadvisor for a list of rules.
5.11.27 The RETAIN Pragma
The RETAIN pragma can be applied to a code or data symbol.
It causes a .retain directive to be generated into the section that contains the definition of the symbol. The .retain
directive indicates to the linker that the section is ineligible for removal during conditional linking. Therefore,
regardless whether or not the section is referenced by another section in the application that is being compiled
and linked, it will be included in the output file result of the link.
The RETAIN pragma has the same effect as using the retain function or variable attribute. See Section 5.17.2
and Section 5.17.4, respectively.
The syntax of the pragma in C is:
#pragma RETAIN ( symbol )
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 113
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
In the Setting Section With SET_DATA_SECTION Pragma example, x and y are put in the section mydata. To
reset the current section to the default used by the compiler, a blank parameter should be passed to the pragma.
An easy way to think of the pragma is that it is like applying the CODE_SECTION or DATA_SECTION pragma to
all symbols below it.
Setting Section With SET_DATA_SECTION Pragma
#pragma SET_DATA_SECTION("mydata")
int x;
int y;
#pragma SET_DATA_SECTION()
The pragmas apply to both declarations and definitions. If applied to a declaration and not the definition, the
pragma that is active at the declaration is used to set the section for that symbol. Here is an example:
Setting a Section With SET_CODE_SECTION Pragma
#pragma SET_CODE_SECTION("func1")
extern void func1();
#pragma SET_CODE_SECTION()
...
void func1() { ... }
In the Setting a Section With SET_CODE_SECTION Pragma example, func1 is placed in section func1. If
conflicting sections are specified at the declaration and definition, a diagnostic is issued.
The current CODE_SECTION and DATA_SECTION pragmas and GCC attributes can be used to override the
SET_CODE_SECTION and SET_DATA_SECTION pragmas. For example:
Overriding SET_DATA_SECTION Setting
#pragma DATA_SECTION(x, "x_data")
#pragma SET_DATA_SECTION("mydata")
int x;
int y;
#pragma SET_DATA_SECTION()
In the Overriding SET_DATA_SECTION Setting example, x is placed in x_data and y is placed in mydata. No
diagnostic is issued for this case.
The pragmas work for both C and C++. In C++, the pragmas are ignored for templates and for implicitly created
objects, such as implicit constructors and virtual function tables.
The SET_DATA_SECTION pragma takes precedence over the --gen_data_subsections=on option if it is used.
114 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
Calls to the applied function are compiled as software interrupts whose number is swi_number. The swi_number
variable must be an integer constant.
A function prototype must exist for the alias and it must occur after the pragma and before the alias is used.
Software interrupts whose number is not known until run time are not supported.
For information about using the GCC function attribute syntax to declare function aliases, see Section 5.17.2.
For more information about using software interrupts, including restrictions on passing arguments and register
usage, see Section 6.7.5.
Using the SWI_ALIAS Pragma C Source File
#pragma SWI_ALIAS(put, 48) /* #pragma SWI_ALIAS(48) for C++ */
int put (char *key, int value);
void error();
main()
{
if (!put("one", 1)) /* calling "put" invokes SWI #48 with 2 arguments */
error(); /* and returns a result. */
}
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 115
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
The C++ syntax for the corresponding attribute is as follows. No C attribute syntax is available. See Section
5.11.18.1 for an example that uses similar syntax.
[[TI::unroll( n )]]
If possible, the compiler unrolls the loop so there are n copies of the original loop. The compiler only unrolls if
it can determine that unrolling by a factor of n is safe. In order to increase the chances the loop is unrolled, the
compiler needs to know certain properties:
• The loop iterates a multiple of n times. This information can be specified to the compiler via the multiple
argument in the MUST_ITERATE pragma.
• The smallest possible number of iterations of the loop
• The largest possible number of iterations of the loop
The compiler can sometimes obtain this information itself by analyzing the code. However, sometimes the
compiler can be overly conservative in its assumptions and therefore generates more code than is necessary
when unrolling. This can also lead to not unrolling at all. Furthermore, if the mechanism that determines when
the loop should exit is complex, the compiler may not be able to determine these properties of the loop. In these
cases, you must tell the compiler the properties of the loop by using the MUST_ITERATE pragma.
Specifying #pragma UNROLL(1) asks that the loop not be unrolled. Automatic loop unrolling also is not
performed in this case.
If multiple UNROLL pragmas are specified for the same loop, it is undefined which pragma is used, if any.
116 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
The WEAK pragma makes symbol a weak reference if it is a reference, or a weak definition, if it is a definition.
The symbol can be a data or function variable. In effect, unresolved weak references do not cause linker errors
and do not have any effect at run time. The following apply for weak references:
• Libraries are not searched to resolve weak references. It is not an error for a weak reference to remain
unresolved.
• During linking, the value of an undefined weak reference is:
– Zero if the relocation type is absolute
– The address of the place if the relocation type is PC-relative
– The address of the nominal base address if the relocation type is base-relative.
A weak definition does not change the rules by which object files are selected from libraries. However, if a link
set contains both a weak definition and a non-weak definition, the non-weak definition is always used.
The WEAK pragma has the same effect as using the weak function or variable attribute. See Section 5.17.2 and
Section 5.17.4, respectively.
5.12 The _Pragma Operator
The ARM C/C++ compiler supports the C99 preprocessor _Pragma() operator. This preprocessor operator is
similar to #pragma directives. However, _Pragma can be used in preprocessing macros (#defines).
The syntax of the operator is:
_Pragma (" string_literal ");
The argument string_literal is interpreted in the same way the tokens following a #pragma directive are
processed. The string_literal must be enclosed in quotes. A quotation mark that is part of the string_literal
must be preceded by a backward slash.
You can use the _Pragma operator to express #pragma directives in macros. For example, the DATA_SECTION
syntax:
#pragma DATA_SECTION( func ," section ")
Is represented by the _Pragma() operator syntax:
_Pragma ("DATA_SECTION( func ,\" section \")")
The following code illustrates using _Pragma to specify the DATA_SECTION pragma in a macro:
...
#define EMIT_PRAGMA(x) _Pragma(#x)
#define COLLECT_DATA(var) EMIT_PRAGMA(DATA_SECTION(var,"mysection"))
COLLECT_DATA(x)
int x;
...
The EMIT_PRAGMA macro is needed to properly expand the quotes that are required to surround the section
argument to the DATA_SECTION pragma.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 117
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
118 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 119
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
120 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
Table 5-4 shows the calling syntax for each intrinsic, along with the corresponding assembly instruction and a
description. See Table 5-3 for a list of which intrinsics are available on the different ARM targets. Additional
intrinsices for getting and setting the CPSR register and to enable/disable interrupts are provided in Section
6.8.1.
Table 5-4. ARM Compiler Intrinsics
Assembly
C/C++ Compiler Intrinsic Instruction Description
int count = _ _clz(int src ); CLZ count , src Returns the count of leading zeros.
void _ _delay_cycles( unsigned int cycles ); varies Delays execution for the specified number
of cycles. The number of cycles must be a
constant.
The __delay_cycles intrinsic inserts code to
consume precisely the number of specified
cycles with no side effects. The number
of cycles delayed must be a compile-time
constant.
Note: Cycle timing is based on 0 wait states.
Results vary with additional wait states. The
implementation does not account for dynamic
prediction. Lower delay cycle counts may be
less accurate given pipeline flush behaviors.
unsigned int dst = _ _get_MSP(void ); MRS dst , MSP Returns the current value of the Main Stack
Pointer.
unsigned int dst = _ _get_PRIMASK(void ); MRS dst , PRIMASK Returns the current value of the Priority
Mask Register. If this value is 1, activation
of all exceptions with configurable priority is
prevented.
unsigned int dest = _ _ldrex(void* src ); LDREX dst , src Loads data from memory address containing
word (32-bit) data
unsigned int dest= _ _ldrexb(void* src ); LDREXB dst , src Loads data from memory address containing
byte data
unsigned long long dest = _ _ldrexd(void* src ); LDREXD dst , src Loads data from memory address with long
long support
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 121
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
122 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 123
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
124 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 125
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
126 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
In addition, the compiler supports many of the intrinsics described in the ARM C Language Extensions (ACLE)
specification. These intrinsics are applicable for the Cortex-M and Cortex-R processor variants. The ACLE
intrinsics are implemented in order to support the development of source code that can be compiled using
ACLE-compliant compilers from multiple vendors for a variety of ARM processors. A number of the intrinsics are
duplicates of intrinsics listed in the previous table but with slightly different names (such as one vs. two leading
underscores).
The compiler does not support all of the ACLE intrinsics listed in the ACLE specification. For example, the
__cls, __clsl, and __clsll ACLE intrinsics are not supported, because the CLS instruction is not available on the
Cortex-M or Cortex-R architectures.
In order to use the ACLE intrinsics, your code must include the provided arm_acle.h header file. For
details about the ACLE intrinsics, see the ACLE specification. For information about which ACLE intrinsics
are supported, see the arm_acle.h file. Where applicable, the declarations of ACLE intrinsics that are not
supported are enclosed in comments in that header file along with a brief explanation of why the intrinsic is not
supported and a reference to the appropriate section in the ACLE specification where the intrinsic is described.
5.15 Object File Symbol Naming Conventions (Linknames)
Each externally visible identifier is assigned a unique symbol name to be used in the object file, a so-called
linkname. This name is assigned by the compiler according to an algorithm which depends on the name, type,
and source language of the symbol. This algorithm may add a prefix to the identifier (typically an underscore),
and it may mangle the name.
User-defined symbols in C code and in assembly code are stored in the same namespace, which means you
are responsible for making sure that your C identifiers do not collide with your assembly code identifiers. You
may have identifiers that collide with assembly keywords (for instance, register names); in this case, the compiler
automatically uses an escape sequence to prevent the collision. The compiler escapes the identifier with double
parallel bars, which instructs the assembler not to treat the identifier as a keyword. You are responsible for
making sure that C identifiers do not collide with user-defined assembly code identifiers.
Name mangling encodes the types of the parameters of a function in the linkname for a function. Name mangling
only occurs for C++ functions which are not declared 'extern "C"'. Mangling allows function overloading, operator
overloading, and type-safe linking. Be aware that the return value of the function is not encoded in the mangled
name, as C++ functions cannot be overloaded based on the return value.
For example, the general form of a C++ linkname for a 32-bit function named func is:
__func__F parmcodes
Where parmcodes is a sequence of letters that encodes the parameter types of func.
For this simple C++ source file:
int foo(int I){ } //global C++ function compiled in 16-bit mode
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 127
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
The linkname of foo is $__foo__Fi, indicating that foo is a 16-bit function that takes a single argument of type int.
To aid inspection and debugging, a name demangling utility is provided that demangles names into those found
in the original C++ source. See Chapter 8 for more information.
The mangling algorithm follows that described in the Itanium C++ ABI (http://www.codesourcery.com/cxx-abi/
abi.html).
int foo(int i) { } would be mangled "_Z3fooi"
Note
EABI Mode C++ Demangling: The EABI mode has a different C++ demangling scheme. For
instance, there is no prefix (either _ or $). Please refer to the ARM Information Center for details.
This definition enables full portability. Defining the symbol to 0 specifies that the "C standard" portability level will
be used.
5.16.1 C99 Support (--c99)
The compiler supports the 1999 standard of C as standardized by the ISO. However, the following list of run-time
functions and features are not implemented or fully supported:
• inttypes.h
– wcstoimax() / wcstoumax()
• stdio.h
– The %e specifier may produce "-0" when "0" is expected by the standard
– snprintf() does not properly pad with spaces when writing to a wide character array
• stdlib.h
– vfscanf() / vscanf() / vsscanf() return value on floating point matching failure is incorrect
• wchar.h
– getws() / fputws()
128 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
– mbrlen()
– mbsrtowcs()
– wcscat()
– wcschr()
– wcscmp() / wcsncmp()
– wcscpy() / wcsncpy()
– wcsftime()
– wcsrtombs()
– wcsstr()
– wcstok()
– wcsxfrm()
– Wide character print / scan functions
– Wide character conversion functions
5.16.2 C11 Support (--c11)
The compiler supports the 2011 standard of C as standardized by the ISO. However, in addition to the list
in Section 5.16.1, the following run-time functions and features are not implemented or fully supported in C11
mode:
• threads.h
5.16.3 Strict ANSI Mode and Relaxed ANSI Mode (--strict_ansi and --relaxed_ansi)
Under relaxed ANSI/ISO mode (the default), the compiler accepts language extensions that could potentially
conflict with a strictly conforming ANSI/ISO C/C++ program. Under strict ANSI mode, these language extensions
are suppressed so that the compiler will accept all strictly conforming programs.
Use the --strict_ansi option when you know your program is a conforming program and it will not compile
in relaxed mode. In this mode, language extensions that conflict with ANSI/ISO C/C++ are disabled and the
compiler will emit error messages where the standard requires it to do so. Violations that are considered
discretionary by the standard may be emitted as warnings instead.
Examples:
The following is strictly conforming C code, but will not be accepted by the compiler in the default relaxed mode.
To get the compiler to accept this code, use strict ANSI mode. The compiler will suppress the interrupt keyword
language exception, and interrupt may then be used as an identifier in the code.
int main()
{
int interrupt = 0;
return 0;
}
The following is not strictly conforming code. The compiler will not accept this code in strict ANSI mode. To get
the compiler to accept it, use relaxed ANSI mode. The compiler will provide the interrupt keyword extension and
will accept the code.
interrupt void isr(void);
int main()
{
return 0;
}
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 129
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
The following code is accepted in all modes. The __interrupt keyword does not conflict with the ANSI/ISO C
standard, so it is always available as a language extension.
__interrupt void isr(void);
int main()
{
return 0;
}
The default mode is relaxed ANSI. This mode can be selected with the --relaxed_ansi (or -pr) option. Relaxed
ANSI mode accepts the broadest range of programs. It accepts all TI language extensions, even those which
conflict with ANSI/ISO, and ignores some ANSI/ISO violations for which the compiler can do something
reasonable. Some GCC language extensions described in Section 5.17 may conflict with strict ANSI/ISO
standards, but many do not conflict with the standards.
5.17 GNU , Clang, and ACLE Language Extensions
The GNU compiler collection (GCC) defines a number of language features not found in the ANSI/ISO C and
C++ standards. The definition and examples of these extensions (for GCC version 4.7) can be found at the GNU
web site, http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/C-Extensions.html#C-Extensions. Most of these extensions
are also available for C++ source code.
The compiler also supports the following Clang macro extensions, which are described in the Clang 6
Documentation:
• __has_feature (up to tests described for Clang 3.5)
• __has_extension (up to tests described for Clang 3.5)
• __has_include
• __has_include_next
• __has_builtin (see Section 5.17.6)
• __has_attribute
In addition, the compiler supports many of the features described in the ARM C Language Extensions (ACLE)
specification. These features are applicable for the Cortex-M and Cortex-R processor variants. ACLE support
affects the pre-defined macros (Table 2-31), function attributes (Section 5.17.2), and intrinsics (Section 5.14) you
may use in C/C++ code. These features are implemented in order to support the development of source code
that can be compiled using ACLE-compliant compilers from multiple vendors for a variety of ARM processors.
5.17.1 Extensions
Most of the GCC language extensions are available in the TI compiler when compiling in relaxed ANSI mode
(--relaxed_ansi).
The extensions that the TI compiler supports are listed in Table 5-5, which is based on the list of extensions
found at the GNU web site. The shaded rows describe extensions that are not supported.
Table 5-5. GCC Language Extensions
Extensions Descriptions
Statement expressions Putting statements and declarations inside expressions (useful for creating smart 'safe' macros)
Local labels Labels local to a statement expression
Labels as values Pointers to labels and computed gotos
Nested functions As in Algol and Pascal, lexical scoping of functions
Constructing calls Dispatching a call to another function
Naming types(1) Giving a name to the type of an expression
typeof operator typeof referring to the type of an expression
Generalized lvalues Using question mark (?) and comma (,) and casts in lvalues
Conditionals Omitting the middle operand of a ?: expression
long long Double long word integers and long long int type
130 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
(1) Feature defined for GCC 3.0; definition and examples at http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/C-Extensions.html#C-Extensions
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 131
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
The aligned function attribute aligns the function using the specified alignment. The alignment must be a power
of 2.
The always_inline function attribute has the same effect as the FUNC_ALWAYS_INLINE pragma. See Section
5.11.12
The calls attribute has the same effect as the CALLS pragma, which is described in Section 5.11.1.
The format attribute is applied to the declarations of printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf,
vsnprintf, scanf, fscanf, vfscanf, vscanf, vsscanf, and sscanf in stdio.h. Thus when GCC extensions are enabled,
the data arguments of these functions are type checked against the format specifiers in the format string
argument and warnings are issued when there is a mismatch. These warnings can be suppressed in the usual
ways if they are not desired.
See Section 5.11.16 for more about using the interrupt function attribute.
The malloc attribute is applied to the declarations of malloc, calloc, realloc and memalign in stdlib.h.
The naked attribute identifies functions written as embedded assembly functions using __asm statements. The
compiler does not generate prologue and epilog sequences for such functions. See Section 5.10.
The noinline function attribute has the same effect as the FUNC_CANNOT_INLINE pragma. See Section
5.11.13
132 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
The ramfunc attribute specifies that a function will be placed in and executed from RAM. The ramfunc attribute
allows the compiler to optimize functions for RAM execution, as well as to automatically copy functions to RAM
on flash-based devices. For example:
__attribute__((ramfunc))
void f(void) {
...
}
The --ramfunc=on option specifies that all functions compiled with this option are placed in and executed from
RAM, even if this function attribute is not used.
Newer TI linker command files support the ramfunc attribute automatically by placing functions with this attribute
in the .TI.ramfunc section. If you have a linker command file that does not include a section specification for
the .TI.ramfunc section, you can modify the linker command file to place this section in RAM. See the ARM
Assembly Language Tools User's Guide for details on section placement.
The target attribute causes a function to be compiled in either ARM (32-bit) or Thumb (16-bit) mode. The target
attribute has the same effect as the CODE_STATE pragma. The following examples use the target attribute.
__attribute__((target("arm"))) void foo(int arg1, int arg2)
__attribute__((target("thumb"))) void foo(int arg1, int arg2)
Note that the "pcs" attribute described in the ACLE specification is not supported.
The retain attribute has the same effect as the RETAIN pragma (Section 5.11.27). That is, the section that
contains the function will not be omitted from conditionally linked output even if it is not referenced elsewhere in
the application.
The section attribute when used on a function has the same effect as the CODE_SECTION pragma. See
Section 5.11.4
The weak attribute has the same effect as the WEAK pragma (Section 5.11.32).
5.17.3 For Loop Attributes
If you are using C++, there are several TI-specific attributes that can be applied to loops. No corresponding
syntax is available in C. The following TI-specific attributes have the same function as their corresponding
pragmas:
• TI::must_iterate
• TI::unroll
See Section 5.11.18.1 for an example that uses a for loop attribute.
5.17.4 Variable Attributes
The following variable attributes are supported:
• aligned
• deprecated
• location
• mode
• noinit
• packed
• persistent
• retain
• section
• transparent_union
• unused
• used
• weak
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 133
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
The aligned attribute used on a variable has the same effect as the DATA_ALIGN pragma. See Section 5.11.6
The location attribute has the same effect as the LOCATION pragma. See Section 5.11.17. For example:
__attribute__((location(0x100))) extern struct PERIPH peripheral;
The noinit and persistent attributes apply to the ROM initialization model and allow an application to avoid
initializing certain global variables during a reset. The alternative RAM initialization model initializes variables
only when the image is loaded; no variables are initialized during a reset. See the "RAM Model vs. ROM Model"
section and its subsections in the ARM Assembly Language Tools User's Guide.
The noinit attribute can be used on uninitialized variables; it prevents those variables from being set to 0
during a reset. The persistent attribute can be used on initialized variables; it prevents those variables from
being initialized during a reset. By default, variables marked noinit or persistent will be placed in sections
named .TI.noinit and .TI.persistent , respectively. The location of these sections is controlled by
the linker command file. Typically .TI.persistent sections are placed in FRAM for devices that support FRAM
and .TI.noinit sections are placed in RAM. Also see Section 5.11.19.
The packed attribute may be applied to individual fields within a struct or union. The packed attribute is
supported on all ARM targets. See the description of the --unaligned_access option for more information on how
the compiler accesses unaligned data.
The retain attribute has the same effect as the RETAIN pragma (Section 5.11.27). That is, the section that
contains the variable will not be omitted from conditionally linked output even if it is not referenced elsewhere in
the application.
The section attribute when used on a variable has the same effect as the DATA_SECTION pragma. See
Section 5.11.7
The used attribute is defined in GCC 4.2 (see http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Variable-
Attributes.html#Variable-Attributes).
The weak attribute has the same effect as the WEAK pragma (Section 5.11.32).
5.17.5 Type Attributes
The following type attributes are supported:
• aligned
• deprecated
• packed
• transparent_union
• unused
The packed attribute is supported for struct and union types. It is supported on all ARM targets if the
--relaxed_ansi option is used. See the description of the --unaligned_access option for more information on
how the compiler accesses unaligned data.
Members of a packed structure are stored as closely to each other as possible, omitting additional bytes of
padding usually added to preserve word-alignment. For example, assuming a word-size of 4 bytes ordinarily has
3 bytes of padding between members c1 and i, and another 3 bytes of trailing padding after member c2, leading
to a total size of 12 bytes:
struct unpacked_struct { char c1; int i; char c2;};
However, the members of a packed struct are byte-aligned. Thus the following does not have any bytes of
padding between or after members and totals 6 bytes:
struct __attribute__((__packed__)) packed_struct { char c1; int i; char c2; };
Subsequently, packed structures in an array are packed together without trailing padding between array
elements.
134 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C/C++ Language Implementation
Bit fields of a packed structure are bit-aligned. The byte alignment of adjacent struct members that are not bit
fields does not change. However, there are no bits of padding between adjacent bit fields.
The "packed" attribute can be applied only to the original definition of a structure or union type. It cannot be
applied with a typedef to a non-packed structure that has already been defined, nor can it be applied to the
declaration of a struct or union object. Therefore, any given structure or union type can only be packed or
non-packed, and all objects of that type will inherit its packed or non-packed attribute.
The "packed" attribute is not applied recursively to structure types that are contained within a packed structure.
Thus, in the following example the member s retains the same internal layout as in the first example above.
There is no padding between c and s, so s falls on an unaligned boundary:
struct __attribute__((__packed__)) outer_packed_struct { char c; struct unpacked_struct s; };
It is illegal to implicitly or explicitly cast the address of a packed struct member as a pointer to any non-packed
type except an unsigned char. In the following example, p1, p2, and the call to foo are all illegal.
void foo(int *param);
struct packed_struct ps;
int *p1 = &ps.i;
int *p2 = (int *)&ps.i;
foo(&ps.i);
However, it is legal to explicitly cast the address of a packed struct member as a pointer to an unsigned char:
unsigned char *pc = (unsigned char *)&ps.i;
The TI compiler also supports an unpacked attribute for an enumeration type to allow you to indicate that the
representation is to be an integer type that is no smaller than int; in other words, it is not packed.
5.17.6 Built-In Functions
The following built-in functions are supported:
• __builtin_abs()
• __builtin_constant_p()
• __builtin_expect()
• __builtin_fabs()
• __builtin_fabsf()
• __builtin_frame_address()
• __builtin_labs()
• __builtin_llabs()
• __builtin_sqrt()
• __builtin_sqrtf()
• __builtin_memcpy()
• __builtin_return_address()
The __builtin_frame_address() function always returns zero unless the argument is a constant zero.
The __builtin_sqrt() and __builtin_sqrtf() functions are supported only when hardware floating point support is
enabled. In addition, the __builtin_sqrt() function is not supported if --float_support is set to fpv4spd16.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 135
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C/C++ Language Implementation www.ti.com
When calling built-in functions that may be unavailable at run-time, use the Clang __has_builtin macro as shown
in the following example to make sure the function is supported:
#if __has_builtin(__builtin_sqrt)
double estimate = __builtin_sqrt(x);
#else
double estimate = fast_approximate_sqrt(x);
#endif
If the built-in function is supported and the device has the appropriate hardware support, the built-in function will
invoke the hardware support.
If the built-in function is supported but the device does not have the appropriate hardware enabled, the built-in
function will usually become a call to an RTS library function. For example, __builtin_sqrt() will become a call to
the library function sqrt().
The __builtin_return_address() function always returns zero.
5.18 AUTOSAR
The ARM compiler supports the AUTOSAR 3.1 standard by providing the following header files:
• Compiler.h
• Platform_Types.h
• Std_Types.h
• Compiler_Cfg.h
Compiler_Cfg.h is an empty file, the contents of which should be provided by the end user. The provided file
contains information on what the contents of the file should look like. It is included by Compiler.h. If a new
Compiler_Cfg.h file is provided by the user, its include path must come before the path to the run-time-support
header files.
More information on AUTOSAR can be found at http://www.autosar.org.
5.19 Compiler Limits
Due to the variety of host systems supported by the C/C++ compiler and the limitations of some of these
systems, the compiler may not be able to successfully compile source files that are excessively large or
complex. In general, exceeding such a system limit prevents continued compilation, so the compiler aborts
immediately after printing the error message. Simplify the program to avoid exceeding a system limit.
Some systems do not allow filenames longer than 500 characters. Make sure your filenames are shorter than
500.
The compiler has no arbitrary limits but is limited by the amount of memory available on the host system.
On smaller host systems such as PCs, the optimizer may run out of memory. If this occurs, the optimizer
terminates and the shell continues compiling the file with the code generator. This results in a file compiled with
no optimization. The optimizer compiles one function at a time, so the most likely cause of this is a large or
extremely complex function in your source module. To correct the problem, your options are:
• Don't optimize the module in question.
• Identify the function that caused the problem and break it down into smaller functions.
• Extract the function from the module and place it in a separate module that can be compiled without
optimization so that the remaining functions can be optimized.
136 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
Chapter 6
Run-Time Environment
This chapter describes the ARM C/C++ run-time environment. To ensure successful execution of C/C++
programs, it is critical that all run-time code maintain this environment. It is also important to follow the guidelines
in this chapter if you write assembly language functions that interface with C/C++ code.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 137
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
Note
The Linker Defines the Memory Map
The linker, not the compiler, defines the memory map and allocates code and data into target memory.
The compiler assumes nothing about the types of memory available, about any locations not available
for code or data (holes), or about any locations reserved for I/O or control purposes. The compiler
produces relocatable code that allows the linker to allocate code and data into the appropriate
memory spaces. For example, you can use the linker to allocate global variables into on-chip RAM
or to allocate executable code into external ROM. You can allocate each block of code or data
individually into memory, but this is not a general practice (an exception to this is memory-mapped I/O,
although you can access physical memory locations with C/C++ pointer types).
6.1.1 Sections
The compiler produces relocatable blocks of code and data called sections, which are allocated in memory in
a variety of ways to conform to a various system configurations. For information about sections and allocating
them, see the introductory object file information in the ARM Assembly Language Tools User's Guide.
There are two basic types of sections:
• Initialized sections contain data or executable code. Initialized sections are usually read-only; exceptions
are noted below. The C/C++ compiler creates the following initialized sections:
– The .binit section contains boot time copy tables. For details on BINIT, see the ARM Assembly Language
Tools User's Guide.
– The .init_array section contains global constructor tables.
– The .ovly section contains copy tables for unions in which different sections have the same run address.
– The .data section contains initialized global and static variables. This section is writable.
– The .const section contains read-only data, typically string constants and static-scoped objects defined
with the C/C++ qualifier const. Note that not all static-scoped objects marked "const" are placed in
the .const section (see Section 5.7.1).
– The .text section contains all the executable code. It also contains string literals, switch tables, and
compiler-generated constants. This section is usually read-only. Note that some string literals may instead
be placed in .const:.string. The placement of string literals depends on the size of the string and the use of
the --embedded_constants option.
– The .TI.crctab section contains CRC checking tables.
• Uninitialized sections reserve space in memory (usually RAM). A program can use this space at run time to
create and store variables. The compiler creates the following uninitialized sections:
– For EABI only, the .bss section reserves space for uninitialized global and static variables. Uninitialized
variables that are also unused are usually created as common symbols (unless you specify --common=off)
instead of being placed in .bss so that they can be excluded from the resulting application.
– The .stack section reserves memory for the C/C++ software stack.
– The .sysmem section reserves space for dynamic memory allocation. This space is used by dynamic
memory allocation routines, such as malloc(), calloc(), realloc(), or new(). If a C/C++ program does not
use these functions, the compiler does not create the .sysmem section.
The assembler creates the default sections .text, .bss, and .data. You can instruct the compiler to create
additional sections by using the CODE_SECTION and DATA_SECTION pragmas (see Section 5.11.4 and
Section 5.11.7).
The linker takes the individual sections from different object files and combines sections that have the same
name. The resulting output sections and the appropriate placement in memory for each section are listed in
138 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
Table 6-1. You can place these output sections anywhere in the address space as needed to meet system
requirements.
Table 6-1. Summary of Sections and Memory Placement
Section Type of Memory Section Type of Memory
.bss RAM .pinit ROM or RAM
.cinit ROM or RAM .stack RAM
.const ROM or RAM .sysmem RAM
.data RAM .text ROM or RAM
.init_array ROM or RAM
You can use the SECTIONS directive in the linker command file to customize the section-allocation process. For
more information about allocating sections into memory, see the linker description chapter in the ARM Assembly
Language Tools User's Guide.
6.1.2 C/C++ System Stack
The C/C++ compiler uses a stack to:
• Allocate local variables
• Pass arguments to functions
• Save register contents
The run-time stack grows from the high addresses to the low addresses. The compiler uses the R13 register to
manage this stack. R13 is the stack pointer (SP), which points to the next unused location on the stack.
The linker sets the stack size, creates a global symbol, __TI_STACK_SIZE, and assigns it a value equal to the
stack size in bytes. The default stack size is 2048 bytes. You can change the stack size at link time by using
the --stack_size option with the linker command. For more information on the --stack_size option, see the linker
description chapter in the ARM Assembly Language Tools User's Guide.
At system initialization, SP is set to a designated address for the top of the stack. This address is the first
location past the end of the .stack section. Since the position of the stack depends on where the .stack section is
allocated, the actual address of the stack is determined at link time.
The C/C++ environment automatically decrements SP at the entry to a function to reserve all the space
necessary for the execution of that function. The stack pointer is incremented at the exit of the function to
restore the stack to the state before the function was entered. If you interface assembly language routines to
C/C++ programs, be sure to restore the stack pointer to the same state it was in before the function was entered.
For more information about using the stack pointer, see Section 6.3; for more information about the stack, see
Section 6.4.
Note
Stack Overflow: The compiler provides no means to check for stack overflow during compilation or
at run time. A stack overflow disrupts the run-time environment, causing your program to fail. Be sure
to allow enough space for the stack to grow. You can use the --entry_hook option to add code to the
beginning of each function to check for stack overflow; see Section 2.15.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 139
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
If you use any C I/O function, the RTS library allocates an I/O buffer for each file you access. This buffer will be
a bit larger than BUFSIZ, which is defined in stdio.h and defaults to 256. Make sure you allocate a heap large
enough for these buffers or use setvbuf to change the buffer to a statically-allocated buffer.
Dynamically allocated objects are not addressed directly (they are always accessed with pointers) and the
memory pool is in a separate section (.sysmem). Therefore, the dynamic memory pool size may be limited only
by the amount of memory in your system. To conserve space in the .bss section, you can allocate large arrays
from the heap instead of defining them as global or static. For example, instead of a definition such as:
struct big table[100];
When allocating from a heap, make sure the size of the heap is large enough for the allocation. This is
particularly important when allocating variable-length arrays.
6.2 Object Representation
For general information about data types, see Section 5.5. This section explains how various data objects are
sized, aligned, and accessed.
6.2.1 Data Type Storage
Table 6-2 lists register and memory storage for various data types:
Table 6-2. Data Representation in Registers and Memory
Data Type Register Storage Memory Storage
char, signed char Bits 0-7 of register(1) 8 bits aligned to 8-bit boundary
unsigned char, bool Bits 0-7 of register 8 bits aligned to 8-bit boundary
short, signed short Bits 0-15 of register(1) 16 bits aligned to 16-bit (halfword) boundary
unsigned short, wchar_t Bits 0-15 of register 16 bits aligned to 16-bit (halfword) boundary
int, signed int Bits 0-31 of register 32 bits aligned to 32-bit (word) boundary
unsigned int Bits 0-31 of register 32 bits aligned to 32-bit (word) boundary
long, signed long Bits 0-31 of register 32 bits aligned to 32-bit (word) boundary
unsigned long Bits 0-31 of register 32 bits aligned to 32-bit (word) boundary
long long Even/odd register pair 64 bits aligned to 32-bit (word) boundary(2)
unsigned long long Even/odd register pair 64 bits aligned to 32-bit (word) boundary(2)
float Bits 0-31 of register 32 bits aligned to 32-bit (word) boundary
double Register pair 64 bits aligned to 32-bit (word) boundary(2)
long double Register pair 64 bits aligned to 32-bit (word) boundary(2)
struct Members stored as individual types require. Members are stored as their individual types
require; aligned according to the member with the
most restrictive alignment requirement.
array Members stored as individual types require. Members are stored as their individual types
require; aligned to 32-bit (word) boundary. All
arrays inside a structure are aligned according to
the type of each element in the array.
pointer to data member Bits 0-31 of register 32 bits aligned to 32-bit (word) boundary
pointer to member function Components stored as individual types require 64 bits aligned to 32-bit (word) boundary
For details about the size of an enum type, see Table 5-2.
140 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
MS LS
S S S S S S S S S S S S S S S S S S S S S S S S S S I I I I I I
31 7 0
MS LS
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 U U U U U U U
31 7 0
MS LS
S S S S S S S S S S S S S S S S S I I I I I I I I I I I I I I I
31 15 0
MS LS
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 U U U U U U U U U U U U U U U U
31 15 0
LEGEND: S = sign, I = signed integer, U = unsigned integer, MS = most significant, LS = least significant
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 141
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
6.2.1.2 float, int, and long Data Types (signed and unsigned)
The int, unsigned int, float, long and unsigned long data types are stored in memory as 32-bit objects at word (4
byte) aligned addresses. Objects of these types are loaded to and stored from bits 0-31 of a register, as shown
in Figure 6-2. In big-endian mode, 4-byte objects are loaded to registers by moving the first byte (that is, the
lower address) of memory to bits 24-31 of the register, moving the second byte of memory to bits 16-23, moving
the third byte to bits 8-15, and moving the fourth byte to bits 0-7. In little-endian mode, 4-byte objects are loaded
to registers by moving the first byte (that is, the lower address) of memory to bits 0-7 of the register, moving the
second byte to bits 8-15, moving the third byte to bits 16-23, and moving the fourth byte to bits 24-31.
Figure 6-2. 32-Bit Data Storage Format
Single-precision floating char
MS LS
S E E E E E E E E M M M M M M M M M M M M M M M M M M M M M M M
31 23 0
MS LS
S I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I I
31 0
MS LS
U U U U U U U U U U U U U U U U U U U U U U U U U U U U U U U U
31 0
LEGEND: S = sign, M = Mantissa, U = unsigned integer, E = exponent, I = signed integer, MS = most significant, LS = least significant
142 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
6.2.1.3 double, long double, and long long Data Types (signed and unsigned)
Double, long double, long long and unsigned long long data types are stored in memory in a pair of registers and
are always referenced as a pair. These types are stored as 64-bit objects at word (4 byte) aligned addresses.
For FPA mode, the word at the lowest address contains the sign bit, the exponent, and the most significant part
of the mantissa. The word at the higher address contains the least significant part of the mantissa. This is true
regardless of the endianness of the target. For VFP mode, the words are ordered based upon the endianness of
the target.
Objects of this type are loaded into and stored in register pairs, as shown in the following figure. The most
significant memory word contains the sign bit, exponent, and the most significant part of the mantissa. The least
significant memory word contains the least significant part of the mantissa.
Figure 6-3. Double-Precision Floating-Point Data Storage Format
Address x
MS
S E E E E E E E E E E E E M M M M M M M M M M M M M M M M M M M
31 20 0
Address x+ 4
LS
M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M
31 0
The parameter d is the offset to be added to the beginning of the class object for this pointer. The parameter I
is the index into the virtual function table, offset by 1. The index enables the NULL pointer to be represented. Its
value is -1 if the function is non-virtual. The parameter f is the pointer to the member function if it is non-virtual,
when I is 0. The 0 is the offset to the virtual function pointer within the class object.
6.2.1.6 Structure and Array Alignment
Structures are aligned according to the member with the most restrictive alignment requirement. Structures are
padded so that the size of the structure is a multiple of its alignment. Arrays are always word aligned. Elements
of arrays are stored in the same manner as if they were individual objects.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 143
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
In this example, bar () is never called as bit field 'a' is unsigned. Use signed int if you need a signed bit field.
• Bit fields of type long long are supported.
• Bit fields are treated as the declared type.
• The size and alignment of the struct containing the bit field depends on the declared type of the bit field. For
example, consider the struct:
struct st {int a:4};
144 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
Figure 6-4 illustrates bit-field packing, using the following bit field definitions:
struct{
int A:7
int B:10
int C:3
int D:2
int E:9
}x;
A0 represents the least significant bit of the field A; A1 represents the next least significant bit, etc. Again,
storage of bit fields in memory is done with a byte-by-byte, rather than bit-by-bit, transfer.
Figure 6-4. Bit-Field Packing in Big-Endian and Little-Endian Formats
Big-endian register
MS LS
A A A A A A A B B B B B B B B B B C C C D D E E E E E E E E E X
6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 2 1 0 1 0 8 7 6 5 4 3 2 1 0 X
31 0
Big-endian memory
A A A A A A A B B B B B B B B B B C C C D D E E E E E E E E E X
6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 2 1 0 1 0 8 7 6 5 4 3 2 1 0 X
Little-endian register
MS LS
X E E E E E E E E E D D C C C B B B B B B B B B B A A A A A A A
X 8 7 6 5 4 3 2 1 0 1 0 2 1 0 9 8 7 6 5 4 3 2 1 0 6 5 4 3 2 1 0
31 0
Little-endian memory
B A A A A A A A B B B B B B B B E E D D C C C B X E E E E E E E
0 6 5 4 3 2 1 0 8 7 6 5 4 3 2 1 1 0 1 0 2 1 0 9 X 8 7 6 5 4 3 2
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 145
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
When a string is used as an initializer, it is simply treated as an initialized array; each character is a separate
initializer. For more information about initialization, see Section 6.10.
• In an expression. For example:
strcpy (s, "abc");
When a string is used in an expression, the string itself is defined in the .const section with the .string
assembler directive, along with a unique label that points to the string; the terminating 0 byte is included.
For example, the following lines define the string abc, and the terminating 0 byte (the label SL5 points to the
string):
.sect ".const"
SL5: .string "abc",0
String labels have the form SLn, where n is a number assigned by the compiler to make the label unique.
The number begins at 0 and is increased by 1 for each string defined. All strings used in a source module are
defined at the end of the compiled assembly language module.
The label SLn represents the address of the string constant. The compiler uses this label to reference the
string expression.
Because strings are stored in the .const section (possibly in ROM) and shared, it is bad practice for a
program to modify a string constant. The following code is an example of incorrect string use:
const char *a = "abc"
a[1] = 'x'; /* Incorrect! undefined behavior */
146 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 147
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
148 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 149
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
• Save-on-entry registers. Registers R4-R11 and R14 (alternate names are V1-V8 and LR). It is the called
function's responsibility to preserve values in these registers. If the called function modifies these registers, it
saves them when it gains control and preserves them when it returns control to the calling function.
For details on the calling conventions in EABI mode or when using a VFP coprocessor, refer to the EABI
documentation located in the ARM Information Center.
Figure 6-5 illustrates a typical function call. In this example, arguments are passed to the function, and the
function uses local variables and calls another function. The first four arguments are passed to registers R0-R3.
This example also shows allocation of a local frame and argument block for the called function. Functions that
have no local variables and do not require an argument block do not allocate a local frame.
Move arguments to
argument block; Allocate new frame and
Before call call function argument block
Low Low Low
Callee’s SP
argument
block
Callee’s
local variables
Register
save area
Caller’s SP SP AP
Argument 5... Argument 1 → register R0 Argument 5...
argument
argument n Argument 2 → register R1 argument n
block
Argument 3 → register R2
Caller’s Caller’s Caller’s
Argument 4 → register R3
local variables local variables local variables
Register Register Register
High save area High High
save area save area
150 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 151
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
Since the stack grows toward smaller addresses, the local and argument data on the stack for the C/C++
function is accessed with a positive offset from the SP or the AP register.
6.5 Accessing Linker Symbols in C and C++
See the section on "Linker Symbols" in the ARM Assembly Language Tools User's Guide for information about
referring to linker symbols in C/C++ code.
6.6 Interfacing C and C++ With Assembly Language
The following are ways to use assembly language with C/C++ code:
• Use separate modules of assembled code and link them with compiled C/C++ modules (see Section 6.6.1).
• Use assembly language variables and constants in C/C++ source (see Section 6.6.3).
• Use inline assembly language embedded directly in the C/C++ source (see Section 6.6.5).
• Modify the assembly language code that the compiler produces (see Section 6.6.6).
6.6.1 Using Assembly Language Modules With C/C++ Code
Interfacing C/C++ with assembly language functions is straightforward if you follow the calling conventions
defined in Section 6.4, and the register conventions defined in Section 6.3. C/C++ code can access variables
and call functions defined in assembly language, and assembly code can access C/C++ variables and call
C/C++ functions.
Follow these guidelines to interface assembly language and C:
• You must preserve any dedicated registers modified by a function. Dedicated registers include:
– Save-on-entry registers (R4-R11 (alternate names are V1 to V8 and LR))
– Stack pointer (SP or R13)
If the SP is used normally, it does not need to be explicitly preserved. In other words, the assembly function is
free to use the stack as long as anything that is pushed onto the stack is popped back off before the function
returns (thus preserving SP).
Any register that is not dedicated can be used freely without first being saved.
• Interrupt routines must save all the registers they use. For more information, see Section 6.7.
• When you call a C/C++ function from assembly language, load the designated registers with arguments and
push the remaining arguments onto the stack as described in Section 6.4.1.
Remember that a function can alter any register not designated as being preserved without having to restore
it. If the contents of any of these registers must be preserved across the call, you must explicitly save them.
• Functions must return values correctly according to their C/C++ declarations. Double values are returned
in R0 and R1, and structures are returned as described in Step 2 of Section 6.4.1. Any other values are
returned in R0.
• No assembly module should use the .cinit section for any purpose other than autoinitialization of global
variables. The C/C++ startup routine assumes that the .cinit section consists entirely of initialization tables.
Disrupting the tables by putting other information in .cinit can cause unpredictable results.
• The compiler assigns linknames to all external objects. Thus, when you write assembly language code, you
must use the same linknames as those assigned by the compiler. See Section 5.15 for details.
152 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
• Any object or function declared in assembly language that is accessed or called from C/C++ must be
declared with the .def or .global directive in the assembly language modifier. This declares the symbol as
external and allows the linker to resolve references to it.
Likewise, to access a C/C++ function or object from assembly language, declare the C/C++ object with
the .ref or .global directive in the assembly language module. This creates an undeclared external reference
that the linker resolves.
6.6.2 Accessing Assembly Language Functions From C/C++
Functions defined in C++ that will be called from assembly should be defined as extern "C" in the C++ file.
Functions defined in assembly that will be called from C++ must be prototyped as extern "C" in C++.
Example 6-1 illustrates a C++ function called main, which calls an assembly language function called asmfunc,
Example 6-2. The asmfunc function takes its single argument, adds it to the C++ global variable called gvar, and
returns the result.
Example 6-1. Calling an Assembly Language Function From a C/C++ Program
extern "C" {
extern int asmfunc(int a); /* declare external asm function */
int gvar = 0; /* define global variable */
}
void main()
{
int I = 5;
I = asmfunc(I); /* call function normally */
.global asmfunc
.global gvar
asmfunc:
LDR r1, gvar_a
LDR r2, [r1, #0]
ADD r0, r0, r2
STR r0, [r1, #0]
MOV pc, lr
gvar_a .field gvar, 32
In the C++ program in Example 6-1, the extern "C" declaration tells the compiler to use C naming conventions
(that is, no name mangling). When the linker resolves the .global _asmfunc reference, the corresponding
definition in the assembly file will match.
The parameter i is passed in R0, and the result is returned in R0. R1 holds the address of the global gvar. R2
holds the value of gvar before adding the value i to it.
6.6.3 Accessing Assembly Language Variables From C/C++
It is sometimes useful for a C/C++ program to access variables or constants defined in assembly language.
There are several methods that you can use to accomplish this, depending on where and how the item is
defined: a variable defined in the .bss section, a variable not defined in the .bss section, or a linker symbol.
6.6.3.1 Accessing Assembly Language Global Variables
Accessing variables from the .bss section or a section named with .usect is straightforward:
1. Use the .bss or .usect directive to define the variable.
2. Use the .def or .global directive to make the definition external.
3. Use the appropriate linkname in assembly language.
4. In C/C++, declare the variable as extern and access it normally.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 153
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
Example 6-4 and Example 6-3 show how you can access a variable defined in .bss.
Example 6-3. Assembly Language Variable Program
Because you are referencing only the symbol's value as stored in the symbol table, the symbol's declared type is
unimportant. In Example 6-5, int is used. You can reference linker-defined symbols in a similar manner.
154 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
Note
Using the asm Statement: Keep the following in mind when using the asm statement:
• Be extremely careful not to disrupt the C/C++ environment. The compiler does not check or
analyze the inserted instructions.
• Avoid inserting jumps or labels into C/C++ code because they can produce unpredictable results
by confusing the register-tracking algorithms that the code generator uses.
• Do not change the value of a C/C++ variable when using an asm statement. This is because the
compiler does not verify such statements. They are inserted as is into the assembly code, and
potentially can cause problems if you are not sure of their effect.
• Do not use the asm statement to insert assembler directives that change the assembly
environment.
• Avoid creating assembly macros in C code and compiling with the --symdebug:dwarf (or -g) option.
The C environment’s debug information and the assembly macro expansion are not compatible.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 155
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
If a C/C++ interrupt routine does not call any other functions, only those registers that the interrupt handler
uses are saved and restored. However, if a C/C++ interrupt routine does call other functions, these functions
can modify unknown registers that the interrupt handler does not use. For this reason, the routine saves all the
save-on-call registers if any other functions are called. (This excludes banked registers.) Do not call interrupt
handling functions directly.
Interrupts can be handled directly with C/C++ functions by using the INTERRUPT pragma or the __interrupt
keyword. For information, see Section 5.11.16 and Section 5.7.2, respectively.
6.7.3 Using Assembly Language Interrupt Routines
You can handle interrupts with assembly language code as long as you follow the same register conventions the
compiler does. Like all assembly functions, interrupt routines can use the stack, access global C/C++ variables,
and call C/C++ functions normally. When calling C/C++ functions, be sure that any save-on-call registers are
preserved before the call because the C/C++ function can modify any of these registers. You do not need to
save save-on-entry registers because they are preserved by the called C/C++ function.
156 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
Note
This section does not apply to Cortex-M devices.
To map Cortex-A interrupt routines to interrupt vectors you need to include a intvecs.asm file. This file will
contain assembly language directives that can be used to set up the ARM's interrupt vectors with branches to
your interrupt routines. Follow these steps to use this file:
1. Using Example 6-7 as a guide, create intvecs.asm and include your interrupt routines. For each routine:
a. At the beginning of the file, add a .global directive that names the routine.
b. Modify the appropriate .word directive to create a branch to the name of your routine.
2. Assemble and link intvecs.asm with your applications code and with the compiler's linker control file
(lnk16.cmd or lnk32.cmd). The control file contains a SECTIONS directive that maps the .intvecs section
into the memory locations 0x00-0x1F.
For example, on an ARM v4 target, if you have written a C interrupt routine for the IRQ interrupt called c_intIRQ
and an assembly language routine for the FIQ interrupt called tim1_int, you should create intvecs.asm as in
Example 6-7.
Example 6-7. Sample intvecs.asm File
.if __TI_EABI_ASSEMBLER
.asg c_intIRQ, C_INTIRQ
.else
.asg _c_intIRQ, C_INTIRQ
.endif
.global _c_int00
.global C_INTIRQ
.global tim1_int
.sect ".intvecs"
B _c_int00 ; reset interrupt
.word 0 ; undefined instruction interrupt
.word 0 ; software interrupt
.word 0 ; abort (prefetch) interrupt
.word 0 ; abort (data) interrupt
.word 0 ; reserved
B C_INTIRQ ; IRQ interrupt
B tim1_int ; FIQ interrupt
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 157
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
158 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 159
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
• The _ _run_address_check function, which returns TRUE if the code performing the call is located at its
run-time address, as assigned by the linker. Otherwise, FALSE is returned. The syntax of the function is:
int _ _run_address_check(void);
160 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
_system_pre_init(): This function provides a place to perform application-specific initialization. It is invoked after
the stack pointer is initialized but before any C/C++ environment setup is performed. For targets that include
MPU support, this function is called after __mpu_init(). By default, _system_pre_init() should return a non-zero
value. The default C/C++ environment setup is bypassed if _system_pre_init() returns 0.
_system_post_cinit(): This function is invoked during C/C++ environment setup, after C/C++ global data is
initialized but before any C++ constructors are called. This function should not return a value.
The _c_int00( ) initialization routine also provides a mechanism for an application to perform the setup (set I/O
registers, enable/disable timers, etc.) before the C/C++ environment is initialized.
6.10.2 Run-Time Stack
The run-time stack is allocated in a single continuous block of memory and grows down from high addresses to
lower addresses. The SP points to the top of the stack.
The code does not check to see if the run-time stack overflows. Stack overflow occurs when the stack grows
beyond the limits of the memory space that was allocated for it. Be sure to allocate adequate memory for the
stack.
The stack size can be changed at link time by using the --stack_size link option on the linker command line and
specifying the stack size as a constant directly after the option.
The C/C++ boot routine shipped with the compiler sets up the user/thread mode run-time stack. If your program
uses a run-time stack when it is in other operating modes, you must also allocate space and set up the run-time
stack corresponding to those modes.
EABI requires that 64-bit data (type long long and long double) be aligned at 64-bits. This requires that the stack
be aligned at a 64-bit boundary at function entry so that local 64-bit variables are allocated in the stack with
correct alignment. The boot routine aligns the stack at a 64-bit boundary.
6.10.3 Automatic Initialization of Variables
Any global variables declared as preinitialized must have initial values assigned to them before a C/C++ program
starts running. The process of retrieving these variables' data and initializing the variables with the data is called
autoinitialization. Internally, the compiler and linker coordinate to produce compressed initialization tables. Your
code should not access the initialization table.
6.10.3.1 Zero Initializing Variables
In ANSI C, global and static variables that are not explicitly initialized must be set to 0 before program execution.
The C/C++ compiler supports preinitialization of uninitialized variables by default. This can be turned off by
specifying the linker option --zero_init=off.
Zero initialization takes place only if the --rom_model linker option, which causes autoinitialization to occur, is
used. If you use the --ram_model option for linking, the linker does not generate initialization records, and the
loader must handle both data and zero initialization.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 161
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
The compiler allocates the variables 'i' and 'a[] to .data section and the initial values are placed directly.
.global i
.data
.align 4
i:
.field 23,32 ; i @ 0
.global a
.data
.align 4
a:
.field 1,32 ; a[0] @ 0
.field 2,32 ; a[1] @ 32
.field 3,32 ; a[2] @ 64
.field 4,32 ; a[3] @ 96
.field 5,32 ; a[4] @ 128
Each compiled module that defines static or global variables contains these .data sections. The linker treats
the .data section like any other initialized section and creates an output section. In the load-time initialization
model, the sections are loaded into memory and used by the program. See Section 6.10.3.5.
In the run-time initialization model, the linker uses the data in these sections to create initialization data and an
additional compressed initialization table. The boot routine processes the initialization table to copy data from
load addresses to run addresses. See Section 6.10.3.3.
6.10.3.3 Autoinitialization of Variables at Run Time
Autoinitializing variables at run time is the default method of autoinitialization. To use this method, invoke the
linker with the --rom_model option.
Using this method, the linker creates a compressed initialization table and initialization data from the direct
initialized sections in the compiled module. The table and data are used by the C/C++ boot routine to initialize
variables in RAM using the table and data in ROM.
Figure 6-6 illustrates autoinitialization at run time. Use this method in any system where your application runs
from code burned into ROM.
Object file Memory
C auto init
table and data C auto init
(ROM) Loader table and data
(.cinit section) (ROM)
Boot
routine
.data
uninitialized
(RAM)
162 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
The linker defined symbols __TI_CINIT_Base and __TI_CINIT_Limit point to the start and end of the table,
respectively. Each entry in this table corresponds to one output section that needs to be initialized. The
initialization data for each output section could be encoded using different encoding.
The load address in the C auto initialization record points to initialization data with the following format:
8-bit index Encoded data
The first 8-bits of the initialization data is the handler index. It indexes into a handler table to get the address of a
handler function that knows how to decode the following data.
The handler table is a list of 32-bit function pointers.
_TI_Handler_Table_Base:
32-bit handler 1 address
The encoded data that follows the 8-bit index can be in one of the following format types. For clarity the 8-bit
index is also depicted for each format.
6.10.3.4.1 Length Followed by Data Format
8-bit index 24-bit padding 32-bit length (N) N byte initialization data (not compressed)
The compiler uses 24-bit padding to align the length field to a 32-bit boundary. The 32-bit length field encodes
the length of the initialization data in bytes (N). N byte initialization data is not compressed and is copied to the
run address as is.
The run-time support library has a function __TI_zero_init() to process this type of initialization data. The first
argument to this function is the address pointing to the byte after the 8-bit index. The second argument is the run
address from the C auto initialization record.
6.10.3.4.2 Zero Initialization Format
The compiler uses 24-bit padding to align the length field to a 32-bit boundary. The 32-bit length field encodes
the number of bytes to be zero initialized.
The run-time support library has a function __TI_zero_init() to process the zero initialization. The first argument
to this function is the address pointing to the byte after the 8-bit index. The second argument is the run address
from the C auto initialization record.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 163
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
The data following the 8-bit index is compressed using Run Length Encoded (RLE) format. uses a simple run
length encoding that can be decompressed using the following algorithm:
1. Read the first byte, Delimiter (D).
2. Read the next byte (B).
3. If B != D, copy B to the output buffer and go to step 2.
4. Read the next byte (L).
a. If L == 0, then length is either a 16-bit, a 24-bit value, or we’ve reached the end of the data, read next
byte (L).
i. If L == 0, length is a 24-bit value or the end of the data is reached, read next byte (L).
1. If L == 0, the end of the data is reached, go to step 7.
2. Else L <<= 16, read next two bytes into lower 16 bits of L to complete 24-bit value for L.
ii. Else L <<= 8, read next byte into lower 8 bits of L to complete 16-bit value for L.
b. Else if L > 0 and L < 4, copy D to the output buffer L times. Go to step 2.
c. Else, length is 8-bit value (L).
5. Read the next byte (C); C is the repeat character.
6. Write C to the output buffer L times; go to step 2.
7. End of processing.
The run-time support library has a routine __TI_decompress_rle24() to decompress data compressed using
RLE. The first argument to this function is the address pointing to the byte after the 8-bit index. The second
argument is the run address from the C auto initialization record.
Note
RLE Decompression Routine
The previous decompression routine, __TI_decompress_rle(), is included in the run-time-support
library for decompressing RLE encodings generated by older versions of the linker.
The data following the 8-bit index is compressed using LZSS compression. The run-time support library has
the routine __TI_decompress_lzss() to decompress the data compressed using LZSS. The first argument to this
function is the address pointing to the byte after the 8-bit index. The second argument is the run address from
the C auto initialization record.
164 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 165
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
.data
Loader
section
.data section
(initialized)
(RAM)
Address of constructor 1
Address of constructor 2
Address of constructor n
SHT$$INIT_ARRAY$$Limit:
166 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
Initialization record n
The corresponding initialization table is as follows. The section .cinit:c is a subsection in the .cinit section that
contains all scalar data. The subsection is handled as one record during initialization, which minimizes the
overall size of the .cinit section.
.sect ".cinit" ; Initialization section
* Initialization record for variable i
.align 4 ; align on word boundary
.field 4,32 ; length of data (1 word)
.field _i+0,32 ; address of i
.field 23,32 ; _i @ 0
* Initialization record for variable a
.sect ".cinit"
.align 4 ; align on word boundary
.field IR1,32 ; Length of data (5 words)
.field _a+0,32 ; Address of a[ ]
.field 1,32 ; _a[0] @ 0
.field 2,32 ; _a[1] @ 32
.field 3,32 ; _a[2] @ 64
.field 4,32 ; _a[3] @ 96
.field 5,32 ; _a[4] @ 128
IR1: .set 20 ; set length symbol
The .cinit section must contain only initialization tables in this format. When interfacing assembly language
modules, do not use the .cinit section for any other purpose.
The table in the .pinit section simply consists of a list of addresses of constructors to be called (as shown in the
following figure). The constructors appear in the table after the .cinit initialization.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 167
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
.pinit section
Address of constructor 1
Address of constructor 2
Address of constructor 3
•
•
Address of constructor n
When you use the --rom_model or --ram_model option, the linker combines the .cinit sections from all the C/C++
modules and appends a null word to the end of the composite .cinit section. This terminating record appears as
a record with a size field of 0 and marks the end of the initialization tables.
Likewise, the --rom_model or --ram_model link option causes the linker to combine all of the .pinit sections from
all C/C++ modules and append a null word to the end of the composite .pinit section. The boot routine knows the
end of the global constructor table when it encounters a null constructor address.
The const-qualified variables are initialized differently; see Section 5.7.1.
168 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 169
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
• None. Dual-state interworking is disabled. This level is invoked with the -md shell option. Functions with this
support can directly call the following functions:
– Functions compiled in the same state
– Functions in a different state that support dual-state interworking
Functions with this support level can indirectly call only functions that do not require a state change and
do not support dual-state interworking. Because functions with this support level do not provide dual-state
interworking, they cannot be called by a function requiring a state change.
Use this support level if you do not require dual-state interworking, have frequent indirect calls, and cannot
tolerate the additional code size or speed incurred by the indirect calls supporting dual-state interworking.
When a program does not require any state changes, the only difference between specifying no support and
default support is that indirect calls are more complex in the default support level.
6.11.2 Implementation
Dual-state support is implemented by providing an alternate entry point for a function. This alternate entry point
is used by functions requiring a state change. Dual-state support handles the change to the correct state and, if
needed, changes the function back to the state of the caller when it returns. Also, indirect calls set up the return
address so that once the called function returns, the state can be reliably changed back to that of the caller.
6.11.2.1 Naming Conventions for Entry Points
The ARM compiler reserves the name space of all identifiers beginning with an underscore ( _ ) or a dollar sign
($). In this dual-state support scheme, all 32-BIS state entry points begin with an underscore, and all 16-BIS
state entry points begin with a dollar sign. All other compiler-generated identifiers, which are independent of the
state of the processor, begin with an underscore. By this convention, all direct calls within a 16-bit function refer
to the entry point beginning with a dollar sign and all direct calls within a 32-bit function refer to the entry point
beginning with an underscore.
6.11.2.2 Indirect Calls
Addresses of functions taken in 16-BIS state use the address of the 16-BIS state entry point to the function (with
bit 0 of the address set). Likewise, addresses of functions taken in 32-BIS state use the address of the 32-BIS
state entry point (with bit 0 of the address cleared). Then all indirect calls are performed by loading the address
of the called function into a register and executing the branch and exchange (BX) instruction. This automatically
changes the state and ensures that the code works correctly, regardless of what state the address was in when it
was taken.
The return address must also be set up so that the state of the processor is consistent and known upon return.
Bit 0 of the address is tested to determine if the BX instruction invokes a state change. If it does not invoke a
state change, the return address is set up for the state of the function. If it does invoke a change, the return
address is set up for the alternate state and code is executed to return to the function's state.
Because the entry point into a function depends upon the state of the function that takes the address, it is more
efficient to take the address of a function when in the same state as that function. This ensures that the address
of the actual function is used, not its alternate entry point. Because the indirect call can invoke a state change
itself, entering a function through its alternate entry point, even if calling it from a different state, is unnecessary.
Example 6-8 shows sum( ) calling max( ) with code that is compiled for the 16-BIS state and supports dual-state
interworking. The sum( ) function is compiled with the -code_state=16 option, which creates 16-bit instructions
for pre-UAL assembly code. (Refer to the ARM Assembly Language Tools User's Guide for information on
UAL syntax.) Example 6-11 shows the same function call with code that is compiled for the 32-BIS state and
supports dual-state interworking. Function max( ) is compiled without the -code_state=16 option, creating 32-bit
instructions.
170 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Run-Time Environment
int total = 0;
sum(int val1, int val2)
{
int val = max(val1, val2);
total += val;
}
;***********************************************************
;* FUNCTION VENEER: _sum *
;***********************************************************
_sum:
.state32
STMFD sp!, {lr}
ADD lr, pc, #1
BX lr
.state16
BL $sum
BX pc
NOP
.state32
LDMFD sp!, {pc}
.state16
.sect ".text"
.global sum
;***********************************************************
;* FUNCTION DEF: $sum *
;***********************************************************
$sum:
PUSH {LR}
BL $max
LDR A2, CON1 ; {_total+0}
LDR A3, [A2, #0]
ADD A1, A1, A3
STR A1, [A2, #0]
POP {PC}
;***********************************************************
;* CONSTANT TABLE *
;***********************************************************
.sect ".text"
.align 4
CON1: .field _total, 32
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 171
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Run-Time Environment www.ti.com
;***********************************************************
;* FUNCTION VENEER: $max *
;***********************************************************
$max:
.state16
BX pc
NOP
.state32
B _max
.text
.global _max
;***********************************************************
;* FUNCTION DEF: _max *
;***********************************************************
_max:
CMP A1, A2
MOVLE A1, A2
BX LR
Since sum( ) is a 16-bit function, its entry point is $sum. Because it was compiled for dual-state interworking, an
alternate entry point, _sum, located in a different section is included. All calls to sum( ) requiring a state change
use the _sum entry point.
The call to max( ) in sum( ) references $max, because sum( ) is a 16-bit function. If max( ) were a 16-bit function,
sum( ) would call the actual entry point for max( ). However, since max( ) is a 32-bit function, $max is the
alternate entry point for max( ) and handles the state change required by sum( ).
172 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using Run-Time-Support Functions and Building Libraries
Chapter 7
Using Run-Time-Support Functions and Building
Libraries
Some of the features of C/C++ (such as I/O, dynamic memory allocation, string operations, and trigonometric
functions) are provided as an ANSI/ISO C/C++ standard library, rather than as part of the compiler itself. The
TI implementation of this library is the run-time-support library (RTS). The C/C++ compiler implements the ISO
standard library except for those facilities that handle exception conditions, signal and locale issues (properties
that depend on local language, nationality, or culture). Using the ANSI/ISO standard library ensures a consistent
set of functions that provide for greater portability.
In addition to the ANSI/ISO-specified functions, the run-time-support library includes routines that give you
processor-specific commands and direct C language I/O requests. These are detailed in Section 7.1 and Section
7.2.
A library-build utility is provided with the code generation tools that lets you create customized run-time-support
libraries. This process is described in Section 7.4.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 173
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using Run-Time-Support Functions and Building Libraries www.ti.com
This definition enables full portability. Defining the symbol to 0 specifies that the "C standard" portability level will
be used.
7.1.2 Header Files
You must use the header files provided with the compiler run-time support when using functions from C/C++
standard library. Set the TI_ARM_C_DIR environment variable to the include directory where the tools are
installed.
174 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using Run-Time-Support Functions and Building Libraries
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 175
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using Run-Time-Support Functions and Building Libraries www.ti.com
176 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using Run-Time-Support Functions and Building Libraries
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 177
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using Run-Time-Support Functions and Building Libraries www.ti.com
Note
Debugger Required for Default HOST: For the default HOST device to work, there must be a
debugger to handle the C I/O requests; the default HOST device cannot work by itself in an embedded
system. To work in an embedded system, you will need to provide an appropriate driver for your
system.
Note
C I/O Mysteriously Fails: If there is not enough space on the heap for a C I/O buffer, operations on
the file will silently fail. If a call to printf() mysteriously fails, this may be the reason. The heap needs to
be at least large enough to allocate a block of size BUFSIZ (defined in stdio.h) for every file on which
I/O is performed, including stdout, stdin, and stderr, plus allocations performed by the user's code,
plus allocation bookkeeping overhead. Alternately, declare a char array of size BUFSIZ and pass it
to setvbuf to avoid dynamic allocation. To set the heap size, use the --heap_size option when linking
(refer to the Linker Description chapter in the ARM Assembly Language Tools User's Guide).
Note
Open Mysteriously Fails: The run-time support limits the total number of open files to a small
number relative to general-purpose processors. If you attempt to open more files than the maximum,
you may find that the open will mysteriously fail. You can increase the number of open files by
extracting the source code from rts.src and editing the constants controlling the size of some of the
C I/O data structures. The macro _NFILE controls how many FILE (fopen) objects can be open at
one time (stdin, stdout, and stderr count against this total). (See also FOPEN_MAX.) The macro
_NSTREAM controls how many low-level file descriptors can be open at one time (the low-level files
underlying stdin, stdout, and stderr count against this total). The macro _NDEVICE controls how many
device drivers are installed at one time (the HOST device counts against this total).
178 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using Run-Time-Support Functions and Building Libraries
Issuing the following compiler command compiles, links, and creates main.out from the run-time-support library:
armcl main.c --run_linker --heap_size=400 --library=rtsv4_A_be_eabi.lib --output_file=main.out
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 179
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using Run-Time-Support Functions and Building Libraries www.ti.com
open
Description The open function opens the file specified by path and prepares it for I/O.
• The path is the filename of the file to be opened, including an optional directory path
and an optional device specifier (see Section 7.2.5).
• The flags are attributes that specify how the file is manipulated. The flags are specified
using the following symbols:
O_RDONLY (0x0000) /* open for reading */
O_WRONLY (0x0001) /* open for writing */
O_RDWR (0x0002) /* open for read & write */
O_APPEND (0x0008) /* append on each write */
O_CREAT (0x0200) /* open with file create */
O_TRUNC (0x0400) /* open with truncation */
O_BINARY (0x8000) /* open in binary mode */
Low-level I/O routines allow or disallow some operations depending on the flags used
when the file was opened. Some flags may not be meaningful for some devices,
depending on how the device implements files.
• The file_descriptor is assigned by open to an opened file.
The next available file descriptor is assigned to each new file opened.
180 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using Run-Time-Support Functions and Building Libraries
close
Description The close function closes the file associated with file_descriptor.
The file_descriptor is the number assigned by open to an opened file.
read
Description The read function reads count characters into the buffer from the file associated with
file_descriptor.
• The file_descriptor is the number assigned by open to an opened file.
• The buffer is where the read characters are placed.
• The count is the number of characters to read from the file.
write
Description The write function writes the number of characters specified by count from the buffer to
the file associated with file_descriptor.
• The file_descriptor is the number assigned by open to an opened file.
• The buffer is where the characters to be written are located.
• The count is the number of characters to write to the file.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 181
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using Run-Time-Support Functions and Building Libraries www.ti.com
lseek
Description The lseek function sets the file position indicator for the given file to a location relative to
the specified origin. The file position indicator measures the position in characters from
the beginning of the file.
• The file_descriptor is the number assigned by open to an opened file.
• The offset indicates the relative offset from the origin in characters.
• The origin is used to indicate which of the base locations the offset is measured from.
The origin must be one of the following macros:
SEEK_SET (0x0000) Beginning of file
SEEK_CUR (0x0001) Current value of the file position indicator
SEEK_END (0x0002) End of file
Return Value The return value is one of the following:
# new value of the file position indicator if successful
(off_t)-1 on failure
unlink
Delete File
Description The unlink function deletes the file specified by path. Depending on the device, a deleted
file may still remain until all file descriptors which have been opened for that file have
been closed. See Section 7.2.3.
The path is the filename of the file, including path information and optional device prefix.
(See Section 7.2.5.)
182 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using Run-Time-Support Functions and Building Libraries
rename
Rename File
Note
The optional device specified in the new name must match the device of the
old name. If they do not match, a file copy would be required to perform the
rename, and rename is not capable of this action.
Note
Although rename is a low-level function, it is defined by the C standard and can
be used by portable applications.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 183
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using Run-Time-Support Functions and Building Libraries www.ti.com
DEV_open
Syntax int DEV_open (const char * path , unsigned flags , int llv_fd );
Description This function finds a file matching path and opens it for I/O as requested by flags.
• The path is the filename of the file to be opened. If the name of a file passed to open
has a device prefix, the device prefix will be stripped by open, so DEV_open will not
see it. (See Section 7.2.5 for details on the device prefix.)
• The flags are attributes that specify how the file is manipulated. The flags are specified
using the following symbols:
O_RDONLY (0x0000) /* open for reading */
O_WRONLY (0x0001) /* open for writing */
O_RDWR (0x0002) /* open for read & write */
O_APPEND (0x0008) /* append on each write */
O_CREAT (0x0200) /* open with file create */
O_TRUNC (0x0400) /* open with truncation */
O_BINARY (0x8000) /* open in binary mode */
Return Value This function must return -1 to indicate an error if for some reason the file could not be
opened; such as the file does not exist, could not be created, or there are too many
files open. The value of errno may optionally be set to indicate the exact error (the
HOST device does not set errno). Some devices might have special failure conditions; for
instance, if a device is read-only, a file cannot be opened O_WRONLY.
On success, this function must return a non-negative file descriptor unique among all
open files handled by the specific device. The file descriptor need not be unique across
devices. The device file descriptor is used only by low-level functions when calling the
device-driver-level functions. The low-level function open allocates its own unique file
descriptor for the high-level functions to call the low-level functions. Code that uses only
high-level I/O functions need not be aware of these file descriptors.
184 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using Run-Time-Support Functions and Building Libraries
DEV_close
Return Value This function should return -1 to indicate an error if the file descriptor is invalid in some
way, such as being out of range or already closed, but this is not required. The user
should not call close() with an invalid file descriptor.
DEV_read
Description The read function reads count bytes from the input file associated with dev_fd.
• The dev_fd is the number assigned by open to an opened file.
• The buf is where the read characters are placed.
• The count is the number of characters to read from the file.
Return Value This function must return -1 to indicate an error if for some reason no bytes could be read
from the file. This could be because of an attempt to read from a O_WRONLY file, or for
device-specific reasons.
If count is 0, no bytes are read and this function returns 0.
This function returns the number of bytes read, from 0 to count. 0 indicates that EOF was
reached before any bytes were read. It is not an error to read less than count bytes; this
is common if the are not enough bytes left in the file or the request was larger than an
internal device buffer size.
DEV_write
Syntax int DEV_write (int dev_fd , const char * buf , unsigned count );
Return Value This function must return -1 to indicate an error if for some reason no bytes could be
written to the file. This could be because of an attempt to read from a O_RDONLY file, or
for device-specific reasons.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 185
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using Run-Time-Support Functions and Building Libraries www.ti.com
DEV_lseek
Description This function sets the file's position indicator for this file descriptor as lseek.
If lseek is supported, it should not allow a seek to before the beginning of the file, but it
should support seeking past the end of the file. Such seeks do not change the size of the
file, but if it is followed by a write, the file size will increase.
Return Value If successful, this function returns the new value of the file position indicator.
This function must return -1 to indicate an error if for some reason no bytes could be
written to the file. For many devices, the lseek operation is nonsensical (e.g. a computer
monitor).
DEV_unlink
Delete File
Description Remove the association of the pathname with the file. This means that the file may no
longer be opened using this name, but the file may not actually be immediately removed.
Depending on the device, the file may be immediately removed, but for a device which
allows open file descriptors to point to unlinked files, the file will not actually be deleted
until the last file descriptor is closed. See Section 7.2.3.
Return Value This function must return -1 to indicate an error if for some reason the file could not be
unlinked (delayed removal does not count as a failure to unlink.)
If successful, this function returns 0.
DEV_rename
Rename File
Description This function changes the name associated with the file.
• The old_name is the current name of the file.
• The new_name is the new name for the file.
Return Value This function must return -1 to indicate an error if for some reason the file could not be
renamed, such as the file does not exist, or the new name already exists.
Note
It is inadvisable to allow renaming a file so that it is on a different device. In
general this would require a whole file copy, which may be more expensive than
you expect.
186 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using Run-Time-Support Functions and Building Libraries
Note
Use Unique Function Names
The function names open, read, write, close, lseek, rename, and unlink are used by the low-level
routines. Use other names for the device-level functions that you write.
Use the low-level function add_device() to add your device to the device_table. The device table is a statically
defined array that supports n devices, where n is defined by the macro _NDEVICE found in stdio.h/cstdio.
The first entry in the device table is predefined to be the host device on which the debugger is running. The
low-level routine add_device() finds the first empty position in the device table and initializes the device fields
with the passed-in arguments. For a complete description, see the add_device function.
Example 7-1. Mapping Default Streams to Device
#include <stdio.h>
#include <file.h>
#include "mydevice.h"
void main()
{
add_device("mydevice", _MSA,
MYDEVICE_open, MYDEVICE_close,
MYDEVICE_read, MYDEVICE_write,
MYDEVICE_lseek, MYDEVICE_unlink, MYDEVICE_rename);
/*-----------------------------------------------------------------------*/
/* Re-open stderr as a MYDEVICE file */
/*-----------------------------------------------------------------------*/
if (!freopen("mydevice:stderrfile", "w", stderr))
{
puts("Failed to freopen stderr");
exit(EXIT_FAILURE);
}
/*-----------------------------------------------------------------------*/
/* stderr should not be fully buffered; we want errors to be seen as */
/* soon as possible. Normally stderr is line-buffered, but this example */
/* does not buffer stderr at all. This means that there will be one call */
/* to write() for each character in the message. */
/*-----------------------------------------------------------------------*/
if (setvbuf(stderr, NULL, _IONBF, 0))
{
puts("Failed to setvbuf stderr");
exit(EXIT_FAILURE);
}
/*-----------------------------------------------------------------------*/
/* Try it out! */
/*-----------------------------------------------------------------------*/
printf("This goes to stdout\n");
fprintf(stderr, "This goes to stderr\n"); }
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 187
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using Run-Time-Support Functions and Building Libraries www.ti.com
If no device prefix is used, the HOST device will be used to open the file.
add_device
Description The add_device function adds a device record to the device table allowing that device
to be used for I/O from C. The first entry in the device table is predefined to be the
HOST device on which the debugger is running. The function add_device() finds the first
empty position in the device table and initializes the fields of the structure that represent a
device.
To open a stream on a newly added device use fopen( ) with a string of the format
devicename : filename as the first argument.
• The name is a character string denoting the device name. The name is limited to 8
characters.
• The flags are device characteristics. The flags are as follows:
_SSA Denotes that the device supports only one open stream at a time
_MSA Denotes that the device supports multiple open streams
More flags can be added by defining them in file.h.
• The dopen, dclose, dread, dwrite, dlseek, dunlink, and drename specifiers are function
pointers to the functions in the device driver that are called by the low-level functions
to perform I/O on the specified device. You must declare these functions with the
interface specified in Section 7.2.2. The device driver for the HOST that the ARM
debugger is run on are included in the C I/O library.
188 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using Run-Time-Support Functions and Building Libraries
add_device (continued)
#include <file.h>
#include <stdio.h>
/****************************************************************************/
/* Declarations of the user-defined device drivers */
/****************************************************************************/
extern int MYDEVICE_open(const char *path, unsigned flags, int fno);
extern int MYDEVICE_close(int fno);
extern int MYDEVICE_read(int fno, char *buffer, unsigned count);
extern int MYDEVICE_write(int fno, const char *buffer, unsigned count);
extern off_t MYDEVICE_lseek(int fno, off_t offset, int origin);
extern int MYDEVICE_unlink(const char *path);
extern int MYDEVICE_rename(const char *old_name, char *new_name);
main()
{
FILE *fid;
add_device("mydevice", _MSA, MYDEVICE_open, MYDEVICE_close, MYDEVICE_read,
MYDEVICE_write, MYDEVICE_lseek, MYDEVICE_unlink, MYDEVICE_rename);
fid = fopen("mydevice:test","w");
fprintf(fid,"Hello, world\n");
fclose(fid);
}
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 189
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using Run-Time-Support Functions and Building Libraries www.ti.com
The arguments to _register_lock() and _register_unlock() should be functions which take no arguments and
return no values, and which implement some sort of global semaphore locking:
extern volatile sig_atomic_t *sema = SHARED_SEMAPHORE_LOCATION;
static int sema_depth = 0;
static void my_lock(void)
{
while (ATOMIC_TEST_AND_SET(sema, MY_UNIQUE_ID) != MY_UNIQUE_ID);
sema_depth++;
}
static void my_unlock(void)
{
if (!--sema_depth) ATOMIC_CLEAR(sema);
}
The run-time-support nests calls to _lock(), so the primitives must keep track of the nesting level.
190 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using Run-Time-Support Functions and Building Libraries
All three of these programs are provided as a non-optional feature of CCS 5.1. They are also available as part of
the optional XDC Tools feature if you are using an earlier version of CCS.
The mklib program looks for these executables in the following order:
1. in your PATH
2. in the directory getenv("CCS_UTILS_DIR")/cygwin
3. in the directory getenv("CCS_UTILS_DIR")/bin
4. in the directory getenv("XDCROOT")
5. in the directory getenv("XDCROOT")/bin
If you are invoking mklib from the command line, and these executables are not in your path, you must set the
environment variable CCS_UTILS_DIR such that getenv("CCS_UTILS_DIR")/bin contains the correct programs.
7.4.2 Using the Library-Build Process
You should normally let the linker automatically rebuild libraries as needed. If necessary, you can run mklib
directly to populate libraries. See Section 7.4.2.2 for situations when you might want to do this.
7.4.2.1 Automatic Standard Library Rebuilding by the Linker
The linker looks for run-time-support libraries primarily through the TI_ARM_C_DIR environment variable.
Typically, one of the pathnames in TI_ARM_C_DIR is your install directory/lib, which contains all of the pre-built
libraries, as well as the index library libc.a. The linker looks in TI_ARM_C_DIR to find a library that is the
best match for the build attributes of the application. The build attributes are set indirectly according to the
command-line options used to build the application. Build attributes include things like CPU revision. If the library
name is explicitly specified (e.g. -library=rtsv4_A_be_eabi), run-time support looks for that library exactly. If the
library name is not specified, the linker uses the index library libc.a to pick an appropriate library. If the library is
specified by path (e.g. –library=/foo/rtsv4_A_be_eabi), it is assumed the library already exists and it will not be
built automatically.
The index library describes a set of libraries with different build attributes. The linker will compare the build
attributes for each potential library with the build attributes of the application and will pick the best fit. For details
on the index library, see the archiver chapter in the ARM Assembly Language Tools User's Guide.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 191
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using Run-Time-Support Functions and Building Libraries www.ti.com
Now that the linker has decided which library to use, it checks whether the run-time-support library is present in
TI_ARM_C_DIR. The library must be in exactly the same directory as the index library libc.a. If the library is not
present, the linker invokes mklib to build it. This happens when the library is missing, regardless of whether the
user specified the name of the library directly or allowed the linker to pick the best library from the index library.
The mklib program builds the requested library and places it in 'lib' directory part of TI_ARM_C_DIR in the same
directory as the index library, so it is available for subsequent compilations.
Things to watch out for:
• The linker invokes mklib and waits for it to finish before finishing the link, so you will experience a one-time
delay when an uncommonly-used library is built for the first time. Build times of 1-5 minutes have been
observed. This depends on the power of the host (number of CPUs, etc).
• In a shared installation, where an installation of the compiler is shared among more than one user, it is
possible that two users might cause the linker to rebuild the same library at the same time. The mklib
program tries to minimize the race condition, but it is possible one build will corrupt the other. In a shared
environment, all libraries which might be needed should be built at install time; see Section 7.4.2.2 for
instructions on invoking mklib directly to avoid this problem.
• The index library must exist, or the linker is unable to rebuild libraries automatically.
• The index library must be in a user-writable directory, or the library is not built. If the compiler installation
must be installed read-only (a good practice for shared installation), any missing libraries must be built at
installation time by invoking mklib directly.
• The mklib program is specific to a certain version of a certain library; you cannot use one compiler version's
run-time support's mklib to build a different compiler version's run-time support library.
7.4.2.2 Invoking mklib Manually
You may need to invoke mklib directly in special circumstances:
• The compiler installation directory is read-only or shared.
• You want to build a variant of the run-time-support library that is not pre-configured in the index library libc.a
or known to mklib. (e.g. a variant with source-level debugging turned on.)
7.4.2.2.1 Building Standard Libraries
You can invoke mklib directly to build any or all of the libraries indexed in the index library libc.a. The libraries
are built with the standard options for that library; the library names and the appropriate standard option sets are
known to mklib.
This is most easily done by changing the working directory to be the compiler run-time-support library directory
'lib' and invoking the mklib executable there:
mklib --pattern=rtsv4_A_be_eabi.lib
Some targets have many libraries, so this step can take a long time. To build a subset of the libraries, invoke
mklib individually for each desired library.
7.4.2.2.3 Building Libraries With Custom Options
You can build a library with any extra custom options desired. This is useful for building a version of the library
with silicon exception workarounds enabled. The generated library is not a standard library, and must not be
placed in the 'lib' directory. It should be placed in a directory local to the project which needs it. To build a
192 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Using Run-Time-Support Functions and Building Libraries
debugging version of the library rtsv4_A_be_eabi, change the working directory to the 'lib' directory and run the
command:
mklib --pattern=rtsv4_A_be_eabi.lib --name=rtsv4_A_be_eabi_debug.lib
--install_to=$Project/Debug --extra_options="-g"
Examples:
To build all standard libraries and place them in the compiler's library directory:
mklib --all --index=$C_DIR/lib
To build one standard library and place it in the compiler's library directory:
mklib --pattern=rtsv4_A_be_eabi.lib --index=$C_DIR/lib
To build a custom library that is just like rtsv4_A_be_eabi.lib, but has symbolic debugging support enabled:
mklib --pattern=rts16.lib --extra_options="-g" --index=$C_DIR/lib --install_to=$Project/Debug
--name=rtsv4_A_be_eabi_debug.lib
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 193
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Using Run-Time-Support Functions and Building Libraries www.ti.com
194 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C++ Name Demangler
Chapter 8
C++ Name Demangler
The C++ compiler implements function overloading, operator overloading, and type-safe linking by encoding
a function's prototype and namespace in its link-level name. The process of encoding the prototype into the
linkname is often referred to as name mangling. When you inspect mangled names, such as in assembly
files, disassembler output, or compiler or linker diagnostic messages, it can be difficult to associate a mangled
name with its corresponding name in the C++ source code. The C++ name demangler is a debugging aid that
translates each mangled name it detects to its original name found in the C++ source code.
These topics tell you how to invoke and use the C++ name demangler. The C++ name demangler reads in input,
looking for mangled names. All unmangled text is copied to output unaltered. All mangled names are demangled
before being copied to output.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 195
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C++ Name Demangler www.ti.com
By default, the C++ name demangler outputs to standard output. You can use the -o file option if you want to
output to a file.
The following options apply only to the C++ name demangler:
--debug (--d) Prints debug messages.
--diag_wrap[=on,off] Sets diagnostic messages to wrap at 79 columns (on, which is the default) or not (off).
--help (-h) Prints a help screen that provides an online summary of the C++ name demangler options.
--output= file (-o) Outputs to the specified file rather than to standard out.
--quiet (-q) Reduces the number of messages generated during execution.
-u Specifies that external names do not have a C++ prefix. (deprecated)
196 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com C++ Name Demangler
Executing the C++ name demangler will demangle all names that it believes to be mangled. Enter:
armdem calories_in_a_banana.asm
The result after running the C++ name demangler is as follows. The linknames in _ZN6bananaC1Ev,
_ZN6banana8caloriesEv, and _ZN6bananaD1Ev are demangled.
calories_in_a_banana():
STMFD SP!, {A3, A4, V1, LR}
MOV A1, SP
BL banana::banana()
BL banana::calories()
MOV V1, A1
MOV A1, SP
BL banana::~banana()
MOV A1, V1
LDMFD SP!, {A3, A4, V1, LR}
BX LR
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 197
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
C++ Name Demangler www.ti.com
198 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Glossary
Appendix A
Glossary
A.1 Terminology
absolute lister A debugging tool that allows you to create assembler listings that contain absolute
addresses.
alias disambiguation A technique that determines when two pointer expressions cannot point to the same
location, allowing the compiler to freely optimize such expressions.
aliasing The ability for a single object to be accessed in more than one way, such as when
two pointers point to a single object. It can disrupt optimization, because any indirect
reference could refer to any other object.
allocation A process in which the linker calculates the final memory addresses of output
sections.
Application Binary A standard that specifies the interface between two object modules. An ABI specifies
Interface (ABI) how functions are called and how information is passed from one program component
to another.
archive library A collection of individual files grouped into a single file by the archiver.
archiver A software program that collects several individual files into a single file called an
archive library. With the archiver, you can add, delete, extract, or replace members of
the archive library.
assembler A software program that creates a machine-language program from a source file
that contains assembly language instructions, directives, and macro definitions. The
assembler substitutes absolute operation codes for symbolic operation codes and
absolute or relocatable addresses for symbolic addresses.
autoinitialization The process of initializing global C variables (contained in the .cinit section) before
program execution begins.
autoinitialization at An autoinitialization method used by the linker when linking C code. The linker uses
run time this method when you invoke it with the --rom_model link option. The linker loads
the .cinit section of data tables into memory, and variables are initialized at run time.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 199
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Glossary www.ti.com
big endian An addressing protocol in which bytes are numbered from left to right within a word.
More significant bytes in a word have lower numbered addresses. Endian ordering is
hardware-specific and is determined at reset. See also little endian
block A set of statements that are grouped together within braces and treated as an entity.
.bss section One of the default object file sections. You use the assembler .bss directive to reserve
a specified amount of space in the memory map that you can use later for storing
data. The .bss section is uninitialized.
byte Per ANSI/ISO C, the smallest addressable unit that can hold a character.
C/C++ compiler A software program that translates C source statements into assembly language
source statements.
code generator A compiler tool that takes the file produced by the parser or the optimizer and
produces an assembly language source file.
COFF Common object file format; a system of object files configured according to a standard
developed by AT&T. This ABI is no longer supported.
command file A file that contains options, filenames, directives, or commands for the linker or hex
conversion utility.
comment A source statement (or portion of a source statement) that documents or improves
readability of a source file. Comments are not compiled, assembled, or linked; they
have no effect on the object file.
compiler program A utility that lets you compile, assemble, and optionally link in one step. The
compiler runs one or more source modules through the compiler (including the parser,
optimizer, and code generator), the assembler, and the linker.
configured memory Memory that the linker has specified for allocation.
cross-reference listing An output file created by the assembler that lists the symbols that were defined, what
line they were defined on, which lines referenced them, and their final values.
.data section One of the default object file sections. The .data section is an initialized section
that contains initialized data. You can use the .data directive to assemble code into
the .data section.
direct call A function call where one function calls another using the function's name.
directives Special-purpose commands that control the actions and functions of a software tool
(as opposed to assembly language instructions, which control the actions of a device).
dynamic memory A technique used by several functions (such as malloc, calloc, and realloc) to
allocation dynamically allocate memory for variables at run time. This is accomplished by
200 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Glossary
defining a large memory pool (heap) and using the functions to allocate memory from
the heap.
ELF Executable and Linkable Format; a system of object files configured according to the
System V Application Binary Interface specification.
environment variable A system symbol that you define and assign to a string. Environmental variables are
often included in Windows batch files or UNIX shell scripts such as .cshrc or .profile.
epilog The portion of code in a function that restores the stack and returns.
executable object file A linked, executable object file that is downloaded and executed on a target system.
external symbol A symbol that is used in the current program module but defined or declared in a
different program module.
file-level optimization A level of optimization where the compiler uses the information that it has about the
entire file to optimize your code (as opposed to program-level optimization, where the
compiler uses information that it has about the entire program to optimize your code).
function inlining The process of inserting code for a function at the point of call. This saves the
overhead of a function call and allows the optimizer to optimize the function in the
context of the surrounding code.
global symbol A symbol that is either defined in the current module and accessed in another, or
accessed in the current module but defined in another.
high-level language The ability of a compiler to retain symbolic and high-level language information (such
debugging as type and function definitions) so that a debugging tool can use this information.
indirect call A function call where one function calls another function by giving the address of the
called function.
initialization at load An autoinitialization method used by the linker when linking C/C++ code. The linker
time uses this method when you invoke it with the --ram_model link option. This method
initializes variables at load time instead of run time.
initialized section A section from an object file that will be linked into an executable object file.
input section A section from an object file that will be linked into an executable object file.
integrated A C/C++ preprocessor that is merged with the parser, allowing for faster compilation.
preprocessor Stand-alone preprocessing or preprocessed listing is also available.
interlist feature A feature that inserts as comments your original C/C++ source statements into the
assembly language output from the assembler. The C/C++ statements are inserted
next to the equivalent assembly instructions.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 201
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Glossary www.ti.com
intrinsics Operators that are used like functions and produce assembly language code that
would otherwise be inexpressible in C, or would take greater time and effort to code.
K&R C Kernighan and Ritchie C, the de facto standard as defined in the first edition of The C
Programming Language (K&R). Most K&R C programs written for earlier, non-ISO C
compilers should correctly compile and run without modification.
label A symbol that begins in column 1 of an assembler source statement and corresponds
to the address of that statement. A label is the only assembler statement that can
begin in column 1.
linker A software program that combines object files to form an executable object file that
can be allocated into system memory and executed by the device.
listing file An output file, created by the assembler, which lists source statements, their line
numbers, and their effects on the section program counter (SPC).
little endian An addressing protocol in which bytes are numbered from right to left within a word.
More significant bytes in a word have higher numbered addresses. Endian ordering is
hardware-specific and is determined at reset. See also big endian
loader A device that places an executable object file into system memory.
loop unrolling An optimization that expands small loops so that each iteration of the loop appears
in your code. Although loop unrolling increases code size, it can improve the
performance of your code.
macro definition A block of source statements that define the name and the code that make up a
macro.
macro expansion The process of inserting source statements into your code in place of a macro call.
map file An output file, created by the linker, which shows the memory configuration, section
composition, section allocation, symbol definitions and the addresses at which the
symbols were defined for your program.
memory map A map of target system memory space that is partitioned into functional blocks.
name mangling A compiler-specific feature that encodes a function name with information regarding
the function's arguments return types.
object file An assembled or linked file that contains machine-language object code.
202 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Glossary
optimizer A software tool that improves the execution speed and reduces the size of C
programs.
options Command-line parameters that allow you to request additional or specific functions
when you invoke a software tool.
parser A software tool that reads the source file, performs preprocessing functions, checks
the syntax, and produces an intermediate file used as input for the optimizer or code
generator.
partitioning The process of assigning a data path to each instruction.
pragma A preprocessor directive that provides directions to the compiler about how to treat a
particular statement.
preprocessor A software tool that interprets macro definitions, expands macros, interprets header
files, interprets conditional compilation, and acts upon preprocessor directives.
program-level An aggressive level of optimization where all of the source files are compiled into
optimization one intermediate file. Because the compiler can see the entire program, several
optimizations are performed with program-level optimization that are rarely applied
during file-level optimization.
push An operation that places a data object on a stack for temporary storage.
quiet run An option that suppresses the normal banner and the progress information.
relocation A process in which the linker adjusts all the references to a symbol when the symbol's
address changes.
run-time environment The run time parameters in which your program must function. These parameters
are defined by the memory and register conventions, stack organization, function call
conventions, and system initialization.
run-time-support Standard ISO functions that perform tasks that are not part of the C language (such as
functions memory allocation, string conversion, and string searches).
run-time-support A library file, rts.src, which contains the source for the run time-support functions.
library
section A relocatable block of code or data that ultimately will be contiguous with other
sections in the memory map.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 203
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Glossary www.ti.com
sign extend A process that fills the unused MSBs of a value with the value's sign bit.
source file A file that contains C/C++ code or assembly language code that is compiled or
assembled to form an object file.
stand-alone A software tool that expands macros, #include files, and conditional compilation as
preprocessor an independent program. It also performs integrated preprocessing, which includes
parsing of instructions.
static variable A variable whose scope is confined to a function or a program. The values of static
variables are not discarded when the function or program is exited; their previous
value is resumed when the function or program is reentered.
storage class An entry in the symbol table that indicates how to access a symbol.
string table A table that stores symbol names that are longer than eight characters (symbol names
of eight characters or longer cannot be stored in the symbol table; instead they are
stored in the string table). The name portion of the symbol's entry points to the
location of the string in the string table.
structure A collection of one or more variables grouped together under a single name.
subsection A relocatable block of code or data that ultimately will occupy continuous space in the
memory map. Subsections are smaller sections within larger sections. Subsections
give you tighter control of the memory map.
symbolic debugging The ability of a software tool to retain symbolic information that can be used by a
debugging tool such as an emulator or simulator.
target system The system on which the object code you have developed is executed.
.text section One of the default object file sections. The .text section is initialized and contains
executable code. You can use the .text directive to assemble code into the .text
section.
trigraph sequence A 3-character sequence that has a meaning (as defined by the ISO 646-1983
Invariant Code Set). These characters cannot be represented in the C character set
and are expanded to one character. For example, the trigraph ??' is expanded to ^.
trip count The number of times that a loop executes before it terminates.
unconfigured memory Memory that is not defined as part of the memory map and cannot be loaded with
code or data.
uninitialized section A object file section that reserves space in the memory map but that has no actual
contents. These sections are built with the .bss and .usect directives.
unsigned value A value that is treated as a nonnegative number, regardless of its actual sign.
variable A symbol representing a quantity that can assume any of a set of values.
204 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Glossary
veneer A sequence of instructions that serves as an alternate entry point into a routine if a
state change is required.
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 205
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Revision History www.ti.com
Revision History
Changes from March 11, 2020 to March 31, 2023 (from Revision V (March 2020) to Revision W
(March 2023)) Page
• Updated the numbering format for tables, figures, and cross-references throughout the document................. 9
• Removed references to the Processors wiki throughout the document............................................................. 9
• The --strict_compatibility linker option no longer has any effect and has been removed from the
documentation.................................................................................................................................................. 25
• Documented predefined macros for ptrdiff_t and size_t types......................................................................... 35
• Corrected names of the --gen_cross_reference_listing and --asm_cross_reference_listing options wherever
they appear.......................................................................................................................................................45
• Clarified that --opt_level=4 must be placed before --run_linker option.............................................................59
• Corrected information about default for --gen_data_subsections option and its interaction with the
SET_DATA_SECTION pragma.........................................................................................................................76
• Updated information about the size of enum types.......................................................................................... 91
• Removed documentation for the CODE_ALIGN pragma, which is not supported. Use the aligned function
attribute instead................................................................................................................................................ 98
• Clarify interaction between --opt_level and FUNCTION_OPTIONS pragma................................................. 105
• Documented C++ attribute syntax for attributes that correspond to the MUST_ITERATE pragma................107
• Added documentation for the PROB_ITERATE pragma................................................................................ 112
• Documented C++ attribute syntax for attributes that correspond to the UNROLL pragma............................ 116
• Added example using the location attribute....................................................................................................133
• Clarified information about string handling functions......................................................................................175
• Added information about time and clock RTS functions.................................................................................176
The following table lists changes made to this document prior to changes to the document numbering format.
The left column identifies the first version of this document in which that particular change appeared.
Earlier Revisions
Version Added Chapter Location Additions / Modifications / Deletions
Clarified that either --rom_model or --ram_model is required if only the
SPNU151V Linking Section 4.3.5 linker is being run, but --rom_model is the default if the compiler runs on
C/C++ files on the same command line.
SPNU151V C/C++ Language Section 5.11.22 The #pragma once is now documented for use in header files.
Run-Time Clarified that zero initialization takes place only if the --rom_model linker
SPNU151V Section 6.10.3.1
Environment option is used, not if the --ram_model option is used.
The default file extensions for object files created by the compiler have
been changed in order to prevent conflicts when C and C++ files have the
SPNU151U -- throughout -- same names. Object files generated from C source files have the .c.obj
extension. Object files generated from C++ source files have the .cpp.obj
extension.
SPNU151T Using the Compiler Section 2.3.1 Added the --emit_references:file linker option.
Documented that C standard macros such as __STDC_VERSION__ are
SPNU151T Using the Compiler Section 2.5.1
supported.
SPNU151T C/C++ Language Section 5.11 Added documentation for the CODE_ALIGN pragma.
SPNU151T C/C++ Language Section 5.11.19 Clarify section placement for the NOINIT and PERSISTENT pragmas.
206 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Revision History
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 207
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Revision History www.ti.com
208 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
www.ti.com Revision History
SPNU151W – JANUARY 1998 – REVISED MARCH 2023 ARM Optimizing C/C++ Compiler 209
Submit Document Feedback v20.2.0.LTS
Copyright © 2023 Texas Instruments Incorporated
Revision History www.ti.com
210 ARM Optimizing C/C++ Compiler SPNU151W – JANUARY 1998 – REVISED MARCH 2023
v20.2.0.LTS Submit Document Feedback
Copyright © 2023 Texas Instruments Incorporated
IMPORTANT NOTICE AND DISCLAIMER
TI PROVIDES TECHNICAL AND RELIABILITY DATA (INCLUDING DATA SHEETS), DESIGN RESOURCES (INCLUDING REFERENCE
DESIGNS), APPLICATION OR OTHER DESIGN ADVICE, WEB TOOLS, SAFETY INFORMATION, AND OTHER RESOURCES “AS IS”
AND WITH ALL FAULTS, AND DISCLAIMS ALL WARRANTIES, EXPRESS AND IMPLIED, INCLUDING WITHOUT LIMITATION ANY
IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT OF THIRD
PARTY INTELLECTUAL PROPERTY RIGHTS.
These resources are intended for skilled developers designing with TI products. You are solely responsible for (1) selecting the appropriate
TI products for your application, (2) designing, validating and testing your application, and (3) ensuring your application meets applicable
standards, and any other safety, security, regulatory or other requirements.
These resources are subject to change without notice. TI grants you permission to use these resources only for development of an
application that uses the TI products described in the resource. Other reproduction and display of these resources is prohibited. No license
is granted to any other TI intellectual property right or to any third party intellectual property right. TI disclaims responsibility for, and you
will fully indemnify TI and its representatives against, any claims, damages, costs, losses, and liabilities arising out of your use of these
resources.
TI’s products are provided subject to TI’s Terms of Sale or other applicable terms available either on ti.com or provided in conjunction with
such TI products. TI’s provision of these resources does not expand or otherwise alter TI’s applicable warranties or warranty disclaimers for
TI products.
TI objects to and rejects any additional or different terms you may have proposed. IMPORTANT NOTICE
Mailing Address: Texas Instruments, Post Office Box 655303, Dallas, Texas 75265
Copyright © 2023, Texas Instruments Incorporated