Go Down

Topic: How Arduino IDE pre-processing work? (Read 642 times) previous topic - next topic

leandrogs

Hi! I was reading some topics and I found this:

Quote
The main sketch is C++, however the IDE does do a certain amount of pre-processing (in particular, generating function prototypes for you) before submitting the code to the main C++ compiler.


Could be found here.

Someone know how this "pre-processing" works? The code is converted to some low level programming language before load the microcontroller with my program? How?

AWOL

Quote
The code is converted to some low level programming language before load the microcontroller with my program?

No, the code is kept as (moderately) high-level C++, but function prototypes are generated, before compilation using avr-gcc.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

holmes4

And a few other things are doe for you such as adding the correct main() ans the standard #include's

Mark

econjack

The preprocessor is a "textual" pass through the source code. For example, you've seen something like:

Code: [Select]

#define MAXARRAYSIZE  100


If you have an expression in your code that reads:

Code: [Select]

for (i = 0; i < MAXARRAYSIZE; i++) {


when the preprocessor finishes, it's as though you wrote:

Code: [Select]

for (i = 0; i < 100; i++) {


The advantage, of course, is that you only need to change the #define to everywhere change the affected value in the source code. The preprocessor also reads function prototypes, like:

Code: [Select]
int myFunction(int val, char *buff);

and constructs an attribute list much like a symbol table that allows it to check all subsequent uses of that function for the correct argument list (i.e., an int and a pointer) and that the return value is used as an int. If also expands any macros you may have written, like:

Code: [Select]

#define ARRAYSIZE(x)   (sizeof(x) / sizeof(x[0]))


so when you write:

Code: [Select]

for (i = 0; i <  ARRAYSIZE(buff) ; i++) {


the preprocessor expands it to:

Code: [Select]

for (i = 0; i <   (sizeof(buff) / sizeof(buff[0])) ; i++) {


I think it helps to simply think of the preprocessor as performing textual replacements, macro expansions, and type checking via prototypes.

PaulS

Quote
The preprocessor is a "textual" pass through the source code.

Which has nothing to do with what the IDE does in terms of processing before it invokes the compiler.

econjack

PaulS: I thought OP understood that the IDE and compiler are separate entities. Rereading his post, perhaps not.

PeterH


PaulS: I thought OP understood that the IDE and compiler are separate entities. Rereading his post, perhaps not.


From the original post I got the impression that the OP understood the distinction correctly - the issue is the misguided and poorly implemented mucking about that the IDE does to your code before invoking the compiler, and nothing to do with the C++ compiler / preprocessor.
I only provide help via the forum - please do not contact me for private consultancy.

leandrogs

Thank you all for answer me about preprocessor and sorry about my late response.

So, now that i understand what preprocessor is, what is running inside the ATMEL?

KeithRB

The hex file which gets loaded on the processor contains Atmel machine code. You can read the datasheet for the opcodes.

leandrogs


The hex file which gets loaded on the processor contains Atmel machine code. You can read the datasheet for the opcodes.


Is there any way to take a look inside the drivers who creates the Atmel machine code?

AWOL

Quote
Is there any way to take a look inside the drivers who creates the Atmel machine code?

The compiler, avr-gcc,  is supplied as binary executable, but the sources will be somewhere on the Web.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

leandrogs


Quote
Is there any way to take a look inside the drivers who creates the Atmel machine code?

The compiler, avr-gcc,  is supplied as binary executable, but the sources will be somewhere on the Web.


Thank you. I'll take a look at both datasheet and avr-gcc this week.

Go Up