• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 PLUMgrid, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "parser.h"
18 #include "type_check.h"
19 #include "codegen_llvm.h"
20 #include "loader.h"
21 
22 using std::string;
23 using std::unique_ptr;
24 using std::vector;
25 
26 namespace ebpf {
27 
BLoader(unsigned flags)28 BLoader::BLoader(unsigned flags) : flags_(flags) {
29   (void)flags_;
30 }
31 
~BLoader()32 BLoader::~BLoader() {
33 }
34 
parse(llvm::Module * mod,const string & filename,const string & proto_filename,TableStorage & ts,const string & id,const std::string & maps_ns)35 int BLoader::parse(llvm::Module *mod, const string &filename, const string &proto_filename,
36                    TableStorage &ts, const string &id, const std::string &maps_ns) {
37   int rc;
38 
39   proto_parser_ = make_unique<ebpf::cc::Parser>(proto_filename);
40   rc = proto_parser_->parse();
41   if (rc) {
42     fprintf(stderr, "In file: %s\n", filename.c_str());
43     return rc;
44   }
45 
46   parser_ = make_unique<ebpf::cc::Parser>(filename);
47   rc = parser_->parse();
48   if (rc) {
49     fprintf(stderr, "In file: %s\n", filename.c_str());
50     return rc;
51   }
52 
53   //ebpf::cc::Printer printer(stderr);
54   //printer.visit(parser_->root_node_);
55 
56   ebpf::cc::TypeCheck type_check(parser_->scopes_.get(), proto_parser_->scopes_.get());
57   auto ret = type_check.visit(parser_->root_node_);
58   if (ret.code() != 0 || ret.msg().size()) {
59     fprintf(stderr, "Type error @line=%d: %s\n", ret.code(), ret.msg().c_str());
60     return -1;
61   }
62 
63   codegen_ = ebpf::make_unique<ebpf::cc::CodegenLLVM>(mod, parser_->scopes_.get(), proto_parser_->scopes_.get());
64   ret = codegen_->visit(parser_->root_node_, ts, id, maps_ns);
65   if (ret.code() != 0 || ret.msg().size()) {
66     fprintf(stderr, "Codegen error @line=%d: %s\n", ret.code(), ret.msg().c_str());
67     return ret.code();
68   }
69 
70   return 0;
71 }
72 
73 }  // namespace ebpf
74