Mana
SymbolFactory.h
Go to the documentation of this file.
1 
8 #pragma once
9 #include "../runner/common/Setup.h"
10 #include "StringPool.h"
11 #include "Symbol.h"
12 #include "TypeDescriptorFactory.h"
13 #include <functional>
14 #include <memory>
15 #include <stack>
16 #include <unordered_map>
17 #include <vector>
18 
19 namespace mana
20 {
21  class CodeBuffer;
22  class CodeGenerator;
23  class DataBuffer;
24  class StringPool;
25  class Symbol;
26  class SyntaxNode;
27  class TypeDescriptorFactory;
28 
29  /*
30  シンボルを生成します
31  */
32  class SymbolFactory final : Noncopyable
33  {
34  public:
35  // deprecated
36  bool IsValid(std::shared_ptr<Symbol> symbolEntry);
37 
38 
40  const std::shared_ptr<CodeBuffer>& codeBuffer,
41  const std::shared_ptr<DataBuffer>& dataBuffer,
42  const std::shared_ptr<StringPool>& stringPool,
43  const std::shared_ptr<TypeDescriptorFactory>& typeDescriptorFactory
44  );
45  ~SymbolFactory() = default;
46 
47  // create symbol
48  std::shared_ptr<Symbol> CreateConstInt(const std::string_view name, const int32_t value);
49  std::shared_ptr<Symbol> CreateConstFloat(const std::string_view name, const float value);
50  std::shared_ptr<Symbol> CreateConstString(const std::string_view name, const std::string_view text);
51  std::shared_ptr<Symbol> CreateVariable(const std::string_view name, const std::shared_ptr<TypeDescriptor>& type, const bool staticVariable, const bool isBlockOpened, const bool isFunctionOpened);
52  std::shared_ptr<Symbol> CreateLabel(const std::string_view name);
53  std::shared_ptr<Symbol> CreateFunction(const std::string_view name, const bool isActorOrStructOpened);
54  std::shared_ptr<Symbol> CreateType(const std::string_view name, const std::shared_ptr<TypeDescriptor>& type);
55  void Destroy(const std::string_view name);
56 
57  bool Each(std::function<bool(const std::shared_ptr<Symbol>&)> function)
58  {
59  for (auto& symbol : mSymbolEntries)
60  {
61  if (!function(symbol))
62  return false;
63  }
64  return true;
65  }
66 
67  bool Each(std::function<bool(const std::shared_ptr<const Symbol>&)> function) const
68  {
69  for (auto& symbol : mSymbolEntries)
70  {
71  if (!function(symbol))
72  return false;
73  }
74  return true;
75  }
76 
77  // Find
78  std::shared_ptr<Symbol> Lookup(const std::string_view name) const;
79  //std::shared_ptr<Symbol> LookupOrCreateDummy(const std::string_view name);
80 
81  void Define(const std::string_view name, std::shared_ptr<Symbol> symbolEntry);
82  void Define(std::shared_ptr<Symbol> symbolEntry);
83  void Undefine(const std::string_view name);
84  void Undefine(std::shared_ptr<Symbol> symbolEntry);
85 
86  // Block management
87  void OpenBlock(const int32_t address);
88  int32_t CloseBlock();
89  size_t GetBlockDepth() const;
90  void EachBlock(std::function<void(const std::shared_ptr<Symbol>&)> function);
91 
92 
93  // TODO:相応しい名前か検討して下さい
94  void ExtendModule(const std::shared_ptr<Symbol>& symbol);
95 
96  void RegisterToBlock(const std::shared_ptr<Symbol>& symbolEntry);
97  void RegisterToBlock(const std::shared_ptr<TypeDescriptor>& typeDescriptor);
98 
99  std::shared_ptr<Symbol> GetLastSymbolEntryInBlock() const;
100  std::shared_ptr<TypeDescriptor> GetLastTypeDescriptorInBlock() const;
101 
102 
103 
105  int32_t OpenBlock(const bool resetMaxFrameMemoryAddress);
106  bool IsOpenBlock() const;
107 
109  // function
110  void OpenFunction(const std::shared_ptr<SyntaxNode>& node, const bool is_action);
111  void OpenFunction2(const std::shared_ptr<const Symbol>& function) const;
112  void CloseFunction(const std::shared_ptr<SyntaxNode>& node, const bool is_action);
113  bool IsFunctionOpened() const { return mIsFunctionOpened; }
114 
115  void BeginNativeFunction();
116  void CloseNativeFunction(const std::shared_ptr<Symbol>& function, const std::shared_ptr<TypeDescriptor>& type);
117 
119  // struct
121  void CommitRegistrationStructure(const std::string_view name);
122  void OpenStructure(const std::string_view name);
123  void CloseStructure();
124 
126  // actor
127  void BeginRegistrationActor(const std::shared_ptr<Symbol>& symbolEntry);
128  void CommitRegistrationActor(const std::string_view name, const std::string_view parent, const std::shared_ptr<TypeDescriptor>& type, const bool phantom);
129  bool IsActorOrStructerOpened() const;
130 
131  void OpenActor(const std::string_view name);
132  void CloseActor();
133  size_t GetNumberOfActors() const;
134 
136  // module
137  void BeginRegistrationModule(const std::shared_ptr<Symbol>& symbolEntry);
138  void CommitRegistrationModule(const std::string_view name);
139 
140  void OpenModule(const std::shared_ptr<Symbol>& symbolEntry);
141  void CloseModule(const std::string_view name);
142  bool IsModuleOpened() const { return mModuleBlockOpened; }
143 
144  void ExtendModule(const std::string_view name);
145 
146 
148  // TODO 適切な関数名を検討して下さい
149  std::shared_ptr<TypeDescriptor> GetCurrentBlockTypeDescriptor() const
150  {
151  return mBlockTypeDescriptor.top();
152  }
153 
155 
156  void AddRequest(const std::shared_ptr<CodeGenerator>& codeGenerator, const IntermediateLanguage opcode, const std::shared_ptr<SyntaxNode>& level, const std::shared_ptr<SyntaxNode>& actor, const std::string_view action) const;
157  void AddJoin(const std::shared_ptr<CodeGenerator>& codeGenerator, const std::shared_ptr<SyntaxNode>& level, const std::shared_ptr<SyntaxNode>& actor) const;
158 
159  void AllocateMemory(const std::shared_ptr<Symbol>& symbolEntry, std::shared_ptr<TypeDescriptor> type, Symbol::MemoryTypeId);
160 
161 
162  //const std::shared_ptr<Symbol>& symbol_get_head_symbol();
163 
164  int32_t GetStaticMemoryAddress() const;
165  void SetStaticMemoryAddress(const int32_t size);
166 
167  int32_t GetGlobalMemoryAddress() const;
168  void SetGlobalMemoryAddress(const int32_t size);
169 
170  void CheckUndefine();
171 
173  bool GenerateActorInformation(OutputStream& stream) const;
174 
175 
176 
177 
178  void symbol_open_actor_register_member(const std::shared_ptr<Symbol>& symbol);
179  void symbol_open_actor_register_member(const std::shared_ptr<TypeDescriptor>& typeDescriptor);
180 
181 
182 
183 
185  {
186  return mReturnAddressList;
187  }
188 
189  void SetReturnAddressList(const address_t returnAddressList)
190  {
191  mReturnAddressList = returnAddressList;
192  }
193 
195  // Dump
196  void Dump(std::ofstream& output) const;
197  void DumpFunctionNameFromAddress(std::ofstream& output, const int32_t address) const;
198 
200  // debug
201  void PrintHeader();
202  void PrintFooter(const std::string_view, const std::shared_ptr<TypeDescriptor>& type);
203  void PrintEntry(const std::shared_ptr<Symbol>& symbolEntry, const std::shared_ptr<TypeDescriptor>& type);
204  void PrintDummyGlobalVariable(size_t size);
205 
206  private:
207  std::shared_ptr<Symbol> CreateSymbol(const std::string_view name, const Symbol::ClassTypeId class_type);
208  std::shared_ptr<Symbol> CreateSymbolWithAddress(const std::string_view name, const Symbol::ClassTypeId class_type, const int32_t address);
209  std::shared_ptr<Symbol> CreateSymbolWithLevel(const std::string_view name, Symbol::ClassTypeId class_type, const size_t blockLevel);
210 
211 
212  bool GenerateActorEntity(OutputStream& stream, const std::shared_ptr<const Symbol>& symbol, const std::shared_ptr<const TypeDescriptor>& type) const;
213 
214  static int32_t symbol_align_size(const int32_t X, const int32_t Y)
215  {
216  return (X + Y - 1) / Y * Y;
217  }
218 
219 
220  private:
221  std::shared_ptr<CodeBuffer> mCodeBuffer;
222  std::shared_ptr<DataBuffer> mDataBuffer;
223  std::shared_ptr<StringPool> mStringPool;
224  std::shared_ptr<TypeDescriptorFactory> mTypeDescriptorFactory;
225 
226  std::unordered_map<std::string_view, std::shared_ptr<Symbol>> mHashChainTable;
227  std::vector<std::shared_ptr<Symbol>> mSymbolEntries;
228 
229  struct BlockEntry final
230  {
231  BlockEntry() = default;
232  void Set(const std::shared_ptr<Symbol>& symbolEntry)
233  {
234  mSymbolEntry = symbolEntry;
235  }
236  void Set(const std::shared_ptr<TypeDescriptor>& typeDescriptor)
237  {
238  mTypeDescriptor = typeDescriptor;
239  }
240 
241  std::shared_ptr<Symbol> mSymbolEntry;
242  std::shared_ptr<TypeDescriptor> mTypeDescriptor;
243  };
244 
245  struct BlockTable final
246  {
247  BlockEntry mHead;
248  int32_t mAllocp;
249 
250  explicit BlockTable(const int32_t allocp)
251  : mAllocp(allocp)
252  {}
253  };
254  std::stack<std::unique_ptr<BlockTable>> mBlockTable;
255 
256  std::stack<std::shared_ptr<TypeDescriptor>> mBlockTypeDescriptor;
257 
258 
259  ssize_t mActorOrStructureLevel = 0;
260  size_t mFunctionBlockLevel = 0;
261  bool mIsFunctionOpened = false;
262  bool mModuleBlockOpened = false;
263 
264 
265  int32_t mStaticMemoryAddress = 0;
266  int32_t mGlobalMemoryAddress = 0;
267  int32_t mActorMemoryAddress = 0;
268  int32_t mLocalMemoryAddress = 0;
269  int32_t mMaxLocalMemoryAddress = 0;
270 
271  int32_t mFrameSizeList = 0;
272  address_t mReturnAddressList = InvalidAddress;
273  };
274 }
Definition: OutputStream.h:15
Definition: SymbolFactory.h:33
int32_t GetGlobalMemoryAddress() const
Definition: SymbolFactory.cpp:1286
bool Each(std::function< bool(const std::shared_ptr< const Symbol > &)> function) const
Definition: SymbolFactory.h:67
void DumpFunctionNameFromAddress(std::ofstream &output, const int32_t address) const
Definition: SymbolFactory.cpp:412
void AllocateMemory(const std::shared_ptr< Symbol > &symbolEntry, std::shared_ptr< TypeDescriptor > type, Symbol::MemoryTypeId)
Definition: SymbolFactory.cpp:1207
void OpenFunction(const std::shared_ptr< SyntaxNode > &node, const bool is_action)
Definition: SymbolFactory.cpp:474
void PrintFooter(const std::string_view, const std::shared_ptr< TypeDescriptor > &type)
Definition: SymbolFactory.cpp:1364
void PrintEntry(const std::shared_ptr< Symbol > &symbolEntry, const std::shared_ptr< TypeDescriptor > &type)
Definition: SymbolFactory.cpp:1399
void BeginNativeFunction()
Definition: SymbolFactory.cpp:651
void AddRequest(const std::shared_ptr< CodeGenerator > &codeGenerator, const IntermediateLanguage opcode, const std::shared_ptr< SyntaxNode > &level, const std::shared_ptr< SyntaxNode > &actor, const std::string_view action) const
Definition: SymbolFactory.cpp:1137
std::shared_ptr< TypeDescriptor > GetLastTypeDescriptorInBlock() const
Definition: SymbolFactory.cpp:393
std::shared_ptr< TypeDescriptor > GetCurrentBlockTypeDescriptor() const
Definition: SymbolFactory.h:149
void BeginRegistrationActor(const std::shared_ptr< Symbol > &symbolEntry)
Definition: SymbolFactory.cpp:819
void Destroy(const std::string_view name)
Definition: SymbolFactory.cpp:231
void OpenFunction2(const std::shared_ptr< const Symbol > &function) const
Definition: SymbolFactory.cpp:550
void SetGlobalMemoryAddress(const int32_t size)
Definition: SymbolFactory.cpp:1291
std::shared_ptr< Symbol > CreateLabel(const std::string_view name)
Definition: SymbolFactory.cpp:162
void CloseNativeFunction(const std::shared_ptr< Symbol > &function, const std::shared_ptr< TypeDescriptor > &type)
Definition: SymbolFactory.cpp:669
void CloseModule(const std::string_view name)
Definition: SymbolFactory.cpp:1093
address_t GetReturnAddressList() const
Definition: SymbolFactory.h:184
void RegisterToBlock(const std::shared_ptr< Symbol > &symbolEntry)
Definition: SymbolFactory.cpp:375
void CommitRegistrationModule(const std::string_view name)
Definition: SymbolFactory.cpp:1036
void OpenBlock(const int32_t address)
Definition: SymbolFactory.cpp:301
void SetStaticMemoryAddress(const int32_t size)
Definition: SymbolFactory.cpp:1280
void BeginRegistrationStructure()
Definition: SymbolFactory.cpp:690
void Define(const std::string_view name, std::shared_ptr< Symbol > symbolEntry)
Definition: SymbolFactory.cpp:281
bool GenerateActorInformation(OutputStream &stream) const
Definition: SymbolFactory.cpp:1433
size_t GetNumberOfActors() const
Definition: SymbolFactory.cpp:1297
void EachBlock(std::function< void(const std::shared_ptr< Symbol > &)> function)
Definition: SymbolFactory.cpp:344
void PrintHeader()
Definition: SymbolFactory.cpp:1347
void PrintDummyGlobalVariable(size_t size)
Definition: SymbolFactory.cpp:1414
void OpenStructure(const std::string_view name)
Definition: SymbolFactory.cpp:754
bool Each(std::function< bool(const std::shared_ptr< Symbol > &)> function)
Definition: SymbolFactory.h:57
void AddJoin(const std::shared_ptr< CodeGenerator > &codeGenerator, const std::shared_ptr< SyntaxNode > &level, const std::shared_ptr< SyntaxNode > &actor) const
Definition: SymbolFactory.cpp:1183
bool IsOpenBlock() const
Definition: SymbolFactory.cpp:468
int32_t CloseBlock()
Definition: SymbolFactory.cpp:306
void CloseStructure()
Definition: SymbolFactory.cpp:794
bool IsValid(std::shared_ptr< Symbol > symbolEntry)
Definition: SymbolFactory.cpp:240
bool IsActorOrStructerOpened() const
Definition: SymbolFactory.cpp:921
void Dump(std::ofstream &output) const
Definition: SymbolFactory.cpp:403
void CheckUndefine()
Definition: SymbolFactory.cpp:1333
void ExtendModule(const std::shared_ptr< Symbol > &symbol)
Definition: SymbolFactory.cpp:359
void CommitRegistrationStructure(const std::string_view name)
Definition: SymbolFactory.cpp:705
int32_t GetStaticMemoryAddress() const
Definition: SymbolFactory.cpp:1275
void OpenModule(const std::shared_ptr< Symbol > &symbolEntry)
Definition: SymbolFactory.cpp:1063
void CloseActor()
Definition: SymbolFactory.cpp:968
std::shared_ptr< Symbol > Lookup(const std::string_view name) const
Definition: SymbolFactory.cpp:260
void OpenActor(const std::string_view name)
Definition: SymbolFactory.cpp:926
SymbolFactory(const std::shared_ptr< CodeBuffer > &codeBuffer, const std::shared_ptr< DataBuffer > &dataBuffer, const std::shared_ptr< StringPool > &stringPool, const std::shared_ptr< TypeDescriptorFactory > &typeDescriptorFactory)
Definition: SymbolFactory.cpp:20
void Undefine(const std::string_view name)
Definition: SymbolFactory.cpp:291
size_t GetBlockDepth() const
Definition: SymbolFactory.cpp:339
void symbol_open_actor_register_member(const std::shared_ptr< Symbol > &symbol)
Definition: SymbolFactory.cpp:805
void BeginRegistrationModule(const std::shared_ptr< Symbol > &symbolEntry)
Definition: SymbolFactory.cpp:997
~SymbolFactory()=default
bool IsModuleOpened() const
Definition: SymbolFactory.h:142
void SetReturnAddressList(const address_t returnAddressList)
Definition: SymbolFactory.h:189
std::shared_ptr< Symbol > CreateConstInt(const std::string_view name, const int32_t value)
Definition: SymbolFactory.cpp:66
bool IsFunctionOpened() const
Definition: SymbolFactory.h:113
std::shared_ptr< Symbol > CreateConstString(const std::string_view name, const std::string_view text)
Definition: SymbolFactory.cpp:108
void CommitRegistrationActor(const std::string_view name, const std::string_view parent, const std::shared_ptr< TypeDescriptor > &type, const bool phantom)
Definition: SymbolFactory.cpp:857
std::shared_ptr< Symbol > CreateType(const std::string_view name, const std::shared_ptr< TypeDescriptor > &type)
Definition: SymbolFactory.cpp:194
std::shared_ptr< Symbol > CreateVariable(const std::string_view name, const std::shared_ptr< TypeDescriptor > &type, const bool staticVariable, const bool isBlockOpened, const bool isFunctionOpened)
Definition: SymbolFactory.cpp:131
std::shared_ptr< Symbol > CreateFunction(const std::string_view name, const bool isActorOrStructOpened)
Definition: SymbolFactory.cpp:175
std::shared_ptr< Symbol > GetLastSymbolEntryInBlock() const
Definition: SymbolFactory.cpp:386
std::shared_ptr< Symbol > CreateConstFloat(const std::string_view name, const float value)
Definition: SymbolFactory.cpp:87
void CloseFunction(const std::shared_ptr< SyntaxNode > &node, const bool is_action)
Definition: SymbolFactory.cpp:587
ClassTypeId
Definition: Symbol.h:25
MemoryTypeId
Definition: Symbol.h:62
Definition: CodeBuffer.cpp:12
std::uint32_t address_t
Definition: Type.h:31
IntermediateLanguage
Definition: FileFormat.h:80
Definition: Noncopyable.h:18