Skip to content

Commit

Permalink
Add pure/localState/packetStata annotations
Browse files Browse the repository at this point in the history
- add annotations in appropriate places in v1mode.p4 and psa.p4
- fix resolveReferences to look at parameters
  • Loading branch information
Chris Dodd committed Dec 30, 2019
1 parent 90a8f3f commit bf457e3
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 6 deletions.
14 changes: 14 additions & 0 deletions frontends/common/resolveReferences/resolveReferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,27 @@ void ResolveReferences::postorder(const IR::P4Parser *p) {
bool ResolveReferences::preorder(const IR::Function* function) {
refMap->usedName(function->name.name);
addToContext(function->type->parameters);
resolveForward.push_back(true); // annotations may refer to arguments
return true;
}

void ResolveReferences::postorder(const IR::Function* function) {
resolveForward.pop_back();
removeFromContext(function->type->parameters);
}

bool ResolveReferences::preorder(const IR::Method* method) {
refMap->usedName(method->name.name);
addToContext(method->type->parameters);
resolveForward.push_back(true); // annotations may refer to arguments
return true;
}

void ResolveReferences::postorder(const IR::Method* method) {
resolveForward.pop_back();
removeFromContext(method->type->parameters);
}

bool ResolveReferences::preorder(const IR::P4Table* t) {
refMap->usedName(t->name.name);
return true;
Expand Down
1 change: 1 addition & 0 deletions frontends/common/resolveReferences/resolveReferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class ResolveReferences : public Inspector {
DECLARE(P4Parser)
DECLARE(P4Action)
DECLARE(Function)
DECLARE(Method)
DECLARE(TableProperties)
DECLARE(Type_Method)
DECLARE(ParserState)
Expand Down
4 changes: 4 additions & 0 deletions frontends/p4/parseAnnotations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ ParseAnnotations::HandlerMap ParseAnnotations::standardHandlers() {
PARSE_EMPTY(IR::Annotation::hiddenAnnotation),
PARSE_EMPTY(IR::Annotation::atomicAnnotation),
PARSE_EMPTY(IR::Annotation::optionalAnnotation),
PARSE_EMPTY(IR::Annotation::pureAnnotation),
PARSE_EMPTY(IR::Annotation::noSideEffectsAnnotation),
PARSE_EMPTY(IR::Annotation::localStateAnnotation),
PARSE_EMPTY(IR::Annotation::packetStateAnnotation),
PARSE(IR::Annotation::localIndexedStateAnnotation, Expression),

// @name and @deprecated have a string literal argument.
PARSE(IR::Annotation::nameAnnotation, StringLiteral),
Expand Down
8 changes: 6 additions & 2 deletions ir/base.def
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,12 @@ class Annotation {
static const cstring optionalAnnotation; /// Optional parameter annotation
static const cstring pkginfoAnnotation; /// Package documentation annotation.
static const cstring deprecatedAnnotation; /// Deprecation annotation.
static const cstring synchronousAnnotation; /// noSideEffects annotation.
static const cstring noSideEffectsAnnotation; /// Synchronous annotation.
static const cstring synchronousAnnotation; /// Synchronous annotation.
static const cstring pureAnnotation; /// extern function/method annotation.
static const cstring noSideEffectsAnnotation; /// extern function/method annotation.
static const cstring localStateAnnotation; /// extern function/method annotation.
static const cstring packetStateAnnotation; /// extern function/method annotation.
static const cstring localIndexedStateAnnotation; /// extern function/method annotation.
static const cstring matchAnnotation; /// Match annotation (for value sets).
toString{ return cstring("@") + name; }
validate{
Expand Down
4 changes: 4 additions & 0 deletions ir/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ const cstring IR::Annotation::optionalAnnotation = "optional";
const cstring IR::Annotation::pkginfoAnnotation = "pkginfo";
const cstring IR::Annotation::deprecatedAnnotation = "deprecated";
const cstring IR::Annotation::synchronousAnnotation = "synchronous";
const cstring IR::Annotation::pureAnnotation = "pure";
const cstring IR::Annotation::noSideEffectsAnnotation = "noSideEffects";
const cstring IR::Annotation::localStateAnnotation = "localState";
const cstring IR::Annotation::packetStateAnnotation = "packetState";
const cstring IR::Annotation::localIndexedStateAnnotation = "localIndexedState";
const cstring IR::Annotation::matchAnnotation = "match";

int Type_Declaration::nextId = 0;
Expand Down
28 changes: 28 additions & 0 deletions p4include/psa.p4
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ struct psa_egress_output_metadata_t {
/// allows those assignments to execute if psa_clone_i2e(istd) returns
/// true. psa_clone_i2e can be implemented by returning istd.clone

@pure
extern bool psa_clone_i2e(in psa_ingress_output_metadata_t istd);

/// During the IngressDeparser execution, psa_resubmit returns true if
Expand All @@ -354,6 +355,7 @@ extern bool psa_clone_i2e(in psa_ingress_output_metadata_t istd);
/// true. psa_resubmit can be implemented by returning (!istd.drop &&
/// istd.resubmit)

@pure
extern bool psa_resubmit(in psa_ingress_output_metadata_t istd);

/// During the IngressDeparser execution, psa_normal returns true if
Expand All @@ -364,6 +366,7 @@ extern bool psa_resubmit(in psa_ingress_output_metadata_t istd);
/// psa_normal(istd) returns true. psa_normal can be implemented by
/// returning (!istd.drop && !istd.resubmit)

@pure
extern bool psa_normal(in psa_ingress_output_metadata_t istd);

/// During the EgressDeparser execution, psa_clone_e2e returns true if
Expand All @@ -374,6 +377,7 @@ extern bool psa_normal(in psa_ingress_output_metadata_t istd);
/// execute if psa_clone_e2e(istd) returns true. psa_clone_e2e can be
/// implemented by returning istd.clone

@pure
extern bool psa_clone_e2e(in psa_egress_output_metadata_t istd);

/// During the EgressDeparser execution, psa_recirculate returns true
Expand All @@ -384,11 +388,14 @@ extern bool psa_clone_e2e(in psa_egress_output_metadata_t istd);
/// can be implemented by returning (!istd.drop && (edstd.egress_port
/// == PSA_PORT_RECIRCULATE))

@pure
extern bool psa_recirculate(in psa_egress_output_metadata_t istd,
in psa_egress_deparser_input_metadata_t edstd);


@localState
extern void assert(in bool check);
@localState
extern void assume(in bool check);

// BEGIN:Match_kinds
Expand Down Expand Up @@ -490,6 +497,7 @@ extern Hash<O> {
/// Compute the hash for data.
/// @param data The data over which to calculate the hash.
/// @return The hash value.
@pure
O get_hash<D>(in D data);

/// Compute the hash for data, with modulo by max, then add base.
Expand All @@ -502,6 +510,7 @@ extern Hash<O> {
/// limit their choice to such values if they wish to
/// maximize portability.
/// @return (base + (h % max)) where h is the hash value.
@pure
O get_hash<T, D>(in T base, in D data, in T max);
}
// END:Hash_extern
Expand All @@ -517,12 +526,15 @@ extern Checksum<W> {
/// time the object is instantiated, that is, whenever the parser or control
/// containing the Checksum object are applied.
/// All state maintained by the Checksum object is independent per packet.
@packetState
void clear();

/// Add data to checksum
@packetState
void update<T>(in T data);

/// Get checksum for data added (and not removed) since last clear
@packetState @noSideEffects
W get();
}
// END:Checksum_extern
Expand All @@ -540,27 +552,33 @@ extern InternetChecksum {
/// initialized as if clear() had been called on it, once for each
/// time the parser or control it is instantiated within is
/// executed. All state maintained by it is independent per packet.
@packetState
void clear();

/// Add data to checksum. data must be a multiple of 16 bits long.
@packetState
void add<T>(in T data);

/// Subtract data from existing checksum. data must be a multiple of
/// 16 bits long.
@packetState
void subtract<T>(in T data);

/// Get checksum for data added (and not removed) since last clear
@packetState @noSideEffects
bit<16> get();

/// Get current state of checksum computation. The return value is
/// only intended to be used for a future call to the set_state
/// method.
@packetState @noSideEffects
bit<16> get_state();

/// Restore the state of the InternetChecksum instance to one
/// returned from an earlier call to the get_state method. This
/// state could have been returned from the same instance of the
/// InternetChecksum extern, or a different one.
@packetState
void set_state(in bit<16> checksum_state);
}
// END:InternetChecksum_extern
Expand All @@ -579,6 +597,7 @@ enum PSA_CounterType_t {

extern Counter<W, S> {
Counter(bit<32> n_counters, PSA_CounterType_t type);
@localIndexedState(index)
void count(in S index);

/*
Expand Down Expand Up @@ -608,6 +627,7 @@ extern Counter<W, S> {
// BEGIN:DirectCounter_extern
extern DirectCounter<W> {
DirectCounter(PSA_CounterType_t type);
@localState
void count();

/*
Expand Down Expand Up @@ -644,11 +664,13 @@ extern Meter<S> {
// Use this method call to perform a color aware meter update (see
// RFC 2698). The color of the packet before the method call was
// made is specified by the color parameter.
@localIndexedState(index)
PSA_MeterColor_t execute(in S index, in PSA_MeterColor_t color);

// Use this method call to perform a color blind meter update (see
// RFC 2698). It may be implemented via a call to execute(index,
// MeterColor_t.GREEN), which has the same behavior.
@localIndexedState(index)
PSA_MeterColor_t execute(in S index);

/*
Expand All @@ -666,7 +688,9 @@ extern Meter<S> {
extern DirectMeter {
DirectMeter(PSA_MeterType_t type);
// See the corresponding methods for extern Meter.
@localState
PSA_MeterColor_t execute(in PSA_MeterColor_t color);
@localState
PSA_MeterColor_t execute();

/*
Expand All @@ -689,7 +713,9 @@ extern Register<T, S> {
/// initial_value.
Register(bit<32> size, T initial_value);

@localIndexedState(index) @noSideEffects
T read (in S index);
@localIndexedState(index)
void write (in S index, in T value);

/*
Expand All @@ -712,6 +738,7 @@ extern Random<T> {
/// arguments to such values if they wish to maximize portability.

Random(T min, T max);
@localState
T read();

/*
Expand Down Expand Up @@ -766,6 +793,7 @@ extern ActionSelector {
// BEGIN:Digest_extern
extern Digest<T> {
Digest(); /// define a digest stream to the control plane
@localState
void pack(in T data); /// emit data into the stream

/*
Expand Down
Loading

0 comments on commit bf457e3

Please sign in to comment.