Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mbudiu #46

Merged
merged 3 commits into from
May 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions backends/bmv2/bmv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ int main(int argc, char *const argv[]) {
if (::errorCount() > 0)
return 1;

auto hook = options.getDebugHook();

// BMV2 is required for compatibility with the previous compiler.
options.preprocessor_options += " -D__TARGET_BMV2__";
auto program = parseP4File(options);
if (program == nullptr || ::errorCount() > 0)
return 1;
FrontEnd frontend;
frontend.addDebugHook(hook);
program = frontend.run(options, program);
if (program == nullptr || ::errorCount() > 0)
return 1;

BMV2::MidEnd midEnd;
midEnd.addDebugHook(hook);
auto blockMap = midEnd.process(options, program);
if (::errorCount() > 0 || blockMap == nullptr)
return 1;
Expand Down
18 changes: 7 additions & 11 deletions backends/bmv2/jsonconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,25 +612,21 @@ JsonConverter::convertActionBody(const IR::Vector<IR::StatOrDecl>* body,

cstring prim;
auto parameters = new Util::JsonArray();
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 argument for %1%", mc);
auto arg = conv->convert(mc->arguments->at(0));
auto obj = conv->convert(builtin->appliedTo);
parameters->append(obj);

if (builtin->name == IR::Type_Header::setValid) {
if (!mc->arguments->at(0)->is<IR::BoolLiteral>()) {
::error("%1%: argument must be a constant for this target", mc);
continue;
}
auto bl = mc->arguments->at(0)->to<IR::BoolLiteral>();
if (bl->value)
prim = "add_header";
else
prim = "remove_header";
prim = "add_header";
} else if (builtin->name == IR::Type_Header::setInvalid) {
prim = "remove_header";
} else if (builtin->name == IR::Type_Stack::push_front) {
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 argument for %1%", mc);
auto arg = conv->convert(mc->arguments->at(0));
prim = "push";
parameters->append(arg);
} else if (builtin->name == IR::Type_Stack::pop_front) {
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 argument for %1%", mc);
auto arg = conv->convert(mc->arguments->at(0));
prim = "pop";
parameters->append(arg);
} else {
Expand Down
12 changes: 5 additions & 7 deletions backends/bmv2/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const IR::P4Program* MidEnd::processV1(CompilerOptions&, const IR::P4Program* pr
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
};
midend.setName("Mid end");
midend.setStopOnError(true);
midend.addDebugHooks(hooks);
program = program->apply(midend);
if (::errorCount() > 0)
return nullptr;
Expand All @@ -69,15 +69,15 @@ const IR::P4Program* MidEnd::processV1_2(CompilerOptions&, const IR::P4Program*
// Move all local declarations to the beginning
new P4::MoveDeclarations(),
new P4::ResolveReferences(&refMap, isv1),
new P4::RemoveReturns(&refMap, true),
new P4::RemoveReturns(&refMap),
// Move some constructor calls into temporaries
new P4::MoveConstructors(isv1),
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
evaluator,
};

simplify.setName("Simplify");
simplify.setStopOnError(true);
simplify.addDebugHooks(hooks);
program = program->apply(simplify);
if (::errorCount() > 0)
return nullptr;
Expand Down Expand Up @@ -113,7 +113,7 @@ const IR::P4Program* MidEnd::processV1_2(CompilerOptions&, const IR::P4Program*
};

midEnd.setName("Mid end");
midEnd.setStopOnError(true);
midEnd.addDebugHooks(hooks);
program = program->apply(midEnd);
if (::errorCount() > 0)
return nullptr;
Expand All @@ -130,13 +130,11 @@ P4::BlockMap* MidEnd::process(CompilerOptions& options, const IR::P4Program* pro
if (program == nullptr)
return nullptr;

std::ostream *midendStream = options.dumpStream("-midend");
// BMv2-specific passes
P4::ReferenceMap refMap;
P4::TypeMap typeMap;
auto evaluator = new P4::EvaluatorPass(isv1);
PassManager backend = {
new P4::ToP4(midendStream, false, options.file),
new P4::TypeChecking(&refMap, &typeMap, isv1),
new P4::SimplifyControlFlow(&refMap, &typeMap),
new P4::TypeChecking(&refMap, &typeMap, isv1),
Expand All @@ -149,7 +147,7 @@ P4::BlockMap* MidEnd::process(CompilerOptions& options, const IR::P4Program* pro
};

backend.setName("Backend");
backend.setStopOnError(true);
backend.addDebugHooks(hooks);
program = program->apply(backend);
if (::errorCount() > 0)
return nullptr;
Expand Down
3 changes: 3 additions & 0 deletions backends/bmv2/midend.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
namespace BMV2 {

class MidEnd {
std::vector<DebugHook> hooks;

const IR::P4Program* processV1(CompilerOptions& options, const IR::P4Program* program);
const IR::P4Program* processV1_2(CompilerOptions& options, const IR::P4Program* program);
public:
P4::BlockMap* process(CompilerOptions& options, const IR::P4Program* program);
void addDebugHook(DebugHook hook) { hooks.push_back(hook); }
};

} // namespace BMV2
Expand Down
14 changes: 5 additions & 9 deletions backends/ebpf/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const IR::P4Program* MidEnd::run(EbpfOptions& options, const IR::P4Program* prog
// Move all local declarations to the beginning
new P4::MoveDeclarations(),
new P4::ResolveReferences(&refMap, isv1),
new P4::RemoveReturns(&refMap, true), // necessary for inlining
new P4::RemoveReturns(&refMap), // necessary for inlining
// Move some constructor calls into temporaries
new P4::MoveConstructors(isv1),
new P4::ResolveReferences(&refMap, isv1),
Expand All @@ -46,7 +46,7 @@ const IR::P4Program* MidEnd::run(EbpfOptions& options, const IR::P4Program* prog
};

simplify.setName("Simplify");
simplify.setStopOnError(true);
simplify.addDebugHooks(hooks);
program = program->apply(simplify);
if (::errorCount() > 0)
return nullptr;
Expand All @@ -72,8 +72,8 @@ const IR::P4Program* MidEnd::run(EbpfOptions& options, const IR::P4Program* prog
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
new P4::TypeChecking(&refMap, &typeMap, isv1),
new P4::SimplifyControlFlow(&refMap, &typeMap),
new P4::ResolveReferences(&refMap, isv1),
new P4::RemoveReturns(&refMap, false), // remove exits
new P4::TypeChecking(&refMap, &typeMap, isv1),
new P4::RemoveExits(&refMap, &typeMap),
new P4::TypeChecking(&refMap, &typeMap, isv1),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::StrengthReduction(),
Expand All @@ -82,15 +82,11 @@ const IR::P4Program* MidEnd::run(EbpfOptions& options, const IR::P4Program* prog
new P4::MoveActionsToTables(&refMap, &typeMap),
};
midEnd.setName("MidEnd");
midEnd.setStopOnError(true);
midEnd.addDebugHooks(hooks);
program = program->apply(midEnd);
if (::errorCount() > 0)
return nullptr;

std::ostream *midendStream = options.dumpStream("-midend");
P4::ToP4 top4(midendStream, false, options.file);
program->apply(top4);

return program;
}

Expand Down
2 changes: 2 additions & 0 deletions backends/ebpf/midend.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
namespace EBPF {

class MidEnd {
std::vector<DebugHook> hooks;
public:
void addDebugHook(DebugHook hook) { hooks.push_back(hook); }
const IR::P4Program* run(EbpfOptions& options, const IR::P4Program* program);
};

Expand Down
38 changes: 18 additions & 20 deletions backends/ebpf/p4c-ebpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,32 @@
#include "midend.h"
#include "ebpfOptions.h"
#include "ebpfBackend.h"
#include "frontends/p4/p4-parse.h"
#include "frontends/common/parseInput.h"
#include "frontends/p4/frontend.h"

void compile(EbpfOptions& options, FILE* in) {
void compile(EbpfOptions& options) {
auto hook = options.getDebugHook();
bool isv1 = options.langVersion == CompilerOptions::FrontendVersion::P4v1;
if (isv1) {
::error("This compiler only handles P4 v1.2");
return;
}
const IR::P4Program* v12 = parse_p4v1_2_file(options.file, in);
options.closeInput(in);
if (ErrorReporter::instance.getErrorCount() > 0) {
::error("%1% errors ecountered, aborting compilation",
ErrorReporter::instance.getErrorCount());
auto program = parseP4File(options);
if (::errorCount() > 0)
return;
}
FrontEnd frontend;
v12 = frontend.run(options, v12);
frontend.addDebugHook(hook);
program = frontend.run(options, program);
if (::errorCount() > 0)
return;

EBPF::MidEnd midend;
v12 = midend.run(options, v12);
midend.addDebugHook(hook);
program = midend.run(options, program);
if (::errorCount() > 0)
return;

EBPF::run_ebpf_backend(options, v12);
EBPF::run_ebpf_backend(options, program);
}

int main(int argc, char *const argv[]) {
Expand All @@ -43,17 +46,12 @@ int main(int argc, char *const argv[]) {
EbpfOptions options;
if (options.process(argc, argv) != nullptr)
options.setInputFile();

if (ErrorReporter::instance.getErrorCount() > 0)
if (::errorCount() > 0)
exit(1);

FILE* in = options.preprocess();
if (in != nullptr) {
compile(options, in);
}
compile(options);

if (verbose)
if (options.verbosity > 0)
std::cerr << "Done." << std::endl;

return ErrorReporter::instance.getErrorCount() > 0;
return ::errorCount() > 0;
}
8 changes: 3 additions & 5 deletions backends/v12test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ v12test_LDADD = libfrontend.a libp4ctoolkit.a
v12test_SOURCES = \
backends/v12test/v12test.cpp \
backends/v12test/midend.h \
backends/v12test/midend.cpp \
backends/v12test/options.h \
backends/v12test/options.cpp
backends/v12test/midend.cpp

cpplint_FILES += $(v12test_SOURCES)

Expand All @@ -27,7 +25,7 @@ CLEANFILES += \
$(srcdir)/v12tests.am: $(srcdir)/testdata/v1_2_samples/*.p4 $(srcdir)/testdata/v1_2_errors/*.p4
@$(GENTESTS) $(srcdir) v12 $(srcdir)/backends/v12test/run-v12-sample.py $^ >$@

$(srcdir)/v1to12tests.am: $(srcdir)/testdata/v1_samples/*.p4
$(srcdir)/v1to12tests.am: $(srcdir)/testdata/v1_samples/*.p4
@$(GENTESTS) $(srcdir) v1to12 $(srcdir)/backends/v12test/run-v12-sample.py $^ >$@

V12BUGS = \
Expand All @@ -37,4 +35,4 @@ V12BUGS = \

XFAIL_TESTS += \
$(V12BUGS) \
v12/testdata/v1_2_samples/cast-call.p4.test
v12/testdata/v1_2_samples/cast-call.p4.test
20 changes: 7 additions & 13 deletions backends/v12test/midend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ P4::BlockMap* MidEnd::process(CompilerOptions& options, const IR::P4Program* pro
P4::ReferenceMap refMap;
P4::TypeMap typeMap;

// TODO: remove table parameters if possible
// TODO: remove action parameters if possible
// TODO: remove table parameters
// TODO: remove action in/out/inout parameters
// TODO: remove expressions in table key
// TODO: break down expression into simple parts
// TODO: def-use analysis
Expand All @@ -45,7 +45,7 @@ P4::BlockMap* MidEnd::process(CompilerOptions& options, const IR::P4Program* pro
// Move all local declarations to the beginning
new P4::MoveDeclarations(),
new P4::ResolveReferences(&refMap, isv1),
new P4::RemoveReturns(&refMap, true),
new P4::RemoveReturns(&refMap),
// Move some constructor calls into temporaries
new P4::MoveConstructors(isv1),
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
Expand All @@ -54,8 +54,7 @@ P4::BlockMap* MidEnd::process(CompilerOptions& options, const IR::P4Program* pro

simplify.setName("Simplify");
simplify.setStopOnError(true);
for (auto h : hooks)
simplify.addDebugHook(h);
simplify.addDebugHooks(hooks);
program = program->apply(simplify);
if (::errorCount() > 0)
return nullptr;
Expand Down Expand Up @@ -83,8 +82,8 @@ P4::BlockMap* MidEnd::process(CompilerOptions& options, const IR::P4Program* pro
new P4::RemoveAllUnusedDeclarations(&refMap, isv1),
new P4::TypeChecking(&refMap, &typeMap, isv1),
new P4::SimplifyControlFlow(&refMap, &typeMap),
new P4::ResolveReferences(&refMap, isv1),
new P4::RemoveReturns(&refMap, false), // remove exits: FIXME: currently incorrect
new P4::TypeChecking(&refMap, &typeMap, isv1),
new P4::RemoveExits(&refMap, &typeMap), // FIXME: currently incorrect
new P4::TypeChecking(&refMap, &typeMap, isv1),
new P4::ConstantFolding(&refMap, &typeMap),
new P4::StrengthReduction(),
Expand All @@ -103,16 +102,11 @@ P4::BlockMap* MidEnd::process(CompilerOptions& options, const IR::P4Program* pro
};
midEnd.setName("MidEnd");
midEnd.setStopOnError(true);
for (auto h : hooks)
midEnd.addDebugHook(h);
midEnd.addDebugHooks(hooks);
program = program->apply(midEnd);
if (::errorCount() > 0)
return nullptr;

std::ostream *midendStream = options.dumpStream("-midend");
P4::ToP4 top4(midendStream, false, options.file);
program->apply(top4);

blockMap = evaluator->getBlockMap();
return blockMap;
}
Expand Down
12 changes: 0 additions & 12 deletions backends/v12test/options.cpp

This file was deleted.

16 changes: 0 additions & 16 deletions backends/v12test/options.h

This file was deleted.

9 changes: 5 additions & 4 deletions backends/v12test/run-v12-sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def compare_files(options, produced, expected):
continue
if ignoreNextMarker:
ignoreNextMarker = False
if marker.match(l):
if marker.match(l):
continue
if l[0] == ' ': continue
result = FAILURE
Expand Down Expand Up @@ -154,16 +154,17 @@ def process_file(options, argv):
base, ext = os.path.splitext(basename)
dirname = os.path.dirname(options.p4filename)
expected_dirname = dirname + "_outputs" # expected outputs are here

if options.verbose:
print("Writing temporary files into ", tmpdir)
ppfile = tmpdir + "/" + basename # after parsing
lastfile = tmpdir + "/" + base + "-last" + ext # last file produced
referenceOutputs = "FrontEnd_11,FrontEnd_12,MidEnd_25"
lastfile = tmpdir + "/" + base + "-FrontEnd_12_RemoveAllUnusedDeclarations" + ext # last file produced by front-end
stderr = tmpdir + "/" + basename + "-stderr"

if not os.path.isfile(options.p4filename):
raise Exception("No such file " + options.p4filename)
args = ["./v12test", "--pp", ppfile, "--dump", tmpdir] + options.compilerOptions
args = ["./v12test", "--pp", ppfile, "--dump", tmpdir, "--top4", referenceOutputs] + options.compilerOptions
if "v1_samples" in options.p4filename:
args.extend(["--p4v", "1.0"]);
args.extend(argv)
Expand Down
Loading