Add simple VM with stack storage
This commit is contained in:
parent
c2985a6bb1
commit
8d97cda751
37
src/server/as/vm.cpp
Normal file
37
src/server/as/vm.cpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
PolyGun
|
||||||
|
|
||||||
|
Copyright (c) 2024 mrkubax10 <mrkubax10@onet.pl>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "server/as/vm.hpp"
|
||||||
|
|
||||||
|
using namespace polygun::server::as;
|
||||||
|
|
||||||
|
VM::VM(unsigned storage_size) :
|
||||||
|
m_storage_size(storage_size),
|
||||||
|
m_stack(new uint8_t[storage_size]),
|
||||||
|
m_stack_ptr(0)
|
||||||
|
{}
|
||||||
|
|
||||||
|
VM::~VM() {
|
||||||
|
delete[] m_stack;
|
||||||
|
}
|
75
src/server/as/vm.hpp
Normal file
75
src/server/as/vm.hpp
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
PolyGun
|
||||||
|
|
||||||
|
Copyright (c) 2024 mrkubax10 <mrkubax10@onet.pl>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef POLYGUN_SERVER_AS_VM_HPP
|
||||||
|
#define POLYGUN_SERVER_AS_VM_HPP
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace polygun::server::as {
|
||||||
|
class VM final {
|
||||||
|
public:
|
||||||
|
enum Location {
|
||||||
|
LOCATION_STACK,
|
||||||
|
LOCATION_STATIC
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
VM(unsigned storage_size);
|
||||||
|
~VM();
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void set_value_at(Location location, unsigned addr, T value) {
|
||||||
|
assert(addr<m_storage_size);
|
||||||
|
switch(location) {
|
||||||
|
case Location::LOCATION_STACK:
|
||||||
|
*reinterpret_cast<T*>(&m_stack[addr]) = value;
|
||||||
|
break;
|
||||||
|
case Location::LOCATION_STATIC:
|
||||||
|
// TODO
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
T get_value_at(Location location, unsigned addr) {
|
||||||
|
assert(addr<m_storage_size);
|
||||||
|
switch(location) {
|
||||||
|
case Location::LOCATION_STACK:
|
||||||
|
return *&m_stack[addr];
|
||||||
|
case Location::LOCATION_STATIC:
|
||||||
|
// TODO
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unsigned get_stack_ptr() const { return m_stack_ptr; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
unsigned m_storage_size;
|
||||||
|
uint8_t* m_stack;
|
||||||
|
unsigned m_stack_ptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // POLYGUN_SERVER_AS_VM_HPP
|
Loading…
x
Reference in New Issue
Block a user