fasmcpp  0.1
fasmcpp.h
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 #include <unordered_map>
6 
7 #include "fasmIO.h"
8 
9 class AssemblyKey;
10 
12 namespace fasmcpp
13 {
15  namespace size_multipliers
16  {
22  constexpr size_t operator""_KB(unsigned long long int size)
23  {
24  return static_cast<size_t>(size * 1024);
25  }
26 
32  constexpr size_t operator""_KB(long double size)
33  {
34  return static_cast<size_t>(size * 1024);
35  }
36 
42  constexpr size_t operator""_MB(unsigned long long int size)
43  {
44  return static_cast<size_t>(size * 1024 * 1024);
45  }
46 
52  constexpr size_t operator""_MB(long double size)
53  {
54  return static_cast<size_t>(size * 1024 * 1024);
55  }
56 
62  constexpr size_t operator""_GB(unsigned long long int size)
63  {
64  return static_cast<size_t>(size * 1024 * 1024 * 1024);
65  }
66 
72  constexpr size_t operator""_GB(long double size)
73  {
74  return static_cast<size_t>(size * 1024 * 1024 * 1024);
75  }
76  }
77 
78  using ::fasmcpp::size_multipliers::operator""_KB;
79 
81  class Assembler
82  {
83  public:
87  Assembler();
97  Assembler(size_t memory, size_t predefinitionsMemory = 10_KB);
98  Assembler(const Assembler&) = delete;
100  Assembler& operator=(const Assembler&) = delete;
101  Assembler& operator=(Assembler&&);
102  ~Assembler();
104  void* getMemory() const;
106  uint32_t getMemorySize() const;
110  char* getPredefinitionsMemory() const;
114  uint32_t getPredefinitionsMemorySize() const;
115 
116  private:
117  void* _assemblerMemory;
118  uint32_t _assemblerMemorySize;
119  char* _predefinitionsMemory = nullptr;
120  uint32_t _predefinitionsMemorySize = 0;
121  };
122 
124  class Assembly
125  {
126  public:
128  Assembly();
130  Assembly(const char* code);
131  Assembly(const Assembly&) = delete;
132  Assembly(Assembly&&);
133  Assembly& operator=(const Assembly&) = delete;
134  Assembly& operator=(Assembly&&);
136  void setAssembler(Assembler* assembler);
138  Assembler* getAssembler() const;
143  bool assemble();
149  int64_t run(bool preserveRegisters = true) const;
151  const char* getErrors() const;
153  const char* getMessages() const;
155  void addError(const char* error, AssemblyKey);
157  void addMessage(const char* message, AssemblyKey);
159  _fasmcpp::AssemblerInput& getInput(AssemblyKey);
161  _fasmcpp::AssemblerOutput& getOutput(AssemblyKey);
169  void addPredefinition(const char* name, const char* value);
177  void addPredefinition(const char* name, const void* value);
179  void clearPredefinitions();
181  const char* getOutput();
183  size_t getOutputSize();
184 
185  private:
186  void allocateExecutableMemory(size_t size);
187  void freeExecutableMemory();
188 
189  Assembler* _assembler;
190  _fasmcpp::AssemblerInput _code;
191  _fasmcpp::AssemblerOutput _bytecode;
192  std::string _errors;
193  std::string _messages;
194  std::unordered_map<std::string, std::string> _predefinitions;
195  bool _predefinitionsChanged = false;
196  void* _executableMemory = nullptr;
197  size_t _executableMemorySize = 0;
198 
199 #ifdef _WIN64
200  const char* rebuildPredefinitionsString();
201 #else
202  std::vector<char> _predefinitionsString;
203 #endif
204  };
205 }
Manages resources for Flat Assembler.
Definition: fasmcpp.h:81
Contains assembly code and its bytecode and predefinitions.
Definition: fasmcpp.h:124
Contains all public symbols.
Definition: fasmcpp.h:12