Helpful ReplyLockedHow to explain XC8 / Hi-Tech generated code to our students ???

Page: 1234 > Showing page 1 of 4
Author
vloki
Jo, alla!
  • Total Posts : 6758
  • Reward points : 0
  • Joined: 2007/10/15 00:51:49
  • Location: Germany
  • Status: offline
2012/08/06 03:07:48 (permalink)
5 (3)

How to explain XC8 / Hi-Tech generated code to our students ???



EDIT(3):
With XC8 V1.20 there is a new FREE optimization option:
New Free mode optimizations
The assembler's jump-to-jump optimizations, which previously was only available with a licensed compiler operating mode,
is now available in Free mode. By default, this optimization is disabled, but it can be enabled from the --OPT option
or the Optimization category in MPLAB X IDE in the usual way.
If enabled, this optimization reduced the size of code output by the compiler.

-> The generated code is more like with the C18 in free mode now ;-)
 
IF a microchip representative reads this:
May we have one more free optimization please mr green, to get rid of nonsense savings of the W register like here:
33:            void main() {      
34:                unsigned char c1, c2;
35:           
36:                c1 = 10; c2 = 20;
7FE4  6E01     MOVWF __pcstackCOMRAM, ACCESS  ????????????????????????????????????????????????????????????????????
7FE6  0E0A     MOVLW 0xA
7FE8  6E02     MOVWF c1, ACCESS
7FEA  5001     MOVF __pcstackCOMRAM, W, ACCESS ???????????????????????????????????????????????????????????????????
7FEC  6E01     MOVWF __pcstackCOMRAM, ACCESS ????????????????????????????????????????????????????????????????????
7FEE  0E14     MOVLW 0x14
7FF0  6E03     MOVWF c2, ACCESS
7FF2  5001     MOVF __pcstackCOMRAM, W, ACCESS ????????????????????????????????????????????????????????????????????
37:                while (1) {
38:                    if (BTN_1 == BTN_ACTIVATED){
7FF4  B481     BTFSC PORTB, 2, ACCESS
7FF6  D002     BRA 0x7FFC
39:                        LED_4 = LED_ON;
7FF8  9A8A     BCF LATB, 5, ACCESS
40:                    }  else
7FFA  D7FC     BRA 0x7FF4
41:                        LED_4 = !LED_ON;
7FFC  8A8A     BSF LATB, 5, ACCESS
42:                }
43:            }






 
At the moment I'm working on a new tutorial for MPLAB-X / XC8
It's based on a former one (MPLAB / C18)

The very first ("Hello World") program is controlling an LED via button in assembler and C.
The code for both is:
Main                        ; main code                  
    btfss   nBUTTON         ; button not activated ?
    bcf     nLED            ;  ? skip switch on
    btfsc   nBUTTON         ; button activated ?
    bsf     nLED            ;  ? skip swich off
    bra     Main
------------------------------------------------------------------------------------
    while (1) {
        if (BTN_1 == BTN_ACTIVATED)
            LED_4 = LED_ON;
          else
            LED_4 = !LED_ON;
    }

The Assembler/Linker produced code:
    8    000E    A481    Main    BTFSS 0xF81, 2, ACCESS                  
    9    0010    9A8A        BCF 0xF8A, 5, ACCESS
    10    0012    B481        BTFSC 0xF81, 2, ACCESS
    11    0014    8A8A        BSF 0xF8A, 5, ACCESS
    12    0016    D7FB        BRA 0xE

With C18 (lite/academic) we get:
114    00E2    B481    main    BTFSC 0xF81, 2, ACCESS                  
    115    00E4    D002        BRA 0xEA
    116    00E6    9A8A        BCF 0xF8A, 5, ACCESS
    117    00E8    D7FC        BRA 0xE2
    118    00EA    8A8A        BSF 0xF8A, 5, ACCESS
    119    00EC    D7FA        BRA 0xE2
This is OK (one word in addition ;-)
 
