clean up object module

This commit is contained in:
IlyaShurupov 2024-03-23 12:32:57 +03:00 committed by Ilya Shurupov
parent e78885d452
commit f8c82b7e29
68 changed files with 1223 additions and 1229 deletions

View file

@ -4,6 +4,7 @@
#include "interpreter/ByteCode.hpp"
#include "primitives/MethodObject.hpp"
using namespace tp;
using namespace obj;
void CallStack::enter(const CallStack::CallFrame& frame) {
@ -14,7 +15,7 @@ void CallStack::enter(const CallStack::CallFrame& frame) {
frame.mMethod->mScript->mBytecode.mArgumentsLoaded = 0;
frame.mMethod->mScript->mBytecode.mInstructionIdx = 0;
obj::NDO->refinc(frame.mMethod);
obj::NDO->increaseReferenceCount(frame.mMethod);
mStack.append(frame);
}
@ -31,4 +32,4 @@ void CallStack::leave() {
ByteCode* CallStack::getBytecode() { return &mStack.last().mMethod->mScript->mBytecode; }
tp::halni CallStack::len() const { return mStack.size(); }
halni CallStack::len() const { return mStack.size(); }

View file

@ -5,20 +5,21 @@
#include "Timing.hpp"
using namespace tp;
using namespace obj;
inline tp::uint1 read_byte(ByteCode* bytecode) {
auto out = (tp::uint1) bytecode->mInstructions[bytecode->mInstructionIdx];
inline uint1 read_byte(ByteCode* bytecode) {
auto out = (uint1) bytecode->mInstructions[bytecode->mInstructionIdx];
bytecode->mInstructionIdx++;
return out;
}
// exeption for opcodes
// reads 2 bytes from instructions in order to load constant onto operands stack
tp::uint2 loadConstDataIdx(ByteCode* bytecode) {
tp::uint2 out = 0;
out |= ((tp::uint2) read_byte(bytecode));
out |= ((tp::uint2) read_byte(bytecode) << 8);
uint2 loadConstDataIdx(ByteCode* bytecode) {
uint2 out = 0;
out |= ((uint2) read_byte(bytecode));
out |= ((uint2) read_byte(bytecode) << 8);
return out;
}
@ -26,9 +27,9 @@ tp::uint2 loadConstDataIdx(ByteCode* bytecode) {
// reads 2 bytes from instructions in order to load constant onto operands stack
void skip_param(ByteCode* bytecode) { bytecode->mInstructionIdx += 2; }
tp::uint2 param(ByteCode* bytecode) { return loadConstDataIdx(bytecode); }
uint2 param(ByteCode* bytecode) { return loadConstDataIdx(bytecode); }
void Interpreter::exec(obj::MethodObject* method, obj::ClassObject* self, obj::DictObject* globals, tp::InitialierList<GlobalDef> globals2) {
void Interpreter::exec(obj::MethodObject* method, obj::ClassObject* self, obj::DictObject* globals, InitialierList<GlobalDef> globals2) {
if (!method->mScript->mBytecode.mInstructions.size()) {
return;
}
@ -52,7 +53,7 @@ void Interpreter::exec(obj::MethodObject* method, obj::ClassObject* self, obj::D
bool Interpreter::finished() { return !mCallStack.len(); }
void Interpreter::stepBytecode() {
tp::halni call_depth = mCallStack.len();
halni call_depth = mCallStack.len();
do {
stepBytecodeIn();
if (finished()) {
@ -66,7 +67,7 @@ void Interpreter::stepBytecodeOut() {
return;
}
tp::halni call_depth = mCallStack.len();
halni call_depth = mCallStack.len();
if (!call_depth) {
return;
}
@ -87,7 +88,7 @@ void Interpreter::stepBytecodeIn() {
auto bytecode = mCallStack.getBytecode();
if (bytecode->mInstructionIdx >= (tp::ualni) bytecode->mInstructions.size()) {
if (bytecode->mInstructionIdx >= (ualni) bytecode->mInstructions.size()) {
// just return
if (mScopeStack.mIdx) {
mScopeStack.leaveScope();
@ -114,14 +115,14 @@ void Interpreter::stepBytecodeIn() {
case OpCode::HALT:
{
while (true) {
tp::sleep(3);
sleep(3);
}
break;
}
case OpCode::TERMINATE:
{
// tp::terminate(0);
// terminate(0);
}
case OpCode::IGNORE:
@ -242,7 +243,7 @@ void Interpreter::stepBytecodeIn() {
auto id = mOperandsStack.getOperand<StringObject>();
auto obj = mOperandsStack.getOperand();
mScopeStack.addLocal(obj, id->val);
NDO->refinc(obj);
NDO->increaseReferenceCount(obj);
// mScopeStack.popTemp();
break;
}
@ -270,7 +271,7 @@ void Interpreter::stepBytecodeIn() {
// class is a function - execute it as a constructor
// PUSH_ARGS protocol
tp::uint2 len = 0;
uint2 len = 0;
mOperandsStack.push((Object*) len);
mOperandsStack.push(nullptr);
@ -318,7 +319,7 @@ void Interpreter::stepBytecodeIn() {
// +4) object2
// ...
tp::uint2 len = read_byte(bytecode);
uint2 len = read_byte(bytecode);
mOperandsStack.push((Object*) len);
mOperandsStack.push(nullptr);
break;
@ -326,11 +327,11 @@ void Interpreter::stepBytecodeIn() {
case OpCode::SAVE_ARGS:
{
tp::uint2 args_len = read_byte(bytecode);
uint2 args_len = read_byte(bytecode);
auto argument = mOperandsStack.getOperand();
while (argument) {
NDO->refinc(argument);
NDO->increaseReferenceCount(argument);
auto argument_id = bytecode->mConstants[loadConstDataIdx(bytecode)];
NDO_CASTV(StringObject, argument_id, id);
@ -340,7 +341,7 @@ void Interpreter::stepBytecodeIn() {
argument = mOperandsStack.getOperand();
}
tp::uint2 saved_len = tp::uint2(tp::ualni(mOperandsStack.getOperand()));
uint2 saved_len = uint2(ualni(mOperandsStack.getOperand()));
ASSERT(args_len == saved_len && "invalid number of arguments passefd");
break;
@ -388,7 +389,7 @@ void Interpreter::stepBytecodeIn() {
ASSERT(left->type == right->type && "addition of different types is not implemented");
ASSERT(left->type->ariphmetics && left->type->ariphmetics->add && "cannot add object of this type");
auto res = NDO->instatiate(left);
auto res = NDO->instantiate(left);
res->type->ariphmetics->add(res, right);
mScopeStack.addTemp(res);
@ -403,7 +404,7 @@ void Interpreter::stepBytecodeIn() {
ASSERT(left->type == right->type && "addition of different types is not implemented");
ASSERT(left->type->ariphmetics && left->type->ariphmetics->add && "cannot add object of this type");
auto res = NDO->instatiate(left);
auto res = NDO->instantiate(left);
res->type->ariphmetics->sub(res, right);
mScopeStack.addTemp(res);
@ -418,7 +419,7 @@ void Interpreter::stepBytecodeIn() {
ASSERT(left->type == right->type && "addition of different types is not implemented");
ASSERT(left->type->ariphmetics && left->type->ariphmetics->add && "cannot add object of this type");
auto res = NDO->instatiate(left);
auto res = NDO->instantiate(left);
res->type->ariphmetics->mul(res, right);
mScopeStack.addTemp(res);
@ -433,7 +434,7 @@ void Interpreter::stepBytecodeIn() {
ASSERT(left->type == right->type && "addition of different types is not implemented");
ASSERT(left->type->ariphmetics && left->type->ariphmetics->add && "cannot add object of this type");
auto res = NDO->instatiate(left);
auto res = NDO->instantiate(left);
res->type->ariphmetics->div(res, right);
mScopeStack.addTemp(res);
@ -593,7 +594,7 @@ void Interpreter::stepBytecodeIn() {
}
}
void Interpreter::execAll(obj::MethodObject* method, obj::ClassObject* self, obj::DictObject* globals, tp::InitialierList<GlobalDef> globals2) {
void Interpreter::execAll(obj::MethodObject* method, obj::ClassObject* self, obj::DictObject* globals, InitialierList<GlobalDef> globals2) {
if (!method->mScript->mBytecode.mInstructions.size()) {
return;
}

View file

@ -1,6 +1,7 @@
#include "interpreter/OperandsStack.hpp"
using namespace tp;
using namespace obj;
OperandStack::OperandStack() {

View file

@ -3,18 +3,17 @@
#include "interpreter/OperatoinCodes.hpp"
namespace obj {
OpcodeInfos gOpcodeInfos;
};
using namespace tp;
using namespace obj;
OpcodeInfos tp::obj::gOpcodeInfos;
#define CONST_IDX_BYTES 2
#define OP(opcode, name, desc, ops, params) add(opcode, { #name, #desc, ops, params });
OpcodeInfos::OperandsInfo::OperandsInfo() {}
OpcodeInfos::OperandsInfo::OperandsInfo(tp::InitialierList<Operand> list) {
OpcodeInfos::OperandsInfo::OperandsInfo(InitialierList<Operand> list) {
DEBUG_ASSERT(MAX_OPERANDS >= list.size());
for (auto item : list) {
buff[len] = item;
@ -23,7 +22,7 @@ OpcodeInfos::OperandsInfo::OperandsInfo(tp::InitialierList<Operand> list) {
}
OpcodeInfos::ParamsInfo::ParamsInfo() {}
OpcodeInfos::ParamsInfo::ParamsInfo(tp::InitialierList<Param> list) {
OpcodeInfos::ParamsInfo::ParamsInfo(InitialierList<Param> list) {
DEBUG_ASSERT(MAX_PARAMS >= list.size());
for (auto item : list) {
buff[len] = item;
@ -31,8 +30,8 @@ OpcodeInfos::ParamsInfo::ParamsInfo(tp::InitialierList<Param> list) {
}
}
tp::uint1 OpcodeInfos::OpInfo::opsize() {
tp::uint1 out = 1;
uint1 OpcodeInfos::OpInfo::opsize() {
uint1 out = 1;
for (auto i = 0; i < params.len; i++) {
out += params.buff[i].bytes;
}
@ -364,8 +363,8 @@ OpcodeInfos::OpcodeInfos() {
}
OpcodeInfos::OpInfo OpcodeInfos::fetch(OpCode code) {
DEBUG_ASSERT((tp::alni) code >= 0 && (tp::alni) code < (tp::alni) OpCode::END_OPCODES);
return buff[(tp::alni) code];
DEBUG_ASSERT((alni) code >= 0 && (alni) code < (alni) OpCode::END_OPCODES);
return buff[(alni) code];
}
void OpcodeInfos::add(OpCode code, const OpInfo& info) { buff[(tp::alni) code] = info; }
void OpcodeInfos::add(OpCode code, const OpInfo& info) { buff[(alni) code] = info; }

View file

@ -1,6 +1,7 @@
#include "interpreter/ScopeStack.hpp"
using namespace tp;
using namespace obj;
Scope::~Scope() {
@ -46,7 +47,7 @@ void ScopeStack::leaveScope() {
}
void ScopeStack::addTemp(obj::Object* tmp) {
obj::NDO->refinc(tmp);
obj::NDO->increaseReferenceCount(tmp);
mBuff[mIdx - 1].mTemps.pushBack(tmp);
}
@ -57,7 +58,7 @@ void ScopeStack::popTemp() {
void ScopeStack::addTempReturn(obj::Object* ret) {
if (mIdx >= 2) {
obj::NDO->refinc(ret);
obj::NDO->increaseReferenceCount(ret);
mBuff[mIdx - 2].mTemps.pushBack(ret);
}
}
@ -69,7 +70,7 @@ void ScopeStack::addLocal(obj::Object* local, const std::string& id) {
if (idx) {
obj::NDO->destroy(locals.getSlotVal(idx));
}
obj::NDO->refinc(local);
obj::NDO->increaseReferenceCount(local);
locals.put(id, local);
}