basicblock.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SANDBOX_LINUX_SECCOMP_BPF_BASICBLOCK_H__
6#define SANDBOX_LINUX_SECCOMP_BPF_BASICBLOCK_H__
7
8#include <vector>
9
10#include "sandbox/linux/seccomp-bpf/instruction.h"
11
12
13namespace playground2 {
14
15struct BasicBlock {
16  BasicBlock();
17  ~BasicBlock();
18
19  // Our implementation of the code generator uses a "Less" operator to
20  // identify common sequences of basic blocks. This would normally be
21  // really easy to do, but STL requires us to wrap the comparator into
22  // a class. We begrudgingly add some code here that provides this wrapping.
23  template<class T> class Less {
24   public:
25    Less(const T& data, int (*cmp)(const BasicBlock *, const BasicBlock *,
26                                   const T& data))
27        : data_(data),
28          cmp_(cmp) {
29    }
30
31    bool operator() (const BasicBlock *a, const BasicBlock *b) const {
32      return cmp_(a, b, data_) < 0;
33    }
34
35   private:
36    const T& data_;
37    int (*cmp_)(const BasicBlock *, const BasicBlock *, const T&);
38  };
39
40  // Basic blocks are essentially nothing more than a set of instructions.
41  std::vector<Instruction *> instructions;
42
43  // In order to compute relative branch offsets we need to keep track of
44  // how far our block is away from the very last basic block. The "offset_"
45  // is measured in number of BPF instructions.
46  int offset;
47};
48
49}  // namespace playground2
50
51#endif  // SANDBOX_LINUX_SECCOMP_BPF_BASICBLOCK_H__
52