Objects Compiled

This commit is contained in:
IlushaShurupov 2023-07-30 18:30:36 +03:00 committed by Ilya Shurupov
parent 31e420b311
commit edf0548046
52 changed files with 384 additions and 370 deletions

View file

@ -0,0 +1,87 @@
#include "NewPlacement.hpp"
#include "Tokenizer.hpp"
#include "compiler/function.h"
#include "primitives/boolobject.h"
#include "primitives/classobject.h"
#include "primitives/colorobject.h"
#include "primitives/dictobject.h"
#include "primitives/enumobject.h"
#include "primitives/floatobject.h"
#include "primitives/interpreterobject.h"
#include "primitives/intobject.h"
#include "primitives/linkobject.h"
#include "primitives/listobject.h"
#include "primitives/methodobject.h"
#include "primitives/nullobject.h"
#include "primitives/stringobject.h"
#include "primitives/typeobject.h"
using namespace obj;
using namespace tp;
static void defineTypes() {
NDO->define(&DictObject::TypeData);
NDO->define(&IntObject::TypeData);
NDO->define(&LinkObject::TypeData);
NDO->define(&ListObject::TypeData);
NDO->define(&NullObject::TypeData);
NDO->define(&StringObject::TypeData);
NDO->define(&BoolObject::TypeData);
NDO->define(&FloatObject::TypeData);
NDO->define(&EnumObject::TypeData);
NDO->define(&ClassObject::TypeData);
NDO->define(&ColorObject::TypeData);
NDO->define(&InterpreterObject::TypeData);
NDO->define(&TypeObject::TypeData);
}
static void defineGroups() {
NDO->type_groups.addType(&DictObject::TypeData, {"Primitives"});
NDO->type_groups.addType(&IntObject::TypeData, {"Primitives"});
NDO->type_groups.addType(&LinkObject::TypeData, {"Primitives"});
NDO->type_groups.addType(&ListObject::TypeData, {"Primitives"});
NDO->type_groups.addType(&NullObject::TypeData, {"Primitives"});
NDO->type_groups.addType(&StringObject::TypeData, {"Primitives"});
NDO->type_groups.addType(&BoolObject::TypeData, {"Primitives"});
NDO->type_groups.addType(&FloatObject::TypeData, {"Primitives"});
NDO->type_groups.addType(&EnumObject::TypeData, {"Primitives"});
NDO->type_groups.addType(&ClassObject::TypeData, {"Primitives"});
NDO->type_groups.addType(&ColorObject::TypeData, {"Primitives"});
NDO->type_groups.addType(&InterpreterObject::TypeData, { "scripting" });
}
static bool init(const tp::ModuleManifest*) {
if (!NDO) NDO = new objects_api();
obj::BCgen::init();
MethodObject::Initialize();
defineTypes();
defineGroups();
return true;
}
static void deinit(const tp::ModuleManifest*) {
NullObject::uninit();
MethodObject::UnInitialize();
obj::BCgen::deinit();
delete NDO;
}
static tp::ModuleManifest* sModuleDependencies[] = {
// &tp::gModuleCompressor,
&tp::gModuleMath,
&tp::gModuleStrings,
&tp::gModuleTokenizer,
NULL
};
tp::ModuleManifest obj::gModuleObjects = tp::ModuleManifest("Objects", init, deinit, sModuleDependencies);

View file

@ -227,24 +227,4 @@ namespace obj {
}
return NULL;
}
objects_api* objects_init() {
if (!NDO) {
NDO = new objects_api();
}
obj::BCgen::init();
return NDO;
}
void objects_finalize() {
if (NDO) {
delete NDO;
}
obj::BCgen::deinit();
}
};

View file

