Changing IR by Visitor

Hi all,

I am new to P4. I want to change IR in the front-end e.g. changing node number. I made a new pass (class) in frontend.cpp by inheriting Visitor to change IR by this pass. Also, I found this code in compiler design slides for changing Visitor but I am not sure about the way of using it for changing IR. I would be very grateful if you can give me some hints. Thank you in advance.

const IR::Node* node;
IR::Visitor v1, v2;
const IR::Node* intermediate = node->apply(v1);
const IR::Node* final = intermediate->apply(v2);

The node numbers are generated internally by the constructors are are used only for debugging.
I don’t see what benefit you have in changing them.
The frontend.cpp is a sequence of passes that are applied to the program. Each pass consumes a program and produces another program. If you insert your pass in that sequence it will be executed when its turn comes. You don’t need to call “apply” - the frontend does that for all passes in the sequence.


Thank you so much. I mentioned changing the node number as a simple example for starting or my main goal is changing node type and adding the new nodes (adding some p4 codes by changing IR). I made this code in frontend.cpp and have this error. I would be very grateful if you can give me some hints (I have read most of the codes in ir directory and tried to define v1 in node.h)

class newpass : public Visitor {
const IR::Node* node;
IR::Visitor v1;
const IR::Node* intermediate = node->v1;

/home/serpico/Desktop/p4c/frontends/p4/frontend.cpp:116:9: error: ‘Visitor’ in namespace ‘IR’ does not name a type
116 | IR::Visitor v1;
| ^~~
/home/serpico/Desktop/p4c/frontends/p4/frontend.cpp:117:42: error: ‘const class IR::Node’ has no member named ‘v1’
117 | const IR::Node* intermediate = node->v1;

That is not legal c++.
Do not inherit Visitor, but one of Inspector or Transform.

1 Like

Thank you so much for your kind help. I can change IR with your hint.