summaryrefslogtreecommitdiffhomepage
path: root/flowgraph/storage.h
blob: c2fa7c5acc771ccadf17b993982b2c3d40833e53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma once

#include "data.h"

#include <cstdint>
#include <string>
#include <vector>

namespace FlowGraph {

 // Parameter to Data class, defining where the data is stored
 // Explicitely not including size
 // But including label/data/pointer
 // -> includes identity of certain data point
 class Storage
 {
 public:
  virtual ~Storage() {} // force class to be polymorphic
 };

 class Constant: public Storage
 {
 public:
  Constant(std::vector<uint8_t> value) {} // little endian data
  const std::vector<uint8_t>& value() const { return m_value; }
 private:
  std::vector<uint8_t> m_value;
 };

 class GlobalStorage : public Storage
 {
 public:
  GlobalStorage(const std::string& name): m_name(name) {}
  const std::string& name() const { return m_name; }
 private:
  std::string m_name;
 };

 class LocalStorage : public Storage
 {
 public:
  LocalStorage(const std::string& name): m_name(name) {}
  const std::string& name() const { return m_name; }
 private:
  std::string m_name;
 };

 // Provide a context for local temporaries name generation
 class LocalScope
 {
 public:
  size_t getNewIndex() { return m_index++; }
 private:
  size_t m_index{ 0 };
 };

 // intermediate results, anonymous values
 // use generated name
 class TemporaryStorage : public Storage
 {
 public:
  TemporaryStorage(LocalScope& scope);
  const std::string& name() const { return m_name; }
 private:
  std::string m_name;
 };

 // dereferenced pointer
 class PointeeStorage : public Storage
 {
 public:
  PointeeStorage(const Data& pointer, const Data& offset): m_pointer(pointer), m_offset(offset) {
   if (pointer.type() != DataType::Pointer)
    throw std::runtime_error("Pointer argument must be a DataType::Pointer");
   if (offset.type() != DataType::Size)
    throw std::runtime_error("Offset argument must be a DataType::Size");
  }
  Data pointer() { return m_pointer; }
  Data offset() { return m_offset; }
 private:
  Data m_pointer;
  Data m_offset;
 };

}