Now XC8 in free mode:
16372    7FE6    D000        BRA 0x7FE8                        <- ???                 
    16373    7FE8    B481        BTFSC 0xF81, 2, ACCESS
    16374    7FEA    D001        BRA 0x7FEE                         <- ???
    16375    7FEC    D001        BRA 0x7FF0                         <- ???
    16376    7FEE    D002        BRA 0x7FF4                         <- ???
    16377    7FF0    9A8A        BCF 0xF8A, 5, ACCESS
    16378    7FF2    D7FA        BRA 0x7FE8
    16379    7FF4    8A8A        BSF 0xF8A, 5, ACCESS
    16380    7FF6    D7F8        BRA 0x7FE8                         <- ???
    16381    7FF8    D7F7        BRA 0x7FE8                         <- ???
    16382    7FFA    D7F6        BRA 0x7FE8                        <- ???
" And I started jumpin up and down yelling, "KILL, KILL," (http://www.arlo.net/resources/lyrics/alices.shtml/)
 
And finally HI-TECH 9.65 lite mode:
16370    7FE2    B481        BTFSC 0xF81, 2, ACCESS                  
    16371    7FE4    EFF6        GOTO 0x7FEC
    16372    7FE6    F03F        NOP
    16373    7FE8    EFF8        GOTO 0x7FF0
    16374    7FEA    F03F        NOP
    16375    7FEC    EFFB        GOTO 0x7FF6
    16376    7FEE    F03F        NOP
    16377    7FF0    9A8A        BCF 0xF8A, 5, ACCESS
    16378    7FF2    EFFC        GOTO 0x7FF8
    16379    7FF4    F03F        NOP
    16380    7FF6    8A8A        BSF 0xF8A, 5, ACCESS
    16381    7FF8    EFF1        GOTO 0x7FE2
    16382    7FFA    F03F        NOP
Wow GOTO !!!
 
Is this normal (in lite/free mode) ?
Is it the same in pro ???
Anyone got an idea why it is done this way ?
 
<edit>
AND FINALLY: Should I better drop the whole idea writing a XC8 tutorial ???


<edit2>
Code produced by SDCC compiler:
                                                  271:   _00109_DS_: 
                                                  272:   ;    .line    45; Quick_main_22a.c    if(BTN_1 == BTN_ACTIVATED)
00CE  B481     BTFSC PORTB, 2, ACCESS             273:       BTFSC    _PORTBbits, 2
00D0  D002     BRA 0xD6                           274:       BRA    _00106_DS_
                                                  275:   ;    .line    46; Quick_main_22a.c    LED_2 = LED_ON;
00D2  968A     BCF LATB, 3, ACCESS                276:       BCF    _LATBbits, 3
00D4  D7FC     BRA 0xCE                           277:       BRA    _00109_DS_
                                                  278:   _00106_DS_:
                                                  279:   ;    .line    48; Quick_main_22a.c    LED_2 = !LED_ON;
00D6  868A     BSF LATB, 3, ACCESS                280:       BSF    _LATBbits, 3
00D8  D7FA     BRA 0xCE                           281:       BRA    _00109_DS_

post edited by vloki - 2013/07/09 07:24:08

Uffbasse !
#1
bxs
Senior Member
  • Total Posts : 166
  • Reward points : 0
  • Joined: 2011/01/01 08:49:16
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/06 06:32:20 (permalink)
0 (2)
For me this is no surprise at all, Hitech/XC8 are pure s**** in free mode.
 
As you saw is not possible to explain how the generated code can be so bad, but for me is very simple, it is done this way on purpose, so the Pro version can look better and you have to pay for it.
 
This is quite sad, I hate this strategics just go get more $$$€€€
 
So forget free and use Pro, or simple use C18, if you don't need a compiler from microchip exist also several good compilers from 3rd party.
 
#2
ytlee
Super Member
  • Total Posts : 1135
  • Reward points : 0
  • Joined: 2003/11/07 12:44:25
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/06 06:56:27 (permalink)
+1 (1)
I wonder if those excess BRAs were placeholders.
#3
vloki
Jo, alla!
  • Total Posts : 6758
  • Reward points : 0
  • Joined: 2007/10/15 00:51:49
  • Location: Germany
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/06 07:43:49 (permalink)
0
bxs
...for me is very simple, it is done this way on purpose, so the Pro version can look better and you have to pay for it.

Yes, it could hardly be a missing optimization pink
 
An optimization would be what we see in the case of the assembly code compared to the C18 generated code.
If the instruction block that has to be executed is one instruction only,
there is no need to skip a jump, the instruction can be skipped directly ;-)

Compilers
Optimizations
Microchip C Compilers are available with different optimization levels to suit your development needs.

