Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * ===================================================================== |
| 3 | * Copyright (c) 2012, PLUMgrid, http://plumgrid.com |
| 4 | * |
| 5 | * This source is subject to the PLUMgrid License. |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF |
| 9 | * ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
| 10 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A |
| 11 | * PARTICULAR PURPOSE. |
| 12 | * |
| 13 | * PLUMgrid confidential information, delete if you are not the |
| 14 | * intended recipient. |
| 15 | * |
| 16 | * ===================================================================== |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <vector> |
| 23 | #include <string> |
| 24 | #include <set> |
| 25 | |
| 26 | #include "cc/node.h" |
| 27 | #include "cc/scope.h" |
| 28 | |
| 29 | namespace ebpf { |
| 30 | namespace cc { |
| 31 | |
| 32 | using std::vector; |
| 33 | using std::string; |
| 34 | using std::set; |
| 35 | |
| 36 | class CodegenC : public Visitor { |
| 37 | public: |
| 38 | CodegenC(FILE* out, Scopes::Ptr scopes, Scopes::Ptr proto_scopes, bool use_pre_header) |
| 39 | : out_(out), indent_(0), tmp_reg_index_(0), scopes_(scopes), |
| 40 | proto_scopes_(proto_scopes), use_pre_header_(use_pre_header) {} |
| 41 | |
| 42 | #define VISIT(type, func) virtual void visit_##func(type* n); |
| 43 | EXPAND_NODES(VISIT) |
| 44 | #undef VISIT |
| 45 | |
| 46 | virtual int visit(Node* n); |
| 47 | |
| 48 | void emit_table_lookup(MethodCallExprNode* n); |
| 49 | void emit_table_update(MethodCallExprNode* n); |
| 50 | void emit_table_delete(MethodCallExprNode* n); |
| 51 | void emit_channel_push(MethodCallExprNode* n); |
| 52 | void emit_channel_push_generic(MethodCallExprNode* n); |
| 53 | void emit_log(MethodCallExprNode* n); |
| 54 | void emit_packet_forward(MethodCallExprNode* n); |
| 55 | void emit_packet_replicate(MethodCallExprNode* n); |
| 56 | void emit_packet_clone_forward(MethodCallExprNode* n); |
| 57 | void emit_packet_forward_self(MethodCallExprNode* n); |
| 58 | void emit_packet_drop(MethodCallExprNode* n); |
| 59 | void emit_packet_broadcast(MethodCallExprNode* n); |
| 60 | void emit_packet_multicast(MethodCallExprNode* n); |
| 61 | void emit_packet_push_header(MethodCallExprNode* n); |
| 62 | void emit_packet_pop_header(MethodCallExprNode* n); |
| 63 | void emit_packet_push_vlan(MethodCallExprNode* n); |
| 64 | void emit_packet_pop_vlan(MethodCallExprNode* n); |
| 65 | void emit_packet_rewrite_field(MethodCallExprNode* n); |
| 66 | void emit_atomic_add(MethodCallExprNode* n); |
| 67 | void emit_cksum(MethodCallExprNode* n); |
| 68 | void emit_incr_cksum_u16(MethodCallExprNode* n); |
| 69 | void emit_incr_cksum_u32(MethodCallExprNode* n); |
| 70 | void emit_lb_hash(MethodCallExprNode* n); |
| 71 | void emit_sizeof(MethodCallExprNode* n); |
| 72 | void emit_get_usec_time(MethodCallExprNode* n); |
| 73 | void emit_forward_to_vnf(MethodCallExprNode* n); |
| 74 | void emit_forward_to_group(MethodCallExprNode* n); |
| 75 | void print_parser(); |
| 76 | void print_timer(); |
| 77 | void print_header(); |
| 78 | void print_footer(); |
| 79 | |
| 80 | private: |
| 81 | void indent(); |
| 82 | |
| 83 | template <typename... Args> void emitln(const char *fmt, Args&&... params); |
| 84 | template <typename... Args> void lnemit(const char *fmt, Args&&... params); |
| 85 | template <typename... Args> void emit(const char *fmt, Args&&... params); |
| 86 | void emitln(const char *s); |
| 87 | void lnemit(const char *s); |
| 88 | void emit(const char *s); |
| 89 | void emit_comment(Node* n); |
| 90 | |
| 91 | FILE* out_; |
| 92 | int indent_; |
| 93 | int tmp_reg_index_; |
| 94 | Scopes::Ptr scopes_; |
| 95 | Scopes::Ptr proto_scopes_; |
| 96 | bool use_pre_header_; |
| 97 | vector<vector<string> > free_instructions_; |
| 98 | vector<string> table_inits_; |
| 99 | map<string, string> proto_rewrites_; |
| 100 | }; |
| 101 | |
| 102 | } // namespace cc |
| 103 | } // namespace ebpf |