Macro usage with variables names

Hi,

I am trying to debug a software by printing out the values of variables.
However, I am having syntax error issue. Any advice ?


error: ‘class Vcordic_pipelined’ has no member named ‘cordic_pipelined__DOT__stage_generate__BRA__i__KET____DOT____Vcellinp__stage_i__x_i’; did you mean ‘cordic_pipelined__DOT__stage_generate__BRA__0__KET____DOT____Vcellinp__stage_i__x_i’?


1
2
3
4
5
6
7
8
9
10
11
#define X(i) uut->cordic_pipelined__DOT__stage_generate__BRA__##i__KET____DOT____Vcellinp__stage_i__x_i

Vcordic_pipelined *uut;                     // Instantiation of module

void cout_debug_msg(void){
    static int i;
    
    for(i=0; i<10; i++){	
	cout << "x[" << i << "] = " << X(i) << endl;
    }
}


Note:
The variables names are from existing software and I could not modify the variables names.
I am trying to print out the values of 10 variables below

uut->cordic_pipelined__DOT__stage_generate__BRA__0__KET____DOT____Vcellinp__stage_i__x_i
uut->cordic_pipelined__DOT__stage_generate__BRA__1__KET____DOT____Vcellinp__stage_i__x_i
uut->cordic_pipelined__DOT__stage_generate__BRA__2__KET____DOT____Vcellinp__stage_i__x_i
uut->cordic_pipelined__DOT__stage_generate__BRA__3__KET____DOT____Vcellinp__stage_i__x_i
uut->cordic_pipelined__DOT__stage_generate__BRA__4__KET____DOT____Vcellinp__stage_i__x_i
uut->cordic_pipelined__DOT__stage_generate__BRA__5__KET____DOT____Vcellinp__stage_i__x_i
uut->cordic_pipelined__DOT__stage_generate__BRA__6__KET____DOT____Vcellinp__stage_i__x_i
uut->cordic_pipelined__DOT__stage_generate__BRA__7__KET____DOT____Vcellinp__stage_i__x_i
uut->cordic_pipelined__DOT__stage_generate__BRA__8__KET____DOT____Vcellinp__stage_i__x_i
uut->cordic_pipelined__DOT__stage_generate__BRA__9__KET____DOT____Vcellinp__stage_i__x_i
Last edited on
Line 1: The ## is a token pasting operator. It pastes the token "i", not the value of i in the loop. You can see this is in the compiler error message:
1
2
cordic_pipelined__DOT__stage_generate__BRA__i__KET____DOT____Vcellinp__stage_i__x_i’;
                                            ^


The following, while not pretty, should work:
1
2
3
4
5
6
7
8
9
10
#define X(i) uut->cordic_pipelined__DOT__stage_generate__BRA__##i__KET____DOT____Vcellinp__stage_i__x_i
#define XOUT(i) cout << "x[" << i << "] = " << X(i) << endl

Vcordic_pipelined *uut;                     // Instantiation of module

void cout_debug_msg(void)
{   XOUT(0);
...
    XOUT(9);
}

Last edited on
@OP,

Remember that macros are replaced int the preprocessor, before things are compiled. The preprocessor has no idea that the "i" in X(i) takes on the values 0, 1, 2, etc. When the loop is unrolled (like @AbstractAnon did for you), each of the values your are interested in can be substituted into your macro. Without unrolling the loop, that is not possible.
@AbstractionAnon :

I am still having syntax error even after using your loop unrolling advice.

What is wrong ?

../cordic_pipelined.cpp: In function ‘void cout_debug_msg()’:
../cordic_pipelined.cpp:17:19: error: ‘class Vcordic_pipelined’ has no member named ‘cordic_pipelined__DOT__stage_generate__BRA__i__KET____DOT____Vcellinp__stage_i__x_i’; did you mean ‘cordic_pipelined__DOT__stage_generate__BRA__0__KET____DOT____Vcellinp__stage_i__x_i’?
#define X(i) uut->cordic_pipelined__DOT__stage_generate__BRA__##i__KET____DOT____Vcellinp__stage_i__x_i
^
../cordic_pipelined.cpp:17:19: note: in definition of macro ‘X’
#define X(i) uut->cordic_pipelined__DOT__stage_generate__BRA__##i__KET____DOT____Vcellinp__stage_i__x_i
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../cordic_pipelined.cpp:40:5: note: in expansion of macro ‘XOUT’
XOUT(0); XOUT(1); XOUT(2); XOUT(3); XOUT(4); XOUT(5); XOUT(6); XOUT(7); XOUT(8); XOUT(9);
^~~~
Last edited on
Try token pasting i the in XOUT macro:
 
#define XOUT(i) cout << "x[" << i << "] = " << X(##i) << endl 

@AbstractionAnon :

Still the same error.
I don't have the definition of Vcordic_pipelined, but the following compiles for me.
The key was to put ## before and after i in the X macro.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

#define X(i) uut.var##i##stage 
#define XOUT(i) cout << "x[" << i << "] = " << X(i) << endl

struct
{   int var0stage;
    int var1stage;
    int var2stage;
} uut;
    
void cout_debug_msg(void)
{   XOUT(0);
    XOUT(1);
    XOUT(2);
}
Last edited on
Topic archived. No new replies allowed.