What we see above in case of HI-$ compilers looks like kind of different DIS-optimization levels.
(Is there a word for the opposite of optimization ? )
post edited by vloki - 2012/08/06 07:50:09

Uffbasse !
#4
1and0
Access is Denied
  • Total Posts : 8392
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/06 08:14:52 (permalink) ☄ Helpful
+2 (1)
Perhaps this can give you ideas about *some* of the BRAs/GOTOs.
 
#5
vloki
Jo, alla!
  • Total Posts : 6758
  • Reward points : 0
  • Joined: 2007/10/15 00:51:49
  • Location: Germany
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/06 09:57:52 (permalink)
0
ytlee
I wonder if those excess BRAs were placeholders.

Hmm, placeholders for multiple conditions ?
Like:
    if(conditon11..condition_1n){
        ...
    }
    else if(condition_21..condition_2n){
        ...
    }
    else ...
 
Everything very "generic" and OOP.
I guess the students will like it LoL
post edited by vloki - 2012/08/06 10:02:02

Uffbasse !
#6
NKurzman
A Guy on the Net
  • Total Posts : 16430
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/06 11:02:04 (permalink)
+2 (1)
there are compiler books ( the dragon book)  Compilers are there own subject.  And Optimization another.
#7
al_bin
Super Member
  • Total Posts : 154
  • Reward points : 0
  • Joined: 2011/02/11 06:28:47
  • Location: 0
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/06 22:37:16 (permalink) ☄ Helpful
0
For this purpose SDCC is good solution.

Albert
#8
vloki
Jo, alla!
  • Total Posts : 6758
  • Reward points : 0
  • Joined: 2007/10/15 00:51:49
  • Location: Germany
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/07 03:04:45 (permalink)
0
1and0
Perhaps this can give you ideas about *some* of the BRAs/GOTOs.

Ok, I tried different conditions like:
void main() { 
    unsigned char c1, c2;
    c1 = 10;
    c2 = 20;
     __init();

    while (1) {
//        if ((BTN_1 == BTN_ACTIVATED)&&(BTN_2 != BTN_ACTIVATED))
        if((c1 >= c2)&&(c2 < 17))
            LED_4 = LED_ON;
        else if((BTN_1 != BTN_ACTIVATED)&&(BTN_2 == BTN_ACTIVATED))
            LED_4 = LED_ON;
        else
            LED_4 = !LED_ON;
    }
}

Always the result is a conditional jump (BTFSx, CPFxLT)  followed by three BRAs
where the 1st one ALWAYS jumps to the 3rd one pink
 
It came to my mind that this possibly be is to adjust the same execution time for condition true and false,
but BRA is 2 cycles always (1+2+2 != 2+2)
 
Another free feature seems to be that the compiler takes extremely care of the working register LoL
It is saved always:
43:                c1 = 10; 
7FAE  6E01     MOVWF 0x1, ACCESS
7FB0  0E0A     MOVLW 0xA
7FB2  6E02     MOVWF c1, ACCESS
7FB4  5001     MOVF 0x1, W, ACCESS
44:                c2 = 20;
7FB6  6E01     MOVWF 0x1, ACCESS
7FB8  0E14     MOVLW 0x14
7FBA  6E03     MOVWF c2, ACCESS
7FBC  5001     MOVF 0x1, W, ACCESS


Uffbasse !
#9
vloki
Jo, alla!
  • Total Posts : 6758
  • Reward points : 0
  • Joined: 2007/10/15 00:51:49
  • Location: Germany
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/07 04:36:15 (permalink)
0
al_bin
For this purpose SDCC is good solution.
Albert

