BlackBone
Windows memory hacking library
 All Classes Functions
AsmStack.hpp
1 #pragma once
2 
3 #pragma warning(disable : 4100)
4 #include "AsmJit/AsmJit.h"
5 #pragma warning(default : 4100)
6 #include "../Include/Macro.h"
7 
8 #include <stdint.h>
9 
10 namespace blackbone
11 {
12 
14 {
15 public:
16  BLACKBONE_API AsmStackAllocator( asmjit::X86Assembler* pAsm, intptr_t baseval = 0x28 )
17  : _pAsm( pAsm )
18  , disp_ofst( sizeof(size_t) )
19  {
20 #ifdef USE64
21  disp_ofst = baseval;
22 #else
23  baseval = 0;
24 #endif
25  }
26 
32  BLACKBONE_API asmjit::Mem AllocVar( intptr_t size )
33  {
34  // Align on word length
35  size = Align( size, sizeof(size_t) );
36 
37 #ifdef USE64
38  auto val = asmjit::Mem( _pAsm->zsp, static_cast<int32_t>(disp_ofst), static_cast<int32_t>(size) );
39 #else
40  auto val = asmjit::Mem( _pAsm->zbp, -disp_ofst - size, size );
41 #endif
42  disp_ofst += size;
43  return val;
44  }
45 
53  BLACKBONE_API bool AllocArray( asmjit::Mem arr[], int count, intptr_t size )
54  {
55  for (int i = 0; i < count; i++)
56  {
57 #ifdef USE64
58  arr[i] = asmjit::Mem( _pAsm->zsp, static_cast<int32_t>(disp_ofst), static_cast<int32_t>(size) );
59 #else
60  arr[i] = asmjit::Mem( _pAsm->zbp, -disp_ofst - size, size );
61 #endif
62  disp_ofst += size;
63  }
64 
65  return true;
66  }
67 
72  BLACKBONE_API inline intptr_t getTotalSize() const { return disp_ofst; };
73 
74 private:
75  asmjit::X86Assembler* _pAsm; // Underlying assembler
76  intptr_t disp_ofst; // Next variable stack offset
77 };
78 
79 //
80 // Helpers
81 //
82 #define ALLOC_STACK_VAR(worker, name, type) asmjit::Mem name( worker.AllocVar( sizeof(type) ) );
83 #define ALLOC_STACK_VAR_S(worker, name, size) asmjit::Mem name( worker.AllocVar( size ) );
84 
85 #define ALLOC_STACK_ARRAY(worker, name, type, count) \
86  asmjit::Mem name[count]; \
87  worker.AllocArray( name, count, sizeof(type) );
88 
89 }
BLACKBONE_API asmjit::Mem AllocVar(intptr_t size)
Allocate stack variable
Definition: AsmStack.hpp:32
Definition: AsmStack.hpp:13
BLACKBONE_API bool AllocArray(asmjit::Mem arr[], int count, intptr_t size)
Allocate array of stack variables
Definition: AsmStack.hpp:53
BLACKBONE_API intptr_t getTotalSize() const
Get total size of all stack variables
Definition: AsmStack.hpp:72
Definition: AsmHelper32.cpp:6