Skip to content

Commit

Permalink
V1Model::convertExternObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
Han Wang committed May 4, 2017
1 parent e29e795 commit ce946ce
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 50 deletions.
3 changes: 2 additions & 1 deletion backends/bmv2/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ p4c_bm2_ss_UNIFIED = \
backends/bmv2/helpers.cpp \
backends/bmv2/lower.cpp \
backends/bmv2/midend.cpp \
backends/bmv2/parser.cpp
backends/bmv2/parser.cpp \
backends/bmv2/v1model.cpp

noinst_HEADERS += \
backends/bmv2/analyzer.h \
Expand Down
65 changes: 38 additions & 27 deletions backends/bmv2/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.
#include "header.h"
#include "parser.h"
#include "deparser.h"
#include "error_code.h"
#include "errorcode.h"
#include "expression.h"
#include "frontends/p4/methodInstance.h"

Expand Down Expand Up @@ -131,6 +131,37 @@ void Backend::addEnums(Util::JsonArray* enums) {
}
}

#ifdef PSA
void Backend::genExternMethod(Util::JsonArray* result, P4::ExternMethod *em) {
auto name = "_" + em->actualExternType->name + "_" + em->method->name.name;
// if (em->originalExternType->name.name == corelib.packetOut.name &&
// em->method->name.name == corelib.packetOut.emit.name) {
// LOG1("print emit method");
// }
auto primitive = mkPrimitive(name, result);
auto parameters = mkParameters(primitive);

auto ext = new Util::JsonObject();
ext->emplace("type", "extern");

//FIXME: have extern pass building a map and lookup here.
if (em->object->is<IR::Parameter>()) {
auto param = em->object->to<IR::Parameter>();
//auto packageObject = resolveParameter(param);
//ext->emplace("value", packageObject->getName());
ext->emplace("value", "FIXME");
} else {
ext->emplace("value", em->object->getName());
}
parameters->append(ext);

for (auto a : *mc->arguments) {
auto arg = conv->convert(a);
parameters->append(arg);
}
}
#endif

