Close

Exploring Microchip XC16 compiler

A project log for MCU how-tos, reviews, rants

As title says, my random essays about microcontrollers, all in one package

jaromir.sukubajaromir.sukuba 01/09/2018 at 10:447 Comments
As I was playing with my LLG project, I spent a few moments with exploring XC16 compiler.

Three facts are known about this one

Honestly, I'm OK with all three points for hobby projects, though I try to use 8- or 32-bitters, where open source compilers are available. Less known fact is that source codes of XC16 are available and free to download, probably mostly to satisfy GNU license requirements. Better than -O1 compiler options are fine for squeezing last bits of optimization efforts, though - that's why the paid version exists. Though the sources are available, in professional circles not everybody will spent their expensive time building the compiler (that is far from being trivial exercise) with nobody to ask questions, so they buy directly the full version plus support from Microchip...

...or something. In fact, I'm able to use optimizations higher than -O1 on free version. Compiler complains I have no valid license, but the code builds and runs just fine, with apparent results of compiler optimization efforts. That's what i did on LLG, where I built the code with -O3 and code execution is indeed faster than with -O1. That is where story could end, but I went further.

I downloaded soruces for XC16 1.33 from here. The archive contains almost 10.000 files in 5200 directories, so I unziped it on temporary location. In directory \v1.33.src\src\XC_GCC\gcc\gcc\config are all targets, including the ones for XC16 /PIC30/ - because originally the compiler was meant for dsPIC30 DSPs, PIC24 and dsPIC33 being derivatives of dsPIC30) - as well as PIC32 /PIC32/.

in PIC30 directory there are files pic30.opt and pic30.c being of interest. At line 3707 of file pic30.c, there is block of code

#elif defined(LICENSE_MANAGER_XCLM)
  if (pic30_license_valid < MCHP_XCLM_VALID_PRO_LICENSE) {
    invalid = (char*) "restricted";
    if (pic30_license_valid < MCHP_XCLM_VALID_STANDARD_LICENSE) {
      nullify_O2 = 1;
    }
    nullify_Os = 1;
    nullify_O3 = 1;
  }
#endif

    #define NULLIFY(X) \
    if ((X) && (message_displayed++ == 0)) \
      fnotice(stderr,"Options have been disabled due to %s license\n" \
              "Visit http://www.microchip.com/ to purchase a new key.\n", \
              invalid); \ 

The variable 'pic30_license_valid' is being set on results of xclm, license checker. So this is where optimizations warning are being emitted. Never mind, lets look further.

#ifdef LICENSE_MANAGER_XCLM
  if (mchp_mafrlcsj) {
    pic30_license_valid = MCHP_XCLM_VALID_PRO_LICENSE;
  } else if (mchp_skip_license_check) {
    pic30_license_valid = -1;
  } else {
    pic30_license_valid = get_license();
  }

 By setting proper value into 'mchp_mafrlcsj' we can omit the license check. The option is entered via command line entry, being described in pic30.opt file, line 228:

mafrlcsj
Target RejectNegative Var(mchp_mafrlcsj) Undocumented

So, entering -mafrlcsj option into command line should be equal to having proper license. When compiling from command line using make or similar tool, it should be straightforward, within MPLABX IDE it works like this:

I created file cmd.txt containing single line

*cc1:+ -mafrlcsj

and in project settings I opted to use this file

and hit compile

Notice the resulting binary is indeed a bit smaller, though at -O1 optimization (as if the optimization beyond -01 would be prohibited) is the binary even bigger - not sure about this one.

I took my LLG sources and performed tests on them (code size and execution time of geolocation algorithm), using all levels of optimizations with and without additional options as desribed here.

It's apparent that with options the compiler tries a bit harder. At Os (where code size is main factor) it gets 20B lower, at O3 (where speed is at premium, code size is secondary) it indeed runs a bit faster.

So, what is described here is option to get full optimization level of XC16 compiler without need to recompile the sources, what is far from being simple task. On the other hand, the gains from full options are not particularly huge, but it doesn't hurt anyway.

I don't think there is anything illegal involved here. The binaries of compiler nor license checker aren't modified in any way, I'm just using command line option that is free version of XC16 distributed with.

All in all, even with 16-bitters from Microchip, one isn't completely lost in corporate greed. Though they are trying to earn as much money as they can, it's still possible to get "canned solution" that you just use with no further fiddling as well as compiler with sources available - to learn and improve.

Discussions

ziggurat29 wrote 06/09/2018 at 18:24 point

(sigh) if you give away all the secrets, you only expedite the arms race; lol! -- aren't things nicer as they are presently, with things being so easy?
(and yes, I consider it an obscenity to charge for the build tools' optimization capability -- especially for one they didn't even write, and especially for a chip company.  Does Microchip want me to spec PICs in my design, or STM32s?)

  Are you sure? yes | no

jaromir.sukuba wrote 06/14/2018 at 13:22 point

Well, I've got full optimizations and I haven't paid a single cent to them, I'm not much into obscenities. What they are charging for is support. I don't need that thing, though.

By the way, the same thing works for PIC32, or if anybody is interested - http://retrobsd.org/wiki/doku.php/doc/toolchain-mips or http://chipkit.net/wpcproduct/mpide/

  Are you sure? yes | no

ziggurat29 wrote 06/14/2018 at 16:21 point

Thanks for showing how to remove the restraining bolt!

  Are you sure? yes | no

Ted Yapo wrote 01/09/2018 at 13:03 point

Very nice!

Too bad XC8 is not based on gcc...$1000 is a lot of money to drop on a compiler :-)

  Are you sure? yes | no

jaromir.sukuba wrote 01/09/2018 at 13:57 point

Well, there exists SDCC (+gputils) which I use on some of my projects. It works just fine for both PIC16 and PIC18 targets, though XC8 is better in some aspects - it has better stdio support, better code density. On the other hand SDCC is getting better with time, newer daily builds produce something like 5-10% less code than stable 3.6 version. 3.7 stable is to be released soon.

It's also great compiler FOR HC(S)08, MCS51 and STM8; for STM8 it has better results in some benchmarks than commercial compilers - see https://webcache.googleusercontent.com/search?q=cache:phXBwcIq4QsJ:https://community.st.com/thread/8847

  Are you sure? yes | no

Ted Yapo wrote 01/09/2018 at 14:01 point

Commercially, there are offerings from CCS, too.  I haven't tried them, but the $150 price tag for an optimizing version is better than $1k.  Unfortunately, they have different versions for 12-bit, 14-bit instructions, etc.  But, really, this stuff should be free.

I will give SDCC a try.

  Are you sure? yes | no

jaromir.sukuba wrote 01/09/2018 at 14:09 point

@Ted Yapo if you feel adventurous you can try Pascal :-) https://github.com/t-edson/PicPas

Apart from XC8 and CCS, there is also commercial offering from Mikroelektronika https://www.mikroe.com/mikroc-pic They have quite wide portfolio of products, but I have no practical experience. Somehow mysterious is http://www.fored.co.uk/html/wiz_c_mx_2_-rad_for_pic.HTM

  Are you sure? yes | no