What is the define __XC__ ?

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
gerrykurz
XCore Addict
Posts: 204
Joined: Sun Jun 01, 2014 10:25 pm

What is the define __XC__ ?

Post by gerrykurz »

The ethernet code is full of conditional assembly statements ifdef __XC__ but nowhere can I find where this is defined or what is means. What is this define used for and where is it defined?


User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

It is defined by the compiler if you are compiling XC code
(as opposed to, say, plain C code).
User avatar
gerrykurz
XCore Addict
Posts: 204
Joined: Sun Jun 01, 2014 10:25 pm

Post by gerrykurz »

Ok, so if you can bear with me for this as I am mainly a hardware guy and not a c guy then in a header file is this code:

/**
* otp_ports_t structure - contains ports used to access the OTP memory.
*/
typedef struct otp_ports_t {
port data;
#ifdef __XC__
out port addr;
out port ctrl;
#else
port addr;
port ctrl;
#endif
} otp_ports_t;

so I understand from your explanation that if the header file is included in an xc source file it will compile differently than in c source file

my question is...why bother....why not just do this

/**
* otp_ports_t structure - contains ports used to access the OTP memory.
*/
typedef struct otp_ports_t {
port data;
port addr;
port ctrl;
} otp_ports_t;
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

Because

Code: Select all

port addr;
and

Code: Select all

out port addr;
are not the same thing?
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

'port' In C is just an unsigned integer, the C compiler doesn't make any assumption beyond this. XC however can reason about the special port type and check that the port isn't being used in incorrect ways. Ports in XC are special resources that the compiler has some understanding of along with their modifiers such as in/out and buffering which allow the compiler to add specific config code etc..

regards
Al
User avatar
gerrykurz
XCore Addict
Posts: 204
Joined: Sun Jun 01, 2014 10:25 pm

Post by gerrykurz »

Ok, I get the distinction but from the overall program structure point of view, the program is written to run on XS1 parts that have physical ports so what is the point of some C code defining "ports" that are just integers?

Why not just assume that the header file will be used in an XC source file?

What is the point of all this?

Also the "data" port is defined as simply a port in XC presumably because it is bidirectional so if I didn't care about the compiler checking on my usage of ports, again why not just forget the use of in/out modifiers and just define everything as port and not worry about C or XC compiler differences.

Just trying to understand why some programmer has done this bit of code.....
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

A resource identifier in C is just an integer, exactly like on
the hardware. C is much closer to the generated machine
code than XC is.

The author of the header file made it work with both C and XC
presumably because it actually _is_ used with both.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

Originally XC lacked some of C's features, pointers for example. Only recently has XC become more complete in this respect. Thus C has often been used in conjunction with XC to perform certain optimised code usage, C libraries can also be used if C is supported allowing the use of legacy libraries.

As for why bother creating a port type in C I guess it makes it more readable and standardises usage/manipulation in the native C support API.


P.S. it's worth poking around the headers in:

Code: Select all

XMOS/xTIMEcomposer/Community_13.1.0/target/include/
And in particular xccompat.h in this case

This helped me in the past

regards
Al