mplab xc8/16 builtin_constant_p - c

I was searching for this in the mplab compiler users guide but haven't found anything. I am asking it here to confirm that I am not blind or anything:
The GCC compiler provides some very interesting and useful built-in functions like __builtin_constant_p(x) or similar stuff. I have never found anything like that in the microchip compilers and I don't think there is.
So the question: Do Microchip XCxx Compilers provide any non-standard built-in functions apart from the device specific ones (like declaring variables at a given register address or declaring an interrupt function)?
EDIT: To clarify some more: I am mostly interested in retrieving information from the compiler. A good example would be something like builtin_constant, as it makes information available to the program which is normally not usable. But I do not limit this question to find constant expressions only.

XC16 manual in google and out rolls: http://ww1.microchip.com/downloads/en/DeviceDoc/50002071E.pdf appendix G.

The same document mentioned by #Marco van de Voort has a list of pre-defined macros in section 19.4 that give you information about the compiler environment and the device.
There is also the somewhat undocumented __DEBUG macro which is defined when running under MPLABX in debug mode (MPLABX defines this in the call to the compiler).

These are the builtins supported by the XC16 compiler
e.g. __builtin_add
For a complete description of the builtins see the MPLAB XC16 compiler user's manual (under "docs" folder of compiler installation) or here: http://www.microchip.com/mymicrochip/filehandler.aspx?ddocname=en559023

Related

How to use GNU atomic memory access functions in keil

I want to use GCC built-in functions like __sync_fetch_and_add, but I compile my code with Keil, if I use these functions in my code, it will be show err like this:
Error: L6218E: Undefined symbol __sync_fetch_and_add_4 (referred from XXXX.o).
I found there are some describes with GNU atomic memory access functions in Keil's documents, so I guess that keil may be support these functions, but I don't know how to use them. Should I include some header files or add some config in keil?
I'm no expert, but the link seems to be about ARM DS-5, which is a separate compiler i.e. not the same as Keil's MDK. So the documentation doesn't apply.
Implementing those functions is not super hard; if all else fails I would look at the compiler output from GCC, and just re-implement it.
Alternatively read up on the LDREX/STREX instructions and those for memory barriers, and have fun! :)
UPDATE: I think __sync_fetch_and_add_4() is new, but Keil only supports GCC's older suite of built-ins. Notice that __sync_fetch_and_add_4 does not appear in the list of functions they say that they support. This GCC manual page says:
Prior to GCC 4.7 the older __sync intrinsics were used. An example of an undefined symbol from the use of __sync_fetch_and_add on an unsupported host is a missing reference to __sync_fetch_and_add_4.
So it seems Keil is tracking a pretty old version of GCC? On the other hand, I do see __sync_fetch_and_add() in the list, and I guess that "magically" generates a call to __sync_fetch_and_add_4() if used on a 32-bit quantity. Weird.

What does the __attribute__((force)) do?

This sounds like something I should be able to Google, but I can't find a good reference. What exactly does __attribute__((force)) do? As in:
return (__attribute__((force)) uint32_t) *p
(this is for an ARM system, cross compiled with clang, but I can't find any reference anywhere on this attribute even in clang/arm specific pages..).
__attribute__((force)) is used to suppress warnings in sparse, the c semantics checker of the linux kernel.
The wikipedia article of sparse lists the following attributes as defined by sparse:
address_space(num)
bitwise
force
context(expression,in_context,out_context)
If you need more information about these, you can take a look at the man page of sparse
In the linux kernel some these attributes are redefined in linux/include/linux/compiler_types.h. For example __force expands to __attribute__((force)) or __bitwise to __attribute__((bitwise)).
However the linux documentation about sparse tells us, that gcc ignores these attributes:
And with gcc, all the “__bitwise”/”__force stuff” goes away, and it all ends up looking just like integers to gcc.

Declaring a class with microchip xc8 compiler

I was trying to declare a class in a library for a projects using xc8 compiler in free mode and the pic16f876a. But it seems that xc8 compiler doesn't accept or
compiles classes...
Is this true? If not, can somebody post an example?
Thanks!!
There are no classes in the paid compiler, either. XC8 is an ANSI C compiler, not a C++ compiler.
You get structs in C, but no member functions ("methods"), constructors, or destructors, and all member variables are "public".
To answer the question in comment, the compiler appears to be ANSI, with the sole documented exception (a huge one):
"Due to limited memory and no hardware
implementation of a data stack, recursion is not supported and functions are not reentrant." -- MPLAB XC8 C Compiler User's Guide [5.2.1]
I found the guide here.
I can't see any comments in their samples, but I assume that ANSI means no // comments, no variable length arrays, no extended integer and float types. I also don't see the use of much of on a PIC, so "ANSI C" probably refers only to the language and not the standard library.

Clang or GCC equivalent of _PGOPTI_Prof_Dump_All() from ICC

Intel C(++) Compiler has very useful functions to help with profile guided optimisation.
_PGOPTI_Prof_Reset_All();
/* code */
_PGOPTI_Prof_Dump_All();
https://software.intel.com/en-us/node/512800
This is particularly useful for profiling shared libraries which one would use with ctypes in Python.
I've been trying to figure out if either Clang or GCC have similar functionality – apparently not.
Profile guided optimization works differently in gcc and it is enabled with compiler switches. See this question for PGO with gcc.
PGO just recently arrived in clang and is only available starting at version 3.5. The clang user manual gives an overview of how to use it.
It turns out that both have an internal and not properly documented function named __gcov_flush which does this. It is only explained in the source.
/* Called before fork or exec - write out profile information
gathered so far and reset it to zero. This avoids duplication or
loss of the profile information gathered so far. */
It's not quite as convenient as the Intel equivalent though and requires some gymnastics to make it work.

How to write pragmas in C

I know that #pragmas are compiler directives which are used to provide additional information to the compiler. My question is that, I need to write some #pragmas for my project. i.e, I need to invoke some particular code when there are some particular pattern in code. Can some one throw light on this ...?
Thanks in advance..!
You can't write your own #pragmas. You must look into your compiler's handbook for which #pragmas are supported.
Alternatively, if your compiler allows you to modify its source code (license- and sourcecode-wise), you might hack some new ones in. Don't expect it to be a trivial task, there's usually no enduser-friendly plug-in write-your-own-pragmas system.
#pragma is a way for compiler vendors to legally implement proprietary extensions. They are hard-coded into the compiler. (And IIRC compilers are required to ignore unknown pragmas.)
Unless you write your own compiler, you cannot create your own pragmas.

Resources