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(const std::function<bool(const std::shared_ptr<Symbol>&)>& function) const
58  {
59  for (auto& symbol : mSymbolEntries)
60  {
61  if (!function(symbol))
62  return false;
63  }
64  return true;
65  }
66 
67  // Find
68  std::shared_ptr<Symbol> Lookup(const std::string_view name) const;
69  //std::shared_ptr<Symbol> LookupOrCreateDummy(const std::string_view name);
70 
71  void Define(const std::string_view name, std::shared_ptr<Symbol> symbolEntry);
72  void Define(std::shared_ptr<Symbol> symbolEntry);
73  void Undefine(const std::string_view name);
74  void Undefine(std::shared_ptr<Symbol> symbolEntry);
75 
76  // Block management
77  void OpenBlock(const int32_t address);
78  int32_t CloseBlock();
79  size_t GetBlockDepth() const;
80  void EachBlock(std::function<void(const std::shared_ptr<Symbol>&)> function);
81 
82 
83  // TODO:相応しい名前か検討して下さい
84  void ExtendModule(const std::shared_ptr<Symbol>& symbol);
85 
86  void RegisterToBlock(const std::shared_ptr<Symbol>& symbolEntry);
87  void RegisterToBlock(const std::shared_ptr<TypeDescriptor>& typeDescriptor);
88 
89  std::shared_ptr<Symbol> GetLastSymbolEntryInBlock() const;
90  std::shared_ptr<TypeDescriptor> GetLastTypeDescriptorInBlock() const;
91 
92 
93 
95  int32_t OpenBlock(const bool resetMaxFrameMemoryAddress);
96  bool IsOpenBlock() const;
97 
99  // function
100  void OpenFunction(const std::shared_ptr<SyntaxNode>& node, const bool is_action);
101  void OpenFunction2(const std::shared_ptr<const Symbol>& function) const;
102  void CloseFunction(const std::shared_ptr<SyntaxNode>& node, const bool is_action);
103  bool IsFunctionOpened() const { return mIsFunctionOpened; }
104 
105  void BeginNativeFunction();
106  void CloseNativeFunction(const std::shared_ptr<Symbol>& function, const std::shared_ptr<TypeDescriptor>& type);
107 
109  // struct
111  void CommitRegistrationStructure(const std::string_view name);
112  void OpenStructure(const std::string_view name);
113  void CloseStructure();
114 
116  // actor
117  void BeginRegistrationActor(const std::shared_ptr<Symbol>& symbolEntry);
118  void CommitRegistrationActor(const std::string_view name, const std::string_view parent, const std::shared_ptr<TypeDescriptor>& type, const bool phantom);
119  bool IsActorOrStructerOpened() const;
120 
121  void OpenActor(const std::string_view name);
122  void CloseActor();
123  size_t GetNumberOfActors() const;
124 
126  // module
127  void BeginRegistrationModule(const std::shared_ptr<Symbol>& symbolEntry);
128  void CommitRegistrationModule(const std::string_view name);
129 
130  void OpenModule(const std::shared_ptr<Symbol>& symbolEntry);
131  void CloseModule(const std::string_view name);
132  bool IsModuleOpened() const { return mModuleBlockOpened; }
133 
134  void ExtendModule(const std::string_view name);
135 
136 
138  // TODO 適切な関数名を検討して下さい
139  std::shared_ptr<TypeDescriptor> GetCurrentBlockTypeDescriptor() const
140  {
141  return mBlockTypeDescriptor.top();
142  }
143 
145 
146  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;
147  void AddJoin(const std::shared_ptr<CodeGenerator>& codeGenerator, const std::shared_ptr<SyntaxNode>& level, const std::shared_ptr<SyntaxNode>& actor) const;
148 
149  void AllocateMemory(const std::shared_ptr<Symbol>& symbolEntry, std::shared_ptr<TypeDescriptor> type, Symbol::MemoryTypeId);
150 
151 
152  //const std::shared_ptr<Symbol>& symbol_get_head_symbol();
153 
154  int32_t GetStaticMemoryAddress() const;
155  void SetStaticMemoryAddress(const int32_t size);
156 
157  int32_t GetGlobalMemoryAddress() const;
158  void SetGlobalMemoryAddress(const int32_t size);
159 
160  void CheckUndefine();
161 
163  bool GenerateActorInformation(OutputStream& stream) const;
164 
165 
166 
167 
168  void symbol_open_actor_register_member(const std::shared_ptr<Symbol>& symbol);
169  void symbol_open_actor_register_member(const std::shared_ptr<TypeDescriptor>& typeDescriptor);
170 
171 
172 
173 
175  {
176  return mReturnAddressList;
177  }
178 
179  void SetReturnAddressList(const address_t returnAddressList)
180  {
181  mReturnAddressList = returnAddressList;
182  }
183 
185  // Dump
186  void Dump(std::ofstream& output) const;
187  void DumpFunctionNameFromAddress(std::ofstream& output, const int32_t address) const;
188 
190  // debug
191  void PrintHeader();
192  void PrintFooter(const std::string_view, const std::shared_ptr<TypeDescriptor>& type);
193  void PrintEntry(const std::shared_ptr<Symbol>& symbolEntry, const std::shared_ptr<TypeDescriptor>& type);
194  void PrintDummyGlobalVariable(size_t size);
195 
196  private:
197  std::shared_ptr<Symbol> CreateSymbol(const std::string_view name, const Symbol::ClassTypeId class_type);
198  std::shared_ptr<Symbol> CreateSymbolWithAddress(const std::string_view name, const Symbol::ClassTypeId class_type, const int32_t address);
199  std::shared_ptr<Symbol> CreateSymbolWithLevel(const std::string_view name, Symbol::ClassTypeId class_type, const size_t blockLevel);
200 
201 
202  bool GenerateActorEntity(OutputStream& stream, const std::shared_ptr<const Symbol>& symbol, const std::shared_ptr<const TypeDescriptor>& type) const;
203 
204  static int32_t symbol_align_size(const int32_t X, const int32_t Y)
205  {
206  return (X + Y - 1) / Y * Y;
207  }
208 
209 
210  private:
211  std::shared_ptr<CodeBuffer> mCodeBuffer;
212  std::shared_ptr<DataBuffer> mDataBuffer;
213  std::shared_ptr<StringPool> mStringPool;
214  std::shared_ptr<TypeDescriptorFactory> mTypeDescriptorFactory;
215 
216  std::unordered_map<std::string_view, std::shared_ptr<Symbol>> mHashChainTable;
217  std::vector<std::shared_ptr<Symbol>> mSymbolEntries;
218 
219  struct BlockEntry final
220  {
221  BlockEntry() = default;
222  void Set(const std::shared_ptr<Symbol>& symbolEntry)
223  {
224  mSymbolEntry = symbolEntry;
225  }
226  void Set(const std::shared_ptr<TypeDescriptor>& typeDescriptor)
227  {
228  mTypeDescriptor = typeDescriptor;
229  }
230 
231  std::shared_ptr<Symbol> mSymbolEntry;
232  std::shared_ptr<TypeDescriptor> mTypeDescriptor;
233  };
234 
235  struct BlockTable final
236  {
237  BlockEntry mHead;
238  int32_t mAllocp;
239 
240  explicit BlockTable(const int32_t allocp)
241  : mAllocp(allocp)
242  {}
243  };
244  std::stack<std::unique_ptr<BlockTable>> mBlockTable;
245 
246  std::stack<std::shared_ptr<TypeDescriptor>> mBlockTypeDescriptor;
247 
248 
249  ssize_t mActorOrStructureLevel = 0;
250  size_t mFunctionBlockLevel = 0;
251  bool mIsFunctionOpened = false;
252  bool mModuleBlockOpened = false;
253 
254 
255  int32_t mStaticMemoryAddress = 0;
256  int32_t mGlobalMemoryAddress = 0;
257  int32_t mActorMemoryAddress = 0;
258  int32_t mLocalMemoryAddress = 0;
259  int32_t mMaxLocalMemoryAddress = 0;
260 
261  int32_t mFrameSizeList = 0;
262  address_t mReturnAddressList = InvalidAddress;
263  };
264 }
Definition: OutputStream.h:15
Definition: SymbolFactory.h:33
int32_t GetGlobalMemoryAddress() const
Definition: SymbolFactory.cpp:1297
void DumpFunctionNameFromAddress(std::ofstream &output, const int32_t address) const
Definition: SymbolFactory.cpp:413
void AllocateMemory(const std::shared_ptr< Symbol > &symbolEntry, std::shared_ptr< TypeDescriptor > type, Symbol::MemoryTypeId)
Definition: SymbolFactory.cpp:1218
void OpenFunction(const std::shared_ptr< SyntaxNode > &node, const bool is_action)
Definition: SymbolFactory.cpp:485
void PrintFooter(const std::string_view, const std::shared_ptr< TypeDescriptor > &type)
Definition: SymbolFactory.cpp:1375
void PrintEntry(const std::shared_ptr< Symbol > &symbolEntry, const std::shared_ptr< TypeDescriptor > &type)
Definition: SymbolFactory.cpp:1410
void BeginNativeFunction()
Definition: SymbolFactory.cpp:662
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:1148
std::shared_ptr< TypeDescriptor > GetLastTypeDescriptorInBlock() const
Definition: SymbolFactory.cpp:394
std::shared_ptr< TypeDescriptor > GetCurrentBlockTypeDescriptor() const
Definition: SymbolFactory.h:139
void BeginRegistrationActor(const std::shared_ptr< Symbol > &symbolEntry)
Definition: SymbolFactory.cpp:830
void Destroy(const std::string_view name)
Definition: SymbolFactory.cpp:232
void OpenFunction2(const std::shared_ptr< const Symbol > &function) const
Definition: SymbolFactory.cpp:561
void SetGlobalMemoryAddress(const int32_t size)
Definition: SymbolFactory.cpp:1302
std::shared_ptr< Symbol > CreateLabel(const std::string_view name)
Definition: SymbolFactory.cpp:163
void CloseNativeFunction(const std::shared_ptr< Symbol > &function, const std::shared_ptr< TypeDescriptor > &type)
Definition: SymbolFactory.cpp:680
void CloseModule(const std::string_view name)
Definition: SymbolFactory.cpp:1104
address_t GetReturnAddressList() const
Definition: SymbolFactory.h:174
void RegisterToBlock(const std::shared_ptr< Symbol > &symbolEntry)
Definition: SymbolFactory.cpp:376
void CommitRegistrationModule(const std::string_view name)
Definition: SymbolFactory.cpp:1047
void OpenBlock(const int32_t address)
Definition: SymbolFactory.cpp:302
void SetStaticMemoryAddress(const int32_t size)
Definition: SymbolFactory.cpp:1291
void BeginRegistrationStructure()
Definition: SymbolFactory.cpp:701
void Define(const std::string_view name, std::shared_ptr< Symbol > symbolEntry)
Definition: SymbolFactory.cpp:282
bool GenerateActorInformation(OutputStream &stream) const
Definition: SymbolFactory.cpp:1444
size_t GetNumberOfActors() const
Definition: SymbolFactory.cpp:1308
void EachBlock(std::function< void(const std::shared_ptr< Symbol > &)> function)
Definition: SymbolFactory.cpp:345
void PrintHeader()
Definition: SymbolFactory.cpp:1358
void PrintDummyGlobalVariable(size_t size)
Definition: SymbolFactory.cpp:1425
void OpenStructure(const std::string_view name)
Definition: SymbolFactory.cpp:765
void AddJoin(const std::shared_ptr< CodeGenerator > &codeGenerator, const std::shared_ptr< SyntaxNode > &level, const std::shared_ptr< SyntaxNode > &actor) const
Definition: SymbolFactory.cpp:1194
bool IsOpenBlock() const
Definition: SymbolFactory.cpp:479
int32_t CloseBlock()
Definition: SymbolFactory.cpp:307
void CloseStructure()
Definition: SymbolFactory.cpp:805
bool IsValid(std::shared_ptr< Symbol > symbolEntry)
Definition: SymbolFactory.cpp:241
bool IsActorOrStructerOpened() const
Definition: SymbolFactory.cpp:932
void Dump(std::ofstream &output) const
Definition: SymbolFactory.cpp:404
void CheckUndefine()
Definition: SymbolFactory.cpp:1344
void ExtendModule(const std::shared_ptr< Symbol > &symbol)
Definition: SymbolFactory.cpp:360
void CommitRegistrationStructure(const std::string_view name)
Definition: SymbolFactory.cpp:716
int32_t GetStaticMemoryAddress() const
Definition: SymbolFactory.cpp:1286
void OpenModule(const std::shared_ptr< Symbol > &symbolEntry)
Definition: SymbolFactory.cpp:1074
void CloseActor()
Definition: SymbolFactory.cpp:979
std::shared_ptr< Symbol > Lookup(const std::string_view name) const
Definition: SymbolFactory.cpp:261
void OpenActor(const std::string_view name)
Definition: SymbolFactory.cpp:937
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:21
void Undefine(const std::string_view name)
Definition: SymbolFactory.cpp:292
size_t GetBlockDepth() const
Definition: SymbolFactory.cpp:340
void symbol_open_actor_register_member(const std::shared_ptr< Symbol > &symbol)
Definition: SymbolFactory.cpp:816
void BeginRegistrationModule(const std::shared_ptr< Symbol > &symbolEntry)
Definition: SymbolFactory.cpp:1008
~SymbolFactory()=default
bool IsModuleOpened() const
Definition: SymbolFactory.h:132
bool Each(const std::function< bool(const std::shared_ptr< Symbol > &)> &function) const
Definition: SymbolFactory.h:57
void SetReturnAddressList(const address_t returnAddressList)
Definition: SymbolFactory.h:179
std::shared_ptr< Symbol > CreateConstInt(const std::string_view name, const int32_t value)
Definition: SymbolFactory.cpp:67
bool IsFunctionOpened() const
Definition: SymbolFactory.h:103
std::shared_ptr< Symbol > CreateConstString(const std::string_view name, const std::string_view text)
Definition: SymbolFactory.cpp:109
void CommitRegistrationActor(const std::string_view name, const std::string_view parent, const std::shared_ptr< TypeDescriptor > &type, const bool phantom)
Definition: SymbolFactory.cpp:868
std::shared_ptr< Symbol > CreateType(const std::string_view name, const std::shared_ptr< TypeDescriptor > &type)
Definition: SymbolFactory.cpp:195
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:132
std::shared_ptr< Symbol > CreateFunction(const std::string_view name, const bool isActorOrStructOpened)
Definition: SymbolFactory.cpp:176
std::shared_ptr< Symbol > GetLastSymbolEntryInBlock() const
Definition: SymbolFactory.cpp:387
std::shared_ptr< Symbol > CreateConstFloat(const std::string_view name, const float value)
Definition: SymbolFactory.cpp:88
void CloseFunction(const std::shared_ptr< SyntaxNode > &node, const bool is_action)
Definition: SymbolFactory.cpp:598
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