1

i want to see output of Preprocessor calculating. Only with strings it works so:

#define XSTR(x) STR(x)
#define STR(x) #x

#define BEGIN "test"

#pragma message ".text= " XSTR(BEGIN)

when i set BEGIN to 32/2 the output is: #pragma message: .text= 32/2.

What can i make to solve this? I don´t won´t solutions like this to search in the .lss file:

uint16_t x = BEGIN;
PORTB = x>>8;
PORTA = x;

Thank you.

9
  • What do you expect the output to be when you define BEGIN to 32/2? Commented Nov 2, 2021 at 13:01
  • And what's an ".lss file"? Commented Nov 2, 2021 at 13:02
  • Not possible... Commented Nov 2, 2021 at 13:06
  • 3
    The C preprocessor will only calculate things inside #if conditions. You won't get it to calculate and print something (let's ignore abominations like BOOST_PP). See also: stackoverflow.com/questions/1560357/… Commented Nov 2, 2021 at 13:08
  • @JohnBollinger an .lss file is the Assembler Output file generated from AVR-GCC. The Value 32/2 is only an example. Later this calculate the .text section. Commented Nov 2, 2021 at 13:12

2 Answers 2

2

What can i make to solve this?

You have to implement math operations in the preprocessor. For example:

#define _div_1_over_1  1
#define _div_2_over_1  2
#define _div_3_over_1  3
// etc...
#define _div_30_over_2  15
#define _div_31_over_2  15
#define _div_32_over_2  16
// etc...
// etc. for every possible a_over_b combinations, many many 1000 lines
#define DIV(a, b)  _div_##a##_over_##b

After that, you finally can:

#define XSTR(x) STR(x)
#define STR(x) #x

#define BEGIN DIV(32, 2)

#pragma message ".text= " XSTR(BEGIN)

Let's say this is one way of implementing division in preprocessor. For other solutions, see BOOST_PP_DIV and P99_DIV from p99 library.

Sign up to request clarification or add additional context in comments.

Comments

1

The C preprocessor just performs text replacement(s).

The first opportunity to see folded constants is in the tree dumps with -fdump-tree-all and then inspect the *.t<nnn> dumps.

To get the asm output from the compiler proper, try -safe-temps -fverbose-asm which inserts C source intermingled as asm comments in the *.s file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.