The Importance of Memory Model Selection When Using the MPLAB® XC16 Compiler

Many compilers employ memory models that act as a set of rules for how code and data are placed in memory areas on the target device and how that code and data are accessed. The MPLAB XC16 C compiler is one such compiler that uses memory models to allow you to take advantage of a relatively complex memory architecture with little effort.

The selection of the memory model settings is important because it can greatly affect the size of the code produced by the compiler and how fast this code will execute. The memory model will control the memory region in which data is linked and how it is accessed. Independent settings are available for basic scalar objects (such as char and int), aggregate objects (arrays and structures), and also for const-qualified objects. The location of objects will dictate the code sequence required to access them. The memory model settings also control the code sequences used to call functions, which again affects your code’s performance.

The MPLAB XC16 C compiler will choose default memory model settings, but these may not be the best for your project, so you should always review and select the best settings for each of your projects. By default MPLAB XC16 will choose small-scalar, large-data for devices that have more than 6 or 7 kbytes of data memory (depending upon the selected target family); otherwise, the compiler will choose a small-data, small-scalar memory model.

If possible, use the small-data and small-scalar settings. These will fully utilise the near data memory of the device, but they can only be used for small projects that have low (less than 6 or 7 kbytes) data-memory requirements. Selecting large-data along with small-scalar will allow the larger aggregate objects to use all the available data memory while allowing your small scalar objects to be efficiently accessed in near memory.

Selecting const-in-data is the most efficient of the const-object settings, but this will increase data memory usage. Select the const-in-code setting if data memory is limited.

The small-code model uses calls whose range is relative to their position. As your code size increases beyond the 32 kWord limit of these calls, it becomes more likely that relative calls will fail to reach their destination and your code will require the absolute calls used by the large-code model. Since the linker will generate an error if relative calls are out of range, start your projects using the small-code model and only swap to the large-code model if errors prevent your code from linking.

Remember that you can use a different memory model selection for each module in your project, though you might have to qualify external references with the near and far specifiers to properly access them in mis-matching modules. You can also qualify variables and functions individually using the near and far qualifiers if you wish to over-ride the currently used memory model selection.

Consult your compiler’s User’s Guide for full information on the memory model options and their effects.

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.