Question: What are the various points to consider to achieve code size optimization in the ImageCraft compiler?
There are several points to keep in mind when optimizing code size in ImageCraft , which will be discussed in this article.
Relocatable start code address
This is the address from where the compiler starts placing relocatable code. It has to be set to a value after the end of boot.asm code. The optimal ‘Relocatable start code address’ is right after the end of boot.asm code. When this address is greater than what it needs to be, HALTs are added into the hex file to fill up the memory between the end of boot.asm code and the beginning of the relocatable. This results in waste of code space.
The end of Boot code can be obtained from the map (project_name.mp) file and the same may be set as the Relocatable code start address.
Area Start End Decimal Bytes (Attributes)
-------------------------------- ---- ---- ------- ----- ------------
TOP 0068 00F5 = 245. bytes (abs,ovr,rom)
In the example above, the area TOP ends at address 00F5. To set the Relocatable code start address, go to Project >> Settings menu and select the Linker option and enter 00F5 in the Relocatable Code Start address parameter.
The size of boot.asm differs for each device family and on the global parameter settings. So, whenever a change is made to the Global Parameters, check the .mp file and adjust the Reloctable Code Start Address parameter.
Function calls in Interrupt Service Routine (ISR)
When a function is called from inside a C ISR, ImageCraft “push”es all the virtual registers (__r0, __r1, __rx, __ry etc) on to stack and “pop”s them before return. Depending on the number of virtual registers in the project, this could add up to 120 bytes of code space. So avoid calling functions from inside ISRs. A good practice is to set flags inside the ISRs and do the actual processing in the foreground.
Configuration Initialization Type
For optimum code size, select ‘Loop(size efficient)’ in the Project >> Settings >> Chip Editor, “Configuration Initialization Type parameter.
When this option is selected, the psocconfigtbl.asm file uses tables to store the initial configuration register values and loops through these tables and loads the configuration registers. This saves a lot of code space at the cost of execution time. When “Direct Write” option is selected, each configuration register is written with a “mov reg…” instruction, which is faster but uses more code space
Sublimation - Delete Unused APIs
Use the Sublimation feature to remove unused APIs from the code. Go to Project >> Settings menu and enable the Sublimation option under the “Code Compression Techniques”
Condensation (Eliminate Duplicate Code)
In the screen shot above, when the Condensation option is enabled, the compiler substitutes repeating code in the project with subroutines and places a call to these subroutines wherever the code appears. This results in good amount of code reduction.
Floating Point operations
Floating point math libraries occupy more code space than integer libraries. So, wherever possible use integer math instead of floating point math.
Convert part of code to Assembly
A carefully written assembly code is often small than compiled ‘C’ code. So, try changing parts of ‘C’ code to assembly code, wherever possible.
“Switch” statement Vs “if-else if” statement
For a switch statement on a single byte variable (BYTE) the ImageCraft compiler will produce more efficient code using an if-else if-else construct rather than a switch construct. The number of bytes different in size is 9 bytes plus 5 bytes for each case. For example a 4 case switch statement with a default clause will use (9 5 * 4) = 29 more bytes than the equivalent implementation using an “if - else if” implementation.
When the switch statement is for a two byte variable (WORD) the resulting code size is nearly identical for either the “switch” or the “if-else if” implementation.