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.

2 Likes

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 {
public:
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.