Skip to content

Commit

Permalink
Merge branch 'master' into issue270
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDodd authored Feb 2, 2017
2 parents c2b1afa + 727910a commit c0800ab
Show file tree
Hide file tree
Showing 24 changed files with 132 additions and 24 deletions.
27 changes: 24 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export IFAIL_TESTS
cpplint_FILES = # Files that should be passed to cpplint (and etags)
ir_DEF_FILES = # Files that generate the IR
extension_frontend_SOURCES = # Files added to libfrontend by extensions
p4include_HEADERS = # p4_16 include files
p4_14include_HEADERS = # p4_14 include files

################ Subproject Makefile inclusions

Expand Down Expand Up @@ -85,6 +87,28 @@ BUILT_SOURCES += \
ir/ir-generated.cpp \
ir/gen-tree-macro.h

######## P4 header files to install

# FIXME -- should these be in $(pkgdatadir) or $(prefix)? setup.h.in
# FIXME -- and check-install-headers: below need to be consistent
p4includedir = $(pkgdatadir)/p4include
p4_14includedir = $(pkgdatadir)/p4_14include
p4include_HEADERS += $(srcdir)/p4include/core.p4 $(srcdir)/p4include/v1model.p4

#### For testing, install headers in the build directory

all-local:
@$(MAKE) install-data pkgdatadir=$(builddir)

# FIXME -- should be a way of getting configure to do this automatcially? or automake?
setup.h: $(srcdir)/setup.h.in Makefile
@sed -e 's|@pkgdatadir[@]|$(pkgdatadir)|g' \
-e 's|@prefix[@]|$(prefix)|g' \
-e 's|@p4includedir[@]|$(p4includedir)|g' \
-e 's|@p4_14includedir[@]|$(p4_14includedir)|g' \
$< >$@
setup.o setup.lo: setup.h

################

# Front-end library
Expand All @@ -106,9 +130,6 @@ cpplint_FILES += $(noinst_HEADERS)

################ Misc custom targets

install:
echo "Installation not yet supported."

clean-local:
-rm -f $(BUILT_SOURCES) $(CLEANFILES)

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ programs.
The code and documentation are hosted in the following git repository:
https://github.com/p4lang/p4c

The code is currently alpha quality. Currently there is no way to
install the compiler.
The code is currently alpha quality.

# Dependences

Expand Down
3 changes: 2 additions & 1 deletion backends/bmv2/bmv2stf.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ def do_command(self, cmd):
elif first == "expect":
interface, data = nextWord(cmd)
data = ''.join(data.split())
self.expected.setdefault(interface, []).append(data)
if data != '':
self.expected.setdefault(interface, []).append(data)
else:
if self.options.verbose:
print("ignoring stf command:", first, cmd)
Expand Down
2 changes: 2 additions & 0 deletions backends/ebpf/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ noinst_HEADERS += \

cpplint_FILES += $(p4c_ebpf_UNIFIED) $(p4c_ebpf_NONUNIFIED)

p4include_HEADERS += $(srcdir)/%reldir%/p4include/ebpf_model.p4

# Generated by the targets below
-include ebpftests.mk

Expand Down
File renamed without changes.
5 changes: 1 addition & 4 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,5 @@ autoreconf -i
mkdir -p build # recommended folder for build
sourcedir=`pwd`
cd build
# TODO: the "prefix" is needed for finding the p4include folder.
# It should be an absolute path. This may need to change
# when we have a proper installation procedure.
../configure CXXFLAGS="-g -O0" --prefix=$sourcedir $*
../configure CXXFLAGS="-g -O0" $*
echo "### Configured for building in 'build' folder"
4 changes: 1 addition & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ AC_CHECK_LIB([rt], [clock_gettime], [], [])
AC_CHECK_LIB([gmp], [__gmpz_init], [], [AC_MSG_ERROR([GNU MP not found])])
AC_CHECK_LIB([gmpxx], [__gmpz_init], [], [AC_MSG_ERROR([GNU MP not found])])

AC_SUBST(P4INCLUDE, $prefix/p4include)

AC_CONFIG_FILES([Makefile setup.h])
AC_CONFIG_FILES([Makefile])
AX_PYTHON_MODULE([difflib], [fatal], [python])
AX_PYTHON_MODULE([shutil], [fatal], [python])
AX_PYTHON_MODULE([tempfile], [fatal], [python])
Expand Down
42 changes: 40 additions & 2 deletions frontends/common/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ limitations under the License.
*/

