139#define CMP_SIGNED 0x08
154 friend bool operator<(
const WidthRec &l,
const WidthRec &r) {
155 if (l.type != r.type)
return (l.type < r.type);
158 return (l.u.value < r.u.value);
162 (l.u.concat.lhsw < r.u.concat.lhsw) ||
163 ((l.u.concat.lhsw == r.u.concat.lhsw) && (l.u.concat.rhsw < r.u.concat.rhsw)));
173 return (l.u.arith.w < r.u.arith.w);
176 return ((l.u.bxsmul.bw < r.u.bxsmul.bw) ||
177 ((l.u.bxsmul.bw == r.u.bxsmul.bw) && (l.u.bxsmul.sv < r.u.bxsmul.sv)));
180 return ((l.u.cast.fw < r.u.cast.fw) ||
181 ((l.u.cast.fw == r.u.cast.fw) && (l.u.cast.tw < r.u.cast.tw)));
186 return ((l.u.shift_x.lw < r.u.shift_x.lw) || ((l.u.shift_x.lw == r.u.shift_x.lw) &&
187 ((l.u.shift_x.rw < r.u.shift_x.rw))));
192 return ((l.u.shift_c.lw < r.u.shift_c.lw) || ((l.u.shift_c.lw == r.u.shift_c.lw) &&
193 ((l.u.shift_c.sv < r.u.shift_c.sv))));
196 return ((l.u.cmp.w < r.u.cmp.w) ||
197 ((l.u.cmp.w == r.u.cmp.w) && (l.u.cmp.cmp < r.u.cmp.cmp)));
201 return ((l.u.sarith.w < r.u.sarith.w) ||
202 ((l.u.sarith.w == r.u.sarith.w) &&
203 (!l.u.sarith.issigned && r.u.sarith.issigned)));
206 return (l.u.assign.w < r.u.assign.w);
211 friend bool operator==(
const WidthRec &l,
const WidthRec &r) {
212 if (l.type != r.type)
return (0);
215 return (l.u.value == r.u.value);
218 return ((l.u.concat.lhsw == r.u.concat.lhsw) &&
219 (l.u.concat.rhsw) == (r.u.concat.rhsw));
229 return (l.u.arith.w == r.u.arith.w);
232 return ((l.u.bxsmul.bw == r.u.bxsmul.bw) && (l.u.bxsmul.sv == r.u.bxsmul.sv));
235 return ((l.u.cast.fw == r.u.cast.fw) && (l.u.cast.tw == r.u.cast.tw));
240 return ((l.u.shift_x.lw == r.u.shift_x.lw) && (l.u.shift_x.rw == r.u.shift_x.rw));
245 return ((l.u.shift_c.lw == r.u.shift_c.lw) && (l.u.shift_c.sv == r.u.shift_c.sv));
248 return ((l.u.cmp.w == r.u.cmp.w) && (l.u.cmp.cmp == r.u.cmp.cmp));
252 return ((l.u.sarith.w == r.u.sarith.w) &&
253 (l.u.sarith.issigned == r.u.sarith.issigned));
256 return (l.u.assign.w == r.u.assign.w);
261 friend bool operator>(
const WidthRec &l,
const WidthRec &r) {
return (r < l); }
262 friend bool operator>=(
const WidthRec &l,
const WidthRec &r) {
return (!(l < r)); }
263 friend bool operator<=(
const WidthRec &l,
const WidthRec &r) {
return (!(r < l)); }
264 friend bool operator!=(
const WidthRec &l,
const WidthRec &r) {
return (!(l == r)); }
267class ScanWidths :
public Inspector {
276 void add_concat(
int,
int);
277 void add_arith(WrType,
int);
278 void add_sarith(WrType,
int,
bool);
279 void add_bxsmul(
int,
unsigned int);
280 void add_cast(
unsigned int,
unsigned int);
281 bool bigXSmallMul(
const IR::Expression *,
const IR::Constant *);
282 void add_shift_c(WrType,
unsigned int,
unsigned int);
283 void add_shift_x(WrType,
unsigned int,
unsigned int);
284 void add_cmp(
unsigned int,
unsigned char);
285 void add_assign(
unsigned int);
289 : nwr(0), wrv(0), typemap(tm), refmap(rm), target(tgt) {}
290 ~ScanWidths(
void) { std::free(wrv); }
291 void expr_common(
const IR::Expression *);
292 bool preorder(
const IR::Expression *)
override;
293 bool preorder(
const IR::Concat *)
override;
294 bool preorder(
const IR::Add *)
override;
295 bool preorder(
const IR::Sub *)
override;
296 bool preorder(
const IR::Mul *)
override;
297 bool preorder(
const IR::Cast *)
override;
298 bool preorder(
const IR::Neg *)
override;
299 bool preorder(
const IR::Cmpl *)
override;
300 bool preorder(
const IR::Shl *)
override;
301 bool preorder(
const IR::Shr *)
override;
302 bool preorder(
const IR::Equ *)
override;
303 bool preorder(
const IR::Neq *)
override;
304 bool preorder(
const IR::Lss *)
override;
305 bool preorder(
const IR::Leq *)
override;
306 bool preorder(
const IR::Grt *)
override;
307 bool preorder(
const IR::Geq *)
override;
308 bool preorder(
const IR::BAnd *)
override;
309 bool preorder(
const IR::BOr *)
override;
310 bool preorder(
const IR::BXor *)
override;
311 bool preorder(
const IR::AddSat *)
override;
312 bool preorder(
const IR::SubSat *)
override;
313 bool preorder(
const IR::AssignmentStatement *)
override;
314 bool preorder(
const IR::MethodCallExpression *)
override;
315 bool arith_common_2(
const IR::Operation_Binary *, WrType,
bool =
true);
316 bool arith_common_1(
const IR::Operation_Unary *, WrType,
bool =
true);
317 bool sarith_common_2(
const IR::Operation_Binary *, WrType,
bool =
true);
319 void end_apply(
const IR::Node *)
override;
320 void revisit_visited();
329class ConvertToBackendIR :
public Inspector {
333 unsigned instance_id;
334 bool is_num_elements;
340 unsigned no_of_instances;
344 enum CounterType { PACKETS, BYTES, PACKETS_AND_BYTES };
345 const IR::ToplevelBlock *tlb;
346 IR::TCPipeline *tcPipeline;
350 unsigned int tableCount = 0;
351 unsigned int actionCount = 0;
352 unsigned int metadataCount = 0;
353 unsigned int labelCount = 0;
354 unsigned int externCount = 0;
355 cstring pipelineName =
nullptr;
356 cstring mainParserName =
nullptr;
369 : tlb(tlb), tcPipeline(pipe), refMap(refMap), typeMap(typeMap), options(options) {}
370 void setPipelineName();
371 cstring getPipelineName() {
return pipelineName; };
372 bool preorder(
const IR::P4Program *p)
override;
373 void postorder(
const IR::P4Action *a)
override;
374 void postorder(
const IR::P4Table *t)
override;
375 void postorder(
const IR::P4Program *p)
override;
376 void postorder(
const IR::Declaration_Instance *d)
override;
377 void postorder(
const IR::Type_Struct *ts)
override;
378 safe_vector<const IR::TCKey *> processExternConstructor(
const IR::Type_Extern *extn,
379 const IR::Declaration_Instance *decl,
380 struct ExternInstance *instance);
381 safe_vector<const IR::TCKey *> processExternControlPath(
const IR::Type_Extern *extn,
382 const IR::Declaration_Instance *decl,
384 cstring getControlPathKeyAnnotation(
const IR::StructField *field);
385 unsigned GetAccessNumericValue(std::string_view access);
386 bool isDuplicateAction(
const IR::P4Action *action);
387 bool isDuplicateOrNoAction(
const IR::P4Action *action);
388 void updateDefaultHitAction(
const IR::P4Table *t, IR::TCTable *tdef);
389 void updateDefaultMissAction(
const IR::P4Table *t, IR::TCTable *tdef);
390 void updateConstEntries(
const IR::P4Table *t, IR::TCTable *tdef);
391 void updateMatchType(
const IR::P4Table *t, IR::TCTable *tabledef);
392 void updateTimerProfiles(IR::TCTable *tabledef);
393 void updatePnaDirectCounter(
const IR::P4Table *t, IR::TCTable *tabledef,
unsigned tentries);
394 void updatePnaDirectMeter(
const IR::P4Table *t, IR::TCTable *tabledef,
unsigned tentries);
396 bool isPnaMainInputMeta(
const IR::Member *mem);
397 bool isPnaMainOutputMeta(
const IR::Member *mem);
398 unsigned int findMappedKernelMeta(
const IR::Member *mem);
399 const IR::Expression *ExtractExpFromCast(
const IR::Expression *exp);
400 unsigned getTcType(
const IR::StringLiteral *sl);
401 unsigned getTableId(cstring tableName)
const;
402 unsigned getActionId(cstring actionName)
const;
403 cstring getExternId(cstring externName)
const;
404 unsigned getExternInstanceId(cstring externName, cstring instanceName)
const;
405 cstring processExternPermission(
const IR::Type_Extern *ext);
406 unsigned getTableKeysize(
unsigned tableId)
const;
407 cstring externalName(
const IR::IDeclaration *declaration)
const;
408 cstring HandleTableAccessPermission(
const IR::P4Table *t);
409 std::pair<cstring, cstring> *GetAnnotatedAccessPath(
const IR::Annotation *anno);
410 void updateAddOnMissTable(
const IR::P4Table *t);
411 bool checkParameterDirection(
const IR::TCAction *tcAction);
412 bool hasExecuteMethod(
const IR::Type_Extern *extn);
413 void addExternTypeInstance(
const IR::Declaration_Instance *decl,
414 IR::TCExternInstance *tcExternInstance, cstring eName);
415 safe_vector<const IR::TCKey *> HandleTypeNameStructField(
const IR::StructField *field,
416 const IR::Type_Extern *extn,
417 const IR::Declaration_Instance *decl,
418 int &kId, cstring annoName);
419 safe_vector<const IR::TCKey *> processCounterControlPathKeys(
420 const IR::Type_Struct *extern_control_path,
const IR::Type_Extern *extn,
421 const IR::Declaration_Instance *decl);
422 CounterType toCounterType(
const int type);