0% found this document useful (0 votes)
4 views38 pages

Lecture9 4a Variable Stack

The document outlines key concepts related to variables, memory allocation, and optimization in the context of the XC32 compiler for PIC32M MCUs. It details the types of variables (auto and non-auto), their storage locations, and the structure of the stack used for function parameters and local variables. Additionally, it discusses optimization levels available in the compiler and their impact on compilation time.

Uploaded by

Hong Hai Nguyen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views38 pages

Lecture9 4a Variable Stack

The document outlines key concepts related to variables, memory allocation, and optimization in the context of the XC32 compiler for PIC32M MCUs. It details the types of variables (auto and non-auto), their storage locations, and the structure of the stack used for function parameters and local variables. Additionally, it discusses optimization levels available in the compiler and their impact on compilation time.

Uploaded by

Hong Hai Nguyen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EE4341 Lecture 9 – 2/6/2023


Variables

Access and Allocation

Optimization
References

MPLAB XC32 C/C++ Compiler User’s Guide for
PIC32M MCUs (DS50002799B, 27 April 2020)

MPLAB XC32 Release Notes, v4.21, (5
December 2022)

MPLAB XC32 User’s Guide for Embedded
Engineers (DS50002509B, 30 March 2017)
These are found at:

[Link]
Data Representation

Multibyte variables are in little-endian form

Least significant byte is at lowest address

0x12345678

High address Low address



Section 9.2 of Compiler User’s Guide
Aside
There is a bit in the MIPS processor which
selects the endianess to use
=> The XC32 compiler assumes little-endian

Integer Data Types
– char, int, long int

Fixed Point Formats

Floating Point Data Types
– Float, double, long double
9.4
Types of Variables

Traditionally distinguish local and global
variables:
– A Local Variable has the scope of the function in
which it is defined
– A Global Variable has the scope across all the
functions
C/C++

C/C++ have three common scopes: block, file,
and program

C/C++ allows static variables which are defined
outside a function but only have scope inside a
file
Where are things put?

Objects are placed in appropriate locations by the
Linker
– Variables and Constants to RAM or ROM as
appropriate
– Stack is in RAM
– Program Code in ROM (sometimes RAM)
– Interrupt Vectors in ROM
What the XC32 compiler does

Variables in data memory can be of two types:

Non-auto Variables have permanent storage duration


and and placed in data memory

Auto Variables are placed in data memory on the stack


Types of variables
For the XC32 compiler there are two types of
variables:

Auto type variables (default)
– Automatic storage duration (on the stack)

Non-auto type variables
– Permanent storage duration (anywhere in RAM)
Non-auto Variables

Global Variables

Static Variables, including locally static variables

Compiler will determine which section the
variables should be put into
● Note, non-auto variables with the const qualifier
will be put in program memory
Auto Variables

Local variables

Function parameters

These are placed on the stack
=> Automatically come into existence when a
function executes and disappear when the function
completes (space is allocated and deallocated)
Memory Allocation and Access


The gp register is used to point to the region where
“global” variables are found

The sp register is used to point to the stack where “local”
variables are found

Initializing these two registers is one of the first things


done at start up
Data Structures

First-In-First-Out data structure
– Buffer, circular buffer

Many peripherals implement buffers in
hardware
– UART, SPI

These are useful for bursty data
Last-In-First-Out data structure
The stack

The stack is a dynamic data structure
=> The stack is essential for most modern C/C++
compilers
The Stack in C

The stack is used to
– Store context
– Store local (automated) variables
– Pass parameters to functions
The XC32 Stack
● The stack is intialized with a value _stack

The default size is 1 KB (256 words)

The initial address is 0xa0020000 for the Core Timer
Example on a PIC32MX470 device

=> Note that the location of the stack depends on data


memory size
The XC32 stack

XC32 has a stack that grows down in memory

oldest element

Decreasing
memory
addresses

newest element

Top of Stack
Where are things on the stack?

The stack contains
arguments, local
variables, and saved
registers

Space allocated 8-
bytes at a time
Stack Structure (assembly
directives)
.frame framereg, frameoffset, retreg

This directive describes the shape of the stack frame. The virtual frame pointer in use

is framereg; normally this is either $fp or $sp. The frame pointer is frameoffset

bytes below the canonical frame address (CFA), which is the value of the stack pointer

on entry to the function. The return address is initially located in retreg until it is saved

as indicated in .mask.

.mask mask, offset

Indicate which of the integer registers are saved in the current function's stack frame.

mask is interpreted a bit mask in which bit n set indicates that register n is saved. The

registers are saved in a block located offset bytes from the canonical frame address

(CFA), which is the value of the stack pointer on entry to the function.
Example

.frame $fp,16,$31 # vars= 0, regs= 0/0, args= 0, gp= 0


.mask 0x00000000,0
Saved Registers
XC32 Stack (function parameters)

The stack pointer is always aligned on an 8-
byte boundary (note that this is the size of the
largest integer or floating point value)

Although some arguments may be passed in
registers, the compiler always allocates stack
space for all paramters passed to a function
Example (page 158)
Example
Function Return Values

Function return values are placed in registers
v0 and v1
Optimization

For the free version of the compiler


Optimization -O0 (no optimization)

Optimization -O1 (level 1)

Optimization -O2 (level 2)
Optimization

From the microchip webpage:
[Link]/en-us/development-tools-tools-and-software/mplab-xc-compilers
Optimization -O0
Optimization -O1
Optimization -O2
Optimization

In general the compiler will take longer to
compile the higher the level of optimization

This may be a concern for large programs

You might also like