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