#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "setup.h"
#include "options.h"
Expand Down Expand Up @@ -130,6 +133,41 @@ void CompilerOptions::setInputFile() {
}
}

std::vector<const char*>* CompilerOptions::process(int argc, char* const argv[]) {
char buffer[PATH_MAX];
int len;
struct stat st;
/* find the path of the executable. We use a number of techniques that may fail
* or work on different systems, and take the first working one we find. Fall
* back to not overriding the compiled-in installation path */
if ((len = readlink("/proc/self/exe", buffer, sizeof(buffer))) > 0 ||
(len = readlink("/proc/curproc/exe", buffer, sizeof(buffer))) > 0 ||
(len = readlink("/proc/curproc/file", buffer, sizeof(buffer))) > 0 ||
(len = readlink("/proc/self/path/a.out", buffer, sizeof(buffer))) > 0) {
buffer[len] = 0;
} else if (argv[0][0] == '/') {
snprintf(buffer, sizeof(buffer), "%s", argv[0]);
} else if (strchr(argv[0], '/')) {
getcwd(buffer, sizeof(buffer));
strncat(buffer, argv[0], sizeof(buffer) - strlen(buffer) - 1);
} else if (getenv("_")) {
strncpy(buffer, getenv("_"), sizeof(buffer));
buffer[sizeof(buffer) - 1] = 0;
} else {
buffer[0] = 0; }

if (char *p = strrchr(buffer, '/')) {
++p;
snprintf(p, buffer + sizeof(buffer) - p, "p4include");
if (stat(buffer, &st) >= 0 && S_ISDIR(st.st_mode))
p4includePath = strdup(buffer);
snprintf(p, buffer + sizeof(buffer) - p, "p4_14include");
if (stat(buffer, &st) >= 0 && S_ISDIR(st.st_mode))
p4_14includePath = strdup(buffer); }

return Util::Options::process(argc, argv);
}

