129#define CMP_SIGNED 0x08
144 friend bool operator<(
const WidthRec &l,
const WidthRec &r) {
145 if (l.type != r.type)
return (l.type < r.type);
148 return (l.u.value < r.u.value);
152 (l.u.concat.lhsw < r.u.concat.lhsw) ||
153 ((l.u.concat.lhsw == r.u.concat.lhsw) && (l.u.concat.rhsw < r.u.concat.rhsw)));
163 return (l.u.arith.w < r.u.arith.w);
166 return ((l.u.bxsmul.bw < r.u.bxsmul.bw) ||
167 ((l.u.bxsmul.bw == r.u.bxsmul.bw) && (l.u.bxsmul.sv < r.u.bxsmul.sv)));
170 return ((l.u.cast.fw < r.u.cast.fw) ||
171 ((l.u.cast.fw == r.u.cast.fw) && (l.u.cast.tw < r.u.cast.tw)));
176 return ((l.u.shift_x.lw < r.u.shift_x.lw) || ((l.u.shift_x.lw == r.u.shift_x.lw) &&
177 ((l.u.shift_x.rw < r.u.shift_x.rw))));
182 return ((l.u.shift_c.lw < r.u.shift_c.lw) || ((l.u.shift_c.lw == r.u.shift_c.lw) &&
183 ((l.u.shift_c.sv < r.u.shift_c.sv))));
186 return ((l.u.cmp.w < r.u.cmp.w) ||
187 ((l.u.cmp.w == r.u.cmp.w) && (l.u.cmp.cmp < r.u.cmp.cmp)));
191 return ((l.u.sarith.w < r.u.sarith.w) ||
192 ((l.u.sarith.w == r.u.sarith.w) &&
193 (!l.u.sarith.issigned && r.u.sarith.issigned)));
196 return (l.u.assign.w < r.u.assign.w);
201 friend bool operator==(
const WidthRec &l,
const WidthRec &r) {
202 if (l.type != r.type)
return (0);
205 return (l.u.value == r.u.value);
208 return ((l.u.concat.lhsw == r.u.concat.lhsw) &&
209 (l.u.concat.rhsw) == (r.u.concat.rhsw));
219 return (l.u.arith.w == r.u.arith.w);
222 return ((l.u.bxsmul.bw == r.u.bxsmul.bw) && (l.u.bxsmul.sv == r.u.bxsmul.sv));
225 return ((l.u.cast.fw == r.u.cast.fw) && (l.u.cast.tw == r.u.cast.tw));
230 return ((l.u.shift_x.lw == r.u.shift_x.lw) && (l.u.shift_x.rw == r.u.shift_x.rw));
235 return ((l.u.shift_c.lw == r.u.shift_c.lw) && (l.u.shift_c.sv == r.u.shift_c.sv));
238 return ((l.u.cmp.w == r.u.cmp.w) && (l.u.cmp.cmp == r.u.cmp.cmp));
242 return ((l.u.sarith.w == r.u.sarith.w) &&
243 (l.u.sarith.issigned == r.u.sarith.issigned));
246 return (l.u.assign.w == r.u.assign.w);
251 friend bool operator>(
const WidthRec &l,
const WidthRec &r) {
return (r < l); }
252 friend bool operator>=(
const WidthRec &l,
const WidthRec &r) {
return (!(l < r)); }
253 friend bool operator<=(
const WidthRec &l,
const WidthRec &r) {
return (!(r < l)); }
254 friend bool operator!=(
const WidthRec &l,
const WidthRec &r) {
return (!(l == r)); }
257class ScanWidths :
public Inspector {
266 void add_concat(
int,
int);
267 void add_arith(WrType,
int);
268 void add_sarith(WrType,
int,
bool);
269 void add_bxsmul(
int,
unsigned int);
270 void add_cast(
unsigned int,
unsigned int);
271 bool bigXSmallMul(
const IR::Expression *,
const IR::Constant *);
272 void add_shift_c(WrType,
unsigned int,
unsigned int);
273 void add_shift_x(WrType,
unsigned int,
unsigned int);
274 void add_cmp(
unsigned int,
unsigned char);
275 void add_assign(
unsigned int);
279 : nwr(0), wrv(0), typemap(tm), refmap(rm), target(tgt) {}
280 ~ScanWidths(
void) { std::free(wrv); }
281 void expr_common(
const IR::Expression *);
282 bool preorder(
const IR::Expression *)
override;
283 bool preorder(
const IR::Concat *)
override;
284 bool preorder(
const IR::Add *)
override;
285 bool preorder(
const IR::Sub *)
override;
286 bool preorder(
const IR::Mul *)
override;
287 bool preorder(
const IR::Cast *)
override;
288 bool preorder(
const IR::Neg *)
override;
289 bool preorder(
const IR::Cmpl *)
override;
290 bool preorder(
const IR::Shl *)
override;
291 bool preorder(
const IR::Shr *)
override;
292 bool preorder(
const IR::Equ *)
override;
293 bool preorder(
const IR::Neq *)
override;
294 bool preorder(
const IR::Lss *)
override;
295 bool preorder(
const IR::Leq *)
override;
296 bool preorder(
const IR::Grt *)
override;
297 bool preorder(
const IR::Geq *)
override;
298 bool preorder(
const IR::BAnd *)
override;
299 bool preorder(
const IR::BOr *)
override;
300 bool preorder(
const IR::BXor *)
override;
301 bool preorder(
const IR::AddSat *)
override;
302 bool preorder(
const IR::SubSat *)
override;
303 bool preorder(
const IR::AssignmentStatement *)
override;
304 bool preorder(
const IR::MethodCallExpression *)
override;
305 bool arith_common_2(
const IR::Operation_Binary *, WrType,
bool =
true);
306 bool arith_common_1(
const IR::Operation_Unary *, WrType,
bool =
true);
307 bool sarith_common_2(
const IR::Operation_Binary *, WrType,
bool =
true);
309 void end_apply(
const IR::Node *)
override;
310 void revisit_visited();
319class ConvertToBackendIR :
public Inspector {
323 unsigned instance_id;
324 bool is_num_elements;
330 unsigned no_of_instances;
334 enum CounterType { PACKETS, BYTES, PACKETS_AND_BYTES };
335 const IR::ToplevelBlock *tlb;
336 IR::TCPipeline *tcPipeline;
340 unsigned int tableCount = 0;
341 unsigned int actionCount = 0;
342 unsigned int metadataCount = 0;
343 unsigned int labelCount = 0;
344 unsigned int externCount = 0;
345 cstring pipelineName =
nullptr;
346 cstring mainParserName =
nullptr;
359 : tlb(tlb), tcPipeline(pipe), refMap(refMap), typeMap(typeMap), options(options) {}
360 void setPipelineName();
361 cstring getPipelineName() {
return pipelineName; };
362 bool preorder(
const IR::P4Program *p)
override;
363 void postorder(
const IR::P4Action *a)
override;
364 void postorder(
const IR::P4Table *t)
override;
365 void postorder(
const IR::P4Program *p)
override;
366 void postorder(
const IR::Declaration_Instance *d)
override;
367 void postorder(
const IR::Type_Struct *ts)
override;
368 safe_vector<const IR::TCKey *> processExternConstructor(
const IR::Type_Extern *extn,
369 const IR::Declaration_Instance *decl,
370 struct ExternInstance *instance);
371 safe_vector<const IR::TCKey *> processExternControlPath(
const IR::Type_Extern *extn,
372 const IR::Declaration_Instance *decl,
374 cstring getControlPathKeyAnnotation(
const IR::StructField *field);
375 unsigned GetAccessNumericValue(std::string_view access);
376 bool isDuplicateAction(
const IR::P4Action *action);
377 bool isDuplicateOrNoAction(
const IR::P4Action *action);
378 void updateDefaultHitAction(
const IR::P4Table *t, IR::TCTable *tdef);
379 void updateDefaultMissAction(
const IR::P4Table *t, IR::TCTable *tdef);
380 void updateConstEntries(
const IR::P4Table *t, IR::TCTable *tdef);
381 void updateMatchType(
const IR::P4Table *t, IR::TCTable *tabledef);
382 void updateTimerProfiles(IR::TCTable *tabledef);
383 void updatePnaDirectCounter(
const IR::P4Table *t, IR::TCTable *tabledef,
unsigned tentries);
384 void updatePnaDirectMeter(
const IR::P4Table *t, IR::TCTable *tabledef,
unsigned tentries);
386 bool isPnaMainInputMeta(
const IR::Member *mem);
387 bool isPnaMainOutputMeta(
const IR::Member *mem);
388 unsigned int findMappedKernelMeta(
const IR::Member *mem);
389 const IR::Expression *ExtractExpFromCast(
const IR::Expression *exp);
390 unsigned getTcType(
const IR::StringLiteral *sl);
391 unsigned getTableId(cstring tableName)
const;
392 unsigned getActionId(cstring actionName)
const;
393 cstring getExternId(cstring externName)
const;
394 unsigned getExternInstanceId(cstring externName, cstring instanceName)
const;
395 cstring processExternPermission(
const IR::Type_Extern *ext);
396 unsigned getTableKeysize(
unsigned tableId)
const;
397 cstring externalName(
const IR::IDeclaration *declaration)
const;
398 cstring HandleTableAccessPermission(
const IR::P4Table *t);
399 std::pair<cstring, cstring> *GetAnnotatedAccessPath(
const IR::Annotation *anno);
400 void updateAddOnMissTable(
const IR::P4Table *t);
401 bool checkParameterDirection(
const IR::TCAction *tcAction);
402 bool hasExecuteMethod(
const IR::Type_Extern *extn);
403 void addExternTypeInstance(
const IR::Declaration_Instance *decl,
404 IR::TCExternInstance *tcExternInstance, cstring eName);
405 safe_vector<const IR::TCKey *> HandleTypeNameStructField(
const IR::StructField *field,
406 const IR::Type_Extern *extn,
407 const IR::Declaration_Instance *decl,
408 int &kId, cstring annoName);
409 safe_vector<const IR::TCKey *> processCounterControlPathKeys(
410 const IR::Type_Struct *extern_control_path,
const IR::Type_Extern *extn,
411 const IR::Declaration_Instance *decl);
412 CounterType toCounterType(
const int type);