Seems it does no support 18F25K22 yet. (Post #3 16f877a and sdcc in mplabx did not help) pink
OK, lets play with a 2550
post edited by vloki - 2012/08/07 05:15:12

Uffbasse !
#10
rpg7
Super Member
  • Total Posts : 1362
  • Reward points : 0
  • Joined: 2003/11/07 12:47:35
  • Status: online
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/07 07:23:44 (permalink)
0
vloki
al_bin
For this purpose SDCC is good solution.
Albert

Seems it does no support 18F25K22 yet. (Post #3 16f877a and sdcc in mplabx did not help) pink
OK, lets play with a 2550

IIRC you just need to write a header file for it. But of course i may be quite wrong :(
 
#11
vloki
Jo, alla!
  • Total Posts : 6758
  • Reward points : 0
  • Joined: 2007/10/15 00:51:49
  • Location: Germany
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/07 07:47:31 (permalink)
0
The header is there I think.
If I select the 25K22 the SDCC toolsuite disappears.
 
BTW, the code generated by SDCC (for a 2550) is:
                                                  271:   _00117_DS_: 
                                                  272:   ;    .line    49; Quick_main_22a.c    if ((BTN_1 == BTN_ACTIVATED)&&(BTN_2 != BTN_ACTIVATED))
00A0  B481     BTFSC PORTB, 2, ACCESS             273:       BTFSC    _PORTBbits, 2
00A2  D004     BRA 0xAC                           274:       BRA    _00110_DS_
00A4  A681     BTFSS PORTB, 3, ACCESS             275:       BTFSS    _PORTBbits, 3
00A6  D002     BRA 0xAC                           276:       BRA    _00110_DS_
                                                  277:   ;    .line    51; Quick_main_22a.c    LED_4 = LED_ON;
00A8  9A8A     BCF LATB, 5, ACCESS                278:       BCF    _LATBbits, 5
00AA  D007     BRA 0xBA                           279:       BRA    _00111_DS_
                                                  280:   _00110_DS_:
                                                  281:   ;    .line    52; Quick_main_22a.c    else if((BTN_1 != BTN_ACTIVATED)||(BTN_2 == BTN_ACTIVATED))
00AC  B481     BTFSC PORTB, 2, ACCESS             282:       BTFSC    _PORTBbits, 2
00AE  D002     BRA 0xB4                           283:       BRA    _00105_DS_
00B0  B681     BTFSC PORTB, 3, ACCESS             284:       BTFSC    _PORTBbits, 3
00B2  D002     BRA 0xB8                           285:       BRA    _00106_DS_
                                                  286:   _00105_DS_:
                                                  287:   ;    .line    53; Quick_main_22a.c    LED_4 = LED_ON;
00B4  9A8A     BCF LATB, 5, ACCESS                288:       BCF    _LATBbits, 5
00B6  D001     BRA 0xBA                           289:       BRA    _00111_DS_
                                                  290:   _00106_DS_:
                                                  291:   ;    .line    55; Quick_main_22a.c    LED_4 = !LED_ON;
00B8  8A8A     BSF LATB, 5, ACCESS                292:       BSF    _LATBbits, 5
                                                  293:   _00111_DS_:
                                                  294:   ;    .line    57; Quick_main_22a.c    if(BTN_3 == BTN_ACTIVATED)
00BA  B881     BTFSC PORTB, 4, ACCESS             295:       BTFSC    _PORTBbits, 4
00BC  D002     BRA 0xC2                           296:       BRA    _00114_DS_
                                                  297:   ;    .line    58; Quick_main_22a.c    LED_2 = LED_ON;
00BE  968A     BCF LATB, 3, ACCESS                298:       BCF    _LATBbits, 3
00C0  D7EF     BRA 0xA0                           299:       BRA    _00117_DS_
                                                  300:   _00114_DS_:
                                                  301:   ;    .line    60; Quick_main_22a.c    LED_2 = !LED_ON;
00C2  868A     BSF LATB, 3, ACCESS                302:       BSF    _LATBbits, 3
00C4  D7ED     BRA 0xA0                           303:       BRA    _00117_DS_
Not to bad IMO
 
<edit>This may be helpful for SDCC in MPLAB X: MPLABX SDCC Toolchain - Rodrigo Almeida
post edited by vloki - 2012/08/07 08:08:52

Uffbasse !
#12
ytlee
Super Member
  • Total Posts : 1135
  • Reward points : 0
  • Joined: 2003/11/07 12:44:25
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/07 08:51:02 (permalink)
0
Is this code generated after the optimizer or before optimizer?
 
If optimizer is on, what level?
post edited by ytlee - 2012/08/07 08:55:11
#13
al_bin
Super Member
  • Total Posts : 154
  • Reward points : 0
  • Joined: 2011/02/11 06:28:47
  • Location: 0
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/07 14:41:09 (permalink)
0
vloki
Seems it does no support 18F25K22 yet. (Post #3 16f877a and sdcc in mplabx did not help) pink
OK, lets play with a 2550

SDCC suport 18F25K22. Only MplabX don't know about it. As for many others.
Is this by purpose ? ;-)
Hack from my post worked for me. But after changes in configurations.xml don't use project properties.
 
Albert
 
post edited by al_bin - 2012/08/07 14:47:25
#14
1and0
Access is Denied
  • Total Posts : 8392
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/07 15:23:10 (permalink)
0
vloki
Always the result is a conditional jump (BTFSx, CPFxLT) followed by three BRAs
where the 1st one ALWAYS jumps to the 3rd one  pink
... and the 2nd one ALWAYS skips the 3rd one.  I have not spent any time to investigate this further, but this is what I've concluded:
 
    if ( expression )
        program statement 1
    else
        program statement 2
 
generates this pseudo code structure 
 
        evaluate the expression     ; start of code generated by the conditional branch template
        btfsx file,bit
        goto else_condition
        goto then_case
else_condition:
       
        goto else_case              ; start of code generated by another compiler template
then_case:
        program statement 1
        goto end_if
else_case:
        program statement 2
        goto end_if
end_if:
 
The first two GOTOs are put down by the conditional branch template of the compiler.  Assuming the first instruction put down by the other template is *always* a GOTO located in consecutive memory, it seems the template that handles the conditional branch does *not* need to put down any GOTOs at all:
 
        evaluate the expression     ; start of code generated by the conditional branch template
        btfsx file,bit
else_condition:
       
        goto else_case              ; start of code generated by another template
then_case:
        program statement 1
        goto end_if
else_case:
        program statement 2
        goto end_if
end_if:
 
Anyway, further study of how nested if() and nested while() fit into this "skeleton code" might help in understanding this generic way of encoding branches with three GOTOs.
Another free feature seems to be that the compiler takes extremely care of the working register LoL
It is saved always:
I have noticed that too.
 
#15
vloki
Jo, alla!
  • Total Posts : 6758
  • Reward points : 0
  • Joined: 2007/10/15 00:51:49
  • Location: Germany
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/08 01:07:00 (permalink)
0
It's interesting how compilers work, but for now I need one that works. (in acceptable manner)
 
So I put XC8 aside and see how it goes with SDCC [:-]
First step is to find out why MPLAB X only shows a few supported PICs.
 

Another frustrated XC compiler user: Please fix how using compiler optimization works with free XC compilers and MPLABX

Uffbasse !
#16
al_bin
Super Member
  • Total Posts : 154
  • Reward points : 0
  • Joined: 2011/02/11 06:28:47
  • Location: 0
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/08 01:51:52 (permalink)
0
vloki
So I put XC8 aside and see how it goes with SDCC [:-]

 
Note that I recomended it only for academic use.
Reasons for it and some usefull informations:
http://embedded.ifmo.ru/s...oft/sdcc/Dutta-121.pdf
 
Albert
#17
Antipodean
Super Member
  • Total Posts : 1644
  • Reward points : 0
  • Joined: 2008/12/09 10:19:08
  • Location: Didcot, United Kingdom
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/08 04:02:06 (permalink)
0
vloki
First step is to find out why MPLAB X only shows a few supported PICs.


You are using th latest version, i.e. 1.30 ?
 
 

Do not use my alias in your message body when replying, your message will disappear ...

Alan
#18
vloki
Jo, alla!
  • Total Posts : 6758
  • Reward points : 0
  • Joined: 2007/10/15 00:51:49
  • Location: Germany
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/08 04:03:22 (permalink)
0
Yes, MPLAB X 1.30 / SDCC 3.2.0

Uffbasse !
#19
drazen
Super Member
  • Total Posts : 2847
  • Reward points : 0
  • Joined: 2003/11/07 12:43:09
  • Location: Croatia
  • Status: offline
Re:How to explain XC8 / Hi-Tech generated code to our students ??? 2012/08/08 04:50:22 (permalink)
0
Years ago while I was working with HiTech I was amazed how good code was produced by free version. I think change happened when HiTech introduced Omniscient optimizations. Maybe I'm wrong but I think this (bad code) is made by purpose to force users to buy, now, very expensive compilers.
#20
Page: 1234 > Showing page 1 of 4
Jump to:
© 2018 APG vNext Commercial Version 4.5