@ -1,5 +1,6 @@
#include "NewPlacement.hpp"
#include "core/object.h"
#include "HeapAllocatorGlobal.hpp"
@ -30,7 +31,7 @@ namespace obj {
}
memh->down = NULL;
memh->flags = NULL;
memh->flags = 0;
#ifdef OBJECT_REF_COUNT
memh->refc = (tp::alni) 1;
@ -185,7 +186,7 @@ namespace obj {
return objsize_file_recursive_util(self, self->type);
}
void object_recursive_save(Archiver& ndf, Object* self, const ObjectType* type) {
void object_recursive_save(ArchiverOut& ndf, Object* self, const ObjectType* type) {
if (type->base) {
object_recursive_save(ndf, self, type->base);
}
@ -196,48 +197,48 @@ namespace obj {
}
}
tp::alni objects_api::save(Archiver& ndf, Object* in) {
tp::alni objects_api::save(ArchiverOut& ndf, Object* in) {
// if already saved return file_adress
if (NDO_MEMH_FROM_NDO(in)->flags != -1) {
return NDO_MEMH_FROM_NDO(in)->flags;
}
// save write adress for parent save function call
tp::alni tmp_adress = ndf.adress;
tp::alni tmp_adress = ndf.getAddress();
// save requested object to first available adress
tp::alni save_adress = ndf.avl_adress;
tp::alni save_adress = ndf.getFreeAddress();
// save file_adress in memhead
NDO_MEMH_FROM_NDO(in)->flags = save_adress;
// update write adress
ndf.adress = save_adress;
ndf.setAddress(save_adress);
// save file object header
ObjectFileHead ofh = { 0, getrefc(in) };
ndf.write(&ofh);
tp::String(in->type->name).save(&ndf);
ndf << ofh;
ndf << tp::String(in->type->name);
// allocate for object file header
ndf.avl_adress += sizeof(ObjectFileHead) + tp::String::Logic::calcLength(in->type->name) + 1;
ndf.setFreeAddress(ndf.getFreeAddress() + sizeof(ObjectFileHead) + tp::String::Logic::calcLength(in->type->name) + 1);
// calc max size needed for saving all hierarchy of types
tp::alni file_alloc_size = objsize_file_util(in, in->type);
// offes first available adress
ndf.avl_adress += file_alloc_size;
ndf.setFreeAddress(ndf.getFreeAddress() + file_alloc_size);
object_recursive_save(ndf, in, in->type);
// restore adress for parent save function
ndf.adress = tmp_adress;
ndf.setAddress(tmp_adress);
// return addres of saved object in file space
return save_adress;
}
void object_recursive_load(Archiver& ndf, Object* out, const ObjectType* type) {
void object_recursive_load(ArchiverIn& ndf, Object* out, const ObjectType* type) {
if (type->base) {
object_recursive_load(ndf, out, type->base);
}
@ -248,22 +249,23 @@ namespace obj {
}
}
Object* objects_api::load(Archiver& ndf, tp::alni file_adress) {
Object* objects_api::load(ArchiverIn& ndf, tp::alni file_adress) {
// check if already saved
if (((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress) {
return ((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress;
}
// save read adress
tp::alni parent_file_adress = ndf.adress;
// save read address
tp::alni parent_file_adress = ndf.getAddress();
// set read adress
ndf.adress = file_adress;
// set read address
ndf.setAddress(file_adress);
ObjectFileHead ofh;
ndf.read<ObjectFileHead>(&ofh);
ndf >> ofh;
tp::String type_name;
type_name.load(&ndf);
ndf >> type_name;
const ObjectType* object_type = NDO->types.get(type_name);
Object* out = ObjectMemAllocate(object_type);
@ -277,40 +279,40 @@ namespace obj {
// save heap adress in "loaded_file"
((ObjectFileHead*) (loaded_file + file_adress))->load_head_adress = out;
// loads recursivelly
// loads recursively
object_recursive_load(ndf, out, object_type);
// restore read adress for parent call to continue
ndf.adress = parent_file_adress;
// restore read address for parent call to continue
ndf.setAddress(parent_file_adress);
// return heap memory adress
return out;
}
bool objects_api::save(Object* in, tp::String path, bool compressed) {
Archiver ndf(path.read(), Archiver::SAVE);
ArchiverOut ndf(path.read());
if (!ndf.opened) {
if (!ndf.isOpened()) {
return false;
}
clear_object_flags();
// save vesion info
// save version info
ObjectsFileHeader header;
ndf.write(&header);
ndf << header;
ndf.avl_adress = ndf.adress;
ndf.setFreeAddress(ndf.getAddress());
// pre allocate
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
if (sl_callbacks[i]) {
DEBUG_ASSERT(sl_callbacks[i]->size);
ndf.avl_adress += sl_callbacks[i]->size(sl_callbacks[i]->self, ndf);
ndf.setFreeAddress(ndf.getFreeAddress() + sl_callbacks[i]->size(sl_callbacks[i]->self, ndf));
}
}
// presave
// pre-save
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
if (sl_callbacks[i] && sl_callbacks[i]->pre_save) {
sl_callbacks[i]->pre_save(sl_callbacks[i]->self, ndf);
@ -319,15 +321,13 @@ namespace obj {
save(ndf, in);
// postsave
// post-save
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
if (sl_callbacks[i] && sl_callbacks[i]->post_save) {
sl_callbacks[i]->post_save(sl_callbacks[i]->self, ndf);
}
}
ndf.disconnect();
// TODO : add compression
/*
if (compressed) {
@ -354,24 +354,28 @@ namespace obj {
File ndf(temp_file_name.cstr(), tp::osfile_openflags::LOAD);
*/
Archiver ndf(path.read(), Archiver::LOAD);
ArchiverIn ndf(path.read());
if (!ndf.opened) {
if (!ndf.isOpened()) {
return NULL;
}
// check for compability
// check for compatibility
ObjectsFileHeader current_header;
ObjectsFileHeader loaded_header(false);
ndf.read(&loaded_header);
ndf >> loaded_header;
if (!tp::memEqual(&current_header, &loaded_header, sizeof(ObjectsFileHeader))) {
return NULL;
}
ndf.adress = 0;
loaded_file = (tp::int1*) malloc(ndf.size());
ndf.read_bytes(loaded_file, ndf.size());
ndf.adress = sizeof(ObjectsFileHeader);
ndf.setAddress(0);
loaded_file = (tp::int1*) malloc(ndf.getSize());
ndf.readBytes(loaded_file, ndf.getSize());
ndf.setAddress(sizeof(ObjectsFileHeader));
// preload
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {
@ -380,7 +384,7 @@ namespace obj {
}
}
Object* out = load(ndf, ndf.adress);
Object* out = load(ndf, ndf.getAddress());
// post
for (tp::alni i = 0; i < SAVE_LOAD_MAX_CALLBACK_SLOTS; i++) {

View file

@ -74,14 +74,14 @@ tp::alni ScriptSection::get_script_file_adress(Script* in) {
return get_script_head_store_adress(in);
}
tp::alni ScriptSection::save_script_table_to_file_size(ScriptSection* self, Archiver& file) {
tp::alni ScriptSection::save_script_table_to_file_size(ScriptSection* self, ArchiverOut& file) {
tp::alni size = 0;
size += 5; // header
size += sizeof(tp::alni); // scripts length
for (auto iter : self->mScripts) {
set_script_head_store_adress(iter.data(), file.adress + size);
set_script_head_store_adress(iter.data(), file.getAddress() + size);
size += sizeof(tp::alni); // scripts string obj ref
size += sizeof(tp::alni); // constants length
@ -91,7 +91,8 @@ tp::alni ScriptSection::save_script_table_to_file_size(ScriptSection* self, Arch
}
// instructions
size += iter->mBytecode.mInstructions.saveSize();
ASSERT(false)
// size += iter->mBytecode.mInstructions.saveSize();
}
size += sizeof(tp::alni); // objects mem offset
@ -99,124 +100,126 @@ tp::alni ScriptSection::save_script_table_to_file_size(ScriptSection* self, Arch
return size;
}
void ScriptSection::save_script_table_to_file(ScriptSection* self, Archiver& file) {
void ScriptSection::save_script_table_to_file(ScriptSection* self, ArchiverOut& file) {
// header
file.write_bytes("/scr/", 5);
file.writeBytes("/scr/", 5);
// scripts length
tp::alni scripts_count = self->mScripts.length();
file.write<tp::alni>(&scripts_count);
file << scripts_count;
for (auto iter : self->mScripts) {
// scripts string obj ref
auto obj_addres = obj::NDO->save(file, iter->mReadable);
file.write(&obj_addres);
file << obj_addres;
// constants length
tp::alni consts_count = iter->mBytecode.mConstants.size();
file.write<tp::alni>(&consts_count);
file << consts_count;
for (auto const_obj : iter->mBytecode.mConstants) {
// constant object addres
auto obj_addres = obj::NDO->save(file, const_obj.data());
file.write(&obj_addres);
file << obj_addres;
}
// mInstructions
iter->mBytecode.mInstructions.save(file);
ASSERT(false)
file << iter->mBytecode.mInstructions;
}
// header
file.write_bytes("/scr/", 5);
file.writeBytes("/scr/", 5);
tp::alni objects_mem_offset = file.avl_adress;
file.write<tp::alni>(&objects_mem_offset);
tp::alni objects_mem_offset = file.getFreeAddress();
file << objects_mem_offset;
}
void load_constants(ScriptSection* self, Archiver& file, tp::alni start_addr) {
auto addres = file.adress;
void load_constants(ScriptSection* self, ArchiverIn& file, tp::alni start_addr) {
auto addres = file.getAddress();
file.adress = start_addr;
file.adress += 5; // header
file.setAddress(start_addr);
file.setAddress( start_addr + 5); // header
tp::alni scripts_count;
file.read<tp::alni>(&scripts_count);
file >> scripts_count;
for (tp::alni i = 0; i < scripts_count; i++) {
auto script = self->get_scritp_from_file_adress(file.adress);
auto script = self->get_scritp_from_file_adress(file.getAddress());
// script text
tp::alni str_addr;
file.read<tp::alni>(&str_addr);
file >> str_addr;
script->mReadable = NDO_CAST(obj::StringObject, obj::NDO->load(file, str_addr));
file.adress += sizeof(tp::alni); // constants length
file.setAddress(file.getAddress() + sizeof(tp::alni)); // constants length
for (auto const_obj : script->mBytecode.mConstants) {
tp::alni consts_addr;
file.read<tp::alni>(&consts_addr);
file >> consts_addr;
const_obj.data() = obj::NDO->load(file, consts_addr);
}
// instructions
file.adress += script->mBytecode.mInstructions.saveSize();
ASSERT(false)
//file.setAddress(file.getAddress() + script->mBytecode.mInstructions.saveSize());
}
file.adress = addres;
file.setAddress(addres);
}
void ScriptSection::load_script_table_from_file(ScriptSection* self, Archiver& file) {
void ScriptSection::load_script_table_from_file(ScriptSection* self, ArchiverIn& file) {
for (auto iter : self->mScripts) {
set_script_head_store_adress(iter.data(), -1);
}
auto section_start_addr = file.adress;
auto section_start_addr = file.getAddress();
// header
file.adress += 5;
file.setAddress(file.getAddress() + 5);
// scripts length
tp::alni scripts_count;
file.read<tp::alni>(&scripts_count);
file >> scripts_count;
for (tp::alni i = 0; i < scripts_count; i++) {
auto new_script = self->createScript();
set_script_head_store_adress(new_script, file.adress);
set_script_head_store_adress(new_script, file.getAddress());
// scripts text
file.adress += sizeof(tp::alni);
file.setAddress(file.getAddress() + sizeof(tp::alni));
// constants length
tp::alni consts_count;
file.read<tp::alni>(&consts_count);
file >> consts_count;
new_script->mBytecode.mConstants.reserve(consts_count);
for (tp::alni j = 0; j < consts_count; j++) {
// constant object addres
tp::alni consts_addr;
file.read<tp::alni>(&consts_addr);
file >> consts_addr;
// skiping
//new_script->mBytecode.mConstants[j] = obj::NDO->load(file, consts_addr);
}
// mInstructions
new_script->mBytecode.mInstructions.load(file);
file >> new_script->mBytecode.mInstructions;
}
load_constants(self, file, section_start_addr);
// header
file.adress += 5;
file.setAddress(file.getAddress() + 5);
tp::alni objects_mem_offset;
file.read<tp::alni>(&objects_mem_offset);
file >> objects_mem_offset;
file.adress = objects_mem_offset;
file.setAddress(objects_mem_offset);
}
Script* ScriptSection::get_scritp_from_file_adress(tp::alni file_adress) {

View file

@ -31,7 +31,7 @@ void obj::TypeGroups::setType(ObjectType* type) {
this->type = type;
}
void obj::TypeGroups::addType(ObjectType* type, tp::init_list<const char*> path, tp::alni cur_dir_idx) {
void obj::TypeGroups::addType(ObjectType* type, tp::InitialierList<const char*> path, tp::alni cur_dir_idx) {
DEBUG_ASSERT(is_group);
tp::alni dir_len = (tp::alni) path.size();

View file

@ -61,7 +61,7 @@ tp::int2 TypeMethods::presents(tp::String id) const {
return -1;
}
TypeMethods::LookupKey TypeMethods::presents(const ObjectType* type, tp::string id) {
TypeMethods::LookupKey TypeMethods::presents(const ObjectType* type, tp::String id) {
tp::int2 depth = 0;
tp::int2 idx = 0;