| The Datasheet Archive - 100 Million Datasheets from 7500 Manufacturers. |
Microcontroller Division Application Team language originally cre
Top Searches for this datasheetWRITING OPTIMIZED HIWARE LANGUAGE Microcontroller Division Application Team language originally created UNIX system computers used wide number application areas especially embedded 8-bit microcontroller systems. This choice extend language micro applications mainly structured language based data types enhanced control structures. portability different microcontroller target machines. purpose this note present write optimized software application embedded system programming. main topics focus write source code that generates smallest code data size. reach this goal some specific language extensions have used like compiler options pragmas. This application note based Hiware compiler some examples applied other compilers. exhaustive more details concerning Hiware compiler please refer "HICROSS ST7" manual. AN1064/1098 1/30 Table Contents INTRODUCTION COMPILER OVERVIEW SYNTAX WRITING BASIC TYPE SIZE LOCAL VARIABLES PARAMETERS STRATEGY PARAMETER PASSING FUNCTION RETURN MEMORY ALLOCATION 2.4.1 CONSTANT MEMORY ALLOCATION 2.4.2 VARIABLE DATA MEMORY ALLOCATION 2.4.3 CODE MEMORY ALLOCATION MEMORY ACCESS POINTER INTERRUPT HANDLING 2.6.1 FUNCTION INTERRUPT SERVICE ROUTINE 2.6.2 INTERRUPT FUNCTIONS HIWARE INTERNAL VARIABLES ASSEMBLER 2.7.1 SYNTAX 2.7.2 ASSEMBLER LANGUAGE 2.7.3 SPECIFIC ASSEMBLER FEATURES GENERAL PROGRAMMING RULES SOURCE HEADER FILES MEANINGFUL NAMING STRATEGY ANSI LANGUAGE GENERIC FUNCTIONS MACROS AVOIDING GOTO STATEMENTS STATIC GLOBAL/LOCAL VARIABLES VOLATILE QUALIFIERS BITFIELD STRUCTURE OPTIMIZING CODE SOURCE MODULES 2/30 Table Contents GENERAL APPROACH OPTIMUM VARIABLE DEFINITIONS 4.2.1 SMALLEST DATA TYPE 4.2.2 SHORT ADDRESSING MODE 4.2.3 VOLATILE QUALIFIER 4.2.4 STATIC LOCAL VARIABLE 4.2.5 UNION TYPE 4.2.6 AVOID ENUM TYPE 4.2.7 OPTIMUM TABLE STRUCTURE ACCESS 4.2.8 MASKED BYTE VARIABLES INSTEAD BITFIELDS 4.2.9 OPTIMIZED TYPE 4.2.10OPTIMUM POINTER ACCESS 4.2.11BASIC TYPE CONVERSION 4.2.12NO OPTIMIZATION: REGISTER, AUTO. TEMPORARY COMPILER VARIABLES OPTIMUM CONSTANT DEFINITIONS OPTIMUM STATEMENT MANAGEMENT 4.5.1 NON-RELEVANT SOURCE CODE 4.5.1.1UNREACHABLE CODE 4.5.1.2REDUNDANT CODE 4.5.2 BYTE SIZE OBJECT OPERATIONS 4.5.2.1OPTIMUM BASIC OPERATIONS 4.5.2.2BYTE DIVISION 4.5.2.3OPTIMUM BYTE SHIFT 4.5.3 WORD SIZE OBJECT OPERATION 4.5.3.1OPTIMUM BASIC OPERATIONS 4.5.3.2WORD MULTIPLICATION DIVISION 4.5.4 CONDITIONAL STATEMENTS 4.5.4.1IF STATEMENT INSTEAD SWITCH 4.5.4.2TEST CONDITION ORDER 4.5.4.3IF STATEMENTS ASSIGNMENTS OPTIMUM FUNCTION MANAGEMENT 4.6.1 FUNCTIONS MACROS 4.6.2 FUNCTION PARAMETERS RETURN VALUE 4.6.3 FUNCTION PARAMETER DECLARATION ORDER CONCLUSION 3/30 COMPILER OVERVIEW SYNTAX COMPILER OVERVIEW SYNTAX Hiware compiler application which launched either through batch command (from make file instance) interactive window interface. both cases command line following structure: C:\HICROSS\PROG\CST7.EXE -Wpd -Cni FILE.C COMPILER OPTIONS COMPILER COMMAND (ONLY NEEDED BATCH STYLE) SOURCE FILE COMPILE Note: active "default.env" file COMPOPTIONS variable filled with default compiler options, these options taken into account each compilation. WRITING BASIC TYPE SIZE ANSI specifies that size basic types given architecture target microcontroller, following table gives default format ST7. Type (unsigned) char signed char unsigned (signed) unsigned short (signed) short enum unsigned long (signed) long float, double 32bit IEEE32 16bit Default format 8bit Default value range -128 -32768 -32768 -32768 -2147483648 65535 32767 65535 32767 32767 4294967295 2147483647 "Hicross ST7" manual more details option 8bit, 16bit, 32bit Format available with option 1.17.E-38F 3.40.E+38F IEEE32 LOCAL VARIABLES PARAMETERS STRATEGY Usually most compilers local variables parameters stored stack. generate most efficient code architecture, Hiware compiler does stack this purpose. dedicated area (called OVERLAP) where data overlapped much possible with reference execution structure preferred. 4/30 WRITING fact, this strategy fact that instruction does allow simple access data stored stack (like A,[SP,#-n]). only possibility would have been instruction solution each stack data access followed A,([#0x0100+n,X] where 0x0100 stack bottom address data place offset). PARAMETER PASSING FUNCTION RETURN described previous paragraph, default function parameters stored dedicated OVERLAP memory area. But, last bytes parameter list also always passed through hardware registers least significant bytes return value also transferred through these registers. char FunctionName (char prmN, char prm1, char prm0) OVERLAP area register register register (int: register) OVERLAP area used store these parameters when needed computation function. This technique does avoid OVERLAP area when possible, does some optimization use. Only stored OVERLAP area char AddChar (char char return(p2+p1); 0000 0002 0004 p1,X A,p1 MEMORY ALLOCATION First compiler memory model chosen based application type. following table gives some guidelines choosing correct compiler memory model option. Compiler Option Memory Model SMALL Local Data Parameters Page Global Data Page Application Type Application with used page Most efficient code generation standard application (local variables parameters used more often than global data). Very large application with local variables parameters which stored page DATA_SEG SHORT. LARGE Page Page 1.N* LARGE Extended Page Page 1.N* *Note: Global variables forced page through pragma 5/30 WRITING 2.4.1 CONSTANT MEMORY ALLOCATION language constant declared with const keyword. default, Hiware compiler allocates constants DEFAULT_RAM area. force constant memory allocation ROM_VAR area compiler option activated. const char table[] {.}; const char *table[] {.}; Note: ROM_VAR keyword always defined linker parameter file. When constant allocated specific area (not default one), pragma CONST_SEG used (this pragma valid next CONST_SEG pragma). #pragma CONST_SEG MY_ROM const char table[] {.}; PLACEMENT DECLARED LINKER PARAMETER FILE 2.4.2 VARIABLE DATA MEMORY ALLOCATION default variables allocated DEFAULT_RAM area. applications some variables must have fixed addresses (hardware register definitions variables forced page ANSI style, only this directly address preprocessor capability. #define (*(char*) (0x0010)) drawback this method that real variables used which makes debugging task harder. resolve this, Hiware compiler provides DATA_SEG pragma. With this pragma variables allocated specific memory area. #pragma DATA_SEG MY_RAM char var; PLACEMENT DECLARED LINKER PARAMETER FILE When variables allocated page (from 0x0000 0x00FF), compiler know this information minimize code when they used. This allocation done through pragma qualifier SHORT. #pragma DATA_SEG SHORT MY_PAGE0 char page0var; PLACEMENT DECLARED LINKER PARAMETER FILE Hiware linker allocates memory sequentially order declaration optimizes data memory space when variable used application. this optimization each pragma DATA_SEG valid next, some care taken when declaring hardware registers. fact, avoid hardware register address 6/30 WRITING shifting, symbol added each object file name linker parameter file where there such declaration. FILE.C #pragma DATA_SEG SHORT REG_AREA volatile char ADCDR; volatile char ADCCSR; #pragma DATA_SEG DEFAULT FILE.PRM NAMES FILE.O+ PLACEMENT REG_AREA NO_INIT 0x0070 0x0072 Without ADCDR register used, ADCCSR allocated address 0x0070 instead 0x0071. Note: pragma DATA_SEG valid next one, when using header file, always remember reselect DEFAULT_RAM area end. FILE.H #pragma DATA_SEG SHORT REG_AREA #pragma DATA_SEG DEFAULT FILE FILE.C #include "FILE.H" char var; Without default pragma, "var" allocated REG_AREA. 2.4.3 CODE MEMORY ALLOCATION some circumstances, applications need allocate part generated code specific area. allow this, Hiware compiler provides pragma called CODE_SEG. #pragma CODE_SEG MY_ROM void FctName1 (void) #pragma CODE_SEG DEFAULT void FctName2 (void) PLACEMENT DECLARED FILE This pragma applied code same DATA_SEG applied variables. This means that valid next pragma statement. 7/30 WRITING MEMORY ACCESS POINTER When LARGE memory models selected (-Ml compiler options), optimum access data located page through pointer reached with "near" keyword. Without near keyword: char near FunctionName (char near ptr) return ((char near) +1); 0000 0002 0003 0004 0006 0008 0009 000A A,#0x01 A,#0x00 With near keyword: 0000 0001 When SMALL memory model selected (-Ms compiler option), only access data located outside page through pointer only accessed with keyword. With keyword: char FunctionName (char ptr) return ((char far) +1); 0000 0002 0003 0004 0006 0008 0009 000A A,#0x01 A,#0x00 Without keyword: 0000 0001 INTERRUPT HANDLING interrupt management capabilities which included ANSI standard, specific pragmas implemented Hiware compiler. 2.6.1 FUNCTION INTERRUPT SERVICE ROUTINE Writing TRAP_PROC pragma before function definition will produce replacement instruction IRET exit this function. #pragma TRAP_PROC void InterruptFct (void) Generated code: IRET Note: secure unused interrupt vectors, write empty dummy interrupt function. 8/30 WRITING 2.6.2 INTERRUPT FUNCTIONS HIWARE INTERNAL VARIABLES default Hiware compiler allocates storage variables extended operations. These variables called _SEX (for indirect storing), _LEX (for indirect loading) _R_Z (temporary storage) located page char *ptr; *ptr 0xFF; 0000 0003 0005 0007 000A 000C 000F A,ptr:0x1 _SEX:0x1,A A,#0xFF X,ptr _SEX,X [_SEX.W],A These variables used time (not controlled programmer) application, either main program interrupt routines. this reason, first development step, pragma SAVE_REGS used save these software registers. second step, after checking generated code interrupt routine, this pragma removed possible. #pragma TRAP_PROC SAVE_REGS void InterruptFct (void) PUSH PUSH PUSH PUSH PUSH PUSH IRET A,_R_Z A,_LEX:0x1 A,_LEX A,_SEX:0x1 A,_SEX _SEX,A _SEX:0x1,A _LEX,A _LEX:0x1,A _R_Z,A INTERRUPT PROLOG bytes cycles INTERRUPT EPILOG bytes cycles Note: drawback using SAVE_REGS pragma that code size increased bytes, interrupt latency increased cycles (4.25µs with fCPU=8MHz) total duration interrupt routine execution increased cycles (9.875µs with fCPU=8MHz). 9/30 WRITING ASSEMBLER critical source code specifically written microcontroller (mandatory write sequence, critical software timing.), language appropriate. provide solution Hiware compiler provides powerful (high level inline) assembler. 2.7.1 SYNTAX Hiware assembler based instruction (cf. Programming Manual) keyword. A,X; SINGLE ASSEMBLER INSTRUCTION ASSEMBLER BLOCK 2.7.2 ASSEMBLER LANGUAGE source data objects directly accessible with assembler (variables, functions, macros.). #define struct {char field1; char field2;} str; char var, array[10]; DIRECT ACCESS A,0xA100 MEMORY A,#CST A,str.field2 A,array[4] A,#var A,#LOW(var) A,#HIGH(var) CALL A,0xA100 A,#0x0A A,str:0x1 A,array:0x7 A,#var A,#var A,#HIGH(var) CALL A,array:7 VARIABLE ADDRESS ACCESS Note: ANSI preprocessor, symbol used string concatenation, instruction uses this symbol immediate values, Hiware compiler provides 10/30 WRITING dedicated NO_STRING_CONSTR pragma which disables this preprocessor feature. This pragma valid source modules. #pragma NO_STRING_CONSTR #define LDA10 A,#10 LDA10; A,#0x0A 2.7.3 SPECIFIC ASSEMBLER FEATURES syntax, branches data insertion code possible. following example shows some possible combinations: clear 0xFF SKIP JRNE loop 0000 0002 0003 0004 0005 0006 INSTRUCTION SKIPPED LOOP SKIP JRNE abs=0003 (X),X <JRF> abs=0005 clear: loop: Notes: compiler optimizes absolute branches into relative ones whenever possible. same ANSI labels, assembler labels valid only current function. Hiware assembler syntax, only operators available constant expressions. These are: A,#12+(2-4)+4*5/2; A,#0x14 11/30 GENERAL PROGRAMMING RULES GENERAL PROGRAMMING RULES goal this chapter give some programming rules improve legibility, portability robustness application source code. some circumstances, these rules help optimize generated code size. SOURCE HEADER FILES structured software application, whenever possible associate header file "*.h" with each "*.c" source file. This header file should contain extern definitions macros corresponding source file (all data functions which used other modules). Note: compiler provides -Wpd option prevent extern definition omissions. prevent #include recursions, header files have preprocessed. avoid duplication declarations several files (*.h *.c), macro definitions variable declarations have defined header file with extern preprocessing. FILE.H #ifndef FILE_H #define FILE_H #ifdef FILE_C #define #else #define extern #endif #define MACRO char myvar; #endif FILE.C #define FILE_C #include "FILE.H" myvar MACRO; Note: Hiware compiler dedicated pragma (called ONCE) replace header file preprocessing (FILE_H). MEANINGFUL NAMING STRATEGY reusable legible software modules, much possible implement function, variable, preprocessor symbol module naming strategy. MOD_FILE.C #define MOD_MACRO MIXCASE char Mod_cVar; TYPE MOD_iFct (void) UPPERCASE MIXCASE UPPERCASE 12/30 GENERAL PROGRAMMING RULES ANSI LANGUAGE most important rule best code portability write program much possible ANSI like language. Sometimes, better pragma instead specific nonANSI instructions unknown pragmas have ignored compiler. GENERIC FUNCTIONS MACROS generic functions much possible instead macros specific functions. Keep only very simple macros which generates optimum code (less instruction bytes than CALL instruction). Note: repeated instruction blocks source code have shrunk single function whenever possible. AVOIDING GOTO STATEMENTS most legible source code goto statement avoided each time there alternative. STATIC GLOBAL/LOCAL VARIABLES good legibility well structured application, following language objects have declared static: global variable used module where declared. function used only module where declared. permanent local variable function. temporary local variable function always declared local variable save area using OVERLAP strategy. char exported_var; static char module_var; static void module_fct (void) void exported_fct (void) char tmp_local_var; static char perm_local_var; VOLATILE QUALIFIERS microcontroller hardware registers which have their value modified directly hardware (status registers.), volatile qualifier added their definition allow compiler disable register value memory optimization. 13/30 GENERAL PROGRAMMING RULES BITFIELD STRUCTURE ANSI does specify allocation order bitfield, this structure should avoided portability reasons. Consequently, bitfield structures should used microcontroller hardware register definitions. 14/30 OPTIMIZING CODE SOURCE MODULES OPTIMIZING CODE SOURCE MODULES GENERAL APPROACH optimum generated code from Hiware compiler, following conditions have met: Choose memory model that suits application, bearing mind that optimum SMALL one, then LARGE finally LARGE Extended avoided possible). When Linking with LARGE memory model, near keyword much possible when accessing short addressing data with pointers. Choose best compiler options: -Cni: default ANSI operations with data type smaller than have promoted (integral promotion). With this option, this integral promotion omitted. aware that this option generate some wrong code like carry mistake following example: char BYTE ADDITION CARRY IGNORED A,c1 A,c2 i1:0x1,A i1,X this optimization option removes assignments local variables referenced later code. void function (char fctcall(); -Os/-Ot: fctcall CALLR fctcall A,#0x01 c1,A -Ou: default compiler optimizes generated code size wise (-Os). This default option based mainly library CALL statements instead single level instruction sequences (when option selected). following example illustrates this compiler option choice. signed char sc1; void function (void) sc1; 00[4] 00[6] 01[5] 00[5] CALL A,sc1 _SEXT16 i1:0x1,A i1,X JRPL label: A,sc1 label i1:0x1,A i1,X Note: optimization options have effect Hiware compiler. 15/30 OPTIMIZING CODE SOURCE MODULES OPTIMUM VARIABLE DEFINITIONS 4.2.1 SMALLEST DATA TYPE optimum code with 8-bit microcontroller, smallest data type size best way. ANSI language smallest types (bitfield) char type bits). instruction provides optimum handling instructions (BRES, BSET, BTJT, BTJF BCP), software this structure much possible binary data (this data always located page other data types, char preferred data type optimum generated code. Using 32-bit variables (floating point arithmetic long types) 8-bit microcontroller inherently inefficient character integer arithmetic should used whenever possible. 4.2.2 SHORT ADDRESSING MODE instruction provides three main advantages variables allocated page (from address 0x0000 0x00FF): short memory addressing access shorter code. instance A,var" instruction needs bytes when variable allocated page while needs three bytes when allocated outside (30% code size gain). ability directly handled instructions usually dedicated registers (INC, DEC, CLR, CPL, NEG, RLC, RRC, SLA, SLL, SRA, SRL,SWAP, TNZ). char #pragma DATA_SEG SHORT ZEROPAGE char cz1, #pragma DATA_SEG DEFAULT c1++; cz1++; A,c1 c1,A A,c1 c1,A handling capability (BRES, BSET, BTJT, BTJF instructions). these three reasons following variables must allocated whenever possible short address memory space (page variables which have used bitfields, most frequently used standard variables. 16/30 OPTIMIZING CODE SOURCE MODULES 4.2.3 VOLATILE QUALIFIER optimum generated code, volatile qualifier used only when needed systematically each hardware register. control register which modified only software hardware microcontroller reset does need volatile variable. volatile char PADR; volatile char PADDR; PADR data register port modified input mode hardware while PADDR only modified software hardware RESET. 4.2.4 STATIC LOCAL VARIABLE From legibility point view local static definitions used preference global ones whenever possible. This guarantees robustness program against wrong static local variable use. case local variable definitions, this qualifier used only permanent data (not overlapped). this static qualifier when needed avoid removing overlap optimization local variables increasing used size. void fct1 (void) static char var; var++; void fct2 (void) static char var; var=0; PERMANENT LIFE (PROGRAM) TEMPORARY LIFE (FUNCTION) 4.2.5 UNION TYPE global variables which have permanent validity during application execution which able defined local variables function, have grouped together optimized into union type structures. this method allows optimum allocation global variables automatically done with OVERLAP segment parameters local variables. MAIN LOOP VAR2 USED VAR1 USED union char var1; char var2; varunion; void main (void) varunion.var1 varunion.var2 Note: var1 value overwritten during application execution var2 one. 17/30 OPTIMIZING CODE SOURCE MODULES 4.2.6 AVOID ENUM TYPE ANSI standard enum type based type. This means that enumerated elements defined bytes target. this reason order generate optimum code, this enum type avoided replaced preprocessor #define command (bit byte format). enum {TRUE, FALSE} binary; (binary TRUE). char varbin; #define BINARY 0x01 Select 0.*/ (varbin BINARY). char list; #define ENUM1 #define ENUM2 list ENUM2; enum {ENUM1, ENUM2, ENUMn} list; list ENUM2; 4.2.7 OPTIMUM TABLE STRUCTURE ACCESS optimum code size this table structure data, each data much possible into 256-byte memory space allow compiler powerful indexed memory access instruction set. char char tc1[20], tc2[10]; struct SType char char f3[8]; char f4[25]; char struct SType ts1[3]; #pragma DATA_SEG SHORT ZEROPAGE struct SType *ps1; #pragma DATA_SEG DEFAULT tc1[c2] tc2[c2]; ps1->f6; s1.f4[c2]; &ts1[2]; ts1[c1].f1 X,c2 A,(tc2,X) (tc1,X),A X,#0x25 A,([ps1.W],X) c1,A X,c2 A,(s1:0x0b,X) c2,A A,#ts1:0x4c ps1:0x1,A A,#HIGH(ts1:76) ps1,A X,c1 A,#0x26 A,#0x01 (ts1,X),A 18/30 OPTIMIZING CODE SOURCE MODULES structure table memory alignment allocation done according defined complex type without creating memory holes. Based previous example, memory implementation table following: ts1[0] ts1[1] .f3[0] ts1[2] .f3[7] .f4[0] .f4[24] BYTES 4.2.8 MASKED BYTE VARIABLES INSTEAD BITFIELDS ANSI language provides bitfield structure definition with direct access data through syntactic combination. compiler optimizes code generates BRES, BSET, BTJT, BTJF instructions when using this bitfield structure (when allocated page #pragma DATA_SEG SHORT ZEROPAGE struct unsigned intb0:1; unsigned intb1:1; unsigned intb2:1;} vbf; #pragma DATA_SEG DEFAULT vbf.b2 (vbf.b0) vbf.b1 BSET vbf,#2 BTJF vbf,#0,label BRES vbf,#1 label: Note: more details concerning order assignment bitfield please refer "Hicross ST7" manual. But, ANSI does specify assignment order bitfield structure, this structure suitable generating portable microcontroller code especially when used hardware register definition. Then facing this limitation, basic type such char combined with mask expression preferable. main advantages are: optimum instruction code (bit instructions whenever possible optimum) optimum byte instruction code (byte assignment when optimum) portability (bit assignment order defined programmer) 19/30 OPTIMIZING CODE SOURCE MODULES following example illustrates these advantages shows purpose -Onbf compiler option.This option mainly needed when handling hardware register where setting clearing sequences critical. #pragma DATA_SEG SHORT ZEROPAGE char var; #pragma DATA_SEG DEFAULT CODE OPTIMIZED 0x20; WITHOUT -Onbf COMPILER OPTION 0xF7; 0x22; BSET vb,#5 BRES vb,#3 A,vb A,#0xF2 vb,A following examples describe masking methods handling. These methods based preprocessor macros which generate optimum code when data located page first example based parameter macros like instruction assembler. optimum code each variable located page MACRO NAME #define SetBit(VAR, PLACE) #define ClrBit(VAR, PLACE) #define ValBit(VAR, PLACE) MACRO SOURCE CODE PLACE) PLACE) PLACE) GENERATED CODE BSET VAR,#PLACE BRES VAR,#PLACE BTJT VAR,#PLACE, label BTJF VAR,#PLACE, label second method based parameter macro. This single parameter "bit address" page data manipulated. This address calculated using following expression: bit@ (byte@ bit_place) MACRO NAME #define BitSet(BIT) #define BitClr(BIT) #define BitVal(BIT) MACRO SOURCE CODE *((unsigned char*) (BIT/8)) (~(1<<(7-BIT%8))) *((unsigned char*) (BIT/8)) (1<<(7-BIT%8)) *((unsigned char*) (BIT/8)) (1<<(7-BIT%8)) GENERATED CODE BSET .,#. BRES .,#. BTJT .,#.,label BTJF .,#.,label 20/30 OPTIMIZING CODE SOURCE MODULES 4.2.9 OPTIMIZED TYPE microcontroller optimized type variable handling bytes). Nevertheless, simple accesses byte variables reduced minimum code size. char ti1[10]; ti1[c1]; A,i2:0x1 i1:0x1,A X,i2 i1,X X,c1 A,(ti1:0x1,X) i1:0x1,A A,(ti1,X) i1,A 4.2.10 OPTIMUM POINTER ACCESS instruction allows indirect memory access only pointers located page these pointers have allocated much possible short access memory area optimum generated code. char *p1; #pragma DATA_SEG SHORT ZEROPAGE char *p2; #pragma DATA_SEG DEFAULT *p1; PAGE OPTIMUM *p2; 01[4] 00[4] 00[6] 00[5] A,p1:0x1 _LEX:0x1,A X,p1 _LEX,X A,[_LEX.W] c1,A A,[p2.W] c2,A 00[6] 00[5] 4.2.11 BASIC TYPE CONVERSION embedded applications, usual convert basic types such char conversely. Here some ways converting these basic types language: #define LSB(WORD)WORD #define MSB(WORD)WORD>>8 #define WORD(MSB,LSB)(int) ((int) char msb, lsb; word1, word2; LSB(word1); MSB(word1); word2 WORD(msb, lsb); A,word1:0x1 lsb,A A,word1 msb,A word2:0x1,X word2,A Note: same conversion applied types keeping optimum generated code. 21/30 OPTIMIZING CODE SOURCE MODULES 4.2.12 OPTIMIZATION: REGISTER, AUTO. accumulator-based machine, ANSI register auto qualifiers have meaning. fact, compiler optimizes code using registers much possible. TEMPORARY COMPILER VARIABLES some complex expressions, compiler temporary variables solve operations. These temporary variables registers Hiware software registers (_SEX, _LEX _R_Z) stack locations (using PUSH instructions) spills temporary variables (spill_0, spill_1.) spills temporary variables managed compiler like local variables parameters, which means that they allocated OVERLAP memory segment. Note: specific applications where stack management critical, compiler used avoid stack location being used temporary storage. char void (void); void function (void) char f(); -Cns PUSH CALL CALL A,c1 c2,A A,c1 spill_0,A A,spill_0 c2,A -Cns option OPTIMUM CONSTANT DEFINITIONS embedded microcontroller application kinds constant definitions exist: simple constant values data constant structure (fixed data lists, function pointer tables.) Both these constants have allocated area (see Section 2.4). 22/30 OPTIMIZING CODE SOURCE MODULES When simple constant value used expression, always defined through preprocessor #define command through const variable avoid space being unnecessarily allocated constant values. #define CONST1 const char CONST2 char CONST1; CONST2; A,#0x0A c1,A other hand, data constants which have accessed like tables strings which have used several time during application, using const variable optimum solution. const char CONST[] {1,2,3}; char CONST[1]; CONST[c2]; A,#0x02 c1,A X,c2 A,(CONST,X) c1,A Note: compiler generates most optimum code either using #define command const variable. When const variable used, allocated data only when mandatory (indexed access.) else compiler replaces constant value directly code. OPTIMUM STATEMENT MANAGEMENT 4.5.1 NON-RELEVANT SOURCE CODE 4.5.1.1 UNREACHABLE CODE compiler does some automatic optimization unreachable code which usually sign programmer error. char c1=2; c1=3; goto label; c1=4; label: c2=5; A,#0x03 c1,A A,#0x05 c2,A 23/30 OPTIMIZING CODE SOURCE MODULES 4.5.1.2 REDUNDANT CODE compiler does some automatic optimization redundant code which usually sign programmer error. char Compiler WARNING message. CODE GENERATION 4.5.2 BYTE SIZE OBJECT OPERATIONS main byte size object language basic standard 4.5.2.1 OPTIMUM BASIC OPERATIONS following example illustrates efficiency byte object used basic expressions. char A,c2 c1,A A,c2 A,c3 c1,A char type. A,c2 A,c3 c1,A A,c3 X,c2 c1,A 4.5.2.2 BYTE DIVISION instruction does provide division capability, application written much possible with power divisions order optimum generated code with shift instructions. Otherwise generated code call Hicross library with resulting increase size needs. char SWAP A,c2 A,#0x0F c1,A CALL A,c3 X,#0x3F _BDIVU c2,A Note: more details Hicross library, please refer "Hicross ST7" manual. 24/30 OPTIMIZING CODE SOURCE MODULES 4.5.2.3 OPTIMUM BYTE SHIFT Inside embedded application, usual shift operations. compiler provides optimum code generation using SWAP instructions instead sequential ones when best choice. char A,c1 c1,A SWAP A,c3 A,#0x0F c3,A SWAP A,c2 A,#0x0F c2,A 4.5.3 WORD SIZE OBJECT OPERATION basic standard type main word size object (two bytes) language. 4.5.3.1 OPTIMUM BASIC OPERATIONS basic operations types like additions subtractions, corresponding resources (ADD, ADC, SUB, SUBC instructions) provide optimum generated code. i1++; A,i2:0x1 A,i3:0x1 i1:0x1,A A,i2 A,i3 i1,A A,i2:0x1 A,i3:0x1 i1:0x1,A A,i2 A,i3 i1,A JRNE label: A,i1:0x1 X,i1 label i1:0x1,A i1,X Note: Either integer addition subtraction least significant byte always affected first. This cause some problems some critical hardware register settings (16-bit timer counter registers). 25/30 OPTIMIZING CODE SOURCE MODULES 4.5.3.2 WORD MULTIPLICATION DIVISION Complex word operations such multiplication division need Hicross library solve requested expression does provide hardware resources these purposes. PUSH PUSH CALL X,i2 A,i2:0x1 A,i3:0x1 X,i3 _IMULS i1,X i1:0x1,A PUSH PUSH CALL X,i3 A,i3:0x1 A,i4:0x1 X,i4 _IDIVS i2,X i2:0x1,A Note: more details Hicross library, please refer "Hicross ST7" manual. 4.5.4 CONDITIONAL STATEMENTS 4.5.4.1 STATEMENT INSTEAD SWITCH ANSI language provides control statements which used same sequential test purpose: IF/ELSE SWITCH/CASE. Hiware compiler better optimized IF/ELSE statements than SWITCH/CASE statements IF/ELSE much possible, also sequential testing. else else else else else (c1==0) c1=1; (c1==10) c1=2; (c1==22) c1=3; (c1==13) c1=4; (c1==45) c1=5; c1=6; switch (c1) case c1=1; case c1=2; case c1=3; case c1=4; case c1=5; default: c1=6; break; break; break; break; break; break; bytes bytes Note: Using IF/ELSE statement also advantage specifying range conditions. 26/30 OPTIMIZING CODE SOURCE MODULES 4.5.4.2 TEST CONDITION ORDER terms execution speed optimization, test condition with highest probability false always considered first. second rule take into account place fastest test conditions first. char 0x55)) ((c2 0x55) JREQ JREQ end: end: A,c1 A,c2 A,#0x55 c1,A JREQ JREQ A,c2 A,#0x55 A,c1 c1,A SAME TEST SAME GENERATED CODE SIZE Note: this rule applied IF/ELSE statements also statements conditioned test (FOR, WHILE.). 4.5.4.3 STATEMENTS ASSIGNMENTS optimum generated code, much possible include assignment condition. char (c1+c2 10)c1 else (c1+=c2) JRNC end: A,c1 A,c2 c1,A A,#0x0A c1,A label1:CLR label2:LD A,c1 A,c2 A,#0x0A label1 A,c1 A,c2 label2 c1,A bytes bytes Note: This rule applied IF/ELSE statements also statements conditioned test (FOR, WHILE.). 27/30 OPTIMIZING CODE SOURCE MODULES OPTIMUM FUNCTION MANAGEMENT 4.6.1 FUNCTIONS MACROS term generated code size short expression sequences, usually better define preprocessor macro instead function (such variable get/clear functions). Given this condition compromise between generated code size legibility selected according priorities depending application. first example that follows, "macro" solution been chosen. major advantage that generated code size minimized each time variable accessed. drawback that there security concerning access variable from other module. FILE.H extern char var; #define GET_Value FILE.C char var; contrary, second example, "function" solution been chosen. major advantage that variable protected against unwanted direct access from another module (declared static file.c module). drawback related CALL instructions (code size execution time increased): total generated code size increased bytes (LD, RET) Each access variable increased cycles (CALL, above data assumes that FILE.H variable allocated outside page FILE.C static char var; char Get_Value(void) return(var); extern char Get_Value(void); 4.6.2 FUNCTION PARAMETERS RETURN VALUE registers used parameter passing function return value, source which generates optimum code contain many functions possible with maximum byte size parameters byte size return value. char fct1(int fct2(char char 28/30 OPTIMIZING CODE SOURCE MODULES 4.6.3 FUNCTION PARAMETER DECLARATION ORDER optimize registers parameter passing, order parameters parameter definition list reversed compared order which they used function. following examples illustrate importance parameter declaration order. first example, code optimum with parameter passing through registers char fctparam1 (char char char return(p2); c1,A A,p2 second example, source code same previous one, only parameter declaration order changed. result this move drastic increase generated code size (more than 130%) OVERLAP segment usage more bytes). char fctparam2 (char char char return(p2); p0,X p2,A A,p0 X,p1 c1,A A,p2 29/30 CONCLUSION CONCLUSION Hiware compiler powerful tool developing embedded application language. Following rules described this document provides major added value generated optimum code microcontroller. PRESENT NOTE WHICH GUIDANCE ONLY AIMS PROVIDING CUSTOMERS WITH INFORMATION REGARDING THEIR PRODUCTS ORDER THEM SAVE TIME. RESULT, STMICROELECTRONICS SHALL HELD LIABLE DIRECT, INDIRECT CONSEQUENTIAL DAMAGES WITH RESPECT CLAIMS ARISING FROM CONTENT SUCH NOTE AND/OR MADE CUSTOMERS INFORMATION CONTAINED HEREIN CONNEXION WITH THEIR PRODUCTS. Information furnished believed accurate reliable. However, STMicroelectronics assumes responsibility consequences such information infringement patents other rights third parties which result from use. license granted implication otherwise under patent patent rights STMicroelectronics. Specifications mentioned this publication subject change without notice. This publication supersedes replaces information previously supplied. STMicroelectronics products authorized critical components life support devices systems without express written approval STMicroelectronics. logo registered trademark STMicroelectronics ©1999 STMicroelectronics Rights Reserved. Purchase Components STMicroelectronics conveys license under Philips Patent. Rights these components system granted provided that system conforms Standard Specification defined Philips. STMicroelectronics Group Companies Australia Brazil Canada China France Germany Italy Japan Korea Malaysia Malta Mexico Morocco Netherlands Singapore Spain Sweden Switzerland Taiwan Thailand United Kingdom U.S.A. http://www.st.com 30/30 Other recent searchesLTC1867L - LTC1867L LTC1867L Datasheet LTC1867L - LTC1867L LTC1867L Datasheet KDS112V - KDS112V KDS112V Datasheet ET9560 - ET9560 ET9560 Datasheet DPDD32MX16WSCY5 - DPDD32MX16WSCY5 DPDD32MX16WSCY5 Datasheet AN97046 - AN97046 AN97046 Datasheet 1786060000 - 1786060000 1786060000 Datasheet 0892DP15B1850 - 0892DP15B1850 0892DP15B1850 Datasheet
Privacy Policy | Disclaimer |