void Backend::convertActionBody(const IR::Vector<IR::StatOrDecl>* body, Util::JsonArray* result) {
//FIXME: conv->createFieldList = true??
for (auto s : *body) {
Expand Down Expand Up @@ -204,32 +235,12 @@ void Backend::convertActionBody(const IR::Vector<IR::StatOrDecl>* body, Util::Js
continue;
} else if (mi->is<P4::ExternMethod>()) {
auto em = mi->to<P4::ExternMethod>();
auto name = "_" + em->actualExternType->name + "_" + em->method->name.name;
// if (em->originalExternType->name.name == corelib.packetOut.name &&
// em->method->name.name == corelib.packetOut.emit.name) {
// LOG1("print emit method");
// }
auto primitive = mkPrimitive(name, result);
auto parameters = mkParameters(primitive);

auto ext = new Util::JsonObject();
ext->emplace("type", "extern");

//FIXME: have extern pass building a map and lookup here.
if (em->object->is<IR::Parameter>()) {
auto param = em->object->to<IR::Parameter>();
//auto packageObject = resolveParameter(param);
//ext->emplace("value", packageObject->getName());
ext->emplace("value", "FIXME");
} else {
ext->emplace("value", em->object->getName());
}
parameters->append(ext);

for (auto a : *mc->arguments) {
auto arg = conv->convert(a);
parameters->append(arg);
}
#ifdef PSA
genExternMethod(result, em);
#else
LOG1("P4V1:: convert " << s);
P4V1::V1Model::convertExternObjects(result, this, em, mc);
#endif
continue;
} else if (mi->is<P4::ExternFunction>()) {
auto ef = mi->to<P4::ExternFunction>();
Expand Down
4 changes: 4 additions & 0 deletions backends/bmv2/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Backend {
Util::JsonObject toplevel;
P4::V2Model& model;
P4V1::V1Model& v1model;
DirectMeterMap meterMap;

using ErrorValue = unsigned int;
using ErrorCodesMap = std::unordered_map<const IR::IDeclaration *, ErrorValue>;
Expand All @@ -73,6 +74,7 @@ class Backend {
protected:
void addMetaInformation();
void addEnums(Util::JsonArray* enums);
void genExternMethod(Util::JsonArray* result, P4::ExternMethod *em);
Backend::ErrorValue retrieveErrorValue(const IR::Member* mem) const;
void convertActionBody(const IR::Vector<IR::StatOrDecl>* body, Util::JsonArray* result);
void createActions(Util::JsonArray* actions);
Expand All @@ -85,6 +87,8 @@ class Backend {
void run(const IR::ToplevelBlock* block);
void serialize(std::ostream& out) const
{ toplevel.serialize(out); }
ExpressionConverter* getExpressionConverter() { return conv; };
DirectMeterMap& getMeterMap() { return meterMap; }
};

} // namespace BMV2
Expand Down
3 changes: 3 additions & 0 deletions backends/bmv2/errorcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ limitations under the License.

namespace BMV2 {


class ErrorCodesVisitor : public Inspector {
Util::JsonArray* errors;
using ErrorValue = unsigned int;
using ErrorCodesMap = std::unordered_map<const IR::IDeclaration *, ErrorValue>;
ErrorCodesMap* errorCodesMap;
protected:
Util::JsonArray* pushNewArray(Util::JsonArray* parent);
Expand Down
40 changes: 23 additions & 17 deletions backends/bmv2/v1model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ limitations under the License.
* This file implements the simple switch model
*/

#include <p4includes/v1model.h>
#include <frontends/p4/fromv1.0/v1model.h>
#include <backends/bmv2/backend.h>

using BMV2::mkPrimitive;
using BMV2::mkParameters;
using BMV2::extVisibleName;

namespace P4V1 {

void V1Model::convertExternObject(Util::JsonObject *o, BMV2::Backend *bmv2,
P4::ExternMethod *em, IR::MethodCallStatement *mc)
void V1Model::convertExternObjects(Util::JsonArray *result, BMV2::Backend *bmv2,
const P4::ExternMethod *em,
const IR::MethodCallExpression *mc)
{
if (em->originalExternType->name == v1model.counter.name) {
if (em->method->name == v1model.counter.increment.name) {
if (em->originalExternType->name == instance.counter.name) {
if (em->method->name == instance.counter.increment.name) {
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 argument for %1%", mc);
auto primitive = mkPrimitive("count", result);
auto parameters = mkParameters(primitive);
Expand All @@ -38,8 +43,8 @@ void V1Model::convertExternObject(Util::JsonObject *o, BMV2::Backend *bmv2,
auto index = bmv2->getExpressionConverter()->convert(mc->arguments->at(0));
parameters->append(index);
}
} else if (em->originalExternType->name == v1model.meter.name) {
if (em->method->name == v1model.meter.executeMeter.name) {
} else if (em->originalExternType->name == instance.meter.name) {
if (em->method->name == instance.meter.executeMeter.name) {
BUG_CHECK(mc->arguments->size() == 2, "Expected 2 arguments for %1%", mc);
auto primitive = mkPrimitive("execute_meter", result);
auto parameters = mkParameters(primitive);
Expand All @@ -52,21 +57,21 @@ void V1Model::convertExternObject(Util::JsonObject *o, BMV2::Backend *bmv2,
auto result = bmv2->getExpressionConverter()->convert(mc->arguments->at(1));
parameters->append(result);
}
} else if (em->originalExternType->name == v1model.registers.name) {
} else if (em->originalExternType->name == instance.registers.name) {
BUG_CHECK(mc->arguments->size() == 2, "Expected 2 arguments for %1%", mc);
auto reg = new Util::JsonObject();
reg->emplace("type", "register_array");
cstring name = extVisibleName(em->object);
reg->emplace("value", name);
if (em->method->name == v1model.registers.read.name) {
if (em->method->name == instance.registers.read.name) {
auto primitive = mkPrimitive("register_read", result);
auto parameters = mkParameters(primitive);
auto dest = bmv2->getExpressionConverter()->convert(mc->arguments->at(0));
parameters->append(dest);
parameters->append(reg);
auto index = bmv2->getExpressionConverter()->convert(mc->arguments->at(1));
parameters->append(index);
} else if (em->method->name == v1model.registers.write.name) {
} else if (em->method->name == instance.registers.write.name) {
auto primitive = mkPrimitive("register_write", result);
auto parameters = mkParameters(primitive);
parameters->append(reg);
Expand All @@ -75,25 +80,26 @@ void V1Model::convertExternObject(Util::JsonObject *o, BMV2::Backend *bmv2,
auto value = bmv2->getExpressionConverter()->convert(mc->arguments->at(1));
parameters->append(value);
}
} else if (em->originalExternType->name == v1model.directMeter.name) {
if (em->method->name == v1model.directMeter.read.name) {
} else if (em->originalExternType->name == instance.directMeter.name) {
if (em->method->name == instance.directMeter.read.name) {
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 argument for %1%", mc);
auto dest = mc->arguments->at(0);
bmv2->getMeterMap().setDestination(em->object, dest);
// Do not generate any code for this operation
}
} else if (em->originalExternType->name == v1model.directCounter.name) {
if (em->method->name == v1model.directCounter.count.name) {
} else if (em->originalExternType->name == instance.directCounter.name) {
if (em->method->name == instance.directCounter.count.name) {
BUG_CHECK(mc->arguments->size() == 0, "Expected 0 argument for %1%", mc);
// Do not generate any code for this operation
}
}

}

void V1Model::convertExternFunctions(Util::JsonObject *o, BMV2::Backend *bmv2,
P4::ExternMethod *em, IR::MethodCallStatement *mc){
void V1Model::convertExternFunctions(Util::JsonArray *result, BMV2::Backend *bmv2,
const P4::ExternMethod *em,
const IR::MethodCallExpression *mc){

}

}
}
16 changes: 11 additions & 5 deletions frontends/p4/fromv1.0/v1model.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ limitations under the License.
#include "lib/cstring.h"
#include "frontends/common/model.h"
#include "frontends/p4/coreLibrary.h"
#include "frontends/p4/methodInstance.h"
#include "ir/ir.h"
#include "lib/json.h"

class BMV2::Backend;
namespace BMV2 {
class Backend;
}

namespace P4V1 {

Expand Down Expand Up @@ -246,6 +249,7 @@ struct TableAttributes_Model {
const unsigned defaultTableSize = 1024;
};


class V1Model : public ::Model::Model {
protected:
V1Model() :
Expand Down Expand Up @@ -301,10 +305,12 @@ class V1Model : public ::Model::Model {
DirectMeter_Model directMeter;
DirectCounter_Model directCounter;

static void convertExternObjects(Util::JsonObject *o, BMV2::Backend *bmv2,
P4::ExternMethod *em, IR::MethodCallStatement *mc);
static void convertExternFunctions(Util::JsonObject *o, BMV2::Backend *bmv2,
P4::ExternMethod *em, IR::MethodCallStatement *mc);
static void convertExternObjects(Util::JsonArray *o, BMV2::Backend *bmv2,
const P4::ExternMethod *em,
const IR::MethodCallExpression *mc);
static void convertExternFunctions(Util::JsonArray *o, BMV2::Backend *bmv2,
const P4::ExternMethod *em,
const IR::MethodCallExpression *mc);

static V1Model instance;
};
Expand Down

0 comments on commit ce946ce

Please sign in to comment.