FILE* CompilerOptions::preprocess() {
FILE* in = nullptr;

Expand All @@ -145,8 +183,8 @@ FILE* CompilerOptions::preprocess() {
#else
std::string cmd("cpp");
#endif
cmd += cstring(" -undef -nostdinc -I") +
p4includePath + " " + preprocessor_options + " " + file;
cmd += cstring(" -undef -nostdinc") + " " + preprocessor_options
+ " -I" + (isv1() ? p4_14includePath : p4includePath) + " " + file;
if (Log::verbose())
std::cerr << "Invoking preprocessor " << std::endl << cmd << std::endl;
in = popen(cmd.c_str(), "r");
Expand Down
1 change: 1 addition & 0 deletions frontends/common/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CompilerOptions : public Util::Options {

public:
CompilerOptions();
std::vector<const char*>* process(int argc, char* const argv[]);

enum class FrontendVersion {
P4_14,
Expand Down
11 changes: 8 additions & 3 deletions frontends/p4/fromv1.0/converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,15 @@ class Rewriter : public Transform {
{ CHECK_NULL(structure); setName("Rewriter"); }

const IR::Node* preorder(IR::V1Program* global) override {
if (LOGGING(2))
dump(global);
if (LOGGING(4)) {
LOG4("#### Initial P4_14 program");
dump(global); }
prune();
return structure->create(global->srcInfo);
auto *rv = structure->create(global->srcInfo);
if (LOGGING(4)) {
LOG4("#### Generated P4_16 program");
dump(rv); }
return rv;
}
};
} // namespace
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/fromv1.0/programStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ ProgramStructure::convertAction(const IR::ActionFunction* action, cstring newNam
}

// Save the original action name in an annotation
auto annos = addNameAnnotation(action->name);
auto annos = addNameAnnotation(action->name, action->annotations);
auto result = new IR::P4Action(
action->srcInfo, newName, annos, new IR::ParameterList(params),
new IR::BlockStatement(body->srcInfo, IR::Annotations::empty, body));
Expand Down
2 changes: 1 addition & 1 deletion frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ const IR::Type* TypeInference::setTypeType(const IR::Type* type, bool learn) {
if (canon != nullptr) {
// Learn the new type
if (canon != orig && learn) {
TypeInference tc(refMap, typeMap, true);
TypeInference tc(refMap, typeMap, readOnly);
(void)canon->apply(tc);
}
auto tt = new IR::Type_Type(canon);
Expand Down
3 changes: 2 additions & 1 deletion ir/v1.def
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ class ActionFunction {
action.visit_children(v);
// DANGER -- visiting action first so type inferencing will push types to
// DANGER -- action args based on use. This is immoral.
for (auto &a : args) v.visit(a);
for (auto &a : args) v.visit(a, "arg");
v.visit(annotations, "annotations");
}
toString {
return cstring("action ") + name + " {\n" +
Expand Down
2 changes: 1 addition & 1 deletion ir/visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class SetupJoinPoints : public Inspector {
};

void ControlFlowVisitor::init_join_flows(const IR::Node *root) {
if (!dynamic_cast<Inspector *>(this))
if (!dynamic_cast<Inspector *>(static_cast<Visitor *>(this)))
BUG("joinFlows only works for Inspector passes currently, not Modifier or Transform");
if (flow_join_points)
flow_join_points->clear();
Expand Down
2 changes: 1 addition & 1 deletion lib/cstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ namespace std {
template<> struct hash<cstring> {
std::size_t operator()(const cstring& c) const {
// This implementation is good for cstring, since the strings are internalized
return hash<const char*>()(c.c_str());
return hash<const void *>()(c.c_str());
}
};
} // namespace std
Expand Down
1 change: 1 addition & 0 deletions setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ limitations under the License.
#include "setup.h"

const char* p4includePath = P4_SYSTEM_INCLUDE_PATH;
const char* p4_14includePath = P4_14_SYSTEM_INCLUDE_PATH;
4 changes: 3 additions & 1 deletion setup.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#ifndef _SETUP_H_
#define _SETUP_H_

#define P4_SYSTEM_INCLUDE_PATH "@P4INCLUDE@"
#define P4_SYSTEM_INCLUDE_PATH "@p4includedir@"
#define P4_14_SYSTEM_INCLUDE_PATH "@p4_14includedir@"
extern const char* p4includePath;
extern const char* p4_14includePath;

#endif /* _SETUP_H_ */
3 changes: 3 additions & 0 deletions testdata/p4_14_samples/counter1.stf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ add tab1 dstAddr:0xa1a2a3a4a5a6 act(port:2) = A
add tab1 dstAddr:0xb1b2b3b4b5b6 act(port:3) = B
add tab1 dstAddr:0xc1c2c3c4c5c6 act(port:5) = C

expect 2
expect 3
expect 5
check_counter cnt($A)
packet 0 a1a2a3a4a5a6 0000
wait
Expand Down
3 changes: 3 additions & 0 deletions testdata/p4_14_samples/counter2.stf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ add tab1 600 dstAddr:0xa1a2a3a4a5a6 act(port:2) = A
add tab1 400 dstAddr:0xb1b2b3b4b5b6 act(port:3) = B
add tab1 200 dstAddr:0xc1c2c3c4c5c6 act(port:5) = C

expect 2
expect 3
expect 5
check_counter cnt($A)
packet 0 a1a2a3a4a5a6 0000
wait
Expand Down
3 changes: 3 additions & 0 deletions testdata/p4_14_samples/counter3.stf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ add tab1 dstAddr:0xa1a2a3a4a5a6 act(port:2) = A
add tab1 dstAddr:0xb1b2b3b4b5b6 act(port:3) = B
add tab1 dstAddr:0xc1c2c3c4c5c6 act(port:5) = C

expect 2
expect 3
expect 5
packet 0 a1a2a3a4a5a6 0000
wait
check_counter cnt($A) bytes == 8
Expand Down
3 changes: 3 additions & 0 deletions testdata/p4_14_samples/counter4.stf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ add tab1 dstAddr:0xa1a2a3a4a5a6 act(port:2, idx:10)
add tab1 dstAddr:0xb1b2b3b4b5b6 act(port:3, idx:11)
add tab1 dstAddr:0xc1c2c3c4c5c6 act(port:5, idx:10)

expect 2
expect 3
expect 5
packet 0 a1a2a3a4a5a6 0000
wait
check_counter cntDum(10) packets == 1
Expand Down
21 changes: 21 additions & 0 deletions testdata/p4_16_errors/issue272.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2017 VMware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <core.p4>

header boolfield {
bool x;
}
6 changes: 6 additions & 0 deletions testdata/p4_16_errors_outputs/issue272.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <core.p4>

header boolfield {
bool x;
}

3 changes: 3 additions & 0 deletions testdata/p4_16_errors_outputs/issue272.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
../testdata/p4_16_errors/issue272.p4(20): error: Field x of header boolfield cannot have type bool
bool x;
^

0 comments on commit c0800ab

Please sign in to comment.