<s> from p4_hlir . hlir import p4_counter , P4_DIRECT , P4_COUNTER_BYTES <EOL> from programSerializer import ProgramSerializer <EOL> from compilationException import * <EOL> import ebpfTable <EOL> import ebpfProgram <EOL> class EbpfCounter ( object ) : <EOL> def __init__ ( self , hlircounter , program ) : <EOL> assert isinstance ( hlircounter , p4_counter ) <EOL> assert isinstance ( program , ebpfProgram . EbpfProgram ) <EOL> self . name = hlircounter . name <EOL> self . hlircounter = hlircounter <EOL> width = hlircounter . min_width <EOL> if width <= <NUM_LIT:64> : <EOL> self . valueTypeName = program . config . uprefix + "<STR_LIT>" <EOL> else : <EOL> raise NotSupportedException ( <EOL> "<STR_LIT>" , hlircounter , width ) <EOL> self . dataMapName = self . name <EOL> if ( ( hlircounter . binding is None ) or <EOL> ( hlircounter . binding [ <NUM_LIT:0> ] != P4_DIRECT ) ) : <EOL> raise NotSupportedException ( <EOL> "<STR_LIT>" , hlircounter ) <EOL> self . autoIncrement = ( hlircounter . binding != None and <EOL> hlircounter . binding [ <NUM_LIT:0> ] == P4_DIRECT ) <EOL> if hlircounter . type is P4_COUNTER_BYTES : <EOL> self . increment = "<STR_LIT>" . format ( program . packetName ) <EOL> else : <EOL> self . increment = "<STR_LIT:1>" <EOL> def getSize ( self , program ) : <EOL> if self . hlircounter . instance_count is not None : <EOL> return self . hlircounter . instance_count <EOL> if self . autoIncrement : <EOL> return self . getTable ( program ) . size <EOL> program . emitWarning ( <EOL> "<STR_LIT>" , self . hlircounter ) <EOL> return <NUM_LIT> <EOL> def getTable ( self , program ) : <EOL> table = program . getTable ( self . hlircounter . binding [ <NUM_LIT:1> ] . name ) <EOL> assert isinstance ( table , ebpfTable . EbpfTable ) <EOL> return table <EOL> def serialize ( self , serializer , program ) : <EOL> assert isinstance ( serializer , ProgramSerializer ) <EOL> if self . autoIncrement : <EOL> keyTypeName = "<STR_LIT>" + self . getTable ( program ) . keyTypeName <EOL> else : <EOL> keyTypeName = program . config . uprefix + "<STR_LIT>" <EOL> program . config . serializeTableDeclaration ( <EOL> serializer , self . dataMapName , True , keyTypeName , <EOL> self . valueTypeName , self . getSize ( program ) ) <EOL> def serializeCode ( self , keyname , serializer , program ) : <EOL> assert isinstance ( serializer , ProgramSerializer ) <EOL> assert isinstance ( program , ebpfProgram . EbpfProgram ) <EOL> serializer . emitIndent ( ) <EOL> serializer . appendFormat ( "<STR_LIT>" , self . name ) <EOL> serializer . newline ( ) <EOL> valueName = "<STR_LIT>" <EOL> initValuename = "<STR_LIT>" <EOL> serializer . emitIndent ( ) <EOL> serializer . appendFormat ( "<STR_LIT>" , self . valueTypeName , valueName ) <EOL> serializer . newline ( ) <EOL> serializer . emitIndent ( ) <EOL> serializer . appendFormat ( "<STR_LIT>" , self . valueTypeName , initValuename ) <EOL> serializer . newline ( ) <EOL> serializer . emitIndent ( ) <EOL> serializer . appendLine ( "<STR_LIT>" ) <EOL> serializer . emitIndent ( ) <EOL> program . config . serializeLookup ( <EOL> serializer , self . dataMapName , keyname , valueName ) <EOL> serializer . newline ( ) <EOL> serializer . emitIndent ( ) <EOL> serializer . appendFormat ( "<STR_LIT>" , valueName ) <EOL> serializer . newline ( ) <EOL> serializer . increaseIndent ( ) <EOL> serializer . emitIndent ( ) <EOL> serializer . appendFormat ( "<STR_LIT>" , <EOL> valueName , self . increment ) <EOL> serializer . newline ( ) <EOL> serializer . decreaseIndent ( ) <EOL> serializer . emitIndent ( ) <EOL> serializer . append ( "<STR_LIT>" ) <EOL> serializer . blockStart ( ) <EOL> serializer . emitIndent ( ) <EOL> serializer . appendFormat ( "<STR_LIT>" , initValuename , self . increment ) <EOL> serializer . newline ( ) <EOL> serializer . emitIndent ( ) <EOL> program . config . serializeUpdate ( <EOL> serializer , self . dataMapName , keyname , initValuename ) <EOL> serializer . newline ( ) <EOL> serializer . blockEnd ( True ) </s>
