Mana
FileFormat.h
Go to the documentation of this file.
1 
8 #pragma once
9 #include "Platform.h"
10 
11 #if MANA_BUILD_TARGET == MANA_BUILD_DEBUG
12 #include "Assert.h"
13 #endif
14 
15 #include <array>
16 #include <string_view>
17 
18 namespace mana
19 {
20  static constexpr const char* Signature = "MaNa";
21  static constexpr uint8_t MajorVersion = 0;
22  static constexpr uint8_t MinorVersion = 13;
23 
27  struct FileHeader
28  {
29  char mHeader[4];
30  uint8_t mMajorVersion;
31  uint8_t mMinorVersion;
32  uint8_t mPad;
33  enum Flag : uint8_t
34  {
38  Compiled
39  };
40  uint8_t mFlag;
41  uint32_t mNumberOfActors;
47  };
48 
53  {
54  enum Flag : uint8_t
55  {
56  Phantom
57  };
58  uint8_t mFlag;
59 
61  uint16_t mNumberOfActions;
62  uint32_t mName;
63  uint32_t mVariableSize;
64  };
65 
70  {
71  uint32_t mName;
72  uint32_t mAddress;
73  };
74 
78  enum class IntermediateLanguage : uint8_t
79  {
80  /* thread */
81  Restart = 0, /* 00 */
82  Halt, /* 01 */
83  Yield, /* 02 */
84  NonPreEmptive, /* 03 */
85  PreEmptive, /* 04 */
86 
87  /* jump */
88  BranchEqual, /* 05 */
89  BranchNotEqual, /* 06 */
90  Branch, /* 07 */
91  BranchSubRoutine, /* 08 */
92  Call, /* 09 */
93  Request, /* 0A */
94  RequestWaitStarting, /* 0B */
95  RequestWaitEnded, /* 0C */
96  Join, /* 0D */
97  Comply, /* 0E */
98  Refuse, /* 0F */
99  LoadReturnAddress, /* 10 */
100  SaveReturnAddress, /* 11 */
101  ReturnFromFunction, /* 12 */
102  ReturnFromAction, /* 13 */
103  Rollback, /* 14 */
104 
105  /* constant */
106  PushZeroInteger, /* 15 */
107  PushZeroFloat, /* 16 */
108  PushChar, /* 17 */
109  PushShort, /* 18 */
110  PushInteger, /* 19 */
111  PushSize, /* 19 */
112  PushFloat, /* 1A */
113  PushString, /* 1B */
114  PushPriority, /* 1C */
115  PushActor, /* 1D */
116  PushSelf, /* 1E */
117  PushSender, /* 1F */
118 
119  /* stack */
120  Allocate, /* 20 */
121  Free, /* 21 */
122  Duplicate, /* 22 */
123  DuplicateData, /* 23 */
124  Remove, /* 24 */
125  RemoveData, /* 25 */
126  LoadStaticAddress, /* 26 */
127  LoadGlobalAddress, /* 27 */
128  LoadFrameAddress, /* 28 */
129  LoadSelfAddress, /* 29 */
130 
131  /* memory operation */
132  LoadChar, /* 2A */
133  LoadShort, /* 2B */
134  LoadInteger, /* 2C */
135  LoadFloat, /* 2D */
136  LoadReference, /* 2E */
137  LoadData, /* 2F */
138  StoreChar, /* 30 */
139  StoreShort, /* 31 */
140  StoreInteger, /* 32 */
141  StoreFloat, /* 33 */
142  StoreReference, /* 34 */
143  StoreData, /* 35 */
144 
145  /* calculation */
146  AddInteger, /* 36 */
147  DivideInteger, /* 37 */
148  MinusInteger, /* 38 */
149  ModInteger, /* 39 */
150  MultiInteger, /* 3A */
151  SubtractInteger, /* 3B */
152 
153  AddFloat, /* 3C */
154  DivideFloat, /* 3D */
155  MinusFloat, /* 3E */
156  ModFloat, /* 3F */
157  MultiFloat, /* 40 */
158  SubtractFloat, /* 41 */
159 
160  And, /* 42 */
161  ExclusiveOr, /* 43 */
162  LogicalAnd, /* 44 */
163  LogicalOr, /* 45 */
164  LogicalNot, /* 46 */
165  Not, /* 47 */
166  Or, /* 48 */
167  ShiftLeft, /* 49 */
168  ShiftRight, /* 4A */
169 
170  CastIntegerToFloat, /* 4B */
171  CastFloatToInteger, /* 4C */
172 
173  /* compare and branch */
174  CompareEqualInteger, /* 4D */
175  CompareNotEqualInteger, /* 4E */
177  CompareGreaterInteger, /* 50 */
178  CompareLessEqualInteger, /* 51 */
179  CompareLessInteger, /* 52 */
180 
181  CompareEqualFloat, /* 53 */
182  CompareNotEqualFloat, /* 54 */
183  CompareGreaterEqualFloat, /* 55 */
184  CompareGreaterFloat, /* 56 */
185  CompareLessEqualFloat, /* 57 */
186  CompareLessFloat, /* 58 */
187 
188  CompareEqualData, /* 59 */
189  CompareNotEqualData, /* 5A */
190  CompareGreaterEqualData, /* 5B */
191  CompareGreaterData, /* 5C */
192  CompareLessEqualData, /* 5D */
193  CompareLessData, /* 5E */
194 
195  /* utility */
196  Print, /* 5F */
197 
198  /* under discussion */
199  DynamicRequest, /* 60 */
201  DynamicRequestWaitEnded, /* 62 */
202  };
203 
204  static constexpr address_t IntermediateLanguageSize = static_cast<address_t>(IntermediateLanguage::DynamicRequestWaitEnded) + 1;
205 
207  {
208  uint8_t mSize;
209 #if MANA_BUILD_TARGET == MANA_BUILD_DEBUG
210  IntermediateLanguage mIntermediateLanguage;
211 #endif
212  };
213 
214 #if MANA_BUILD_TARGET == MANA_BUILD_DEBUG
215 #define MANA_FILE_FORMAT_DEBUG_PARAMETER(code, size) (1 + (size)), (IntermediateLanguage::code)
216 #else
217 #define MANA_FILE_FORMAT_DEBUG_PARAMETER(code, size) (1 + (size))
218 #endif
219 
221  {
222  static constexpr std::array<IntermediateLanguageProperty, static_cast<address_t>(IntermediateLanguageSize)> properties =
223  {
224  {
225  // thread
231 
232  // jump
237  { MANA_FILE_FORMAT_DEBUG_PARAMETER(Call, sizeof(address_t) + sizeof(int16_t) + sizeof(int16_t) /* + (program[6] * sizeof(int16_t))*/)}, // 引数によってサイズが変わる
249 
250  // constant
253  { MANA_FILE_FORMAT_DEBUG_PARAMETER(PushChar, sizeof(int8_t)) },
254  { MANA_FILE_FORMAT_DEBUG_PARAMETER(PushShort, sizeof(int16_t)) },
255  { MANA_FILE_FORMAT_DEBUG_PARAMETER(PushInteger, sizeof(int32_t)) },
257  { MANA_FILE_FORMAT_DEBUG_PARAMETER(PushFloat, sizeof(float)) },
263 
264  // stack
275 
276  // memory operation
289 
290  // calculation
297 
304 
314 
317 
318  // compare and branch
325 
332 
339 
340  // utility
342 
343  // under discussion
347  }
348  };
349 
350  const IntermediateLanguageProperty& property = properties.at(static_cast<size_t>(code));
351 #if MANA_BUILD_TARGET == MANA_BUILD_DEBUG
352  MANA_ASSERT(property.mIntermediateLanguage == code);
353 #endif
354  return property;
355  }
356 
357 #undef MANA_FILE_FORMAT_DEBUG_PARAMETER
358 
359  inline address_t GetInstructionSize(const void* codeBuffer, const address_t index)
360  {
361  const uint8_t* program = &static_cast<const uint8_t*>(codeBuffer)[index];
362  const IntermediateLanguage code = static_cast<IntermediateLanguage>(*program);
363  if (IntermediateLanguage::Call == code)
364  {
365  // Size varies depending on the argument.
366  return sizeof(address_t) + sizeof(int16_t) + sizeof(int16_t) + (program[6] * sizeof(int16_t));
367  }
368  else
369  {
371  }
372  }
373 }
#define MANA_ASSERT(expression)
Definition: Assert.h:30
#define MANA_FILE_FORMAT_DEBUG_PARAMETER(code, size)
Definition: FileFormat.h:217
Definition: CodeBuffer.cpp:12
std::uint32_t address_t
Definition: Type.h:30
IntermediateLanguage
Definition: FileFormat.h:79
const IntermediateLanguageProperty & GetIntermediateLanguageProperty(const IntermediateLanguage code)
Definition: FileFormat.h:220
address_t GetInstructionSize(const void *codeBuffer, const address_t index)
Definition: FileFormat.h:359
Definition: FileFormat.h:70
uint32_t mName
Definition: FileFormat.h:71
uint32_t mAddress
Definition: FileFormat.h:72
Definition: FileFormat.h:53
uint32_t mVariableSize
インスタンス変数サイズ
Definition: FileFormat.h:63
uint16_t mNumberOfActions
アクション数
Definition: FileFormat.h:61
uint8_t mFlag
Definition: FileFormat.h:58
uint32_t mName
アクター名
Definition: FileFormat.h:62
uint8_t mNumberOfInstances
インスタンス数(未使用)
Definition: FileFormat.h:60
Flag
Definition: FileFormat.h:55
@ Phantom
フラグ:ファントム
Definition: FileFormat.h:56
Definition: FileFormat.h:28
uint8_t mMinorVersion
Definition: FileFormat.h:31
char mHeader[4]
Definition: FileFormat.h:29
uint8_t mPad
Definition: FileFormat.h:32
uint8_t mMajorVersion
Definition: FileFormat.h:30
uint32_t mSizeOfInstructionPool
Definition: FileFormat.h:43
uint32_t mSizeOfStaticMemory
Definition: FileFormat.h:44
uint32_t mNumberOfActors
Definition: FileFormat.h:41
uint32_t mSizeOfConstantPool
Definition: FileFormat.h:42
uint8_t mFlag
Definition: FileFormat.h:40
uint32_t mSizeOfGlobalMemory
Definition: FileFormat.h:45
uint32_t mRandomSeedNumber
Definition: FileFormat.h:46
Flag
Definition: FileFormat.h:34
@ Compiled
フラグ:コンパイル済み
Definition: FileFormat.h:38
@ Resource
フラグ:リソースセクションあり
Definition: FileFormat.h:35
@ Is64Bit
フラグ:64ビットでコンパイルされた
Definition: FileFormat.h:36
@ BigEndian
フラグ:ビックエンディアンでコンパイルされた
Definition: FileFormat.h:37
Definition: FileFormat.h:207
uint8_t mSize
Definition: FileFormat.h:208